]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
PR c++/81866 - ICE with member template and default targ.
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
54 {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) tree saved_access_scope;
71
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr;
76
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
79 //
80 // Implementation of the RAII helper for creating new local
81 // specializations.
82 local_specialization_stack::local_specialization_stack (lss_policy policy)
83 : saved (local_specializations)
84 {
85 if (policy == lss_blank || !saved)
86 local_specializations = new hash_map<tree, tree>;
87 else
88 local_specializations = new hash_map<tree, tree>(*saved);
89 }
90
91 local_specialization_stack::~local_specialization_stack ()
92 {
93 delete local_specializations;
94 local_specializations = saved;
95 }
96
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth;
99
100 struct GTY((for_user)) spec_entry
101 {
102 tree tmpl;
103 tree args;
104 tree spec;
105 };
106
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 {
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
111 };
112
113 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114
115 static GTY (()) hash_table<spec_hasher> *type_specializations;
116
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131
132 enum template_base_result {
133 tbr_incomplete_type,
134 tbr_ambiguous_baseclass,
135 tbr_success
136 };
137
138 static void push_access_scope (tree);
139 static void pop_access_scope (tree);
140 static bool resolve_overloaded_unification (tree, tree, tree, tree,
141 unification_kind_t, int,
142 bool);
143 static int try_one_overload (tree, tree, tree, tree, tree,
144 unification_kind_t, int, bool, bool);
145 static int unify (tree, tree, tree, tree, int, bool);
146 static void add_pending_template (tree);
147 static tree reopen_tinst_level (struct tinst_level *);
148 static tree tsubst_initializer_list (tree, tree);
149 static tree get_partial_spec_bindings (tree, tree, tree);
150 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
153 bool, bool);
154 static void tsubst_enum (tree, tree, tree);
155 static tree add_to_template_args (tree, tree);
156 static tree add_outermost_template_args (tree, tree);
157 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
158 static int check_non_deducible_conversion (tree, tree, int, int,
159 struct conversion **, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
161 tree);
162 static int type_unification_real (tree, tree, tree, const tree *,
163 unsigned int, int, unification_kind_t,
164 vec<deferred_access_check, va_gc> **,
165 bool);
166 static void note_template_header (int);
167 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
168 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
169 static tree convert_template_argument (tree, tree, tree,
170 tsubst_flags_t, int, tree);
171 static tree for_each_template_parm (tree, tree_fn_t, void*,
172 hash_set<tree> *, bool, tree_fn_t = NULL);
173 static tree expand_template_argument_pack (tree);
174 static tree build_template_parm_index (int, int, int, tree, tree);
175 static bool inline_needs_template_parms (tree, bool);
176 static void push_inline_template_parms_recursive (tree, int);
177 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
178 static int mark_template_parm (tree, void *);
179 static int template_parm_this_level_p (tree, void *);
180 static tree tsubst_friend_function (tree, tree);
181 static tree tsubst_friend_class (tree, tree);
182 static int can_complete_type_without_circularity (tree);
183 static tree get_bindings (tree, tree, tree, bool);
184 static int template_decl_level (tree);
185 static int check_cv_quals_for_unify (int, tree, tree);
186 static void template_parm_level_and_index (tree, int*, int*);
187 static int unify_pack_expansion (tree, tree, tree,
188 tree, unification_kind_t, bool, bool);
189 static tree copy_template_args (tree);
190 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
193 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
194 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
195 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
196 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
197 static bool check_specialization_scope (void);
198 static tree process_partial_specialization (tree);
199 static void set_current_access_from_decl (tree);
200 static enum template_base_result get_template_base (tree, tree, tree, tree,
201 bool , tree *);
202 static tree try_class_unification (tree, tree, tree, tree, bool);
203 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
204 tree, tree);
205 static bool template_template_parm_bindings_ok_p (tree, tree);
206 static void tsubst_default_arguments (tree, tsubst_flags_t);
207 static tree for_each_template_parm_r (tree *, int *, void *);
208 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
209 static void copy_default_args_to_explicit_spec (tree);
210 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
211 static bool dependent_template_arg_p (tree);
212 static bool any_template_arguments_need_structural_equality_p (tree);
213 static bool dependent_type_p_r (tree);
214 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
215 static tree tsubst_decl (tree, tree, tsubst_flags_t);
216 static void perform_typedefs_access_check (tree tmpl, tree targs);
217 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
218 location_t);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
225 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
226 static tree make_argument_pack (tree);
227 static void register_parameter_specializations (tree, tree);
228 static tree enclosing_instantiation_of (tree tctx);
229
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
234
235 static void
236 push_access_scope (tree t)
237 {
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
239 || TREE_CODE (t) == TYPE_DECL);
240
241 if (DECL_FRIEND_CONTEXT (t))
242 push_nested_class (DECL_FRIEND_CONTEXT (t));
243 else if (DECL_CLASS_SCOPE_P (t))
244 push_nested_class (DECL_CONTEXT (t));
245 else
246 push_to_top_level ();
247
248 if (TREE_CODE (t) == FUNCTION_DECL)
249 {
250 saved_access_scope = tree_cons
251 (NULL_TREE, current_function_decl, saved_access_scope);
252 current_function_decl = t;
253 }
254 }
255
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
258
259 static void
260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 {
264 current_function_decl = TREE_VALUE (saved_access_scope);
265 saved_access_scope = TREE_CHAIN (saved_access_scope);
266 }
267
268 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
269 pop_nested_class ();
270 else
271 pop_from_top_level ();
272 }
273
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
278
279 tree
280 finish_member_template_decl (tree decl)
281 {
282 if (decl == error_mark_node)
283 return error_mark_node;
284
285 gcc_assert (DECL_P (decl));
286
287 if (TREE_CODE (decl) == TYPE_DECL)
288 {
289 tree type;
290
291 type = TREE_TYPE (decl);
292 if (type == error_mark_node)
293 return error_mark_node;
294 if (MAYBE_CLASS_TYPE_P (type)
295 && CLASSTYPE_TEMPLATE_INFO (type)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
297 {
298 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
299 check_member_template (tmpl);
300 return tmpl;
301 }
302 return NULL_TREE;
303 }
304 else if (TREE_CODE (decl) == FIELD_DECL)
305 error ("data member %qD cannot be a member template", decl);
306 else if (DECL_TEMPLATE_INFO (decl))
307 {
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
309 {
310 check_member_template (DECL_TI_TEMPLATE (decl));
311 return DECL_TI_TEMPLATE (decl);
312 }
313 else
314 return decl;
315 }
316 else
317 error ("invalid member template declaration %qD", decl);
318
319 return error_mark_node;
320 }
321
322 /* Create a template info node. */
323
324 tree
325 build_template_info (tree template_decl, tree template_args)
326 {
327 tree result = make_node (TEMPLATE_INFO);
328 TI_TEMPLATE (result) = template_decl;
329 TI_ARGS (result) = template_args;
330 return result;
331 }
332
333 /* Return the template info node corresponding to T, whatever T is. */
334
335 tree
336 get_template_info (const_tree t)
337 {
338 tree tinfo = NULL_TREE;
339
340 if (!t || t == error_mark_node)
341 return NULL;
342
343 if (TREE_CODE (t) == NAMESPACE_DECL
344 || TREE_CODE (t) == PARM_DECL)
345 return NULL;
346
347 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
348 tinfo = DECL_TEMPLATE_INFO (t);
349
350 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
351 t = TREE_TYPE (t);
352
353 if (OVERLOAD_TYPE_P (t))
354 tinfo = TYPE_TEMPLATE_INFO (t);
355 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
356 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
357
358 return tinfo;
359 }
360
361 /* Returns the template nesting level of the indicated class TYPE.
362
363 For example, in:
364 template <class T>
365 struct A
366 {
367 template <class U>
368 struct B {};
369 };
370
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
374
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
377 always safe. */
378
379 int
380 template_class_depth (tree type)
381 {
382 int depth;
383
384 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
385 {
386 tree tinfo = get_template_info (type);
387
388 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
390 ++depth;
391
392 if (DECL_P (type))
393 type = CP_DECL_CONTEXT (type);
394 else if (LAMBDA_TYPE_P (type))
395 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
396 else
397 type = CP_TYPE_CONTEXT (type);
398 }
399
400 return depth;
401 }
402
403 /* Return TRUE if NODE instantiates a template that has arguments of
404 its own, be it directly a primary template or indirectly through a
405 partial specializations. */
406 static bool
407 instantiates_primary_template_p (tree node)
408 {
409 tree tinfo = get_template_info (node);
410 if (!tinfo)
411 return false;
412
413 tree tmpl = TI_TEMPLATE (tinfo);
414 if (PRIMARY_TEMPLATE_P (tmpl))
415 return true;
416
417 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
418 return false;
419
420 /* So now we know we have a specialization, but it could be a full
421 or a partial specialization. To tell which, compare the depth of
422 its template arguments with those of its context. */
423
424 tree ctxt = DECL_CONTEXT (tmpl);
425 tree ctinfo = get_template_info (ctxt);
426 if (!ctinfo)
427 return true;
428
429 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
430 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
431 }
432
433 /* Subroutine of maybe_begin_member_template_processing.
434 Returns true if processing DECL needs us to push template parms. */
435
436 static bool
437 inline_needs_template_parms (tree decl, bool nsdmi)
438 {
439 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
440 return false;
441
442 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
443 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
444 }
445
446 /* Subroutine of maybe_begin_member_template_processing.
447 Push the template parms in PARMS, starting from LEVELS steps into the
448 chain, and ending at the beginning, since template parms are listed
449 innermost first. */
450
451 static void
452 push_inline_template_parms_recursive (tree parmlist, int levels)
453 {
454 tree parms = TREE_VALUE (parmlist);
455 int i;
456
457 if (levels > 1)
458 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
459
460 ++processing_template_decl;
461 current_template_parms
462 = tree_cons (size_int (processing_template_decl),
463 parms, current_template_parms);
464 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
465
466 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
467 NULL);
468 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
469 {
470 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
471
472 if (error_operand_p (parm))
473 continue;
474
475 gcc_assert (DECL_P (parm));
476
477 switch (TREE_CODE (parm))
478 {
479 case TYPE_DECL:
480 case TEMPLATE_DECL:
481 pushdecl (parm);
482 break;
483
484 case PARM_DECL:
485 /* Push the CONST_DECL. */
486 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
487 break;
488
489 default:
490 gcc_unreachable ();
491 }
492 }
493 }
494
495 /* Restore the template parameter context for a member template, a
496 friend template defined in a class definition, or a non-template
497 member of template class. */
498
499 void
500 maybe_begin_member_template_processing (tree decl)
501 {
502 tree parms;
503 int levels = 0;
504 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
505
506 if (nsdmi)
507 {
508 tree ctx = DECL_CONTEXT (decl);
509 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
510 /* Disregard full specializations (c++/60999). */
511 && uses_template_parms (ctx)
512 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
513 }
514
515 if (inline_needs_template_parms (decl, nsdmi))
516 {
517 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
518 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
519
520 if (DECL_TEMPLATE_SPECIALIZATION (decl))
521 {
522 --levels;
523 parms = TREE_CHAIN (parms);
524 }
525
526 push_inline_template_parms_recursive (parms, levels);
527 }
528
529 /* Remember how many levels of template parameters we pushed so that
530 we can pop them later. */
531 inline_parm_levels.safe_push (levels);
532 }
533
534 /* Undo the effects of maybe_begin_member_template_processing. */
535
536 void
537 maybe_end_member_template_processing (void)
538 {
539 int i;
540 int last;
541
542 if (inline_parm_levels.length () == 0)
543 return;
544
545 last = inline_parm_levels.pop ();
546 for (i = 0; i < last; ++i)
547 {
548 --processing_template_decl;
549 current_template_parms = TREE_CHAIN (current_template_parms);
550 poplevel (0, 0, 0);
551 }
552 }
553
554 /* Return a new template argument vector which contains all of ARGS,
555 but has as its innermost set of arguments the EXTRA_ARGS. */
556
557 static tree
558 add_to_template_args (tree args, tree extra_args)
559 {
560 tree new_args;
561 int extra_depth;
562 int i;
563 int j;
564
565 if (args == NULL_TREE || extra_args == error_mark_node)
566 return extra_args;
567
568 extra_depth = TMPL_ARGS_DEPTH (extra_args);
569 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
570
571 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
572 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
573
574 for (j = 1; j <= extra_depth; ++j, ++i)
575 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
576
577 return new_args;
578 }
579
580 /* Like add_to_template_args, but only the outermost ARGS are added to
581 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
582 (EXTRA_ARGS) levels are added. This function is used to combine
583 the template arguments from a partial instantiation with the
584 template arguments used to attain the full instantiation from the
585 partial instantiation. */
586
587 static tree
588 add_outermost_template_args (tree args, tree extra_args)
589 {
590 tree new_args;
591
592 /* If there are more levels of EXTRA_ARGS than there are ARGS,
593 something very fishy is going on. */
594 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
595
596 /* If *all* the new arguments will be the EXTRA_ARGS, just return
597 them. */
598 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
599 return extra_args;
600
601 /* For the moment, we make ARGS look like it contains fewer levels. */
602 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
603
604 new_args = add_to_template_args (args, extra_args);
605
606 /* Now, we restore ARGS to its full dimensions. */
607 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
608
609 return new_args;
610 }
611
612 /* Return the N levels of innermost template arguments from the ARGS. */
613
614 tree
615 get_innermost_template_args (tree args, int n)
616 {
617 tree new_args;
618 int extra_levels;
619 int i;
620
621 gcc_assert (n >= 0);
622
623 /* If N is 1, just return the innermost set of template arguments. */
624 if (n == 1)
625 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
626
627 /* If we're not removing anything, just return the arguments we were
628 given. */
629 extra_levels = TMPL_ARGS_DEPTH (args) - n;
630 gcc_assert (extra_levels >= 0);
631 if (extra_levels == 0)
632 return args;
633
634 /* Make a new set of arguments, not containing the outer arguments. */
635 new_args = make_tree_vec (n);
636 for (i = 1; i <= n; ++i)
637 SET_TMPL_ARGS_LEVEL (new_args, i,
638 TMPL_ARGS_LEVEL (args, i + extra_levels));
639
640 return new_args;
641 }
642
643 /* The inverse of get_innermost_template_args: Return all but the innermost
644 EXTRA_LEVELS levels of template arguments from the ARGS. */
645
646 static tree
647 strip_innermost_template_args (tree args, int extra_levels)
648 {
649 tree new_args;
650 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
651 int i;
652
653 gcc_assert (n >= 0);
654
655 /* If N is 1, just return the outermost set of template arguments. */
656 if (n == 1)
657 return TMPL_ARGS_LEVEL (args, 1);
658
659 /* If we're not removing anything, just return the arguments we were
660 given. */
661 gcc_assert (extra_levels >= 0);
662 if (extra_levels == 0)
663 return args;
664
665 /* Make a new set of arguments, not containing the inner arguments. */
666 new_args = make_tree_vec (n);
667 for (i = 1; i <= n; ++i)
668 SET_TMPL_ARGS_LEVEL (new_args, i,
669 TMPL_ARGS_LEVEL (args, i));
670
671 return new_args;
672 }
673
674 /* We've got a template header coming up; push to a new level for storing
675 the parms. */
676
677 void
678 begin_template_parm_list (void)
679 {
680 /* We use a non-tag-transparent scope here, which causes pushtag to
681 put tags in this scope, rather than in the enclosing class or
682 namespace scope. This is the right thing, since we want
683 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
684 global template class, push_template_decl handles putting the
685 TEMPLATE_DECL into top-level scope. For a nested template class,
686 e.g.:
687
688 template <class T> struct S1 {
689 template <class T> struct S2 {};
690 };
691
692 pushtag contains special code to insert the TEMPLATE_DECL for S2
693 at the right scope. */
694 begin_scope (sk_template_parms, NULL);
695 ++processing_template_decl;
696 ++processing_template_parmlist;
697 note_template_header (0);
698
699 /* Add a dummy parameter level while we process the parameter list. */
700 current_template_parms
701 = tree_cons (size_int (processing_template_decl),
702 make_tree_vec (0),
703 current_template_parms);
704 }
705
706 /* This routine is called when a specialization is declared. If it is
707 invalid to declare a specialization here, an error is reported and
708 false is returned, otherwise this routine will return true. */
709
710 static bool
711 check_specialization_scope (void)
712 {
713 tree scope = current_scope ();
714
715 /* [temp.expl.spec]
716
717 An explicit specialization shall be declared in the namespace of
718 which the template is a member, or, for member templates, in the
719 namespace of which the enclosing class or enclosing class
720 template is a member. An explicit specialization of a member
721 function, member class or static data member of a class template
722 shall be declared in the namespace of which the class template
723 is a member. */
724 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
725 {
726 error ("explicit specialization in non-namespace scope %qD", scope);
727 return false;
728 }
729
730 /* [temp.expl.spec]
731
732 In an explicit specialization declaration for a member of a class
733 template or a member template that appears in namespace scope,
734 the member template and some of its enclosing class templates may
735 remain unspecialized, except that the declaration shall not
736 explicitly specialize a class member template if its enclosing
737 class templates are not explicitly specialized as well. */
738 if (current_template_parms)
739 {
740 error ("enclosing class templates are not explicitly specialized");
741 return false;
742 }
743
744 return true;
745 }
746
747 /* We've just seen template <>. */
748
749 bool
750 begin_specialization (void)
751 {
752 begin_scope (sk_template_spec, NULL);
753 note_template_header (1);
754 return check_specialization_scope ();
755 }
756
757 /* Called at then end of processing a declaration preceded by
758 template<>. */
759
760 void
761 end_specialization (void)
762 {
763 finish_scope ();
764 reset_specialization ();
765 }
766
767 /* Any template <>'s that we have seen thus far are not referring to a
768 function specialization. */
769
770 void
771 reset_specialization (void)
772 {
773 processing_specialization = 0;
774 template_header_count = 0;
775 }
776
777 /* We've just seen a template header. If SPECIALIZATION is nonzero,
778 it was of the form template <>. */
779
780 static void
781 note_template_header (int specialization)
782 {
783 processing_specialization = specialization;
784 template_header_count++;
785 }
786
787 /* We're beginning an explicit instantiation. */
788
789 void
790 begin_explicit_instantiation (void)
791 {
792 gcc_assert (!processing_explicit_instantiation);
793 processing_explicit_instantiation = true;
794 }
795
796
797 void
798 end_explicit_instantiation (void)
799 {
800 gcc_assert (processing_explicit_instantiation);
801 processing_explicit_instantiation = false;
802 }
803
804 /* An explicit specialization or partial specialization of TMPL is being
805 declared. Check that the namespace in which the specialization is
806 occurring is permissible. Returns false iff it is invalid to
807 specialize TMPL in the current namespace. */
808
809 static bool
810 check_specialization_namespace (tree tmpl)
811 {
812 tree tpl_ns = decl_namespace_context (tmpl);
813
814 /* [tmpl.expl.spec]
815
816 An explicit specialization shall be declared in a namespace enclosing the
817 specialized template. An explicit specialization whose declarator-id is
818 not qualified shall be declared in the nearest enclosing namespace of the
819 template, or, if the namespace is inline (7.3.1), any namespace from its
820 enclosing namespace set. */
821 if (current_scope() != DECL_CONTEXT (tmpl)
822 && !at_namespace_scope_p ())
823 {
824 error ("specialization of %qD must appear at namespace scope", tmpl);
825 return false;
826 }
827
828 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
829 /* Same or enclosing namespace. */
830 return true;
831 else
832 {
833 auto_diagnostic_group d;
834 if (permerror (input_location,
835 "specialization of %qD in different namespace", tmpl))
836 inform (DECL_SOURCE_LOCATION (tmpl),
837 " from definition of %q#D", tmpl);
838 return false;
839 }
840 }
841
842 /* SPEC is an explicit instantiation. Check that it is valid to
843 perform this explicit instantiation in the current namespace. */
844
845 static void
846 check_explicit_instantiation_namespace (tree spec)
847 {
848 tree ns;
849
850 /* DR 275: An explicit instantiation shall appear in an enclosing
851 namespace of its template. */
852 ns = decl_namespace_context (spec);
853 if (!is_nested_namespace (current_namespace, ns))
854 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
855 "(which does not enclose namespace %qD)",
856 spec, current_namespace, ns);
857 }
858
859 // Returns the type of a template specialization only if that
860 // specialization needs to be defined. Otherwise (e.g., if the type has
861 // already been defined), the function returns NULL_TREE.
862 static tree
863 maybe_new_partial_specialization (tree type)
864 {
865 // An implicit instantiation of an incomplete type implies
866 // the definition of a new class template.
867 //
868 // template<typename T>
869 // struct S;
870 //
871 // template<typename T>
872 // struct S<T*>;
873 //
874 // Here, S<T*> is an implicit instantiation of S whose type
875 // is incomplete.
876 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
877 return type;
878
879 // It can also be the case that TYPE is a completed specialization.
880 // Continuing the previous example, suppose we also declare:
881 //
882 // template<typename T>
883 // requires Integral<T>
884 // struct S<T*>;
885 //
886 // Here, S<T*> refers to the specialization S<T*> defined
887 // above. However, we need to differentiate definitions because
888 // we intend to define a new partial specialization. In this case,
889 // we rely on the fact that the constraints are different for
890 // this declaration than that above.
891 //
892 // Note that we also get here for injected class names and
893 // late-parsed template definitions. We must ensure that we
894 // do not create new type declarations for those cases.
895 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
896 {
897 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
898 tree args = CLASSTYPE_TI_ARGS (type);
899
900 // If there are no template parameters, this cannot be a new
901 // partial template specializtion?
902 if (!current_template_parms)
903 return NULL_TREE;
904
905 // The injected-class-name is not a new partial specialization.
906 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
907 return NULL_TREE;
908
909 // If the constraints are not the same as those of the primary
910 // then, we can probably create a new specialization.
911 tree type_constr = current_template_constraints ();
912
913 if (type == TREE_TYPE (tmpl))
914 {
915 tree main_constr = get_constraints (tmpl);
916 if (equivalent_constraints (type_constr, main_constr))
917 return NULL_TREE;
918 }
919
920 // Also, if there's a pre-existing specialization with matching
921 // constraints, then this also isn't new.
922 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
923 while (specs)
924 {
925 tree spec_tmpl = TREE_VALUE (specs);
926 tree spec_args = TREE_PURPOSE (specs);
927 tree spec_constr = get_constraints (spec_tmpl);
928 if (comp_template_args (args, spec_args)
929 && equivalent_constraints (type_constr, spec_constr))
930 return NULL_TREE;
931 specs = TREE_CHAIN (specs);
932 }
933
934 // Create a new type node (and corresponding type decl)
935 // for the newly declared specialization.
936 tree t = make_class_type (TREE_CODE (type));
937 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
938 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
939
940 /* We only need a separate type node for storing the definition of this
941 partial specialization; uses of S<T*> are unconstrained, so all are
942 equivalent. So keep TYPE_CANONICAL the same. */
943 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
944
945 // Build the corresponding type decl.
946 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
947 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
948 DECL_SOURCE_LOCATION (d) = input_location;
949
950 return t;
951 }
952
953 return NULL_TREE;
954 }
955
956 /* The TYPE is being declared. If it is a template type, that means it
957 is a partial specialization. Do appropriate error-checking. */
958
959 tree
960 maybe_process_partial_specialization (tree type)
961 {
962 tree context;
963
964 if (type == error_mark_node)
965 return error_mark_node;
966
967 /* A lambda that appears in specialization context is not itself a
968 specialization. */
969 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
970 return type;
971
972 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
973 {
974 error ("name of class shadows template template parameter %qD",
975 TYPE_NAME (type));
976 return error_mark_node;
977 }
978
979 context = TYPE_CONTEXT (type);
980
981 if (TYPE_ALIAS_P (type))
982 {
983 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
984
985 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
986 error ("specialization of alias template %qD",
987 TI_TEMPLATE (tinfo));
988 else
989 error ("explicit specialization of non-template %qT", type);
990 return error_mark_node;
991 }
992 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
993 {
994 /* This is for ordinary explicit specialization and partial
995 specialization of a template class such as:
996
997 template <> class C<int>;
998
999 or:
1000
1001 template <class T> class C<T*>;
1002
1003 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1004
1005 if (tree t = maybe_new_partial_specialization (type))
1006 {
1007 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1008 && !at_namespace_scope_p ())
1009 return error_mark_node;
1010 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1011 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1012 if (processing_template_decl)
1013 {
1014 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1015 if (decl == error_mark_node)
1016 return error_mark_node;
1017 return TREE_TYPE (decl);
1018 }
1019 }
1020 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1021 error ("specialization of %qT after instantiation", type);
1022 else if (errorcount && !processing_specialization
1023 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1024 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1025 /* Trying to define a specialization either without a template<> header
1026 or in an inappropriate place. We've already given an error, so just
1027 bail now so we don't actually define the specialization. */
1028 return error_mark_node;
1029 }
1030 else if (CLASS_TYPE_P (type)
1031 && !CLASSTYPE_USE_TEMPLATE (type)
1032 && CLASSTYPE_TEMPLATE_INFO (type)
1033 && context && CLASS_TYPE_P (context)
1034 && CLASSTYPE_TEMPLATE_INFO (context))
1035 {
1036 /* This is for an explicit specialization of member class
1037 template according to [temp.expl.spec/18]:
1038
1039 template <> template <class U> class C<int>::D;
1040
1041 The context `C<int>' must be an implicit instantiation.
1042 Otherwise this is just a member class template declared
1043 earlier like:
1044
1045 template <> class C<int> { template <class U> class D; };
1046 template <> template <class U> class C<int>::D;
1047
1048 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1049 while in the second case, `C<int>::D' is a primary template
1050 and `C<T>::D' may not exist. */
1051
1052 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1053 && !COMPLETE_TYPE_P (type))
1054 {
1055 tree t;
1056 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1057
1058 if (current_namespace
1059 != decl_namespace_context (tmpl))
1060 {
1061 permerror (input_location,
1062 "specializing %q#T in different namespace", type);
1063 permerror (DECL_SOURCE_LOCATION (tmpl),
1064 " from definition of %q#D", tmpl);
1065 }
1066
1067 /* Check for invalid specialization after instantiation:
1068
1069 template <> template <> class C<int>::D<int>;
1070 template <> template <class U> class C<int>::D; */
1071
1072 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1073 t; t = TREE_CHAIN (t))
1074 {
1075 tree inst = TREE_VALUE (t);
1076 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1077 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1078 {
1079 /* We already have a full specialization of this partial
1080 instantiation, or a full specialization has been
1081 looked up but not instantiated. Reassign it to the
1082 new member specialization template. */
1083 spec_entry elt;
1084 spec_entry *entry;
1085
1086 elt.tmpl = most_general_template (tmpl);
1087 elt.args = CLASSTYPE_TI_ARGS (inst);
1088 elt.spec = inst;
1089
1090 type_specializations->remove_elt (&elt);
1091
1092 elt.tmpl = tmpl;
1093 CLASSTYPE_TI_ARGS (inst)
1094 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1095
1096 spec_entry **slot
1097 = type_specializations->find_slot (&elt, INSERT);
1098 entry = ggc_alloc<spec_entry> ();
1099 *entry = elt;
1100 *slot = entry;
1101 }
1102 else
1103 /* But if we've had an implicit instantiation, that's a
1104 problem ([temp.expl.spec]/6). */
1105 error ("specialization %qT after instantiation %qT",
1106 type, inst);
1107 }
1108
1109 /* Mark TYPE as a specialization. And as a result, we only
1110 have one level of template argument for the innermost
1111 class template. */
1112 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1113 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1114 CLASSTYPE_TI_ARGS (type)
1115 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1116 }
1117 }
1118 else if (processing_specialization)
1119 {
1120 /* Someday C++0x may allow for enum template specialization. */
1121 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1122 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1123 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1124 "of %qD not allowed by ISO C++", type);
1125 else
1126 {
1127 error ("explicit specialization of non-template %qT", type);
1128 return error_mark_node;
1129 }
1130 }
1131
1132 return type;
1133 }
1134
1135 /* Returns nonzero if we can optimize the retrieval of specializations
1136 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1137 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1138
1139 static inline bool
1140 optimize_specialization_lookup_p (tree tmpl)
1141 {
1142 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1143 && DECL_CLASS_SCOPE_P (tmpl)
1144 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1145 parameter. */
1146 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1147 /* The optimized lookup depends on the fact that the
1148 template arguments for the member function template apply
1149 purely to the containing class, which is not true if the
1150 containing class is an explicit or partial
1151 specialization. */
1152 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1153 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1154 && !DECL_CONV_FN_P (tmpl)
1155 /* It is possible to have a template that is not a member
1156 template and is not a member of a template class:
1157
1158 template <typename T>
1159 struct S { friend A::f(); };
1160
1161 Here, the friend function is a template, but the context does
1162 not have template information. The optimized lookup relies
1163 on having ARGS be the template arguments for both the class
1164 and the function template. */
1165 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1166 }
1167
1168 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1169 gone through coerce_template_parms by now. */
1170
1171 static void
1172 verify_unstripped_args_1 (tree inner)
1173 {
1174 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1175 {
1176 tree arg = TREE_VEC_ELT (inner, i);
1177 if (TREE_CODE (arg) == TEMPLATE_DECL)
1178 /* OK */;
1179 else if (TYPE_P (arg))
1180 gcc_assert (strip_typedefs (arg, NULL) == arg);
1181 else if (ARGUMENT_PACK_P (arg))
1182 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1183 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1184 /* Allow typedefs on the type of a non-type argument, since a
1185 parameter can have them. */;
1186 else
1187 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1188 }
1189 }
1190
1191 static void
1192 verify_unstripped_args (tree args)
1193 {
1194 ++processing_template_decl;
1195 if (!any_dependent_template_arguments_p (args))
1196 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1197 --processing_template_decl;
1198 }
1199
1200 /* Retrieve the specialization (in the sense of [temp.spec] - a
1201 specialization is either an instantiation or an explicit
1202 specialization) of TMPL for the given template ARGS. If there is
1203 no such specialization, return NULL_TREE. The ARGS are a vector of
1204 arguments, or a vector of vectors of arguments, in the case of
1205 templates with more than one level of parameters.
1206
1207 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1208 then we search for a partial specialization matching ARGS. This
1209 parameter is ignored if TMPL is not a class template.
1210
1211 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1212 result is a NONTYPE_ARGUMENT_PACK. */
1213
1214 static tree
1215 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1216 {
1217 if (tmpl == NULL_TREE)
1218 return NULL_TREE;
1219
1220 if (args == error_mark_node)
1221 return NULL_TREE;
1222
1223 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1224 || TREE_CODE (tmpl) == FIELD_DECL);
1225
1226 /* There should be as many levels of arguments as there are
1227 levels of parameters. */
1228 gcc_assert (TMPL_ARGS_DEPTH (args)
1229 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1230 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1231 : template_class_depth (DECL_CONTEXT (tmpl))));
1232
1233 if (flag_checking)
1234 verify_unstripped_args (args);
1235
1236 /* Lambda functions in templates aren't instantiated normally, but through
1237 tsubst_lambda_expr. */
1238 if (lambda_fn_in_template_p (tmpl))
1239 return NULL_TREE;
1240
1241 if (optimize_specialization_lookup_p (tmpl))
1242 {
1243 /* The template arguments actually apply to the containing
1244 class. Find the class specialization with those
1245 arguments. */
1246 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1247 tree class_specialization
1248 = retrieve_specialization (class_template, args, 0);
1249 if (!class_specialization)
1250 return NULL_TREE;
1251
1252 /* Find the instance of TMPL. */
1253 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1254 for (ovl_iterator iter (fns); iter; ++iter)
1255 {
1256 tree fn = *iter;
1257 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1258 /* using-declarations can add base methods to the method vec,
1259 and we don't want those here. */
1260 && DECL_CONTEXT (fn) == class_specialization)
1261 return fn;
1262 }
1263 return NULL_TREE;
1264 }
1265 else
1266 {
1267 spec_entry *found;
1268 spec_entry elt;
1269 hash_table<spec_hasher> *specializations;
1270
1271 elt.tmpl = tmpl;
1272 elt.args = args;
1273 elt.spec = NULL_TREE;
1274
1275 if (DECL_CLASS_TEMPLATE_P (tmpl))
1276 specializations = type_specializations;
1277 else
1278 specializations = decl_specializations;
1279
1280 if (hash == 0)
1281 hash = spec_hasher::hash (&elt);
1282 found = specializations->find_with_hash (&elt, hash);
1283 if (found)
1284 return found->spec;
1285 }
1286
1287 return NULL_TREE;
1288 }
1289
1290 /* Like retrieve_specialization, but for local declarations. */
1291
1292 tree
1293 retrieve_local_specialization (tree tmpl)
1294 {
1295 if (local_specializations == NULL)
1296 return NULL_TREE;
1297
1298 tree *slot = local_specializations->get (tmpl);
1299 return slot ? *slot : NULL_TREE;
1300 }
1301
1302 /* Returns nonzero iff DECL is a specialization of TMPL. */
1303
1304 int
1305 is_specialization_of (tree decl, tree tmpl)
1306 {
1307 tree t;
1308
1309 if (TREE_CODE (decl) == FUNCTION_DECL)
1310 {
1311 for (t = decl;
1312 t != NULL_TREE;
1313 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1314 if (t == tmpl)
1315 return 1;
1316 }
1317 else
1318 {
1319 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1320
1321 for (t = TREE_TYPE (decl);
1322 t != NULL_TREE;
1323 t = CLASSTYPE_USE_TEMPLATE (t)
1324 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1325 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1326 return 1;
1327 }
1328
1329 return 0;
1330 }
1331
1332 /* Returns nonzero iff DECL is a specialization of friend declaration
1333 FRIEND_DECL according to [temp.friend]. */
1334
1335 bool
1336 is_specialization_of_friend (tree decl, tree friend_decl)
1337 {
1338 bool need_template = true;
1339 int template_depth;
1340
1341 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1342 || TREE_CODE (decl) == TYPE_DECL);
1343
1344 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1345 of a template class, we want to check if DECL is a specialization
1346 if this. */
1347 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1348 && DECL_TEMPLATE_INFO (friend_decl)
1349 && !DECL_USE_TEMPLATE (friend_decl))
1350 {
1351 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1352 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1353 need_template = false;
1354 }
1355 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1356 && !PRIMARY_TEMPLATE_P (friend_decl))
1357 need_template = false;
1358
1359 /* There is nothing to do if this is not a template friend. */
1360 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1361 return false;
1362
1363 if (is_specialization_of (decl, friend_decl))
1364 return true;
1365
1366 /* [temp.friend/6]
1367 A member of a class template may be declared to be a friend of a
1368 non-template class. In this case, the corresponding member of
1369 every specialization of the class template is a friend of the
1370 class granting friendship.
1371
1372 For example, given a template friend declaration
1373
1374 template <class T> friend void A<T>::f();
1375
1376 the member function below is considered a friend
1377
1378 template <> struct A<int> {
1379 void f();
1380 };
1381
1382 For this type of template friend, TEMPLATE_DEPTH below will be
1383 nonzero. To determine if DECL is a friend of FRIEND, we first
1384 check if the enclosing class is a specialization of another. */
1385
1386 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1387 if (template_depth
1388 && DECL_CLASS_SCOPE_P (decl)
1389 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1390 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1391 {
1392 /* Next, we check the members themselves. In order to handle
1393 a few tricky cases, such as when FRIEND_DECL's are
1394
1395 template <class T> friend void A<T>::g(T t);
1396 template <class T> template <T t> friend void A<T>::h();
1397
1398 and DECL's are
1399
1400 void A<int>::g(int);
1401 template <int> void A<int>::h();
1402
1403 we need to figure out ARGS, the template arguments from
1404 the context of DECL. This is required for template substitution
1405 of `T' in the function parameter of `g' and template parameter
1406 of `h' in the above examples. Here ARGS corresponds to `int'. */
1407
1408 tree context = DECL_CONTEXT (decl);
1409 tree args = NULL_TREE;
1410 int current_depth = 0;
1411
1412 while (current_depth < template_depth)
1413 {
1414 if (CLASSTYPE_TEMPLATE_INFO (context))
1415 {
1416 if (current_depth == 0)
1417 args = TYPE_TI_ARGS (context);
1418 else
1419 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1420 current_depth++;
1421 }
1422 context = TYPE_CONTEXT (context);
1423 }
1424
1425 if (TREE_CODE (decl) == FUNCTION_DECL)
1426 {
1427 bool is_template;
1428 tree friend_type;
1429 tree decl_type;
1430 tree friend_args_type;
1431 tree decl_args_type;
1432
1433 /* Make sure that both DECL and FRIEND_DECL are templates or
1434 non-templates. */
1435 is_template = DECL_TEMPLATE_INFO (decl)
1436 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1437 if (need_template ^ is_template)
1438 return false;
1439 else if (is_template)
1440 {
1441 /* If both are templates, check template parameter list. */
1442 tree friend_parms
1443 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1444 args, tf_none);
1445 if (!comp_template_parms
1446 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1447 friend_parms))
1448 return false;
1449
1450 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1451 }
1452 else
1453 decl_type = TREE_TYPE (decl);
1454
1455 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1456 tf_none, NULL_TREE);
1457 if (friend_type == error_mark_node)
1458 return false;
1459
1460 /* Check if return types match. */
1461 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1462 return false;
1463
1464 /* Check if function parameter types match, ignoring the
1465 `this' parameter. */
1466 friend_args_type = TYPE_ARG_TYPES (friend_type);
1467 decl_args_type = TYPE_ARG_TYPES (decl_type);
1468 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1469 friend_args_type = TREE_CHAIN (friend_args_type);
1470 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1471 decl_args_type = TREE_CHAIN (decl_args_type);
1472
1473 return compparms (decl_args_type, friend_args_type);
1474 }
1475 else
1476 {
1477 /* DECL is a TYPE_DECL */
1478 bool is_template;
1479 tree decl_type = TREE_TYPE (decl);
1480
1481 /* Make sure that both DECL and FRIEND_DECL are templates or
1482 non-templates. */
1483 is_template
1484 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1485 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1486
1487 if (need_template ^ is_template)
1488 return false;
1489 else if (is_template)
1490 {
1491 tree friend_parms;
1492 /* If both are templates, check the name of the two
1493 TEMPLATE_DECL's first because is_friend didn't. */
1494 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1495 != DECL_NAME (friend_decl))
1496 return false;
1497
1498 /* Now check template parameter list. */
1499 friend_parms
1500 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1501 args, tf_none);
1502 return comp_template_parms
1503 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1504 friend_parms);
1505 }
1506 else
1507 return (DECL_NAME (decl)
1508 == DECL_NAME (friend_decl));
1509 }
1510 }
1511 return false;
1512 }
1513
1514 /* Register the specialization SPEC as a specialization of TMPL with
1515 the indicated ARGS. IS_FRIEND indicates whether the specialization
1516 is actually just a friend declaration. ATTRLIST is the list of
1517 attributes that the specialization is declared with or NULL when
1518 it isn't. Returns SPEC, or an equivalent prior declaration, if
1519 available.
1520
1521 We also store instantiations of field packs in the hash table, even
1522 though they are not themselves templates, to make lookup easier. */
1523
1524 static tree
1525 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1526 hashval_t hash)
1527 {
1528 tree fn;
1529 spec_entry **slot = NULL;
1530 spec_entry elt;
1531
1532 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1533 || (TREE_CODE (tmpl) == FIELD_DECL
1534 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1535
1536 if (TREE_CODE (spec) == FUNCTION_DECL
1537 && uses_template_parms (DECL_TI_ARGS (spec)))
1538 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1539 register it; we want the corresponding TEMPLATE_DECL instead.
1540 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1541 the more obvious `uses_template_parms (spec)' to avoid problems
1542 with default function arguments. In particular, given
1543 something like this:
1544
1545 template <class T> void f(T t1, T t = T())
1546
1547 the default argument expression is not substituted for in an
1548 instantiation unless and until it is actually needed. */
1549 return spec;
1550
1551 if (optimize_specialization_lookup_p (tmpl))
1552 /* We don't put these specializations in the hash table, but we might
1553 want to give an error about a mismatch. */
1554 fn = retrieve_specialization (tmpl, args, 0);
1555 else
1556 {
1557 elt.tmpl = tmpl;
1558 elt.args = args;
1559 elt.spec = spec;
1560
1561 if (hash == 0)
1562 hash = spec_hasher::hash (&elt);
1563
1564 slot =
1565 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1566 if (*slot)
1567 fn = ((spec_entry *) *slot)->spec;
1568 else
1569 fn = NULL_TREE;
1570 }
1571
1572 /* We can sometimes try to re-register a specialization that we've
1573 already got. In particular, regenerate_decl_from_template calls
1574 duplicate_decls which will update the specialization list. But,
1575 we'll still get called again here anyhow. It's more convenient
1576 to simply allow this than to try to prevent it. */
1577 if (fn == spec)
1578 return spec;
1579 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1580 {
1581 if (DECL_TEMPLATE_INSTANTIATION (fn))
1582 {
1583 if (DECL_ODR_USED (fn)
1584 || DECL_EXPLICIT_INSTANTIATION (fn))
1585 {
1586 error ("specialization of %qD after instantiation",
1587 fn);
1588 return error_mark_node;
1589 }
1590 else
1591 {
1592 tree clone;
1593 /* This situation should occur only if the first
1594 specialization is an implicit instantiation, the
1595 second is an explicit specialization, and the
1596 implicit instantiation has not yet been used. That
1597 situation can occur if we have implicitly
1598 instantiated a member function and then specialized
1599 it later.
1600
1601 We can also wind up here if a friend declaration that
1602 looked like an instantiation turns out to be a
1603 specialization:
1604
1605 template <class T> void foo(T);
1606 class S { friend void foo<>(int) };
1607 template <> void foo(int);
1608
1609 We transform the existing DECL in place so that any
1610 pointers to it become pointers to the updated
1611 declaration.
1612
1613 If there was a definition for the template, but not
1614 for the specialization, we want this to look as if
1615 there were no definition, and vice versa. */
1616 DECL_INITIAL (fn) = NULL_TREE;
1617 duplicate_decls (spec, fn, is_friend);
1618 /* The call to duplicate_decls will have applied
1619 [temp.expl.spec]:
1620
1621 An explicit specialization of a function template
1622 is inline only if it is explicitly declared to be,
1623 and independently of whether its function template
1624 is.
1625
1626 to the primary function; now copy the inline bits to
1627 the various clones. */
1628 FOR_EACH_CLONE (clone, fn)
1629 {
1630 DECL_DECLARED_INLINE_P (clone)
1631 = DECL_DECLARED_INLINE_P (fn);
1632 DECL_SOURCE_LOCATION (clone)
1633 = DECL_SOURCE_LOCATION (fn);
1634 DECL_DELETED_FN (clone)
1635 = DECL_DELETED_FN (fn);
1636 }
1637 check_specialization_namespace (tmpl);
1638
1639 return fn;
1640 }
1641 }
1642 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1643 {
1644 tree dd = duplicate_decls (spec, fn, is_friend);
1645 if (dd == error_mark_node)
1646 /* We've already complained in duplicate_decls. */
1647 return error_mark_node;
1648
1649 if (dd == NULL_TREE && DECL_INITIAL (spec))
1650 /* Dup decl failed, but this is a new definition. Set the
1651 line number so any errors match this new
1652 definition. */
1653 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1654
1655 return fn;
1656 }
1657 }
1658 else if (fn)
1659 return duplicate_decls (spec, fn, is_friend);
1660
1661 /* A specialization must be declared in the same namespace as the
1662 template it is specializing. */
1663 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1664 && !check_specialization_namespace (tmpl))
1665 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1666
1667 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1668 {
1669 spec_entry *entry = ggc_alloc<spec_entry> ();
1670 gcc_assert (tmpl && args && spec);
1671 *entry = elt;
1672 *slot = entry;
1673 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1674 && PRIMARY_TEMPLATE_P (tmpl)
1675 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1676 || variable_template_p (tmpl))
1677 /* If TMPL is a forward declaration of a template function, keep a list
1678 of all specializations in case we need to reassign them to a friend
1679 template later in tsubst_friend_function.
1680
1681 Also keep a list of all variable template instantiations so that
1682 process_partial_specialization can check whether a later partial
1683 specialization would have used it. */
1684 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1685 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1686 }
1687
1688 return spec;
1689 }
1690
1691 /* Returns true iff two spec_entry nodes are equivalent. */
1692
1693 int comparing_specializations;
1694
1695 bool
1696 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1697 {
1698 int equal;
1699
1700 ++comparing_specializations;
1701 equal = (e1->tmpl == e2->tmpl
1702 && comp_template_args (e1->args, e2->args));
1703 if (equal && flag_concepts
1704 /* tmpl could be a FIELD_DECL for a capture pack. */
1705 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1706 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1707 && uses_template_parms (e1->args))
1708 {
1709 /* Partial specializations of a variable template can be distinguished by
1710 constraints. */
1711 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1712 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1713 equal = equivalent_constraints (c1, c2);
1714 }
1715 --comparing_specializations;
1716
1717 return equal;
1718 }
1719
1720 /* Returns a hash for a template TMPL and template arguments ARGS. */
1721
1722 static hashval_t
1723 hash_tmpl_and_args (tree tmpl, tree args)
1724 {
1725 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1726 return iterative_hash_template_arg (args, val);
1727 }
1728
1729 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1730 ignoring SPEC. */
1731
1732 hashval_t
1733 spec_hasher::hash (spec_entry *e)
1734 {
1735 return hash_tmpl_and_args (e->tmpl, e->args);
1736 }
1737
1738 /* Recursively calculate a hash value for a template argument ARG, for use
1739 in the hash tables of template specializations. */
1740
1741 hashval_t
1742 iterative_hash_template_arg (tree arg, hashval_t val)
1743 {
1744 unsigned HOST_WIDE_INT i;
1745 enum tree_code code;
1746 char tclass;
1747
1748 if (arg == NULL_TREE)
1749 return iterative_hash_object (arg, val);
1750
1751 if (!TYPE_P (arg))
1752 STRIP_NOPS (arg);
1753
1754 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1755 gcc_unreachable ();
1756
1757 code = TREE_CODE (arg);
1758 tclass = TREE_CODE_CLASS (code);
1759
1760 val = iterative_hash_object (code, val);
1761
1762 switch (code)
1763 {
1764 case ERROR_MARK:
1765 return val;
1766
1767 case IDENTIFIER_NODE:
1768 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1769
1770 case TREE_VEC:
1771 {
1772 int i, len = TREE_VEC_LENGTH (arg);
1773 for (i = 0; i < len; ++i)
1774 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1775 return val;
1776 }
1777
1778 case TYPE_PACK_EXPANSION:
1779 case EXPR_PACK_EXPANSION:
1780 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1781 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1782
1783 case TYPE_ARGUMENT_PACK:
1784 case NONTYPE_ARGUMENT_PACK:
1785 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1786
1787 case TREE_LIST:
1788 for (; arg; arg = TREE_CHAIN (arg))
1789 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1790 return val;
1791
1792 case OVERLOAD:
1793 for (lkp_iterator iter (arg); iter; ++iter)
1794 val = iterative_hash_template_arg (*iter, val);
1795 return val;
1796
1797 case CONSTRUCTOR:
1798 {
1799 tree field, value;
1800 iterative_hash_template_arg (TREE_TYPE (arg), val);
1801 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1802 {
1803 val = iterative_hash_template_arg (field, val);
1804 val = iterative_hash_template_arg (value, val);
1805 }
1806 return val;
1807 }
1808
1809 case PARM_DECL:
1810 if (!DECL_ARTIFICIAL (arg))
1811 {
1812 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1813 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1814 }
1815 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1816
1817 case TARGET_EXPR:
1818 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1819
1820 case PTRMEM_CST:
1821 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1822 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1823
1824 case TEMPLATE_PARM_INDEX:
1825 val = iterative_hash_template_arg
1826 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1827 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1828 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1829
1830 case TRAIT_EXPR:
1831 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1832 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1833 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1834
1835 case BASELINK:
1836 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1837 val);
1838 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1839 val);
1840
1841 case MODOP_EXPR:
1842 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1843 code = TREE_CODE (TREE_OPERAND (arg, 1));
1844 val = iterative_hash_object (code, val);
1845 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1846
1847 case LAMBDA_EXPR:
1848 /* [temp.over.link] Two lambda-expressions are never considered
1849 equivalent.
1850
1851 So just hash the closure type. */
1852 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1853
1854 case CAST_EXPR:
1855 case IMPLICIT_CONV_EXPR:
1856 case STATIC_CAST_EXPR:
1857 case REINTERPRET_CAST_EXPR:
1858 case CONST_CAST_EXPR:
1859 case DYNAMIC_CAST_EXPR:
1860 case NEW_EXPR:
1861 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1862 /* Now hash operands as usual. */
1863 break;
1864
1865 case CALL_EXPR:
1866 {
1867 tree fn = CALL_EXPR_FN (arg);
1868 if (tree name = dependent_name (fn))
1869 {
1870 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1871 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1872 fn = name;
1873 }
1874 val = iterative_hash_template_arg (fn, val);
1875 call_expr_arg_iterator ai;
1876 for (tree x = first_call_expr_arg (arg, &ai); x;
1877 x = next_call_expr_arg (&ai))
1878 val = iterative_hash_template_arg (x, val);
1879 return val;
1880 }
1881
1882 default:
1883 break;
1884 }
1885
1886 switch (tclass)
1887 {
1888 case tcc_type:
1889 if (alias_template_specialization_p (arg))
1890 {
1891 // We want an alias specialization that survived strip_typedefs
1892 // to hash differently from its TYPE_CANONICAL, to avoid hash
1893 // collisions that compare as different in template_args_equal.
1894 // These could be dependent specializations that strip_typedefs
1895 // left alone, or untouched specializations because
1896 // coerce_template_parms returns the unconverted template
1897 // arguments if it sees incomplete argument packs.
1898 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1899 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1900 }
1901 if (TYPE_CANONICAL (arg))
1902 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1903 val);
1904 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1905 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1906 /* Otherwise just compare the types during lookup. */
1907 return val;
1908
1909 case tcc_declaration:
1910 case tcc_constant:
1911 return iterative_hash_expr (arg, val);
1912
1913 default:
1914 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1915 {
1916 unsigned n = cp_tree_operand_length (arg);
1917 for (i = 0; i < n; ++i)
1918 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1919 return val;
1920 }
1921 }
1922 gcc_unreachable ();
1923 return 0;
1924 }
1925
1926 /* Unregister the specialization SPEC as a specialization of TMPL.
1927 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1928 if the SPEC was listed as a specialization of TMPL.
1929
1930 Note that SPEC has been ggc_freed, so we can't look inside it. */
1931
1932 bool
1933 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1934 {
1935 spec_entry *entry;
1936 spec_entry elt;
1937
1938 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1939 elt.args = TI_ARGS (tinfo);
1940 elt.spec = NULL_TREE;
1941
1942 entry = decl_specializations->find (&elt);
1943 if (entry != NULL)
1944 {
1945 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1946 gcc_assert (new_spec != NULL_TREE);
1947 entry->spec = new_spec;
1948 return 1;
1949 }
1950
1951 return 0;
1952 }
1953
1954 /* Like register_specialization, but for local declarations. We are
1955 registering SPEC, an instantiation of TMPL. */
1956
1957 void
1958 register_local_specialization (tree spec, tree tmpl)
1959 {
1960 gcc_assert (tmpl != spec);
1961 local_specializations->put (tmpl, spec);
1962 }
1963
1964 /* TYPE is a class type. Returns true if TYPE is an explicitly
1965 specialized class. */
1966
1967 bool
1968 explicit_class_specialization_p (tree type)
1969 {
1970 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1971 return false;
1972 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1973 }
1974
1975 /* Print the list of functions at FNS, going through all the overloads
1976 for each element of the list. Alternatively, FNS cannot be a
1977 TREE_LIST, in which case it will be printed together with all the
1978 overloads.
1979
1980 MORE and *STR should respectively be FALSE and NULL when the function
1981 is called from the outside. They are used internally on recursive
1982 calls. print_candidates manages the two parameters and leaves NULL
1983 in *STR when it ends. */
1984
1985 static void
1986 print_candidates_1 (tree fns, char **str, bool more = false)
1987 {
1988 if (TREE_CODE (fns) == TREE_LIST)
1989 for (; fns; fns = TREE_CHAIN (fns))
1990 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1991 else
1992 for (lkp_iterator iter (fns); iter;)
1993 {
1994 tree cand = *iter;
1995 ++iter;
1996
1997 const char *pfx = *str;
1998 if (!pfx)
1999 {
2000 if (more || iter)
2001 pfx = _("candidates are:");
2002 else
2003 pfx = _("candidate is:");
2004 *str = get_spaces (pfx);
2005 }
2006 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2007 }
2008 }
2009
2010 /* Print the list of candidate FNS in an error message. FNS can also
2011 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2012
2013 void
2014 print_candidates (tree fns)
2015 {
2016 char *str = NULL;
2017 print_candidates_1 (fns, &str);
2018 free (str);
2019 }
2020
2021 /* Get a (possibly) constrained template declaration for the
2022 purpose of ordering candidates. */
2023 static tree
2024 get_template_for_ordering (tree list)
2025 {
2026 gcc_assert (TREE_CODE (list) == TREE_LIST);
2027 tree f = TREE_VALUE (list);
2028 if (tree ti = DECL_TEMPLATE_INFO (f))
2029 return TI_TEMPLATE (ti);
2030 return f;
2031 }
2032
2033 /* Among candidates having the same signature, return the
2034 most constrained or NULL_TREE if there is no best candidate.
2035 If the signatures of candidates vary (e.g., template
2036 specialization vs. member function), then there can be no
2037 most constrained.
2038
2039 Note that we don't compare constraints on the functions
2040 themselves, but rather those of their templates. */
2041 static tree
2042 most_constrained_function (tree candidates)
2043 {
2044 // Try to find the best candidate in a first pass.
2045 tree champ = candidates;
2046 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2047 {
2048 int winner = more_constrained (get_template_for_ordering (champ),
2049 get_template_for_ordering (c));
2050 if (winner == -1)
2051 champ = c; // The candidate is more constrained
2052 else if (winner == 0)
2053 return NULL_TREE; // Neither is more constrained
2054 }
2055
2056 // Verify that the champ is better than previous candidates.
2057 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2058 if (!more_constrained (get_template_for_ordering (champ),
2059 get_template_for_ordering (c)))
2060 return NULL_TREE;
2061 }
2062
2063 return champ;
2064 }
2065
2066
2067 /* Returns the template (one of the functions given by TEMPLATE_ID)
2068 which can be specialized to match the indicated DECL with the
2069 explicit template args given in TEMPLATE_ID. The DECL may be
2070 NULL_TREE if none is available. In that case, the functions in
2071 TEMPLATE_ID are non-members.
2072
2073 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2074 specialization of a member template.
2075
2076 The TEMPLATE_COUNT is the number of references to qualifying
2077 template classes that appeared in the name of the function. See
2078 check_explicit_specialization for a more accurate description.
2079
2080 TSK indicates what kind of template declaration (if any) is being
2081 declared. TSK_TEMPLATE indicates that the declaration given by
2082 DECL, though a FUNCTION_DECL, has template parameters, and is
2083 therefore a template function.
2084
2085 The template args (those explicitly specified and those deduced)
2086 are output in a newly created vector *TARGS_OUT.
2087
2088 If it is impossible to determine the result, an error message is
2089 issued. The error_mark_node is returned to indicate failure. */
2090
2091 static tree
2092 determine_specialization (tree template_id,
2093 tree decl,
2094 tree* targs_out,
2095 int need_member_template,
2096 int template_count,
2097 tmpl_spec_kind tsk)
2098 {
2099 tree fns;
2100 tree targs;
2101 tree explicit_targs;
2102 tree candidates = NULL_TREE;
2103
2104 /* A TREE_LIST of templates of which DECL may be a specialization.
2105 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2106 corresponding TREE_PURPOSE is the set of template arguments that,
2107 when used to instantiate the template, would produce a function
2108 with the signature of DECL. */
2109 tree templates = NULL_TREE;
2110 int header_count;
2111 cp_binding_level *b;
2112
2113 *targs_out = NULL_TREE;
2114
2115 if (template_id == error_mark_node || decl == error_mark_node)
2116 return error_mark_node;
2117
2118 /* We shouldn't be specializing a member template of an
2119 unspecialized class template; we already gave an error in
2120 check_specialization_scope, now avoid crashing. */
2121 if (!VAR_P (decl)
2122 && template_count && DECL_CLASS_SCOPE_P (decl)
2123 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2124 {
2125 gcc_assert (errorcount);
2126 return error_mark_node;
2127 }
2128
2129 fns = TREE_OPERAND (template_id, 0);
2130 explicit_targs = TREE_OPERAND (template_id, 1);
2131
2132 if (fns == error_mark_node)
2133 return error_mark_node;
2134
2135 /* Check for baselinks. */
2136 if (BASELINK_P (fns))
2137 fns = BASELINK_FUNCTIONS (fns);
2138
2139 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2140 {
2141 error ("%qD is not a function template", fns);
2142 return error_mark_node;
2143 }
2144 else if (VAR_P (decl) && !variable_template_p (fns))
2145 {
2146 error ("%qD is not a variable template", fns);
2147 return error_mark_node;
2148 }
2149
2150 /* Count the number of template headers specified for this
2151 specialization. */
2152 header_count = 0;
2153 for (b = current_binding_level;
2154 b->kind == sk_template_parms;
2155 b = b->level_chain)
2156 ++header_count;
2157
2158 tree orig_fns = fns;
2159
2160 if (variable_template_p (fns))
2161 {
2162 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2163 targs = coerce_template_parms (parms, explicit_targs, fns,
2164 tf_warning_or_error,
2165 /*req_all*/true, /*use_defarg*/true);
2166 if (targs != error_mark_node)
2167 templates = tree_cons (targs, fns, templates);
2168 }
2169 else for (lkp_iterator iter (fns); iter; ++iter)
2170 {
2171 tree fn = *iter;
2172
2173 if (TREE_CODE (fn) == TEMPLATE_DECL)
2174 {
2175 tree decl_arg_types;
2176 tree fn_arg_types;
2177 tree insttype;
2178
2179 /* In case of explicit specialization, we need to check if
2180 the number of template headers appearing in the specialization
2181 is correct. This is usually done in check_explicit_specialization,
2182 but the check done there cannot be exhaustive when specializing
2183 member functions. Consider the following code:
2184
2185 template <> void A<int>::f(int);
2186 template <> template <> void A<int>::f(int);
2187
2188 Assuming that A<int> is not itself an explicit specialization
2189 already, the first line specializes "f" which is a non-template
2190 member function, whilst the second line specializes "f" which
2191 is a template member function. So both lines are syntactically
2192 correct, and check_explicit_specialization does not reject
2193 them.
2194
2195 Here, we can do better, as we are matching the specialization
2196 against the declarations. We count the number of template
2197 headers, and we check if they match TEMPLATE_COUNT + 1
2198 (TEMPLATE_COUNT is the number of qualifying template classes,
2199 plus there must be another header for the member template
2200 itself).
2201
2202 Notice that if header_count is zero, this is not a
2203 specialization but rather a template instantiation, so there
2204 is no check we can perform here. */
2205 if (header_count && header_count != template_count + 1)
2206 continue;
2207
2208 /* Check that the number of template arguments at the
2209 innermost level for DECL is the same as for FN. */
2210 if (current_binding_level->kind == sk_template_parms
2211 && !current_binding_level->explicit_spec_p
2212 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2213 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2214 (current_template_parms))))
2215 continue;
2216
2217 /* DECL might be a specialization of FN. */
2218 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2219 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2220
2221 /* For a non-static member function, we need to make sure
2222 that the const qualification is the same. Since
2223 get_bindings does not try to merge the "this" parameter,
2224 we must do the comparison explicitly. */
2225 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2226 {
2227 if (!same_type_p (TREE_VALUE (fn_arg_types),
2228 TREE_VALUE (decl_arg_types)))
2229 continue;
2230
2231 /* And the ref-qualification. */
2232 if (type_memfn_rqual (TREE_TYPE (decl))
2233 != type_memfn_rqual (TREE_TYPE (fn)))
2234 continue;
2235 }
2236
2237 /* Skip the "this" parameter and, for constructors of
2238 classes with virtual bases, the VTT parameter. A
2239 full specialization of a constructor will have a VTT
2240 parameter, but a template never will. */
2241 decl_arg_types
2242 = skip_artificial_parms_for (decl, decl_arg_types);
2243 fn_arg_types
2244 = skip_artificial_parms_for (fn, fn_arg_types);
2245
2246 /* Function templates cannot be specializations; there are
2247 no partial specializations of functions. Therefore, if
2248 the type of DECL does not match FN, there is no
2249 match.
2250
2251 Note that it should never be the case that we have both
2252 candidates added here, and for regular member functions
2253 below. */
2254 if (tsk == tsk_template)
2255 {
2256 if (compparms (fn_arg_types, decl_arg_types))
2257 candidates = tree_cons (NULL_TREE, fn, candidates);
2258 continue;
2259 }
2260
2261 /* See whether this function might be a specialization of this
2262 template. Suppress access control because we might be trying
2263 to make this specialization a friend, and we have already done
2264 access control for the declaration of the specialization. */
2265 push_deferring_access_checks (dk_no_check);
2266 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2267 pop_deferring_access_checks ();
2268
2269 if (!targs)
2270 /* We cannot deduce template arguments that when used to
2271 specialize TMPL will produce DECL. */
2272 continue;
2273
2274 if (uses_template_parms (targs))
2275 /* We deduced something involving 'auto', which isn't a valid
2276 template argument. */
2277 continue;
2278
2279 /* Remove, from the set of candidates, all those functions
2280 whose constraints are not satisfied. */
2281 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2282 continue;
2283
2284 // Then, try to form the new function type.
2285 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2286 if (insttype == error_mark_node)
2287 continue;
2288 fn_arg_types
2289 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2290 if (!compparms (fn_arg_types, decl_arg_types))
2291 continue;
2292
2293 /* Save this template, and the arguments deduced. */
2294 templates = tree_cons (targs, fn, templates);
2295 }
2296 else if (need_member_template)
2297 /* FN is an ordinary member function, and we need a
2298 specialization of a member template. */
2299 ;
2300 else if (TREE_CODE (fn) != FUNCTION_DECL)
2301 /* We can get IDENTIFIER_NODEs here in certain erroneous
2302 cases. */
2303 ;
2304 else if (!DECL_FUNCTION_MEMBER_P (fn))
2305 /* This is just an ordinary non-member function. Nothing can
2306 be a specialization of that. */
2307 ;
2308 else if (DECL_ARTIFICIAL (fn))
2309 /* Cannot specialize functions that are created implicitly. */
2310 ;
2311 else
2312 {
2313 tree decl_arg_types;
2314
2315 /* This is an ordinary member function. However, since
2316 we're here, we can assume its enclosing class is a
2317 template class. For example,
2318
2319 template <typename T> struct S { void f(); };
2320 template <> void S<int>::f() {}
2321
2322 Here, S<int>::f is a non-template, but S<int> is a
2323 template class. If FN has the same type as DECL, we
2324 might be in business. */
2325
2326 if (!DECL_TEMPLATE_INFO (fn))
2327 /* Its enclosing class is an explicit specialization
2328 of a template class. This is not a candidate. */
2329 continue;
2330
2331 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2332 TREE_TYPE (TREE_TYPE (fn))))
2333 /* The return types differ. */
2334 continue;
2335
2336 /* Adjust the type of DECL in case FN is a static member. */
2337 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2338 if (DECL_STATIC_FUNCTION_P (fn)
2339 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2340 decl_arg_types = TREE_CHAIN (decl_arg_types);
2341
2342 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2343 decl_arg_types))
2344 continue;
2345
2346 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2347 && (type_memfn_rqual (TREE_TYPE (decl))
2348 != type_memfn_rqual (TREE_TYPE (fn))))
2349 continue;
2350
2351 // If the deduced arguments do not satisfy the constraints,
2352 // this is not a candidate.
2353 if (flag_concepts && !constraints_satisfied_p (fn))
2354 continue;
2355
2356 // Add the candidate.
2357 candidates = tree_cons (NULL_TREE, fn, candidates);
2358 }
2359 }
2360
2361 if (templates && TREE_CHAIN (templates))
2362 {
2363 /* We have:
2364
2365 [temp.expl.spec]
2366
2367 It is possible for a specialization with a given function
2368 signature to be instantiated from more than one function
2369 template. In such cases, explicit specification of the
2370 template arguments must be used to uniquely identify the
2371 function template specialization being specialized.
2372
2373 Note that here, there's no suggestion that we're supposed to
2374 determine which of the candidate templates is most
2375 specialized. However, we, also have:
2376
2377 [temp.func.order]
2378
2379 Partial ordering of overloaded function template
2380 declarations is used in the following contexts to select
2381 the function template to which a function template
2382 specialization refers:
2383
2384 -- when an explicit specialization refers to a function
2385 template.
2386
2387 So, we do use the partial ordering rules, at least for now.
2388 This extension can only serve to make invalid programs valid,
2389 so it's safe. And, there is strong anecdotal evidence that
2390 the committee intended the partial ordering rules to apply;
2391 the EDG front end has that behavior, and John Spicer claims
2392 that the committee simply forgot to delete the wording in
2393 [temp.expl.spec]. */
2394 tree tmpl = most_specialized_instantiation (templates);
2395 if (tmpl != error_mark_node)
2396 {
2397 templates = tmpl;
2398 TREE_CHAIN (templates) = NULL_TREE;
2399 }
2400 }
2401
2402 // Concepts allows multiple declarations of member functions
2403 // with the same signature. Like above, we need to rely on
2404 // on the partial ordering of those candidates to determine which
2405 // is the best.
2406 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2407 {
2408 if (tree cand = most_constrained_function (candidates))
2409 {
2410 candidates = cand;
2411 TREE_CHAIN (cand) = NULL_TREE;
2412 }
2413 }
2414
2415 if (templates == NULL_TREE && candidates == NULL_TREE)
2416 {
2417 error ("template-id %qD for %q+D does not match any template "
2418 "declaration", template_id, decl);
2419 if (header_count && header_count != template_count + 1)
2420 inform (input_location, "saw %d %<template<>%>, need %d for "
2421 "specializing a member function template",
2422 header_count, template_count + 1);
2423 else
2424 print_candidates (orig_fns);
2425 return error_mark_node;
2426 }
2427 else if ((templates && TREE_CHAIN (templates))
2428 || (candidates && TREE_CHAIN (candidates))
2429 || (templates && candidates))
2430 {
2431 error ("ambiguous template specialization %qD for %q+D",
2432 template_id, decl);
2433 candidates = chainon (candidates, templates);
2434 print_candidates (candidates);
2435 return error_mark_node;
2436 }
2437
2438 /* We have one, and exactly one, match. */
2439 if (candidates)
2440 {
2441 tree fn = TREE_VALUE (candidates);
2442 *targs_out = copy_node (DECL_TI_ARGS (fn));
2443
2444 // Propagate the candidate's constraints to the declaration.
2445 set_constraints (decl, get_constraints (fn));
2446
2447 /* DECL is a re-declaration or partial instantiation of a template
2448 function. */
2449 if (TREE_CODE (fn) == TEMPLATE_DECL)
2450 return fn;
2451 /* It was a specialization of an ordinary member function in a
2452 template class. */
2453 return DECL_TI_TEMPLATE (fn);
2454 }
2455
2456 /* It was a specialization of a template. */
2457 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2458 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2459 {
2460 *targs_out = copy_node (targs);
2461 SET_TMPL_ARGS_LEVEL (*targs_out,
2462 TMPL_ARGS_DEPTH (*targs_out),
2463 TREE_PURPOSE (templates));
2464 }
2465 else
2466 *targs_out = TREE_PURPOSE (templates);
2467 return TREE_VALUE (templates);
2468 }
2469
2470 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2471 but with the default argument values filled in from those in the
2472 TMPL_TYPES. */
2473
2474 static tree
2475 copy_default_args_to_explicit_spec_1 (tree spec_types,
2476 tree tmpl_types)
2477 {
2478 tree new_spec_types;
2479
2480 if (!spec_types)
2481 return NULL_TREE;
2482
2483 if (spec_types == void_list_node)
2484 return void_list_node;
2485
2486 /* Substitute into the rest of the list. */
2487 new_spec_types =
2488 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2489 TREE_CHAIN (tmpl_types));
2490
2491 /* Add the default argument for this parameter. */
2492 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2493 TREE_VALUE (spec_types),
2494 new_spec_types);
2495 }
2496
2497 /* DECL is an explicit specialization. Replicate default arguments
2498 from the template it specializes. (That way, code like:
2499
2500 template <class T> void f(T = 3);
2501 template <> void f(double);
2502 void g () { f (); }
2503
2504 works, as required.) An alternative approach would be to look up
2505 the correct default arguments at the call-site, but this approach
2506 is consistent with how implicit instantiations are handled. */
2507
2508 static void
2509 copy_default_args_to_explicit_spec (tree decl)
2510 {
2511 tree tmpl;
2512 tree spec_types;
2513 tree tmpl_types;
2514 tree new_spec_types;
2515 tree old_type;
2516 tree new_type;
2517 tree t;
2518 tree object_type = NULL_TREE;
2519 tree in_charge = NULL_TREE;
2520 tree vtt = NULL_TREE;
2521
2522 /* See if there's anything we need to do. */
2523 tmpl = DECL_TI_TEMPLATE (decl);
2524 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2525 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2526 if (TREE_PURPOSE (t))
2527 break;
2528 if (!t)
2529 return;
2530
2531 old_type = TREE_TYPE (decl);
2532 spec_types = TYPE_ARG_TYPES (old_type);
2533
2534 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2535 {
2536 /* Remove the this pointer, but remember the object's type for
2537 CV quals. */
2538 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2539 spec_types = TREE_CHAIN (spec_types);
2540 tmpl_types = TREE_CHAIN (tmpl_types);
2541
2542 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2543 {
2544 /* DECL may contain more parameters than TMPL due to the extra
2545 in-charge parameter in constructors and destructors. */
2546 in_charge = spec_types;
2547 spec_types = TREE_CHAIN (spec_types);
2548 }
2549 if (DECL_HAS_VTT_PARM_P (decl))
2550 {
2551 vtt = spec_types;
2552 spec_types = TREE_CHAIN (spec_types);
2553 }
2554 }
2555
2556 /* Compute the merged default arguments. */
2557 new_spec_types =
2558 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2559
2560 /* Compute the new FUNCTION_TYPE. */
2561 if (object_type)
2562 {
2563 if (vtt)
2564 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2565 TREE_VALUE (vtt),
2566 new_spec_types);
2567
2568 if (in_charge)
2569 /* Put the in-charge parameter back. */
2570 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2571 TREE_VALUE (in_charge),
2572 new_spec_types);
2573
2574 new_type = build_method_type_directly (object_type,
2575 TREE_TYPE (old_type),
2576 new_spec_types);
2577 }
2578 else
2579 new_type = build_function_type (TREE_TYPE (old_type),
2580 new_spec_types);
2581 new_type = cp_build_type_attribute_variant (new_type,
2582 TYPE_ATTRIBUTES (old_type));
2583 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2584
2585 TREE_TYPE (decl) = new_type;
2586 }
2587
2588 /* Return the number of template headers we expect to see for a definition
2589 or specialization of CTYPE or one of its non-template members. */
2590
2591 int
2592 num_template_headers_for_class (tree ctype)
2593 {
2594 int num_templates = 0;
2595
2596 while (ctype && CLASS_TYPE_P (ctype))
2597 {
2598 /* You're supposed to have one `template <...>' for every
2599 template class, but you don't need one for a full
2600 specialization. For example:
2601
2602 template <class T> struct S{};
2603 template <> struct S<int> { void f(); };
2604 void S<int>::f () {}
2605
2606 is correct; there shouldn't be a `template <>' for the
2607 definition of `S<int>::f'. */
2608 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2609 /* If CTYPE does not have template information of any
2610 kind, then it is not a template, nor is it nested
2611 within a template. */
2612 break;
2613 if (explicit_class_specialization_p (ctype))
2614 break;
2615 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2616 ++num_templates;
2617
2618 ctype = TYPE_CONTEXT (ctype);
2619 }
2620
2621 return num_templates;
2622 }
2623
2624 /* Do a simple sanity check on the template headers that precede the
2625 variable declaration DECL. */
2626
2627 void
2628 check_template_variable (tree decl)
2629 {
2630 tree ctx = CP_DECL_CONTEXT (decl);
2631 int wanted = num_template_headers_for_class (ctx);
2632 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2633 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2634 {
2635 if (cxx_dialect < cxx14)
2636 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2637 "variable templates only available with "
2638 "%<-std=c++14%> or %<-std=gnu++14%>");
2639
2640 // Namespace-scope variable templates should have a template header.
2641 ++wanted;
2642 }
2643 if (template_header_count > wanted)
2644 {
2645 auto_diagnostic_group d;
2646 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2647 "too many template headers for %qD "
2648 "(should be %d)",
2649 decl, wanted);
2650 if (warned && CLASS_TYPE_P (ctx)
2651 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2652 inform (DECL_SOURCE_LOCATION (decl),
2653 "members of an explicitly specialized class are defined "
2654 "without a template header");
2655 }
2656 }
2657
2658 /* An explicit specialization whose declarator-id or class-head-name is not
2659 qualified shall be declared in the nearest enclosing namespace of the
2660 template, or, if the namespace is inline (7.3.1), any namespace from its
2661 enclosing namespace set.
2662
2663 If the name declared in the explicit instantiation is an unqualified name,
2664 the explicit instantiation shall appear in the namespace where its template
2665 is declared or, if that namespace is inline (7.3.1), any namespace from its
2666 enclosing namespace set. */
2667
2668 void
2669 check_unqualified_spec_or_inst (tree t, location_t loc)
2670 {
2671 tree tmpl = most_general_template (t);
2672 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2673 && !is_nested_namespace (current_namespace,
2674 CP_DECL_CONTEXT (tmpl), true))
2675 {
2676 if (processing_specialization)
2677 permerror (loc, "explicit specialization of %qD outside its "
2678 "namespace must use a nested-name-specifier", tmpl);
2679 else if (processing_explicit_instantiation
2680 && cxx_dialect >= cxx11)
2681 /* This was allowed in C++98, so only pedwarn. */
2682 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2683 "outside its namespace must use a nested-name-"
2684 "specifier", tmpl);
2685 }
2686 }
2687
2688 /* Warn for a template specialization SPEC that is missing some of a set
2689 of function or type attributes that the template TEMPL is declared with.
2690 ATTRLIST is a list of additional attributes that SPEC should be taken
2691 to ultimately be declared with. */
2692
2693 static void
2694 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2695 {
2696 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2697 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2698
2699 /* Avoid warning if the difference between the primary and
2700 the specialization is not in one of the attributes below. */
2701 const char* const blacklist[] = {
2702 "alloc_align", "alloc_size", "assume_aligned", "format",
2703 "format_arg", "malloc", "nonnull", NULL
2704 };
2705
2706 /* Put together a list of the black listed attributes that the primary
2707 template is declared with that the specialization is not, in case
2708 it's not apparent from the most recent declaration of the primary. */
2709 pretty_printer str;
2710 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2711 blacklist, &str);
2712
2713 if (!nattrs)
2714 return;
2715
2716 auto_diagnostic_group d;
2717 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2718 "explicit specialization %q#D may be missing attributes",
2719 spec))
2720 inform (DECL_SOURCE_LOCATION (tmpl),
2721 nattrs > 1
2722 ? G_("missing primary template attributes %s")
2723 : G_("missing primary template attribute %s"),
2724 pp_formatted_text (&str));
2725 }
2726
2727 /* Check to see if the function just declared, as indicated in
2728 DECLARATOR, and in DECL, is a specialization of a function
2729 template. We may also discover that the declaration is an explicit
2730 instantiation at this point.
2731
2732 Returns DECL, or an equivalent declaration that should be used
2733 instead if all goes well. Issues an error message if something is
2734 amiss. Returns error_mark_node if the error is not easily
2735 recoverable.
2736
2737 FLAGS is a bitmask consisting of the following flags:
2738
2739 2: The function has a definition.
2740 4: The function is a friend.
2741
2742 The TEMPLATE_COUNT is the number of references to qualifying
2743 template classes that appeared in the name of the function. For
2744 example, in
2745
2746 template <class T> struct S { void f(); };
2747 void S<int>::f();
2748
2749 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2750 classes are not counted in the TEMPLATE_COUNT, so that in
2751
2752 template <class T> struct S {};
2753 template <> struct S<int> { void f(); }
2754 template <> void S<int>::f();
2755
2756 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2757 invalid; there should be no template <>.)
2758
2759 If the function is a specialization, it is marked as such via
2760 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2761 is set up correctly, and it is added to the list of specializations
2762 for that template. */
2763
2764 tree
2765 check_explicit_specialization (tree declarator,
2766 tree decl,
2767 int template_count,
2768 int flags,
2769 tree attrlist)
2770 {
2771 int have_def = flags & 2;
2772 int is_friend = flags & 4;
2773 bool is_concept = flags & 8;
2774 int specialization = 0;
2775 int explicit_instantiation = 0;
2776 int member_specialization = 0;
2777 tree ctype = DECL_CLASS_CONTEXT (decl);
2778 tree dname = DECL_NAME (decl);
2779 tmpl_spec_kind tsk;
2780
2781 if (is_friend)
2782 {
2783 if (!processing_specialization)
2784 tsk = tsk_none;
2785 else
2786 tsk = tsk_excessive_parms;
2787 }
2788 else
2789 tsk = current_tmpl_spec_kind (template_count);
2790
2791 switch (tsk)
2792 {
2793 case tsk_none:
2794 if (processing_specialization && !VAR_P (decl))
2795 {
2796 specialization = 1;
2797 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2798 }
2799 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2800 {
2801 if (is_friend)
2802 /* This could be something like:
2803
2804 template <class T> void f(T);
2805 class S { friend void f<>(int); } */
2806 specialization = 1;
2807 else
2808 {
2809 /* This case handles bogus declarations like template <>
2810 template <class T> void f<int>(); */
2811
2812 error ("template-id %qD in declaration of primary template",
2813 declarator);
2814 return decl;
2815 }
2816 }
2817 break;
2818
2819 case tsk_invalid_member_spec:
2820 /* The error has already been reported in
2821 check_specialization_scope. */
2822 return error_mark_node;
2823
2824 case tsk_invalid_expl_inst:
2825 error ("template parameter list used in explicit instantiation");
2826
2827 /* Fall through. */
2828
2829 case tsk_expl_inst:
2830 if (have_def)
2831 error ("definition provided for explicit instantiation");
2832
2833 explicit_instantiation = 1;
2834 break;
2835
2836 case tsk_excessive_parms:
2837 case tsk_insufficient_parms:
2838 if (tsk == tsk_excessive_parms)
2839 error ("too many template parameter lists in declaration of %qD",
2840 decl);
2841 else if (template_header_count)
2842 error("too few template parameter lists in declaration of %qD", decl);
2843 else
2844 error("explicit specialization of %qD must be introduced by "
2845 "%<template <>%>", decl);
2846
2847 /* Fall through. */
2848 case tsk_expl_spec:
2849 if (is_concept)
2850 error ("explicit specialization declared %<concept%>");
2851
2852 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2853 /* In cases like template<> constexpr bool v = true;
2854 We'll give an error in check_template_variable. */
2855 break;
2856
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2858 if (ctype)
2859 member_specialization = 1;
2860 else
2861 specialization = 1;
2862 break;
2863
2864 case tsk_template:
2865 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2866 {
2867 /* This case handles bogus declarations like template <>
2868 template <class T> void f<int>(); */
2869
2870 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2871 error ("template-id %qD in declaration of primary template",
2872 declarator);
2873 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2874 {
2875 /* Partial specialization of variable template. */
2876 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2877 specialization = 1;
2878 goto ok;
2879 }
2880 else if (cxx_dialect < cxx14)
2881 error ("non-type partial specialization %qD "
2882 "is not allowed", declarator);
2883 else
2884 error ("non-class, non-variable partial specialization %qD "
2885 "is not allowed", declarator);
2886 return decl;
2887 ok:;
2888 }
2889
2890 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2891 /* This is a specialization of a member template, without
2892 specialization the containing class. Something like:
2893
2894 template <class T> struct S {
2895 template <class U> void f (U);
2896 };
2897 template <> template <class U> void S<int>::f(U) {}
2898
2899 That's a specialization -- but of the entire template. */
2900 specialization = 1;
2901 break;
2902
2903 default:
2904 gcc_unreachable ();
2905 }
2906
2907 if ((specialization || member_specialization)
2908 /* This doesn't apply to variable templates. */
2909 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2910 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2911 {
2912 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2913 for (; t; t = TREE_CHAIN (t))
2914 if (TREE_PURPOSE (t))
2915 {
2916 permerror (input_location,
2917 "default argument specified in explicit specialization");
2918 break;
2919 }
2920 }
2921
2922 if (specialization || member_specialization || explicit_instantiation)
2923 {
2924 tree tmpl = NULL_TREE;
2925 tree targs = NULL_TREE;
2926 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2927
2928 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2929 if (!was_template_id)
2930 {
2931 tree fns;
2932
2933 gcc_assert (identifier_p (declarator));
2934 if (ctype)
2935 fns = dname;
2936 else
2937 {
2938 /* If there is no class context, the explicit instantiation
2939 must be at namespace scope. */
2940 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2941
2942 /* Find the namespace binding, using the declaration
2943 context. */
2944 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2945 false, true);
2946 if (fns == error_mark_node)
2947 /* If lookup fails, look for a friend declaration so we can
2948 give a better diagnostic. */
2949 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2950 /*type*/false, /*complain*/true,
2951 /*hidden*/true);
2952
2953 if (fns == error_mark_node || !is_overloaded_fn (fns))
2954 {
2955 error ("%qD is not a template function", dname);
2956 fns = error_mark_node;
2957 }
2958 }
2959
2960 declarator = lookup_template_function (fns, NULL_TREE);
2961 }
2962
2963 if (declarator == error_mark_node)
2964 return error_mark_node;
2965
2966 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2967 {
2968 if (!explicit_instantiation)
2969 /* A specialization in class scope. This is invalid,
2970 but the error will already have been flagged by
2971 check_specialization_scope. */
2972 return error_mark_node;
2973 else
2974 {
2975 /* It's not valid to write an explicit instantiation in
2976 class scope, e.g.:
2977
2978 class C { template void f(); }
2979
2980 This case is caught by the parser. However, on
2981 something like:
2982
2983 template class C { void f(); };
2984
2985 (which is invalid) we can get here. The error will be
2986 issued later. */
2987 ;
2988 }
2989
2990 return decl;
2991 }
2992 else if (ctype != NULL_TREE
2993 && (identifier_p (TREE_OPERAND (declarator, 0))))
2994 {
2995 // We'll match variable templates in start_decl.
2996 if (VAR_P (decl))
2997 return decl;
2998
2999 /* Find the list of functions in ctype that have the same
3000 name as the declared function. */
3001 tree name = TREE_OPERAND (declarator, 0);
3002
3003 if (constructor_name_p (name, ctype))
3004 {
3005 if (DECL_CONSTRUCTOR_P (decl)
3006 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3007 : !CLASSTYPE_DESTRUCTOR (ctype))
3008 {
3009 /* From [temp.expl.spec]:
3010
3011 If such an explicit specialization for the member
3012 of a class template names an implicitly-declared
3013 special member function (clause _special_), the
3014 program is ill-formed.
3015
3016 Similar language is found in [temp.explicit]. */
3017 error ("specialization of implicitly-declared special member function");
3018 return error_mark_node;
3019 }
3020
3021 name = DECL_NAME (decl);
3022 }
3023
3024 /* For a type-conversion operator, We might be looking for
3025 `operator int' which will be a specialization of
3026 `operator T'. Grab all the conversion operators, and
3027 then select from them. */
3028 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3029 ? conv_op_identifier : name);
3030
3031 if (fns == NULL_TREE)
3032 {
3033 error ("no member function %qD declared in %qT", name, ctype);
3034 return error_mark_node;
3035 }
3036 else
3037 TREE_OPERAND (declarator, 0) = fns;
3038 }
3039
3040 /* Figure out what exactly is being specialized at this point.
3041 Note that for an explicit instantiation, even one for a
3042 member function, we cannot tell a priori whether the
3043 instantiation is for a member template, or just a member
3044 function of a template class. Even if a member template is
3045 being instantiated, the member template arguments may be
3046 elided if they can be deduced from the rest of the
3047 declaration. */
3048 tmpl = determine_specialization (declarator, decl,
3049 &targs,
3050 member_specialization,
3051 template_count,
3052 tsk);
3053
3054 if (!tmpl || tmpl == error_mark_node)
3055 /* We couldn't figure out what this declaration was
3056 specializing. */
3057 return error_mark_node;
3058 else
3059 {
3060 if (TREE_CODE (decl) == FUNCTION_DECL
3061 && DECL_HIDDEN_FRIEND_P (tmpl))
3062 {
3063 auto_diagnostic_group d;
3064 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3065 "friend declaration %qD is not visible to "
3066 "explicit specialization", tmpl))
3067 inform (DECL_SOURCE_LOCATION (tmpl),
3068 "friend declaration here");
3069 }
3070 else if (!ctype && !is_friend
3071 && CP_DECL_CONTEXT (decl) == current_namespace)
3072 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3073
3074 tree gen_tmpl = most_general_template (tmpl);
3075
3076 if (explicit_instantiation)
3077 {
3078 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3079 is done by do_decl_instantiation later. */
3080
3081 int arg_depth = TMPL_ARGS_DEPTH (targs);
3082 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3083
3084 if (arg_depth > parm_depth)
3085 {
3086 /* If TMPL is not the most general template (for
3087 example, if TMPL is a friend template that is
3088 injected into namespace scope), then there will
3089 be too many levels of TARGS. Remove some of them
3090 here. */
3091 int i;
3092 tree new_targs;
3093
3094 new_targs = make_tree_vec (parm_depth);
3095 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3096 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3097 = TREE_VEC_ELT (targs, i);
3098 targs = new_targs;
3099 }
3100
3101 return instantiate_template (tmpl, targs, tf_error);
3102 }
3103
3104 /* If we thought that the DECL was a member function, but it
3105 turns out to be specializing a static member function,
3106 make DECL a static member function as well. */
3107 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3108 && DECL_STATIC_FUNCTION_P (tmpl)
3109 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3110 revert_static_member_fn (decl);
3111
3112 /* If this is a specialization of a member template of a
3113 template class, we want to return the TEMPLATE_DECL, not
3114 the specialization of it. */
3115 if (tsk == tsk_template && !was_template_id)
3116 {
3117 tree result = DECL_TEMPLATE_RESULT (tmpl);
3118 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3119 DECL_INITIAL (result) = NULL_TREE;
3120 if (have_def)
3121 {
3122 tree parm;
3123 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3124 DECL_SOURCE_LOCATION (result)
3125 = DECL_SOURCE_LOCATION (decl);
3126 /* We want to use the argument list specified in the
3127 definition, not in the original declaration. */
3128 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3129 for (parm = DECL_ARGUMENTS (result); parm;
3130 parm = DECL_CHAIN (parm))
3131 DECL_CONTEXT (parm) = result;
3132 }
3133 return register_specialization (tmpl, gen_tmpl, targs,
3134 is_friend, 0);
3135 }
3136
3137 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3138 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3139
3140 if (was_template_id)
3141 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3142
3143 /* Inherit default function arguments from the template
3144 DECL is specializing. */
3145 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3146 copy_default_args_to_explicit_spec (decl);
3147
3148 /* This specialization has the same protection as the
3149 template it specializes. */
3150 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3151 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3152
3153 /* 7.1.1-1 [dcl.stc]
3154
3155 A storage-class-specifier shall not be specified in an
3156 explicit specialization...
3157
3158 The parser rejects these, so unless action is taken here,
3159 explicit function specializations will always appear with
3160 global linkage.
3161
3162 The action recommended by the C++ CWG in response to C++
3163 defect report 605 is to make the storage class and linkage
3164 of the explicit specialization match the templated function:
3165
3166 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3167 */
3168 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3169 {
3170 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3171 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3172
3173 /* A concept cannot be specialized. */
3174 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3175 {
3176 error ("explicit specialization of function concept %qD",
3177 gen_tmpl);
3178 return error_mark_node;
3179 }
3180
3181 /* This specialization has the same linkage and visibility as
3182 the function template it specializes. */
3183 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3184 if (! TREE_PUBLIC (decl))
3185 {
3186 DECL_INTERFACE_KNOWN (decl) = 1;
3187 DECL_NOT_REALLY_EXTERN (decl) = 1;
3188 }
3189 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3190 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3191 {
3192 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3193 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3194 }
3195 }
3196
3197 /* If DECL is a friend declaration, declared using an
3198 unqualified name, the namespace associated with DECL may
3199 have been set incorrectly. For example, in:
3200
3201 template <typename T> void f(T);
3202 namespace N {
3203 struct S { friend void f<int>(int); }
3204 }
3205
3206 we will have set the DECL_CONTEXT for the friend
3207 declaration to N, rather than to the global namespace. */
3208 if (DECL_NAMESPACE_SCOPE_P (decl))
3209 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3210
3211 if (is_friend && !have_def)
3212 /* This is not really a declaration of a specialization.
3213 It's just the name of an instantiation. But, it's not
3214 a request for an instantiation, either. */
3215 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3216 else if (TREE_CODE (decl) == FUNCTION_DECL)
3217 /* A specialization is not necessarily COMDAT. */
3218 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3219 && DECL_DECLARED_INLINE_P (decl));
3220 else if (VAR_P (decl))
3221 DECL_COMDAT (decl) = false;
3222
3223 /* If this is a full specialization, register it so that we can find
3224 it again. Partial specializations will be registered in
3225 process_partial_specialization. */
3226 if (!processing_template_decl)
3227 {
3228 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3229
3230 decl = register_specialization (decl, gen_tmpl, targs,
3231 is_friend, 0);
3232 }
3233
3234
3235 /* A 'structor should already have clones. */
3236 gcc_assert (decl == error_mark_node
3237 || variable_template_p (tmpl)
3238 || !(DECL_CONSTRUCTOR_P (decl)
3239 || DECL_DESTRUCTOR_P (decl))
3240 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3241 }
3242 }
3243
3244 return decl;
3245 }
3246
3247 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3248 parameters. These are represented in the same format used for
3249 DECL_TEMPLATE_PARMS. */
3250
3251 int
3252 comp_template_parms (const_tree parms1, const_tree parms2)
3253 {
3254 const_tree p1;
3255 const_tree p2;
3256
3257 if (parms1 == parms2)
3258 return 1;
3259
3260 for (p1 = parms1, p2 = parms2;
3261 p1 != NULL_TREE && p2 != NULL_TREE;
3262 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3263 {
3264 tree t1 = TREE_VALUE (p1);
3265 tree t2 = TREE_VALUE (p2);
3266 int i;
3267
3268 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3269 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3270
3271 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3272 return 0;
3273
3274 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3275 {
3276 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3277 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3278
3279 /* If either of the template parameters are invalid, assume
3280 they match for the sake of error recovery. */
3281 if (error_operand_p (parm1) || error_operand_p (parm2))
3282 return 1;
3283
3284 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3285 return 0;
3286
3287 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3288 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3289 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3290 continue;
3291 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3292 return 0;
3293 }
3294 }
3295
3296 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3297 /* One set of parameters has more parameters lists than the
3298 other. */
3299 return 0;
3300
3301 return 1;
3302 }
3303
3304 /* Determine whether PARM is a parameter pack. */
3305
3306 bool
3307 template_parameter_pack_p (const_tree parm)
3308 {
3309 /* Determine if we have a non-type template parameter pack. */
3310 if (TREE_CODE (parm) == PARM_DECL)
3311 return (DECL_TEMPLATE_PARM_P (parm)
3312 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3313 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3314 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3315
3316 /* If this is a list of template parameters, we could get a
3317 TYPE_DECL or a TEMPLATE_DECL. */
3318 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3319 parm = TREE_TYPE (parm);
3320
3321 /* Otherwise it must be a type template parameter. */
3322 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3323 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3324 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3325 }
3326
3327 /* Determine if T is a function parameter pack. */
3328
3329 bool
3330 function_parameter_pack_p (const_tree t)
3331 {
3332 if (t && TREE_CODE (t) == PARM_DECL)
3333 return DECL_PACK_P (t);
3334 return false;
3335 }
3336
3337 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3338 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3339
3340 tree
3341 get_function_template_decl (const_tree primary_func_tmpl_inst)
3342 {
3343 if (! primary_func_tmpl_inst
3344 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3345 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3346 return NULL;
3347
3348 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3349 }
3350
3351 /* Return true iff the function parameter PARAM_DECL was expanded
3352 from the function parameter pack PACK. */
3353
3354 bool
3355 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3356 {
3357 if (DECL_ARTIFICIAL (param_decl)
3358 || !function_parameter_pack_p (pack))
3359 return false;
3360
3361 /* The parameter pack and its pack arguments have the same
3362 DECL_PARM_INDEX. */
3363 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3364 }
3365
3366 /* Determine whether ARGS describes a variadic template args list,
3367 i.e., one that is terminated by a template argument pack. */
3368
3369 static bool
3370 template_args_variadic_p (tree args)
3371 {
3372 int nargs;
3373 tree last_parm;
3374
3375 if (args == NULL_TREE)
3376 return false;
3377
3378 args = INNERMOST_TEMPLATE_ARGS (args);
3379 nargs = TREE_VEC_LENGTH (args);
3380
3381 if (nargs == 0)
3382 return false;
3383
3384 last_parm = TREE_VEC_ELT (args, nargs - 1);
3385
3386 return ARGUMENT_PACK_P (last_parm);
3387 }
3388
3389 /* Generate a new name for the parameter pack name NAME (an
3390 IDENTIFIER_NODE) that incorporates its */
3391
3392 static tree
3393 make_ith_pack_parameter_name (tree name, int i)
3394 {
3395 /* Munge the name to include the parameter index. */
3396 #define NUMBUF_LEN 128
3397 char numbuf[NUMBUF_LEN];
3398 char* newname;
3399 int newname_len;
3400
3401 if (name == NULL_TREE)
3402 return name;
3403 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3404 newname_len = IDENTIFIER_LENGTH (name)
3405 + strlen (numbuf) + 2;
3406 newname = (char*)alloca (newname_len);
3407 snprintf (newname, newname_len,
3408 "%s#%i", IDENTIFIER_POINTER (name), i);
3409 return get_identifier (newname);
3410 }
3411
3412 /* Return true if T is a primary function, class or alias template
3413 specialization, not including the template pattern. */
3414
3415 bool
3416 primary_template_specialization_p (const_tree t)
3417 {
3418 if (!t)
3419 return false;
3420
3421 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3422 return (DECL_LANG_SPECIFIC (t)
3423 && DECL_USE_TEMPLATE (t)
3424 && DECL_TEMPLATE_INFO (t)
3425 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3426 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3427 return (CLASSTYPE_TEMPLATE_INFO (t)
3428 && CLASSTYPE_USE_TEMPLATE (t)
3429 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3430 else if (alias_template_specialization_p (t))
3431 return true;
3432 return false;
3433 }
3434
3435 /* Return true if PARM is a template template parameter. */
3436
3437 bool
3438 template_template_parameter_p (const_tree parm)
3439 {
3440 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3441 }
3442
3443 /* Return true iff PARM is a DECL representing a type template
3444 parameter. */
3445
3446 bool
3447 template_type_parameter_p (const_tree parm)
3448 {
3449 return (parm
3450 && (TREE_CODE (parm) == TYPE_DECL
3451 || TREE_CODE (parm) == TEMPLATE_DECL)
3452 && DECL_TEMPLATE_PARM_P (parm));
3453 }
3454
3455 /* Return the template parameters of T if T is a
3456 primary template instantiation, NULL otherwise. */
3457
3458 tree
3459 get_primary_template_innermost_parameters (const_tree t)
3460 {
3461 tree parms = NULL, template_info = NULL;
3462
3463 if ((template_info = get_template_info (t))
3464 && primary_template_specialization_p (t))
3465 parms = INNERMOST_TEMPLATE_PARMS
3466 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3467
3468 return parms;
3469 }
3470
3471 /* Return the template parameters of the LEVELth level from the full list
3472 of template parameters PARMS. */
3473
3474 tree
3475 get_template_parms_at_level (tree parms, int level)
3476 {
3477 tree p;
3478 if (!parms
3479 || TREE_CODE (parms) != TREE_LIST
3480 || level > TMPL_PARMS_DEPTH (parms))
3481 return NULL_TREE;
3482
3483 for (p = parms; p; p = TREE_CHAIN (p))
3484 if (TMPL_PARMS_DEPTH (p) == level)
3485 return p;
3486
3487 return NULL_TREE;
3488 }
3489
3490 /* Returns the template arguments of T if T is a template instantiation,
3491 NULL otherwise. */
3492
3493 tree
3494 get_template_innermost_arguments (const_tree t)
3495 {
3496 tree args = NULL, template_info = NULL;
3497
3498 if ((template_info = get_template_info (t))
3499 && TI_ARGS (template_info))
3500 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3501
3502 return args;
3503 }
3504
3505 /* Return the argument pack elements of T if T is a template argument pack,
3506 NULL otherwise. */
3507
3508 tree
3509 get_template_argument_pack_elems (const_tree t)
3510 {
3511 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3512 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3513 return NULL;
3514
3515 return ARGUMENT_PACK_ARGS (t);
3516 }
3517
3518 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3519 ARGUMENT_PACK_SELECT represents. */
3520
3521 static tree
3522 argument_pack_select_arg (tree t)
3523 {
3524 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3525 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3526
3527 /* If the selected argument is an expansion E, that most likely means we were
3528 called from gen_elem_of_pack_expansion_instantiation during the
3529 substituting of an argument pack (of which the Ith element is a pack
3530 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3531 In this case, the Ith element resulting from this substituting is going to
3532 be a pack expansion, which pattern is the pattern of E. Let's return the
3533 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3534 resulting pack expansion from it. */
3535 if (PACK_EXPANSION_P (arg))
3536 {
3537 /* Make sure we aren't throwing away arg info. */
3538 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3539 arg = PACK_EXPANSION_PATTERN (arg);
3540 }
3541
3542 return arg;
3543 }
3544
3545
3546 /* True iff FN is a function representing a built-in variadic parameter
3547 pack. */
3548
3549 bool
3550 builtin_pack_fn_p (tree fn)
3551 {
3552 if (!fn
3553 || TREE_CODE (fn) != FUNCTION_DECL
3554 || !DECL_IS_BUILTIN (fn))
3555 return false;
3556
3557 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3558 return true;
3559
3560 return false;
3561 }
3562
3563 /* True iff CALL is a call to a function representing a built-in variadic
3564 parameter pack. */
3565
3566 static bool
3567 builtin_pack_call_p (tree call)
3568 {
3569 if (TREE_CODE (call) != CALL_EXPR)
3570 return false;
3571 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3572 }
3573
3574 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3575
3576 static tree
3577 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3578 tree in_decl)
3579 {
3580 tree ohi = CALL_EXPR_ARG (call, 0);
3581 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3582 false/*fn*/, true/*int_cst*/);
3583
3584 if (value_dependent_expression_p (hi))
3585 {
3586 if (hi != ohi)
3587 {
3588 call = copy_node (call);
3589 CALL_EXPR_ARG (call, 0) = hi;
3590 }
3591 tree ex = make_pack_expansion (call, complain);
3592 tree vec = make_tree_vec (1);
3593 TREE_VEC_ELT (vec, 0) = ex;
3594 return vec;
3595 }
3596 else
3597 {
3598 hi = cxx_constant_value (hi);
3599 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3600
3601 /* Calculate the largest value of len that won't make the size of the vec
3602 overflow an int. The compiler will exceed resource limits long before
3603 this, but it seems a decent place to diagnose. */
3604 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3605
3606 if (len < 0 || len > max)
3607 {
3608 if ((complain & tf_error)
3609 && hi != error_mark_node)
3610 error ("argument to __integer_pack must be between 0 and %d", max);
3611 return error_mark_node;
3612 }
3613
3614 tree vec = make_tree_vec (len);
3615
3616 for (int i = 0; i < len; ++i)
3617 TREE_VEC_ELT (vec, i) = size_int (i);
3618
3619 return vec;
3620 }
3621 }
3622
3623 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3624 CALL. */
3625
3626 static tree
3627 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3628 tree in_decl)
3629 {
3630 if (!builtin_pack_call_p (call))
3631 return NULL_TREE;
3632
3633 tree fn = CALL_EXPR_FN (call);
3634
3635 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3636 return expand_integer_pack (call, args, complain, in_decl);
3637
3638 return NULL_TREE;
3639 }
3640
3641 /* Structure used to track the progress of find_parameter_packs_r. */
3642 struct find_parameter_pack_data
3643 {
3644 /* TREE_LIST that will contain all of the parameter packs found by
3645 the traversal. */
3646 tree* parameter_packs;
3647
3648 /* Set of AST nodes that have been visited by the traversal. */
3649 hash_set<tree> *visited;
3650
3651 /* True iff we're making a type pack expansion. */
3652 bool type_pack_expansion_p;
3653 };
3654
3655 /* Identifies all of the argument packs that occur in a template
3656 argument and appends them to the TREE_LIST inside DATA, which is a
3657 find_parameter_pack_data structure. This is a subroutine of
3658 make_pack_expansion and uses_parameter_packs. */
3659 static tree
3660 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3661 {
3662 tree t = *tp;
3663 struct find_parameter_pack_data* ppd =
3664 (struct find_parameter_pack_data*)data;
3665 bool parameter_pack_p = false;
3666
3667 /* Handle type aliases/typedefs. */
3668 if (TYPE_ALIAS_P (t))
3669 {
3670 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3671 cp_walk_tree (&TI_ARGS (tinfo),
3672 &find_parameter_packs_r,
3673 ppd, ppd->visited);
3674 *walk_subtrees = 0;
3675 return NULL_TREE;
3676 }
3677
3678 /* Identify whether this is a parameter pack or not. */
3679 switch (TREE_CODE (t))
3680 {
3681 case TEMPLATE_PARM_INDEX:
3682 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3683 parameter_pack_p = true;
3684 break;
3685
3686 case TEMPLATE_TYPE_PARM:
3687 t = TYPE_MAIN_VARIANT (t);
3688 /* FALLTHRU */
3689 case TEMPLATE_TEMPLATE_PARM:
3690 /* If the placeholder appears in the decl-specifier-seq of a function
3691 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3692 is a pack expansion, the invented template parameter is a template
3693 parameter pack. */
3694 if (ppd->type_pack_expansion_p && is_auto (t))
3695 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3696 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3697 parameter_pack_p = true;
3698 break;
3699
3700 case FIELD_DECL:
3701 case PARM_DECL:
3702 if (DECL_PACK_P (t))
3703 {
3704 /* We don't want to walk into the type of a PARM_DECL,
3705 because we don't want to see the type parameter pack. */
3706 *walk_subtrees = 0;
3707 parameter_pack_p = true;
3708 }
3709 break;
3710
3711 case VAR_DECL:
3712 if (DECL_PACK_P (t))
3713 {
3714 /* We don't want to walk into the type of a variadic capture proxy,
3715 because we don't want to see the type parameter pack. */
3716 *walk_subtrees = 0;
3717 parameter_pack_p = true;
3718 }
3719 else if (variable_template_specialization_p (t))
3720 {
3721 cp_walk_tree (&DECL_TI_ARGS (t),
3722 find_parameter_packs_r,
3723 ppd, ppd->visited);
3724 *walk_subtrees = 0;
3725 }
3726 break;
3727
3728 case CALL_EXPR:
3729 if (builtin_pack_call_p (t))
3730 parameter_pack_p = true;
3731 break;
3732
3733 case BASES:
3734 parameter_pack_p = true;
3735 break;
3736 default:
3737 /* Not a parameter pack. */
3738 break;
3739 }
3740
3741 if (parameter_pack_p)
3742 {
3743 /* Add this parameter pack to the list. */
3744 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3745 }
3746
3747 if (TYPE_P (t))
3748 cp_walk_tree (&TYPE_CONTEXT (t),
3749 &find_parameter_packs_r, ppd, ppd->visited);
3750
3751 /* This switch statement will return immediately if we don't find a
3752 parameter pack. */
3753 switch (TREE_CODE (t))
3754 {
3755 case TEMPLATE_PARM_INDEX:
3756 return NULL_TREE;
3757
3758 case BOUND_TEMPLATE_TEMPLATE_PARM:
3759 /* Check the template itself. */
3760 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3761 &find_parameter_packs_r, ppd, ppd->visited);
3762 /* Check the template arguments. */
3763 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3764 ppd->visited);
3765 *walk_subtrees = 0;
3766 return NULL_TREE;
3767
3768 case TEMPLATE_TYPE_PARM:
3769 case TEMPLATE_TEMPLATE_PARM:
3770 return NULL_TREE;
3771
3772 case PARM_DECL:
3773 return NULL_TREE;
3774
3775 case DECL_EXPR:
3776 /* Ignore the declaration of a capture proxy for a parameter pack. */
3777 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3778 *walk_subtrees = 0;
3779 return NULL_TREE;
3780
3781 case RECORD_TYPE:
3782 if (TYPE_PTRMEMFUNC_P (t))
3783 return NULL_TREE;
3784 /* Fall through. */
3785
3786 case UNION_TYPE:
3787 case ENUMERAL_TYPE:
3788 if (TYPE_TEMPLATE_INFO (t))
3789 cp_walk_tree (&TYPE_TI_ARGS (t),
3790 &find_parameter_packs_r, ppd, ppd->visited);
3791
3792 *walk_subtrees = 0;
3793 return NULL_TREE;
3794
3795 case TEMPLATE_DECL:
3796 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3797 return NULL_TREE;
3798 gcc_fallthrough();
3799
3800 case CONSTRUCTOR:
3801 cp_walk_tree (&TREE_TYPE (t),
3802 &find_parameter_packs_r, ppd, ppd->visited);
3803 return NULL_TREE;
3804
3805 case TYPENAME_TYPE:
3806 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3807 ppd, ppd->visited);
3808 *walk_subtrees = 0;
3809 return NULL_TREE;
3810
3811 case TYPE_PACK_EXPANSION:
3812 case EXPR_PACK_EXPANSION:
3813 *walk_subtrees = 0;
3814 return NULL_TREE;
3815
3816 case INTEGER_TYPE:
3817 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3818 ppd, ppd->visited);
3819 *walk_subtrees = 0;
3820 return NULL_TREE;
3821
3822 case IDENTIFIER_NODE:
3823 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3824 ppd->visited);
3825 *walk_subtrees = 0;
3826 return NULL_TREE;
3827
3828 case LAMBDA_EXPR:
3829 {
3830 /* Look at explicit captures. */
3831 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3832 cap; cap = TREE_CHAIN (cap))
3833 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3834 ppd->visited);
3835 /* Since we defer implicit capture, look in the parms and body. */
3836 tree fn = lambda_function (t);
3837 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3838 ppd->visited);
3839 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3840 ppd->visited);
3841 *walk_subtrees = 0;
3842 return NULL_TREE;
3843 }
3844
3845 case DECLTYPE_TYPE:
3846 {
3847 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3848 type_pack_expansion_p to false so that any placeholders
3849 within the expression don't get marked as parameter packs. */
3850 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3851 ppd->type_pack_expansion_p = false;
3852 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3853 ppd, ppd->visited);
3854 ppd->type_pack_expansion_p = type_pack_expansion_p;
3855 *walk_subtrees = 0;
3856 return NULL_TREE;
3857 }
3858
3859 case IF_STMT:
3860 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3861 ppd, ppd->visited);
3862 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3863 ppd, ppd->visited);
3864 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3865 ppd, ppd->visited);
3866 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3867 *walk_subtrees = 0;
3868 return NULL_TREE;
3869
3870 default:
3871 return NULL_TREE;
3872 }
3873
3874 return NULL_TREE;
3875 }
3876
3877 /* Determines if the expression or type T uses any parameter packs. */
3878 bool
3879 uses_parameter_packs (tree t)
3880 {
3881 tree parameter_packs = NULL_TREE;
3882 struct find_parameter_pack_data ppd;
3883 ppd.parameter_packs = &parameter_packs;
3884 ppd.visited = new hash_set<tree>;
3885 ppd.type_pack_expansion_p = false;
3886 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3887 delete ppd.visited;
3888 return parameter_packs != NULL_TREE;
3889 }
3890
3891 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3892 representation a base-class initializer into a parameter pack
3893 expansion. If all goes well, the resulting node will be an
3894 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3895 respectively. */
3896 tree
3897 make_pack_expansion (tree arg, tsubst_flags_t complain)
3898 {
3899 tree result;
3900 tree parameter_packs = NULL_TREE;
3901 bool for_types = false;
3902 struct find_parameter_pack_data ppd;
3903
3904 if (!arg || arg == error_mark_node)
3905 return arg;
3906
3907 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3908 {
3909 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3910 class initializer. In this case, the TREE_PURPOSE will be a
3911 _TYPE node (representing the base class expansion we're
3912 initializing) and the TREE_VALUE will be a TREE_LIST
3913 containing the initialization arguments.
3914
3915 The resulting expansion looks somewhat different from most
3916 expansions. Rather than returning just one _EXPANSION, we
3917 return a TREE_LIST whose TREE_PURPOSE is a
3918 TYPE_PACK_EXPANSION containing the bases that will be
3919 initialized. The TREE_VALUE will be identical to the
3920 original TREE_VALUE, which is a list of arguments that will
3921 be passed to each base. We do not introduce any new pack
3922 expansion nodes into the TREE_VALUE (although it is possible
3923 that some already exist), because the TREE_PURPOSE and
3924 TREE_VALUE all need to be expanded together with the same
3925 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3926 resulting TREE_PURPOSE will mention the parameter packs in
3927 both the bases and the arguments to the bases. */
3928 tree purpose;
3929 tree value;
3930 tree parameter_packs = NULL_TREE;
3931
3932 /* Determine which parameter packs will be used by the base
3933 class expansion. */
3934 ppd.visited = new hash_set<tree>;
3935 ppd.parameter_packs = &parameter_packs;
3936 ppd.type_pack_expansion_p = false;
3937 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3938 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3939 &ppd, ppd.visited);
3940
3941 if (parameter_packs == NULL_TREE)
3942 {
3943 if (complain & tf_error)
3944 error ("base initializer expansion %qT contains no parameter packs",
3945 arg);
3946 delete ppd.visited;
3947 return error_mark_node;
3948 }
3949
3950 if (TREE_VALUE (arg) != void_type_node)
3951 {
3952 /* Collect the sets of parameter packs used in each of the
3953 initialization arguments. */
3954 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3955 {
3956 /* Determine which parameter packs will be expanded in this
3957 argument. */
3958 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3959 &ppd, ppd.visited);
3960 }
3961 }
3962
3963 delete ppd.visited;
3964
3965 /* Create the pack expansion type for the base type. */
3966 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3967 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3968 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3969 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3970
3971 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3972 they will rarely be compared to anything. */
3973 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3974
3975 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3976 }
3977
3978 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3979 for_types = true;
3980
3981 /* Build the PACK_EXPANSION_* node. */
3982 result = for_types
3983 ? cxx_make_type (TYPE_PACK_EXPANSION)
3984 : make_node (EXPR_PACK_EXPANSION);
3985 SET_PACK_EXPANSION_PATTERN (result, arg);
3986 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3987 {
3988 /* Propagate type and const-expression information. */
3989 TREE_TYPE (result) = TREE_TYPE (arg);
3990 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3991 /* Mark this read now, since the expansion might be length 0. */
3992 mark_exp_read (arg);
3993 }
3994 else
3995 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3996 they will rarely be compared to anything. */
3997 SET_TYPE_STRUCTURAL_EQUALITY (result);
3998
3999 /* Determine which parameter packs will be expanded. */
4000 ppd.parameter_packs = &parameter_packs;
4001 ppd.visited = new hash_set<tree>;
4002 ppd.type_pack_expansion_p = TYPE_P (arg);
4003 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4004 delete ppd.visited;
4005
4006 /* Make sure we found some parameter packs. */
4007 if (parameter_packs == NULL_TREE)
4008 {
4009 if (complain & tf_error)
4010 {
4011 if (TYPE_P (arg))
4012 error ("expansion pattern %qT contains no parameter packs", arg);
4013 else
4014 error ("expansion pattern %qE contains no parameter packs", arg);
4015 }
4016 return error_mark_node;
4017 }
4018 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4019
4020 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4021
4022 return result;
4023 }
4024
4025 /* Checks T for any "bare" parameter packs, which have not yet been
4026 expanded, and issues an error if any are found. This operation can
4027 only be done on full expressions or types (e.g., an expression
4028 statement, "if" condition, etc.), because we could have expressions like:
4029
4030 foo(f(g(h(args)))...)
4031
4032 where "args" is a parameter pack. check_for_bare_parameter_packs
4033 should not be called for the subexpressions args, h(args),
4034 g(h(args)), or f(g(h(args))), because we would produce erroneous
4035 error messages.
4036
4037 Returns TRUE and emits an error if there were bare parameter packs,
4038 returns FALSE otherwise. */
4039 bool
4040 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4041 {
4042 tree parameter_packs = NULL_TREE;
4043 struct find_parameter_pack_data ppd;
4044
4045 if (!processing_template_decl || !t || t == error_mark_node)
4046 return false;
4047
4048 /* A lambda might use a parameter pack from the containing context. */
4049 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4050 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4051 return false;
4052
4053 if (TREE_CODE (t) == TYPE_DECL)
4054 t = TREE_TYPE (t);
4055
4056 ppd.parameter_packs = &parameter_packs;
4057 ppd.visited = new hash_set<tree>;
4058 ppd.type_pack_expansion_p = false;
4059 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4060 delete ppd.visited;
4061
4062 if (parameter_packs)
4063 {
4064 if (loc == UNKNOWN_LOCATION)
4065 loc = cp_expr_loc_or_loc (t, input_location);
4066 error_at (loc, "parameter packs not expanded with %<...%>:");
4067 while (parameter_packs)
4068 {
4069 tree pack = TREE_VALUE (parameter_packs);
4070 tree name = NULL_TREE;
4071
4072 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4073 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4074 name = TYPE_NAME (pack);
4075 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4076 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4077 else if (TREE_CODE (pack) == CALL_EXPR)
4078 name = DECL_NAME (CALL_EXPR_FN (pack));
4079 else
4080 name = DECL_NAME (pack);
4081
4082 if (name)
4083 inform (loc, " %qD", name);
4084 else
4085 inform (loc, " <anonymous>");
4086
4087 parameter_packs = TREE_CHAIN (parameter_packs);
4088 }
4089
4090 return true;
4091 }
4092
4093 return false;
4094 }
4095
4096 /* Expand any parameter packs that occur in the template arguments in
4097 ARGS. */
4098 tree
4099 expand_template_argument_pack (tree args)
4100 {
4101 if (args == error_mark_node)
4102 return error_mark_node;
4103
4104 tree result_args = NULL_TREE;
4105 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4106 int num_result_args = -1;
4107 int non_default_args_count = -1;
4108
4109 /* First, determine if we need to expand anything, and the number of
4110 slots we'll need. */
4111 for (in_arg = 0; in_arg < nargs; ++in_arg)
4112 {
4113 tree arg = TREE_VEC_ELT (args, in_arg);
4114 if (arg == NULL_TREE)
4115 return args;
4116 if (ARGUMENT_PACK_P (arg))
4117 {
4118 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4119 if (num_result_args < 0)
4120 num_result_args = in_arg + num_packed;
4121 else
4122 num_result_args += num_packed;
4123 }
4124 else
4125 {
4126 if (num_result_args >= 0)
4127 num_result_args++;
4128 }
4129 }
4130
4131 /* If no expansion is necessary, we're done. */
4132 if (num_result_args < 0)
4133 return args;
4134
4135 /* Expand arguments. */
4136 result_args = make_tree_vec (num_result_args);
4137 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4138 non_default_args_count =
4139 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4140 for (in_arg = 0; in_arg < nargs; ++in_arg)
4141 {
4142 tree arg = TREE_VEC_ELT (args, in_arg);
4143 if (ARGUMENT_PACK_P (arg))
4144 {
4145 tree packed = ARGUMENT_PACK_ARGS (arg);
4146 int i, num_packed = TREE_VEC_LENGTH (packed);
4147 for (i = 0; i < num_packed; ++i, ++out_arg)
4148 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4149 if (non_default_args_count > 0)
4150 non_default_args_count += num_packed - 1;
4151 }
4152 else
4153 {
4154 TREE_VEC_ELT (result_args, out_arg) = arg;
4155 ++out_arg;
4156 }
4157 }
4158 if (non_default_args_count >= 0)
4159 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4160 return result_args;
4161 }
4162
4163 /* Checks if DECL shadows a template parameter.
4164
4165 [temp.local]: A template-parameter shall not be redeclared within its
4166 scope (including nested scopes).
4167
4168 Emits an error and returns TRUE if the DECL shadows a parameter,
4169 returns FALSE otherwise. */
4170
4171 bool
4172 check_template_shadow (tree decl)
4173 {
4174 tree olddecl;
4175
4176 /* If we're not in a template, we can't possibly shadow a template
4177 parameter. */
4178 if (!current_template_parms)
4179 return true;
4180
4181 /* Figure out what we're shadowing. */
4182 decl = OVL_FIRST (decl);
4183 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4184
4185 /* If there's no previous binding for this name, we're not shadowing
4186 anything, let alone a template parameter. */
4187 if (!olddecl)
4188 return true;
4189
4190 /* If we're not shadowing a template parameter, we're done. Note
4191 that OLDDECL might be an OVERLOAD (or perhaps even an
4192 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4193 node. */
4194 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4195 return true;
4196
4197 /* We check for decl != olddecl to avoid bogus errors for using a
4198 name inside a class. We check TPFI to avoid duplicate errors for
4199 inline member templates. */
4200 if (decl == olddecl
4201 || (DECL_TEMPLATE_PARM_P (decl)
4202 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4203 return true;
4204
4205 /* Don't complain about the injected class name, as we've already
4206 complained about the class itself. */
4207 if (DECL_SELF_REFERENCE_P (decl))
4208 return false;
4209
4210 if (DECL_TEMPLATE_PARM_P (decl))
4211 error ("declaration of template parameter %q+D shadows "
4212 "template parameter", decl);
4213 else
4214 error ("declaration of %q+#D shadows template parameter", decl);
4215 inform (DECL_SOURCE_LOCATION (olddecl),
4216 "template parameter %qD declared here", olddecl);
4217 return false;
4218 }
4219
4220 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4221 ORIG_LEVEL, DECL, and TYPE. */
4222
4223 static tree
4224 build_template_parm_index (int index,
4225 int level,
4226 int orig_level,
4227 tree decl,
4228 tree type)
4229 {
4230 tree t = make_node (TEMPLATE_PARM_INDEX);
4231 TEMPLATE_PARM_IDX (t) = index;
4232 TEMPLATE_PARM_LEVEL (t) = level;
4233 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4234 TEMPLATE_PARM_DECL (t) = decl;
4235 TREE_TYPE (t) = type;
4236 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4237 TREE_READONLY (t) = TREE_READONLY (decl);
4238
4239 return t;
4240 }
4241
4242 /* Find the canonical type parameter for the given template type
4243 parameter. Returns the canonical type parameter, which may be TYPE
4244 if no such parameter existed. */
4245
4246 static tree
4247 canonical_type_parameter (tree type)
4248 {
4249 tree list;
4250 int idx = TEMPLATE_TYPE_IDX (type);
4251 if (!canonical_template_parms)
4252 vec_alloc (canonical_template_parms, idx + 1);
4253
4254 if (canonical_template_parms->length () <= (unsigned) idx)
4255 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4256
4257 list = (*canonical_template_parms)[idx];
4258 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4259 list = TREE_CHAIN (list);
4260
4261 if (list)
4262 return TREE_VALUE (list);
4263 else
4264 {
4265 (*canonical_template_parms)[idx]
4266 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4267 return type;
4268 }
4269 }
4270
4271 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4272 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4273 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4274 new one is created. */
4275
4276 static tree
4277 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4278 tsubst_flags_t complain)
4279 {
4280 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4281 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4282 != TEMPLATE_PARM_LEVEL (index) - levels)
4283 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4284 {
4285 tree orig_decl = TEMPLATE_PARM_DECL (index);
4286 tree decl, t;
4287
4288 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4289 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4290 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4291 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4292 DECL_ARTIFICIAL (decl) = 1;
4293 SET_DECL_TEMPLATE_PARM_P (decl);
4294
4295 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4296 TEMPLATE_PARM_LEVEL (index) - levels,
4297 TEMPLATE_PARM_ORIG_LEVEL (index),
4298 decl, type);
4299 TEMPLATE_PARM_DESCENDANTS (index) = t;
4300 TEMPLATE_PARM_PARAMETER_PACK (t)
4301 = TEMPLATE_PARM_PARAMETER_PACK (index);
4302
4303 /* Template template parameters need this. */
4304 if (TREE_CODE (decl) == TEMPLATE_DECL)
4305 {
4306 DECL_TEMPLATE_RESULT (decl)
4307 = build_decl (DECL_SOURCE_LOCATION (decl),
4308 TYPE_DECL, DECL_NAME (decl), type);
4309 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4310 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4311 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4312 }
4313 }
4314
4315 return TEMPLATE_PARM_DESCENDANTS (index);
4316 }
4317
4318 /* Process information from new template parameter PARM and append it
4319 to the LIST being built. This new parameter is a non-type
4320 parameter iff IS_NON_TYPE is true. This new parameter is a
4321 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4322 is in PARM_LOC. */
4323
4324 tree
4325 process_template_parm (tree list, location_t parm_loc, tree parm,
4326 bool is_non_type, bool is_parameter_pack)
4327 {
4328 tree decl = 0;
4329 int idx = 0;
4330
4331 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4332 tree defval = TREE_PURPOSE (parm);
4333 tree constr = TREE_TYPE (parm);
4334
4335 if (list)
4336 {
4337 tree p = tree_last (list);
4338
4339 if (p && TREE_VALUE (p) != error_mark_node)
4340 {
4341 p = TREE_VALUE (p);
4342 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4343 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4344 else
4345 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4346 }
4347
4348 ++idx;
4349 }
4350
4351 if (is_non_type)
4352 {
4353 parm = TREE_VALUE (parm);
4354
4355 SET_DECL_TEMPLATE_PARM_P (parm);
4356
4357 if (TREE_TYPE (parm) != error_mark_node)
4358 {
4359 /* [temp.param]
4360
4361 The top-level cv-qualifiers on the template-parameter are
4362 ignored when determining its type. */
4363 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4364 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4365 TREE_TYPE (parm) = error_mark_node;
4366 else if (uses_parameter_packs (TREE_TYPE (parm))
4367 && !is_parameter_pack
4368 /* If we're in a nested template parameter list, the template
4369 template parameter could be a parameter pack. */
4370 && processing_template_parmlist == 1)
4371 {
4372 /* This template parameter is not a parameter pack, but it
4373 should be. Complain about "bare" parameter packs. */
4374 check_for_bare_parameter_packs (TREE_TYPE (parm));
4375
4376 /* Recover by calling this a parameter pack. */
4377 is_parameter_pack = true;
4378 }
4379 }
4380
4381 /* A template parameter is not modifiable. */
4382 TREE_CONSTANT (parm) = 1;
4383 TREE_READONLY (parm) = 1;
4384 decl = build_decl (parm_loc,
4385 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4386 TREE_CONSTANT (decl) = 1;
4387 TREE_READONLY (decl) = 1;
4388 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4389 = build_template_parm_index (idx, processing_template_decl,
4390 processing_template_decl,
4391 decl, TREE_TYPE (parm));
4392
4393 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4394 = is_parameter_pack;
4395 }
4396 else
4397 {
4398 tree t;
4399 parm = TREE_VALUE (TREE_VALUE (parm));
4400
4401 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4402 {
4403 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4404 /* This is for distinguishing between real templates and template
4405 template parameters */
4406 TREE_TYPE (parm) = t;
4407 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4408 decl = parm;
4409 }
4410 else
4411 {
4412 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4413 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4414 decl = build_decl (parm_loc,
4415 TYPE_DECL, parm, t);
4416 }
4417
4418 TYPE_NAME (t) = decl;
4419 TYPE_STUB_DECL (t) = decl;
4420 parm = decl;
4421 TEMPLATE_TYPE_PARM_INDEX (t)
4422 = build_template_parm_index (idx, processing_template_decl,
4423 processing_template_decl,
4424 decl, TREE_TYPE (parm));
4425 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4426 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4427 }
4428 DECL_ARTIFICIAL (decl) = 1;
4429 SET_DECL_TEMPLATE_PARM_P (decl);
4430
4431 /* Build requirements for the type/template parameter.
4432 This must be done after SET_DECL_TEMPLATE_PARM_P or
4433 process_template_parm could fail. */
4434 tree reqs = finish_shorthand_constraint (parm, constr);
4435
4436 pushdecl (decl);
4437
4438 /* Build the parameter node linking the parameter declaration,
4439 its default argument (if any), and its constraints (if any). */
4440 parm = build_tree_list (defval, parm);
4441 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4442
4443 return chainon (list, parm);
4444 }
4445
4446 /* The end of a template parameter list has been reached. Process the
4447 tree list into a parameter vector, converting each parameter into a more
4448 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4449 as PARM_DECLs. */
4450
4451 tree
4452 end_template_parm_list (tree parms)
4453 {
4454 int nparms;
4455 tree parm, next;
4456 tree saved_parmlist = make_tree_vec (list_length (parms));
4457
4458 /* Pop the dummy parameter level and add the real one. */
4459 current_template_parms = TREE_CHAIN (current_template_parms);
4460
4461 current_template_parms
4462 = tree_cons (size_int (processing_template_decl),
4463 saved_parmlist, current_template_parms);
4464
4465 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4466 {
4467 next = TREE_CHAIN (parm);
4468 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4469 TREE_CHAIN (parm) = NULL_TREE;
4470 }
4471
4472 --processing_template_parmlist;
4473
4474 return saved_parmlist;
4475 }
4476
4477 // Explicitly indicate the end of the template parameter list. We assume
4478 // that the current template parameters have been constructed and/or
4479 // managed explicitly, as when creating new template template parameters
4480 // from a shorthand constraint.
4481 void
4482 end_template_parm_list ()
4483 {
4484 --processing_template_parmlist;
4485 }
4486
4487 /* end_template_decl is called after a template declaration is seen. */
4488
4489 void
4490 end_template_decl (void)
4491 {
4492 reset_specialization ();
4493
4494 if (! processing_template_decl)
4495 return;
4496
4497 /* This matches the pushlevel in begin_template_parm_list. */
4498 finish_scope ();
4499
4500 --processing_template_decl;
4501 current_template_parms = TREE_CHAIN (current_template_parms);
4502 }
4503
4504 /* Takes a TREE_LIST representing a template parameter and convert it
4505 into an argument suitable to be passed to the type substitution
4506 functions. Note that If the TREE_LIST contains an error_mark
4507 node, the returned argument is error_mark_node. */
4508
4509 tree
4510 template_parm_to_arg (tree t)
4511 {
4512
4513 if (t == NULL_TREE
4514 || TREE_CODE (t) != TREE_LIST)
4515 return t;
4516
4517 if (error_operand_p (TREE_VALUE (t)))
4518 return error_mark_node;
4519
4520 t = TREE_VALUE (t);
4521
4522 if (TREE_CODE (t) == TYPE_DECL
4523 || TREE_CODE (t) == TEMPLATE_DECL)
4524 {
4525 t = TREE_TYPE (t);
4526
4527 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4528 {
4529 /* Turn this argument into a TYPE_ARGUMENT_PACK
4530 with a single element, which expands T. */
4531 tree vec = make_tree_vec (1);
4532 if (CHECKING_P)
4533 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4534
4535 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4536
4537 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4538 SET_ARGUMENT_PACK_ARGS (t, vec);
4539 }
4540 }
4541 else
4542 {
4543 t = DECL_INITIAL (t);
4544
4545 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4546 {
4547 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4548 with a single element, which expands T. */
4549 tree vec = make_tree_vec (1);
4550 if (CHECKING_P)
4551 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4552
4553 t = convert_from_reference (t);
4554 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4555
4556 t = make_node (NONTYPE_ARGUMENT_PACK);
4557 SET_ARGUMENT_PACK_ARGS (t, vec);
4558 }
4559 else
4560 t = convert_from_reference (t);
4561 }
4562 return t;
4563 }
4564
4565 /* Given a single level of template parameters (a TREE_VEC), return it
4566 as a set of template arguments. */
4567
4568 static tree
4569 template_parms_level_to_args (tree parms)
4570 {
4571 tree a = copy_node (parms);
4572 TREE_TYPE (a) = NULL_TREE;
4573 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4574 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4575
4576 if (CHECKING_P)
4577 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4578
4579 return a;
4580 }
4581
4582 /* Given a set of template parameters, return them as a set of template
4583 arguments. The template parameters are represented as a TREE_VEC, in
4584 the form documented in cp-tree.h for template arguments. */
4585
4586 static tree
4587 template_parms_to_args (tree parms)
4588 {
4589 tree header;
4590 tree args = NULL_TREE;
4591 int length = TMPL_PARMS_DEPTH (parms);
4592 int l = length;
4593
4594 /* If there is only one level of template parameters, we do not
4595 create a TREE_VEC of TREE_VECs. Instead, we return a single
4596 TREE_VEC containing the arguments. */
4597 if (length > 1)
4598 args = make_tree_vec (length);
4599
4600 for (header = parms; header; header = TREE_CHAIN (header))
4601 {
4602 tree a = template_parms_level_to_args (TREE_VALUE (header));
4603
4604 if (length > 1)
4605 TREE_VEC_ELT (args, --l) = a;
4606 else
4607 args = a;
4608 }
4609
4610 return args;
4611 }
4612
4613 /* Within the declaration of a template, return the currently active
4614 template parameters as an argument TREE_VEC. */
4615
4616 static tree
4617 current_template_args (void)
4618 {
4619 return template_parms_to_args (current_template_parms);
4620 }
4621
4622 /* Update the declared TYPE by doing any lookups which were thought to be
4623 dependent, but are not now that we know the SCOPE of the declarator. */
4624
4625 tree
4626 maybe_update_decl_type (tree orig_type, tree scope)
4627 {
4628 tree type = orig_type;
4629
4630 if (type == NULL_TREE)
4631 return type;
4632
4633 if (TREE_CODE (orig_type) == TYPE_DECL)
4634 type = TREE_TYPE (type);
4635
4636 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4637 && dependent_type_p (type)
4638 /* Don't bother building up the args in this case. */
4639 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4640 {
4641 /* tsubst in the args corresponding to the template parameters,
4642 including auto if present. Most things will be unchanged, but
4643 make_typename_type and tsubst_qualified_id will resolve
4644 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4645 tree args = current_template_args ();
4646 tree auto_node = type_uses_auto (type);
4647 tree pushed;
4648 if (auto_node)
4649 {
4650 tree auto_vec = make_tree_vec (1);
4651 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4652 args = add_to_template_args (args, auto_vec);
4653 }
4654 pushed = push_scope (scope);
4655 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4656 if (pushed)
4657 pop_scope (scope);
4658 }
4659
4660 if (type == error_mark_node)
4661 return orig_type;
4662
4663 if (TREE_CODE (orig_type) == TYPE_DECL)
4664 {
4665 if (same_type_p (type, TREE_TYPE (orig_type)))
4666 type = orig_type;
4667 else
4668 type = TYPE_NAME (type);
4669 }
4670 return type;
4671 }
4672
4673 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4674 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4675 the new template is a member template. */
4676
4677 static tree
4678 build_template_decl (tree decl, tree parms, bool member_template_p)
4679 {
4680 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4681 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4682 DECL_TEMPLATE_PARMS (tmpl) = parms;
4683 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4684 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4685 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4686
4687 return tmpl;
4688 }
4689
4690 struct template_parm_data
4691 {
4692 /* The level of the template parameters we are currently
4693 processing. */
4694 int level;
4695
4696 /* The index of the specialization argument we are currently
4697 processing. */
4698 int current_arg;
4699
4700 /* An array whose size is the number of template parameters. The
4701 elements are nonzero if the parameter has been used in any one
4702 of the arguments processed so far. */
4703 int* parms;
4704
4705 /* An array whose size is the number of template arguments. The
4706 elements are nonzero if the argument makes use of template
4707 parameters of this level. */
4708 int* arg_uses_template_parms;
4709 };
4710
4711 /* Subroutine of push_template_decl used to see if each template
4712 parameter in a partial specialization is used in the explicit
4713 argument list. If T is of the LEVEL given in DATA (which is
4714 treated as a template_parm_data*), then DATA->PARMS is marked
4715 appropriately. */
4716
4717 static int
4718 mark_template_parm (tree t, void* data)
4719 {
4720 int level;
4721 int idx;
4722 struct template_parm_data* tpd = (struct template_parm_data*) data;
4723
4724 template_parm_level_and_index (t, &level, &idx);
4725
4726 if (level == tpd->level)
4727 {
4728 tpd->parms[idx] = 1;
4729 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4730 }
4731
4732 /* In C++17 the type of a non-type argument is a deduced context. */
4733 if (cxx_dialect >= cxx17
4734 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4735 for_each_template_parm (TREE_TYPE (t),
4736 &mark_template_parm,
4737 data,
4738 NULL,
4739 /*include_nondeduced_p=*/false);
4740
4741 /* Return zero so that for_each_template_parm will continue the
4742 traversal of the tree; we want to mark *every* template parm. */
4743 return 0;
4744 }
4745
4746 /* Process the partial specialization DECL. */
4747
4748 static tree
4749 process_partial_specialization (tree decl)
4750 {
4751 tree type = TREE_TYPE (decl);
4752 tree tinfo = get_template_info (decl);
4753 tree maintmpl = TI_TEMPLATE (tinfo);
4754 tree specargs = TI_ARGS (tinfo);
4755 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4756 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4757 tree inner_parms;
4758 tree inst;
4759 int nargs = TREE_VEC_LENGTH (inner_args);
4760 int ntparms;
4761 int i;
4762 bool did_error_intro = false;
4763 struct template_parm_data tpd;
4764 struct template_parm_data tpd2;
4765
4766 gcc_assert (current_template_parms);
4767
4768 /* A concept cannot be specialized. */
4769 if (flag_concepts && variable_concept_p (maintmpl))
4770 {
4771 error ("specialization of variable concept %q#D", maintmpl);
4772 return error_mark_node;
4773 }
4774
4775 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4776 ntparms = TREE_VEC_LENGTH (inner_parms);
4777
4778 /* We check that each of the template parameters given in the
4779 partial specialization is used in the argument list to the
4780 specialization. For example:
4781
4782 template <class T> struct S;
4783 template <class T> struct S<T*>;
4784
4785 The second declaration is OK because `T*' uses the template
4786 parameter T, whereas
4787
4788 template <class T> struct S<int>;
4789
4790 is no good. Even trickier is:
4791
4792 template <class T>
4793 struct S1
4794 {
4795 template <class U>
4796 struct S2;
4797 template <class U>
4798 struct S2<T>;
4799 };
4800
4801 The S2<T> declaration is actually invalid; it is a
4802 full-specialization. Of course,
4803
4804 template <class U>
4805 struct S2<T (*)(U)>;
4806
4807 or some such would have been OK. */
4808 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4809 tpd.parms = XALLOCAVEC (int, ntparms);
4810 memset (tpd.parms, 0, sizeof (int) * ntparms);
4811
4812 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4813 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4814 for (i = 0; i < nargs; ++i)
4815 {
4816 tpd.current_arg = i;
4817 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4818 &mark_template_parm,
4819 &tpd,
4820 NULL,
4821 /*include_nondeduced_p=*/false);
4822 }
4823 for (i = 0; i < ntparms; ++i)
4824 if (tpd.parms[i] == 0)
4825 {
4826 /* One of the template parms was not used in a deduced context in the
4827 specialization. */
4828 if (!did_error_intro)
4829 {
4830 error ("template parameters not deducible in "
4831 "partial specialization:");
4832 did_error_intro = true;
4833 }
4834
4835 inform (input_location, " %qD",
4836 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4837 }
4838
4839 if (did_error_intro)
4840 return error_mark_node;
4841
4842 /* [temp.class.spec]
4843
4844 The argument list of the specialization shall not be identical to
4845 the implicit argument list of the primary template. */
4846 tree main_args
4847 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4848 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4849 && (!flag_concepts
4850 || !strictly_subsumes (current_template_constraints (),
4851 get_constraints (maintmpl))))
4852 {
4853 if (!flag_concepts)
4854 error ("partial specialization %q+D does not specialize "
4855 "any template arguments; to define the primary template, "
4856 "remove the template argument list", decl);
4857 else
4858 error ("partial specialization %q+D does not specialize any "
4859 "template arguments and is not more constrained than "
4860 "the primary template; to define the primary template, "
4861 "remove the template argument list", decl);
4862 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4863 }
4864
4865 /* A partial specialization that replaces multiple parameters of the
4866 primary template with a pack expansion is less specialized for those
4867 parameters. */
4868 if (nargs < DECL_NTPARMS (maintmpl))
4869 {
4870 error ("partial specialization is not more specialized than the "
4871 "primary template because it replaces multiple parameters "
4872 "with a pack expansion");
4873 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4874 /* Avoid crash in process_partial_specialization. */
4875 return decl;
4876 }
4877
4878 /* If we aren't in a dependent class, we can actually try deduction. */
4879 else if (tpd.level == 1
4880 /* FIXME we should be able to handle a partial specialization of a
4881 partial instantiation, but currently we can't (c++/41727). */
4882 && TMPL_ARGS_DEPTH (specargs) == 1
4883 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4884 {
4885 auto_diagnostic_group d;
4886 if (permerror (input_location, "partial specialization %qD is not "
4887 "more specialized than", decl))
4888 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4889 maintmpl);
4890 }
4891
4892 /* [temp.class.spec]
4893
4894 A partially specialized non-type argument expression shall not
4895 involve template parameters of the partial specialization except
4896 when the argument expression is a simple identifier.
4897
4898 The type of a template parameter corresponding to a specialized
4899 non-type argument shall not be dependent on a parameter of the
4900 specialization.
4901
4902 Also, we verify that pack expansions only occur at the
4903 end of the argument list. */
4904 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4905 tpd2.parms = 0;
4906 for (i = 0; i < nargs; ++i)
4907 {
4908 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4909 tree arg = TREE_VEC_ELT (inner_args, i);
4910 tree packed_args = NULL_TREE;
4911 int j, len = 1;
4912
4913 if (ARGUMENT_PACK_P (arg))
4914 {
4915 /* Extract the arguments from the argument pack. We'll be
4916 iterating over these in the following loop. */
4917 packed_args = ARGUMENT_PACK_ARGS (arg);
4918 len = TREE_VEC_LENGTH (packed_args);
4919 }
4920
4921 for (j = 0; j < len; j++)
4922 {
4923 if (packed_args)
4924 /* Get the Jth argument in the parameter pack. */
4925 arg = TREE_VEC_ELT (packed_args, j);
4926
4927 if (PACK_EXPANSION_P (arg))
4928 {
4929 /* Pack expansions must come at the end of the
4930 argument list. */
4931 if ((packed_args && j < len - 1)
4932 || (!packed_args && i < nargs - 1))
4933 {
4934 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4935 error ("parameter pack argument %qE must be at the "
4936 "end of the template argument list", arg);
4937 else
4938 error ("parameter pack argument %qT must be at the "
4939 "end of the template argument list", arg);
4940 }
4941 }
4942
4943 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4944 /* We only care about the pattern. */
4945 arg = PACK_EXPANSION_PATTERN (arg);
4946
4947 if (/* These first two lines are the `non-type' bit. */
4948 !TYPE_P (arg)
4949 && TREE_CODE (arg) != TEMPLATE_DECL
4950 /* This next two lines are the `argument expression is not just a
4951 simple identifier' condition and also the `specialized
4952 non-type argument' bit. */
4953 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4954 && !(REFERENCE_REF_P (arg)
4955 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4956 {
4957 if ((!packed_args && tpd.arg_uses_template_parms[i])
4958 || (packed_args && uses_template_parms (arg)))
4959 error ("template argument %qE involves template parameter(s)",
4960 arg);
4961 else
4962 {
4963 /* Look at the corresponding template parameter,
4964 marking which template parameters its type depends
4965 upon. */
4966 tree type = TREE_TYPE (parm);
4967
4968 if (!tpd2.parms)
4969 {
4970 /* We haven't yet initialized TPD2. Do so now. */
4971 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4972 /* The number of parameters here is the number in the
4973 main template, which, as checked in the assertion
4974 above, is NARGS. */
4975 tpd2.parms = XALLOCAVEC (int, nargs);
4976 tpd2.level =
4977 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4978 }
4979
4980 /* Mark the template parameters. But this time, we're
4981 looking for the template parameters of the main
4982 template, not in the specialization. */
4983 tpd2.current_arg = i;
4984 tpd2.arg_uses_template_parms[i] = 0;
4985 memset (tpd2.parms, 0, sizeof (int) * nargs);
4986 for_each_template_parm (type,
4987 &mark_template_parm,
4988 &tpd2,
4989 NULL,
4990 /*include_nondeduced_p=*/false);
4991
4992 if (tpd2.arg_uses_template_parms [i])
4993 {
4994 /* The type depended on some template parameters.
4995 If they are fully specialized in the
4996 specialization, that's OK. */
4997 int j;
4998 int count = 0;
4999 for (j = 0; j < nargs; ++j)
5000 if (tpd2.parms[j] != 0
5001 && tpd.arg_uses_template_parms [j])
5002 ++count;
5003 if (count != 0)
5004 error_n (input_location, count,
5005 "type %qT of template argument %qE depends "
5006 "on a template parameter",
5007 "type %qT of template argument %qE depends "
5008 "on template parameters",
5009 type,
5010 arg);
5011 }
5012 }
5013 }
5014 }
5015 }
5016
5017 /* We should only get here once. */
5018 if (TREE_CODE (decl) == TYPE_DECL)
5019 gcc_assert (!COMPLETE_TYPE_P (type));
5020
5021 // Build the template decl.
5022 tree tmpl = build_template_decl (decl, current_template_parms,
5023 DECL_MEMBER_TEMPLATE_P (maintmpl));
5024 TREE_TYPE (tmpl) = type;
5025 DECL_TEMPLATE_RESULT (tmpl) = decl;
5026 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5027 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5028 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5029
5030 /* Give template template parms a DECL_CONTEXT of the template
5031 for which they are a parameter. */
5032 for (i = 0; i < ntparms; ++i)
5033 {
5034 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5035 if (TREE_CODE (parm) == TEMPLATE_DECL)
5036 DECL_CONTEXT (parm) = tmpl;
5037 }
5038
5039 if (VAR_P (decl))
5040 /* We didn't register this in check_explicit_specialization so we could
5041 wait until the constraints were set. */
5042 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5043 else
5044 associate_classtype_constraints (type);
5045
5046 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5047 = tree_cons (specargs, tmpl,
5048 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5049 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5050
5051 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5052 inst = TREE_CHAIN (inst))
5053 {
5054 tree instance = TREE_VALUE (inst);
5055 if (TYPE_P (instance)
5056 ? (COMPLETE_TYPE_P (instance)
5057 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5058 : DECL_TEMPLATE_INSTANTIATION (instance))
5059 {
5060 tree spec = most_specialized_partial_spec (instance, tf_none);
5061 tree inst_decl = (DECL_P (instance)
5062 ? instance : TYPE_NAME (instance));
5063 if (!spec)
5064 /* OK */;
5065 else if (spec == error_mark_node)
5066 permerror (input_location,
5067 "declaration of %qD ambiguates earlier template "
5068 "instantiation for %qD", decl, inst_decl);
5069 else if (TREE_VALUE (spec) == tmpl)
5070 permerror (input_location,
5071 "partial specialization of %qD after instantiation "
5072 "of %qD", decl, inst_decl);
5073 }
5074 }
5075
5076 return decl;
5077 }
5078
5079 /* PARM is a template parameter of some form; return the corresponding
5080 TEMPLATE_PARM_INDEX. */
5081
5082 static tree
5083 get_template_parm_index (tree parm)
5084 {
5085 if (TREE_CODE (parm) == PARM_DECL
5086 || TREE_CODE (parm) == CONST_DECL)
5087 parm = DECL_INITIAL (parm);
5088 else if (TREE_CODE (parm) == TYPE_DECL
5089 || TREE_CODE (parm) == TEMPLATE_DECL)
5090 parm = TREE_TYPE (parm);
5091 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5092 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5093 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5094 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5095 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5096 return parm;
5097 }
5098
5099 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5100 parameter packs used by the template parameter PARM. */
5101
5102 static void
5103 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5104 {
5105 /* A type parm can't refer to another parm. */
5106 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5107 return;
5108 else if (TREE_CODE (parm) == PARM_DECL)
5109 {
5110 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5111 ppd, ppd->visited);
5112 return;
5113 }
5114
5115 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5116
5117 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5118 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5119 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5120 }
5121
5122 /* PARM is a template parameter pack. Return any parameter packs used in
5123 its type or the type of any of its template parameters. If there are
5124 any such packs, it will be instantiated into a fixed template parameter
5125 list by partial instantiation rather than be fully deduced. */
5126
5127 tree
5128 fixed_parameter_pack_p (tree parm)
5129 {
5130 /* This can only be true in a member template. */
5131 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5132 return NULL_TREE;
5133 /* This can only be true for a parameter pack. */
5134 if (!template_parameter_pack_p (parm))
5135 return NULL_TREE;
5136 /* A type parm can't refer to another parm. */
5137 if (TREE_CODE (parm) == TYPE_DECL)
5138 return NULL_TREE;
5139
5140 tree parameter_packs = NULL_TREE;
5141 struct find_parameter_pack_data ppd;
5142 ppd.parameter_packs = &parameter_packs;
5143 ppd.visited = new hash_set<tree>;
5144 ppd.type_pack_expansion_p = false;
5145
5146 fixed_parameter_pack_p_1 (parm, &ppd);
5147
5148 delete ppd.visited;
5149 return parameter_packs;
5150 }
5151
5152 /* Check that a template declaration's use of default arguments and
5153 parameter packs is not invalid. Here, PARMS are the template
5154 parameters. IS_PRIMARY is true if DECL is the thing declared by
5155 a primary template. IS_PARTIAL is true if DECL is a partial
5156 specialization.
5157
5158 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5159 function template declaration or a friend class template
5160 declaration. In the function case, 1 indicates a declaration, 2
5161 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5162 emitted for extraneous default arguments.
5163
5164 Returns TRUE if there were no errors found, FALSE otherwise. */
5165
5166 bool
5167 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5168 bool is_partial, int is_friend_decl)
5169 {
5170 const char *msg;
5171 int last_level_to_check;
5172 tree parm_level;
5173 bool no_errors = true;
5174
5175 /* [temp.param]
5176
5177 A default template-argument shall not be specified in a
5178 function template declaration or a function template definition, nor
5179 in the template-parameter-list of the definition of a member of a
5180 class template. */
5181
5182 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5183 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5184 /* You can't have a function template declaration in a local
5185 scope, nor you can you define a member of a class template in a
5186 local scope. */
5187 return true;
5188
5189 if ((TREE_CODE (decl) == TYPE_DECL
5190 && TREE_TYPE (decl)
5191 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5192 || (TREE_CODE (decl) == FUNCTION_DECL
5193 && LAMBDA_FUNCTION_P (decl)))
5194 /* A lambda doesn't have an explicit declaration; don't complain
5195 about the parms of the enclosing class. */
5196 return true;
5197
5198 if (current_class_type
5199 && !TYPE_BEING_DEFINED (current_class_type)
5200 && DECL_LANG_SPECIFIC (decl)
5201 && DECL_DECLARES_FUNCTION_P (decl)
5202 /* If this is either a friend defined in the scope of the class
5203 or a member function. */
5204 && (DECL_FUNCTION_MEMBER_P (decl)
5205 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5206 : DECL_FRIEND_CONTEXT (decl)
5207 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5208 : false)
5209 /* And, if it was a member function, it really was defined in
5210 the scope of the class. */
5211 && (!DECL_FUNCTION_MEMBER_P (decl)
5212 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5213 /* We already checked these parameters when the template was
5214 declared, so there's no need to do it again now. This function
5215 was defined in class scope, but we're processing its body now
5216 that the class is complete. */
5217 return true;
5218
5219 /* Core issue 226 (C++0x only): the following only applies to class
5220 templates. */
5221 if (is_primary
5222 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5223 {
5224 /* [temp.param]
5225
5226 If a template-parameter has a default template-argument, all
5227 subsequent template-parameters shall have a default
5228 template-argument supplied. */
5229 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5230 {
5231 tree inner_parms = TREE_VALUE (parm_level);
5232 int ntparms = TREE_VEC_LENGTH (inner_parms);
5233 int seen_def_arg_p = 0;
5234 int i;
5235
5236 for (i = 0; i < ntparms; ++i)
5237 {
5238 tree parm = TREE_VEC_ELT (inner_parms, i);
5239
5240 if (parm == error_mark_node)
5241 continue;
5242
5243 if (TREE_PURPOSE (parm))
5244 seen_def_arg_p = 1;
5245 else if (seen_def_arg_p
5246 && !template_parameter_pack_p (TREE_VALUE (parm)))
5247 {
5248 error ("no default argument for %qD", TREE_VALUE (parm));
5249 /* For better subsequent error-recovery, we indicate that
5250 there should have been a default argument. */
5251 TREE_PURPOSE (parm) = error_mark_node;
5252 no_errors = false;
5253 }
5254 else if (!is_partial
5255 && !is_friend_decl
5256 /* Don't complain about an enclosing partial
5257 specialization. */
5258 && parm_level == parms
5259 && TREE_CODE (decl) == TYPE_DECL
5260 && i < ntparms - 1
5261 && template_parameter_pack_p (TREE_VALUE (parm))
5262 /* A fixed parameter pack will be partially
5263 instantiated into a fixed length list. */
5264 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5265 {
5266 /* A primary class template can only have one
5267 parameter pack, at the end of the template
5268 parameter list. */
5269
5270 error ("parameter pack %q+D must be at the end of the"
5271 " template parameter list", TREE_VALUE (parm));
5272
5273 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5274 = error_mark_node;
5275 no_errors = false;
5276 }
5277 }
5278 }
5279 }
5280
5281 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5282 || is_partial
5283 || !is_primary
5284 || is_friend_decl)
5285 /* For an ordinary class template, default template arguments are
5286 allowed at the innermost level, e.g.:
5287 template <class T = int>
5288 struct S {};
5289 but, in a partial specialization, they're not allowed even
5290 there, as we have in [temp.class.spec]:
5291
5292 The template parameter list of a specialization shall not
5293 contain default template argument values.
5294
5295 So, for a partial specialization, or for a function template
5296 (in C++98/C++03), we look at all of them. */
5297 ;
5298 else
5299 /* But, for a primary class template that is not a partial
5300 specialization we look at all template parameters except the
5301 innermost ones. */
5302 parms = TREE_CHAIN (parms);
5303
5304 /* Figure out what error message to issue. */
5305 if (is_friend_decl == 2)
5306 msg = G_("default template arguments may not be used in function template "
5307 "friend re-declaration");
5308 else if (is_friend_decl)
5309 msg = G_("default template arguments may not be used in template "
5310 "friend declarations");
5311 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5312 msg = G_("default template arguments may not be used in function templates "
5313 "without %<-std=c++11%> or %<-std=gnu++11%>");
5314 else if (is_partial)
5315 msg = G_("default template arguments may not be used in "
5316 "partial specializations");
5317 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5318 msg = G_("default argument for template parameter for class enclosing %qD");
5319 else
5320 /* Per [temp.param]/9, "A default template-argument shall not be
5321 specified in the template-parameter-lists of the definition of
5322 a member of a class template that appears outside of the member's
5323 class.", thus if we aren't handling a member of a class template
5324 there is no need to examine the parameters. */
5325 return true;
5326
5327 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5328 /* If we're inside a class definition, there's no need to
5329 examine the parameters to the class itself. On the one
5330 hand, they will be checked when the class is defined, and,
5331 on the other, default arguments are valid in things like:
5332 template <class T = double>
5333 struct S { template <class U> void f(U); };
5334 Here the default argument for `S' has no bearing on the
5335 declaration of `f'. */
5336 last_level_to_check = template_class_depth (current_class_type) + 1;
5337 else
5338 /* Check everything. */
5339 last_level_to_check = 0;
5340
5341 for (parm_level = parms;
5342 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5343 parm_level = TREE_CHAIN (parm_level))
5344 {
5345 tree inner_parms = TREE_VALUE (parm_level);
5346 int i;
5347 int ntparms;
5348
5349 ntparms = TREE_VEC_LENGTH (inner_parms);
5350 for (i = 0; i < ntparms; ++i)
5351 {
5352 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5353 continue;
5354
5355 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5356 {
5357 if (msg)
5358 {
5359 no_errors = false;
5360 if (is_friend_decl == 2)
5361 return no_errors;
5362
5363 error (msg, decl);
5364 msg = 0;
5365 }
5366
5367 /* Clear out the default argument so that we are not
5368 confused later. */
5369 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5370 }
5371 }
5372
5373 /* At this point, if we're still interested in issuing messages,
5374 they must apply to classes surrounding the object declared. */
5375 if (msg)
5376 msg = G_("default argument for template parameter for class "
5377 "enclosing %qD");
5378 }
5379
5380 return no_errors;
5381 }
5382
5383 /* Worker for push_template_decl_real, called via
5384 for_each_template_parm. DATA is really an int, indicating the
5385 level of the parameters we are interested in. If T is a template
5386 parameter of that level, return nonzero. */
5387
5388 static int
5389 template_parm_this_level_p (tree t, void* data)
5390 {
5391 int this_level = *(int *)data;
5392 int level;
5393
5394 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5395 level = TEMPLATE_PARM_LEVEL (t);
5396 else
5397 level = TEMPLATE_TYPE_LEVEL (t);
5398 return level == this_level;
5399 }
5400
5401 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5402 DATA is really an int, indicating the innermost outer level of parameters.
5403 If T is a template parameter of that level or further out, return
5404 nonzero. */
5405
5406 static int
5407 template_parm_outer_level (tree t, void *data)
5408 {
5409 int this_level = *(int *)data;
5410 int level;
5411
5412 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5413 level = TEMPLATE_PARM_LEVEL (t);
5414 else
5415 level = TEMPLATE_TYPE_LEVEL (t);
5416 return level <= this_level;
5417 }
5418
5419 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5420 parameters given by current_template_args, or reuses a
5421 previously existing one, if appropriate. Returns the DECL, or an
5422 equivalent one, if it is replaced via a call to duplicate_decls.
5423
5424 If IS_FRIEND is true, DECL is a friend declaration. */
5425
5426 tree
5427 push_template_decl_real (tree decl, bool is_friend)
5428 {
5429 tree tmpl;
5430 tree args;
5431 tree info;
5432 tree ctx;
5433 bool is_primary;
5434 bool is_partial;
5435 int new_template_p = 0;
5436 /* True if the template is a member template, in the sense of
5437 [temp.mem]. */
5438 bool member_template_p = false;
5439
5440 if (decl == error_mark_node || !current_template_parms)
5441 return error_mark_node;
5442
5443 /* See if this is a partial specialization. */
5444 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5445 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5446 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5447 || (VAR_P (decl)
5448 && DECL_LANG_SPECIFIC (decl)
5449 && DECL_TEMPLATE_SPECIALIZATION (decl)
5450 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5451
5452 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5453 is_friend = true;
5454
5455 if (is_friend)
5456 /* For a friend, we want the context of the friend, not
5457 the type of which it is a friend. */
5458 ctx = CP_DECL_CONTEXT (decl);
5459 else if (CP_DECL_CONTEXT (decl)
5460 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5461 /* In the case of a virtual function, we want the class in which
5462 it is defined. */
5463 ctx = CP_DECL_CONTEXT (decl);
5464 else
5465 /* Otherwise, if we're currently defining some class, the DECL
5466 is assumed to be a member of the class. */
5467 ctx = current_scope ();
5468
5469 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5470 ctx = NULL_TREE;
5471
5472 if (!DECL_CONTEXT (decl))
5473 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5474
5475 /* See if this is a primary template. */
5476 if (is_friend && ctx
5477 && uses_template_parms_level (ctx, processing_template_decl))
5478 /* A friend template that specifies a class context, i.e.
5479 template <typename T> friend void A<T>::f();
5480 is not primary. */
5481 is_primary = false;
5482 else if (TREE_CODE (decl) == TYPE_DECL
5483 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5484 is_primary = false;
5485 else
5486 is_primary = template_parm_scope_p ();
5487
5488 if (is_primary)
5489 {
5490 warning (OPT_Wtemplates, "template %qD declared", decl);
5491
5492 if (DECL_CLASS_SCOPE_P (decl))
5493 member_template_p = true;
5494 if (TREE_CODE (decl) == TYPE_DECL
5495 && anon_aggrname_p (DECL_NAME (decl)))
5496 {
5497 error ("template class without a name");
5498 return error_mark_node;
5499 }
5500 else if (TREE_CODE (decl) == FUNCTION_DECL)
5501 {
5502 if (member_template_p)
5503 {
5504 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5505 error ("member template %qD may not have virt-specifiers", decl);
5506 }
5507 if (DECL_DESTRUCTOR_P (decl))
5508 {
5509 /* [temp.mem]
5510
5511 A destructor shall not be a member template. */
5512 error ("destructor %qD declared as member template", decl);
5513 return error_mark_node;
5514 }
5515 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5516 && (!prototype_p (TREE_TYPE (decl))
5517 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5518 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5519 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5520 == void_list_node)))
5521 {
5522 /* [basic.stc.dynamic.allocation]
5523
5524 An allocation function can be a function
5525 template. ... Template allocation functions shall
5526 have two or more parameters. */
5527 error ("invalid template declaration of %qD", decl);
5528 return error_mark_node;
5529 }
5530 }
5531 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5532 && CLASS_TYPE_P (TREE_TYPE (decl)))
5533 {
5534 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5535 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5536 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5537 {
5538 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5539 if (TREE_CODE (t) == TYPE_DECL)
5540 t = TREE_TYPE (t);
5541 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5542 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5543 }
5544 }
5545 else if (TREE_CODE (decl) == TYPE_DECL
5546 && TYPE_DECL_ALIAS_P (decl))
5547 /* alias-declaration */
5548 gcc_assert (!DECL_ARTIFICIAL (decl));
5549 else if (VAR_P (decl))
5550 /* C++14 variable template. */;
5551 else
5552 {
5553 error ("template declaration of %q#D", decl);
5554 return error_mark_node;
5555 }
5556 }
5557
5558 /* Check to see that the rules regarding the use of default
5559 arguments are not being violated. We check args for a friend
5560 functions when we know whether it's a definition, introducing
5561 declaration or re-declaration. */
5562 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5563 check_default_tmpl_args (decl, current_template_parms,
5564 is_primary, is_partial, is_friend);
5565
5566 /* Ensure that there are no parameter packs in the type of this
5567 declaration that have not been expanded. */
5568 if (TREE_CODE (decl) == FUNCTION_DECL)
5569 {
5570 /* Check each of the arguments individually to see if there are
5571 any bare parameter packs. */
5572 tree type = TREE_TYPE (decl);
5573 tree arg = DECL_ARGUMENTS (decl);
5574 tree argtype = TYPE_ARG_TYPES (type);
5575
5576 while (arg && argtype)
5577 {
5578 if (!DECL_PACK_P (arg)
5579 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5580 {
5581 /* This is a PARM_DECL that contains unexpanded parameter
5582 packs. We have already complained about this in the
5583 check_for_bare_parameter_packs call, so just replace
5584 these types with ERROR_MARK_NODE. */
5585 TREE_TYPE (arg) = error_mark_node;
5586 TREE_VALUE (argtype) = error_mark_node;
5587 }
5588
5589 arg = DECL_CHAIN (arg);
5590 argtype = TREE_CHAIN (argtype);
5591 }
5592
5593 /* Check for bare parameter packs in the return type and the
5594 exception specifiers. */
5595 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5596 /* Errors were already issued, set return type to int
5597 as the frontend doesn't expect error_mark_node as
5598 the return type. */
5599 TREE_TYPE (type) = integer_type_node;
5600 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5601 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5602 }
5603 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5604 && TYPE_DECL_ALIAS_P (decl))
5605 ? DECL_ORIGINAL_TYPE (decl)
5606 : TREE_TYPE (decl)))
5607 {
5608 TREE_TYPE (decl) = error_mark_node;
5609 return error_mark_node;
5610 }
5611
5612 if (is_partial)
5613 return process_partial_specialization (decl);
5614
5615 args = current_template_args ();
5616
5617 if (!ctx
5618 || TREE_CODE (ctx) == FUNCTION_DECL
5619 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5620 || (TREE_CODE (decl) == TYPE_DECL
5621 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5622 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5623 {
5624 if (DECL_LANG_SPECIFIC (decl)
5625 && DECL_TEMPLATE_INFO (decl)
5626 && DECL_TI_TEMPLATE (decl))
5627 tmpl = DECL_TI_TEMPLATE (decl);
5628 /* If DECL is a TYPE_DECL for a class-template, then there won't
5629 be DECL_LANG_SPECIFIC. The information equivalent to
5630 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5631 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5632 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5633 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5634 {
5635 /* Since a template declaration already existed for this
5636 class-type, we must be redeclaring it here. Make sure
5637 that the redeclaration is valid. */
5638 redeclare_class_template (TREE_TYPE (decl),
5639 current_template_parms,
5640 current_template_constraints ());
5641 /* We don't need to create a new TEMPLATE_DECL; just use the
5642 one we already had. */
5643 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5644 }
5645 else
5646 {
5647 tmpl = build_template_decl (decl, current_template_parms,
5648 member_template_p);
5649 new_template_p = 1;
5650
5651 if (DECL_LANG_SPECIFIC (decl)
5652 && DECL_TEMPLATE_SPECIALIZATION (decl))
5653 {
5654 /* A specialization of a member template of a template
5655 class. */
5656 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5657 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5658 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5659 }
5660 }
5661 }
5662 else
5663 {
5664 tree a, t, current, parms;
5665 int i;
5666 tree tinfo = get_template_info (decl);
5667
5668 if (!tinfo)
5669 {
5670 error ("template definition of non-template %q#D", decl);
5671 return error_mark_node;
5672 }
5673
5674 tmpl = TI_TEMPLATE (tinfo);
5675
5676 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5677 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5678 && DECL_TEMPLATE_SPECIALIZATION (decl)
5679 && DECL_MEMBER_TEMPLATE_P (tmpl))
5680 {
5681 tree new_tmpl;
5682
5683 /* The declaration is a specialization of a member
5684 template, declared outside the class. Therefore, the
5685 innermost template arguments will be NULL, so we
5686 replace them with the arguments determined by the
5687 earlier call to check_explicit_specialization. */
5688 args = DECL_TI_ARGS (decl);
5689
5690 new_tmpl
5691 = build_template_decl (decl, current_template_parms,
5692 member_template_p);
5693 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5694 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5695 DECL_TI_TEMPLATE (decl) = new_tmpl;
5696 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5697 DECL_TEMPLATE_INFO (new_tmpl)
5698 = build_template_info (tmpl, args);
5699
5700 register_specialization (new_tmpl,
5701 most_general_template (tmpl),
5702 args,
5703 is_friend, 0);
5704 return decl;
5705 }
5706
5707 /* Make sure the template headers we got make sense. */
5708
5709 parms = DECL_TEMPLATE_PARMS (tmpl);
5710 i = TMPL_PARMS_DEPTH (parms);
5711 if (TMPL_ARGS_DEPTH (args) != i)
5712 {
5713 error ("expected %d levels of template parms for %q#D, got %d",
5714 i, decl, TMPL_ARGS_DEPTH (args));
5715 DECL_INTERFACE_KNOWN (decl) = 1;
5716 return error_mark_node;
5717 }
5718 else
5719 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5720 {
5721 a = TMPL_ARGS_LEVEL (args, i);
5722 t = INNERMOST_TEMPLATE_PARMS (parms);
5723
5724 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5725 {
5726 if (current == decl)
5727 error ("got %d template parameters for %q#D",
5728 TREE_VEC_LENGTH (a), decl);
5729 else
5730 error ("got %d template parameters for %q#T",
5731 TREE_VEC_LENGTH (a), current);
5732 error (" but %d required", TREE_VEC_LENGTH (t));
5733 /* Avoid crash in import_export_decl. */
5734 DECL_INTERFACE_KNOWN (decl) = 1;
5735 return error_mark_node;
5736 }
5737
5738 if (current == decl)
5739 current = ctx;
5740 else if (current == NULL_TREE)
5741 /* Can happen in erroneous input. */
5742 break;
5743 else
5744 current = get_containing_scope (current);
5745 }
5746
5747 /* Check that the parms are used in the appropriate qualifying scopes
5748 in the declarator. */
5749 if (!comp_template_args
5750 (TI_ARGS (tinfo),
5751 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5752 {
5753 error ("template arguments to %qD do not match original "
5754 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5755 if (!uses_template_parms (TI_ARGS (tinfo)))
5756 inform (input_location, "use %<template<>%> for"
5757 " an explicit specialization");
5758 /* Avoid crash in import_export_decl. */
5759 DECL_INTERFACE_KNOWN (decl) = 1;
5760 return error_mark_node;
5761 }
5762 }
5763
5764 DECL_TEMPLATE_RESULT (tmpl) = decl;
5765 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5766
5767 /* Push template declarations for global functions and types. Note
5768 that we do not try to push a global template friend declared in a
5769 template class; such a thing may well depend on the template
5770 parameters of the class. */
5771 if (new_template_p && !ctx
5772 && !(is_friend && template_class_depth (current_class_type) > 0))
5773 {
5774 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5775 if (tmpl == error_mark_node)
5776 return error_mark_node;
5777
5778 /* Hide template friend classes that haven't been declared yet. */
5779 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5780 {
5781 DECL_ANTICIPATED (tmpl) = 1;
5782 DECL_FRIEND_P (tmpl) = 1;
5783 }
5784 }
5785
5786 if (is_primary)
5787 {
5788 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5789
5790 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5791
5792 /* Give template template parms a DECL_CONTEXT of the template
5793 for which they are a parameter. */
5794 parms = INNERMOST_TEMPLATE_PARMS (parms);
5795 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5796 {
5797 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5798 if (TREE_CODE (parm) == TEMPLATE_DECL)
5799 DECL_CONTEXT (parm) = tmpl;
5800 }
5801
5802 if (TREE_CODE (decl) == TYPE_DECL
5803 && TYPE_DECL_ALIAS_P (decl)
5804 && complex_alias_template_p (tmpl))
5805 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5806 }
5807
5808 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5809 back to its most general template. If TMPL is a specialization,
5810 ARGS may only have the innermost set of arguments. Add the missing
5811 argument levels if necessary. */
5812 if (DECL_TEMPLATE_INFO (tmpl))
5813 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5814
5815 info = build_template_info (tmpl, args);
5816
5817 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5818 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5819 else
5820 {
5821 if (is_primary)
5822 retrofit_lang_decl (decl);
5823 if (DECL_LANG_SPECIFIC (decl))
5824 DECL_TEMPLATE_INFO (decl) = info;
5825 }
5826
5827 if (flag_implicit_templates
5828 && !is_friend
5829 && TREE_PUBLIC (decl)
5830 && VAR_OR_FUNCTION_DECL_P (decl))
5831 /* Set DECL_COMDAT on template instantiations; if we force
5832 them to be emitted by explicit instantiation or -frepo,
5833 mark_needed will tell cgraph to do the right thing. */
5834 DECL_COMDAT (decl) = true;
5835
5836 return DECL_TEMPLATE_RESULT (tmpl);
5837 }
5838
5839 tree
5840 push_template_decl (tree decl)
5841 {
5842 return push_template_decl_real (decl, false);
5843 }
5844
5845 /* FN is an inheriting constructor that inherits from the constructor
5846 template INHERITED; turn FN into a constructor template with a matching
5847 template header. */
5848
5849 tree
5850 add_inherited_template_parms (tree fn, tree inherited)
5851 {
5852 tree inner_parms
5853 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5854 inner_parms = copy_node (inner_parms);
5855 tree parms
5856 = tree_cons (size_int (processing_template_decl + 1),
5857 inner_parms, current_template_parms);
5858 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5859 tree args = template_parms_to_args (parms);
5860 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5861 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5862 DECL_TEMPLATE_RESULT (tmpl) = fn;
5863 DECL_ARTIFICIAL (tmpl) = true;
5864 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5865 return tmpl;
5866 }
5867
5868 /* Called when a class template TYPE is redeclared with the indicated
5869 template PARMS, e.g.:
5870
5871 template <class T> struct S;
5872 template <class T> struct S {}; */
5873
5874 bool
5875 redeclare_class_template (tree type, tree parms, tree cons)
5876 {
5877 tree tmpl;
5878 tree tmpl_parms;
5879 int i;
5880
5881 if (!TYPE_TEMPLATE_INFO (type))
5882 {
5883 error ("%qT is not a template type", type);
5884 return false;
5885 }
5886
5887 tmpl = TYPE_TI_TEMPLATE (type);
5888 if (!PRIMARY_TEMPLATE_P (tmpl))
5889 /* The type is nested in some template class. Nothing to worry
5890 about here; there are no new template parameters for the nested
5891 type. */
5892 return true;
5893
5894 if (!parms)
5895 {
5896 error ("template specifiers not specified in declaration of %qD",
5897 tmpl);
5898 return false;
5899 }
5900
5901 parms = INNERMOST_TEMPLATE_PARMS (parms);
5902 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5903
5904 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5905 {
5906 error_n (input_location, TREE_VEC_LENGTH (parms),
5907 "redeclared with %d template parameter",
5908 "redeclared with %d template parameters",
5909 TREE_VEC_LENGTH (parms));
5910 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5911 "previous declaration %qD used %d template parameter",
5912 "previous declaration %qD used %d template parameters",
5913 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5914 return false;
5915 }
5916
5917 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5918 {
5919 tree tmpl_parm;
5920 tree parm;
5921 tree tmpl_default;
5922 tree parm_default;
5923
5924 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5925 || TREE_VEC_ELT (parms, i) == error_mark_node)
5926 continue;
5927
5928 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5929 if (error_operand_p (tmpl_parm))
5930 return false;
5931
5932 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5933 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5934 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5935
5936 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5937 TEMPLATE_DECL. */
5938 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5939 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5940 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5941 || (TREE_CODE (tmpl_parm) != PARM_DECL
5942 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5943 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5944 || (TREE_CODE (tmpl_parm) == PARM_DECL
5945 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5946 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5947 {
5948 error ("template parameter %q+#D", tmpl_parm);
5949 error ("redeclared here as %q#D", parm);
5950 return false;
5951 }
5952
5953 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5954 {
5955 /* We have in [temp.param]:
5956
5957 A template-parameter may not be given default arguments
5958 by two different declarations in the same scope. */
5959 error_at (input_location, "redefinition of default argument for %q#D", parm);
5960 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5961 "original definition appeared here");
5962 return false;
5963 }
5964
5965 if (parm_default != NULL_TREE)
5966 /* Update the previous template parameters (which are the ones
5967 that will really count) with the new default value. */
5968 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5969 else if (tmpl_default != NULL_TREE)
5970 /* Update the new parameters, too; they'll be used as the
5971 parameters for any members. */
5972 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5973
5974 /* Give each template template parm in this redeclaration a
5975 DECL_CONTEXT of the template for which they are a parameter. */
5976 if (TREE_CODE (parm) == TEMPLATE_DECL)
5977 {
5978 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5979 DECL_CONTEXT (parm) = tmpl;
5980 }
5981
5982 if (TREE_CODE (parm) == TYPE_DECL)
5983 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5984 }
5985
5986 // Cannot redeclare a class template with a different set of constraints.
5987 if (!equivalent_constraints (get_constraints (tmpl), cons))
5988 {
5989 error_at (input_location, "redeclaration %q#D with different "
5990 "constraints", tmpl);
5991 inform (DECL_SOURCE_LOCATION (tmpl),
5992 "original declaration appeared here");
5993 }
5994
5995 return true;
5996 }
5997
5998 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5999 to be used when the caller has already checked
6000 (processing_template_decl
6001 && !instantiation_dependent_expression_p (expr)
6002 && potential_constant_expression (expr))
6003 and cleared processing_template_decl. */
6004
6005 tree
6006 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6007 {
6008 return tsubst_copy_and_build (expr,
6009 /*args=*/NULL_TREE,
6010 complain,
6011 /*in_decl=*/NULL_TREE,
6012 /*function_p=*/false,
6013 /*integral_constant_expression_p=*/true);
6014 }
6015
6016 /* Simplify EXPR if it is a non-dependent expression. Returns the
6017 (possibly simplified) expression. */
6018
6019 tree
6020 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6021 {
6022 if (expr == NULL_TREE)
6023 return NULL_TREE;
6024
6025 /* If we're in a template, but EXPR isn't value dependent, simplify
6026 it. We're supposed to treat:
6027
6028 template <typename T> void f(T[1 + 1]);
6029 template <typename T> void f(T[2]);
6030
6031 as two declarations of the same function, for example. */
6032 if (processing_template_decl
6033 && is_nondependent_constant_expression (expr))
6034 {
6035 processing_template_decl_sentinel s;
6036 expr = instantiate_non_dependent_expr_internal (expr, complain);
6037 }
6038 return expr;
6039 }
6040
6041 tree
6042 instantiate_non_dependent_expr (tree expr)
6043 {
6044 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6045 }
6046
6047 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6048 an uninstantiated expression. */
6049
6050 tree
6051 instantiate_non_dependent_or_null (tree expr)
6052 {
6053 if (expr == NULL_TREE)
6054 return NULL_TREE;
6055 if (processing_template_decl)
6056 {
6057 if (!is_nondependent_constant_expression (expr))
6058 expr = NULL_TREE;
6059 else
6060 {
6061 processing_template_decl_sentinel s;
6062 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6063 }
6064 }
6065 return expr;
6066 }
6067
6068 /* True iff T is a specialization of a variable template. */
6069
6070 bool
6071 variable_template_specialization_p (tree t)
6072 {
6073 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6074 return false;
6075 tree tmpl = DECL_TI_TEMPLATE (t);
6076 return variable_template_p (tmpl);
6077 }
6078
6079 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6080 template declaration, or a TYPE_DECL for an alias declaration. */
6081
6082 bool
6083 alias_type_or_template_p (tree t)
6084 {
6085 if (t == NULL_TREE)
6086 return false;
6087 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6088 || (TYPE_P (t)
6089 && TYPE_NAME (t)
6090 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6091 || DECL_ALIAS_TEMPLATE_P (t));
6092 }
6093
6094 /* Return TRUE iff T is a specialization of an alias template. */
6095
6096 bool
6097 alias_template_specialization_p (const_tree t)
6098 {
6099 /* It's an alias template specialization if it's an alias and its
6100 TYPE_NAME is a specialization of a primary template. */
6101 if (TYPE_ALIAS_P (t))
6102 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6103 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6104
6105 return false;
6106 }
6107
6108 /* An alias template is complex from a SFINAE perspective if a template-id
6109 using that alias can be ill-formed when the expansion is not, as with
6110 the void_t template. We determine this by checking whether the
6111 expansion for the alias template uses all its template parameters. */
6112
6113 struct uses_all_template_parms_data
6114 {
6115 int level;
6116 bool *seen;
6117 };
6118
6119 static int
6120 uses_all_template_parms_r (tree t, void *data_)
6121 {
6122 struct uses_all_template_parms_data &data
6123 = *(struct uses_all_template_parms_data*)data_;
6124 tree idx = get_template_parm_index (t);
6125
6126 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6127 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6128 return 0;
6129 }
6130
6131 static bool
6132 complex_alias_template_p (const_tree tmpl)
6133 {
6134 struct uses_all_template_parms_data data;
6135 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6136 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6137 data.level = TMPL_PARMS_DEPTH (parms);
6138 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6139 data.seen = XALLOCAVEC (bool, len);
6140 for (int i = 0; i < len; ++i)
6141 data.seen[i] = false;
6142
6143 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6144 for (int i = 0; i < len; ++i)
6145 if (!data.seen[i])
6146 return true;
6147 return false;
6148 }
6149
6150 /* Return TRUE iff T is a specialization of a complex alias template with
6151 dependent template-arguments. */
6152
6153 bool
6154 dependent_alias_template_spec_p (const_tree t)
6155 {
6156 if (!alias_template_specialization_p (t))
6157 return false;
6158
6159 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6160 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6161 return false;
6162
6163 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6164 if (!any_dependent_template_arguments_p (args))
6165 return false;
6166
6167 return true;
6168 }
6169
6170 /* Return the number of innermost template parameters in TMPL. */
6171
6172 static int
6173 num_innermost_template_parms (tree tmpl)
6174 {
6175 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6176 return TREE_VEC_LENGTH (parms);
6177 }
6178
6179 /* Return either TMPL or another template that it is equivalent to under DR
6180 1286: An alias that just changes the name of a template is equivalent to
6181 the other template. */
6182
6183 static tree
6184 get_underlying_template (tree tmpl)
6185 {
6186 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6187 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6188 {
6189 /* Determine if the alias is equivalent to an underlying template. */
6190 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6191 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6192 if (!tinfo)
6193 break;
6194
6195 tree underlying = TI_TEMPLATE (tinfo);
6196 if (!PRIMARY_TEMPLATE_P (underlying)
6197 || (num_innermost_template_parms (tmpl)
6198 != num_innermost_template_parms (underlying)))
6199 break;
6200
6201 tree alias_args = INNERMOST_TEMPLATE_ARGS
6202 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6203 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6204 break;
6205
6206 /* Alias is equivalent. Strip it and repeat. */
6207 tmpl = underlying;
6208 }
6209
6210 return tmpl;
6211 }
6212
6213 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6214 must be a reference-to-function or a pointer-to-function type, as specified
6215 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6216 and check that the resulting function has external linkage. */
6217
6218 static tree
6219 convert_nontype_argument_function (tree type, tree expr,
6220 tsubst_flags_t complain)
6221 {
6222 tree fns = expr;
6223 tree fn, fn_no_ptr;
6224 linkage_kind linkage;
6225
6226 fn = instantiate_type (type, fns, tf_none);
6227 if (fn == error_mark_node)
6228 return error_mark_node;
6229
6230 if (value_dependent_expression_p (fn))
6231 goto accept;
6232
6233 fn_no_ptr = strip_fnptr_conv (fn);
6234 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6235 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6236 if (BASELINK_P (fn_no_ptr))
6237 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6238
6239 /* [temp.arg.nontype]/1
6240
6241 A template-argument for a non-type, non-template template-parameter
6242 shall be one of:
6243 [...]
6244 -- the address of an object or function with external [C++11: or
6245 internal] linkage. */
6246
6247 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6248 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6249 {
6250 if (complain & tf_error)
6251 {
6252 error ("%qE is not a valid template argument for type %qT",
6253 expr, type);
6254 if (TYPE_PTR_P (type))
6255 inform (input_location, "it must be the address of a function "
6256 "with external linkage");
6257 else
6258 inform (input_location, "it must be the name of a function with "
6259 "external linkage");
6260 }
6261 return NULL_TREE;
6262 }
6263
6264 linkage = decl_linkage (fn_no_ptr);
6265 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6266 {
6267 if (complain & tf_error)
6268 {
6269 if (cxx_dialect >= cxx11)
6270 error ("%qE is not a valid template argument for type %qT "
6271 "because %qD has no linkage",
6272 expr, type, fn_no_ptr);
6273 else
6274 error ("%qE is not a valid template argument for type %qT "
6275 "because %qD does not have external linkage",
6276 expr, type, fn_no_ptr);
6277 }
6278 return NULL_TREE;
6279 }
6280
6281 accept:
6282 if (TYPE_REF_P (type))
6283 {
6284 if (REFERENCE_REF_P (fn))
6285 fn = TREE_OPERAND (fn, 0);
6286 else
6287 fn = build_address (fn);
6288 }
6289 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6290 fn = build_nop (type, fn);
6291
6292 return fn;
6293 }
6294
6295 /* Subroutine of convert_nontype_argument.
6296 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6297 Emit an error otherwise. */
6298
6299 static bool
6300 check_valid_ptrmem_cst_expr (tree type, tree expr,
6301 tsubst_flags_t complain)
6302 {
6303 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6304 tree orig_expr = expr;
6305 STRIP_NOPS (expr);
6306 if (null_ptr_cst_p (expr))
6307 return true;
6308 if (TREE_CODE (expr) == PTRMEM_CST
6309 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6310 PTRMEM_CST_CLASS (expr)))
6311 return true;
6312 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6313 return true;
6314 if (processing_template_decl
6315 && TREE_CODE (expr) == ADDR_EXPR
6316 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6317 return true;
6318 if (complain & tf_error)
6319 {
6320 error_at (loc, "%qE is not a valid template argument for type %qT",
6321 orig_expr, type);
6322 if (TREE_CODE (expr) != PTRMEM_CST)
6323 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6324 else
6325 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6326 }
6327 return false;
6328 }
6329
6330 /* Returns TRUE iff the address of OP is value-dependent.
6331
6332 14.6.2.4 [temp.dep.temp]:
6333 A non-integral non-type template-argument is dependent if its type is
6334 dependent or it has either of the following forms
6335 qualified-id
6336 & qualified-id
6337 and contains a nested-name-specifier which specifies a class-name that
6338 names a dependent type.
6339
6340 We generalize this to just say that the address of a member of a
6341 dependent class is value-dependent; the above doesn't cover the
6342 address of a static data member named with an unqualified-id. */
6343
6344 static bool
6345 has_value_dependent_address (tree op)
6346 {
6347 /* We could use get_inner_reference here, but there's no need;
6348 this is only relevant for template non-type arguments, which
6349 can only be expressed as &id-expression. */
6350 if (DECL_P (op))
6351 {
6352 tree ctx = CP_DECL_CONTEXT (op);
6353 if (TYPE_P (ctx) && dependent_type_p (ctx))
6354 return true;
6355 }
6356
6357 return false;
6358 }
6359
6360 /* The next set of functions are used for providing helpful explanatory
6361 diagnostics for failed overload resolution. Their messages should be
6362 indented by two spaces for consistency with the messages in
6363 call.c */
6364
6365 static int
6366 unify_success (bool /*explain_p*/)
6367 {
6368 return 0;
6369 }
6370
6371 /* Other failure functions should call this one, to provide a single function
6372 for setting a breakpoint on. */
6373
6374 static int
6375 unify_invalid (bool /*explain_p*/)
6376 {
6377 return 1;
6378 }
6379
6380 static int
6381 unify_parameter_deduction_failure (bool explain_p, tree parm)
6382 {
6383 if (explain_p)
6384 inform (input_location,
6385 " couldn%'t deduce template parameter %qD", parm);
6386 return unify_invalid (explain_p);
6387 }
6388
6389 static int
6390 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6391 {
6392 if (explain_p)
6393 inform (input_location,
6394 " types %qT and %qT have incompatible cv-qualifiers",
6395 parm, arg);
6396 return unify_invalid (explain_p);
6397 }
6398
6399 static int
6400 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6401 {
6402 if (explain_p)
6403 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6404 return unify_invalid (explain_p);
6405 }
6406
6407 static int
6408 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6409 {
6410 if (explain_p)
6411 inform (input_location,
6412 " template parameter %qD is not a parameter pack, but "
6413 "argument %qD is",
6414 parm, arg);
6415 return unify_invalid (explain_p);
6416 }
6417
6418 static int
6419 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6420 {
6421 if (explain_p)
6422 inform (input_location,
6423 " template argument %qE does not match "
6424 "pointer-to-member constant %qE",
6425 arg, parm);
6426 return unify_invalid (explain_p);
6427 }
6428
6429 static int
6430 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6431 {
6432 if (explain_p)
6433 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6434 return unify_invalid (explain_p);
6435 }
6436
6437 static int
6438 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6439 {
6440 if (explain_p)
6441 inform (input_location,
6442 " inconsistent parameter pack deduction with %qT and %qT",
6443 old_arg, new_arg);
6444 return unify_invalid (explain_p);
6445 }
6446
6447 static int
6448 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6449 {
6450 if (explain_p)
6451 {
6452 if (TYPE_P (parm))
6453 inform (input_location,
6454 " deduced conflicting types for parameter %qT (%qT and %qT)",
6455 parm, first, second);
6456 else
6457 inform (input_location,
6458 " deduced conflicting values for non-type parameter "
6459 "%qE (%qE and %qE)", parm, first, second);
6460 }
6461 return unify_invalid (explain_p);
6462 }
6463
6464 static int
6465 unify_vla_arg (bool explain_p, tree arg)
6466 {
6467 if (explain_p)
6468 inform (input_location,
6469 " variable-sized array type %qT is not "
6470 "a valid template argument",
6471 arg);
6472 return unify_invalid (explain_p);
6473 }
6474
6475 static int
6476 unify_method_type_error (bool explain_p, tree arg)
6477 {
6478 if (explain_p)
6479 inform (input_location,
6480 " member function type %qT is not a valid template argument",
6481 arg);
6482 return unify_invalid (explain_p);
6483 }
6484
6485 static int
6486 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6487 {
6488 if (explain_p)
6489 {
6490 if (least_p)
6491 inform_n (input_location, wanted,
6492 " candidate expects at least %d argument, %d provided",
6493 " candidate expects at least %d arguments, %d provided",
6494 wanted, have);
6495 else
6496 inform_n (input_location, wanted,
6497 " candidate expects %d argument, %d provided",
6498 " candidate expects %d arguments, %d provided",
6499 wanted, have);
6500 }
6501 return unify_invalid (explain_p);
6502 }
6503
6504 static int
6505 unify_too_many_arguments (bool explain_p, int have, int wanted)
6506 {
6507 return unify_arity (explain_p, have, wanted);
6508 }
6509
6510 static int
6511 unify_too_few_arguments (bool explain_p, int have, int wanted,
6512 bool least_p = false)
6513 {
6514 return unify_arity (explain_p, have, wanted, least_p);
6515 }
6516
6517 static int
6518 unify_arg_conversion (bool explain_p, tree to_type,
6519 tree from_type, tree arg)
6520 {
6521 if (explain_p)
6522 inform (cp_expr_loc_or_loc (arg, input_location),
6523 " cannot convert %qE (type %qT) to type %qT",
6524 arg, from_type, to_type);
6525 return unify_invalid (explain_p);
6526 }
6527
6528 static int
6529 unify_no_common_base (bool explain_p, enum template_base_result r,
6530 tree parm, tree arg)
6531 {
6532 if (explain_p)
6533 switch (r)
6534 {
6535 case tbr_ambiguous_baseclass:
6536 inform (input_location, " %qT is an ambiguous base class of %qT",
6537 parm, arg);
6538 break;
6539 default:
6540 inform (input_location, " %qT is not derived from %qT", arg, parm);
6541 break;
6542 }
6543 return unify_invalid (explain_p);
6544 }
6545
6546 static int
6547 unify_inconsistent_template_template_parameters (bool explain_p)
6548 {
6549 if (explain_p)
6550 inform (input_location,
6551 " template parameters of a template template argument are "
6552 "inconsistent with other deduced template arguments");
6553 return unify_invalid (explain_p);
6554 }
6555
6556 static int
6557 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6558 {
6559 if (explain_p)
6560 inform (input_location,
6561 " can%'t deduce a template for %qT from non-template type %qT",
6562 parm, arg);
6563 return unify_invalid (explain_p);
6564 }
6565
6566 static int
6567 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6568 {
6569 if (explain_p)
6570 inform (input_location,
6571 " template argument %qE does not match %qE", arg, parm);
6572 return unify_invalid (explain_p);
6573 }
6574
6575 /* True if T is a C++20 template parameter object to store the argument for a
6576 template parameter of class type. */
6577
6578 bool
6579 template_parm_object_p (const_tree t)
6580 {
6581 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6582 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6583 }
6584
6585 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6586 argument for TYPE, points to an unsuitable object. */
6587
6588 static bool
6589 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6590 {
6591 switch (TREE_CODE (expr))
6592 {
6593 CASE_CONVERT:
6594 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6595 complain);
6596
6597 case TARGET_EXPR:
6598 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6599 complain);
6600
6601 case CONSTRUCTOR:
6602 {
6603 unsigned i; tree elt;
6604 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6605 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6606 return true;
6607 }
6608 break;
6609
6610 case ADDR_EXPR:
6611 {
6612 tree decl = TREE_OPERAND (expr, 0);
6613
6614 if (!VAR_P (decl))
6615 {
6616 if (complain & tf_error)
6617 error ("%qE is not a valid template argument of type %qT "
6618 "because %qE is not a variable", expr, type, decl);
6619 return true;
6620 }
6621 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6622 {
6623 if (complain & tf_error)
6624 error ("%qE is not a valid template argument of type %qT "
6625 "in C++98 because %qD does not have external linkage",
6626 expr, type, decl);
6627 return true;
6628 }
6629 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6630 && decl_linkage (decl) == lk_none)
6631 {
6632 if (complain & tf_error)
6633 error ("%qE is not a valid template argument of type %qT "
6634 "because %qD has no linkage", expr, type, decl);
6635 return true;
6636 }
6637 /* C++17: For a non-type template-parameter of reference or pointer
6638 type, the value of the constant expression shall not refer to (or
6639 for a pointer type, shall not be the address of):
6640 * a subobject (4.5),
6641 * a temporary object (15.2),
6642 * a string literal (5.13.5),
6643 * the result of a typeid expression (8.2.8), or
6644 * a predefined __func__ variable (11.4.1). */
6645 else if (DECL_ARTIFICIAL (decl))
6646 {
6647 if (complain & tf_error)
6648 error ("the address of %qD is not a valid template argument",
6649 decl);
6650 return true;
6651 }
6652 else if (!same_type_ignoring_top_level_qualifiers_p
6653 (strip_array_types (TREE_TYPE (type)),
6654 strip_array_types (TREE_TYPE (decl))))
6655 {
6656 if (complain & tf_error)
6657 error ("the address of the %qT subobject of %qD is not a "
6658 "valid template argument", TREE_TYPE (type), decl);
6659 return true;
6660 }
6661 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6662 {
6663 if (complain & tf_error)
6664 error ("the address of %qD is not a valid template argument "
6665 "because it does not have static storage duration",
6666 decl);
6667 return true;
6668 }
6669 }
6670 break;
6671
6672 default:
6673 if (!INDIRECT_TYPE_P (type))
6674 /* We're only concerned about pointers and references here. */;
6675 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6676 /* Null pointer values are OK in C++11. */;
6677 else
6678 {
6679 if (VAR_P (expr))
6680 {
6681 if (complain & tf_error)
6682 error ("%qD is not a valid template argument "
6683 "because %qD is a variable, not the address of "
6684 "a variable", expr, expr);
6685 return true;
6686 }
6687 else
6688 {
6689 if (complain & tf_error)
6690 error ("%qE is not a valid template argument for %qT "
6691 "because it is not the address of a variable",
6692 expr, type);
6693 return true;
6694 }
6695 }
6696 }
6697 return false;
6698
6699 }
6700
6701 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6702 template argument EXPR. */
6703
6704 static tree
6705 get_template_parm_object (tree expr, tsubst_flags_t complain)
6706 {
6707 if (TREE_CODE (expr) == TARGET_EXPR)
6708 expr = TARGET_EXPR_INITIAL (expr);
6709
6710 if (!TREE_CONSTANT (expr))
6711 {
6712 if ((complain & tf_error)
6713 && require_rvalue_constant_expression (expr))
6714 cxx_constant_value (expr);
6715 return error_mark_node;
6716 }
6717 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6718 return error_mark_node;
6719
6720 tree name = mangle_template_parm_object (expr);
6721 tree decl = get_global_binding (name);
6722 if (decl)
6723 return decl;
6724
6725 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6726 decl = create_temporary_var (type);
6727 TREE_STATIC (decl) = true;
6728 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6729 TREE_READONLY (decl) = true;
6730 DECL_NAME (decl) = name;
6731 SET_DECL_ASSEMBLER_NAME (decl, name);
6732 DECL_CONTEXT (decl) = global_namespace;
6733 comdat_linkage (decl);
6734 pushdecl_top_level_and_finish (decl, expr);
6735 return decl;
6736 }
6737
6738 /* Attempt to convert the non-type template parameter EXPR to the
6739 indicated TYPE. If the conversion is successful, return the
6740 converted value. If the conversion is unsuccessful, return
6741 NULL_TREE if we issued an error message, or error_mark_node if we
6742 did not. We issue error messages for out-and-out bad template
6743 parameters, but not simply because the conversion failed, since we
6744 might be just trying to do argument deduction. Both TYPE and EXPR
6745 must be non-dependent.
6746
6747 The conversion follows the special rules described in
6748 [temp.arg.nontype], and it is much more strict than an implicit
6749 conversion.
6750
6751 This function is called twice for each template argument (see
6752 lookup_template_class for a more accurate description of this
6753 problem). This means that we need to handle expressions which
6754 are not valid in a C++ source, but can be created from the
6755 first call (for instance, casts to perform conversions). These
6756 hacks can go away after we fix the double coercion problem. */
6757
6758 static tree
6759 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6760 {
6761 tree expr_type;
6762 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6763
6764 /* Detect immediately string literals as invalid non-type argument.
6765 This special-case is not needed for correctness (we would easily
6766 catch this later), but only to provide better diagnostic for this
6767 common user mistake. As suggested by DR 100, we do not mention
6768 linkage issues in the diagnostic as this is not the point. */
6769 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6770 {
6771 if (complain & tf_error)
6772 error ("%qE is not a valid template argument for type %qT "
6773 "because string literals can never be used in this context",
6774 expr, type);
6775 return NULL_TREE;
6776 }
6777
6778 /* Add the ADDR_EXPR now for the benefit of
6779 value_dependent_expression_p. */
6780 if (TYPE_PTROBV_P (type)
6781 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6782 {
6783 expr = decay_conversion (expr, complain);
6784 if (expr == error_mark_node)
6785 return error_mark_node;
6786 }
6787
6788 /* If we are in a template, EXPR may be non-dependent, but still
6789 have a syntactic, rather than semantic, form. For example, EXPR
6790 might be a SCOPE_REF, rather than the VAR_DECL to which the
6791 SCOPE_REF refers. Preserving the qualifying scope is necessary
6792 so that access checking can be performed when the template is
6793 instantiated -- but here we need the resolved form so that we can
6794 convert the argument. */
6795 bool non_dep = false;
6796 if (TYPE_REF_OBJ_P (type)
6797 && has_value_dependent_address (expr))
6798 /* If we want the address and it's value-dependent, don't fold. */;
6799 else if (processing_template_decl
6800 && is_nondependent_constant_expression (expr))
6801 non_dep = true;
6802 if (error_operand_p (expr))
6803 return error_mark_node;
6804 expr_type = TREE_TYPE (expr);
6805
6806 /* If the argument is non-dependent, perform any conversions in
6807 non-dependent context as well. */
6808 processing_template_decl_sentinel s (non_dep);
6809 if (non_dep)
6810 expr = instantiate_non_dependent_expr_internal (expr, complain);
6811
6812 if (value_dependent_expression_p (expr))
6813 expr = canonicalize_expr_argument (expr, complain);
6814
6815 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6816 to a non-type argument of "nullptr". */
6817 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6818 expr = fold_simple (convert (type, expr));
6819
6820 /* In C++11, integral or enumeration non-type template arguments can be
6821 arbitrary constant expressions. Pointer and pointer to
6822 member arguments can be general constant expressions that evaluate
6823 to a null value, but otherwise still need to be of a specific form. */
6824 if (cxx_dialect >= cxx11)
6825 {
6826 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
6827 /* A PTRMEM_CST is already constant, and a valid template
6828 argument for a parameter of pointer to member type, we just want
6829 to leave it in that form rather than lower it to a
6830 CONSTRUCTOR. */;
6831 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6832 || cxx_dialect >= cxx17)
6833 {
6834 /* C++17: A template-argument for a non-type template-parameter shall
6835 be a converted constant expression (8.20) of the type of the
6836 template-parameter. */
6837 expr = build_converted_constant_expr (type, expr, complain);
6838 if (expr == error_mark_node)
6839 /* Make sure we return NULL_TREE only if we have really issued
6840 an error, as described above. */
6841 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6842 expr = maybe_constant_value (expr, NULL_TREE,
6843 /*manifestly_const_eval=*/true);
6844 expr = convert_from_reference (expr);
6845 }
6846 else if (TYPE_PTR_OR_PTRMEM_P (type))
6847 {
6848 tree folded = maybe_constant_value (expr, NULL_TREE,
6849 /*manifestly_const_eval=*/true);
6850 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6851 : null_member_pointer_value_p (folded))
6852 expr = folded;
6853 }
6854 }
6855
6856 if (TYPE_REF_P (type))
6857 expr = mark_lvalue_use (expr);
6858 else
6859 expr = mark_rvalue_use (expr);
6860
6861 /* HACK: Due to double coercion, we can get a
6862 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6863 which is the tree that we built on the first call (see
6864 below when coercing to reference to object or to reference to
6865 function). We just strip everything and get to the arg.
6866 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6867 for examples. */
6868 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6869 {
6870 tree probe_type, probe = expr;
6871 if (REFERENCE_REF_P (probe))
6872 probe = TREE_OPERAND (probe, 0);
6873 probe_type = TREE_TYPE (probe);
6874 if (TREE_CODE (probe) == NOP_EXPR)
6875 {
6876 /* ??? Maybe we could use convert_from_reference here, but we
6877 would need to relax its constraints because the NOP_EXPR
6878 could actually change the type to something more cv-qualified,
6879 and this is not folded by convert_from_reference. */
6880 tree addr = TREE_OPERAND (probe, 0);
6881 if (TYPE_REF_P (probe_type)
6882 && TREE_CODE (addr) == ADDR_EXPR
6883 && TYPE_PTR_P (TREE_TYPE (addr))
6884 && (same_type_ignoring_top_level_qualifiers_p
6885 (TREE_TYPE (probe_type),
6886 TREE_TYPE (TREE_TYPE (addr)))))
6887 {
6888 expr = TREE_OPERAND (addr, 0);
6889 expr_type = TREE_TYPE (probe_type);
6890 }
6891 }
6892 }
6893
6894 /* [temp.arg.nontype]/5, bullet 1
6895
6896 For a non-type template-parameter of integral or enumeration type,
6897 integral promotions (_conv.prom_) and integral conversions
6898 (_conv.integral_) are applied. */
6899 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6900 {
6901 if (cxx_dialect < cxx11)
6902 {
6903 tree t = build_converted_constant_expr (type, expr, complain);
6904 t = maybe_constant_value (t);
6905 if (t != error_mark_node)
6906 expr = t;
6907 }
6908
6909 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6910 return error_mark_node;
6911
6912 /* Notice that there are constant expressions like '4 % 0' which
6913 do not fold into integer constants. */
6914 if (TREE_CODE (expr) != INTEGER_CST
6915 && !value_dependent_expression_p (expr))
6916 {
6917 if (complain & tf_error)
6918 {
6919 int errs = errorcount, warns = warningcount + werrorcount;
6920 if (!require_potential_constant_expression (expr))
6921 expr = error_mark_node;
6922 else
6923 expr = cxx_constant_value (expr);
6924 if (errorcount > errs || warningcount + werrorcount > warns)
6925 inform (loc, "in template argument for type %qT", type);
6926 if (expr == error_mark_node)
6927 return NULL_TREE;
6928 /* else cxx_constant_value complained but gave us
6929 a real constant, so go ahead. */
6930 if (TREE_CODE (expr) != INTEGER_CST)
6931 {
6932 /* Some assemble time constant expressions like
6933 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6934 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6935 as we can emit them into .rodata initializers of
6936 variables, yet they can't fold into an INTEGER_CST at
6937 compile time. Refuse them here. */
6938 gcc_checking_assert (reduced_constant_expression_p (expr));
6939 error_at (loc, "template argument %qE for type %qT not "
6940 "a constant integer", expr, type);
6941 return NULL_TREE;
6942 }
6943 }
6944 else
6945 return NULL_TREE;
6946 }
6947
6948 /* Avoid typedef problems. */
6949 if (TREE_TYPE (expr) != type)
6950 expr = fold_convert (type, expr);
6951 }
6952 /* [temp.arg.nontype]/5, bullet 2
6953
6954 For a non-type template-parameter of type pointer to object,
6955 qualification conversions (_conv.qual_) and the array-to-pointer
6956 conversion (_conv.array_) are applied. */
6957 else if (TYPE_PTROBV_P (type))
6958 {
6959 tree decayed = expr;
6960
6961 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6962 decay_conversion or an explicit cast. If it's a problematic cast,
6963 we'll complain about it below. */
6964 if (TREE_CODE (expr) == NOP_EXPR)
6965 {
6966 tree probe = expr;
6967 STRIP_NOPS (probe);
6968 if (TREE_CODE (probe) == ADDR_EXPR
6969 && TYPE_PTR_P (TREE_TYPE (probe)))
6970 {
6971 expr = probe;
6972 expr_type = TREE_TYPE (expr);
6973 }
6974 }
6975
6976 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6977
6978 A template-argument for a non-type, non-template template-parameter
6979 shall be one of: [...]
6980
6981 -- the name of a non-type template-parameter;
6982 -- the address of an object or function with external linkage, [...]
6983 expressed as "& id-expression" where the & is optional if the name
6984 refers to a function or array, or if the corresponding
6985 template-parameter is a reference.
6986
6987 Here, we do not care about functions, as they are invalid anyway
6988 for a parameter of type pointer-to-object. */
6989
6990 if (value_dependent_expression_p (expr))
6991 /* Non-type template parameters are OK. */
6992 ;
6993 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6994 /* Null pointer values are OK in C++11. */;
6995 else if (TREE_CODE (expr) != ADDR_EXPR
6996 && !INDIRECT_TYPE_P (expr_type))
6997 /* Other values, like integer constants, might be valid
6998 non-type arguments of some other type. */
6999 return error_mark_node;
7000 else if (invalid_tparm_referent_p (type, expr, complain))
7001 return NULL_TREE;
7002
7003 expr = decayed;
7004
7005 expr = perform_qualification_conversions (type, expr);
7006 if (expr == error_mark_node)
7007 return error_mark_node;
7008 }
7009 /* [temp.arg.nontype]/5, bullet 3
7010
7011 For a non-type template-parameter of type reference to object, no
7012 conversions apply. The type referred to by the reference may be more
7013 cv-qualified than the (otherwise identical) type of the
7014 template-argument. The template-parameter is bound directly to the
7015 template-argument, which must be an lvalue. */
7016 else if (TYPE_REF_OBJ_P (type))
7017 {
7018 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7019 expr_type))
7020 return error_mark_node;
7021
7022 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7023 {
7024 if (complain & tf_error)
7025 error ("%qE is not a valid template argument for type %qT "
7026 "because of conflicts in cv-qualification", expr, type);
7027 return NULL_TREE;
7028 }
7029
7030 if (!lvalue_p (expr))
7031 {
7032 if (complain & tf_error)
7033 error ("%qE is not a valid template argument for type %qT "
7034 "because it is not an lvalue", expr, type);
7035 return NULL_TREE;
7036 }
7037
7038 /* [temp.arg.nontype]/1
7039
7040 A template-argument for a non-type, non-template template-parameter
7041 shall be one of: [...]
7042
7043 -- the address of an object or function with external linkage. */
7044 if (INDIRECT_REF_P (expr)
7045 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7046 {
7047 expr = TREE_OPERAND (expr, 0);
7048 if (DECL_P (expr))
7049 {
7050 if (complain & tf_error)
7051 error ("%q#D is not a valid template argument for type %qT "
7052 "because a reference variable does not have a constant "
7053 "address", expr, type);
7054 return NULL_TREE;
7055 }
7056 }
7057
7058 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7059 && value_dependent_expression_p (expr))
7060 /* OK, dependent reference. We don't want to ask whether a DECL is
7061 itself value-dependent, since what we want here is its address. */;
7062 else
7063 {
7064 expr = build_address (expr);
7065
7066 if (invalid_tparm_referent_p (type, expr, complain))
7067 return NULL_TREE;
7068 }
7069
7070 if (!same_type_p (type, TREE_TYPE (expr)))
7071 expr = build_nop (type, expr);
7072 }
7073 /* [temp.arg.nontype]/5, bullet 4
7074
7075 For a non-type template-parameter of type pointer to function, only
7076 the function-to-pointer conversion (_conv.func_) is applied. If the
7077 template-argument represents a set of overloaded functions (or a
7078 pointer to such), the matching function is selected from the set
7079 (_over.over_). */
7080 else if (TYPE_PTRFN_P (type))
7081 {
7082 /* If the argument is a template-id, we might not have enough
7083 context information to decay the pointer. */
7084 if (!type_unknown_p (expr_type))
7085 {
7086 expr = decay_conversion (expr, complain);
7087 if (expr == error_mark_node)
7088 return error_mark_node;
7089 }
7090
7091 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7092 /* Null pointer values are OK in C++11. */
7093 return perform_qualification_conversions (type, expr);
7094
7095 expr = convert_nontype_argument_function (type, expr, complain);
7096 if (!expr || expr == error_mark_node)
7097 return expr;
7098 }
7099 /* [temp.arg.nontype]/5, bullet 5
7100
7101 For a non-type template-parameter of type reference to function, no
7102 conversions apply. If the template-argument represents a set of
7103 overloaded functions, the matching function is selected from the set
7104 (_over.over_). */
7105 else if (TYPE_REFFN_P (type))
7106 {
7107 if (TREE_CODE (expr) == ADDR_EXPR)
7108 {
7109 if (complain & tf_error)
7110 {
7111 error ("%qE is not a valid template argument for type %qT "
7112 "because it is a pointer", expr, type);
7113 inform (input_location, "try using %qE instead",
7114 TREE_OPERAND (expr, 0));
7115 }
7116 return NULL_TREE;
7117 }
7118
7119 expr = convert_nontype_argument_function (type, expr, complain);
7120 if (!expr || expr == error_mark_node)
7121 return expr;
7122 }
7123 /* [temp.arg.nontype]/5, bullet 6
7124
7125 For a non-type template-parameter of type pointer to member function,
7126 no conversions apply. If the template-argument represents a set of
7127 overloaded member functions, the matching member function is selected
7128 from the set (_over.over_). */
7129 else if (TYPE_PTRMEMFUNC_P (type))
7130 {
7131 expr = instantiate_type (type, expr, tf_none);
7132 if (expr == error_mark_node)
7133 return error_mark_node;
7134
7135 /* [temp.arg.nontype] bullet 1 says the pointer to member
7136 expression must be a pointer-to-member constant. */
7137 if (!value_dependent_expression_p (expr)
7138 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7139 return NULL_TREE;
7140
7141 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7142 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7143 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7144 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7145 }
7146 /* [temp.arg.nontype]/5, bullet 7
7147
7148 For a non-type template-parameter of type pointer to data member,
7149 qualification conversions (_conv.qual_) are applied. */
7150 else if (TYPE_PTRDATAMEM_P (type))
7151 {
7152 /* [temp.arg.nontype] bullet 1 says the pointer to member
7153 expression must be a pointer-to-member constant. */
7154 if (!value_dependent_expression_p (expr)
7155 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7156 return NULL_TREE;
7157
7158 expr = perform_qualification_conversions (type, expr);
7159 if (expr == error_mark_node)
7160 return expr;
7161 }
7162 else if (NULLPTR_TYPE_P (type))
7163 {
7164 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7165 {
7166 if (complain & tf_error)
7167 error ("%qE is not a valid template argument for type %qT "
7168 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7169 return NULL_TREE;
7170 }
7171 return expr;
7172 }
7173 else if (CLASS_TYPE_P (type))
7174 {
7175 /* Replace the argument with a reference to the corresponding template
7176 parameter object. */
7177 if (!value_dependent_expression_p (expr))
7178 expr = get_template_parm_object (expr, complain);
7179 if (expr == error_mark_node)
7180 return NULL_TREE;
7181 }
7182 /* A template non-type parameter must be one of the above. */
7183 else
7184 gcc_unreachable ();
7185
7186 /* Sanity check: did we actually convert the argument to the
7187 right type? */
7188 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7189 (type, TREE_TYPE (expr)));
7190 return convert_from_reference (expr);
7191 }
7192
7193 /* Subroutine of coerce_template_template_parms, which returns 1 if
7194 PARM_PARM and ARG_PARM match using the rule for the template
7195 parameters of template template parameters. Both PARM and ARG are
7196 template parameters; the rest of the arguments are the same as for
7197 coerce_template_template_parms.
7198 */
7199 static int
7200 coerce_template_template_parm (tree parm,
7201 tree arg,
7202 tsubst_flags_t complain,
7203 tree in_decl,
7204 tree outer_args)
7205 {
7206 if (arg == NULL_TREE || error_operand_p (arg)
7207 || parm == NULL_TREE || error_operand_p (parm))
7208 return 0;
7209
7210 if (TREE_CODE (arg) != TREE_CODE (parm))
7211 return 0;
7212
7213 switch (TREE_CODE (parm))
7214 {
7215 case TEMPLATE_DECL:
7216 /* We encounter instantiations of templates like
7217 template <template <template <class> class> class TT>
7218 class C; */
7219 {
7220 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7221 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7222
7223 if (!coerce_template_template_parms
7224 (parmparm, argparm, complain, in_decl, outer_args))
7225 return 0;
7226 }
7227 /* Fall through. */
7228
7229 case TYPE_DECL:
7230 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7231 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7232 /* Argument is a parameter pack but parameter is not. */
7233 return 0;
7234 break;
7235
7236 case PARM_DECL:
7237 /* The tsubst call is used to handle cases such as
7238
7239 template <int> class C {};
7240 template <class T, template <T> class TT> class D {};
7241 D<int, C> d;
7242
7243 i.e. the parameter list of TT depends on earlier parameters. */
7244 if (!uses_template_parms (TREE_TYPE (arg)))
7245 {
7246 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7247 if (!uses_template_parms (t)
7248 && !same_type_p (t, TREE_TYPE (arg)))
7249 return 0;
7250 }
7251
7252 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7253 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7254 /* Argument is a parameter pack but parameter is not. */
7255 return 0;
7256
7257 break;
7258
7259 default:
7260 gcc_unreachable ();
7261 }
7262
7263 return 1;
7264 }
7265
7266 /* Coerce template argument list ARGLIST for use with template
7267 template-parameter TEMPL. */
7268
7269 static tree
7270 coerce_template_args_for_ttp (tree templ, tree arglist,
7271 tsubst_flags_t complain)
7272 {
7273 /* Consider an example where a template template parameter declared as
7274
7275 template <class T, class U = std::allocator<T> > class TT
7276
7277 The template parameter level of T and U are one level larger than
7278 of TT. To proper process the default argument of U, say when an
7279 instantiation `TT<int>' is seen, we need to build the full
7280 arguments containing {int} as the innermost level. Outer levels,
7281 available when not appearing as default template argument, can be
7282 obtained from the arguments of the enclosing template.
7283
7284 Suppose that TT is later substituted with std::vector. The above
7285 instantiation is `TT<int, std::allocator<T> >' with TT at
7286 level 1, and T at level 2, while the template arguments at level 1
7287 becomes {std::vector} and the inner level 2 is {int}. */
7288
7289 tree outer = DECL_CONTEXT (templ);
7290 if (outer)
7291 {
7292 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7293 /* We want arguments for the partial specialization, not arguments for
7294 the primary template. */
7295 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7296 else
7297 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7298 }
7299 else if (current_template_parms)
7300 {
7301 /* This is an argument of the current template, so we haven't set
7302 DECL_CONTEXT yet. */
7303 tree relevant_template_parms;
7304
7305 /* Parameter levels that are greater than the level of the given
7306 template template parm are irrelevant. */
7307 relevant_template_parms = current_template_parms;
7308 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7309 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7310 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7311
7312 outer = template_parms_to_args (relevant_template_parms);
7313 }
7314
7315 if (outer)
7316 arglist = add_to_template_args (outer, arglist);
7317
7318 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7319 return coerce_template_parms (parmlist, arglist, templ,
7320 complain,
7321 /*require_all_args=*/true,
7322 /*use_default_args=*/true);
7323 }
7324
7325 /* A cache of template template parameters with match-all default
7326 arguments. */
7327 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7328 static void
7329 store_defaulted_ttp (tree v, tree t)
7330 {
7331 if (!defaulted_ttp_cache)
7332 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7333 defaulted_ttp_cache->put (v, t);
7334 }
7335 static tree
7336 lookup_defaulted_ttp (tree v)
7337 {
7338 if (defaulted_ttp_cache)
7339 if (tree *p = defaulted_ttp_cache->get (v))
7340 return *p;
7341 return NULL_TREE;
7342 }
7343
7344 /* T is a bound template template-parameter. Copy its arguments into default
7345 arguments of the template template-parameter's template parameters. */
7346
7347 static tree
7348 add_defaults_to_ttp (tree otmpl)
7349 {
7350 if (tree c = lookup_defaulted_ttp (otmpl))
7351 return c;
7352
7353 tree ntmpl = copy_node (otmpl);
7354
7355 tree ntype = copy_node (TREE_TYPE (otmpl));
7356 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7357 TYPE_MAIN_VARIANT (ntype) = ntype;
7358 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7359 TYPE_NAME (ntype) = ntmpl;
7360 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7361
7362 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7363 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7364 TEMPLATE_PARM_DECL (idx) = ntmpl;
7365 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7366
7367 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7368 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7369 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7370 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7371 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7372 {
7373 tree o = TREE_VEC_ELT (vec, i);
7374 if (!template_parameter_pack_p (TREE_VALUE (o)))
7375 {
7376 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7377 TREE_PURPOSE (n) = any_targ_node;
7378 }
7379 }
7380
7381 store_defaulted_ttp (otmpl, ntmpl);
7382 return ntmpl;
7383 }
7384
7385 /* ARG is a bound potential template template-argument, and PARGS is a list
7386 of arguments for the corresponding template template-parameter. Adjust
7387 PARGS as appropriate for application to ARG's template, and if ARG is a
7388 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7389 arguments to the template template parameter. */
7390
7391 static tree
7392 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7393 {
7394 ++processing_template_decl;
7395 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7396 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7397 {
7398 /* When comparing two template template-parameters in partial ordering,
7399 rewrite the one currently being used as an argument to have default
7400 arguments for all parameters. */
7401 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7402 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7403 if (pargs != error_mark_node)
7404 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7405 TYPE_TI_ARGS (arg));
7406 }
7407 else
7408 {
7409 tree aparms
7410 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7411 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7412 /*require_all*/true,
7413 /*use_default*/true);
7414 }
7415 --processing_template_decl;
7416 return pargs;
7417 }
7418
7419 /* Subroutine of unify for the case when PARM is a
7420 BOUND_TEMPLATE_TEMPLATE_PARM. */
7421
7422 static int
7423 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7424 bool explain_p)
7425 {
7426 tree parmvec = TYPE_TI_ARGS (parm);
7427 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7428
7429 /* The template template parm might be variadic and the argument
7430 not, so flatten both argument lists. */
7431 parmvec = expand_template_argument_pack (parmvec);
7432 argvec = expand_template_argument_pack (argvec);
7433
7434 if (flag_new_ttp)
7435 {
7436 /* In keeping with P0522R0, adjust P's template arguments
7437 to apply to A's template; then flatten it again. */
7438 tree nparmvec = parmvec;
7439 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7440 nparmvec = expand_template_argument_pack (nparmvec);
7441
7442 if (unify (tparms, targs, nparmvec, argvec,
7443 UNIFY_ALLOW_NONE, explain_p))
7444 return 1;
7445
7446 /* If the P0522 adjustment eliminated a pack expansion, deduce
7447 empty packs. */
7448 if (flag_new_ttp
7449 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7450 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7451 DEDUCE_EXACT, /*sub*/true, explain_p))
7452 return 1;
7453 }
7454 else
7455 {
7456 /* Deduce arguments T, i from TT<T> or TT<i>.
7457 We check each element of PARMVEC and ARGVEC individually
7458 rather than the whole TREE_VEC since they can have
7459 different number of elements, which is allowed under N2555. */
7460
7461 int len = TREE_VEC_LENGTH (parmvec);
7462
7463 /* Check if the parameters end in a pack, making them
7464 variadic. */
7465 int parm_variadic_p = 0;
7466 if (len > 0
7467 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7468 parm_variadic_p = 1;
7469
7470 for (int i = 0; i < len - parm_variadic_p; ++i)
7471 /* If the template argument list of P contains a pack
7472 expansion that is not the last template argument, the
7473 entire template argument list is a non-deduced
7474 context. */
7475 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7476 return unify_success (explain_p);
7477
7478 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7479 return unify_too_few_arguments (explain_p,
7480 TREE_VEC_LENGTH (argvec), len);
7481
7482 for (int i = 0; i < len - parm_variadic_p; ++i)
7483 if (unify (tparms, targs,
7484 TREE_VEC_ELT (parmvec, i),
7485 TREE_VEC_ELT (argvec, i),
7486 UNIFY_ALLOW_NONE, explain_p))
7487 return 1;
7488
7489 if (parm_variadic_p
7490 && unify_pack_expansion (tparms, targs,
7491 parmvec, argvec,
7492 DEDUCE_EXACT,
7493 /*subr=*/true, explain_p))
7494 return 1;
7495 }
7496
7497 return 0;
7498 }
7499
7500 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7501 template template parameters. Both PARM_PARMS and ARG_PARMS are
7502 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7503 or PARM_DECL.
7504
7505 Consider the example:
7506 template <class T> class A;
7507 template<template <class U> class TT> class B;
7508
7509 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7510 the parameters to A, and OUTER_ARGS contains A. */
7511
7512 static int
7513 coerce_template_template_parms (tree parm_parms,
7514 tree arg_parms,
7515 tsubst_flags_t complain,
7516 tree in_decl,
7517 tree outer_args)
7518 {
7519 int nparms, nargs, i;
7520 tree parm, arg;
7521 int variadic_p = 0;
7522
7523 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7524 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7525
7526 nparms = TREE_VEC_LENGTH (parm_parms);
7527 nargs = TREE_VEC_LENGTH (arg_parms);
7528
7529 if (flag_new_ttp)
7530 {
7531 /* P0522R0: A template template-parameter P is at least as specialized as
7532 a template template-argument A if, given the following rewrite to two
7533 function templates, the function template corresponding to P is at
7534 least as specialized as the function template corresponding to A
7535 according to the partial ordering rules for function templates
7536 ([temp.func.order]). Given an invented class template X with the
7537 template parameter list of A (including default arguments):
7538
7539 * Each of the two function templates has the same template parameters,
7540 respectively, as P or A.
7541
7542 * Each function template has a single function parameter whose type is
7543 a specialization of X with template arguments corresponding to the
7544 template parameters from the respective function template where, for
7545 each template parameter PP in the template parameter list of the
7546 function template, a corresponding template argument AA is formed. If
7547 PP declares a parameter pack, then AA is the pack expansion
7548 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7549
7550 If the rewrite produces an invalid type, then P is not at least as
7551 specialized as A. */
7552
7553 /* So coerce P's args to apply to A's parms, and then deduce between A's
7554 args and the converted args. If that succeeds, A is at least as
7555 specialized as P, so they match.*/
7556 tree pargs = template_parms_level_to_args (parm_parms);
7557 ++processing_template_decl;
7558 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7559 /*require_all*/true, /*use_default*/true);
7560 --processing_template_decl;
7561 if (pargs != error_mark_node)
7562 {
7563 tree targs = make_tree_vec (nargs);
7564 tree aargs = template_parms_level_to_args (arg_parms);
7565 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7566 /*explain*/false))
7567 return 1;
7568 }
7569 }
7570
7571 /* Determine whether we have a parameter pack at the end of the
7572 template template parameter's template parameter list. */
7573 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7574 {
7575 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7576
7577 if (error_operand_p (parm))
7578 return 0;
7579
7580 switch (TREE_CODE (parm))
7581 {
7582 case TEMPLATE_DECL:
7583 case TYPE_DECL:
7584 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7585 variadic_p = 1;
7586 break;
7587
7588 case PARM_DECL:
7589 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7590 variadic_p = 1;
7591 break;
7592
7593 default:
7594 gcc_unreachable ();
7595 }
7596 }
7597
7598 if (nargs != nparms
7599 && !(variadic_p && nargs >= nparms - 1))
7600 return 0;
7601
7602 /* Check all of the template parameters except the parameter pack at
7603 the end (if any). */
7604 for (i = 0; i < nparms - variadic_p; ++i)
7605 {
7606 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7607 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7608 continue;
7609
7610 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7611 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7612
7613 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7614 outer_args))
7615 return 0;
7616
7617 }
7618
7619 if (variadic_p)
7620 {
7621 /* Check each of the template parameters in the template
7622 argument against the template parameter pack at the end of
7623 the template template parameter. */
7624 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7625 return 0;
7626
7627 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7628
7629 for (; i < nargs; ++i)
7630 {
7631 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7632 continue;
7633
7634 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7635
7636 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7637 outer_args))
7638 return 0;
7639 }
7640 }
7641
7642 return 1;
7643 }
7644
7645 /* Verifies that the deduced template arguments (in TARGS) for the
7646 template template parameters (in TPARMS) represent valid bindings,
7647 by comparing the template parameter list of each template argument
7648 to the template parameter list of its corresponding template
7649 template parameter, in accordance with DR150. This
7650 routine can only be called after all template arguments have been
7651 deduced. It will return TRUE if all of the template template
7652 parameter bindings are okay, FALSE otherwise. */
7653 bool
7654 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7655 {
7656 int i, ntparms = TREE_VEC_LENGTH (tparms);
7657 bool ret = true;
7658
7659 /* We're dealing with template parms in this process. */
7660 ++processing_template_decl;
7661
7662 targs = INNERMOST_TEMPLATE_ARGS (targs);
7663
7664 for (i = 0; i < ntparms; ++i)
7665 {
7666 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7667 tree targ = TREE_VEC_ELT (targs, i);
7668
7669 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7670 {
7671 tree packed_args = NULL_TREE;
7672 int idx, len = 1;
7673
7674 if (ARGUMENT_PACK_P (targ))
7675 {
7676 /* Look inside the argument pack. */
7677 packed_args = ARGUMENT_PACK_ARGS (targ);
7678 len = TREE_VEC_LENGTH (packed_args);
7679 }
7680
7681 for (idx = 0; idx < len; ++idx)
7682 {
7683 tree targ_parms = NULL_TREE;
7684
7685 if (packed_args)
7686 /* Extract the next argument from the argument
7687 pack. */
7688 targ = TREE_VEC_ELT (packed_args, idx);
7689
7690 if (PACK_EXPANSION_P (targ))
7691 /* Look at the pattern of the pack expansion. */
7692 targ = PACK_EXPANSION_PATTERN (targ);
7693
7694 /* Extract the template parameters from the template
7695 argument. */
7696 if (TREE_CODE (targ) == TEMPLATE_DECL)
7697 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7698 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7699 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7700
7701 /* Verify that we can coerce the template template
7702 parameters from the template argument to the template
7703 parameter. This requires an exact match. */
7704 if (targ_parms
7705 && !coerce_template_template_parms
7706 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7707 targ_parms,
7708 tf_none,
7709 tparm,
7710 targs))
7711 {
7712 ret = false;
7713 goto out;
7714 }
7715 }
7716 }
7717 }
7718
7719 out:
7720
7721 --processing_template_decl;
7722 return ret;
7723 }
7724
7725 /* Since type attributes aren't mangled, we need to strip them from
7726 template type arguments. */
7727
7728 static tree
7729 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7730 {
7731 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7732 return arg;
7733 bool removed_attributes = false;
7734 tree canon = strip_typedefs (arg, &removed_attributes);
7735 if (removed_attributes
7736 && (complain & tf_warning))
7737 warning (OPT_Wignored_attributes,
7738 "ignoring attributes on template argument %qT", arg);
7739 return canon;
7740 }
7741
7742 /* And from inside dependent non-type arguments like sizeof(Type). */
7743
7744 static tree
7745 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7746 {
7747 if (!arg || arg == error_mark_node)
7748 return arg;
7749 bool removed_attributes = false;
7750 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7751 if (removed_attributes
7752 && (complain & tf_warning))
7753 warning (OPT_Wignored_attributes,
7754 "ignoring attributes in template argument %qE", arg);
7755 return canon;
7756 }
7757
7758 // A template declaration can be substituted for a constrained
7759 // template template parameter only when the argument is more
7760 // constrained than the parameter.
7761 static bool
7762 is_compatible_template_arg (tree parm, tree arg)
7763 {
7764 tree parm_cons = get_constraints (parm);
7765
7766 /* For now, allow constrained template template arguments
7767 and unconstrained template template parameters. */
7768 if (parm_cons == NULL_TREE)
7769 return true;
7770
7771 tree arg_cons = get_constraints (arg);
7772
7773 // If the template parameter is constrained, we need to rewrite its
7774 // constraints in terms of the ARG's template parameters. This ensures
7775 // that all of the template parameter types will have the same depth.
7776 //
7777 // Note that this is only valid when coerce_template_template_parm is
7778 // true for the innermost template parameters of PARM and ARG. In other
7779 // words, because coercion is successful, this conversion will be valid.
7780 if (parm_cons)
7781 {
7782 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7783 parm_cons = tsubst_constraint_info (parm_cons,
7784 INNERMOST_TEMPLATE_ARGS (args),
7785 tf_none, NULL_TREE);
7786 if (parm_cons == error_mark_node)
7787 return false;
7788 }
7789
7790 return subsumes (parm_cons, arg_cons);
7791 }
7792
7793 // Convert a placeholder argument into a binding to the original
7794 // parameter. The original parameter is saved as the TREE_TYPE of
7795 // ARG.
7796 static inline tree
7797 convert_wildcard_argument (tree parm, tree arg)
7798 {
7799 TREE_TYPE (arg) = parm;
7800 return arg;
7801 }
7802
7803 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7804 because one of them is dependent. But we need to represent the
7805 conversion for the benefit of cp_tree_equal. */
7806
7807 static tree
7808 maybe_convert_nontype_argument (tree type, tree arg)
7809 {
7810 /* Auto parms get no conversion. */
7811 if (type_uses_auto (type))
7812 return arg;
7813 /* We don't need or want to add this conversion now if we're going to use the
7814 argument for deduction. */
7815 if (value_dependent_expression_p (arg))
7816 return arg;
7817
7818 type = cv_unqualified (type);
7819 tree argtype = TREE_TYPE (arg);
7820 if (same_type_p (type, argtype))
7821 return arg;
7822
7823 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7824 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7825 return arg;
7826 }
7827
7828 /* Convert the indicated template ARG as necessary to match the
7829 indicated template PARM. Returns the converted ARG, or
7830 error_mark_node if the conversion was unsuccessful. Error and
7831 warning messages are issued under control of COMPLAIN. This
7832 conversion is for the Ith parameter in the parameter list. ARGS is
7833 the full set of template arguments deduced so far. */
7834
7835 static tree
7836 convert_template_argument (tree parm,
7837 tree arg,
7838 tree args,
7839 tsubst_flags_t complain,
7840 int i,
7841 tree in_decl)
7842 {
7843 tree orig_arg;
7844 tree val;
7845 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7846
7847 if (parm == error_mark_node || error_operand_p (arg))
7848 return error_mark_node;
7849
7850 /* Trivially convert placeholders. */
7851 if (TREE_CODE (arg) == WILDCARD_DECL)
7852 return convert_wildcard_argument (parm, arg);
7853
7854 if (arg == any_targ_node)
7855 return arg;
7856
7857 if (TREE_CODE (arg) == TREE_LIST
7858 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7859 {
7860 /* The template argument was the name of some
7861 member function. That's usually
7862 invalid, but static members are OK. In any
7863 case, grab the underlying fields/functions
7864 and issue an error later if required. */
7865 orig_arg = TREE_VALUE (arg);
7866 TREE_TYPE (arg) = unknown_type_node;
7867 }
7868
7869 orig_arg = arg;
7870
7871 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7872 requires_type = (TREE_CODE (parm) == TYPE_DECL
7873 || requires_tmpl_type);
7874
7875 /* When determining whether an argument pack expansion is a template,
7876 look at the pattern. */
7877 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7878 arg = PACK_EXPANSION_PATTERN (arg);
7879
7880 /* Deal with an injected-class-name used as a template template arg. */
7881 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7882 {
7883 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7884 if (TREE_CODE (t) == TEMPLATE_DECL)
7885 {
7886 if (cxx_dialect >= cxx11)
7887 /* OK under DR 1004. */;
7888 else if (complain & tf_warning_or_error)
7889 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7890 " used as template template argument", TYPE_NAME (arg));
7891 else if (flag_pedantic_errors)
7892 t = arg;
7893
7894 arg = t;
7895 }
7896 }
7897
7898 is_tmpl_type =
7899 ((TREE_CODE (arg) == TEMPLATE_DECL
7900 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7901 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7902 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7903 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7904
7905 if (is_tmpl_type
7906 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7907 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7908 arg = TYPE_STUB_DECL (arg);
7909
7910 is_type = TYPE_P (arg) || is_tmpl_type;
7911
7912 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7913 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7914 {
7915 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7916 {
7917 if (complain & tf_error)
7918 error ("invalid use of destructor %qE as a type", orig_arg);
7919 return error_mark_node;
7920 }
7921
7922 permerror (input_location,
7923 "to refer to a type member of a template parameter, "
7924 "use %<typename %E%>", orig_arg);
7925
7926 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7927 TREE_OPERAND (arg, 1),
7928 typename_type,
7929 complain);
7930 arg = orig_arg;
7931 is_type = 1;
7932 }
7933 if (is_type != requires_type)
7934 {
7935 if (in_decl)
7936 {
7937 if (complain & tf_error)
7938 {
7939 error ("type/value mismatch at argument %d in template "
7940 "parameter list for %qD",
7941 i + 1, in_decl);
7942 if (is_type)
7943 inform (input_location,
7944 " expected a constant of type %qT, got %qT",
7945 TREE_TYPE (parm),
7946 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7947 else if (requires_tmpl_type)
7948 inform (input_location,
7949 " expected a class template, got %qE", orig_arg);
7950 else
7951 inform (input_location,
7952 " expected a type, got %qE", orig_arg);
7953 }
7954 }
7955 return error_mark_node;
7956 }
7957 if (is_tmpl_type ^ requires_tmpl_type)
7958 {
7959 if (in_decl && (complain & tf_error))
7960 {
7961 error ("type/value mismatch at argument %d in template "
7962 "parameter list for %qD",
7963 i + 1, in_decl);
7964 if (is_tmpl_type)
7965 inform (input_location,
7966 " expected a type, got %qT", DECL_NAME (arg));
7967 else
7968 inform (input_location,
7969 " expected a class template, got %qT", orig_arg);
7970 }
7971 return error_mark_node;
7972 }
7973
7974 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7975 /* We already did the appropriate conversion when packing args. */
7976 val = orig_arg;
7977 else if (is_type)
7978 {
7979 if (requires_tmpl_type)
7980 {
7981 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7982 /* The number of argument required is not known yet.
7983 Just accept it for now. */
7984 val = orig_arg;
7985 else
7986 {
7987 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7988 tree argparm;
7989
7990 /* Strip alias templates that are equivalent to another
7991 template. */
7992 arg = get_underlying_template (arg);
7993 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7994
7995 if (coerce_template_template_parms (parmparm, argparm,
7996 complain, in_decl,
7997 args))
7998 {
7999 val = arg;
8000
8001 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8002 TEMPLATE_DECL. */
8003 if (val != error_mark_node)
8004 {
8005 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8006 val = TREE_TYPE (val);
8007 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8008 val = make_pack_expansion (val, complain);
8009 }
8010 }
8011 else
8012 {
8013 if (in_decl && (complain & tf_error))
8014 {
8015 error ("type/value mismatch at argument %d in "
8016 "template parameter list for %qD",
8017 i + 1, in_decl);
8018 inform (input_location,
8019 " expected a template of type %qD, got %qT",
8020 parm, orig_arg);
8021 }
8022
8023 val = error_mark_node;
8024 }
8025
8026 // Check that the constraints are compatible before allowing the
8027 // substitution.
8028 if (val != error_mark_node)
8029 if (!is_compatible_template_arg (parm, arg))
8030 {
8031 if (in_decl && (complain & tf_error))
8032 {
8033 error ("constraint mismatch at argument %d in "
8034 "template parameter list for %qD",
8035 i + 1, in_decl);
8036 inform (input_location, " expected %qD but got %qD",
8037 parm, arg);
8038 }
8039 val = error_mark_node;
8040 }
8041 }
8042 }
8043 else
8044 val = orig_arg;
8045 /* We only form one instance of each template specialization.
8046 Therefore, if we use a non-canonical variant (i.e., a
8047 typedef), any future messages referring to the type will use
8048 the typedef, which is confusing if those future uses do not
8049 themselves also use the typedef. */
8050 if (TYPE_P (val))
8051 val = canonicalize_type_argument (val, complain);
8052 }
8053 else
8054 {
8055 tree t = TREE_TYPE (parm);
8056
8057 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8058 > TMPL_ARGS_DEPTH (args))
8059 /* We don't have enough levels of args to do any substitution. This
8060 can happen in the context of -fnew-ttp-matching. */;
8061 else if (tree a = type_uses_auto (t))
8062 {
8063 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8064 if (t == error_mark_node)
8065 return error_mark_node;
8066 }
8067 else
8068 t = tsubst (t, args, complain, in_decl);
8069
8070 if (invalid_nontype_parm_type_p (t, complain))
8071 return error_mark_node;
8072
8073 if (t != TREE_TYPE (parm))
8074 t = canonicalize_type_argument (t, complain);
8075
8076 if (!type_dependent_expression_p (orig_arg)
8077 && !uses_template_parms (t))
8078 /* We used to call digest_init here. However, digest_init
8079 will report errors, which we don't want when complain
8080 is zero. More importantly, digest_init will try too
8081 hard to convert things: for example, `0' should not be
8082 converted to pointer type at this point according to
8083 the standard. Accepting this is not merely an
8084 extension, since deciding whether or not these
8085 conversions can occur is part of determining which
8086 function template to call, or whether a given explicit
8087 argument specification is valid. */
8088 val = convert_nontype_argument (t, orig_arg, complain);
8089 else
8090 {
8091 val = canonicalize_expr_argument (orig_arg, complain);
8092 val = maybe_convert_nontype_argument (t, val);
8093 }
8094
8095
8096 if (val == NULL_TREE)
8097 val = error_mark_node;
8098 else if (val == error_mark_node && (complain & tf_error))
8099 error ("could not convert template argument %qE from %qT to %qT",
8100 orig_arg, TREE_TYPE (orig_arg), t);
8101
8102 if (INDIRECT_REF_P (val))
8103 {
8104 /* Reject template arguments that are references to built-in
8105 functions with no library fallbacks. */
8106 const_tree inner = TREE_OPERAND (val, 0);
8107 const_tree innertype = TREE_TYPE (inner);
8108 if (innertype
8109 && TYPE_REF_P (innertype)
8110 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8111 && TREE_OPERAND_LENGTH (inner) > 0
8112 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8113 return error_mark_node;
8114 }
8115
8116 if (TREE_CODE (val) == SCOPE_REF)
8117 {
8118 /* Strip typedefs from the SCOPE_REF. */
8119 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8120 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8121 complain);
8122 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8123 QUALIFIED_NAME_IS_TEMPLATE (val));
8124 }
8125 }
8126
8127 return val;
8128 }
8129
8130 /* Coerces the remaining template arguments in INNER_ARGS (from
8131 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8132 Returns the coerced argument pack. PARM_IDX is the position of this
8133 parameter in the template parameter list. ARGS is the original
8134 template argument list. */
8135 static tree
8136 coerce_template_parameter_pack (tree parms,
8137 int parm_idx,
8138 tree args,
8139 tree inner_args,
8140 int arg_idx,
8141 tree new_args,
8142 int* lost,
8143 tree in_decl,
8144 tsubst_flags_t complain)
8145 {
8146 tree parm = TREE_VEC_ELT (parms, parm_idx);
8147 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8148 tree packed_args;
8149 tree argument_pack;
8150 tree packed_parms = NULL_TREE;
8151
8152 if (arg_idx > nargs)
8153 arg_idx = nargs;
8154
8155 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8156 {
8157 /* When the template parameter is a non-type template parameter pack
8158 or template template parameter pack whose type or template
8159 parameters use parameter packs, we know exactly how many arguments
8160 we are looking for. Build a vector of the instantiated decls for
8161 these template parameters in PACKED_PARMS. */
8162 /* We can't use make_pack_expansion here because it would interpret a
8163 _DECL as a use rather than a declaration. */
8164 tree decl = TREE_VALUE (parm);
8165 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8166 SET_PACK_EXPANSION_PATTERN (exp, decl);
8167 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8168 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8169
8170 TREE_VEC_LENGTH (args)--;
8171 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8172 TREE_VEC_LENGTH (args)++;
8173
8174 if (packed_parms == error_mark_node)
8175 return error_mark_node;
8176
8177 /* If we're doing a partial instantiation of a member template,
8178 verify that all of the types used for the non-type
8179 template parameter pack are, in fact, valid for non-type
8180 template parameters. */
8181 if (arg_idx < nargs
8182 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8183 {
8184 int j, len = TREE_VEC_LENGTH (packed_parms);
8185 for (j = 0; j < len; ++j)
8186 {
8187 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8188 if (invalid_nontype_parm_type_p (t, complain))
8189 return error_mark_node;
8190 }
8191 /* We don't know how many args we have yet, just
8192 use the unconverted ones for now. */
8193 return NULL_TREE;
8194 }
8195
8196 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8197 }
8198 /* Check if we have a placeholder pack, which indicates we're
8199 in the context of a introduction list. In that case we want
8200 to match this pack to the single placeholder. */
8201 else if (arg_idx < nargs
8202 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8203 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8204 {
8205 nargs = arg_idx + 1;
8206 packed_args = make_tree_vec (1);
8207 }
8208 else
8209 packed_args = make_tree_vec (nargs - arg_idx);
8210
8211 /* Convert the remaining arguments, which will be a part of the
8212 parameter pack "parm". */
8213 int first_pack_arg = arg_idx;
8214 for (; arg_idx < nargs; ++arg_idx)
8215 {
8216 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8217 tree actual_parm = TREE_VALUE (parm);
8218 int pack_idx = arg_idx - first_pack_arg;
8219
8220 if (packed_parms)
8221 {
8222 /* Once we've packed as many args as we have types, stop. */
8223 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8224 break;
8225 else if (PACK_EXPANSION_P (arg))
8226 /* We don't know how many args we have yet, just
8227 use the unconverted ones for now. */
8228 return NULL_TREE;
8229 else
8230 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8231 }
8232
8233 if (arg == error_mark_node)
8234 {
8235 if (complain & tf_error)
8236 error ("template argument %d is invalid", arg_idx + 1);
8237 }
8238 else
8239 arg = convert_template_argument (actual_parm,
8240 arg, new_args, complain, parm_idx,
8241 in_decl);
8242 if (arg == error_mark_node)
8243 (*lost)++;
8244 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8245 }
8246
8247 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8248 && TREE_VEC_LENGTH (packed_args) > 0)
8249 {
8250 if (complain & tf_error)
8251 error ("wrong number of template arguments (%d, should be %d)",
8252 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8253 return error_mark_node;
8254 }
8255
8256 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8257 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8258 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8259 else
8260 {
8261 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8262 TREE_CONSTANT (argument_pack) = 1;
8263 }
8264
8265 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8266 if (CHECKING_P)
8267 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8268 TREE_VEC_LENGTH (packed_args));
8269 return argument_pack;
8270 }
8271
8272 /* Returns the number of pack expansions in the template argument vector
8273 ARGS. */
8274
8275 static int
8276 pack_expansion_args_count (tree args)
8277 {
8278 int i;
8279 int count = 0;
8280 if (args)
8281 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8282 {
8283 tree elt = TREE_VEC_ELT (args, i);
8284 if (elt && PACK_EXPANSION_P (elt))
8285 ++count;
8286 }
8287 return count;
8288 }
8289
8290 /* Convert all template arguments to their appropriate types, and
8291 return a vector containing the innermost resulting template
8292 arguments. If any error occurs, return error_mark_node. Error and
8293 warning messages are issued under control of COMPLAIN.
8294
8295 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8296 for arguments not specified in ARGS. Otherwise, if
8297 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8298 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8299 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8300 ARGS. */
8301
8302 static tree
8303 coerce_template_parms (tree parms,
8304 tree args,
8305 tree in_decl,
8306 tsubst_flags_t complain,
8307 bool require_all_args,
8308 bool use_default_args)
8309 {
8310 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8311 tree orig_inner_args;
8312 tree inner_args;
8313 tree new_args;
8314 tree new_inner_args;
8315
8316 /* When used as a boolean value, indicates whether this is a
8317 variadic template parameter list. Since it's an int, we can also
8318 subtract it from nparms to get the number of non-variadic
8319 parameters. */
8320 int variadic_p = 0;
8321 int variadic_args_p = 0;
8322 int post_variadic_parms = 0;
8323
8324 /* Adjustment to nparms for fixed parameter packs. */
8325 int fixed_pack_adjust = 0;
8326 int fixed_packs = 0;
8327 int missing = 0;
8328
8329 /* Likewise for parameters with default arguments. */
8330 int default_p = 0;
8331
8332 if (args == error_mark_node)
8333 return error_mark_node;
8334
8335 nparms = TREE_VEC_LENGTH (parms);
8336
8337 /* Determine if there are any parameter packs or default arguments. */
8338 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8339 {
8340 tree parm = TREE_VEC_ELT (parms, parm_idx);
8341 if (variadic_p)
8342 ++post_variadic_parms;
8343 if (template_parameter_pack_p (TREE_VALUE (parm)))
8344 ++variadic_p;
8345 if (TREE_PURPOSE (parm))
8346 ++default_p;
8347 }
8348
8349 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8350 /* If there are no parameters that follow a parameter pack, we need to
8351 expand any argument packs so that we can deduce a parameter pack from
8352 some non-packed args followed by an argument pack, as in variadic85.C.
8353 If there are such parameters, we need to leave argument packs intact
8354 so the arguments are assigned properly. This can happen when dealing
8355 with a nested class inside a partial specialization of a class
8356 template, as in variadic92.C, or when deducing a template parameter pack
8357 from a sub-declarator, as in variadic114.C. */
8358 if (!post_variadic_parms)
8359 inner_args = expand_template_argument_pack (inner_args);
8360
8361 /* Count any pack expansion args. */
8362 variadic_args_p = pack_expansion_args_count (inner_args);
8363
8364 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8365 if ((nargs - variadic_args_p > nparms && !variadic_p)
8366 || (nargs < nparms - variadic_p
8367 && require_all_args
8368 && !variadic_args_p
8369 && (!use_default_args
8370 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8371 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8372 {
8373 bad_nargs:
8374 if (complain & tf_error)
8375 {
8376 if (variadic_p || default_p)
8377 {
8378 nparms -= variadic_p + default_p;
8379 error ("wrong number of template arguments "
8380 "(%d, should be at least %d)", nargs, nparms);
8381 }
8382 else
8383 error ("wrong number of template arguments "
8384 "(%d, should be %d)", nargs, nparms);
8385
8386 if (in_decl)
8387 inform (DECL_SOURCE_LOCATION (in_decl),
8388 "provided for %qD", in_decl);
8389 }
8390
8391 return error_mark_node;
8392 }
8393 /* We can't pass a pack expansion to a non-pack parameter of an alias
8394 template (DR 1430). */
8395 else if (in_decl
8396 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8397 || concept_template_p (in_decl))
8398 && variadic_args_p
8399 && nargs - variadic_args_p < nparms - variadic_p)
8400 {
8401 if (complain & tf_error)
8402 {
8403 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8404 {
8405 tree arg = TREE_VEC_ELT (inner_args, i);
8406 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8407
8408 if (PACK_EXPANSION_P (arg)
8409 && !template_parameter_pack_p (parm))
8410 {
8411 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8412 error_at (location_of (arg),
8413 "pack expansion argument for non-pack parameter "
8414 "%qD of alias template %qD", parm, in_decl);
8415 else
8416 error_at (location_of (arg),
8417 "pack expansion argument for non-pack parameter "
8418 "%qD of concept %qD", parm, in_decl);
8419 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8420 goto found;
8421 }
8422 }
8423 gcc_unreachable ();
8424 found:;
8425 }
8426 return error_mark_node;
8427 }
8428
8429 /* We need to evaluate the template arguments, even though this
8430 template-id may be nested within a "sizeof". */
8431 cp_evaluated ev;
8432
8433 new_inner_args = make_tree_vec (nparms);
8434 new_args = add_outermost_template_args (args, new_inner_args);
8435 int pack_adjust = 0;
8436 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8437 {
8438 tree arg;
8439 tree parm;
8440
8441 /* Get the Ith template parameter. */
8442 parm = TREE_VEC_ELT (parms, parm_idx);
8443
8444 if (parm == error_mark_node)
8445 {
8446 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8447 continue;
8448 }
8449
8450 /* Calculate the next argument. */
8451 if (arg_idx < nargs)
8452 arg = TREE_VEC_ELT (inner_args, arg_idx);
8453 else
8454 arg = NULL_TREE;
8455
8456 if (template_parameter_pack_p (TREE_VALUE (parm))
8457 && (arg || !(complain & tf_partial))
8458 && !(arg && ARGUMENT_PACK_P (arg)))
8459 {
8460 /* Some arguments will be placed in the
8461 template parameter pack PARM. */
8462 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8463 inner_args, arg_idx,
8464 new_args, &lost,
8465 in_decl, complain);
8466
8467 if (arg == NULL_TREE)
8468 {
8469 /* We don't know how many args we have yet, just use the
8470 unconverted (and still packed) ones for now. */
8471 new_inner_args = orig_inner_args;
8472 arg_idx = nargs;
8473 break;
8474 }
8475
8476 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8477
8478 /* Store this argument. */
8479 if (arg == error_mark_node)
8480 {
8481 lost++;
8482 /* We are done with all of the arguments. */
8483 arg_idx = nargs;
8484 break;
8485 }
8486 else
8487 {
8488 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8489 arg_idx += pack_adjust;
8490 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8491 {
8492 ++fixed_packs;
8493 fixed_pack_adjust += pack_adjust;
8494 }
8495 }
8496
8497 continue;
8498 }
8499 else if (arg)
8500 {
8501 if (PACK_EXPANSION_P (arg))
8502 {
8503 /* "If every valid specialization of a variadic template
8504 requires an empty template parameter pack, the template is
8505 ill-formed, no diagnostic required." So check that the
8506 pattern works with this parameter. */
8507 tree pattern = PACK_EXPANSION_PATTERN (arg);
8508 tree conv = convert_template_argument (TREE_VALUE (parm),
8509 pattern, new_args,
8510 complain, parm_idx,
8511 in_decl);
8512 if (conv == error_mark_node)
8513 {
8514 if (complain & tf_error)
8515 inform (input_location, "so any instantiation with a "
8516 "non-empty parameter pack would be ill-formed");
8517 ++lost;
8518 }
8519 else if (TYPE_P (conv) && !TYPE_P (pattern))
8520 /* Recover from missing typename. */
8521 TREE_VEC_ELT (inner_args, arg_idx)
8522 = make_pack_expansion (conv, complain);
8523
8524 /* We don't know how many args we have yet, just
8525 use the unconverted ones for now. */
8526 new_inner_args = inner_args;
8527 arg_idx = nargs;
8528 break;
8529 }
8530 }
8531 else if (require_all_args)
8532 {
8533 /* There must be a default arg in this case. */
8534 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8535 complain, in_decl);
8536 /* The position of the first default template argument,
8537 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8538 Record that. */
8539 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8541 arg_idx - pack_adjust);
8542 }
8543 else
8544 break;
8545
8546 if (arg == error_mark_node)
8547 {
8548 if (complain & tf_error)
8549 error ("template argument %d is invalid", arg_idx + 1);
8550 }
8551 else if (!arg)
8552 {
8553 /* This can occur if there was an error in the template
8554 parameter list itself (which we would already have
8555 reported) that we are trying to recover from, e.g., a class
8556 template with a parameter list such as
8557 template<typename..., typename> (cpp0x/variadic150.C). */
8558 ++lost;
8559
8560 /* This can also happen with a fixed parameter pack (71834). */
8561 if (arg_idx >= nargs)
8562 ++missing;
8563 }
8564 else
8565 arg = convert_template_argument (TREE_VALUE (parm),
8566 arg, new_args, complain,
8567 parm_idx, in_decl);
8568
8569 if (arg == error_mark_node)
8570 lost++;
8571 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8572 }
8573
8574 if (missing || arg_idx < nargs - variadic_args_p)
8575 {
8576 /* If we had fixed parameter packs, we didn't know how many arguments we
8577 actually needed earlier; now we do. */
8578 nparms += fixed_pack_adjust;
8579 variadic_p -= fixed_packs;
8580 goto bad_nargs;
8581 }
8582
8583 if (arg_idx < nargs)
8584 {
8585 /* We had some pack expansion arguments that will only work if the packs
8586 are empty, but wait until instantiation time to complain.
8587 See variadic-ttp3.C. */
8588 int len = nparms + (nargs - arg_idx);
8589 tree args = make_tree_vec (len);
8590 int i = 0;
8591 for (; i < nparms; ++i)
8592 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8593 for (; i < len; ++i, ++arg_idx)
8594 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8595 arg_idx - pack_adjust);
8596 new_inner_args = args;
8597 }
8598
8599 if (lost)
8600 {
8601 gcc_assert (!(complain & tf_error) || seen_error ());
8602 return error_mark_node;
8603 }
8604
8605 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8606 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8607 TREE_VEC_LENGTH (new_inner_args));
8608
8609 return new_inner_args;
8610 }
8611
8612 /* Convert all template arguments to their appropriate types, and
8613 return a vector containing the innermost resulting template
8614 arguments. If any error occurs, return error_mark_node. Error and
8615 warning messages are not issued.
8616
8617 Note that no function argument deduction is performed, and default
8618 arguments are used to fill in unspecified arguments. */
8619 tree
8620 coerce_template_parms (tree parms, tree args, tree in_decl)
8621 {
8622 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8623 }
8624
8625 /* Convert all template arguments to their appropriate type, and
8626 instantiate default arguments as needed. This returns a vector
8627 containing the innermost resulting template arguments, or
8628 error_mark_node if unsuccessful. */
8629 tree
8630 coerce_template_parms (tree parms, tree args, tree in_decl,
8631 tsubst_flags_t complain)
8632 {
8633 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8634 }
8635
8636 /* Like coerce_template_parms. If PARMS represents all template
8637 parameters levels, this function returns a vector of vectors
8638 representing all the resulting argument levels. Note that in this
8639 case, only the innermost arguments are coerced because the
8640 outermost ones are supposed to have been coerced already.
8641
8642 Otherwise, if PARMS represents only (the innermost) vector of
8643 parameters, this function returns a vector containing just the
8644 innermost resulting arguments. */
8645
8646 static tree
8647 coerce_innermost_template_parms (tree parms,
8648 tree args,
8649 tree in_decl,
8650 tsubst_flags_t complain,
8651 bool require_all_args,
8652 bool use_default_args)
8653 {
8654 int parms_depth = TMPL_PARMS_DEPTH (parms);
8655 int args_depth = TMPL_ARGS_DEPTH (args);
8656 tree coerced_args;
8657
8658 if (parms_depth > 1)
8659 {
8660 coerced_args = make_tree_vec (parms_depth);
8661 tree level;
8662 int cur_depth;
8663
8664 for (level = parms, cur_depth = parms_depth;
8665 parms_depth > 0 && level != NULL_TREE;
8666 level = TREE_CHAIN (level), --cur_depth)
8667 {
8668 tree l;
8669 if (cur_depth == args_depth)
8670 l = coerce_template_parms (TREE_VALUE (level),
8671 args, in_decl, complain,
8672 require_all_args,
8673 use_default_args);
8674 else
8675 l = TMPL_ARGS_LEVEL (args, cur_depth);
8676
8677 if (l == error_mark_node)
8678 return error_mark_node;
8679
8680 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8681 }
8682 }
8683 else
8684 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8685 args, in_decl, complain,
8686 require_all_args,
8687 use_default_args);
8688 return coerced_args;
8689 }
8690
8691 /* Returns 1 if template args OT and NT are equivalent. */
8692
8693 int
8694 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8695 {
8696 if (nt == ot)
8697 return 1;
8698 if (nt == NULL_TREE || ot == NULL_TREE)
8699 return false;
8700 if (nt == any_targ_node || ot == any_targ_node)
8701 return true;
8702
8703 if (TREE_CODE (nt) == TREE_VEC)
8704 /* For member templates */
8705 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8706 else if (PACK_EXPANSION_P (ot))
8707 return (PACK_EXPANSION_P (nt)
8708 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8709 PACK_EXPANSION_PATTERN (nt))
8710 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8711 PACK_EXPANSION_EXTRA_ARGS (nt)));
8712 else if (ARGUMENT_PACK_P (ot))
8713 {
8714 int i, len;
8715 tree opack, npack;
8716
8717 if (!ARGUMENT_PACK_P (nt))
8718 return 0;
8719
8720 opack = ARGUMENT_PACK_ARGS (ot);
8721 npack = ARGUMENT_PACK_ARGS (nt);
8722 len = TREE_VEC_LENGTH (opack);
8723 if (TREE_VEC_LENGTH (npack) != len)
8724 return 0;
8725 for (i = 0; i < len; ++i)
8726 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8727 TREE_VEC_ELT (npack, i)))
8728 return 0;
8729 return 1;
8730 }
8731 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8732 gcc_unreachable ();
8733 else if (TYPE_P (nt))
8734 {
8735 if (!TYPE_P (ot))
8736 return false;
8737 /* Don't treat an alias template specialization with dependent
8738 arguments as equivalent to its underlying type when used as a
8739 template argument; we need them to be distinct so that we
8740 substitute into the specialization arguments at instantiation
8741 time. And aliases can't be equivalent without being ==, so
8742 we don't need to look any deeper.
8743
8744 During partial ordering, however, we need to treat them normally so
8745 that we can order uses of the same alias with different
8746 cv-qualification (79960). */
8747 if (!partial_order
8748 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8749 return false;
8750 else
8751 return same_type_p (ot, nt);
8752 }
8753 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8754 return 0;
8755 else
8756 {
8757 /* Try to treat a template non-type argument that has been converted
8758 to the parameter type as equivalent to one that hasn't yet. */
8759 for (enum tree_code code1 = TREE_CODE (ot);
8760 CONVERT_EXPR_CODE_P (code1)
8761 || code1 == NON_LVALUE_EXPR;
8762 code1 = TREE_CODE (ot))
8763 ot = TREE_OPERAND (ot, 0);
8764 for (enum tree_code code2 = TREE_CODE (nt);
8765 CONVERT_EXPR_CODE_P (code2)
8766 || code2 == NON_LVALUE_EXPR;
8767 code2 = TREE_CODE (nt))
8768 nt = TREE_OPERAND (nt, 0);
8769
8770 return cp_tree_equal (ot, nt);
8771 }
8772 }
8773
8774 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8775 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8776 NEWARG_PTR with the offending arguments if they are non-NULL. */
8777
8778 int
8779 comp_template_args (tree oldargs, tree newargs,
8780 tree *oldarg_ptr, tree *newarg_ptr,
8781 bool partial_order)
8782 {
8783 int i;
8784
8785 if (oldargs == newargs)
8786 return 1;
8787
8788 if (!oldargs || !newargs)
8789 return 0;
8790
8791 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8792 return 0;
8793
8794 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8795 {
8796 tree nt = TREE_VEC_ELT (newargs, i);
8797 tree ot = TREE_VEC_ELT (oldargs, i);
8798
8799 if (! template_args_equal (ot, nt, partial_order))
8800 {
8801 if (oldarg_ptr != NULL)
8802 *oldarg_ptr = ot;
8803 if (newarg_ptr != NULL)
8804 *newarg_ptr = nt;
8805 return 0;
8806 }
8807 }
8808 return 1;
8809 }
8810
8811 inline bool
8812 comp_template_args_porder (tree oargs, tree nargs)
8813 {
8814 return comp_template_args (oargs, nargs, NULL, NULL, true);
8815 }
8816
8817 /* Implement a freelist interface for objects of type T.
8818
8819 Head is a separate object, rather than a regular member, so that we
8820 can define it as a GTY deletable pointer, which is highly
8821 desirable. A data member could be declared that way, but then the
8822 containing object would implicitly get GTY((user)), which would
8823 prevent us from instantiating freelists as global objects.
8824 Although this way we can create freelist global objects, they're
8825 such thin wrappers that instantiating temporaries at every use
8826 loses nothing and saves permanent storage for the freelist object.
8827
8828 Member functions next, anew, poison and reinit have default
8829 implementations that work for most of the types we're interested
8830 in, but if they don't work for some type, they should be explicitly
8831 specialized. See the comments before them for requirements, and
8832 the example specializations for the tree_list_freelist. */
8833 template <typename T>
8834 class freelist
8835 {
8836 /* Return the next object in a chain. We could just do type
8837 punning, but if we access the object with its underlying type, we
8838 avoid strict-aliasing trouble. This needs only work between
8839 poison and reinit. */
8840 static T *&next (T *obj) { return obj->next; }
8841
8842 /* Return a newly allocated, uninitialized or minimally-initialized
8843 object of type T. Any initialization performed by anew should
8844 either remain across the life of the object and the execution of
8845 poison, or be redone by reinit. */
8846 static T *anew () { return ggc_alloc<T> (); }
8847
8848 /* Optionally scribble all over the bits holding the object, so that
8849 they become (mostly?) uninitialized memory. This is called while
8850 preparing to make the object part of the free list. */
8851 static void poison (T *obj) {
8852 T *p ATTRIBUTE_UNUSED = obj;
8853 T **q ATTRIBUTE_UNUSED = &next (obj);
8854
8855 #ifdef ENABLE_GC_CHECKING
8856 /* Poison the data, to indicate the data is garbage. */
8857 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8858 memset (p, 0xa5, sizeof (*p));
8859 #endif
8860 /* Let valgrind know the object is free. */
8861 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8862
8863 /* Let valgrind know the next portion of the object is available,
8864 but uninitialized. */
8865 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8866 }
8867
8868 /* Bring an object that underwent at least one lifecycle after anew
8869 and before the most recent free and poison, back to a usable
8870 state, reinitializing whatever is needed for it to be
8871 functionally equivalent to an object just allocated and returned
8872 by anew. This may poison or clear the next field, used by
8873 freelist housekeeping after poison was called. */
8874 static void reinit (T *obj) {
8875 T **q ATTRIBUTE_UNUSED = &next (obj);
8876
8877 #ifdef ENABLE_GC_CHECKING
8878 memset (q, 0xa5, sizeof (*q));
8879 #endif
8880 /* Let valgrind know the entire object is available, but
8881 uninitialized. */
8882 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8883 }
8884
8885 /* Reference a GTY-deletable pointer that points to the first object
8886 in the free list proper. */
8887 T *&head;
8888 public:
8889 /* Construct a freelist object chaining objects off of HEAD. */
8890 freelist (T *&head) : head(head) {}
8891
8892 /* Add OBJ to the free object list. The former head becomes OBJ's
8893 successor. */
8894 void free (T *obj)
8895 {
8896 poison (obj);
8897 next (obj) = head;
8898 head = obj;
8899 }
8900
8901 /* Take an object from the free list, if one is available, or
8902 allocate a new one. Objects taken from the free list should be
8903 regarded as filled with garbage, except for bits that are
8904 configured to be preserved across free and alloc. */
8905 T *alloc ()
8906 {
8907 if (head)
8908 {
8909 T *obj = head;
8910 head = next (head);
8911 reinit (obj);
8912 return obj;
8913 }
8914 else
8915 return anew ();
8916 }
8917 };
8918
8919 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8920 want to allocate a TREE_LIST using the usual interface, and ensure
8921 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8922 build_tree_list logic in reinit, so this could go out of sync. */
8923 template <>
8924 inline tree &
8925 freelist<tree_node>::next (tree obj)
8926 {
8927 return TREE_CHAIN (obj);
8928 }
8929 template <>
8930 inline tree
8931 freelist<tree_node>::anew ()
8932 {
8933 return build_tree_list (NULL, NULL);
8934 }
8935 template <>
8936 inline void
8937 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8938 {
8939 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8940 tree p ATTRIBUTE_UNUSED = obj;
8941 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8942 tree *q ATTRIBUTE_UNUSED = &next (obj);
8943
8944 #ifdef ENABLE_GC_CHECKING
8945 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8946
8947 /* Poison the data, to indicate the data is garbage. */
8948 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8949 memset (p, 0xa5, size);
8950 #endif
8951 /* Let valgrind know the object is free. */
8952 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8953 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8954 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8955 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8956
8957 #ifdef ENABLE_GC_CHECKING
8958 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8959 /* Keep TREE_CHAIN functional. */
8960 TREE_SET_CODE (obj, TREE_LIST);
8961 #else
8962 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8963 #endif
8964 }
8965 template <>
8966 inline void
8967 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
8968 {
8969 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8970
8971 #ifdef ENABLE_GC_CHECKING
8972 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8973 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8974 memset (obj, 0, sizeof (tree_list));
8975 #endif
8976
8977 /* Let valgrind know the entire object is available, but
8978 uninitialized. */
8979 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8980
8981 #ifdef ENABLE_GC_CHECKING
8982 TREE_SET_CODE (obj, TREE_LIST);
8983 #else
8984 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8985 #endif
8986 }
8987
8988 /* Point to the first object in the TREE_LIST freelist. */
8989 static GTY((deletable)) tree tree_list_freelist_head;
8990 /* Return the/an actual TREE_LIST freelist. */
8991 static inline freelist<tree_node>
8992 tree_list_freelist ()
8993 {
8994 return tree_list_freelist_head;
8995 }
8996
8997 /* Point to the first object in the tinst_level freelist. */
8998 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
8999 /* Return the/an actual tinst_level freelist. */
9000 static inline freelist<tinst_level>
9001 tinst_level_freelist ()
9002 {
9003 return tinst_level_freelist_head;
9004 }
9005
9006 /* Point to the first object in the pending_template freelist. */
9007 static GTY((deletable)) pending_template *pending_template_freelist_head;
9008 /* Return the/an actual pending_template freelist. */
9009 static inline freelist<pending_template>
9010 pending_template_freelist ()
9011 {
9012 return pending_template_freelist_head;
9013 }
9014
9015 /* Build the TREE_LIST object out of a split list, store it
9016 permanently, and return it. */
9017 tree
9018 tinst_level::to_list ()
9019 {
9020 gcc_assert (split_list_p ());
9021 tree ret = tree_list_freelist ().alloc ();
9022 TREE_PURPOSE (ret) = tldcl;
9023 TREE_VALUE (ret) = targs;
9024 tldcl = ret;
9025 targs = NULL;
9026 gcc_assert (tree_list_p ());
9027 return ret;
9028 }
9029
9030 const unsigned short tinst_level::refcount_infinity;
9031
9032 /* Increment OBJ's refcount unless it is already infinite. */
9033 static tinst_level *
9034 inc_refcount_use (tinst_level *obj)
9035 {
9036 if (obj && obj->refcount != tinst_level::refcount_infinity)
9037 ++obj->refcount;
9038 return obj;
9039 }
9040
9041 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9042 void
9043 tinst_level::free (tinst_level *obj)
9044 {
9045 if (obj->tree_list_p ())
9046 tree_list_freelist ().free (obj->get_node ());
9047 tinst_level_freelist ().free (obj);
9048 }
9049
9050 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9051 OBJ's DECL and OBJ, and start over with the tinst_level object that
9052 used to be referenced by OBJ's NEXT. */
9053 static void
9054 dec_refcount_use (tinst_level *obj)
9055 {
9056 while (obj
9057 && obj->refcount != tinst_level::refcount_infinity
9058 && !--obj->refcount)
9059 {
9060 tinst_level *next = obj->next;
9061 tinst_level::free (obj);
9062 obj = next;
9063 }
9064 }
9065
9066 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9067 and of the former PTR. Omitting the second argument is equivalent
9068 to passing (T*)NULL; this is allowed because passing the
9069 zero-valued integral constant NULL confuses type deduction and/or
9070 overload resolution. */
9071 template <typename T>
9072 static void
9073 set_refcount_ptr (T *& ptr, T *obj = NULL)
9074 {
9075 T *save = ptr;
9076 ptr = inc_refcount_use (obj);
9077 dec_refcount_use (save);
9078 }
9079
9080 static void
9081 add_pending_template (tree d)
9082 {
9083 tree ti = (TYPE_P (d)
9084 ? CLASSTYPE_TEMPLATE_INFO (d)
9085 : DECL_TEMPLATE_INFO (d));
9086 struct pending_template *pt;
9087 int level;
9088
9089 if (TI_PENDING_TEMPLATE_FLAG (ti))
9090 return;
9091
9092 /* We are called both from instantiate_decl, where we've already had a
9093 tinst_level pushed, and instantiate_template, where we haven't.
9094 Compensate. */
9095 gcc_assert (TREE_CODE (d) != TREE_LIST);
9096 level = !current_tinst_level
9097 || current_tinst_level->maybe_get_node () != d;
9098
9099 if (level)
9100 push_tinst_level (d);
9101
9102 pt = pending_template_freelist ().alloc ();
9103 pt->next = NULL;
9104 pt->tinst = NULL;
9105 set_refcount_ptr (pt->tinst, current_tinst_level);
9106 if (last_pending_template)
9107 last_pending_template->next = pt;
9108 else
9109 pending_templates = pt;
9110
9111 last_pending_template = pt;
9112
9113 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9114
9115 if (level)
9116 pop_tinst_level ();
9117 }
9118
9119
9120 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9121 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9122 documentation for TEMPLATE_ID_EXPR. */
9123
9124 tree
9125 lookup_template_function (tree fns, tree arglist)
9126 {
9127 if (fns == error_mark_node || arglist == error_mark_node)
9128 return error_mark_node;
9129
9130 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9131
9132 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9133 {
9134 error ("%q#D is not a function template", fns);
9135 return error_mark_node;
9136 }
9137
9138 if (BASELINK_P (fns))
9139 {
9140 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9141 unknown_type_node,
9142 BASELINK_FUNCTIONS (fns),
9143 arglist);
9144 return fns;
9145 }
9146
9147 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9148 }
9149
9150 /* Within the scope of a template class S<T>, the name S gets bound
9151 (in build_self_reference) to a TYPE_DECL for the class, not a
9152 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9153 or one of its enclosing classes, and that type is a template,
9154 return the associated TEMPLATE_DECL. Otherwise, the original
9155 DECL is returned.
9156
9157 Also handle the case when DECL is a TREE_LIST of ambiguous
9158 injected-class-names from different bases. */
9159
9160 tree
9161 maybe_get_template_decl_from_type_decl (tree decl)
9162 {
9163 if (decl == NULL_TREE)
9164 return decl;
9165
9166 /* DR 176: A lookup that finds an injected-class-name (10.2
9167 [class.member.lookup]) can result in an ambiguity in certain cases
9168 (for example, if it is found in more than one base class). If all of
9169 the injected-class-names that are found refer to specializations of
9170 the same class template, and if the name is followed by a
9171 template-argument-list, the reference refers to the class template
9172 itself and not a specialization thereof, and is not ambiguous. */
9173 if (TREE_CODE (decl) == TREE_LIST)
9174 {
9175 tree t, tmpl = NULL_TREE;
9176 for (t = decl; t; t = TREE_CHAIN (t))
9177 {
9178 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9179 if (!tmpl)
9180 tmpl = elt;
9181 else if (tmpl != elt)
9182 break;
9183 }
9184 if (tmpl && t == NULL_TREE)
9185 return tmpl;
9186 else
9187 return decl;
9188 }
9189
9190 return (decl != NULL_TREE
9191 && DECL_SELF_REFERENCE_P (decl)
9192 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9193 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9194 }
9195
9196 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9197 parameters, find the desired type.
9198
9199 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9200
9201 IN_DECL, if non-NULL, is the template declaration we are trying to
9202 instantiate.
9203
9204 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9205 the class we are looking up.
9206
9207 Issue error and warning messages under control of COMPLAIN.
9208
9209 If the template class is really a local class in a template
9210 function, then the FUNCTION_CONTEXT is the function in which it is
9211 being instantiated.
9212
9213 ??? Note that this function is currently called *twice* for each
9214 template-id: the first time from the parser, while creating the
9215 incomplete type (finish_template_type), and the second type during the
9216 real instantiation (instantiate_template_class). This is surely something
9217 that we want to avoid. It also causes some problems with argument
9218 coercion (see convert_nontype_argument for more information on this). */
9219
9220 static tree
9221 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9222 int entering_scope, tsubst_flags_t complain)
9223 {
9224 tree templ = NULL_TREE, parmlist;
9225 tree t;
9226 spec_entry **slot;
9227 spec_entry *entry;
9228 spec_entry elt;
9229 hashval_t hash;
9230
9231 if (identifier_p (d1))
9232 {
9233 tree value = innermost_non_namespace_value (d1);
9234 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9235 templ = value;
9236 else
9237 {
9238 if (context)
9239 push_decl_namespace (context);
9240 templ = lookup_name (d1);
9241 templ = maybe_get_template_decl_from_type_decl (templ);
9242 if (context)
9243 pop_decl_namespace ();
9244 }
9245 if (templ)
9246 context = DECL_CONTEXT (templ);
9247 }
9248 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9249 {
9250 tree type = TREE_TYPE (d1);
9251
9252 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9253 an implicit typename for the second A. Deal with it. */
9254 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9255 type = TREE_TYPE (type);
9256
9257 if (CLASSTYPE_TEMPLATE_INFO (type))
9258 {
9259 templ = CLASSTYPE_TI_TEMPLATE (type);
9260 d1 = DECL_NAME (templ);
9261 }
9262 }
9263 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9264 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9265 {
9266 templ = TYPE_TI_TEMPLATE (d1);
9267 d1 = DECL_NAME (templ);
9268 }
9269 else if (DECL_TYPE_TEMPLATE_P (d1))
9270 {
9271 templ = d1;
9272 d1 = DECL_NAME (templ);
9273 context = DECL_CONTEXT (templ);
9274 }
9275 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9276 {
9277 templ = d1;
9278 d1 = DECL_NAME (templ);
9279 }
9280
9281 /* Issue an error message if we didn't find a template. */
9282 if (! templ)
9283 {
9284 if (complain & tf_error)
9285 error ("%qT is not a template", d1);
9286 return error_mark_node;
9287 }
9288
9289 if (TREE_CODE (templ) != TEMPLATE_DECL
9290 /* Make sure it's a user visible template, if it was named by
9291 the user. */
9292 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9293 && !PRIMARY_TEMPLATE_P (templ)))
9294 {
9295 if (complain & tf_error)
9296 {
9297 error ("non-template type %qT used as a template", d1);
9298 if (in_decl)
9299 error ("for template declaration %q+D", in_decl);
9300 }
9301 return error_mark_node;
9302 }
9303
9304 complain &= ~tf_user;
9305
9306 /* An alias that just changes the name of a template is equivalent to the
9307 other template, so if any of the arguments are pack expansions, strip
9308 the alias to avoid problems with a pack expansion passed to a non-pack
9309 alias template parameter (DR 1430). */
9310 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9311 templ = get_underlying_template (templ);
9312
9313 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9314 {
9315 tree parm;
9316 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9317 if (arglist2 == error_mark_node
9318 || (!uses_template_parms (arglist2)
9319 && check_instantiated_args (templ, arglist2, complain)))
9320 return error_mark_node;
9321
9322 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9323 return parm;
9324 }
9325 else
9326 {
9327 tree template_type = TREE_TYPE (templ);
9328 tree gen_tmpl;
9329 tree type_decl;
9330 tree found = NULL_TREE;
9331 int arg_depth;
9332 int parm_depth;
9333 int is_dependent_type;
9334 int use_partial_inst_tmpl = false;
9335
9336 if (template_type == error_mark_node)
9337 /* An error occurred while building the template TEMPL, and a
9338 diagnostic has most certainly been emitted for that
9339 already. Let's propagate that error. */
9340 return error_mark_node;
9341
9342 gen_tmpl = most_general_template (templ);
9343 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9344 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9345 arg_depth = TMPL_ARGS_DEPTH (arglist);
9346
9347 if (arg_depth == 1 && parm_depth > 1)
9348 {
9349 /* We've been given an incomplete set of template arguments.
9350 For example, given:
9351
9352 template <class T> struct S1 {
9353 template <class U> struct S2 {};
9354 template <class U> struct S2<U*> {};
9355 };
9356
9357 we will be called with an ARGLIST of `U*', but the
9358 TEMPLATE will be `template <class T> template
9359 <class U> struct S1<T>::S2'. We must fill in the missing
9360 arguments. */
9361 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9362 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9363 arg_depth = TMPL_ARGS_DEPTH (arglist);
9364 }
9365
9366 /* Now we should have enough arguments. */
9367 gcc_assert (parm_depth == arg_depth);
9368
9369 /* From here on, we're only interested in the most general
9370 template. */
9371
9372 /* Calculate the BOUND_ARGS. These will be the args that are
9373 actually tsubst'd into the definition to create the
9374 instantiation. */
9375 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9376 complain,
9377 /*require_all_args=*/true,
9378 /*use_default_args=*/true);
9379
9380 if (arglist == error_mark_node)
9381 /* We were unable to bind the arguments. */
9382 return error_mark_node;
9383
9384 /* In the scope of a template class, explicit references to the
9385 template class refer to the type of the template, not any
9386 instantiation of it. For example, in:
9387
9388 template <class T> class C { void f(C<T>); }
9389
9390 the `C<T>' is just the same as `C'. Outside of the
9391 class, however, such a reference is an instantiation. */
9392 if (entering_scope
9393 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9394 || currently_open_class (template_type))
9395 {
9396 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9397
9398 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9399 return template_type;
9400 }
9401
9402 /* If we already have this specialization, return it. */
9403 elt.tmpl = gen_tmpl;
9404 elt.args = arglist;
9405 elt.spec = NULL_TREE;
9406 hash = spec_hasher::hash (&elt);
9407 entry = type_specializations->find_with_hash (&elt, hash);
9408
9409 if (entry)
9410 return entry->spec;
9411
9412 /* If the the template's constraints are not satisfied,
9413 then we cannot form a valid type.
9414
9415 Note that the check is deferred until after the hash
9416 lookup. This prevents redundant checks on previously
9417 instantiated specializations. */
9418 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9419 {
9420 if (complain & tf_error)
9421 {
9422 auto_diagnostic_group d;
9423 error ("template constraint failure");
9424 diagnose_constraints (input_location, gen_tmpl, arglist);
9425 }
9426 return error_mark_node;
9427 }
9428
9429 is_dependent_type = uses_template_parms (arglist);
9430
9431 /* If the deduced arguments are invalid, then the binding
9432 failed. */
9433 if (!is_dependent_type
9434 && check_instantiated_args (gen_tmpl,
9435 INNERMOST_TEMPLATE_ARGS (arglist),
9436 complain))
9437 return error_mark_node;
9438
9439 if (!is_dependent_type
9440 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9441 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9442 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9443 {
9444 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9445 DECL_NAME (gen_tmpl),
9446 /*tag_scope=*/ts_global);
9447 return found;
9448 }
9449
9450 context = DECL_CONTEXT (gen_tmpl);
9451 if (context && TYPE_P (context))
9452 {
9453 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9454 context = complete_type (context);
9455 }
9456 else
9457 context = tsubst (context, arglist, complain, in_decl);
9458
9459 if (context == error_mark_node)
9460 return error_mark_node;
9461
9462 if (!context)
9463 context = global_namespace;
9464
9465 /* Create the type. */
9466 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9467 {
9468 /* The user referred to a specialization of an alias
9469 template represented by GEN_TMPL.
9470
9471 [temp.alias]/2 says:
9472
9473 When a template-id refers to the specialization of an
9474 alias template, it is equivalent to the associated
9475 type obtained by substitution of its
9476 template-arguments for the template-parameters in the
9477 type-id of the alias template. */
9478
9479 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9480 /* Note that the call above (by indirectly calling
9481 register_specialization in tsubst_decl) registers the
9482 TYPE_DECL representing the specialization of the alias
9483 template. So next time someone substitutes ARGLIST for
9484 the template parms into the alias template (GEN_TMPL),
9485 she'll get that TYPE_DECL back. */
9486
9487 if (t == error_mark_node)
9488 return t;
9489 }
9490 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9491 {
9492 if (!is_dependent_type)
9493 {
9494 set_current_access_from_decl (TYPE_NAME (template_type));
9495 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9496 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9497 arglist, complain, in_decl),
9498 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9499 arglist, complain, in_decl),
9500 SCOPED_ENUM_P (template_type), NULL);
9501
9502 if (t == error_mark_node)
9503 return t;
9504 }
9505 else
9506 {
9507 /* We don't want to call start_enum for this type, since
9508 the values for the enumeration constants may involve
9509 template parameters. And, no one should be interested
9510 in the enumeration constants for such a type. */
9511 t = cxx_make_type (ENUMERAL_TYPE);
9512 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9513 }
9514 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9515 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9516 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9517 }
9518 else if (CLASS_TYPE_P (template_type))
9519 {
9520 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9521 instantiated here. */
9522 gcc_assert (!LAMBDA_TYPE_P (template_type));
9523
9524 t = make_class_type (TREE_CODE (template_type));
9525 CLASSTYPE_DECLARED_CLASS (t)
9526 = CLASSTYPE_DECLARED_CLASS (template_type);
9527 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9528
9529 /* A local class. Make sure the decl gets registered properly. */
9530 if (context == current_function_decl)
9531 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9532 == error_mark_node)
9533 return error_mark_node;
9534
9535 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9536 /* This instantiation is another name for the primary
9537 template type. Set the TYPE_CANONICAL field
9538 appropriately. */
9539 TYPE_CANONICAL (t) = template_type;
9540 else if (any_template_arguments_need_structural_equality_p (arglist))
9541 /* Some of the template arguments require structural
9542 equality testing, so this template class requires
9543 structural equality testing. */
9544 SET_TYPE_STRUCTURAL_EQUALITY (t);
9545 }
9546 else
9547 gcc_unreachable ();
9548
9549 /* If we called start_enum or pushtag above, this information
9550 will already be set up. */
9551 if (!TYPE_NAME (t))
9552 {
9553 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9554
9555 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9556 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9557 DECL_SOURCE_LOCATION (type_decl)
9558 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9559 }
9560 else
9561 type_decl = TYPE_NAME (t);
9562
9563 if (CLASS_TYPE_P (template_type))
9564 {
9565 TREE_PRIVATE (type_decl)
9566 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9567 TREE_PROTECTED (type_decl)
9568 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9569 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9570 {
9571 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9572 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9573 }
9574 }
9575
9576 if (OVERLOAD_TYPE_P (t)
9577 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9578 {
9579 static const char *tags[] = {"abi_tag", "may_alias"};
9580
9581 for (unsigned ix = 0; ix != 2; ix++)
9582 {
9583 tree attributes
9584 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9585
9586 if (attributes)
9587 TYPE_ATTRIBUTES (t)
9588 = tree_cons (TREE_PURPOSE (attributes),
9589 TREE_VALUE (attributes),
9590 TYPE_ATTRIBUTES (t));
9591 }
9592 }
9593
9594 /* Let's consider the explicit specialization of a member
9595 of a class template specialization that is implicitly instantiated,
9596 e.g.:
9597 template<class T>
9598 struct S
9599 {
9600 template<class U> struct M {}; //#0
9601 };
9602
9603 template<>
9604 template<>
9605 struct S<int>::M<char> //#1
9606 {
9607 int i;
9608 };
9609 [temp.expl.spec]/4 says this is valid.
9610
9611 In this case, when we write:
9612 S<int>::M<char> m;
9613
9614 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9615 the one of #0.
9616
9617 When we encounter #1, we want to store the partial instantiation
9618 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9619
9620 For all cases other than this "explicit specialization of member of a
9621 class template", we just want to store the most general template into
9622 the CLASSTYPE_TI_TEMPLATE of M.
9623
9624 This case of "explicit specialization of member of a class template"
9625 only happens when:
9626 1/ the enclosing class is an instantiation of, and therefore not
9627 the same as, the context of the most general template, and
9628 2/ we aren't looking at the partial instantiation itself, i.e.
9629 the innermost arguments are not the same as the innermost parms of
9630 the most general template.
9631
9632 So it's only when 1/ and 2/ happens that we want to use the partial
9633 instantiation of the member template in lieu of its most general
9634 template. */
9635
9636 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9637 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9638 /* the enclosing class must be an instantiation... */
9639 && CLASS_TYPE_P (context)
9640 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9641 {
9642 TREE_VEC_LENGTH (arglist)--;
9643 ++processing_template_decl;
9644 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9645 tree partial_inst_args =
9646 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9647 arglist, complain, NULL_TREE);
9648 --processing_template_decl;
9649 TREE_VEC_LENGTH (arglist)++;
9650 if (partial_inst_args == error_mark_node)
9651 return error_mark_node;
9652 use_partial_inst_tmpl =
9653 /*...and we must not be looking at the partial instantiation
9654 itself. */
9655 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9656 partial_inst_args);
9657 }
9658
9659 if (!use_partial_inst_tmpl)
9660 /* This case is easy; there are no member templates involved. */
9661 found = gen_tmpl;
9662 else
9663 {
9664 /* This is a full instantiation of a member template. Find
9665 the partial instantiation of which this is an instance. */
9666
9667 /* Temporarily reduce by one the number of levels in the ARGLIST
9668 so as to avoid comparing the last set of arguments. */
9669 TREE_VEC_LENGTH (arglist)--;
9670 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9671 TREE_VEC_LENGTH (arglist)++;
9672 /* FOUND is either a proper class type, or an alias
9673 template specialization. In the later case, it's a
9674 TYPE_DECL, resulting from the substituting of arguments
9675 for parameters in the TYPE_DECL of the alias template
9676 done earlier. So be careful while getting the template
9677 of FOUND. */
9678 found = (TREE_CODE (found) == TEMPLATE_DECL
9679 ? found
9680 : (TREE_CODE (found) == TYPE_DECL
9681 ? DECL_TI_TEMPLATE (found)
9682 : CLASSTYPE_TI_TEMPLATE (found)));
9683
9684 if (DECL_CLASS_TEMPLATE_P (found)
9685 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9686 {
9687 /* If this partial instantiation is specialized, we want to
9688 use it for hash table lookup. */
9689 elt.tmpl = found;
9690 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9691 hash = spec_hasher::hash (&elt);
9692 }
9693 }
9694
9695 // Build template info for the new specialization.
9696 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9697
9698 elt.spec = t;
9699 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9700 gcc_checking_assert (*slot == NULL);
9701 entry = ggc_alloc<spec_entry> ();
9702 *entry = elt;
9703 *slot = entry;
9704
9705 /* Note this use of the partial instantiation so we can check it
9706 later in maybe_process_partial_specialization. */
9707 DECL_TEMPLATE_INSTANTIATIONS (found)
9708 = tree_cons (arglist, t,
9709 DECL_TEMPLATE_INSTANTIATIONS (found));
9710
9711 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9712 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9713 /* Now that the type has been registered on the instantiations
9714 list, we set up the enumerators. Because the enumeration
9715 constants may involve the enumeration type itself, we make
9716 sure to register the type first, and then create the
9717 constants. That way, doing tsubst_expr for the enumeration
9718 constants won't result in recursive calls here; we'll find
9719 the instantiation and exit above. */
9720 tsubst_enum (template_type, t, arglist);
9721
9722 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9723 /* If the type makes use of template parameters, the
9724 code that generates debugging information will crash. */
9725 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9726
9727 /* Possibly limit visibility based on template args. */
9728 TREE_PUBLIC (type_decl) = 1;
9729 determine_visibility (type_decl);
9730
9731 inherit_targ_abi_tags (t);
9732
9733 return t;
9734 }
9735 }
9736
9737 /* Wrapper for lookup_template_class_1. */
9738
9739 tree
9740 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9741 int entering_scope, tsubst_flags_t complain)
9742 {
9743 tree ret;
9744 timevar_push (TV_TEMPLATE_INST);
9745 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9746 entering_scope, complain);
9747 timevar_pop (TV_TEMPLATE_INST);
9748 return ret;
9749 }
9750
9751 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9752
9753 tree
9754 lookup_template_variable (tree templ, tree arglist)
9755 {
9756 /* The type of the expression is NULL_TREE since the template-id could refer
9757 to an explicit or partial specialization. */
9758 tree type = NULL_TREE;
9759 if (flag_concepts && variable_concept_p (templ))
9760 /* Except that concepts are always bool. */
9761 type = boolean_type_node;
9762 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9763 }
9764
9765 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9766
9767 tree
9768 finish_template_variable (tree var, tsubst_flags_t complain)
9769 {
9770 tree templ = TREE_OPERAND (var, 0);
9771 tree arglist = TREE_OPERAND (var, 1);
9772
9773 /* We never want to return a VAR_DECL for a variable concept, since they
9774 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9775 bool concept_p = flag_concepts && variable_concept_p (templ);
9776 if (concept_p && processing_template_decl)
9777 return var;
9778
9779 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9780 arglist = add_outermost_template_args (tmpl_args, arglist);
9781
9782 templ = most_general_template (templ);
9783 tree parms = DECL_TEMPLATE_PARMS (templ);
9784 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9785 /*req_all*/true,
9786 /*use_default*/true);
9787
9788 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9789 {
9790 if (complain & tf_error)
9791 {
9792 auto_diagnostic_group d;
9793 error ("use of invalid variable template %qE", var);
9794 diagnose_constraints (location_of (var), templ, arglist);
9795 }
9796 return error_mark_node;
9797 }
9798
9799 /* If a template-id refers to a specialization of a variable
9800 concept, then the expression is true if and only if the
9801 concept's constraints are satisfied by the given template
9802 arguments.
9803
9804 NOTE: This is an extension of Concepts Lite TS that
9805 allows constraints to be used in expressions. */
9806 if (concept_p)
9807 {
9808 tree decl = DECL_TEMPLATE_RESULT (templ);
9809 return evaluate_variable_concept (decl, arglist);
9810 }
9811
9812 return instantiate_template (templ, arglist, complain);
9813 }
9814
9815 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9816 TARGS template args, and instantiate it if it's not dependent. */
9817
9818 tree
9819 lookup_and_finish_template_variable (tree templ, tree targs,
9820 tsubst_flags_t complain)
9821 {
9822 templ = lookup_template_variable (templ, targs);
9823 if (!any_dependent_template_arguments_p (targs))
9824 {
9825 templ = finish_template_variable (templ, complain);
9826 mark_used (templ);
9827 }
9828
9829 return convert_from_reference (templ);
9830 }
9831
9832 \f
9833 struct pair_fn_data
9834 {
9835 tree_fn_t fn;
9836 tree_fn_t any_fn;
9837 void *data;
9838 /* True when we should also visit template parameters that occur in
9839 non-deduced contexts. */
9840 bool include_nondeduced_p;
9841 hash_set<tree> *visited;
9842 };
9843
9844 /* Called from for_each_template_parm via walk_tree. */
9845
9846 static tree
9847 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9848 {
9849 tree t = *tp;
9850 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9851 tree_fn_t fn = pfd->fn;
9852 void *data = pfd->data;
9853 tree result = NULL_TREE;
9854
9855 #define WALK_SUBTREE(NODE) \
9856 do \
9857 { \
9858 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9859 pfd->include_nondeduced_p, \
9860 pfd->any_fn); \
9861 if (result) goto out; \
9862 } \
9863 while (0)
9864
9865 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9866 return t;
9867
9868 if (TYPE_P (t)
9869 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9870 WALK_SUBTREE (TYPE_CONTEXT (t));
9871
9872 switch (TREE_CODE (t))
9873 {
9874 case RECORD_TYPE:
9875 if (TYPE_PTRMEMFUNC_P (t))
9876 break;
9877 /* Fall through. */
9878
9879 case UNION_TYPE:
9880 case ENUMERAL_TYPE:
9881 if (!TYPE_TEMPLATE_INFO (t))
9882 *walk_subtrees = 0;
9883 else
9884 WALK_SUBTREE (TYPE_TI_ARGS (t));
9885 break;
9886
9887 case INTEGER_TYPE:
9888 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9889 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9890 break;
9891
9892 case METHOD_TYPE:
9893 /* Since we're not going to walk subtrees, we have to do this
9894 explicitly here. */
9895 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9896 /* Fall through. */
9897
9898 case FUNCTION_TYPE:
9899 /* Check the return type. */
9900 WALK_SUBTREE (TREE_TYPE (t));
9901
9902 /* Check the parameter types. Since default arguments are not
9903 instantiated until they are needed, the TYPE_ARG_TYPES may
9904 contain expressions that involve template parameters. But,
9905 no-one should be looking at them yet. And, once they're
9906 instantiated, they don't contain template parameters, so
9907 there's no point in looking at them then, either. */
9908 {
9909 tree parm;
9910
9911 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9912 WALK_SUBTREE (TREE_VALUE (parm));
9913
9914 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9915 want walk_tree walking into them itself. */
9916 *walk_subtrees = 0;
9917 }
9918
9919 if (flag_noexcept_type)
9920 {
9921 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9922 if (spec)
9923 WALK_SUBTREE (TREE_PURPOSE (spec));
9924 }
9925 break;
9926
9927 case TYPEOF_TYPE:
9928 case DECLTYPE_TYPE:
9929 case UNDERLYING_TYPE:
9930 if (pfd->include_nondeduced_p
9931 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9932 pfd->visited,
9933 pfd->include_nondeduced_p,
9934 pfd->any_fn))
9935 return error_mark_node;
9936 *walk_subtrees = false;
9937 break;
9938
9939 case FUNCTION_DECL:
9940 case VAR_DECL:
9941 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9942 WALK_SUBTREE (DECL_TI_ARGS (t));
9943 /* Fall through. */
9944
9945 case PARM_DECL:
9946 case CONST_DECL:
9947 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9948 WALK_SUBTREE (DECL_INITIAL (t));
9949 if (DECL_CONTEXT (t)
9950 && pfd->include_nondeduced_p)
9951 WALK_SUBTREE (DECL_CONTEXT (t));
9952 break;
9953
9954 case BOUND_TEMPLATE_TEMPLATE_PARM:
9955 /* Record template parameters such as `T' inside `TT<T>'. */
9956 WALK_SUBTREE (TYPE_TI_ARGS (t));
9957 /* Fall through. */
9958
9959 case TEMPLATE_TEMPLATE_PARM:
9960 case TEMPLATE_TYPE_PARM:
9961 case TEMPLATE_PARM_INDEX:
9962 if (fn && (*fn)(t, data))
9963 return t;
9964 else if (!fn)
9965 return t;
9966 break;
9967
9968 case TEMPLATE_DECL:
9969 /* A template template parameter is encountered. */
9970 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9971 WALK_SUBTREE (TREE_TYPE (t));
9972
9973 /* Already substituted template template parameter */
9974 *walk_subtrees = 0;
9975 break;
9976
9977 case TYPENAME_TYPE:
9978 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9979 partial instantiation. */
9980 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9981 break;
9982
9983 case CONSTRUCTOR:
9984 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9985 && pfd->include_nondeduced_p)
9986 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9987 break;
9988
9989 case INDIRECT_REF:
9990 case COMPONENT_REF:
9991 /* If there's no type, then this thing must be some expression
9992 involving template parameters. */
9993 if (!fn && !TREE_TYPE (t))
9994 return error_mark_node;
9995 break;
9996
9997 case MODOP_EXPR:
9998 case CAST_EXPR:
9999 case IMPLICIT_CONV_EXPR:
10000 case REINTERPRET_CAST_EXPR:
10001 case CONST_CAST_EXPR:
10002 case STATIC_CAST_EXPR:
10003 case DYNAMIC_CAST_EXPR:
10004 case ARROW_EXPR:
10005 case DOTSTAR_EXPR:
10006 case TYPEID_EXPR:
10007 case PSEUDO_DTOR_EXPR:
10008 if (!fn)
10009 return error_mark_node;
10010 break;
10011
10012 default:
10013 break;
10014 }
10015
10016 #undef WALK_SUBTREE
10017
10018 /* We didn't find any template parameters we liked. */
10019 out:
10020 return result;
10021 }
10022
10023 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10024 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10025 call FN with the parameter and the DATA.
10026 If FN returns nonzero, the iteration is terminated, and
10027 for_each_template_parm returns 1. Otherwise, the iteration
10028 continues. If FN never returns a nonzero value, the value
10029 returned by for_each_template_parm is 0. If FN is NULL, it is
10030 considered to be the function which always returns 1.
10031
10032 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10033 parameters that occur in non-deduced contexts. When false, only
10034 visits those template parameters that can be deduced. */
10035
10036 static tree
10037 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10038 hash_set<tree> *visited,
10039 bool include_nondeduced_p,
10040 tree_fn_t any_fn)
10041 {
10042 struct pair_fn_data pfd;
10043 tree result;
10044
10045 /* Set up. */
10046 pfd.fn = fn;
10047 pfd.any_fn = any_fn;
10048 pfd.data = data;
10049 pfd.include_nondeduced_p = include_nondeduced_p;
10050
10051 /* Walk the tree. (Conceptually, we would like to walk without
10052 duplicates, but for_each_template_parm_r recursively calls
10053 for_each_template_parm, so we would need to reorganize a fair
10054 bit to use walk_tree_without_duplicates, so we keep our own
10055 visited list.) */
10056 if (visited)
10057 pfd.visited = visited;
10058 else
10059 pfd.visited = new hash_set<tree>;
10060 result = cp_walk_tree (&t,
10061 for_each_template_parm_r,
10062 &pfd,
10063 pfd.visited);
10064
10065 /* Clean up. */
10066 if (!visited)
10067 {
10068 delete pfd.visited;
10069 pfd.visited = 0;
10070 }
10071
10072 return result;
10073 }
10074
10075 /* Returns true if T depends on any template parameter. */
10076
10077 int
10078 uses_template_parms (tree t)
10079 {
10080 if (t == NULL_TREE)
10081 return false;
10082
10083 bool dependent_p;
10084 int saved_processing_template_decl;
10085
10086 saved_processing_template_decl = processing_template_decl;
10087 if (!saved_processing_template_decl)
10088 processing_template_decl = 1;
10089 if (TYPE_P (t))
10090 dependent_p = dependent_type_p (t);
10091 else if (TREE_CODE (t) == TREE_VEC)
10092 dependent_p = any_dependent_template_arguments_p (t);
10093 else if (TREE_CODE (t) == TREE_LIST)
10094 dependent_p = (uses_template_parms (TREE_VALUE (t))
10095 || uses_template_parms (TREE_CHAIN (t)));
10096 else if (TREE_CODE (t) == TYPE_DECL)
10097 dependent_p = dependent_type_p (TREE_TYPE (t));
10098 else if (DECL_P (t)
10099 || EXPR_P (t)
10100 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10101 || TREE_CODE (t) == OVERLOAD
10102 || BASELINK_P (t)
10103 || identifier_p (t)
10104 || TREE_CODE (t) == TRAIT_EXPR
10105 || TREE_CODE (t) == CONSTRUCTOR
10106 || CONSTANT_CLASS_P (t))
10107 dependent_p = (type_dependent_expression_p (t)
10108 || value_dependent_expression_p (t));
10109 else
10110 {
10111 gcc_assert (t == error_mark_node);
10112 dependent_p = false;
10113 }
10114
10115 processing_template_decl = saved_processing_template_decl;
10116
10117 return dependent_p;
10118 }
10119
10120 /* Returns true iff current_function_decl is an incompletely instantiated
10121 template. Useful instead of processing_template_decl because the latter
10122 is set to 0 during instantiate_non_dependent_expr. */
10123
10124 bool
10125 in_template_function (void)
10126 {
10127 tree fn = current_function_decl;
10128 bool ret;
10129 ++processing_template_decl;
10130 ret = (fn && DECL_LANG_SPECIFIC (fn)
10131 && DECL_TEMPLATE_INFO (fn)
10132 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10133 --processing_template_decl;
10134 return ret;
10135 }
10136
10137 /* Returns true if T depends on any template parameter with level LEVEL. */
10138
10139 bool
10140 uses_template_parms_level (tree t, int level)
10141 {
10142 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10143 /*include_nondeduced_p=*/true);
10144 }
10145
10146 /* Returns true if the signature of DECL depends on any template parameter from
10147 its enclosing class. */
10148
10149 bool
10150 uses_outer_template_parms (tree decl)
10151 {
10152 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10153 if (depth == 0)
10154 return false;
10155 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10156 &depth, NULL, /*include_nondeduced_p=*/true))
10157 return true;
10158 if (PRIMARY_TEMPLATE_P (decl)
10159 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10160 (DECL_TEMPLATE_PARMS (decl)),
10161 template_parm_outer_level,
10162 &depth, NULL, /*include_nondeduced_p=*/true))
10163 return true;
10164 tree ci = get_constraints (decl);
10165 if (ci)
10166 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10167 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10168 &depth, NULL, /*nondeduced*/true))
10169 return true;
10170 return false;
10171 }
10172
10173 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10174 ill-formed translation unit, i.e. a variable or function that isn't
10175 usable in a constant expression. */
10176
10177 static inline bool
10178 neglectable_inst_p (tree d)
10179 {
10180 return (d && DECL_P (d)
10181 && !undeduced_auto_decl (d)
10182 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10183 : decl_maybe_constant_var_p (d)));
10184 }
10185
10186 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10187 neglectable and instantiated from within an erroneous instantiation. */
10188
10189 static bool
10190 limit_bad_template_recursion (tree decl)
10191 {
10192 struct tinst_level *lev = current_tinst_level;
10193 int errs = errorcount + sorrycount;
10194 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10195 return false;
10196
10197 for (; lev; lev = lev->next)
10198 if (neglectable_inst_p (lev->maybe_get_node ()))
10199 break;
10200
10201 return (lev && errs > lev->errors);
10202 }
10203
10204 static int tinst_depth;
10205 extern int max_tinst_depth;
10206 int depth_reached;
10207
10208 static GTY(()) struct tinst_level *last_error_tinst_level;
10209
10210 /* We're starting to instantiate D; record the template instantiation context
10211 at LOC for diagnostics and to restore it later. */
10212
10213 static bool
10214 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10215 {
10216 struct tinst_level *new_level;
10217
10218 if (tinst_depth >= max_tinst_depth)
10219 {
10220 /* Tell error.c not to try to instantiate any templates. */
10221 at_eof = 2;
10222 fatal_error (input_location,
10223 "template instantiation depth exceeds maximum of %d"
10224 " (use %<-ftemplate-depth=%> to increase the maximum)",
10225 max_tinst_depth);
10226 return false;
10227 }
10228
10229 /* If the current instantiation caused problems, don't let it instantiate
10230 anything else. Do allow deduction substitution and decls usable in
10231 constant expressions. */
10232 if (!targs && limit_bad_template_recursion (tldcl))
10233 return false;
10234
10235 /* When not -quiet, dump template instantiations other than functions, since
10236 announce_function will take care of those. */
10237 if (!quiet_flag && !targs
10238 && TREE_CODE (tldcl) != TREE_LIST
10239 && TREE_CODE (tldcl) != FUNCTION_DECL)
10240 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10241
10242 new_level = tinst_level_freelist ().alloc ();
10243 new_level->tldcl = tldcl;
10244 new_level->targs = targs;
10245 new_level->locus = loc;
10246 new_level->errors = errorcount + sorrycount;
10247 new_level->next = NULL;
10248 new_level->refcount = 0;
10249 set_refcount_ptr (new_level->next, current_tinst_level);
10250 set_refcount_ptr (current_tinst_level, new_level);
10251
10252 ++tinst_depth;
10253 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10254 depth_reached = tinst_depth;
10255
10256 return true;
10257 }
10258
10259 /* We're starting substitution of TMPL<ARGS>; record the template
10260 substitution context for diagnostics and to restore it later. */
10261
10262 static bool
10263 push_tinst_level (tree tmpl, tree args)
10264 {
10265 return push_tinst_level_loc (tmpl, args, input_location);
10266 }
10267
10268 /* We're starting to instantiate D; record INPUT_LOCATION and the
10269 template instantiation context for diagnostics and to restore it
10270 later. */
10271
10272 bool
10273 push_tinst_level (tree d)
10274 {
10275 return push_tinst_level_loc (d, input_location);
10276 }
10277
10278 /* Likewise, but record LOC as the program location. */
10279
10280 bool
10281 push_tinst_level_loc (tree d, location_t loc)
10282 {
10283 gcc_assert (TREE_CODE (d) != TREE_LIST);
10284 return push_tinst_level_loc (d, NULL, loc);
10285 }
10286
10287 /* We're done instantiating this template; return to the instantiation
10288 context. */
10289
10290 void
10291 pop_tinst_level (void)
10292 {
10293 /* Restore the filename and line number stashed away when we started
10294 this instantiation. */
10295 input_location = current_tinst_level->locus;
10296 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10297 --tinst_depth;
10298 }
10299
10300 /* We're instantiating a deferred template; restore the template
10301 instantiation context in which the instantiation was requested, which
10302 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10303
10304 static tree
10305 reopen_tinst_level (struct tinst_level *level)
10306 {
10307 struct tinst_level *t;
10308
10309 tinst_depth = 0;
10310 for (t = level; t; t = t->next)
10311 ++tinst_depth;
10312
10313 set_refcount_ptr (current_tinst_level, level);
10314 pop_tinst_level ();
10315 if (current_tinst_level)
10316 current_tinst_level->errors = errorcount+sorrycount;
10317 return level->maybe_get_node ();
10318 }
10319
10320 /* Returns the TINST_LEVEL which gives the original instantiation
10321 context. */
10322
10323 struct tinst_level *
10324 outermost_tinst_level (void)
10325 {
10326 struct tinst_level *level = current_tinst_level;
10327 if (level)
10328 while (level->next)
10329 level = level->next;
10330 return level;
10331 }
10332
10333 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10334 vector of template arguments, as for tsubst.
10335
10336 Returns an appropriate tsubst'd friend declaration. */
10337
10338 static tree
10339 tsubst_friend_function (tree decl, tree args)
10340 {
10341 tree new_friend;
10342
10343 if (TREE_CODE (decl) == FUNCTION_DECL
10344 && DECL_TEMPLATE_INSTANTIATION (decl)
10345 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10346 /* This was a friend declared with an explicit template
10347 argument list, e.g.:
10348
10349 friend void f<>(T);
10350
10351 to indicate that f was a template instantiation, not a new
10352 function declaration. Now, we have to figure out what
10353 instantiation of what template. */
10354 {
10355 tree template_id, arglist, fns;
10356 tree new_args;
10357 tree tmpl;
10358 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10359
10360 /* Friend functions are looked up in the containing namespace scope.
10361 We must enter that scope, to avoid finding member functions of the
10362 current class with same name. */
10363 push_nested_namespace (ns);
10364 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10365 tf_warning_or_error, NULL_TREE,
10366 /*integral_constant_expression_p=*/false);
10367 pop_nested_namespace (ns);
10368 arglist = tsubst (DECL_TI_ARGS (decl), args,
10369 tf_warning_or_error, NULL_TREE);
10370 template_id = lookup_template_function (fns, arglist);
10371
10372 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10373 tmpl = determine_specialization (template_id, new_friend,
10374 &new_args,
10375 /*need_member_template=*/0,
10376 TREE_VEC_LENGTH (args),
10377 tsk_none);
10378 return instantiate_template (tmpl, new_args, tf_error);
10379 }
10380
10381 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10382
10383 /* The NEW_FRIEND will look like an instantiation, to the
10384 compiler, but is not an instantiation from the point of view of
10385 the language. For example, we might have had:
10386
10387 template <class T> struct S {
10388 template <class U> friend void f(T, U);
10389 };
10390
10391 Then, in S<int>, template <class U> void f(int, U) is not an
10392 instantiation of anything. */
10393 if (new_friend == error_mark_node)
10394 return error_mark_node;
10395
10396 DECL_USE_TEMPLATE (new_friend) = 0;
10397 if (TREE_CODE (decl) == TEMPLATE_DECL)
10398 {
10399 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10400 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10401 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10402 }
10403
10404 /* The mangled name for the NEW_FRIEND is incorrect. The function
10405 is not a template instantiation and should not be mangled like
10406 one. Therefore, we forget the mangling here; we'll recompute it
10407 later if we need it. */
10408 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10409 {
10410 SET_DECL_RTL (new_friend, NULL);
10411 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10412 }
10413
10414 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10415 {
10416 tree old_decl;
10417 tree new_friend_template_info;
10418 tree new_friend_result_template_info;
10419 tree ns;
10420 int new_friend_is_defn;
10421
10422 /* We must save some information from NEW_FRIEND before calling
10423 duplicate decls since that function will free NEW_FRIEND if
10424 possible. */
10425 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10426 new_friend_is_defn =
10427 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10428 (template_for_substitution (new_friend)))
10429 != NULL_TREE);
10430 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10431 {
10432 /* This declaration is a `primary' template. */
10433 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10434
10435 new_friend_result_template_info
10436 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10437 }
10438 else
10439 new_friend_result_template_info = NULL_TREE;
10440
10441 /* Inside pushdecl_namespace_level, we will push into the
10442 current namespace. However, the friend function should go
10443 into the namespace of the template. */
10444 ns = decl_namespace_context (new_friend);
10445 push_nested_namespace (ns);
10446 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10447 pop_nested_namespace (ns);
10448
10449 if (old_decl == error_mark_node)
10450 return error_mark_node;
10451
10452 if (old_decl != new_friend)
10453 {
10454 /* This new friend declaration matched an existing
10455 declaration. For example, given:
10456
10457 template <class T> void f(T);
10458 template <class U> class C {
10459 template <class T> friend void f(T) {}
10460 };
10461
10462 the friend declaration actually provides the definition
10463 of `f', once C has been instantiated for some type. So,
10464 old_decl will be the out-of-class template declaration,
10465 while new_friend is the in-class definition.
10466
10467 But, if `f' was called before this point, the
10468 instantiation of `f' will have DECL_TI_ARGS corresponding
10469 to `T' but not to `U', references to which might appear
10470 in the definition of `f'. Previously, the most general
10471 template for an instantiation of `f' was the out-of-class
10472 version; now it is the in-class version. Therefore, we
10473 run through all specialization of `f', adding to their
10474 DECL_TI_ARGS appropriately. In particular, they need a
10475 new set of outer arguments, corresponding to the
10476 arguments for this class instantiation.
10477
10478 The same situation can arise with something like this:
10479
10480 friend void f(int);
10481 template <class T> class C {
10482 friend void f(T) {}
10483 };
10484
10485 when `C<int>' is instantiated. Now, `f(int)' is defined
10486 in the class. */
10487
10488 if (!new_friend_is_defn)
10489 /* On the other hand, if the in-class declaration does
10490 *not* provide a definition, then we don't want to alter
10491 existing definitions. We can just leave everything
10492 alone. */
10493 ;
10494 else
10495 {
10496 tree new_template = TI_TEMPLATE (new_friend_template_info);
10497 tree new_args = TI_ARGS (new_friend_template_info);
10498
10499 /* Overwrite whatever template info was there before, if
10500 any, with the new template information pertaining to
10501 the declaration. */
10502 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10503
10504 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10505 {
10506 /* We should have called reregister_specialization in
10507 duplicate_decls. */
10508 gcc_assert (retrieve_specialization (new_template,
10509 new_args, 0)
10510 == old_decl);
10511
10512 /* Instantiate it if the global has already been used. */
10513 if (DECL_ODR_USED (old_decl))
10514 instantiate_decl (old_decl, /*defer_ok=*/true,
10515 /*expl_inst_class_mem_p=*/false);
10516 }
10517 else
10518 {
10519 tree t;
10520
10521 /* Indicate that the old function template is a partial
10522 instantiation. */
10523 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10524 = new_friend_result_template_info;
10525
10526 gcc_assert (new_template
10527 == most_general_template (new_template));
10528 gcc_assert (new_template != old_decl);
10529
10530 /* Reassign any specializations already in the hash table
10531 to the new more general template, and add the
10532 additional template args. */
10533 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10534 t != NULL_TREE;
10535 t = TREE_CHAIN (t))
10536 {
10537 tree spec = TREE_VALUE (t);
10538 spec_entry elt;
10539
10540 elt.tmpl = old_decl;
10541 elt.args = DECL_TI_ARGS (spec);
10542 elt.spec = NULL_TREE;
10543
10544 decl_specializations->remove_elt (&elt);
10545
10546 DECL_TI_ARGS (spec)
10547 = add_outermost_template_args (new_args,
10548 DECL_TI_ARGS (spec));
10549
10550 register_specialization
10551 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10552
10553 }
10554 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10555 }
10556 }
10557
10558 /* The information from NEW_FRIEND has been merged into OLD_DECL
10559 by duplicate_decls. */
10560 new_friend = old_decl;
10561 }
10562 }
10563 else
10564 {
10565 tree context = DECL_CONTEXT (new_friend);
10566 bool dependent_p;
10567
10568 /* In the code
10569 template <class T> class C {
10570 template <class U> friend void C1<U>::f (); // case 1
10571 friend void C2<T>::f (); // case 2
10572 };
10573 we only need to make sure CONTEXT is a complete type for
10574 case 2. To distinguish between the two cases, we note that
10575 CONTEXT of case 1 remains dependent type after tsubst while
10576 this isn't true for case 2. */
10577 ++processing_template_decl;
10578 dependent_p = dependent_type_p (context);
10579 --processing_template_decl;
10580
10581 if (!dependent_p
10582 && !complete_type_or_else (context, NULL_TREE))
10583 return error_mark_node;
10584
10585 if (COMPLETE_TYPE_P (context))
10586 {
10587 tree fn = new_friend;
10588 /* do_friend adds the TEMPLATE_DECL for any member friend
10589 template even if it isn't a member template, i.e.
10590 template <class T> friend A<T>::f();
10591 Look through it in that case. */
10592 if (TREE_CODE (fn) == TEMPLATE_DECL
10593 && !PRIMARY_TEMPLATE_P (fn))
10594 fn = DECL_TEMPLATE_RESULT (fn);
10595 /* Check to see that the declaration is really present, and,
10596 possibly obtain an improved declaration. */
10597 fn = check_classfn (context, fn, NULL_TREE);
10598
10599 if (fn)
10600 new_friend = fn;
10601 }
10602 }
10603
10604 return new_friend;
10605 }
10606
10607 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10608 template arguments, as for tsubst.
10609
10610 Returns an appropriate tsubst'd friend type or error_mark_node on
10611 failure. */
10612
10613 static tree
10614 tsubst_friend_class (tree friend_tmpl, tree args)
10615 {
10616 tree tmpl;
10617
10618 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10619 {
10620 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10621 return TREE_TYPE (tmpl);
10622 }
10623
10624 tree context = CP_DECL_CONTEXT (friend_tmpl);
10625 if (TREE_CODE (context) == NAMESPACE_DECL)
10626 push_nested_namespace (context);
10627 else
10628 {
10629 context = tsubst (context, args, tf_error, NULL_TREE);
10630 push_nested_class (context);
10631 }
10632
10633 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10634 /*non_class=*/false, /*block_p=*/false,
10635 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10636
10637 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10638 {
10639 /* The friend template has already been declared. Just
10640 check to see that the declarations match, and install any new
10641 default parameters. We must tsubst the default parameters,
10642 of course. We only need the innermost template parameters
10643 because that is all that redeclare_class_template will look
10644 at. */
10645 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10646 > TMPL_ARGS_DEPTH (args))
10647 {
10648 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10649 args, tf_warning_or_error);
10650 location_t saved_input_location = input_location;
10651 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10652 tree cons = get_constraints (tmpl);
10653 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10654 input_location = saved_input_location;
10655 }
10656 }
10657 else
10658 {
10659 /* The friend template has not already been declared. In this
10660 case, the instantiation of the template class will cause the
10661 injection of this template into the namespace scope. */
10662 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10663
10664 if (tmpl != error_mark_node)
10665 {
10666 /* The new TMPL is not an instantiation of anything, so we
10667 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10668 for the new type because that is supposed to be the
10669 corresponding template decl, i.e., TMPL. */
10670 DECL_USE_TEMPLATE (tmpl) = 0;
10671 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10672 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10673 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10674 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10675
10676 /* It is hidden. */
10677 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10678 DECL_ANTICIPATED (tmpl)
10679 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10680
10681 /* Inject this template into the enclosing namspace scope. */
10682 tmpl = pushdecl_namespace_level (tmpl, true);
10683 }
10684 }
10685
10686 if (TREE_CODE (context) == NAMESPACE_DECL)
10687 pop_nested_namespace (context);
10688 else
10689 pop_nested_class ();
10690
10691 return TREE_TYPE (tmpl);
10692 }
10693
10694 /* Returns zero if TYPE cannot be completed later due to circularity.
10695 Otherwise returns one. */
10696
10697 static int
10698 can_complete_type_without_circularity (tree type)
10699 {
10700 if (type == NULL_TREE || type == error_mark_node)
10701 return 0;
10702 else if (COMPLETE_TYPE_P (type))
10703 return 1;
10704 else if (TREE_CODE (type) == ARRAY_TYPE)
10705 return can_complete_type_without_circularity (TREE_TYPE (type));
10706 else if (CLASS_TYPE_P (type)
10707 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10708 return 0;
10709 else
10710 return 1;
10711 }
10712
10713 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10714 tsubst_flags_t, tree);
10715
10716 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10717 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10718
10719 static tree
10720 tsubst_attribute (tree t, tree *decl_p, tree args,
10721 tsubst_flags_t complain, tree in_decl)
10722 {
10723 gcc_assert (ATTR_IS_DEPENDENT (t));
10724
10725 tree val = TREE_VALUE (t);
10726 if (val == NULL_TREE)
10727 /* Nothing to do. */;
10728 else if ((flag_openmp || flag_openmp_simd)
10729 && is_attribute_p ("omp declare simd",
10730 get_attribute_name (t)))
10731 {
10732 tree clauses = TREE_VALUE (val);
10733 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10734 complain, in_decl);
10735 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10736 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10737 tree parms = DECL_ARGUMENTS (*decl_p);
10738 clauses
10739 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10740 if (clauses)
10741 val = build_tree_list (NULL_TREE, clauses);
10742 else
10743 val = NULL_TREE;
10744 }
10745 /* If the first attribute argument is an identifier, don't
10746 pass it through tsubst. Attributes like mode, format,
10747 cleanup and several target specific attributes expect it
10748 unmodified. */
10749 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10750 {
10751 tree chain
10752 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10753 /*integral_constant_expression_p=*/false);
10754 if (chain != TREE_CHAIN (val))
10755 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10756 }
10757 else if (PACK_EXPANSION_P (val))
10758 {
10759 /* An attribute pack expansion. */
10760 tree purp = TREE_PURPOSE (t);
10761 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10762 if (pack == error_mark_node)
10763 return error_mark_node;
10764 int len = TREE_VEC_LENGTH (pack);
10765 tree list = NULL_TREE;
10766 tree *q = &list;
10767 for (int i = 0; i < len; ++i)
10768 {
10769 tree elt = TREE_VEC_ELT (pack, i);
10770 *q = build_tree_list (purp, elt);
10771 q = &TREE_CHAIN (*q);
10772 }
10773 return list;
10774 }
10775 else
10776 val = tsubst_expr (val, args, complain, in_decl,
10777 /*integral_constant_expression_p=*/false);
10778
10779 if (val != TREE_VALUE (t))
10780 return build_tree_list (TREE_PURPOSE (t), val);
10781 return t;
10782 }
10783
10784 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10785 unchanged or a new TREE_LIST chain. */
10786
10787 static tree
10788 tsubst_attributes (tree attributes, tree args,
10789 tsubst_flags_t complain, tree in_decl)
10790 {
10791 tree last_dep = NULL_TREE;
10792
10793 for (tree t = attributes; t; t = TREE_CHAIN (t))
10794 if (ATTR_IS_DEPENDENT (t))
10795 {
10796 last_dep = t;
10797 attributes = copy_list (attributes);
10798 break;
10799 }
10800
10801 if (last_dep)
10802 for (tree *p = &attributes; *p; )
10803 {
10804 tree t = *p;
10805 if (ATTR_IS_DEPENDENT (t))
10806 {
10807 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10808 if (subst != t)
10809 {
10810 *p = subst;
10811 while (*p)
10812 p = &TREE_CHAIN (*p);
10813 *p = TREE_CHAIN (t);
10814 continue;
10815 }
10816 }
10817 p = &TREE_CHAIN (*p);
10818 }
10819
10820 return attributes;
10821 }
10822
10823 /* Apply any attributes which had to be deferred until instantiation
10824 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10825 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10826
10827 static void
10828 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10829 tree args, tsubst_flags_t complain, tree in_decl)
10830 {
10831 tree last_dep = NULL_TREE;
10832 tree t;
10833 tree *p;
10834
10835 if (attributes == NULL_TREE)
10836 return;
10837
10838 if (DECL_P (*decl_p))
10839 {
10840 if (TREE_TYPE (*decl_p) == error_mark_node)
10841 return;
10842 p = &DECL_ATTRIBUTES (*decl_p);
10843 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10844 to our attributes parameter. */
10845 gcc_assert (*p == attributes);
10846 }
10847 else
10848 {
10849 p = &TYPE_ATTRIBUTES (*decl_p);
10850 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10851 lookup_template_class_1, and should be preserved. */
10852 gcc_assert (*p != attributes);
10853 while (*p)
10854 p = &TREE_CHAIN (*p);
10855 }
10856
10857 for (t = attributes; t; t = TREE_CHAIN (t))
10858 if (ATTR_IS_DEPENDENT (t))
10859 {
10860 last_dep = t;
10861 attributes = copy_list (attributes);
10862 break;
10863 }
10864
10865 *p = attributes;
10866 if (last_dep)
10867 {
10868 tree late_attrs = NULL_TREE;
10869 tree *q = &late_attrs;
10870
10871 for (; *p; )
10872 {
10873 t = *p;
10874 if (ATTR_IS_DEPENDENT (t))
10875 {
10876 *p = TREE_CHAIN (t);
10877 TREE_CHAIN (t) = NULL_TREE;
10878 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10879 while (*q)
10880 q = &TREE_CHAIN (*q);
10881 }
10882 else
10883 p = &TREE_CHAIN (t);
10884 }
10885
10886 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10887 }
10888 }
10889
10890 /* Perform (or defer) access check for typedefs that were referenced
10891 from within the template TMPL code.
10892 This is a subroutine of instantiate_decl and instantiate_class_template.
10893 TMPL is the template to consider and TARGS is the list of arguments of
10894 that template. */
10895
10896 static void
10897 perform_typedefs_access_check (tree tmpl, tree targs)
10898 {
10899 location_t saved_location;
10900 unsigned i;
10901 qualified_typedef_usage_t *iter;
10902
10903 if (!tmpl
10904 || (!CLASS_TYPE_P (tmpl)
10905 && TREE_CODE (tmpl) != FUNCTION_DECL))
10906 return;
10907
10908 saved_location = input_location;
10909 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10910 {
10911 tree type_decl = iter->typedef_decl;
10912 tree type_scope = iter->context;
10913
10914 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10915 continue;
10916
10917 if (uses_template_parms (type_decl))
10918 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10919 if (uses_template_parms (type_scope))
10920 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10921
10922 /* Make access check error messages point to the location
10923 of the use of the typedef. */
10924 input_location = iter->locus;
10925 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10926 type_decl, type_decl,
10927 tf_warning_or_error);
10928 }
10929 input_location = saved_location;
10930 }
10931
10932 static tree
10933 instantiate_class_template_1 (tree type)
10934 {
10935 tree templ, args, pattern, t, member;
10936 tree typedecl;
10937 tree pbinfo;
10938 tree base_list;
10939 unsigned int saved_maximum_field_alignment;
10940 tree fn_context;
10941
10942 if (type == error_mark_node)
10943 return error_mark_node;
10944
10945 if (COMPLETE_OR_OPEN_TYPE_P (type)
10946 || uses_template_parms (type))
10947 return type;
10948
10949 /* Figure out which template is being instantiated. */
10950 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10951 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10952
10953 /* Mark the type as in the process of being defined. */
10954 TYPE_BEING_DEFINED (type) = 1;
10955
10956 /* We may be in the middle of deferred access check. Disable
10957 it now. */
10958 deferring_access_check_sentinel acs (dk_no_deferred);
10959
10960 /* Determine what specialization of the original template to
10961 instantiate. */
10962 t = most_specialized_partial_spec (type, tf_warning_or_error);
10963 if (t == error_mark_node)
10964 return error_mark_node;
10965 else if (t)
10966 {
10967 /* This TYPE is actually an instantiation of a partial
10968 specialization. We replace the innermost set of ARGS with
10969 the arguments appropriate for substitution. For example,
10970 given:
10971
10972 template <class T> struct S {};
10973 template <class T> struct S<T*> {};
10974
10975 and supposing that we are instantiating S<int*>, ARGS will
10976 presently be {int*} -- but we need {int}. */
10977 pattern = TREE_TYPE (t);
10978 args = TREE_PURPOSE (t);
10979 }
10980 else
10981 {
10982 pattern = TREE_TYPE (templ);
10983 args = CLASSTYPE_TI_ARGS (type);
10984 }
10985
10986 /* If the template we're instantiating is incomplete, then clearly
10987 there's nothing we can do. */
10988 if (!COMPLETE_TYPE_P (pattern))
10989 {
10990 /* We can try again later. */
10991 TYPE_BEING_DEFINED (type) = 0;
10992 return type;
10993 }
10994
10995 /* If we've recursively instantiated too many templates, stop. */
10996 if (! push_tinst_level (type))
10997 return type;
10998
10999 int saved_unevaluated_operand = cp_unevaluated_operand;
11000 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11001
11002 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11003 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11004 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11005 fn_context = error_mark_node;
11006 if (!fn_context)
11007 push_to_top_level ();
11008 else
11009 {
11010 cp_unevaluated_operand = 0;
11011 c_inhibit_evaluation_warnings = 0;
11012 }
11013 /* Use #pragma pack from the template context. */
11014 saved_maximum_field_alignment = maximum_field_alignment;
11015 maximum_field_alignment = TYPE_PRECISION (pattern);
11016
11017 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11018
11019 /* Set the input location to the most specialized template definition.
11020 This is needed if tsubsting causes an error. */
11021 typedecl = TYPE_MAIN_DECL (pattern);
11022 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11023 DECL_SOURCE_LOCATION (typedecl);
11024
11025 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11026 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11027 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11028 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11029 if (ANON_AGGR_TYPE_P (pattern))
11030 SET_ANON_AGGR_TYPE_P (type);
11031 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11032 {
11033 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11034 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11035 /* Adjust visibility for template arguments. */
11036 determine_visibility (TYPE_MAIN_DECL (type));
11037 }
11038 if (CLASS_TYPE_P (type))
11039 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11040
11041 pbinfo = TYPE_BINFO (pattern);
11042
11043 /* We should never instantiate a nested class before its enclosing
11044 class; we need to look up the nested class by name before we can
11045 instantiate it, and that lookup should instantiate the enclosing
11046 class. */
11047 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11048 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11049
11050 base_list = NULL_TREE;
11051 if (BINFO_N_BASE_BINFOS (pbinfo))
11052 {
11053 tree pbase_binfo;
11054 tree pushed_scope;
11055 int i;
11056
11057 /* We must enter the scope containing the type, as that is where
11058 the accessibility of types named in dependent bases are
11059 looked up from. */
11060 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11061
11062 /* Substitute into each of the bases to determine the actual
11063 basetypes. */
11064 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11065 {
11066 tree base;
11067 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11068 tree expanded_bases = NULL_TREE;
11069 int idx, len = 1;
11070
11071 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11072 {
11073 expanded_bases =
11074 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11075 args, tf_error, NULL_TREE);
11076 if (expanded_bases == error_mark_node)
11077 continue;
11078
11079 len = TREE_VEC_LENGTH (expanded_bases);
11080 }
11081
11082 for (idx = 0; idx < len; idx++)
11083 {
11084 if (expanded_bases)
11085 /* Extract the already-expanded base class. */
11086 base = TREE_VEC_ELT (expanded_bases, idx);
11087 else
11088 /* Substitute to figure out the base class. */
11089 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11090 NULL_TREE);
11091
11092 if (base == error_mark_node)
11093 continue;
11094
11095 base_list = tree_cons (access, base, base_list);
11096 if (BINFO_VIRTUAL_P (pbase_binfo))
11097 TREE_TYPE (base_list) = integer_type_node;
11098 }
11099 }
11100
11101 /* The list is now in reverse order; correct that. */
11102 base_list = nreverse (base_list);
11103
11104 if (pushed_scope)
11105 pop_scope (pushed_scope);
11106 }
11107 /* Now call xref_basetypes to set up all the base-class
11108 information. */
11109 xref_basetypes (type, base_list);
11110
11111 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11112 (int) ATTR_FLAG_TYPE_IN_PLACE,
11113 args, tf_error, NULL_TREE);
11114 fixup_attribute_variants (type);
11115
11116 /* Now that our base classes are set up, enter the scope of the
11117 class, so that name lookups into base classes, etc. will work
11118 correctly. This is precisely analogous to what we do in
11119 begin_class_definition when defining an ordinary non-template
11120 class, except we also need to push the enclosing classes. */
11121 push_nested_class (type);
11122
11123 /* Now members are processed in the order of declaration. */
11124 for (member = CLASSTYPE_DECL_LIST (pattern);
11125 member; member = TREE_CHAIN (member))
11126 {
11127 tree t = TREE_VALUE (member);
11128
11129 if (TREE_PURPOSE (member))
11130 {
11131 if (TYPE_P (t))
11132 {
11133 if (LAMBDA_TYPE_P (t))
11134 /* A closure type for a lambda in an NSDMI or default argument.
11135 Ignore it; it will be regenerated when needed. */
11136 continue;
11137
11138 /* Build new CLASSTYPE_NESTED_UTDS. */
11139
11140 tree newtag;
11141 bool class_template_p;
11142
11143 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11144 && TYPE_LANG_SPECIFIC (t)
11145 && CLASSTYPE_IS_TEMPLATE (t));
11146 /* If the member is a class template, then -- even after
11147 substitution -- there may be dependent types in the
11148 template argument list for the class. We increment
11149 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11150 that function will assume that no types are dependent
11151 when outside of a template. */
11152 if (class_template_p)
11153 ++processing_template_decl;
11154 newtag = tsubst (t, args, tf_error, NULL_TREE);
11155 if (class_template_p)
11156 --processing_template_decl;
11157 if (newtag == error_mark_node)
11158 continue;
11159
11160 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11161 {
11162 tree name = TYPE_IDENTIFIER (t);
11163
11164 if (class_template_p)
11165 /* Unfortunately, lookup_template_class sets
11166 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11167 instantiation (i.e., for the type of a member
11168 template class nested within a template class.)
11169 This behavior is required for
11170 maybe_process_partial_specialization to work
11171 correctly, but is not accurate in this case;
11172 the TAG is not an instantiation of anything.
11173 (The corresponding TEMPLATE_DECL is an
11174 instantiation, but the TYPE is not.) */
11175 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11176
11177 /* Now, we call pushtag to put this NEWTAG into the scope of
11178 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11179 pushtag calling push_template_decl. We don't have to do
11180 this for enums because it will already have been done in
11181 tsubst_enum. */
11182 if (name)
11183 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11184 pushtag (name, newtag, /*tag_scope=*/ts_current);
11185 }
11186 }
11187 else if (DECL_DECLARES_FUNCTION_P (t))
11188 {
11189 tree r;
11190
11191 if (TREE_CODE (t) == TEMPLATE_DECL)
11192 ++processing_template_decl;
11193 r = tsubst (t, args, tf_error, NULL_TREE);
11194 if (TREE_CODE (t) == TEMPLATE_DECL)
11195 --processing_template_decl;
11196 set_current_access_from_decl (r);
11197 finish_member_declaration (r);
11198 /* Instantiate members marked with attribute used. */
11199 if (r != error_mark_node && DECL_PRESERVE_P (r))
11200 mark_used (r);
11201 if (TREE_CODE (r) == FUNCTION_DECL
11202 && DECL_OMP_DECLARE_REDUCTION_P (r))
11203 cp_check_omp_declare_reduction (r);
11204 }
11205 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11206 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11207 /* A closure type for a lambda in an NSDMI or default argument.
11208 Ignore it; it will be regenerated when needed. */;
11209 else
11210 {
11211 /* Build new TYPE_FIELDS. */
11212 if (TREE_CODE (t) == STATIC_ASSERT)
11213 {
11214 tree condition;
11215
11216 ++c_inhibit_evaluation_warnings;
11217 condition =
11218 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11219 tf_warning_or_error, NULL_TREE,
11220 /*integral_constant_expression_p=*/true);
11221 --c_inhibit_evaluation_warnings;
11222
11223 finish_static_assert (condition,
11224 STATIC_ASSERT_MESSAGE (t),
11225 STATIC_ASSERT_SOURCE_LOCATION (t),
11226 /*member_p=*/true);
11227 }
11228 else if (TREE_CODE (t) != CONST_DECL)
11229 {
11230 tree r;
11231 tree vec = NULL_TREE;
11232 int len = 1;
11233
11234 /* The file and line for this declaration, to
11235 assist in error message reporting. Since we
11236 called push_tinst_level above, we don't need to
11237 restore these. */
11238 input_location = DECL_SOURCE_LOCATION (t);
11239
11240 if (TREE_CODE (t) == TEMPLATE_DECL)
11241 ++processing_template_decl;
11242 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11243 if (TREE_CODE (t) == TEMPLATE_DECL)
11244 --processing_template_decl;
11245
11246 if (TREE_CODE (r) == TREE_VEC)
11247 {
11248 /* A capture pack became multiple fields. */
11249 vec = r;
11250 len = TREE_VEC_LENGTH (vec);
11251 }
11252
11253 for (int i = 0; i < len; ++i)
11254 {
11255 if (vec)
11256 r = TREE_VEC_ELT (vec, i);
11257 if (VAR_P (r))
11258 {
11259 /* In [temp.inst]:
11260
11261 [t]he initialization (and any associated
11262 side-effects) of a static data member does
11263 not occur unless the static data member is
11264 itself used in a way that requires the
11265 definition of the static data member to
11266 exist.
11267
11268 Therefore, we do not substitute into the
11269 initialized for the static data member here. */
11270 finish_static_data_member_decl
11271 (r,
11272 /*init=*/NULL_TREE,
11273 /*init_const_expr_p=*/false,
11274 /*asmspec_tree=*/NULL_TREE,
11275 /*flags=*/0);
11276 /* Instantiate members marked with attribute used. */
11277 if (r != error_mark_node && DECL_PRESERVE_P (r))
11278 mark_used (r);
11279 }
11280 else if (TREE_CODE (r) == FIELD_DECL)
11281 {
11282 /* Determine whether R has a valid type and can be
11283 completed later. If R is invalid, then its type
11284 is replaced by error_mark_node. */
11285 tree rtype = TREE_TYPE (r);
11286 if (can_complete_type_without_circularity (rtype))
11287 complete_type (rtype);
11288
11289 if (!complete_or_array_type_p (rtype))
11290 {
11291 /* If R's type couldn't be completed and
11292 it isn't a flexible array member (whose
11293 type is incomplete by definition) give
11294 an error. */
11295 cxx_incomplete_type_error (r, rtype);
11296 TREE_TYPE (r) = error_mark_node;
11297 }
11298 else if (TREE_CODE (rtype) == ARRAY_TYPE
11299 && TYPE_DOMAIN (rtype) == NULL_TREE
11300 && (TREE_CODE (type) == UNION_TYPE
11301 || TREE_CODE (type) == QUAL_UNION_TYPE))
11302 {
11303 error ("flexible array member %qD in union", r);
11304 TREE_TYPE (r) = error_mark_node;
11305 }
11306 }
11307
11308 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11309 such a thing will already have been added to the field
11310 list by tsubst_enum in finish_member_declaration in the
11311 CLASSTYPE_NESTED_UTDS case above. */
11312 if (!(TREE_CODE (r) == TYPE_DECL
11313 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11314 && DECL_ARTIFICIAL (r)))
11315 {
11316 set_current_access_from_decl (r);
11317 finish_member_declaration (r);
11318 }
11319 }
11320 }
11321 }
11322 }
11323 else
11324 {
11325 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11326 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11327 {
11328 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11329
11330 tree friend_type = t;
11331 bool adjust_processing_template_decl = false;
11332
11333 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11334 {
11335 /* template <class T> friend class C; */
11336 friend_type = tsubst_friend_class (friend_type, args);
11337 adjust_processing_template_decl = true;
11338 }
11339 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11340 {
11341 /* template <class T> friend class C::D; */
11342 friend_type = tsubst (friend_type, args,
11343 tf_warning_or_error, NULL_TREE);
11344 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11345 friend_type = TREE_TYPE (friend_type);
11346 adjust_processing_template_decl = true;
11347 }
11348 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11349 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11350 {
11351 /* This could be either
11352
11353 friend class T::C;
11354
11355 when dependent_type_p is false or
11356
11357 template <class U> friend class T::C;
11358
11359 otherwise. */
11360 /* Bump processing_template_decl in case this is something like
11361 template <class T> friend struct A<T>::B. */
11362 ++processing_template_decl;
11363 friend_type = tsubst (friend_type, args,
11364 tf_warning_or_error, NULL_TREE);
11365 if (dependent_type_p (friend_type))
11366 adjust_processing_template_decl = true;
11367 --processing_template_decl;
11368 }
11369 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11370 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11371 && TYPE_HIDDEN_P (friend_type))
11372 {
11373 /* friend class C;
11374
11375 where C hasn't been declared yet. Let's lookup name
11376 from namespace scope directly, bypassing any name that
11377 come from dependent base class. */
11378 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11379
11380 /* The call to xref_tag_from_type does injection for friend
11381 classes. */
11382 push_nested_namespace (ns);
11383 friend_type =
11384 xref_tag_from_type (friend_type, NULL_TREE,
11385 /*tag_scope=*/ts_current);
11386 pop_nested_namespace (ns);
11387 }
11388 else if (uses_template_parms (friend_type))
11389 /* friend class C<T>; */
11390 friend_type = tsubst (friend_type, args,
11391 tf_warning_or_error, NULL_TREE);
11392 /* Otherwise it's
11393
11394 friend class C;
11395
11396 where C is already declared or
11397
11398 friend class C<int>;
11399
11400 We don't have to do anything in these cases. */
11401
11402 if (adjust_processing_template_decl)
11403 /* Trick make_friend_class into realizing that the friend
11404 we're adding is a template, not an ordinary class. It's
11405 important that we use make_friend_class since it will
11406 perform some error-checking and output cross-reference
11407 information. */
11408 ++processing_template_decl;
11409
11410 if (friend_type != error_mark_node)
11411 make_friend_class (type, friend_type, /*complain=*/false);
11412
11413 if (adjust_processing_template_decl)
11414 --processing_template_decl;
11415 }
11416 else
11417 {
11418 /* Build new DECL_FRIENDLIST. */
11419 tree r;
11420
11421 /* The file and line for this declaration, to
11422 assist in error message reporting. Since we
11423 called push_tinst_level above, we don't need to
11424 restore these. */
11425 input_location = DECL_SOURCE_LOCATION (t);
11426
11427 if (TREE_CODE (t) == TEMPLATE_DECL)
11428 {
11429 ++processing_template_decl;
11430 push_deferring_access_checks (dk_no_check);
11431 }
11432
11433 r = tsubst_friend_function (t, args);
11434 add_friend (type, r, /*complain=*/false);
11435 if (TREE_CODE (t) == TEMPLATE_DECL)
11436 {
11437 pop_deferring_access_checks ();
11438 --processing_template_decl;
11439 }
11440 }
11441 }
11442 }
11443
11444 if (fn_context)
11445 {
11446 /* Restore these before substituting into the lambda capture
11447 initializers. */
11448 cp_unevaluated_operand = saved_unevaluated_operand;
11449 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11450 }
11451
11452 /* Set the file and line number information to whatever is given for
11453 the class itself. This puts error messages involving generated
11454 implicit functions at a predictable point, and the same point
11455 that would be used for non-template classes. */
11456 input_location = DECL_SOURCE_LOCATION (typedecl);
11457
11458 unreverse_member_declarations (type);
11459 finish_struct_1 (type);
11460 TYPE_BEING_DEFINED (type) = 0;
11461
11462 /* We don't instantiate default arguments for member functions. 14.7.1:
11463
11464 The implicit instantiation of a class template specialization causes
11465 the implicit instantiation of the declarations, but not of the
11466 definitions or default arguments, of the class member functions,
11467 member classes, static data members and member templates.... */
11468
11469 /* Some typedefs referenced from within the template code need to be access
11470 checked at template instantiation time, i.e now. These types were
11471 added to the template at parsing time. Let's get those and perform
11472 the access checks then. */
11473 perform_typedefs_access_check (pattern, args);
11474 perform_deferred_access_checks (tf_warning_or_error);
11475 pop_nested_class ();
11476 maximum_field_alignment = saved_maximum_field_alignment;
11477 if (!fn_context)
11478 pop_from_top_level ();
11479 pop_tinst_level ();
11480
11481 /* The vtable for a template class can be emitted in any translation
11482 unit in which the class is instantiated. When there is no key
11483 method, however, finish_struct_1 will already have added TYPE to
11484 the keyed_classes. */
11485 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11486 vec_safe_push (keyed_classes, type);
11487
11488 return type;
11489 }
11490
11491 /* Wrapper for instantiate_class_template_1. */
11492
11493 tree
11494 instantiate_class_template (tree type)
11495 {
11496 tree ret;
11497 timevar_push (TV_TEMPLATE_INST);
11498 ret = instantiate_class_template_1 (type);
11499 timevar_pop (TV_TEMPLATE_INST);
11500 return ret;
11501 }
11502
11503 static tree
11504 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11505 {
11506 tree r;
11507
11508 if (!t)
11509 r = t;
11510 else if (TYPE_P (t))
11511 r = tsubst (t, args, complain, in_decl);
11512 else
11513 {
11514 if (!(complain & tf_warning))
11515 ++c_inhibit_evaluation_warnings;
11516 r = tsubst_expr (t, args, complain, in_decl,
11517 /*integral_constant_expression_p=*/true);
11518 if (!(complain & tf_warning))
11519 --c_inhibit_evaluation_warnings;
11520 }
11521 return r;
11522 }
11523
11524 /* Given a function parameter pack TMPL_PARM and some function parameters
11525 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11526 and set *SPEC_P to point at the next point in the list. */
11527
11528 tree
11529 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11530 {
11531 /* Collect all of the extra "packed" parameters into an
11532 argument pack. */
11533 tree parmvec;
11534 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11535 tree spec_parm = *spec_p;
11536 int i, len;
11537
11538 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11539 if (tmpl_parm
11540 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11541 break;
11542
11543 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11544 parmvec = make_tree_vec (len);
11545 spec_parm = *spec_p;
11546 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11547 {
11548 tree elt = spec_parm;
11549 if (DECL_PACK_P (elt))
11550 elt = make_pack_expansion (elt);
11551 TREE_VEC_ELT (parmvec, i) = elt;
11552 }
11553
11554 /* Build the argument packs. */
11555 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11556 *spec_p = spec_parm;
11557
11558 return argpack;
11559 }
11560
11561 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11562 NONTYPE_ARGUMENT_PACK. */
11563
11564 static tree
11565 make_fnparm_pack (tree spec_parm)
11566 {
11567 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11568 }
11569
11570 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11571 pack expansion with no extra args, 2 if it has extra args, or 0
11572 if it is not a pack expansion. */
11573
11574 static int
11575 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11576 {
11577 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11578 /* We're being called before this happens in tsubst_pack_expansion. */
11579 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11580 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11581 if (i >= TREE_VEC_LENGTH (vec))
11582 return 0;
11583 tree elt = TREE_VEC_ELT (vec, i);
11584 if (DECL_P (elt))
11585 /* A decl pack is itself an expansion. */
11586 elt = TREE_TYPE (elt);
11587 if (!PACK_EXPANSION_P (elt))
11588 return 0;
11589 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11590 return 2;
11591 return 1;
11592 }
11593
11594
11595 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11596
11597 static tree
11598 make_argument_pack_select (tree arg_pack, unsigned index)
11599 {
11600 tree aps = make_node (ARGUMENT_PACK_SELECT);
11601
11602 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11603 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11604
11605 return aps;
11606 }
11607
11608 /* This is a subroutine of tsubst_pack_expansion.
11609
11610 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11611 mechanism to store the (non complete list of) arguments of the
11612 substitution and return a non substituted pack expansion, in order
11613 to wait for when we have enough arguments to really perform the
11614 substitution. */
11615
11616 static bool
11617 use_pack_expansion_extra_args_p (tree parm_packs,
11618 int arg_pack_len,
11619 bool has_empty_arg)
11620 {
11621 /* If one pack has an expansion and another pack has a normal
11622 argument or if one pack has an empty argument and an another
11623 one hasn't then tsubst_pack_expansion cannot perform the
11624 substitution and need to fall back on the
11625 PACK_EXPANSION_EXTRA mechanism. */
11626 if (parm_packs == NULL_TREE)
11627 return false;
11628 else if (has_empty_arg)
11629 return true;
11630
11631 bool has_expansion_arg = false;
11632 for (int i = 0 ; i < arg_pack_len; ++i)
11633 {
11634 bool has_non_expansion_arg = false;
11635 for (tree parm_pack = parm_packs;
11636 parm_pack;
11637 parm_pack = TREE_CHAIN (parm_pack))
11638 {
11639 tree arg = TREE_VALUE (parm_pack);
11640
11641 int exp = argument_pack_element_is_expansion_p (arg, i);
11642 if (exp == 2)
11643 /* We can't substitute a pack expansion with extra args into
11644 our pattern. */
11645 return true;
11646 else if (exp)
11647 has_expansion_arg = true;
11648 else
11649 has_non_expansion_arg = true;
11650 }
11651
11652 if (has_expansion_arg && has_non_expansion_arg)
11653 return true;
11654 }
11655 return false;
11656 }
11657
11658 /* [temp.variadic]/6 says that:
11659
11660 The instantiation of a pack expansion [...]
11661 produces a list E1,E2, ..., En, where N is the number of elements
11662 in the pack expansion parameters.
11663
11664 This subroutine of tsubst_pack_expansion produces one of these Ei.
11665
11666 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11667 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11668 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11669 INDEX is the index 'i' of the element Ei to produce. ARGS,
11670 COMPLAIN, and IN_DECL are the same parameters as for the
11671 tsubst_pack_expansion function.
11672
11673 The function returns the resulting Ei upon successful completion,
11674 or error_mark_node.
11675
11676 Note that this function possibly modifies the ARGS parameter, so
11677 it's the responsibility of the caller to restore it. */
11678
11679 static tree
11680 gen_elem_of_pack_expansion_instantiation (tree pattern,
11681 tree parm_packs,
11682 unsigned index,
11683 tree args /* This parm gets
11684 modified. */,
11685 tsubst_flags_t complain,
11686 tree in_decl)
11687 {
11688 tree t;
11689 bool ith_elem_is_expansion = false;
11690
11691 /* For each parameter pack, change the substitution of the parameter
11692 pack to the ith argument in its argument pack, then expand the
11693 pattern. */
11694 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11695 {
11696 tree parm = TREE_PURPOSE (pack);
11697 tree arg_pack = TREE_VALUE (pack);
11698 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11699
11700 ith_elem_is_expansion |=
11701 argument_pack_element_is_expansion_p (arg_pack, index);
11702
11703 /* Select the Ith argument from the pack. */
11704 if (TREE_CODE (parm) == PARM_DECL
11705 || VAR_P (parm)
11706 || TREE_CODE (parm) == FIELD_DECL)
11707 {
11708 if (index == 0)
11709 {
11710 aps = make_argument_pack_select (arg_pack, index);
11711 if (!mark_used (parm, complain) && !(complain & tf_error))
11712 return error_mark_node;
11713 register_local_specialization (aps, parm);
11714 }
11715 else
11716 aps = retrieve_local_specialization (parm);
11717 }
11718 else
11719 {
11720 int idx, level;
11721 template_parm_level_and_index (parm, &level, &idx);
11722
11723 if (index == 0)
11724 {
11725 aps = make_argument_pack_select (arg_pack, index);
11726 /* Update the corresponding argument. */
11727 TMPL_ARG (args, level, idx) = aps;
11728 }
11729 else
11730 /* Re-use the ARGUMENT_PACK_SELECT. */
11731 aps = TMPL_ARG (args, level, idx);
11732 }
11733 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11734 }
11735
11736 // Any local specialization bindings arising from this substitution
11737 // cannot be reused for a different INDEX.
11738 local_specialization_stack lss (lss_copy);
11739
11740 /* Substitute into the PATTERN with the (possibly altered)
11741 arguments. */
11742 if (pattern == in_decl)
11743 /* Expanding a fixed parameter pack from
11744 coerce_template_parameter_pack. */
11745 t = tsubst_decl (pattern, args, complain);
11746 else if (pattern == error_mark_node)
11747 t = error_mark_node;
11748 else if (constraint_p (pattern))
11749 {
11750 if (processing_template_decl)
11751 t = tsubst_constraint (pattern, args, complain, in_decl);
11752 else
11753 t = (constraints_satisfied_p (pattern, args)
11754 ? boolean_true_node : boolean_false_node);
11755 }
11756 else if (!TYPE_P (pattern))
11757 t = tsubst_expr (pattern, args, complain, in_decl,
11758 /*integral_constant_expression_p=*/false);
11759 else
11760 t = tsubst (pattern, args, complain, in_decl);
11761
11762 /* If the Ith argument pack element is a pack expansion, then
11763 the Ith element resulting from the substituting is going to
11764 be a pack expansion as well. */
11765 if (ith_elem_is_expansion)
11766 t = make_pack_expansion (t, complain);
11767
11768 return t;
11769 }
11770
11771 /* When the unexpanded parameter pack in a fold expression expands to an empty
11772 sequence, the value of the expression is as follows; the program is
11773 ill-formed if the operator is not listed in this table.
11774
11775 && true
11776 || false
11777 , void() */
11778
11779 tree
11780 expand_empty_fold (tree t, tsubst_flags_t complain)
11781 {
11782 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11783 if (!FOLD_EXPR_MODIFY_P (t))
11784 switch (code)
11785 {
11786 case TRUTH_ANDIF_EXPR:
11787 return boolean_true_node;
11788 case TRUTH_ORIF_EXPR:
11789 return boolean_false_node;
11790 case COMPOUND_EXPR:
11791 return void_node;
11792 default:
11793 break;
11794 }
11795
11796 if (complain & tf_error)
11797 error_at (location_of (t),
11798 "fold of empty expansion over %O", code);
11799 return error_mark_node;
11800 }
11801
11802 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11803 form an expression that combines the two terms using the
11804 operator of T. */
11805
11806 static tree
11807 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11808 {
11809 tree op = FOLD_EXPR_OP (t);
11810 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11811
11812 // Handle compound assignment operators.
11813 if (FOLD_EXPR_MODIFY_P (t))
11814 return build_x_modify_expr (input_location, left, code, right, complain);
11815
11816 switch (code)
11817 {
11818 case COMPOUND_EXPR:
11819 return build_x_compound_expr (input_location, left, right, complain);
11820 default:
11821 return build_x_binary_op (input_location, code,
11822 left, TREE_CODE (left),
11823 right, TREE_CODE (right),
11824 /*overload=*/NULL,
11825 complain);
11826 }
11827 }
11828
11829 /* Substitute ARGS into the pack of a fold expression T. */
11830
11831 static inline tree
11832 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11833 {
11834 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11835 }
11836
11837 /* Substitute ARGS into the pack of a fold expression T. */
11838
11839 static inline tree
11840 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11841 {
11842 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11843 }
11844
11845 /* Expand a PACK of arguments into a grouped as left fold.
11846 Given a pack containing elements A0, A1, ..., An and an
11847 operator @, this builds the expression:
11848
11849 ((A0 @ A1) @ A2) ... @ An
11850
11851 Note that PACK must not be empty.
11852
11853 The operator is defined by the original fold expression T. */
11854
11855 static tree
11856 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11857 {
11858 tree left = TREE_VEC_ELT (pack, 0);
11859 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11860 {
11861 tree right = TREE_VEC_ELT (pack, i);
11862 left = fold_expression (t, left, right, complain);
11863 }
11864 return left;
11865 }
11866
11867 /* Substitute into a unary left fold expression. */
11868
11869 static tree
11870 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11871 tree in_decl)
11872 {
11873 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11874 if (pack == error_mark_node)
11875 return error_mark_node;
11876 if (PACK_EXPANSION_P (pack))
11877 {
11878 tree r = copy_node (t);
11879 FOLD_EXPR_PACK (r) = pack;
11880 return r;
11881 }
11882 if (TREE_VEC_LENGTH (pack) == 0)
11883 return expand_empty_fold (t, complain);
11884 else
11885 return expand_left_fold (t, pack, complain);
11886 }
11887
11888 /* Substitute into a binary left fold expression.
11889
11890 Do ths by building a single (non-empty) vector of argumnts and
11891 building the expression from those elements. */
11892
11893 static tree
11894 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11895 tree in_decl)
11896 {
11897 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11898 if (pack == error_mark_node)
11899 return error_mark_node;
11900 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11901 if (init == error_mark_node)
11902 return error_mark_node;
11903
11904 if (PACK_EXPANSION_P (pack))
11905 {
11906 tree r = copy_node (t);
11907 FOLD_EXPR_PACK (r) = pack;
11908 FOLD_EXPR_INIT (r) = init;
11909 return r;
11910 }
11911
11912 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11913 TREE_VEC_ELT (vec, 0) = init;
11914 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11915 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11916
11917 return expand_left_fold (t, vec, complain);
11918 }
11919
11920 /* Expand a PACK of arguments into a grouped as right fold.
11921 Given a pack containing elementns A0, A1, ..., and an
11922 operator @, this builds the expression:
11923
11924 A0@ ... (An-2 @ (An-1 @ An))
11925
11926 Note that PACK must not be empty.
11927
11928 The operator is defined by the original fold expression T. */
11929
11930 tree
11931 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11932 {
11933 // Build the expression.
11934 int n = TREE_VEC_LENGTH (pack);
11935 tree right = TREE_VEC_ELT (pack, n - 1);
11936 for (--n; n != 0; --n)
11937 {
11938 tree left = TREE_VEC_ELT (pack, n - 1);
11939 right = fold_expression (t, left, right, complain);
11940 }
11941 return right;
11942 }
11943
11944 /* Substitute into a unary right fold expression. */
11945
11946 static tree
11947 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11948 tree in_decl)
11949 {
11950 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11951 if (pack == error_mark_node)
11952 return error_mark_node;
11953 if (PACK_EXPANSION_P (pack))
11954 {
11955 tree r = copy_node (t);
11956 FOLD_EXPR_PACK (r) = pack;
11957 return r;
11958 }
11959 if (TREE_VEC_LENGTH (pack) == 0)
11960 return expand_empty_fold (t, complain);
11961 else
11962 return expand_right_fold (t, pack, complain);
11963 }
11964
11965 /* Substitute into a binary right fold expression.
11966
11967 Do ths by building a single (non-empty) vector of arguments and
11968 building the expression from those elements. */
11969
11970 static tree
11971 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11972 tree in_decl)
11973 {
11974 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11975 if (pack == error_mark_node)
11976 return error_mark_node;
11977 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11978 if (init == error_mark_node)
11979 return error_mark_node;
11980
11981 if (PACK_EXPANSION_P (pack))
11982 {
11983 tree r = copy_node (t);
11984 FOLD_EXPR_PACK (r) = pack;
11985 FOLD_EXPR_INIT (r) = init;
11986 return r;
11987 }
11988
11989 int n = TREE_VEC_LENGTH (pack);
11990 tree vec = make_tree_vec (n + 1);
11991 for (int i = 0; i < n; ++i)
11992 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11993 TREE_VEC_ELT (vec, n) = init;
11994
11995 return expand_right_fold (t, vec, complain);
11996 }
11997
11998 /* Walk through the pattern of a pack expansion, adding everything in
11999 local_specializations to a list. */
12000
12001 struct el_data
12002 {
12003 hash_set<tree> internal;
12004 tree extra;
12005 tsubst_flags_t complain;
12006
12007 el_data (tsubst_flags_t c)
12008 : extra (NULL_TREE), complain (c) {}
12009 };
12010 static tree
12011 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12012 {
12013 el_data &data = *reinterpret_cast<el_data*>(data_);
12014 tree *extra = &data.extra;
12015 tsubst_flags_t complain = data.complain;
12016
12017 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12018 /* Remember local typedefs (85214). */
12019 tp = &TYPE_NAME (*tp);
12020
12021 if (TREE_CODE (*tp) == DECL_EXPR)
12022 data.internal.add (DECL_EXPR_DECL (*tp));
12023 else if (tree spec = retrieve_local_specialization (*tp))
12024 {
12025 if (data.internal.contains (*tp))
12026 /* Don't mess with variables declared within the pattern. */
12027 return NULL_TREE;
12028 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12029 {
12030 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12031 tree args = ARGUMENT_PACK_ARGS (spec);
12032 if (TREE_VEC_LENGTH (args) == 1)
12033 {
12034 tree elt = TREE_VEC_ELT (args, 0);
12035 if (PACK_EXPANSION_P (elt))
12036 elt = PACK_EXPANSION_PATTERN (elt);
12037 if (DECL_PACK_P (elt))
12038 spec = elt;
12039 }
12040 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12041 {
12042 /* Handle lambda capture here, since we aren't doing any
12043 substitution now, and so tsubst_copy won't call
12044 process_outer_var_ref. */
12045 tree args = ARGUMENT_PACK_ARGS (spec);
12046 int len = TREE_VEC_LENGTH (args);
12047 for (int i = 0; i < len; ++i)
12048 {
12049 tree arg = TREE_VEC_ELT (args, i);
12050 tree carg = arg;
12051 if (outer_automatic_var_p (arg))
12052 carg = process_outer_var_ref (arg, complain);
12053 if (carg != arg)
12054 {
12055 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12056 proxies. */
12057 if (i == 0)
12058 {
12059 spec = copy_node (spec);
12060 args = copy_node (args);
12061 SET_ARGUMENT_PACK_ARGS (spec, args);
12062 register_local_specialization (spec, *tp);
12063 }
12064 TREE_VEC_ELT (args, i) = carg;
12065 }
12066 }
12067 }
12068 }
12069 if (outer_automatic_var_p (spec))
12070 spec = process_outer_var_ref (spec, complain);
12071 *extra = tree_cons (*tp, spec, *extra);
12072 }
12073 return NULL_TREE;
12074 }
12075 static tree
12076 extract_local_specs (tree pattern, tsubst_flags_t complain)
12077 {
12078 el_data data (complain);
12079 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12080 return data.extra;
12081 }
12082
12083 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12084 for use in PACK_EXPANSION_EXTRA_ARGS. */
12085
12086 tree
12087 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12088 {
12089 tree extra = args;
12090 if (local_specializations)
12091 if (tree locals = extract_local_specs (pattern, complain))
12092 extra = tree_cons (NULL_TREE, extra, locals);
12093 return extra;
12094 }
12095
12096 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12097 normal template args to ARGS. */
12098
12099 tree
12100 add_extra_args (tree extra, tree args)
12101 {
12102 if (extra && TREE_CODE (extra) == TREE_LIST)
12103 {
12104 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12105 {
12106 /* The partial instantiation involved local declarations collected in
12107 extract_local_specs; map from the general template to our local
12108 context. */
12109 tree gen = TREE_PURPOSE (elt);
12110 tree inst = TREE_VALUE (elt);
12111 if (DECL_P (inst))
12112 if (tree local = retrieve_local_specialization (inst))
12113 inst = local;
12114 /* else inst is already a full instantiation of the pack. */
12115 register_local_specialization (inst, gen);
12116 }
12117 gcc_assert (!TREE_PURPOSE (extra));
12118 extra = TREE_VALUE (extra);
12119 }
12120 return add_to_template_args (extra, args);
12121 }
12122
12123 /* Substitute ARGS into T, which is an pack expansion
12124 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12125 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12126 (if only a partial substitution could be performed) or
12127 ERROR_MARK_NODE if there was an error. */
12128 tree
12129 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12130 tree in_decl)
12131 {
12132 tree pattern;
12133 tree pack, packs = NULL_TREE;
12134 bool unsubstituted_packs = false;
12135 bool unsubstituted_fn_pack = false;
12136 int i, len = -1;
12137 tree result;
12138 hash_map<tree, tree> *saved_local_specializations = NULL;
12139 bool need_local_specializations = false;
12140 int levels;
12141
12142 gcc_assert (PACK_EXPANSION_P (t));
12143 pattern = PACK_EXPANSION_PATTERN (t);
12144
12145 /* Add in any args remembered from an earlier partial instantiation. */
12146 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12147
12148 levels = TMPL_ARGS_DEPTH (args);
12149
12150 /* Determine the argument packs that will instantiate the parameter
12151 packs used in the expansion expression. While we're at it,
12152 compute the number of arguments to be expanded and make sure it
12153 is consistent. */
12154 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12155 pack = TREE_CHAIN (pack))
12156 {
12157 tree parm_pack = TREE_VALUE (pack);
12158 tree arg_pack = NULL_TREE;
12159 tree orig_arg = NULL_TREE;
12160 int level = 0;
12161
12162 if (TREE_CODE (parm_pack) == BASES)
12163 {
12164 gcc_assert (parm_pack == pattern);
12165 if (BASES_DIRECT (parm_pack))
12166 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12167 args, complain,
12168 in_decl, false),
12169 complain);
12170 else
12171 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12172 args, complain, in_decl,
12173 false), complain);
12174 }
12175 else if (builtin_pack_call_p (parm_pack))
12176 {
12177 if (parm_pack != pattern)
12178 {
12179 if (complain & tf_error)
12180 sorry ("%qE is not the entire pattern of the pack expansion",
12181 parm_pack);
12182 return error_mark_node;
12183 }
12184 return expand_builtin_pack_call (parm_pack, args,
12185 complain, in_decl);
12186 }
12187 else if (TREE_CODE (parm_pack) == PARM_DECL)
12188 {
12189 /* We know we have correct local_specializations if this
12190 expansion is at function scope, or if we're dealing with a
12191 local parameter in a requires expression; for the latter,
12192 tsubst_requires_expr set it up appropriately. */
12193 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12194 arg_pack = retrieve_local_specialization (parm_pack);
12195 else
12196 /* We can't rely on local_specializations for a parameter
12197 name used later in a function declaration (such as in a
12198 late-specified return type). Even if it exists, it might
12199 have the wrong value for a recursive call. */
12200 need_local_specializations = true;
12201
12202 if (!arg_pack)
12203 {
12204 /* This parameter pack was used in an unevaluated context. Just
12205 make a dummy decl, since it's only used for its type. */
12206 ++cp_unevaluated_operand;
12207 arg_pack = tsubst_decl (parm_pack, args, complain);
12208 --cp_unevaluated_operand;
12209 if (arg_pack && DECL_PACK_P (arg_pack))
12210 /* Partial instantiation of the parm_pack, we can't build
12211 up an argument pack yet. */
12212 arg_pack = NULL_TREE;
12213 else
12214 arg_pack = make_fnparm_pack (arg_pack);
12215 }
12216 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12217 /* This argument pack isn't fully instantiated yet. We set this
12218 flag rather than clear arg_pack because we do want to do the
12219 optimization below, and we don't want to substitute directly
12220 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12221 where it isn't expected). */
12222 unsubstituted_fn_pack = true;
12223 }
12224 else if (is_capture_proxy (parm_pack))
12225 {
12226 arg_pack = retrieve_local_specialization (parm_pack);
12227 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12228 unsubstituted_fn_pack = true;
12229 }
12230 else
12231 {
12232 int idx;
12233 template_parm_level_and_index (parm_pack, &level, &idx);
12234
12235 if (level <= levels)
12236 arg_pack = TMPL_ARG (args, level, idx);
12237 }
12238
12239 orig_arg = arg_pack;
12240 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12241 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12242
12243 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12244 /* This can only happen if we forget to expand an argument
12245 pack somewhere else. Just return an error, silently. */
12246 {
12247 result = make_tree_vec (1);
12248 TREE_VEC_ELT (result, 0) = error_mark_node;
12249 return result;
12250 }
12251
12252 if (arg_pack)
12253 {
12254 int my_len =
12255 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12256
12257 /* Don't bother trying to do a partial substitution with
12258 incomplete packs; we'll try again after deduction. */
12259 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12260 return t;
12261
12262 if (len < 0)
12263 len = my_len;
12264 else if (len != my_len
12265 && !unsubstituted_fn_pack)
12266 {
12267 if (!(complain & tf_error))
12268 /* Fail quietly. */;
12269 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12270 error ("mismatched argument pack lengths while expanding %qT",
12271 pattern);
12272 else
12273 error ("mismatched argument pack lengths while expanding %qE",
12274 pattern);
12275 return error_mark_node;
12276 }
12277
12278 /* Keep track of the parameter packs and their corresponding
12279 argument packs. */
12280 packs = tree_cons (parm_pack, arg_pack, packs);
12281 TREE_TYPE (packs) = orig_arg;
12282 }
12283 else
12284 {
12285 /* We can't substitute for this parameter pack. We use a flag as
12286 well as the missing_level counter because function parameter
12287 packs don't have a level. */
12288 gcc_assert (processing_template_decl || is_auto (parm_pack));
12289 unsubstituted_packs = true;
12290 }
12291 }
12292
12293 /* If the expansion is just T..., return the matching argument pack, unless
12294 we need to call convert_from_reference on all the elements. This is an
12295 important optimization; see c++/68422. */
12296 if (!unsubstituted_packs
12297 && TREE_PURPOSE (packs) == pattern)
12298 {
12299 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12300
12301 /* If the argument pack is a single pack expansion, pull it out. */
12302 if (TREE_VEC_LENGTH (args) == 1
12303 && pack_expansion_args_count (args))
12304 return TREE_VEC_ELT (args, 0);
12305
12306 /* Types need no adjustment, nor does sizeof..., and if we still have
12307 some pack expansion args we won't do anything yet. */
12308 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12309 || PACK_EXPANSION_SIZEOF_P (t)
12310 || pack_expansion_args_count (args))
12311 return args;
12312 /* Also optimize expression pack expansions if we can tell that the
12313 elements won't have reference type. */
12314 tree type = TREE_TYPE (pattern);
12315 if (type && !TYPE_REF_P (type)
12316 && !PACK_EXPANSION_P (type)
12317 && !WILDCARD_TYPE_P (type))
12318 return args;
12319 /* Otherwise use the normal path so we get convert_from_reference. */
12320 }
12321
12322 /* We cannot expand this expansion expression, because we don't have
12323 all of the argument packs we need. */
12324 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12325 {
12326 /* We got some full packs, but we can't substitute them in until we
12327 have values for all the packs. So remember these until then. */
12328
12329 t = make_pack_expansion (pattern, complain);
12330 PACK_EXPANSION_EXTRA_ARGS (t)
12331 = build_extra_args (pattern, args, complain);
12332 return t;
12333 }
12334 else if (unsubstituted_packs)
12335 {
12336 /* There were no real arguments, we're just replacing a parameter
12337 pack with another version of itself. Substitute into the
12338 pattern and return a PACK_EXPANSION_*. The caller will need to
12339 deal with that. */
12340 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12341 t = tsubst_expr (pattern, args, complain, in_decl,
12342 /*integral_constant_expression_p=*/false);
12343 else
12344 t = tsubst (pattern, args, complain, in_decl);
12345 t = make_pack_expansion (t, complain);
12346 return t;
12347 }
12348
12349 gcc_assert (len >= 0);
12350
12351 if (need_local_specializations)
12352 {
12353 /* We're in a late-specified return type, so create our own local
12354 specializations map; the current map is either NULL or (in the
12355 case of recursive unification) might have bindings that we don't
12356 want to use or alter. */
12357 saved_local_specializations = local_specializations;
12358 local_specializations = new hash_map<tree, tree>;
12359 }
12360
12361 /* For each argument in each argument pack, substitute into the
12362 pattern. */
12363 result = make_tree_vec (len);
12364 tree elem_args = copy_template_args (args);
12365 for (i = 0; i < len; ++i)
12366 {
12367 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12368 i,
12369 elem_args, complain,
12370 in_decl);
12371 TREE_VEC_ELT (result, i) = t;
12372 if (t == error_mark_node)
12373 {
12374 result = error_mark_node;
12375 break;
12376 }
12377 }
12378
12379 /* Update ARGS to restore the substitution from parameter packs to
12380 their argument packs. */
12381 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12382 {
12383 tree parm = TREE_PURPOSE (pack);
12384
12385 if (TREE_CODE (parm) == PARM_DECL
12386 || VAR_P (parm)
12387 || TREE_CODE (parm) == FIELD_DECL)
12388 register_local_specialization (TREE_TYPE (pack), parm);
12389 else
12390 {
12391 int idx, level;
12392
12393 if (TREE_VALUE (pack) == NULL_TREE)
12394 continue;
12395
12396 template_parm_level_and_index (parm, &level, &idx);
12397
12398 /* Update the corresponding argument. */
12399 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12400 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12401 TREE_TYPE (pack);
12402 else
12403 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12404 }
12405 }
12406
12407 if (need_local_specializations)
12408 {
12409 delete local_specializations;
12410 local_specializations = saved_local_specializations;
12411 }
12412
12413 /* If the dependent pack arguments were such that we end up with only a
12414 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12415 if (len == 1 && TREE_CODE (result) == TREE_VEC
12416 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12417 return TREE_VEC_ELT (result, 0);
12418
12419 return result;
12420 }
12421
12422 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12423 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12424 parameter packs; all parms generated from a function parameter pack will
12425 have the same DECL_PARM_INDEX. */
12426
12427 tree
12428 get_pattern_parm (tree parm, tree tmpl)
12429 {
12430 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12431 tree patparm;
12432
12433 if (DECL_ARTIFICIAL (parm))
12434 {
12435 for (patparm = DECL_ARGUMENTS (pattern);
12436 patparm; patparm = DECL_CHAIN (patparm))
12437 if (DECL_ARTIFICIAL (patparm)
12438 && DECL_NAME (parm) == DECL_NAME (patparm))
12439 break;
12440 }
12441 else
12442 {
12443 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12444 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12445 gcc_assert (DECL_PARM_INDEX (patparm)
12446 == DECL_PARM_INDEX (parm));
12447 }
12448
12449 return patparm;
12450 }
12451
12452 /* Make an argument pack out of the TREE_VEC VEC. */
12453
12454 static tree
12455 make_argument_pack (tree vec)
12456 {
12457 tree pack;
12458 tree elt = TREE_VEC_ELT (vec, 0);
12459 if (TYPE_P (elt))
12460 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12461 else
12462 {
12463 pack = make_node (NONTYPE_ARGUMENT_PACK);
12464 TREE_CONSTANT (pack) = 1;
12465 }
12466 SET_ARGUMENT_PACK_ARGS (pack, vec);
12467 return pack;
12468 }
12469
12470 /* Return an exact copy of template args T that can be modified
12471 independently. */
12472
12473 static tree
12474 copy_template_args (tree t)
12475 {
12476 if (t == error_mark_node)
12477 return t;
12478
12479 int len = TREE_VEC_LENGTH (t);
12480 tree new_vec = make_tree_vec (len);
12481
12482 for (int i = 0; i < len; ++i)
12483 {
12484 tree elt = TREE_VEC_ELT (t, i);
12485 if (elt && TREE_CODE (elt) == TREE_VEC)
12486 elt = copy_template_args (elt);
12487 TREE_VEC_ELT (new_vec, i) = elt;
12488 }
12489
12490 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12491 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12492
12493 return new_vec;
12494 }
12495
12496 /* Substitute ARGS into the vector or list of template arguments T. */
12497
12498 static tree
12499 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12500 {
12501 tree orig_t = t;
12502 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12503 tree *elts;
12504
12505 if (t == error_mark_node)
12506 return error_mark_node;
12507
12508 len = TREE_VEC_LENGTH (t);
12509 elts = XALLOCAVEC (tree, len);
12510
12511 for (i = 0; i < len; i++)
12512 {
12513 tree orig_arg = TREE_VEC_ELT (t, i);
12514 tree new_arg;
12515
12516 if (TREE_CODE (orig_arg) == TREE_VEC)
12517 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12518 else if (PACK_EXPANSION_P (orig_arg))
12519 {
12520 /* Substitute into an expansion expression. */
12521 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12522
12523 if (TREE_CODE (new_arg) == TREE_VEC)
12524 /* Add to the expanded length adjustment the number of
12525 expanded arguments. We subtract one from this
12526 measurement, because the argument pack expression
12527 itself is already counted as 1 in
12528 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12529 the argument pack is empty. */
12530 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12531 }
12532 else if (ARGUMENT_PACK_P (orig_arg))
12533 {
12534 /* Substitute into each of the arguments. */
12535 new_arg = TYPE_P (orig_arg)
12536 ? cxx_make_type (TREE_CODE (orig_arg))
12537 : make_node (TREE_CODE (orig_arg));
12538
12539 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12540 args, complain, in_decl);
12541 if (pack_args == error_mark_node)
12542 new_arg = error_mark_node;
12543 else
12544 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12545
12546 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12547 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12548 }
12549 else
12550 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12551
12552 if (new_arg == error_mark_node)
12553 return error_mark_node;
12554
12555 elts[i] = new_arg;
12556 if (new_arg != orig_arg)
12557 need_new = 1;
12558 }
12559
12560 if (!need_new)
12561 return t;
12562
12563 /* Make space for the expanded arguments coming from template
12564 argument packs. */
12565 t = make_tree_vec (len + expanded_len_adjust);
12566 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12567 arguments for a member template.
12568 In that case each TREE_VEC in ORIG_T represents a level of template
12569 arguments, and ORIG_T won't carry any non defaulted argument count.
12570 It will rather be the nested TREE_VECs that will carry one.
12571 In other words, ORIG_T carries a non defaulted argument count only
12572 if it doesn't contain any nested TREE_VEC. */
12573 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12574 {
12575 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12576 count += expanded_len_adjust;
12577 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12578 }
12579 for (i = 0, out = 0; i < len; i++)
12580 {
12581 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12582 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12583 && TREE_CODE (elts[i]) == TREE_VEC)
12584 {
12585 int idx;
12586
12587 /* Now expand the template argument pack "in place". */
12588 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12589 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12590 }
12591 else
12592 {
12593 TREE_VEC_ELT (t, out) = elts[i];
12594 out++;
12595 }
12596 }
12597
12598 return t;
12599 }
12600
12601 /* Substitute ARGS into one level PARMS of template parameters. */
12602
12603 static tree
12604 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12605 {
12606 if (parms == error_mark_node)
12607 return error_mark_node;
12608
12609 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12610
12611 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12612 {
12613 tree tuple = TREE_VEC_ELT (parms, i);
12614
12615 if (tuple == error_mark_node)
12616 continue;
12617
12618 TREE_VEC_ELT (new_vec, i) =
12619 tsubst_template_parm (tuple, args, complain);
12620 }
12621
12622 return new_vec;
12623 }
12624
12625 /* Return the result of substituting ARGS into the template parameters
12626 given by PARMS. If there are m levels of ARGS and m + n levels of
12627 PARMS, then the result will contain n levels of PARMS. For
12628 example, if PARMS is `template <class T> template <class U>
12629 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12630 result will be `template <int*, double, class V>'. */
12631
12632 static tree
12633 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12634 {
12635 tree r = NULL_TREE;
12636 tree* new_parms;
12637
12638 /* When substituting into a template, we must set
12639 PROCESSING_TEMPLATE_DECL as the template parameters may be
12640 dependent if they are based on one-another, and the dependency
12641 predicates are short-circuit outside of templates. */
12642 ++processing_template_decl;
12643
12644 for (new_parms = &r;
12645 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12646 new_parms = &(TREE_CHAIN (*new_parms)),
12647 parms = TREE_CHAIN (parms))
12648 {
12649 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12650 args, complain);
12651 *new_parms =
12652 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12653 - TMPL_ARGS_DEPTH (args)),
12654 new_vec, NULL_TREE);
12655 }
12656
12657 --processing_template_decl;
12658
12659 return r;
12660 }
12661
12662 /* Return the result of substituting ARGS into one template parameter
12663 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12664 parameter and which TREE_PURPOSE is the default argument of the
12665 template parameter. */
12666
12667 static tree
12668 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12669 {
12670 tree default_value, parm_decl;
12671
12672 if (args == NULL_TREE
12673 || t == NULL_TREE
12674 || t == error_mark_node)
12675 return t;
12676
12677 gcc_assert (TREE_CODE (t) == TREE_LIST);
12678
12679 default_value = TREE_PURPOSE (t);
12680 parm_decl = TREE_VALUE (t);
12681
12682 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12683 if (TREE_CODE (parm_decl) == PARM_DECL
12684 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12685 parm_decl = error_mark_node;
12686 default_value = tsubst_template_arg (default_value, args,
12687 complain, NULL_TREE);
12688
12689 return build_tree_list (default_value, parm_decl);
12690 }
12691
12692 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12693 type T. If T is not an aggregate or enumeration type, it is
12694 handled as if by tsubst. IN_DECL is as for tsubst. If
12695 ENTERING_SCOPE is nonzero, T is the context for a template which
12696 we are presently tsubst'ing. Return the substituted value. */
12697
12698 static tree
12699 tsubst_aggr_type (tree t,
12700 tree args,
12701 tsubst_flags_t complain,
12702 tree in_decl,
12703 int entering_scope)
12704 {
12705 if (t == NULL_TREE)
12706 return NULL_TREE;
12707
12708 switch (TREE_CODE (t))
12709 {
12710 case RECORD_TYPE:
12711 if (TYPE_PTRMEMFUNC_P (t))
12712 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12713
12714 /* Fall through. */
12715 case ENUMERAL_TYPE:
12716 case UNION_TYPE:
12717 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12718 {
12719 tree argvec;
12720 tree context;
12721 tree r;
12722
12723 /* In "sizeof(X<I>)" we need to evaluate "I". */
12724 cp_evaluated ev;
12725
12726 /* First, determine the context for the type we are looking
12727 up. */
12728 context = TYPE_CONTEXT (t);
12729 if (context && TYPE_P (context))
12730 {
12731 context = tsubst_aggr_type (context, args, complain,
12732 in_decl, /*entering_scope=*/1);
12733 /* If context is a nested class inside a class template,
12734 it may still need to be instantiated (c++/33959). */
12735 context = complete_type (context);
12736 }
12737
12738 /* Then, figure out what arguments are appropriate for the
12739 type we are trying to find. For example, given:
12740
12741 template <class T> struct S;
12742 template <class T, class U> void f(T, U) { S<U> su; }
12743
12744 and supposing that we are instantiating f<int, double>,
12745 then our ARGS will be {int, double}, but, when looking up
12746 S we only want {double}. */
12747 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12748 complain, in_decl);
12749 if (argvec == error_mark_node)
12750 r = error_mark_node;
12751 else
12752 {
12753 r = lookup_template_class (t, argvec, in_decl, context,
12754 entering_scope, complain);
12755 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12756 }
12757
12758 return r;
12759 }
12760 else
12761 /* This is not a template type, so there's nothing to do. */
12762 return t;
12763
12764 default:
12765 return tsubst (t, args, complain, in_decl);
12766 }
12767 }
12768
12769 static GTY((cache)) tree_cache_map *defarg_inst;
12770
12771 /* Substitute into the default argument ARG (a default argument for
12772 FN), which has the indicated TYPE. */
12773
12774 tree
12775 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12776 tsubst_flags_t complain)
12777 {
12778 int errs = errorcount + sorrycount;
12779
12780 /* This can happen in invalid code. */
12781 if (TREE_CODE (arg) == DEFAULT_ARG)
12782 return arg;
12783
12784 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12785 parm = chain_index (parmnum, parm);
12786 tree parmtype = TREE_TYPE (parm);
12787 if (DECL_BY_REFERENCE (parm))
12788 parmtype = TREE_TYPE (parmtype);
12789 if (parmtype == error_mark_node)
12790 return error_mark_node;
12791
12792 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12793
12794 tree *slot;
12795 if (defarg_inst && (slot = defarg_inst->get (parm)))
12796 return *slot;
12797
12798 /* This default argument came from a template. Instantiate the
12799 default argument here, not in tsubst. In the case of
12800 something like:
12801
12802 template <class T>
12803 struct S {
12804 static T t();
12805 void f(T = t());
12806 };
12807
12808 we must be careful to do name lookup in the scope of S<T>,
12809 rather than in the current class. */
12810 push_to_top_level ();
12811 push_access_scope (fn);
12812 push_deferring_access_checks (dk_no_deferred);
12813 start_lambda_scope (parm);
12814
12815 /* The default argument expression may cause implicitly defined
12816 member functions to be synthesized, which will result in garbage
12817 collection. We must treat this situation as if we were within
12818 the body of function so as to avoid collecting live data on the
12819 stack. */
12820 ++function_depth;
12821 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12822 complain, NULL_TREE,
12823 /*integral_constant_expression_p=*/false);
12824 --function_depth;
12825
12826 finish_lambda_scope ();
12827
12828 /* Make sure the default argument is reasonable. */
12829 arg = check_default_argument (type, arg, complain);
12830
12831 if (errorcount+sorrycount > errs
12832 && (complain & tf_warning_or_error))
12833 inform (input_location,
12834 " when instantiating default argument for call to %qD", fn);
12835
12836 pop_deferring_access_checks ();
12837 pop_access_scope (fn);
12838 pop_from_top_level ();
12839
12840 if (arg != error_mark_node && !cp_unevaluated_operand)
12841 {
12842 if (!defarg_inst)
12843 defarg_inst = tree_cache_map::create_ggc (37);
12844 defarg_inst->put (parm, arg);
12845 }
12846
12847 return arg;
12848 }
12849
12850 /* Substitute into all the default arguments for FN. */
12851
12852 static void
12853 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12854 {
12855 tree arg;
12856 tree tmpl_args;
12857
12858 tmpl_args = DECL_TI_ARGS (fn);
12859
12860 /* If this function is not yet instantiated, we certainly don't need
12861 its default arguments. */
12862 if (uses_template_parms (tmpl_args))
12863 return;
12864 /* Don't do this again for clones. */
12865 if (DECL_CLONED_FUNCTION_P (fn))
12866 return;
12867
12868 int i = 0;
12869 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12870 arg;
12871 arg = TREE_CHAIN (arg), ++i)
12872 if (TREE_PURPOSE (arg))
12873 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12874 TREE_VALUE (arg),
12875 TREE_PURPOSE (arg),
12876 complain);
12877 }
12878
12879 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12880 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12881
12882 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12883
12884 void
12885 store_explicit_specifier (tree v, tree t)
12886 {
12887 if (!explicit_specifier_map)
12888 explicit_specifier_map = tree_cache_map::create_ggc (37);
12889 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12890 explicit_specifier_map->put (v, t);
12891 }
12892
12893 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12894
12895 static tree
12896 lookup_explicit_specifier (tree v)
12897 {
12898 return *explicit_specifier_map->get (v);
12899 }
12900
12901 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12902
12903 static tree
12904 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12905 tree lambda_fntype)
12906 {
12907 tree gen_tmpl, argvec;
12908 hashval_t hash = 0;
12909 tree in_decl = t;
12910
12911 /* Nobody should be tsubst'ing into non-template functions. */
12912 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12913
12914 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12915 {
12916 /* If T is not dependent, just return it. */
12917 if (!uses_template_parms (DECL_TI_ARGS (t))
12918 && !LAMBDA_FUNCTION_P (t))
12919 return t;
12920
12921 /* Calculate the most general template of which R is a
12922 specialization. */
12923 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12924
12925 /* We're substituting a lambda function under tsubst_lambda_expr but not
12926 directly from it; find the matching function we're already inside.
12927 But don't do this if T is a generic lambda with a single level of
12928 template parms, as in that case we're doing a normal instantiation. */
12929 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12930 && (!generic_lambda_fn_p (t)
12931 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12932 return enclosing_instantiation_of (t);
12933
12934 /* Calculate the complete set of arguments used to
12935 specialize R. */
12936 argvec = tsubst_template_args (DECL_TI_ARGS
12937 (DECL_TEMPLATE_RESULT
12938 (DECL_TI_TEMPLATE (t))),
12939 args, complain, in_decl);
12940 if (argvec == error_mark_node)
12941 return error_mark_node;
12942
12943 /* Check to see if we already have this specialization. */
12944 if (!lambda_fntype)
12945 {
12946 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12947 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12948 return spec;
12949 }
12950
12951 /* We can see more levels of arguments than parameters if
12952 there was a specialization of a member template, like
12953 this:
12954
12955 template <class T> struct S { template <class U> void f(); }
12956 template <> template <class U> void S<int>::f(U);
12957
12958 Here, we'll be substituting into the specialization,
12959 because that's where we can find the code we actually
12960 want to generate, but we'll have enough arguments for
12961 the most general template.
12962
12963 We also deal with the peculiar case:
12964
12965 template <class T> struct S {
12966 template <class U> friend void f();
12967 };
12968 template <class U> void f() {}
12969 template S<int>;
12970 template void f<double>();
12971
12972 Here, the ARGS for the instantiation of will be {int,
12973 double}. But, we only need as many ARGS as there are
12974 levels of template parameters in CODE_PATTERN. We are
12975 careful not to get fooled into reducing the ARGS in
12976 situations like:
12977
12978 template <class T> struct S { template <class U> void f(U); }
12979 template <class T> template <> void S<T>::f(int) {}
12980
12981 which we can spot because the pattern will be a
12982 specialization in this case. */
12983 int args_depth = TMPL_ARGS_DEPTH (args);
12984 int parms_depth =
12985 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12986
12987 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12988 args = get_innermost_template_args (args, parms_depth);
12989 }
12990 else
12991 {
12992 /* This special case arises when we have something like this:
12993
12994 template <class T> struct S {
12995 friend void f<int>(int, double);
12996 };
12997
12998 Here, the DECL_TI_TEMPLATE for the friend declaration
12999 will be an IDENTIFIER_NODE. We are being called from
13000 tsubst_friend_function, and we want only to create a
13001 new decl (R) with appropriate types so that we can call
13002 determine_specialization. */
13003 gen_tmpl = NULL_TREE;
13004 argvec = NULL_TREE;
13005 }
13006
13007 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13008 : NULL_TREE);
13009 tree ctx = closure ? closure : DECL_CONTEXT (t);
13010 bool member = ctx && TYPE_P (ctx);
13011
13012 if (member && !closure)
13013 ctx = tsubst_aggr_type (ctx, args,
13014 complain, t, /*entering_scope=*/1);
13015
13016 tree type = (lambda_fntype ? lambda_fntype
13017 : tsubst (TREE_TYPE (t), args,
13018 complain | tf_fndecl_type, in_decl));
13019 if (type == error_mark_node)
13020 return error_mark_node;
13021
13022 /* If we hit excessive deduction depth, the type is bogus even if
13023 it isn't error_mark_node, so don't build a decl. */
13024 if (excessive_deduction_depth)
13025 return error_mark_node;
13026
13027 /* We do NOT check for matching decls pushed separately at this
13028 point, as they may not represent instantiations of this
13029 template, and in any case are considered separate under the
13030 discrete model. */
13031 tree r = copy_decl (t);
13032 DECL_USE_TEMPLATE (r) = 0;
13033 TREE_TYPE (r) = type;
13034 /* Clear out the mangled name and RTL for the instantiation. */
13035 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13036 SET_DECL_RTL (r, NULL);
13037 /* Leave DECL_INITIAL set on deleted instantiations. */
13038 if (!DECL_DELETED_FN (r))
13039 DECL_INITIAL (r) = NULL_TREE;
13040 DECL_CONTEXT (r) = ctx;
13041
13042 /* Handle explicit(dependent-expr). */
13043 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13044 {
13045 tree spec = lookup_explicit_specifier (t);
13046 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13047 /*function_p=*/false,
13048 /*i_c_e_p=*/true);
13049 spec = build_explicit_specifier (spec, complain);
13050 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13051 }
13052
13053 /* OpenMP UDRs have the only argument a reference to the declared
13054 type. We want to diagnose if the declared type is a reference,
13055 which is invalid, but as references to references are usually
13056 quietly merged, diagnose it here. */
13057 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13058 {
13059 tree argtype
13060 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13061 argtype = tsubst (argtype, args, complain, in_decl);
13062 if (TYPE_REF_P (argtype))
13063 error_at (DECL_SOURCE_LOCATION (t),
13064 "reference type %qT in "
13065 "%<#pragma omp declare reduction%>", argtype);
13066 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13067 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13068 argtype);
13069 }
13070
13071 if (member && DECL_CONV_FN_P (r))
13072 /* Type-conversion operator. Reconstruct the name, in
13073 case it's the name of one of the template's parameters. */
13074 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13075
13076 tree parms = DECL_ARGUMENTS (t);
13077 if (closure)
13078 parms = DECL_CHAIN (parms);
13079 parms = tsubst (parms, args, complain, t);
13080 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13081 DECL_CONTEXT (parm) = r;
13082 if (closure)
13083 {
13084 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13085 DECL_CHAIN (tparm) = parms;
13086 parms = tparm;
13087 }
13088 DECL_ARGUMENTS (r) = parms;
13089 DECL_RESULT (r) = NULL_TREE;
13090
13091 TREE_STATIC (r) = 0;
13092 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13093 DECL_EXTERNAL (r) = 1;
13094 /* If this is an instantiation of a function with internal
13095 linkage, we already know what object file linkage will be
13096 assigned to the instantiation. */
13097 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13098 DECL_DEFER_OUTPUT (r) = 0;
13099 DECL_CHAIN (r) = NULL_TREE;
13100 DECL_PENDING_INLINE_INFO (r) = 0;
13101 DECL_PENDING_INLINE_P (r) = 0;
13102 DECL_SAVED_TREE (r) = NULL_TREE;
13103 DECL_STRUCT_FUNCTION (r) = NULL;
13104 TREE_USED (r) = 0;
13105 /* We'll re-clone as appropriate in instantiate_template. */
13106 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13107
13108 /* If we aren't complaining now, return on error before we register
13109 the specialization so that we'll complain eventually. */
13110 if ((complain & tf_error) == 0
13111 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13112 && !grok_op_properties (r, /*complain=*/false))
13113 return error_mark_node;
13114
13115 /* When instantiating a constrained member, substitute
13116 into the constraints to create a new constraint. */
13117 if (tree ci = get_constraints (t))
13118 if (member)
13119 {
13120 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13121 set_constraints (r, ci);
13122 }
13123
13124 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13125 SET_DECL_FRIEND_CONTEXT (r,
13126 tsubst (DECL_FRIEND_CONTEXT (t),
13127 args, complain, in_decl));
13128
13129 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13130 this in the special friend case mentioned above where
13131 GEN_TMPL is NULL. */
13132 if (gen_tmpl && !closure)
13133 {
13134 DECL_TEMPLATE_INFO (r)
13135 = build_template_info (gen_tmpl, argvec);
13136 SET_DECL_IMPLICIT_INSTANTIATION (r);
13137
13138 tree new_r
13139 = register_specialization (r, gen_tmpl, argvec, false, hash);
13140 if (new_r != r)
13141 /* We instantiated this while substituting into
13142 the type earlier (template/friend54.C). */
13143 return new_r;
13144
13145 /* We're not supposed to instantiate default arguments
13146 until they are called, for a template. But, for a
13147 declaration like:
13148
13149 template <class T> void f ()
13150 { extern void g(int i = T()); }
13151
13152 we should do the substitution when the template is
13153 instantiated. We handle the member function case in
13154 instantiate_class_template since the default arguments
13155 might refer to other members of the class. */
13156 if (!member
13157 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13158 && !uses_template_parms (argvec))
13159 tsubst_default_arguments (r, complain);
13160 }
13161 else
13162 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13163
13164 /* Copy the list of befriending classes. */
13165 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13166 *friends;
13167 friends = &TREE_CHAIN (*friends))
13168 {
13169 *friends = copy_node (*friends);
13170 TREE_VALUE (*friends)
13171 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13172 }
13173
13174 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13175 {
13176 maybe_retrofit_in_chrg (r);
13177 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13178 return error_mark_node;
13179 /* If this is an instantiation of a member template, clone it.
13180 If it isn't, that'll be handled by
13181 clone_constructors_and_destructors. */
13182 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13183 clone_function_decl (r, /*update_methods=*/false);
13184 }
13185 else if ((complain & tf_error) != 0
13186 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13187 && !grok_op_properties (r, /*complain=*/true))
13188 return error_mark_node;
13189
13190 /* Possibly limit visibility based on template args. */
13191 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13192 if (DECL_VISIBILITY_SPECIFIED (t))
13193 {
13194 DECL_VISIBILITY_SPECIFIED (r) = 0;
13195 DECL_ATTRIBUTES (r)
13196 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13197 }
13198 determine_visibility (r);
13199 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13200 && !processing_template_decl)
13201 defaulted_late_check (r);
13202
13203 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13204 args, complain, in_decl);
13205 return r;
13206 }
13207
13208 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13209
13210 static tree
13211 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13212 tree lambda_fntype)
13213 {
13214 /* We can get here when processing a member function template,
13215 member class template, or template template parameter. */
13216 tree decl = DECL_TEMPLATE_RESULT (t);
13217 tree in_decl = t;
13218 tree spec;
13219 tree tmpl_args;
13220 tree full_args;
13221 tree r;
13222 hashval_t hash = 0;
13223
13224 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13225 {
13226 /* Template template parameter is treated here. */
13227 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13228 if (new_type == error_mark_node)
13229 r = error_mark_node;
13230 /* If we get a real template back, return it. This can happen in
13231 the context of most_specialized_partial_spec. */
13232 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13233 r = new_type;
13234 else
13235 /* The new TEMPLATE_DECL was built in
13236 reduce_template_parm_level. */
13237 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13238 return r;
13239 }
13240
13241 if (!lambda_fntype)
13242 {
13243 /* We might already have an instance of this template.
13244 The ARGS are for the surrounding class type, so the
13245 full args contain the tsubst'd args for the context,
13246 plus the innermost args from the template decl. */
13247 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13248 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13249 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13250 /* Because this is a template, the arguments will still be
13251 dependent, even after substitution. If
13252 PROCESSING_TEMPLATE_DECL is not set, the dependency
13253 predicates will short-circuit. */
13254 ++processing_template_decl;
13255 full_args = tsubst_template_args (tmpl_args, args,
13256 complain, in_decl);
13257 --processing_template_decl;
13258 if (full_args == error_mark_node)
13259 return error_mark_node;
13260
13261 /* If this is a default template template argument,
13262 tsubst might not have changed anything. */
13263 if (full_args == tmpl_args)
13264 return t;
13265
13266 hash = hash_tmpl_and_args (t, full_args);
13267 spec = retrieve_specialization (t, full_args, hash);
13268 if (spec != NULL_TREE)
13269 {
13270 if (TYPE_P (spec))
13271 /* Type partial instantiations are stored as the type by
13272 lookup_template_class_1, not here as the template. */
13273 spec = CLASSTYPE_TI_TEMPLATE (spec);
13274 return spec;
13275 }
13276 }
13277
13278 /* Make a new template decl. It will be similar to the
13279 original, but will record the current template arguments.
13280 We also create a new function declaration, which is just
13281 like the old one, but points to this new template, rather
13282 than the old one. */
13283 r = copy_decl (t);
13284 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13285 DECL_CHAIN (r) = NULL_TREE;
13286
13287 // Build new template info linking to the original template decl.
13288 if (!lambda_fntype)
13289 {
13290 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13291 SET_DECL_IMPLICIT_INSTANTIATION (r);
13292 }
13293 else
13294 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13295
13296 /* The template parameters for this new template are all the
13297 template parameters for the old template, except the
13298 outermost level of parameters. */
13299 DECL_TEMPLATE_PARMS (r)
13300 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13301 complain);
13302
13303 if (TREE_CODE (decl) == TYPE_DECL
13304 && !TYPE_DECL_ALIAS_P (decl))
13305 {
13306 tree new_type;
13307 ++processing_template_decl;
13308 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13309 --processing_template_decl;
13310 if (new_type == error_mark_node)
13311 return error_mark_node;
13312
13313 TREE_TYPE (r) = new_type;
13314 /* For a partial specialization, we need to keep pointing to
13315 the primary template. */
13316 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13317 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13318 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13319 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13320 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13321 }
13322 else
13323 {
13324 tree new_decl;
13325 ++processing_template_decl;
13326 if (TREE_CODE (decl) == FUNCTION_DECL)
13327 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13328 else
13329 new_decl = tsubst (decl, args, complain, in_decl);
13330 --processing_template_decl;
13331 if (new_decl == error_mark_node)
13332 return error_mark_node;
13333
13334 DECL_TEMPLATE_RESULT (r) = new_decl;
13335 TREE_TYPE (r) = TREE_TYPE (new_decl);
13336 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13337 if (lambda_fntype)
13338 {
13339 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13340 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13341 }
13342 else
13343 {
13344 DECL_TI_TEMPLATE (new_decl) = r;
13345 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13346 }
13347 }
13348
13349 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13350 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13351
13352 if (PRIMARY_TEMPLATE_P (t))
13353 DECL_PRIMARY_TEMPLATE (r) = r;
13354
13355 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13356 && !lambda_fntype)
13357 /* Record this non-type partial instantiation. */
13358 register_specialization (r, t,
13359 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13360 false, hash);
13361
13362 return r;
13363 }
13364
13365 /* True if FN is the op() for a lambda in an uninstantiated template. */
13366
13367 bool
13368 lambda_fn_in_template_p (tree fn)
13369 {
13370 if (!fn || !LAMBDA_FUNCTION_P (fn))
13371 return false;
13372 tree closure = DECL_CONTEXT (fn);
13373 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13374 }
13375
13376 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13377 which the above is true. */
13378
13379 bool
13380 instantiated_lambda_fn_p (tree fn)
13381 {
13382 if (!fn || !LAMBDA_FUNCTION_P (fn))
13383 return false;
13384 tree closure = DECL_CONTEXT (fn);
13385 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13386 return LAMBDA_EXPR_INSTANTIATED (lam);
13387 }
13388
13389 /* We're instantiating a variable from template function TCTX. Return the
13390 corresponding current enclosing scope. This gets complicated because lambda
13391 functions in templates are regenerated rather than instantiated, but generic
13392 lambda functions are subsequently instantiated. */
13393
13394 static tree
13395 enclosing_instantiation_of (tree otctx)
13396 {
13397 tree tctx = otctx;
13398 tree fn = current_function_decl;
13399 int lambda_count = 0;
13400
13401 for (; tctx && (lambda_fn_in_template_p (tctx)
13402 || instantiated_lambda_fn_p (tctx));
13403 tctx = decl_function_context (tctx))
13404 ++lambda_count;
13405 for (; fn; fn = decl_function_context (fn))
13406 {
13407 tree ofn = fn;
13408 int flambda_count = 0;
13409 for (; fn && instantiated_lambda_fn_p (fn);
13410 fn = decl_function_context (fn))
13411 ++flambda_count;
13412 if ((fn && DECL_TEMPLATE_INFO (fn))
13413 ? most_general_template (fn) != most_general_template (tctx)
13414 : fn != tctx)
13415 continue;
13416 if (flambda_count != lambda_count)
13417 {
13418 gcc_assert (flambda_count > lambda_count);
13419 for (; flambda_count > lambda_count; --flambda_count)
13420 ofn = decl_function_context (ofn);
13421 }
13422 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13423 || DECL_CONV_FN_P (ofn));
13424 return ofn;
13425 }
13426 gcc_unreachable ();
13427 }
13428
13429 /* Substitute the ARGS into the T, which is a _DECL. Return the
13430 result of the substitution. Issue error and warning messages under
13431 control of COMPLAIN. */
13432
13433 static tree
13434 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13435 {
13436 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13437 location_t saved_loc;
13438 tree r = NULL_TREE;
13439 tree in_decl = t;
13440 hashval_t hash = 0;
13441
13442 /* Set the filename and linenumber to improve error-reporting. */
13443 saved_loc = input_location;
13444 input_location = DECL_SOURCE_LOCATION (t);
13445
13446 switch (TREE_CODE (t))
13447 {
13448 case TEMPLATE_DECL:
13449 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13450 break;
13451
13452 case FUNCTION_DECL:
13453 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13454 break;
13455
13456 case PARM_DECL:
13457 {
13458 tree type = NULL_TREE;
13459 int i, len = 1;
13460 tree expanded_types = NULL_TREE;
13461 tree prev_r = NULL_TREE;
13462 tree first_r = NULL_TREE;
13463
13464 if (DECL_PACK_P (t))
13465 {
13466 /* If there is a local specialization that isn't a
13467 parameter pack, it means that we're doing a "simple"
13468 substitution from inside tsubst_pack_expansion. Just
13469 return the local specialization (which will be a single
13470 parm). */
13471 tree spec = retrieve_local_specialization (t);
13472 if (spec
13473 && TREE_CODE (spec) == PARM_DECL
13474 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13475 RETURN (spec);
13476
13477 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13478 the parameters in this function parameter pack. */
13479 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13480 complain, in_decl);
13481 if (TREE_CODE (expanded_types) == TREE_VEC)
13482 {
13483 len = TREE_VEC_LENGTH (expanded_types);
13484
13485 /* Zero-length parameter packs are boring. Just substitute
13486 into the chain. */
13487 if (len == 0)
13488 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13489 TREE_CHAIN (t)));
13490 }
13491 else
13492 {
13493 /* All we did was update the type. Make a note of that. */
13494 type = expanded_types;
13495 expanded_types = NULL_TREE;
13496 }
13497 }
13498
13499 /* Loop through all of the parameters we'll build. When T is
13500 a function parameter pack, LEN is the number of expanded
13501 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13502 r = NULL_TREE;
13503 for (i = 0; i < len; ++i)
13504 {
13505 prev_r = r;
13506 r = copy_node (t);
13507 if (DECL_TEMPLATE_PARM_P (t))
13508 SET_DECL_TEMPLATE_PARM_P (r);
13509
13510 if (expanded_types)
13511 /* We're on the Ith parameter of the function parameter
13512 pack. */
13513 {
13514 /* Get the Ith type. */
13515 type = TREE_VEC_ELT (expanded_types, i);
13516
13517 /* Rename the parameter to include the index. */
13518 DECL_NAME (r)
13519 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13520 }
13521 else if (!type)
13522 /* We're dealing with a normal parameter. */
13523 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13524
13525 type = type_decays_to (type);
13526 TREE_TYPE (r) = type;
13527 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13528
13529 if (DECL_INITIAL (r))
13530 {
13531 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13532 DECL_INITIAL (r) = TREE_TYPE (r);
13533 else
13534 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13535 complain, in_decl);
13536 }
13537
13538 DECL_CONTEXT (r) = NULL_TREE;
13539
13540 if (!DECL_TEMPLATE_PARM_P (r))
13541 DECL_ARG_TYPE (r) = type_passed_as (type);
13542
13543 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13544 args, complain, in_decl);
13545
13546 /* Keep track of the first new parameter we
13547 generate. That's what will be returned to the
13548 caller. */
13549 if (!first_r)
13550 first_r = r;
13551
13552 /* Build a proper chain of parameters when substituting
13553 into a function parameter pack. */
13554 if (prev_r)
13555 DECL_CHAIN (prev_r) = r;
13556 }
13557
13558 /* If cp_unevaluated_operand is set, we're just looking for a
13559 single dummy parameter, so don't keep going. */
13560 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13561 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13562 complain, DECL_CHAIN (t));
13563
13564 /* FIRST_R contains the start of the chain we've built. */
13565 r = first_r;
13566 }
13567 break;
13568
13569 case FIELD_DECL:
13570 {
13571 tree type = NULL_TREE;
13572 tree vec = NULL_TREE;
13573 tree expanded_types = NULL_TREE;
13574 int len = 1;
13575
13576 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13577 {
13578 /* This field is a lambda capture pack. Return a TREE_VEC of
13579 the expanded fields to instantiate_class_template_1. */
13580 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13581 complain, in_decl);
13582 if (TREE_CODE (expanded_types) == TREE_VEC)
13583 {
13584 len = TREE_VEC_LENGTH (expanded_types);
13585 vec = make_tree_vec (len);
13586 }
13587 else
13588 {
13589 /* All we did was update the type. Make a note of that. */
13590 type = expanded_types;
13591 expanded_types = NULL_TREE;
13592 }
13593 }
13594
13595 for (int i = 0; i < len; ++i)
13596 {
13597 r = copy_decl (t);
13598 if (expanded_types)
13599 {
13600 type = TREE_VEC_ELT (expanded_types, i);
13601 DECL_NAME (r)
13602 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13603 }
13604 else if (!type)
13605 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13606
13607 if (type == error_mark_node)
13608 RETURN (error_mark_node);
13609 TREE_TYPE (r) = type;
13610 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13611
13612 if (DECL_C_BIT_FIELD (r))
13613 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13614 number of bits. */
13615 DECL_BIT_FIELD_REPRESENTATIVE (r)
13616 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13617 complain, in_decl,
13618 /*integral_constant_expression_p=*/true);
13619 if (DECL_INITIAL (t))
13620 {
13621 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13622 NSDMI in perform_member_init. Still set DECL_INITIAL
13623 so that we know there is one. */
13624 DECL_INITIAL (r) = void_node;
13625 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13626 retrofit_lang_decl (r);
13627 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13628 }
13629 /* We don't have to set DECL_CONTEXT here; it is set by
13630 finish_member_declaration. */
13631 DECL_CHAIN (r) = NULL_TREE;
13632
13633 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13634 args, complain, in_decl);
13635
13636 if (vec)
13637 TREE_VEC_ELT (vec, i) = r;
13638 }
13639
13640 if (vec)
13641 r = vec;
13642 }
13643 break;
13644
13645 case USING_DECL:
13646 /* We reach here only for member using decls. We also need to check
13647 uses_template_parms because DECL_DEPENDENT_P is not set for a
13648 using-declaration that designates a member of the current
13649 instantiation (c++/53549). */
13650 if (DECL_DEPENDENT_P (t)
13651 || uses_template_parms (USING_DECL_SCOPE (t)))
13652 {
13653 tree scope = USING_DECL_SCOPE (t);
13654 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13655 if (PACK_EXPANSION_P (scope))
13656 {
13657 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13658 int len = TREE_VEC_LENGTH (vec);
13659 r = make_tree_vec (len);
13660 for (int i = 0; i < len; ++i)
13661 {
13662 tree escope = TREE_VEC_ELT (vec, i);
13663 tree elt = do_class_using_decl (escope, name);
13664 if (!elt)
13665 {
13666 r = error_mark_node;
13667 break;
13668 }
13669 else
13670 {
13671 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13672 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13673 }
13674 TREE_VEC_ELT (r, i) = elt;
13675 }
13676 }
13677 else
13678 {
13679 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13680 complain, in_decl);
13681 r = do_class_using_decl (inst_scope, name);
13682 if (!r)
13683 r = error_mark_node;
13684 else
13685 {
13686 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13687 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13688 }
13689 }
13690 }
13691 else
13692 {
13693 r = copy_node (t);
13694 DECL_CHAIN (r) = NULL_TREE;
13695 }
13696 break;
13697
13698 case TYPE_DECL:
13699 case VAR_DECL:
13700 {
13701 tree argvec = NULL_TREE;
13702 tree gen_tmpl = NULL_TREE;
13703 tree spec;
13704 tree tmpl = NULL_TREE;
13705 tree ctx;
13706 tree type = NULL_TREE;
13707 bool local_p;
13708
13709 if (TREE_TYPE (t) == error_mark_node)
13710 RETURN (error_mark_node);
13711
13712 if (TREE_CODE (t) == TYPE_DECL
13713 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13714 {
13715 /* If this is the canonical decl, we don't have to
13716 mess with instantiations, and often we can't (for
13717 typename, template type parms and such). Note that
13718 TYPE_NAME is not correct for the above test if
13719 we've copied the type for a typedef. */
13720 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13721 if (type == error_mark_node)
13722 RETURN (error_mark_node);
13723 r = TYPE_NAME (type);
13724 break;
13725 }
13726
13727 /* Check to see if we already have the specialization we
13728 need. */
13729 spec = NULL_TREE;
13730 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13731 {
13732 /* T is a static data member or namespace-scope entity.
13733 We have to substitute into namespace-scope variables
13734 (not just variable templates) because of cases like:
13735
13736 template <class T> void f() { extern T t; }
13737
13738 where the entity referenced is not known until
13739 instantiation time. */
13740 local_p = false;
13741 ctx = DECL_CONTEXT (t);
13742 if (DECL_CLASS_SCOPE_P (t))
13743 {
13744 ctx = tsubst_aggr_type (ctx, args,
13745 complain,
13746 in_decl, /*entering_scope=*/1);
13747 /* If CTX is unchanged, then T is in fact the
13748 specialization we want. That situation occurs when
13749 referencing a static data member within in its own
13750 class. We can use pointer equality, rather than
13751 same_type_p, because DECL_CONTEXT is always
13752 canonical... */
13753 if (ctx == DECL_CONTEXT (t)
13754 /* ... unless T is a member template; in which
13755 case our caller can be willing to create a
13756 specialization of that template represented
13757 by T. */
13758 && !(DECL_TI_TEMPLATE (t)
13759 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13760 spec = t;
13761 }
13762
13763 if (!spec)
13764 {
13765 tmpl = DECL_TI_TEMPLATE (t);
13766 gen_tmpl = most_general_template (tmpl);
13767 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13768 if (argvec != error_mark_node)
13769 argvec = (coerce_innermost_template_parms
13770 (DECL_TEMPLATE_PARMS (gen_tmpl),
13771 argvec, t, complain,
13772 /*all*/true, /*defarg*/true));
13773 if (argvec == error_mark_node)
13774 RETURN (error_mark_node);
13775 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13776 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13777 }
13778 }
13779 else
13780 {
13781 /* A local variable. */
13782 local_p = true;
13783 /* Subsequent calls to pushdecl will fill this in. */
13784 ctx = NULL_TREE;
13785 /* Unless this is a reference to a static variable from an
13786 enclosing function, in which case we need to fill it in now. */
13787 if (TREE_STATIC (t))
13788 {
13789 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13790 if (fn != current_function_decl)
13791 ctx = fn;
13792 }
13793 spec = retrieve_local_specialization (t);
13794 }
13795 /* If we already have the specialization we need, there is
13796 nothing more to do. */
13797 if (spec)
13798 {
13799 r = spec;
13800 break;
13801 }
13802
13803 /* Create a new node for the specialization we need. */
13804 if (type == NULL_TREE)
13805 {
13806 if (is_typedef_decl (t))
13807 type = DECL_ORIGINAL_TYPE (t);
13808 else
13809 type = TREE_TYPE (t);
13810 if (VAR_P (t)
13811 && VAR_HAD_UNKNOWN_BOUND (t)
13812 && type != error_mark_node)
13813 type = strip_array_domain (type);
13814 tree sub_args = args;
13815 if (tree auto_node = type_uses_auto (type))
13816 {
13817 /* Mask off any template args past the variable's context so we
13818 don't replace the auto with an unrelated argument. */
13819 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13820 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13821 if (extra > 0)
13822 /* This should never happen with the new lambda instantiation
13823 model, but keep the handling just in case. */
13824 gcc_assert (!CHECKING_P),
13825 sub_args = strip_innermost_template_args (args, extra);
13826 }
13827 type = tsubst (type, sub_args, complain, in_decl);
13828 /* Substituting the type might have recursively instantiated this
13829 same alias (c++/86171). */
13830 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13831 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13832 {
13833 r = spec;
13834 break;
13835 }
13836 }
13837 r = copy_decl (t);
13838 if (VAR_P (r))
13839 {
13840 DECL_INITIALIZED_P (r) = 0;
13841 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13842 if (type == error_mark_node)
13843 RETURN (error_mark_node);
13844 if (TREE_CODE (type) == FUNCTION_TYPE)
13845 {
13846 /* It may seem that this case cannot occur, since:
13847
13848 typedef void f();
13849 void g() { f x; }
13850
13851 declares a function, not a variable. However:
13852
13853 typedef void f();
13854 template <typename T> void g() { T t; }
13855 template void g<f>();
13856
13857 is an attempt to declare a variable with function
13858 type. */
13859 error ("variable %qD has function type",
13860 /* R is not yet sufficiently initialized, so we
13861 just use its name. */
13862 DECL_NAME (r));
13863 RETURN (error_mark_node);
13864 }
13865 type = complete_type (type);
13866 /* Wait until cp_finish_decl to set this again, to handle
13867 circular dependency (template/instantiate6.C). */
13868 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13869 type = check_var_type (DECL_NAME (r), type);
13870
13871 if (DECL_HAS_VALUE_EXPR_P (t))
13872 {
13873 tree ve = DECL_VALUE_EXPR (t);
13874 ve = tsubst_expr (ve, args, complain, in_decl,
13875 /*constant_expression_p=*/false);
13876 if (REFERENCE_REF_P (ve))
13877 {
13878 gcc_assert (TYPE_REF_P (type));
13879 ve = TREE_OPERAND (ve, 0);
13880 }
13881 SET_DECL_VALUE_EXPR (r, ve);
13882 }
13883 if (CP_DECL_THREAD_LOCAL_P (r)
13884 && !processing_template_decl)
13885 set_decl_tls_model (r, decl_default_tls_model (r));
13886 }
13887 else if (DECL_SELF_REFERENCE_P (t))
13888 SET_DECL_SELF_REFERENCE_P (r);
13889 TREE_TYPE (r) = type;
13890 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13891 DECL_CONTEXT (r) = ctx;
13892 /* Clear out the mangled name and RTL for the instantiation. */
13893 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13894 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13895 SET_DECL_RTL (r, NULL);
13896 /* The initializer must not be expanded until it is required;
13897 see [temp.inst]. */
13898 DECL_INITIAL (r) = NULL_TREE;
13899 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13900 if (VAR_P (r))
13901 {
13902 if (DECL_LANG_SPECIFIC (r))
13903 SET_DECL_DEPENDENT_INIT_P (r, false);
13904
13905 SET_DECL_MODE (r, VOIDmode);
13906
13907 /* Possibly limit visibility based on template args. */
13908 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13909 if (DECL_VISIBILITY_SPECIFIED (t))
13910 {
13911 DECL_VISIBILITY_SPECIFIED (r) = 0;
13912 DECL_ATTRIBUTES (r)
13913 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13914 }
13915 determine_visibility (r);
13916 }
13917
13918 if (!local_p)
13919 {
13920 /* A static data member declaration is always marked
13921 external when it is declared in-class, even if an
13922 initializer is present. We mimic the non-template
13923 processing here. */
13924 DECL_EXTERNAL (r) = 1;
13925 if (DECL_NAMESPACE_SCOPE_P (t))
13926 DECL_NOT_REALLY_EXTERN (r) = 1;
13927
13928 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13929 SET_DECL_IMPLICIT_INSTANTIATION (r);
13930 register_specialization (r, gen_tmpl, argvec, false, hash);
13931 }
13932 else
13933 {
13934 if (DECL_LANG_SPECIFIC (r))
13935 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13936 if (!cp_unevaluated_operand)
13937 register_local_specialization (r, t);
13938 }
13939
13940 DECL_CHAIN (r) = NULL_TREE;
13941
13942 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13943 /*flags=*/0,
13944 args, complain, in_decl);
13945
13946 /* Preserve a typedef that names a type. */
13947 if (is_typedef_decl (r) && type != error_mark_node)
13948 {
13949 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13950 set_underlying_type (r);
13951 if (TYPE_DECL_ALIAS_P (r))
13952 /* An alias template specialization can be dependent
13953 even if its underlying type is not. */
13954 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13955 }
13956
13957 layout_decl (r, 0);
13958 }
13959 break;
13960
13961 default:
13962 gcc_unreachable ();
13963 }
13964 #undef RETURN
13965
13966 out:
13967 /* Restore the file and line information. */
13968 input_location = saved_loc;
13969
13970 return r;
13971 }
13972
13973 /* Substitute into the ARG_TYPES of a function type.
13974 If END is a TREE_CHAIN, leave it and any following types
13975 un-substituted. */
13976
13977 static tree
13978 tsubst_arg_types (tree arg_types,
13979 tree args,
13980 tree end,
13981 tsubst_flags_t complain,
13982 tree in_decl)
13983 {
13984 tree remaining_arg_types;
13985 tree type = NULL_TREE;
13986 int i = 1;
13987 tree expanded_args = NULL_TREE;
13988 tree default_arg;
13989
13990 if (!arg_types || arg_types == void_list_node || arg_types == end)
13991 return arg_types;
13992
13993 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13994 args, end, complain, in_decl);
13995 if (remaining_arg_types == error_mark_node)
13996 return error_mark_node;
13997
13998 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13999 {
14000 /* For a pack expansion, perform substitution on the
14001 entire expression. Later on, we'll handle the arguments
14002 one-by-one. */
14003 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14004 args, complain, in_decl);
14005
14006 if (TREE_CODE (expanded_args) == TREE_VEC)
14007 /* So that we'll spin through the parameters, one by one. */
14008 i = TREE_VEC_LENGTH (expanded_args);
14009 else
14010 {
14011 /* We only partially substituted into the parameter
14012 pack. Our type is TYPE_PACK_EXPANSION. */
14013 type = expanded_args;
14014 expanded_args = NULL_TREE;
14015 }
14016 }
14017
14018 while (i > 0) {
14019 --i;
14020
14021 if (expanded_args)
14022 type = TREE_VEC_ELT (expanded_args, i);
14023 else if (!type)
14024 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14025
14026 if (type == error_mark_node)
14027 return error_mark_node;
14028 if (VOID_TYPE_P (type))
14029 {
14030 if (complain & tf_error)
14031 {
14032 error ("invalid parameter type %qT", type);
14033 if (in_decl)
14034 error ("in declaration %q+D", in_decl);
14035 }
14036 return error_mark_node;
14037 }
14038 /* DR 657. */
14039 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14040 return error_mark_node;
14041
14042 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14043 top-level qualifiers as required. */
14044 type = cv_unqualified (type_decays_to (type));
14045
14046 /* We do not substitute into default arguments here. The standard
14047 mandates that they be instantiated only when needed, which is
14048 done in build_over_call. */
14049 default_arg = TREE_PURPOSE (arg_types);
14050
14051 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14052 since the new op() won't have any associated template arguments for us
14053 to refer to later. */
14054 if (lambda_fn_in_template_p (in_decl))
14055 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14056 false/*fn*/, false/*constexpr*/);
14057
14058 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
14059 {
14060 /* We've instantiated a template before its default arguments
14061 have been parsed. This can happen for a nested template
14062 class, and is not an error unless we require the default
14063 argument in a call of this function. */
14064 remaining_arg_types =
14065 tree_cons (default_arg, type, remaining_arg_types);
14066 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
14067 }
14068 else
14069 remaining_arg_types =
14070 hash_tree_cons (default_arg, type, remaining_arg_types);
14071 }
14072
14073 return remaining_arg_types;
14074 }
14075
14076 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14077 *not* handle the exception-specification for FNTYPE, because the
14078 initial substitution of explicitly provided template parameters
14079 during argument deduction forbids substitution into the
14080 exception-specification:
14081
14082 [temp.deduct]
14083
14084 All references in the function type of the function template to the
14085 corresponding template parameters are replaced by the specified tem-
14086 plate argument values. If a substitution in a template parameter or
14087 in the function type of the function template results in an invalid
14088 type, type deduction fails. [Note: The equivalent substitution in
14089 exception specifications is done only when the function is instanti-
14090 ated, at which point a program is ill-formed if the substitution
14091 results in an invalid type.] */
14092
14093 static tree
14094 tsubst_function_type (tree t,
14095 tree args,
14096 tsubst_flags_t complain,
14097 tree in_decl)
14098 {
14099 tree return_type;
14100 tree arg_types = NULL_TREE;
14101 tree fntype;
14102
14103 /* The TYPE_CONTEXT is not used for function/method types. */
14104 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14105
14106 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14107 failure. */
14108 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14109
14110 if (late_return_type_p)
14111 {
14112 /* Substitute the argument types. */
14113 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14114 complain, in_decl);
14115 if (arg_types == error_mark_node)
14116 return error_mark_node;
14117
14118 tree save_ccp = current_class_ptr;
14119 tree save_ccr = current_class_ref;
14120 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14121 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14122 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14123 if (do_inject)
14124 {
14125 /* DR 1207: 'this' is in scope in the trailing return type. */
14126 inject_this_parameter (this_type, cp_type_quals (this_type));
14127 }
14128
14129 /* Substitute the return type. */
14130 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14131
14132 if (do_inject)
14133 {
14134 current_class_ptr = save_ccp;
14135 current_class_ref = save_ccr;
14136 }
14137 }
14138 else
14139 /* Substitute the return type. */
14140 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14141
14142 if (return_type == error_mark_node)
14143 return error_mark_node;
14144 /* DR 486 clarifies that creation of a function type with an
14145 invalid return type is a deduction failure. */
14146 if (TREE_CODE (return_type) == ARRAY_TYPE
14147 || TREE_CODE (return_type) == FUNCTION_TYPE)
14148 {
14149 if (complain & tf_error)
14150 {
14151 if (TREE_CODE (return_type) == ARRAY_TYPE)
14152 error ("function returning an array");
14153 else
14154 error ("function returning a function");
14155 }
14156 return error_mark_node;
14157 }
14158 /* And DR 657. */
14159 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14160 return error_mark_node;
14161
14162 if (!late_return_type_p)
14163 {
14164 /* Substitute the argument types. */
14165 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14166 complain, in_decl);
14167 if (arg_types == error_mark_node)
14168 return error_mark_node;
14169 }
14170
14171 /* Construct a new type node and return it. */
14172 if (TREE_CODE (t) == FUNCTION_TYPE)
14173 {
14174 fntype = build_function_type (return_type, arg_types);
14175 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14176 }
14177 else
14178 {
14179 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14180 /* Don't pick up extra function qualifiers from the basetype. */
14181 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14182 if (! MAYBE_CLASS_TYPE_P (r))
14183 {
14184 /* [temp.deduct]
14185
14186 Type deduction may fail for any of the following
14187 reasons:
14188
14189 -- Attempting to create "pointer to member of T" when T
14190 is not a class type. */
14191 if (complain & tf_error)
14192 error ("creating pointer to member function of non-class type %qT",
14193 r);
14194 return error_mark_node;
14195 }
14196
14197 fntype = build_method_type_directly (r, return_type,
14198 TREE_CHAIN (arg_types));
14199 }
14200 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14201
14202 /* See comment above. */
14203 tree raises = NULL_TREE;
14204 cp_ref_qualifier rqual = type_memfn_rqual (t);
14205 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14206
14207 return fntype;
14208 }
14209
14210 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14211 ARGS into that specification, and return the substituted
14212 specification. If there is no specification, return NULL_TREE. */
14213
14214 static tree
14215 tsubst_exception_specification (tree fntype,
14216 tree args,
14217 tsubst_flags_t complain,
14218 tree in_decl,
14219 bool defer_ok)
14220 {
14221 tree specs;
14222 tree new_specs;
14223
14224 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14225 new_specs = NULL_TREE;
14226 if (specs && TREE_PURPOSE (specs))
14227 {
14228 /* A noexcept-specifier. */
14229 tree expr = TREE_PURPOSE (specs);
14230 if (TREE_CODE (expr) == INTEGER_CST)
14231 new_specs = expr;
14232 else if (defer_ok)
14233 {
14234 /* Defer instantiation of noexcept-specifiers to avoid
14235 excessive instantiations (c++/49107). */
14236 new_specs = make_node (DEFERRED_NOEXCEPT);
14237 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14238 {
14239 /* We already partially instantiated this member template,
14240 so combine the new args with the old. */
14241 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14242 = DEFERRED_NOEXCEPT_PATTERN (expr);
14243 DEFERRED_NOEXCEPT_ARGS (new_specs)
14244 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14245 }
14246 else
14247 {
14248 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14249 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14250 }
14251 }
14252 else
14253 {
14254 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14255 {
14256 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14257 args);
14258 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14259 }
14260 new_specs = tsubst_copy_and_build
14261 (expr, args, complain, in_decl, /*function_p=*/false,
14262 /*integral_constant_expression_p=*/true);
14263 }
14264 new_specs = build_noexcept_spec (new_specs, complain);
14265 }
14266 else if (specs)
14267 {
14268 if (! TREE_VALUE (specs))
14269 new_specs = specs;
14270 else
14271 while (specs)
14272 {
14273 tree spec;
14274 int i, len = 1;
14275 tree expanded_specs = NULL_TREE;
14276
14277 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14278 {
14279 /* Expand the pack expansion type. */
14280 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14281 args, complain,
14282 in_decl);
14283
14284 if (expanded_specs == error_mark_node)
14285 return error_mark_node;
14286 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14287 len = TREE_VEC_LENGTH (expanded_specs);
14288 else
14289 {
14290 /* We're substituting into a member template, so
14291 we got a TYPE_PACK_EXPANSION back. Add that
14292 expansion and move on. */
14293 gcc_assert (TREE_CODE (expanded_specs)
14294 == TYPE_PACK_EXPANSION);
14295 new_specs = add_exception_specifier (new_specs,
14296 expanded_specs,
14297 complain);
14298 specs = TREE_CHAIN (specs);
14299 continue;
14300 }
14301 }
14302
14303 for (i = 0; i < len; ++i)
14304 {
14305 if (expanded_specs)
14306 spec = TREE_VEC_ELT (expanded_specs, i);
14307 else
14308 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14309 if (spec == error_mark_node)
14310 return spec;
14311 new_specs = add_exception_specifier (new_specs, spec,
14312 complain);
14313 }
14314
14315 specs = TREE_CHAIN (specs);
14316 }
14317 }
14318 return new_specs;
14319 }
14320
14321 /* Take the tree structure T and replace template parameters used
14322 therein with the argument vector ARGS. IN_DECL is an associated
14323 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14324 Issue error and warning messages under control of COMPLAIN. Note
14325 that we must be relatively non-tolerant of extensions here, in
14326 order to preserve conformance; if we allow substitutions that
14327 should not be allowed, we may allow argument deductions that should
14328 not succeed, and therefore report ambiguous overload situations
14329 where there are none. In theory, we could allow the substitution,
14330 but indicate that it should have failed, and allow our caller to
14331 make sure that the right thing happens, but we don't try to do this
14332 yet.
14333
14334 This function is used for dealing with types, decls and the like;
14335 for expressions, use tsubst_expr or tsubst_copy. */
14336
14337 tree
14338 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14339 {
14340 enum tree_code code;
14341 tree type, r = NULL_TREE;
14342
14343 if (t == NULL_TREE || t == error_mark_node
14344 || t == integer_type_node
14345 || t == void_type_node
14346 || t == char_type_node
14347 || t == unknown_type_node
14348 || TREE_CODE (t) == NAMESPACE_DECL
14349 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14350 return t;
14351
14352 if (DECL_P (t))
14353 return tsubst_decl (t, args, complain);
14354
14355 if (args == NULL_TREE)
14356 return t;
14357
14358 code = TREE_CODE (t);
14359
14360 if (code == IDENTIFIER_NODE)
14361 type = IDENTIFIER_TYPE_VALUE (t);
14362 else
14363 type = TREE_TYPE (t);
14364
14365 gcc_assert (type != unknown_type_node);
14366
14367 /* Reuse typedefs. We need to do this to handle dependent attributes,
14368 such as attribute aligned. */
14369 if (TYPE_P (t)
14370 && typedef_variant_p (t))
14371 {
14372 tree decl = TYPE_NAME (t);
14373
14374 if (alias_template_specialization_p (t))
14375 {
14376 /* DECL represents an alias template and we want to
14377 instantiate it. */
14378 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14379 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14380 r = instantiate_alias_template (tmpl, gen_args, complain);
14381 }
14382 else if (DECL_CLASS_SCOPE_P (decl)
14383 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14384 && uses_template_parms (DECL_CONTEXT (decl)))
14385 {
14386 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14387 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14388 r = retrieve_specialization (tmpl, gen_args, 0);
14389 }
14390 else if (DECL_FUNCTION_SCOPE_P (decl)
14391 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14392 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14393 r = retrieve_local_specialization (decl);
14394 else
14395 /* The typedef is from a non-template context. */
14396 return t;
14397
14398 if (r)
14399 {
14400 r = TREE_TYPE (r);
14401 r = cp_build_qualified_type_real
14402 (r, cp_type_quals (t) | cp_type_quals (r),
14403 complain | tf_ignore_bad_quals);
14404 return r;
14405 }
14406 else
14407 {
14408 /* We don't have an instantiation yet, so drop the typedef. */
14409 int quals = cp_type_quals (t);
14410 t = DECL_ORIGINAL_TYPE (decl);
14411 t = cp_build_qualified_type_real (t, quals,
14412 complain | tf_ignore_bad_quals);
14413 }
14414 }
14415
14416 bool fndecl_type = (complain & tf_fndecl_type);
14417 complain &= ~tf_fndecl_type;
14418
14419 if (type
14420 && code != TYPENAME_TYPE
14421 && code != TEMPLATE_TYPE_PARM
14422 && code != TEMPLATE_PARM_INDEX
14423 && code != IDENTIFIER_NODE
14424 && code != FUNCTION_TYPE
14425 && code != METHOD_TYPE)
14426 type = tsubst (type, args, complain, in_decl);
14427 if (type == error_mark_node)
14428 return error_mark_node;
14429
14430 switch (code)
14431 {
14432 case RECORD_TYPE:
14433 case UNION_TYPE:
14434 case ENUMERAL_TYPE:
14435 return tsubst_aggr_type (t, args, complain, in_decl,
14436 /*entering_scope=*/0);
14437
14438 case ERROR_MARK:
14439 case IDENTIFIER_NODE:
14440 case VOID_TYPE:
14441 case REAL_TYPE:
14442 case COMPLEX_TYPE:
14443 case VECTOR_TYPE:
14444 case BOOLEAN_TYPE:
14445 case NULLPTR_TYPE:
14446 case LANG_TYPE:
14447 return t;
14448
14449 case INTEGER_TYPE:
14450 if (t == integer_type_node)
14451 return t;
14452
14453 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14454 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14455 return t;
14456
14457 {
14458 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14459
14460 max = tsubst_expr (omax, args, complain, in_decl,
14461 /*integral_constant_expression_p=*/false);
14462
14463 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14464 needed. */
14465 if (TREE_CODE (max) == NOP_EXPR
14466 && TREE_SIDE_EFFECTS (omax)
14467 && !TREE_TYPE (max))
14468 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14469
14470 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14471 with TREE_SIDE_EFFECTS that indicates this is not an integral
14472 constant expression. */
14473 if (processing_template_decl
14474 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14475 {
14476 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14477 TREE_SIDE_EFFECTS (max) = 1;
14478 }
14479
14480 return compute_array_index_type (NULL_TREE, max, complain);
14481 }
14482
14483 case TEMPLATE_TYPE_PARM:
14484 case TEMPLATE_TEMPLATE_PARM:
14485 case BOUND_TEMPLATE_TEMPLATE_PARM:
14486 case TEMPLATE_PARM_INDEX:
14487 {
14488 int idx;
14489 int level;
14490 int levels;
14491 tree arg = NULL_TREE;
14492
14493 /* Early in template argument deduction substitution, we don't
14494 want to reduce the level of 'auto', or it will be confused
14495 with a normal template parm in subsequent deduction. */
14496 if (is_auto (t) && (complain & tf_partial))
14497 return t;
14498
14499 r = NULL_TREE;
14500
14501 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14502 template_parm_level_and_index (t, &level, &idx);
14503
14504 levels = TMPL_ARGS_DEPTH (args);
14505 if (level <= levels
14506 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14507 {
14508 arg = TMPL_ARG (args, level, idx);
14509
14510 /* See through ARGUMENT_PACK_SELECT arguments. */
14511 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14512 arg = argument_pack_select_arg (arg);
14513 }
14514
14515 if (arg == error_mark_node)
14516 return error_mark_node;
14517 else if (arg != NULL_TREE)
14518 {
14519 if (ARGUMENT_PACK_P (arg))
14520 /* If ARG is an argument pack, we don't actually want to
14521 perform a substitution here, because substitutions
14522 for argument packs are only done
14523 element-by-element. We can get to this point when
14524 substituting the type of a non-type template
14525 parameter pack, when that type actually contains
14526 template parameter packs from an outer template, e.g.,
14527
14528 template<typename... Types> struct A {
14529 template<Types... Values> struct B { };
14530 }; */
14531 return t;
14532
14533 if (code == TEMPLATE_TYPE_PARM)
14534 {
14535 int quals;
14536 gcc_assert (TYPE_P (arg));
14537
14538 quals = cp_type_quals (arg) | cp_type_quals (t);
14539
14540 return cp_build_qualified_type_real
14541 (arg, quals, complain | tf_ignore_bad_quals);
14542 }
14543 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14544 {
14545 /* We are processing a type constructed from a
14546 template template parameter. */
14547 tree argvec = tsubst (TYPE_TI_ARGS (t),
14548 args, complain, in_decl);
14549 if (argvec == error_mark_node)
14550 return error_mark_node;
14551
14552 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14553 || TREE_CODE (arg) == TEMPLATE_DECL
14554 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14555
14556 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14557 /* Consider this code:
14558
14559 template <template <class> class Template>
14560 struct Internal {
14561 template <class Arg> using Bind = Template<Arg>;
14562 };
14563
14564 template <template <class> class Template, class Arg>
14565 using Instantiate = Template<Arg>; //#0
14566
14567 template <template <class> class Template,
14568 class Argument>
14569 using Bind =
14570 Instantiate<Internal<Template>::template Bind,
14571 Argument>; //#1
14572
14573 When #1 is parsed, the
14574 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14575 parameter `Template' in #0 matches the
14576 UNBOUND_CLASS_TEMPLATE representing the argument
14577 `Internal<Template>::template Bind'; We then want
14578 to assemble the type `Bind<Argument>' that can't
14579 be fully created right now, because
14580 `Internal<Template>' not being complete, the Bind
14581 template cannot be looked up in that context. So
14582 we need to "store" `Bind<Argument>' for later
14583 when the context of Bind becomes complete. Let's
14584 store that in a TYPENAME_TYPE. */
14585 return make_typename_type (TYPE_CONTEXT (arg),
14586 build_nt (TEMPLATE_ID_EXPR,
14587 TYPE_IDENTIFIER (arg),
14588 argvec),
14589 typename_type,
14590 complain);
14591
14592 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14593 are resolving nested-types in the signature of a
14594 member function templates. Otherwise ARG is a
14595 TEMPLATE_DECL and is the real template to be
14596 instantiated. */
14597 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14598 arg = TYPE_NAME (arg);
14599
14600 r = lookup_template_class (arg,
14601 argvec, in_decl,
14602 DECL_CONTEXT (arg),
14603 /*entering_scope=*/0,
14604 complain);
14605 return cp_build_qualified_type_real
14606 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14607 }
14608 else if (code == TEMPLATE_TEMPLATE_PARM)
14609 return arg;
14610 else
14611 /* TEMPLATE_PARM_INDEX. */
14612 return convert_from_reference (unshare_expr (arg));
14613 }
14614
14615 if (level == 1)
14616 /* This can happen during the attempted tsubst'ing in
14617 unify. This means that we don't yet have any information
14618 about the template parameter in question. */
14619 return t;
14620
14621 /* If we get here, we must have been looking at a parm for a
14622 more deeply nested template. Make a new version of this
14623 template parameter, but with a lower level. */
14624 switch (code)
14625 {
14626 case TEMPLATE_TYPE_PARM:
14627 case TEMPLATE_TEMPLATE_PARM:
14628 case BOUND_TEMPLATE_TEMPLATE_PARM:
14629 if (cp_type_quals (t))
14630 {
14631 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14632 r = cp_build_qualified_type_real
14633 (r, cp_type_quals (t),
14634 complain | (code == TEMPLATE_TYPE_PARM
14635 ? tf_ignore_bad_quals : 0));
14636 }
14637 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14638 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14639 && (r = (TEMPLATE_PARM_DESCENDANTS
14640 (TEMPLATE_TYPE_PARM_INDEX (t))))
14641 && (r = TREE_TYPE (r))
14642 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14643 /* Break infinite recursion when substituting the constraints
14644 of a constrained placeholder. */;
14645 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14646 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14647 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14648 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14649 r = TEMPLATE_PARM_DESCENDANTS (arg))
14650 && (TEMPLATE_PARM_LEVEL (r)
14651 == TEMPLATE_PARM_LEVEL (arg) - levels))
14652 /* Cache the simple case of lowering a type parameter. */
14653 r = TREE_TYPE (r);
14654 else
14655 {
14656 r = copy_type (t);
14657 TEMPLATE_TYPE_PARM_INDEX (r)
14658 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14659 r, levels, args, complain);
14660 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14661 TYPE_MAIN_VARIANT (r) = r;
14662 TYPE_POINTER_TO (r) = NULL_TREE;
14663 TYPE_REFERENCE_TO (r) = NULL_TREE;
14664
14665 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14666 {
14667 /* Propagate constraints on placeholders. */
14668 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14669 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14670 = tsubst_constraint (constr, args, complain, in_decl);
14671 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14672 {
14673 pl = tsubst_copy (pl, args, complain, in_decl);
14674 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14675 }
14676 }
14677
14678 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14679 /* We have reduced the level of the template
14680 template parameter, but not the levels of its
14681 template parameters, so canonical_type_parameter
14682 will not be able to find the canonical template
14683 template parameter for this level. Thus, we
14684 require structural equality checking to compare
14685 TEMPLATE_TEMPLATE_PARMs. */
14686 SET_TYPE_STRUCTURAL_EQUALITY (r);
14687 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14688 SET_TYPE_STRUCTURAL_EQUALITY (r);
14689 else
14690 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14691
14692 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14693 {
14694 tree tinfo = TYPE_TEMPLATE_INFO (t);
14695 /* We might need to substitute into the types of non-type
14696 template parameters. */
14697 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14698 complain, in_decl);
14699 if (tmpl == error_mark_node)
14700 return error_mark_node;
14701 tree argvec = tsubst (TI_ARGS (tinfo), args,
14702 complain, in_decl);
14703 if (argvec == error_mark_node)
14704 return error_mark_node;
14705
14706 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14707 = build_template_info (tmpl, argvec);
14708 }
14709 }
14710 break;
14711
14712 case TEMPLATE_PARM_INDEX:
14713 /* OK, now substitute the type of the non-type parameter. We
14714 couldn't do it earlier because it might be an auto parameter,
14715 and we wouldn't need to if we had an argument. */
14716 type = tsubst (type, args, complain, in_decl);
14717 if (type == error_mark_node)
14718 return error_mark_node;
14719 r = reduce_template_parm_level (t, type, levels, args, complain);
14720 break;
14721
14722 default:
14723 gcc_unreachable ();
14724 }
14725
14726 return r;
14727 }
14728
14729 case TREE_LIST:
14730 {
14731 tree purpose, value, chain;
14732
14733 if (t == void_list_node)
14734 return t;
14735
14736 purpose = TREE_PURPOSE (t);
14737 if (purpose)
14738 {
14739 purpose = tsubst (purpose, args, complain, in_decl);
14740 if (purpose == error_mark_node)
14741 return error_mark_node;
14742 }
14743 value = TREE_VALUE (t);
14744 if (value)
14745 {
14746 value = tsubst (value, args, complain, in_decl);
14747 if (value == error_mark_node)
14748 return error_mark_node;
14749 }
14750 chain = TREE_CHAIN (t);
14751 if (chain && chain != void_type_node)
14752 {
14753 chain = tsubst (chain, args, complain, in_decl);
14754 if (chain == error_mark_node)
14755 return error_mark_node;
14756 }
14757 if (purpose == TREE_PURPOSE (t)
14758 && value == TREE_VALUE (t)
14759 && chain == TREE_CHAIN (t))
14760 return t;
14761 return hash_tree_cons (purpose, value, chain);
14762 }
14763
14764 case TREE_BINFO:
14765 /* We should never be tsubsting a binfo. */
14766 gcc_unreachable ();
14767
14768 case TREE_VEC:
14769 /* A vector of template arguments. */
14770 gcc_assert (!type);
14771 return tsubst_template_args (t, args, complain, in_decl);
14772
14773 case POINTER_TYPE:
14774 case REFERENCE_TYPE:
14775 {
14776 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14777 return t;
14778
14779 /* [temp.deduct]
14780
14781 Type deduction may fail for any of the following
14782 reasons:
14783
14784 -- Attempting to create a pointer to reference type.
14785 -- Attempting to create a reference to a reference type or
14786 a reference to void.
14787
14788 Core issue 106 says that creating a reference to a reference
14789 during instantiation is no longer a cause for failure. We
14790 only enforce this check in strict C++98 mode. */
14791 if ((TYPE_REF_P (type)
14792 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14793 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14794 {
14795 static location_t last_loc;
14796
14797 /* We keep track of the last time we issued this error
14798 message to avoid spewing a ton of messages during a
14799 single bad template instantiation. */
14800 if (complain & tf_error
14801 && last_loc != input_location)
14802 {
14803 if (VOID_TYPE_P (type))
14804 error ("forming reference to void");
14805 else if (code == POINTER_TYPE)
14806 error ("forming pointer to reference type %qT", type);
14807 else
14808 error ("forming reference to reference type %qT", type);
14809 last_loc = input_location;
14810 }
14811
14812 return error_mark_node;
14813 }
14814 else if (TREE_CODE (type) == FUNCTION_TYPE
14815 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14816 || type_memfn_rqual (type) != REF_QUAL_NONE))
14817 {
14818 if (complain & tf_error)
14819 {
14820 if (code == POINTER_TYPE)
14821 error ("forming pointer to qualified function type %qT",
14822 type);
14823 else
14824 error ("forming reference to qualified function type %qT",
14825 type);
14826 }
14827 return error_mark_node;
14828 }
14829 else if (code == POINTER_TYPE)
14830 {
14831 r = build_pointer_type (type);
14832 if (TREE_CODE (type) == METHOD_TYPE)
14833 r = build_ptrmemfunc_type (r);
14834 }
14835 else if (TYPE_REF_P (type))
14836 /* In C++0x, during template argument substitution, when there is an
14837 attempt to create a reference to a reference type, reference
14838 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14839
14840 "If a template-argument for a template-parameter T names a type
14841 that is a reference to a type A, an attempt to create the type
14842 'lvalue reference to cv T' creates the type 'lvalue reference to
14843 A,' while an attempt to create the type type rvalue reference to
14844 cv T' creates the type T"
14845 */
14846 r = cp_build_reference_type
14847 (TREE_TYPE (type),
14848 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14849 else
14850 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14851 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14852
14853 if (r != error_mark_node)
14854 /* Will this ever be needed for TYPE_..._TO values? */
14855 layout_type (r);
14856
14857 return r;
14858 }
14859 case OFFSET_TYPE:
14860 {
14861 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14862 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14863 {
14864 /* [temp.deduct]
14865
14866 Type deduction may fail for any of the following
14867 reasons:
14868
14869 -- Attempting to create "pointer to member of T" when T
14870 is not a class type. */
14871 if (complain & tf_error)
14872 error ("creating pointer to member of non-class type %qT", r);
14873 return error_mark_node;
14874 }
14875 if (TYPE_REF_P (type))
14876 {
14877 if (complain & tf_error)
14878 error ("creating pointer to member reference type %qT", type);
14879 return error_mark_node;
14880 }
14881 if (VOID_TYPE_P (type))
14882 {
14883 if (complain & tf_error)
14884 error ("creating pointer to member of type void");
14885 return error_mark_node;
14886 }
14887 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14888 if (TREE_CODE (type) == FUNCTION_TYPE)
14889 {
14890 /* The type of the implicit object parameter gets its
14891 cv-qualifiers from the FUNCTION_TYPE. */
14892 tree memptr;
14893 tree method_type
14894 = build_memfn_type (type, r, type_memfn_quals (type),
14895 type_memfn_rqual (type));
14896 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14897 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14898 complain);
14899 }
14900 else
14901 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14902 cp_type_quals (t),
14903 complain);
14904 }
14905 case FUNCTION_TYPE:
14906 case METHOD_TYPE:
14907 {
14908 tree fntype;
14909 tree specs;
14910 fntype = tsubst_function_type (t, args, complain, in_decl);
14911 if (fntype == error_mark_node)
14912 return error_mark_node;
14913
14914 /* Substitute the exception specification. */
14915 specs = tsubst_exception_specification (t, args, complain, in_decl,
14916 /*defer_ok*/fndecl_type);
14917 if (specs == error_mark_node)
14918 return error_mark_node;
14919 if (specs)
14920 fntype = build_exception_variant (fntype, specs);
14921 return fntype;
14922 }
14923 case ARRAY_TYPE:
14924 {
14925 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14926 if (domain == error_mark_node)
14927 return error_mark_node;
14928
14929 /* As an optimization, we avoid regenerating the array type if
14930 it will obviously be the same as T. */
14931 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14932 return t;
14933
14934 /* These checks should match the ones in create_array_type_for_decl.
14935
14936 [temp.deduct]
14937
14938 The deduction may fail for any of the following reasons:
14939
14940 -- Attempting to create an array with an element type that
14941 is void, a function type, or a reference type, or [DR337]
14942 an abstract class type. */
14943 if (VOID_TYPE_P (type)
14944 || TREE_CODE (type) == FUNCTION_TYPE
14945 || (TREE_CODE (type) == ARRAY_TYPE
14946 && TYPE_DOMAIN (type) == NULL_TREE)
14947 || TYPE_REF_P (type))
14948 {
14949 if (complain & tf_error)
14950 error ("creating array of %qT", type);
14951 return error_mark_node;
14952 }
14953
14954 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14955 return error_mark_node;
14956
14957 r = build_cplus_array_type (type, domain);
14958
14959 if (!valid_array_size_p (input_location, r, in_decl,
14960 (complain & tf_error)))
14961 return error_mark_node;
14962
14963 if (TYPE_USER_ALIGN (t))
14964 {
14965 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14966 TYPE_USER_ALIGN (r) = 1;
14967 }
14968
14969 return r;
14970 }
14971
14972 case TYPENAME_TYPE:
14973 {
14974 tree ctx = TYPE_CONTEXT (t);
14975 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
14976 {
14977 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
14978 if (ctx == error_mark_node
14979 || TREE_VEC_LENGTH (ctx) > 1)
14980 return error_mark_node;
14981 if (TREE_VEC_LENGTH (ctx) == 0)
14982 {
14983 if (complain & tf_error)
14984 error ("%qD is instantiated for an empty pack",
14985 TYPENAME_TYPE_FULLNAME (t));
14986 return error_mark_node;
14987 }
14988 ctx = TREE_VEC_ELT (ctx, 0);
14989 }
14990 else
14991 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
14992 /*entering_scope=*/1);
14993 if (ctx == error_mark_node)
14994 return error_mark_node;
14995
14996 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14997 complain, in_decl);
14998 if (f == error_mark_node)
14999 return error_mark_node;
15000
15001 if (!MAYBE_CLASS_TYPE_P (ctx))
15002 {
15003 if (complain & tf_error)
15004 error ("%qT is not a class, struct, or union type", ctx);
15005 return error_mark_node;
15006 }
15007 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15008 {
15009 /* Normally, make_typename_type does not require that the CTX
15010 have complete type in order to allow things like:
15011
15012 template <class T> struct S { typename S<T>::X Y; };
15013
15014 But, such constructs have already been resolved by this
15015 point, so here CTX really should have complete type, unless
15016 it's a partial instantiation. */
15017 ctx = complete_type (ctx);
15018 if (!COMPLETE_TYPE_P (ctx))
15019 {
15020 if (complain & tf_error)
15021 cxx_incomplete_type_error (NULL_TREE, ctx);
15022 return error_mark_node;
15023 }
15024 }
15025
15026 f = make_typename_type (ctx, f, typename_type,
15027 complain | tf_keep_type_decl);
15028 if (f == error_mark_node)
15029 return f;
15030 if (TREE_CODE (f) == TYPE_DECL)
15031 {
15032 complain |= tf_ignore_bad_quals;
15033 f = TREE_TYPE (f);
15034 }
15035
15036 if (TREE_CODE (f) != TYPENAME_TYPE)
15037 {
15038 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15039 {
15040 if (complain & tf_error)
15041 error ("%qT resolves to %qT, which is not an enumeration type",
15042 t, f);
15043 else
15044 return error_mark_node;
15045 }
15046 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15047 {
15048 if (complain & tf_error)
15049 error ("%qT resolves to %qT, which is is not a class type",
15050 t, f);
15051 else
15052 return error_mark_node;
15053 }
15054 }
15055
15056 return cp_build_qualified_type_real
15057 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15058 }
15059
15060 case UNBOUND_CLASS_TEMPLATE:
15061 {
15062 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15063 in_decl, /*entering_scope=*/1);
15064 tree name = TYPE_IDENTIFIER (t);
15065 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15066
15067 if (ctx == error_mark_node || name == error_mark_node)
15068 return error_mark_node;
15069
15070 if (parm_list)
15071 parm_list = tsubst_template_parms (parm_list, args, complain);
15072 return make_unbound_class_template (ctx, name, parm_list, complain);
15073 }
15074
15075 case TYPEOF_TYPE:
15076 {
15077 tree type;
15078
15079 ++cp_unevaluated_operand;
15080 ++c_inhibit_evaluation_warnings;
15081
15082 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15083 complain, in_decl,
15084 /*integral_constant_expression_p=*/false);
15085
15086 --cp_unevaluated_operand;
15087 --c_inhibit_evaluation_warnings;
15088
15089 type = finish_typeof (type);
15090 return cp_build_qualified_type_real (type,
15091 cp_type_quals (t)
15092 | cp_type_quals (type),
15093 complain);
15094 }
15095
15096 case DECLTYPE_TYPE:
15097 {
15098 tree type;
15099
15100 ++cp_unevaluated_operand;
15101 ++c_inhibit_evaluation_warnings;
15102
15103 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15104 complain|tf_decltype, in_decl,
15105 /*function_p*/false,
15106 /*integral_constant_expression*/false);
15107
15108 if (DECLTYPE_FOR_INIT_CAPTURE (t))
15109 {
15110 if (type == NULL_TREE)
15111 {
15112 if (complain & tf_error)
15113 error ("empty initializer in lambda init-capture");
15114 type = error_mark_node;
15115 }
15116 else if (TREE_CODE (type) == TREE_LIST)
15117 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
15118 }
15119
15120 --cp_unevaluated_operand;
15121 --c_inhibit_evaluation_warnings;
15122
15123 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15124 type = lambda_capture_field_type (type,
15125 DECLTYPE_FOR_INIT_CAPTURE (t),
15126 DECLTYPE_FOR_REF_CAPTURE (t));
15127 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15128 type = lambda_proxy_type (type);
15129 else
15130 {
15131 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15132 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15133 && EXPR_P (type))
15134 /* In a template ~id could be either a complement expression
15135 or an unqualified-id naming a destructor; if instantiating
15136 it produces an expression, it's not an id-expression or
15137 member access. */
15138 id = false;
15139 type = finish_decltype_type (type, id, complain);
15140 }
15141 return cp_build_qualified_type_real (type,
15142 cp_type_quals (t)
15143 | cp_type_quals (type),
15144 complain | tf_ignore_bad_quals);
15145 }
15146
15147 case UNDERLYING_TYPE:
15148 {
15149 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15150 complain, in_decl);
15151 return finish_underlying_type (type);
15152 }
15153
15154 case TYPE_ARGUMENT_PACK:
15155 case NONTYPE_ARGUMENT_PACK:
15156 {
15157 tree r;
15158
15159 if (code == NONTYPE_ARGUMENT_PACK)
15160 r = make_node (code);
15161 else
15162 r = cxx_make_type (code);
15163
15164 tree pack_args = ARGUMENT_PACK_ARGS (t);
15165 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15166 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15167
15168 return r;
15169 }
15170
15171 case VOID_CST:
15172 case INTEGER_CST:
15173 case REAL_CST:
15174 case STRING_CST:
15175 case PLUS_EXPR:
15176 case MINUS_EXPR:
15177 case NEGATE_EXPR:
15178 case NOP_EXPR:
15179 case INDIRECT_REF:
15180 case ADDR_EXPR:
15181 case CALL_EXPR:
15182 case ARRAY_REF:
15183 case SCOPE_REF:
15184 /* We should use one of the expression tsubsts for these codes. */
15185 gcc_unreachable ();
15186
15187 default:
15188 sorry ("use of %qs in template", get_tree_code_name (code));
15189 return error_mark_node;
15190 }
15191 }
15192
15193 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15194 expression on the left-hand side of the "." or "->" operator. We
15195 only do the lookup if we had a dependent BASELINK. Otherwise we
15196 adjust it onto the instantiated heirarchy. */
15197
15198 static tree
15199 tsubst_baselink (tree baselink, tree object_type,
15200 tree args, tsubst_flags_t complain, tree in_decl)
15201 {
15202 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15203 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15204 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15205
15206 tree optype = BASELINK_OPTYPE (baselink);
15207 optype = tsubst (optype, args, complain, in_decl);
15208
15209 tree template_args = NULL_TREE;
15210 bool template_id_p = false;
15211 tree fns = BASELINK_FUNCTIONS (baselink);
15212 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15213 {
15214 template_id_p = true;
15215 template_args = TREE_OPERAND (fns, 1);
15216 fns = TREE_OPERAND (fns, 0);
15217 if (template_args)
15218 template_args = tsubst_template_args (template_args, args,
15219 complain, in_decl);
15220 }
15221
15222 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15223 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15224 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15225
15226 if (dependent_p)
15227 {
15228 tree name = OVL_NAME (fns);
15229 if (IDENTIFIER_CONV_OP_P (name))
15230 name = make_conv_op_name (optype);
15231
15232 if (name == complete_dtor_identifier)
15233 /* Treat as-if non-dependent below. */
15234 dependent_p = false;
15235
15236 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15237 if (!baselink)
15238 {
15239 if ((complain & tf_error)
15240 && constructor_name_p (name, qualifying_scope))
15241 error ("cannot call constructor %<%T::%D%> directly",
15242 qualifying_scope, name);
15243 return error_mark_node;
15244 }
15245
15246 if (BASELINK_P (baselink))
15247 fns = BASELINK_FUNCTIONS (baselink);
15248 }
15249 else
15250 /* We're going to overwrite pieces below, make a duplicate. */
15251 baselink = copy_node (baselink);
15252
15253 /* If lookup found a single function, mark it as used at this point.
15254 (If lookup found multiple functions the one selected later by
15255 overload resolution will be marked as used at that point.) */
15256 if (!template_id_p && !really_overloaded_fn (fns))
15257 {
15258 tree fn = OVL_FIRST (fns);
15259 bool ok = mark_used (fn, complain);
15260 if (!ok && !(complain & tf_error))
15261 return error_mark_node;
15262 if (ok && BASELINK_P (baselink))
15263 /* We might have instantiated an auto function. */
15264 TREE_TYPE (baselink) = TREE_TYPE (fn);
15265 }
15266
15267 if (BASELINK_P (baselink))
15268 {
15269 /* Add back the template arguments, if present. */
15270 if (template_id_p)
15271 BASELINK_FUNCTIONS (baselink)
15272 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15273
15274 /* Update the conversion operator type. */
15275 BASELINK_OPTYPE (baselink) = optype;
15276 }
15277
15278 if (!object_type)
15279 object_type = current_class_type;
15280
15281 if (qualified_p || !dependent_p)
15282 {
15283 baselink = adjust_result_of_qualified_name_lookup (baselink,
15284 qualifying_scope,
15285 object_type);
15286 if (!qualified_p)
15287 /* We need to call adjust_result_of_qualified_name_lookup in case the
15288 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15289 so that we still get virtual function binding. */
15290 BASELINK_QUALIFIED_P (baselink) = false;
15291 }
15292
15293 return baselink;
15294 }
15295
15296 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15297 true if the qualified-id will be a postfix-expression in-and-of
15298 itself; false if more of the postfix-expression follows the
15299 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15300 of "&". */
15301
15302 static tree
15303 tsubst_qualified_id (tree qualified_id, tree args,
15304 tsubst_flags_t complain, tree in_decl,
15305 bool done, bool address_p)
15306 {
15307 tree expr;
15308 tree scope;
15309 tree name;
15310 bool is_template;
15311 tree template_args;
15312 location_t loc = UNKNOWN_LOCATION;
15313
15314 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15315
15316 /* Figure out what name to look up. */
15317 name = TREE_OPERAND (qualified_id, 1);
15318 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15319 {
15320 is_template = true;
15321 loc = EXPR_LOCATION (name);
15322 template_args = TREE_OPERAND (name, 1);
15323 if (template_args)
15324 template_args = tsubst_template_args (template_args, args,
15325 complain, in_decl);
15326 if (template_args == error_mark_node)
15327 return error_mark_node;
15328 name = TREE_OPERAND (name, 0);
15329 }
15330 else
15331 {
15332 is_template = false;
15333 template_args = NULL_TREE;
15334 }
15335
15336 /* Substitute into the qualifying scope. When there are no ARGS, we
15337 are just trying to simplify a non-dependent expression. In that
15338 case the qualifying scope may be dependent, and, in any case,
15339 substituting will not help. */
15340 scope = TREE_OPERAND (qualified_id, 0);
15341 if (args)
15342 {
15343 scope = tsubst (scope, args, complain, in_decl);
15344 expr = tsubst_copy (name, args, complain, in_decl);
15345 }
15346 else
15347 expr = name;
15348
15349 if (dependent_scope_p (scope))
15350 {
15351 if (is_template)
15352 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15353 tree r = build_qualified_name (NULL_TREE, scope, expr,
15354 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15355 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15356 return r;
15357 }
15358
15359 if (!BASELINK_P (name) && !DECL_P (expr))
15360 {
15361 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15362 {
15363 /* A BIT_NOT_EXPR is used to represent a destructor. */
15364 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15365 {
15366 error ("qualifying type %qT does not match destructor name ~%qT",
15367 scope, TREE_OPERAND (expr, 0));
15368 expr = error_mark_node;
15369 }
15370 else
15371 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15372 /*is_type_p=*/0, false);
15373 }
15374 else
15375 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15376 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15377 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15378 {
15379 if (complain & tf_error)
15380 {
15381 error ("dependent-name %qE is parsed as a non-type, but "
15382 "instantiation yields a type", qualified_id);
15383 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15384 }
15385 return error_mark_node;
15386 }
15387 }
15388
15389 if (DECL_P (expr))
15390 {
15391 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15392 scope);
15393 /* Remember that there was a reference to this entity. */
15394 if (!mark_used (expr, complain) && !(complain & tf_error))
15395 return error_mark_node;
15396 }
15397
15398 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15399 {
15400 if (complain & tf_error)
15401 qualified_name_lookup_error (scope,
15402 TREE_OPERAND (qualified_id, 1),
15403 expr, input_location);
15404 return error_mark_node;
15405 }
15406
15407 if (is_template)
15408 {
15409 /* We may be repeating a check already done during parsing, but
15410 if it was well-formed and passed then, it will pass again
15411 now, and if it didn't, we wouldn't have got here. The case
15412 we want to catch is when we couldn't tell then, and can now,
15413 namely when templ prior to substitution was an
15414 identifier. */
15415 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15416 return error_mark_node;
15417
15418 if (variable_template_p (expr))
15419 expr = lookup_and_finish_template_variable (expr, template_args,
15420 complain);
15421 else
15422 expr = lookup_template_function (expr, template_args);
15423 }
15424
15425 if (expr == error_mark_node && complain & tf_error)
15426 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15427 expr, input_location);
15428 else if (TYPE_P (scope))
15429 {
15430 expr = (adjust_result_of_qualified_name_lookup
15431 (expr, scope, current_nonlambda_class_type ()));
15432 expr = (finish_qualified_id_expr
15433 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15434 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15435 /*template_arg_p=*/false, complain));
15436 }
15437
15438 /* Expressions do not generally have reference type. */
15439 if (TREE_CODE (expr) != SCOPE_REF
15440 /* However, if we're about to form a pointer-to-member, we just
15441 want the referenced member referenced. */
15442 && TREE_CODE (expr) != OFFSET_REF)
15443 expr = convert_from_reference (expr);
15444
15445 if (REF_PARENTHESIZED_P (qualified_id))
15446 expr = force_paren_expr (expr);
15447
15448 return expr;
15449 }
15450
15451 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15452 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15453 for tsubst. */
15454
15455 static tree
15456 tsubst_init (tree init, tree decl, tree args,
15457 tsubst_flags_t complain, tree in_decl)
15458 {
15459 if (!init)
15460 return NULL_TREE;
15461
15462 init = tsubst_expr (init, args, complain, in_decl, false);
15463
15464 tree type = TREE_TYPE (decl);
15465
15466 if (!init && type != error_mark_node)
15467 {
15468 if (tree auto_node = type_uses_auto (type))
15469 {
15470 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15471 {
15472 if (complain & tf_error)
15473 error ("initializer for %q#D expands to an empty list "
15474 "of expressions", decl);
15475 return error_mark_node;
15476 }
15477 }
15478 else if (!dependent_type_p (type))
15479 {
15480 /* If we had an initializer but it
15481 instantiated to nothing,
15482 value-initialize the object. This will
15483 only occur when the initializer was a
15484 pack expansion where the parameter packs
15485 used in that expansion were of length
15486 zero. */
15487 init = build_value_init (type, complain);
15488 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15489 init = get_target_expr_sfinae (init, complain);
15490 if (TREE_CODE (init) == TARGET_EXPR)
15491 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15492 }
15493 }
15494
15495 return init;
15496 }
15497
15498 /* Like tsubst, but deals with expressions. This function just replaces
15499 template parms; to finish processing the resultant expression, use
15500 tsubst_copy_and_build or tsubst_expr. */
15501
15502 static tree
15503 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15504 {
15505 enum tree_code code;
15506 tree r;
15507
15508 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15509 return t;
15510
15511 code = TREE_CODE (t);
15512
15513 switch (code)
15514 {
15515 case PARM_DECL:
15516 r = retrieve_local_specialization (t);
15517
15518 if (r == NULL_TREE)
15519 {
15520 /* We get here for a use of 'this' in an NSDMI. */
15521 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15522 return current_class_ptr;
15523
15524 /* This can happen for a parameter name used later in a function
15525 declaration (such as in a late-specified return type). Just
15526 make a dummy decl, since it's only used for its type. */
15527 gcc_assert (cp_unevaluated_operand != 0);
15528 r = tsubst_decl (t, args, complain);
15529 /* Give it the template pattern as its context; its true context
15530 hasn't been instantiated yet and this is good enough for
15531 mangling. */
15532 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15533 }
15534
15535 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15536 r = argument_pack_select_arg (r);
15537 if (!mark_used (r, complain) && !(complain & tf_error))
15538 return error_mark_node;
15539 return r;
15540
15541 case CONST_DECL:
15542 {
15543 tree enum_type;
15544 tree v;
15545
15546 if (DECL_TEMPLATE_PARM_P (t))
15547 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15548 /* There is no need to substitute into namespace-scope
15549 enumerators. */
15550 if (DECL_NAMESPACE_SCOPE_P (t))
15551 return t;
15552 /* If ARGS is NULL, then T is known to be non-dependent. */
15553 if (args == NULL_TREE)
15554 return scalar_constant_value (t);
15555
15556 /* Unfortunately, we cannot just call lookup_name here.
15557 Consider:
15558
15559 template <int I> int f() {
15560 enum E { a = I };
15561 struct S { void g() { E e = a; } };
15562 };
15563
15564 When we instantiate f<7>::S::g(), say, lookup_name is not
15565 clever enough to find f<7>::a. */
15566 enum_type
15567 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15568 /*entering_scope=*/0);
15569
15570 for (v = TYPE_VALUES (enum_type);
15571 v != NULL_TREE;
15572 v = TREE_CHAIN (v))
15573 if (TREE_PURPOSE (v) == DECL_NAME (t))
15574 return TREE_VALUE (v);
15575
15576 /* We didn't find the name. That should never happen; if
15577 name-lookup found it during preliminary parsing, we
15578 should find it again here during instantiation. */
15579 gcc_unreachable ();
15580 }
15581 return t;
15582
15583 case FIELD_DECL:
15584 if (DECL_CONTEXT (t))
15585 {
15586 tree ctx;
15587
15588 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15589 /*entering_scope=*/1);
15590 if (ctx != DECL_CONTEXT (t))
15591 {
15592 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15593 if (!r)
15594 {
15595 if (complain & tf_error)
15596 error ("using invalid field %qD", t);
15597 return error_mark_node;
15598 }
15599 return r;
15600 }
15601 }
15602
15603 return t;
15604
15605 case VAR_DECL:
15606 case FUNCTION_DECL:
15607 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15608 r = tsubst (t, args, complain, in_decl);
15609 else if (local_variable_p (t)
15610 && uses_template_parms (DECL_CONTEXT (t)))
15611 {
15612 r = retrieve_local_specialization (t);
15613 if (r == NULL_TREE)
15614 {
15615 /* First try name lookup to find the instantiation. */
15616 r = lookup_name (DECL_NAME (t));
15617 if (r)
15618 {
15619 if (!VAR_P (r))
15620 {
15621 /* During error-recovery we may find a non-variable,
15622 even an OVERLOAD: just bail out and avoid ICEs and
15623 duplicate diagnostics (c++/62207). */
15624 gcc_assert (seen_error ());
15625 return error_mark_node;
15626 }
15627 if (!is_capture_proxy (r))
15628 {
15629 /* Make sure the one we found is the one we want. */
15630 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15631 if (ctx != DECL_CONTEXT (r))
15632 r = NULL_TREE;
15633 }
15634 }
15635
15636 if (r)
15637 /* OK */;
15638 else
15639 {
15640 /* This can happen for a variable used in a
15641 late-specified return type of a local lambda, or for a
15642 local static or constant. Building a new VAR_DECL
15643 should be OK in all those cases. */
15644 r = tsubst_decl (t, args, complain);
15645 if (local_specializations)
15646 /* Avoid infinite recursion (79640). */
15647 register_local_specialization (r, t);
15648 if (decl_maybe_constant_var_p (r))
15649 {
15650 /* We can't call cp_finish_decl, so handle the
15651 initializer by hand. */
15652 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15653 complain, in_decl);
15654 if (!processing_template_decl)
15655 init = maybe_constant_init (init);
15656 if (processing_template_decl
15657 ? potential_constant_expression (init)
15658 : reduced_constant_expression_p (init))
15659 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15660 = TREE_CONSTANT (r) = true;
15661 DECL_INITIAL (r) = init;
15662 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15663 TREE_TYPE (r)
15664 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15665 complain, adc_variable_type);
15666 }
15667 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15668 || decl_constant_var_p (r)
15669 || seen_error ());
15670 if (!processing_template_decl
15671 && !TREE_STATIC (r))
15672 r = process_outer_var_ref (r, complain);
15673 }
15674 /* Remember this for subsequent uses. */
15675 if (local_specializations)
15676 register_local_specialization (r, t);
15677 }
15678 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15679 r = argument_pack_select_arg (r);
15680 }
15681 else
15682 r = t;
15683 if (!mark_used (r, complain))
15684 return error_mark_node;
15685 return r;
15686
15687 case NAMESPACE_DECL:
15688 return t;
15689
15690 case OVERLOAD:
15691 return t;
15692
15693 case BASELINK:
15694 return tsubst_baselink (t, current_nonlambda_class_type (),
15695 args, complain, in_decl);
15696
15697 case TEMPLATE_DECL:
15698 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15699 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15700 args, complain, in_decl);
15701 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15702 return tsubst (t, args, complain, in_decl);
15703 else if (DECL_CLASS_SCOPE_P (t)
15704 && uses_template_parms (DECL_CONTEXT (t)))
15705 {
15706 /* Template template argument like the following example need
15707 special treatment:
15708
15709 template <template <class> class TT> struct C {};
15710 template <class T> struct D {
15711 template <class U> struct E {};
15712 C<E> c; // #1
15713 };
15714 D<int> d; // #2
15715
15716 We are processing the template argument `E' in #1 for
15717 the template instantiation #2. Originally, `E' is a
15718 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15719 have to substitute this with one having context `D<int>'. */
15720
15721 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15722 if (dependent_scope_p (context))
15723 {
15724 /* When rewriting a constructor into a deduction guide, a
15725 non-dependent name can become dependent, so memtmpl<args>
15726 becomes context::template memtmpl<args>. */
15727 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15728 return build_qualified_name (type, context, DECL_NAME (t),
15729 /*template*/true);
15730 }
15731 return lookup_field (context, DECL_NAME(t), 0, false);
15732 }
15733 else
15734 /* Ordinary template template argument. */
15735 return t;
15736
15737 case NON_LVALUE_EXPR:
15738 case VIEW_CONVERT_EXPR:
15739 {
15740 /* Handle location wrappers by substituting the wrapped node
15741 first, *then* reusing the resulting type. Doing the type
15742 first ensures that we handle template parameters and
15743 parameter pack expansions. */
15744 if (location_wrapper_p (t))
15745 {
15746 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15747 complain, in_decl);
15748 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15749 }
15750 tree op = TREE_OPERAND (t, 0);
15751 if (code == VIEW_CONVERT_EXPR
15752 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15753 {
15754 /* Wrapper to make a C++20 template parameter object const. */
15755 op = tsubst_copy (op, args, complain, in_decl);
15756 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15757 {
15758 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15759 return build1 (code, type, op);
15760 }
15761 else
15762 {
15763 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15764 return op;
15765 }
15766 }
15767 /* We shouldn't see any other uses of these in templates. */
15768 gcc_unreachable ();
15769 }
15770
15771 case CAST_EXPR:
15772 case REINTERPRET_CAST_EXPR:
15773 case CONST_CAST_EXPR:
15774 case STATIC_CAST_EXPR:
15775 case DYNAMIC_CAST_EXPR:
15776 case IMPLICIT_CONV_EXPR:
15777 case CONVERT_EXPR:
15778 case NOP_EXPR:
15779 {
15780 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15781 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15782 return build1 (code, type, op0);
15783 }
15784
15785 case SIZEOF_EXPR:
15786 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15787 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15788 {
15789 tree expanded, op = TREE_OPERAND (t, 0);
15790 int len = 0;
15791
15792 if (SIZEOF_EXPR_TYPE_P (t))
15793 op = TREE_TYPE (op);
15794
15795 ++cp_unevaluated_operand;
15796 ++c_inhibit_evaluation_warnings;
15797 /* We only want to compute the number of arguments. */
15798 if (PACK_EXPANSION_P (op))
15799 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15800 else
15801 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15802 args, complain, in_decl);
15803 --cp_unevaluated_operand;
15804 --c_inhibit_evaluation_warnings;
15805
15806 if (TREE_CODE (expanded) == TREE_VEC)
15807 {
15808 len = TREE_VEC_LENGTH (expanded);
15809 /* Set TREE_USED for the benefit of -Wunused. */
15810 for (int i = 0; i < len; i++)
15811 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15812 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15813 }
15814
15815 if (expanded == error_mark_node)
15816 return error_mark_node;
15817 else if (PACK_EXPANSION_P (expanded)
15818 || (TREE_CODE (expanded) == TREE_VEC
15819 && pack_expansion_args_count (expanded)))
15820
15821 {
15822 if (PACK_EXPANSION_P (expanded))
15823 /* OK. */;
15824 else if (TREE_VEC_LENGTH (expanded) == 1)
15825 expanded = TREE_VEC_ELT (expanded, 0);
15826 else
15827 expanded = make_argument_pack (expanded);
15828
15829 if (TYPE_P (expanded))
15830 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15831 false,
15832 complain & tf_error);
15833 else
15834 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15835 complain & tf_error);
15836 }
15837 else
15838 return build_int_cst (size_type_node, len);
15839 }
15840 if (SIZEOF_EXPR_TYPE_P (t))
15841 {
15842 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15843 args, complain, in_decl);
15844 r = build1 (NOP_EXPR, r, error_mark_node);
15845 r = build1 (SIZEOF_EXPR,
15846 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15847 SIZEOF_EXPR_TYPE_P (r) = 1;
15848 return r;
15849 }
15850 /* Fall through */
15851
15852 case INDIRECT_REF:
15853 case NEGATE_EXPR:
15854 case TRUTH_NOT_EXPR:
15855 case BIT_NOT_EXPR:
15856 case ADDR_EXPR:
15857 case UNARY_PLUS_EXPR: /* Unary + */
15858 case ALIGNOF_EXPR:
15859 case AT_ENCODE_EXPR:
15860 case ARROW_EXPR:
15861 case THROW_EXPR:
15862 case TYPEID_EXPR:
15863 case REALPART_EXPR:
15864 case IMAGPART_EXPR:
15865 case PAREN_EXPR:
15866 {
15867 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15868 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15869 r = build1 (code, type, op0);
15870 if (code == ALIGNOF_EXPR)
15871 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15872 return r;
15873 }
15874
15875 case COMPONENT_REF:
15876 {
15877 tree object;
15878 tree name;
15879
15880 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15881 name = TREE_OPERAND (t, 1);
15882 if (TREE_CODE (name) == BIT_NOT_EXPR)
15883 {
15884 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15885 complain, in_decl);
15886 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15887 }
15888 else if (TREE_CODE (name) == SCOPE_REF
15889 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15890 {
15891 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15892 complain, in_decl);
15893 name = TREE_OPERAND (name, 1);
15894 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15895 complain, in_decl);
15896 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15897 name = build_qualified_name (/*type=*/NULL_TREE,
15898 base, name,
15899 /*template_p=*/false);
15900 }
15901 else if (BASELINK_P (name))
15902 name = tsubst_baselink (name,
15903 non_reference (TREE_TYPE (object)),
15904 args, complain,
15905 in_decl);
15906 else
15907 name = tsubst_copy (name, args, complain, in_decl);
15908 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15909 }
15910
15911 case PLUS_EXPR:
15912 case MINUS_EXPR:
15913 case MULT_EXPR:
15914 case TRUNC_DIV_EXPR:
15915 case CEIL_DIV_EXPR:
15916 case FLOOR_DIV_EXPR:
15917 case ROUND_DIV_EXPR:
15918 case EXACT_DIV_EXPR:
15919 case BIT_AND_EXPR:
15920 case BIT_IOR_EXPR:
15921 case BIT_XOR_EXPR:
15922 case TRUNC_MOD_EXPR:
15923 case FLOOR_MOD_EXPR:
15924 case TRUTH_ANDIF_EXPR:
15925 case TRUTH_ORIF_EXPR:
15926 case TRUTH_AND_EXPR:
15927 case TRUTH_OR_EXPR:
15928 case RSHIFT_EXPR:
15929 case LSHIFT_EXPR:
15930 case RROTATE_EXPR:
15931 case LROTATE_EXPR:
15932 case EQ_EXPR:
15933 case NE_EXPR:
15934 case MAX_EXPR:
15935 case MIN_EXPR:
15936 case LE_EXPR:
15937 case GE_EXPR:
15938 case LT_EXPR:
15939 case GT_EXPR:
15940 case COMPOUND_EXPR:
15941 case DOTSTAR_EXPR:
15942 case MEMBER_REF:
15943 case PREDECREMENT_EXPR:
15944 case PREINCREMENT_EXPR:
15945 case POSTDECREMENT_EXPR:
15946 case POSTINCREMENT_EXPR:
15947 {
15948 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15949 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15950 return build_nt (code, op0, op1);
15951 }
15952
15953 case SCOPE_REF:
15954 {
15955 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15956 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15957 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15958 QUALIFIED_NAME_IS_TEMPLATE (t));
15959 }
15960
15961 case ARRAY_REF:
15962 {
15963 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15964 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15965 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15966 }
15967
15968 case CALL_EXPR:
15969 {
15970 int n = VL_EXP_OPERAND_LENGTH (t);
15971 tree result = build_vl_exp (CALL_EXPR, n);
15972 int i;
15973 for (i = 0; i < n; i++)
15974 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15975 complain, in_decl);
15976 return result;
15977 }
15978
15979 case COND_EXPR:
15980 case MODOP_EXPR:
15981 case PSEUDO_DTOR_EXPR:
15982 case VEC_PERM_EXPR:
15983 {
15984 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15985 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15986 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15987 r = build_nt (code, op0, op1, op2);
15988 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15989 return r;
15990 }
15991
15992 case NEW_EXPR:
15993 {
15994 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15995 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15996 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15997 r = build_nt (code, op0, op1, op2);
15998 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15999 return r;
16000 }
16001
16002 case DELETE_EXPR:
16003 {
16004 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16005 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16006 r = build_nt (code, op0, op1);
16007 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16008 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16009 return r;
16010 }
16011
16012 case TEMPLATE_ID_EXPR:
16013 {
16014 /* Substituted template arguments */
16015 tree fn = TREE_OPERAND (t, 0);
16016 tree targs = TREE_OPERAND (t, 1);
16017
16018 fn = tsubst_copy (fn, args, complain, in_decl);
16019 if (targs)
16020 targs = tsubst_template_args (targs, args, complain, in_decl);
16021
16022 return lookup_template_function (fn, targs);
16023 }
16024
16025 case TREE_LIST:
16026 {
16027 tree purpose, value, chain;
16028
16029 if (t == void_list_node)
16030 return t;
16031
16032 purpose = TREE_PURPOSE (t);
16033 if (purpose)
16034 purpose = tsubst_copy (purpose, args, complain, in_decl);
16035 value = TREE_VALUE (t);
16036 if (value)
16037 value = tsubst_copy (value, args, complain, in_decl);
16038 chain = TREE_CHAIN (t);
16039 if (chain && chain != void_type_node)
16040 chain = tsubst_copy (chain, args, complain, in_decl);
16041 if (purpose == TREE_PURPOSE (t)
16042 && value == TREE_VALUE (t)
16043 && chain == TREE_CHAIN (t))
16044 return t;
16045 return tree_cons (purpose, value, chain);
16046 }
16047
16048 case RECORD_TYPE:
16049 case UNION_TYPE:
16050 case ENUMERAL_TYPE:
16051 case INTEGER_TYPE:
16052 case TEMPLATE_TYPE_PARM:
16053 case TEMPLATE_TEMPLATE_PARM:
16054 case BOUND_TEMPLATE_TEMPLATE_PARM:
16055 case TEMPLATE_PARM_INDEX:
16056 case POINTER_TYPE:
16057 case REFERENCE_TYPE:
16058 case OFFSET_TYPE:
16059 case FUNCTION_TYPE:
16060 case METHOD_TYPE:
16061 case ARRAY_TYPE:
16062 case TYPENAME_TYPE:
16063 case UNBOUND_CLASS_TEMPLATE:
16064 case TYPEOF_TYPE:
16065 case DECLTYPE_TYPE:
16066 case TYPE_DECL:
16067 return tsubst (t, args, complain, in_decl);
16068
16069 case USING_DECL:
16070 t = DECL_NAME (t);
16071 /* Fall through. */
16072 case IDENTIFIER_NODE:
16073 if (IDENTIFIER_CONV_OP_P (t))
16074 {
16075 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16076 return make_conv_op_name (new_type);
16077 }
16078 else
16079 return t;
16080
16081 case CONSTRUCTOR:
16082 /* This is handled by tsubst_copy_and_build. */
16083 gcc_unreachable ();
16084
16085 case VA_ARG_EXPR:
16086 {
16087 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16088 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16089 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16090 }
16091
16092 case CLEANUP_POINT_EXPR:
16093 /* We shouldn't have built any of these during initial template
16094 generation. Instead, they should be built during instantiation
16095 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16096 gcc_unreachable ();
16097
16098 case OFFSET_REF:
16099 {
16100 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16101 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16102 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16103 r = build2 (code, type, op0, op1);
16104 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16105 if (!mark_used (TREE_OPERAND (r, 1), complain)
16106 && !(complain & tf_error))
16107 return error_mark_node;
16108 return r;
16109 }
16110
16111 case EXPR_PACK_EXPANSION:
16112 error ("invalid use of pack expansion expression");
16113 return error_mark_node;
16114
16115 case NONTYPE_ARGUMENT_PACK:
16116 error ("use %<...%> to expand argument pack");
16117 return error_mark_node;
16118
16119 case VOID_CST:
16120 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16121 return t;
16122
16123 case INTEGER_CST:
16124 case REAL_CST:
16125 case STRING_CST:
16126 case COMPLEX_CST:
16127 {
16128 /* Instantiate any typedefs in the type. */
16129 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16130 r = fold_convert (type, t);
16131 gcc_assert (TREE_CODE (r) == code);
16132 return r;
16133 }
16134
16135 case PTRMEM_CST:
16136 /* These can sometimes show up in a partial instantiation, but never
16137 involve template parms. */
16138 gcc_assert (!uses_template_parms (t));
16139 return t;
16140
16141 case UNARY_LEFT_FOLD_EXPR:
16142 return tsubst_unary_left_fold (t, args, complain, in_decl);
16143 case UNARY_RIGHT_FOLD_EXPR:
16144 return tsubst_unary_right_fold (t, args, complain, in_decl);
16145 case BINARY_LEFT_FOLD_EXPR:
16146 return tsubst_binary_left_fold (t, args, complain, in_decl);
16147 case BINARY_RIGHT_FOLD_EXPR:
16148 return tsubst_binary_right_fold (t, args, complain, in_decl);
16149 case PREDICT_EXPR:
16150 return t;
16151
16152 case DEBUG_BEGIN_STMT:
16153 /* ??? There's no point in copying it for now, but maybe some
16154 day it will contain more information, such as a pointer back
16155 to the containing function, inlined copy or so. */
16156 return t;
16157
16158 default:
16159 /* We shouldn't get here, but keep going if !flag_checking. */
16160 if (flag_checking)
16161 gcc_unreachable ();
16162 return t;
16163 }
16164 }
16165
16166 /* Helper function for tsubst_omp_clauses, used for instantiation of
16167 OMP_CLAUSE_DECL of clauses. */
16168
16169 static tree
16170 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16171 tree in_decl, tree *iterator_cache)
16172 {
16173 if (decl == NULL_TREE)
16174 return NULL_TREE;
16175
16176 /* Handle OpenMP iterators. */
16177 if (TREE_CODE (decl) == TREE_LIST
16178 && TREE_PURPOSE (decl)
16179 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16180 {
16181 tree ret;
16182 if (iterator_cache[0] == TREE_PURPOSE (decl))
16183 ret = iterator_cache[1];
16184 else
16185 {
16186 tree *tp = &ret;
16187 begin_scope (sk_omp, NULL);
16188 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16189 {
16190 *tp = copy_node (it);
16191 TREE_VEC_ELT (*tp, 0)
16192 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16193 TREE_VEC_ELT (*tp, 1)
16194 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16195 /*integral_constant_expression_p=*/false);
16196 TREE_VEC_ELT (*tp, 2)
16197 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16198 /*integral_constant_expression_p=*/false);
16199 TREE_VEC_ELT (*tp, 3)
16200 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16201 /*integral_constant_expression_p=*/false);
16202 TREE_CHAIN (*tp) = NULL_TREE;
16203 tp = &TREE_CHAIN (*tp);
16204 }
16205 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16206 iterator_cache[0] = TREE_PURPOSE (decl);
16207 iterator_cache[1] = ret;
16208 }
16209 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16210 args, complain,
16211 in_decl, NULL));
16212 }
16213
16214 /* Handle an OpenMP array section represented as a TREE_LIST (or
16215 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16216 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16217 TREE_LIST. We can handle it exactly the same as an array section
16218 (purpose, value, and a chain), even though the nomenclature
16219 (low_bound, length, etc) is different. */
16220 if (TREE_CODE (decl) == TREE_LIST)
16221 {
16222 tree low_bound
16223 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16224 /*integral_constant_expression_p=*/false);
16225 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16226 /*integral_constant_expression_p=*/false);
16227 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16228 in_decl, NULL);
16229 if (TREE_PURPOSE (decl) == low_bound
16230 && TREE_VALUE (decl) == length
16231 && TREE_CHAIN (decl) == chain)
16232 return decl;
16233 tree ret = tree_cons (low_bound, length, chain);
16234 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16235 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16236 return ret;
16237 }
16238 tree ret = tsubst_expr (decl, args, complain, in_decl,
16239 /*integral_constant_expression_p=*/false);
16240 /* Undo convert_from_reference tsubst_expr could have called. */
16241 if (decl
16242 && REFERENCE_REF_P (ret)
16243 && !REFERENCE_REF_P (decl))
16244 ret = TREE_OPERAND (ret, 0);
16245 return ret;
16246 }
16247
16248 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16249
16250 static tree
16251 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16252 tree args, tsubst_flags_t complain, tree in_decl)
16253 {
16254 tree new_clauses = NULL_TREE, nc, oc;
16255 tree linear_no_step = NULL_TREE;
16256 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16257
16258 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16259 {
16260 nc = copy_node (oc);
16261 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16262 new_clauses = nc;
16263
16264 switch (OMP_CLAUSE_CODE (nc))
16265 {
16266 case OMP_CLAUSE_LASTPRIVATE:
16267 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16268 {
16269 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16270 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16271 in_decl, /*integral_constant_expression_p=*/false);
16272 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16273 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16274 }
16275 /* FALLTHRU */
16276 case OMP_CLAUSE_PRIVATE:
16277 case OMP_CLAUSE_SHARED:
16278 case OMP_CLAUSE_FIRSTPRIVATE:
16279 case OMP_CLAUSE_COPYIN:
16280 case OMP_CLAUSE_COPYPRIVATE:
16281 case OMP_CLAUSE_UNIFORM:
16282 case OMP_CLAUSE_DEPEND:
16283 case OMP_CLAUSE_FROM:
16284 case OMP_CLAUSE_TO:
16285 case OMP_CLAUSE_MAP:
16286 case OMP_CLAUSE_NONTEMPORAL:
16287 case OMP_CLAUSE_USE_DEVICE_PTR:
16288 case OMP_CLAUSE_IS_DEVICE_PTR:
16289 OMP_CLAUSE_DECL (nc)
16290 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16291 in_decl, iterator_cache);
16292 break;
16293 case OMP_CLAUSE_TILE:
16294 case OMP_CLAUSE_IF:
16295 case OMP_CLAUSE_NUM_THREADS:
16296 case OMP_CLAUSE_SCHEDULE:
16297 case OMP_CLAUSE_COLLAPSE:
16298 case OMP_CLAUSE_FINAL:
16299 case OMP_CLAUSE_DEVICE:
16300 case OMP_CLAUSE_DIST_SCHEDULE:
16301 case OMP_CLAUSE_NUM_TEAMS:
16302 case OMP_CLAUSE_THREAD_LIMIT:
16303 case OMP_CLAUSE_SAFELEN:
16304 case OMP_CLAUSE_SIMDLEN:
16305 case OMP_CLAUSE_NUM_TASKS:
16306 case OMP_CLAUSE_GRAINSIZE:
16307 case OMP_CLAUSE_PRIORITY:
16308 case OMP_CLAUSE_ORDERED:
16309 case OMP_CLAUSE_HINT:
16310 case OMP_CLAUSE_NUM_GANGS:
16311 case OMP_CLAUSE_NUM_WORKERS:
16312 case OMP_CLAUSE_VECTOR_LENGTH:
16313 case OMP_CLAUSE_WORKER:
16314 case OMP_CLAUSE_VECTOR:
16315 case OMP_CLAUSE_ASYNC:
16316 case OMP_CLAUSE_WAIT:
16317 OMP_CLAUSE_OPERAND (nc, 0)
16318 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16319 in_decl, /*integral_constant_expression_p=*/false);
16320 break;
16321 case OMP_CLAUSE_REDUCTION:
16322 case OMP_CLAUSE_IN_REDUCTION:
16323 case OMP_CLAUSE_TASK_REDUCTION:
16324 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16325 {
16326 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16327 if (TREE_CODE (placeholder) == SCOPE_REF)
16328 {
16329 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16330 complain, in_decl);
16331 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16332 = build_qualified_name (NULL_TREE, scope,
16333 TREE_OPERAND (placeholder, 1),
16334 false);
16335 }
16336 else
16337 gcc_assert (identifier_p (placeholder));
16338 }
16339 OMP_CLAUSE_DECL (nc)
16340 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16341 in_decl, NULL);
16342 break;
16343 case OMP_CLAUSE_GANG:
16344 case OMP_CLAUSE_ALIGNED:
16345 OMP_CLAUSE_DECL (nc)
16346 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16347 in_decl, NULL);
16348 OMP_CLAUSE_OPERAND (nc, 1)
16349 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16350 in_decl, /*integral_constant_expression_p=*/false);
16351 break;
16352 case OMP_CLAUSE_LINEAR:
16353 OMP_CLAUSE_DECL (nc)
16354 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16355 in_decl, NULL);
16356 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16357 {
16358 gcc_assert (!linear_no_step);
16359 linear_no_step = nc;
16360 }
16361 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16362 OMP_CLAUSE_LINEAR_STEP (nc)
16363 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16364 complain, in_decl, NULL);
16365 else
16366 OMP_CLAUSE_LINEAR_STEP (nc)
16367 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16368 in_decl,
16369 /*integral_constant_expression_p=*/false);
16370 break;
16371 case OMP_CLAUSE_NOWAIT:
16372 case OMP_CLAUSE_DEFAULT:
16373 case OMP_CLAUSE_UNTIED:
16374 case OMP_CLAUSE_MERGEABLE:
16375 case OMP_CLAUSE_INBRANCH:
16376 case OMP_CLAUSE_NOTINBRANCH:
16377 case OMP_CLAUSE_PROC_BIND:
16378 case OMP_CLAUSE_FOR:
16379 case OMP_CLAUSE_PARALLEL:
16380 case OMP_CLAUSE_SECTIONS:
16381 case OMP_CLAUSE_TASKGROUP:
16382 case OMP_CLAUSE_NOGROUP:
16383 case OMP_CLAUSE_THREADS:
16384 case OMP_CLAUSE_SIMD:
16385 case OMP_CLAUSE_DEFAULTMAP:
16386 case OMP_CLAUSE_INDEPENDENT:
16387 case OMP_CLAUSE_AUTO:
16388 case OMP_CLAUSE_SEQ:
16389 case OMP_CLAUSE_IF_PRESENT:
16390 case OMP_CLAUSE_FINALIZE:
16391 break;
16392 default:
16393 gcc_unreachable ();
16394 }
16395 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16396 switch (OMP_CLAUSE_CODE (nc))
16397 {
16398 case OMP_CLAUSE_SHARED:
16399 case OMP_CLAUSE_PRIVATE:
16400 case OMP_CLAUSE_FIRSTPRIVATE:
16401 case OMP_CLAUSE_LASTPRIVATE:
16402 case OMP_CLAUSE_COPYPRIVATE:
16403 case OMP_CLAUSE_LINEAR:
16404 case OMP_CLAUSE_REDUCTION:
16405 case OMP_CLAUSE_IN_REDUCTION:
16406 case OMP_CLAUSE_TASK_REDUCTION:
16407 case OMP_CLAUSE_USE_DEVICE_PTR:
16408 case OMP_CLAUSE_IS_DEVICE_PTR:
16409 /* tsubst_expr on SCOPE_REF results in returning
16410 finish_non_static_data_member result. Undo that here. */
16411 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16412 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16413 == IDENTIFIER_NODE))
16414 {
16415 tree t = OMP_CLAUSE_DECL (nc);
16416 tree v = t;
16417 while (v)
16418 switch (TREE_CODE (v))
16419 {
16420 case COMPONENT_REF:
16421 case MEM_REF:
16422 case INDIRECT_REF:
16423 CASE_CONVERT:
16424 case POINTER_PLUS_EXPR:
16425 v = TREE_OPERAND (v, 0);
16426 continue;
16427 case PARM_DECL:
16428 if (DECL_CONTEXT (v) == current_function_decl
16429 && DECL_ARTIFICIAL (v)
16430 && DECL_NAME (v) == this_identifier)
16431 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16432 /* FALLTHRU */
16433 default:
16434 v = NULL_TREE;
16435 break;
16436 }
16437 }
16438 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16439 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16440 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16441 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16442 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16443 {
16444 tree decl = OMP_CLAUSE_DECL (nc);
16445 if (VAR_P (decl))
16446 {
16447 retrofit_lang_decl (decl);
16448 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16449 }
16450 }
16451 break;
16452 default:
16453 break;
16454 }
16455 }
16456
16457 new_clauses = nreverse (new_clauses);
16458 if (ort != C_ORT_OMP_DECLARE_SIMD)
16459 {
16460 new_clauses = finish_omp_clauses (new_clauses, ort);
16461 if (linear_no_step)
16462 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16463 if (nc == linear_no_step)
16464 {
16465 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16466 break;
16467 }
16468 }
16469 return new_clauses;
16470 }
16471
16472 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16473
16474 static tree
16475 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16476 tree in_decl)
16477 {
16478 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16479
16480 tree purpose, value, chain;
16481
16482 if (t == NULL)
16483 return t;
16484
16485 if (TREE_CODE (t) != TREE_LIST)
16486 return tsubst_copy_and_build (t, args, complain, in_decl,
16487 /*function_p=*/false,
16488 /*integral_constant_expression_p=*/false);
16489
16490 if (t == void_list_node)
16491 return t;
16492
16493 purpose = TREE_PURPOSE (t);
16494 if (purpose)
16495 purpose = RECUR (purpose);
16496 value = TREE_VALUE (t);
16497 if (value)
16498 {
16499 if (TREE_CODE (value) != LABEL_DECL)
16500 value = RECUR (value);
16501 else
16502 {
16503 value = lookup_label (DECL_NAME (value));
16504 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16505 TREE_USED (value) = 1;
16506 }
16507 }
16508 chain = TREE_CHAIN (t);
16509 if (chain && chain != void_type_node)
16510 chain = RECUR (chain);
16511 return tree_cons (purpose, value, chain);
16512 #undef RECUR
16513 }
16514
16515 /* Used to temporarily communicate the list of #pragma omp parallel
16516 clauses to #pragma omp for instantiation if they are combined
16517 together. */
16518
16519 static tree *omp_parallel_combined_clauses;
16520
16521 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
16522 tree *, unsigned int *);
16523
16524 /* Substitute one OMP_FOR iterator. */
16525
16526 static bool
16527 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
16528 tree initv, tree condv, tree incrv, tree *clauses,
16529 tree args, tsubst_flags_t complain, tree in_decl,
16530 bool integral_constant_expression_p)
16531 {
16532 #define RECUR(NODE) \
16533 tsubst_expr ((NODE), args, complain, in_decl, \
16534 integral_constant_expression_p)
16535 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
16536 bool ret = false;
16537
16538 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16539 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16540
16541 decl = TREE_OPERAND (init, 0);
16542 init = TREE_OPERAND (init, 1);
16543 tree decl_expr = NULL_TREE;
16544 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
16545 if (range_for)
16546 {
16547 bool decomp = false;
16548 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
16549 {
16550 tree v = DECL_VALUE_EXPR (decl);
16551 if (TREE_CODE (v) == ARRAY_REF
16552 && VAR_P (TREE_OPERAND (v, 0))
16553 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
16554 {
16555 tree decomp_first = NULL_TREE;
16556 unsigned decomp_cnt = 0;
16557 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
16558 maybe_push_decl (d);
16559 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
16560 in_decl, &decomp_first, &decomp_cnt);
16561 decomp = true;
16562 if (d == error_mark_node)
16563 decl = error_mark_node;
16564 else
16565 for (unsigned int i = 0; i < decomp_cnt; i++)
16566 {
16567 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
16568 {
16569 tree v = build_nt (ARRAY_REF, d,
16570 size_int (decomp_cnt - i - 1),
16571 NULL_TREE, NULL_TREE);
16572 SET_DECL_VALUE_EXPR (decomp_first, v);
16573 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
16574 }
16575 fit_decomposition_lang_decl (decomp_first, d);
16576 decomp_first = DECL_CHAIN (decomp_first);
16577 }
16578 }
16579 }
16580 decl = tsubst_decl (decl, args, complain);
16581 if (!decomp)
16582 maybe_push_decl (decl);
16583 }
16584 else if (init && TREE_CODE (init) == DECL_EXPR)
16585 {
16586 /* We need to jump through some hoops to handle declarations in the
16587 init-statement, since we might need to handle auto deduction,
16588 but we need to keep control of initialization. */
16589 decl_expr = init;
16590 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16591 decl = tsubst_decl (decl, args, complain);
16592 }
16593 else
16594 {
16595 if (TREE_CODE (decl) == SCOPE_REF)
16596 {
16597 decl = RECUR (decl);
16598 if (TREE_CODE (decl) == COMPONENT_REF)
16599 {
16600 tree v = decl;
16601 while (v)
16602 switch (TREE_CODE (v))
16603 {
16604 case COMPONENT_REF:
16605 case MEM_REF:
16606 case INDIRECT_REF:
16607 CASE_CONVERT:
16608 case POINTER_PLUS_EXPR:
16609 v = TREE_OPERAND (v, 0);
16610 continue;
16611 case PARM_DECL:
16612 if (DECL_CONTEXT (v) == current_function_decl
16613 && DECL_ARTIFICIAL (v)
16614 && DECL_NAME (v) == this_identifier)
16615 {
16616 decl = TREE_OPERAND (decl, 1);
16617 decl = omp_privatize_field (decl, false);
16618 }
16619 /* FALLTHRU */
16620 default:
16621 v = NULL_TREE;
16622 break;
16623 }
16624 }
16625 }
16626 else
16627 decl = RECUR (decl);
16628 }
16629 init = RECUR (init);
16630
16631 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16632 {
16633 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16634 if (TREE_CODE (o) == TREE_LIST)
16635 TREE_VEC_ELT (orig_declv, i)
16636 = tree_cons (RECUR (TREE_PURPOSE (o)),
16637 RECUR (TREE_VALUE (o)),
16638 NULL_TREE);
16639 else
16640 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16641 }
16642
16643 if (range_for)
16644 {
16645 tree this_pre_body = NULL_TREE;
16646 tree orig_init = NULL_TREE;
16647 tree orig_decl = NULL_TREE;
16648 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
16649 orig_init, cond, incr);
16650 if (orig_decl)
16651 {
16652 if (orig_declv == NULL_TREE)
16653 orig_declv = copy_node (declv);
16654 TREE_VEC_ELT (orig_declv, i) = orig_decl;
16655 ret = true;
16656 }
16657 else if (orig_declv)
16658 TREE_VEC_ELT (orig_declv, i) = decl;
16659 }
16660
16661 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16662 if (!range_for && auto_node && init)
16663 TREE_TYPE (decl)
16664 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16665
16666 gcc_assert (!type_dependent_expression_p (decl));
16667
16668 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
16669 {
16670 if (decl_expr)
16671 {
16672 /* Declare the variable, but don't let that initialize it. */
16673 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16674 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16675 RECUR (decl_expr);
16676 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16677 }
16678
16679 if (!range_for)
16680 {
16681 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16682 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16683 if (TREE_CODE (incr) == MODIFY_EXPR)
16684 {
16685 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16686 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16687 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16688 NOP_EXPR, rhs, complain);
16689 }
16690 else
16691 incr = RECUR (incr);
16692 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16693 TREE_VEC_ELT (orig_declv, i) = decl;
16694 }
16695 TREE_VEC_ELT (declv, i) = decl;
16696 TREE_VEC_ELT (initv, i) = init;
16697 TREE_VEC_ELT (condv, i) = cond;
16698 TREE_VEC_ELT (incrv, i) = incr;
16699 return ret;
16700 }
16701
16702 if (decl_expr)
16703 {
16704 /* Declare and initialize the variable. */
16705 RECUR (decl_expr);
16706 init = NULL_TREE;
16707 }
16708 else if (init)
16709 {
16710 tree *pc;
16711 int j;
16712 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16713 {
16714 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16715 {
16716 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16717 && OMP_CLAUSE_DECL (*pc) == decl)
16718 break;
16719 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16720 && OMP_CLAUSE_DECL (*pc) == decl)
16721 {
16722 if (j)
16723 break;
16724 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16725 tree c = *pc;
16726 *pc = OMP_CLAUSE_CHAIN (c);
16727 OMP_CLAUSE_CHAIN (c) = *clauses;
16728 *clauses = c;
16729 }
16730 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16731 && OMP_CLAUSE_DECL (*pc) == decl)
16732 {
16733 error ("iteration variable %qD should not be firstprivate",
16734 decl);
16735 *pc = OMP_CLAUSE_CHAIN (*pc);
16736 }
16737 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16738 && OMP_CLAUSE_DECL (*pc) == decl)
16739 {
16740 error ("iteration variable %qD should not be reduction",
16741 decl);
16742 *pc = OMP_CLAUSE_CHAIN (*pc);
16743 }
16744 else
16745 pc = &OMP_CLAUSE_CHAIN (*pc);
16746 }
16747 if (*pc)
16748 break;
16749 }
16750 if (*pc == NULL_TREE)
16751 {
16752 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16753 OMP_CLAUSE_DECL (c) = decl;
16754 c = finish_omp_clauses (c, C_ORT_OMP);
16755 if (c)
16756 {
16757 OMP_CLAUSE_CHAIN (c) = *clauses;
16758 *clauses = c;
16759 }
16760 }
16761 }
16762 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16763 if (COMPARISON_CLASS_P (cond))
16764 {
16765 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16766 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16767 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16768 }
16769 else
16770 cond = RECUR (cond);
16771 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16772 switch (TREE_CODE (incr))
16773 {
16774 case PREINCREMENT_EXPR:
16775 case PREDECREMENT_EXPR:
16776 case POSTINCREMENT_EXPR:
16777 case POSTDECREMENT_EXPR:
16778 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16779 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16780 break;
16781 case MODIFY_EXPR:
16782 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16783 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16784 {
16785 tree rhs = TREE_OPERAND (incr, 1);
16786 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16787 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16788 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16789 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16790 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16791 rhs0, rhs1));
16792 }
16793 else
16794 incr = RECUR (incr);
16795 break;
16796 case MODOP_EXPR:
16797 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16798 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16799 {
16800 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16801 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16802 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16803 TREE_TYPE (decl), lhs,
16804 RECUR (TREE_OPERAND (incr, 2))));
16805 }
16806 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16807 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16808 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16809 {
16810 tree rhs = TREE_OPERAND (incr, 2);
16811 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16812 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16813 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16814 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16815 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16816 rhs0, rhs1));
16817 }
16818 else
16819 incr = RECUR (incr);
16820 break;
16821 default:
16822 incr = RECUR (incr);
16823 break;
16824 }
16825
16826 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16827 TREE_VEC_ELT (orig_declv, i) = decl;
16828 TREE_VEC_ELT (declv, i) = decl;
16829 TREE_VEC_ELT (initv, i) = init;
16830 TREE_VEC_ELT (condv, i) = cond;
16831 TREE_VEC_ELT (incrv, i) = incr;
16832 return false;
16833 #undef RECUR
16834 }
16835
16836 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16837 of OMP_TARGET's body. */
16838
16839 static tree
16840 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16841 {
16842 *walk_subtrees = 0;
16843 switch (TREE_CODE (*tp))
16844 {
16845 case OMP_TEAMS:
16846 return *tp;
16847 case BIND_EXPR:
16848 case STATEMENT_LIST:
16849 *walk_subtrees = 1;
16850 break;
16851 default:
16852 break;
16853 }
16854 return NULL_TREE;
16855 }
16856
16857 /* Helper function for tsubst_expr. For decomposition declaration
16858 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16859 also the corresponding decls representing the identifiers
16860 of the decomposition declaration. Return DECL if successful
16861 or error_mark_node otherwise, set *FIRST to the first decl
16862 in the list chained through DECL_CHAIN and *CNT to the number
16863 of such decls. */
16864
16865 static tree
16866 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16867 tsubst_flags_t complain, tree in_decl, tree *first,
16868 unsigned int *cnt)
16869 {
16870 tree decl2, decl3, prev = decl;
16871 *cnt = 0;
16872 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16873 for (decl2 = DECL_CHAIN (pattern_decl);
16874 decl2
16875 && VAR_P (decl2)
16876 && DECL_DECOMPOSITION_P (decl2)
16877 && DECL_NAME (decl2);
16878 decl2 = DECL_CHAIN (decl2))
16879 {
16880 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16881 {
16882 gcc_assert (errorcount);
16883 return error_mark_node;
16884 }
16885 (*cnt)++;
16886 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16887 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16888 tree v = DECL_VALUE_EXPR (decl2);
16889 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16890 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16891 decl3 = tsubst (decl2, args, complain, in_decl);
16892 SET_DECL_VALUE_EXPR (decl2, v);
16893 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16894 if (VAR_P (decl3))
16895 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16896 else
16897 {
16898 gcc_assert (errorcount);
16899 decl = error_mark_node;
16900 continue;
16901 }
16902 maybe_push_decl (decl3);
16903 if (error_operand_p (decl3))
16904 decl = error_mark_node;
16905 else if (decl != error_mark_node
16906 && DECL_CHAIN (decl3) != prev
16907 && decl != prev)
16908 {
16909 gcc_assert (errorcount);
16910 decl = error_mark_node;
16911 }
16912 else
16913 prev = decl3;
16914 }
16915 *first = prev;
16916 return decl;
16917 }
16918
16919 /* Return the proper local_specialization for init-capture pack DECL. */
16920
16921 static tree
16922 lookup_init_capture_pack (tree decl)
16923 {
16924 /* We handle normal pack captures by forwarding to the specialization of the
16925 captured parameter. We can't do that for pack init-captures; we need them
16926 to have their own local_specialization. We created the individual
16927 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16928 when we process the DECL_EXPR for the pack init-capture in the template.
16929 So, how do we find them? We don't know the capture proxy pack when
16930 building the individual resulting proxies, and we don't know the
16931 individual proxies when instantiating the pack. What we have in common is
16932 the FIELD_DECL.
16933
16934 So...when we instantiate the FIELD_DECL, we stick the result in
16935 local_specializations. Then at the DECL_EXPR we look up that result, see
16936 how many elements it has, synthesize the names, and look them up. */
16937
16938 tree cname = DECL_NAME (decl);
16939 tree val = DECL_VALUE_EXPR (decl);
16940 tree field = TREE_OPERAND (val, 1);
16941 gcc_assert (TREE_CODE (field) == FIELD_DECL);
16942 tree fpack = retrieve_local_specialization (field);
16943 if (fpack == error_mark_node)
16944 return error_mark_node;
16945
16946 int len = 1;
16947 tree vec = NULL_TREE;
16948 tree r = NULL_TREE;
16949 if (TREE_CODE (fpack) == TREE_VEC)
16950 {
16951 len = TREE_VEC_LENGTH (fpack);
16952 vec = make_tree_vec (len);
16953 r = make_node (NONTYPE_ARGUMENT_PACK);
16954 SET_ARGUMENT_PACK_ARGS (r, vec);
16955 }
16956 for (int i = 0; i < len; ++i)
16957 {
16958 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
16959 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
16960 if (vec)
16961 TREE_VEC_ELT (vec, i) = elt;
16962 else
16963 r = elt;
16964 }
16965 return r;
16966 }
16967
16968 /* Like tsubst_copy for expressions, etc. but also does semantic
16969 processing. */
16970
16971 tree
16972 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16973 bool integral_constant_expression_p)
16974 {
16975 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16976 #define RECUR(NODE) \
16977 tsubst_expr ((NODE), args, complain, in_decl, \
16978 integral_constant_expression_p)
16979
16980 tree stmt, tmp;
16981 tree r;
16982 location_t loc;
16983
16984 if (t == NULL_TREE || t == error_mark_node)
16985 return t;
16986
16987 loc = input_location;
16988 if (location_t eloc = cp_expr_location (t))
16989 input_location = eloc;
16990 if (STATEMENT_CODE_P (TREE_CODE (t)))
16991 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16992
16993 switch (TREE_CODE (t))
16994 {
16995 case STATEMENT_LIST:
16996 {
16997 tree_stmt_iterator i;
16998 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16999 RECUR (tsi_stmt (i));
17000 break;
17001 }
17002
17003 case CTOR_INITIALIZER:
17004 finish_mem_initializers (tsubst_initializer_list
17005 (TREE_OPERAND (t, 0), args));
17006 break;
17007
17008 case RETURN_EXPR:
17009 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17010 break;
17011
17012 case EXPR_STMT:
17013 tmp = RECUR (EXPR_STMT_EXPR (t));
17014 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17015 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17016 else
17017 finish_expr_stmt (tmp);
17018 break;
17019
17020 case USING_STMT:
17021 finish_local_using_directive (USING_STMT_NAMESPACE (t),
17022 /*attribs=*/NULL_TREE);
17023 break;
17024
17025 case DECL_EXPR:
17026 {
17027 tree decl, pattern_decl;
17028 tree init;
17029
17030 pattern_decl = decl = DECL_EXPR_DECL (t);
17031 if (TREE_CODE (decl) == LABEL_DECL)
17032 finish_label_decl (DECL_NAME (decl));
17033 else if (TREE_CODE (decl) == USING_DECL)
17034 {
17035 tree scope = USING_DECL_SCOPE (decl);
17036 tree name = DECL_NAME (decl);
17037
17038 scope = tsubst (scope, args, complain, in_decl);
17039 decl = lookup_qualified_name (scope, name,
17040 /*is_type_p=*/false,
17041 /*complain=*/false);
17042 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
17043 qualified_name_lookup_error (scope, name, decl, input_location);
17044 else
17045 finish_local_using_decl (decl, scope, name);
17046 }
17047 else if (is_capture_proxy (decl)
17048 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17049 {
17050 /* We're in tsubst_lambda_expr, we've already inserted a new
17051 capture proxy, so look it up and register it. */
17052 tree inst;
17053 if (!DECL_PACK_P (decl))
17054 {
17055 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17056 /*nonclass*/1, /*block_p=*/true,
17057 /*ns_only*/0, LOOKUP_HIDDEN);
17058 gcc_assert (inst != decl && is_capture_proxy (inst));
17059 }
17060 else if (is_normal_capture_proxy (decl))
17061 {
17062 inst = (retrieve_local_specialization
17063 (DECL_CAPTURED_VARIABLE (decl)));
17064 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17065 }
17066 else
17067 inst = lookup_init_capture_pack (decl);
17068
17069 register_local_specialization (inst, decl);
17070 break;
17071 }
17072 else if (DECL_PRETTY_FUNCTION_P (decl))
17073 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17074 DECL_NAME (decl),
17075 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17076 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17077 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17078 /* Don't copy the old closure; we'll create a new one in
17079 tsubst_lambda_expr. */
17080 break;
17081 else
17082 {
17083 init = DECL_INITIAL (decl);
17084 decl = tsubst (decl, args, complain, in_decl);
17085 if (decl != error_mark_node)
17086 {
17087 /* By marking the declaration as instantiated, we avoid
17088 trying to instantiate it. Since instantiate_decl can't
17089 handle local variables, and since we've already done
17090 all that needs to be done, that's the right thing to
17091 do. */
17092 if (VAR_P (decl))
17093 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17094 if (VAR_P (decl) && !DECL_NAME (decl)
17095 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17096 /* Anonymous aggregates are a special case. */
17097 finish_anon_union (decl);
17098 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17099 {
17100 DECL_CONTEXT (decl) = current_function_decl;
17101 if (DECL_NAME (decl) == this_identifier)
17102 {
17103 tree lam = DECL_CONTEXT (current_function_decl);
17104 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17105 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17106 }
17107 insert_capture_proxy (decl);
17108 }
17109 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17110 /* We already did a pushtag. */;
17111 else if (TREE_CODE (decl) == FUNCTION_DECL
17112 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17113 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17114 {
17115 DECL_CONTEXT (decl) = NULL_TREE;
17116 pushdecl (decl);
17117 DECL_CONTEXT (decl) = current_function_decl;
17118 cp_check_omp_declare_reduction (decl);
17119 }
17120 else
17121 {
17122 int const_init = false;
17123 unsigned int cnt = 0;
17124 tree first = NULL_TREE, ndecl = error_mark_node;
17125 maybe_push_decl (decl);
17126
17127 if (VAR_P (decl)
17128 && DECL_DECOMPOSITION_P (decl)
17129 && TREE_TYPE (pattern_decl) != error_mark_node)
17130 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17131 complain, in_decl, &first,
17132 &cnt);
17133
17134 init = tsubst_init (init, decl, args, complain, in_decl);
17135
17136 if (VAR_P (decl))
17137 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17138 (pattern_decl));
17139
17140 if (ndecl != error_mark_node)
17141 cp_maybe_mangle_decomp (ndecl, first, cnt);
17142
17143 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
17144
17145 if (ndecl != error_mark_node)
17146 cp_finish_decomp (ndecl, first, cnt);
17147 }
17148 }
17149 }
17150
17151 break;
17152 }
17153
17154 case FOR_STMT:
17155 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17156 RECUR (FOR_INIT_STMT (t));
17157 finish_init_stmt (stmt);
17158 tmp = RECUR (FOR_COND (t));
17159 finish_for_cond (tmp, stmt, false, 0);
17160 tmp = RECUR (FOR_EXPR (t));
17161 finish_for_expr (tmp, stmt);
17162 {
17163 bool prev = note_iteration_stmt_body_start ();
17164 RECUR (FOR_BODY (t));
17165 note_iteration_stmt_body_end (prev);
17166 }
17167 finish_for_stmt (stmt);
17168 break;
17169
17170 case RANGE_FOR_STMT:
17171 {
17172 /* Construct another range_for, if this is not a final
17173 substitution (for inside inside a generic lambda of a
17174 template). Otherwise convert to a regular for. */
17175 tree decl, expr;
17176 stmt = (processing_template_decl
17177 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17178 : begin_for_stmt (NULL_TREE, NULL_TREE));
17179 RECUR (RANGE_FOR_INIT_STMT (t));
17180 decl = RANGE_FOR_DECL (t);
17181 decl = tsubst (decl, args, complain, in_decl);
17182 maybe_push_decl (decl);
17183 expr = RECUR (RANGE_FOR_EXPR (t));
17184
17185 tree decomp_first = NULL_TREE;
17186 unsigned decomp_cnt = 0;
17187 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17188 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17189 complain, in_decl,
17190 &decomp_first, &decomp_cnt);
17191
17192 if (processing_template_decl)
17193 {
17194 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17195 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17196 finish_range_for_decl (stmt, decl, expr);
17197 if (decomp_first && decl != error_mark_node)
17198 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17199 }
17200 else
17201 {
17202 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17203 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17204 stmt = cp_convert_range_for (stmt, decl, expr,
17205 decomp_first, decomp_cnt,
17206 RANGE_FOR_IVDEP (t), unroll);
17207 }
17208
17209 bool prev = note_iteration_stmt_body_start ();
17210 RECUR (RANGE_FOR_BODY (t));
17211 note_iteration_stmt_body_end (prev);
17212 finish_for_stmt (stmt);
17213 }
17214 break;
17215
17216 case WHILE_STMT:
17217 stmt = begin_while_stmt ();
17218 tmp = RECUR (WHILE_COND (t));
17219 finish_while_stmt_cond (tmp, stmt, false, 0);
17220 {
17221 bool prev = note_iteration_stmt_body_start ();
17222 RECUR (WHILE_BODY (t));
17223 note_iteration_stmt_body_end (prev);
17224 }
17225 finish_while_stmt (stmt);
17226 break;
17227
17228 case DO_STMT:
17229 stmt = begin_do_stmt ();
17230 {
17231 bool prev = note_iteration_stmt_body_start ();
17232 RECUR (DO_BODY (t));
17233 note_iteration_stmt_body_end (prev);
17234 }
17235 finish_do_body (stmt);
17236 tmp = RECUR (DO_COND (t));
17237 finish_do_stmt (tmp, stmt, false, 0);
17238 break;
17239
17240 case IF_STMT:
17241 stmt = begin_if_stmt ();
17242 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17243 if (IF_STMT_CONSTEXPR_P (t))
17244 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17245 tmp = RECUR (IF_COND (t));
17246 tmp = finish_if_stmt_cond (tmp, stmt);
17247 if (IF_STMT_CONSTEXPR_P (t)
17248 && instantiation_dependent_expression_p (tmp))
17249 {
17250 /* We're partially instantiating a generic lambda, but the condition
17251 of the constexpr if is still dependent. Don't substitute into the
17252 branches now, just remember the template arguments. */
17253 do_poplevel (IF_SCOPE (stmt));
17254 IF_COND (stmt) = IF_COND (t);
17255 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17256 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17257 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17258 add_stmt (stmt);
17259 break;
17260 }
17261 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17262 /* Don't instantiate the THEN_CLAUSE. */;
17263 else
17264 {
17265 tree folded = fold_non_dependent_expr (tmp, complain);
17266 bool inhibit = integer_zerop (folded);
17267 if (inhibit)
17268 ++c_inhibit_evaluation_warnings;
17269 RECUR (THEN_CLAUSE (t));
17270 if (inhibit)
17271 --c_inhibit_evaluation_warnings;
17272 }
17273 finish_then_clause (stmt);
17274
17275 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17276 /* Don't instantiate the ELSE_CLAUSE. */;
17277 else if (ELSE_CLAUSE (t))
17278 {
17279 tree folded = fold_non_dependent_expr (tmp, complain);
17280 bool inhibit = integer_nonzerop (folded);
17281 begin_else_clause (stmt);
17282 if (inhibit)
17283 ++c_inhibit_evaluation_warnings;
17284 RECUR (ELSE_CLAUSE (t));
17285 if (inhibit)
17286 --c_inhibit_evaluation_warnings;
17287 finish_else_clause (stmt);
17288 }
17289
17290 finish_if_stmt (stmt);
17291 break;
17292
17293 case BIND_EXPR:
17294 if (BIND_EXPR_BODY_BLOCK (t))
17295 stmt = begin_function_body ();
17296 else
17297 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17298 ? BCS_TRY_BLOCK : 0);
17299
17300 RECUR (BIND_EXPR_BODY (t));
17301
17302 if (BIND_EXPR_BODY_BLOCK (t))
17303 finish_function_body (stmt);
17304 else
17305 finish_compound_stmt (stmt);
17306 break;
17307
17308 case BREAK_STMT:
17309 finish_break_stmt ();
17310 break;
17311
17312 case CONTINUE_STMT:
17313 finish_continue_stmt ();
17314 break;
17315
17316 case SWITCH_STMT:
17317 stmt = begin_switch_stmt ();
17318 tmp = RECUR (SWITCH_STMT_COND (t));
17319 finish_switch_cond (tmp, stmt);
17320 RECUR (SWITCH_STMT_BODY (t));
17321 finish_switch_stmt (stmt);
17322 break;
17323
17324 case CASE_LABEL_EXPR:
17325 {
17326 tree decl = CASE_LABEL (t);
17327 tree low = RECUR (CASE_LOW (t));
17328 tree high = RECUR (CASE_HIGH (t));
17329 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17330 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17331 {
17332 tree label = CASE_LABEL (l);
17333 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17334 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17335 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17336 }
17337 }
17338 break;
17339
17340 case LABEL_EXPR:
17341 {
17342 tree decl = LABEL_EXPR_LABEL (t);
17343 tree label;
17344
17345 label = finish_label_stmt (DECL_NAME (decl));
17346 if (TREE_CODE (label) == LABEL_DECL)
17347 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17348 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17349 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17350 }
17351 break;
17352
17353 case GOTO_EXPR:
17354 tmp = GOTO_DESTINATION (t);
17355 if (TREE_CODE (tmp) != LABEL_DECL)
17356 /* Computed goto's must be tsubst'd into. On the other hand,
17357 non-computed gotos must not be; the identifier in question
17358 will have no binding. */
17359 tmp = RECUR (tmp);
17360 else
17361 tmp = DECL_NAME (tmp);
17362 finish_goto_stmt (tmp);
17363 break;
17364
17365 case ASM_EXPR:
17366 {
17367 tree string = RECUR (ASM_STRING (t));
17368 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17369 complain, in_decl);
17370 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17371 complain, in_decl);
17372 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17373 complain, in_decl);
17374 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17375 complain, in_decl);
17376 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
17377 clobbers, labels, ASM_INLINE_P (t));
17378 tree asm_expr = tmp;
17379 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17380 asm_expr = TREE_OPERAND (asm_expr, 0);
17381 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17382 }
17383 break;
17384
17385 case TRY_BLOCK:
17386 if (CLEANUP_P (t))
17387 {
17388 stmt = begin_try_block ();
17389 RECUR (TRY_STMTS (t));
17390 finish_cleanup_try_block (stmt);
17391 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17392 }
17393 else
17394 {
17395 tree compound_stmt = NULL_TREE;
17396
17397 if (FN_TRY_BLOCK_P (t))
17398 stmt = begin_function_try_block (&compound_stmt);
17399 else
17400 stmt = begin_try_block ();
17401
17402 RECUR (TRY_STMTS (t));
17403
17404 if (FN_TRY_BLOCK_P (t))
17405 finish_function_try_block (stmt);
17406 else
17407 finish_try_block (stmt);
17408
17409 RECUR (TRY_HANDLERS (t));
17410 if (FN_TRY_BLOCK_P (t))
17411 finish_function_handler_sequence (stmt, compound_stmt);
17412 else
17413 finish_handler_sequence (stmt);
17414 }
17415 break;
17416
17417 case HANDLER:
17418 {
17419 tree decl = HANDLER_PARMS (t);
17420
17421 if (decl)
17422 {
17423 decl = tsubst (decl, args, complain, in_decl);
17424 /* Prevent instantiate_decl from trying to instantiate
17425 this variable. We've already done all that needs to be
17426 done. */
17427 if (decl != error_mark_node)
17428 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17429 }
17430 stmt = begin_handler ();
17431 finish_handler_parms (decl, stmt);
17432 RECUR (HANDLER_BODY (t));
17433 finish_handler (stmt);
17434 }
17435 break;
17436
17437 case TAG_DEFN:
17438 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17439 if (CLASS_TYPE_P (tmp))
17440 {
17441 /* Local classes are not independent templates; they are
17442 instantiated along with their containing function. And this
17443 way we don't have to deal with pushing out of one local class
17444 to instantiate a member of another local class. */
17445 /* Closures are handled by the LAMBDA_EXPR. */
17446 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17447 complete_type (tmp);
17448 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17449 if ((VAR_P (fld)
17450 || (TREE_CODE (fld) == FUNCTION_DECL
17451 && !DECL_ARTIFICIAL (fld)))
17452 && DECL_TEMPLATE_INSTANTIATION (fld))
17453 instantiate_decl (fld, /*defer_ok=*/false,
17454 /*expl_inst_class=*/false);
17455 }
17456 break;
17457
17458 case STATIC_ASSERT:
17459 {
17460 tree condition;
17461
17462 ++c_inhibit_evaluation_warnings;
17463 condition =
17464 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17465 args,
17466 complain, in_decl,
17467 /*integral_constant_expression_p=*/true);
17468 --c_inhibit_evaluation_warnings;
17469
17470 finish_static_assert (condition,
17471 STATIC_ASSERT_MESSAGE (t),
17472 STATIC_ASSERT_SOURCE_LOCATION (t),
17473 /*member_p=*/false);
17474 }
17475 break;
17476
17477 case OACC_KERNELS:
17478 case OACC_PARALLEL:
17479 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17480 in_decl);
17481 stmt = begin_omp_parallel ();
17482 RECUR (OMP_BODY (t));
17483 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17484 break;
17485
17486 case OMP_PARALLEL:
17487 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17488 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17489 complain, in_decl);
17490 if (OMP_PARALLEL_COMBINED (t))
17491 omp_parallel_combined_clauses = &tmp;
17492 stmt = begin_omp_parallel ();
17493 RECUR (OMP_PARALLEL_BODY (t));
17494 gcc_assert (omp_parallel_combined_clauses == NULL);
17495 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17496 = OMP_PARALLEL_COMBINED (t);
17497 pop_omp_privatization_clauses (r);
17498 break;
17499
17500 case OMP_TASK:
17501 if (OMP_TASK_BODY (t) == NULL_TREE)
17502 {
17503 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17504 complain, in_decl);
17505 t = copy_node (t);
17506 OMP_TASK_CLAUSES (t) = tmp;
17507 add_stmt (t);
17508 break;
17509 }
17510 r = push_omp_privatization_clauses (false);
17511 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17512 complain, in_decl);
17513 stmt = begin_omp_task ();
17514 RECUR (OMP_TASK_BODY (t));
17515 finish_omp_task (tmp, stmt);
17516 pop_omp_privatization_clauses (r);
17517 break;
17518
17519 case OMP_FOR:
17520 case OMP_SIMD:
17521 case OMP_DISTRIBUTE:
17522 case OMP_TASKLOOP:
17523 case OACC_LOOP:
17524 {
17525 tree clauses, body, pre_body;
17526 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17527 tree orig_declv = NULL_TREE;
17528 tree incrv = NULL_TREE;
17529 enum c_omp_region_type ort = C_ORT_OMP;
17530 bool any_range_for = false;
17531 int i;
17532
17533 if (TREE_CODE (t) == OACC_LOOP)
17534 ort = C_ORT_ACC;
17535
17536 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17537 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17538 in_decl);
17539 if (OMP_FOR_INIT (t) != NULL_TREE)
17540 {
17541 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17542 if (OMP_FOR_ORIG_DECLS (t))
17543 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17544 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17545 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17546 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17547 }
17548
17549 keep_next_level (true);
17550 stmt = begin_omp_structured_block ();
17551
17552 pre_body = push_stmt_list ();
17553 RECUR (OMP_FOR_PRE_BODY (t));
17554 pre_body = pop_stmt_list (pre_body);
17555
17556 if (OMP_FOR_INIT (t) != NULL_TREE)
17557 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17558 any_range_for
17559 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
17560 condv, incrv, &clauses, args,
17561 complain, in_decl,
17562 integral_constant_expression_p);
17563 omp_parallel_combined_clauses = NULL;
17564
17565 if (any_range_for)
17566 {
17567 gcc_assert (orig_declv);
17568 body = begin_omp_structured_block ();
17569 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17570 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
17571 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
17572 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
17573 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
17574 TREE_VEC_ELT (declv, i));
17575 }
17576 else
17577 body = push_stmt_list ();
17578 RECUR (OMP_FOR_BODY (t));
17579 if (any_range_for)
17580 body = finish_omp_structured_block (body);
17581 else
17582 body = pop_stmt_list (body);
17583
17584 if (OMP_FOR_INIT (t) != NULL_TREE)
17585 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17586 orig_declv, initv, condv, incrv, body, pre_body,
17587 NULL, clauses);
17588 else
17589 {
17590 t = make_node (TREE_CODE (t));
17591 TREE_TYPE (t) = void_type_node;
17592 OMP_FOR_BODY (t) = body;
17593 OMP_FOR_PRE_BODY (t) = pre_body;
17594 OMP_FOR_CLAUSES (t) = clauses;
17595 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17596 add_stmt (t);
17597 }
17598
17599 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
17600 t));
17601 pop_omp_privatization_clauses (r);
17602 }
17603 break;
17604
17605 case OMP_SECTIONS:
17606 omp_parallel_combined_clauses = NULL;
17607 /* FALLTHRU */
17608 case OMP_SINGLE:
17609 case OMP_TEAMS:
17610 case OMP_CRITICAL:
17611 case OMP_TASKGROUP:
17612 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17613 && OMP_TEAMS_COMBINED (t));
17614 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17615 in_decl);
17616 if (TREE_CODE (t) == OMP_TEAMS)
17617 {
17618 keep_next_level (true);
17619 stmt = begin_omp_structured_block ();
17620 RECUR (OMP_BODY (t));
17621 stmt = finish_omp_structured_block (stmt);
17622 }
17623 else
17624 {
17625 stmt = push_stmt_list ();
17626 RECUR (OMP_BODY (t));
17627 stmt = pop_stmt_list (stmt);
17628 }
17629
17630 t = copy_node (t);
17631 OMP_BODY (t) = stmt;
17632 OMP_CLAUSES (t) = tmp;
17633 add_stmt (t);
17634 pop_omp_privatization_clauses (r);
17635 break;
17636
17637 case OMP_DEPOBJ:
17638 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
17639 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
17640 {
17641 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17642 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
17643 {
17644 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
17645 args, complain, in_decl);
17646 if (tmp == NULL_TREE)
17647 tmp = error_mark_node;
17648 }
17649 else
17650 {
17651 kind = (enum omp_clause_depend_kind)
17652 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
17653 tmp = NULL_TREE;
17654 }
17655 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
17656 }
17657 else
17658 finish_omp_depobj (EXPR_LOCATION (t), r,
17659 OMP_CLAUSE_DEPEND_SOURCE,
17660 OMP_DEPOBJ_CLAUSES (t));
17661 break;
17662
17663 case OACC_DATA:
17664 case OMP_TARGET_DATA:
17665 case OMP_TARGET:
17666 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17667 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17668 in_decl);
17669 keep_next_level (true);
17670 stmt = begin_omp_structured_block ();
17671
17672 RECUR (OMP_BODY (t));
17673 stmt = finish_omp_structured_block (stmt);
17674
17675 t = copy_node (t);
17676 OMP_BODY (t) = stmt;
17677 OMP_CLAUSES (t) = tmp;
17678 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17679 {
17680 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17681 if (teams)
17682 {
17683 /* For combined target teams, ensure the num_teams and
17684 thread_limit clause expressions are evaluated on the host,
17685 before entering the target construct. */
17686 tree c;
17687 for (c = OMP_TEAMS_CLAUSES (teams);
17688 c; c = OMP_CLAUSE_CHAIN (c))
17689 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17690 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17691 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17692 {
17693 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17694 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17695 if (expr == error_mark_node)
17696 continue;
17697 tmp = TARGET_EXPR_SLOT (expr);
17698 add_stmt (expr);
17699 OMP_CLAUSE_OPERAND (c, 0) = expr;
17700 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17701 OMP_CLAUSE_FIRSTPRIVATE);
17702 OMP_CLAUSE_DECL (tc) = tmp;
17703 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17704 OMP_TARGET_CLAUSES (t) = tc;
17705 }
17706 }
17707 }
17708 add_stmt (t);
17709 break;
17710
17711 case OACC_DECLARE:
17712 t = copy_node (t);
17713 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17714 complain, in_decl);
17715 OACC_DECLARE_CLAUSES (t) = tmp;
17716 add_stmt (t);
17717 break;
17718
17719 case OMP_TARGET_UPDATE:
17720 case OMP_TARGET_ENTER_DATA:
17721 case OMP_TARGET_EXIT_DATA:
17722 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17723 complain, in_decl);
17724 t = copy_node (t);
17725 OMP_STANDALONE_CLAUSES (t) = tmp;
17726 add_stmt (t);
17727 break;
17728
17729 case OACC_ENTER_DATA:
17730 case OACC_EXIT_DATA:
17731 case OACC_UPDATE:
17732 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17733 complain, in_decl);
17734 t = copy_node (t);
17735 OMP_STANDALONE_CLAUSES (t) = tmp;
17736 add_stmt (t);
17737 break;
17738
17739 case OMP_ORDERED:
17740 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17741 complain, in_decl);
17742 stmt = push_stmt_list ();
17743 RECUR (OMP_BODY (t));
17744 stmt = pop_stmt_list (stmt);
17745
17746 t = copy_node (t);
17747 OMP_BODY (t) = stmt;
17748 OMP_ORDERED_CLAUSES (t) = tmp;
17749 add_stmt (t);
17750 break;
17751
17752 case OMP_SECTION:
17753 case OMP_MASTER:
17754 stmt = push_stmt_list ();
17755 RECUR (OMP_BODY (t));
17756 stmt = pop_stmt_list (stmt);
17757
17758 t = copy_node (t);
17759 OMP_BODY (t) = stmt;
17760 add_stmt (t);
17761 break;
17762
17763 case OMP_ATOMIC:
17764 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17765 tmp = NULL_TREE;
17766 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
17767 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
17768 complain, in_decl);
17769 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17770 {
17771 tree op1 = TREE_OPERAND (t, 1);
17772 tree rhs1 = NULL_TREE;
17773 tree lhs, rhs;
17774 if (TREE_CODE (op1) == COMPOUND_EXPR)
17775 {
17776 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17777 op1 = TREE_OPERAND (op1, 1);
17778 }
17779 lhs = RECUR (TREE_OPERAND (op1, 0));
17780 rhs = RECUR (TREE_OPERAND (op1, 1));
17781 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
17782 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
17783 OMP_ATOMIC_MEMORY_ORDER (t));
17784 }
17785 else
17786 {
17787 tree op1 = TREE_OPERAND (t, 1);
17788 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17789 tree rhs1 = NULL_TREE;
17790 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17791 enum tree_code opcode = NOP_EXPR;
17792 if (code == OMP_ATOMIC_READ)
17793 {
17794 v = RECUR (TREE_OPERAND (op1, 0));
17795 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17796 }
17797 else if (code == OMP_ATOMIC_CAPTURE_OLD
17798 || code == OMP_ATOMIC_CAPTURE_NEW)
17799 {
17800 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17801 v = RECUR (TREE_OPERAND (op1, 0));
17802 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17803 if (TREE_CODE (op11) == COMPOUND_EXPR)
17804 {
17805 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17806 op11 = TREE_OPERAND (op11, 1);
17807 }
17808 lhs = RECUR (TREE_OPERAND (op11, 0));
17809 rhs = RECUR (TREE_OPERAND (op11, 1));
17810 opcode = TREE_CODE (op11);
17811 if (opcode == MODIFY_EXPR)
17812 opcode = NOP_EXPR;
17813 }
17814 else
17815 {
17816 code = OMP_ATOMIC;
17817 lhs = RECUR (TREE_OPERAND (op1, 0));
17818 rhs = RECUR (TREE_OPERAND (op1, 1));
17819 }
17820 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
17821 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
17822 }
17823 break;
17824
17825 case TRANSACTION_EXPR:
17826 {
17827 int flags = 0;
17828 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17829 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17830
17831 if (TRANSACTION_EXPR_IS_STMT (t))
17832 {
17833 tree body = TRANSACTION_EXPR_BODY (t);
17834 tree noex = NULL_TREE;
17835 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17836 {
17837 noex = MUST_NOT_THROW_COND (body);
17838 if (noex == NULL_TREE)
17839 noex = boolean_true_node;
17840 body = TREE_OPERAND (body, 0);
17841 }
17842 stmt = begin_transaction_stmt (input_location, NULL, flags);
17843 RECUR (body);
17844 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17845 }
17846 else
17847 {
17848 stmt = build_transaction_expr (EXPR_LOCATION (t),
17849 RECUR (TRANSACTION_EXPR_BODY (t)),
17850 flags, NULL_TREE);
17851 RETURN (stmt);
17852 }
17853 }
17854 break;
17855
17856 case MUST_NOT_THROW_EXPR:
17857 {
17858 tree op0 = RECUR (TREE_OPERAND (t, 0));
17859 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17860 RETURN (build_must_not_throw_expr (op0, cond));
17861 }
17862
17863 case EXPR_PACK_EXPANSION:
17864 error ("invalid use of pack expansion expression");
17865 RETURN (error_mark_node);
17866
17867 case NONTYPE_ARGUMENT_PACK:
17868 error ("use %<...%> to expand argument pack");
17869 RETURN (error_mark_node);
17870
17871 case COMPOUND_EXPR:
17872 tmp = RECUR (TREE_OPERAND (t, 0));
17873 if (tmp == NULL_TREE)
17874 /* If the first operand was a statement, we're done with it. */
17875 RETURN (RECUR (TREE_OPERAND (t, 1)));
17876 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17877 RECUR (TREE_OPERAND (t, 1)),
17878 complain));
17879
17880 case ANNOTATE_EXPR:
17881 tmp = RECUR (TREE_OPERAND (t, 0));
17882 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17883 TREE_TYPE (tmp), tmp,
17884 RECUR (TREE_OPERAND (t, 1)),
17885 RECUR (TREE_OPERAND (t, 2))));
17886
17887 case PREDICT_EXPR:
17888 RETURN (add_stmt (copy_node (t)));
17889
17890 default:
17891 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17892
17893 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17894 /*function_p=*/false,
17895 integral_constant_expression_p));
17896 }
17897
17898 RETURN (NULL_TREE);
17899 out:
17900 input_location = loc;
17901 return r;
17902 #undef RECUR
17903 #undef RETURN
17904 }
17905
17906 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17907 function. For description of the body see comment above
17908 cp_parser_omp_declare_reduction_exprs. */
17909
17910 static void
17911 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17912 {
17913 if (t == NULL_TREE || t == error_mark_node)
17914 return;
17915
17916 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17917
17918 tree_stmt_iterator tsi;
17919 int i;
17920 tree stmts[7];
17921 memset (stmts, 0, sizeof stmts);
17922 for (i = 0, tsi = tsi_start (t);
17923 i < 7 && !tsi_end_p (tsi);
17924 i++, tsi_next (&tsi))
17925 stmts[i] = tsi_stmt (tsi);
17926 gcc_assert (tsi_end_p (tsi));
17927
17928 if (i >= 3)
17929 {
17930 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17931 && TREE_CODE (stmts[1]) == DECL_EXPR);
17932 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17933 args, complain, in_decl);
17934 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17935 args, complain, in_decl);
17936 DECL_CONTEXT (omp_out) = current_function_decl;
17937 DECL_CONTEXT (omp_in) = current_function_decl;
17938 keep_next_level (true);
17939 tree block = begin_omp_structured_block ();
17940 tsubst_expr (stmts[2], args, complain, in_decl, false);
17941 block = finish_omp_structured_block (block);
17942 block = maybe_cleanup_point_expr_void (block);
17943 add_decl_expr (omp_out);
17944 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17945 TREE_NO_WARNING (omp_out) = 1;
17946 add_decl_expr (omp_in);
17947 finish_expr_stmt (block);
17948 }
17949 if (i >= 6)
17950 {
17951 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17952 && TREE_CODE (stmts[4]) == DECL_EXPR);
17953 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17954 args, complain, in_decl);
17955 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17956 args, complain, in_decl);
17957 DECL_CONTEXT (omp_priv) = current_function_decl;
17958 DECL_CONTEXT (omp_orig) = current_function_decl;
17959 keep_next_level (true);
17960 tree block = begin_omp_structured_block ();
17961 tsubst_expr (stmts[5], args, complain, in_decl, false);
17962 block = finish_omp_structured_block (block);
17963 block = maybe_cleanup_point_expr_void (block);
17964 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17965 add_decl_expr (omp_priv);
17966 add_decl_expr (omp_orig);
17967 finish_expr_stmt (block);
17968 if (i == 7)
17969 add_decl_expr (omp_orig);
17970 }
17971 }
17972
17973 /* T is a postfix-expression that is not being used in a function
17974 call. Return the substituted version of T. */
17975
17976 static tree
17977 tsubst_non_call_postfix_expression (tree t, tree args,
17978 tsubst_flags_t complain,
17979 tree in_decl)
17980 {
17981 if (TREE_CODE (t) == SCOPE_REF)
17982 t = tsubst_qualified_id (t, args, complain, in_decl,
17983 /*done=*/false, /*address_p=*/false);
17984 else
17985 t = tsubst_copy_and_build (t, args, complain, in_decl,
17986 /*function_p=*/false,
17987 /*integral_constant_expression_p=*/false);
17988
17989 return t;
17990 }
17991
17992 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17993 instantiation context. Instantiating a pack expansion containing a lambda
17994 might result in multiple lambdas all based on the same lambda in the
17995 template. */
17996
17997 tree
17998 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17999 {
18000 tree oldfn = lambda_function (t);
18001 in_decl = oldfn;
18002
18003 /* If we have already specialized this lambda expr, reuse it. See
18004 PR c++/87322. */
18005 if (local_specializations)
18006 if (tree r = retrieve_local_specialization (t))
18007 return r;
18008
18009 tree r = build_lambda_expr ();
18010
18011 if (local_specializations)
18012 register_local_specialization (r, t);
18013
18014 LAMBDA_EXPR_LOCATION (r)
18015 = LAMBDA_EXPR_LOCATION (t);
18016 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18017 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18018 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18019 LAMBDA_EXPR_INSTANTIATED (r) = true;
18020
18021 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18022 /* A lambda in a default argument outside a class gets no
18023 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18024 tsubst_default_argument calls start_lambda_scope, so we need to
18025 specifically ignore it here, and use the global scope. */
18026 record_null_lambda_scope (r);
18027 else
18028 record_lambda_scope (r);
18029
18030 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18031 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18032
18033 vec<tree,va_gc>* field_packs = NULL;
18034
18035 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18036 cap = TREE_CHAIN (cap))
18037 {
18038 tree ofield = TREE_PURPOSE (cap);
18039 if (PACK_EXPANSION_P (ofield))
18040 ofield = PACK_EXPANSION_PATTERN (ofield);
18041 tree field = tsubst_decl (ofield, args, complain);
18042
18043 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18044 {
18045 /* Remember these for when we've pushed local_specializations. */
18046 vec_safe_push (field_packs, ofield);
18047 vec_safe_push (field_packs, field);
18048 }
18049
18050 if (field == error_mark_node)
18051 return error_mark_node;
18052
18053 tree init = TREE_VALUE (cap);
18054 if (PACK_EXPANSION_P (init))
18055 init = tsubst_pack_expansion (init, args, complain, in_decl);
18056 else
18057 init = tsubst_copy_and_build (init, args, complain, in_decl,
18058 /*fn*/false, /*constexpr*/false);
18059
18060 if (TREE_CODE (field) == TREE_VEC)
18061 {
18062 int len = TREE_VEC_LENGTH (field);
18063 gcc_assert (TREE_CODE (init) == TREE_VEC
18064 && TREE_VEC_LENGTH (init) == len);
18065 for (int i = 0; i < len; ++i)
18066 LAMBDA_EXPR_CAPTURE_LIST (r)
18067 = tree_cons (TREE_VEC_ELT (field, i),
18068 TREE_VEC_ELT (init, i),
18069 LAMBDA_EXPR_CAPTURE_LIST (r));
18070 }
18071 else
18072 {
18073 LAMBDA_EXPR_CAPTURE_LIST (r)
18074 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
18075
18076 if (id_equal (DECL_NAME (field), "__this"))
18077 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18078 }
18079 }
18080
18081 tree type = begin_lambda_type (r);
18082 if (type == error_mark_node)
18083 return error_mark_node;
18084
18085 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18086 determine_visibility (TYPE_NAME (type));
18087
18088 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18089
18090 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18091 ? DECL_TI_TEMPLATE (oldfn)
18092 : NULL_TREE);
18093
18094 tree fntype = static_fn_type (oldfn);
18095 if (oldtmpl)
18096 ++processing_template_decl;
18097 fntype = tsubst (fntype, args, complain, in_decl);
18098 if (oldtmpl)
18099 --processing_template_decl;
18100
18101 if (fntype == error_mark_node)
18102 r = error_mark_node;
18103 else
18104 {
18105 /* The body of a lambda-expression is not a subexpression of the
18106 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18107 which would be skipped if cp_unevaluated_operand. */
18108 cp_evaluated ev;
18109
18110 /* Fix the type of 'this'. */
18111 fntype = build_memfn_type (fntype, type,
18112 type_memfn_quals (fntype),
18113 type_memfn_rqual (fntype));
18114 tree fn, tmpl;
18115 if (oldtmpl)
18116 {
18117 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18118 fn = DECL_TEMPLATE_RESULT (tmpl);
18119 finish_member_declaration (tmpl);
18120 }
18121 else
18122 {
18123 tmpl = NULL_TREE;
18124 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18125 finish_member_declaration (fn);
18126 }
18127
18128 /* Let finish_function set this. */
18129 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18130
18131 bool nested = cfun;
18132 if (nested)
18133 push_function_context ();
18134 else
18135 /* Still increment function_depth so that we don't GC in the
18136 middle of an expression. */
18137 ++function_depth;
18138
18139 local_specialization_stack s (lss_copy);
18140
18141 tree body = start_lambda_function (fn, r);
18142
18143 /* Now record them for lookup_init_capture_pack. */
18144 int fplen = vec_safe_length (field_packs);
18145 for (int i = 0; i < fplen; )
18146 {
18147 tree pack = (*field_packs)[i++];
18148 tree inst = (*field_packs)[i++];
18149 register_local_specialization (inst, pack);
18150 }
18151 release_tree_vector (field_packs);
18152
18153 register_parameter_specializations (oldfn, fn);
18154
18155 if (oldtmpl)
18156 {
18157 /* We might not partially instantiate some parts of the function, so
18158 copy these flags from the original template. */
18159 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18160 current_function_returns_value = ol->returns_value;
18161 current_function_returns_null = ol->returns_null;
18162 current_function_returns_abnormally = ol->returns_abnormally;
18163 current_function_infinite_loop = ol->infinite_loop;
18164 }
18165
18166 /* [temp.deduct] A lambda-expression appearing in a function type or a
18167 template parameter is not considered part of the immediate context for
18168 the purposes of template argument deduction. */
18169 complain = tf_warning_or_error;
18170
18171 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18172 /*constexpr*/false);
18173
18174 finish_lambda_function (body);
18175
18176 if (nested)
18177 pop_function_context ();
18178 else
18179 --function_depth;
18180
18181 /* The capture list was built up in reverse order; fix that now. */
18182 LAMBDA_EXPR_CAPTURE_LIST (r)
18183 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18184
18185 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18186
18187 maybe_add_lambda_conv_op (type);
18188 }
18189
18190 finish_struct (type, /*attr*/NULL_TREE);
18191
18192 insert_pending_capture_proxies ();
18193
18194 return r;
18195 }
18196
18197 /* Like tsubst but deals with expressions and performs semantic
18198 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18199
18200 tree
18201 tsubst_copy_and_build (tree t,
18202 tree args,
18203 tsubst_flags_t complain,
18204 tree in_decl,
18205 bool function_p,
18206 bool integral_constant_expression_p)
18207 {
18208 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18209 #define RECUR(NODE) \
18210 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18211 /*function_p=*/false, \
18212 integral_constant_expression_p)
18213
18214 tree retval, op1;
18215 location_t loc;
18216
18217 if (t == NULL_TREE || t == error_mark_node)
18218 return t;
18219
18220 loc = input_location;
18221 if (location_t eloc = cp_expr_location (t))
18222 input_location = eloc;
18223
18224 /* N3276 decltype magic only applies to calls at the top level or on the
18225 right side of a comma. */
18226 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18227 complain &= ~tf_decltype;
18228
18229 switch (TREE_CODE (t))
18230 {
18231 case USING_DECL:
18232 t = DECL_NAME (t);
18233 /* Fall through. */
18234 case IDENTIFIER_NODE:
18235 {
18236 tree decl;
18237 cp_id_kind idk;
18238 bool non_integral_constant_expression_p;
18239 const char *error_msg;
18240
18241 if (IDENTIFIER_CONV_OP_P (t))
18242 {
18243 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18244 t = make_conv_op_name (new_type);
18245 }
18246
18247 /* Look up the name. */
18248 decl = lookup_name (t);
18249
18250 /* By convention, expressions use ERROR_MARK_NODE to indicate
18251 failure, not NULL_TREE. */
18252 if (decl == NULL_TREE)
18253 decl = error_mark_node;
18254
18255 decl = finish_id_expression (t, decl, NULL_TREE,
18256 &idk,
18257 integral_constant_expression_p,
18258 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18259 &non_integral_constant_expression_p,
18260 /*template_p=*/false,
18261 /*done=*/true,
18262 /*address_p=*/false,
18263 /*template_arg_p=*/false,
18264 &error_msg,
18265 input_location);
18266 if (error_msg)
18267 error (error_msg);
18268 if (!function_p && identifier_p (decl))
18269 {
18270 if (complain & tf_error)
18271 unqualified_name_lookup_error (decl);
18272 decl = error_mark_node;
18273 }
18274 RETURN (decl);
18275 }
18276
18277 case TEMPLATE_ID_EXPR:
18278 {
18279 tree object;
18280 tree templ = RECUR (TREE_OPERAND (t, 0));
18281 tree targs = TREE_OPERAND (t, 1);
18282
18283 if (targs)
18284 targs = tsubst_template_args (targs, args, complain, in_decl);
18285 if (targs == error_mark_node)
18286 RETURN (error_mark_node);
18287
18288 if (TREE_CODE (templ) == SCOPE_REF)
18289 {
18290 tree name = TREE_OPERAND (templ, 1);
18291 tree tid = lookup_template_function (name, targs);
18292 TREE_OPERAND (templ, 1) = tid;
18293 RETURN (templ);
18294 }
18295
18296 if (variable_template_p (templ))
18297 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18298
18299 if (TREE_CODE (templ) == COMPONENT_REF)
18300 {
18301 object = TREE_OPERAND (templ, 0);
18302 templ = TREE_OPERAND (templ, 1);
18303 }
18304 else
18305 object = NULL_TREE;
18306 templ = lookup_template_function (templ, targs);
18307
18308 if (object)
18309 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18310 object, templ, NULL_TREE));
18311 else
18312 RETURN (baselink_for_fns (templ));
18313 }
18314
18315 case INDIRECT_REF:
18316 {
18317 tree r = RECUR (TREE_OPERAND (t, 0));
18318
18319 if (REFERENCE_REF_P (t))
18320 {
18321 /* A type conversion to reference type will be enclosed in
18322 such an indirect ref, but the substitution of the cast
18323 will have also added such an indirect ref. */
18324 r = convert_from_reference (r);
18325 }
18326 else
18327 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18328 complain|decltype_flag);
18329
18330 if (REF_PARENTHESIZED_P (t))
18331 r = force_paren_expr (r);
18332
18333 RETURN (r);
18334 }
18335
18336 case NOP_EXPR:
18337 {
18338 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18339 tree op0 = RECUR (TREE_OPERAND (t, 0));
18340 RETURN (build_nop (type, op0));
18341 }
18342
18343 case IMPLICIT_CONV_EXPR:
18344 {
18345 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18346 tree expr = RECUR (TREE_OPERAND (t, 0));
18347 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18348 {
18349 retval = copy_node (t);
18350 TREE_TYPE (retval) = type;
18351 TREE_OPERAND (retval, 0) = expr;
18352 RETURN (retval);
18353 }
18354 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18355 /* We'll pass this to convert_nontype_argument again, we don't need
18356 to actually perform any conversion here. */
18357 RETURN (expr);
18358 int flags = LOOKUP_IMPLICIT;
18359 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18360 flags = LOOKUP_NORMAL;
18361 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18362 flags |= LOOKUP_NO_NARROWING;
18363 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18364 flags));
18365 }
18366
18367 case CONVERT_EXPR:
18368 {
18369 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18370 tree op0 = RECUR (TREE_OPERAND (t, 0));
18371 if (op0 == error_mark_node)
18372 RETURN (error_mark_node);
18373 RETURN (build1 (CONVERT_EXPR, type, op0));
18374 }
18375
18376 case CAST_EXPR:
18377 case REINTERPRET_CAST_EXPR:
18378 case CONST_CAST_EXPR:
18379 case DYNAMIC_CAST_EXPR:
18380 case STATIC_CAST_EXPR:
18381 {
18382 tree type;
18383 tree op, r = NULL_TREE;
18384
18385 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18386 if (integral_constant_expression_p
18387 && !cast_valid_in_integral_constant_expression_p (type))
18388 {
18389 if (complain & tf_error)
18390 error ("a cast to a type other than an integral or "
18391 "enumeration type cannot appear in a constant-expression");
18392 RETURN (error_mark_node);
18393 }
18394
18395 op = RECUR (TREE_OPERAND (t, 0));
18396
18397 warning_sentinel s(warn_useless_cast);
18398 warning_sentinel s2(warn_ignored_qualifiers);
18399 switch (TREE_CODE (t))
18400 {
18401 case CAST_EXPR:
18402 r = build_functional_cast (type, op, complain);
18403 break;
18404 case REINTERPRET_CAST_EXPR:
18405 r = build_reinterpret_cast (type, op, complain);
18406 break;
18407 case CONST_CAST_EXPR:
18408 r = build_const_cast (type, op, complain);
18409 break;
18410 case DYNAMIC_CAST_EXPR:
18411 r = build_dynamic_cast (type, op, complain);
18412 break;
18413 case STATIC_CAST_EXPR:
18414 r = build_static_cast (type, op, complain);
18415 break;
18416 default:
18417 gcc_unreachable ();
18418 }
18419
18420 RETURN (r);
18421 }
18422
18423 case POSTDECREMENT_EXPR:
18424 case POSTINCREMENT_EXPR:
18425 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18426 args, complain, in_decl);
18427 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18428 complain|decltype_flag));
18429
18430 case PREDECREMENT_EXPR:
18431 case PREINCREMENT_EXPR:
18432 case NEGATE_EXPR:
18433 case BIT_NOT_EXPR:
18434 case ABS_EXPR:
18435 case TRUTH_NOT_EXPR:
18436 case UNARY_PLUS_EXPR: /* Unary + */
18437 case REALPART_EXPR:
18438 case IMAGPART_EXPR:
18439 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18440 RECUR (TREE_OPERAND (t, 0)),
18441 complain|decltype_flag));
18442
18443 case FIX_TRUNC_EXPR:
18444 gcc_unreachable ();
18445
18446 case ADDR_EXPR:
18447 op1 = TREE_OPERAND (t, 0);
18448 if (TREE_CODE (op1) == LABEL_DECL)
18449 RETURN (finish_label_address_expr (DECL_NAME (op1),
18450 EXPR_LOCATION (op1)));
18451 if (TREE_CODE (op1) == SCOPE_REF)
18452 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18453 /*done=*/true, /*address_p=*/true);
18454 else
18455 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18456 in_decl);
18457 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18458 complain|decltype_flag));
18459
18460 case PLUS_EXPR:
18461 case MINUS_EXPR:
18462 case MULT_EXPR:
18463 case TRUNC_DIV_EXPR:
18464 case CEIL_DIV_EXPR:
18465 case FLOOR_DIV_EXPR:
18466 case ROUND_DIV_EXPR:
18467 case EXACT_DIV_EXPR:
18468 case BIT_AND_EXPR:
18469 case BIT_IOR_EXPR:
18470 case BIT_XOR_EXPR:
18471 case TRUNC_MOD_EXPR:
18472 case FLOOR_MOD_EXPR:
18473 case TRUTH_ANDIF_EXPR:
18474 case TRUTH_ORIF_EXPR:
18475 case TRUTH_AND_EXPR:
18476 case TRUTH_OR_EXPR:
18477 case RSHIFT_EXPR:
18478 case LSHIFT_EXPR:
18479 case RROTATE_EXPR:
18480 case LROTATE_EXPR:
18481 case EQ_EXPR:
18482 case NE_EXPR:
18483 case MAX_EXPR:
18484 case MIN_EXPR:
18485 case LE_EXPR:
18486 case GE_EXPR:
18487 case LT_EXPR:
18488 case GT_EXPR:
18489 case MEMBER_REF:
18490 case DOTSTAR_EXPR:
18491 {
18492 warning_sentinel s1(warn_type_limits);
18493 warning_sentinel s2(warn_div_by_zero);
18494 warning_sentinel s3(warn_logical_op);
18495 warning_sentinel s4(warn_tautological_compare);
18496 tree op0 = RECUR (TREE_OPERAND (t, 0));
18497 tree op1 = RECUR (TREE_OPERAND (t, 1));
18498 tree r = build_x_binary_op
18499 (input_location, TREE_CODE (t),
18500 op0,
18501 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18502 ? ERROR_MARK
18503 : TREE_CODE (TREE_OPERAND (t, 0))),
18504 op1,
18505 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18506 ? ERROR_MARK
18507 : TREE_CODE (TREE_OPERAND (t, 1))),
18508 /*overload=*/NULL,
18509 complain|decltype_flag);
18510 if (EXPR_P (r) && TREE_NO_WARNING (t))
18511 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18512
18513 RETURN (r);
18514 }
18515
18516 case POINTER_PLUS_EXPR:
18517 {
18518 tree op0 = RECUR (TREE_OPERAND (t, 0));
18519 tree op1 = RECUR (TREE_OPERAND (t, 1));
18520 RETURN (fold_build_pointer_plus (op0, op1));
18521 }
18522
18523 case SCOPE_REF:
18524 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18525 /*address_p=*/false));
18526 case ARRAY_REF:
18527 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18528 args, complain, in_decl);
18529 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18530 RECUR (TREE_OPERAND (t, 1)),
18531 complain|decltype_flag));
18532
18533 case SIZEOF_EXPR:
18534 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18535 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18536 RETURN (tsubst_copy (t, args, complain, in_decl));
18537 /* Fall through */
18538
18539 case ALIGNOF_EXPR:
18540 {
18541 tree r;
18542
18543 op1 = TREE_OPERAND (t, 0);
18544 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18545 op1 = TREE_TYPE (op1);
18546 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18547 && ALIGNOF_EXPR_STD_P (t));
18548 if (!args)
18549 {
18550 /* When there are no ARGS, we are trying to evaluate a
18551 non-dependent expression from the parser. Trying to do
18552 the substitutions may not work. */
18553 if (!TYPE_P (op1))
18554 op1 = TREE_TYPE (op1);
18555 }
18556 else
18557 {
18558 ++cp_unevaluated_operand;
18559 ++c_inhibit_evaluation_warnings;
18560 if (TYPE_P (op1))
18561 op1 = tsubst (op1, args, complain, in_decl);
18562 else
18563 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18564 /*function_p=*/false,
18565 /*integral_constant_expression_p=*/
18566 false);
18567 --cp_unevaluated_operand;
18568 --c_inhibit_evaluation_warnings;
18569 }
18570 if (TYPE_P (op1))
18571 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18572 complain & tf_error);
18573 else
18574 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18575 complain & tf_error);
18576 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18577 {
18578 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18579 {
18580 if (!processing_template_decl && TYPE_P (op1))
18581 {
18582 r = build_min (SIZEOF_EXPR, size_type_node,
18583 build1 (NOP_EXPR, op1, error_mark_node));
18584 SIZEOF_EXPR_TYPE_P (r) = 1;
18585 }
18586 else
18587 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18588 TREE_SIDE_EFFECTS (r) = 0;
18589 TREE_READONLY (r) = 1;
18590 }
18591 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18592 }
18593 RETURN (r);
18594 }
18595
18596 case AT_ENCODE_EXPR:
18597 {
18598 op1 = TREE_OPERAND (t, 0);
18599 ++cp_unevaluated_operand;
18600 ++c_inhibit_evaluation_warnings;
18601 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18602 /*function_p=*/false,
18603 /*integral_constant_expression_p=*/false);
18604 --cp_unevaluated_operand;
18605 --c_inhibit_evaluation_warnings;
18606 RETURN (objc_build_encode_expr (op1));
18607 }
18608
18609 case NOEXCEPT_EXPR:
18610 op1 = TREE_OPERAND (t, 0);
18611 ++cp_unevaluated_operand;
18612 ++c_inhibit_evaluation_warnings;
18613 ++cp_noexcept_operand;
18614 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18615 /*function_p=*/false,
18616 /*integral_constant_expression_p=*/false);
18617 --cp_unevaluated_operand;
18618 --c_inhibit_evaluation_warnings;
18619 --cp_noexcept_operand;
18620 RETURN (finish_noexcept_expr (op1, complain));
18621
18622 case MODOP_EXPR:
18623 {
18624 warning_sentinel s(warn_div_by_zero);
18625 tree lhs = RECUR (TREE_OPERAND (t, 0));
18626 tree rhs = RECUR (TREE_OPERAND (t, 2));
18627 tree r = build_x_modify_expr
18628 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18629 complain|decltype_flag);
18630 /* TREE_NO_WARNING must be set if either the expression was
18631 parenthesized or it uses an operator such as >>= rather
18632 than plain assignment. In the former case, it was already
18633 set and must be copied. In the latter case,
18634 build_x_modify_expr sets it and it must not be reset
18635 here. */
18636 if (TREE_NO_WARNING (t))
18637 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18638
18639 RETURN (r);
18640 }
18641
18642 case ARROW_EXPR:
18643 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18644 args, complain, in_decl);
18645 /* Remember that there was a reference to this entity. */
18646 if (DECL_P (op1)
18647 && !mark_used (op1, complain) && !(complain & tf_error))
18648 RETURN (error_mark_node);
18649 RETURN (build_x_arrow (input_location, op1, complain));
18650
18651 case NEW_EXPR:
18652 {
18653 tree placement = RECUR (TREE_OPERAND (t, 0));
18654 tree init = RECUR (TREE_OPERAND (t, 3));
18655 vec<tree, va_gc> *placement_vec;
18656 vec<tree, va_gc> *init_vec;
18657 tree ret;
18658
18659 if (placement == NULL_TREE)
18660 placement_vec = NULL;
18661 else
18662 {
18663 placement_vec = make_tree_vector ();
18664 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18665 vec_safe_push (placement_vec, TREE_VALUE (placement));
18666 }
18667
18668 /* If there was an initializer in the original tree, but it
18669 instantiated to an empty list, then we should pass a
18670 non-NULL empty vector to tell build_new that it was an
18671 empty initializer() rather than no initializer. This can
18672 only happen when the initializer is a pack expansion whose
18673 parameter packs are of length zero. */
18674 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18675 init_vec = NULL;
18676 else
18677 {
18678 init_vec = make_tree_vector ();
18679 if (init == void_node)
18680 gcc_assert (init_vec != NULL);
18681 else
18682 {
18683 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18684 vec_safe_push (init_vec, TREE_VALUE (init));
18685 }
18686 }
18687
18688 /* Avoid passing an enclosing decl to valid_array_size_p. */
18689 in_decl = NULL_TREE;
18690
18691 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18692 tree op2 = RECUR (TREE_OPERAND (t, 2));
18693 ret = build_new (&placement_vec, op1, op2, &init_vec,
18694 NEW_EXPR_USE_GLOBAL (t),
18695 complain);
18696
18697 if (placement_vec != NULL)
18698 release_tree_vector (placement_vec);
18699 if (init_vec != NULL)
18700 release_tree_vector (init_vec);
18701
18702 RETURN (ret);
18703 }
18704
18705 case DELETE_EXPR:
18706 {
18707 tree op0 = RECUR (TREE_OPERAND (t, 0));
18708 tree op1 = RECUR (TREE_OPERAND (t, 1));
18709 RETURN (delete_sanity (op0, op1,
18710 DELETE_EXPR_USE_VEC (t),
18711 DELETE_EXPR_USE_GLOBAL (t),
18712 complain));
18713 }
18714
18715 case COMPOUND_EXPR:
18716 {
18717 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18718 complain & ~tf_decltype, in_decl,
18719 /*function_p=*/false,
18720 integral_constant_expression_p);
18721 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18722 op0,
18723 RECUR (TREE_OPERAND (t, 1)),
18724 complain|decltype_flag));
18725 }
18726
18727 case CALL_EXPR:
18728 {
18729 tree function;
18730 vec<tree, va_gc> *call_args;
18731 unsigned int nargs, i;
18732 bool qualified_p;
18733 bool koenig_p;
18734 tree ret;
18735
18736 function = CALL_EXPR_FN (t);
18737 /* Internal function with no arguments. */
18738 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18739 RETURN (t);
18740
18741 /* When we parsed the expression, we determined whether or
18742 not Koenig lookup should be performed. */
18743 koenig_p = KOENIG_LOOKUP_P (t);
18744 if (function == NULL_TREE)
18745 {
18746 koenig_p = false;
18747 qualified_p = false;
18748 }
18749 else if (TREE_CODE (function) == SCOPE_REF)
18750 {
18751 qualified_p = true;
18752 function = tsubst_qualified_id (function, args, complain, in_decl,
18753 /*done=*/false,
18754 /*address_p=*/false);
18755 }
18756 else if (koenig_p && identifier_p (function))
18757 {
18758 /* Do nothing; calling tsubst_copy_and_build on an identifier
18759 would incorrectly perform unqualified lookup again.
18760
18761 Note that we can also have an IDENTIFIER_NODE if the earlier
18762 unqualified lookup found a member function; in that case
18763 koenig_p will be false and we do want to do the lookup
18764 again to find the instantiated member function.
18765
18766 FIXME but doing that causes c++/15272, so we need to stop
18767 using IDENTIFIER_NODE in that situation. */
18768 qualified_p = false;
18769 }
18770 else
18771 {
18772 if (TREE_CODE (function) == COMPONENT_REF)
18773 {
18774 tree op = TREE_OPERAND (function, 1);
18775
18776 qualified_p = (TREE_CODE (op) == SCOPE_REF
18777 || (BASELINK_P (op)
18778 && BASELINK_QUALIFIED_P (op)));
18779 }
18780 else
18781 qualified_p = false;
18782
18783 if (TREE_CODE (function) == ADDR_EXPR
18784 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18785 /* Avoid error about taking the address of a constructor. */
18786 function = TREE_OPERAND (function, 0);
18787
18788 function = tsubst_copy_and_build (function, args, complain,
18789 in_decl,
18790 !qualified_p,
18791 integral_constant_expression_p);
18792
18793 if (BASELINK_P (function))
18794 qualified_p = true;
18795 }
18796
18797 nargs = call_expr_nargs (t);
18798 call_args = make_tree_vector ();
18799 for (i = 0; i < nargs; ++i)
18800 {
18801 tree arg = CALL_EXPR_ARG (t, i);
18802
18803 if (!PACK_EXPANSION_P (arg))
18804 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18805 else
18806 {
18807 /* Expand the pack expansion and push each entry onto
18808 CALL_ARGS. */
18809 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18810 if (TREE_CODE (arg) == TREE_VEC)
18811 {
18812 unsigned int len, j;
18813
18814 len = TREE_VEC_LENGTH (arg);
18815 for (j = 0; j < len; ++j)
18816 {
18817 tree value = TREE_VEC_ELT (arg, j);
18818 if (value != NULL_TREE)
18819 value = convert_from_reference (value);
18820 vec_safe_push (call_args, value);
18821 }
18822 }
18823 else
18824 {
18825 /* A partial substitution. Add one entry. */
18826 vec_safe_push (call_args, arg);
18827 }
18828 }
18829 }
18830
18831 /* Stripped-down processing for a call in a thunk. Specifically, in
18832 the thunk template for a generic lambda. */
18833 if (CALL_FROM_THUNK_P (t))
18834 {
18835 tree thisarg = NULL_TREE;
18836 if (TREE_CODE (function) == COMPONENT_REF)
18837 {
18838 thisarg = TREE_OPERAND (function, 0);
18839 if (TREE_CODE (thisarg) == INDIRECT_REF)
18840 thisarg = TREE_OPERAND (thisarg, 0);
18841 function = TREE_OPERAND (function, 1);
18842 if (TREE_CODE (function) == BASELINK)
18843 function = BASELINK_FUNCTIONS (function);
18844 }
18845 /* We aren't going to do normal overload resolution, so force the
18846 template-id to resolve. */
18847 function = resolve_nondeduced_context (function, complain);
18848 for (unsigned i = 0; i < nargs; ++i)
18849 {
18850 /* In a thunk, pass through args directly, without any
18851 conversions. */
18852 tree arg = (*call_args)[i];
18853 while (TREE_CODE (arg) != PARM_DECL)
18854 arg = TREE_OPERAND (arg, 0);
18855 (*call_args)[i] = arg;
18856 }
18857 if (thisarg)
18858 {
18859 /* Shift the other args over to make room. */
18860 vec_safe_push (call_args, (*call_args)[nargs-1]);
18861 for (int i = nargs-1; i > 0; --i)
18862 (*call_args)[i] = (*call_args)[i-1];
18863 (*call_args)[0] = thisarg;
18864 }
18865 ret = build_call_a (function, call_args->length (),
18866 call_args->address ());
18867 /* The thunk location is not interesting. */
18868 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
18869 CALL_FROM_THUNK_P (ret) = true;
18870 if (CLASS_TYPE_P (TREE_TYPE (ret)))
18871 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
18872
18873 release_tree_vector (call_args);
18874 RETURN (ret);
18875 }
18876
18877 /* We do not perform argument-dependent lookup if normal
18878 lookup finds a non-function, in accordance with the
18879 expected resolution of DR 218. */
18880 if (koenig_p
18881 && ((is_overloaded_fn (function)
18882 /* If lookup found a member function, the Koenig lookup is
18883 not appropriate, even if an unqualified-name was used
18884 to denote the function. */
18885 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18886 || identifier_p (function))
18887 /* Only do this when substitution turns a dependent call
18888 into a non-dependent call. */
18889 && type_dependent_expression_p_push (t)
18890 && !any_type_dependent_arguments_p (call_args))
18891 function = perform_koenig_lookup (function, call_args, tf_none);
18892
18893 if (function != NULL_TREE
18894 && identifier_p (function)
18895 && !any_type_dependent_arguments_p (call_args))
18896 {
18897 if (koenig_p && (complain & tf_warning_or_error))
18898 {
18899 /* For backwards compatibility and good diagnostics, try
18900 the unqualified lookup again if we aren't in SFINAE
18901 context. */
18902 tree unq = (tsubst_copy_and_build
18903 (function, args, complain, in_decl, true,
18904 integral_constant_expression_p));
18905 if (unq == error_mark_node)
18906 {
18907 release_tree_vector (call_args);
18908 RETURN (error_mark_node);
18909 }
18910
18911 if (unq != function)
18912 {
18913 /* In a lambda fn, we have to be careful to not
18914 introduce new this captures. Legacy code can't
18915 be using lambdas anyway, so it's ok to be
18916 stricter. */
18917 bool in_lambda = (current_class_type
18918 && LAMBDA_TYPE_P (current_class_type));
18919 char const *const msg
18920 = G_("%qD was not declared in this scope, "
18921 "and no declarations were found by "
18922 "argument-dependent lookup at the point "
18923 "of instantiation");
18924
18925 bool diag = true;
18926 if (in_lambda)
18927 error_at (cp_expr_loc_or_loc (t, input_location),
18928 msg, function);
18929 else
18930 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18931 msg, function);
18932 if (diag)
18933 {
18934 tree fn = unq;
18935
18936 if (INDIRECT_REF_P (fn))
18937 fn = TREE_OPERAND (fn, 0);
18938 if (is_overloaded_fn (fn))
18939 fn = get_first_fn (fn);
18940
18941 if (!DECL_P (fn))
18942 /* Can't say anything more. */;
18943 else if (DECL_CLASS_SCOPE_P (fn))
18944 {
18945 location_t loc = cp_expr_loc_or_loc (t,
18946 input_location);
18947 inform (loc,
18948 "declarations in dependent base %qT are "
18949 "not found by unqualified lookup",
18950 DECL_CLASS_CONTEXT (fn));
18951 if (current_class_ptr)
18952 inform (loc,
18953 "use %<this->%D%> instead", function);
18954 else
18955 inform (loc,
18956 "use %<%T::%D%> instead",
18957 current_class_name, function);
18958 }
18959 else
18960 inform (DECL_SOURCE_LOCATION (fn),
18961 "%qD declared here, later in the "
18962 "translation unit", fn);
18963 if (in_lambda)
18964 {
18965 release_tree_vector (call_args);
18966 RETURN (error_mark_node);
18967 }
18968 }
18969
18970 function = unq;
18971 }
18972 }
18973 if (identifier_p (function))
18974 {
18975 if (complain & tf_error)
18976 unqualified_name_lookup_error (function);
18977 release_tree_vector (call_args);
18978 RETURN (error_mark_node);
18979 }
18980 }
18981
18982 /* Remember that there was a reference to this entity. */
18983 if (function != NULL_TREE
18984 && DECL_P (function)
18985 && !mark_used (function, complain) && !(complain & tf_error))
18986 {
18987 release_tree_vector (call_args);
18988 RETURN (error_mark_node);
18989 }
18990
18991 /* Put back tf_decltype for the actual call. */
18992 complain |= decltype_flag;
18993
18994 if (function == NULL_TREE)
18995 switch (CALL_EXPR_IFN (t))
18996 {
18997 case IFN_LAUNDER:
18998 gcc_assert (nargs == 1);
18999 if (vec_safe_length (call_args) != 1)
19000 {
19001 error_at (cp_expr_loc_or_loc (t, input_location),
19002 "wrong number of arguments to "
19003 "%<__builtin_launder%>");
19004 ret = error_mark_node;
19005 }
19006 else
19007 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
19008 input_location),
19009 (*call_args)[0], complain);
19010 break;
19011
19012 case IFN_VEC_CONVERT:
19013 gcc_assert (nargs == 1);
19014 if (vec_safe_length (call_args) != 1)
19015 {
19016 error_at (cp_expr_loc_or_loc (t, input_location),
19017 "wrong number of arguments to "
19018 "%<__builtin_convertvector%>");
19019 ret = error_mark_node;
19020 break;
19021 }
19022 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19023 tsubst (TREE_TYPE (t), args,
19024 complain, in_decl),
19025 complain);
19026 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19027 {
19028 release_tree_vector (call_args);
19029 RETURN (ret);
19030 }
19031 break;
19032
19033 default:
19034 /* Unsupported internal function with arguments. */
19035 gcc_unreachable ();
19036 }
19037 else if (TREE_CODE (function) == OFFSET_REF
19038 || TREE_CODE (function) == DOTSTAR_EXPR
19039 || TREE_CODE (function) == MEMBER_REF)
19040 ret = build_offset_ref_call_from_tree (function, &call_args,
19041 complain);
19042 else if (TREE_CODE (function) == COMPONENT_REF)
19043 {
19044 tree instance = TREE_OPERAND (function, 0);
19045 tree fn = TREE_OPERAND (function, 1);
19046
19047 if (processing_template_decl
19048 && (type_dependent_expression_p (instance)
19049 || (!BASELINK_P (fn)
19050 && TREE_CODE (fn) != FIELD_DECL)
19051 || type_dependent_expression_p (fn)
19052 || any_type_dependent_arguments_p (call_args)))
19053 ret = build_min_nt_call_vec (function, call_args);
19054 else if (!BASELINK_P (fn))
19055 ret = finish_call_expr (function, &call_args,
19056 /*disallow_virtual=*/false,
19057 /*koenig_p=*/false,
19058 complain);
19059 else
19060 ret = (build_new_method_call
19061 (instance, fn,
19062 &call_args, NULL_TREE,
19063 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19064 /*fn_p=*/NULL,
19065 complain));
19066 }
19067 else
19068 ret = finish_call_expr (function, &call_args,
19069 /*disallow_virtual=*/qualified_p,
19070 koenig_p,
19071 complain);
19072
19073 release_tree_vector (call_args);
19074
19075 if (ret != error_mark_node)
19076 {
19077 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19078 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19079 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19080 if (op || ord || rev)
19081 {
19082 function = extract_call_expr (ret);
19083 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19084 CALL_EXPR_ORDERED_ARGS (function) = ord;
19085 CALL_EXPR_REVERSE_ARGS (function) = rev;
19086 }
19087 }
19088
19089 RETURN (ret);
19090 }
19091
19092 case COND_EXPR:
19093 {
19094 tree cond = RECUR (TREE_OPERAND (t, 0));
19095 cond = mark_rvalue_use (cond);
19096 tree folded_cond = fold_non_dependent_expr (cond, complain);
19097 tree exp1, exp2;
19098
19099 if (TREE_CODE (folded_cond) == INTEGER_CST)
19100 {
19101 if (integer_zerop (folded_cond))
19102 {
19103 ++c_inhibit_evaluation_warnings;
19104 exp1 = RECUR (TREE_OPERAND (t, 1));
19105 --c_inhibit_evaluation_warnings;
19106 exp2 = RECUR (TREE_OPERAND (t, 2));
19107 }
19108 else
19109 {
19110 exp1 = RECUR (TREE_OPERAND (t, 1));
19111 ++c_inhibit_evaluation_warnings;
19112 exp2 = RECUR (TREE_OPERAND (t, 2));
19113 --c_inhibit_evaluation_warnings;
19114 }
19115 cond = folded_cond;
19116 }
19117 else
19118 {
19119 exp1 = RECUR (TREE_OPERAND (t, 1));
19120 exp2 = RECUR (TREE_OPERAND (t, 2));
19121 }
19122
19123 warning_sentinel s(warn_duplicated_branches);
19124 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19125 cond, exp1, exp2, complain));
19126 }
19127
19128 case PSEUDO_DTOR_EXPR:
19129 {
19130 tree op0 = RECUR (TREE_OPERAND (t, 0));
19131 tree op1 = RECUR (TREE_OPERAND (t, 1));
19132 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19133 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19134 input_location));
19135 }
19136
19137 case TREE_LIST:
19138 {
19139 tree purpose, value, chain;
19140
19141 if (t == void_list_node)
19142 RETURN (t);
19143
19144 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19145 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19146 {
19147 /* We have pack expansions, so expand those and
19148 create a new list out of it. */
19149 tree purposevec = NULL_TREE;
19150 tree valuevec = NULL_TREE;
19151 tree chain;
19152 int i, len = -1;
19153
19154 /* Expand the argument expressions. */
19155 if (TREE_PURPOSE (t))
19156 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19157 complain, in_decl);
19158 if (TREE_VALUE (t))
19159 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19160 complain, in_decl);
19161
19162 /* Build the rest of the list. */
19163 chain = TREE_CHAIN (t);
19164 if (chain && chain != void_type_node)
19165 chain = RECUR (chain);
19166
19167 /* Determine the number of arguments. */
19168 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19169 {
19170 len = TREE_VEC_LENGTH (purposevec);
19171 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19172 }
19173 else if (TREE_CODE (valuevec) == TREE_VEC)
19174 len = TREE_VEC_LENGTH (valuevec);
19175 else
19176 {
19177 /* Since we only performed a partial substitution into
19178 the argument pack, we only RETURN (a single list
19179 node. */
19180 if (purposevec == TREE_PURPOSE (t)
19181 && valuevec == TREE_VALUE (t)
19182 && chain == TREE_CHAIN (t))
19183 RETURN (t);
19184
19185 RETURN (tree_cons (purposevec, valuevec, chain));
19186 }
19187
19188 /* Convert the argument vectors into a TREE_LIST */
19189 i = len;
19190 while (i > 0)
19191 {
19192 /* Grab the Ith values. */
19193 i--;
19194 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19195 : NULL_TREE;
19196 value
19197 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19198 : NULL_TREE;
19199
19200 /* Build the list (backwards). */
19201 chain = tree_cons (purpose, value, chain);
19202 }
19203
19204 RETURN (chain);
19205 }
19206
19207 purpose = TREE_PURPOSE (t);
19208 if (purpose)
19209 purpose = RECUR (purpose);
19210 value = TREE_VALUE (t);
19211 if (value)
19212 value = RECUR (value);
19213 chain = TREE_CHAIN (t);
19214 if (chain && chain != void_type_node)
19215 chain = RECUR (chain);
19216 if (purpose == TREE_PURPOSE (t)
19217 && value == TREE_VALUE (t)
19218 && chain == TREE_CHAIN (t))
19219 RETURN (t);
19220 RETURN (tree_cons (purpose, value, chain));
19221 }
19222
19223 case COMPONENT_REF:
19224 {
19225 tree object;
19226 tree object_type;
19227 tree member;
19228 tree r;
19229
19230 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19231 args, complain, in_decl);
19232 /* Remember that there was a reference to this entity. */
19233 if (DECL_P (object)
19234 && !mark_used (object, complain) && !(complain & tf_error))
19235 RETURN (error_mark_node);
19236 object_type = TREE_TYPE (object);
19237
19238 member = TREE_OPERAND (t, 1);
19239 if (BASELINK_P (member))
19240 member = tsubst_baselink (member,
19241 non_reference (TREE_TYPE (object)),
19242 args, complain, in_decl);
19243 else
19244 member = tsubst_copy (member, args, complain, in_decl);
19245 if (member == error_mark_node)
19246 RETURN (error_mark_node);
19247
19248 if (TREE_CODE (member) == FIELD_DECL)
19249 {
19250 r = finish_non_static_data_member (member, object, NULL_TREE);
19251 if (TREE_CODE (r) == COMPONENT_REF)
19252 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19253 RETURN (r);
19254 }
19255 else if (type_dependent_expression_p (object))
19256 /* We can't do much here. */;
19257 else if (!CLASS_TYPE_P (object_type))
19258 {
19259 if (scalarish_type_p (object_type))
19260 {
19261 tree s = NULL_TREE;
19262 tree dtor = member;
19263
19264 if (TREE_CODE (dtor) == SCOPE_REF)
19265 {
19266 s = TREE_OPERAND (dtor, 0);
19267 dtor = TREE_OPERAND (dtor, 1);
19268 }
19269 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19270 {
19271 dtor = TREE_OPERAND (dtor, 0);
19272 if (TYPE_P (dtor))
19273 RETURN (finish_pseudo_destructor_expr
19274 (object, s, dtor, input_location));
19275 }
19276 }
19277 }
19278 else if (TREE_CODE (member) == SCOPE_REF
19279 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19280 {
19281 /* Lookup the template functions now that we know what the
19282 scope is. */
19283 tree scope = TREE_OPERAND (member, 0);
19284 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19285 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19286 member = lookup_qualified_name (scope, tmpl,
19287 /*is_type_p=*/false,
19288 /*complain=*/false);
19289 if (BASELINK_P (member))
19290 {
19291 BASELINK_FUNCTIONS (member)
19292 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19293 args);
19294 member = (adjust_result_of_qualified_name_lookup
19295 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19296 object_type));
19297 }
19298 else
19299 {
19300 qualified_name_lookup_error (scope, tmpl, member,
19301 input_location);
19302 RETURN (error_mark_node);
19303 }
19304 }
19305 else if (TREE_CODE (member) == SCOPE_REF
19306 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19307 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19308 {
19309 if (complain & tf_error)
19310 {
19311 if (TYPE_P (TREE_OPERAND (member, 0)))
19312 error ("%qT is not a class or namespace",
19313 TREE_OPERAND (member, 0));
19314 else
19315 error ("%qD is not a class or namespace",
19316 TREE_OPERAND (member, 0));
19317 }
19318 RETURN (error_mark_node);
19319 }
19320
19321 r = finish_class_member_access_expr (object, member,
19322 /*template_p=*/false,
19323 complain);
19324 if (TREE_CODE (r) == COMPONENT_REF)
19325 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19326 RETURN (r);
19327 }
19328
19329 case THROW_EXPR:
19330 RETURN (build_throw
19331 (RECUR (TREE_OPERAND (t, 0))));
19332
19333 case CONSTRUCTOR:
19334 {
19335 vec<constructor_elt, va_gc> *n;
19336 constructor_elt *ce;
19337 unsigned HOST_WIDE_INT idx;
19338 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19339 bool process_index_p;
19340 int newlen;
19341 bool need_copy_p = false;
19342 tree r;
19343
19344 if (type == error_mark_node)
19345 RETURN (error_mark_node);
19346
19347 /* We do not want to process the index of aggregate
19348 initializers as they are identifier nodes which will be
19349 looked up by digest_init. */
19350 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19351
19352 if (null_member_pointer_value_p (t))
19353 {
19354 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19355 RETURN (t);
19356 }
19357
19358 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19359 newlen = vec_safe_length (n);
19360 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19361 {
19362 if (ce->index && process_index_p
19363 /* An identifier index is looked up in the type
19364 being initialized, not the current scope. */
19365 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19366 ce->index = RECUR (ce->index);
19367
19368 if (PACK_EXPANSION_P (ce->value))
19369 {
19370 /* Substitute into the pack expansion. */
19371 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19372 in_decl);
19373
19374 if (ce->value == error_mark_node
19375 || PACK_EXPANSION_P (ce->value))
19376 ;
19377 else if (TREE_VEC_LENGTH (ce->value) == 1)
19378 /* Just move the argument into place. */
19379 ce->value = TREE_VEC_ELT (ce->value, 0);
19380 else
19381 {
19382 /* Update the length of the final CONSTRUCTOR
19383 arguments vector, and note that we will need to
19384 copy.*/
19385 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19386 need_copy_p = true;
19387 }
19388 }
19389 else
19390 ce->value = RECUR (ce->value);
19391 }
19392
19393 if (need_copy_p)
19394 {
19395 vec<constructor_elt, va_gc> *old_n = n;
19396
19397 vec_alloc (n, newlen);
19398 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19399 {
19400 if (TREE_CODE (ce->value) == TREE_VEC)
19401 {
19402 int i, len = TREE_VEC_LENGTH (ce->value);
19403 for (i = 0; i < len; ++i)
19404 CONSTRUCTOR_APPEND_ELT (n, 0,
19405 TREE_VEC_ELT (ce->value, i));
19406 }
19407 else
19408 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19409 }
19410 }
19411
19412 r = build_constructor (init_list_type_node, n);
19413 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19414 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
19415 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
19416
19417 if (TREE_HAS_CONSTRUCTOR (t))
19418 {
19419 fcl_t cl = fcl_functional;
19420 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
19421 cl = fcl_c99;
19422 RETURN (finish_compound_literal (type, r, complain, cl));
19423 }
19424
19425 TREE_TYPE (r) = type;
19426 RETURN (r);
19427 }
19428
19429 case TYPEID_EXPR:
19430 {
19431 tree operand_0 = TREE_OPERAND (t, 0);
19432 if (TYPE_P (operand_0))
19433 {
19434 operand_0 = tsubst (operand_0, args, complain, in_decl);
19435 RETURN (get_typeid (operand_0, complain));
19436 }
19437 else
19438 {
19439 operand_0 = RECUR (operand_0);
19440 RETURN (build_typeid (operand_0, complain));
19441 }
19442 }
19443
19444 case VAR_DECL:
19445 if (!args)
19446 RETURN (t);
19447 /* Fall through */
19448
19449 case PARM_DECL:
19450 {
19451 tree r = tsubst_copy (t, args, complain, in_decl);
19452 /* ??? We're doing a subset of finish_id_expression here. */
19453 if (tree wrap = maybe_get_tls_wrapper_call (r))
19454 /* Replace an evaluated use of the thread_local variable with
19455 a call to its wrapper. */
19456 r = wrap;
19457 else if (outer_automatic_var_p (r))
19458 r = process_outer_var_ref (r, complain);
19459
19460 if (!TYPE_REF_P (TREE_TYPE (t)))
19461 /* If the original type was a reference, we'll be wrapped in
19462 the appropriate INDIRECT_REF. */
19463 r = convert_from_reference (r);
19464 RETURN (r);
19465 }
19466
19467 case VA_ARG_EXPR:
19468 {
19469 tree op0 = RECUR (TREE_OPERAND (t, 0));
19470 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19471 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19472 }
19473
19474 case OFFSETOF_EXPR:
19475 {
19476 tree object_ptr
19477 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19478 in_decl, /*function_p=*/false,
19479 /*integral_constant_expression_p=*/false);
19480 RETURN (finish_offsetof (object_ptr,
19481 RECUR (TREE_OPERAND (t, 0)),
19482 EXPR_LOCATION (t)));
19483 }
19484
19485 case ADDRESSOF_EXPR:
19486 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19487 RECUR (TREE_OPERAND (t, 0)), complain));
19488
19489 case TRAIT_EXPR:
19490 {
19491 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19492 complain, in_decl);
19493
19494 tree type2 = TRAIT_EXPR_TYPE2 (t);
19495 if (type2 && TREE_CODE (type2) == TREE_LIST)
19496 type2 = RECUR (type2);
19497 else if (type2)
19498 type2 = tsubst (type2, args, complain, in_decl);
19499
19500 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
19501 }
19502
19503 case STMT_EXPR:
19504 {
19505 tree old_stmt_expr = cur_stmt_expr;
19506 tree stmt_expr = begin_stmt_expr ();
19507
19508 cur_stmt_expr = stmt_expr;
19509 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19510 integral_constant_expression_p);
19511 stmt_expr = finish_stmt_expr (stmt_expr, false);
19512 cur_stmt_expr = old_stmt_expr;
19513
19514 /* If the resulting list of expression statement is empty,
19515 fold it further into void_node. */
19516 if (empty_expr_stmt_p (stmt_expr))
19517 stmt_expr = void_node;
19518
19519 RETURN (stmt_expr);
19520 }
19521
19522 case LAMBDA_EXPR:
19523 {
19524 if (complain & tf_partial)
19525 {
19526 /* We don't have a full set of template arguments yet; don't touch
19527 the lambda at all. */
19528 gcc_assert (processing_template_decl);
19529 return t;
19530 }
19531 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19532
19533 RETURN (build_lambda_object (r));
19534 }
19535
19536 case TARGET_EXPR:
19537 /* We can get here for a constant initializer of non-dependent type.
19538 FIXME stop folding in cp_parser_initializer_clause. */
19539 {
19540 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19541 complain);
19542 RETURN (r);
19543 }
19544
19545 case TRANSACTION_EXPR:
19546 RETURN (tsubst_expr(t, args, complain, in_decl,
19547 integral_constant_expression_p));
19548
19549 case PAREN_EXPR:
19550 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19551
19552 case VEC_PERM_EXPR:
19553 {
19554 tree op0 = RECUR (TREE_OPERAND (t, 0));
19555 tree op1 = RECUR (TREE_OPERAND (t, 1));
19556 tree op2 = RECUR (TREE_OPERAND (t, 2));
19557 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19558 complain));
19559 }
19560
19561 case REQUIRES_EXPR:
19562 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19563
19564 case RANGE_EXPR:
19565 /* No need to substitute further, a RANGE_EXPR will always be built
19566 with constant operands. */
19567 RETURN (t);
19568
19569 case NON_LVALUE_EXPR:
19570 case VIEW_CONVERT_EXPR:
19571 if (location_wrapper_p (t))
19572 /* We need to do this here as well as in tsubst_copy so we get the
19573 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19574 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19575 EXPR_LOCATION (t)));
19576 /* fallthrough. */
19577
19578 default:
19579 /* Handle Objective-C++ constructs, if appropriate. */
19580 {
19581 tree subst
19582 = objcp_tsubst_copy_and_build (t, args, complain,
19583 in_decl, /*function_p=*/false);
19584 if (subst)
19585 RETURN (subst);
19586 }
19587 RETURN (tsubst_copy (t, args, complain, in_decl));
19588 }
19589
19590 #undef RECUR
19591 #undef RETURN
19592 out:
19593 input_location = loc;
19594 return retval;
19595 }
19596
19597 /* Verify that the instantiated ARGS are valid. For type arguments,
19598 make sure that the type's linkage is ok. For non-type arguments,
19599 make sure they are constants if they are integral or enumerations.
19600 Emit an error under control of COMPLAIN, and return TRUE on error. */
19601
19602 static bool
19603 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19604 {
19605 if (dependent_template_arg_p (t))
19606 return false;
19607 if (ARGUMENT_PACK_P (t))
19608 {
19609 tree vec = ARGUMENT_PACK_ARGS (t);
19610 int len = TREE_VEC_LENGTH (vec);
19611 bool result = false;
19612 int i;
19613
19614 for (i = 0; i < len; ++i)
19615 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19616 result = true;
19617 return result;
19618 }
19619 else if (TYPE_P (t))
19620 {
19621 /* [basic.link]: A name with no linkage (notably, the name
19622 of a class or enumeration declared in a local scope)
19623 shall not be used to declare an entity with linkage.
19624 This implies that names with no linkage cannot be used as
19625 template arguments
19626
19627 DR 757 relaxes this restriction for C++0x. */
19628 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19629 : no_linkage_check (t, /*relaxed_p=*/false));
19630
19631 if (nt)
19632 {
19633 /* DR 488 makes use of a type with no linkage cause
19634 type deduction to fail. */
19635 if (complain & tf_error)
19636 {
19637 if (TYPE_UNNAMED_P (nt))
19638 error ("%qT is/uses unnamed type", t);
19639 else
19640 error ("template argument for %qD uses local type %qT",
19641 tmpl, t);
19642 }
19643 return true;
19644 }
19645 /* In order to avoid all sorts of complications, we do not
19646 allow variably-modified types as template arguments. */
19647 else if (variably_modified_type_p (t, NULL_TREE))
19648 {
19649 if (complain & tf_error)
19650 error ("%qT is a variably modified type", t);
19651 return true;
19652 }
19653 }
19654 /* Class template and alias template arguments should be OK. */
19655 else if (DECL_TYPE_TEMPLATE_P (t))
19656 ;
19657 /* A non-type argument of integral or enumerated type must be a
19658 constant. */
19659 else if (TREE_TYPE (t)
19660 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19661 && !REFERENCE_REF_P (t)
19662 && !TREE_CONSTANT (t))
19663 {
19664 if (complain & tf_error)
19665 error ("integral expression %qE is not constant", t);
19666 return true;
19667 }
19668 return false;
19669 }
19670
19671 static bool
19672 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19673 {
19674 int ix, len = DECL_NTPARMS (tmpl);
19675 bool result = false;
19676
19677 for (ix = 0; ix != len; ix++)
19678 {
19679 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19680 result = true;
19681 }
19682 if (result && (complain & tf_error))
19683 error (" trying to instantiate %qD", tmpl);
19684 return result;
19685 }
19686
19687 /* We're out of SFINAE context now, so generate diagnostics for the access
19688 errors we saw earlier when instantiating D from TMPL and ARGS. */
19689
19690 static void
19691 recheck_decl_substitution (tree d, tree tmpl, tree args)
19692 {
19693 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19694 tree type = TREE_TYPE (pattern);
19695 location_t loc = input_location;
19696
19697 push_access_scope (d);
19698 push_deferring_access_checks (dk_no_deferred);
19699 input_location = DECL_SOURCE_LOCATION (pattern);
19700 tsubst (type, args, tf_warning_or_error, d);
19701 input_location = loc;
19702 pop_deferring_access_checks ();
19703 pop_access_scope (d);
19704 }
19705
19706 /* Instantiate the indicated variable, function, or alias template TMPL with
19707 the template arguments in TARG_PTR. */
19708
19709 static tree
19710 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19711 {
19712 tree targ_ptr = orig_args;
19713 tree fndecl;
19714 tree gen_tmpl;
19715 tree spec;
19716 bool access_ok = true;
19717
19718 if (tmpl == error_mark_node)
19719 return error_mark_node;
19720
19721 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19722
19723 /* If this function is a clone, handle it specially. */
19724 if (DECL_CLONED_FUNCTION_P (tmpl))
19725 {
19726 tree spec;
19727 tree clone;
19728
19729 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19730 DECL_CLONED_FUNCTION. */
19731 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19732 targ_ptr, complain);
19733 if (spec == error_mark_node)
19734 return error_mark_node;
19735
19736 /* Look for the clone. */
19737 FOR_EACH_CLONE (clone, spec)
19738 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19739 return clone;
19740 /* We should always have found the clone by now. */
19741 gcc_unreachable ();
19742 return NULL_TREE;
19743 }
19744
19745 if (targ_ptr == error_mark_node)
19746 return error_mark_node;
19747
19748 /* Check to see if we already have this specialization. */
19749 gen_tmpl = most_general_template (tmpl);
19750 if (TMPL_ARGS_DEPTH (targ_ptr)
19751 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19752 /* targ_ptr only has the innermost template args, so add the outer ones
19753 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19754 the case of a non-dependent call within a template definition). */
19755 targ_ptr = (add_outermost_template_args
19756 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19757 targ_ptr));
19758
19759 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19760 but it doesn't seem to be on the hot path. */
19761 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19762
19763 gcc_assert (tmpl == gen_tmpl
19764 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19765 == spec)
19766 || fndecl == NULL_TREE);
19767
19768 if (spec != NULL_TREE)
19769 {
19770 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19771 {
19772 if (complain & tf_error)
19773 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19774 return error_mark_node;
19775 }
19776 return spec;
19777 }
19778
19779 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19780 complain))
19781 return error_mark_node;
19782
19783 /* We are building a FUNCTION_DECL, during which the access of its
19784 parameters and return types have to be checked. However this
19785 FUNCTION_DECL which is the desired context for access checking
19786 is not built yet. We solve this chicken-and-egg problem by
19787 deferring all checks until we have the FUNCTION_DECL. */
19788 push_deferring_access_checks (dk_deferred);
19789
19790 /* Instantiation of the function happens in the context of the function
19791 template, not the context of the overload resolution we're doing. */
19792 push_to_top_level ();
19793 /* If there are dependent arguments, e.g. because we're doing partial
19794 ordering, make sure processing_template_decl stays set. */
19795 if (uses_template_parms (targ_ptr))
19796 ++processing_template_decl;
19797 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19798 {
19799 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19800 complain, gen_tmpl, true);
19801 push_nested_class (ctx);
19802 }
19803
19804 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19805
19806 fndecl = NULL_TREE;
19807 if (VAR_P (pattern))
19808 {
19809 /* We need to determine if we're using a partial or explicit
19810 specialization now, because the type of the variable could be
19811 different. */
19812 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19813 tree elt = most_specialized_partial_spec (tid, complain);
19814 if (elt == error_mark_node)
19815 pattern = error_mark_node;
19816 else if (elt)
19817 {
19818 tree partial_tmpl = TREE_VALUE (elt);
19819 tree partial_args = TREE_PURPOSE (elt);
19820 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19821 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19822 }
19823 }
19824
19825 /* Substitute template parameters to obtain the specialization. */
19826 if (fndecl == NULL_TREE)
19827 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19828 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19829 pop_nested_class ();
19830 pop_from_top_level ();
19831
19832 if (fndecl == error_mark_node)
19833 {
19834 pop_deferring_access_checks ();
19835 return error_mark_node;
19836 }
19837
19838 /* The DECL_TI_TEMPLATE should always be the immediate parent
19839 template, not the most general template. */
19840 DECL_TI_TEMPLATE (fndecl) = tmpl;
19841 DECL_TI_ARGS (fndecl) = targ_ptr;
19842
19843 /* Now we know the specialization, compute access previously
19844 deferred. Do no access control for inheriting constructors,
19845 as we already checked access for the inherited constructor. */
19846 if (!(flag_new_inheriting_ctors
19847 && DECL_INHERITED_CTOR (fndecl)))
19848 {
19849 push_access_scope (fndecl);
19850 if (!perform_deferred_access_checks (complain))
19851 access_ok = false;
19852 pop_access_scope (fndecl);
19853 }
19854 pop_deferring_access_checks ();
19855
19856 /* If we've just instantiated the main entry point for a function,
19857 instantiate all the alternate entry points as well. We do this
19858 by cloning the instantiation of the main entry point, not by
19859 instantiating the template clones. */
19860 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19861 clone_function_decl (fndecl, /*update_methods=*/false);
19862
19863 if (!access_ok)
19864 {
19865 if (!(complain & tf_error))
19866 {
19867 /* Remember to reinstantiate when we're out of SFINAE so the user
19868 can see the errors. */
19869 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19870 }
19871 return error_mark_node;
19872 }
19873 return fndecl;
19874 }
19875
19876 /* Wrapper for instantiate_template_1. */
19877
19878 tree
19879 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19880 {
19881 tree ret;
19882 timevar_push (TV_TEMPLATE_INST);
19883 ret = instantiate_template_1 (tmpl, orig_args, complain);
19884 timevar_pop (TV_TEMPLATE_INST);
19885 return ret;
19886 }
19887
19888 /* Instantiate the alias template TMPL with ARGS. Also push a template
19889 instantiation level, which instantiate_template doesn't do because
19890 functions and variables have sufficient context established by the
19891 callers. */
19892
19893 static tree
19894 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19895 {
19896 if (tmpl == error_mark_node || args == error_mark_node)
19897 return error_mark_node;
19898 if (!push_tinst_level (tmpl, args))
19899 return error_mark_node;
19900
19901 args =
19902 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19903 args, tmpl, complain,
19904 /*require_all_args=*/true,
19905 /*use_default_args=*/true);
19906
19907 tree r = instantiate_template (tmpl, args, complain);
19908 pop_tinst_level ();
19909
19910 return r;
19911 }
19912
19913 /* PARM is a template parameter pack for FN. Returns true iff
19914 PARM is used in a deducible way in the argument list of FN. */
19915
19916 static bool
19917 pack_deducible_p (tree parm, tree fn)
19918 {
19919 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19920 for (; t; t = TREE_CHAIN (t))
19921 {
19922 tree type = TREE_VALUE (t);
19923 tree packs;
19924 if (!PACK_EXPANSION_P (type))
19925 continue;
19926 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19927 packs; packs = TREE_CHAIN (packs))
19928 if (template_args_equal (TREE_VALUE (packs), parm))
19929 {
19930 /* The template parameter pack is used in a function parameter
19931 pack. If this is the end of the parameter list, the
19932 template parameter pack is deducible. */
19933 if (TREE_CHAIN (t) == void_list_node)
19934 return true;
19935 else
19936 /* Otherwise, not. Well, it could be deduced from
19937 a non-pack parameter, but doing so would end up with
19938 a deduction mismatch, so don't bother. */
19939 return false;
19940 }
19941 }
19942 /* The template parameter pack isn't used in any function parameter
19943 packs, but it might be used deeper, e.g. tuple<Args...>. */
19944 return true;
19945 }
19946
19947 /* Subroutine of fn_type_unification: check non-dependent parms for
19948 convertibility. */
19949
19950 static int
19951 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19952 tree fn, unification_kind_t strict, int flags,
19953 struct conversion **convs, bool explain_p)
19954 {
19955 /* Non-constructor methods need to leave a conversion for 'this', which
19956 isn't included in nargs here. */
19957 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19958 && !DECL_CONSTRUCTOR_P (fn));
19959
19960 for (unsigned ia = 0;
19961 parms && parms != void_list_node && ia < nargs; )
19962 {
19963 tree parm = TREE_VALUE (parms);
19964
19965 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19966 && (!TREE_CHAIN (parms)
19967 || TREE_CHAIN (parms) == void_list_node))
19968 /* For a function parameter pack that occurs at the end of the
19969 parameter-declaration-list, the type A of each remaining
19970 argument of the call is compared with the type P of the
19971 declarator-id of the function parameter pack. */
19972 break;
19973
19974 parms = TREE_CHAIN (parms);
19975
19976 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19977 /* For a function parameter pack that does not occur at the
19978 end of the parameter-declaration-list, the type of the
19979 parameter pack is a non-deduced context. */
19980 continue;
19981
19982 if (!uses_template_parms (parm))
19983 {
19984 tree arg = args[ia];
19985 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
19986 int lflags = conv_flags (ia, nargs, fn, arg, flags);
19987
19988 if (check_non_deducible_conversion (parm, arg, strict, lflags,
19989 conv_p, explain_p))
19990 return 1;
19991 }
19992
19993 ++ia;
19994 }
19995
19996 return 0;
19997 }
19998
19999 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20000 NARGS elements of the arguments that are being used when calling
20001 it. TARGS is a vector into which the deduced template arguments
20002 are placed.
20003
20004 Returns either a FUNCTION_DECL for the matching specialization of FN or
20005 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20006 true, diagnostics will be printed to explain why it failed.
20007
20008 If FN is a conversion operator, or we are trying to produce a specific
20009 specialization, RETURN_TYPE is the return type desired.
20010
20011 The EXPLICIT_TARGS are explicit template arguments provided via a
20012 template-id.
20013
20014 The parameter STRICT is one of:
20015
20016 DEDUCE_CALL:
20017 We are deducing arguments for a function call, as in
20018 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20019 deducing arguments for a call to the result of a conversion
20020 function template, as in [over.call.object].
20021
20022 DEDUCE_CONV:
20023 We are deducing arguments for a conversion function, as in
20024 [temp.deduct.conv].
20025
20026 DEDUCE_EXACT:
20027 We are deducing arguments when doing an explicit instantiation
20028 as in [temp.explicit], when determining an explicit specialization
20029 as in [temp.expl.spec], or when taking the address of a function
20030 template, as in [temp.deduct.funcaddr]. */
20031
20032 tree
20033 fn_type_unification (tree fn,
20034 tree explicit_targs,
20035 tree targs,
20036 const tree *args,
20037 unsigned int nargs,
20038 tree return_type,
20039 unification_kind_t strict,
20040 int flags,
20041 struct conversion **convs,
20042 bool explain_p,
20043 bool decltype_p)
20044 {
20045 tree parms;
20046 tree fntype;
20047 tree decl = NULL_TREE;
20048 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20049 bool ok;
20050 static int deduction_depth;
20051 /* type_unification_real will pass back any access checks from default
20052 template argument substitution. */
20053 vec<deferred_access_check, va_gc> *checks = NULL;
20054 /* We don't have all the template args yet. */
20055 bool incomplete = true;
20056
20057 tree orig_fn = fn;
20058 if (flag_new_inheriting_ctors)
20059 fn = strip_inheriting_ctors (fn);
20060
20061 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20062 tree r = error_mark_node;
20063
20064 tree full_targs = targs;
20065 if (TMPL_ARGS_DEPTH (targs)
20066 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20067 full_targs = (add_outermost_template_args
20068 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20069 targs));
20070
20071 if (decltype_p)
20072 complain |= tf_decltype;
20073
20074 /* In C++0x, it's possible to have a function template whose type depends
20075 on itself recursively. This is most obvious with decltype, but can also
20076 occur with enumeration scope (c++/48969). So we need to catch infinite
20077 recursion and reject the substitution at deduction time; this function
20078 will return error_mark_node for any repeated substitution.
20079
20080 This also catches excessive recursion such as when f<N> depends on
20081 f<N-1> across all integers, and returns error_mark_node for all the
20082 substitutions back up to the initial one.
20083
20084 This is, of course, not reentrant. */
20085 if (excessive_deduction_depth)
20086 return error_mark_node;
20087 ++deduction_depth;
20088
20089 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20090
20091 fntype = TREE_TYPE (fn);
20092 if (explicit_targs)
20093 {
20094 /* [temp.deduct]
20095
20096 The specified template arguments must match the template
20097 parameters in kind (i.e., type, nontype, template), and there
20098 must not be more arguments than there are parameters;
20099 otherwise type deduction fails.
20100
20101 Nontype arguments must match the types of the corresponding
20102 nontype template parameters, or must be convertible to the
20103 types of the corresponding nontype parameters as specified in
20104 _temp.arg.nontype_, otherwise type deduction fails.
20105
20106 All references in the function type of the function template
20107 to the corresponding template parameters are replaced by the
20108 specified template argument values. If a substitution in a
20109 template parameter or in the function type of the function
20110 template results in an invalid type, type deduction fails. */
20111 int i, len = TREE_VEC_LENGTH (tparms);
20112 location_t loc = input_location;
20113 incomplete = false;
20114
20115 if (explicit_targs == error_mark_node)
20116 goto fail;
20117
20118 if (TMPL_ARGS_DEPTH (explicit_targs)
20119 < TMPL_ARGS_DEPTH (full_targs))
20120 explicit_targs = add_outermost_template_args (full_targs,
20121 explicit_targs);
20122
20123 /* Adjust any explicit template arguments before entering the
20124 substitution context. */
20125 explicit_targs
20126 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20127 complain|tf_partial,
20128 /*require_all_args=*/false,
20129 /*use_default_args=*/false));
20130 if (explicit_targs == error_mark_node)
20131 goto fail;
20132
20133 /* Substitute the explicit args into the function type. This is
20134 necessary so that, for instance, explicitly declared function
20135 arguments can match null pointed constants. If we were given
20136 an incomplete set of explicit args, we must not do semantic
20137 processing during substitution as we could create partial
20138 instantiations. */
20139 for (i = 0; i < len; i++)
20140 {
20141 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20142 bool parameter_pack = false;
20143 tree targ = TREE_VEC_ELT (explicit_targs, i);
20144
20145 /* Dig out the actual parm. */
20146 if (TREE_CODE (parm) == TYPE_DECL
20147 || TREE_CODE (parm) == TEMPLATE_DECL)
20148 {
20149 parm = TREE_TYPE (parm);
20150 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20151 }
20152 else if (TREE_CODE (parm) == PARM_DECL)
20153 {
20154 parm = DECL_INITIAL (parm);
20155 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20156 }
20157
20158 if (!parameter_pack && targ == NULL_TREE)
20159 /* No explicit argument for this template parameter. */
20160 incomplete = true;
20161
20162 if (parameter_pack && pack_deducible_p (parm, fn))
20163 {
20164 /* Mark the argument pack as "incomplete". We could
20165 still deduce more arguments during unification.
20166 We remove this mark in type_unification_real. */
20167 if (targ)
20168 {
20169 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20170 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20171 = ARGUMENT_PACK_ARGS (targ);
20172 }
20173
20174 /* We have some incomplete argument packs. */
20175 incomplete = true;
20176 }
20177 }
20178
20179 if (incomplete)
20180 {
20181 if (!push_tinst_level (fn, explicit_targs))
20182 {
20183 excessive_deduction_depth = true;
20184 goto fail;
20185 }
20186 ++processing_template_decl;
20187 input_location = DECL_SOURCE_LOCATION (fn);
20188 /* Ignore any access checks; we'll see them again in
20189 instantiate_template and they might have the wrong
20190 access path at this point. */
20191 push_deferring_access_checks (dk_deferred);
20192 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20193 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20194 pop_deferring_access_checks ();
20195 input_location = loc;
20196 --processing_template_decl;
20197 pop_tinst_level ();
20198
20199 if (fntype == error_mark_node)
20200 goto fail;
20201 }
20202
20203 /* Place the explicitly specified arguments in TARGS. */
20204 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20205 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20206 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20207 if (!incomplete && CHECKING_P
20208 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20209 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20210 (targs, NUM_TMPL_ARGS (explicit_targs));
20211 }
20212
20213 if (return_type && strict != DEDUCE_CALL)
20214 {
20215 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20216 new_args[0] = return_type;
20217 memcpy (new_args + 1, args, nargs * sizeof (tree));
20218 args = new_args;
20219 ++nargs;
20220 }
20221
20222 if (!incomplete)
20223 goto deduced;
20224
20225 /* Never do unification on the 'this' parameter. */
20226 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20227
20228 if (return_type && strict == DEDUCE_CALL)
20229 {
20230 /* We're deducing for a call to the result of a template conversion
20231 function. The parms we really want are in return_type. */
20232 if (INDIRECT_TYPE_P (return_type))
20233 return_type = TREE_TYPE (return_type);
20234 parms = TYPE_ARG_TYPES (return_type);
20235 }
20236 else if (return_type)
20237 {
20238 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20239 }
20240
20241 /* We allow incomplete unification without an error message here
20242 because the standard doesn't seem to explicitly prohibit it. Our
20243 callers must be ready to deal with unification failures in any
20244 event. */
20245
20246 /* If we aren't explaining yet, push tinst context so we can see where
20247 any errors (e.g. from class instantiations triggered by instantiation
20248 of default template arguments) come from. If we are explaining, this
20249 context is redundant. */
20250 if (!explain_p && !push_tinst_level (fn, targs))
20251 {
20252 excessive_deduction_depth = true;
20253 goto fail;
20254 }
20255
20256 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20257 full_targs, parms, args, nargs, /*subr=*/0,
20258 strict, &checks, explain_p);
20259 if (!explain_p)
20260 pop_tinst_level ();
20261 if (!ok)
20262 goto fail;
20263
20264 /* Now that we have bindings for all of the template arguments,
20265 ensure that the arguments deduced for the template template
20266 parameters have compatible template parameter lists. We cannot
20267 check this property before we have deduced all template
20268 arguments, because the template parameter types of a template
20269 template parameter might depend on prior template parameters
20270 deduced after the template template parameter. The following
20271 ill-formed example illustrates this issue:
20272
20273 template<typename T, template<T> class C> void f(C<5>, T);
20274
20275 template<int N> struct X {};
20276
20277 void g() {
20278 f(X<5>(), 5l); // error: template argument deduction fails
20279 }
20280
20281 The template parameter list of 'C' depends on the template type
20282 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20283 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20284 time that we deduce 'C'. */
20285 if (!template_template_parm_bindings_ok_p
20286 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20287 {
20288 unify_inconsistent_template_template_parameters (explain_p);
20289 goto fail;
20290 }
20291
20292 /* DR 1391: All parameters have args, now check non-dependent parms for
20293 convertibility. */
20294 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20295 convs, explain_p))
20296 goto fail;
20297
20298 deduced:
20299 /* All is well so far. Now, check:
20300
20301 [temp.deduct]
20302
20303 When all template arguments have been deduced, all uses of
20304 template parameters in nondeduced contexts are replaced with
20305 the corresponding deduced argument values. If the
20306 substitution results in an invalid type, as described above,
20307 type deduction fails. */
20308 if (!push_tinst_level (fn, targs))
20309 {
20310 excessive_deduction_depth = true;
20311 goto fail;
20312 }
20313
20314 /* Also collect access checks from the instantiation. */
20315 reopen_deferring_access_checks (checks);
20316
20317 decl = instantiate_template (fn, targs, complain);
20318
20319 checks = get_deferred_access_checks ();
20320 pop_deferring_access_checks ();
20321
20322 pop_tinst_level ();
20323
20324 if (decl == error_mark_node)
20325 goto fail;
20326
20327 /* Now perform any access checks encountered during substitution. */
20328 push_access_scope (decl);
20329 ok = perform_access_checks (checks, complain);
20330 pop_access_scope (decl);
20331 if (!ok)
20332 goto fail;
20333
20334 /* If we're looking for an exact match, check that what we got
20335 is indeed an exact match. It might not be if some template
20336 parameters are used in non-deduced contexts. But don't check
20337 for an exact match if we have dependent template arguments;
20338 in that case we're doing partial ordering, and we already know
20339 that we have two candidates that will provide the actual type. */
20340 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20341 {
20342 tree substed = TREE_TYPE (decl);
20343 unsigned int i;
20344
20345 tree sarg
20346 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20347 if (return_type)
20348 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20349 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20350 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20351 {
20352 unify_type_mismatch (explain_p, args[i],
20353 TREE_VALUE (sarg));
20354 goto fail;
20355 }
20356 }
20357
20358 /* After doing deduction with the inherited constructor, actually return an
20359 instantiation of the inheriting constructor. */
20360 if (orig_fn != fn)
20361 decl = instantiate_template (orig_fn, targs, complain);
20362
20363 r = decl;
20364
20365 fail:
20366 --deduction_depth;
20367 if (excessive_deduction_depth)
20368 {
20369 if (deduction_depth == 0)
20370 /* Reset once we're all the way out. */
20371 excessive_deduction_depth = false;
20372 }
20373
20374 return r;
20375 }
20376
20377 /* Adjust types before performing type deduction, as described in
20378 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20379 sections are symmetric. PARM is the type of a function parameter
20380 or the return type of the conversion function. ARG is the type of
20381 the argument passed to the call, or the type of the value
20382 initialized with the result of the conversion function.
20383 ARG_EXPR is the original argument expression, which may be null. */
20384
20385 static int
20386 maybe_adjust_types_for_deduction (unification_kind_t strict,
20387 tree* parm,
20388 tree* arg,
20389 tree arg_expr)
20390 {
20391 int result = 0;
20392
20393 switch (strict)
20394 {
20395 case DEDUCE_CALL:
20396 break;
20397
20398 case DEDUCE_CONV:
20399 /* Swap PARM and ARG throughout the remainder of this
20400 function; the handling is precisely symmetric since PARM
20401 will initialize ARG rather than vice versa. */
20402 std::swap (parm, arg);
20403 break;
20404
20405 case DEDUCE_EXACT:
20406 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20407 too, but here handle it by stripping the reference from PARM
20408 rather than by adding it to ARG. */
20409 if (TYPE_REF_P (*parm)
20410 && TYPE_REF_IS_RVALUE (*parm)
20411 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20412 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20413 && TYPE_REF_P (*arg)
20414 && !TYPE_REF_IS_RVALUE (*arg))
20415 *parm = TREE_TYPE (*parm);
20416 /* Nothing else to do in this case. */
20417 return 0;
20418
20419 default:
20420 gcc_unreachable ();
20421 }
20422
20423 if (!TYPE_REF_P (*parm))
20424 {
20425 /* [temp.deduct.call]
20426
20427 If P is not a reference type:
20428
20429 --If A is an array type, the pointer type produced by the
20430 array-to-pointer standard conversion (_conv.array_) is
20431 used in place of A for type deduction; otherwise,
20432
20433 --If A is a function type, the pointer type produced by
20434 the function-to-pointer standard conversion
20435 (_conv.func_) is used in place of A for type deduction;
20436 otherwise,
20437
20438 --If A is a cv-qualified type, the top level
20439 cv-qualifiers of A's type are ignored for type
20440 deduction. */
20441 if (TREE_CODE (*arg) == ARRAY_TYPE)
20442 *arg = build_pointer_type (TREE_TYPE (*arg));
20443 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20444 *arg = build_pointer_type (*arg);
20445 else
20446 *arg = TYPE_MAIN_VARIANT (*arg);
20447 }
20448
20449 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20450 reference to a cv-unqualified template parameter that does not represent a
20451 template parameter of a class template (during class template argument
20452 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20453 an lvalue, the type "lvalue reference to A" is used in place of A for type
20454 deduction. */
20455 if (TYPE_REF_P (*parm)
20456 && TYPE_REF_IS_RVALUE (*parm)
20457 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20458 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20459 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20460 && (arg_expr ? lvalue_p (arg_expr)
20461 /* try_one_overload doesn't provide an arg_expr, but
20462 functions are always lvalues. */
20463 : TREE_CODE (*arg) == FUNCTION_TYPE))
20464 *arg = build_reference_type (*arg);
20465
20466 /* [temp.deduct.call]
20467
20468 If P is a cv-qualified type, the top level cv-qualifiers
20469 of P's type are ignored for type deduction. If P is a
20470 reference type, the type referred to by P is used for
20471 type deduction. */
20472 *parm = TYPE_MAIN_VARIANT (*parm);
20473 if (TYPE_REF_P (*parm))
20474 {
20475 *parm = TREE_TYPE (*parm);
20476 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20477 }
20478
20479 /* DR 322. For conversion deduction, remove a reference type on parm
20480 too (which has been swapped into ARG). */
20481 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20482 *arg = TREE_TYPE (*arg);
20483
20484 return result;
20485 }
20486
20487 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20488 template which doesn't contain any deducible template parameters; check if
20489 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20490 unify_one_argument. */
20491
20492 static int
20493 check_non_deducible_conversion (tree parm, tree arg, int strict,
20494 int flags, struct conversion **conv_p,
20495 bool explain_p)
20496 {
20497 tree type;
20498
20499 if (!TYPE_P (arg))
20500 type = TREE_TYPE (arg);
20501 else
20502 type = arg;
20503
20504 if (same_type_p (parm, type))
20505 return unify_success (explain_p);
20506
20507 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20508 if (strict == DEDUCE_CONV)
20509 {
20510 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20511 return unify_success (explain_p);
20512 }
20513 else if (strict != DEDUCE_EXACT)
20514 {
20515 bool ok = false;
20516 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20517 if (conv_p)
20518 /* Avoid recalculating this in add_function_candidate. */
20519 ok = (*conv_p
20520 = good_conversion (parm, type, conv_arg, flags, complain));
20521 else
20522 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20523 if (ok)
20524 return unify_success (explain_p);
20525 }
20526
20527 if (strict == DEDUCE_EXACT)
20528 return unify_type_mismatch (explain_p, parm, arg);
20529 else
20530 return unify_arg_conversion (explain_p, parm, type, arg);
20531 }
20532
20533 static bool uses_deducible_template_parms (tree type);
20534
20535 /* Returns true iff the expression EXPR is one from which a template
20536 argument can be deduced. In other words, if it's an undecorated
20537 use of a template non-type parameter. */
20538
20539 static bool
20540 deducible_expression (tree expr)
20541 {
20542 /* Strip implicit conversions. */
20543 while (CONVERT_EXPR_P (expr))
20544 expr = TREE_OPERAND (expr, 0);
20545 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20546 }
20547
20548 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20549 deducible way; that is, if it has a max value of <PARM> - 1. */
20550
20551 static bool
20552 deducible_array_bound (tree domain)
20553 {
20554 if (domain == NULL_TREE)
20555 return false;
20556
20557 tree max = TYPE_MAX_VALUE (domain);
20558 if (TREE_CODE (max) != MINUS_EXPR)
20559 return false;
20560
20561 return deducible_expression (TREE_OPERAND (max, 0));
20562 }
20563
20564 /* Returns true iff the template arguments ARGS use a template parameter
20565 in a deducible way. */
20566
20567 static bool
20568 deducible_template_args (tree args)
20569 {
20570 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20571 {
20572 bool deducible;
20573 tree elt = TREE_VEC_ELT (args, i);
20574 if (ARGUMENT_PACK_P (elt))
20575 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20576 else
20577 {
20578 if (PACK_EXPANSION_P (elt))
20579 elt = PACK_EXPANSION_PATTERN (elt);
20580 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20581 deducible = true;
20582 else if (TYPE_P (elt))
20583 deducible = uses_deducible_template_parms (elt);
20584 else
20585 deducible = deducible_expression (elt);
20586 }
20587 if (deducible)
20588 return true;
20589 }
20590 return false;
20591 }
20592
20593 /* Returns true iff TYPE contains any deducible references to template
20594 parameters, as per 14.8.2.5. */
20595
20596 static bool
20597 uses_deducible_template_parms (tree type)
20598 {
20599 if (PACK_EXPANSION_P (type))
20600 type = PACK_EXPANSION_PATTERN (type);
20601
20602 /* T
20603 cv-list T
20604 TT<T>
20605 TT<i>
20606 TT<> */
20607 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20608 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20609 return true;
20610
20611 /* T*
20612 T&
20613 T&& */
20614 if (INDIRECT_TYPE_P (type))
20615 return uses_deducible_template_parms (TREE_TYPE (type));
20616
20617 /* T[integer-constant ]
20618 type [i] */
20619 if (TREE_CODE (type) == ARRAY_TYPE)
20620 return (uses_deducible_template_parms (TREE_TYPE (type))
20621 || deducible_array_bound (TYPE_DOMAIN (type)));
20622
20623 /* T type ::*
20624 type T::*
20625 T T::*
20626 T (type ::*)()
20627 type (T::*)()
20628 type (type ::*)(T)
20629 type (T::*)(T)
20630 T (type ::*)(T)
20631 T (T::*)()
20632 T (T::*)(T) */
20633 if (TYPE_PTRMEM_P (type))
20634 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20635 || (uses_deducible_template_parms
20636 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20637
20638 /* template-name <T> (where template-name refers to a class template)
20639 template-name <i> (where template-name refers to a class template) */
20640 if (CLASS_TYPE_P (type)
20641 && CLASSTYPE_TEMPLATE_INFO (type)
20642 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20643 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20644 (CLASSTYPE_TI_ARGS (type)));
20645
20646 /* type (T)
20647 T()
20648 T(T) */
20649 if (TREE_CODE (type) == FUNCTION_TYPE
20650 || TREE_CODE (type) == METHOD_TYPE)
20651 {
20652 if (uses_deducible_template_parms (TREE_TYPE (type)))
20653 return true;
20654 tree parm = TYPE_ARG_TYPES (type);
20655 if (TREE_CODE (type) == METHOD_TYPE)
20656 parm = TREE_CHAIN (parm);
20657 for (; parm; parm = TREE_CHAIN (parm))
20658 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20659 return true;
20660 }
20661
20662 return false;
20663 }
20664
20665 /* Subroutine of type_unification_real and unify_pack_expansion to
20666 handle unification of a single P/A pair. Parameters are as
20667 for those functions. */
20668
20669 static int
20670 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20671 int subr, unification_kind_t strict,
20672 bool explain_p)
20673 {
20674 tree arg_expr = NULL_TREE;
20675 int arg_strict;
20676
20677 if (arg == error_mark_node || parm == error_mark_node)
20678 return unify_invalid (explain_p);
20679 if (arg == unknown_type_node)
20680 /* We can't deduce anything from this, but we might get all the
20681 template args from other function args. */
20682 return unify_success (explain_p);
20683
20684 /* Implicit conversions (Clause 4) will be performed on a function
20685 argument to convert it to the type of the corresponding function
20686 parameter if the parameter type contains no template-parameters that
20687 participate in template argument deduction. */
20688 if (strict != DEDUCE_EXACT
20689 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20690 /* For function parameters with no deducible template parameters,
20691 just return. We'll check non-dependent conversions later. */
20692 return unify_success (explain_p);
20693
20694 switch (strict)
20695 {
20696 case DEDUCE_CALL:
20697 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20698 | UNIFY_ALLOW_MORE_CV_QUAL
20699 | UNIFY_ALLOW_DERIVED);
20700 break;
20701
20702 case DEDUCE_CONV:
20703 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20704 break;
20705
20706 case DEDUCE_EXACT:
20707 arg_strict = UNIFY_ALLOW_NONE;
20708 break;
20709
20710 default:
20711 gcc_unreachable ();
20712 }
20713
20714 /* We only do these transformations if this is the top-level
20715 parameter_type_list in a call or declaration matching; in other
20716 situations (nested function declarators, template argument lists) we
20717 won't be comparing a type to an expression, and we don't do any type
20718 adjustments. */
20719 if (!subr)
20720 {
20721 if (!TYPE_P (arg))
20722 {
20723 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20724 if (type_unknown_p (arg))
20725 {
20726 /* [temp.deduct.type] A template-argument can be
20727 deduced from a pointer to function or pointer
20728 to member function argument if the set of
20729 overloaded functions does not contain function
20730 templates and at most one of a set of
20731 overloaded functions provides a unique
20732 match. */
20733 resolve_overloaded_unification (tparms, targs, parm,
20734 arg, strict,
20735 arg_strict, explain_p);
20736 /* If a unique match was not found, this is a
20737 non-deduced context, so we still succeed. */
20738 return unify_success (explain_p);
20739 }
20740
20741 arg_expr = arg;
20742 arg = unlowered_expr_type (arg);
20743 if (arg == error_mark_node)
20744 return unify_invalid (explain_p);
20745 }
20746
20747 arg_strict |=
20748 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20749 }
20750 else
20751 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20752 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20753 return unify_template_argument_mismatch (explain_p, parm, arg);
20754
20755 /* For deduction from an init-list we need the actual list. */
20756 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20757 arg = arg_expr;
20758 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20759 }
20760
20761 /* for_each_template_parm callback that always returns 0. */
20762
20763 static int
20764 zero_r (tree, void *)
20765 {
20766 return 0;
20767 }
20768
20769 /* for_each_template_parm any_fn callback to handle deduction of a template
20770 type argument from the type of an array bound. */
20771
20772 static int
20773 array_deduction_r (tree t, void *data)
20774 {
20775 tree_pair_p d = (tree_pair_p)data;
20776 tree &tparms = d->purpose;
20777 tree &targs = d->value;
20778
20779 if (TREE_CODE (t) == ARRAY_TYPE)
20780 if (tree dom = TYPE_DOMAIN (t))
20781 if (tree max = TYPE_MAX_VALUE (dom))
20782 {
20783 if (TREE_CODE (max) == MINUS_EXPR)
20784 max = TREE_OPERAND (max, 0);
20785 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20786 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20787 UNIFY_ALLOW_NONE, /*explain*/false);
20788 }
20789
20790 /* Keep walking. */
20791 return 0;
20792 }
20793
20794 /* Try to deduce any not-yet-deduced template type arguments from the type of
20795 an array bound. This is handled separately from unify because 14.8.2.5 says
20796 "The type of a type parameter is only deduced from an array bound if it is
20797 not otherwise deduced." */
20798
20799 static void
20800 try_array_deduction (tree tparms, tree targs, tree parm)
20801 {
20802 tree_pair_s data = { tparms, targs };
20803 hash_set<tree> visited;
20804 for_each_template_parm (parm, zero_r, &data, &visited,
20805 /*nondeduced*/false, array_deduction_r);
20806 }
20807
20808 /* Most parms like fn_type_unification.
20809
20810 If SUBR is 1, we're being called recursively (to unify the
20811 arguments of a function or method parameter of a function
20812 template).
20813
20814 CHECKS is a pointer to a vector of access checks encountered while
20815 substituting default template arguments. */
20816
20817 static int
20818 type_unification_real (tree tparms,
20819 tree full_targs,
20820 tree xparms,
20821 const tree *xargs,
20822 unsigned int xnargs,
20823 int subr,
20824 unification_kind_t strict,
20825 vec<deferred_access_check, va_gc> **checks,
20826 bool explain_p)
20827 {
20828 tree parm, arg;
20829 int i;
20830 int ntparms = TREE_VEC_LENGTH (tparms);
20831 int saw_undeduced = 0;
20832 tree parms;
20833 const tree *args;
20834 unsigned int nargs;
20835 unsigned int ia;
20836
20837 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20838 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20839 gcc_assert (ntparms > 0);
20840
20841 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20842
20843 /* Reset the number of non-defaulted template arguments contained
20844 in TARGS. */
20845 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20846
20847 again:
20848 parms = xparms;
20849 args = xargs;
20850 nargs = xnargs;
20851
20852 ia = 0;
20853 while (parms && parms != void_list_node
20854 && ia < nargs)
20855 {
20856 parm = TREE_VALUE (parms);
20857
20858 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20859 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20860 /* For a function parameter pack that occurs at the end of the
20861 parameter-declaration-list, the type A of each remaining
20862 argument of the call is compared with the type P of the
20863 declarator-id of the function parameter pack. */
20864 break;
20865
20866 parms = TREE_CHAIN (parms);
20867
20868 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20869 /* For a function parameter pack that does not occur at the
20870 end of the parameter-declaration-list, the type of the
20871 parameter pack is a non-deduced context. */
20872 continue;
20873
20874 arg = args[ia];
20875 ++ia;
20876
20877 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20878 explain_p))
20879 return 1;
20880 }
20881
20882 if (parms
20883 && parms != void_list_node
20884 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20885 {
20886 /* Unify the remaining arguments with the pack expansion type. */
20887 tree argvec;
20888 tree parmvec = make_tree_vec (1);
20889
20890 /* Allocate a TREE_VEC and copy in all of the arguments */
20891 argvec = make_tree_vec (nargs - ia);
20892 for (i = 0; ia < nargs; ++ia, ++i)
20893 TREE_VEC_ELT (argvec, i) = args[ia];
20894
20895 /* Copy the parameter into parmvec. */
20896 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20897 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20898 /*subr=*/subr, explain_p))
20899 return 1;
20900
20901 /* Advance to the end of the list of parameters. */
20902 parms = TREE_CHAIN (parms);
20903 }
20904
20905 /* Fail if we've reached the end of the parm list, and more args
20906 are present, and the parm list isn't variadic. */
20907 if (ia < nargs && parms == void_list_node)
20908 return unify_too_many_arguments (explain_p, nargs, ia);
20909 /* Fail if parms are left and they don't have default values and
20910 they aren't all deduced as empty packs (c++/57397). This is
20911 consistent with sufficient_parms_p. */
20912 if (parms && parms != void_list_node
20913 && TREE_PURPOSE (parms) == NULL_TREE)
20914 {
20915 unsigned int count = nargs;
20916 tree p = parms;
20917 bool type_pack_p;
20918 do
20919 {
20920 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20921 if (!type_pack_p)
20922 count++;
20923 p = TREE_CHAIN (p);
20924 }
20925 while (p && p != void_list_node);
20926 if (count != nargs)
20927 return unify_too_few_arguments (explain_p, ia, count,
20928 type_pack_p);
20929 }
20930
20931 if (!subr)
20932 {
20933 tsubst_flags_t complain = (explain_p
20934 ? tf_warning_or_error
20935 : tf_none);
20936 bool tried_array_deduction = (cxx_dialect < cxx17);
20937
20938 for (i = 0; i < ntparms; i++)
20939 {
20940 tree targ = TREE_VEC_ELT (targs, i);
20941 tree tparm = TREE_VEC_ELT (tparms, i);
20942
20943 /* Clear the "incomplete" flags on all argument packs now so that
20944 substituting them into later default arguments works. */
20945 if (targ && ARGUMENT_PACK_P (targ))
20946 {
20947 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20948 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20949 }
20950
20951 if (targ || tparm == error_mark_node)
20952 continue;
20953 tparm = TREE_VALUE (tparm);
20954
20955 if (TREE_CODE (tparm) == TYPE_DECL
20956 && !tried_array_deduction)
20957 {
20958 try_array_deduction (tparms, targs, xparms);
20959 tried_array_deduction = true;
20960 if (TREE_VEC_ELT (targs, i))
20961 continue;
20962 }
20963
20964 /* If this is an undeduced nontype parameter that depends on
20965 a type parameter, try another pass; its type may have been
20966 deduced from a later argument than the one from which
20967 this parameter can be deduced. */
20968 if (TREE_CODE (tparm) == PARM_DECL
20969 && uses_template_parms (TREE_TYPE (tparm))
20970 && saw_undeduced < 2)
20971 {
20972 saw_undeduced = 1;
20973 continue;
20974 }
20975
20976 /* Core issue #226 (C++0x) [temp.deduct]:
20977
20978 If a template argument has not been deduced, its
20979 default template argument, if any, is used.
20980
20981 When we are in C++98 mode, TREE_PURPOSE will either
20982 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20983 to explicitly check cxx_dialect here. */
20984 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
20985 /* OK, there is a default argument. Wait until after the
20986 conversion check to do substitution. */
20987 continue;
20988
20989 /* If the type parameter is a parameter pack, then it will
20990 be deduced to an empty parameter pack. */
20991 if (template_parameter_pack_p (tparm))
20992 {
20993 tree arg;
20994
20995 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
20996 {
20997 arg = make_node (NONTYPE_ARGUMENT_PACK);
20998 TREE_CONSTANT (arg) = 1;
20999 }
21000 else
21001 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21002
21003 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21004
21005 TREE_VEC_ELT (targs, i) = arg;
21006 continue;
21007 }
21008
21009 return unify_parameter_deduction_failure (explain_p, tparm);
21010 }
21011
21012 /* Now substitute into the default template arguments. */
21013 for (i = 0; i < ntparms; i++)
21014 {
21015 tree targ = TREE_VEC_ELT (targs, i);
21016 tree tparm = TREE_VEC_ELT (tparms, i);
21017
21018 if (targ || tparm == error_mark_node)
21019 continue;
21020 tree parm = TREE_VALUE (tparm);
21021 tree arg = TREE_PURPOSE (tparm);
21022 reopen_deferring_access_checks (*checks);
21023 location_t save_loc = input_location;
21024 if (DECL_P (parm))
21025 input_location = DECL_SOURCE_LOCATION (parm);
21026
21027 if (saw_undeduced == 1
21028 && TREE_CODE (parm) == PARM_DECL
21029 && uses_template_parms (TREE_TYPE (parm)))
21030 {
21031 /* The type of this non-type parameter depends on undeduced
21032 parameters. Don't try to use its default argument yet,
21033 since we might deduce an argument for it on the next pass,
21034 but do check whether the arguments we already have cause
21035 substitution failure, so that that happens before we try
21036 later default arguments (78489). */
21037 ++processing_template_decl;
21038 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21039 NULL_TREE);
21040 --processing_template_decl;
21041 if (type == error_mark_node)
21042 arg = error_mark_node;
21043 else
21044 arg = NULL_TREE;
21045 }
21046 else
21047 {
21048 /* Even if the call is happening in template context, getting
21049 here means it's non-dependent, and a default argument is
21050 considered a separate definition under [temp.decls], so we can
21051 do this substitution without processing_template_decl. This
21052 is important if the default argument contains something that
21053 might be instantiation-dependent like access (87480). */
21054 processing_template_decl_sentinel s;
21055 tree substed = NULL_TREE;
21056 if (saw_undeduced == 1)
21057 {
21058 /* First instatiate in template context, in case we still
21059 depend on undeduced template parameters. */
21060 ++processing_template_decl;
21061 substed = tsubst_template_arg (arg, full_targs, complain,
21062 NULL_TREE);
21063 --processing_template_decl;
21064 if (substed != error_mark_node
21065 && !uses_template_parms (substed))
21066 /* We replaced all the tparms, substitute again out of
21067 template context. */
21068 substed = NULL_TREE;
21069 }
21070 if (!substed)
21071 substed = tsubst_template_arg (arg, full_targs, complain,
21072 NULL_TREE);
21073
21074 if (!uses_template_parms (substed))
21075 arg = convert_template_argument (parm, substed, full_targs,
21076 complain, i, NULL_TREE);
21077 else if (saw_undeduced == 1)
21078 arg = NULL_TREE;
21079 else
21080 arg = error_mark_node;
21081 }
21082
21083 input_location = save_loc;
21084 *checks = get_deferred_access_checks ();
21085 pop_deferring_access_checks ();
21086
21087 if (arg == error_mark_node)
21088 return 1;
21089 else if (arg)
21090 {
21091 TREE_VEC_ELT (targs, i) = arg;
21092 /* The position of the first default template argument,
21093 is also the number of non-defaulted arguments in TARGS.
21094 Record that. */
21095 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21096 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21097 }
21098 }
21099
21100 if (saw_undeduced++ == 1)
21101 goto again;
21102 }
21103
21104 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21105 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21106
21107 return unify_success (explain_p);
21108 }
21109
21110 /* Subroutine of type_unification_real. Args are like the variables
21111 at the call site. ARG is an overloaded function (or template-id);
21112 we try deducing template args from each of the overloads, and if
21113 only one succeeds, we go with that. Modifies TARGS and returns
21114 true on success. */
21115
21116 static bool
21117 resolve_overloaded_unification (tree tparms,
21118 tree targs,
21119 tree parm,
21120 tree arg,
21121 unification_kind_t strict,
21122 int sub_strict,
21123 bool explain_p)
21124 {
21125 tree tempargs = copy_node (targs);
21126 int good = 0;
21127 tree goodfn = NULL_TREE;
21128 bool addr_p;
21129
21130 if (TREE_CODE (arg) == ADDR_EXPR)
21131 {
21132 arg = TREE_OPERAND (arg, 0);
21133 addr_p = true;
21134 }
21135 else
21136 addr_p = false;
21137
21138 if (TREE_CODE (arg) == COMPONENT_REF)
21139 /* Handle `&x' where `x' is some static or non-static member
21140 function name. */
21141 arg = TREE_OPERAND (arg, 1);
21142
21143 if (TREE_CODE (arg) == OFFSET_REF)
21144 arg = TREE_OPERAND (arg, 1);
21145
21146 /* Strip baselink information. */
21147 if (BASELINK_P (arg))
21148 arg = BASELINK_FUNCTIONS (arg);
21149
21150 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21151 {
21152 /* If we got some explicit template args, we need to plug them into
21153 the affected templates before we try to unify, in case the
21154 explicit args will completely resolve the templates in question. */
21155
21156 int ok = 0;
21157 tree expl_subargs = TREE_OPERAND (arg, 1);
21158 arg = TREE_OPERAND (arg, 0);
21159
21160 for (lkp_iterator iter (arg); iter; ++iter)
21161 {
21162 tree fn = *iter;
21163 tree subargs, elem;
21164
21165 if (TREE_CODE (fn) != TEMPLATE_DECL)
21166 continue;
21167
21168 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21169 expl_subargs, NULL_TREE, tf_none,
21170 /*require_all_args=*/true,
21171 /*use_default_args=*/true);
21172 if (subargs != error_mark_node
21173 && !any_dependent_template_arguments_p (subargs))
21174 {
21175 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21176 if (try_one_overload (tparms, targs, tempargs, parm,
21177 elem, strict, sub_strict, addr_p, explain_p)
21178 && (!goodfn || !same_type_p (goodfn, elem)))
21179 {
21180 goodfn = elem;
21181 ++good;
21182 }
21183 }
21184 else if (subargs)
21185 ++ok;
21186 }
21187 /* If no templates (or more than one) are fully resolved by the
21188 explicit arguments, this template-id is a non-deduced context; it
21189 could still be OK if we deduce all template arguments for the
21190 enclosing call through other arguments. */
21191 if (good != 1)
21192 good = ok;
21193 }
21194 else if (TREE_CODE (arg) != OVERLOAD
21195 && TREE_CODE (arg) != FUNCTION_DECL)
21196 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21197 -- but the deduction does not succeed because the expression is
21198 not just the function on its own. */
21199 return false;
21200 else
21201 for (lkp_iterator iter (arg); iter; ++iter)
21202 {
21203 tree fn = *iter;
21204 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21205 strict, sub_strict, addr_p, explain_p)
21206 && (!goodfn || !decls_match (goodfn, fn)))
21207 {
21208 goodfn = fn;
21209 ++good;
21210 }
21211 }
21212
21213 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21214 to function or pointer to member function argument if the set of
21215 overloaded functions does not contain function templates and at most
21216 one of a set of overloaded functions provides a unique match.
21217
21218 So if we found multiple possibilities, we return success but don't
21219 deduce anything. */
21220
21221 if (good == 1)
21222 {
21223 int i = TREE_VEC_LENGTH (targs);
21224 for (; i--; )
21225 if (TREE_VEC_ELT (tempargs, i))
21226 {
21227 tree old = TREE_VEC_ELT (targs, i);
21228 tree new_ = TREE_VEC_ELT (tempargs, i);
21229 if (new_ && old && ARGUMENT_PACK_P (old)
21230 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21231 /* Don't forget explicit template arguments in a pack. */
21232 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21233 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21234 TREE_VEC_ELT (targs, i) = new_;
21235 }
21236 }
21237 if (good)
21238 return true;
21239
21240 return false;
21241 }
21242
21243 /* Core DR 115: In contexts where deduction is done and fails, or in
21244 contexts where deduction is not done, if a template argument list is
21245 specified and it, along with any default template arguments, identifies
21246 a single function template specialization, then the template-id is an
21247 lvalue for the function template specialization. */
21248
21249 tree
21250 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21251 {
21252 tree expr, offset, baselink;
21253 bool addr;
21254
21255 if (!type_unknown_p (orig_expr))
21256 return orig_expr;
21257
21258 expr = orig_expr;
21259 addr = false;
21260 offset = NULL_TREE;
21261 baselink = NULL_TREE;
21262
21263 if (TREE_CODE (expr) == ADDR_EXPR)
21264 {
21265 expr = TREE_OPERAND (expr, 0);
21266 addr = true;
21267 }
21268 if (TREE_CODE (expr) == OFFSET_REF)
21269 {
21270 offset = expr;
21271 expr = TREE_OPERAND (expr, 1);
21272 }
21273 if (BASELINK_P (expr))
21274 {
21275 baselink = expr;
21276 expr = BASELINK_FUNCTIONS (expr);
21277 }
21278
21279 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21280 {
21281 int good = 0;
21282 tree goodfn = NULL_TREE;
21283
21284 /* If we got some explicit template args, we need to plug them into
21285 the affected templates before we try to unify, in case the
21286 explicit args will completely resolve the templates in question. */
21287
21288 tree expl_subargs = TREE_OPERAND (expr, 1);
21289 tree arg = TREE_OPERAND (expr, 0);
21290 tree badfn = NULL_TREE;
21291 tree badargs = NULL_TREE;
21292
21293 for (lkp_iterator iter (arg); iter; ++iter)
21294 {
21295 tree fn = *iter;
21296 tree subargs, elem;
21297
21298 if (TREE_CODE (fn) != TEMPLATE_DECL)
21299 continue;
21300
21301 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21302 expl_subargs, NULL_TREE, tf_none,
21303 /*require_all_args=*/true,
21304 /*use_default_args=*/true);
21305 if (subargs != error_mark_node
21306 && !any_dependent_template_arguments_p (subargs))
21307 {
21308 elem = instantiate_template (fn, subargs, tf_none);
21309 if (elem == error_mark_node)
21310 {
21311 badfn = fn;
21312 badargs = subargs;
21313 }
21314 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21315 {
21316 goodfn = elem;
21317 ++good;
21318 }
21319 }
21320 }
21321 if (good == 1)
21322 {
21323 mark_used (goodfn);
21324 expr = goodfn;
21325 if (baselink)
21326 expr = build_baselink (BASELINK_BINFO (baselink),
21327 BASELINK_ACCESS_BINFO (baselink),
21328 expr, BASELINK_OPTYPE (baselink));
21329 if (offset)
21330 {
21331 tree base
21332 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21333 expr = build_offset_ref (base, expr, addr, complain);
21334 }
21335 if (addr)
21336 expr = cp_build_addr_expr (expr, complain);
21337 return expr;
21338 }
21339 else if (good == 0 && badargs && (complain & tf_error))
21340 /* There were no good options and at least one bad one, so let the
21341 user know what the problem is. */
21342 instantiate_template (badfn, badargs, complain);
21343 }
21344 return orig_expr;
21345 }
21346
21347 /* As above, but error out if the expression remains overloaded. */
21348
21349 tree
21350 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21351 {
21352 exp = resolve_nondeduced_context (exp, complain);
21353 if (type_unknown_p (exp))
21354 {
21355 if (complain & tf_error)
21356 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21357 return error_mark_node;
21358 }
21359 return exp;
21360 }
21361
21362 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21363 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21364 different overloads deduce different arguments for a given parm.
21365 ADDR_P is true if the expression for which deduction is being
21366 performed was of the form "& fn" rather than simply "fn".
21367
21368 Returns 1 on success. */
21369
21370 static int
21371 try_one_overload (tree tparms,
21372 tree orig_targs,
21373 tree targs,
21374 tree parm,
21375 tree arg,
21376 unification_kind_t strict,
21377 int sub_strict,
21378 bool addr_p,
21379 bool explain_p)
21380 {
21381 int nargs;
21382 tree tempargs;
21383 int i;
21384
21385 if (arg == error_mark_node)
21386 return 0;
21387
21388 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21389 to function or pointer to member function argument if the set of
21390 overloaded functions does not contain function templates and at most
21391 one of a set of overloaded functions provides a unique match.
21392
21393 So if this is a template, just return success. */
21394
21395 if (uses_template_parms (arg))
21396 return 1;
21397
21398 if (TREE_CODE (arg) == METHOD_TYPE)
21399 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21400 else if (addr_p)
21401 arg = build_pointer_type (arg);
21402
21403 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21404
21405 /* We don't copy orig_targs for this because if we have already deduced
21406 some template args from previous args, unify would complain when we
21407 try to deduce a template parameter for the same argument, even though
21408 there isn't really a conflict. */
21409 nargs = TREE_VEC_LENGTH (targs);
21410 tempargs = make_tree_vec (nargs);
21411
21412 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21413 return 0;
21414
21415 /* First make sure we didn't deduce anything that conflicts with
21416 explicitly specified args. */
21417 for (i = nargs; i--; )
21418 {
21419 tree elt = TREE_VEC_ELT (tempargs, i);
21420 tree oldelt = TREE_VEC_ELT (orig_targs, i);
21421
21422 if (!elt)
21423 /*NOP*/;
21424 else if (uses_template_parms (elt))
21425 /* Since we're unifying against ourselves, we will fill in
21426 template args used in the function parm list with our own
21427 template parms. Discard them. */
21428 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
21429 else if (oldelt && ARGUMENT_PACK_P (oldelt))
21430 {
21431 /* Check that the argument at each index of the deduced argument pack
21432 is equivalent to the corresponding explicitly specified argument.
21433 We may have deduced more arguments than were explicitly specified,
21434 and that's OK. */
21435
21436 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21437 that's wrong if we deduce the same argument pack from multiple
21438 function arguments: it's only incomplete the first time. */
21439
21440 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
21441 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
21442
21443 if (TREE_VEC_LENGTH (deduced_pack)
21444 < TREE_VEC_LENGTH (explicit_pack))
21445 return 0;
21446
21447 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
21448 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
21449 TREE_VEC_ELT (deduced_pack, j)))
21450 return 0;
21451 }
21452 else if (oldelt && !template_args_equal (oldelt, elt))
21453 return 0;
21454 }
21455
21456 for (i = nargs; i--; )
21457 {
21458 tree elt = TREE_VEC_ELT (tempargs, i);
21459
21460 if (elt)
21461 TREE_VEC_ELT (targs, i) = elt;
21462 }
21463
21464 return 1;
21465 }
21466
21467 /* PARM is a template class (perhaps with unbound template
21468 parameters). ARG is a fully instantiated type. If ARG can be
21469 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21470 TARGS are as for unify. */
21471
21472 static tree
21473 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21474 bool explain_p)
21475 {
21476 tree copy_of_targs;
21477
21478 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21479 return NULL_TREE;
21480 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21481 /* Matches anything. */;
21482 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21483 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21484 return NULL_TREE;
21485
21486 /* We need to make a new template argument vector for the call to
21487 unify. If we used TARGS, we'd clutter it up with the result of
21488 the attempted unification, even if this class didn't work out.
21489 We also don't want to commit ourselves to all the unifications
21490 we've already done, since unification is supposed to be done on
21491 an argument-by-argument basis. In other words, consider the
21492 following pathological case:
21493
21494 template <int I, int J, int K>
21495 struct S {};
21496
21497 template <int I, int J>
21498 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21499
21500 template <int I, int J, int K>
21501 void f(S<I, J, K>, S<I, I, I>);
21502
21503 void g() {
21504 S<0, 0, 0> s0;
21505 S<0, 1, 2> s2;
21506
21507 f(s0, s2);
21508 }
21509
21510 Now, by the time we consider the unification involving `s2', we
21511 already know that we must have `f<0, 0, 0>'. But, even though
21512 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21513 because there are two ways to unify base classes of S<0, 1, 2>
21514 with S<I, I, I>. If we kept the already deduced knowledge, we
21515 would reject the possibility I=1. */
21516 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21517
21518 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21519 {
21520 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21521 return NULL_TREE;
21522 return arg;
21523 }
21524
21525 /* If unification failed, we're done. */
21526 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21527 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21528 return NULL_TREE;
21529
21530 return arg;
21531 }
21532
21533 /* Given a template type PARM and a class type ARG, find the unique
21534 base type in ARG that is an instance of PARM. We do not examine
21535 ARG itself; only its base-classes. If there is not exactly one
21536 appropriate base class, return NULL_TREE. PARM may be the type of
21537 a partial specialization, as well as a plain template type. Used
21538 by unify. */
21539
21540 static enum template_base_result
21541 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21542 bool explain_p, tree *result)
21543 {
21544 tree rval = NULL_TREE;
21545 tree binfo;
21546
21547 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21548
21549 binfo = TYPE_BINFO (complete_type (arg));
21550 if (!binfo)
21551 {
21552 /* The type could not be completed. */
21553 *result = NULL_TREE;
21554 return tbr_incomplete_type;
21555 }
21556
21557 /* Walk in inheritance graph order. The search order is not
21558 important, and this avoids multiple walks of virtual bases. */
21559 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21560 {
21561 tree r = try_class_unification (tparms, targs, parm,
21562 BINFO_TYPE (binfo), explain_p);
21563
21564 if (r)
21565 {
21566 /* If there is more than one satisfactory baseclass, then:
21567
21568 [temp.deduct.call]
21569
21570 If they yield more than one possible deduced A, the type
21571 deduction fails.
21572
21573 applies. */
21574 if (rval && !same_type_p (r, rval))
21575 {
21576 *result = NULL_TREE;
21577 return tbr_ambiguous_baseclass;
21578 }
21579
21580 rval = r;
21581 }
21582 }
21583
21584 *result = rval;
21585 return tbr_success;
21586 }
21587
21588 /* Returns the level of DECL, which declares a template parameter. */
21589
21590 static int
21591 template_decl_level (tree decl)
21592 {
21593 switch (TREE_CODE (decl))
21594 {
21595 case TYPE_DECL:
21596 case TEMPLATE_DECL:
21597 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21598
21599 case PARM_DECL:
21600 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21601
21602 default:
21603 gcc_unreachable ();
21604 }
21605 return 0;
21606 }
21607
21608 /* Decide whether ARG can be unified with PARM, considering only the
21609 cv-qualifiers of each type, given STRICT as documented for unify.
21610 Returns nonzero iff the unification is OK on that basis. */
21611
21612 static int
21613 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21614 {
21615 int arg_quals = cp_type_quals (arg);
21616 int parm_quals = cp_type_quals (parm);
21617
21618 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21619 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21620 {
21621 /* Although a CVR qualifier is ignored when being applied to a
21622 substituted template parameter ([8.3.2]/1 for example), that
21623 does not allow us to unify "const T" with "int&" because both
21624 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21625 It is ok when we're allowing additional CV qualifiers
21626 at the outer level [14.8.2.1]/3,1st bullet. */
21627 if ((TYPE_REF_P (arg)
21628 || TREE_CODE (arg) == FUNCTION_TYPE
21629 || TREE_CODE (arg) == METHOD_TYPE)
21630 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21631 return 0;
21632
21633 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21634 && (parm_quals & TYPE_QUAL_RESTRICT))
21635 return 0;
21636 }
21637
21638 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21639 && (arg_quals & parm_quals) != parm_quals)
21640 return 0;
21641
21642 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21643 && (parm_quals & arg_quals) != arg_quals)
21644 return 0;
21645
21646 return 1;
21647 }
21648
21649 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21650 void
21651 template_parm_level_and_index (tree parm, int* level, int* index)
21652 {
21653 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21654 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21655 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21656 {
21657 *index = TEMPLATE_TYPE_IDX (parm);
21658 *level = TEMPLATE_TYPE_LEVEL (parm);
21659 }
21660 else
21661 {
21662 *index = TEMPLATE_PARM_IDX (parm);
21663 *level = TEMPLATE_PARM_LEVEL (parm);
21664 }
21665 }
21666
21667 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21668 do { \
21669 if (unify (TP, TA, P, A, S, EP)) \
21670 return 1; \
21671 } while (0)
21672
21673 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21674 expansion at the end of PACKED_PARMS. Returns 0 if the type
21675 deduction succeeds, 1 otherwise. STRICT is the same as in
21676 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21677 function call argument list. We'll need to adjust the arguments to make them
21678 types. SUBR tells us if this is from a recursive call to
21679 type_unification_real, or for comparing two template argument
21680 lists. */
21681
21682 static int
21683 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21684 tree packed_args, unification_kind_t strict,
21685 bool subr, bool explain_p)
21686 {
21687 tree parm
21688 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21689 tree pattern = PACK_EXPANSION_PATTERN (parm);
21690 tree pack, packs = NULL_TREE;
21691 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21692
21693 /* Add in any args remembered from an earlier partial instantiation. */
21694 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21695 int levels = TMPL_ARGS_DEPTH (targs);
21696
21697 packed_args = expand_template_argument_pack (packed_args);
21698
21699 int len = TREE_VEC_LENGTH (packed_args);
21700
21701 /* Determine the parameter packs we will be deducing from the
21702 pattern, and record their current deductions. */
21703 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21704 pack; pack = TREE_CHAIN (pack))
21705 {
21706 tree parm_pack = TREE_VALUE (pack);
21707 int idx, level;
21708
21709 /* Only template parameter packs can be deduced, not e.g. function
21710 parameter packs or __bases or __integer_pack. */
21711 if (!TEMPLATE_PARM_P (parm_pack))
21712 continue;
21713
21714 /* Determine the index and level of this parameter pack. */
21715 template_parm_level_and_index (parm_pack, &level, &idx);
21716 if (level < levels)
21717 continue;
21718
21719 /* Keep track of the parameter packs and their corresponding
21720 argument packs. */
21721 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21722 TREE_TYPE (packs) = make_tree_vec (len - start);
21723 }
21724
21725 /* Loop through all of the arguments that have not yet been
21726 unified and unify each with the pattern. */
21727 for (i = start; i < len; i++)
21728 {
21729 tree parm;
21730 bool any_explicit = false;
21731 tree arg = TREE_VEC_ELT (packed_args, i);
21732
21733 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21734 or the element of its argument pack at the current index if
21735 this argument was explicitly specified. */
21736 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21737 {
21738 int idx, level;
21739 tree arg, pargs;
21740 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21741
21742 arg = NULL_TREE;
21743 if (TREE_VALUE (pack)
21744 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21745 && (i - start < TREE_VEC_LENGTH (pargs)))
21746 {
21747 any_explicit = true;
21748 arg = TREE_VEC_ELT (pargs, i - start);
21749 }
21750 TMPL_ARG (targs, level, idx) = arg;
21751 }
21752
21753 /* If we had explicit template arguments, substitute them into the
21754 pattern before deduction. */
21755 if (any_explicit)
21756 {
21757 /* Some arguments might still be unspecified or dependent. */
21758 bool dependent;
21759 ++processing_template_decl;
21760 dependent = any_dependent_template_arguments_p (targs);
21761 if (!dependent)
21762 --processing_template_decl;
21763 parm = tsubst (pattern, targs,
21764 explain_p ? tf_warning_or_error : tf_none,
21765 NULL_TREE);
21766 if (dependent)
21767 --processing_template_decl;
21768 if (parm == error_mark_node)
21769 return 1;
21770 }
21771 else
21772 parm = pattern;
21773
21774 /* Unify the pattern with the current argument. */
21775 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21776 explain_p))
21777 return 1;
21778
21779 /* For each parameter pack, collect the deduced value. */
21780 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21781 {
21782 int idx, level;
21783 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21784
21785 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21786 TMPL_ARG (targs, level, idx);
21787 }
21788 }
21789
21790 /* Verify that the results of unification with the parameter packs
21791 produce results consistent with what we've seen before, and make
21792 the deduced argument packs available. */
21793 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21794 {
21795 tree old_pack = TREE_VALUE (pack);
21796 tree new_args = TREE_TYPE (pack);
21797 int i, len = TREE_VEC_LENGTH (new_args);
21798 int idx, level;
21799 bool nondeduced_p = false;
21800
21801 /* By default keep the original deduced argument pack.
21802 If necessary, more specific code is going to update the
21803 resulting deduced argument later down in this function. */
21804 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21805 TMPL_ARG (targs, level, idx) = old_pack;
21806
21807 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21808 actually deduce anything. */
21809 for (i = 0; i < len && !nondeduced_p; ++i)
21810 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21811 nondeduced_p = true;
21812 if (nondeduced_p)
21813 continue;
21814
21815 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21816 {
21817 /* If we had fewer function args than explicit template args,
21818 just use the explicits. */
21819 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21820 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21821 if (len < explicit_len)
21822 new_args = explicit_args;
21823 }
21824
21825 if (!old_pack)
21826 {
21827 tree result;
21828 /* Build the deduced *_ARGUMENT_PACK. */
21829 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21830 {
21831 result = make_node (NONTYPE_ARGUMENT_PACK);
21832 TREE_CONSTANT (result) = 1;
21833 }
21834 else
21835 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21836
21837 SET_ARGUMENT_PACK_ARGS (result, new_args);
21838
21839 /* Note the deduced argument packs for this parameter
21840 pack. */
21841 TMPL_ARG (targs, level, idx) = result;
21842 }
21843 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21844 && (ARGUMENT_PACK_ARGS (old_pack)
21845 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21846 {
21847 /* We only had the explicitly-provided arguments before, but
21848 now we have a complete set of arguments. */
21849 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21850
21851 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21852 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21853 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21854 }
21855 else
21856 {
21857 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21858 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21859
21860 if (!comp_template_args (old_args, new_args,
21861 &bad_old_arg, &bad_new_arg))
21862 /* Inconsistent unification of this parameter pack. */
21863 return unify_parameter_pack_inconsistent (explain_p,
21864 bad_old_arg,
21865 bad_new_arg);
21866 }
21867 }
21868
21869 return unify_success (explain_p);
21870 }
21871
21872 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21873 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21874 parameters and return value are as for unify. */
21875
21876 static int
21877 unify_array_domain (tree tparms, tree targs,
21878 tree parm_dom, tree arg_dom,
21879 bool explain_p)
21880 {
21881 tree parm_max;
21882 tree arg_max;
21883 bool parm_cst;
21884 bool arg_cst;
21885
21886 /* Our representation of array types uses "N - 1" as the
21887 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21888 not an integer constant. We cannot unify arbitrarily
21889 complex expressions, so we eliminate the MINUS_EXPRs
21890 here. */
21891 parm_max = TYPE_MAX_VALUE (parm_dom);
21892 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21893 if (!parm_cst)
21894 {
21895 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21896 parm_max = TREE_OPERAND (parm_max, 0);
21897 }
21898 arg_max = TYPE_MAX_VALUE (arg_dom);
21899 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21900 if (!arg_cst)
21901 {
21902 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21903 trying to unify the type of a variable with the type
21904 of a template parameter. For example:
21905
21906 template <unsigned int N>
21907 void f (char (&) [N]);
21908 int g();
21909 void h(int i) {
21910 char a[g(i)];
21911 f(a);
21912 }
21913
21914 Here, the type of the ARG will be "int [g(i)]", and
21915 may be a SAVE_EXPR, etc. */
21916 if (TREE_CODE (arg_max) != MINUS_EXPR)
21917 return unify_vla_arg (explain_p, arg_dom);
21918 arg_max = TREE_OPERAND (arg_max, 0);
21919 }
21920
21921 /* If only one of the bounds used a MINUS_EXPR, compensate
21922 by adding one to the other bound. */
21923 if (parm_cst && !arg_cst)
21924 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21925 integer_type_node,
21926 parm_max,
21927 integer_one_node);
21928 else if (arg_cst && !parm_cst)
21929 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21930 integer_type_node,
21931 arg_max,
21932 integer_one_node);
21933
21934 return unify (tparms, targs, parm_max, arg_max,
21935 UNIFY_ALLOW_INTEGER, explain_p);
21936 }
21937
21938 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21939
21940 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21941
21942 static pa_kind_t
21943 pa_kind (tree t)
21944 {
21945 if (PACK_EXPANSION_P (t))
21946 t = PACK_EXPANSION_PATTERN (t);
21947 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21948 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21949 || DECL_TYPE_TEMPLATE_P (t))
21950 return pa_tmpl;
21951 else if (TYPE_P (t))
21952 return pa_type;
21953 else
21954 return pa_expr;
21955 }
21956
21957 /* Deduce the value of template parameters. TPARMS is the (innermost)
21958 set of template parameters to a template. TARGS is the bindings
21959 for those template parameters, as determined thus far; TARGS may
21960 include template arguments for outer levels of template parameters
21961 as well. PARM is a parameter to a template function, or a
21962 subcomponent of that parameter; ARG is the corresponding argument.
21963 This function attempts to match PARM with ARG in a manner
21964 consistent with the existing assignments in TARGS. If more values
21965 are deduced, then TARGS is updated.
21966
21967 Returns 0 if the type deduction succeeds, 1 otherwise. The
21968 parameter STRICT is a bitwise or of the following flags:
21969
21970 UNIFY_ALLOW_NONE:
21971 Require an exact match between PARM and ARG.
21972 UNIFY_ALLOW_MORE_CV_QUAL:
21973 Allow the deduced ARG to be more cv-qualified (by qualification
21974 conversion) than ARG.
21975 UNIFY_ALLOW_LESS_CV_QUAL:
21976 Allow the deduced ARG to be less cv-qualified than ARG.
21977 UNIFY_ALLOW_DERIVED:
21978 Allow the deduced ARG to be a template base class of ARG,
21979 or a pointer to a template base class of the type pointed to by
21980 ARG.
21981 UNIFY_ALLOW_INTEGER:
21982 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21983 case for more information.
21984 UNIFY_ALLOW_OUTER_LEVEL:
21985 This is the outermost level of a deduction. Used to determine validity
21986 of qualification conversions. A valid qualification conversion must
21987 have const qualified pointers leading up to the inner type which
21988 requires additional CV quals, except at the outer level, where const
21989 is not required [conv.qual]. It would be normal to set this flag in
21990 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21991 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21992 This is the outermost level of a deduction, and PARM can be more CV
21993 qualified at this point.
21994 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21995 This is the outermost level of a deduction, and PARM can be less CV
21996 qualified at this point. */
21997
21998 static int
21999 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22000 bool explain_p)
22001 {
22002 int idx;
22003 tree targ;
22004 tree tparm;
22005 int strict_in = strict;
22006 tsubst_flags_t complain = (explain_p
22007 ? tf_warning_or_error
22008 : tf_none);
22009
22010 /* I don't think this will do the right thing with respect to types.
22011 But the only case I've seen it in so far has been array bounds, where
22012 signedness is the only information lost, and I think that will be
22013 okay. */
22014 while (CONVERT_EXPR_P (parm))
22015 parm = TREE_OPERAND (parm, 0);
22016
22017 if (arg == error_mark_node)
22018 return unify_invalid (explain_p);
22019 if (arg == unknown_type_node
22020 || arg == init_list_type_node)
22021 /* We can't deduce anything from this, but we might get all the
22022 template args from other function args. */
22023 return unify_success (explain_p);
22024
22025 if (parm == any_targ_node || arg == any_targ_node)
22026 return unify_success (explain_p);
22027
22028 /* If PARM uses template parameters, then we can't bail out here,
22029 even if ARG == PARM, since we won't record unifications for the
22030 template parameters. We might need them if we're trying to
22031 figure out which of two things is more specialized. */
22032 if (arg == parm && !uses_template_parms (parm))
22033 return unify_success (explain_p);
22034
22035 /* Handle init lists early, so the rest of the function can assume
22036 we're dealing with a type. */
22037 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22038 {
22039 tree elt, elttype;
22040 unsigned i;
22041 tree orig_parm = parm;
22042
22043 /* Replace T with std::initializer_list<T> for deduction. */
22044 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22045 && flag_deduce_init_list)
22046 parm = listify (parm);
22047
22048 if (!is_std_init_list (parm)
22049 && TREE_CODE (parm) != ARRAY_TYPE)
22050 /* We can only deduce from an initializer list argument if the
22051 parameter is std::initializer_list or an array; otherwise this
22052 is a non-deduced context. */
22053 return unify_success (explain_p);
22054
22055 if (TREE_CODE (parm) == ARRAY_TYPE)
22056 elttype = TREE_TYPE (parm);
22057 else
22058 {
22059 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22060 /* Deduction is defined in terms of a single type, so just punt
22061 on the (bizarre) std::initializer_list<T...>. */
22062 if (PACK_EXPANSION_P (elttype))
22063 return unify_success (explain_p);
22064 }
22065
22066 if (strict != DEDUCE_EXACT
22067 && TYPE_P (elttype)
22068 && !uses_deducible_template_parms (elttype))
22069 /* If ELTTYPE has no deducible template parms, skip deduction from
22070 the list elements. */;
22071 else
22072 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22073 {
22074 int elt_strict = strict;
22075
22076 if (elt == error_mark_node)
22077 return unify_invalid (explain_p);
22078
22079 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22080 {
22081 tree type = TREE_TYPE (elt);
22082 if (type == error_mark_node)
22083 return unify_invalid (explain_p);
22084 /* It should only be possible to get here for a call. */
22085 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22086 elt_strict |= maybe_adjust_types_for_deduction
22087 (DEDUCE_CALL, &elttype, &type, elt);
22088 elt = type;
22089 }
22090
22091 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22092 explain_p);
22093 }
22094
22095 if (TREE_CODE (parm) == ARRAY_TYPE
22096 && deducible_array_bound (TYPE_DOMAIN (parm)))
22097 {
22098 /* Also deduce from the length of the initializer list. */
22099 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22100 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22101 if (idx == error_mark_node)
22102 return unify_invalid (explain_p);
22103 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22104 idx, explain_p);
22105 }
22106
22107 /* If the std::initializer_list<T> deduction worked, replace the
22108 deduced A with std::initializer_list<A>. */
22109 if (orig_parm != parm)
22110 {
22111 idx = TEMPLATE_TYPE_IDX (orig_parm);
22112 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22113 targ = listify (targ);
22114 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22115 }
22116 return unify_success (explain_p);
22117 }
22118
22119 /* If parm and arg aren't the same kind of thing (template, type, or
22120 expression), fail early. */
22121 if (pa_kind (parm) != pa_kind (arg))
22122 return unify_invalid (explain_p);
22123
22124 /* Immediately reject some pairs that won't unify because of
22125 cv-qualification mismatches. */
22126 if (TREE_CODE (arg) == TREE_CODE (parm)
22127 && TYPE_P (arg)
22128 /* It is the elements of the array which hold the cv quals of an array
22129 type, and the elements might be template type parms. We'll check
22130 when we recurse. */
22131 && TREE_CODE (arg) != ARRAY_TYPE
22132 /* We check the cv-qualifiers when unifying with template type
22133 parameters below. We want to allow ARG `const T' to unify with
22134 PARM `T' for example, when computing which of two templates
22135 is more specialized, for example. */
22136 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22137 && !check_cv_quals_for_unify (strict_in, arg, parm))
22138 return unify_cv_qual_mismatch (explain_p, parm, arg);
22139
22140 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22141 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22142 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22143 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22144 strict &= ~UNIFY_ALLOW_DERIVED;
22145 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22146 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22147
22148 switch (TREE_CODE (parm))
22149 {
22150 case TYPENAME_TYPE:
22151 case SCOPE_REF:
22152 case UNBOUND_CLASS_TEMPLATE:
22153 /* In a type which contains a nested-name-specifier, template
22154 argument values cannot be deduced for template parameters used
22155 within the nested-name-specifier. */
22156 return unify_success (explain_p);
22157
22158 case TEMPLATE_TYPE_PARM:
22159 case TEMPLATE_TEMPLATE_PARM:
22160 case BOUND_TEMPLATE_TEMPLATE_PARM:
22161 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22162 if (error_operand_p (tparm))
22163 return unify_invalid (explain_p);
22164
22165 if (TEMPLATE_TYPE_LEVEL (parm)
22166 != template_decl_level (tparm))
22167 /* The PARM is not one we're trying to unify. Just check
22168 to see if it matches ARG. */
22169 {
22170 if (TREE_CODE (arg) == TREE_CODE (parm)
22171 && (is_auto (parm) ? is_auto (arg)
22172 : same_type_p (parm, arg)))
22173 return unify_success (explain_p);
22174 else
22175 return unify_type_mismatch (explain_p, parm, arg);
22176 }
22177 idx = TEMPLATE_TYPE_IDX (parm);
22178 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22179 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22180 if (error_operand_p (tparm))
22181 return unify_invalid (explain_p);
22182
22183 /* Check for mixed types and values. */
22184 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22185 && TREE_CODE (tparm) != TYPE_DECL)
22186 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22187 && TREE_CODE (tparm) != TEMPLATE_DECL))
22188 gcc_unreachable ();
22189
22190 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22191 {
22192 if ((strict_in & UNIFY_ALLOW_DERIVED)
22193 && CLASS_TYPE_P (arg))
22194 {
22195 /* First try to match ARG directly. */
22196 tree t = try_class_unification (tparms, targs, parm, arg,
22197 explain_p);
22198 if (!t)
22199 {
22200 /* Otherwise, look for a suitable base of ARG, as below. */
22201 enum template_base_result r;
22202 r = get_template_base (tparms, targs, parm, arg,
22203 explain_p, &t);
22204 if (!t)
22205 return unify_no_common_base (explain_p, r, parm, arg);
22206 arg = t;
22207 }
22208 }
22209 /* ARG must be constructed from a template class or a template
22210 template parameter. */
22211 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22212 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22213 return unify_template_deduction_failure (explain_p, parm, arg);
22214
22215 /* Deduce arguments T, i from TT<T> or TT<i>. */
22216 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22217 return 1;
22218
22219 arg = TYPE_TI_TEMPLATE (arg);
22220
22221 /* Fall through to deduce template name. */
22222 }
22223
22224 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22225 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22226 {
22227 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22228
22229 /* Simple cases: Value already set, does match or doesn't. */
22230 if (targ != NULL_TREE && template_args_equal (targ, arg))
22231 return unify_success (explain_p);
22232 else if (targ)
22233 return unify_inconsistency (explain_p, parm, targ, arg);
22234 }
22235 else
22236 {
22237 /* If PARM is `const T' and ARG is only `int', we don't have
22238 a match unless we are allowing additional qualification.
22239 If ARG is `const int' and PARM is just `T' that's OK;
22240 that binds `const int' to `T'. */
22241 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22242 arg, parm))
22243 return unify_cv_qual_mismatch (explain_p, parm, arg);
22244
22245 /* Consider the case where ARG is `const volatile int' and
22246 PARM is `const T'. Then, T should be `volatile int'. */
22247 arg = cp_build_qualified_type_real
22248 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22249 if (arg == error_mark_node)
22250 return unify_invalid (explain_p);
22251
22252 /* Simple cases: Value already set, does match or doesn't. */
22253 if (targ != NULL_TREE && same_type_p (targ, arg))
22254 return unify_success (explain_p);
22255 else if (targ)
22256 return unify_inconsistency (explain_p, parm, targ, arg);
22257
22258 /* Make sure that ARG is not a variable-sized array. (Note
22259 that were talking about variable-sized arrays (like
22260 `int[n]'), rather than arrays of unknown size (like
22261 `int[]').) We'll get very confused by such a type since
22262 the bound of the array is not constant, and therefore
22263 not mangleable. Besides, such types are not allowed in
22264 ISO C++, so we can do as we please here. We do allow
22265 them for 'auto' deduction, since that isn't ABI-exposed. */
22266 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22267 return unify_vla_arg (explain_p, arg);
22268
22269 /* Strip typedefs as in convert_template_argument. */
22270 arg = canonicalize_type_argument (arg, tf_none);
22271 }
22272
22273 /* If ARG is a parameter pack or an expansion, we cannot unify
22274 against it unless PARM is also a parameter pack. */
22275 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22276 && !template_parameter_pack_p (parm))
22277 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22278
22279 /* If the argument deduction results is a METHOD_TYPE,
22280 then there is a problem.
22281 METHOD_TYPE doesn't map to any real C++ type the result of
22282 the deduction cannot be of that type. */
22283 if (TREE_CODE (arg) == METHOD_TYPE)
22284 return unify_method_type_error (explain_p, arg);
22285
22286 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22287 return unify_success (explain_p);
22288
22289 case TEMPLATE_PARM_INDEX:
22290 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22291 if (error_operand_p (tparm))
22292 return unify_invalid (explain_p);
22293
22294 if (TEMPLATE_PARM_LEVEL (parm)
22295 != template_decl_level (tparm))
22296 {
22297 /* The PARM is not one we're trying to unify. Just check
22298 to see if it matches ARG. */
22299 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22300 && cp_tree_equal (parm, arg));
22301 if (result)
22302 unify_expression_unequal (explain_p, parm, arg);
22303 return result;
22304 }
22305
22306 idx = TEMPLATE_PARM_IDX (parm);
22307 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22308
22309 if (targ)
22310 {
22311 if ((strict & UNIFY_ALLOW_INTEGER)
22312 && TREE_TYPE (targ) && TREE_TYPE (arg)
22313 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22314 /* We're deducing from an array bound, the type doesn't matter. */
22315 arg = fold_convert (TREE_TYPE (targ), arg);
22316 int x = !cp_tree_equal (targ, arg);
22317 if (x)
22318 unify_inconsistency (explain_p, parm, targ, arg);
22319 return x;
22320 }
22321
22322 /* [temp.deduct.type] If, in the declaration of a function template
22323 with a non-type template-parameter, the non-type
22324 template-parameter is used in an expression in the function
22325 parameter-list and, if the corresponding template-argument is
22326 deduced, the template-argument type shall match the type of the
22327 template-parameter exactly, except that a template-argument
22328 deduced from an array bound may be of any integral type.
22329 The non-type parameter might use already deduced type parameters. */
22330 tparm = TREE_TYPE (parm);
22331 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22332 /* We don't have enough levels of args to do any substitution. This
22333 can happen in the context of -fnew-ttp-matching. */;
22334 else
22335 {
22336 ++processing_template_decl;
22337 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22338 --processing_template_decl;
22339
22340 if (tree a = type_uses_auto (tparm))
22341 {
22342 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22343 if (tparm == error_mark_node)
22344 return 1;
22345 }
22346 }
22347
22348 if (!TREE_TYPE (arg))
22349 /* Template-parameter dependent expression. Just accept it for now.
22350 It will later be processed in convert_template_argument. */
22351 ;
22352 else if (same_type_p (non_reference (TREE_TYPE (arg)),
22353 non_reference (tparm)))
22354 /* OK */;
22355 else if ((strict & UNIFY_ALLOW_INTEGER)
22356 && CP_INTEGRAL_TYPE_P (tparm))
22357 /* Convert the ARG to the type of PARM; the deduced non-type
22358 template argument must exactly match the types of the
22359 corresponding parameter. */
22360 arg = fold (build_nop (tparm, arg));
22361 else if (uses_template_parms (tparm))
22362 {
22363 /* We haven't deduced the type of this parameter yet. */
22364 if (cxx_dialect >= cxx17
22365 /* We deduce from array bounds in try_array_deduction. */
22366 && !(strict & UNIFY_ALLOW_INTEGER))
22367 {
22368 /* Deduce it from the non-type argument. */
22369 tree atype = TREE_TYPE (arg);
22370 RECUR_AND_CHECK_FAILURE (tparms, targs,
22371 tparm, atype,
22372 UNIFY_ALLOW_NONE, explain_p);
22373 }
22374 else
22375 /* Try again later. */
22376 return unify_success (explain_p);
22377 }
22378 else
22379 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22380
22381 /* If ARG is a parameter pack or an expansion, we cannot unify
22382 against it unless PARM is also a parameter pack. */
22383 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22384 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22385 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22386
22387 {
22388 bool removed_attr = false;
22389 arg = strip_typedefs_expr (arg, &removed_attr);
22390 }
22391 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22392 return unify_success (explain_p);
22393
22394 case PTRMEM_CST:
22395 {
22396 /* A pointer-to-member constant can be unified only with
22397 another constant. */
22398 if (TREE_CODE (arg) != PTRMEM_CST)
22399 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22400
22401 /* Just unify the class member. It would be useless (and possibly
22402 wrong, depending on the strict flags) to unify also
22403 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22404 arg refer to the same variable, even if through different
22405 classes. For instance:
22406
22407 struct A { int x; };
22408 struct B : A { };
22409
22410 Unification of &A::x and &B::x must succeed. */
22411 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22412 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22413 }
22414
22415 case POINTER_TYPE:
22416 {
22417 if (!TYPE_PTR_P (arg))
22418 return unify_type_mismatch (explain_p, parm, arg);
22419
22420 /* [temp.deduct.call]
22421
22422 A can be another pointer or pointer to member type that can
22423 be converted to the deduced A via a qualification
22424 conversion (_conv.qual_).
22425
22426 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22427 This will allow for additional cv-qualification of the
22428 pointed-to types if appropriate. */
22429
22430 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
22431 /* The derived-to-base conversion only persists through one
22432 level of pointers. */
22433 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
22434
22435 return unify (tparms, targs, TREE_TYPE (parm),
22436 TREE_TYPE (arg), strict, explain_p);
22437 }
22438
22439 case REFERENCE_TYPE:
22440 if (!TYPE_REF_P (arg))
22441 return unify_type_mismatch (explain_p, parm, arg);
22442 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22443 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22444
22445 case ARRAY_TYPE:
22446 if (TREE_CODE (arg) != ARRAY_TYPE)
22447 return unify_type_mismatch (explain_p, parm, arg);
22448 if ((TYPE_DOMAIN (parm) == NULL_TREE)
22449 != (TYPE_DOMAIN (arg) == NULL_TREE))
22450 return unify_type_mismatch (explain_p, parm, arg);
22451 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22452 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22453 if (TYPE_DOMAIN (parm) != NULL_TREE)
22454 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22455 TYPE_DOMAIN (arg), explain_p);
22456 return unify_success (explain_p);
22457
22458 case REAL_TYPE:
22459 case COMPLEX_TYPE:
22460 case VECTOR_TYPE:
22461 case INTEGER_TYPE:
22462 case BOOLEAN_TYPE:
22463 case ENUMERAL_TYPE:
22464 case VOID_TYPE:
22465 case NULLPTR_TYPE:
22466 if (TREE_CODE (arg) != TREE_CODE (parm))
22467 return unify_type_mismatch (explain_p, parm, arg);
22468
22469 /* We have already checked cv-qualification at the top of the
22470 function. */
22471 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22472 return unify_type_mismatch (explain_p, parm, arg);
22473
22474 /* As far as unification is concerned, this wins. Later checks
22475 will invalidate it if necessary. */
22476 return unify_success (explain_p);
22477
22478 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22479 /* Type INTEGER_CST can come from ordinary constant template args. */
22480 case INTEGER_CST:
22481 while (CONVERT_EXPR_P (arg))
22482 arg = TREE_OPERAND (arg, 0);
22483
22484 if (TREE_CODE (arg) != INTEGER_CST)
22485 return unify_template_argument_mismatch (explain_p, parm, arg);
22486 return (tree_int_cst_equal (parm, arg)
22487 ? unify_success (explain_p)
22488 : unify_template_argument_mismatch (explain_p, parm, arg));
22489
22490 case TREE_VEC:
22491 {
22492 int i, len, argslen;
22493 int parm_variadic_p = 0;
22494
22495 if (TREE_CODE (arg) != TREE_VEC)
22496 return unify_template_argument_mismatch (explain_p, parm, arg);
22497
22498 len = TREE_VEC_LENGTH (parm);
22499 argslen = TREE_VEC_LENGTH (arg);
22500
22501 /* Check for pack expansions in the parameters. */
22502 for (i = 0; i < len; ++i)
22503 {
22504 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22505 {
22506 if (i == len - 1)
22507 /* We can unify against something with a trailing
22508 parameter pack. */
22509 parm_variadic_p = 1;
22510 else
22511 /* [temp.deduct.type]/9: If the template argument list of
22512 P contains a pack expansion that is not the last
22513 template argument, the entire template argument list
22514 is a non-deduced context. */
22515 return unify_success (explain_p);
22516 }
22517 }
22518
22519 /* If we don't have enough arguments to satisfy the parameters
22520 (not counting the pack expression at the end), or we have
22521 too many arguments for a parameter list that doesn't end in
22522 a pack expression, we can't unify. */
22523 if (parm_variadic_p
22524 ? argslen < len - parm_variadic_p
22525 : argslen != len)
22526 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22527
22528 /* Unify all of the parameters that precede the (optional)
22529 pack expression. */
22530 for (i = 0; i < len - parm_variadic_p; ++i)
22531 {
22532 RECUR_AND_CHECK_FAILURE (tparms, targs,
22533 TREE_VEC_ELT (parm, i),
22534 TREE_VEC_ELT (arg, i),
22535 UNIFY_ALLOW_NONE, explain_p);
22536 }
22537 if (parm_variadic_p)
22538 return unify_pack_expansion (tparms, targs, parm, arg,
22539 DEDUCE_EXACT,
22540 /*subr=*/true, explain_p);
22541 return unify_success (explain_p);
22542 }
22543
22544 case RECORD_TYPE:
22545 case UNION_TYPE:
22546 if (TREE_CODE (arg) != TREE_CODE (parm))
22547 return unify_type_mismatch (explain_p, parm, arg);
22548
22549 if (TYPE_PTRMEMFUNC_P (parm))
22550 {
22551 if (!TYPE_PTRMEMFUNC_P (arg))
22552 return unify_type_mismatch (explain_p, parm, arg);
22553
22554 return unify (tparms, targs,
22555 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22556 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22557 strict, explain_p);
22558 }
22559 else if (TYPE_PTRMEMFUNC_P (arg))
22560 return unify_type_mismatch (explain_p, parm, arg);
22561
22562 if (CLASSTYPE_TEMPLATE_INFO (parm))
22563 {
22564 tree t = NULL_TREE;
22565
22566 if (strict_in & UNIFY_ALLOW_DERIVED)
22567 {
22568 /* First, we try to unify the PARM and ARG directly. */
22569 t = try_class_unification (tparms, targs,
22570 parm, arg, explain_p);
22571
22572 if (!t)
22573 {
22574 /* Fallback to the special case allowed in
22575 [temp.deduct.call]:
22576
22577 If P is a class, and P has the form
22578 template-id, then A can be a derived class of
22579 the deduced A. Likewise, if P is a pointer to
22580 a class of the form template-id, A can be a
22581 pointer to a derived class pointed to by the
22582 deduced A. */
22583 enum template_base_result r;
22584 r = get_template_base (tparms, targs, parm, arg,
22585 explain_p, &t);
22586
22587 if (!t)
22588 {
22589 /* Don't give the derived diagnostic if we're
22590 already dealing with the same template. */
22591 bool same_template
22592 = (CLASSTYPE_TEMPLATE_INFO (arg)
22593 && (CLASSTYPE_TI_TEMPLATE (parm)
22594 == CLASSTYPE_TI_TEMPLATE (arg)));
22595 return unify_no_common_base (explain_p && !same_template,
22596 r, parm, arg);
22597 }
22598 }
22599 }
22600 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22601 && (CLASSTYPE_TI_TEMPLATE (parm)
22602 == CLASSTYPE_TI_TEMPLATE (arg)))
22603 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22604 Then, we should unify `int' and `U'. */
22605 t = arg;
22606 else
22607 /* There's no chance of unification succeeding. */
22608 return unify_type_mismatch (explain_p, parm, arg);
22609
22610 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22611 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22612 }
22613 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22614 return unify_type_mismatch (explain_p, parm, arg);
22615 return unify_success (explain_p);
22616
22617 case METHOD_TYPE:
22618 case FUNCTION_TYPE:
22619 {
22620 unsigned int nargs;
22621 tree *args;
22622 tree a;
22623 unsigned int i;
22624
22625 if (TREE_CODE (arg) != TREE_CODE (parm))
22626 return unify_type_mismatch (explain_p, parm, arg);
22627
22628 /* CV qualifications for methods can never be deduced, they must
22629 match exactly. We need to check them explicitly here,
22630 because type_unification_real treats them as any other
22631 cv-qualified parameter. */
22632 if (TREE_CODE (parm) == METHOD_TYPE
22633 && (!check_cv_quals_for_unify
22634 (UNIFY_ALLOW_NONE,
22635 class_of_this_parm (arg),
22636 class_of_this_parm (parm))))
22637 return unify_cv_qual_mismatch (explain_p, parm, arg);
22638 if (TREE_CODE (arg) == FUNCTION_TYPE
22639 && type_memfn_quals (parm) != type_memfn_quals (arg))
22640 return unify_cv_qual_mismatch (explain_p, parm, arg);
22641 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22642 return unify_type_mismatch (explain_p, parm, arg);
22643
22644 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22645 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22646
22647 nargs = list_length (TYPE_ARG_TYPES (arg));
22648 args = XALLOCAVEC (tree, nargs);
22649 for (a = TYPE_ARG_TYPES (arg), i = 0;
22650 a != NULL_TREE && a != void_list_node;
22651 a = TREE_CHAIN (a), ++i)
22652 args[i] = TREE_VALUE (a);
22653 nargs = i;
22654
22655 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22656 args, nargs, 1, DEDUCE_EXACT,
22657 NULL, explain_p))
22658 return 1;
22659
22660 if (flag_noexcept_type)
22661 {
22662 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22663 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22664 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22665 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22666 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22667 && uses_template_parms (TREE_PURPOSE (pspec)))
22668 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22669 TREE_PURPOSE (aspec),
22670 UNIFY_ALLOW_NONE, explain_p);
22671 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22672 return unify_type_mismatch (explain_p, parm, arg);
22673 }
22674
22675 return 0;
22676 }
22677
22678 case OFFSET_TYPE:
22679 /* Unify a pointer to member with a pointer to member function, which
22680 deduces the type of the member as a function type. */
22681 if (TYPE_PTRMEMFUNC_P (arg))
22682 {
22683 /* Check top-level cv qualifiers */
22684 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22685 return unify_cv_qual_mismatch (explain_p, parm, arg);
22686
22687 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22688 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22689 UNIFY_ALLOW_NONE, explain_p);
22690
22691 /* Determine the type of the function we are unifying against. */
22692 tree fntype = static_fn_type (arg);
22693
22694 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22695 }
22696
22697 if (TREE_CODE (arg) != OFFSET_TYPE)
22698 return unify_type_mismatch (explain_p, parm, arg);
22699 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22700 TYPE_OFFSET_BASETYPE (arg),
22701 UNIFY_ALLOW_NONE, explain_p);
22702 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22703 strict, explain_p);
22704
22705 case CONST_DECL:
22706 if (DECL_TEMPLATE_PARM_P (parm))
22707 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22708 if (arg != scalar_constant_value (parm))
22709 return unify_template_argument_mismatch (explain_p, parm, arg);
22710 return unify_success (explain_p);
22711
22712 case FIELD_DECL:
22713 case TEMPLATE_DECL:
22714 /* Matched cases are handled by the ARG == PARM test above. */
22715 return unify_template_argument_mismatch (explain_p, parm, arg);
22716
22717 case VAR_DECL:
22718 /* We might get a variable as a non-type template argument in parm if the
22719 corresponding parameter is type-dependent. Make any necessary
22720 adjustments based on whether arg is a reference. */
22721 if (CONSTANT_CLASS_P (arg))
22722 parm = fold_non_dependent_expr (parm, complain);
22723 else if (REFERENCE_REF_P (arg))
22724 {
22725 tree sub = TREE_OPERAND (arg, 0);
22726 STRIP_NOPS (sub);
22727 if (TREE_CODE (sub) == ADDR_EXPR)
22728 arg = TREE_OPERAND (sub, 0);
22729 }
22730 /* Now use the normal expression code to check whether they match. */
22731 goto expr;
22732
22733 case TYPE_ARGUMENT_PACK:
22734 case NONTYPE_ARGUMENT_PACK:
22735 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22736 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22737
22738 case TYPEOF_TYPE:
22739 case DECLTYPE_TYPE:
22740 case UNDERLYING_TYPE:
22741 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22742 or UNDERLYING_TYPE nodes. */
22743 return unify_success (explain_p);
22744
22745 case ERROR_MARK:
22746 /* Unification fails if we hit an error node. */
22747 return unify_invalid (explain_p);
22748
22749 case INDIRECT_REF:
22750 if (REFERENCE_REF_P (parm))
22751 {
22752 bool pexp = PACK_EXPANSION_P (arg);
22753 if (pexp)
22754 arg = PACK_EXPANSION_PATTERN (arg);
22755 if (REFERENCE_REF_P (arg))
22756 arg = TREE_OPERAND (arg, 0);
22757 if (pexp)
22758 arg = make_pack_expansion (arg, complain);
22759 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22760 strict, explain_p);
22761 }
22762 /* FALLTHRU */
22763
22764 default:
22765 /* An unresolved overload is a nondeduced context. */
22766 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22767 return unify_success (explain_p);
22768 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
22769 expr:
22770 /* We must be looking at an expression. This can happen with
22771 something like:
22772
22773 template <int I>
22774 void foo(S<I>, S<I + 2>);
22775
22776 This is a "nondeduced context":
22777
22778 [deduct.type]
22779
22780 The nondeduced contexts are:
22781
22782 --A type that is a template-id in which one or more of
22783 the template-arguments is an expression that references
22784 a template-parameter.
22785
22786 In these cases, we assume deduction succeeded, but don't
22787 actually infer any unifications. */
22788
22789 if (!uses_template_parms (parm)
22790 && !template_args_equal (parm, arg))
22791 return unify_expression_unequal (explain_p, parm, arg);
22792 else
22793 return unify_success (explain_p);
22794 }
22795 }
22796 #undef RECUR_AND_CHECK_FAILURE
22797 \f
22798 /* Note that DECL can be defined in this translation unit, if
22799 required. */
22800
22801 static void
22802 mark_definable (tree decl)
22803 {
22804 tree clone;
22805 DECL_NOT_REALLY_EXTERN (decl) = 1;
22806 FOR_EACH_CLONE (clone, decl)
22807 DECL_NOT_REALLY_EXTERN (clone) = 1;
22808 }
22809
22810 /* Called if RESULT is explicitly instantiated, or is a member of an
22811 explicitly instantiated class. */
22812
22813 void
22814 mark_decl_instantiated (tree result, int extern_p)
22815 {
22816 SET_DECL_EXPLICIT_INSTANTIATION (result);
22817
22818 /* If this entity has already been written out, it's too late to
22819 make any modifications. */
22820 if (TREE_ASM_WRITTEN (result))
22821 return;
22822
22823 /* For anonymous namespace we don't need to do anything. */
22824 if (decl_anon_ns_mem_p (result))
22825 {
22826 gcc_assert (!TREE_PUBLIC (result));
22827 return;
22828 }
22829
22830 if (TREE_CODE (result) != FUNCTION_DECL)
22831 /* The TREE_PUBLIC flag for function declarations will have been
22832 set correctly by tsubst. */
22833 TREE_PUBLIC (result) = 1;
22834
22835 /* This might have been set by an earlier implicit instantiation. */
22836 DECL_COMDAT (result) = 0;
22837
22838 if (extern_p)
22839 DECL_NOT_REALLY_EXTERN (result) = 0;
22840 else
22841 {
22842 mark_definable (result);
22843 mark_needed (result);
22844 /* Always make artificials weak. */
22845 if (DECL_ARTIFICIAL (result) && flag_weak)
22846 comdat_linkage (result);
22847 /* For WIN32 we also want to put explicit instantiations in
22848 linkonce sections. */
22849 else if (TREE_PUBLIC (result))
22850 maybe_make_one_only (result);
22851 if (TREE_CODE (result) == FUNCTION_DECL
22852 && DECL_TEMPLATE_INSTANTIATED (result))
22853 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22854 since start_preparsed_function wouldn't have if we had an earlier
22855 extern explicit instantiation. */
22856 DECL_EXTERNAL (result) = 0;
22857 }
22858
22859 /* If EXTERN_P, then this function will not be emitted -- unless
22860 followed by an explicit instantiation, at which point its linkage
22861 will be adjusted. If !EXTERN_P, then this function will be
22862 emitted here. In neither circumstance do we want
22863 import_export_decl to adjust the linkage. */
22864 DECL_INTERFACE_KNOWN (result) = 1;
22865 }
22866
22867 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22868 important template arguments. If any are missing, we check whether
22869 they're important by using error_mark_node for substituting into any
22870 args that were used for partial ordering (the ones between ARGS and END)
22871 and seeing if it bubbles up. */
22872
22873 static bool
22874 check_undeduced_parms (tree targs, tree args, tree end)
22875 {
22876 bool found = false;
22877 int i;
22878 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22879 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22880 {
22881 found = true;
22882 TREE_VEC_ELT (targs, i) = error_mark_node;
22883 }
22884 if (found)
22885 {
22886 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22887 if (substed == error_mark_node)
22888 return true;
22889 }
22890 return false;
22891 }
22892
22893 /* Given two function templates PAT1 and PAT2, return:
22894
22895 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22896 -1 if PAT2 is more specialized than PAT1.
22897 0 if neither is more specialized.
22898
22899 LEN indicates the number of parameters we should consider
22900 (defaulted parameters should not be considered).
22901
22902 The 1998 std underspecified function template partial ordering, and
22903 DR214 addresses the issue. We take pairs of arguments, one from
22904 each of the templates, and deduce them against each other. One of
22905 the templates will be more specialized if all the *other*
22906 template's arguments deduce against its arguments and at least one
22907 of its arguments *does* *not* deduce against the other template's
22908 corresponding argument. Deduction is done as for class templates.
22909 The arguments used in deduction have reference and top level cv
22910 qualifiers removed. Iff both arguments were originally reference
22911 types *and* deduction succeeds in both directions, an lvalue reference
22912 wins against an rvalue reference and otherwise the template
22913 with the more cv-qualified argument wins for that pairing (if
22914 neither is more cv-qualified, they both are equal). Unlike regular
22915 deduction, after all the arguments have been deduced in this way,
22916 we do *not* verify the deduced template argument values can be
22917 substituted into non-deduced contexts.
22918
22919 The logic can be a bit confusing here, because we look at deduce1 and
22920 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22921 can find template arguments for pat1 to make arg1 look like arg2, that
22922 means that arg2 is at least as specialized as arg1. */
22923
22924 int
22925 more_specialized_fn (tree pat1, tree pat2, int len)
22926 {
22927 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22928 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22929 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22930 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22931 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22932 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22933 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22934 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22935 tree origs1, origs2;
22936 bool lose1 = false;
22937 bool lose2 = false;
22938
22939 /* Remove the this parameter from non-static member functions. If
22940 one is a non-static member function and the other is not a static
22941 member function, remove the first parameter from that function
22942 also. This situation occurs for operator functions where we
22943 locate both a member function (with this pointer) and non-member
22944 operator (with explicit first operand). */
22945 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22946 {
22947 len--; /* LEN is the number of significant arguments for DECL1 */
22948 args1 = TREE_CHAIN (args1);
22949 if (!DECL_STATIC_FUNCTION_P (decl2))
22950 args2 = TREE_CHAIN (args2);
22951 }
22952 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22953 {
22954 args2 = TREE_CHAIN (args2);
22955 if (!DECL_STATIC_FUNCTION_P (decl1))
22956 {
22957 len--;
22958 args1 = TREE_CHAIN (args1);
22959 }
22960 }
22961
22962 /* If only one is a conversion operator, they are unordered. */
22963 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22964 return 0;
22965
22966 /* Consider the return type for a conversion function */
22967 if (DECL_CONV_FN_P (decl1))
22968 {
22969 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22970 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22971 len++;
22972 }
22973
22974 processing_template_decl++;
22975
22976 origs1 = args1;
22977 origs2 = args2;
22978
22979 while (len--
22980 /* Stop when an ellipsis is seen. */
22981 && args1 != NULL_TREE && args2 != NULL_TREE)
22982 {
22983 tree arg1 = TREE_VALUE (args1);
22984 tree arg2 = TREE_VALUE (args2);
22985 int deduce1, deduce2;
22986 int quals1 = -1;
22987 int quals2 = -1;
22988 int ref1 = 0;
22989 int ref2 = 0;
22990
22991 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22992 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22993 {
22994 /* When both arguments are pack expansions, we need only
22995 unify the patterns themselves. */
22996 arg1 = PACK_EXPANSION_PATTERN (arg1);
22997 arg2 = PACK_EXPANSION_PATTERN (arg2);
22998
22999 /* This is the last comparison we need to do. */
23000 len = 0;
23001 }
23002
23003 /* DR 1847: If a particular P contains no template-parameters that
23004 participate in template argument deduction, that P is not used to
23005 determine the ordering. */
23006 if (!uses_deducible_template_parms (arg1)
23007 && !uses_deducible_template_parms (arg2))
23008 goto next;
23009
23010 if (TYPE_REF_P (arg1))
23011 {
23012 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23013 arg1 = TREE_TYPE (arg1);
23014 quals1 = cp_type_quals (arg1);
23015 }
23016
23017 if (TYPE_REF_P (arg2))
23018 {
23019 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23020 arg2 = TREE_TYPE (arg2);
23021 quals2 = cp_type_quals (arg2);
23022 }
23023
23024 arg1 = TYPE_MAIN_VARIANT (arg1);
23025 arg2 = TYPE_MAIN_VARIANT (arg2);
23026
23027 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23028 {
23029 int i, len2 = remaining_arguments (args2);
23030 tree parmvec = make_tree_vec (1);
23031 tree argvec = make_tree_vec (len2);
23032 tree ta = args2;
23033
23034 /* Setup the parameter vector, which contains only ARG1. */
23035 TREE_VEC_ELT (parmvec, 0) = arg1;
23036
23037 /* Setup the argument vector, which contains the remaining
23038 arguments. */
23039 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23040 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23041
23042 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23043 argvec, DEDUCE_EXACT,
23044 /*subr=*/true, /*explain_p=*/false)
23045 == 0);
23046
23047 /* We cannot deduce in the other direction, because ARG1 is
23048 a pack expansion but ARG2 is not. */
23049 deduce2 = 0;
23050 }
23051 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23052 {
23053 int i, len1 = remaining_arguments (args1);
23054 tree parmvec = make_tree_vec (1);
23055 tree argvec = make_tree_vec (len1);
23056 tree ta = args1;
23057
23058 /* Setup the parameter vector, which contains only ARG1. */
23059 TREE_VEC_ELT (parmvec, 0) = arg2;
23060
23061 /* Setup the argument vector, which contains the remaining
23062 arguments. */
23063 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23064 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23065
23066 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23067 argvec, DEDUCE_EXACT,
23068 /*subr=*/true, /*explain_p=*/false)
23069 == 0);
23070
23071 /* We cannot deduce in the other direction, because ARG2 is
23072 a pack expansion but ARG1 is not.*/
23073 deduce1 = 0;
23074 }
23075
23076 else
23077 {
23078 /* The normal case, where neither argument is a pack
23079 expansion. */
23080 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23081 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23082 == 0);
23083 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23084 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23085 == 0);
23086 }
23087
23088 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23089 arg2, then arg2 is not as specialized as arg1. */
23090 if (!deduce1)
23091 lose2 = true;
23092 if (!deduce2)
23093 lose1 = true;
23094
23095 /* "If, for a given type, deduction succeeds in both directions
23096 (i.e., the types are identical after the transformations above)
23097 and both P and A were reference types (before being replaced with
23098 the type referred to above):
23099 - if the type from the argument template was an lvalue reference and
23100 the type from the parameter template was not, the argument type is
23101 considered to be more specialized than the other; otherwise,
23102 - if the type from the argument template is more cv-qualified
23103 than the type from the parameter template (as described above),
23104 the argument type is considered to be more specialized than the other;
23105 otherwise,
23106 - neither type is more specialized than the other." */
23107
23108 if (deduce1 && deduce2)
23109 {
23110 if (ref1 && ref2 && ref1 != ref2)
23111 {
23112 if (ref1 > ref2)
23113 lose1 = true;
23114 else
23115 lose2 = true;
23116 }
23117 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23118 {
23119 if ((quals1 & quals2) == quals2)
23120 lose2 = true;
23121 if ((quals1 & quals2) == quals1)
23122 lose1 = true;
23123 }
23124 }
23125
23126 if (lose1 && lose2)
23127 /* We've failed to deduce something in either direction.
23128 These must be unordered. */
23129 break;
23130
23131 next:
23132
23133 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23134 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23135 /* We have already processed all of the arguments in our
23136 handing of the pack expansion type. */
23137 len = 0;
23138
23139 args1 = TREE_CHAIN (args1);
23140 args2 = TREE_CHAIN (args2);
23141 }
23142
23143 /* "In most cases, all template parameters must have values in order for
23144 deduction to succeed, but for partial ordering purposes a template
23145 parameter may remain without a value provided it is not used in the
23146 types being used for partial ordering."
23147
23148 Thus, if we are missing any of the targs1 we need to substitute into
23149 origs1, then pat2 is not as specialized as pat1. This can happen when
23150 there is a nondeduced context. */
23151 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23152 lose2 = true;
23153 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23154 lose1 = true;
23155
23156 processing_template_decl--;
23157
23158 /* If both deductions succeed, the partial ordering selects the more
23159 constrained template. */
23160 if (!lose1 && !lose2)
23161 {
23162 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
23163 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
23164 lose1 = !subsumes_constraints (c1, c2);
23165 lose2 = !subsumes_constraints (c2, c1);
23166 }
23167
23168 /* All things being equal, if the next argument is a pack expansion
23169 for one function but not for the other, prefer the
23170 non-variadic function. FIXME this is bogus; see c++/41958. */
23171 if (lose1 == lose2
23172 && args1 && TREE_VALUE (args1)
23173 && args2 && TREE_VALUE (args2))
23174 {
23175 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23176 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23177 }
23178
23179 if (lose1 == lose2)
23180 return 0;
23181 else if (!lose1)
23182 return 1;
23183 else
23184 return -1;
23185 }
23186
23187 /* Determine which of two partial specializations of TMPL is more
23188 specialized.
23189
23190 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23191 to the first partial specialization. The TREE_PURPOSE is the
23192 innermost set of template parameters for the partial
23193 specialization. PAT2 is similar, but for the second template.
23194
23195 Return 1 if the first partial specialization is more specialized;
23196 -1 if the second is more specialized; 0 if neither is more
23197 specialized.
23198
23199 See [temp.class.order] for information about determining which of
23200 two templates is more specialized. */
23201
23202 static int
23203 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23204 {
23205 tree targs;
23206 int winner = 0;
23207 bool any_deductions = false;
23208
23209 tree tmpl1 = TREE_VALUE (pat1);
23210 tree tmpl2 = TREE_VALUE (pat2);
23211 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23212 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23213
23214 /* Just like what happens for functions, if we are ordering between
23215 different template specializations, we may encounter dependent
23216 types in the arguments, and we need our dependency check functions
23217 to behave correctly. */
23218 ++processing_template_decl;
23219 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23220 if (targs)
23221 {
23222 --winner;
23223 any_deductions = true;
23224 }
23225
23226 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23227 if (targs)
23228 {
23229 ++winner;
23230 any_deductions = true;
23231 }
23232 --processing_template_decl;
23233
23234 /* If both deductions succeed, the partial ordering selects the more
23235 constrained template. */
23236 if (!winner && any_deductions)
23237 return more_constrained (tmpl1, tmpl2);
23238
23239 /* In the case of a tie where at least one of the templates
23240 has a parameter pack at the end, the template with the most
23241 non-packed parameters wins. */
23242 if (winner == 0
23243 && any_deductions
23244 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23245 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23246 {
23247 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23248 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23249 int len1 = TREE_VEC_LENGTH (args1);
23250 int len2 = TREE_VEC_LENGTH (args2);
23251
23252 /* We don't count the pack expansion at the end. */
23253 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23254 --len1;
23255 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23256 --len2;
23257
23258 if (len1 > len2)
23259 return 1;
23260 else if (len1 < len2)
23261 return -1;
23262 }
23263
23264 return winner;
23265 }
23266
23267 /* Return the template arguments that will produce the function signature
23268 DECL from the function template FN, with the explicit template
23269 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23270 also match. Return NULL_TREE if no satisfactory arguments could be
23271 found. */
23272
23273 static tree
23274 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23275 {
23276 int ntparms = DECL_NTPARMS (fn);
23277 tree targs = make_tree_vec (ntparms);
23278 tree decl_type = TREE_TYPE (decl);
23279 tree decl_arg_types;
23280 tree *args;
23281 unsigned int nargs, ix;
23282 tree arg;
23283
23284 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23285
23286 /* Never do unification on the 'this' parameter. */
23287 decl_arg_types = skip_artificial_parms_for (decl,
23288 TYPE_ARG_TYPES (decl_type));
23289
23290 nargs = list_length (decl_arg_types);
23291 args = XALLOCAVEC (tree, nargs);
23292 for (arg = decl_arg_types, ix = 0;
23293 arg != NULL_TREE && arg != void_list_node;
23294 arg = TREE_CHAIN (arg), ++ix)
23295 args[ix] = TREE_VALUE (arg);
23296
23297 if (fn_type_unification (fn, explicit_args, targs,
23298 args, ix,
23299 (check_rettype || DECL_CONV_FN_P (fn)
23300 ? TREE_TYPE (decl_type) : NULL_TREE),
23301 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23302 /*explain_p=*/false,
23303 /*decltype*/false)
23304 == error_mark_node)
23305 return NULL_TREE;
23306
23307 return targs;
23308 }
23309
23310 /* Return the innermost template arguments that, when applied to a partial
23311 specialization SPEC_TMPL of TMPL, yield the ARGS.
23312
23313 For example, suppose we have:
23314
23315 template <class T, class U> struct S {};
23316 template <class T> struct S<T*, int> {};
23317
23318 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23319 partial specialization and the ARGS will be {double*, int}. The resulting
23320 vector will be {double}, indicating that `T' is bound to `double'. */
23321
23322 static tree
23323 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23324 {
23325 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23326 tree spec_args
23327 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23328 int i, ntparms = TREE_VEC_LENGTH (tparms);
23329 tree deduced_args;
23330 tree innermost_deduced_args;
23331
23332 innermost_deduced_args = make_tree_vec (ntparms);
23333 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23334 {
23335 deduced_args = copy_node (args);
23336 SET_TMPL_ARGS_LEVEL (deduced_args,
23337 TMPL_ARGS_DEPTH (deduced_args),
23338 innermost_deduced_args);
23339 }
23340 else
23341 deduced_args = innermost_deduced_args;
23342
23343 bool tried_array_deduction = (cxx_dialect < cxx17);
23344 again:
23345 if (unify (tparms, deduced_args,
23346 INNERMOST_TEMPLATE_ARGS (spec_args),
23347 INNERMOST_TEMPLATE_ARGS (args),
23348 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23349 return NULL_TREE;
23350
23351 for (i = 0; i < ntparms; ++i)
23352 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23353 {
23354 if (!tried_array_deduction)
23355 {
23356 try_array_deduction (tparms, innermost_deduced_args,
23357 INNERMOST_TEMPLATE_ARGS (spec_args));
23358 tried_array_deduction = true;
23359 if (TREE_VEC_ELT (innermost_deduced_args, i))
23360 goto again;
23361 }
23362 return NULL_TREE;
23363 }
23364
23365 if (!push_tinst_level (spec_tmpl, deduced_args))
23366 {
23367 excessive_deduction_depth = true;
23368 return NULL_TREE;
23369 }
23370
23371 /* Verify that nondeduced template arguments agree with the type
23372 obtained from argument deduction.
23373
23374 For example:
23375
23376 struct A { typedef int X; };
23377 template <class T, class U> struct C {};
23378 template <class T> struct C<T, typename T::X> {};
23379
23380 Then with the instantiation `C<A, int>', we can deduce that
23381 `T' is `A' but unify () does not check whether `typename T::X'
23382 is `int'. */
23383 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23384
23385 if (spec_args != error_mark_node)
23386 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23387 INNERMOST_TEMPLATE_ARGS (spec_args),
23388 tmpl, tf_none, false, false);
23389
23390 pop_tinst_level ();
23391
23392 if (spec_args == error_mark_node
23393 /* We only need to check the innermost arguments; the other
23394 arguments will always agree. */
23395 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23396 INNERMOST_TEMPLATE_ARGS (args)))
23397 return NULL_TREE;
23398
23399 /* Now that we have bindings for all of the template arguments,
23400 ensure that the arguments deduced for the template template
23401 parameters have compatible template parameter lists. See the use
23402 of template_template_parm_bindings_ok_p in fn_type_unification
23403 for more information. */
23404 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23405 return NULL_TREE;
23406
23407 return deduced_args;
23408 }
23409
23410 // Compare two function templates T1 and T2 by deducing bindings
23411 // from one against the other. If both deductions succeed, compare
23412 // constraints to see which is more constrained.
23413 static int
23414 more_specialized_inst (tree t1, tree t2)
23415 {
23416 int fate = 0;
23417 int count = 0;
23418
23419 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
23420 {
23421 --fate;
23422 ++count;
23423 }
23424
23425 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
23426 {
23427 ++fate;
23428 ++count;
23429 }
23430
23431 // If both deductions succeed, then one may be more constrained.
23432 if (count == 2 && fate == 0)
23433 fate = more_constrained (t1, t2);
23434
23435 return fate;
23436 }
23437
23438 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23439 Return the TREE_LIST node with the most specialized template, if
23440 any. If there is no most specialized template, the error_mark_node
23441 is returned.
23442
23443 Note that this function does not look at, or modify, the
23444 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23445 returned is one of the elements of INSTANTIATIONS, callers may
23446 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23447 and retrieve it from the value returned. */
23448
23449 tree
23450 most_specialized_instantiation (tree templates)
23451 {
23452 tree fn, champ;
23453
23454 ++processing_template_decl;
23455
23456 champ = templates;
23457 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
23458 {
23459 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
23460 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
23461 if (fate == -1)
23462 champ = fn;
23463 else if (!fate)
23464 {
23465 /* Equally specialized, move to next function. If there
23466 is no next function, nothing's most specialized. */
23467 fn = TREE_CHAIN (fn);
23468 champ = fn;
23469 if (!fn)
23470 break;
23471 }
23472 }
23473
23474 if (champ)
23475 /* Now verify that champ is better than everything earlier in the
23476 instantiation list. */
23477 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23478 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23479 {
23480 champ = NULL_TREE;
23481 break;
23482 }
23483 }
23484
23485 processing_template_decl--;
23486
23487 if (!champ)
23488 return error_mark_node;
23489
23490 return champ;
23491 }
23492
23493 /* If DECL is a specialization of some template, return the most
23494 general such template. Otherwise, returns NULL_TREE.
23495
23496 For example, given:
23497
23498 template <class T> struct S { template <class U> void f(U); };
23499
23500 if TMPL is `template <class U> void S<int>::f(U)' this will return
23501 the full template. This function will not trace past partial
23502 specializations, however. For example, given in addition:
23503
23504 template <class T> struct S<T*> { template <class U> void f(U); };
23505
23506 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23507 `template <class T> template <class U> S<T*>::f(U)'. */
23508
23509 tree
23510 most_general_template (tree decl)
23511 {
23512 if (TREE_CODE (decl) != TEMPLATE_DECL)
23513 {
23514 if (tree tinfo = get_template_info (decl))
23515 decl = TI_TEMPLATE (tinfo);
23516 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23517 template friend, or a FIELD_DECL for a capture pack. */
23518 if (TREE_CODE (decl) != TEMPLATE_DECL)
23519 return NULL_TREE;
23520 }
23521
23522 /* Look for more and more general templates. */
23523 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23524 {
23525 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23526 (See cp-tree.h for details.) */
23527 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23528 break;
23529
23530 if (CLASS_TYPE_P (TREE_TYPE (decl))
23531 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23532 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23533 break;
23534
23535 /* Stop if we run into an explicitly specialized class template. */
23536 if (!DECL_NAMESPACE_SCOPE_P (decl)
23537 && DECL_CONTEXT (decl)
23538 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23539 break;
23540
23541 decl = DECL_TI_TEMPLATE (decl);
23542 }
23543
23544 return decl;
23545 }
23546
23547 /* Return the most specialized of the template partial specializations
23548 which can produce TARGET, a specialization of some class or variable
23549 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23550 a TEMPLATE_DECL node corresponding to the partial specialization, while
23551 the TREE_PURPOSE is the set of template arguments that must be
23552 substituted into the template pattern in order to generate TARGET.
23553
23554 If the choice of partial specialization is ambiguous, a diagnostic
23555 is issued, and the error_mark_node is returned. If there are no
23556 partial specializations matching TARGET, then NULL_TREE is
23557 returned, indicating that the primary template should be used. */
23558
23559 static tree
23560 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23561 {
23562 tree list = NULL_TREE;
23563 tree t;
23564 tree champ;
23565 int fate;
23566 bool ambiguous_p;
23567 tree outer_args = NULL_TREE;
23568 tree tmpl, args;
23569
23570 if (TYPE_P (target))
23571 {
23572 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23573 tmpl = TI_TEMPLATE (tinfo);
23574 args = TI_ARGS (tinfo);
23575 }
23576 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23577 {
23578 tmpl = TREE_OPERAND (target, 0);
23579 args = TREE_OPERAND (target, 1);
23580 }
23581 else if (VAR_P (target))
23582 {
23583 tree tinfo = DECL_TEMPLATE_INFO (target);
23584 tmpl = TI_TEMPLATE (tinfo);
23585 args = TI_ARGS (tinfo);
23586 }
23587 else
23588 gcc_unreachable ();
23589
23590 tree main_tmpl = most_general_template (tmpl);
23591
23592 /* For determining which partial specialization to use, only the
23593 innermost args are interesting. */
23594 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23595 {
23596 outer_args = strip_innermost_template_args (args, 1);
23597 args = INNERMOST_TEMPLATE_ARGS (args);
23598 }
23599
23600 /* The caller hasn't called push_to_top_level yet, but we need
23601 get_partial_spec_bindings to be done in non-template context so that we'll
23602 fully resolve everything. */
23603 processing_template_decl_sentinel ptds;
23604
23605 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23606 {
23607 tree spec_args;
23608 tree spec_tmpl = TREE_VALUE (t);
23609
23610 if (outer_args)
23611 {
23612 /* Substitute in the template args from the enclosing class. */
23613 ++processing_template_decl;
23614 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23615 --processing_template_decl;
23616 }
23617
23618 if (spec_tmpl == error_mark_node)
23619 return error_mark_node;
23620
23621 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23622 if (spec_args)
23623 {
23624 if (outer_args)
23625 spec_args = add_to_template_args (outer_args, spec_args);
23626
23627 /* Keep the candidate only if the constraints are satisfied,
23628 or if we're not compiling with concepts. */
23629 if (!flag_concepts
23630 || constraints_satisfied_p (spec_tmpl, spec_args))
23631 {
23632 list = tree_cons (spec_args, TREE_VALUE (t), list);
23633 TREE_TYPE (list) = TREE_TYPE (t);
23634 }
23635 }
23636 }
23637
23638 if (! list)
23639 return NULL_TREE;
23640
23641 ambiguous_p = false;
23642 t = list;
23643 champ = t;
23644 t = TREE_CHAIN (t);
23645 for (; t; t = TREE_CHAIN (t))
23646 {
23647 fate = more_specialized_partial_spec (tmpl, champ, t);
23648 if (fate == 1)
23649 ;
23650 else
23651 {
23652 if (fate == 0)
23653 {
23654 t = TREE_CHAIN (t);
23655 if (! t)
23656 {
23657 ambiguous_p = true;
23658 break;
23659 }
23660 }
23661 champ = t;
23662 }
23663 }
23664
23665 if (!ambiguous_p)
23666 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23667 {
23668 fate = more_specialized_partial_spec (tmpl, champ, t);
23669 if (fate != 1)
23670 {
23671 ambiguous_p = true;
23672 break;
23673 }
23674 }
23675
23676 if (ambiguous_p)
23677 {
23678 const char *str;
23679 char *spaces = NULL;
23680 if (!(complain & tf_error))
23681 return error_mark_node;
23682 if (TYPE_P (target))
23683 error ("ambiguous template instantiation for %q#T", target);
23684 else
23685 error ("ambiguous template instantiation for %q#D", target);
23686 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23687 for (t = list; t; t = TREE_CHAIN (t))
23688 {
23689 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23690 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23691 "%s %#qS", spaces ? spaces : str, subst);
23692 spaces = spaces ? spaces : get_spaces (str);
23693 }
23694 free (spaces);
23695 return error_mark_node;
23696 }
23697
23698 return champ;
23699 }
23700
23701 /* Explicitly instantiate DECL. */
23702
23703 void
23704 do_decl_instantiation (tree decl, tree storage)
23705 {
23706 tree result = NULL_TREE;
23707 int extern_p = 0;
23708
23709 if (!decl || decl == error_mark_node)
23710 /* An error occurred, for which grokdeclarator has already issued
23711 an appropriate message. */
23712 return;
23713 else if (! DECL_LANG_SPECIFIC (decl))
23714 {
23715 error ("explicit instantiation of non-template %q#D", decl);
23716 return;
23717 }
23718 else if (DECL_DECLARED_CONCEPT_P (decl))
23719 {
23720 if (VAR_P (decl))
23721 error ("explicit instantiation of variable concept %q#D", decl);
23722 else
23723 error ("explicit instantiation of function concept %q#D", decl);
23724 return;
23725 }
23726
23727 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23728 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23729
23730 if (VAR_P (decl) && !var_templ)
23731 {
23732 /* There is an asymmetry here in the way VAR_DECLs and
23733 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23734 the latter, the DECL we get back will be marked as a
23735 template instantiation, and the appropriate
23736 DECL_TEMPLATE_INFO will be set up. This does not happen for
23737 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23738 should handle VAR_DECLs as it currently handles
23739 FUNCTION_DECLs. */
23740 if (!DECL_CLASS_SCOPE_P (decl))
23741 {
23742 error ("%qD is not a static data member of a class template", decl);
23743 return;
23744 }
23745 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23746 if (!result || !VAR_P (result))
23747 {
23748 error ("no matching template for %qD found", decl);
23749 return;
23750 }
23751 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23752 {
23753 error ("type %qT for explicit instantiation %qD does not match "
23754 "declared type %qT", TREE_TYPE (result), decl,
23755 TREE_TYPE (decl));
23756 return;
23757 }
23758 }
23759 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23760 {
23761 error ("explicit instantiation of %q#D", decl);
23762 return;
23763 }
23764 else
23765 result = decl;
23766
23767 /* Check for various error cases. Note that if the explicit
23768 instantiation is valid the RESULT will currently be marked as an
23769 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23770 until we get here. */
23771
23772 if (DECL_TEMPLATE_SPECIALIZATION (result))
23773 {
23774 /* DR 259 [temp.spec].
23775
23776 Both an explicit instantiation and a declaration of an explicit
23777 specialization shall not appear in a program unless the explicit
23778 instantiation follows a declaration of the explicit specialization.
23779
23780 For a given set of template parameters, if an explicit
23781 instantiation of a template appears after a declaration of an
23782 explicit specialization for that template, the explicit
23783 instantiation has no effect. */
23784 return;
23785 }
23786 else if (DECL_EXPLICIT_INSTANTIATION (result))
23787 {
23788 /* [temp.spec]
23789
23790 No program shall explicitly instantiate any template more
23791 than once.
23792
23793 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23794 the first instantiation was `extern' and the second is not,
23795 and EXTERN_P for the opposite case. */
23796 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23797 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23798 /* If an "extern" explicit instantiation follows an ordinary
23799 explicit instantiation, the template is instantiated. */
23800 if (extern_p)
23801 return;
23802 }
23803 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23804 {
23805 error ("no matching template for %qD found", result);
23806 return;
23807 }
23808 else if (!DECL_TEMPLATE_INFO (result))
23809 {
23810 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23811 return;
23812 }
23813
23814 if (storage == NULL_TREE)
23815 ;
23816 else if (storage == ridpointers[(int) RID_EXTERN])
23817 {
23818 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23819 pedwarn (input_location, OPT_Wpedantic,
23820 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23821 "instantiations");
23822 extern_p = 1;
23823 }
23824 else
23825 error ("storage class %qD applied to template instantiation", storage);
23826
23827 check_explicit_instantiation_namespace (result);
23828 mark_decl_instantiated (result, extern_p);
23829 if (! extern_p)
23830 instantiate_decl (result, /*defer_ok=*/true,
23831 /*expl_inst_class_mem_p=*/false);
23832 }
23833
23834 static void
23835 mark_class_instantiated (tree t, int extern_p)
23836 {
23837 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23838 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23839 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23840 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23841 if (! extern_p)
23842 {
23843 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23844 rest_of_type_compilation (t, 1);
23845 }
23846 }
23847
23848 /* Called from do_type_instantiation through binding_table_foreach to
23849 do recursive instantiation for the type bound in ENTRY. */
23850 static void
23851 bt_instantiate_type_proc (binding_entry entry, void *data)
23852 {
23853 tree storage = *(tree *) data;
23854
23855 if (MAYBE_CLASS_TYPE_P (entry->type)
23856 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23857 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23858 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23859 }
23860
23861 /* Perform an explicit instantiation of template class T. STORAGE, if
23862 non-null, is the RID for extern, inline or static. COMPLAIN is
23863 nonzero if this is called from the parser, zero if called recursively,
23864 since the standard is unclear (as detailed below). */
23865
23866 void
23867 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23868 {
23869 int extern_p = 0;
23870 int nomem_p = 0;
23871 int static_p = 0;
23872 int previous_instantiation_extern_p = 0;
23873
23874 if (TREE_CODE (t) == TYPE_DECL)
23875 t = TREE_TYPE (t);
23876
23877 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23878 {
23879 tree tmpl =
23880 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23881 if (tmpl)
23882 error ("explicit instantiation of non-class template %qD", tmpl);
23883 else
23884 error ("explicit instantiation of non-template type %qT", t);
23885 return;
23886 }
23887
23888 complete_type (t);
23889
23890 if (!COMPLETE_TYPE_P (t))
23891 {
23892 if (complain & tf_error)
23893 error ("explicit instantiation of %q#T before definition of template",
23894 t);
23895 return;
23896 }
23897
23898 if (storage != NULL_TREE)
23899 {
23900 if (!in_system_header_at (input_location))
23901 {
23902 if (storage == ridpointers[(int) RID_EXTERN])
23903 {
23904 if (cxx_dialect == cxx98)
23905 pedwarn (input_location, OPT_Wpedantic,
23906 "ISO C++ 1998 forbids the use of %<extern%> on "
23907 "explicit instantiations");
23908 }
23909 else
23910 pedwarn (input_location, OPT_Wpedantic,
23911 "ISO C++ forbids the use of %qE"
23912 " on explicit instantiations", storage);
23913 }
23914
23915 if (storage == ridpointers[(int) RID_INLINE])
23916 nomem_p = 1;
23917 else if (storage == ridpointers[(int) RID_EXTERN])
23918 extern_p = 1;
23919 else if (storage == ridpointers[(int) RID_STATIC])
23920 static_p = 1;
23921 else
23922 {
23923 error ("storage class %qD applied to template instantiation",
23924 storage);
23925 extern_p = 0;
23926 }
23927 }
23928
23929 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23930 {
23931 /* DR 259 [temp.spec].
23932
23933 Both an explicit instantiation and a declaration of an explicit
23934 specialization shall not appear in a program unless the explicit
23935 instantiation follows a declaration of the explicit specialization.
23936
23937 For a given set of template parameters, if an explicit
23938 instantiation of a template appears after a declaration of an
23939 explicit specialization for that template, the explicit
23940 instantiation has no effect. */
23941 return;
23942 }
23943 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23944 {
23945 /* [temp.spec]
23946
23947 No program shall explicitly instantiate any template more
23948 than once.
23949
23950 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23951 instantiation was `extern'. If EXTERN_P then the second is.
23952 These cases are OK. */
23953 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23954
23955 if (!previous_instantiation_extern_p && !extern_p
23956 && (complain & tf_error))
23957 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23958
23959 /* If we've already instantiated the template, just return now. */
23960 if (!CLASSTYPE_INTERFACE_ONLY (t))
23961 return;
23962 }
23963
23964 check_explicit_instantiation_namespace (TYPE_NAME (t));
23965 mark_class_instantiated (t, extern_p);
23966
23967 if (nomem_p)
23968 return;
23969
23970 /* In contrast to implicit instantiation, where only the
23971 declarations, and not the definitions, of members are
23972 instantiated, we have here:
23973
23974 [temp.explicit]
23975
23976 The explicit instantiation of a class template specialization
23977 implies the instantiation of all of its members not
23978 previously explicitly specialized in the translation unit
23979 containing the explicit instantiation.
23980
23981 Of course, we can't instantiate member template classes, since we
23982 don't have any arguments for them. Note that the standard is
23983 unclear on whether the instantiation of the members are
23984 *explicit* instantiations or not. However, the most natural
23985 interpretation is that it should be an explicit
23986 instantiation. */
23987 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
23988 if ((VAR_P (fld)
23989 || (TREE_CODE (fld) == FUNCTION_DECL
23990 && !static_p
23991 && user_provided_p (fld)))
23992 && DECL_TEMPLATE_INSTANTIATION (fld))
23993 {
23994 mark_decl_instantiated (fld, extern_p);
23995 if (! extern_p)
23996 instantiate_decl (fld, /*defer_ok=*/true,
23997 /*expl_inst_class_mem_p=*/true);
23998 }
23999
24000 if (CLASSTYPE_NESTED_UTDS (t))
24001 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24002 bt_instantiate_type_proc, &storage);
24003 }
24004
24005 /* Given a function DECL, which is a specialization of TMPL, modify
24006 DECL to be a re-instantiation of TMPL with the same template
24007 arguments. TMPL should be the template into which tsubst'ing
24008 should occur for DECL, not the most general template.
24009
24010 One reason for doing this is a scenario like this:
24011
24012 template <class T>
24013 void f(const T&, int i);
24014
24015 void g() { f(3, 7); }
24016
24017 template <class T>
24018 void f(const T& t, const int i) { }
24019
24020 Note that when the template is first instantiated, with
24021 instantiate_template, the resulting DECL will have no name for the
24022 first parameter, and the wrong type for the second. So, when we go
24023 to instantiate the DECL, we regenerate it. */
24024
24025 static void
24026 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24027 {
24028 /* The arguments used to instantiate DECL, from the most general
24029 template. */
24030 tree code_pattern;
24031
24032 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24033
24034 /* Make sure that we can see identifiers, and compute access
24035 correctly. */
24036 push_access_scope (decl);
24037
24038 if (TREE_CODE (decl) == FUNCTION_DECL)
24039 {
24040 tree decl_parm;
24041 tree pattern_parm;
24042 tree specs;
24043 int args_depth;
24044 int parms_depth;
24045
24046 args_depth = TMPL_ARGS_DEPTH (args);
24047 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24048 if (args_depth > parms_depth)
24049 args = get_innermost_template_args (args, parms_depth);
24050
24051 /* Instantiate a dynamic exception-specification. noexcept will be
24052 handled below. */
24053 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24054 if (TREE_VALUE (raises))
24055 {
24056 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24057 args, tf_error, NULL_TREE,
24058 /*defer_ok*/false);
24059 if (specs && specs != error_mark_node)
24060 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24061 specs);
24062 }
24063
24064 /* Merge parameter declarations. */
24065 decl_parm = skip_artificial_parms_for (decl,
24066 DECL_ARGUMENTS (decl));
24067 pattern_parm
24068 = skip_artificial_parms_for (code_pattern,
24069 DECL_ARGUMENTS (code_pattern));
24070 while (decl_parm && !DECL_PACK_P (pattern_parm))
24071 {
24072 tree parm_type;
24073 tree attributes;
24074
24075 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24076 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24077 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24078 NULL_TREE);
24079 parm_type = type_decays_to (parm_type);
24080 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24081 TREE_TYPE (decl_parm) = parm_type;
24082 attributes = DECL_ATTRIBUTES (pattern_parm);
24083 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24084 {
24085 DECL_ATTRIBUTES (decl_parm) = attributes;
24086 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24087 }
24088 decl_parm = DECL_CHAIN (decl_parm);
24089 pattern_parm = DECL_CHAIN (pattern_parm);
24090 }
24091 /* Merge any parameters that match with the function parameter
24092 pack. */
24093 if (pattern_parm && DECL_PACK_P (pattern_parm))
24094 {
24095 int i, len;
24096 tree expanded_types;
24097 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24098 the parameters in this function parameter pack. */
24099 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24100 args, tf_error, NULL_TREE);
24101 len = TREE_VEC_LENGTH (expanded_types);
24102 for (i = 0; i < len; i++)
24103 {
24104 tree parm_type;
24105 tree attributes;
24106
24107 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24108 /* Rename the parameter to include the index. */
24109 DECL_NAME (decl_parm) =
24110 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24111 parm_type = TREE_VEC_ELT (expanded_types, i);
24112 parm_type = type_decays_to (parm_type);
24113 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24114 TREE_TYPE (decl_parm) = parm_type;
24115 attributes = DECL_ATTRIBUTES (pattern_parm);
24116 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24117 {
24118 DECL_ATTRIBUTES (decl_parm) = attributes;
24119 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24120 }
24121 decl_parm = DECL_CHAIN (decl_parm);
24122 }
24123 }
24124 /* Merge additional specifiers from the CODE_PATTERN. */
24125 if (DECL_DECLARED_INLINE_P (code_pattern)
24126 && !DECL_DECLARED_INLINE_P (decl))
24127 DECL_DECLARED_INLINE_P (decl) = 1;
24128
24129 maybe_instantiate_noexcept (decl, tf_error);
24130 }
24131 else if (VAR_P (decl))
24132 {
24133 start_lambda_scope (decl);
24134 DECL_INITIAL (decl) =
24135 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24136 tf_error, DECL_TI_TEMPLATE (decl));
24137 finish_lambda_scope ();
24138 if (VAR_HAD_UNKNOWN_BOUND (decl))
24139 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24140 tf_error, DECL_TI_TEMPLATE (decl));
24141 }
24142 else
24143 gcc_unreachable ();
24144
24145 pop_access_scope (decl);
24146 }
24147
24148 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24149 substituted to get DECL. */
24150
24151 tree
24152 template_for_substitution (tree decl)
24153 {
24154 tree tmpl = DECL_TI_TEMPLATE (decl);
24155
24156 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24157 for the instantiation. This is not always the most general
24158 template. Consider, for example:
24159
24160 template <class T>
24161 struct S { template <class U> void f();
24162 template <> void f<int>(); };
24163
24164 and an instantiation of S<double>::f<int>. We want TD to be the
24165 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24166 while (/* An instantiation cannot have a definition, so we need a
24167 more general template. */
24168 DECL_TEMPLATE_INSTANTIATION (tmpl)
24169 /* We must also deal with friend templates. Given:
24170
24171 template <class T> struct S {
24172 template <class U> friend void f() {};
24173 };
24174
24175 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24176 so far as the language is concerned, but that's still
24177 where we get the pattern for the instantiation from. On
24178 other hand, if the definition comes outside the class, say:
24179
24180 template <class T> struct S {
24181 template <class U> friend void f();
24182 };
24183 template <class U> friend void f() {}
24184
24185 we don't need to look any further. That's what the check for
24186 DECL_INITIAL is for. */
24187 || (TREE_CODE (decl) == FUNCTION_DECL
24188 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24189 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24190 {
24191 /* The present template, TD, should not be a definition. If it
24192 were a definition, we should be using it! Note that we
24193 cannot restructure the loop to just keep going until we find
24194 a template with a definition, since that might go too far if
24195 a specialization was declared, but not defined. */
24196
24197 /* Fetch the more general template. */
24198 tmpl = DECL_TI_TEMPLATE (tmpl);
24199 }
24200
24201 return tmpl;
24202 }
24203
24204 /* Returns true if we need to instantiate this template instance even if we
24205 know we aren't going to emit it. */
24206
24207 bool
24208 always_instantiate_p (tree decl)
24209 {
24210 /* We always instantiate inline functions so that we can inline them. An
24211 explicit instantiation declaration prohibits implicit instantiation of
24212 non-inline functions. With high levels of optimization, we would
24213 normally inline non-inline functions -- but we're not allowed to do
24214 that for "extern template" functions. Therefore, we check
24215 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24216 return ((TREE_CODE (decl) == FUNCTION_DECL
24217 && (DECL_DECLARED_INLINE_P (decl)
24218 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24219 /* And we need to instantiate static data members so that
24220 their initializers are available in integral constant
24221 expressions. */
24222 || (VAR_P (decl)
24223 && decl_maybe_constant_var_p (decl)));
24224 }
24225
24226 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24227 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24228 error, true otherwise. */
24229
24230 bool
24231 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24232 {
24233 tree fntype, spec, noex, clone;
24234
24235 /* Don't instantiate a noexcept-specification from template context. */
24236 if (processing_template_decl
24237 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24238 return true;
24239
24240 if (DECL_CLONED_FUNCTION_P (fn))
24241 fn = DECL_CLONED_FUNCTION (fn);
24242
24243 tree orig_fn = NULL_TREE;
24244 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24245 its FUNCTION_DECL for the rest of this function -- push_access_scope
24246 doesn't accept TEMPLATE_DECLs. */
24247 if (DECL_FUNCTION_TEMPLATE_P (fn))
24248 {
24249 orig_fn = fn;
24250 fn = DECL_TEMPLATE_RESULT (fn);
24251 }
24252
24253 fntype = TREE_TYPE (fn);
24254 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24255
24256 if (!spec || !TREE_PURPOSE (spec))
24257 return true;
24258
24259 noex = TREE_PURPOSE (spec);
24260
24261 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24262 {
24263 static hash_set<tree>* fns = new hash_set<tree>;
24264 bool added = false;
24265 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24266 {
24267 spec = get_defaulted_eh_spec (fn, complain);
24268 if (spec == error_mark_node)
24269 /* This might have failed because of an unparsed DMI, so
24270 let's try again later. */
24271 return false;
24272 }
24273 else if (!(added = !fns->add (fn)))
24274 {
24275 /* If hash_set::add returns true, the element was already there. */
24276 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24277 DECL_SOURCE_LOCATION (fn));
24278 error_at (loc,
24279 "exception specification of %qD depends on itself",
24280 fn);
24281 spec = noexcept_false_spec;
24282 }
24283 else if (push_tinst_level (fn))
24284 {
24285 push_access_scope (fn);
24286 push_deferring_access_checks (dk_no_deferred);
24287 input_location = DECL_SOURCE_LOCATION (fn);
24288
24289 tree save_ccp = current_class_ptr;
24290 tree save_ccr = current_class_ref;
24291 /* If needed, set current_class_ptr for the benefit of
24292 tsubst_copy/PARM_DECL. */
24293 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24294 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24295 {
24296 tree this_parm = DECL_ARGUMENTS (tdecl);
24297 current_class_ptr = NULL_TREE;
24298 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24299 current_class_ptr = this_parm;
24300 }
24301
24302 /* If this function is represented by a TEMPLATE_DECL, then
24303 the deferred noexcept-specification might still contain
24304 dependent types, even after substitution. And we need the
24305 dependency check functions to work in build_noexcept_spec. */
24306 if (orig_fn)
24307 ++processing_template_decl;
24308
24309 /* Do deferred instantiation of the noexcept-specifier. */
24310 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24311 DEFERRED_NOEXCEPT_ARGS (noex),
24312 tf_warning_or_error, fn,
24313 /*function_p=*/false,
24314 /*i_c_e_p=*/true);
24315
24316 current_class_ptr = save_ccp;
24317 current_class_ref = save_ccr;
24318
24319 /* Build up the noexcept-specification. */
24320 spec = build_noexcept_spec (noex, tf_warning_or_error);
24321
24322 if (orig_fn)
24323 --processing_template_decl;
24324
24325 pop_deferring_access_checks ();
24326 pop_access_scope (fn);
24327 pop_tinst_level ();
24328 }
24329 else
24330 spec = noexcept_false_spec;
24331
24332 if (added)
24333 fns->remove (fn);
24334
24335 if (spec == error_mark_node)
24336 {
24337 /* This failed with a hard error, so let's go with false. */
24338 gcc_assert (seen_error ());
24339 spec = noexcept_false_spec;
24340 }
24341
24342 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24343 if (orig_fn)
24344 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24345 }
24346
24347 FOR_EACH_CLONE (clone, fn)
24348 {
24349 if (TREE_TYPE (clone) == fntype)
24350 TREE_TYPE (clone) = TREE_TYPE (fn);
24351 else
24352 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24353 }
24354
24355 return true;
24356 }
24357
24358 /* We're starting to process the function INST, an instantiation of PATTERN;
24359 add their parameters to local_specializations. */
24360
24361 static void
24362 register_parameter_specializations (tree pattern, tree inst)
24363 {
24364 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24365 tree spec_parm = DECL_ARGUMENTS (inst);
24366 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24367 {
24368 register_local_specialization (spec_parm, tmpl_parm);
24369 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24370 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24371 }
24372 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24373 {
24374 if (!DECL_PACK_P (tmpl_parm))
24375 {
24376 register_local_specialization (spec_parm, tmpl_parm);
24377 spec_parm = DECL_CHAIN (spec_parm);
24378 }
24379 else
24380 {
24381 /* Register the (value) argument pack as a specialization of
24382 TMPL_PARM, then move on. */
24383 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24384 register_local_specialization (argpack, tmpl_parm);
24385 }
24386 }
24387 gcc_assert (!spec_parm);
24388 }
24389
24390 /* Produce the definition of D, a _DECL generated from a template. If
24391 DEFER_OK is true, then we don't have to actually do the
24392 instantiation now; we just have to do it sometime. Normally it is
24393 an error if this is an explicit instantiation but D is undefined.
24394 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24395 instantiated class template. */
24396
24397 tree
24398 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24399 {
24400 tree tmpl = DECL_TI_TEMPLATE (d);
24401 tree gen_args;
24402 tree args;
24403 tree td;
24404 tree code_pattern;
24405 tree spec;
24406 tree gen_tmpl;
24407 bool pattern_defined;
24408 location_t saved_loc = input_location;
24409 int saved_unevaluated_operand = cp_unevaluated_operand;
24410 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24411 bool external_p;
24412 bool deleted_p;
24413
24414 /* This function should only be used to instantiate templates for
24415 functions and static member variables. */
24416 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24417
24418 /* A concept is never instantiated. */
24419 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24420
24421 /* Variables are never deferred; if instantiation is required, they
24422 are instantiated right away. That allows for better code in the
24423 case that an expression refers to the value of the variable --
24424 if the variable has a constant value the referring expression can
24425 take advantage of that fact. */
24426 if (VAR_P (d))
24427 defer_ok = false;
24428
24429 /* Don't instantiate cloned functions. Instead, instantiate the
24430 functions they cloned. */
24431 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
24432 d = DECL_CLONED_FUNCTION (d);
24433
24434 if (DECL_TEMPLATE_INSTANTIATED (d)
24435 || (TREE_CODE (d) == FUNCTION_DECL
24436 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
24437 || DECL_TEMPLATE_SPECIALIZATION (d))
24438 /* D has already been instantiated or explicitly specialized, so
24439 there's nothing for us to do here.
24440
24441 It might seem reasonable to check whether or not D is an explicit
24442 instantiation, and, if so, stop here. But when an explicit
24443 instantiation is deferred until the end of the compilation,
24444 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24445 the instantiation. */
24446 return d;
24447
24448 /* Check to see whether we know that this template will be
24449 instantiated in some other file, as with "extern template"
24450 extension. */
24451 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
24452
24453 /* In general, we do not instantiate such templates. */
24454 if (external_p && !always_instantiate_p (d))
24455 return d;
24456
24457 gen_tmpl = most_general_template (tmpl);
24458 gen_args = DECL_TI_ARGS (d);
24459
24460 if (tmpl != gen_tmpl)
24461 /* We should already have the extra args. */
24462 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
24463 == TMPL_ARGS_DEPTH (gen_args));
24464 /* And what's in the hash table should match D. */
24465 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
24466 || spec == NULL_TREE);
24467
24468 /* This needs to happen before any tsubsting. */
24469 if (! push_tinst_level (d))
24470 return d;
24471
24472 timevar_push (TV_TEMPLATE_INST);
24473
24474 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24475 for the instantiation. */
24476 td = template_for_substitution (d);
24477 args = gen_args;
24478
24479 if (VAR_P (d))
24480 {
24481 /* Look up an explicit specialization, if any. */
24482 tree tid = lookup_template_variable (gen_tmpl, gen_args);
24483 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
24484 if (elt && elt != error_mark_node)
24485 {
24486 td = TREE_VALUE (elt);
24487 args = TREE_PURPOSE (elt);
24488 }
24489 }
24490
24491 code_pattern = DECL_TEMPLATE_RESULT (td);
24492
24493 /* We should never be trying to instantiate a member of a class
24494 template or partial specialization. */
24495 gcc_assert (d != code_pattern);
24496
24497 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
24498 || DECL_TEMPLATE_SPECIALIZATION (td))
24499 /* In the case of a friend template whose definition is provided
24500 outside the class, we may have too many arguments. Drop the
24501 ones we don't need. The same is true for specializations. */
24502 args = get_innermost_template_args
24503 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
24504
24505 if (TREE_CODE (d) == FUNCTION_DECL)
24506 {
24507 deleted_p = DECL_DELETED_FN (code_pattern);
24508 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
24509 && DECL_INITIAL (code_pattern) != error_mark_node)
24510 || DECL_DEFAULTED_FN (code_pattern)
24511 || deleted_p);
24512 }
24513 else
24514 {
24515 deleted_p = false;
24516 if (DECL_CLASS_SCOPE_P (code_pattern))
24517 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
24518 else
24519 pattern_defined = ! DECL_EXTERNAL (code_pattern);
24520 }
24521
24522 /* We may be in the middle of deferred access check. Disable it now. */
24523 push_deferring_access_checks (dk_no_deferred);
24524
24525 /* Unless an explicit instantiation directive has already determined
24526 the linkage of D, remember that a definition is available for
24527 this entity. */
24528 if (pattern_defined
24529 && !DECL_INTERFACE_KNOWN (d)
24530 && !DECL_NOT_REALLY_EXTERN (d))
24531 mark_definable (d);
24532
24533 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24534 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24535 input_location = DECL_SOURCE_LOCATION (d);
24536
24537 /* If D is a member of an explicitly instantiated class template,
24538 and no definition is available, treat it like an implicit
24539 instantiation. */
24540 if (!pattern_defined && expl_inst_class_mem_p
24541 && DECL_EXPLICIT_INSTANTIATION (d))
24542 {
24543 /* Leave linkage flags alone on instantiations with anonymous
24544 visibility. */
24545 if (TREE_PUBLIC (d))
24546 {
24547 DECL_NOT_REALLY_EXTERN (d) = 0;
24548 DECL_INTERFACE_KNOWN (d) = 0;
24549 }
24550 SET_DECL_IMPLICIT_INSTANTIATION (d);
24551 }
24552
24553 /* Defer all other templates, unless we have been explicitly
24554 forbidden from doing so. */
24555 if (/* If there is no definition, we cannot instantiate the
24556 template. */
24557 ! pattern_defined
24558 /* If it's OK to postpone instantiation, do so. */
24559 || defer_ok
24560 /* If this is a static data member that will be defined
24561 elsewhere, we don't want to instantiate the entire data
24562 member, but we do want to instantiate the initializer so that
24563 we can substitute that elsewhere. */
24564 || (external_p && VAR_P (d))
24565 /* Handle here a deleted function too, avoid generating
24566 its body (c++/61080). */
24567 || deleted_p)
24568 {
24569 /* The definition of the static data member is now required so
24570 we must substitute the initializer. */
24571 if (VAR_P (d)
24572 && !DECL_INITIAL (d)
24573 && DECL_INITIAL (code_pattern))
24574 {
24575 tree ns;
24576 tree init;
24577 bool const_init = false;
24578 bool enter_context = DECL_CLASS_SCOPE_P (d);
24579
24580 ns = decl_namespace_context (d);
24581 push_nested_namespace (ns);
24582 if (enter_context)
24583 push_nested_class (DECL_CONTEXT (d));
24584 init = tsubst_expr (DECL_INITIAL (code_pattern),
24585 args,
24586 tf_warning_or_error, NULL_TREE,
24587 /*integral_constant_expression_p=*/false);
24588 /* If instantiating the initializer involved instantiating this
24589 again, don't call cp_finish_decl twice. */
24590 if (!DECL_INITIAL (d))
24591 {
24592 /* Make sure the initializer is still constant, in case of
24593 circular dependency (template/instantiate6.C). */
24594 const_init
24595 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24596 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24597 /*asmspec_tree=*/NULL_TREE,
24598 LOOKUP_ONLYCONVERTING);
24599 }
24600 if (enter_context)
24601 pop_nested_class ();
24602 pop_nested_namespace (ns);
24603 }
24604
24605 /* We restore the source position here because it's used by
24606 add_pending_template. */
24607 input_location = saved_loc;
24608
24609 if (at_eof && !pattern_defined
24610 && DECL_EXPLICIT_INSTANTIATION (d)
24611 && DECL_NOT_REALLY_EXTERN (d))
24612 /* [temp.explicit]
24613
24614 The definition of a non-exported function template, a
24615 non-exported member function template, or a non-exported
24616 member function or static data member of a class template
24617 shall be present in every translation unit in which it is
24618 explicitly instantiated. */
24619 permerror (input_location, "explicit instantiation of %qD "
24620 "but no definition available", d);
24621
24622 /* If we're in unevaluated context, we just wanted to get the
24623 constant value; this isn't an odr use, so don't queue
24624 a full instantiation. */
24625 if (cp_unevaluated_operand != 0)
24626 goto out;
24627 /* ??? Historically, we have instantiated inline functions, even
24628 when marked as "extern template". */
24629 if (!(external_p && VAR_P (d)))
24630 add_pending_template (d);
24631 goto out;
24632 }
24633 /* Tell the repository that D is available in this translation unit
24634 -- and see if it is supposed to be instantiated here. */
24635 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
24636 {
24637 /* In a PCH file, despite the fact that the repository hasn't
24638 requested instantiation in the PCH it is still possible that
24639 an instantiation will be required in a file that includes the
24640 PCH. */
24641 if (pch_file)
24642 add_pending_template (d);
24643 /* Instantiate inline functions so that the inliner can do its
24644 job, even though we'll not be emitting a copy of this
24645 function. */
24646 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
24647 goto out;
24648 }
24649
24650 bool push_to_top, nested;
24651 tree fn_context;
24652 fn_context = decl_function_context (d);
24653 if (LAMBDA_FUNCTION_P (d))
24654 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24655 fn_context = NULL_TREE;
24656 nested = current_function_decl != NULL_TREE;
24657 push_to_top = !(nested && fn_context == current_function_decl);
24658
24659 vec<tree> omp_privatization_save;
24660 if (nested)
24661 save_omp_privatization_clauses (omp_privatization_save);
24662
24663 if (push_to_top)
24664 push_to_top_level ();
24665 else
24666 {
24667 gcc_assert (!processing_template_decl);
24668 push_function_context ();
24669 cp_unevaluated_operand = 0;
24670 c_inhibit_evaluation_warnings = 0;
24671 }
24672
24673 /* Mark D as instantiated so that recursive calls to
24674 instantiate_decl do not try to instantiate it again. */
24675 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24676
24677 /* Regenerate the declaration in case the template has been modified
24678 by a subsequent redeclaration. */
24679 regenerate_decl_from_template (d, td, args);
24680
24681 /* We already set the file and line above. Reset them now in case
24682 they changed as a result of calling regenerate_decl_from_template. */
24683 input_location = DECL_SOURCE_LOCATION (d);
24684
24685 if (VAR_P (d))
24686 {
24687 tree init;
24688 bool const_init = false;
24689
24690 /* Clear out DECL_RTL; whatever was there before may not be right
24691 since we've reset the type of the declaration. */
24692 SET_DECL_RTL (d, NULL);
24693 DECL_IN_AGGR_P (d) = 0;
24694
24695 /* The initializer is placed in DECL_INITIAL by
24696 regenerate_decl_from_template so we don't need to
24697 push/pop_access_scope again here. Pull it out so that
24698 cp_finish_decl can process it. */
24699 init = DECL_INITIAL (d);
24700 DECL_INITIAL (d) = NULL_TREE;
24701 DECL_INITIALIZED_P (d) = 0;
24702
24703 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24704 initializer. That function will defer actual emission until
24705 we have a chance to determine linkage. */
24706 DECL_EXTERNAL (d) = 0;
24707
24708 /* Enter the scope of D so that access-checking works correctly. */
24709 bool enter_context = DECL_CLASS_SCOPE_P (d);
24710 if (enter_context)
24711 push_nested_class (DECL_CONTEXT (d));
24712
24713 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24714 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24715
24716 if (enter_context)
24717 pop_nested_class ();
24718
24719 if (variable_template_p (gen_tmpl))
24720 note_variable_template_instantiation (d);
24721 }
24722 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24723 synthesize_method (d);
24724 else if (TREE_CODE (d) == FUNCTION_DECL)
24725 {
24726 /* Set up the list of local specializations. */
24727 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24728 tree block = NULL_TREE;
24729
24730 /* Set up context. */
24731 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24732 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24733 block = push_stmt_list ();
24734 else
24735 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24736
24737 /* Some typedefs referenced from within the template code need to be
24738 access checked at template instantiation time, i.e now. These
24739 types were added to the template at parsing time. Let's get those
24740 and perform the access checks then. */
24741 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24742 args);
24743
24744 /* Create substitution entries for the parameters. */
24745 register_parameter_specializations (code_pattern, d);
24746
24747 /* Substitute into the body of the function. */
24748 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24749 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24750 tf_warning_or_error, tmpl);
24751 else
24752 {
24753 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24754 tf_warning_or_error, tmpl,
24755 /*integral_constant_expression_p=*/false);
24756
24757 /* Set the current input_location to the end of the function
24758 so that finish_function knows where we are. */
24759 input_location
24760 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24761
24762 /* Remember if we saw an infinite loop in the template. */
24763 current_function_infinite_loop
24764 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24765 }
24766
24767 /* Finish the function. */
24768 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24769 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24770 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24771 else
24772 {
24773 d = finish_function (/*inline_p=*/false);
24774 expand_or_defer_fn (d);
24775 }
24776
24777 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24778 cp_check_omp_declare_reduction (d);
24779 }
24780
24781 /* We're not deferring instantiation any more. */
24782 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24783
24784 if (push_to_top)
24785 pop_from_top_level ();
24786 else
24787 pop_function_context ();
24788
24789 if (nested)
24790 restore_omp_privatization_clauses (omp_privatization_save);
24791
24792 out:
24793 pop_deferring_access_checks ();
24794 timevar_pop (TV_TEMPLATE_INST);
24795 pop_tinst_level ();
24796 input_location = saved_loc;
24797 cp_unevaluated_operand = saved_unevaluated_operand;
24798 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24799
24800 return d;
24801 }
24802
24803 /* Run through the list of templates that we wish we could
24804 instantiate, and instantiate any we can. RETRIES is the
24805 number of times we retry pending template instantiation. */
24806
24807 void
24808 instantiate_pending_templates (int retries)
24809 {
24810 int reconsider;
24811 location_t saved_loc = input_location;
24812
24813 /* Instantiating templates may trigger vtable generation. This in turn
24814 may require further template instantiations. We place a limit here
24815 to avoid infinite loop. */
24816 if (pending_templates && retries >= max_tinst_depth)
24817 {
24818 tree decl = pending_templates->tinst->maybe_get_node ();
24819
24820 fatal_error (input_location,
24821 "template instantiation depth exceeds maximum of %d"
24822 " instantiating %q+D, possibly from virtual table generation"
24823 " (use %<-ftemplate-depth=%> to increase the maximum)",
24824 max_tinst_depth, decl);
24825 if (TREE_CODE (decl) == FUNCTION_DECL)
24826 /* Pretend that we defined it. */
24827 DECL_INITIAL (decl) = error_mark_node;
24828 return;
24829 }
24830
24831 do
24832 {
24833 struct pending_template **t = &pending_templates;
24834 struct pending_template *last = NULL;
24835 reconsider = 0;
24836 while (*t)
24837 {
24838 tree instantiation = reopen_tinst_level ((*t)->tinst);
24839 bool complete = false;
24840
24841 if (TYPE_P (instantiation))
24842 {
24843 if (!COMPLETE_TYPE_P (instantiation))
24844 {
24845 instantiate_class_template (instantiation);
24846 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24847 for (tree fld = TYPE_FIELDS (instantiation);
24848 fld; fld = TREE_CHAIN (fld))
24849 if ((VAR_P (fld)
24850 || (TREE_CODE (fld) == FUNCTION_DECL
24851 && !DECL_ARTIFICIAL (fld)))
24852 && DECL_TEMPLATE_INSTANTIATION (fld))
24853 instantiate_decl (fld,
24854 /*defer_ok=*/false,
24855 /*expl_inst_class_mem_p=*/false);
24856
24857 if (COMPLETE_TYPE_P (instantiation))
24858 reconsider = 1;
24859 }
24860
24861 complete = COMPLETE_TYPE_P (instantiation);
24862 }
24863 else
24864 {
24865 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24866 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24867 {
24868 instantiation
24869 = instantiate_decl (instantiation,
24870 /*defer_ok=*/false,
24871 /*expl_inst_class_mem_p=*/false);
24872 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24873 reconsider = 1;
24874 }
24875
24876 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24877 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24878 }
24879
24880 if (complete)
24881 {
24882 /* If INSTANTIATION has been instantiated, then we don't
24883 need to consider it again in the future. */
24884 struct pending_template *drop = *t;
24885 *t = (*t)->next;
24886 set_refcount_ptr (drop->tinst);
24887 pending_template_freelist ().free (drop);
24888 }
24889 else
24890 {
24891 last = *t;
24892 t = &(*t)->next;
24893 }
24894 tinst_depth = 0;
24895 set_refcount_ptr (current_tinst_level);
24896 }
24897 last_pending_template = last;
24898 }
24899 while (reconsider);
24900
24901 input_location = saved_loc;
24902 }
24903
24904 /* Substitute ARGVEC into T, which is a list of initializers for
24905 either base class or a non-static data member. The TREE_PURPOSEs
24906 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24907 instantiate_decl. */
24908
24909 static tree
24910 tsubst_initializer_list (tree t, tree argvec)
24911 {
24912 tree inits = NULL_TREE;
24913 tree target_ctor = error_mark_node;
24914
24915 for (; t; t = TREE_CHAIN (t))
24916 {
24917 tree decl;
24918 tree init;
24919 tree expanded_bases = NULL_TREE;
24920 tree expanded_arguments = NULL_TREE;
24921 int i, len = 1;
24922
24923 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24924 {
24925 tree expr;
24926 tree arg;
24927
24928 /* Expand the base class expansion type into separate base
24929 classes. */
24930 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24931 tf_warning_or_error,
24932 NULL_TREE);
24933 if (expanded_bases == error_mark_node)
24934 continue;
24935
24936 /* We'll be building separate TREE_LISTs of arguments for
24937 each base. */
24938 len = TREE_VEC_LENGTH (expanded_bases);
24939 expanded_arguments = make_tree_vec (len);
24940 for (i = 0; i < len; i++)
24941 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24942
24943 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24944 expand each argument in the TREE_VALUE of t. */
24945 expr = make_node (EXPR_PACK_EXPANSION);
24946 PACK_EXPANSION_LOCAL_P (expr) = true;
24947 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24948 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24949
24950 if (TREE_VALUE (t) == void_type_node)
24951 /* VOID_TYPE_NODE is used to indicate
24952 value-initialization. */
24953 {
24954 for (i = 0; i < len; i++)
24955 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24956 }
24957 else
24958 {
24959 /* Substitute parameter packs into each argument in the
24960 TREE_LIST. */
24961 in_base_initializer = 1;
24962 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24963 {
24964 tree expanded_exprs;
24965
24966 /* Expand the argument. */
24967 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24968 expanded_exprs
24969 = tsubst_pack_expansion (expr, argvec,
24970 tf_warning_or_error,
24971 NULL_TREE);
24972 if (expanded_exprs == error_mark_node)
24973 continue;
24974
24975 /* Prepend each of the expanded expressions to the
24976 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24977 for (i = 0; i < len; i++)
24978 {
24979 TREE_VEC_ELT (expanded_arguments, i) =
24980 tree_cons (NULL_TREE,
24981 TREE_VEC_ELT (expanded_exprs, i),
24982 TREE_VEC_ELT (expanded_arguments, i));
24983 }
24984 }
24985 in_base_initializer = 0;
24986
24987 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24988 since we built them backwards. */
24989 for (i = 0; i < len; i++)
24990 {
24991 TREE_VEC_ELT (expanded_arguments, i) =
24992 nreverse (TREE_VEC_ELT (expanded_arguments, i));
24993 }
24994 }
24995 }
24996
24997 for (i = 0; i < len; ++i)
24998 {
24999 if (expanded_bases)
25000 {
25001 decl = TREE_VEC_ELT (expanded_bases, i);
25002 decl = expand_member_init (decl);
25003 init = TREE_VEC_ELT (expanded_arguments, i);
25004 }
25005 else
25006 {
25007 tree tmp;
25008 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25009 tf_warning_or_error, NULL_TREE);
25010
25011 decl = expand_member_init (decl);
25012 if (decl && !DECL_P (decl))
25013 in_base_initializer = 1;
25014
25015 init = TREE_VALUE (t);
25016 tmp = init;
25017 if (init != void_type_node)
25018 init = tsubst_expr (init, argvec,
25019 tf_warning_or_error, NULL_TREE,
25020 /*integral_constant_expression_p=*/false);
25021 if (init == NULL_TREE && tmp != NULL_TREE)
25022 /* If we had an initializer but it instantiated to nothing,
25023 value-initialize the object. This will only occur when
25024 the initializer was a pack expansion where the parameter
25025 packs used in that expansion were of length zero. */
25026 init = void_type_node;
25027 in_base_initializer = 0;
25028 }
25029
25030 if (target_ctor != error_mark_node
25031 && init != error_mark_node)
25032 {
25033 error ("mem-initializer for %qD follows constructor delegation",
25034 decl);
25035 return inits;
25036 }
25037 /* Look for a target constructor. */
25038 if (init != error_mark_node
25039 && decl && CLASS_TYPE_P (decl)
25040 && same_type_p (decl, current_class_type))
25041 {
25042 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25043 if (inits)
25044 {
25045 error ("constructor delegation follows mem-initializer for %qD",
25046 TREE_PURPOSE (inits));
25047 continue;
25048 }
25049 target_ctor = init;
25050 }
25051
25052 if (decl)
25053 {
25054 init = build_tree_list (decl, init);
25055 TREE_CHAIN (init) = inits;
25056 inits = init;
25057 }
25058 }
25059 }
25060 return inits;
25061 }
25062
25063 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25064
25065 static void
25066 set_current_access_from_decl (tree decl)
25067 {
25068 if (TREE_PRIVATE (decl))
25069 current_access_specifier = access_private_node;
25070 else if (TREE_PROTECTED (decl))
25071 current_access_specifier = access_protected_node;
25072 else
25073 current_access_specifier = access_public_node;
25074 }
25075
25076 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25077 is the instantiation (which should have been created with
25078 start_enum) and ARGS are the template arguments to use. */
25079
25080 static void
25081 tsubst_enum (tree tag, tree newtag, tree args)
25082 {
25083 tree e;
25084
25085 if (SCOPED_ENUM_P (newtag))
25086 begin_scope (sk_scoped_enum, newtag);
25087
25088 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25089 {
25090 tree value;
25091 tree decl;
25092
25093 decl = TREE_VALUE (e);
25094 /* Note that in a template enum, the TREE_VALUE is the
25095 CONST_DECL, not the corresponding INTEGER_CST. */
25096 value = tsubst_expr (DECL_INITIAL (decl),
25097 args, tf_warning_or_error, NULL_TREE,
25098 /*integral_constant_expression_p=*/true);
25099
25100 /* Give this enumeration constant the correct access. */
25101 set_current_access_from_decl (decl);
25102
25103 /* Actually build the enumerator itself. Here we're assuming that
25104 enumerators can't have dependent attributes. */
25105 build_enumerator (DECL_NAME (decl), value, newtag,
25106 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25107 }
25108
25109 if (SCOPED_ENUM_P (newtag))
25110 finish_scope ();
25111
25112 finish_enum_value_list (newtag);
25113 finish_enum (newtag);
25114
25115 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25116 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25117 }
25118
25119 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25120 its type -- but without substituting the innermost set of template
25121 arguments. So, innermost set of template parameters will appear in
25122 the type. */
25123
25124 tree
25125 get_mostly_instantiated_function_type (tree decl)
25126 {
25127 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25128 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25129 }
25130
25131 /* Return truthvalue if we're processing a template different from
25132 the last one involved in diagnostics. */
25133 bool
25134 problematic_instantiation_changed (void)
25135 {
25136 return current_tinst_level != last_error_tinst_level;
25137 }
25138
25139 /* Remember current template involved in diagnostics. */
25140 void
25141 record_last_problematic_instantiation (void)
25142 {
25143 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25144 }
25145
25146 struct tinst_level *
25147 current_instantiation (void)
25148 {
25149 return current_tinst_level;
25150 }
25151
25152 /* Return TRUE if current_function_decl is being instantiated, false
25153 otherwise. */
25154
25155 bool
25156 instantiating_current_function_p (void)
25157 {
25158 return (current_instantiation ()
25159 && (current_instantiation ()->maybe_get_node ()
25160 == current_function_decl));
25161 }
25162
25163 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25164 type. Return false for ok, true for disallowed. Issue error and
25165 inform messages under control of COMPLAIN. */
25166
25167 static bool
25168 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25169 {
25170 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25171 return false;
25172 else if (TYPE_PTR_P (type))
25173 return false;
25174 else if (TYPE_REF_P (type)
25175 && !TYPE_REF_IS_RVALUE (type))
25176 return false;
25177 else if (TYPE_PTRMEM_P (type))
25178 return false;
25179 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25180 return false;
25181 else if (TREE_CODE (type) == TYPENAME_TYPE)
25182 return false;
25183 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25184 return false;
25185 else if (TREE_CODE (type) == NULLPTR_TYPE)
25186 return false;
25187 /* A bound template template parm could later be instantiated to have a valid
25188 nontype parm type via an alias template. */
25189 else if (cxx_dialect >= cxx11
25190 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25191 return false;
25192 else if (CLASS_TYPE_P (type))
25193 {
25194 if (cxx_dialect < cxx2a)
25195 {
25196 error ("non-type template parameters of class type only available "
25197 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25198 return true;
25199 }
25200 if (!complete_type_or_else (type, NULL_TREE))
25201 return true;
25202 if (!literal_type_p (type))
25203 {
25204 error ("%qT is not a valid type for a template non-type parameter "
25205 "because it is not literal", type);
25206 explain_non_literal_class (type);
25207 return true;
25208 }
25209 if (cp_has_mutable_p (type))
25210 {
25211 error ("%qT is not a valid type for a template non-type parameter "
25212 "because it has a mutable member", type);
25213 return true;
25214 }
25215 /* FIXME check op<=> and strong structural equality once spaceship is
25216 implemented. */
25217 return false;
25218 }
25219
25220 if (complain & tf_error)
25221 {
25222 if (type == error_mark_node)
25223 inform (input_location, "invalid template non-type parameter");
25224 else
25225 error ("%q#T is not a valid type for a template non-type parameter",
25226 type);
25227 }
25228 return true;
25229 }
25230
25231 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25232 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25233
25234 static bool
25235 dependent_type_p_r (tree type)
25236 {
25237 tree scope;
25238
25239 /* [temp.dep.type]
25240
25241 A type is dependent if it is:
25242
25243 -- a template parameter. Template template parameters are types
25244 for us (since TYPE_P holds true for them) so we handle
25245 them here. */
25246 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25247 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25248 return true;
25249 /* -- a qualified-id with a nested-name-specifier which contains a
25250 class-name that names a dependent type or whose unqualified-id
25251 names a dependent type. */
25252 if (TREE_CODE (type) == TYPENAME_TYPE)
25253 return true;
25254
25255 /* An alias template specialization can be dependent even if the
25256 resulting type is not. */
25257 if (dependent_alias_template_spec_p (type))
25258 return true;
25259
25260 /* -- a cv-qualified type where the cv-unqualified type is
25261 dependent.
25262 No code is necessary for this bullet; the code below handles
25263 cv-qualified types, and we don't want to strip aliases with
25264 TYPE_MAIN_VARIANT because of DR 1558. */
25265 /* -- a compound type constructed from any dependent type. */
25266 if (TYPE_PTRMEM_P (type))
25267 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25268 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25269 (type)));
25270 else if (INDIRECT_TYPE_P (type))
25271 return dependent_type_p (TREE_TYPE (type));
25272 else if (TREE_CODE (type) == FUNCTION_TYPE
25273 || TREE_CODE (type) == METHOD_TYPE)
25274 {
25275 tree arg_type;
25276
25277 if (dependent_type_p (TREE_TYPE (type)))
25278 return true;
25279 for (arg_type = TYPE_ARG_TYPES (type);
25280 arg_type;
25281 arg_type = TREE_CHAIN (arg_type))
25282 if (dependent_type_p (TREE_VALUE (arg_type)))
25283 return true;
25284 if (cxx_dialect >= cxx17)
25285 /* A value-dependent noexcept-specifier makes the type dependent. */
25286 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25287 if (tree noex = TREE_PURPOSE (spec))
25288 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25289 affect overload resolution and treating it as dependent breaks
25290 things. */
25291 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25292 && value_dependent_expression_p (noex))
25293 return true;
25294 return false;
25295 }
25296 /* -- an array type constructed from any dependent type or whose
25297 size is specified by a constant expression that is
25298 value-dependent.
25299
25300 We checked for type- and value-dependence of the bounds in
25301 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25302 if (TREE_CODE (type) == ARRAY_TYPE)
25303 {
25304 if (TYPE_DOMAIN (type)
25305 && dependent_type_p (TYPE_DOMAIN (type)))
25306 return true;
25307 return dependent_type_p (TREE_TYPE (type));
25308 }
25309
25310 /* -- a template-id in which either the template name is a template
25311 parameter ... */
25312 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25313 return true;
25314 /* ... or any of the template arguments is a dependent type or
25315 an expression that is type-dependent or value-dependent. */
25316 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25317 && (any_dependent_template_arguments_p
25318 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25319 return true;
25320
25321 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25322 dependent; if the argument of the `typeof' expression is not
25323 type-dependent, then it should already been have resolved. */
25324 if (TREE_CODE (type) == TYPEOF_TYPE
25325 || TREE_CODE (type) == DECLTYPE_TYPE
25326 || TREE_CODE (type) == UNDERLYING_TYPE)
25327 return true;
25328
25329 /* A template argument pack is dependent if any of its packed
25330 arguments are. */
25331 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25332 {
25333 tree args = ARGUMENT_PACK_ARGS (type);
25334 int i, len = TREE_VEC_LENGTH (args);
25335 for (i = 0; i < len; ++i)
25336 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25337 return true;
25338 }
25339
25340 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25341 be template parameters. */
25342 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25343 return true;
25344
25345 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25346 return true;
25347
25348 /* The standard does not specifically mention types that are local
25349 to template functions or local classes, but they should be
25350 considered dependent too. For example:
25351
25352 template <int I> void f() {
25353 enum E { a = I };
25354 S<sizeof (E)> s;
25355 }
25356
25357 The size of `E' cannot be known until the value of `I' has been
25358 determined. Therefore, `E' must be considered dependent. */
25359 scope = TYPE_CONTEXT (type);
25360 if (scope && TYPE_P (scope))
25361 return dependent_type_p (scope);
25362 /* Don't use type_dependent_expression_p here, as it can lead
25363 to infinite recursion trying to determine whether a lambda
25364 nested in a lambda is dependent (c++/47687). */
25365 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25366 && DECL_LANG_SPECIFIC (scope)
25367 && DECL_TEMPLATE_INFO (scope)
25368 && (any_dependent_template_arguments_p
25369 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25370 return true;
25371
25372 /* Other types are non-dependent. */
25373 return false;
25374 }
25375
25376 /* Returns TRUE if TYPE is dependent, in the sense of
25377 [temp.dep.type]. Note that a NULL type is considered dependent. */
25378
25379 bool
25380 dependent_type_p (tree type)
25381 {
25382 /* If there are no template parameters in scope, then there can't be
25383 any dependent types. */
25384 if (!processing_template_decl)
25385 {
25386 /* If we are not processing a template, then nobody should be
25387 providing us with a dependent type. */
25388 gcc_assert (type);
25389 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25390 return false;
25391 }
25392
25393 /* If the type is NULL, we have not computed a type for the entity
25394 in question; in that case, the type is dependent. */
25395 if (!type)
25396 return true;
25397
25398 /* Erroneous types can be considered non-dependent. */
25399 if (type == error_mark_node)
25400 return false;
25401
25402 /* Getting here with global_type_node means we improperly called this
25403 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25404 gcc_checking_assert (type != global_type_node);
25405
25406 /* If we have not already computed the appropriate value for TYPE,
25407 do so now. */
25408 if (!TYPE_DEPENDENT_P_VALID (type))
25409 {
25410 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25411 TYPE_DEPENDENT_P_VALID (type) = 1;
25412 }
25413
25414 return TYPE_DEPENDENT_P (type);
25415 }
25416
25417 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25418 lookup. In other words, a dependent type that is not the current
25419 instantiation. */
25420
25421 bool
25422 dependent_scope_p (tree scope)
25423 {
25424 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25425 && !currently_open_class (scope));
25426 }
25427
25428 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25429 an unknown base of 'this' (and is therefore instantiation-dependent). */
25430
25431 static bool
25432 unknown_base_ref_p (tree t)
25433 {
25434 if (!current_class_ptr)
25435 return false;
25436
25437 tree mem = TREE_OPERAND (t, 1);
25438 if (shared_member_p (mem))
25439 return false;
25440
25441 tree cur = current_nonlambda_class_type ();
25442 if (!any_dependent_bases_p (cur))
25443 return false;
25444
25445 tree ctx = TREE_OPERAND (t, 0);
25446 if (DERIVED_FROM_P (ctx, cur))
25447 return false;
25448
25449 return true;
25450 }
25451
25452 /* T is a SCOPE_REF; return whether we need to consider it
25453 instantiation-dependent so that we can check access at instantiation
25454 time even though we know which member it resolves to. */
25455
25456 static bool
25457 instantiation_dependent_scope_ref_p (tree t)
25458 {
25459 if (DECL_P (TREE_OPERAND (t, 1))
25460 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
25461 && !unknown_base_ref_p (t)
25462 && accessible_in_template_p (TREE_OPERAND (t, 0),
25463 TREE_OPERAND (t, 1)))
25464 return false;
25465 else
25466 return true;
25467 }
25468
25469 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25470 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25471 expression. */
25472
25473 /* Note that this predicate is not appropriate for general expressions;
25474 only constant expressions (that satisfy potential_constant_expression)
25475 can be tested for value dependence. */
25476
25477 bool
25478 value_dependent_expression_p (tree expression)
25479 {
25480 if (!processing_template_decl || expression == NULL_TREE)
25481 return false;
25482
25483 /* A type-dependent expression is also value-dependent. */
25484 if (type_dependent_expression_p (expression))
25485 return true;
25486
25487 switch (TREE_CODE (expression))
25488 {
25489 case BASELINK:
25490 /* A dependent member function of the current instantiation. */
25491 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
25492
25493 case FUNCTION_DECL:
25494 /* A dependent member function of the current instantiation. */
25495 if (DECL_CLASS_SCOPE_P (expression)
25496 && dependent_type_p (DECL_CONTEXT (expression)))
25497 return true;
25498 break;
25499
25500 case IDENTIFIER_NODE:
25501 /* A name that has not been looked up -- must be dependent. */
25502 return true;
25503
25504 case TEMPLATE_PARM_INDEX:
25505 /* A non-type template parm. */
25506 return true;
25507
25508 case CONST_DECL:
25509 /* A non-type template parm. */
25510 if (DECL_TEMPLATE_PARM_P (expression))
25511 return true;
25512 return value_dependent_expression_p (DECL_INITIAL (expression));
25513
25514 case VAR_DECL:
25515 /* A constant with literal type and is initialized
25516 with an expression that is value-dependent. */
25517 if (DECL_DEPENDENT_INIT_P (expression)
25518 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25519 || TYPE_REF_P (TREE_TYPE (expression)))
25520 return true;
25521 if (DECL_HAS_VALUE_EXPR_P (expression))
25522 {
25523 tree value_expr = DECL_VALUE_EXPR (expression);
25524 if (value_dependent_expression_p (value_expr))
25525 return true;
25526 }
25527 return false;
25528
25529 case DYNAMIC_CAST_EXPR:
25530 case STATIC_CAST_EXPR:
25531 case CONST_CAST_EXPR:
25532 case REINTERPRET_CAST_EXPR:
25533 case CAST_EXPR:
25534 case IMPLICIT_CONV_EXPR:
25535 /* These expressions are value-dependent if the type to which
25536 the cast occurs is dependent or the expression being casted
25537 is value-dependent. */
25538 {
25539 tree type = TREE_TYPE (expression);
25540
25541 if (dependent_type_p (type))
25542 return true;
25543
25544 /* A functional cast has a list of operands. */
25545 expression = TREE_OPERAND (expression, 0);
25546 if (!expression)
25547 {
25548 /* If there are no operands, it must be an expression such
25549 as "int()". This should not happen for aggregate types
25550 because it would form non-constant expressions. */
25551 gcc_assert (cxx_dialect >= cxx11
25552 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25553
25554 return false;
25555 }
25556
25557 if (TREE_CODE (expression) == TREE_LIST)
25558 return any_value_dependent_elements_p (expression);
25559
25560 return value_dependent_expression_p (expression);
25561 }
25562
25563 case SIZEOF_EXPR:
25564 if (SIZEOF_EXPR_TYPE_P (expression))
25565 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25566 /* FALLTHRU */
25567 case ALIGNOF_EXPR:
25568 case TYPEID_EXPR:
25569 /* A `sizeof' expression is value-dependent if the operand is
25570 type-dependent or is a pack expansion. */
25571 expression = TREE_OPERAND (expression, 0);
25572 if (PACK_EXPANSION_P (expression))
25573 return true;
25574 else if (TYPE_P (expression))
25575 return dependent_type_p (expression);
25576 return instantiation_dependent_uneval_expression_p (expression);
25577
25578 case AT_ENCODE_EXPR:
25579 /* An 'encode' expression is value-dependent if the operand is
25580 type-dependent. */
25581 expression = TREE_OPERAND (expression, 0);
25582 return dependent_type_p (expression);
25583
25584 case NOEXCEPT_EXPR:
25585 expression = TREE_OPERAND (expression, 0);
25586 return instantiation_dependent_uneval_expression_p (expression);
25587
25588 case SCOPE_REF:
25589 /* All instantiation-dependent expressions should also be considered
25590 value-dependent. */
25591 return instantiation_dependent_scope_ref_p (expression);
25592
25593 case COMPONENT_REF:
25594 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25595 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25596
25597 case NONTYPE_ARGUMENT_PACK:
25598 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25599 is value-dependent. */
25600 {
25601 tree values = ARGUMENT_PACK_ARGS (expression);
25602 int i, len = TREE_VEC_LENGTH (values);
25603
25604 for (i = 0; i < len; ++i)
25605 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25606 return true;
25607
25608 return false;
25609 }
25610
25611 case TRAIT_EXPR:
25612 {
25613 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25614
25615 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25616 return true;
25617
25618 if (!type2)
25619 return false;
25620
25621 if (TREE_CODE (type2) != TREE_LIST)
25622 return dependent_type_p (type2);
25623
25624 for (; type2; type2 = TREE_CHAIN (type2))
25625 if (dependent_type_p (TREE_VALUE (type2)))
25626 return true;
25627
25628 return false;
25629 }
25630
25631 case MODOP_EXPR:
25632 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25633 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25634
25635 case ARRAY_REF:
25636 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25637 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25638
25639 case ADDR_EXPR:
25640 {
25641 tree op = TREE_OPERAND (expression, 0);
25642 return (value_dependent_expression_p (op)
25643 || has_value_dependent_address (op));
25644 }
25645
25646 case REQUIRES_EXPR:
25647 /* Treat all requires-expressions as value-dependent so
25648 we don't try to fold them. */
25649 return true;
25650
25651 case TYPE_REQ:
25652 return dependent_type_p (TREE_OPERAND (expression, 0));
25653
25654 case CALL_EXPR:
25655 {
25656 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25657 return true;
25658 tree fn = get_callee_fndecl (expression);
25659 int i, nargs;
25660 nargs = call_expr_nargs (expression);
25661 for (i = 0; i < nargs; ++i)
25662 {
25663 tree op = CALL_EXPR_ARG (expression, i);
25664 /* In a call to a constexpr member function, look through the
25665 implicit ADDR_EXPR on the object argument so that it doesn't
25666 cause the call to be considered value-dependent. We also
25667 look through it in potential_constant_expression. */
25668 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25669 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25670 && TREE_CODE (op) == ADDR_EXPR)
25671 op = TREE_OPERAND (op, 0);
25672 if (value_dependent_expression_p (op))
25673 return true;
25674 }
25675 return false;
25676 }
25677
25678 case TEMPLATE_ID_EXPR:
25679 return variable_concept_p (TREE_OPERAND (expression, 0));
25680
25681 case CONSTRUCTOR:
25682 {
25683 unsigned ix;
25684 tree val;
25685 if (dependent_type_p (TREE_TYPE (expression)))
25686 return true;
25687 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25688 if (value_dependent_expression_p (val))
25689 return true;
25690 return false;
25691 }
25692
25693 case STMT_EXPR:
25694 /* Treat a GNU statement expression as dependent to avoid crashing
25695 under instantiate_non_dependent_expr; it can't be constant. */
25696 return true;
25697
25698 default:
25699 /* A constant expression is value-dependent if any subexpression is
25700 value-dependent. */
25701 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25702 {
25703 case tcc_reference:
25704 case tcc_unary:
25705 case tcc_comparison:
25706 case tcc_binary:
25707 case tcc_expression:
25708 case tcc_vl_exp:
25709 {
25710 int i, len = cp_tree_operand_length (expression);
25711
25712 for (i = 0; i < len; i++)
25713 {
25714 tree t = TREE_OPERAND (expression, i);
25715
25716 /* In some cases, some of the operands may be missing.
25717 (For example, in the case of PREDECREMENT_EXPR, the
25718 amount to increment by may be missing.) That doesn't
25719 make the expression dependent. */
25720 if (t && value_dependent_expression_p (t))
25721 return true;
25722 }
25723 }
25724 break;
25725 default:
25726 break;
25727 }
25728 break;
25729 }
25730
25731 /* The expression is not value-dependent. */
25732 return false;
25733 }
25734
25735 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25736 [temp.dep.expr]. Note that an expression with no type is
25737 considered dependent. Other parts of the compiler arrange for an
25738 expression with type-dependent subexpressions to have no type, so
25739 this function doesn't have to be fully recursive. */
25740
25741 bool
25742 type_dependent_expression_p (tree expression)
25743 {
25744 if (!processing_template_decl)
25745 return false;
25746
25747 if (expression == NULL_TREE || expression == error_mark_node)
25748 return false;
25749
25750 STRIP_ANY_LOCATION_WRAPPER (expression);
25751
25752 /* An unresolved name is always dependent. */
25753 if (identifier_p (expression)
25754 || TREE_CODE (expression) == USING_DECL
25755 || TREE_CODE (expression) == WILDCARD_DECL)
25756 return true;
25757
25758 /* A lambda-expression in template context is dependent. dependent_type_p is
25759 true for a lambda in the scope of a class or function template, but that
25760 doesn't cover all template contexts, like a default template argument. */
25761 if (TREE_CODE (expression) == LAMBDA_EXPR)
25762 return true;
25763
25764 /* A fold expression is type-dependent. */
25765 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25766 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25767 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25768 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25769 return true;
25770
25771 /* Some expression forms are never type-dependent. */
25772 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25773 || TREE_CODE (expression) == SIZEOF_EXPR
25774 || TREE_CODE (expression) == ALIGNOF_EXPR
25775 || TREE_CODE (expression) == AT_ENCODE_EXPR
25776 || TREE_CODE (expression) == NOEXCEPT_EXPR
25777 || TREE_CODE (expression) == TRAIT_EXPR
25778 || TREE_CODE (expression) == TYPEID_EXPR
25779 || TREE_CODE (expression) == DELETE_EXPR
25780 || TREE_CODE (expression) == VEC_DELETE_EXPR
25781 || TREE_CODE (expression) == THROW_EXPR
25782 || TREE_CODE (expression) == REQUIRES_EXPR)
25783 return false;
25784
25785 /* The types of these expressions depends only on the type to which
25786 the cast occurs. */
25787 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25788 || TREE_CODE (expression) == STATIC_CAST_EXPR
25789 || TREE_CODE (expression) == CONST_CAST_EXPR
25790 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25791 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25792 || TREE_CODE (expression) == CAST_EXPR)
25793 return dependent_type_p (TREE_TYPE (expression));
25794
25795 /* The types of these expressions depends only on the type created
25796 by the expression. */
25797 if (TREE_CODE (expression) == NEW_EXPR
25798 || TREE_CODE (expression) == VEC_NEW_EXPR)
25799 {
25800 /* For NEW_EXPR tree nodes created inside a template, either
25801 the object type itself or a TREE_LIST may appear as the
25802 operand 1. */
25803 tree type = TREE_OPERAND (expression, 1);
25804 if (TREE_CODE (type) == TREE_LIST)
25805 /* This is an array type. We need to check array dimensions
25806 as well. */
25807 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25808 || value_dependent_expression_p
25809 (TREE_OPERAND (TREE_VALUE (type), 1));
25810 else
25811 return dependent_type_p (type);
25812 }
25813
25814 if (TREE_CODE (expression) == SCOPE_REF)
25815 {
25816 tree scope = TREE_OPERAND (expression, 0);
25817 tree name = TREE_OPERAND (expression, 1);
25818
25819 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25820 contains an identifier associated by name lookup with one or more
25821 declarations declared with a dependent type, or...a
25822 nested-name-specifier or qualified-id that names a member of an
25823 unknown specialization. */
25824 return (type_dependent_expression_p (name)
25825 || dependent_scope_p (scope));
25826 }
25827
25828 if (TREE_CODE (expression) == TEMPLATE_DECL
25829 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25830 return uses_outer_template_parms (expression);
25831
25832 if (TREE_CODE (expression) == STMT_EXPR)
25833 expression = stmt_expr_value_expr (expression);
25834
25835 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25836 {
25837 tree elt;
25838 unsigned i;
25839
25840 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25841 {
25842 if (type_dependent_expression_p (elt))
25843 return true;
25844 }
25845 return false;
25846 }
25847
25848 /* A static data member of the current instantiation with incomplete
25849 array type is type-dependent, as the definition and specializations
25850 can have different bounds. */
25851 if (VAR_P (expression)
25852 && DECL_CLASS_SCOPE_P (expression)
25853 && dependent_type_p (DECL_CONTEXT (expression))
25854 && VAR_HAD_UNKNOWN_BOUND (expression))
25855 return true;
25856
25857 /* An array of unknown bound depending on a variadic parameter, eg:
25858
25859 template<typename... Args>
25860 void foo (Args... args)
25861 {
25862 int arr[] = { args... };
25863 }
25864
25865 template<int... vals>
25866 void bar ()
25867 {
25868 int arr[] = { vals... };
25869 }
25870
25871 If the array has no length and has an initializer, it must be that
25872 we couldn't determine its length in cp_complete_array_type because
25873 it is dependent. */
25874 if (VAR_P (expression)
25875 && TREE_TYPE (expression) != NULL_TREE
25876 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25877 && !TYPE_DOMAIN (TREE_TYPE (expression))
25878 && DECL_INITIAL (expression))
25879 return true;
25880
25881 /* A function or variable template-id is type-dependent if it has any
25882 dependent template arguments. */
25883 if (VAR_OR_FUNCTION_DECL_P (expression)
25884 && DECL_LANG_SPECIFIC (expression)
25885 && DECL_TEMPLATE_INFO (expression))
25886 {
25887 /* Consider the innermost template arguments, since those are the ones
25888 that come from the template-id; the template arguments for the
25889 enclosing class do not make it type-dependent unless they are used in
25890 the type of the decl. */
25891 if (instantiates_primary_template_p (expression)
25892 && (any_dependent_template_arguments_p
25893 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25894 return true;
25895 }
25896
25897 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25898 type-dependent. Checking this is important for functions with auto return
25899 type, which looks like a dependent type. */
25900 if (TREE_CODE (expression) == FUNCTION_DECL
25901 && !(DECL_CLASS_SCOPE_P (expression)
25902 && dependent_type_p (DECL_CONTEXT (expression)))
25903 && !(DECL_LANG_SPECIFIC (expression)
25904 && DECL_FRIEND_P (expression)
25905 && (!DECL_FRIEND_CONTEXT (expression)
25906 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25907 && !DECL_LOCAL_FUNCTION_P (expression))
25908 {
25909 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25910 || undeduced_auto_decl (expression));
25911 return false;
25912 }
25913
25914 /* Always dependent, on the number of arguments if nothing else. */
25915 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25916 return true;
25917
25918 if (TREE_TYPE (expression) == unknown_type_node)
25919 {
25920 if (TREE_CODE (expression) == ADDR_EXPR)
25921 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25922 if (TREE_CODE (expression) == COMPONENT_REF
25923 || TREE_CODE (expression) == OFFSET_REF)
25924 {
25925 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25926 return true;
25927 expression = TREE_OPERAND (expression, 1);
25928 if (identifier_p (expression))
25929 return false;
25930 }
25931 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25932 if (TREE_CODE (expression) == SCOPE_REF)
25933 return false;
25934
25935 if (BASELINK_P (expression))
25936 {
25937 if (BASELINK_OPTYPE (expression)
25938 && dependent_type_p (BASELINK_OPTYPE (expression)))
25939 return true;
25940 expression = BASELINK_FUNCTIONS (expression);
25941 }
25942
25943 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25944 {
25945 if (any_dependent_template_arguments_p
25946 (TREE_OPERAND (expression, 1)))
25947 return true;
25948 expression = TREE_OPERAND (expression, 0);
25949 if (identifier_p (expression))
25950 return true;
25951 }
25952
25953 gcc_assert (TREE_CODE (expression) == OVERLOAD
25954 || TREE_CODE (expression) == FUNCTION_DECL);
25955
25956 for (lkp_iterator iter (expression); iter; ++iter)
25957 if (type_dependent_expression_p (*iter))
25958 return true;
25959
25960 return false;
25961 }
25962
25963 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25964
25965 /* Dependent type attributes might not have made it from the decl to
25966 the type yet. */
25967 if (DECL_P (expression)
25968 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
25969 return true;
25970
25971 return (dependent_type_p (TREE_TYPE (expression)));
25972 }
25973
25974 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25975 type-dependent if the expression refers to a member of the current
25976 instantiation and the type of the referenced member is dependent, or the
25977 class member access expression refers to a member of an unknown
25978 specialization.
25979
25980 This function returns true if the OBJECT in such a class member access
25981 expression is of an unknown specialization. */
25982
25983 bool
25984 type_dependent_object_expression_p (tree object)
25985 {
25986 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25987 dependent. */
25988 if (TREE_CODE (object) == IDENTIFIER_NODE)
25989 return true;
25990 tree scope = TREE_TYPE (object);
25991 return (!scope || dependent_scope_p (scope));
25992 }
25993
25994 /* walk_tree callback function for instantiation_dependent_expression_p,
25995 below. Returns non-zero if a dependent subexpression is found. */
25996
25997 static tree
25998 instantiation_dependent_r (tree *tp, int *walk_subtrees,
25999 void * /*data*/)
26000 {
26001 if (TYPE_P (*tp))
26002 {
26003 /* We don't have to worry about decltype currently because decltype
26004 of an instantiation-dependent expr is a dependent type. This
26005 might change depending on the resolution of DR 1172. */
26006 *walk_subtrees = false;
26007 return NULL_TREE;
26008 }
26009 enum tree_code code = TREE_CODE (*tp);
26010 switch (code)
26011 {
26012 /* Don't treat an argument list as dependent just because it has no
26013 TREE_TYPE. */
26014 case TREE_LIST:
26015 case TREE_VEC:
26016 case NONTYPE_ARGUMENT_PACK:
26017 return NULL_TREE;
26018
26019 case TEMPLATE_PARM_INDEX:
26020 if (dependent_type_p (TREE_TYPE (*tp)))
26021 return *tp;
26022 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26023 return *tp;
26024 /* We'll check value-dependence separately. */
26025 return NULL_TREE;
26026
26027 /* Handle expressions with type operands. */
26028 case SIZEOF_EXPR:
26029 case ALIGNOF_EXPR:
26030 case TYPEID_EXPR:
26031 case AT_ENCODE_EXPR:
26032 {
26033 tree op = TREE_OPERAND (*tp, 0);
26034 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26035 op = TREE_TYPE (op);
26036 if (TYPE_P (op))
26037 {
26038 if (dependent_type_p (op))
26039 return *tp;
26040 else
26041 {
26042 *walk_subtrees = false;
26043 return NULL_TREE;
26044 }
26045 }
26046 break;
26047 }
26048
26049 case COMPONENT_REF:
26050 if (identifier_p (TREE_OPERAND (*tp, 1)))
26051 /* In a template, finish_class_member_access_expr creates a
26052 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26053 type-dependent, so that we can check access control at
26054 instantiation time (PR 42277). See also Core issue 1273. */
26055 return *tp;
26056 break;
26057
26058 case SCOPE_REF:
26059 if (instantiation_dependent_scope_ref_p (*tp))
26060 return *tp;
26061 else
26062 break;
26063
26064 /* Treat statement-expressions as dependent. */
26065 case BIND_EXPR:
26066 return *tp;
26067
26068 /* Treat requires-expressions as dependent. */
26069 case REQUIRES_EXPR:
26070 return *tp;
26071
26072 case CALL_EXPR:
26073 /* Treat calls to function concepts as dependent. */
26074 if (function_concept_check_p (*tp))
26075 return *tp;
26076 break;
26077
26078 case TEMPLATE_ID_EXPR:
26079 /* And variable concepts. */
26080 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
26081 return *tp;
26082 break;
26083
26084 case CONSTRUCTOR:
26085 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26086 return *tp;
26087 break;
26088
26089 default:
26090 break;
26091 }
26092
26093 if (type_dependent_expression_p (*tp))
26094 return *tp;
26095 else
26096 return NULL_TREE;
26097 }
26098
26099 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26100 sense defined by the ABI:
26101
26102 "An expression is instantiation-dependent if it is type-dependent
26103 or value-dependent, or it has a subexpression that is type-dependent
26104 or value-dependent."
26105
26106 Except don't actually check value-dependence for unevaluated expressions,
26107 because in sizeof(i) we don't care about the value of i. Checking
26108 type-dependence will in turn check value-dependence of array bounds/template
26109 arguments as needed. */
26110
26111 bool
26112 instantiation_dependent_uneval_expression_p (tree expression)
26113 {
26114 tree result;
26115
26116 if (!processing_template_decl)
26117 return false;
26118
26119 if (expression == error_mark_node)
26120 return false;
26121
26122 result = cp_walk_tree_without_duplicates (&expression,
26123 instantiation_dependent_r, NULL);
26124 return result != NULL_TREE;
26125 }
26126
26127 /* As above, but also check value-dependence of the expression as a whole. */
26128
26129 bool
26130 instantiation_dependent_expression_p (tree expression)
26131 {
26132 return (instantiation_dependent_uneval_expression_p (expression)
26133 || value_dependent_expression_p (expression));
26134 }
26135
26136 /* Like type_dependent_expression_p, but it also works while not processing
26137 a template definition, i.e. during substitution or mangling. */
26138
26139 bool
26140 type_dependent_expression_p_push (tree expr)
26141 {
26142 bool b;
26143 ++processing_template_decl;
26144 b = type_dependent_expression_p (expr);
26145 --processing_template_decl;
26146 return b;
26147 }
26148
26149 /* Returns TRUE if ARGS contains a type-dependent expression. */
26150
26151 bool
26152 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26153 {
26154 unsigned int i;
26155 tree arg;
26156
26157 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26158 {
26159 if (type_dependent_expression_p (arg))
26160 return true;
26161 }
26162 return false;
26163 }
26164
26165 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26166 expressions) contains any type-dependent expressions. */
26167
26168 bool
26169 any_type_dependent_elements_p (const_tree list)
26170 {
26171 for (; list; list = TREE_CHAIN (list))
26172 if (type_dependent_expression_p (TREE_VALUE (list)))
26173 return true;
26174
26175 return false;
26176 }
26177
26178 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26179 expressions) contains any value-dependent expressions. */
26180
26181 bool
26182 any_value_dependent_elements_p (const_tree list)
26183 {
26184 for (; list; list = TREE_CHAIN (list))
26185 if (value_dependent_expression_p (TREE_VALUE (list)))
26186 return true;
26187
26188 return false;
26189 }
26190
26191 /* Returns TRUE if the ARG (a template argument) is dependent. */
26192
26193 bool
26194 dependent_template_arg_p (tree arg)
26195 {
26196 if (!processing_template_decl)
26197 return false;
26198
26199 /* Assume a template argument that was wrongly written by the user
26200 is dependent. This is consistent with what
26201 any_dependent_template_arguments_p [that calls this function]
26202 does. */
26203 if (!arg || arg == error_mark_node)
26204 return true;
26205
26206 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26207 arg = argument_pack_select_arg (arg);
26208
26209 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26210 return true;
26211 if (TREE_CODE (arg) == TEMPLATE_DECL)
26212 {
26213 if (DECL_TEMPLATE_PARM_P (arg))
26214 return true;
26215 /* A member template of a dependent class is not necessarily
26216 type-dependent, but it is a dependent template argument because it
26217 will be a member of an unknown specialization to that template. */
26218 tree scope = CP_DECL_CONTEXT (arg);
26219 return TYPE_P (scope) && dependent_type_p (scope);
26220 }
26221 else if (ARGUMENT_PACK_P (arg))
26222 {
26223 tree args = ARGUMENT_PACK_ARGS (arg);
26224 int i, len = TREE_VEC_LENGTH (args);
26225 for (i = 0; i < len; ++i)
26226 {
26227 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26228 return true;
26229 }
26230
26231 return false;
26232 }
26233 else if (TYPE_P (arg))
26234 return dependent_type_p (arg);
26235 else
26236 return (type_dependent_expression_p (arg)
26237 || value_dependent_expression_p (arg));
26238 }
26239
26240 /* Returns true if ARGS (a collection of template arguments) contains
26241 any types that require structural equality testing. */
26242
26243 bool
26244 any_template_arguments_need_structural_equality_p (tree args)
26245 {
26246 int i;
26247 int j;
26248
26249 if (!args)
26250 return false;
26251 if (args == error_mark_node)
26252 return true;
26253
26254 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26255 {
26256 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26257 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26258 {
26259 tree arg = TREE_VEC_ELT (level, j);
26260 tree packed_args = NULL_TREE;
26261 int k, len = 1;
26262
26263 if (ARGUMENT_PACK_P (arg))
26264 {
26265 /* Look inside the argument pack. */
26266 packed_args = ARGUMENT_PACK_ARGS (arg);
26267 len = TREE_VEC_LENGTH (packed_args);
26268 }
26269
26270 for (k = 0; k < len; ++k)
26271 {
26272 if (packed_args)
26273 arg = TREE_VEC_ELT (packed_args, k);
26274
26275 if (error_operand_p (arg))
26276 return true;
26277 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26278 continue;
26279 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26280 return true;
26281 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26282 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26283 return true;
26284 }
26285 }
26286 }
26287
26288 return false;
26289 }
26290
26291 /* Returns true if ARGS (a collection of template arguments) contains
26292 any dependent arguments. */
26293
26294 bool
26295 any_dependent_template_arguments_p (const_tree args)
26296 {
26297 int i;
26298 int j;
26299
26300 if (!args)
26301 return false;
26302 if (args == error_mark_node)
26303 return true;
26304
26305 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26306 {
26307 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26308 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26309 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26310 return true;
26311 }
26312
26313 return false;
26314 }
26315
26316 /* Returns true if ARGS contains any errors. */
26317
26318 bool
26319 any_erroneous_template_args_p (const_tree args)
26320 {
26321 int i;
26322 int j;
26323
26324 if (args == error_mark_node)
26325 return true;
26326
26327 if (args && TREE_CODE (args) != TREE_VEC)
26328 {
26329 if (tree ti = get_template_info (args))
26330 args = TI_ARGS (ti);
26331 else
26332 args = NULL_TREE;
26333 }
26334
26335 if (!args)
26336 return false;
26337
26338 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26339 {
26340 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26341 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26342 if (error_operand_p (TREE_VEC_ELT (level, j)))
26343 return true;
26344 }
26345
26346 return false;
26347 }
26348
26349 /* Returns TRUE if the template TMPL is type-dependent. */
26350
26351 bool
26352 dependent_template_p (tree tmpl)
26353 {
26354 if (TREE_CODE (tmpl) == OVERLOAD)
26355 {
26356 for (lkp_iterator iter (tmpl); iter; ++iter)
26357 if (dependent_template_p (*iter))
26358 return true;
26359 return false;
26360 }
26361
26362 /* Template template parameters are dependent. */
26363 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26364 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26365 return true;
26366 /* So are names that have not been looked up. */
26367 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26368 return true;
26369 return false;
26370 }
26371
26372 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26373
26374 bool
26375 dependent_template_id_p (tree tmpl, tree args)
26376 {
26377 return (dependent_template_p (tmpl)
26378 || any_dependent_template_arguments_p (args));
26379 }
26380
26381 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26382 are dependent. */
26383
26384 bool
26385 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26386 {
26387 int i;
26388
26389 if (!processing_template_decl)
26390 return false;
26391
26392 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26393 {
26394 tree decl = TREE_VEC_ELT (declv, i);
26395 tree init = TREE_VEC_ELT (initv, i);
26396 tree cond = TREE_VEC_ELT (condv, i);
26397 tree incr = TREE_VEC_ELT (incrv, i);
26398
26399 if (type_dependent_expression_p (decl)
26400 || TREE_CODE (decl) == SCOPE_REF)
26401 return true;
26402
26403 if (init && type_dependent_expression_p (init))
26404 return true;
26405
26406 if (cond == global_namespace)
26407 return true;
26408
26409 if (type_dependent_expression_p (cond))
26410 return true;
26411
26412 if (COMPARISON_CLASS_P (cond)
26413 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26414 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26415 return true;
26416
26417 if (TREE_CODE (incr) == MODOP_EXPR)
26418 {
26419 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26420 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26421 return true;
26422 }
26423 else if (type_dependent_expression_p (incr))
26424 return true;
26425 else if (TREE_CODE (incr) == MODIFY_EXPR)
26426 {
26427 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
26428 return true;
26429 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
26430 {
26431 tree t = TREE_OPERAND (incr, 1);
26432 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
26433 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
26434 return true;
26435
26436 /* If this loop has a class iterator with != comparison
26437 with increment other than i++/++i/i--/--i, make sure the
26438 increment is constant. */
26439 if (CLASS_TYPE_P (TREE_TYPE (decl))
26440 && TREE_CODE (cond) == NE_EXPR)
26441 {
26442 if (TREE_OPERAND (t, 0) == decl)
26443 t = TREE_OPERAND (t, 1);
26444 else
26445 t = TREE_OPERAND (t, 0);
26446 if (TREE_CODE (t) != INTEGER_CST)
26447 return true;
26448 }
26449 }
26450 }
26451 }
26452
26453 return false;
26454 }
26455
26456 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26457 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26458 no such TYPE can be found. Note that this function peers inside
26459 uninstantiated templates and therefore should be used only in
26460 extremely limited situations. ONLY_CURRENT_P restricts this
26461 peering to the currently open classes hierarchy (which is required
26462 when comparing types). */
26463
26464 tree
26465 resolve_typename_type (tree type, bool only_current_p)
26466 {
26467 tree scope;
26468 tree name;
26469 tree decl;
26470 int quals;
26471 tree pushed_scope;
26472 tree result;
26473
26474 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
26475
26476 scope = TYPE_CONTEXT (type);
26477 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26478 gcc_checking_assert (uses_template_parms (scope));
26479
26480 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26481 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26482 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26483 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26484 identifier of the TYPENAME_TYPE anymore.
26485 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26486 TYPENAME_TYPE instead, we avoid messing up with a possible
26487 typedef variant case. */
26488 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
26489
26490 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26491 it first before we can figure out what NAME refers to. */
26492 if (TREE_CODE (scope) == TYPENAME_TYPE)
26493 {
26494 if (TYPENAME_IS_RESOLVING_P (scope))
26495 /* Given a class template A with a dependent base with nested type C,
26496 typedef typename A::C::C C will land us here, as trying to resolve
26497 the initial A::C leads to the local C typedef, which leads back to
26498 A::C::C. So we break the recursion now. */
26499 return type;
26500 else
26501 scope = resolve_typename_type (scope, only_current_p);
26502 }
26503 /* If we don't know what SCOPE refers to, then we cannot resolve the
26504 TYPENAME_TYPE. */
26505 if (!CLASS_TYPE_P (scope))
26506 return type;
26507 /* If this is a typedef, we don't want to look inside (c++/11987). */
26508 if (typedef_variant_p (type))
26509 return type;
26510 /* If SCOPE isn't the template itself, it will not have a valid
26511 TYPE_FIELDS list. */
26512 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
26513 /* scope is either the template itself or a compatible instantiation
26514 like X<T>, so look up the name in the original template. */
26515 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
26516 /* If scope has no fields, it can't be a current instantiation. Check this
26517 before currently_open_class to avoid infinite recursion (71515). */
26518 if (!TYPE_FIELDS (scope))
26519 return type;
26520 /* If the SCOPE is not the current instantiation, there's no reason
26521 to look inside it. */
26522 if (only_current_p && !currently_open_class (scope))
26523 return type;
26524 /* Enter the SCOPE so that name lookup will be resolved as if we
26525 were in the class definition. In particular, SCOPE will no
26526 longer be considered a dependent type. */
26527 pushed_scope = push_scope (scope);
26528 /* Look up the declaration. */
26529 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
26530 tf_warning_or_error);
26531
26532 result = NULL_TREE;
26533
26534 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26535 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26536 tree fullname = TYPENAME_TYPE_FULLNAME (type);
26537 if (!decl)
26538 /*nop*/;
26539 else if (identifier_p (fullname)
26540 && TREE_CODE (decl) == TYPE_DECL)
26541 {
26542 result = TREE_TYPE (decl);
26543 if (result == error_mark_node)
26544 result = NULL_TREE;
26545 }
26546 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
26547 && DECL_CLASS_TEMPLATE_P (decl))
26548 {
26549 /* Obtain the template and the arguments. */
26550 tree tmpl = TREE_OPERAND (fullname, 0);
26551 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
26552 {
26553 /* We get here with a plain identifier because a previous tentative
26554 parse of the nested-name-specifier as part of a ptr-operator saw
26555 ::template X<A>. The use of ::template is necessary in a
26556 ptr-operator, but wrong in a declarator-id.
26557
26558 [temp.names]: In a qualified-id of a declarator-id, the keyword
26559 template shall not appear at the top level. */
26560 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
26561 "keyword %<template%> not allowed in declarator-id");
26562 tmpl = decl;
26563 }
26564 tree args = TREE_OPERAND (fullname, 1);
26565 /* Instantiate the template. */
26566 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26567 /*entering_scope=*/true,
26568 tf_error | tf_user);
26569 if (result == error_mark_node)
26570 result = NULL_TREE;
26571 }
26572
26573 /* Leave the SCOPE. */
26574 if (pushed_scope)
26575 pop_scope (pushed_scope);
26576
26577 /* If we failed to resolve it, return the original typename. */
26578 if (!result)
26579 return type;
26580
26581 /* If lookup found a typename type, resolve that too. */
26582 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26583 {
26584 /* Ill-formed programs can cause infinite recursion here, so we
26585 must catch that. */
26586 TYPENAME_IS_RESOLVING_P (result) = 1;
26587 result = resolve_typename_type (result, only_current_p);
26588 TYPENAME_IS_RESOLVING_P (result) = 0;
26589 }
26590
26591 /* Qualify the resulting type. */
26592 quals = cp_type_quals (type);
26593 if (quals)
26594 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26595
26596 return result;
26597 }
26598
26599 /* EXPR is an expression which is not type-dependent. Return a proxy
26600 for EXPR that can be used to compute the types of larger
26601 expressions containing EXPR. */
26602
26603 tree
26604 build_non_dependent_expr (tree expr)
26605 {
26606 tree orig_expr = expr;
26607 tree inner_expr;
26608
26609 /* When checking, try to get a constant value for all non-dependent
26610 expressions in order to expose bugs in *_dependent_expression_p
26611 and constexpr. This can affect code generation, see PR70704, so
26612 only do this for -fchecking=2. */
26613 if (flag_checking > 1
26614 && cxx_dialect >= cxx11
26615 /* Don't do this during nsdmi parsing as it can lead to
26616 unexpected recursive instantiations. */
26617 && !parsing_nsdmi ()
26618 /* Don't do this during concept expansion either and for
26619 the same reason. */
26620 && !expanding_concept ())
26621 fold_non_dependent_expr (expr, tf_none);
26622
26623 STRIP_ANY_LOCATION_WRAPPER (expr);
26624
26625 /* Preserve OVERLOADs; the functions must be available to resolve
26626 types. */
26627 inner_expr = expr;
26628 if (TREE_CODE (inner_expr) == STMT_EXPR)
26629 inner_expr = stmt_expr_value_expr (inner_expr);
26630 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26631 inner_expr = TREE_OPERAND (inner_expr, 0);
26632 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26633 inner_expr = TREE_OPERAND (inner_expr, 1);
26634 if (is_overloaded_fn (inner_expr)
26635 || TREE_CODE (inner_expr) == OFFSET_REF)
26636 return orig_expr;
26637 /* There is no need to return a proxy for a variable or enumerator. */
26638 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26639 return orig_expr;
26640 /* Preserve string constants; conversions from string constants to
26641 "char *" are allowed, even though normally a "const char *"
26642 cannot be used to initialize a "char *". */
26643 if (TREE_CODE (expr) == STRING_CST)
26644 return orig_expr;
26645 /* Preserve void and arithmetic constants, as an optimization -- there is no
26646 reason to create a new node. */
26647 if (TREE_CODE (expr) == VOID_CST
26648 || TREE_CODE (expr) == INTEGER_CST
26649 || TREE_CODE (expr) == REAL_CST)
26650 return orig_expr;
26651 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26652 There is at least one place where we want to know that a
26653 particular expression is a throw-expression: when checking a ?:
26654 expression, there are special rules if the second or third
26655 argument is a throw-expression. */
26656 if (TREE_CODE (expr) == THROW_EXPR)
26657 return orig_expr;
26658
26659 /* Don't wrap an initializer list, we need to be able to look inside. */
26660 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26661 return orig_expr;
26662
26663 /* Don't wrap a dummy object, we need to be able to test for it. */
26664 if (is_dummy_object (expr))
26665 return orig_expr;
26666
26667 if (TREE_CODE (expr) == COND_EXPR)
26668 return build3 (COND_EXPR,
26669 TREE_TYPE (expr),
26670 TREE_OPERAND (expr, 0),
26671 (TREE_OPERAND (expr, 1)
26672 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26673 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26674 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26675 if (TREE_CODE (expr) == COMPOUND_EXPR
26676 && !COMPOUND_EXPR_OVERLOADED (expr))
26677 return build2 (COMPOUND_EXPR,
26678 TREE_TYPE (expr),
26679 TREE_OPERAND (expr, 0),
26680 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26681
26682 /* If the type is unknown, it can't really be non-dependent */
26683 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26684
26685 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26686 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26687 TREE_TYPE (expr), expr);
26688 }
26689
26690 /* ARGS is a vector of expressions as arguments to a function call.
26691 Replace the arguments with equivalent non-dependent expressions.
26692 This modifies ARGS in place. */
26693
26694 void
26695 make_args_non_dependent (vec<tree, va_gc> *args)
26696 {
26697 unsigned int ix;
26698 tree arg;
26699
26700 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26701 {
26702 tree newarg = build_non_dependent_expr (arg);
26703 if (newarg != arg)
26704 (*args)[ix] = newarg;
26705 }
26706 }
26707
26708 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26709 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26710 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26711
26712 static tree
26713 make_auto_1 (tree name, bool set_canonical)
26714 {
26715 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26716 TYPE_NAME (au) = build_decl (input_location,
26717 TYPE_DECL, name, au);
26718 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26719 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26720 (0, processing_template_decl + 1, processing_template_decl + 1,
26721 TYPE_NAME (au), NULL_TREE);
26722 if (set_canonical)
26723 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26724 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26725 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26726
26727 return au;
26728 }
26729
26730 tree
26731 make_decltype_auto (void)
26732 {
26733 return make_auto_1 (decltype_auto_identifier, true);
26734 }
26735
26736 tree
26737 make_auto (void)
26738 {
26739 return make_auto_1 (auto_identifier, true);
26740 }
26741
26742 /* Return a C++17 deduction placeholder for class template TMPL. */
26743
26744 tree
26745 make_template_placeholder (tree tmpl)
26746 {
26747 tree t = make_auto_1 (auto_identifier, false);
26748 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26749 /* Our canonical type depends on the placeholder. */
26750 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26751 return t;
26752 }
26753
26754 /* True iff T is a C++17 class template deduction placeholder. */
26755
26756 bool
26757 template_placeholder_p (tree t)
26758 {
26759 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26760 }
26761
26762 /* Make a "constrained auto" type-specifier. This is an
26763 auto type with constraints that must be associated after
26764 deduction. The constraint is formed from the given
26765 CONC and its optional sequence of arguments, which are
26766 non-null if written as partial-concept-id. */
26767
26768 tree
26769 make_constrained_auto (tree con, tree args)
26770 {
26771 tree type = make_auto_1 (auto_identifier, false);
26772
26773 /* Build the constraint. */
26774 tree tmpl = DECL_TI_TEMPLATE (con);
26775 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26776 expr = build_concept_check (expr, type, args);
26777
26778 tree constr = normalize_expression (expr);
26779 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26780
26781 /* Our canonical type depends on the constraint. */
26782 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26783
26784 /* Attach the constraint to the type declaration. */
26785 tree decl = TYPE_NAME (type);
26786 return decl;
26787 }
26788
26789 /* Given type ARG, return std::initializer_list<ARG>. */
26790
26791 static tree
26792 listify (tree arg)
26793 {
26794 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26795
26796 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26797 {
26798 gcc_rich_location richloc (input_location);
26799 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26800 error_at (&richloc,
26801 "deducing from brace-enclosed initializer list"
26802 " requires %<#include <initializer_list>%>");
26803
26804 return error_mark_node;
26805 }
26806 tree argvec = make_tree_vec (1);
26807 TREE_VEC_ELT (argvec, 0) = arg;
26808
26809 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26810 NULL_TREE, 0, tf_warning_or_error);
26811 }
26812
26813 /* Replace auto in TYPE with std::initializer_list<auto>. */
26814
26815 static tree
26816 listify_autos (tree type, tree auto_node)
26817 {
26818 tree init_auto = listify (auto_node);
26819 tree argvec = make_tree_vec (1);
26820 TREE_VEC_ELT (argvec, 0) = init_auto;
26821 if (processing_template_decl)
26822 argvec = add_to_template_args (current_template_args (), argvec);
26823 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26824 }
26825
26826 /* Hash traits for hashing possibly constrained 'auto'
26827 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26828
26829 struct auto_hash : default_hash_traits<tree>
26830 {
26831 static inline hashval_t hash (tree);
26832 static inline bool equal (tree, tree);
26833 };
26834
26835 /* Hash the 'auto' T. */
26836
26837 inline hashval_t
26838 auto_hash::hash (tree t)
26839 {
26840 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26841 /* Matching constrained-type-specifiers denote the same template
26842 parameter, so hash the constraint. */
26843 return hash_placeholder_constraint (c);
26844 else
26845 /* But unconstrained autos are all separate, so just hash the pointer. */
26846 return iterative_hash_object (t, 0);
26847 }
26848
26849 /* Compare two 'auto's. */
26850
26851 inline bool
26852 auto_hash::equal (tree t1, tree t2)
26853 {
26854 if (t1 == t2)
26855 return true;
26856
26857 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26858 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26859
26860 /* Two unconstrained autos are distinct. */
26861 if (!c1 || !c2)
26862 return false;
26863
26864 return equivalent_placeholder_constraints (c1, c2);
26865 }
26866
26867 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26868 constrained) auto, add it to the vector. */
26869
26870 static int
26871 extract_autos_r (tree t, void *data)
26872 {
26873 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26874 if (is_auto (t))
26875 {
26876 /* All the autos were built with index 0; fix that up now. */
26877 tree *p = hash.find_slot (t, INSERT);
26878 unsigned idx;
26879 if (*p)
26880 /* If this is a repeated constrained-type-specifier, use the index we
26881 chose before. */
26882 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26883 else
26884 {
26885 /* Otherwise this is new, so use the current count. */
26886 *p = t;
26887 idx = hash.elements () - 1;
26888 }
26889 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26890 }
26891
26892 /* Always keep walking. */
26893 return 0;
26894 }
26895
26896 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26897 says they can appear anywhere in the type. */
26898
26899 static tree
26900 extract_autos (tree type)
26901 {
26902 hash_set<tree> visited;
26903 hash_table<auto_hash> hash (2);
26904
26905 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26906
26907 tree tree_vec = make_tree_vec (hash.elements());
26908 for (hash_table<auto_hash>::iterator iter = hash.begin();
26909 iter != hash.end(); ++iter)
26910 {
26911 tree elt = *iter;
26912 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26913 TREE_VEC_ELT (tree_vec, i)
26914 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26915 }
26916
26917 return tree_vec;
26918 }
26919
26920 /* The stem for deduction guide names. */
26921 const char *const dguide_base = "__dguide_";
26922
26923 /* Return the name for a deduction guide for class template TMPL. */
26924
26925 tree
26926 dguide_name (tree tmpl)
26927 {
26928 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26929 tree tname = TYPE_IDENTIFIER (type);
26930 char *buf = (char *) alloca (1 + strlen (dguide_base)
26931 + IDENTIFIER_LENGTH (tname));
26932 memcpy (buf, dguide_base, strlen (dguide_base));
26933 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26934 IDENTIFIER_LENGTH (tname) + 1);
26935 tree dname = get_identifier (buf);
26936 TREE_TYPE (dname) = type;
26937 return dname;
26938 }
26939
26940 /* True if NAME is the name of a deduction guide. */
26941
26942 bool
26943 dguide_name_p (tree name)
26944 {
26945 return (TREE_CODE (name) == IDENTIFIER_NODE
26946 && TREE_TYPE (name)
26947 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26948 strlen (dguide_base)));
26949 }
26950
26951 /* True if FN is a deduction guide. */
26952
26953 bool
26954 deduction_guide_p (const_tree fn)
26955 {
26956 if (DECL_P (fn))
26957 if (tree name = DECL_NAME (fn))
26958 return dguide_name_p (name);
26959 return false;
26960 }
26961
26962 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26963
26964 bool
26965 copy_guide_p (const_tree fn)
26966 {
26967 gcc_assert (deduction_guide_p (fn));
26968 if (!DECL_ARTIFICIAL (fn))
26969 return false;
26970 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
26971 return (TREE_CHAIN (parms) == void_list_node
26972 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
26973 }
26974
26975 /* True if FN is a guide generated from a constructor template. */
26976
26977 bool
26978 template_guide_p (const_tree fn)
26979 {
26980 gcc_assert (deduction_guide_p (fn));
26981 if (!DECL_ARTIFICIAL (fn))
26982 return false;
26983 tree tmpl = DECL_TI_TEMPLATE (fn);
26984 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
26985 return PRIMARY_TEMPLATE_P (org);
26986 return false;
26987 }
26988
26989 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26990 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26991 template parameter types. Note that the handling of template template
26992 parameters relies on current_template_parms being set appropriately for the
26993 new template. */
26994
26995 static tree
26996 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
26997 tree tsubst_args, tsubst_flags_t complain)
26998 {
26999 if (olddecl == error_mark_node)
27000 return error_mark_node;
27001
27002 tree oldidx = get_template_parm_index (olddecl);
27003
27004 tree newtype;
27005 if (TREE_CODE (olddecl) == TYPE_DECL
27006 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27007 {
27008 tree oldtype = TREE_TYPE (olddecl);
27009 newtype = cxx_make_type (TREE_CODE (oldtype));
27010 TYPE_MAIN_VARIANT (newtype) = newtype;
27011 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27012 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27013 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27014 }
27015 else
27016 {
27017 newtype = TREE_TYPE (olddecl);
27018 if (type_uses_auto (newtype))
27019 {
27020 // Substitute once to fix references to other template parameters.
27021 newtype = tsubst (newtype, tsubst_args,
27022 complain|tf_partial, NULL_TREE);
27023 // Now substitute again to reduce the level of the auto.
27024 newtype = tsubst (newtype, current_template_args (),
27025 complain, NULL_TREE);
27026 }
27027 else
27028 newtype = tsubst (newtype, tsubst_args,
27029 complain, NULL_TREE);
27030 }
27031
27032 tree newdecl
27033 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27034 DECL_NAME (olddecl), newtype);
27035 SET_DECL_TEMPLATE_PARM_P (newdecl);
27036
27037 tree newidx;
27038 if (TREE_CODE (olddecl) == TYPE_DECL
27039 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27040 {
27041 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27042 = build_template_parm_index (index, level, level,
27043 newdecl, newtype);
27044 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27045 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27046 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27047 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27048
27049 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27050 {
27051 DECL_TEMPLATE_RESULT (newdecl)
27052 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27053 DECL_NAME (olddecl), newtype);
27054 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27055 // First create a copy (ttargs) of tsubst_args with an
27056 // additional level for the template template parameter's own
27057 // template parameters (ttparms).
27058 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27059 (DECL_TEMPLATE_PARMS (olddecl)));
27060 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27061 tree ttargs = make_tree_vec (depth + 1);
27062 for (int i = 0; i < depth; ++i)
27063 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27064 TREE_VEC_ELT (ttargs, depth)
27065 = template_parms_level_to_args (ttparms);
27066 // Substitute ttargs into ttparms to fix references to
27067 // other template parameters.
27068 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27069 complain|tf_partial);
27070 // Now substitute again with args based on tparms, to reduce
27071 // the level of the ttparms.
27072 ttargs = current_template_args ();
27073 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27074 complain);
27075 // Finally, tack the adjusted parms onto tparms.
27076 ttparms = tree_cons (size_int (depth), ttparms,
27077 current_template_parms);
27078 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27079 }
27080 }
27081 else
27082 {
27083 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27084 tree newconst
27085 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27086 TREE_CODE (oldconst),
27087 DECL_NAME (oldconst), newtype);
27088 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27089 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27090 SET_DECL_TEMPLATE_PARM_P (newconst);
27091 newidx = build_template_parm_index (index, level, level,
27092 newconst, newtype);
27093 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27094 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27095 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27096 }
27097
27098 return newdecl;
27099 }
27100
27101 /* Returns a C++17 class deduction guide template based on the constructor
27102 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27103 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27104
27105 static tree
27106 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
27107 {
27108 tree type, tparms, targs, fparms, fargs, ci;
27109 bool memtmpl = false;
27110 bool explicit_p;
27111 location_t loc;
27112 tree fn_tmpl = NULL_TREE;
27113
27114 if (TYPE_P (ctor))
27115 {
27116 type = ctor;
27117 bool copy_p = TYPE_REF_P (type);
27118 if (copy_p)
27119 {
27120 type = TREE_TYPE (type);
27121 fparms = tree_cons (NULL_TREE, type, void_list_node);
27122 }
27123 else
27124 fparms = void_list_node;
27125
27126 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27127 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27128 targs = CLASSTYPE_TI_ARGS (type);
27129 ci = NULL_TREE;
27130 fargs = NULL_TREE;
27131 loc = DECL_SOURCE_LOCATION (ctmpl);
27132 explicit_p = false;
27133 }
27134 else
27135 {
27136 ++processing_template_decl;
27137 bool ok = true;
27138
27139 fn_tmpl
27140 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27141 : DECL_TI_TEMPLATE (ctor));
27142 if (outer_args)
27143 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27144 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27145
27146 type = DECL_CONTEXT (ctor);
27147
27148 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27149 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27150 fully specialized args for the enclosing class. Strip those off, as
27151 the deduction guide won't have those template parameters. */
27152 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27153 TMPL_PARMS_DEPTH (tparms));
27154 /* Discard the 'this' parameter. */
27155 fparms = FUNCTION_ARG_CHAIN (ctor);
27156 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27157 ci = get_constraints (ctor);
27158 loc = DECL_SOURCE_LOCATION (ctor);
27159 explicit_p = DECL_NONCONVERTING_P (ctor);
27160
27161 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27162 {
27163 memtmpl = true;
27164
27165 /* For a member template constructor, we need to flatten the two
27166 template parameter lists into one, and then adjust the function
27167 signature accordingly. This gets...complicated. */
27168 tree save_parms = current_template_parms;
27169
27170 /* For a member template we should have two levels of parms/args, one
27171 for the class and one for the constructor. We stripped
27172 specialized args for further enclosing classes above. */
27173 const int depth = 2;
27174 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27175
27176 /* Template args for translating references to the two-level template
27177 parameters into references to the one-level template parameters we
27178 are creating. */
27179 tree tsubst_args = copy_node (targs);
27180 TMPL_ARGS_LEVEL (tsubst_args, depth)
27181 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27182
27183 /* Template parms for the constructor template. */
27184 tree ftparms = TREE_VALUE (tparms);
27185 unsigned flen = TREE_VEC_LENGTH (ftparms);
27186 /* Template parms for the class template. */
27187 tparms = TREE_CHAIN (tparms);
27188 tree ctparms = TREE_VALUE (tparms);
27189 unsigned clen = TREE_VEC_LENGTH (ctparms);
27190 /* Template parms for the deduction guide start as a copy of the
27191 template parms for the class. We set current_template_parms for
27192 lookup_template_class_1. */
27193 current_template_parms = tparms = copy_node (tparms);
27194 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27195 for (unsigned i = 0; i < clen; ++i)
27196 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27197
27198 /* Now we need to rewrite the constructor parms to append them to the
27199 class parms. */
27200 for (unsigned i = 0; i < flen; ++i)
27201 {
27202 unsigned index = i + clen;
27203 unsigned level = 1;
27204 tree oldelt = TREE_VEC_ELT (ftparms, i);
27205 tree olddecl = TREE_VALUE (oldelt);
27206 tree newdecl = rewrite_template_parm (olddecl, index, level,
27207 tsubst_args, complain);
27208 if (newdecl == error_mark_node)
27209 ok = false;
27210 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27211 tsubst_args, complain, ctor);
27212 tree list = build_tree_list (newdef, newdecl);
27213 TEMPLATE_PARM_CONSTRAINTS (list)
27214 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27215 tsubst_args, complain, ctor);
27216 TREE_VEC_ELT (new_vec, index) = list;
27217 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27218 }
27219
27220 /* Now we have a final set of template parms to substitute into the
27221 function signature. */
27222 targs = template_parms_to_args (tparms);
27223 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27224 complain, ctor);
27225 if (fparms == error_mark_node)
27226 ok = false;
27227 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27228 if (ci)
27229 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27230
27231 current_template_parms = save_parms;
27232 }
27233
27234 --processing_template_decl;
27235 if (!ok)
27236 return error_mark_node;
27237 }
27238
27239 if (!memtmpl)
27240 {
27241 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27242 tparms = copy_node (tparms);
27243 INNERMOST_TEMPLATE_PARMS (tparms)
27244 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27245 }
27246
27247 tree fntype = build_function_type (type, fparms);
27248 tree ded_fn = build_lang_decl_loc (loc,
27249 FUNCTION_DECL,
27250 dguide_name (type), fntype);
27251 DECL_ARGUMENTS (ded_fn) = fargs;
27252 DECL_ARTIFICIAL (ded_fn) = true;
27253 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27254 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27255 DECL_ARTIFICIAL (ded_tmpl) = true;
27256 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27257 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27258 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27259 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27260 if (DECL_P (ctor))
27261 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27262 if (ci)
27263 set_constraints (ded_tmpl, ci);
27264
27265 return ded_tmpl;
27266 }
27267
27268 /* Deduce template arguments for the class template placeholder PTYPE for
27269 template TMPL based on the initializer INIT, and return the resulting
27270 type. */
27271
27272 static tree
27273 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
27274 tsubst_flags_t complain)
27275 {
27276 if (!DECL_CLASS_TEMPLATE_P (tmpl))
27277 {
27278 /* We should have handled this in the caller. */
27279 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27280 return ptype;
27281 if (complain & tf_error)
27282 error ("non-class template %qT used without template arguments", tmpl);
27283 return error_mark_node;
27284 }
27285 if (init && TREE_TYPE (init) == ptype)
27286 /* Using the template parm as its own argument. */
27287 return ptype;
27288
27289 tree type = TREE_TYPE (tmpl);
27290
27291 bool try_list_ctor = false;
27292
27293 vec<tree,va_gc> *args;
27294 if (init == NULL_TREE
27295 || TREE_CODE (init) == TREE_LIST)
27296 args = make_tree_vector_from_list (init);
27297 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
27298 {
27299 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
27300 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
27301 {
27302 /* As an exception, the first phase in 16.3.1.7 (considering the
27303 initializer list as a single argument) is omitted if the
27304 initializer list consists of a single expression of type cv U,
27305 where U is a specialization of C or a class derived from a
27306 specialization of C. */
27307 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
27308 tree etype = TREE_TYPE (elt);
27309
27310 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
27311 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27312 int err = unify (tparms, targs, type, etype,
27313 UNIFY_ALLOW_DERIVED, /*explain*/false);
27314 if (err == 0)
27315 try_list_ctor = false;
27316 ggc_free (targs);
27317 }
27318 if (try_list_ctor || is_std_init_list (type))
27319 args = make_tree_vector_single (init);
27320 else
27321 args = make_tree_vector_from_ctor (init);
27322 }
27323 else
27324 args = make_tree_vector_single (init);
27325
27326 tree dname = dguide_name (tmpl);
27327 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
27328 /*type*/false, /*complain*/false,
27329 /*hidden*/false);
27330 bool elided = false;
27331 if (cands == error_mark_node)
27332 cands = NULL_TREE;
27333
27334 /* Prune explicit deduction guides in copy-initialization context. */
27335 if (flags & LOOKUP_ONLYCONVERTING)
27336 {
27337 for (lkp_iterator iter (cands); !elided && iter; ++iter)
27338 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27339 elided = true;
27340
27341 if (elided)
27342 {
27343 /* Found a nonconverting guide, prune the candidates. */
27344 tree pruned = NULL_TREE;
27345 for (lkp_iterator iter (cands); iter; ++iter)
27346 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27347 pruned = lookup_add (*iter, pruned);
27348
27349 cands = pruned;
27350 }
27351 }
27352
27353 tree outer_args = NULL_TREE;
27354 if (DECL_CLASS_SCOPE_P (tmpl)
27355 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
27356 {
27357 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
27358 type = TREE_TYPE (most_general_template (tmpl));
27359 }
27360
27361 bool saw_ctor = false;
27362 // FIXME cache artificial deduction guides
27363 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
27364 {
27365 /* Skip inherited constructors. */
27366 if (iter.using_p ())
27367 continue;
27368
27369 tree guide = build_deduction_guide (*iter, outer_args, complain);
27370 if (guide == error_mark_node)
27371 return error_mark_node;
27372 if ((flags & LOOKUP_ONLYCONVERTING)
27373 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
27374 elided = true;
27375 else
27376 cands = lookup_add (guide, cands);
27377
27378 saw_ctor = true;
27379 }
27380
27381 tree call = error_mark_node;
27382
27383 /* If this is list-initialization and the class has a list constructor, first
27384 try deducing from the list as a single argument, as [over.match.list]. */
27385 tree list_cands = NULL_TREE;
27386 if (try_list_ctor && cands)
27387 for (lkp_iterator iter (cands); iter; ++iter)
27388 {
27389 tree dg = *iter;
27390 if (is_list_ctor (dg))
27391 list_cands = lookup_add (dg, list_cands);
27392 }
27393 if (list_cands)
27394 {
27395 ++cp_unevaluated_operand;
27396 call = build_new_function_call (list_cands, &args, tf_decltype);
27397 --cp_unevaluated_operand;
27398
27399 if (call == error_mark_node)
27400 {
27401 /* That didn't work, now try treating the list as a sequence of
27402 arguments. */
27403 release_tree_vector (args);
27404 args = make_tree_vector_from_ctor (init);
27405 }
27406 }
27407
27408 /* Maybe generate an implicit deduction guide. */
27409 if (call == error_mark_node && args->length () < 2)
27410 {
27411 tree gtype = NULL_TREE;
27412
27413 if (args->length () == 1)
27414 /* Generate a copy guide. */
27415 gtype = build_reference_type (type);
27416 else if (!saw_ctor)
27417 /* Generate a default guide. */
27418 gtype = type;
27419
27420 if (gtype)
27421 {
27422 tree guide = build_deduction_guide (gtype, outer_args, complain);
27423 if (guide == error_mark_node)
27424 return error_mark_node;
27425 cands = lookup_add (guide, cands);
27426 }
27427 }
27428
27429 if (elided && !cands)
27430 {
27431 error ("cannot deduce template arguments for copy-initialization"
27432 " of %qT, as it has no non-explicit deduction guides or "
27433 "user-declared constructors", type);
27434 return error_mark_node;
27435 }
27436 else if (!cands && call == error_mark_node)
27437 {
27438 error ("cannot deduce template arguments of %qT, as it has no viable "
27439 "deduction guides", type);
27440 return error_mark_node;
27441 }
27442
27443 if (call == error_mark_node)
27444 {
27445 ++cp_unevaluated_operand;
27446 call = build_new_function_call (cands, &args, tf_decltype);
27447 --cp_unevaluated_operand;
27448 }
27449
27450 if (call == error_mark_node && (complain & tf_warning_or_error))
27451 {
27452 error ("class template argument deduction failed:");
27453
27454 ++cp_unevaluated_operand;
27455 call = build_new_function_call (cands, &args, complain | tf_decltype);
27456 --cp_unevaluated_operand;
27457
27458 if (elided)
27459 inform (input_location, "explicit deduction guides not considered "
27460 "for copy-initialization");
27461 }
27462
27463 release_tree_vector (args);
27464
27465 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
27466 }
27467
27468 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27469 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27470 The CONTEXT determines the context in which auto deduction is performed
27471 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27472 OUTER_TARGS are used during template argument deduction
27473 (context == adc_unify) to properly substitute the result, and is ignored
27474 in other contexts.
27475
27476 For partial-concept-ids, extra args may be appended to the list of deduced
27477 template arguments prior to determining constraint satisfaction. */
27478
27479 tree
27480 do_auto_deduction (tree type, tree init, tree auto_node,
27481 tsubst_flags_t complain, auto_deduction_context context,
27482 tree outer_targs, int flags)
27483 {
27484 tree targs;
27485
27486 if (init == error_mark_node)
27487 return error_mark_node;
27488
27489 if (init && type_dependent_expression_p (init)
27490 && context != adc_unify)
27491 /* Defining a subset of type-dependent expressions that we can deduce
27492 from ahead of time isn't worth the trouble. */
27493 return type;
27494
27495 /* Similarly, we can't deduce from another undeduced decl. */
27496 if (init && undeduced_auto_decl (init))
27497 return type;
27498
27499 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
27500 /* C++17 class template argument deduction. */
27501 return do_class_deduction (type, tmpl, init, flags, complain);
27502
27503 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
27504 /* Nothing we can do with this, even in deduction context. */
27505 return type;
27506
27507 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27508 with either a new invented type template parameter U or, if the
27509 initializer is a braced-init-list (8.5.4), with
27510 std::initializer_list<U>. */
27511 if (BRACE_ENCLOSED_INITIALIZER_P (init))
27512 {
27513 if (!DIRECT_LIST_INIT_P (init))
27514 type = listify_autos (type, auto_node);
27515 else if (CONSTRUCTOR_NELTS (init) == 1)
27516 init = CONSTRUCTOR_ELT (init, 0)->value;
27517 else
27518 {
27519 if (complain & tf_warning_or_error)
27520 {
27521 if (permerror (input_location, "direct-list-initialization of "
27522 "%<auto%> requires exactly one element"))
27523 inform (input_location,
27524 "for deduction to %<std::initializer_list%>, use copy-"
27525 "list-initialization (i.e. add %<=%> before the %<{%>)");
27526 }
27527 type = listify_autos (type, auto_node);
27528 }
27529 }
27530
27531 if (type == error_mark_node)
27532 return error_mark_node;
27533
27534 init = resolve_nondeduced_context (init, complain);
27535
27536 if (context == adc_decomp_type
27537 && auto_node == type
27538 && init != error_mark_node
27539 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
27540 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27541 and initializer has array type, deduce cv-qualified array type. */
27542 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
27543 complain);
27544 else if (AUTO_IS_DECLTYPE (auto_node))
27545 {
27546 tree stripped_init = tree_strip_any_location_wrapper (init);
27547 bool id = (DECL_P (stripped_init)
27548 || ((TREE_CODE (init) == COMPONENT_REF
27549 || TREE_CODE (init) == SCOPE_REF)
27550 && !REF_PARENTHESIZED_P (init)));
27551 targs = make_tree_vec (1);
27552 TREE_VEC_ELT (targs, 0)
27553 = finish_decltype_type (init, id, tf_warning_or_error);
27554 if (type != auto_node)
27555 {
27556 if (complain & tf_error)
27557 error ("%qT as type rather than plain %<decltype(auto)%>", type);
27558 return error_mark_node;
27559 }
27560 }
27561 else
27562 {
27563 tree parms = build_tree_list (NULL_TREE, type);
27564 tree tparms;
27565
27566 if (flag_concepts)
27567 tparms = extract_autos (type);
27568 else
27569 {
27570 tparms = make_tree_vec (1);
27571 TREE_VEC_ELT (tparms, 0)
27572 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27573 }
27574
27575 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27576 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27577 DEDUCE_CALL,
27578 NULL, /*explain_p=*/false);
27579 if (val > 0)
27580 {
27581 if (processing_template_decl)
27582 /* Try again at instantiation time. */
27583 return type;
27584 if (type && type != error_mark_node
27585 && (complain & tf_error))
27586 /* If type is error_mark_node a diagnostic must have been
27587 emitted by now. Also, having a mention to '<type error>'
27588 in the diagnostic is not really useful to the user. */
27589 {
27590 if (cfun && auto_node == current_function_auto_return_pattern
27591 && LAMBDA_FUNCTION_P (current_function_decl))
27592 error ("unable to deduce lambda return type from %qE", init);
27593 else
27594 error ("unable to deduce %qT from %qE", type, init);
27595 type_unification_real (tparms, targs, parms, &init, 1, 0,
27596 DEDUCE_CALL,
27597 NULL, /*explain_p=*/true);
27598 }
27599 return error_mark_node;
27600 }
27601 }
27602
27603 /* Check any placeholder constraints against the deduced type. */
27604 if (flag_concepts && !processing_template_decl)
27605 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27606 {
27607 /* Use the deduced type to check the associated constraints. If we
27608 have a partial-concept-id, rebuild the argument list so that
27609 we check using the extra arguments. */
27610 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27611 tree cargs = CHECK_CONSTR_ARGS (constr);
27612 if (TREE_VEC_LENGTH (cargs) > 1)
27613 {
27614 cargs = copy_node (cargs);
27615 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27616 }
27617 else
27618 cargs = targs;
27619 if (!constraints_satisfied_p (constr, cargs))
27620 {
27621 if (complain & tf_warning_or_error)
27622 {
27623 auto_diagnostic_group d;
27624 switch (context)
27625 {
27626 case adc_unspecified:
27627 case adc_unify:
27628 error("placeholder constraints not satisfied");
27629 break;
27630 case adc_variable_type:
27631 case adc_decomp_type:
27632 error ("deduced initializer does not satisfy "
27633 "placeholder constraints");
27634 break;
27635 case adc_return_type:
27636 error ("deduced return type does not satisfy "
27637 "placeholder constraints");
27638 break;
27639 case adc_requirement:
27640 error ("deduced expression type does not satisfy "
27641 "placeholder constraints");
27642 break;
27643 }
27644 diagnose_constraints (input_location, constr, targs);
27645 }
27646 return error_mark_node;
27647 }
27648 }
27649
27650 if (processing_template_decl && context != adc_unify)
27651 outer_targs = current_template_args ();
27652 targs = add_to_template_args (outer_targs, targs);
27653 return tsubst (type, targs, complain, NULL_TREE);
27654 }
27655
27656 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27657 result. */
27658
27659 tree
27660 splice_late_return_type (tree type, tree late_return_type)
27661 {
27662 if (is_auto (type))
27663 {
27664 if (late_return_type)
27665 return late_return_type;
27666
27667 tree idx = get_template_parm_index (type);
27668 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27669 /* In an abbreviated function template we didn't know we were dealing
27670 with a function template when we saw the auto return type, so update
27671 it to have the correct level. */
27672 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27673 }
27674 return type;
27675 }
27676
27677 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27678 'decltype(auto)' or a deduced class template. */
27679
27680 bool
27681 is_auto (const_tree type)
27682 {
27683 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27684 && (TYPE_IDENTIFIER (type) == auto_identifier
27685 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
27686 return true;
27687 else
27688 return false;
27689 }
27690
27691 /* for_each_template_parm callback for type_uses_auto. */
27692
27693 int
27694 is_auto_r (tree tp, void */*data*/)
27695 {
27696 return is_auto (tp);
27697 }
27698
27699 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27700 a use of `auto'. Returns NULL_TREE otherwise. */
27701
27702 tree
27703 type_uses_auto (tree type)
27704 {
27705 if (type == NULL_TREE)
27706 return NULL_TREE;
27707 else if (flag_concepts)
27708 {
27709 /* The Concepts TS allows multiple autos in one type-specifier; just
27710 return the first one we find, do_auto_deduction will collect all of
27711 them. */
27712 if (uses_template_parms (type))
27713 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27714 /*visited*/NULL, /*nondeduced*/false);
27715 else
27716 return NULL_TREE;
27717 }
27718 else
27719 return find_type_usage (type, is_auto);
27720 }
27721
27722 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27723 concepts are enabled, auto is acceptable in template arguments, but
27724 only when TEMPL identifies a template class. Return TRUE if any
27725 such errors were reported. */
27726
27727 bool
27728 check_auto_in_tmpl_args (tree tmpl, tree args)
27729 {
27730 /* If there were previous errors, nevermind. */
27731 if (!args || TREE_CODE (args) != TREE_VEC)
27732 return false;
27733
27734 /* If TMPL is an identifier, we're parsing and we can't tell yet
27735 whether TMPL is supposed to be a type, a function or a variable.
27736 We'll only be able to tell during template substitution, so we
27737 expect to be called again then. If concepts are enabled and we
27738 know we have a type, we're ok. */
27739 if (flag_concepts
27740 && (identifier_p (tmpl)
27741 || (DECL_P (tmpl)
27742 && (DECL_TYPE_TEMPLATE_P (tmpl)
27743 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27744 return false;
27745
27746 /* Quickly search for any occurrences of auto; usually there won't
27747 be any, and then we'll avoid allocating the vector. */
27748 if (!type_uses_auto (args))
27749 return false;
27750
27751 bool errors = false;
27752
27753 tree vec = extract_autos (args);
27754 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27755 {
27756 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27757 error_at (DECL_SOURCE_LOCATION (xauto),
27758 "invalid use of %qT in template argument", xauto);
27759 errors = true;
27760 }
27761
27762 return errors;
27763 }
27764
27765 /* For a given template T, return the vector of typedefs referenced
27766 in T for which access check is needed at T instantiation time.
27767 T is either a FUNCTION_DECL or a RECORD_TYPE.
27768 Those typedefs were added to T by the function
27769 append_type_to_template_for_access_check. */
27770
27771 vec<qualified_typedef_usage_t, va_gc> *
27772 get_types_needing_access_check (tree t)
27773 {
27774 tree ti;
27775 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27776
27777 if (!t || t == error_mark_node)
27778 return NULL;
27779
27780 if (!(ti = get_template_info (t)))
27781 return NULL;
27782
27783 if (CLASS_TYPE_P (t)
27784 || TREE_CODE (t) == FUNCTION_DECL)
27785 {
27786 if (!TI_TEMPLATE (ti))
27787 return NULL;
27788
27789 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27790 }
27791
27792 return result;
27793 }
27794
27795 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27796 tied to T. That list of typedefs will be access checked at
27797 T instantiation time.
27798 T is either a FUNCTION_DECL or a RECORD_TYPE.
27799 TYPE_DECL is a TYPE_DECL node representing a typedef.
27800 SCOPE is the scope through which TYPE_DECL is accessed.
27801 LOCATION is the location of the usage point of TYPE_DECL.
27802
27803 This function is a subroutine of
27804 append_type_to_template_for_access_check. */
27805
27806 static void
27807 append_type_to_template_for_access_check_1 (tree t,
27808 tree type_decl,
27809 tree scope,
27810 location_t location)
27811 {
27812 qualified_typedef_usage_t typedef_usage;
27813 tree ti;
27814
27815 if (!t || t == error_mark_node)
27816 return;
27817
27818 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27819 || CLASS_TYPE_P (t))
27820 && type_decl
27821 && TREE_CODE (type_decl) == TYPE_DECL
27822 && scope);
27823
27824 if (!(ti = get_template_info (t)))
27825 return;
27826
27827 gcc_assert (TI_TEMPLATE (ti));
27828
27829 typedef_usage.typedef_decl = type_decl;
27830 typedef_usage.context = scope;
27831 typedef_usage.locus = location;
27832
27833 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27834 }
27835
27836 /* Append TYPE_DECL to the template TEMPL.
27837 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27838 At TEMPL instanciation time, TYPE_DECL will be checked to see
27839 if it can be accessed through SCOPE.
27840 LOCATION is the location of the usage point of TYPE_DECL.
27841
27842 e.g. consider the following code snippet:
27843
27844 class C
27845 {
27846 typedef int myint;
27847 };
27848
27849 template<class U> struct S
27850 {
27851 C::myint mi; // <-- usage point of the typedef C::myint
27852 };
27853
27854 S<char> s;
27855
27856 At S<char> instantiation time, we need to check the access of C::myint
27857 In other words, we need to check the access of the myint typedef through
27858 the C scope. For that purpose, this function will add the myint typedef
27859 and the scope C through which its being accessed to a list of typedefs
27860 tied to the template S. That list will be walked at template instantiation
27861 time and access check performed on each typedefs it contains.
27862 Note that this particular code snippet should yield an error because
27863 myint is private to C. */
27864
27865 void
27866 append_type_to_template_for_access_check (tree templ,
27867 tree type_decl,
27868 tree scope,
27869 location_t location)
27870 {
27871 qualified_typedef_usage_t *iter;
27872 unsigned i;
27873
27874 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27875
27876 /* Make sure we don't append the type to the template twice. */
27877 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27878 if (iter->typedef_decl == type_decl && scope == iter->context)
27879 return;
27880
27881 append_type_to_template_for_access_check_1 (templ, type_decl,
27882 scope, location);
27883 }
27884
27885 /* Convert the generic type parameters in PARM that match the types given in the
27886 range [START_IDX, END_IDX) from the current_template_parms into generic type
27887 packs. */
27888
27889 tree
27890 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27891 {
27892 tree current = current_template_parms;
27893 int depth = TMPL_PARMS_DEPTH (current);
27894 current = INNERMOST_TEMPLATE_PARMS (current);
27895 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27896
27897 for (int i = 0; i < start_idx; ++i)
27898 TREE_VEC_ELT (replacement, i)
27899 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27900
27901 for (int i = start_idx; i < end_idx; ++i)
27902 {
27903 /* Create a distinct parameter pack type from the current parm and add it
27904 to the replacement args to tsubst below into the generic function
27905 parameter. */
27906
27907 tree o = TREE_TYPE (TREE_VALUE
27908 (TREE_VEC_ELT (current, i)));
27909 tree t = copy_type (o);
27910 TEMPLATE_TYPE_PARM_INDEX (t)
27911 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27912 o, 0, 0, tf_none);
27913 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27914 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27915 TYPE_MAIN_VARIANT (t) = t;
27916 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27917 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27918 TREE_VEC_ELT (replacement, i) = t;
27919 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27920 }
27921
27922 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27923 TREE_VEC_ELT (replacement, i)
27924 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27925
27926 /* If there are more levels then build up the replacement with the outer
27927 template parms. */
27928 if (depth > 1)
27929 replacement = add_to_template_args (template_parms_to_args
27930 (TREE_CHAIN (current_template_parms)),
27931 replacement);
27932
27933 return tsubst (parm, replacement, tf_none, NULL_TREE);
27934 }
27935
27936 /* Entries in the decl_constraint hash table. */
27937 struct GTY((for_user)) constr_entry
27938 {
27939 tree decl;
27940 tree ci;
27941 };
27942
27943 /* Hashing function and equality for constraint entries. */
27944 struct constr_hasher : ggc_ptr_hash<constr_entry>
27945 {
27946 static hashval_t hash (constr_entry *e)
27947 {
27948 return (hashval_t)DECL_UID (e->decl);
27949 }
27950
27951 static bool equal (constr_entry *e1, constr_entry *e2)
27952 {
27953 return e1->decl == e2->decl;
27954 }
27955 };
27956
27957 /* A mapping from declarations to constraint information. Note that
27958 both templates and their underlying declarations are mapped to the
27959 same constraint information.
27960
27961 FIXME: This is defined in pt.c because garbage collection
27962 code is not being generated for constraint.cc. */
27963
27964 static GTY (()) hash_table<constr_hasher> *decl_constraints;
27965
27966 /* Returns the template constraints of declaration T. If T is not
27967 constrained, return NULL_TREE. Note that T must be non-null. */
27968
27969 tree
27970 get_constraints (tree t)
27971 {
27972 if (!flag_concepts)
27973 return NULL_TREE;
27974
27975 gcc_assert (DECL_P (t));
27976 if (TREE_CODE (t) == TEMPLATE_DECL)
27977 t = DECL_TEMPLATE_RESULT (t);
27978 constr_entry elt = { t, NULL_TREE };
27979 constr_entry* found = decl_constraints->find (&elt);
27980 if (found)
27981 return found->ci;
27982 else
27983 return NULL_TREE;
27984 }
27985
27986 /* Associate the given constraint information CI with the declaration
27987 T. If T is a template, then the constraints are associated with
27988 its underlying declaration. Don't build associations if CI is
27989 NULL_TREE. */
27990
27991 void
27992 set_constraints (tree t, tree ci)
27993 {
27994 if (!ci)
27995 return;
27996 gcc_assert (t && flag_concepts);
27997 if (TREE_CODE (t) == TEMPLATE_DECL)
27998 t = DECL_TEMPLATE_RESULT (t);
27999 gcc_assert (!get_constraints (t));
28000 constr_entry elt = {t, ci};
28001 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
28002 constr_entry* entry = ggc_alloc<constr_entry> ();
28003 *entry = elt;
28004 *slot = entry;
28005 }
28006
28007 /* Remove the associated constraints of the declaration T. */
28008
28009 void
28010 remove_constraints (tree t)
28011 {
28012 gcc_assert (DECL_P (t));
28013 if (TREE_CODE (t) == TEMPLATE_DECL)
28014 t = DECL_TEMPLATE_RESULT (t);
28015
28016 constr_entry elt = {t, NULL_TREE};
28017 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
28018 if (slot)
28019 decl_constraints->clear_slot (slot);
28020 }
28021
28022 /* Memoized satisfaction results for declarations. This
28023 maps the pair (constraint_info, arguments) to the result computed
28024 by constraints_satisfied_p. */
28025
28026 struct GTY((for_user)) constraint_sat_entry
28027 {
28028 tree ci;
28029 tree args;
28030 tree result;
28031 };
28032
28033 /* Hashing function and equality for constraint entries. */
28034
28035 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
28036 {
28037 static hashval_t hash (constraint_sat_entry *e)
28038 {
28039 hashval_t val = iterative_hash_object(e->ci, 0);
28040 return iterative_hash_template_arg (e->args, val);
28041 }
28042
28043 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
28044 {
28045 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
28046 }
28047 };
28048
28049 /* Memoized satisfaction results for concept checks. */
28050
28051 struct GTY((for_user)) concept_spec_entry
28052 {
28053 tree tmpl;
28054 tree args;
28055 tree result;
28056 };
28057
28058 /* Hashing function and equality for constraint entries. */
28059
28060 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
28061 {
28062 static hashval_t hash (concept_spec_entry *e)
28063 {
28064 return hash_tmpl_and_args (e->tmpl, e->args);
28065 }
28066
28067 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
28068 {
28069 ++comparing_specializations;
28070 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
28071 --comparing_specializations;
28072 return eq;
28073 }
28074 };
28075
28076 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
28077 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
28078
28079 /* Search for a memoized satisfaction result. Returns one of the
28080 truth value nodes if previously memoized, or NULL_TREE otherwise. */
28081
28082 tree
28083 lookup_constraint_satisfaction (tree ci, tree args)
28084 {
28085 constraint_sat_entry elt = { ci, args, NULL_TREE };
28086 constraint_sat_entry* found = constraint_memos->find (&elt);
28087 if (found)
28088 return found->result;
28089 else
28090 return NULL_TREE;
28091 }
28092
28093 /* Memoize the result of a satisfication test. Returns the saved result. */
28094
28095 tree
28096 memoize_constraint_satisfaction (tree ci, tree args, tree result)
28097 {
28098 constraint_sat_entry elt = {ci, args, result};
28099 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
28100 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
28101 *entry = elt;
28102 *slot = entry;
28103 return result;
28104 }
28105
28106 /* Search for a memoized satisfaction result for a concept. */
28107
28108 tree
28109 lookup_concept_satisfaction (tree tmpl, tree args)
28110 {
28111 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28112 concept_spec_entry* found = concept_memos->find (&elt);
28113 if (found)
28114 return found->result;
28115 else
28116 return NULL_TREE;
28117 }
28118
28119 /* Memoize the result of a concept check. Returns the saved result. */
28120
28121 tree
28122 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
28123 {
28124 concept_spec_entry elt = {tmpl, args, result};
28125 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
28126 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28127 *entry = elt;
28128 *slot = entry;
28129 return result;
28130 }
28131
28132 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
28133
28134 /* Returns a prior concept specialization. This returns the substituted
28135 and normalized constraints defined by the concept. */
28136
28137 tree
28138 get_concept_expansion (tree tmpl, tree args)
28139 {
28140 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28141 concept_spec_entry* found = concept_expansions->find (&elt);
28142 if (found)
28143 return found->result;
28144 else
28145 return NULL_TREE;
28146 }
28147
28148 /* Save a concept expansion for later. */
28149
28150 tree
28151 save_concept_expansion (tree tmpl, tree args, tree def)
28152 {
28153 concept_spec_entry elt = {tmpl, args, def};
28154 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
28155 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28156 *entry = elt;
28157 *slot = entry;
28158 return def;
28159 }
28160
28161 static hashval_t
28162 hash_subsumption_args (tree t1, tree t2)
28163 {
28164 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
28165 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
28166 int val = 0;
28167 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
28168 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
28169 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
28170 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
28171 return val;
28172 }
28173
28174 /* Compare the constraints of two subsumption entries. The LEFT1 and
28175 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
28176 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
28177
28178 static bool
28179 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
28180 {
28181 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
28182 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
28183 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
28184 CHECK_CONSTR_ARGS (right1)))
28185 return comp_template_args (CHECK_CONSTR_ARGS (left2),
28186 CHECK_CONSTR_ARGS (right2));
28187 return false;
28188 }
28189
28190 /* Key/value pair for learning and memoizing subsumption results. This
28191 associates a pair of check constraints (including arguments) with
28192 a boolean value indicating the result. */
28193
28194 struct GTY((for_user)) subsumption_entry
28195 {
28196 tree t1;
28197 tree t2;
28198 bool result;
28199 };
28200
28201 /* Hashing function and equality for constraint entries. */
28202
28203 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
28204 {
28205 static hashval_t hash (subsumption_entry *e)
28206 {
28207 return hash_subsumption_args (e->t1, e->t2);
28208 }
28209
28210 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
28211 {
28212 ++comparing_specializations;
28213 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
28214 --comparing_specializations;
28215 return eq;
28216 }
28217 };
28218
28219 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
28220
28221 /* Search for a previously cached subsumption result. */
28222
28223 bool*
28224 lookup_subsumption_result (tree t1, tree t2)
28225 {
28226 subsumption_entry elt = { t1, t2, false };
28227 subsumption_entry* found = subsumption_table->find (&elt);
28228 if (found)
28229 return &found->result;
28230 else
28231 return 0;
28232 }
28233
28234 /* Save a subsumption result. */
28235
28236 bool
28237 save_subsumption_result (tree t1, tree t2, bool result)
28238 {
28239 subsumption_entry elt = {t1, t2, result};
28240 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
28241 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
28242 *entry = elt;
28243 *slot = entry;
28244 return result;
28245 }
28246
28247 /* Set up the hash table for constraint association. */
28248
28249 void
28250 init_constraint_processing (void)
28251 {
28252 if (!flag_concepts)
28253 return;
28254
28255 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
28256 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
28257 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
28258 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
28259 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
28260 }
28261
28262 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28263 0..N-1. */
28264
28265 void
28266 declare_integer_pack (void)
28267 {
28268 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28269 build_function_type_list (integer_type_node,
28270 integer_type_node,
28271 NULL_TREE),
28272 NULL_TREE, ECF_CONST);
28273 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28274 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
28275 DECL_FUNCTION_CODE (ipfn)
28276 = (enum built_in_function) (int) CP_BUILT_IN_INTEGER_PACK;
28277 }
28278
28279 /* Set up the hash tables for template instantiations. */
28280
28281 void
28282 init_template_processing (void)
28283 {
28284 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
28285 type_specializations = hash_table<spec_hasher>::create_ggc (37);
28286
28287 if (cxx_dialect >= cxx11)
28288 declare_integer_pack ();
28289 }
28290
28291 /* Print stats about the template hash tables for -fstats. */
28292
28293 void
28294 print_template_statistics (void)
28295 {
28296 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28297 "%f collisions\n", (long) decl_specializations->size (),
28298 (long) decl_specializations->elements (),
28299 decl_specializations->collisions ());
28300 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28301 "%f collisions\n", (long) type_specializations->size (),
28302 (long) type_specializations->elements (),
28303 type_specializations->collisions ());
28304 }
28305
28306 #if CHECKING_P
28307
28308 namespace selftest {
28309
28310 /* Verify that build_non_dependent_expr () works, for various expressions,
28311 and that location wrappers don't affect the results. */
28312
28313 static void
28314 test_build_non_dependent_expr ()
28315 {
28316 location_t loc = BUILTINS_LOCATION;
28317
28318 /* Verify constants, without and with location wrappers. */
28319 tree int_cst = build_int_cst (integer_type_node, 42);
28320 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28321
28322 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28323 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28324 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28325
28326 tree string_lit = build_string (4, "foo");
28327 TREE_TYPE (string_lit) = char_array_type_node;
28328 string_lit = fix_string_type (string_lit);
28329 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28330
28331 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28332 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28333 ASSERT_EQ (wrapped_string_lit,
28334 build_non_dependent_expr (wrapped_string_lit));
28335 }
28336
28337 /* Verify that type_dependent_expression_p () works correctly, even
28338 in the presence of location wrapper nodes. */
28339
28340 static void
28341 test_type_dependent_expression_p ()
28342 {
28343 location_t loc = BUILTINS_LOCATION;
28344
28345 tree name = get_identifier ("foo");
28346
28347 /* If no templates are involved, nothing is type-dependent. */
28348 gcc_assert (!processing_template_decl);
28349 ASSERT_FALSE (type_dependent_expression_p (name));
28350
28351 ++processing_template_decl;
28352
28353 /* Within a template, an unresolved name is always type-dependent. */
28354 ASSERT_TRUE (type_dependent_expression_p (name));
28355
28356 /* Ensure it copes with NULL_TREE and errors. */
28357 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28358 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28359
28360 /* A USING_DECL in a template should be type-dependent, even if wrapped
28361 with a location wrapper (PR c++/83799). */
28362 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28363 TREE_TYPE (using_decl) = integer_type_node;
28364 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28365 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28366 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28367 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28368
28369 --processing_template_decl;
28370 }
28371
28372 /* Run all of the selftests within this file. */
28373
28374 void
28375 cp_pt_c_tests ()
28376 {
28377 test_build_non_dependent_expr ();
28378 test_type_dependent_expression_p ();
28379 }
28380
28381 } // namespace selftest
28382
28383 #endif /* #if CHECKING_P */
28384
28385 #include "gt-cp-pt.h"