]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
[C++ PATCH] Kill DECL_SAVED_FUNCTION_DATA
[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 decl = pushdecl (decl);
4437 if (!is_non_type)
4438 parm = decl;
4439
4440 /* Build the parameter node linking the parameter declaration,
4441 its default argument (if any), and its constraints (if any). */
4442 parm = build_tree_list (defval, parm);
4443 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4444
4445 return chainon (list, parm);
4446 }
4447
4448 /* The end of a template parameter list has been reached. Process the
4449 tree list into a parameter vector, converting each parameter into a more
4450 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4451 as PARM_DECLs. */
4452
4453 tree
4454 end_template_parm_list (tree parms)
4455 {
4456 int nparms;
4457 tree parm, next;
4458 tree saved_parmlist = make_tree_vec (list_length (parms));
4459
4460 /* Pop the dummy parameter level and add the real one. */
4461 current_template_parms = TREE_CHAIN (current_template_parms);
4462
4463 current_template_parms
4464 = tree_cons (size_int (processing_template_decl),
4465 saved_parmlist, current_template_parms);
4466
4467 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4468 {
4469 next = TREE_CHAIN (parm);
4470 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4471 TREE_CHAIN (parm) = NULL_TREE;
4472 }
4473
4474 --processing_template_parmlist;
4475
4476 return saved_parmlist;
4477 }
4478
4479 // Explicitly indicate the end of the template parameter list. We assume
4480 // that the current template parameters have been constructed and/or
4481 // managed explicitly, as when creating new template template parameters
4482 // from a shorthand constraint.
4483 void
4484 end_template_parm_list ()
4485 {
4486 --processing_template_parmlist;
4487 }
4488
4489 /* end_template_decl is called after a template declaration is seen. */
4490
4491 void
4492 end_template_decl (void)
4493 {
4494 reset_specialization ();
4495
4496 if (! processing_template_decl)
4497 return;
4498
4499 /* This matches the pushlevel in begin_template_parm_list. */
4500 finish_scope ();
4501
4502 --processing_template_decl;
4503 current_template_parms = TREE_CHAIN (current_template_parms);
4504 }
4505
4506 /* Takes a TREE_LIST representing a template parameter and convert it
4507 into an argument suitable to be passed to the type substitution
4508 functions. Note that If the TREE_LIST contains an error_mark
4509 node, the returned argument is error_mark_node. */
4510
4511 tree
4512 template_parm_to_arg (tree t)
4513 {
4514
4515 if (t == NULL_TREE
4516 || TREE_CODE (t) != TREE_LIST)
4517 return t;
4518
4519 if (error_operand_p (TREE_VALUE (t)))
4520 return error_mark_node;
4521
4522 t = TREE_VALUE (t);
4523
4524 if (TREE_CODE (t) == TYPE_DECL
4525 || TREE_CODE (t) == TEMPLATE_DECL)
4526 {
4527 t = TREE_TYPE (t);
4528
4529 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4530 {
4531 /* Turn this argument into a TYPE_ARGUMENT_PACK
4532 with a single element, which expands T. */
4533 tree vec = make_tree_vec (1);
4534 if (CHECKING_P)
4535 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4536
4537 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4538
4539 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4540 SET_ARGUMENT_PACK_ARGS (t, vec);
4541 }
4542 }
4543 else
4544 {
4545 t = DECL_INITIAL (t);
4546
4547 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4548 {
4549 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4550 with a single element, which expands T. */
4551 tree vec = make_tree_vec (1);
4552 if (CHECKING_P)
4553 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4554
4555 t = convert_from_reference (t);
4556 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4557
4558 t = make_node (NONTYPE_ARGUMENT_PACK);
4559 SET_ARGUMENT_PACK_ARGS (t, vec);
4560 }
4561 else
4562 t = convert_from_reference (t);
4563 }
4564 return t;
4565 }
4566
4567 /* Given a single level of template parameters (a TREE_VEC), return it
4568 as a set of template arguments. */
4569
4570 static tree
4571 template_parms_level_to_args (tree parms)
4572 {
4573 tree a = copy_node (parms);
4574 TREE_TYPE (a) = NULL_TREE;
4575 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4576 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4577
4578 if (CHECKING_P)
4579 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4580
4581 return a;
4582 }
4583
4584 /* Given a set of template parameters, return them as a set of template
4585 arguments. The template parameters are represented as a TREE_VEC, in
4586 the form documented in cp-tree.h for template arguments. */
4587
4588 static tree
4589 template_parms_to_args (tree parms)
4590 {
4591 tree header;
4592 tree args = NULL_TREE;
4593 int length = TMPL_PARMS_DEPTH (parms);
4594 int l = length;
4595
4596 /* If there is only one level of template parameters, we do not
4597 create a TREE_VEC of TREE_VECs. Instead, we return a single
4598 TREE_VEC containing the arguments. */
4599 if (length > 1)
4600 args = make_tree_vec (length);
4601
4602 for (header = parms; header; header = TREE_CHAIN (header))
4603 {
4604 tree a = template_parms_level_to_args (TREE_VALUE (header));
4605
4606 if (length > 1)
4607 TREE_VEC_ELT (args, --l) = a;
4608 else
4609 args = a;
4610 }
4611
4612 return args;
4613 }
4614
4615 /* Within the declaration of a template, return the currently active
4616 template parameters as an argument TREE_VEC. */
4617
4618 static tree
4619 current_template_args (void)
4620 {
4621 return template_parms_to_args (current_template_parms);
4622 }
4623
4624 /* Update the declared TYPE by doing any lookups which were thought to be
4625 dependent, but are not now that we know the SCOPE of the declarator. */
4626
4627 tree
4628 maybe_update_decl_type (tree orig_type, tree scope)
4629 {
4630 tree type = orig_type;
4631
4632 if (type == NULL_TREE)
4633 return type;
4634
4635 if (TREE_CODE (orig_type) == TYPE_DECL)
4636 type = TREE_TYPE (type);
4637
4638 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4639 && dependent_type_p (type)
4640 /* Don't bother building up the args in this case. */
4641 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4642 {
4643 /* tsubst in the args corresponding to the template parameters,
4644 including auto if present. Most things will be unchanged, but
4645 make_typename_type and tsubst_qualified_id will resolve
4646 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4647 tree args = current_template_args ();
4648 tree auto_node = type_uses_auto (type);
4649 tree pushed;
4650 if (auto_node)
4651 {
4652 tree auto_vec = make_tree_vec (1);
4653 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4654 args = add_to_template_args (args, auto_vec);
4655 }
4656 pushed = push_scope (scope);
4657 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4658 if (pushed)
4659 pop_scope (scope);
4660 }
4661
4662 if (type == error_mark_node)
4663 return orig_type;
4664
4665 if (TREE_CODE (orig_type) == TYPE_DECL)
4666 {
4667 if (same_type_p (type, TREE_TYPE (orig_type)))
4668 type = orig_type;
4669 else
4670 type = TYPE_NAME (type);
4671 }
4672 return type;
4673 }
4674
4675 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4676 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4677 the new template is a member template. */
4678
4679 static tree
4680 build_template_decl (tree decl, tree parms, bool member_template_p)
4681 {
4682 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4683 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4684 DECL_TEMPLATE_PARMS (tmpl) = parms;
4685 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4686 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4687 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4688
4689 return tmpl;
4690 }
4691
4692 struct template_parm_data
4693 {
4694 /* The level of the template parameters we are currently
4695 processing. */
4696 int level;
4697
4698 /* The index of the specialization argument we are currently
4699 processing. */
4700 int current_arg;
4701
4702 /* An array whose size is the number of template parameters. The
4703 elements are nonzero if the parameter has been used in any one
4704 of the arguments processed so far. */
4705 int* parms;
4706
4707 /* An array whose size is the number of template arguments. The
4708 elements are nonzero if the argument makes use of template
4709 parameters of this level. */
4710 int* arg_uses_template_parms;
4711 };
4712
4713 /* Subroutine of push_template_decl used to see if each template
4714 parameter in a partial specialization is used in the explicit
4715 argument list. If T is of the LEVEL given in DATA (which is
4716 treated as a template_parm_data*), then DATA->PARMS is marked
4717 appropriately. */
4718
4719 static int
4720 mark_template_parm (tree t, void* data)
4721 {
4722 int level;
4723 int idx;
4724 struct template_parm_data* tpd = (struct template_parm_data*) data;
4725
4726 template_parm_level_and_index (t, &level, &idx);
4727
4728 if (level == tpd->level)
4729 {
4730 tpd->parms[idx] = 1;
4731 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4732 }
4733
4734 /* In C++17 the type of a non-type argument is a deduced context. */
4735 if (cxx_dialect >= cxx17
4736 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4737 for_each_template_parm (TREE_TYPE (t),
4738 &mark_template_parm,
4739 data,
4740 NULL,
4741 /*include_nondeduced_p=*/false);
4742
4743 /* Return zero so that for_each_template_parm will continue the
4744 traversal of the tree; we want to mark *every* template parm. */
4745 return 0;
4746 }
4747
4748 /* Process the partial specialization DECL. */
4749
4750 static tree
4751 process_partial_specialization (tree decl)
4752 {
4753 tree type = TREE_TYPE (decl);
4754 tree tinfo = get_template_info (decl);
4755 tree maintmpl = TI_TEMPLATE (tinfo);
4756 tree specargs = TI_ARGS (tinfo);
4757 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4758 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4759 tree inner_parms;
4760 tree inst;
4761 int nargs = TREE_VEC_LENGTH (inner_args);
4762 int ntparms;
4763 int i;
4764 bool did_error_intro = false;
4765 struct template_parm_data tpd;
4766 struct template_parm_data tpd2;
4767
4768 gcc_assert (current_template_parms);
4769
4770 /* A concept cannot be specialized. */
4771 if (flag_concepts && variable_concept_p (maintmpl))
4772 {
4773 error ("specialization of variable concept %q#D", maintmpl);
4774 return error_mark_node;
4775 }
4776
4777 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4778 ntparms = TREE_VEC_LENGTH (inner_parms);
4779
4780 /* We check that each of the template parameters given in the
4781 partial specialization is used in the argument list to the
4782 specialization. For example:
4783
4784 template <class T> struct S;
4785 template <class T> struct S<T*>;
4786
4787 The second declaration is OK because `T*' uses the template
4788 parameter T, whereas
4789
4790 template <class T> struct S<int>;
4791
4792 is no good. Even trickier is:
4793
4794 template <class T>
4795 struct S1
4796 {
4797 template <class U>
4798 struct S2;
4799 template <class U>
4800 struct S2<T>;
4801 };
4802
4803 The S2<T> declaration is actually invalid; it is a
4804 full-specialization. Of course,
4805
4806 template <class U>
4807 struct S2<T (*)(U)>;
4808
4809 or some such would have been OK. */
4810 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4811 tpd.parms = XALLOCAVEC (int, ntparms);
4812 memset (tpd.parms, 0, sizeof (int) * ntparms);
4813
4814 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4815 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4816 for (i = 0; i < nargs; ++i)
4817 {
4818 tpd.current_arg = i;
4819 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4820 &mark_template_parm,
4821 &tpd,
4822 NULL,
4823 /*include_nondeduced_p=*/false);
4824 }
4825 for (i = 0; i < ntparms; ++i)
4826 if (tpd.parms[i] == 0)
4827 {
4828 /* One of the template parms was not used in a deduced context in the
4829 specialization. */
4830 if (!did_error_intro)
4831 {
4832 error ("template parameters not deducible in "
4833 "partial specialization:");
4834 did_error_intro = true;
4835 }
4836
4837 inform (input_location, " %qD",
4838 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4839 }
4840
4841 if (did_error_intro)
4842 return error_mark_node;
4843
4844 /* [temp.class.spec]
4845
4846 The argument list of the specialization shall not be identical to
4847 the implicit argument list of the primary template. */
4848 tree main_args
4849 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4850 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4851 && (!flag_concepts
4852 || !strictly_subsumes (current_template_constraints (),
4853 get_constraints (maintmpl))))
4854 {
4855 if (!flag_concepts)
4856 error ("partial specialization %q+D does not specialize "
4857 "any template arguments; to define the primary template, "
4858 "remove the template argument list", decl);
4859 else
4860 error ("partial specialization %q+D does not specialize any "
4861 "template arguments and is not more constrained than "
4862 "the primary template; to define the primary template, "
4863 "remove the template argument list", decl);
4864 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4865 }
4866
4867 /* A partial specialization that replaces multiple parameters of the
4868 primary template with a pack expansion is less specialized for those
4869 parameters. */
4870 if (nargs < DECL_NTPARMS (maintmpl))
4871 {
4872 error ("partial specialization is not more specialized than the "
4873 "primary template because it replaces multiple parameters "
4874 "with a pack expansion");
4875 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4876 /* Avoid crash in process_partial_specialization. */
4877 return decl;
4878 }
4879
4880 /* If we aren't in a dependent class, we can actually try deduction. */
4881 else if (tpd.level == 1
4882 /* FIXME we should be able to handle a partial specialization of a
4883 partial instantiation, but currently we can't (c++/41727). */
4884 && TMPL_ARGS_DEPTH (specargs) == 1
4885 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4886 {
4887 auto_diagnostic_group d;
4888 if (permerror (input_location, "partial specialization %qD is not "
4889 "more specialized than", decl))
4890 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4891 maintmpl);
4892 }
4893
4894 /* [temp.class.spec]
4895
4896 A partially specialized non-type argument expression shall not
4897 involve template parameters of the partial specialization except
4898 when the argument expression is a simple identifier.
4899
4900 The type of a template parameter corresponding to a specialized
4901 non-type argument shall not be dependent on a parameter of the
4902 specialization.
4903
4904 Also, we verify that pack expansions only occur at the
4905 end of the argument list. */
4906 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4907 tpd2.parms = 0;
4908 for (i = 0; i < nargs; ++i)
4909 {
4910 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4911 tree arg = TREE_VEC_ELT (inner_args, i);
4912 tree packed_args = NULL_TREE;
4913 int j, len = 1;
4914
4915 if (ARGUMENT_PACK_P (arg))
4916 {
4917 /* Extract the arguments from the argument pack. We'll be
4918 iterating over these in the following loop. */
4919 packed_args = ARGUMENT_PACK_ARGS (arg);
4920 len = TREE_VEC_LENGTH (packed_args);
4921 }
4922
4923 for (j = 0; j < len; j++)
4924 {
4925 if (packed_args)
4926 /* Get the Jth argument in the parameter pack. */
4927 arg = TREE_VEC_ELT (packed_args, j);
4928
4929 if (PACK_EXPANSION_P (arg))
4930 {
4931 /* Pack expansions must come at the end of the
4932 argument list. */
4933 if ((packed_args && j < len - 1)
4934 || (!packed_args && i < nargs - 1))
4935 {
4936 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4937 error ("parameter pack argument %qE must be at the "
4938 "end of the template argument list", arg);
4939 else
4940 error ("parameter pack argument %qT must be at the "
4941 "end of the template argument list", arg);
4942 }
4943 }
4944
4945 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4946 /* We only care about the pattern. */
4947 arg = PACK_EXPANSION_PATTERN (arg);
4948
4949 if (/* These first two lines are the `non-type' bit. */
4950 !TYPE_P (arg)
4951 && TREE_CODE (arg) != TEMPLATE_DECL
4952 /* This next two lines are the `argument expression is not just a
4953 simple identifier' condition and also the `specialized
4954 non-type argument' bit. */
4955 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4956 && !(REFERENCE_REF_P (arg)
4957 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4958 {
4959 if ((!packed_args && tpd.arg_uses_template_parms[i])
4960 || (packed_args && uses_template_parms (arg)))
4961 error ("template argument %qE involves template parameter(s)",
4962 arg);
4963 else
4964 {
4965 /* Look at the corresponding template parameter,
4966 marking which template parameters its type depends
4967 upon. */
4968 tree type = TREE_TYPE (parm);
4969
4970 if (!tpd2.parms)
4971 {
4972 /* We haven't yet initialized TPD2. Do so now. */
4973 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4974 /* The number of parameters here is the number in the
4975 main template, which, as checked in the assertion
4976 above, is NARGS. */
4977 tpd2.parms = XALLOCAVEC (int, nargs);
4978 tpd2.level =
4979 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4980 }
4981
4982 /* Mark the template parameters. But this time, we're
4983 looking for the template parameters of the main
4984 template, not in the specialization. */
4985 tpd2.current_arg = i;
4986 tpd2.arg_uses_template_parms[i] = 0;
4987 memset (tpd2.parms, 0, sizeof (int) * nargs);
4988 for_each_template_parm (type,
4989 &mark_template_parm,
4990 &tpd2,
4991 NULL,
4992 /*include_nondeduced_p=*/false);
4993
4994 if (tpd2.arg_uses_template_parms [i])
4995 {
4996 /* The type depended on some template parameters.
4997 If they are fully specialized in the
4998 specialization, that's OK. */
4999 int j;
5000 int count = 0;
5001 for (j = 0; j < nargs; ++j)
5002 if (tpd2.parms[j] != 0
5003 && tpd.arg_uses_template_parms [j])
5004 ++count;
5005 if (count != 0)
5006 error_n (input_location, count,
5007 "type %qT of template argument %qE depends "
5008 "on a template parameter",
5009 "type %qT of template argument %qE depends "
5010 "on template parameters",
5011 type,
5012 arg);
5013 }
5014 }
5015 }
5016 }
5017 }
5018
5019 /* We should only get here once. */
5020 if (TREE_CODE (decl) == TYPE_DECL)
5021 gcc_assert (!COMPLETE_TYPE_P (type));
5022
5023 // Build the template decl.
5024 tree tmpl = build_template_decl (decl, current_template_parms,
5025 DECL_MEMBER_TEMPLATE_P (maintmpl));
5026 TREE_TYPE (tmpl) = type;
5027 DECL_TEMPLATE_RESULT (tmpl) = decl;
5028 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5029 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5030 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5031
5032 /* Give template template parms a DECL_CONTEXT of the template
5033 for which they are a parameter. */
5034 for (i = 0; i < ntparms; ++i)
5035 {
5036 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5037 if (TREE_CODE (parm) == TEMPLATE_DECL)
5038 DECL_CONTEXT (parm) = tmpl;
5039 }
5040
5041 if (VAR_P (decl))
5042 /* We didn't register this in check_explicit_specialization so we could
5043 wait until the constraints were set. */
5044 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5045 else
5046 associate_classtype_constraints (type);
5047
5048 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5049 = tree_cons (specargs, tmpl,
5050 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5051 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5052
5053 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5054 inst = TREE_CHAIN (inst))
5055 {
5056 tree instance = TREE_VALUE (inst);
5057 if (TYPE_P (instance)
5058 ? (COMPLETE_TYPE_P (instance)
5059 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5060 : DECL_TEMPLATE_INSTANTIATION (instance))
5061 {
5062 tree spec = most_specialized_partial_spec (instance, tf_none);
5063 tree inst_decl = (DECL_P (instance)
5064 ? instance : TYPE_NAME (instance));
5065 if (!spec)
5066 /* OK */;
5067 else if (spec == error_mark_node)
5068 permerror (input_location,
5069 "declaration of %qD ambiguates earlier template "
5070 "instantiation for %qD", decl, inst_decl);
5071 else if (TREE_VALUE (spec) == tmpl)
5072 permerror (input_location,
5073 "partial specialization of %qD after instantiation "
5074 "of %qD", decl, inst_decl);
5075 }
5076 }
5077
5078 return decl;
5079 }
5080
5081 /* PARM is a template parameter of some form; return the corresponding
5082 TEMPLATE_PARM_INDEX. */
5083
5084 static tree
5085 get_template_parm_index (tree parm)
5086 {
5087 if (TREE_CODE (parm) == PARM_DECL
5088 || TREE_CODE (parm) == CONST_DECL)
5089 parm = DECL_INITIAL (parm);
5090 else if (TREE_CODE (parm) == TYPE_DECL
5091 || TREE_CODE (parm) == TEMPLATE_DECL)
5092 parm = TREE_TYPE (parm);
5093 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5094 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5095 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5096 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5097 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5098 return parm;
5099 }
5100
5101 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5102 parameter packs used by the template parameter PARM. */
5103
5104 static void
5105 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5106 {
5107 /* A type parm can't refer to another parm. */
5108 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5109 return;
5110 else if (TREE_CODE (parm) == PARM_DECL)
5111 {
5112 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5113 ppd, ppd->visited);
5114 return;
5115 }
5116
5117 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5118
5119 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5120 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5121 {
5122 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5123 if (template_parameter_pack_p (p))
5124 /* Any packs in the type are expanded by this parameter. */;
5125 else
5126 fixed_parameter_pack_p_1 (p, ppd);
5127 }
5128 }
5129
5130 /* PARM is a template parameter pack. Return any parameter packs used in
5131 its type or the type of any of its template parameters. If there are
5132 any such packs, it will be instantiated into a fixed template parameter
5133 list by partial instantiation rather than be fully deduced. */
5134
5135 tree
5136 fixed_parameter_pack_p (tree parm)
5137 {
5138 /* This can only be true in a member template. */
5139 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5140 return NULL_TREE;
5141 /* This can only be true for a parameter pack. */
5142 if (!template_parameter_pack_p (parm))
5143 return NULL_TREE;
5144 /* A type parm can't refer to another parm. */
5145 if (TREE_CODE (parm) == TYPE_DECL)
5146 return NULL_TREE;
5147
5148 tree parameter_packs = NULL_TREE;
5149 struct find_parameter_pack_data ppd;
5150 ppd.parameter_packs = &parameter_packs;
5151 ppd.visited = new hash_set<tree>;
5152 ppd.type_pack_expansion_p = false;
5153
5154 fixed_parameter_pack_p_1 (parm, &ppd);
5155
5156 delete ppd.visited;
5157 return parameter_packs;
5158 }
5159
5160 /* Check that a template declaration's use of default arguments and
5161 parameter packs is not invalid. Here, PARMS are the template
5162 parameters. IS_PRIMARY is true if DECL is the thing declared by
5163 a primary template. IS_PARTIAL is true if DECL is a partial
5164 specialization.
5165
5166 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5167 function template declaration or a friend class template
5168 declaration. In the function case, 1 indicates a declaration, 2
5169 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5170 emitted for extraneous default arguments.
5171
5172 Returns TRUE if there were no errors found, FALSE otherwise. */
5173
5174 bool
5175 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5176 bool is_partial, int is_friend_decl)
5177 {
5178 const char *msg;
5179 int last_level_to_check;
5180 tree parm_level;
5181 bool no_errors = true;
5182
5183 /* [temp.param]
5184
5185 A default template-argument shall not be specified in a
5186 function template declaration or a function template definition, nor
5187 in the template-parameter-list of the definition of a member of a
5188 class template. */
5189
5190 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5191 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5192 /* You can't have a function template declaration in a local
5193 scope, nor you can you define a member of a class template in a
5194 local scope. */
5195 return true;
5196
5197 if ((TREE_CODE (decl) == TYPE_DECL
5198 && TREE_TYPE (decl)
5199 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5200 || (TREE_CODE (decl) == FUNCTION_DECL
5201 && LAMBDA_FUNCTION_P (decl)))
5202 /* A lambda doesn't have an explicit declaration; don't complain
5203 about the parms of the enclosing class. */
5204 return true;
5205
5206 if (current_class_type
5207 && !TYPE_BEING_DEFINED (current_class_type)
5208 && DECL_LANG_SPECIFIC (decl)
5209 && DECL_DECLARES_FUNCTION_P (decl)
5210 /* If this is either a friend defined in the scope of the class
5211 or a member function. */
5212 && (DECL_FUNCTION_MEMBER_P (decl)
5213 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5214 : DECL_FRIEND_CONTEXT (decl)
5215 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5216 : false)
5217 /* And, if it was a member function, it really was defined in
5218 the scope of the class. */
5219 && (!DECL_FUNCTION_MEMBER_P (decl)
5220 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5221 /* We already checked these parameters when the template was
5222 declared, so there's no need to do it again now. This function
5223 was defined in class scope, but we're processing its body now
5224 that the class is complete. */
5225 return true;
5226
5227 /* Core issue 226 (C++0x only): the following only applies to class
5228 templates. */
5229 if (is_primary
5230 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5231 {
5232 /* [temp.param]
5233
5234 If a template-parameter has a default template-argument, all
5235 subsequent template-parameters shall have a default
5236 template-argument supplied. */
5237 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5238 {
5239 tree inner_parms = TREE_VALUE (parm_level);
5240 int ntparms = TREE_VEC_LENGTH (inner_parms);
5241 int seen_def_arg_p = 0;
5242 int i;
5243
5244 for (i = 0; i < ntparms; ++i)
5245 {
5246 tree parm = TREE_VEC_ELT (inner_parms, i);
5247
5248 if (parm == error_mark_node)
5249 continue;
5250
5251 if (TREE_PURPOSE (parm))
5252 seen_def_arg_p = 1;
5253 else if (seen_def_arg_p
5254 && !template_parameter_pack_p (TREE_VALUE (parm)))
5255 {
5256 error ("no default argument for %qD", TREE_VALUE (parm));
5257 /* For better subsequent error-recovery, we indicate that
5258 there should have been a default argument. */
5259 TREE_PURPOSE (parm) = error_mark_node;
5260 no_errors = false;
5261 }
5262 else if (!is_partial
5263 && !is_friend_decl
5264 /* Don't complain about an enclosing partial
5265 specialization. */
5266 && parm_level == parms
5267 && TREE_CODE (decl) == TYPE_DECL
5268 && i < ntparms - 1
5269 && template_parameter_pack_p (TREE_VALUE (parm))
5270 /* A fixed parameter pack will be partially
5271 instantiated into a fixed length list. */
5272 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5273 {
5274 /* A primary class template can only have one
5275 parameter pack, at the end of the template
5276 parameter list. */
5277
5278 error ("parameter pack %q+D must be at the end of the"
5279 " template parameter list", TREE_VALUE (parm));
5280
5281 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5282 = error_mark_node;
5283 no_errors = false;
5284 }
5285 }
5286 }
5287 }
5288
5289 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5290 || is_partial
5291 || !is_primary
5292 || is_friend_decl)
5293 /* For an ordinary class template, default template arguments are
5294 allowed at the innermost level, e.g.:
5295 template <class T = int>
5296 struct S {};
5297 but, in a partial specialization, they're not allowed even
5298 there, as we have in [temp.class.spec]:
5299
5300 The template parameter list of a specialization shall not
5301 contain default template argument values.
5302
5303 So, for a partial specialization, or for a function template
5304 (in C++98/C++03), we look at all of them. */
5305 ;
5306 else
5307 /* But, for a primary class template that is not a partial
5308 specialization we look at all template parameters except the
5309 innermost ones. */
5310 parms = TREE_CHAIN (parms);
5311
5312 /* Figure out what error message to issue. */
5313 if (is_friend_decl == 2)
5314 msg = G_("default template arguments may not be used in function template "
5315 "friend re-declaration");
5316 else if (is_friend_decl)
5317 msg = G_("default template arguments may not be used in template "
5318 "friend declarations");
5319 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5320 msg = G_("default template arguments may not be used in function templates "
5321 "without %<-std=c++11%> or %<-std=gnu++11%>");
5322 else if (is_partial)
5323 msg = G_("default template arguments may not be used in "
5324 "partial specializations");
5325 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5326 msg = G_("default argument for template parameter for class enclosing %qD");
5327 else
5328 /* Per [temp.param]/9, "A default template-argument shall not be
5329 specified in the template-parameter-lists of the definition of
5330 a member of a class template that appears outside of the member's
5331 class.", thus if we aren't handling a member of a class template
5332 there is no need to examine the parameters. */
5333 return true;
5334
5335 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5336 /* If we're inside a class definition, there's no need to
5337 examine the parameters to the class itself. On the one
5338 hand, they will be checked when the class is defined, and,
5339 on the other, default arguments are valid in things like:
5340 template <class T = double>
5341 struct S { template <class U> void f(U); };
5342 Here the default argument for `S' has no bearing on the
5343 declaration of `f'. */
5344 last_level_to_check = template_class_depth (current_class_type) + 1;
5345 else
5346 /* Check everything. */
5347 last_level_to_check = 0;
5348
5349 for (parm_level = parms;
5350 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5351 parm_level = TREE_CHAIN (parm_level))
5352 {
5353 tree inner_parms = TREE_VALUE (parm_level);
5354 int i;
5355 int ntparms;
5356
5357 ntparms = TREE_VEC_LENGTH (inner_parms);
5358 for (i = 0; i < ntparms; ++i)
5359 {
5360 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5361 continue;
5362
5363 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5364 {
5365 if (msg)
5366 {
5367 no_errors = false;
5368 if (is_friend_decl == 2)
5369 return no_errors;
5370
5371 error (msg, decl);
5372 msg = 0;
5373 }
5374
5375 /* Clear out the default argument so that we are not
5376 confused later. */
5377 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5378 }
5379 }
5380
5381 /* At this point, if we're still interested in issuing messages,
5382 they must apply to classes surrounding the object declared. */
5383 if (msg)
5384 msg = G_("default argument for template parameter for class "
5385 "enclosing %qD");
5386 }
5387
5388 return no_errors;
5389 }
5390
5391 /* Worker for push_template_decl_real, called via
5392 for_each_template_parm. DATA is really an int, indicating the
5393 level of the parameters we are interested in. If T is a template
5394 parameter of that level, return nonzero. */
5395
5396 static int
5397 template_parm_this_level_p (tree t, void* data)
5398 {
5399 int this_level = *(int *)data;
5400 int level;
5401
5402 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5403 level = TEMPLATE_PARM_LEVEL (t);
5404 else
5405 level = TEMPLATE_TYPE_LEVEL (t);
5406 return level == this_level;
5407 }
5408
5409 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5410 DATA is really an int, indicating the innermost outer level of parameters.
5411 If T is a template parameter of that level or further out, return
5412 nonzero. */
5413
5414 static int
5415 template_parm_outer_level (tree t, void *data)
5416 {
5417 int this_level = *(int *)data;
5418 int level;
5419
5420 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5421 level = TEMPLATE_PARM_LEVEL (t);
5422 else
5423 level = TEMPLATE_TYPE_LEVEL (t);
5424 return level <= this_level;
5425 }
5426
5427 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5428 parameters given by current_template_args, or reuses a
5429 previously existing one, if appropriate. Returns the DECL, or an
5430 equivalent one, if it is replaced via a call to duplicate_decls.
5431
5432 If IS_FRIEND is true, DECL is a friend declaration. */
5433
5434 tree
5435 push_template_decl_real (tree decl, bool is_friend)
5436 {
5437 tree tmpl;
5438 tree args;
5439 tree info;
5440 tree ctx;
5441 bool is_primary;
5442 bool is_partial;
5443 int new_template_p = 0;
5444 /* True if the template is a member template, in the sense of
5445 [temp.mem]. */
5446 bool member_template_p = false;
5447
5448 if (decl == error_mark_node || !current_template_parms)
5449 return error_mark_node;
5450
5451 /* See if this is a partial specialization. */
5452 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5453 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5454 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5455 || (VAR_P (decl)
5456 && DECL_LANG_SPECIFIC (decl)
5457 && DECL_TEMPLATE_SPECIALIZATION (decl)
5458 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5459
5460 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5461 is_friend = true;
5462
5463 if (is_friend)
5464 /* For a friend, we want the context of the friend, not
5465 the type of which it is a friend. */
5466 ctx = CP_DECL_CONTEXT (decl);
5467 else if (CP_DECL_CONTEXT (decl)
5468 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5469 /* In the case of a virtual function, we want the class in which
5470 it is defined. */
5471 ctx = CP_DECL_CONTEXT (decl);
5472 else
5473 /* Otherwise, if we're currently defining some class, the DECL
5474 is assumed to be a member of the class. */
5475 ctx = current_scope ();
5476
5477 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5478 ctx = NULL_TREE;
5479
5480 if (!DECL_CONTEXT (decl))
5481 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5482
5483 /* See if this is a primary template. */
5484 if (is_friend && ctx
5485 && uses_template_parms_level (ctx, processing_template_decl))
5486 /* A friend template that specifies a class context, i.e.
5487 template <typename T> friend void A<T>::f();
5488 is not primary. */
5489 is_primary = false;
5490 else if (TREE_CODE (decl) == TYPE_DECL
5491 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5492 is_primary = false;
5493 else
5494 is_primary = template_parm_scope_p ();
5495
5496 if (is_primary)
5497 {
5498 warning (OPT_Wtemplates, "template %qD declared", decl);
5499
5500 if (DECL_CLASS_SCOPE_P (decl))
5501 member_template_p = true;
5502 if (TREE_CODE (decl) == TYPE_DECL
5503 && anon_aggrname_p (DECL_NAME (decl)))
5504 {
5505 error ("template class without a name");
5506 return error_mark_node;
5507 }
5508 else if (TREE_CODE (decl) == FUNCTION_DECL)
5509 {
5510 if (member_template_p)
5511 {
5512 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5513 error ("member template %qD may not have virt-specifiers", decl);
5514 }
5515 if (DECL_DESTRUCTOR_P (decl))
5516 {
5517 /* [temp.mem]
5518
5519 A destructor shall not be a member template. */
5520 error ("destructor %qD declared as member template", decl);
5521 return error_mark_node;
5522 }
5523 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5524 && (!prototype_p (TREE_TYPE (decl))
5525 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5526 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5527 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5528 == void_list_node)))
5529 {
5530 /* [basic.stc.dynamic.allocation]
5531
5532 An allocation function can be a function
5533 template. ... Template allocation functions shall
5534 have two or more parameters. */
5535 error ("invalid template declaration of %qD", decl);
5536 return error_mark_node;
5537 }
5538 }
5539 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5540 && CLASS_TYPE_P (TREE_TYPE (decl)))
5541 {
5542 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5543 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5544 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5545 {
5546 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5547 if (TREE_CODE (t) == TYPE_DECL)
5548 t = TREE_TYPE (t);
5549 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5550 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5551 }
5552 }
5553 else if (TREE_CODE (decl) == TYPE_DECL
5554 && TYPE_DECL_ALIAS_P (decl))
5555 /* alias-declaration */
5556 gcc_assert (!DECL_ARTIFICIAL (decl));
5557 else if (VAR_P (decl))
5558 /* C++14 variable template. */;
5559 else
5560 {
5561 error ("template declaration of %q#D", decl);
5562 return error_mark_node;
5563 }
5564 }
5565
5566 /* Check to see that the rules regarding the use of default
5567 arguments are not being violated. We check args for a friend
5568 functions when we know whether it's a definition, introducing
5569 declaration or re-declaration. */
5570 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5571 check_default_tmpl_args (decl, current_template_parms,
5572 is_primary, is_partial, is_friend);
5573
5574 /* Ensure that there are no parameter packs in the type of this
5575 declaration that have not been expanded. */
5576 if (TREE_CODE (decl) == FUNCTION_DECL)
5577 {
5578 /* Check each of the arguments individually to see if there are
5579 any bare parameter packs. */
5580 tree type = TREE_TYPE (decl);
5581 tree arg = DECL_ARGUMENTS (decl);
5582 tree argtype = TYPE_ARG_TYPES (type);
5583
5584 while (arg && argtype)
5585 {
5586 if (!DECL_PACK_P (arg)
5587 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5588 {
5589 /* This is a PARM_DECL that contains unexpanded parameter
5590 packs. We have already complained about this in the
5591 check_for_bare_parameter_packs call, so just replace
5592 these types with ERROR_MARK_NODE. */
5593 TREE_TYPE (arg) = error_mark_node;
5594 TREE_VALUE (argtype) = error_mark_node;
5595 }
5596
5597 arg = DECL_CHAIN (arg);
5598 argtype = TREE_CHAIN (argtype);
5599 }
5600
5601 /* Check for bare parameter packs in the return type and the
5602 exception specifiers. */
5603 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5604 /* Errors were already issued, set return type to int
5605 as the frontend doesn't expect error_mark_node as
5606 the return type. */
5607 TREE_TYPE (type) = integer_type_node;
5608 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5609 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5610 }
5611 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5612 && TYPE_DECL_ALIAS_P (decl))
5613 ? DECL_ORIGINAL_TYPE (decl)
5614 : TREE_TYPE (decl)))
5615 {
5616 TREE_TYPE (decl) = error_mark_node;
5617 return error_mark_node;
5618 }
5619
5620 if (is_partial)
5621 return process_partial_specialization (decl);
5622
5623 args = current_template_args ();
5624
5625 if (!ctx
5626 || TREE_CODE (ctx) == FUNCTION_DECL
5627 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5628 || (TREE_CODE (decl) == TYPE_DECL
5629 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5630 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5631 {
5632 if (DECL_LANG_SPECIFIC (decl)
5633 && DECL_TEMPLATE_INFO (decl)
5634 && DECL_TI_TEMPLATE (decl))
5635 tmpl = DECL_TI_TEMPLATE (decl);
5636 /* If DECL is a TYPE_DECL for a class-template, then there won't
5637 be DECL_LANG_SPECIFIC. The information equivalent to
5638 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5639 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5640 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5641 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5642 {
5643 /* Since a template declaration already existed for this
5644 class-type, we must be redeclaring it here. Make sure
5645 that the redeclaration is valid. */
5646 redeclare_class_template (TREE_TYPE (decl),
5647 current_template_parms,
5648 current_template_constraints ());
5649 /* We don't need to create a new TEMPLATE_DECL; just use the
5650 one we already had. */
5651 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5652 }
5653 else
5654 {
5655 tmpl = build_template_decl (decl, current_template_parms,
5656 member_template_p);
5657 new_template_p = 1;
5658
5659 if (DECL_LANG_SPECIFIC (decl)
5660 && DECL_TEMPLATE_SPECIALIZATION (decl))
5661 {
5662 /* A specialization of a member template of a template
5663 class. */
5664 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5665 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5666 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5667 }
5668 }
5669 }
5670 else
5671 {
5672 tree a, t, current, parms;
5673 int i;
5674 tree tinfo = get_template_info (decl);
5675
5676 if (!tinfo)
5677 {
5678 error ("template definition of non-template %q#D", decl);
5679 return error_mark_node;
5680 }
5681
5682 tmpl = TI_TEMPLATE (tinfo);
5683
5684 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5685 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5686 && DECL_TEMPLATE_SPECIALIZATION (decl)
5687 && DECL_MEMBER_TEMPLATE_P (tmpl))
5688 {
5689 tree new_tmpl;
5690
5691 /* The declaration is a specialization of a member
5692 template, declared outside the class. Therefore, the
5693 innermost template arguments will be NULL, so we
5694 replace them with the arguments determined by the
5695 earlier call to check_explicit_specialization. */
5696 args = DECL_TI_ARGS (decl);
5697
5698 new_tmpl
5699 = build_template_decl (decl, current_template_parms,
5700 member_template_p);
5701 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5702 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5703 DECL_TI_TEMPLATE (decl) = new_tmpl;
5704 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5705 DECL_TEMPLATE_INFO (new_tmpl)
5706 = build_template_info (tmpl, args);
5707
5708 register_specialization (new_tmpl,
5709 most_general_template (tmpl),
5710 args,
5711 is_friend, 0);
5712 return decl;
5713 }
5714
5715 /* Make sure the template headers we got make sense. */
5716
5717 parms = DECL_TEMPLATE_PARMS (tmpl);
5718 i = TMPL_PARMS_DEPTH (parms);
5719 if (TMPL_ARGS_DEPTH (args) != i)
5720 {
5721 error ("expected %d levels of template parms for %q#D, got %d",
5722 i, decl, TMPL_ARGS_DEPTH (args));
5723 DECL_INTERFACE_KNOWN (decl) = 1;
5724 return error_mark_node;
5725 }
5726 else
5727 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5728 {
5729 a = TMPL_ARGS_LEVEL (args, i);
5730 t = INNERMOST_TEMPLATE_PARMS (parms);
5731
5732 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5733 {
5734 if (current == decl)
5735 error ("got %d template parameters for %q#D",
5736 TREE_VEC_LENGTH (a), decl);
5737 else
5738 error ("got %d template parameters for %q#T",
5739 TREE_VEC_LENGTH (a), current);
5740 error (" but %d required", TREE_VEC_LENGTH (t));
5741 /* Avoid crash in import_export_decl. */
5742 DECL_INTERFACE_KNOWN (decl) = 1;
5743 return error_mark_node;
5744 }
5745
5746 if (current == decl)
5747 current = ctx;
5748 else if (current == NULL_TREE)
5749 /* Can happen in erroneous input. */
5750 break;
5751 else
5752 current = get_containing_scope (current);
5753 }
5754
5755 /* Check that the parms are used in the appropriate qualifying scopes
5756 in the declarator. */
5757 if (!comp_template_args
5758 (TI_ARGS (tinfo),
5759 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5760 {
5761 error ("template arguments to %qD do not match original "
5762 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5763 if (!uses_template_parms (TI_ARGS (tinfo)))
5764 inform (input_location, "use %<template<>%> for"
5765 " an explicit specialization");
5766 /* Avoid crash in import_export_decl. */
5767 DECL_INTERFACE_KNOWN (decl) = 1;
5768 return error_mark_node;
5769 }
5770 }
5771
5772 DECL_TEMPLATE_RESULT (tmpl) = decl;
5773 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5774
5775 /* Push template declarations for global functions and types. Note
5776 that we do not try to push a global template friend declared in a
5777 template class; such a thing may well depend on the template
5778 parameters of the class. */
5779 if (new_template_p && !ctx
5780 && !(is_friend && template_class_depth (current_class_type) > 0))
5781 {
5782 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5783 if (tmpl == error_mark_node)
5784 return error_mark_node;
5785
5786 /* Hide template friend classes that haven't been declared yet. */
5787 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5788 {
5789 DECL_ANTICIPATED (tmpl) = 1;
5790 DECL_FRIEND_P (tmpl) = 1;
5791 }
5792 }
5793
5794 if (is_primary)
5795 {
5796 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5797
5798 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5799
5800 /* Give template template parms a DECL_CONTEXT of the template
5801 for which they are a parameter. */
5802 parms = INNERMOST_TEMPLATE_PARMS (parms);
5803 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5804 {
5805 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5806 if (TREE_CODE (parm) == TEMPLATE_DECL)
5807 DECL_CONTEXT (parm) = tmpl;
5808 }
5809
5810 if (TREE_CODE (decl) == TYPE_DECL
5811 && TYPE_DECL_ALIAS_P (decl)
5812 && complex_alias_template_p (tmpl))
5813 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5814 }
5815
5816 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5817 back to its most general template. If TMPL is a specialization,
5818 ARGS may only have the innermost set of arguments. Add the missing
5819 argument levels if necessary. */
5820 if (DECL_TEMPLATE_INFO (tmpl))
5821 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5822
5823 info = build_template_info (tmpl, args);
5824
5825 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5826 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5827 else
5828 {
5829 if (is_primary)
5830 retrofit_lang_decl (decl);
5831 if (DECL_LANG_SPECIFIC (decl))
5832 DECL_TEMPLATE_INFO (decl) = info;
5833 }
5834
5835 if (flag_implicit_templates
5836 && !is_friend
5837 && TREE_PUBLIC (decl)
5838 && VAR_OR_FUNCTION_DECL_P (decl))
5839 /* Set DECL_COMDAT on template instantiations; if we force
5840 them to be emitted by explicit instantiation or -frepo,
5841 mark_needed will tell cgraph to do the right thing. */
5842 DECL_COMDAT (decl) = true;
5843
5844 return DECL_TEMPLATE_RESULT (tmpl);
5845 }
5846
5847 tree
5848 push_template_decl (tree decl)
5849 {
5850 return push_template_decl_real (decl, false);
5851 }
5852
5853 /* FN is an inheriting constructor that inherits from the constructor
5854 template INHERITED; turn FN into a constructor template with a matching
5855 template header. */
5856
5857 tree
5858 add_inherited_template_parms (tree fn, tree inherited)
5859 {
5860 tree inner_parms
5861 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5862 inner_parms = copy_node (inner_parms);
5863 tree parms
5864 = tree_cons (size_int (processing_template_decl + 1),
5865 inner_parms, current_template_parms);
5866 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5867 tree args = template_parms_to_args (parms);
5868 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5869 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5870 DECL_TEMPLATE_RESULT (tmpl) = fn;
5871 DECL_ARTIFICIAL (tmpl) = true;
5872 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5873 return tmpl;
5874 }
5875
5876 /* Called when a class template TYPE is redeclared with the indicated
5877 template PARMS, e.g.:
5878
5879 template <class T> struct S;
5880 template <class T> struct S {}; */
5881
5882 bool
5883 redeclare_class_template (tree type, tree parms, tree cons)
5884 {
5885 tree tmpl;
5886 tree tmpl_parms;
5887 int i;
5888
5889 if (!TYPE_TEMPLATE_INFO (type))
5890 {
5891 error ("%qT is not a template type", type);
5892 return false;
5893 }
5894
5895 tmpl = TYPE_TI_TEMPLATE (type);
5896 if (!PRIMARY_TEMPLATE_P (tmpl))
5897 /* The type is nested in some template class. Nothing to worry
5898 about here; there are no new template parameters for the nested
5899 type. */
5900 return true;
5901
5902 if (!parms)
5903 {
5904 error ("template specifiers not specified in declaration of %qD",
5905 tmpl);
5906 return false;
5907 }
5908
5909 parms = INNERMOST_TEMPLATE_PARMS (parms);
5910 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5911
5912 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5913 {
5914 error_n (input_location, TREE_VEC_LENGTH (parms),
5915 "redeclared with %d template parameter",
5916 "redeclared with %d template parameters",
5917 TREE_VEC_LENGTH (parms));
5918 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5919 "previous declaration %qD used %d template parameter",
5920 "previous declaration %qD used %d template parameters",
5921 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5922 return false;
5923 }
5924
5925 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5926 {
5927 tree tmpl_parm;
5928 tree parm;
5929 tree tmpl_default;
5930 tree parm_default;
5931
5932 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5933 || TREE_VEC_ELT (parms, i) == error_mark_node)
5934 continue;
5935
5936 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5937 if (error_operand_p (tmpl_parm))
5938 return false;
5939
5940 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5941 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5942 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5943
5944 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5945 TEMPLATE_DECL. */
5946 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5947 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5948 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5949 || (TREE_CODE (tmpl_parm) != PARM_DECL
5950 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5951 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5952 || (TREE_CODE (tmpl_parm) == PARM_DECL
5953 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5954 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5955 {
5956 error ("template parameter %q+#D", tmpl_parm);
5957 error ("redeclared here as %q#D", parm);
5958 return false;
5959 }
5960
5961 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5962 {
5963 /* We have in [temp.param]:
5964
5965 A template-parameter may not be given default arguments
5966 by two different declarations in the same scope. */
5967 error_at (input_location, "redefinition of default argument for %q#D", parm);
5968 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5969 "original definition appeared here");
5970 return false;
5971 }
5972
5973 if (parm_default != NULL_TREE)
5974 /* Update the previous template parameters (which are the ones
5975 that will really count) with the new default value. */
5976 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5977 else if (tmpl_default != NULL_TREE)
5978 /* Update the new parameters, too; they'll be used as the
5979 parameters for any members. */
5980 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5981
5982 /* Give each template template parm in this redeclaration a
5983 DECL_CONTEXT of the template for which they are a parameter. */
5984 if (TREE_CODE (parm) == TEMPLATE_DECL)
5985 {
5986 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5987 DECL_CONTEXT (parm) = tmpl;
5988 }
5989
5990 if (TREE_CODE (parm) == TYPE_DECL)
5991 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5992 }
5993
5994 // Cannot redeclare a class template with a different set of constraints.
5995 if (!equivalent_constraints (get_constraints (tmpl), cons))
5996 {
5997 error_at (input_location, "redeclaration %q#D with different "
5998 "constraints", tmpl);
5999 inform (DECL_SOURCE_LOCATION (tmpl),
6000 "original declaration appeared here");
6001 }
6002
6003 return true;
6004 }
6005
6006 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6007 to be used when the caller has already checked
6008 (processing_template_decl
6009 && !instantiation_dependent_expression_p (expr)
6010 && potential_constant_expression (expr))
6011 and cleared processing_template_decl. */
6012
6013 tree
6014 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6015 {
6016 return tsubst_copy_and_build (expr,
6017 /*args=*/NULL_TREE,
6018 complain,
6019 /*in_decl=*/NULL_TREE,
6020 /*function_p=*/false,
6021 /*integral_constant_expression_p=*/true);
6022 }
6023
6024 /* Simplify EXPR if it is a non-dependent expression. Returns the
6025 (possibly simplified) expression. */
6026
6027 tree
6028 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6029 {
6030 if (expr == NULL_TREE)
6031 return NULL_TREE;
6032
6033 /* If we're in a template, but EXPR isn't value dependent, simplify
6034 it. We're supposed to treat:
6035
6036 template <typename T> void f(T[1 + 1]);
6037 template <typename T> void f(T[2]);
6038
6039 as two declarations of the same function, for example. */
6040 if (processing_template_decl
6041 && is_nondependent_constant_expression (expr))
6042 {
6043 processing_template_decl_sentinel s;
6044 expr = instantiate_non_dependent_expr_internal (expr, complain);
6045 }
6046 return expr;
6047 }
6048
6049 tree
6050 instantiate_non_dependent_expr (tree expr)
6051 {
6052 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6053 }
6054
6055 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6056 an uninstantiated expression. */
6057
6058 tree
6059 instantiate_non_dependent_or_null (tree expr)
6060 {
6061 if (expr == NULL_TREE)
6062 return NULL_TREE;
6063 if (processing_template_decl)
6064 {
6065 if (!is_nondependent_constant_expression (expr))
6066 expr = NULL_TREE;
6067 else
6068 {
6069 processing_template_decl_sentinel s;
6070 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6071 }
6072 }
6073 return expr;
6074 }
6075
6076 /* True iff T is a specialization of a variable template. */
6077
6078 bool
6079 variable_template_specialization_p (tree t)
6080 {
6081 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6082 return false;
6083 tree tmpl = DECL_TI_TEMPLATE (t);
6084 return variable_template_p (tmpl);
6085 }
6086
6087 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6088 template declaration, or a TYPE_DECL for an alias declaration. */
6089
6090 bool
6091 alias_type_or_template_p (tree t)
6092 {
6093 if (t == NULL_TREE)
6094 return false;
6095 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6096 || (TYPE_P (t)
6097 && TYPE_NAME (t)
6098 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6099 || DECL_ALIAS_TEMPLATE_P (t));
6100 }
6101
6102 /* Return TRUE iff T is a specialization of an alias template. */
6103
6104 bool
6105 alias_template_specialization_p (const_tree t)
6106 {
6107 /* It's an alias template specialization if it's an alias and its
6108 TYPE_NAME is a specialization of a primary template. */
6109 if (TYPE_ALIAS_P (t))
6110 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6111 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6112
6113 return false;
6114 }
6115
6116 /* An alias template is complex from a SFINAE perspective if a template-id
6117 using that alias can be ill-formed when the expansion is not, as with
6118 the void_t template. We determine this by checking whether the
6119 expansion for the alias template uses all its template parameters. */
6120
6121 struct uses_all_template_parms_data
6122 {
6123 int level;
6124 bool *seen;
6125 };
6126
6127 static int
6128 uses_all_template_parms_r (tree t, void *data_)
6129 {
6130 struct uses_all_template_parms_data &data
6131 = *(struct uses_all_template_parms_data*)data_;
6132 tree idx = get_template_parm_index (t);
6133
6134 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6135 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6136 return 0;
6137 }
6138
6139 static bool
6140 complex_alias_template_p (const_tree tmpl)
6141 {
6142 struct uses_all_template_parms_data data;
6143 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6144 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6145 data.level = TMPL_PARMS_DEPTH (parms);
6146 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6147 data.seen = XALLOCAVEC (bool, len);
6148 for (int i = 0; i < len; ++i)
6149 data.seen[i] = false;
6150
6151 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6152 for (int i = 0; i < len; ++i)
6153 if (!data.seen[i])
6154 return true;
6155 return false;
6156 }
6157
6158 /* Return TRUE iff T is a specialization of a complex alias template with
6159 dependent template-arguments. */
6160
6161 bool
6162 dependent_alias_template_spec_p (const_tree t)
6163 {
6164 if (!alias_template_specialization_p (t))
6165 return false;
6166
6167 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6168 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6169 return false;
6170
6171 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6172 if (!any_dependent_template_arguments_p (args))
6173 return false;
6174
6175 return true;
6176 }
6177
6178 /* Return the number of innermost template parameters in TMPL. */
6179
6180 static int
6181 num_innermost_template_parms (tree tmpl)
6182 {
6183 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6184 return TREE_VEC_LENGTH (parms);
6185 }
6186
6187 /* Return either TMPL or another template that it is equivalent to under DR
6188 1286: An alias that just changes the name of a template is equivalent to
6189 the other template. */
6190
6191 static tree
6192 get_underlying_template (tree tmpl)
6193 {
6194 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6195 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6196 {
6197 /* Determine if the alias is equivalent to an underlying template. */
6198 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6199 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6200 if (!tinfo)
6201 break;
6202
6203 tree underlying = TI_TEMPLATE (tinfo);
6204 if (!PRIMARY_TEMPLATE_P (underlying)
6205 || (num_innermost_template_parms (tmpl)
6206 != num_innermost_template_parms (underlying)))
6207 break;
6208
6209 tree alias_args = INNERMOST_TEMPLATE_ARGS
6210 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6211 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6212 break;
6213
6214 /* Alias is equivalent. Strip it and repeat. */
6215 tmpl = underlying;
6216 }
6217
6218 return tmpl;
6219 }
6220
6221 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6222 must be a reference-to-function or a pointer-to-function type, as specified
6223 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6224 and check that the resulting function has external linkage. */
6225
6226 static tree
6227 convert_nontype_argument_function (tree type, tree expr,
6228 tsubst_flags_t complain)
6229 {
6230 tree fns = expr;
6231 tree fn, fn_no_ptr;
6232 linkage_kind linkage;
6233
6234 fn = instantiate_type (type, fns, tf_none);
6235 if (fn == error_mark_node)
6236 return error_mark_node;
6237
6238 if (value_dependent_expression_p (fn))
6239 goto accept;
6240
6241 fn_no_ptr = strip_fnptr_conv (fn);
6242 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6243 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6244 if (BASELINK_P (fn_no_ptr))
6245 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6246
6247 /* [temp.arg.nontype]/1
6248
6249 A template-argument for a non-type, non-template template-parameter
6250 shall be one of:
6251 [...]
6252 -- the address of an object or function with external [C++11: or
6253 internal] linkage. */
6254
6255 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6256 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6257 {
6258 if (complain & tf_error)
6259 {
6260 error ("%qE is not a valid template argument for type %qT",
6261 expr, type);
6262 if (TYPE_PTR_P (type))
6263 inform (input_location, "it must be the address of a function "
6264 "with external linkage");
6265 else
6266 inform (input_location, "it must be the name of a function with "
6267 "external linkage");
6268 }
6269 return NULL_TREE;
6270 }
6271
6272 linkage = decl_linkage (fn_no_ptr);
6273 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6274 {
6275 if (complain & tf_error)
6276 {
6277 if (cxx_dialect >= cxx11)
6278 error ("%qE is not a valid template argument for type %qT "
6279 "because %qD has no linkage",
6280 expr, type, fn_no_ptr);
6281 else
6282 error ("%qE is not a valid template argument for type %qT "
6283 "because %qD does not have external linkage",
6284 expr, type, fn_no_ptr);
6285 }
6286 return NULL_TREE;
6287 }
6288
6289 accept:
6290 if (TYPE_REF_P (type))
6291 {
6292 if (REFERENCE_REF_P (fn))
6293 fn = TREE_OPERAND (fn, 0);
6294 else
6295 fn = build_address (fn);
6296 }
6297 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6298 fn = build_nop (type, fn);
6299
6300 return fn;
6301 }
6302
6303 /* Subroutine of convert_nontype_argument.
6304 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6305 Emit an error otherwise. */
6306
6307 static bool
6308 check_valid_ptrmem_cst_expr (tree type, tree expr,
6309 tsubst_flags_t complain)
6310 {
6311 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6312 tree orig_expr = expr;
6313 STRIP_NOPS (expr);
6314 if (null_ptr_cst_p (expr))
6315 return true;
6316 if (TREE_CODE (expr) == PTRMEM_CST
6317 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6318 PTRMEM_CST_CLASS (expr)))
6319 return true;
6320 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6321 return true;
6322 if (processing_template_decl
6323 && TREE_CODE (expr) == ADDR_EXPR
6324 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6325 return true;
6326 if (complain & tf_error)
6327 {
6328 error_at (loc, "%qE is not a valid template argument for type %qT",
6329 orig_expr, type);
6330 if (TREE_CODE (expr) != PTRMEM_CST)
6331 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6332 else
6333 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6334 }
6335 return false;
6336 }
6337
6338 /* Returns TRUE iff the address of OP is value-dependent.
6339
6340 14.6.2.4 [temp.dep.temp]:
6341 A non-integral non-type template-argument is dependent if its type is
6342 dependent or it has either of the following forms
6343 qualified-id
6344 & qualified-id
6345 and contains a nested-name-specifier which specifies a class-name that
6346 names a dependent type.
6347
6348 We generalize this to just say that the address of a member of a
6349 dependent class is value-dependent; the above doesn't cover the
6350 address of a static data member named with an unqualified-id. */
6351
6352 static bool
6353 has_value_dependent_address (tree op)
6354 {
6355 /* We could use get_inner_reference here, but there's no need;
6356 this is only relevant for template non-type arguments, which
6357 can only be expressed as &id-expression. */
6358 if (DECL_P (op))
6359 {
6360 tree ctx = CP_DECL_CONTEXT (op);
6361 if (TYPE_P (ctx) && dependent_type_p (ctx))
6362 return true;
6363 }
6364
6365 return false;
6366 }
6367
6368 /* The next set of functions are used for providing helpful explanatory
6369 diagnostics for failed overload resolution. Their messages should be
6370 indented by two spaces for consistency with the messages in
6371 call.c */
6372
6373 static int
6374 unify_success (bool /*explain_p*/)
6375 {
6376 return 0;
6377 }
6378
6379 /* Other failure functions should call this one, to provide a single function
6380 for setting a breakpoint on. */
6381
6382 static int
6383 unify_invalid (bool /*explain_p*/)
6384 {
6385 return 1;
6386 }
6387
6388 static int
6389 unify_parameter_deduction_failure (bool explain_p, tree parm)
6390 {
6391 if (explain_p)
6392 inform (input_location,
6393 " couldn%'t deduce template parameter %qD", parm);
6394 return unify_invalid (explain_p);
6395 }
6396
6397 static int
6398 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6399 {
6400 if (explain_p)
6401 inform (input_location,
6402 " types %qT and %qT have incompatible cv-qualifiers",
6403 parm, arg);
6404 return unify_invalid (explain_p);
6405 }
6406
6407 static int
6408 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6409 {
6410 if (explain_p)
6411 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6412 return unify_invalid (explain_p);
6413 }
6414
6415 static int
6416 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6417 {
6418 if (explain_p)
6419 inform (input_location,
6420 " template parameter %qD is not a parameter pack, but "
6421 "argument %qD is",
6422 parm, arg);
6423 return unify_invalid (explain_p);
6424 }
6425
6426 static int
6427 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6428 {
6429 if (explain_p)
6430 inform (input_location,
6431 " template argument %qE does not match "
6432 "pointer-to-member constant %qE",
6433 arg, parm);
6434 return unify_invalid (explain_p);
6435 }
6436
6437 static int
6438 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6439 {
6440 if (explain_p)
6441 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6442 return unify_invalid (explain_p);
6443 }
6444
6445 static int
6446 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6447 {
6448 if (explain_p)
6449 inform (input_location,
6450 " inconsistent parameter pack deduction with %qT and %qT",
6451 old_arg, new_arg);
6452 return unify_invalid (explain_p);
6453 }
6454
6455 static int
6456 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6457 {
6458 if (explain_p)
6459 {
6460 if (TYPE_P (parm))
6461 inform (input_location,
6462 " deduced conflicting types for parameter %qT (%qT and %qT)",
6463 parm, first, second);
6464 else
6465 inform (input_location,
6466 " deduced conflicting values for non-type parameter "
6467 "%qE (%qE and %qE)", parm, first, second);
6468 }
6469 return unify_invalid (explain_p);
6470 }
6471
6472 static int
6473 unify_vla_arg (bool explain_p, tree arg)
6474 {
6475 if (explain_p)
6476 inform (input_location,
6477 " variable-sized array type %qT is not "
6478 "a valid template argument",
6479 arg);
6480 return unify_invalid (explain_p);
6481 }
6482
6483 static int
6484 unify_method_type_error (bool explain_p, tree arg)
6485 {
6486 if (explain_p)
6487 inform (input_location,
6488 " member function type %qT is not a valid template argument",
6489 arg);
6490 return unify_invalid (explain_p);
6491 }
6492
6493 static int
6494 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6495 {
6496 if (explain_p)
6497 {
6498 if (least_p)
6499 inform_n (input_location, wanted,
6500 " candidate expects at least %d argument, %d provided",
6501 " candidate expects at least %d arguments, %d provided",
6502 wanted, have);
6503 else
6504 inform_n (input_location, wanted,
6505 " candidate expects %d argument, %d provided",
6506 " candidate expects %d arguments, %d provided",
6507 wanted, have);
6508 }
6509 return unify_invalid (explain_p);
6510 }
6511
6512 static int
6513 unify_too_many_arguments (bool explain_p, int have, int wanted)
6514 {
6515 return unify_arity (explain_p, have, wanted);
6516 }
6517
6518 static int
6519 unify_too_few_arguments (bool explain_p, int have, int wanted,
6520 bool least_p = false)
6521 {
6522 return unify_arity (explain_p, have, wanted, least_p);
6523 }
6524
6525 static int
6526 unify_arg_conversion (bool explain_p, tree to_type,
6527 tree from_type, tree arg)
6528 {
6529 if (explain_p)
6530 inform (cp_expr_loc_or_loc (arg, input_location),
6531 " cannot convert %qE (type %qT) to type %qT",
6532 arg, from_type, to_type);
6533 return unify_invalid (explain_p);
6534 }
6535
6536 static int
6537 unify_no_common_base (bool explain_p, enum template_base_result r,
6538 tree parm, tree arg)
6539 {
6540 if (explain_p)
6541 switch (r)
6542 {
6543 case tbr_ambiguous_baseclass:
6544 inform (input_location, " %qT is an ambiguous base class of %qT",
6545 parm, arg);
6546 break;
6547 default:
6548 inform (input_location, " %qT is not derived from %qT", arg, parm);
6549 break;
6550 }
6551 return unify_invalid (explain_p);
6552 }
6553
6554 static int
6555 unify_inconsistent_template_template_parameters (bool explain_p)
6556 {
6557 if (explain_p)
6558 inform (input_location,
6559 " template parameters of a template template argument are "
6560 "inconsistent with other deduced template arguments");
6561 return unify_invalid (explain_p);
6562 }
6563
6564 static int
6565 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6566 {
6567 if (explain_p)
6568 inform (input_location,
6569 " can%'t deduce a template for %qT from non-template type %qT",
6570 parm, arg);
6571 return unify_invalid (explain_p);
6572 }
6573
6574 static int
6575 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6576 {
6577 if (explain_p)
6578 inform (input_location,
6579 " template argument %qE does not match %qE", arg, parm);
6580 return unify_invalid (explain_p);
6581 }
6582
6583 /* True if T is a C++20 template parameter object to store the argument for a
6584 template parameter of class type. */
6585
6586 bool
6587 template_parm_object_p (const_tree t)
6588 {
6589 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6590 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6591 }
6592
6593 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6594 argument for TYPE, points to an unsuitable object. */
6595
6596 static bool
6597 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6598 {
6599 switch (TREE_CODE (expr))
6600 {
6601 CASE_CONVERT:
6602 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6603 complain);
6604
6605 case TARGET_EXPR:
6606 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6607 complain);
6608
6609 case CONSTRUCTOR:
6610 {
6611 unsigned i; tree elt;
6612 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6613 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6614 return true;
6615 }
6616 break;
6617
6618 case ADDR_EXPR:
6619 {
6620 tree decl = TREE_OPERAND (expr, 0);
6621
6622 if (!VAR_P (decl))
6623 {
6624 if (complain & tf_error)
6625 error ("%qE is not a valid template argument of type %qT "
6626 "because %qE is not a variable", expr, type, decl);
6627 return true;
6628 }
6629 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6630 {
6631 if (complain & tf_error)
6632 error ("%qE is not a valid template argument of type %qT "
6633 "in C++98 because %qD does not have external linkage",
6634 expr, type, decl);
6635 return true;
6636 }
6637 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6638 && decl_linkage (decl) == lk_none)
6639 {
6640 if (complain & tf_error)
6641 error ("%qE is not a valid template argument of type %qT "
6642 "because %qD has no linkage", expr, type, decl);
6643 return true;
6644 }
6645 /* C++17: For a non-type template-parameter of reference or pointer
6646 type, the value of the constant expression shall not refer to (or
6647 for a pointer type, shall not be the address of):
6648 * a subobject (4.5),
6649 * a temporary object (15.2),
6650 * a string literal (5.13.5),
6651 * the result of a typeid expression (8.2.8), or
6652 * a predefined __func__ variable (11.4.1). */
6653 else if (DECL_ARTIFICIAL (decl))
6654 {
6655 if (complain & tf_error)
6656 error ("the address of %qD is not a valid template argument",
6657 decl);
6658 return true;
6659 }
6660 else if (!same_type_ignoring_top_level_qualifiers_p
6661 (strip_array_types (TREE_TYPE (type)),
6662 strip_array_types (TREE_TYPE (decl))))
6663 {
6664 if (complain & tf_error)
6665 error ("the address of the %qT subobject of %qD is not a "
6666 "valid template argument", TREE_TYPE (type), decl);
6667 return true;
6668 }
6669 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6670 {
6671 if (complain & tf_error)
6672 error ("the address of %qD is not a valid template argument "
6673 "because it does not have static storage duration",
6674 decl);
6675 return true;
6676 }
6677 }
6678 break;
6679
6680 default:
6681 if (!INDIRECT_TYPE_P (type))
6682 /* We're only concerned about pointers and references here. */;
6683 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6684 /* Null pointer values are OK in C++11. */;
6685 else
6686 {
6687 if (VAR_P (expr))
6688 {
6689 if (complain & tf_error)
6690 error ("%qD is not a valid template argument "
6691 "because %qD is a variable, not the address of "
6692 "a variable", expr, expr);
6693 return true;
6694 }
6695 else
6696 {
6697 if (complain & tf_error)
6698 error ("%qE is not a valid template argument for %qT "
6699 "because it is not the address of a variable",
6700 expr, type);
6701 return true;
6702 }
6703 }
6704 }
6705 return false;
6706
6707 }
6708
6709 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6710 template argument EXPR. */
6711
6712 static tree
6713 get_template_parm_object (tree expr, tsubst_flags_t complain)
6714 {
6715 if (TREE_CODE (expr) == TARGET_EXPR)
6716 expr = TARGET_EXPR_INITIAL (expr);
6717
6718 if (!TREE_CONSTANT (expr))
6719 {
6720 if ((complain & tf_error)
6721 && require_rvalue_constant_expression (expr))
6722 cxx_constant_value (expr);
6723 return error_mark_node;
6724 }
6725 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6726 return error_mark_node;
6727
6728 tree name = mangle_template_parm_object (expr);
6729 tree decl = get_global_binding (name);
6730 if (decl)
6731 return decl;
6732
6733 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6734 decl = create_temporary_var (type);
6735 TREE_STATIC (decl) = true;
6736 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6737 TREE_READONLY (decl) = true;
6738 DECL_NAME (decl) = name;
6739 SET_DECL_ASSEMBLER_NAME (decl, name);
6740 DECL_CONTEXT (decl) = global_namespace;
6741 comdat_linkage (decl);
6742 pushdecl_top_level_and_finish (decl, expr);
6743 return decl;
6744 }
6745
6746 /* Attempt to convert the non-type template parameter EXPR to the
6747 indicated TYPE. If the conversion is successful, return the
6748 converted value. If the conversion is unsuccessful, return
6749 NULL_TREE if we issued an error message, or error_mark_node if we
6750 did not. We issue error messages for out-and-out bad template
6751 parameters, but not simply because the conversion failed, since we
6752 might be just trying to do argument deduction. Both TYPE and EXPR
6753 must be non-dependent.
6754
6755 The conversion follows the special rules described in
6756 [temp.arg.nontype], and it is much more strict than an implicit
6757 conversion.
6758
6759 This function is called twice for each template argument (see
6760 lookup_template_class for a more accurate description of this
6761 problem). This means that we need to handle expressions which
6762 are not valid in a C++ source, but can be created from the
6763 first call (for instance, casts to perform conversions). These
6764 hacks can go away after we fix the double coercion problem. */
6765
6766 static tree
6767 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6768 {
6769 tree expr_type;
6770 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6771
6772 /* Detect immediately string literals as invalid non-type argument.
6773 This special-case is not needed for correctness (we would easily
6774 catch this later), but only to provide better diagnostic for this
6775 common user mistake. As suggested by DR 100, we do not mention
6776 linkage issues in the diagnostic as this is not the point. */
6777 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6778 {
6779 if (complain & tf_error)
6780 error ("%qE is not a valid template argument for type %qT "
6781 "because string literals can never be used in this context",
6782 expr, type);
6783 return NULL_TREE;
6784 }
6785
6786 /* Add the ADDR_EXPR now for the benefit of
6787 value_dependent_expression_p. */
6788 if (TYPE_PTROBV_P (type)
6789 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6790 {
6791 expr = decay_conversion (expr, complain);
6792 if (expr == error_mark_node)
6793 return error_mark_node;
6794 }
6795
6796 /* If we are in a template, EXPR may be non-dependent, but still
6797 have a syntactic, rather than semantic, form. For example, EXPR
6798 might be a SCOPE_REF, rather than the VAR_DECL to which the
6799 SCOPE_REF refers. Preserving the qualifying scope is necessary
6800 so that access checking can be performed when the template is
6801 instantiated -- but here we need the resolved form so that we can
6802 convert the argument. */
6803 bool non_dep = false;
6804 if (TYPE_REF_OBJ_P (type)
6805 && has_value_dependent_address (expr))
6806 /* If we want the address and it's value-dependent, don't fold. */;
6807 else if (processing_template_decl
6808 && is_nondependent_constant_expression (expr))
6809 non_dep = true;
6810 if (error_operand_p (expr))
6811 return error_mark_node;
6812 expr_type = TREE_TYPE (expr);
6813
6814 /* If the argument is non-dependent, perform any conversions in
6815 non-dependent context as well. */
6816 processing_template_decl_sentinel s (non_dep);
6817 if (non_dep)
6818 expr = instantiate_non_dependent_expr_internal (expr, complain);
6819
6820 if (value_dependent_expression_p (expr))
6821 expr = canonicalize_expr_argument (expr, complain);
6822
6823 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6824 to a non-type argument of "nullptr". */
6825 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6826 expr = fold_simple (convert (type, expr));
6827
6828 /* In C++11, integral or enumeration non-type template arguments can be
6829 arbitrary constant expressions. Pointer and pointer to
6830 member arguments can be general constant expressions that evaluate
6831 to a null value, but otherwise still need to be of a specific form. */
6832 if (cxx_dialect >= cxx11)
6833 {
6834 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
6835 /* A PTRMEM_CST is already constant, and a valid template
6836 argument for a parameter of pointer to member type, we just want
6837 to leave it in that form rather than lower it to a
6838 CONSTRUCTOR. */;
6839 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6840 || cxx_dialect >= cxx17)
6841 {
6842 /* Calling build_converted_constant_expr might create a call to
6843 a conversion function with a value-dependent argument, which
6844 could invoke taking the address of a temporary representing
6845 the result of the conversion. */
6846 if (COMPOUND_LITERAL_P (expr)
6847 && CONSTRUCTOR_IS_DEPENDENT (expr)
6848 && MAYBE_CLASS_TYPE_P (expr_type)
6849 && TYPE_HAS_CONVERSION (expr_type))
6850 {
6851 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
6852 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
6853 return expr;
6854 }
6855 /* C++17: A template-argument for a non-type template-parameter shall
6856 be a converted constant expression (8.20) of the type of the
6857 template-parameter. */
6858 expr = build_converted_constant_expr (type, expr, complain);
6859 if (expr == error_mark_node)
6860 /* Make sure we return NULL_TREE only if we have really issued
6861 an error, as described above. */
6862 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6863 expr = maybe_constant_value (expr, NULL_TREE,
6864 /*manifestly_const_eval=*/true);
6865 expr = convert_from_reference (expr);
6866 }
6867 else if (TYPE_PTR_OR_PTRMEM_P (type))
6868 {
6869 tree folded = maybe_constant_value (expr, NULL_TREE,
6870 /*manifestly_const_eval=*/true);
6871 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6872 : null_member_pointer_value_p (folded))
6873 expr = folded;
6874 }
6875 }
6876
6877 if (TYPE_REF_P (type))
6878 expr = mark_lvalue_use (expr);
6879 else
6880 expr = mark_rvalue_use (expr);
6881
6882 /* HACK: Due to double coercion, we can get a
6883 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6884 which is the tree that we built on the first call (see
6885 below when coercing to reference to object or to reference to
6886 function). We just strip everything and get to the arg.
6887 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6888 for examples. */
6889 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6890 {
6891 tree probe_type, probe = expr;
6892 if (REFERENCE_REF_P (probe))
6893 probe = TREE_OPERAND (probe, 0);
6894 probe_type = TREE_TYPE (probe);
6895 if (TREE_CODE (probe) == NOP_EXPR)
6896 {
6897 /* ??? Maybe we could use convert_from_reference here, but we
6898 would need to relax its constraints because the NOP_EXPR
6899 could actually change the type to something more cv-qualified,
6900 and this is not folded by convert_from_reference. */
6901 tree addr = TREE_OPERAND (probe, 0);
6902 if (TYPE_REF_P (probe_type)
6903 && TREE_CODE (addr) == ADDR_EXPR
6904 && TYPE_PTR_P (TREE_TYPE (addr))
6905 && (same_type_ignoring_top_level_qualifiers_p
6906 (TREE_TYPE (probe_type),
6907 TREE_TYPE (TREE_TYPE (addr)))))
6908 {
6909 expr = TREE_OPERAND (addr, 0);
6910 expr_type = TREE_TYPE (probe_type);
6911 }
6912 }
6913 }
6914
6915 /* [temp.arg.nontype]/5, bullet 1
6916
6917 For a non-type template-parameter of integral or enumeration type,
6918 integral promotions (_conv.prom_) and integral conversions
6919 (_conv.integral_) are applied. */
6920 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6921 {
6922 if (cxx_dialect < cxx11)
6923 {
6924 tree t = build_converted_constant_expr (type, expr, complain);
6925 t = maybe_constant_value (t);
6926 if (t != error_mark_node)
6927 expr = t;
6928 }
6929
6930 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6931 return error_mark_node;
6932
6933 /* Notice that there are constant expressions like '4 % 0' which
6934 do not fold into integer constants. */
6935 if (TREE_CODE (expr) != INTEGER_CST
6936 && !value_dependent_expression_p (expr))
6937 {
6938 if (complain & tf_error)
6939 {
6940 int errs = errorcount, warns = warningcount + werrorcount;
6941 if (!require_potential_constant_expression (expr))
6942 expr = error_mark_node;
6943 else
6944 expr = cxx_constant_value (expr);
6945 if (errorcount > errs || warningcount + werrorcount > warns)
6946 inform (loc, "in template argument for type %qT", type);
6947 if (expr == error_mark_node)
6948 return NULL_TREE;
6949 /* else cxx_constant_value complained but gave us
6950 a real constant, so go ahead. */
6951 if (TREE_CODE (expr) != INTEGER_CST)
6952 {
6953 /* Some assemble time constant expressions like
6954 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6955 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6956 as we can emit them into .rodata initializers of
6957 variables, yet they can't fold into an INTEGER_CST at
6958 compile time. Refuse them here. */
6959 gcc_checking_assert (reduced_constant_expression_p (expr));
6960 error_at (loc, "template argument %qE for type %qT not "
6961 "a constant integer", expr, type);
6962 return NULL_TREE;
6963 }
6964 }
6965 else
6966 return NULL_TREE;
6967 }
6968
6969 /* Avoid typedef problems. */
6970 if (TREE_TYPE (expr) != type)
6971 expr = fold_convert (type, expr);
6972 }
6973 /* [temp.arg.nontype]/5, bullet 2
6974
6975 For a non-type template-parameter of type pointer to object,
6976 qualification conversions (_conv.qual_) and the array-to-pointer
6977 conversion (_conv.array_) are applied. */
6978 else if (TYPE_PTROBV_P (type))
6979 {
6980 tree decayed = expr;
6981
6982 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6983 decay_conversion or an explicit cast. If it's a problematic cast,
6984 we'll complain about it below. */
6985 if (TREE_CODE (expr) == NOP_EXPR)
6986 {
6987 tree probe = expr;
6988 STRIP_NOPS (probe);
6989 if (TREE_CODE (probe) == ADDR_EXPR
6990 && TYPE_PTR_P (TREE_TYPE (probe)))
6991 {
6992 expr = probe;
6993 expr_type = TREE_TYPE (expr);
6994 }
6995 }
6996
6997 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6998
6999 A template-argument for a non-type, non-template template-parameter
7000 shall be one of: [...]
7001
7002 -- the name of a non-type template-parameter;
7003 -- the address of an object or function with external linkage, [...]
7004 expressed as "& id-expression" where the & is optional if the name
7005 refers to a function or array, or if the corresponding
7006 template-parameter is a reference.
7007
7008 Here, we do not care about functions, as they are invalid anyway
7009 for a parameter of type pointer-to-object. */
7010
7011 if (value_dependent_expression_p (expr))
7012 /* Non-type template parameters are OK. */
7013 ;
7014 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7015 /* Null pointer values are OK in C++11. */;
7016 else if (TREE_CODE (expr) != ADDR_EXPR
7017 && !INDIRECT_TYPE_P (expr_type))
7018 /* Other values, like integer constants, might be valid
7019 non-type arguments of some other type. */
7020 return error_mark_node;
7021 else if (invalid_tparm_referent_p (type, expr, complain))
7022 return NULL_TREE;
7023
7024 expr = decayed;
7025
7026 expr = perform_qualification_conversions (type, expr);
7027 if (expr == error_mark_node)
7028 return error_mark_node;
7029 }
7030 /* [temp.arg.nontype]/5, bullet 3
7031
7032 For a non-type template-parameter of type reference to object, no
7033 conversions apply. The type referred to by the reference may be more
7034 cv-qualified than the (otherwise identical) type of the
7035 template-argument. The template-parameter is bound directly to the
7036 template-argument, which must be an lvalue. */
7037 else if (TYPE_REF_OBJ_P (type))
7038 {
7039 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7040 expr_type))
7041 return error_mark_node;
7042
7043 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7044 {
7045 if (complain & tf_error)
7046 error ("%qE is not a valid template argument for type %qT "
7047 "because of conflicts in cv-qualification", expr, type);
7048 return NULL_TREE;
7049 }
7050
7051 if (!lvalue_p (expr))
7052 {
7053 if (complain & tf_error)
7054 error ("%qE is not a valid template argument for type %qT "
7055 "because it is not an lvalue", expr, type);
7056 return NULL_TREE;
7057 }
7058
7059 /* [temp.arg.nontype]/1
7060
7061 A template-argument for a non-type, non-template template-parameter
7062 shall be one of: [...]
7063
7064 -- the address of an object or function with external linkage. */
7065 if (INDIRECT_REF_P (expr)
7066 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7067 {
7068 expr = TREE_OPERAND (expr, 0);
7069 if (DECL_P (expr))
7070 {
7071 if (complain & tf_error)
7072 error ("%q#D is not a valid template argument for type %qT "
7073 "because a reference variable does not have a constant "
7074 "address", expr, type);
7075 return NULL_TREE;
7076 }
7077 }
7078
7079 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7080 && value_dependent_expression_p (expr))
7081 /* OK, dependent reference. We don't want to ask whether a DECL is
7082 itself value-dependent, since what we want here is its address. */;
7083 else
7084 {
7085 expr = build_address (expr);
7086
7087 if (invalid_tparm_referent_p (type, expr, complain))
7088 return NULL_TREE;
7089 }
7090
7091 if (!same_type_p (type, TREE_TYPE (expr)))
7092 expr = build_nop (type, expr);
7093 }
7094 /* [temp.arg.nontype]/5, bullet 4
7095
7096 For a non-type template-parameter of type pointer to function, only
7097 the function-to-pointer conversion (_conv.func_) is applied. If the
7098 template-argument represents a set of overloaded functions (or a
7099 pointer to such), the matching function is selected from the set
7100 (_over.over_). */
7101 else if (TYPE_PTRFN_P (type))
7102 {
7103 /* If the argument is a template-id, we might not have enough
7104 context information to decay the pointer. */
7105 if (!type_unknown_p (expr_type))
7106 {
7107 expr = decay_conversion (expr, complain);
7108 if (expr == error_mark_node)
7109 return error_mark_node;
7110 }
7111
7112 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7113 /* Null pointer values are OK in C++11. */
7114 return perform_qualification_conversions (type, expr);
7115
7116 expr = convert_nontype_argument_function (type, expr, complain);
7117 if (!expr || expr == error_mark_node)
7118 return expr;
7119 }
7120 /* [temp.arg.nontype]/5, bullet 5
7121
7122 For a non-type template-parameter of type reference to function, no
7123 conversions apply. If the template-argument represents a set of
7124 overloaded functions, the matching function is selected from the set
7125 (_over.over_). */
7126 else if (TYPE_REFFN_P (type))
7127 {
7128 if (TREE_CODE (expr) == ADDR_EXPR)
7129 {
7130 if (complain & tf_error)
7131 {
7132 error ("%qE is not a valid template argument for type %qT "
7133 "because it is a pointer", expr, type);
7134 inform (input_location, "try using %qE instead",
7135 TREE_OPERAND (expr, 0));
7136 }
7137 return NULL_TREE;
7138 }
7139
7140 expr = convert_nontype_argument_function (type, expr, complain);
7141 if (!expr || expr == error_mark_node)
7142 return expr;
7143 }
7144 /* [temp.arg.nontype]/5, bullet 6
7145
7146 For a non-type template-parameter of type pointer to member function,
7147 no conversions apply. If the template-argument represents a set of
7148 overloaded member functions, the matching member function is selected
7149 from the set (_over.over_). */
7150 else if (TYPE_PTRMEMFUNC_P (type))
7151 {
7152 expr = instantiate_type (type, expr, tf_none);
7153 if (expr == error_mark_node)
7154 return error_mark_node;
7155
7156 /* [temp.arg.nontype] bullet 1 says the pointer to member
7157 expression must be a pointer-to-member constant. */
7158 if (!value_dependent_expression_p (expr)
7159 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7160 return NULL_TREE;
7161
7162 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7163 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7164 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7165 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7166 }
7167 /* [temp.arg.nontype]/5, bullet 7
7168
7169 For a non-type template-parameter of type pointer to data member,
7170 qualification conversions (_conv.qual_) are applied. */
7171 else if (TYPE_PTRDATAMEM_P (type))
7172 {
7173 /* [temp.arg.nontype] bullet 1 says the pointer to member
7174 expression must be a pointer-to-member constant. */
7175 if (!value_dependent_expression_p (expr)
7176 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7177 return NULL_TREE;
7178
7179 expr = perform_qualification_conversions (type, expr);
7180 if (expr == error_mark_node)
7181 return expr;
7182 }
7183 else if (NULLPTR_TYPE_P (type))
7184 {
7185 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7186 {
7187 if (complain & tf_error)
7188 error ("%qE is not a valid template argument for type %qT "
7189 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7190 return NULL_TREE;
7191 }
7192 return expr;
7193 }
7194 else if (CLASS_TYPE_P (type))
7195 {
7196 /* Replace the argument with a reference to the corresponding template
7197 parameter object. */
7198 if (!value_dependent_expression_p (expr))
7199 expr = get_template_parm_object (expr, complain);
7200 if (expr == error_mark_node)
7201 return NULL_TREE;
7202 }
7203 /* A template non-type parameter must be one of the above. */
7204 else
7205 gcc_unreachable ();
7206
7207 /* Sanity check: did we actually convert the argument to the
7208 right type? */
7209 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7210 (type, TREE_TYPE (expr)));
7211 return convert_from_reference (expr);
7212 }
7213
7214 /* Subroutine of coerce_template_template_parms, which returns 1 if
7215 PARM_PARM and ARG_PARM match using the rule for the template
7216 parameters of template template parameters. Both PARM and ARG are
7217 template parameters; the rest of the arguments are the same as for
7218 coerce_template_template_parms.
7219 */
7220 static int
7221 coerce_template_template_parm (tree parm,
7222 tree arg,
7223 tsubst_flags_t complain,
7224 tree in_decl,
7225 tree outer_args)
7226 {
7227 if (arg == NULL_TREE || error_operand_p (arg)
7228 || parm == NULL_TREE || error_operand_p (parm))
7229 return 0;
7230
7231 if (TREE_CODE (arg) != TREE_CODE (parm))
7232 return 0;
7233
7234 switch (TREE_CODE (parm))
7235 {
7236 case TEMPLATE_DECL:
7237 /* We encounter instantiations of templates like
7238 template <template <template <class> class> class TT>
7239 class C; */
7240 {
7241 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7242 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7243
7244 if (!coerce_template_template_parms
7245 (parmparm, argparm, complain, in_decl, outer_args))
7246 return 0;
7247 }
7248 /* Fall through. */
7249
7250 case TYPE_DECL:
7251 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7252 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7253 /* Argument is a parameter pack but parameter is not. */
7254 return 0;
7255 break;
7256
7257 case PARM_DECL:
7258 /* The tsubst call is used to handle cases such as
7259
7260 template <int> class C {};
7261 template <class T, template <T> class TT> class D {};
7262 D<int, C> d;
7263
7264 i.e. the parameter list of TT depends on earlier parameters. */
7265 if (!uses_template_parms (TREE_TYPE (arg)))
7266 {
7267 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7268 if (!uses_template_parms (t)
7269 && !same_type_p (t, TREE_TYPE (arg)))
7270 return 0;
7271 }
7272
7273 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7274 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7275 /* Argument is a parameter pack but parameter is not. */
7276 return 0;
7277
7278 break;
7279
7280 default:
7281 gcc_unreachable ();
7282 }
7283
7284 return 1;
7285 }
7286
7287 /* Coerce template argument list ARGLIST for use with template
7288 template-parameter TEMPL. */
7289
7290 static tree
7291 coerce_template_args_for_ttp (tree templ, tree arglist,
7292 tsubst_flags_t complain)
7293 {
7294 /* Consider an example where a template template parameter declared as
7295
7296 template <class T, class U = std::allocator<T> > class TT
7297
7298 The template parameter level of T and U are one level larger than
7299 of TT. To proper process the default argument of U, say when an
7300 instantiation `TT<int>' is seen, we need to build the full
7301 arguments containing {int} as the innermost level. Outer levels,
7302 available when not appearing as default template argument, can be
7303 obtained from the arguments of the enclosing template.
7304
7305 Suppose that TT is later substituted with std::vector. The above
7306 instantiation is `TT<int, std::allocator<T> >' with TT at
7307 level 1, and T at level 2, while the template arguments at level 1
7308 becomes {std::vector} and the inner level 2 is {int}. */
7309
7310 tree outer = DECL_CONTEXT (templ);
7311 if (outer)
7312 {
7313 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7314 /* We want arguments for the partial specialization, not arguments for
7315 the primary template. */
7316 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7317 else
7318 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7319 }
7320 else if (current_template_parms)
7321 {
7322 /* This is an argument of the current template, so we haven't set
7323 DECL_CONTEXT yet. */
7324 tree relevant_template_parms;
7325
7326 /* Parameter levels that are greater than the level of the given
7327 template template parm are irrelevant. */
7328 relevant_template_parms = current_template_parms;
7329 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7330 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7331 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7332
7333 outer = template_parms_to_args (relevant_template_parms);
7334 }
7335
7336 if (outer)
7337 arglist = add_to_template_args (outer, arglist);
7338
7339 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7340 return coerce_template_parms (parmlist, arglist, templ,
7341 complain,
7342 /*require_all_args=*/true,
7343 /*use_default_args=*/true);
7344 }
7345
7346 /* A cache of template template parameters with match-all default
7347 arguments. */
7348 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7349 static void
7350 store_defaulted_ttp (tree v, tree t)
7351 {
7352 if (!defaulted_ttp_cache)
7353 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7354 defaulted_ttp_cache->put (v, t);
7355 }
7356 static tree
7357 lookup_defaulted_ttp (tree v)
7358 {
7359 if (defaulted_ttp_cache)
7360 if (tree *p = defaulted_ttp_cache->get (v))
7361 return *p;
7362 return NULL_TREE;
7363 }
7364
7365 /* T is a bound template template-parameter. Copy its arguments into default
7366 arguments of the template template-parameter's template parameters. */
7367
7368 static tree
7369 add_defaults_to_ttp (tree otmpl)
7370 {
7371 if (tree c = lookup_defaulted_ttp (otmpl))
7372 return c;
7373
7374 tree ntmpl = copy_node (otmpl);
7375
7376 tree ntype = copy_node (TREE_TYPE (otmpl));
7377 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7378 TYPE_MAIN_VARIANT (ntype) = ntype;
7379 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7380 TYPE_NAME (ntype) = ntmpl;
7381 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7382
7383 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7384 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7385 TEMPLATE_PARM_DECL (idx) = ntmpl;
7386 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7387
7388 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7389 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7390 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7391 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7392 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7393 {
7394 tree o = TREE_VEC_ELT (vec, i);
7395 if (!template_parameter_pack_p (TREE_VALUE (o)))
7396 {
7397 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7398 TREE_PURPOSE (n) = any_targ_node;
7399 }
7400 }
7401
7402 store_defaulted_ttp (otmpl, ntmpl);
7403 return ntmpl;
7404 }
7405
7406 /* ARG is a bound potential template template-argument, and PARGS is a list
7407 of arguments for the corresponding template template-parameter. Adjust
7408 PARGS as appropriate for application to ARG's template, and if ARG is a
7409 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7410 arguments to the template template parameter. */
7411
7412 static tree
7413 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7414 {
7415 ++processing_template_decl;
7416 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7417 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7418 {
7419 /* When comparing two template template-parameters in partial ordering,
7420 rewrite the one currently being used as an argument to have default
7421 arguments for all parameters. */
7422 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7423 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7424 if (pargs != error_mark_node)
7425 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7426 TYPE_TI_ARGS (arg));
7427 }
7428 else
7429 {
7430 tree aparms
7431 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7432 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7433 /*require_all*/true,
7434 /*use_default*/true);
7435 }
7436 --processing_template_decl;
7437 return pargs;
7438 }
7439
7440 /* Subroutine of unify for the case when PARM is a
7441 BOUND_TEMPLATE_TEMPLATE_PARM. */
7442
7443 static int
7444 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7445 bool explain_p)
7446 {
7447 tree parmvec = TYPE_TI_ARGS (parm);
7448 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7449
7450 /* The template template parm might be variadic and the argument
7451 not, so flatten both argument lists. */
7452 parmvec = expand_template_argument_pack (parmvec);
7453 argvec = expand_template_argument_pack (argvec);
7454
7455 if (flag_new_ttp)
7456 {
7457 /* In keeping with P0522R0, adjust P's template arguments
7458 to apply to A's template; then flatten it again. */
7459 tree nparmvec = parmvec;
7460 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7461 nparmvec = expand_template_argument_pack (nparmvec);
7462
7463 if (unify (tparms, targs, nparmvec, argvec,
7464 UNIFY_ALLOW_NONE, explain_p))
7465 return 1;
7466
7467 /* If the P0522 adjustment eliminated a pack expansion, deduce
7468 empty packs. */
7469 if (flag_new_ttp
7470 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7471 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7472 DEDUCE_EXACT, /*sub*/true, explain_p))
7473 return 1;
7474 }
7475 else
7476 {
7477 /* Deduce arguments T, i from TT<T> or TT<i>.
7478 We check each element of PARMVEC and ARGVEC individually
7479 rather than the whole TREE_VEC since they can have
7480 different number of elements, which is allowed under N2555. */
7481
7482 int len = TREE_VEC_LENGTH (parmvec);
7483
7484 /* Check if the parameters end in a pack, making them
7485 variadic. */
7486 int parm_variadic_p = 0;
7487 if (len > 0
7488 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7489 parm_variadic_p = 1;
7490
7491 for (int i = 0; i < len - parm_variadic_p; ++i)
7492 /* If the template argument list of P contains a pack
7493 expansion that is not the last template argument, the
7494 entire template argument list is a non-deduced
7495 context. */
7496 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7497 return unify_success (explain_p);
7498
7499 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7500 return unify_too_few_arguments (explain_p,
7501 TREE_VEC_LENGTH (argvec), len);
7502
7503 for (int i = 0; i < len - parm_variadic_p; ++i)
7504 if (unify (tparms, targs,
7505 TREE_VEC_ELT (parmvec, i),
7506 TREE_VEC_ELT (argvec, i),
7507 UNIFY_ALLOW_NONE, explain_p))
7508 return 1;
7509
7510 if (parm_variadic_p
7511 && unify_pack_expansion (tparms, targs,
7512 parmvec, argvec,
7513 DEDUCE_EXACT,
7514 /*subr=*/true, explain_p))
7515 return 1;
7516 }
7517
7518 return 0;
7519 }
7520
7521 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7522 template template parameters. Both PARM_PARMS and ARG_PARMS are
7523 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7524 or PARM_DECL.
7525
7526 Consider the example:
7527 template <class T> class A;
7528 template<template <class U> class TT> class B;
7529
7530 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7531 the parameters to A, and OUTER_ARGS contains A. */
7532
7533 static int
7534 coerce_template_template_parms (tree parm_parms,
7535 tree arg_parms,
7536 tsubst_flags_t complain,
7537 tree in_decl,
7538 tree outer_args)
7539 {
7540 int nparms, nargs, i;
7541 tree parm, arg;
7542 int variadic_p = 0;
7543
7544 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7545 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7546
7547 nparms = TREE_VEC_LENGTH (parm_parms);
7548 nargs = TREE_VEC_LENGTH (arg_parms);
7549
7550 if (flag_new_ttp)
7551 {
7552 /* P0522R0: A template template-parameter P is at least as specialized as
7553 a template template-argument A if, given the following rewrite to two
7554 function templates, the function template corresponding to P is at
7555 least as specialized as the function template corresponding to A
7556 according to the partial ordering rules for function templates
7557 ([temp.func.order]). Given an invented class template X with the
7558 template parameter list of A (including default arguments):
7559
7560 * Each of the two function templates has the same template parameters,
7561 respectively, as P or A.
7562
7563 * Each function template has a single function parameter whose type is
7564 a specialization of X with template arguments corresponding to the
7565 template parameters from the respective function template where, for
7566 each template parameter PP in the template parameter list of the
7567 function template, a corresponding template argument AA is formed. If
7568 PP declares a parameter pack, then AA is the pack expansion
7569 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7570
7571 If the rewrite produces an invalid type, then P is not at least as
7572 specialized as A. */
7573
7574 /* So coerce P's args to apply to A's parms, and then deduce between A's
7575 args and the converted args. If that succeeds, A is at least as
7576 specialized as P, so they match.*/
7577 tree pargs = template_parms_level_to_args (parm_parms);
7578 pargs = add_outermost_template_args (outer_args, pargs);
7579 ++processing_template_decl;
7580 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7581 /*require_all*/true, /*use_default*/true);
7582 --processing_template_decl;
7583 if (pargs != error_mark_node)
7584 {
7585 tree targs = make_tree_vec (nargs);
7586 tree aargs = template_parms_level_to_args (arg_parms);
7587 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7588 /*explain*/false))
7589 return 1;
7590 }
7591 }
7592
7593 /* Determine whether we have a parameter pack at the end of the
7594 template template parameter's template parameter list. */
7595 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7596 {
7597 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7598
7599 if (error_operand_p (parm))
7600 return 0;
7601
7602 switch (TREE_CODE (parm))
7603 {
7604 case TEMPLATE_DECL:
7605 case TYPE_DECL:
7606 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7607 variadic_p = 1;
7608 break;
7609
7610 case PARM_DECL:
7611 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7612 variadic_p = 1;
7613 break;
7614
7615 default:
7616 gcc_unreachable ();
7617 }
7618 }
7619
7620 if (nargs != nparms
7621 && !(variadic_p && nargs >= nparms - 1))
7622 return 0;
7623
7624 /* Check all of the template parameters except the parameter pack at
7625 the end (if any). */
7626 for (i = 0; i < nparms - variadic_p; ++i)
7627 {
7628 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7629 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7630 continue;
7631
7632 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7633 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7634
7635 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7636 outer_args))
7637 return 0;
7638
7639 }
7640
7641 if (variadic_p)
7642 {
7643 /* Check each of the template parameters in the template
7644 argument against the template parameter pack at the end of
7645 the template template parameter. */
7646 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7647 return 0;
7648
7649 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7650
7651 for (; i < nargs; ++i)
7652 {
7653 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7654 continue;
7655
7656 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7657
7658 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7659 outer_args))
7660 return 0;
7661 }
7662 }
7663
7664 return 1;
7665 }
7666
7667 /* Verifies that the deduced template arguments (in TARGS) for the
7668 template template parameters (in TPARMS) represent valid bindings,
7669 by comparing the template parameter list of each template argument
7670 to the template parameter list of its corresponding template
7671 template parameter, in accordance with DR150. This
7672 routine can only be called after all template arguments have been
7673 deduced. It will return TRUE if all of the template template
7674 parameter bindings are okay, FALSE otherwise. */
7675 bool
7676 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7677 {
7678 int i, ntparms = TREE_VEC_LENGTH (tparms);
7679 bool ret = true;
7680
7681 /* We're dealing with template parms in this process. */
7682 ++processing_template_decl;
7683
7684 targs = INNERMOST_TEMPLATE_ARGS (targs);
7685
7686 for (i = 0; i < ntparms; ++i)
7687 {
7688 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7689 tree targ = TREE_VEC_ELT (targs, i);
7690
7691 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7692 {
7693 tree packed_args = NULL_TREE;
7694 int idx, len = 1;
7695
7696 if (ARGUMENT_PACK_P (targ))
7697 {
7698 /* Look inside the argument pack. */
7699 packed_args = ARGUMENT_PACK_ARGS (targ);
7700 len = TREE_VEC_LENGTH (packed_args);
7701 }
7702
7703 for (idx = 0; idx < len; ++idx)
7704 {
7705 tree targ_parms = NULL_TREE;
7706
7707 if (packed_args)
7708 /* Extract the next argument from the argument
7709 pack. */
7710 targ = TREE_VEC_ELT (packed_args, idx);
7711
7712 if (PACK_EXPANSION_P (targ))
7713 /* Look at the pattern of the pack expansion. */
7714 targ = PACK_EXPANSION_PATTERN (targ);
7715
7716 /* Extract the template parameters from the template
7717 argument. */
7718 if (TREE_CODE (targ) == TEMPLATE_DECL)
7719 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7720 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7721 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7722
7723 /* Verify that we can coerce the template template
7724 parameters from the template argument to the template
7725 parameter. This requires an exact match. */
7726 if (targ_parms
7727 && !coerce_template_template_parms
7728 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7729 targ_parms,
7730 tf_none,
7731 tparm,
7732 targs))
7733 {
7734 ret = false;
7735 goto out;
7736 }
7737 }
7738 }
7739 }
7740
7741 out:
7742
7743 --processing_template_decl;
7744 return ret;
7745 }
7746
7747 /* Since type attributes aren't mangled, we need to strip them from
7748 template type arguments. */
7749
7750 static tree
7751 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7752 {
7753 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7754 return arg;
7755 bool removed_attributes = false;
7756 tree canon = strip_typedefs (arg, &removed_attributes);
7757 if (removed_attributes
7758 && (complain & tf_warning))
7759 warning (OPT_Wignored_attributes,
7760 "ignoring attributes on template argument %qT", arg);
7761 return canon;
7762 }
7763
7764 /* And from inside dependent non-type arguments like sizeof(Type). */
7765
7766 static tree
7767 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7768 {
7769 if (!arg || arg == error_mark_node)
7770 return arg;
7771 bool removed_attributes = false;
7772 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7773 if (removed_attributes
7774 && (complain & tf_warning))
7775 warning (OPT_Wignored_attributes,
7776 "ignoring attributes in template argument %qE", arg);
7777 return canon;
7778 }
7779
7780 // A template declaration can be substituted for a constrained
7781 // template template parameter only when the argument is more
7782 // constrained than the parameter.
7783 static bool
7784 is_compatible_template_arg (tree parm, tree arg)
7785 {
7786 tree parm_cons = get_constraints (parm);
7787
7788 /* For now, allow constrained template template arguments
7789 and unconstrained template template parameters. */
7790 if (parm_cons == NULL_TREE)
7791 return true;
7792
7793 tree arg_cons = get_constraints (arg);
7794
7795 // If the template parameter is constrained, we need to rewrite its
7796 // constraints in terms of the ARG's template parameters. This ensures
7797 // that all of the template parameter types will have the same depth.
7798 //
7799 // Note that this is only valid when coerce_template_template_parm is
7800 // true for the innermost template parameters of PARM and ARG. In other
7801 // words, because coercion is successful, this conversion will be valid.
7802 if (parm_cons)
7803 {
7804 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7805 parm_cons = tsubst_constraint_info (parm_cons,
7806 INNERMOST_TEMPLATE_ARGS (args),
7807 tf_none, NULL_TREE);
7808 if (parm_cons == error_mark_node)
7809 return false;
7810 }
7811
7812 return subsumes (parm_cons, arg_cons);
7813 }
7814
7815 // Convert a placeholder argument into a binding to the original
7816 // parameter. The original parameter is saved as the TREE_TYPE of
7817 // ARG.
7818 static inline tree
7819 convert_wildcard_argument (tree parm, tree arg)
7820 {
7821 TREE_TYPE (arg) = parm;
7822 return arg;
7823 }
7824
7825 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7826 because one of them is dependent. But we need to represent the
7827 conversion for the benefit of cp_tree_equal. */
7828
7829 static tree
7830 maybe_convert_nontype_argument (tree type, tree arg)
7831 {
7832 /* Auto parms get no conversion. */
7833 if (type_uses_auto (type))
7834 return arg;
7835 /* We don't need or want to add this conversion now if we're going to use the
7836 argument for deduction. */
7837 if (value_dependent_expression_p (arg))
7838 return arg;
7839
7840 type = cv_unqualified (type);
7841 tree argtype = TREE_TYPE (arg);
7842 if (same_type_p (type, argtype))
7843 return arg;
7844
7845 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7846 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7847 return arg;
7848 }
7849
7850 /* Convert the indicated template ARG as necessary to match the
7851 indicated template PARM. Returns the converted ARG, or
7852 error_mark_node if the conversion was unsuccessful. Error and
7853 warning messages are issued under control of COMPLAIN. This
7854 conversion is for the Ith parameter in the parameter list. ARGS is
7855 the full set of template arguments deduced so far. */
7856
7857 static tree
7858 convert_template_argument (tree parm,
7859 tree arg,
7860 tree args,
7861 tsubst_flags_t complain,
7862 int i,
7863 tree in_decl)
7864 {
7865 tree orig_arg;
7866 tree val;
7867 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7868
7869 if (parm == error_mark_node || error_operand_p (arg))
7870 return error_mark_node;
7871
7872 /* Trivially convert placeholders. */
7873 if (TREE_CODE (arg) == WILDCARD_DECL)
7874 return convert_wildcard_argument (parm, arg);
7875
7876 if (arg == any_targ_node)
7877 return arg;
7878
7879 if (TREE_CODE (arg) == TREE_LIST
7880 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7881 {
7882 /* The template argument was the name of some
7883 member function. That's usually
7884 invalid, but static members are OK. In any
7885 case, grab the underlying fields/functions
7886 and issue an error later if required. */
7887 orig_arg = TREE_VALUE (arg);
7888 TREE_TYPE (arg) = unknown_type_node;
7889 }
7890
7891 orig_arg = arg;
7892
7893 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7894 requires_type = (TREE_CODE (parm) == TYPE_DECL
7895 || requires_tmpl_type);
7896
7897 /* When determining whether an argument pack expansion is a template,
7898 look at the pattern. */
7899 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7900 arg = PACK_EXPANSION_PATTERN (arg);
7901
7902 /* Deal with an injected-class-name used as a template template arg. */
7903 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7904 {
7905 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7906 if (TREE_CODE (t) == TEMPLATE_DECL)
7907 {
7908 if (cxx_dialect >= cxx11)
7909 /* OK under DR 1004. */;
7910 else if (complain & tf_warning_or_error)
7911 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7912 " used as template template argument", TYPE_NAME (arg));
7913 else if (flag_pedantic_errors)
7914 t = arg;
7915
7916 arg = t;
7917 }
7918 }
7919
7920 is_tmpl_type =
7921 ((TREE_CODE (arg) == TEMPLATE_DECL
7922 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7923 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7924 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7925 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7926
7927 if (is_tmpl_type
7928 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7929 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7930 arg = TYPE_STUB_DECL (arg);
7931
7932 is_type = TYPE_P (arg) || is_tmpl_type;
7933
7934 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7935 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7936 {
7937 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7938 {
7939 if (complain & tf_error)
7940 error ("invalid use of destructor %qE as a type", orig_arg);
7941 return error_mark_node;
7942 }
7943
7944 permerror (input_location,
7945 "to refer to a type member of a template parameter, "
7946 "use %<typename %E%>", orig_arg);
7947
7948 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7949 TREE_OPERAND (arg, 1),
7950 typename_type,
7951 complain);
7952 arg = orig_arg;
7953 is_type = 1;
7954 }
7955 if (is_type != requires_type)
7956 {
7957 if (in_decl)
7958 {
7959 if (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_type)
7965 inform (input_location,
7966 " expected a constant of type %qT, got %qT",
7967 TREE_TYPE (parm),
7968 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7969 else if (requires_tmpl_type)
7970 inform (input_location,
7971 " expected a class template, got %qE", orig_arg);
7972 else
7973 inform (input_location,
7974 " expected a type, got %qE", orig_arg);
7975 }
7976 }
7977 return error_mark_node;
7978 }
7979 if (is_tmpl_type ^ requires_tmpl_type)
7980 {
7981 if (in_decl && (complain & tf_error))
7982 {
7983 error ("type/value mismatch at argument %d in template "
7984 "parameter list for %qD",
7985 i + 1, in_decl);
7986 if (is_tmpl_type)
7987 inform (input_location,
7988 " expected a type, got %qT", DECL_NAME (arg));
7989 else
7990 inform (input_location,
7991 " expected a class template, got %qT", orig_arg);
7992 }
7993 return error_mark_node;
7994 }
7995
7996 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7997 /* We already did the appropriate conversion when packing args. */
7998 val = orig_arg;
7999 else if (is_type)
8000 {
8001 if (requires_tmpl_type)
8002 {
8003 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8004 /* The number of argument required is not known yet.
8005 Just accept it for now. */
8006 val = orig_arg;
8007 else
8008 {
8009 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8010 tree argparm;
8011
8012 /* Strip alias templates that are equivalent to another
8013 template. */
8014 arg = get_underlying_template (arg);
8015 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8016
8017 if (coerce_template_template_parms (parmparm, argparm,
8018 complain, in_decl,
8019 args))
8020 {
8021 val = arg;
8022
8023 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8024 TEMPLATE_DECL. */
8025 if (val != error_mark_node)
8026 {
8027 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8028 val = TREE_TYPE (val);
8029 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8030 val = make_pack_expansion (val, complain);
8031 }
8032 }
8033 else
8034 {
8035 if (in_decl && (complain & tf_error))
8036 {
8037 error ("type/value mismatch at argument %d in "
8038 "template parameter list for %qD",
8039 i + 1, in_decl);
8040 inform (input_location,
8041 " expected a template of type %qD, got %qT",
8042 parm, orig_arg);
8043 }
8044
8045 val = error_mark_node;
8046 }
8047
8048 // Check that the constraints are compatible before allowing the
8049 // substitution.
8050 if (val != error_mark_node)
8051 if (!is_compatible_template_arg (parm, arg))
8052 {
8053 if (in_decl && (complain & tf_error))
8054 {
8055 error ("constraint mismatch at argument %d in "
8056 "template parameter list for %qD",
8057 i + 1, in_decl);
8058 inform (input_location, " expected %qD but got %qD",
8059 parm, arg);
8060 }
8061 val = error_mark_node;
8062 }
8063 }
8064 }
8065 else
8066 val = orig_arg;
8067 /* We only form one instance of each template specialization.
8068 Therefore, if we use a non-canonical variant (i.e., a
8069 typedef), any future messages referring to the type will use
8070 the typedef, which is confusing if those future uses do not
8071 themselves also use the typedef. */
8072 if (TYPE_P (val))
8073 val = canonicalize_type_argument (val, complain);
8074 }
8075 else
8076 {
8077 tree t = TREE_TYPE (parm);
8078
8079 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8080 > TMPL_ARGS_DEPTH (args))
8081 /* We don't have enough levels of args to do any substitution. This
8082 can happen in the context of -fnew-ttp-matching. */;
8083 else if (tree a = type_uses_auto (t))
8084 {
8085 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8086 if (t == error_mark_node)
8087 return error_mark_node;
8088 }
8089 else
8090 t = tsubst (t, args, complain, in_decl);
8091
8092 if (invalid_nontype_parm_type_p (t, complain))
8093 return error_mark_node;
8094
8095 if (t != TREE_TYPE (parm))
8096 t = canonicalize_type_argument (t, complain);
8097
8098 if (!type_dependent_expression_p (orig_arg)
8099 && !uses_template_parms (t))
8100 /* We used to call digest_init here. However, digest_init
8101 will report errors, which we don't want when complain
8102 is zero. More importantly, digest_init will try too
8103 hard to convert things: for example, `0' should not be
8104 converted to pointer type at this point according to
8105 the standard. Accepting this is not merely an
8106 extension, since deciding whether or not these
8107 conversions can occur is part of determining which
8108 function template to call, or whether a given explicit
8109 argument specification is valid. */
8110 val = convert_nontype_argument (t, orig_arg, complain);
8111 else
8112 {
8113 val = canonicalize_expr_argument (orig_arg, complain);
8114 val = maybe_convert_nontype_argument (t, val);
8115 }
8116
8117
8118 if (val == NULL_TREE)
8119 val = error_mark_node;
8120 else if (val == error_mark_node && (complain & tf_error))
8121 error ("could not convert template argument %qE from %qT to %qT",
8122 orig_arg, TREE_TYPE (orig_arg), t);
8123
8124 if (INDIRECT_REF_P (val))
8125 {
8126 /* Reject template arguments that are references to built-in
8127 functions with no library fallbacks. */
8128 const_tree inner = TREE_OPERAND (val, 0);
8129 const_tree innertype = TREE_TYPE (inner);
8130 if (innertype
8131 && TYPE_REF_P (innertype)
8132 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8133 && TREE_OPERAND_LENGTH (inner) > 0
8134 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8135 return error_mark_node;
8136 }
8137
8138 if (TREE_CODE (val) == SCOPE_REF)
8139 {
8140 /* Strip typedefs from the SCOPE_REF. */
8141 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8142 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8143 complain);
8144 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8145 QUALIFIED_NAME_IS_TEMPLATE (val));
8146 }
8147 }
8148
8149 return val;
8150 }
8151
8152 /* Coerces the remaining template arguments in INNER_ARGS (from
8153 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8154 Returns the coerced argument pack. PARM_IDX is the position of this
8155 parameter in the template parameter list. ARGS is the original
8156 template argument list. */
8157 static tree
8158 coerce_template_parameter_pack (tree parms,
8159 int parm_idx,
8160 tree args,
8161 tree inner_args,
8162 int arg_idx,
8163 tree new_args,
8164 int* lost,
8165 tree in_decl,
8166 tsubst_flags_t complain)
8167 {
8168 tree parm = TREE_VEC_ELT (parms, parm_idx);
8169 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8170 tree packed_args;
8171 tree argument_pack;
8172 tree packed_parms = NULL_TREE;
8173
8174 if (arg_idx > nargs)
8175 arg_idx = nargs;
8176
8177 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8178 {
8179 /* When the template parameter is a non-type template parameter pack
8180 or template template parameter pack whose type or template
8181 parameters use parameter packs, we know exactly how many arguments
8182 we are looking for. Build a vector of the instantiated decls for
8183 these template parameters in PACKED_PARMS. */
8184 /* We can't use make_pack_expansion here because it would interpret a
8185 _DECL as a use rather than a declaration. */
8186 tree decl = TREE_VALUE (parm);
8187 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8188 SET_PACK_EXPANSION_PATTERN (exp, decl);
8189 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8190 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8191
8192 TREE_VEC_LENGTH (args)--;
8193 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8194 TREE_VEC_LENGTH (args)++;
8195
8196 if (packed_parms == error_mark_node)
8197 return error_mark_node;
8198
8199 /* If we're doing a partial instantiation of a member template,
8200 verify that all of the types used for the non-type
8201 template parameter pack are, in fact, valid for non-type
8202 template parameters. */
8203 if (arg_idx < nargs
8204 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8205 {
8206 int j, len = TREE_VEC_LENGTH (packed_parms);
8207 for (j = 0; j < len; ++j)
8208 {
8209 tree t = TREE_VEC_ELT (packed_parms, j);
8210 if (TREE_CODE (t) == PARM_DECL
8211 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8212 return error_mark_node;
8213 }
8214 /* We don't know how many args we have yet, just
8215 use the unconverted ones for now. */
8216 return NULL_TREE;
8217 }
8218
8219 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8220 }
8221 /* Check if we have a placeholder pack, which indicates we're
8222 in the context of a introduction list. In that case we want
8223 to match this pack to the single placeholder. */
8224 else if (arg_idx < nargs
8225 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8226 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8227 {
8228 nargs = arg_idx + 1;
8229 packed_args = make_tree_vec (1);
8230 }
8231 else
8232 packed_args = make_tree_vec (nargs - arg_idx);
8233
8234 /* Convert the remaining arguments, which will be a part of the
8235 parameter pack "parm". */
8236 int first_pack_arg = arg_idx;
8237 for (; arg_idx < nargs; ++arg_idx)
8238 {
8239 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8240 tree actual_parm = TREE_VALUE (parm);
8241 int pack_idx = arg_idx - first_pack_arg;
8242
8243 if (packed_parms)
8244 {
8245 /* Once we've packed as many args as we have types, stop. */
8246 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8247 break;
8248 else if (PACK_EXPANSION_P (arg))
8249 /* We don't know how many args we have yet, just
8250 use the unconverted ones for now. */
8251 return NULL_TREE;
8252 else
8253 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8254 }
8255
8256 if (arg == error_mark_node)
8257 {
8258 if (complain & tf_error)
8259 error ("template argument %d is invalid", arg_idx + 1);
8260 }
8261 else
8262 arg = convert_template_argument (actual_parm,
8263 arg, new_args, complain, parm_idx,
8264 in_decl);
8265 if (arg == error_mark_node)
8266 (*lost)++;
8267 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8268 }
8269
8270 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8271 && TREE_VEC_LENGTH (packed_args) > 0)
8272 {
8273 if (complain & tf_error)
8274 error ("wrong number of template arguments (%d, should be %d)",
8275 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8276 return error_mark_node;
8277 }
8278
8279 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8280 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8281 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8282 else
8283 {
8284 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8285 TREE_CONSTANT (argument_pack) = 1;
8286 }
8287
8288 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8289 if (CHECKING_P)
8290 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8291 TREE_VEC_LENGTH (packed_args));
8292 return argument_pack;
8293 }
8294
8295 /* Returns the number of pack expansions in the template argument vector
8296 ARGS. */
8297
8298 static int
8299 pack_expansion_args_count (tree args)
8300 {
8301 int i;
8302 int count = 0;
8303 if (args)
8304 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8305 {
8306 tree elt = TREE_VEC_ELT (args, i);
8307 if (elt && PACK_EXPANSION_P (elt))
8308 ++count;
8309 }
8310 return count;
8311 }
8312
8313 /* Convert all template arguments to their appropriate types, and
8314 return a vector containing the innermost resulting template
8315 arguments. If any error occurs, return error_mark_node. Error and
8316 warning messages are issued under control of COMPLAIN.
8317
8318 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8319 for arguments not specified in ARGS. Otherwise, if
8320 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8321 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8322 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8323 ARGS. */
8324
8325 static tree
8326 coerce_template_parms (tree parms,
8327 tree args,
8328 tree in_decl,
8329 tsubst_flags_t complain,
8330 bool require_all_args,
8331 bool use_default_args)
8332 {
8333 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8334 tree orig_inner_args;
8335 tree inner_args;
8336 tree new_args;
8337 tree new_inner_args;
8338
8339 /* When used as a boolean value, indicates whether this is a
8340 variadic template parameter list. Since it's an int, we can also
8341 subtract it from nparms to get the number of non-variadic
8342 parameters. */
8343 int variadic_p = 0;
8344 int variadic_args_p = 0;
8345 int post_variadic_parms = 0;
8346
8347 /* Adjustment to nparms for fixed parameter packs. */
8348 int fixed_pack_adjust = 0;
8349 int fixed_packs = 0;
8350 int missing = 0;
8351
8352 /* Likewise for parameters with default arguments. */
8353 int default_p = 0;
8354
8355 if (args == error_mark_node)
8356 return error_mark_node;
8357
8358 nparms = TREE_VEC_LENGTH (parms);
8359
8360 /* Determine if there are any parameter packs or default arguments. */
8361 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8362 {
8363 tree parm = TREE_VEC_ELT (parms, parm_idx);
8364 if (variadic_p)
8365 ++post_variadic_parms;
8366 if (template_parameter_pack_p (TREE_VALUE (parm)))
8367 ++variadic_p;
8368 if (TREE_PURPOSE (parm))
8369 ++default_p;
8370 }
8371
8372 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8373 /* If there are no parameters that follow a parameter pack, we need to
8374 expand any argument packs so that we can deduce a parameter pack from
8375 some non-packed args followed by an argument pack, as in variadic85.C.
8376 If there are such parameters, we need to leave argument packs intact
8377 so the arguments are assigned properly. This can happen when dealing
8378 with a nested class inside a partial specialization of a class
8379 template, as in variadic92.C, or when deducing a template parameter pack
8380 from a sub-declarator, as in variadic114.C. */
8381 if (!post_variadic_parms)
8382 inner_args = expand_template_argument_pack (inner_args);
8383
8384 /* Count any pack expansion args. */
8385 variadic_args_p = pack_expansion_args_count (inner_args);
8386
8387 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8388 if ((nargs - variadic_args_p > nparms && !variadic_p)
8389 || (nargs < nparms - variadic_p
8390 && require_all_args
8391 && !variadic_args_p
8392 && (!use_default_args
8393 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8394 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8395 {
8396 bad_nargs:
8397 if (complain & tf_error)
8398 {
8399 if (variadic_p || default_p)
8400 {
8401 nparms -= variadic_p + default_p;
8402 error ("wrong number of template arguments "
8403 "(%d, should be at least %d)", nargs, nparms);
8404 }
8405 else
8406 error ("wrong number of template arguments "
8407 "(%d, should be %d)", nargs, nparms);
8408
8409 if (in_decl)
8410 inform (DECL_SOURCE_LOCATION (in_decl),
8411 "provided for %qD", in_decl);
8412 }
8413
8414 return error_mark_node;
8415 }
8416 /* We can't pass a pack expansion to a non-pack parameter of an alias
8417 template (DR 1430). */
8418 else if (in_decl
8419 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8420 || concept_template_p (in_decl))
8421 && variadic_args_p
8422 && nargs - variadic_args_p < nparms - variadic_p)
8423 {
8424 if (complain & tf_error)
8425 {
8426 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8427 {
8428 tree arg = TREE_VEC_ELT (inner_args, i);
8429 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8430
8431 if (PACK_EXPANSION_P (arg)
8432 && !template_parameter_pack_p (parm))
8433 {
8434 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8435 error_at (location_of (arg),
8436 "pack expansion argument for non-pack parameter "
8437 "%qD of alias template %qD", parm, in_decl);
8438 else
8439 error_at (location_of (arg),
8440 "pack expansion argument for non-pack parameter "
8441 "%qD of concept %qD", parm, in_decl);
8442 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8443 goto found;
8444 }
8445 }
8446 gcc_unreachable ();
8447 found:;
8448 }
8449 return error_mark_node;
8450 }
8451
8452 /* We need to evaluate the template arguments, even though this
8453 template-id may be nested within a "sizeof". */
8454 cp_evaluated ev;
8455
8456 new_inner_args = make_tree_vec (nparms);
8457 new_args = add_outermost_template_args (args, new_inner_args);
8458 int pack_adjust = 0;
8459 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8460 {
8461 tree arg;
8462 tree parm;
8463
8464 /* Get the Ith template parameter. */
8465 parm = TREE_VEC_ELT (parms, parm_idx);
8466
8467 if (parm == error_mark_node)
8468 {
8469 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8470 continue;
8471 }
8472
8473 /* Calculate the next argument. */
8474 if (arg_idx < nargs)
8475 arg = TREE_VEC_ELT (inner_args, arg_idx);
8476 else
8477 arg = NULL_TREE;
8478
8479 if (template_parameter_pack_p (TREE_VALUE (parm))
8480 && (arg || require_all_args || !(complain & tf_partial))
8481 && !(arg && ARGUMENT_PACK_P (arg)))
8482 {
8483 /* Some arguments will be placed in the
8484 template parameter pack PARM. */
8485 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8486 inner_args, arg_idx,
8487 new_args, &lost,
8488 in_decl, complain);
8489
8490 if (arg == NULL_TREE)
8491 {
8492 /* We don't know how many args we have yet, just use the
8493 unconverted (and still packed) ones for now. */
8494 new_inner_args = orig_inner_args;
8495 arg_idx = nargs;
8496 break;
8497 }
8498
8499 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8500
8501 /* Store this argument. */
8502 if (arg == error_mark_node)
8503 {
8504 lost++;
8505 /* We are done with all of the arguments. */
8506 arg_idx = nargs;
8507 break;
8508 }
8509 else
8510 {
8511 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8512 arg_idx += pack_adjust;
8513 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8514 {
8515 ++fixed_packs;
8516 fixed_pack_adjust += pack_adjust;
8517 }
8518 }
8519
8520 continue;
8521 }
8522 else if (arg)
8523 {
8524 if (PACK_EXPANSION_P (arg))
8525 {
8526 /* "If every valid specialization of a variadic template
8527 requires an empty template parameter pack, the template is
8528 ill-formed, no diagnostic required." So check that the
8529 pattern works with this parameter. */
8530 tree pattern = PACK_EXPANSION_PATTERN (arg);
8531 tree conv = convert_template_argument (TREE_VALUE (parm),
8532 pattern, new_args,
8533 complain, parm_idx,
8534 in_decl);
8535 if (conv == error_mark_node)
8536 {
8537 if (complain & tf_error)
8538 inform (input_location, "so any instantiation with a "
8539 "non-empty parameter pack would be ill-formed");
8540 ++lost;
8541 }
8542 else if (TYPE_P (conv) && !TYPE_P (pattern))
8543 /* Recover from missing typename. */
8544 TREE_VEC_ELT (inner_args, arg_idx)
8545 = make_pack_expansion (conv, complain);
8546
8547 /* We don't know how many args we have yet, just
8548 use the unconverted ones for now. */
8549 new_inner_args = inner_args;
8550 arg_idx = nargs;
8551 break;
8552 }
8553 }
8554 else if (require_all_args)
8555 {
8556 /* There must be a default arg in this case. */
8557 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8558 complain, in_decl);
8559 /* The position of the first default template argument,
8560 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8561 Record that. */
8562 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8563 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8564 arg_idx - pack_adjust);
8565 }
8566 else
8567 break;
8568
8569 if (arg == error_mark_node)
8570 {
8571 if (complain & tf_error)
8572 error ("template argument %d is invalid", arg_idx + 1);
8573 }
8574 else if (!arg)
8575 {
8576 /* This can occur if there was an error in the template
8577 parameter list itself (which we would already have
8578 reported) that we are trying to recover from, e.g., a class
8579 template with a parameter list such as
8580 template<typename..., typename> (cpp0x/variadic150.C). */
8581 ++lost;
8582
8583 /* This can also happen with a fixed parameter pack (71834). */
8584 if (arg_idx >= nargs)
8585 ++missing;
8586 }
8587 else
8588 arg = convert_template_argument (TREE_VALUE (parm),
8589 arg, new_args, complain,
8590 parm_idx, in_decl);
8591
8592 if (arg == error_mark_node)
8593 lost++;
8594 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8595 }
8596
8597 if (missing || arg_idx < nargs - variadic_args_p)
8598 {
8599 /* If we had fixed parameter packs, we didn't know how many arguments we
8600 actually needed earlier; now we do. */
8601 nparms += fixed_pack_adjust;
8602 variadic_p -= fixed_packs;
8603 goto bad_nargs;
8604 }
8605
8606 if (arg_idx < nargs)
8607 {
8608 /* We had some pack expansion arguments that will only work if the packs
8609 are empty, but wait until instantiation time to complain.
8610 See variadic-ttp3.C. */
8611 int len = nparms + (nargs - arg_idx);
8612 tree args = make_tree_vec (len);
8613 int i = 0;
8614 for (; i < nparms; ++i)
8615 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8616 for (; i < len; ++i, ++arg_idx)
8617 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8618 arg_idx - pack_adjust);
8619 new_inner_args = args;
8620 }
8621
8622 if (lost)
8623 {
8624 gcc_assert (!(complain & tf_error) || seen_error ());
8625 return error_mark_node;
8626 }
8627
8628 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8629 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8630 TREE_VEC_LENGTH (new_inner_args));
8631
8632 return new_inner_args;
8633 }
8634
8635 /* Convert all template arguments to their appropriate types, and
8636 return a vector containing the innermost resulting template
8637 arguments. If any error occurs, return error_mark_node. Error and
8638 warning messages are not issued.
8639
8640 Note that no function argument deduction is performed, and default
8641 arguments are used to fill in unspecified arguments. */
8642 tree
8643 coerce_template_parms (tree parms, tree args, tree in_decl)
8644 {
8645 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8646 }
8647
8648 /* Convert all template arguments to their appropriate type, and
8649 instantiate default arguments as needed. This returns a vector
8650 containing the innermost resulting template arguments, or
8651 error_mark_node if unsuccessful. */
8652 tree
8653 coerce_template_parms (tree parms, tree args, tree in_decl,
8654 tsubst_flags_t complain)
8655 {
8656 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8657 }
8658
8659 /* Like coerce_template_parms. If PARMS represents all template
8660 parameters levels, this function returns a vector of vectors
8661 representing all the resulting argument levels. Note that in this
8662 case, only the innermost arguments are coerced because the
8663 outermost ones are supposed to have been coerced already.
8664
8665 Otherwise, if PARMS represents only (the innermost) vector of
8666 parameters, this function returns a vector containing just the
8667 innermost resulting arguments. */
8668
8669 static tree
8670 coerce_innermost_template_parms (tree parms,
8671 tree args,
8672 tree in_decl,
8673 tsubst_flags_t complain,
8674 bool require_all_args,
8675 bool use_default_args)
8676 {
8677 int parms_depth = TMPL_PARMS_DEPTH (parms);
8678 int args_depth = TMPL_ARGS_DEPTH (args);
8679 tree coerced_args;
8680
8681 if (parms_depth > 1)
8682 {
8683 coerced_args = make_tree_vec (parms_depth);
8684 tree level;
8685 int cur_depth;
8686
8687 for (level = parms, cur_depth = parms_depth;
8688 parms_depth > 0 && level != NULL_TREE;
8689 level = TREE_CHAIN (level), --cur_depth)
8690 {
8691 tree l;
8692 if (cur_depth == args_depth)
8693 l = coerce_template_parms (TREE_VALUE (level),
8694 args, in_decl, complain,
8695 require_all_args,
8696 use_default_args);
8697 else
8698 l = TMPL_ARGS_LEVEL (args, cur_depth);
8699
8700 if (l == error_mark_node)
8701 return error_mark_node;
8702
8703 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8704 }
8705 }
8706 else
8707 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8708 args, in_decl, complain,
8709 require_all_args,
8710 use_default_args);
8711 return coerced_args;
8712 }
8713
8714 /* Returns 1 if template args OT and NT are equivalent. */
8715
8716 int
8717 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8718 {
8719 if (nt == ot)
8720 return 1;
8721 if (nt == NULL_TREE || ot == NULL_TREE)
8722 return false;
8723 if (nt == any_targ_node || ot == any_targ_node)
8724 return true;
8725
8726 if (TREE_CODE (nt) == TREE_VEC)
8727 /* For member templates */
8728 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8729 else if (PACK_EXPANSION_P (ot))
8730 return (PACK_EXPANSION_P (nt)
8731 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8732 PACK_EXPANSION_PATTERN (nt))
8733 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8734 PACK_EXPANSION_EXTRA_ARGS (nt)));
8735 else if (ARGUMENT_PACK_P (ot))
8736 {
8737 int i, len;
8738 tree opack, npack;
8739
8740 if (!ARGUMENT_PACK_P (nt))
8741 return 0;
8742
8743 opack = ARGUMENT_PACK_ARGS (ot);
8744 npack = ARGUMENT_PACK_ARGS (nt);
8745 len = TREE_VEC_LENGTH (opack);
8746 if (TREE_VEC_LENGTH (npack) != len)
8747 return 0;
8748 for (i = 0; i < len; ++i)
8749 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8750 TREE_VEC_ELT (npack, i)))
8751 return 0;
8752 return 1;
8753 }
8754 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8755 gcc_unreachable ();
8756 else if (TYPE_P (nt))
8757 {
8758 if (!TYPE_P (ot))
8759 return false;
8760 /* Don't treat an alias template specialization with dependent
8761 arguments as equivalent to its underlying type when used as a
8762 template argument; we need them to be distinct so that we
8763 substitute into the specialization arguments at instantiation
8764 time. And aliases can't be equivalent without being ==, so
8765 we don't need to look any deeper.
8766
8767 During partial ordering, however, we need to treat them normally so
8768 that we can order uses of the same alias with different
8769 cv-qualification (79960). */
8770 if (!partial_order
8771 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8772 return false;
8773 else
8774 return same_type_p (ot, nt);
8775 }
8776 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8777 return 0;
8778 else
8779 {
8780 /* Try to treat a template non-type argument that has been converted
8781 to the parameter type as equivalent to one that hasn't yet. */
8782 for (enum tree_code code1 = TREE_CODE (ot);
8783 CONVERT_EXPR_CODE_P (code1)
8784 || code1 == NON_LVALUE_EXPR;
8785 code1 = TREE_CODE (ot))
8786 ot = TREE_OPERAND (ot, 0);
8787 for (enum tree_code code2 = TREE_CODE (nt);
8788 CONVERT_EXPR_CODE_P (code2)
8789 || code2 == NON_LVALUE_EXPR;
8790 code2 = TREE_CODE (nt))
8791 nt = TREE_OPERAND (nt, 0);
8792
8793 return cp_tree_equal (ot, nt);
8794 }
8795 }
8796
8797 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8798 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8799 NEWARG_PTR with the offending arguments if they are non-NULL. */
8800
8801 int
8802 comp_template_args (tree oldargs, tree newargs,
8803 tree *oldarg_ptr, tree *newarg_ptr,
8804 bool partial_order)
8805 {
8806 int i;
8807
8808 if (oldargs == newargs)
8809 return 1;
8810
8811 if (!oldargs || !newargs)
8812 return 0;
8813
8814 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8815 return 0;
8816
8817 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8818 {
8819 tree nt = TREE_VEC_ELT (newargs, i);
8820 tree ot = TREE_VEC_ELT (oldargs, i);
8821
8822 if (! template_args_equal (ot, nt, partial_order))
8823 {
8824 if (oldarg_ptr != NULL)
8825 *oldarg_ptr = ot;
8826 if (newarg_ptr != NULL)
8827 *newarg_ptr = nt;
8828 return 0;
8829 }
8830 }
8831 return 1;
8832 }
8833
8834 inline bool
8835 comp_template_args_porder (tree oargs, tree nargs)
8836 {
8837 return comp_template_args (oargs, nargs, NULL, NULL, true);
8838 }
8839
8840 /* Implement a freelist interface for objects of type T.
8841
8842 Head is a separate object, rather than a regular member, so that we
8843 can define it as a GTY deletable pointer, which is highly
8844 desirable. A data member could be declared that way, but then the
8845 containing object would implicitly get GTY((user)), which would
8846 prevent us from instantiating freelists as global objects.
8847 Although this way we can create freelist global objects, they're
8848 such thin wrappers that instantiating temporaries at every use
8849 loses nothing and saves permanent storage for the freelist object.
8850
8851 Member functions next, anew, poison and reinit have default
8852 implementations that work for most of the types we're interested
8853 in, but if they don't work for some type, they should be explicitly
8854 specialized. See the comments before them for requirements, and
8855 the example specializations for the tree_list_freelist. */
8856 template <typename T>
8857 class freelist
8858 {
8859 /* Return the next object in a chain. We could just do type
8860 punning, but if we access the object with its underlying type, we
8861 avoid strict-aliasing trouble. This needs only work between
8862 poison and reinit. */
8863 static T *&next (T *obj) { return obj->next; }
8864
8865 /* Return a newly allocated, uninitialized or minimally-initialized
8866 object of type T. Any initialization performed by anew should
8867 either remain across the life of the object and the execution of
8868 poison, or be redone by reinit. */
8869 static T *anew () { return ggc_alloc<T> (); }
8870
8871 /* Optionally scribble all over the bits holding the object, so that
8872 they become (mostly?) uninitialized memory. This is called while
8873 preparing to make the object part of the free list. */
8874 static void poison (T *obj) {
8875 T *p ATTRIBUTE_UNUSED = obj;
8876 T **q ATTRIBUTE_UNUSED = &next (obj);
8877
8878 #ifdef ENABLE_GC_CHECKING
8879 /* Poison the data, to indicate the data is garbage. */
8880 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8881 memset (p, 0xa5, sizeof (*p));
8882 #endif
8883 /* Let valgrind know the object is free. */
8884 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8885
8886 /* Let valgrind know the next portion of the object is available,
8887 but uninitialized. */
8888 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8889 }
8890
8891 /* Bring an object that underwent at least one lifecycle after anew
8892 and before the most recent free and poison, back to a usable
8893 state, reinitializing whatever is needed for it to be
8894 functionally equivalent to an object just allocated and returned
8895 by anew. This may poison or clear the next field, used by
8896 freelist housekeeping after poison was called. */
8897 static void reinit (T *obj) {
8898 T **q ATTRIBUTE_UNUSED = &next (obj);
8899
8900 #ifdef ENABLE_GC_CHECKING
8901 memset (q, 0xa5, sizeof (*q));
8902 #endif
8903 /* Let valgrind know the entire object is available, but
8904 uninitialized. */
8905 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8906 }
8907
8908 /* Reference a GTY-deletable pointer that points to the first object
8909 in the free list proper. */
8910 T *&head;
8911 public:
8912 /* Construct a freelist object chaining objects off of HEAD. */
8913 freelist (T *&head) : head(head) {}
8914
8915 /* Add OBJ to the free object list. The former head becomes OBJ's
8916 successor. */
8917 void free (T *obj)
8918 {
8919 poison (obj);
8920 next (obj) = head;
8921 head = obj;
8922 }
8923
8924 /* Take an object from the free list, if one is available, or
8925 allocate a new one. Objects taken from the free list should be
8926 regarded as filled with garbage, except for bits that are
8927 configured to be preserved across free and alloc. */
8928 T *alloc ()
8929 {
8930 if (head)
8931 {
8932 T *obj = head;
8933 head = next (head);
8934 reinit (obj);
8935 return obj;
8936 }
8937 else
8938 return anew ();
8939 }
8940 };
8941
8942 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8943 want to allocate a TREE_LIST using the usual interface, and ensure
8944 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8945 build_tree_list logic in reinit, so this could go out of sync. */
8946 template <>
8947 inline tree &
8948 freelist<tree_node>::next (tree obj)
8949 {
8950 return TREE_CHAIN (obj);
8951 }
8952 template <>
8953 inline tree
8954 freelist<tree_node>::anew ()
8955 {
8956 return build_tree_list (NULL, NULL);
8957 }
8958 template <>
8959 inline void
8960 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8961 {
8962 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8963 tree p ATTRIBUTE_UNUSED = obj;
8964 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8965 tree *q ATTRIBUTE_UNUSED = &next (obj);
8966
8967 #ifdef ENABLE_GC_CHECKING
8968 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8969
8970 /* Poison the data, to indicate the data is garbage. */
8971 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8972 memset (p, 0xa5, size);
8973 #endif
8974 /* Let valgrind know the object is free. */
8975 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8976 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8977 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8978 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8979
8980 #ifdef ENABLE_GC_CHECKING
8981 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8982 /* Keep TREE_CHAIN functional. */
8983 TREE_SET_CODE (obj, TREE_LIST);
8984 #else
8985 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8986 #endif
8987 }
8988 template <>
8989 inline void
8990 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
8991 {
8992 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8993
8994 #ifdef ENABLE_GC_CHECKING
8995 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8996 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8997 memset (obj, 0, sizeof (tree_list));
8998 #endif
8999
9000 /* Let valgrind know the entire object is available, but
9001 uninitialized. */
9002 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9003
9004 #ifdef ENABLE_GC_CHECKING
9005 TREE_SET_CODE (obj, TREE_LIST);
9006 #else
9007 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9008 #endif
9009 }
9010
9011 /* Point to the first object in the TREE_LIST freelist. */
9012 static GTY((deletable)) tree tree_list_freelist_head;
9013 /* Return the/an actual TREE_LIST freelist. */
9014 static inline freelist<tree_node>
9015 tree_list_freelist ()
9016 {
9017 return tree_list_freelist_head;
9018 }
9019
9020 /* Point to the first object in the tinst_level freelist. */
9021 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9022 /* Return the/an actual tinst_level freelist. */
9023 static inline freelist<tinst_level>
9024 tinst_level_freelist ()
9025 {
9026 return tinst_level_freelist_head;
9027 }
9028
9029 /* Point to the first object in the pending_template freelist. */
9030 static GTY((deletable)) pending_template *pending_template_freelist_head;
9031 /* Return the/an actual pending_template freelist. */
9032 static inline freelist<pending_template>
9033 pending_template_freelist ()
9034 {
9035 return pending_template_freelist_head;
9036 }
9037
9038 /* Build the TREE_LIST object out of a split list, store it
9039 permanently, and return it. */
9040 tree
9041 tinst_level::to_list ()
9042 {
9043 gcc_assert (split_list_p ());
9044 tree ret = tree_list_freelist ().alloc ();
9045 TREE_PURPOSE (ret) = tldcl;
9046 TREE_VALUE (ret) = targs;
9047 tldcl = ret;
9048 targs = NULL;
9049 gcc_assert (tree_list_p ());
9050 return ret;
9051 }
9052
9053 const unsigned short tinst_level::refcount_infinity;
9054
9055 /* Increment OBJ's refcount unless it is already infinite. */
9056 static tinst_level *
9057 inc_refcount_use (tinst_level *obj)
9058 {
9059 if (obj && obj->refcount != tinst_level::refcount_infinity)
9060 ++obj->refcount;
9061 return obj;
9062 }
9063
9064 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9065 void
9066 tinst_level::free (tinst_level *obj)
9067 {
9068 if (obj->tree_list_p ())
9069 tree_list_freelist ().free (obj->get_node ());
9070 tinst_level_freelist ().free (obj);
9071 }
9072
9073 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9074 OBJ's DECL and OBJ, and start over with the tinst_level object that
9075 used to be referenced by OBJ's NEXT. */
9076 static void
9077 dec_refcount_use (tinst_level *obj)
9078 {
9079 while (obj
9080 && obj->refcount != tinst_level::refcount_infinity
9081 && !--obj->refcount)
9082 {
9083 tinst_level *next = obj->next;
9084 tinst_level::free (obj);
9085 obj = next;
9086 }
9087 }
9088
9089 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9090 and of the former PTR. Omitting the second argument is equivalent
9091 to passing (T*)NULL; this is allowed because passing the
9092 zero-valued integral constant NULL confuses type deduction and/or
9093 overload resolution. */
9094 template <typename T>
9095 static void
9096 set_refcount_ptr (T *& ptr, T *obj = NULL)
9097 {
9098 T *save = ptr;
9099 ptr = inc_refcount_use (obj);
9100 dec_refcount_use (save);
9101 }
9102
9103 static void
9104 add_pending_template (tree d)
9105 {
9106 tree ti = (TYPE_P (d)
9107 ? CLASSTYPE_TEMPLATE_INFO (d)
9108 : DECL_TEMPLATE_INFO (d));
9109 struct pending_template *pt;
9110 int level;
9111
9112 if (TI_PENDING_TEMPLATE_FLAG (ti))
9113 return;
9114
9115 /* We are called both from instantiate_decl, where we've already had a
9116 tinst_level pushed, and instantiate_template, where we haven't.
9117 Compensate. */
9118 gcc_assert (TREE_CODE (d) != TREE_LIST);
9119 level = !current_tinst_level
9120 || current_tinst_level->maybe_get_node () != d;
9121
9122 if (level)
9123 push_tinst_level (d);
9124
9125 pt = pending_template_freelist ().alloc ();
9126 pt->next = NULL;
9127 pt->tinst = NULL;
9128 set_refcount_ptr (pt->tinst, current_tinst_level);
9129 if (last_pending_template)
9130 last_pending_template->next = pt;
9131 else
9132 pending_templates = pt;
9133
9134 last_pending_template = pt;
9135
9136 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9137
9138 if (level)
9139 pop_tinst_level ();
9140 }
9141
9142
9143 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9144 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9145 documentation for TEMPLATE_ID_EXPR. */
9146
9147 tree
9148 lookup_template_function (tree fns, tree arglist)
9149 {
9150 if (fns == error_mark_node || arglist == error_mark_node)
9151 return error_mark_node;
9152
9153 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9154
9155 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9156 {
9157 error ("%q#D is not a function template", fns);
9158 return error_mark_node;
9159 }
9160
9161 if (BASELINK_P (fns))
9162 {
9163 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9164 unknown_type_node,
9165 BASELINK_FUNCTIONS (fns),
9166 arglist);
9167 return fns;
9168 }
9169
9170 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9171 }
9172
9173 /* Within the scope of a template class S<T>, the name S gets bound
9174 (in build_self_reference) to a TYPE_DECL for the class, not a
9175 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9176 or one of its enclosing classes, and that type is a template,
9177 return the associated TEMPLATE_DECL. Otherwise, the original
9178 DECL is returned.
9179
9180 Also handle the case when DECL is a TREE_LIST of ambiguous
9181 injected-class-names from different bases. */
9182
9183 tree
9184 maybe_get_template_decl_from_type_decl (tree decl)
9185 {
9186 if (decl == NULL_TREE)
9187 return decl;
9188
9189 /* DR 176: A lookup that finds an injected-class-name (10.2
9190 [class.member.lookup]) can result in an ambiguity in certain cases
9191 (for example, if it is found in more than one base class). If all of
9192 the injected-class-names that are found refer to specializations of
9193 the same class template, and if the name is followed by a
9194 template-argument-list, the reference refers to the class template
9195 itself and not a specialization thereof, and is not ambiguous. */
9196 if (TREE_CODE (decl) == TREE_LIST)
9197 {
9198 tree t, tmpl = NULL_TREE;
9199 for (t = decl; t; t = TREE_CHAIN (t))
9200 {
9201 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9202 if (!tmpl)
9203 tmpl = elt;
9204 else if (tmpl != elt)
9205 break;
9206 }
9207 if (tmpl && t == NULL_TREE)
9208 return tmpl;
9209 else
9210 return decl;
9211 }
9212
9213 return (decl != NULL_TREE
9214 && DECL_SELF_REFERENCE_P (decl)
9215 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9216 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9217 }
9218
9219 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9220 parameters, find the desired type.
9221
9222 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9223
9224 IN_DECL, if non-NULL, is the template declaration we are trying to
9225 instantiate.
9226
9227 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9228 the class we are looking up.
9229
9230 Issue error and warning messages under control of COMPLAIN.
9231
9232 If the template class is really a local class in a template
9233 function, then the FUNCTION_CONTEXT is the function in which it is
9234 being instantiated.
9235
9236 ??? Note that this function is currently called *twice* for each
9237 template-id: the first time from the parser, while creating the
9238 incomplete type (finish_template_type), and the second type during the
9239 real instantiation (instantiate_template_class). This is surely something
9240 that we want to avoid. It also causes some problems with argument
9241 coercion (see convert_nontype_argument for more information on this). */
9242
9243 static tree
9244 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9245 int entering_scope, tsubst_flags_t complain)
9246 {
9247 tree templ = NULL_TREE, parmlist;
9248 tree t;
9249 spec_entry **slot;
9250 spec_entry *entry;
9251 spec_entry elt;
9252 hashval_t hash;
9253
9254 if (identifier_p (d1))
9255 {
9256 tree value = innermost_non_namespace_value (d1);
9257 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9258 templ = value;
9259 else
9260 {
9261 if (context)
9262 push_decl_namespace (context);
9263 templ = lookup_name (d1);
9264 templ = maybe_get_template_decl_from_type_decl (templ);
9265 if (context)
9266 pop_decl_namespace ();
9267 }
9268 if (templ)
9269 context = DECL_CONTEXT (templ);
9270 }
9271 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9272 {
9273 tree type = TREE_TYPE (d1);
9274
9275 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9276 an implicit typename for the second A. Deal with it. */
9277 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9278 type = TREE_TYPE (type);
9279
9280 if (CLASSTYPE_TEMPLATE_INFO (type))
9281 {
9282 templ = CLASSTYPE_TI_TEMPLATE (type);
9283 d1 = DECL_NAME (templ);
9284 }
9285 }
9286 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9287 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9288 {
9289 templ = TYPE_TI_TEMPLATE (d1);
9290 d1 = DECL_NAME (templ);
9291 }
9292 else if (DECL_TYPE_TEMPLATE_P (d1))
9293 {
9294 templ = d1;
9295 d1 = DECL_NAME (templ);
9296 context = DECL_CONTEXT (templ);
9297 }
9298 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9299 {
9300 templ = d1;
9301 d1 = DECL_NAME (templ);
9302 }
9303
9304 /* Issue an error message if we didn't find a template. */
9305 if (! templ)
9306 {
9307 if (complain & tf_error)
9308 error ("%qT is not a template", d1);
9309 return error_mark_node;
9310 }
9311
9312 if (TREE_CODE (templ) != TEMPLATE_DECL
9313 /* Make sure it's a user visible template, if it was named by
9314 the user. */
9315 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9316 && !PRIMARY_TEMPLATE_P (templ)))
9317 {
9318 if (complain & tf_error)
9319 {
9320 error ("non-template type %qT used as a template", d1);
9321 if (in_decl)
9322 error ("for template declaration %q+D", in_decl);
9323 }
9324 return error_mark_node;
9325 }
9326
9327 complain &= ~tf_user;
9328
9329 /* An alias that just changes the name of a template is equivalent to the
9330 other template, so if any of the arguments are pack expansions, strip
9331 the alias to avoid problems with a pack expansion passed to a non-pack
9332 alias template parameter (DR 1430). */
9333 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9334 templ = get_underlying_template (templ);
9335
9336 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9337 {
9338 tree parm;
9339 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9340 if (arglist2 == error_mark_node
9341 || (!uses_template_parms (arglist2)
9342 && check_instantiated_args (templ, arglist2, complain)))
9343 return error_mark_node;
9344
9345 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9346 return parm;
9347 }
9348 else
9349 {
9350 tree template_type = TREE_TYPE (templ);
9351 tree gen_tmpl;
9352 tree type_decl;
9353 tree found = NULL_TREE;
9354 int arg_depth;
9355 int parm_depth;
9356 int is_dependent_type;
9357 int use_partial_inst_tmpl = false;
9358
9359 if (template_type == error_mark_node)
9360 /* An error occurred while building the template TEMPL, and a
9361 diagnostic has most certainly been emitted for that
9362 already. Let's propagate that error. */
9363 return error_mark_node;
9364
9365 gen_tmpl = most_general_template (templ);
9366 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9367 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9368 arg_depth = TMPL_ARGS_DEPTH (arglist);
9369
9370 if (arg_depth == 1 && parm_depth > 1)
9371 {
9372 /* We've been given an incomplete set of template arguments.
9373 For example, given:
9374
9375 template <class T> struct S1 {
9376 template <class U> struct S2 {};
9377 template <class U> struct S2<U*> {};
9378 };
9379
9380 we will be called with an ARGLIST of `U*', but the
9381 TEMPLATE will be `template <class T> template
9382 <class U> struct S1<T>::S2'. We must fill in the missing
9383 arguments. */
9384 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9385 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9386 arg_depth = TMPL_ARGS_DEPTH (arglist);
9387 }
9388
9389 /* Now we should have enough arguments. */
9390 gcc_assert (parm_depth == arg_depth);
9391
9392 /* From here on, we're only interested in the most general
9393 template. */
9394
9395 /* Calculate the BOUND_ARGS. These will be the args that are
9396 actually tsubst'd into the definition to create the
9397 instantiation. */
9398 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9399 complain,
9400 /*require_all_args=*/true,
9401 /*use_default_args=*/true);
9402
9403 if (arglist == error_mark_node)
9404 /* We were unable to bind the arguments. */
9405 return error_mark_node;
9406
9407 /* In the scope of a template class, explicit references to the
9408 template class refer to the type of the template, not any
9409 instantiation of it. For example, in:
9410
9411 template <class T> class C { void f(C<T>); }
9412
9413 the `C<T>' is just the same as `C'. Outside of the
9414 class, however, such a reference is an instantiation. */
9415 if (entering_scope
9416 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9417 || currently_open_class (template_type))
9418 {
9419 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9420
9421 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9422 return template_type;
9423 }
9424
9425 /* If we already have this specialization, return it. */
9426 elt.tmpl = gen_tmpl;
9427 elt.args = arglist;
9428 elt.spec = NULL_TREE;
9429 hash = spec_hasher::hash (&elt);
9430 entry = type_specializations->find_with_hash (&elt, hash);
9431
9432 if (entry)
9433 return entry->spec;
9434
9435 /* If the the template's constraints are not satisfied,
9436 then we cannot form a valid type.
9437
9438 Note that the check is deferred until after the hash
9439 lookup. This prevents redundant checks on previously
9440 instantiated specializations. */
9441 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9442 {
9443 if (complain & tf_error)
9444 {
9445 auto_diagnostic_group d;
9446 error ("template constraint failure");
9447 diagnose_constraints (input_location, gen_tmpl, arglist);
9448 }
9449 return error_mark_node;
9450 }
9451
9452 is_dependent_type = uses_template_parms (arglist);
9453
9454 /* If the deduced arguments are invalid, then the binding
9455 failed. */
9456 if (!is_dependent_type
9457 && check_instantiated_args (gen_tmpl,
9458 INNERMOST_TEMPLATE_ARGS (arglist),
9459 complain))
9460 return error_mark_node;
9461
9462 if (!is_dependent_type
9463 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9464 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9465 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9466 {
9467 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9468 DECL_NAME (gen_tmpl),
9469 /*tag_scope=*/ts_global);
9470 return found;
9471 }
9472
9473 context = DECL_CONTEXT (gen_tmpl);
9474 if (context && TYPE_P (context))
9475 {
9476 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9477 context = complete_type (context);
9478 }
9479 else
9480 context = tsubst (context, arglist, complain, in_decl);
9481
9482 if (context == error_mark_node)
9483 return error_mark_node;
9484
9485 if (!context)
9486 context = global_namespace;
9487
9488 /* Create the type. */
9489 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9490 {
9491 /* The user referred to a specialization of an alias
9492 template represented by GEN_TMPL.
9493
9494 [temp.alias]/2 says:
9495
9496 When a template-id refers to the specialization of an
9497 alias template, it is equivalent to the associated
9498 type obtained by substitution of its
9499 template-arguments for the template-parameters in the
9500 type-id of the alias template. */
9501
9502 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9503 /* Note that the call above (by indirectly calling
9504 register_specialization in tsubst_decl) registers the
9505 TYPE_DECL representing the specialization of the alias
9506 template. So next time someone substitutes ARGLIST for
9507 the template parms into the alias template (GEN_TMPL),
9508 she'll get that TYPE_DECL back. */
9509
9510 if (t == error_mark_node)
9511 return t;
9512 }
9513 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9514 {
9515 if (!is_dependent_type)
9516 {
9517 set_current_access_from_decl (TYPE_NAME (template_type));
9518 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9519 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9520 arglist, complain, in_decl),
9521 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9522 arglist, complain, in_decl),
9523 SCOPED_ENUM_P (template_type), NULL);
9524
9525 if (t == error_mark_node)
9526 return t;
9527 }
9528 else
9529 {
9530 /* We don't want to call start_enum for this type, since
9531 the values for the enumeration constants may involve
9532 template parameters. And, no one should be interested
9533 in the enumeration constants for such a type. */
9534 t = cxx_make_type (ENUMERAL_TYPE);
9535 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9536 }
9537 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9538 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9539 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9540 }
9541 else if (CLASS_TYPE_P (template_type))
9542 {
9543 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9544 instantiated here. */
9545 gcc_assert (!LAMBDA_TYPE_P (template_type));
9546
9547 t = make_class_type (TREE_CODE (template_type));
9548 CLASSTYPE_DECLARED_CLASS (t)
9549 = CLASSTYPE_DECLARED_CLASS (template_type);
9550 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9551
9552 /* A local class. Make sure the decl gets registered properly. */
9553 if (context == current_function_decl)
9554 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9555 == error_mark_node)
9556 return error_mark_node;
9557
9558 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9559 /* This instantiation is another name for the primary
9560 template type. Set the TYPE_CANONICAL field
9561 appropriately. */
9562 TYPE_CANONICAL (t) = template_type;
9563 else if (any_template_arguments_need_structural_equality_p (arglist))
9564 /* Some of the template arguments require structural
9565 equality testing, so this template class requires
9566 structural equality testing. */
9567 SET_TYPE_STRUCTURAL_EQUALITY (t);
9568 }
9569 else
9570 gcc_unreachable ();
9571
9572 /* If we called start_enum or pushtag above, this information
9573 will already be set up. */
9574 if (!TYPE_NAME (t))
9575 {
9576 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9577
9578 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9579 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9580 DECL_SOURCE_LOCATION (type_decl)
9581 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9582 }
9583 else
9584 type_decl = TYPE_NAME (t);
9585
9586 if (CLASS_TYPE_P (template_type))
9587 {
9588 TREE_PRIVATE (type_decl)
9589 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9590 TREE_PROTECTED (type_decl)
9591 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9592 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9593 {
9594 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9595 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9596 }
9597 }
9598
9599 if (OVERLOAD_TYPE_P (t)
9600 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9601 {
9602 static const char *tags[] = {"abi_tag", "may_alias"};
9603
9604 for (unsigned ix = 0; ix != 2; ix++)
9605 {
9606 tree attributes
9607 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9608
9609 if (attributes)
9610 TYPE_ATTRIBUTES (t)
9611 = tree_cons (TREE_PURPOSE (attributes),
9612 TREE_VALUE (attributes),
9613 TYPE_ATTRIBUTES (t));
9614 }
9615 }
9616
9617 /* Let's consider the explicit specialization of a member
9618 of a class template specialization that is implicitly instantiated,
9619 e.g.:
9620 template<class T>
9621 struct S
9622 {
9623 template<class U> struct M {}; //#0
9624 };
9625
9626 template<>
9627 template<>
9628 struct S<int>::M<char> //#1
9629 {
9630 int i;
9631 };
9632 [temp.expl.spec]/4 says this is valid.
9633
9634 In this case, when we write:
9635 S<int>::M<char> m;
9636
9637 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9638 the one of #0.
9639
9640 When we encounter #1, we want to store the partial instantiation
9641 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9642
9643 For all cases other than this "explicit specialization of member of a
9644 class template", we just want to store the most general template into
9645 the CLASSTYPE_TI_TEMPLATE of M.
9646
9647 This case of "explicit specialization of member of a class template"
9648 only happens when:
9649 1/ the enclosing class is an instantiation of, and therefore not
9650 the same as, the context of the most general template, and
9651 2/ we aren't looking at the partial instantiation itself, i.e.
9652 the innermost arguments are not the same as the innermost parms of
9653 the most general template.
9654
9655 So it's only when 1/ and 2/ happens that we want to use the partial
9656 instantiation of the member template in lieu of its most general
9657 template. */
9658
9659 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9660 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9661 /* the enclosing class must be an instantiation... */
9662 && CLASS_TYPE_P (context)
9663 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9664 {
9665 TREE_VEC_LENGTH (arglist)--;
9666 ++processing_template_decl;
9667 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9668 tree partial_inst_args =
9669 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9670 arglist, complain, NULL_TREE);
9671 --processing_template_decl;
9672 TREE_VEC_LENGTH (arglist)++;
9673 if (partial_inst_args == error_mark_node)
9674 return error_mark_node;
9675 use_partial_inst_tmpl =
9676 /*...and we must not be looking at the partial instantiation
9677 itself. */
9678 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9679 partial_inst_args);
9680 }
9681
9682 if (!use_partial_inst_tmpl)
9683 /* This case is easy; there are no member templates involved. */
9684 found = gen_tmpl;
9685 else
9686 {
9687 /* This is a full instantiation of a member template. Find
9688 the partial instantiation of which this is an instance. */
9689
9690 /* Temporarily reduce by one the number of levels in the ARGLIST
9691 so as to avoid comparing the last set of arguments. */
9692 TREE_VEC_LENGTH (arglist)--;
9693 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9694 TREE_VEC_LENGTH (arglist)++;
9695 /* FOUND is either a proper class type, or an alias
9696 template specialization. In the later case, it's a
9697 TYPE_DECL, resulting from the substituting of arguments
9698 for parameters in the TYPE_DECL of the alias template
9699 done earlier. So be careful while getting the template
9700 of FOUND. */
9701 found = (TREE_CODE (found) == TEMPLATE_DECL
9702 ? found
9703 : (TREE_CODE (found) == TYPE_DECL
9704 ? DECL_TI_TEMPLATE (found)
9705 : CLASSTYPE_TI_TEMPLATE (found)));
9706
9707 if (DECL_CLASS_TEMPLATE_P (found)
9708 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9709 {
9710 /* If this partial instantiation is specialized, we want to
9711 use it for hash table lookup. */
9712 elt.tmpl = found;
9713 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9714 hash = spec_hasher::hash (&elt);
9715 }
9716 }
9717
9718 // Build template info for the new specialization.
9719 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9720
9721 elt.spec = t;
9722 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9723 gcc_checking_assert (*slot == NULL);
9724 entry = ggc_alloc<spec_entry> ();
9725 *entry = elt;
9726 *slot = entry;
9727
9728 /* Note this use of the partial instantiation so we can check it
9729 later in maybe_process_partial_specialization. */
9730 DECL_TEMPLATE_INSTANTIATIONS (found)
9731 = tree_cons (arglist, t,
9732 DECL_TEMPLATE_INSTANTIATIONS (found));
9733
9734 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9735 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9736 /* Now that the type has been registered on the instantiations
9737 list, we set up the enumerators. Because the enumeration
9738 constants may involve the enumeration type itself, we make
9739 sure to register the type first, and then create the
9740 constants. That way, doing tsubst_expr for the enumeration
9741 constants won't result in recursive calls here; we'll find
9742 the instantiation and exit above. */
9743 tsubst_enum (template_type, t, arglist);
9744
9745 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9746 /* If the type makes use of template parameters, the
9747 code that generates debugging information will crash. */
9748 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9749
9750 /* Possibly limit visibility based on template args. */
9751 TREE_PUBLIC (type_decl) = 1;
9752 determine_visibility (type_decl);
9753
9754 inherit_targ_abi_tags (t);
9755
9756 return t;
9757 }
9758 }
9759
9760 /* Wrapper for lookup_template_class_1. */
9761
9762 tree
9763 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9764 int entering_scope, tsubst_flags_t complain)
9765 {
9766 tree ret;
9767 timevar_push (TV_TEMPLATE_INST);
9768 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9769 entering_scope, complain);
9770 timevar_pop (TV_TEMPLATE_INST);
9771 return ret;
9772 }
9773
9774 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9775
9776 tree
9777 lookup_template_variable (tree templ, tree arglist)
9778 {
9779 /* The type of the expression is NULL_TREE since the template-id could refer
9780 to an explicit or partial specialization. */
9781 tree type = NULL_TREE;
9782 if (flag_concepts && variable_concept_p (templ))
9783 /* Except that concepts are always bool. */
9784 type = boolean_type_node;
9785 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9786 }
9787
9788 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9789
9790 tree
9791 finish_template_variable (tree var, tsubst_flags_t complain)
9792 {
9793 tree templ = TREE_OPERAND (var, 0);
9794 tree arglist = TREE_OPERAND (var, 1);
9795
9796 /* We never want to return a VAR_DECL for a variable concept, since they
9797 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9798 bool concept_p = flag_concepts && variable_concept_p (templ);
9799 if (concept_p && processing_template_decl)
9800 return var;
9801
9802 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9803 arglist = add_outermost_template_args (tmpl_args, arglist);
9804
9805 templ = most_general_template (templ);
9806 tree parms = DECL_TEMPLATE_PARMS (templ);
9807 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9808 /*req_all*/true,
9809 /*use_default*/true);
9810
9811 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9812 {
9813 if (complain & tf_error)
9814 {
9815 auto_diagnostic_group d;
9816 error ("use of invalid variable template %qE", var);
9817 diagnose_constraints (location_of (var), templ, arglist);
9818 }
9819 return error_mark_node;
9820 }
9821
9822 /* If a template-id refers to a specialization of a variable
9823 concept, then the expression is true if and only if the
9824 concept's constraints are satisfied by the given template
9825 arguments.
9826
9827 NOTE: This is an extension of Concepts Lite TS that
9828 allows constraints to be used in expressions. */
9829 if (concept_p)
9830 {
9831 tree decl = DECL_TEMPLATE_RESULT (templ);
9832 return evaluate_variable_concept (decl, arglist);
9833 }
9834
9835 return instantiate_template (templ, arglist, complain);
9836 }
9837
9838 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9839 TARGS template args, and instantiate it if it's not dependent. */
9840
9841 tree
9842 lookup_and_finish_template_variable (tree templ, tree targs,
9843 tsubst_flags_t complain)
9844 {
9845 templ = lookup_template_variable (templ, targs);
9846 if (!any_dependent_template_arguments_p (targs))
9847 {
9848 templ = finish_template_variable (templ, complain);
9849 mark_used (templ);
9850 }
9851
9852 return convert_from_reference (templ);
9853 }
9854
9855 \f
9856 struct pair_fn_data
9857 {
9858 tree_fn_t fn;
9859 tree_fn_t any_fn;
9860 void *data;
9861 /* True when we should also visit template parameters that occur in
9862 non-deduced contexts. */
9863 bool include_nondeduced_p;
9864 hash_set<tree> *visited;
9865 };
9866
9867 /* Called from for_each_template_parm via walk_tree. */
9868
9869 static tree
9870 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9871 {
9872 tree t = *tp;
9873 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9874 tree_fn_t fn = pfd->fn;
9875 void *data = pfd->data;
9876 tree result = NULL_TREE;
9877
9878 #define WALK_SUBTREE(NODE) \
9879 do \
9880 { \
9881 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9882 pfd->include_nondeduced_p, \
9883 pfd->any_fn); \
9884 if (result) goto out; \
9885 } \
9886 while (0)
9887
9888 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9889 return t;
9890
9891 if (TYPE_P (t)
9892 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9893 WALK_SUBTREE (TYPE_CONTEXT (t));
9894
9895 switch (TREE_CODE (t))
9896 {
9897 case RECORD_TYPE:
9898 if (TYPE_PTRMEMFUNC_P (t))
9899 break;
9900 /* Fall through. */
9901
9902 case UNION_TYPE:
9903 case ENUMERAL_TYPE:
9904 if (!TYPE_TEMPLATE_INFO (t))
9905 *walk_subtrees = 0;
9906 else
9907 WALK_SUBTREE (TYPE_TI_ARGS (t));
9908 break;
9909
9910 case INTEGER_TYPE:
9911 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9912 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9913 break;
9914
9915 case METHOD_TYPE:
9916 /* Since we're not going to walk subtrees, we have to do this
9917 explicitly here. */
9918 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9919 /* Fall through. */
9920
9921 case FUNCTION_TYPE:
9922 /* Check the return type. */
9923 WALK_SUBTREE (TREE_TYPE (t));
9924
9925 /* Check the parameter types. Since default arguments are not
9926 instantiated until they are needed, the TYPE_ARG_TYPES may
9927 contain expressions that involve template parameters. But,
9928 no-one should be looking at them yet. And, once they're
9929 instantiated, they don't contain template parameters, so
9930 there's no point in looking at them then, either. */
9931 {
9932 tree parm;
9933
9934 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9935 WALK_SUBTREE (TREE_VALUE (parm));
9936
9937 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9938 want walk_tree walking into them itself. */
9939 *walk_subtrees = 0;
9940 }
9941
9942 if (flag_noexcept_type)
9943 {
9944 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9945 if (spec)
9946 WALK_SUBTREE (TREE_PURPOSE (spec));
9947 }
9948 break;
9949
9950 case TYPEOF_TYPE:
9951 case DECLTYPE_TYPE:
9952 case UNDERLYING_TYPE:
9953 if (pfd->include_nondeduced_p
9954 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9955 pfd->visited,
9956 pfd->include_nondeduced_p,
9957 pfd->any_fn))
9958 return error_mark_node;
9959 *walk_subtrees = false;
9960 break;
9961
9962 case FUNCTION_DECL:
9963 case VAR_DECL:
9964 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9965 WALK_SUBTREE (DECL_TI_ARGS (t));
9966 /* Fall through. */
9967
9968 case PARM_DECL:
9969 case CONST_DECL:
9970 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9971 WALK_SUBTREE (DECL_INITIAL (t));
9972 if (DECL_CONTEXT (t)
9973 && pfd->include_nondeduced_p)
9974 WALK_SUBTREE (DECL_CONTEXT (t));
9975 break;
9976
9977 case BOUND_TEMPLATE_TEMPLATE_PARM:
9978 /* Record template parameters such as `T' inside `TT<T>'. */
9979 WALK_SUBTREE (TYPE_TI_ARGS (t));
9980 /* Fall through. */
9981
9982 case TEMPLATE_TEMPLATE_PARM:
9983 case TEMPLATE_TYPE_PARM:
9984 case TEMPLATE_PARM_INDEX:
9985 if (fn && (*fn)(t, data))
9986 return t;
9987 else if (!fn)
9988 return t;
9989 break;
9990
9991 case TEMPLATE_DECL:
9992 /* A template template parameter is encountered. */
9993 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9994 WALK_SUBTREE (TREE_TYPE (t));
9995
9996 /* Already substituted template template parameter */
9997 *walk_subtrees = 0;
9998 break;
9999
10000 case TYPENAME_TYPE:
10001 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10002 partial instantiation. */
10003 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10004 break;
10005
10006 case CONSTRUCTOR:
10007 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10008 && pfd->include_nondeduced_p)
10009 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10010 break;
10011
10012 case INDIRECT_REF:
10013 case COMPONENT_REF:
10014 /* If there's no type, then this thing must be some expression
10015 involving template parameters. */
10016 if (!fn && !TREE_TYPE (t))
10017 return error_mark_node;
10018 break;
10019
10020 case MODOP_EXPR:
10021 case CAST_EXPR:
10022 case IMPLICIT_CONV_EXPR:
10023 case REINTERPRET_CAST_EXPR:
10024 case CONST_CAST_EXPR:
10025 case STATIC_CAST_EXPR:
10026 case DYNAMIC_CAST_EXPR:
10027 case ARROW_EXPR:
10028 case DOTSTAR_EXPR:
10029 case TYPEID_EXPR:
10030 case PSEUDO_DTOR_EXPR:
10031 if (!fn)
10032 return error_mark_node;
10033 break;
10034
10035 default:
10036 break;
10037 }
10038
10039 #undef WALK_SUBTREE
10040
10041 /* We didn't find any template parameters we liked. */
10042 out:
10043 return result;
10044 }
10045
10046 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10047 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10048 call FN with the parameter and the DATA.
10049 If FN returns nonzero, the iteration is terminated, and
10050 for_each_template_parm returns 1. Otherwise, the iteration
10051 continues. If FN never returns a nonzero value, the value
10052 returned by for_each_template_parm is 0. If FN is NULL, it is
10053 considered to be the function which always returns 1.
10054
10055 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10056 parameters that occur in non-deduced contexts. When false, only
10057 visits those template parameters that can be deduced. */
10058
10059 static tree
10060 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10061 hash_set<tree> *visited,
10062 bool include_nondeduced_p,
10063 tree_fn_t any_fn)
10064 {
10065 struct pair_fn_data pfd;
10066 tree result;
10067
10068 /* Set up. */
10069 pfd.fn = fn;
10070 pfd.any_fn = any_fn;
10071 pfd.data = data;
10072 pfd.include_nondeduced_p = include_nondeduced_p;
10073
10074 /* Walk the tree. (Conceptually, we would like to walk without
10075 duplicates, but for_each_template_parm_r recursively calls
10076 for_each_template_parm, so we would need to reorganize a fair
10077 bit to use walk_tree_without_duplicates, so we keep our own
10078 visited list.) */
10079 if (visited)
10080 pfd.visited = visited;
10081 else
10082 pfd.visited = new hash_set<tree>;
10083 result = cp_walk_tree (&t,
10084 for_each_template_parm_r,
10085 &pfd,
10086 pfd.visited);
10087
10088 /* Clean up. */
10089 if (!visited)
10090 {
10091 delete pfd.visited;
10092 pfd.visited = 0;
10093 }
10094
10095 return result;
10096 }
10097
10098 /* Returns true if T depends on any template parameter. */
10099
10100 int
10101 uses_template_parms (tree t)
10102 {
10103 if (t == NULL_TREE)
10104 return false;
10105
10106 bool dependent_p;
10107 int saved_processing_template_decl;
10108
10109 saved_processing_template_decl = processing_template_decl;
10110 if (!saved_processing_template_decl)
10111 processing_template_decl = 1;
10112 if (TYPE_P (t))
10113 dependent_p = dependent_type_p (t);
10114 else if (TREE_CODE (t) == TREE_VEC)
10115 dependent_p = any_dependent_template_arguments_p (t);
10116 else if (TREE_CODE (t) == TREE_LIST)
10117 dependent_p = (uses_template_parms (TREE_VALUE (t))
10118 || uses_template_parms (TREE_CHAIN (t)));
10119 else if (TREE_CODE (t) == TYPE_DECL)
10120 dependent_p = dependent_type_p (TREE_TYPE (t));
10121 else if (DECL_P (t)
10122 || EXPR_P (t)
10123 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10124 || TREE_CODE (t) == OVERLOAD
10125 || BASELINK_P (t)
10126 || identifier_p (t)
10127 || TREE_CODE (t) == TRAIT_EXPR
10128 || TREE_CODE (t) == CONSTRUCTOR
10129 || CONSTANT_CLASS_P (t))
10130 dependent_p = (type_dependent_expression_p (t)
10131 || value_dependent_expression_p (t));
10132 else
10133 {
10134 gcc_assert (t == error_mark_node);
10135 dependent_p = false;
10136 }
10137
10138 processing_template_decl = saved_processing_template_decl;
10139
10140 return dependent_p;
10141 }
10142
10143 /* Returns true iff current_function_decl is an incompletely instantiated
10144 template. Useful instead of processing_template_decl because the latter
10145 is set to 0 during instantiate_non_dependent_expr. */
10146
10147 bool
10148 in_template_function (void)
10149 {
10150 tree fn = current_function_decl;
10151 bool ret;
10152 ++processing_template_decl;
10153 ret = (fn && DECL_LANG_SPECIFIC (fn)
10154 && DECL_TEMPLATE_INFO (fn)
10155 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10156 --processing_template_decl;
10157 return ret;
10158 }
10159
10160 /* Returns true if T depends on any template parameter with level LEVEL. */
10161
10162 bool
10163 uses_template_parms_level (tree t, int level)
10164 {
10165 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10166 /*include_nondeduced_p=*/true);
10167 }
10168
10169 /* Returns true if the signature of DECL depends on any template parameter from
10170 its enclosing class. */
10171
10172 bool
10173 uses_outer_template_parms (tree decl)
10174 {
10175 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10176 if (depth == 0)
10177 return false;
10178 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10179 &depth, NULL, /*include_nondeduced_p=*/true))
10180 return true;
10181 if (PRIMARY_TEMPLATE_P (decl)
10182 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10183 (DECL_TEMPLATE_PARMS (decl)),
10184 template_parm_outer_level,
10185 &depth, NULL, /*include_nondeduced_p=*/true))
10186 return true;
10187 tree ci = get_constraints (decl);
10188 if (ci)
10189 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10190 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10191 &depth, NULL, /*nondeduced*/true))
10192 return true;
10193 return false;
10194 }
10195
10196 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10197 ill-formed translation unit, i.e. a variable or function that isn't
10198 usable in a constant expression. */
10199
10200 static inline bool
10201 neglectable_inst_p (tree d)
10202 {
10203 return (d && DECL_P (d)
10204 && !undeduced_auto_decl (d)
10205 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10206 : decl_maybe_constant_var_p (d)));
10207 }
10208
10209 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10210 neglectable and instantiated from within an erroneous instantiation. */
10211
10212 static bool
10213 limit_bad_template_recursion (tree decl)
10214 {
10215 struct tinst_level *lev = current_tinst_level;
10216 int errs = errorcount + sorrycount;
10217 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10218 return false;
10219
10220 for (; lev; lev = lev->next)
10221 if (neglectable_inst_p (lev->maybe_get_node ()))
10222 break;
10223
10224 return (lev && errs > lev->errors);
10225 }
10226
10227 static int tinst_depth;
10228 extern int max_tinst_depth;
10229 int depth_reached;
10230
10231 static GTY(()) struct tinst_level *last_error_tinst_level;
10232
10233 /* We're starting to instantiate D; record the template instantiation context
10234 at LOC for diagnostics and to restore it later. */
10235
10236 static bool
10237 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10238 {
10239 struct tinst_level *new_level;
10240
10241 if (tinst_depth >= max_tinst_depth)
10242 {
10243 /* Tell error.c not to try to instantiate any templates. */
10244 at_eof = 2;
10245 fatal_error (input_location,
10246 "template instantiation depth exceeds maximum of %d"
10247 " (use %<-ftemplate-depth=%> to increase the maximum)",
10248 max_tinst_depth);
10249 return false;
10250 }
10251
10252 /* If the current instantiation caused problems, don't let it instantiate
10253 anything else. Do allow deduction substitution and decls usable in
10254 constant expressions. */
10255 if (!targs && limit_bad_template_recursion (tldcl))
10256 return false;
10257
10258 /* When not -quiet, dump template instantiations other than functions, since
10259 announce_function will take care of those. */
10260 if (!quiet_flag && !targs
10261 && TREE_CODE (tldcl) != TREE_LIST
10262 && TREE_CODE (tldcl) != FUNCTION_DECL)
10263 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10264
10265 new_level = tinst_level_freelist ().alloc ();
10266 new_level->tldcl = tldcl;
10267 new_level->targs = targs;
10268 new_level->locus = loc;
10269 new_level->errors = errorcount + sorrycount;
10270 new_level->next = NULL;
10271 new_level->refcount = 0;
10272 set_refcount_ptr (new_level->next, current_tinst_level);
10273 set_refcount_ptr (current_tinst_level, new_level);
10274
10275 ++tinst_depth;
10276 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10277 depth_reached = tinst_depth;
10278
10279 return true;
10280 }
10281
10282 /* We're starting substitution of TMPL<ARGS>; record the template
10283 substitution context for diagnostics and to restore it later. */
10284
10285 static bool
10286 push_tinst_level (tree tmpl, tree args)
10287 {
10288 return push_tinst_level_loc (tmpl, args, input_location);
10289 }
10290
10291 /* We're starting to instantiate D; record INPUT_LOCATION and the
10292 template instantiation context for diagnostics and to restore it
10293 later. */
10294
10295 bool
10296 push_tinst_level (tree d)
10297 {
10298 return push_tinst_level_loc (d, input_location);
10299 }
10300
10301 /* Likewise, but record LOC as the program location. */
10302
10303 bool
10304 push_tinst_level_loc (tree d, location_t loc)
10305 {
10306 gcc_assert (TREE_CODE (d) != TREE_LIST);
10307 return push_tinst_level_loc (d, NULL, loc);
10308 }
10309
10310 /* We're done instantiating this template; return to the instantiation
10311 context. */
10312
10313 void
10314 pop_tinst_level (void)
10315 {
10316 /* Restore the filename and line number stashed away when we started
10317 this instantiation. */
10318 input_location = current_tinst_level->locus;
10319 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10320 --tinst_depth;
10321 }
10322
10323 /* We're instantiating a deferred template; restore the template
10324 instantiation context in which the instantiation was requested, which
10325 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10326
10327 static tree
10328 reopen_tinst_level (struct tinst_level *level)
10329 {
10330 struct tinst_level *t;
10331
10332 tinst_depth = 0;
10333 for (t = level; t; t = t->next)
10334 ++tinst_depth;
10335
10336 set_refcount_ptr (current_tinst_level, level);
10337 pop_tinst_level ();
10338 if (current_tinst_level)
10339 current_tinst_level->errors = errorcount+sorrycount;
10340 return level->maybe_get_node ();
10341 }
10342
10343 /* Returns the TINST_LEVEL which gives the original instantiation
10344 context. */
10345
10346 struct tinst_level *
10347 outermost_tinst_level (void)
10348 {
10349 struct tinst_level *level = current_tinst_level;
10350 if (level)
10351 while (level->next)
10352 level = level->next;
10353 return level;
10354 }
10355
10356 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10357 vector of template arguments, as for tsubst.
10358
10359 Returns an appropriate tsubst'd friend declaration. */
10360
10361 static tree
10362 tsubst_friend_function (tree decl, tree args)
10363 {
10364 tree new_friend;
10365
10366 if (TREE_CODE (decl) == FUNCTION_DECL
10367 && DECL_TEMPLATE_INSTANTIATION (decl)
10368 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10369 /* This was a friend declared with an explicit template
10370 argument list, e.g.:
10371
10372 friend void f<>(T);
10373
10374 to indicate that f was a template instantiation, not a new
10375 function declaration. Now, we have to figure out what
10376 instantiation of what template. */
10377 {
10378 tree template_id, arglist, fns;
10379 tree new_args;
10380 tree tmpl;
10381 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10382
10383 /* Friend functions are looked up in the containing namespace scope.
10384 We must enter that scope, to avoid finding member functions of the
10385 current class with same name. */
10386 push_nested_namespace (ns);
10387 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10388 tf_warning_or_error, NULL_TREE,
10389 /*integral_constant_expression_p=*/false);
10390 pop_nested_namespace (ns);
10391 arglist = tsubst (DECL_TI_ARGS (decl), args,
10392 tf_warning_or_error, NULL_TREE);
10393 template_id = lookup_template_function (fns, arglist);
10394
10395 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10396 tmpl = determine_specialization (template_id, new_friend,
10397 &new_args,
10398 /*need_member_template=*/0,
10399 TREE_VEC_LENGTH (args),
10400 tsk_none);
10401 return instantiate_template (tmpl, new_args, tf_error);
10402 }
10403
10404 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10405
10406 /* The NEW_FRIEND will look like an instantiation, to the
10407 compiler, but is not an instantiation from the point of view of
10408 the language. For example, we might have had:
10409
10410 template <class T> struct S {
10411 template <class U> friend void f(T, U);
10412 };
10413
10414 Then, in S<int>, template <class U> void f(int, U) is not an
10415 instantiation of anything. */
10416 if (new_friend == error_mark_node)
10417 return error_mark_node;
10418
10419 DECL_USE_TEMPLATE (new_friend) = 0;
10420 if (TREE_CODE (decl) == TEMPLATE_DECL)
10421 {
10422 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10423 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10424 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10425 }
10426
10427 /* The mangled name for the NEW_FRIEND is incorrect. The function
10428 is not a template instantiation and should not be mangled like
10429 one. Therefore, we forget the mangling here; we'll recompute it
10430 later if we need it. */
10431 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10432 {
10433 SET_DECL_RTL (new_friend, NULL);
10434 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10435 }
10436
10437 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10438 {
10439 tree old_decl;
10440 tree new_friend_template_info;
10441 tree new_friend_result_template_info;
10442 tree ns;
10443 int new_friend_is_defn;
10444
10445 /* We must save some information from NEW_FRIEND before calling
10446 duplicate decls since that function will free NEW_FRIEND if
10447 possible. */
10448 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10449 new_friend_is_defn =
10450 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10451 (template_for_substitution (new_friend)))
10452 != NULL_TREE);
10453 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10454 {
10455 /* This declaration is a `primary' template. */
10456 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10457
10458 new_friend_result_template_info
10459 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10460 }
10461 else
10462 new_friend_result_template_info = NULL_TREE;
10463
10464 /* Inside pushdecl_namespace_level, we will push into the
10465 current namespace. However, the friend function should go
10466 into the namespace of the template. */
10467 ns = decl_namespace_context (new_friend);
10468 push_nested_namespace (ns);
10469 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10470 pop_nested_namespace (ns);
10471
10472 if (old_decl == error_mark_node)
10473 return error_mark_node;
10474
10475 if (old_decl != new_friend)
10476 {
10477 /* This new friend declaration matched an existing
10478 declaration. For example, given:
10479
10480 template <class T> void f(T);
10481 template <class U> class C {
10482 template <class T> friend void f(T) {}
10483 };
10484
10485 the friend declaration actually provides the definition
10486 of `f', once C has been instantiated for some type. So,
10487 old_decl will be the out-of-class template declaration,
10488 while new_friend is the in-class definition.
10489
10490 But, if `f' was called before this point, the
10491 instantiation of `f' will have DECL_TI_ARGS corresponding
10492 to `T' but not to `U', references to which might appear
10493 in the definition of `f'. Previously, the most general
10494 template for an instantiation of `f' was the out-of-class
10495 version; now it is the in-class version. Therefore, we
10496 run through all specialization of `f', adding to their
10497 DECL_TI_ARGS appropriately. In particular, they need a
10498 new set of outer arguments, corresponding to the
10499 arguments for this class instantiation.
10500
10501 The same situation can arise with something like this:
10502
10503 friend void f(int);
10504 template <class T> class C {
10505 friend void f(T) {}
10506 };
10507
10508 when `C<int>' is instantiated. Now, `f(int)' is defined
10509 in the class. */
10510
10511 if (!new_friend_is_defn)
10512 /* On the other hand, if the in-class declaration does
10513 *not* provide a definition, then we don't want to alter
10514 existing definitions. We can just leave everything
10515 alone. */
10516 ;
10517 else
10518 {
10519 tree new_template = TI_TEMPLATE (new_friend_template_info);
10520 tree new_args = TI_ARGS (new_friend_template_info);
10521
10522 /* Overwrite whatever template info was there before, if
10523 any, with the new template information pertaining to
10524 the declaration. */
10525 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10526
10527 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10528 {
10529 /* We should have called reregister_specialization in
10530 duplicate_decls. */
10531 gcc_assert (retrieve_specialization (new_template,
10532 new_args, 0)
10533 == old_decl);
10534
10535 /* Instantiate it if the global has already been used. */
10536 if (DECL_ODR_USED (old_decl))
10537 instantiate_decl (old_decl, /*defer_ok=*/true,
10538 /*expl_inst_class_mem_p=*/false);
10539 }
10540 else
10541 {
10542 tree t;
10543
10544 /* Indicate that the old function template is a partial
10545 instantiation. */
10546 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10547 = new_friend_result_template_info;
10548
10549 gcc_assert (new_template
10550 == most_general_template (new_template));
10551 gcc_assert (new_template != old_decl);
10552
10553 /* Reassign any specializations already in the hash table
10554 to the new more general template, and add the
10555 additional template args. */
10556 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10557 t != NULL_TREE;
10558 t = TREE_CHAIN (t))
10559 {
10560 tree spec = TREE_VALUE (t);
10561 spec_entry elt;
10562
10563 elt.tmpl = old_decl;
10564 elt.args = DECL_TI_ARGS (spec);
10565 elt.spec = NULL_TREE;
10566
10567 decl_specializations->remove_elt (&elt);
10568
10569 DECL_TI_ARGS (spec)
10570 = add_outermost_template_args (new_args,
10571 DECL_TI_ARGS (spec));
10572
10573 register_specialization
10574 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10575
10576 }
10577 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10578 }
10579 }
10580
10581 /* The information from NEW_FRIEND has been merged into OLD_DECL
10582 by duplicate_decls. */
10583 new_friend = old_decl;
10584 }
10585 }
10586 else
10587 {
10588 tree context = DECL_CONTEXT (new_friend);
10589 bool dependent_p;
10590
10591 /* In the code
10592 template <class T> class C {
10593 template <class U> friend void C1<U>::f (); // case 1
10594 friend void C2<T>::f (); // case 2
10595 };
10596 we only need to make sure CONTEXT is a complete type for
10597 case 2. To distinguish between the two cases, we note that
10598 CONTEXT of case 1 remains dependent type after tsubst while
10599 this isn't true for case 2. */
10600 ++processing_template_decl;
10601 dependent_p = dependent_type_p (context);
10602 --processing_template_decl;
10603
10604 if (!dependent_p
10605 && !complete_type_or_else (context, NULL_TREE))
10606 return error_mark_node;
10607
10608 if (COMPLETE_TYPE_P (context))
10609 {
10610 tree fn = new_friend;
10611 /* do_friend adds the TEMPLATE_DECL for any member friend
10612 template even if it isn't a member template, i.e.
10613 template <class T> friend A<T>::f();
10614 Look through it in that case. */
10615 if (TREE_CODE (fn) == TEMPLATE_DECL
10616 && !PRIMARY_TEMPLATE_P (fn))
10617 fn = DECL_TEMPLATE_RESULT (fn);
10618 /* Check to see that the declaration is really present, and,
10619 possibly obtain an improved declaration. */
10620 fn = check_classfn (context, fn, NULL_TREE);
10621
10622 if (fn)
10623 new_friend = fn;
10624 }
10625 }
10626
10627 return new_friend;
10628 }
10629
10630 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10631 template arguments, as for tsubst.
10632
10633 Returns an appropriate tsubst'd friend type or error_mark_node on
10634 failure. */
10635
10636 static tree
10637 tsubst_friend_class (tree friend_tmpl, tree args)
10638 {
10639 tree tmpl;
10640
10641 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10642 {
10643 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10644 return TREE_TYPE (tmpl);
10645 }
10646
10647 tree context = CP_DECL_CONTEXT (friend_tmpl);
10648 if (TREE_CODE (context) == NAMESPACE_DECL)
10649 push_nested_namespace (context);
10650 else
10651 {
10652 context = tsubst (context, args, tf_error, NULL_TREE);
10653 push_nested_class (context);
10654 }
10655
10656 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10657 /*non_class=*/false, /*block_p=*/false,
10658 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10659
10660 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10661 {
10662 /* The friend template has already been declared. Just
10663 check to see that the declarations match, and install any new
10664 default parameters. We must tsubst the default parameters,
10665 of course. We only need the innermost template parameters
10666 because that is all that redeclare_class_template will look
10667 at. */
10668 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10669 > TMPL_ARGS_DEPTH (args))
10670 {
10671 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10672 args, tf_warning_or_error);
10673 location_t saved_input_location = input_location;
10674 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10675 tree cons = get_constraints (tmpl);
10676 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10677 input_location = saved_input_location;
10678 }
10679 }
10680 else
10681 {
10682 /* The friend template has not already been declared. In this
10683 case, the instantiation of the template class will cause the
10684 injection of this template into the namespace scope. */
10685 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10686
10687 if (tmpl != error_mark_node)
10688 {
10689 /* The new TMPL is not an instantiation of anything, so we
10690 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10691 for the new type because that is supposed to be the
10692 corresponding template decl, i.e., TMPL. */
10693 DECL_USE_TEMPLATE (tmpl) = 0;
10694 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10695 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10696 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10697 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10698
10699 /* It is hidden. */
10700 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10701 DECL_ANTICIPATED (tmpl)
10702 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10703
10704 /* Inject this template into the enclosing namspace scope. */
10705 tmpl = pushdecl_namespace_level (tmpl, true);
10706 }
10707 }
10708
10709 if (TREE_CODE (context) == NAMESPACE_DECL)
10710 pop_nested_namespace (context);
10711 else
10712 pop_nested_class ();
10713
10714 return TREE_TYPE (tmpl);
10715 }
10716
10717 /* Returns zero if TYPE cannot be completed later due to circularity.
10718 Otherwise returns one. */
10719
10720 static int
10721 can_complete_type_without_circularity (tree type)
10722 {
10723 if (type == NULL_TREE || type == error_mark_node)
10724 return 0;
10725 else if (COMPLETE_TYPE_P (type))
10726 return 1;
10727 else if (TREE_CODE (type) == ARRAY_TYPE)
10728 return can_complete_type_without_circularity (TREE_TYPE (type));
10729 else if (CLASS_TYPE_P (type)
10730 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10731 return 0;
10732 else
10733 return 1;
10734 }
10735
10736 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10737 tsubst_flags_t, tree);
10738
10739 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10740 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10741
10742 static tree
10743 tsubst_attribute (tree t, tree *decl_p, tree args,
10744 tsubst_flags_t complain, tree in_decl)
10745 {
10746 gcc_assert (ATTR_IS_DEPENDENT (t));
10747
10748 tree val = TREE_VALUE (t);
10749 if (val == NULL_TREE)
10750 /* Nothing to do. */;
10751 else if ((flag_openmp || flag_openmp_simd)
10752 && is_attribute_p ("omp declare simd",
10753 get_attribute_name (t)))
10754 {
10755 tree clauses = TREE_VALUE (val);
10756 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10757 complain, in_decl);
10758 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10759 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10760 tree parms = DECL_ARGUMENTS (*decl_p);
10761 clauses
10762 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10763 if (clauses)
10764 val = build_tree_list (NULL_TREE, clauses);
10765 else
10766 val = NULL_TREE;
10767 }
10768 /* If the first attribute argument is an identifier, don't
10769 pass it through tsubst. Attributes like mode, format,
10770 cleanup and several target specific attributes expect it
10771 unmodified. */
10772 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10773 {
10774 tree chain
10775 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10776 /*integral_constant_expression_p=*/false);
10777 if (chain != TREE_CHAIN (val))
10778 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10779 }
10780 else if (PACK_EXPANSION_P (val))
10781 {
10782 /* An attribute pack expansion. */
10783 tree purp = TREE_PURPOSE (t);
10784 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10785 if (pack == error_mark_node)
10786 return error_mark_node;
10787 int len = TREE_VEC_LENGTH (pack);
10788 tree list = NULL_TREE;
10789 tree *q = &list;
10790 for (int i = 0; i < len; ++i)
10791 {
10792 tree elt = TREE_VEC_ELT (pack, i);
10793 *q = build_tree_list (purp, elt);
10794 q = &TREE_CHAIN (*q);
10795 }
10796 return list;
10797 }
10798 else
10799 val = tsubst_expr (val, args, complain, in_decl,
10800 /*integral_constant_expression_p=*/false);
10801
10802 if (val != TREE_VALUE (t))
10803 return build_tree_list (TREE_PURPOSE (t), val);
10804 return t;
10805 }
10806
10807 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10808 unchanged or a new TREE_LIST chain. */
10809
10810 static tree
10811 tsubst_attributes (tree attributes, tree args,
10812 tsubst_flags_t complain, tree in_decl)
10813 {
10814 tree last_dep = NULL_TREE;
10815
10816 for (tree t = attributes; t; t = TREE_CHAIN (t))
10817 if (ATTR_IS_DEPENDENT (t))
10818 {
10819 last_dep = t;
10820 attributes = copy_list (attributes);
10821 break;
10822 }
10823
10824 if (last_dep)
10825 for (tree *p = &attributes; *p; )
10826 {
10827 tree t = *p;
10828 if (ATTR_IS_DEPENDENT (t))
10829 {
10830 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10831 if (subst != t)
10832 {
10833 *p = subst;
10834 while (*p)
10835 p = &TREE_CHAIN (*p);
10836 *p = TREE_CHAIN (t);
10837 continue;
10838 }
10839 }
10840 p = &TREE_CHAIN (*p);
10841 }
10842
10843 return attributes;
10844 }
10845
10846 /* Apply any attributes which had to be deferred until instantiation
10847 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10848 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10849
10850 static void
10851 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10852 tree args, tsubst_flags_t complain, tree in_decl)
10853 {
10854 tree last_dep = NULL_TREE;
10855 tree t;
10856 tree *p;
10857
10858 if (attributes == NULL_TREE)
10859 return;
10860
10861 if (DECL_P (*decl_p))
10862 {
10863 if (TREE_TYPE (*decl_p) == error_mark_node)
10864 return;
10865 p = &DECL_ATTRIBUTES (*decl_p);
10866 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10867 to our attributes parameter. */
10868 gcc_assert (*p == attributes);
10869 }
10870 else
10871 {
10872 p = &TYPE_ATTRIBUTES (*decl_p);
10873 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10874 lookup_template_class_1, and should be preserved. */
10875 gcc_assert (*p != attributes);
10876 while (*p)
10877 p = &TREE_CHAIN (*p);
10878 }
10879
10880 for (t = attributes; t; t = TREE_CHAIN (t))
10881 if (ATTR_IS_DEPENDENT (t))
10882 {
10883 last_dep = t;
10884 attributes = copy_list (attributes);
10885 break;
10886 }
10887
10888 *p = attributes;
10889 if (last_dep)
10890 {
10891 tree late_attrs = NULL_TREE;
10892 tree *q = &late_attrs;
10893
10894 for (; *p; )
10895 {
10896 t = *p;
10897 if (ATTR_IS_DEPENDENT (t))
10898 {
10899 *p = TREE_CHAIN (t);
10900 TREE_CHAIN (t) = NULL_TREE;
10901 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10902 while (*q)
10903 q = &TREE_CHAIN (*q);
10904 }
10905 else
10906 p = &TREE_CHAIN (t);
10907 }
10908
10909 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10910 }
10911 }
10912
10913 /* Perform (or defer) access check for typedefs that were referenced
10914 from within the template TMPL code.
10915 This is a subroutine of instantiate_decl and instantiate_class_template.
10916 TMPL is the template to consider and TARGS is the list of arguments of
10917 that template. */
10918
10919 static void
10920 perform_typedefs_access_check (tree tmpl, tree targs)
10921 {
10922 location_t saved_location;
10923 unsigned i;
10924 qualified_typedef_usage_t *iter;
10925
10926 if (!tmpl
10927 || (!CLASS_TYPE_P (tmpl)
10928 && TREE_CODE (tmpl) != FUNCTION_DECL))
10929 return;
10930
10931 saved_location = input_location;
10932 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10933 {
10934 tree type_decl = iter->typedef_decl;
10935 tree type_scope = iter->context;
10936
10937 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10938 continue;
10939
10940 if (uses_template_parms (type_decl))
10941 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10942 if (uses_template_parms (type_scope))
10943 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10944
10945 /* Make access check error messages point to the location
10946 of the use of the typedef. */
10947 input_location = iter->locus;
10948 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10949 type_decl, type_decl,
10950 tf_warning_or_error);
10951 }
10952 input_location = saved_location;
10953 }
10954
10955 static tree
10956 instantiate_class_template_1 (tree type)
10957 {
10958 tree templ, args, pattern, t, member;
10959 tree typedecl;
10960 tree pbinfo;
10961 tree base_list;
10962 unsigned int saved_maximum_field_alignment;
10963 tree fn_context;
10964
10965 if (type == error_mark_node)
10966 return error_mark_node;
10967
10968 if (COMPLETE_OR_OPEN_TYPE_P (type)
10969 || uses_template_parms (type))
10970 return type;
10971
10972 /* Figure out which template is being instantiated. */
10973 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10974 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10975
10976 /* Mark the type as in the process of being defined. */
10977 TYPE_BEING_DEFINED (type) = 1;
10978
10979 /* We may be in the middle of deferred access check. Disable
10980 it now. */
10981 deferring_access_check_sentinel acs (dk_no_deferred);
10982
10983 /* Determine what specialization of the original template to
10984 instantiate. */
10985 t = most_specialized_partial_spec (type, tf_warning_or_error);
10986 if (t == error_mark_node)
10987 return error_mark_node;
10988 else if (t)
10989 {
10990 /* This TYPE is actually an instantiation of a partial
10991 specialization. We replace the innermost set of ARGS with
10992 the arguments appropriate for substitution. For example,
10993 given:
10994
10995 template <class T> struct S {};
10996 template <class T> struct S<T*> {};
10997
10998 and supposing that we are instantiating S<int*>, ARGS will
10999 presently be {int*} -- but we need {int}. */
11000 pattern = TREE_TYPE (t);
11001 args = TREE_PURPOSE (t);
11002 }
11003 else
11004 {
11005 pattern = TREE_TYPE (templ);
11006 args = CLASSTYPE_TI_ARGS (type);
11007 }
11008
11009 /* If the template we're instantiating is incomplete, then clearly
11010 there's nothing we can do. */
11011 if (!COMPLETE_TYPE_P (pattern))
11012 {
11013 /* We can try again later. */
11014 TYPE_BEING_DEFINED (type) = 0;
11015 return type;
11016 }
11017
11018 /* If we've recursively instantiated too many templates, stop. */
11019 if (! push_tinst_level (type))
11020 return type;
11021
11022 int saved_unevaluated_operand = cp_unevaluated_operand;
11023 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11024
11025 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11026 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11027 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11028 fn_context = error_mark_node;
11029 if (!fn_context)
11030 push_to_top_level ();
11031 else
11032 {
11033 cp_unevaluated_operand = 0;
11034 c_inhibit_evaluation_warnings = 0;
11035 }
11036 /* Use #pragma pack from the template context. */
11037 saved_maximum_field_alignment = maximum_field_alignment;
11038 maximum_field_alignment = TYPE_PRECISION (pattern);
11039
11040 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11041
11042 /* Set the input location to the most specialized template definition.
11043 This is needed if tsubsting causes an error. */
11044 typedecl = TYPE_MAIN_DECL (pattern);
11045 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11046 DECL_SOURCE_LOCATION (typedecl);
11047
11048 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11049 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11050 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11051 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11052 if (ANON_AGGR_TYPE_P (pattern))
11053 SET_ANON_AGGR_TYPE_P (type);
11054 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11055 {
11056 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11057 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11058 /* Adjust visibility for template arguments. */
11059 determine_visibility (TYPE_MAIN_DECL (type));
11060 }
11061 if (CLASS_TYPE_P (type))
11062 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11063
11064 pbinfo = TYPE_BINFO (pattern);
11065
11066 /* We should never instantiate a nested class before its enclosing
11067 class; we need to look up the nested class by name before we can
11068 instantiate it, and that lookup should instantiate the enclosing
11069 class. */
11070 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11071 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11072
11073 base_list = NULL_TREE;
11074 if (BINFO_N_BASE_BINFOS (pbinfo))
11075 {
11076 tree pbase_binfo;
11077 tree pushed_scope;
11078 int i;
11079
11080 /* We must enter the scope containing the type, as that is where
11081 the accessibility of types named in dependent bases are
11082 looked up from. */
11083 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11084
11085 /* Substitute into each of the bases to determine the actual
11086 basetypes. */
11087 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11088 {
11089 tree base;
11090 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11091 tree expanded_bases = NULL_TREE;
11092 int idx, len = 1;
11093
11094 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11095 {
11096 expanded_bases =
11097 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11098 args, tf_error, NULL_TREE);
11099 if (expanded_bases == error_mark_node)
11100 continue;
11101
11102 len = TREE_VEC_LENGTH (expanded_bases);
11103 }
11104
11105 for (idx = 0; idx < len; idx++)
11106 {
11107 if (expanded_bases)
11108 /* Extract the already-expanded base class. */
11109 base = TREE_VEC_ELT (expanded_bases, idx);
11110 else
11111 /* Substitute to figure out the base class. */
11112 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11113 NULL_TREE);
11114
11115 if (base == error_mark_node)
11116 continue;
11117
11118 base_list = tree_cons (access, base, base_list);
11119 if (BINFO_VIRTUAL_P (pbase_binfo))
11120 TREE_TYPE (base_list) = integer_type_node;
11121 }
11122 }
11123
11124 /* The list is now in reverse order; correct that. */
11125 base_list = nreverse (base_list);
11126
11127 if (pushed_scope)
11128 pop_scope (pushed_scope);
11129 }
11130 /* Now call xref_basetypes to set up all the base-class
11131 information. */
11132 xref_basetypes (type, base_list);
11133
11134 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11135 (int) ATTR_FLAG_TYPE_IN_PLACE,
11136 args, tf_error, NULL_TREE);
11137 fixup_attribute_variants (type);
11138
11139 /* Now that our base classes are set up, enter the scope of the
11140 class, so that name lookups into base classes, etc. will work
11141 correctly. This is precisely analogous to what we do in
11142 begin_class_definition when defining an ordinary non-template
11143 class, except we also need to push the enclosing classes. */
11144 push_nested_class (type);
11145
11146 /* Now members are processed in the order of declaration. */
11147 for (member = CLASSTYPE_DECL_LIST (pattern);
11148 member; member = TREE_CHAIN (member))
11149 {
11150 tree t = TREE_VALUE (member);
11151
11152 if (TREE_PURPOSE (member))
11153 {
11154 if (TYPE_P (t))
11155 {
11156 if (LAMBDA_TYPE_P (t))
11157 /* A closure type for a lambda in an NSDMI or default argument.
11158 Ignore it; it will be regenerated when needed. */
11159 continue;
11160
11161 /* Build new CLASSTYPE_NESTED_UTDS. */
11162
11163 tree newtag;
11164 bool class_template_p;
11165
11166 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11167 && TYPE_LANG_SPECIFIC (t)
11168 && CLASSTYPE_IS_TEMPLATE (t));
11169 /* If the member is a class template, then -- even after
11170 substitution -- there may be dependent types in the
11171 template argument list for the class. We increment
11172 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11173 that function will assume that no types are dependent
11174 when outside of a template. */
11175 if (class_template_p)
11176 ++processing_template_decl;
11177 newtag = tsubst (t, args, tf_error, NULL_TREE);
11178 if (class_template_p)
11179 --processing_template_decl;
11180 if (newtag == error_mark_node)
11181 continue;
11182
11183 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11184 {
11185 tree name = TYPE_IDENTIFIER (t);
11186
11187 if (class_template_p)
11188 /* Unfortunately, lookup_template_class sets
11189 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11190 instantiation (i.e., for the type of a member
11191 template class nested within a template class.)
11192 This behavior is required for
11193 maybe_process_partial_specialization to work
11194 correctly, but is not accurate in this case;
11195 the TAG is not an instantiation of anything.
11196 (The corresponding TEMPLATE_DECL is an
11197 instantiation, but the TYPE is not.) */
11198 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11199
11200 /* Now, we call pushtag to put this NEWTAG into the scope of
11201 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11202 pushtag calling push_template_decl. We don't have to do
11203 this for enums because it will already have been done in
11204 tsubst_enum. */
11205 if (name)
11206 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11207 pushtag (name, newtag, /*tag_scope=*/ts_current);
11208 }
11209 }
11210 else if (DECL_DECLARES_FUNCTION_P (t))
11211 {
11212 tree r;
11213
11214 if (TREE_CODE (t) == TEMPLATE_DECL)
11215 ++processing_template_decl;
11216 r = tsubst (t, args, tf_error, NULL_TREE);
11217 if (TREE_CODE (t) == TEMPLATE_DECL)
11218 --processing_template_decl;
11219 set_current_access_from_decl (r);
11220 finish_member_declaration (r);
11221 /* Instantiate members marked with attribute used. */
11222 if (r != error_mark_node && DECL_PRESERVE_P (r))
11223 mark_used (r);
11224 if (TREE_CODE (r) == FUNCTION_DECL
11225 && DECL_OMP_DECLARE_REDUCTION_P (r))
11226 cp_check_omp_declare_reduction (r);
11227 }
11228 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11229 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11230 /* A closure type for a lambda in an NSDMI or default argument.
11231 Ignore it; it will be regenerated when needed. */;
11232 else
11233 {
11234 /* Build new TYPE_FIELDS. */
11235 if (TREE_CODE (t) == STATIC_ASSERT)
11236 {
11237 tree condition;
11238
11239 ++c_inhibit_evaluation_warnings;
11240 condition =
11241 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11242 tf_warning_or_error, NULL_TREE,
11243 /*integral_constant_expression_p=*/true);
11244 --c_inhibit_evaluation_warnings;
11245
11246 finish_static_assert (condition,
11247 STATIC_ASSERT_MESSAGE (t),
11248 STATIC_ASSERT_SOURCE_LOCATION (t),
11249 /*member_p=*/true);
11250 }
11251 else if (TREE_CODE (t) != CONST_DECL)
11252 {
11253 tree r;
11254 tree vec = NULL_TREE;
11255 int len = 1;
11256
11257 /* The file and line for this declaration, to
11258 assist in error message reporting. Since we
11259 called push_tinst_level above, we don't need to
11260 restore these. */
11261 input_location = DECL_SOURCE_LOCATION (t);
11262
11263 if (TREE_CODE (t) == TEMPLATE_DECL)
11264 ++processing_template_decl;
11265 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11266 if (TREE_CODE (t) == TEMPLATE_DECL)
11267 --processing_template_decl;
11268
11269 if (TREE_CODE (r) == TREE_VEC)
11270 {
11271 /* A capture pack became multiple fields. */
11272 vec = r;
11273 len = TREE_VEC_LENGTH (vec);
11274 }
11275
11276 for (int i = 0; i < len; ++i)
11277 {
11278 if (vec)
11279 r = TREE_VEC_ELT (vec, i);
11280 if (VAR_P (r))
11281 {
11282 /* In [temp.inst]:
11283
11284 [t]he initialization (and any associated
11285 side-effects) of a static data member does
11286 not occur unless the static data member is
11287 itself used in a way that requires the
11288 definition of the static data member to
11289 exist.
11290
11291 Therefore, we do not substitute into the
11292 initialized for the static data member here. */
11293 finish_static_data_member_decl
11294 (r,
11295 /*init=*/NULL_TREE,
11296 /*init_const_expr_p=*/false,
11297 /*asmspec_tree=*/NULL_TREE,
11298 /*flags=*/0);
11299 /* Instantiate members marked with attribute used. */
11300 if (r != error_mark_node && DECL_PRESERVE_P (r))
11301 mark_used (r);
11302 }
11303 else if (TREE_CODE (r) == FIELD_DECL)
11304 {
11305 /* Determine whether R has a valid type and can be
11306 completed later. If R is invalid, then its type
11307 is replaced by error_mark_node. */
11308 tree rtype = TREE_TYPE (r);
11309 if (can_complete_type_without_circularity (rtype))
11310 complete_type (rtype);
11311
11312 if (!complete_or_array_type_p (rtype))
11313 {
11314 /* If R's type couldn't be completed and
11315 it isn't a flexible array member (whose
11316 type is incomplete by definition) give
11317 an error. */
11318 cxx_incomplete_type_error (r, rtype);
11319 TREE_TYPE (r) = error_mark_node;
11320 }
11321 else if (TREE_CODE (rtype) == ARRAY_TYPE
11322 && TYPE_DOMAIN (rtype) == NULL_TREE
11323 && (TREE_CODE (type) == UNION_TYPE
11324 || TREE_CODE (type) == QUAL_UNION_TYPE))
11325 {
11326 error ("flexible array member %qD in union", r);
11327 TREE_TYPE (r) = error_mark_node;
11328 }
11329 }
11330
11331 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11332 such a thing will already have been added to the field
11333 list by tsubst_enum in finish_member_declaration in the
11334 CLASSTYPE_NESTED_UTDS case above. */
11335 if (!(TREE_CODE (r) == TYPE_DECL
11336 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11337 && DECL_ARTIFICIAL (r)))
11338 {
11339 set_current_access_from_decl (r);
11340 finish_member_declaration (r);
11341 }
11342 }
11343 }
11344 }
11345 }
11346 else
11347 {
11348 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11349 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11350 {
11351 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11352
11353 tree friend_type = t;
11354 bool adjust_processing_template_decl = false;
11355
11356 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11357 {
11358 /* template <class T> friend class C; */
11359 friend_type = tsubst_friend_class (friend_type, args);
11360 adjust_processing_template_decl = true;
11361 }
11362 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11363 {
11364 /* template <class T> friend class C::D; */
11365 friend_type = tsubst (friend_type, args,
11366 tf_warning_or_error, NULL_TREE);
11367 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11368 friend_type = TREE_TYPE (friend_type);
11369 adjust_processing_template_decl = true;
11370 }
11371 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11372 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11373 {
11374 /* This could be either
11375
11376 friend class T::C;
11377
11378 when dependent_type_p is false or
11379
11380 template <class U> friend class T::C;
11381
11382 otherwise. */
11383 /* Bump processing_template_decl in case this is something like
11384 template <class T> friend struct A<T>::B. */
11385 ++processing_template_decl;
11386 friend_type = tsubst (friend_type, args,
11387 tf_warning_or_error, NULL_TREE);
11388 if (dependent_type_p (friend_type))
11389 adjust_processing_template_decl = true;
11390 --processing_template_decl;
11391 }
11392 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11393 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11394 && TYPE_HIDDEN_P (friend_type))
11395 {
11396 /* friend class C;
11397
11398 where C hasn't been declared yet. Let's lookup name
11399 from namespace scope directly, bypassing any name that
11400 come from dependent base class. */
11401 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11402
11403 /* The call to xref_tag_from_type does injection for friend
11404 classes. */
11405 push_nested_namespace (ns);
11406 friend_type =
11407 xref_tag_from_type (friend_type, NULL_TREE,
11408 /*tag_scope=*/ts_current);
11409 pop_nested_namespace (ns);
11410 }
11411 else if (uses_template_parms (friend_type))
11412 /* friend class C<T>; */
11413 friend_type = tsubst (friend_type, args,
11414 tf_warning_or_error, NULL_TREE);
11415 /* Otherwise it's
11416
11417 friend class C;
11418
11419 where C is already declared or
11420
11421 friend class C<int>;
11422
11423 We don't have to do anything in these cases. */
11424
11425 if (adjust_processing_template_decl)
11426 /* Trick make_friend_class into realizing that the friend
11427 we're adding is a template, not an ordinary class. It's
11428 important that we use make_friend_class since it will
11429 perform some error-checking and output cross-reference
11430 information. */
11431 ++processing_template_decl;
11432
11433 if (friend_type != error_mark_node)
11434 make_friend_class (type, friend_type, /*complain=*/false);
11435
11436 if (adjust_processing_template_decl)
11437 --processing_template_decl;
11438 }
11439 else
11440 {
11441 /* Build new DECL_FRIENDLIST. */
11442 tree r;
11443
11444 /* The file and line for this declaration, to
11445 assist in error message reporting. Since we
11446 called push_tinst_level above, we don't need to
11447 restore these. */
11448 input_location = DECL_SOURCE_LOCATION (t);
11449
11450 if (TREE_CODE (t) == TEMPLATE_DECL)
11451 {
11452 ++processing_template_decl;
11453 push_deferring_access_checks (dk_no_check);
11454 }
11455
11456 r = tsubst_friend_function (t, args);
11457 add_friend (type, r, /*complain=*/false);
11458 if (TREE_CODE (t) == TEMPLATE_DECL)
11459 {
11460 pop_deferring_access_checks ();
11461 --processing_template_decl;
11462 }
11463 }
11464 }
11465 }
11466
11467 if (fn_context)
11468 {
11469 /* Restore these before substituting into the lambda capture
11470 initializers. */
11471 cp_unevaluated_operand = saved_unevaluated_operand;
11472 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11473 }
11474
11475 /* Set the file and line number information to whatever is given for
11476 the class itself. This puts error messages involving generated
11477 implicit functions at a predictable point, and the same point
11478 that would be used for non-template classes. */
11479 input_location = DECL_SOURCE_LOCATION (typedecl);
11480
11481 unreverse_member_declarations (type);
11482 finish_struct_1 (type);
11483 TYPE_BEING_DEFINED (type) = 0;
11484
11485 /* We don't instantiate default arguments for member functions. 14.7.1:
11486
11487 The implicit instantiation of a class template specialization causes
11488 the implicit instantiation of the declarations, but not of the
11489 definitions or default arguments, of the class member functions,
11490 member classes, static data members and member templates.... */
11491
11492 /* Some typedefs referenced from within the template code need to be access
11493 checked at template instantiation time, i.e now. These types were
11494 added to the template at parsing time. Let's get those and perform
11495 the access checks then. */
11496 perform_typedefs_access_check (pattern, args);
11497 perform_deferred_access_checks (tf_warning_or_error);
11498 pop_nested_class ();
11499 maximum_field_alignment = saved_maximum_field_alignment;
11500 if (!fn_context)
11501 pop_from_top_level ();
11502 pop_tinst_level ();
11503
11504 /* The vtable for a template class can be emitted in any translation
11505 unit in which the class is instantiated. When there is no key
11506 method, however, finish_struct_1 will already have added TYPE to
11507 the keyed_classes. */
11508 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11509 vec_safe_push (keyed_classes, type);
11510
11511 return type;
11512 }
11513
11514 /* Wrapper for instantiate_class_template_1. */
11515
11516 tree
11517 instantiate_class_template (tree type)
11518 {
11519 tree ret;
11520 timevar_push (TV_TEMPLATE_INST);
11521 ret = instantiate_class_template_1 (type);
11522 timevar_pop (TV_TEMPLATE_INST);
11523 return ret;
11524 }
11525
11526 static tree
11527 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11528 {
11529 tree r;
11530
11531 if (!t)
11532 r = t;
11533 else if (TYPE_P (t))
11534 r = tsubst (t, args, complain, in_decl);
11535 else
11536 {
11537 if (!(complain & tf_warning))
11538 ++c_inhibit_evaluation_warnings;
11539 r = tsubst_expr (t, args, complain, in_decl,
11540 /*integral_constant_expression_p=*/true);
11541 if (!(complain & tf_warning))
11542 --c_inhibit_evaluation_warnings;
11543 }
11544 return r;
11545 }
11546
11547 /* Given a function parameter pack TMPL_PARM and some function parameters
11548 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11549 and set *SPEC_P to point at the next point in the list. */
11550
11551 tree
11552 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11553 {
11554 /* Collect all of the extra "packed" parameters into an
11555 argument pack. */
11556 tree parmvec;
11557 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11558 tree spec_parm = *spec_p;
11559 int i, len;
11560
11561 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11562 if (tmpl_parm
11563 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11564 break;
11565
11566 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11567 parmvec = make_tree_vec (len);
11568 spec_parm = *spec_p;
11569 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11570 {
11571 tree elt = spec_parm;
11572 if (DECL_PACK_P (elt))
11573 elt = make_pack_expansion (elt);
11574 TREE_VEC_ELT (parmvec, i) = elt;
11575 }
11576
11577 /* Build the argument packs. */
11578 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11579 *spec_p = spec_parm;
11580
11581 return argpack;
11582 }
11583
11584 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11585 NONTYPE_ARGUMENT_PACK. */
11586
11587 static tree
11588 make_fnparm_pack (tree spec_parm)
11589 {
11590 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11591 }
11592
11593 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11594 pack expansion with no extra args, 2 if it has extra args, or 0
11595 if it is not a pack expansion. */
11596
11597 static int
11598 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11599 {
11600 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11601 /* We're being called before this happens in tsubst_pack_expansion. */
11602 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11603 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11604 if (i >= TREE_VEC_LENGTH (vec))
11605 return 0;
11606 tree elt = TREE_VEC_ELT (vec, i);
11607 if (DECL_P (elt))
11608 /* A decl pack is itself an expansion. */
11609 elt = TREE_TYPE (elt);
11610 if (!PACK_EXPANSION_P (elt))
11611 return 0;
11612 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11613 return 2;
11614 return 1;
11615 }
11616
11617
11618 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11619
11620 static tree
11621 make_argument_pack_select (tree arg_pack, unsigned index)
11622 {
11623 tree aps = make_node (ARGUMENT_PACK_SELECT);
11624
11625 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11626 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11627
11628 return aps;
11629 }
11630
11631 /* This is a subroutine of tsubst_pack_expansion.
11632
11633 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11634 mechanism to store the (non complete list of) arguments of the
11635 substitution and return a non substituted pack expansion, in order
11636 to wait for when we have enough arguments to really perform the
11637 substitution. */
11638
11639 static bool
11640 use_pack_expansion_extra_args_p (tree parm_packs,
11641 int arg_pack_len,
11642 bool has_empty_arg)
11643 {
11644 /* If one pack has an expansion and another pack has a normal
11645 argument or if one pack has an empty argument and an another
11646 one hasn't then tsubst_pack_expansion cannot perform the
11647 substitution and need to fall back on the
11648 PACK_EXPANSION_EXTRA mechanism. */
11649 if (parm_packs == NULL_TREE)
11650 return false;
11651 else if (has_empty_arg)
11652 return true;
11653
11654 bool has_expansion_arg = false;
11655 for (int i = 0 ; i < arg_pack_len; ++i)
11656 {
11657 bool has_non_expansion_arg = false;
11658 for (tree parm_pack = parm_packs;
11659 parm_pack;
11660 parm_pack = TREE_CHAIN (parm_pack))
11661 {
11662 tree arg = TREE_VALUE (parm_pack);
11663
11664 int exp = argument_pack_element_is_expansion_p (arg, i);
11665 if (exp == 2)
11666 /* We can't substitute a pack expansion with extra args into
11667 our pattern. */
11668 return true;
11669 else if (exp)
11670 has_expansion_arg = true;
11671 else
11672 has_non_expansion_arg = true;
11673 }
11674
11675 if (has_expansion_arg && has_non_expansion_arg)
11676 return true;
11677 }
11678 return false;
11679 }
11680
11681 /* [temp.variadic]/6 says that:
11682
11683 The instantiation of a pack expansion [...]
11684 produces a list E1,E2, ..., En, where N is the number of elements
11685 in the pack expansion parameters.
11686
11687 This subroutine of tsubst_pack_expansion produces one of these Ei.
11688
11689 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11690 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11691 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11692 INDEX is the index 'i' of the element Ei to produce. ARGS,
11693 COMPLAIN, and IN_DECL are the same parameters as for the
11694 tsubst_pack_expansion function.
11695
11696 The function returns the resulting Ei upon successful completion,
11697 or error_mark_node.
11698
11699 Note that this function possibly modifies the ARGS parameter, so
11700 it's the responsibility of the caller to restore it. */
11701
11702 static tree
11703 gen_elem_of_pack_expansion_instantiation (tree pattern,
11704 tree parm_packs,
11705 unsigned index,
11706 tree args /* This parm gets
11707 modified. */,
11708 tsubst_flags_t complain,
11709 tree in_decl)
11710 {
11711 tree t;
11712 bool ith_elem_is_expansion = false;
11713
11714 /* For each parameter pack, change the substitution of the parameter
11715 pack to the ith argument in its argument pack, then expand the
11716 pattern. */
11717 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11718 {
11719 tree parm = TREE_PURPOSE (pack);
11720 tree arg_pack = TREE_VALUE (pack);
11721 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11722
11723 ith_elem_is_expansion |=
11724 argument_pack_element_is_expansion_p (arg_pack, index);
11725
11726 /* Select the Ith argument from the pack. */
11727 if (TREE_CODE (parm) == PARM_DECL
11728 || VAR_P (parm)
11729 || TREE_CODE (parm) == FIELD_DECL)
11730 {
11731 if (index == 0)
11732 {
11733 aps = make_argument_pack_select (arg_pack, index);
11734 if (!mark_used (parm, complain) && !(complain & tf_error))
11735 return error_mark_node;
11736 register_local_specialization (aps, parm);
11737 }
11738 else
11739 aps = retrieve_local_specialization (parm);
11740 }
11741 else
11742 {
11743 int idx, level;
11744 template_parm_level_and_index (parm, &level, &idx);
11745
11746 if (index == 0)
11747 {
11748 aps = make_argument_pack_select (arg_pack, index);
11749 /* Update the corresponding argument. */
11750 TMPL_ARG (args, level, idx) = aps;
11751 }
11752 else
11753 /* Re-use the ARGUMENT_PACK_SELECT. */
11754 aps = TMPL_ARG (args, level, idx);
11755 }
11756 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11757 }
11758
11759 // Any local specialization bindings arising from this substitution
11760 // cannot be reused for a different INDEX.
11761 local_specialization_stack lss (lss_copy);
11762
11763 /* Substitute into the PATTERN with the (possibly altered)
11764 arguments. */
11765 if (pattern == in_decl)
11766 /* Expanding a fixed parameter pack from
11767 coerce_template_parameter_pack. */
11768 t = tsubst_decl (pattern, args, complain);
11769 else if (pattern == error_mark_node)
11770 t = error_mark_node;
11771 else if (constraint_p (pattern))
11772 {
11773 if (processing_template_decl)
11774 t = tsubst_constraint (pattern, args, complain, in_decl);
11775 else
11776 t = (constraints_satisfied_p (pattern, args)
11777 ? boolean_true_node : boolean_false_node);
11778 }
11779 else if (!TYPE_P (pattern))
11780 t = tsubst_expr (pattern, args, complain, in_decl,
11781 /*integral_constant_expression_p=*/false);
11782 else
11783 t = tsubst (pattern, args, complain, in_decl);
11784
11785 /* If the Ith argument pack element is a pack expansion, then
11786 the Ith element resulting from the substituting is going to
11787 be a pack expansion as well. */
11788 if (ith_elem_is_expansion)
11789 t = make_pack_expansion (t, complain);
11790
11791 return t;
11792 }
11793
11794 /* When the unexpanded parameter pack in a fold expression expands to an empty
11795 sequence, the value of the expression is as follows; the program is
11796 ill-formed if the operator is not listed in this table.
11797
11798 && true
11799 || false
11800 , void() */
11801
11802 tree
11803 expand_empty_fold (tree t, tsubst_flags_t complain)
11804 {
11805 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11806 if (!FOLD_EXPR_MODIFY_P (t))
11807 switch (code)
11808 {
11809 case TRUTH_ANDIF_EXPR:
11810 return boolean_true_node;
11811 case TRUTH_ORIF_EXPR:
11812 return boolean_false_node;
11813 case COMPOUND_EXPR:
11814 return void_node;
11815 default:
11816 break;
11817 }
11818
11819 if (complain & tf_error)
11820 error_at (location_of (t),
11821 "fold of empty expansion over %O", code);
11822 return error_mark_node;
11823 }
11824
11825 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11826 form an expression that combines the two terms using the
11827 operator of T. */
11828
11829 static tree
11830 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11831 {
11832 tree op = FOLD_EXPR_OP (t);
11833 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11834
11835 // Handle compound assignment operators.
11836 if (FOLD_EXPR_MODIFY_P (t))
11837 return build_x_modify_expr (input_location, left, code, right, complain);
11838
11839 switch (code)
11840 {
11841 case COMPOUND_EXPR:
11842 return build_x_compound_expr (input_location, left, right, complain);
11843 default:
11844 return build_x_binary_op (input_location, code,
11845 left, TREE_CODE (left),
11846 right, TREE_CODE (right),
11847 /*overload=*/NULL,
11848 complain);
11849 }
11850 }
11851
11852 /* Substitute ARGS into the pack of a fold expression T. */
11853
11854 static inline tree
11855 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11856 {
11857 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11858 }
11859
11860 /* Substitute ARGS into the pack of a fold expression T. */
11861
11862 static inline tree
11863 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11864 {
11865 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11866 }
11867
11868 /* Expand a PACK of arguments into a grouped as left fold.
11869 Given a pack containing elements A0, A1, ..., An and an
11870 operator @, this builds the expression:
11871
11872 ((A0 @ A1) @ A2) ... @ An
11873
11874 Note that PACK must not be empty.
11875
11876 The operator is defined by the original fold expression T. */
11877
11878 static tree
11879 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11880 {
11881 tree left = TREE_VEC_ELT (pack, 0);
11882 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11883 {
11884 tree right = TREE_VEC_ELT (pack, i);
11885 left = fold_expression (t, left, right, complain);
11886 }
11887 return left;
11888 }
11889
11890 /* Substitute into a unary left fold expression. */
11891
11892 static tree
11893 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11894 tree in_decl)
11895 {
11896 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11897 if (pack == error_mark_node)
11898 return error_mark_node;
11899 if (PACK_EXPANSION_P (pack))
11900 {
11901 tree r = copy_node (t);
11902 FOLD_EXPR_PACK (r) = pack;
11903 return r;
11904 }
11905 if (TREE_VEC_LENGTH (pack) == 0)
11906 return expand_empty_fold (t, complain);
11907 else
11908 return expand_left_fold (t, pack, complain);
11909 }
11910
11911 /* Substitute into a binary left fold expression.
11912
11913 Do ths by building a single (non-empty) vector of argumnts and
11914 building the expression from those elements. */
11915
11916 static tree
11917 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11918 tree in_decl)
11919 {
11920 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11921 if (pack == error_mark_node)
11922 return error_mark_node;
11923 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11924 if (init == error_mark_node)
11925 return error_mark_node;
11926
11927 if (PACK_EXPANSION_P (pack))
11928 {
11929 tree r = copy_node (t);
11930 FOLD_EXPR_PACK (r) = pack;
11931 FOLD_EXPR_INIT (r) = init;
11932 return r;
11933 }
11934
11935 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11936 TREE_VEC_ELT (vec, 0) = init;
11937 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11938 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11939
11940 return expand_left_fold (t, vec, complain);
11941 }
11942
11943 /* Expand a PACK of arguments into a grouped as right fold.
11944 Given a pack containing elementns A0, A1, ..., and an
11945 operator @, this builds the expression:
11946
11947 A0@ ... (An-2 @ (An-1 @ An))
11948
11949 Note that PACK must not be empty.
11950
11951 The operator is defined by the original fold expression T. */
11952
11953 tree
11954 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11955 {
11956 // Build the expression.
11957 int n = TREE_VEC_LENGTH (pack);
11958 tree right = TREE_VEC_ELT (pack, n - 1);
11959 for (--n; n != 0; --n)
11960 {
11961 tree left = TREE_VEC_ELT (pack, n - 1);
11962 right = fold_expression (t, left, right, complain);
11963 }
11964 return right;
11965 }
11966
11967 /* Substitute into a unary right fold expression. */
11968
11969 static tree
11970 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11971 tree in_decl)
11972 {
11973 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11974 if (pack == error_mark_node)
11975 return error_mark_node;
11976 if (PACK_EXPANSION_P (pack))
11977 {
11978 tree r = copy_node (t);
11979 FOLD_EXPR_PACK (r) = pack;
11980 return r;
11981 }
11982 if (TREE_VEC_LENGTH (pack) == 0)
11983 return expand_empty_fold (t, complain);
11984 else
11985 return expand_right_fold (t, pack, complain);
11986 }
11987
11988 /* Substitute into a binary right fold expression.
11989
11990 Do ths by building a single (non-empty) vector of arguments and
11991 building the expression from those elements. */
11992
11993 static tree
11994 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11995 tree in_decl)
11996 {
11997 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11998 if (pack == error_mark_node)
11999 return error_mark_node;
12000 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12001 if (init == error_mark_node)
12002 return error_mark_node;
12003
12004 if (PACK_EXPANSION_P (pack))
12005 {
12006 tree r = copy_node (t);
12007 FOLD_EXPR_PACK (r) = pack;
12008 FOLD_EXPR_INIT (r) = init;
12009 return r;
12010 }
12011
12012 int n = TREE_VEC_LENGTH (pack);
12013 tree vec = make_tree_vec (n + 1);
12014 for (int i = 0; i < n; ++i)
12015 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12016 TREE_VEC_ELT (vec, n) = init;
12017
12018 return expand_right_fold (t, vec, complain);
12019 }
12020
12021 /* Walk through the pattern of a pack expansion, adding everything in
12022 local_specializations to a list. */
12023
12024 struct el_data
12025 {
12026 hash_set<tree> internal;
12027 tree extra;
12028 tsubst_flags_t complain;
12029
12030 el_data (tsubst_flags_t c)
12031 : extra (NULL_TREE), complain (c) {}
12032 };
12033 static tree
12034 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12035 {
12036 el_data &data = *reinterpret_cast<el_data*>(data_);
12037 tree *extra = &data.extra;
12038 tsubst_flags_t complain = data.complain;
12039
12040 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12041 /* Remember local typedefs (85214). */
12042 tp = &TYPE_NAME (*tp);
12043
12044 if (TREE_CODE (*tp) == DECL_EXPR)
12045 data.internal.add (DECL_EXPR_DECL (*tp));
12046 else if (tree spec = retrieve_local_specialization (*tp))
12047 {
12048 if (data.internal.contains (*tp))
12049 /* Don't mess with variables declared within the pattern. */
12050 return NULL_TREE;
12051 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12052 {
12053 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12054 tree args = ARGUMENT_PACK_ARGS (spec);
12055 if (TREE_VEC_LENGTH (args) == 1)
12056 {
12057 tree elt = TREE_VEC_ELT (args, 0);
12058 if (PACK_EXPANSION_P (elt))
12059 elt = PACK_EXPANSION_PATTERN (elt);
12060 if (DECL_PACK_P (elt))
12061 spec = elt;
12062 }
12063 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12064 {
12065 /* Handle lambda capture here, since we aren't doing any
12066 substitution now, and so tsubst_copy won't call
12067 process_outer_var_ref. */
12068 tree args = ARGUMENT_PACK_ARGS (spec);
12069 int len = TREE_VEC_LENGTH (args);
12070 for (int i = 0; i < len; ++i)
12071 {
12072 tree arg = TREE_VEC_ELT (args, i);
12073 tree carg = arg;
12074 if (outer_automatic_var_p (arg))
12075 carg = process_outer_var_ref (arg, complain);
12076 if (carg != arg)
12077 {
12078 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12079 proxies. */
12080 if (i == 0)
12081 {
12082 spec = copy_node (spec);
12083 args = copy_node (args);
12084 SET_ARGUMENT_PACK_ARGS (spec, args);
12085 register_local_specialization (spec, *tp);
12086 }
12087 TREE_VEC_ELT (args, i) = carg;
12088 }
12089 }
12090 }
12091 }
12092 if (outer_automatic_var_p (spec))
12093 spec = process_outer_var_ref (spec, complain);
12094 *extra = tree_cons (*tp, spec, *extra);
12095 }
12096 return NULL_TREE;
12097 }
12098 static tree
12099 extract_local_specs (tree pattern, tsubst_flags_t complain)
12100 {
12101 el_data data (complain);
12102 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12103 return data.extra;
12104 }
12105
12106 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12107 for use in PACK_EXPANSION_EXTRA_ARGS. */
12108
12109 tree
12110 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12111 {
12112 tree extra = args;
12113 if (local_specializations)
12114 if (tree locals = extract_local_specs (pattern, complain))
12115 extra = tree_cons (NULL_TREE, extra, locals);
12116 return extra;
12117 }
12118
12119 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12120 normal template args to ARGS. */
12121
12122 tree
12123 add_extra_args (tree extra, tree args)
12124 {
12125 if (extra && TREE_CODE (extra) == TREE_LIST)
12126 {
12127 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12128 {
12129 /* The partial instantiation involved local declarations collected in
12130 extract_local_specs; map from the general template to our local
12131 context. */
12132 tree gen = TREE_PURPOSE (elt);
12133 tree inst = TREE_VALUE (elt);
12134 if (DECL_P (inst))
12135 if (tree local = retrieve_local_specialization (inst))
12136 inst = local;
12137 /* else inst is already a full instantiation of the pack. */
12138 register_local_specialization (inst, gen);
12139 }
12140 gcc_assert (!TREE_PURPOSE (extra));
12141 extra = TREE_VALUE (extra);
12142 }
12143 return add_to_template_args (extra, args);
12144 }
12145
12146 /* Substitute ARGS into T, which is an pack expansion
12147 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12148 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12149 (if only a partial substitution could be performed) or
12150 ERROR_MARK_NODE if there was an error. */
12151 tree
12152 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12153 tree in_decl)
12154 {
12155 tree pattern;
12156 tree pack, packs = NULL_TREE;
12157 bool unsubstituted_packs = false;
12158 bool unsubstituted_fn_pack = false;
12159 int i, len = -1;
12160 tree result;
12161 hash_map<tree, tree> *saved_local_specializations = NULL;
12162 bool need_local_specializations = false;
12163 int levels;
12164
12165 gcc_assert (PACK_EXPANSION_P (t));
12166 pattern = PACK_EXPANSION_PATTERN (t);
12167
12168 /* Add in any args remembered from an earlier partial instantiation. */
12169 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12170
12171 levels = TMPL_ARGS_DEPTH (args);
12172
12173 /* Determine the argument packs that will instantiate the parameter
12174 packs used in the expansion expression. While we're at it,
12175 compute the number of arguments to be expanded and make sure it
12176 is consistent. */
12177 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12178 pack = TREE_CHAIN (pack))
12179 {
12180 tree parm_pack = TREE_VALUE (pack);
12181 tree arg_pack = NULL_TREE;
12182 tree orig_arg = NULL_TREE;
12183 int level = 0;
12184
12185 if (TREE_CODE (parm_pack) == BASES)
12186 {
12187 gcc_assert (parm_pack == pattern);
12188 if (BASES_DIRECT (parm_pack))
12189 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12190 args, complain,
12191 in_decl, false),
12192 complain);
12193 else
12194 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12195 args, complain, in_decl,
12196 false), complain);
12197 }
12198 else if (builtin_pack_call_p (parm_pack))
12199 {
12200 if (parm_pack != pattern)
12201 {
12202 if (complain & tf_error)
12203 sorry ("%qE is not the entire pattern of the pack expansion",
12204 parm_pack);
12205 return error_mark_node;
12206 }
12207 return expand_builtin_pack_call (parm_pack, args,
12208 complain, in_decl);
12209 }
12210 else if (TREE_CODE (parm_pack) == PARM_DECL)
12211 {
12212 /* We know we have correct local_specializations if this
12213 expansion is at function scope, or if we're dealing with a
12214 local parameter in a requires expression; for the latter,
12215 tsubst_requires_expr set it up appropriately. */
12216 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12217 arg_pack = retrieve_local_specialization (parm_pack);
12218 else
12219 /* We can't rely on local_specializations for a parameter
12220 name used later in a function declaration (such as in a
12221 late-specified return type). Even if it exists, it might
12222 have the wrong value for a recursive call. */
12223 need_local_specializations = true;
12224
12225 if (!arg_pack)
12226 {
12227 /* This parameter pack was used in an unevaluated context. Just
12228 make a dummy decl, since it's only used for its type. */
12229 ++cp_unevaluated_operand;
12230 arg_pack = tsubst_decl (parm_pack, args, complain);
12231 --cp_unevaluated_operand;
12232 if (arg_pack && DECL_PACK_P (arg_pack))
12233 /* Partial instantiation of the parm_pack, we can't build
12234 up an argument pack yet. */
12235 arg_pack = NULL_TREE;
12236 else
12237 arg_pack = make_fnparm_pack (arg_pack);
12238 }
12239 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12240 /* This argument pack isn't fully instantiated yet. We set this
12241 flag rather than clear arg_pack because we do want to do the
12242 optimization below, and we don't want to substitute directly
12243 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12244 where it isn't expected). */
12245 unsubstituted_fn_pack = true;
12246 }
12247 else if (is_capture_proxy (parm_pack))
12248 {
12249 arg_pack = retrieve_local_specialization (parm_pack);
12250 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12251 unsubstituted_fn_pack = true;
12252 }
12253 else
12254 {
12255 int idx;
12256 template_parm_level_and_index (parm_pack, &level, &idx);
12257
12258 if (level <= levels)
12259 arg_pack = TMPL_ARG (args, level, idx);
12260 }
12261
12262 orig_arg = arg_pack;
12263 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12264 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12265
12266 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12267 /* This can only happen if we forget to expand an argument
12268 pack somewhere else. Just return an error, silently. */
12269 {
12270 result = make_tree_vec (1);
12271 TREE_VEC_ELT (result, 0) = error_mark_node;
12272 return result;
12273 }
12274
12275 if (arg_pack)
12276 {
12277 int my_len =
12278 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12279
12280 /* Don't bother trying to do a partial substitution with
12281 incomplete packs; we'll try again after deduction. */
12282 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12283 return t;
12284
12285 if (len < 0)
12286 len = my_len;
12287 else if (len != my_len
12288 && !unsubstituted_fn_pack)
12289 {
12290 if (!(complain & tf_error))
12291 /* Fail quietly. */;
12292 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12293 error ("mismatched argument pack lengths while expanding %qT",
12294 pattern);
12295 else
12296 error ("mismatched argument pack lengths while expanding %qE",
12297 pattern);
12298 return error_mark_node;
12299 }
12300
12301 /* Keep track of the parameter packs and their corresponding
12302 argument packs. */
12303 packs = tree_cons (parm_pack, arg_pack, packs);
12304 TREE_TYPE (packs) = orig_arg;
12305 }
12306 else
12307 {
12308 /* We can't substitute for this parameter pack. We use a flag as
12309 well as the missing_level counter because function parameter
12310 packs don't have a level. */
12311 gcc_assert (processing_template_decl || is_auto (parm_pack));
12312 unsubstituted_packs = true;
12313 }
12314 }
12315
12316 /* If the expansion is just T..., return the matching argument pack, unless
12317 we need to call convert_from_reference on all the elements. This is an
12318 important optimization; see c++/68422. */
12319 if (!unsubstituted_packs
12320 && TREE_PURPOSE (packs) == pattern)
12321 {
12322 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12323
12324 /* If the argument pack is a single pack expansion, pull it out. */
12325 if (TREE_VEC_LENGTH (args) == 1
12326 && pack_expansion_args_count (args))
12327 return TREE_VEC_ELT (args, 0);
12328
12329 /* Types need no adjustment, nor does sizeof..., and if we still have
12330 some pack expansion args we won't do anything yet. */
12331 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12332 || PACK_EXPANSION_SIZEOF_P (t)
12333 || pack_expansion_args_count (args))
12334 return args;
12335 /* Also optimize expression pack expansions if we can tell that the
12336 elements won't have reference type. */
12337 tree type = TREE_TYPE (pattern);
12338 if (type && !TYPE_REF_P (type)
12339 && !PACK_EXPANSION_P (type)
12340 && !WILDCARD_TYPE_P (type))
12341 return args;
12342 /* Otherwise use the normal path so we get convert_from_reference. */
12343 }
12344
12345 /* We cannot expand this expansion expression, because we don't have
12346 all of the argument packs we need. */
12347 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12348 {
12349 /* We got some full packs, but we can't substitute them in until we
12350 have values for all the packs. So remember these until then. */
12351
12352 t = make_pack_expansion (pattern, complain);
12353 PACK_EXPANSION_EXTRA_ARGS (t)
12354 = build_extra_args (pattern, args, complain);
12355 return t;
12356 }
12357 else if (unsubstituted_packs)
12358 {
12359 /* There were no real arguments, we're just replacing a parameter
12360 pack with another version of itself. Substitute into the
12361 pattern and return a PACK_EXPANSION_*. The caller will need to
12362 deal with that. */
12363 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12364 t = tsubst_expr (pattern, args, complain, in_decl,
12365 /*integral_constant_expression_p=*/false);
12366 else
12367 t = tsubst (pattern, args, complain, in_decl);
12368 t = make_pack_expansion (t, complain);
12369 return t;
12370 }
12371
12372 gcc_assert (len >= 0);
12373
12374 if (need_local_specializations)
12375 {
12376 /* We're in a late-specified return type, so create our own local
12377 specializations map; the current map is either NULL or (in the
12378 case of recursive unification) might have bindings that we don't
12379 want to use or alter. */
12380 saved_local_specializations = local_specializations;
12381 local_specializations = new hash_map<tree, tree>;
12382 }
12383
12384 /* For each argument in each argument pack, substitute into the
12385 pattern. */
12386 result = make_tree_vec (len);
12387 tree elem_args = copy_template_args (args);
12388 for (i = 0; i < len; ++i)
12389 {
12390 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12391 i,
12392 elem_args, complain,
12393 in_decl);
12394 TREE_VEC_ELT (result, i) = t;
12395 if (t == error_mark_node)
12396 {
12397 result = error_mark_node;
12398 break;
12399 }
12400 }
12401
12402 /* Update ARGS to restore the substitution from parameter packs to
12403 their argument packs. */
12404 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12405 {
12406 tree parm = TREE_PURPOSE (pack);
12407
12408 if (TREE_CODE (parm) == PARM_DECL
12409 || VAR_P (parm)
12410 || TREE_CODE (parm) == FIELD_DECL)
12411 register_local_specialization (TREE_TYPE (pack), parm);
12412 else
12413 {
12414 int idx, level;
12415
12416 if (TREE_VALUE (pack) == NULL_TREE)
12417 continue;
12418
12419 template_parm_level_and_index (parm, &level, &idx);
12420
12421 /* Update the corresponding argument. */
12422 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12423 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12424 TREE_TYPE (pack);
12425 else
12426 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12427 }
12428 }
12429
12430 if (need_local_specializations)
12431 {
12432 delete local_specializations;
12433 local_specializations = saved_local_specializations;
12434 }
12435
12436 /* If the dependent pack arguments were such that we end up with only a
12437 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12438 if (len == 1 && TREE_CODE (result) == TREE_VEC
12439 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12440 return TREE_VEC_ELT (result, 0);
12441
12442 return result;
12443 }
12444
12445 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12446 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12447 parameter packs; all parms generated from a function parameter pack will
12448 have the same DECL_PARM_INDEX. */
12449
12450 tree
12451 get_pattern_parm (tree parm, tree tmpl)
12452 {
12453 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12454 tree patparm;
12455
12456 if (DECL_ARTIFICIAL (parm))
12457 {
12458 for (patparm = DECL_ARGUMENTS (pattern);
12459 patparm; patparm = DECL_CHAIN (patparm))
12460 if (DECL_ARTIFICIAL (patparm)
12461 && DECL_NAME (parm) == DECL_NAME (patparm))
12462 break;
12463 }
12464 else
12465 {
12466 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12467 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12468 gcc_assert (DECL_PARM_INDEX (patparm)
12469 == DECL_PARM_INDEX (parm));
12470 }
12471
12472 return patparm;
12473 }
12474
12475 /* Make an argument pack out of the TREE_VEC VEC. */
12476
12477 static tree
12478 make_argument_pack (tree vec)
12479 {
12480 tree pack;
12481 tree elt = TREE_VEC_ELT (vec, 0);
12482 if (TYPE_P (elt))
12483 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12484 else
12485 {
12486 pack = make_node (NONTYPE_ARGUMENT_PACK);
12487 TREE_CONSTANT (pack) = 1;
12488 }
12489 SET_ARGUMENT_PACK_ARGS (pack, vec);
12490 return pack;
12491 }
12492
12493 /* Return an exact copy of template args T that can be modified
12494 independently. */
12495
12496 static tree
12497 copy_template_args (tree t)
12498 {
12499 if (t == error_mark_node)
12500 return t;
12501
12502 int len = TREE_VEC_LENGTH (t);
12503 tree new_vec = make_tree_vec (len);
12504
12505 for (int i = 0; i < len; ++i)
12506 {
12507 tree elt = TREE_VEC_ELT (t, i);
12508 if (elt && TREE_CODE (elt) == TREE_VEC)
12509 elt = copy_template_args (elt);
12510 TREE_VEC_ELT (new_vec, i) = elt;
12511 }
12512
12513 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12514 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12515
12516 return new_vec;
12517 }
12518
12519 /* Substitute ARGS into the vector or list of template arguments T. */
12520
12521 static tree
12522 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12523 {
12524 tree orig_t = t;
12525 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12526 tree *elts;
12527
12528 if (t == error_mark_node)
12529 return error_mark_node;
12530
12531 len = TREE_VEC_LENGTH (t);
12532 elts = XALLOCAVEC (tree, len);
12533
12534 for (i = 0; i < len; i++)
12535 {
12536 tree orig_arg = TREE_VEC_ELT (t, i);
12537 tree new_arg;
12538
12539 if (TREE_CODE (orig_arg) == TREE_VEC)
12540 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12541 else if (PACK_EXPANSION_P (orig_arg))
12542 {
12543 /* Substitute into an expansion expression. */
12544 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12545
12546 if (TREE_CODE (new_arg) == TREE_VEC)
12547 /* Add to the expanded length adjustment the number of
12548 expanded arguments. We subtract one from this
12549 measurement, because the argument pack expression
12550 itself is already counted as 1 in
12551 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12552 the argument pack is empty. */
12553 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12554 }
12555 else if (ARGUMENT_PACK_P (orig_arg))
12556 {
12557 /* Substitute into each of the arguments. */
12558 new_arg = TYPE_P (orig_arg)
12559 ? cxx_make_type (TREE_CODE (orig_arg))
12560 : make_node (TREE_CODE (orig_arg));
12561
12562 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12563 args, complain, in_decl);
12564 if (pack_args == error_mark_node)
12565 new_arg = error_mark_node;
12566 else
12567 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12568
12569 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12570 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12571 }
12572 else
12573 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12574
12575 if (new_arg == error_mark_node)
12576 return error_mark_node;
12577
12578 elts[i] = new_arg;
12579 if (new_arg != orig_arg)
12580 need_new = 1;
12581 }
12582
12583 if (!need_new)
12584 return t;
12585
12586 /* Make space for the expanded arguments coming from template
12587 argument packs. */
12588 t = make_tree_vec (len + expanded_len_adjust);
12589 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12590 arguments for a member template.
12591 In that case each TREE_VEC in ORIG_T represents a level of template
12592 arguments, and ORIG_T won't carry any non defaulted argument count.
12593 It will rather be the nested TREE_VECs that will carry one.
12594 In other words, ORIG_T carries a non defaulted argument count only
12595 if it doesn't contain any nested TREE_VEC. */
12596 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12597 {
12598 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12599 count += expanded_len_adjust;
12600 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12601 }
12602 for (i = 0, out = 0; i < len; i++)
12603 {
12604 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12605 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12606 && TREE_CODE (elts[i]) == TREE_VEC)
12607 {
12608 int idx;
12609
12610 /* Now expand the template argument pack "in place". */
12611 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12612 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12613 }
12614 else
12615 {
12616 TREE_VEC_ELT (t, out) = elts[i];
12617 out++;
12618 }
12619 }
12620
12621 return t;
12622 }
12623
12624 /* Substitute ARGS into one level PARMS of template parameters. */
12625
12626 static tree
12627 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12628 {
12629 if (parms == error_mark_node)
12630 return error_mark_node;
12631
12632 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12633
12634 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12635 {
12636 tree tuple = TREE_VEC_ELT (parms, i);
12637
12638 if (tuple == error_mark_node)
12639 continue;
12640
12641 TREE_VEC_ELT (new_vec, i) =
12642 tsubst_template_parm (tuple, args, complain);
12643 }
12644
12645 return new_vec;
12646 }
12647
12648 /* Return the result of substituting ARGS into the template parameters
12649 given by PARMS. If there are m levels of ARGS and m + n levels of
12650 PARMS, then the result will contain n levels of PARMS. For
12651 example, if PARMS is `template <class T> template <class U>
12652 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12653 result will be `template <int*, double, class V>'. */
12654
12655 static tree
12656 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12657 {
12658 tree r = NULL_TREE;
12659 tree* new_parms;
12660
12661 /* When substituting into a template, we must set
12662 PROCESSING_TEMPLATE_DECL as the template parameters may be
12663 dependent if they are based on one-another, and the dependency
12664 predicates are short-circuit outside of templates. */
12665 ++processing_template_decl;
12666
12667 for (new_parms = &r;
12668 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12669 new_parms = &(TREE_CHAIN (*new_parms)),
12670 parms = TREE_CHAIN (parms))
12671 {
12672 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12673 args, complain);
12674 *new_parms =
12675 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12676 - TMPL_ARGS_DEPTH (args)),
12677 new_vec, NULL_TREE);
12678 }
12679
12680 --processing_template_decl;
12681
12682 return r;
12683 }
12684
12685 /* Return the result of substituting ARGS into one template parameter
12686 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12687 parameter and which TREE_PURPOSE is the default argument of the
12688 template parameter. */
12689
12690 static tree
12691 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12692 {
12693 tree default_value, parm_decl;
12694
12695 if (args == NULL_TREE
12696 || t == NULL_TREE
12697 || t == error_mark_node)
12698 return t;
12699
12700 gcc_assert (TREE_CODE (t) == TREE_LIST);
12701
12702 default_value = TREE_PURPOSE (t);
12703 parm_decl = TREE_VALUE (t);
12704
12705 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12706 if (TREE_CODE (parm_decl) == PARM_DECL
12707 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12708 parm_decl = error_mark_node;
12709 default_value = tsubst_template_arg (default_value, args,
12710 complain, NULL_TREE);
12711
12712 return build_tree_list (default_value, parm_decl);
12713 }
12714
12715 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12716 type T. If T is not an aggregate or enumeration type, it is
12717 handled as if by tsubst. IN_DECL is as for tsubst. If
12718 ENTERING_SCOPE is nonzero, T is the context for a template which
12719 we are presently tsubst'ing. Return the substituted value. */
12720
12721 static tree
12722 tsubst_aggr_type (tree t,
12723 tree args,
12724 tsubst_flags_t complain,
12725 tree in_decl,
12726 int entering_scope)
12727 {
12728 if (t == NULL_TREE)
12729 return NULL_TREE;
12730
12731 switch (TREE_CODE (t))
12732 {
12733 case RECORD_TYPE:
12734 if (TYPE_PTRMEMFUNC_P (t))
12735 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12736
12737 /* Fall through. */
12738 case ENUMERAL_TYPE:
12739 case UNION_TYPE:
12740 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12741 {
12742 tree argvec;
12743 tree context;
12744 tree r;
12745
12746 /* In "sizeof(X<I>)" we need to evaluate "I". */
12747 cp_evaluated ev;
12748
12749 /* First, determine the context for the type we are looking
12750 up. */
12751 context = TYPE_CONTEXT (t);
12752 if (context && TYPE_P (context))
12753 {
12754 context = tsubst_aggr_type (context, args, complain,
12755 in_decl, /*entering_scope=*/1);
12756 /* If context is a nested class inside a class template,
12757 it may still need to be instantiated (c++/33959). */
12758 context = complete_type (context);
12759 }
12760
12761 /* Then, figure out what arguments are appropriate for the
12762 type we are trying to find. For example, given:
12763
12764 template <class T> struct S;
12765 template <class T, class U> void f(T, U) { S<U> su; }
12766
12767 and supposing that we are instantiating f<int, double>,
12768 then our ARGS will be {int, double}, but, when looking up
12769 S we only want {double}. */
12770 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12771 complain, in_decl);
12772 if (argvec == error_mark_node)
12773 r = error_mark_node;
12774 else
12775 {
12776 r = lookup_template_class (t, argvec, in_decl, context,
12777 entering_scope, complain);
12778 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12779 }
12780
12781 return r;
12782 }
12783 else
12784 /* This is not a template type, so there's nothing to do. */
12785 return t;
12786
12787 default:
12788 return tsubst (t, args, complain, in_decl);
12789 }
12790 }
12791
12792 static GTY((cache)) tree_cache_map *defarg_inst;
12793
12794 /* Substitute into the default argument ARG (a default argument for
12795 FN), which has the indicated TYPE. */
12796
12797 tree
12798 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12799 tsubst_flags_t complain)
12800 {
12801 int errs = errorcount + sorrycount;
12802
12803 /* This can happen in invalid code. */
12804 if (TREE_CODE (arg) == DEFAULT_ARG)
12805 return arg;
12806
12807 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12808 parm = chain_index (parmnum, parm);
12809 tree parmtype = TREE_TYPE (parm);
12810 if (DECL_BY_REFERENCE (parm))
12811 parmtype = TREE_TYPE (parmtype);
12812 if (parmtype == error_mark_node)
12813 return error_mark_node;
12814
12815 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12816
12817 tree *slot;
12818 if (defarg_inst && (slot = defarg_inst->get (parm)))
12819 return *slot;
12820
12821 /* This default argument came from a template. Instantiate the
12822 default argument here, not in tsubst. In the case of
12823 something like:
12824
12825 template <class T>
12826 struct S {
12827 static T t();
12828 void f(T = t());
12829 };
12830
12831 we must be careful to do name lookup in the scope of S<T>,
12832 rather than in the current class. */
12833 push_to_top_level ();
12834 push_access_scope (fn);
12835 push_deferring_access_checks (dk_no_deferred);
12836 start_lambda_scope (parm);
12837
12838 /* The default argument expression may cause implicitly defined
12839 member functions to be synthesized, which will result in garbage
12840 collection. We must treat this situation as if we were within
12841 the body of function so as to avoid collecting live data on the
12842 stack. */
12843 ++function_depth;
12844 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12845 complain, NULL_TREE,
12846 /*integral_constant_expression_p=*/false);
12847 --function_depth;
12848
12849 finish_lambda_scope ();
12850
12851 /* Make sure the default argument is reasonable. */
12852 arg = check_default_argument (type, arg, complain);
12853
12854 if (errorcount+sorrycount > errs
12855 && (complain & tf_warning_or_error))
12856 inform (input_location,
12857 " when instantiating default argument for call to %qD", fn);
12858
12859 pop_deferring_access_checks ();
12860 pop_access_scope (fn);
12861 pop_from_top_level ();
12862
12863 if (arg != error_mark_node && !cp_unevaluated_operand)
12864 {
12865 if (!defarg_inst)
12866 defarg_inst = tree_cache_map::create_ggc (37);
12867 defarg_inst->put (parm, arg);
12868 }
12869
12870 return arg;
12871 }
12872
12873 /* Substitute into all the default arguments for FN. */
12874
12875 static void
12876 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12877 {
12878 tree arg;
12879 tree tmpl_args;
12880
12881 tmpl_args = DECL_TI_ARGS (fn);
12882
12883 /* If this function is not yet instantiated, we certainly don't need
12884 its default arguments. */
12885 if (uses_template_parms (tmpl_args))
12886 return;
12887 /* Don't do this again for clones. */
12888 if (DECL_CLONED_FUNCTION_P (fn))
12889 return;
12890
12891 int i = 0;
12892 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12893 arg;
12894 arg = TREE_CHAIN (arg), ++i)
12895 if (TREE_PURPOSE (arg))
12896 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12897 TREE_VALUE (arg),
12898 TREE_PURPOSE (arg),
12899 complain);
12900 }
12901
12902 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12903 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12904
12905 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12906
12907 void
12908 store_explicit_specifier (tree v, tree t)
12909 {
12910 if (!explicit_specifier_map)
12911 explicit_specifier_map = tree_cache_map::create_ggc (37);
12912 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12913 explicit_specifier_map->put (v, t);
12914 }
12915
12916 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12917
12918 static tree
12919 lookup_explicit_specifier (tree v)
12920 {
12921 return *explicit_specifier_map->get (v);
12922 }
12923
12924 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12925
12926 static tree
12927 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12928 tree lambda_fntype)
12929 {
12930 tree gen_tmpl, argvec;
12931 hashval_t hash = 0;
12932 tree in_decl = t;
12933
12934 /* Nobody should be tsubst'ing into non-template functions. */
12935 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12936
12937 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12938 {
12939 /* If T is not dependent, just return it. */
12940 if (!uses_template_parms (DECL_TI_ARGS (t))
12941 && !LAMBDA_FUNCTION_P (t))
12942 return t;
12943
12944 /* Calculate the most general template of which R is a
12945 specialization. */
12946 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12947
12948 /* We're substituting a lambda function under tsubst_lambda_expr but not
12949 directly from it; find the matching function we're already inside.
12950 But don't do this if T is a generic lambda with a single level of
12951 template parms, as in that case we're doing a normal instantiation. */
12952 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12953 && (!generic_lambda_fn_p (t)
12954 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12955 return enclosing_instantiation_of (t);
12956
12957 /* Calculate the complete set of arguments used to
12958 specialize R. */
12959 argvec = tsubst_template_args (DECL_TI_ARGS
12960 (DECL_TEMPLATE_RESULT
12961 (DECL_TI_TEMPLATE (t))),
12962 args, complain, in_decl);
12963 if (argvec == error_mark_node)
12964 return error_mark_node;
12965
12966 /* Check to see if we already have this specialization. */
12967 if (!lambda_fntype)
12968 {
12969 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12970 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12971 return spec;
12972 }
12973
12974 /* We can see more levels of arguments than parameters if
12975 there was a specialization of a member template, like
12976 this:
12977
12978 template <class T> struct S { template <class U> void f(); }
12979 template <> template <class U> void S<int>::f(U);
12980
12981 Here, we'll be substituting into the specialization,
12982 because that's where we can find the code we actually
12983 want to generate, but we'll have enough arguments for
12984 the most general template.
12985
12986 We also deal with the peculiar case:
12987
12988 template <class T> struct S {
12989 template <class U> friend void f();
12990 };
12991 template <class U> void f() {}
12992 template S<int>;
12993 template void f<double>();
12994
12995 Here, the ARGS for the instantiation of will be {int,
12996 double}. But, we only need as many ARGS as there are
12997 levels of template parameters in CODE_PATTERN. We are
12998 careful not to get fooled into reducing the ARGS in
12999 situations like:
13000
13001 template <class T> struct S { template <class U> void f(U); }
13002 template <class T> template <> void S<T>::f(int) {}
13003
13004 which we can spot because the pattern will be a
13005 specialization in this case. */
13006 int args_depth = TMPL_ARGS_DEPTH (args);
13007 int parms_depth =
13008 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13009
13010 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13011 args = get_innermost_template_args (args, parms_depth);
13012 }
13013 else
13014 {
13015 /* This special case arises when we have something like this:
13016
13017 template <class T> struct S {
13018 friend void f<int>(int, double);
13019 };
13020
13021 Here, the DECL_TI_TEMPLATE for the friend declaration
13022 will be an IDENTIFIER_NODE. We are being called from
13023 tsubst_friend_function, and we want only to create a
13024 new decl (R) with appropriate types so that we can call
13025 determine_specialization. */
13026 gen_tmpl = NULL_TREE;
13027 argvec = NULL_TREE;
13028 }
13029
13030 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13031 : NULL_TREE);
13032 tree ctx = closure ? closure : DECL_CONTEXT (t);
13033 bool member = ctx && TYPE_P (ctx);
13034
13035 if (member && !closure)
13036 ctx = tsubst_aggr_type (ctx, args,
13037 complain, t, /*entering_scope=*/1);
13038
13039 tree type = (lambda_fntype ? lambda_fntype
13040 : tsubst (TREE_TYPE (t), args,
13041 complain | tf_fndecl_type, in_decl));
13042 if (type == error_mark_node)
13043 return error_mark_node;
13044
13045 /* If we hit excessive deduction depth, the type is bogus even if
13046 it isn't error_mark_node, so don't build a decl. */
13047 if (excessive_deduction_depth)
13048 return error_mark_node;
13049
13050 /* We do NOT check for matching decls pushed separately at this
13051 point, as they may not represent instantiations of this
13052 template, and in any case are considered separate under the
13053 discrete model. */
13054 tree r = copy_decl (t);
13055 DECL_USE_TEMPLATE (r) = 0;
13056 TREE_TYPE (r) = type;
13057 /* Clear out the mangled name and RTL for the instantiation. */
13058 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13059 SET_DECL_RTL (r, NULL);
13060 /* Leave DECL_INITIAL set on deleted instantiations. */
13061 if (!DECL_DELETED_FN (r))
13062 DECL_INITIAL (r) = NULL_TREE;
13063 DECL_CONTEXT (r) = ctx;
13064
13065 /* Handle explicit(dependent-expr). */
13066 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13067 {
13068 tree spec = lookup_explicit_specifier (t);
13069 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13070 /*function_p=*/false,
13071 /*i_c_e_p=*/true);
13072 spec = build_explicit_specifier (spec, complain);
13073 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13074 }
13075
13076 /* OpenMP UDRs have the only argument a reference to the declared
13077 type. We want to diagnose if the declared type is a reference,
13078 which is invalid, but as references to references are usually
13079 quietly merged, diagnose it here. */
13080 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13081 {
13082 tree argtype
13083 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13084 argtype = tsubst (argtype, args, complain, in_decl);
13085 if (TYPE_REF_P (argtype))
13086 error_at (DECL_SOURCE_LOCATION (t),
13087 "reference type %qT in "
13088 "%<#pragma omp declare reduction%>", argtype);
13089 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13090 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13091 argtype);
13092 }
13093
13094 if (member && DECL_CONV_FN_P (r))
13095 /* Type-conversion operator. Reconstruct the name, in
13096 case it's the name of one of the template's parameters. */
13097 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13098
13099 tree parms = DECL_ARGUMENTS (t);
13100 if (closure)
13101 parms = DECL_CHAIN (parms);
13102 parms = tsubst (parms, args, complain, t);
13103 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13104 DECL_CONTEXT (parm) = r;
13105 if (closure)
13106 {
13107 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13108 DECL_CHAIN (tparm) = parms;
13109 parms = tparm;
13110 }
13111 DECL_ARGUMENTS (r) = parms;
13112 DECL_RESULT (r) = NULL_TREE;
13113
13114 TREE_STATIC (r) = 0;
13115 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13116 DECL_EXTERNAL (r) = 1;
13117 /* If this is an instantiation of a function with internal
13118 linkage, we already know what object file linkage will be
13119 assigned to the instantiation. */
13120 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13121 DECL_DEFER_OUTPUT (r) = 0;
13122 DECL_CHAIN (r) = NULL_TREE;
13123 DECL_PENDING_INLINE_INFO (r) = 0;
13124 DECL_PENDING_INLINE_P (r) = 0;
13125 DECL_SAVED_TREE (r) = NULL_TREE;
13126 DECL_STRUCT_FUNCTION (r) = NULL;
13127 TREE_USED (r) = 0;
13128 /* We'll re-clone as appropriate in instantiate_template. */
13129 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13130
13131 /* If we aren't complaining now, return on error before we register
13132 the specialization so that we'll complain eventually. */
13133 if ((complain & tf_error) == 0
13134 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13135 && !grok_op_properties (r, /*complain=*/false))
13136 return error_mark_node;
13137
13138 /* When instantiating a constrained member, substitute
13139 into the constraints to create a new constraint. */
13140 if (tree ci = get_constraints (t))
13141 if (member)
13142 {
13143 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13144 set_constraints (r, ci);
13145 }
13146
13147 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13148 SET_DECL_FRIEND_CONTEXT (r,
13149 tsubst (DECL_FRIEND_CONTEXT (t),
13150 args, complain, in_decl));
13151
13152 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13153 this in the special friend case mentioned above where
13154 GEN_TMPL is NULL. */
13155 if (gen_tmpl && !closure)
13156 {
13157 DECL_TEMPLATE_INFO (r)
13158 = build_template_info (gen_tmpl, argvec);
13159 SET_DECL_IMPLICIT_INSTANTIATION (r);
13160
13161 tree new_r
13162 = register_specialization (r, gen_tmpl, argvec, false, hash);
13163 if (new_r != r)
13164 /* We instantiated this while substituting into
13165 the type earlier (template/friend54.C). */
13166 return new_r;
13167
13168 /* We're not supposed to instantiate default arguments
13169 until they are called, for a template. But, for a
13170 declaration like:
13171
13172 template <class T> void f ()
13173 { extern void g(int i = T()); }
13174
13175 we should do the substitution when the template is
13176 instantiated. We handle the member function case in
13177 instantiate_class_template since the default arguments
13178 might refer to other members of the class. */
13179 if (!member
13180 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13181 && !uses_template_parms (argvec))
13182 tsubst_default_arguments (r, complain);
13183 }
13184 else
13185 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13186
13187 /* Copy the list of befriending classes. */
13188 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13189 *friends;
13190 friends = &TREE_CHAIN (*friends))
13191 {
13192 *friends = copy_node (*friends);
13193 TREE_VALUE (*friends)
13194 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13195 }
13196
13197 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13198 {
13199 maybe_retrofit_in_chrg (r);
13200 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13201 return error_mark_node;
13202 /* If this is an instantiation of a member template, clone it.
13203 If it isn't, that'll be handled by
13204 clone_constructors_and_destructors. */
13205 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13206 clone_function_decl (r, /*update_methods=*/false);
13207 }
13208 else if ((complain & tf_error) != 0
13209 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13210 && !grok_op_properties (r, /*complain=*/true))
13211 return error_mark_node;
13212
13213 /* Possibly limit visibility based on template args. */
13214 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13215 if (DECL_VISIBILITY_SPECIFIED (t))
13216 {
13217 DECL_VISIBILITY_SPECIFIED (r) = 0;
13218 DECL_ATTRIBUTES (r)
13219 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13220 }
13221 determine_visibility (r);
13222 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13223 && !processing_template_decl)
13224 defaulted_late_check (r);
13225
13226 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13227 args, complain, in_decl);
13228 return r;
13229 }
13230
13231 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13232
13233 static tree
13234 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13235 tree lambda_fntype)
13236 {
13237 /* We can get here when processing a member function template,
13238 member class template, or template template parameter. */
13239 tree decl = DECL_TEMPLATE_RESULT (t);
13240 tree in_decl = t;
13241 tree spec;
13242 tree tmpl_args;
13243 tree full_args;
13244 tree r;
13245 hashval_t hash = 0;
13246
13247 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13248 {
13249 /* Template template parameter is treated here. */
13250 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13251 if (new_type == error_mark_node)
13252 r = error_mark_node;
13253 /* If we get a real template back, return it. This can happen in
13254 the context of most_specialized_partial_spec. */
13255 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13256 r = new_type;
13257 else
13258 /* The new TEMPLATE_DECL was built in
13259 reduce_template_parm_level. */
13260 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13261 return r;
13262 }
13263
13264 if (!lambda_fntype)
13265 {
13266 /* We might already have an instance of this template.
13267 The ARGS are for the surrounding class type, so the
13268 full args contain the tsubst'd args for the context,
13269 plus the innermost args from the template decl. */
13270 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13271 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13272 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13273 /* Because this is a template, the arguments will still be
13274 dependent, even after substitution. If
13275 PROCESSING_TEMPLATE_DECL is not set, the dependency
13276 predicates will short-circuit. */
13277 ++processing_template_decl;
13278 full_args = tsubst_template_args (tmpl_args, args,
13279 complain, in_decl);
13280 --processing_template_decl;
13281 if (full_args == error_mark_node)
13282 return error_mark_node;
13283
13284 /* If this is a default template template argument,
13285 tsubst might not have changed anything. */
13286 if (full_args == tmpl_args)
13287 return t;
13288
13289 hash = hash_tmpl_and_args (t, full_args);
13290 spec = retrieve_specialization (t, full_args, hash);
13291 if (spec != NULL_TREE)
13292 {
13293 if (TYPE_P (spec))
13294 /* Type partial instantiations are stored as the type by
13295 lookup_template_class_1, not here as the template. */
13296 spec = CLASSTYPE_TI_TEMPLATE (spec);
13297 return spec;
13298 }
13299 }
13300
13301 /* Make a new template decl. It will be similar to the
13302 original, but will record the current template arguments.
13303 We also create a new function declaration, which is just
13304 like the old one, but points to this new template, rather
13305 than the old one. */
13306 r = copy_decl (t);
13307 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13308 DECL_CHAIN (r) = NULL_TREE;
13309
13310 // Build new template info linking to the original template decl.
13311 if (!lambda_fntype)
13312 {
13313 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13314 SET_DECL_IMPLICIT_INSTANTIATION (r);
13315 }
13316 else
13317 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13318
13319 /* The template parameters for this new template are all the
13320 template parameters for the old template, except the
13321 outermost level of parameters. */
13322 DECL_TEMPLATE_PARMS (r)
13323 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13324 complain);
13325
13326 if (TREE_CODE (decl) == TYPE_DECL
13327 && !TYPE_DECL_ALIAS_P (decl))
13328 {
13329 tree new_type;
13330 ++processing_template_decl;
13331 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13332 --processing_template_decl;
13333 if (new_type == error_mark_node)
13334 return error_mark_node;
13335
13336 TREE_TYPE (r) = new_type;
13337 /* For a partial specialization, we need to keep pointing to
13338 the primary template. */
13339 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13340 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13341 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13342 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13343 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13344 }
13345 else
13346 {
13347 tree new_decl;
13348 ++processing_template_decl;
13349 if (TREE_CODE (decl) == FUNCTION_DECL)
13350 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13351 else
13352 new_decl = tsubst (decl, args, complain, in_decl);
13353 --processing_template_decl;
13354 if (new_decl == error_mark_node)
13355 return error_mark_node;
13356
13357 DECL_TEMPLATE_RESULT (r) = new_decl;
13358 TREE_TYPE (r) = TREE_TYPE (new_decl);
13359 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13360 if (lambda_fntype)
13361 {
13362 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13363 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13364 }
13365 else
13366 {
13367 DECL_TI_TEMPLATE (new_decl) = r;
13368 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13369 }
13370 }
13371
13372 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13373 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13374
13375 if (PRIMARY_TEMPLATE_P (t))
13376 DECL_PRIMARY_TEMPLATE (r) = r;
13377
13378 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13379 && !lambda_fntype)
13380 /* Record this non-type partial instantiation. */
13381 register_specialization (r, t,
13382 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13383 false, hash);
13384
13385 return r;
13386 }
13387
13388 /* True if FN is the op() for a lambda in an uninstantiated template. */
13389
13390 bool
13391 lambda_fn_in_template_p (tree fn)
13392 {
13393 if (!fn || !LAMBDA_FUNCTION_P (fn))
13394 return false;
13395 tree closure = DECL_CONTEXT (fn);
13396 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13397 }
13398
13399 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13400 which the above is true. */
13401
13402 bool
13403 instantiated_lambda_fn_p (tree fn)
13404 {
13405 if (!fn || !LAMBDA_FUNCTION_P (fn))
13406 return false;
13407 tree closure = DECL_CONTEXT (fn);
13408 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13409 return LAMBDA_EXPR_INSTANTIATED (lam);
13410 }
13411
13412 /* We're instantiating a variable from template function TCTX. Return the
13413 corresponding current enclosing scope. This gets complicated because lambda
13414 functions in templates are regenerated rather than instantiated, but generic
13415 lambda functions are subsequently instantiated. */
13416
13417 static tree
13418 enclosing_instantiation_of (tree otctx)
13419 {
13420 tree tctx = otctx;
13421 tree fn = current_function_decl;
13422 int lambda_count = 0;
13423
13424 for (; tctx && (lambda_fn_in_template_p (tctx)
13425 || instantiated_lambda_fn_p (tctx));
13426 tctx = decl_function_context (tctx))
13427 ++lambda_count;
13428 for (; fn; fn = decl_function_context (fn))
13429 {
13430 tree ofn = fn;
13431 int flambda_count = 0;
13432 for (; fn && instantiated_lambda_fn_p (fn);
13433 fn = decl_function_context (fn))
13434 ++flambda_count;
13435 if ((fn && DECL_TEMPLATE_INFO (fn))
13436 ? most_general_template (fn) != most_general_template (tctx)
13437 : fn != tctx)
13438 continue;
13439 if (flambda_count != lambda_count)
13440 {
13441 gcc_assert (flambda_count > lambda_count);
13442 for (; flambda_count > lambda_count; --flambda_count)
13443 ofn = decl_function_context (ofn);
13444 }
13445 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13446 || DECL_CONV_FN_P (ofn));
13447 return ofn;
13448 }
13449 gcc_unreachable ();
13450 }
13451
13452 /* Substitute the ARGS into the T, which is a _DECL. Return the
13453 result of the substitution. Issue error and warning messages under
13454 control of COMPLAIN. */
13455
13456 static tree
13457 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13458 {
13459 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13460 location_t saved_loc;
13461 tree r = NULL_TREE;
13462 tree in_decl = t;
13463 hashval_t hash = 0;
13464
13465 /* Set the filename and linenumber to improve error-reporting. */
13466 saved_loc = input_location;
13467 input_location = DECL_SOURCE_LOCATION (t);
13468
13469 switch (TREE_CODE (t))
13470 {
13471 case TEMPLATE_DECL:
13472 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13473 break;
13474
13475 case FUNCTION_DECL:
13476 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13477 break;
13478
13479 case PARM_DECL:
13480 {
13481 tree type = NULL_TREE;
13482 int i, len = 1;
13483 tree expanded_types = NULL_TREE;
13484 tree prev_r = NULL_TREE;
13485 tree first_r = NULL_TREE;
13486
13487 if (DECL_PACK_P (t))
13488 {
13489 /* If there is a local specialization that isn't a
13490 parameter pack, it means that we're doing a "simple"
13491 substitution from inside tsubst_pack_expansion. Just
13492 return the local specialization (which will be a single
13493 parm). */
13494 tree spec = retrieve_local_specialization (t);
13495 if (spec
13496 && TREE_CODE (spec) == PARM_DECL
13497 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13498 RETURN (spec);
13499
13500 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13501 the parameters in this function parameter pack. */
13502 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13503 complain, in_decl);
13504 if (TREE_CODE (expanded_types) == TREE_VEC)
13505 {
13506 len = TREE_VEC_LENGTH (expanded_types);
13507
13508 /* Zero-length parameter packs are boring. Just substitute
13509 into the chain. */
13510 if (len == 0)
13511 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13512 TREE_CHAIN (t)));
13513 }
13514 else
13515 {
13516 /* All we did was update the type. Make a note of that. */
13517 type = expanded_types;
13518 expanded_types = NULL_TREE;
13519 }
13520 }
13521
13522 /* Loop through all of the parameters we'll build. When T is
13523 a function parameter pack, LEN is the number of expanded
13524 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13525 r = NULL_TREE;
13526 for (i = 0; i < len; ++i)
13527 {
13528 prev_r = r;
13529 r = copy_node (t);
13530 if (DECL_TEMPLATE_PARM_P (t))
13531 SET_DECL_TEMPLATE_PARM_P (r);
13532
13533 if (expanded_types)
13534 /* We're on the Ith parameter of the function parameter
13535 pack. */
13536 {
13537 /* Get the Ith type. */
13538 type = TREE_VEC_ELT (expanded_types, i);
13539
13540 /* Rename the parameter to include the index. */
13541 DECL_NAME (r)
13542 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13543 }
13544 else if (!type)
13545 /* We're dealing with a normal parameter. */
13546 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13547
13548 type = type_decays_to (type);
13549 TREE_TYPE (r) = type;
13550 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13551
13552 if (DECL_INITIAL (r))
13553 {
13554 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13555 DECL_INITIAL (r) = TREE_TYPE (r);
13556 else
13557 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13558 complain, in_decl);
13559 }
13560
13561 DECL_CONTEXT (r) = NULL_TREE;
13562
13563 if (!DECL_TEMPLATE_PARM_P (r))
13564 DECL_ARG_TYPE (r) = type_passed_as (type);
13565
13566 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13567 args, complain, in_decl);
13568
13569 /* Keep track of the first new parameter we
13570 generate. That's what will be returned to the
13571 caller. */
13572 if (!first_r)
13573 first_r = r;
13574
13575 /* Build a proper chain of parameters when substituting
13576 into a function parameter pack. */
13577 if (prev_r)
13578 DECL_CHAIN (prev_r) = r;
13579 }
13580
13581 /* If cp_unevaluated_operand is set, we're just looking for a
13582 single dummy parameter, so don't keep going. */
13583 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13584 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13585 complain, DECL_CHAIN (t));
13586
13587 /* FIRST_R contains the start of the chain we've built. */
13588 r = first_r;
13589 }
13590 break;
13591
13592 case FIELD_DECL:
13593 {
13594 tree type = NULL_TREE;
13595 tree vec = NULL_TREE;
13596 tree expanded_types = NULL_TREE;
13597 int len = 1;
13598
13599 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13600 {
13601 /* This field is a lambda capture pack. Return a TREE_VEC of
13602 the expanded fields to instantiate_class_template_1. */
13603 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13604 complain, in_decl);
13605 if (TREE_CODE (expanded_types) == TREE_VEC)
13606 {
13607 len = TREE_VEC_LENGTH (expanded_types);
13608 vec = make_tree_vec (len);
13609 }
13610 else
13611 {
13612 /* All we did was update the type. Make a note of that. */
13613 type = expanded_types;
13614 expanded_types = NULL_TREE;
13615 }
13616 }
13617
13618 for (int i = 0; i < len; ++i)
13619 {
13620 r = copy_decl (t);
13621 if (expanded_types)
13622 {
13623 type = TREE_VEC_ELT (expanded_types, i);
13624 DECL_NAME (r)
13625 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13626 }
13627 else if (!type)
13628 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13629
13630 if (type == error_mark_node)
13631 RETURN (error_mark_node);
13632 TREE_TYPE (r) = type;
13633 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13634
13635 if (DECL_C_BIT_FIELD (r))
13636 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13637 number of bits. */
13638 DECL_BIT_FIELD_REPRESENTATIVE (r)
13639 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13640 complain, in_decl,
13641 /*integral_constant_expression_p=*/true);
13642 if (DECL_INITIAL (t))
13643 {
13644 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13645 NSDMI in perform_member_init. Still set DECL_INITIAL
13646 so that we know there is one. */
13647 DECL_INITIAL (r) = void_node;
13648 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13649 retrofit_lang_decl (r);
13650 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13651 }
13652 /* We don't have to set DECL_CONTEXT here; it is set by
13653 finish_member_declaration. */
13654 DECL_CHAIN (r) = NULL_TREE;
13655
13656 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13657 args, complain, in_decl);
13658
13659 if (vec)
13660 TREE_VEC_ELT (vec, i) = r;
13661 }
13662
13663 if (vec)
13664 r = vec;
13665 }
13666 break;
13667
13668 case USING_DECL:
13669 /* We reach here only for member using decls. We also need to check
13670 uses_template_parms because DECL_DEPENDENT_P is not set for a
13671 using-declaration that designates a member of the current
13672 instantiation (c++/53549). */
13673 if (DECL_DEPENDENT_P (t)
13674 || uses_template_parms (USING_DECL_SCOPE (t)))
13675 {
13676 tree scope = USING_DECL_SCOPE (t);
13677 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13678 if (PACK_EXPANSION_P (scope))
13679 {
13680 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13681 int len = TREE_VEC_LENGTH (vec);
13682 r = make_tree_vec (len);
13683 for (int i = 0; i < len; ++i)
13684 {
13685 tree escope = TREE_VEC_ELT (vec, i);
13686 tree elt = do_class_using_decl (escope, name);
13687 if (!elt)
13688 {
13689 r = error_mark_node;
13690 break;
13691 }
13692 else
13693 {
13694 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13695 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13696 }
13697 TREE_VEC_ELT (r, i) = elt;
13698 }
13699 }
13700 else
13701 {
13702 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13703 complain, in_decl);
13704 r = do_class_using_decl (inst_scope, name);
13705 if (!r)
13706 r = error_mark_node;
13707 else
13708 {
13709 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13710 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13711 }
13712 }
13713 }
13714 else
13715 {
13716 r = copy_node (t);
13717 DECL_CHAIN (r) = NULL_TREE;
13718 }
13719 break;
13720
13721 case TYPE_DECL:
13722 case VAR_DECL:
13723 {
13724 tree argvec = NULL_TREE;
13725 tree gen_tmpl = NULL_TREE;
13726 tree spec;
13727 tree tmpl = NULL_TREE;
13728 tree ctx;
13729 tree type = NULL_TREE;
13730 bool local_p;
13731
13732 if (TREE_TYPE (t) == error_mark_node)
13733 RETURN (error_mark_node);
13734
13735 if (TREE_CODE (t) == TYPE_DECL
13736 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13737 {
13738 /* If this is the canonical decl, we don't have to
13739 mess with instantiations, and often we can't (for
13740 typename, template type parms and such). Note that
13741 TYPE_NAME is not correct for the above test if
13742 we've copied the type for a typedef. */
13743 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13744 if (type == error_mark_node)
13745 RETURN (error_mark_node);
13746 r = TYPE_NAME (type);
13747 break;
13748 }
13749
13750 /* Check to see if we already have the specialization we
13751 need. */
13752 spec = NULL_TREE;
13753 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13754 {
13755 /* T is a static data member or namespace-scope entity.
13756 We have to substitute into namespace-scope variables
13757 (not just variable templates) because of cases like:
13758
13759 template <class T> void f() { extern T t; }
13760
13761 where the entity referenced is not known until
13762 instantiation time. */
13763 local_p = false;
13764 ctx = DECL_CONTEXT (t);
13765 if (DECL_CLASS_SCOPE_P (t))
13766 {
13767 ctx = tsubst_aggr_type (ctx, args,
13768 complain,
13769 in_decl, /*entering_scope=*/1);
13770 /* If CTX is unchanged, then T is in fact the
13771 specialization we want. That situation occurs when
13772 referencing a static data member within in its own
13773 class. We can use pointer equality, rather than
13774 same_type_p, because DECL_CONTEXT is always
13775 canonical... */
13776 if (ctx == DECL_CONTEXT (t)
13777 /* ... unless T is a member template; in which
13778 case our caller can be willing to create a
13779 specialization of that template represented
13780 by T. */
13781 && !(DECL_TI_TEMPLATE (t)
13782 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13783 spec = t;
13784 }
13785
13786 if (!spec)
13787 {
13788 tmpl = DECL_TI_TEMPLATE (t);
13789 gen_tmpl = most_general_template (tmpl);
13790 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13791 if (argvec != error_mark_node)
13792 argvec = (coerce_innermost_template_parms
13793 (DECL_TEMPLATE_PARMS (gen_tmpl),
13794 argvec, t, complain,
13795 /*all*/true, /*defarg*/true));
13796 if (argvec == error_mark_node)
13797 RETURN (error_mark_node);
13798 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13799 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13800 }
13801 }
13802 else
13803 {
13804 /* A local variable. */
13805 local_p = true;
13806 /* Subsequent calls to pushdecl will fill this in. */
13807 ctx = NULL_TREE;
13808 /* Unless this is a reference to a static variable from an
13809 enclosing function, in which case we need to fill it in now. */
13810 if (TREE_STATIC (t))
13811 {
13812 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13813 if (fn != current_function_decl)
13814 ctx = fn;
13815 }
13816 spec = retrieve_local_specialization (t);
13817 }
13818 /* If we already have the specialization we need, there is
13819 nothing more to do. */
13820 if (spec)
13821 {
13822 r = spec;
13823 break;
13824 }
13825
13826 /* Create a new node for the specialization we need. */
13827 if (type == NULL_TREE)
13828 {
13829 if (is_typedef_decl (t))
13830 type = DECL_ORIGINAL_TYPE (t);
13831 else
13832 type = TREE_TYPE (t);
13833 if (VAR_P (t)
13834 && VAR_HAD_UNKNOWN_BOUND (t)
13835 && type != error_mark_node)
13836 type = strip_array_domain (type);
13837 tree sub_args = args;
13838 if (tree auto_node = type_uses_auto (type))
13839 {
13840 /* Mask off any template args past the variable's context so we
13841 don't replace the auto with an unrelated argument. */
13842 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13843 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13844 if (extra > 0)
13845 /* This should never happen with the new lambda instantiation
13846 model, but keep the handling just in case. */
13847 gcc_assert (!CHECKING_P),
13848 sub_args = strip_innermost_template_args (args, extra);
13849 }
13850 type = tsubst (type, sub_args, complain, in_decl);
13851 /* Substituting the type might have recursively instantiated this
13852 same alias (c++/86171). */
13853 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13854 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13855 {
13856 r = spec;
13857 break;
13858 }
13859 }
13860 r = copy_decl (t);
13861 if (VAR_P (r))
13862 {
13863 DECL_INITIALIZED_P (r) = 0;
13864 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13865 if (type == error_mark_node)
13866 RETURN (error_mark_node);
13867 if (TREE_CODE (type) == FUNCTION_TYPE)
13868 {
13869 /* It may seem that this case cannot occur, since:
13870
13871 typedef void f();
13872 void g() { f x; }
13873
13874 declares a function, not a variable. However:
13875
13876 typedef void f();
13877 template <typename T> void g() { T t; }
13878 template void g<f>();
13879
13880 is an attempt to declare a variable with function
13881 type. */
13882 error ("variable %qD has function type",
13883 /* R is not yet sufficiently initialized, so we
13884 just use its name. */
13885 DECL_NAME (r));
13886 RETURN (error_mark_node);
13887 }
13888 type = complete_type (type);
13889 /* Wait until cp_finish_decl to set this again, to handle
13890 circular dependency (template/instantiate6.C). */
13891 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13892 type = check_var_type (DECL_NAME (r), type);
13893
13894 if (DECL_HAS_VALUE_EXPR_P (t))
13895 {
13896 tree ve = DECL_VALUE_EXPR (t);
13897 ve = tsubst_expr (ve, args, complain, in_decl,
13898 /*constant_expression_p=*/false);
13899 if (REFERENCE_REF_P (ve))
13900 {
13901 gcc_assert (TYPE_REF_P (type));
13902 ve = TREE_OPERAND (ve, 0);
13903 }
13904 SET_DECL_VALUE_EXPR (r, ve);
13905 }
13906 if (CP_DECL_THREAD_LOCAL_P (r)
13907 && !processing_template_decl)
13908 set_decl_tls_model (r, decl_default_tls_model (r));
13909 }
13910 else if (DECL_SELF_REFERENCE_P (t))
13911 SET_DECL_SELF_REFERENCE_P (r);
13912 TREE_TYPE (r) = type;
13913 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13914 DECL_CONTEXT (r) = ctx;
13915 /* Clear out the mangled name and RTL for the instantiation. */
13916 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13917 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13918 SET_DECL_RTL (r, NULL);
13919 /* The initializer must not be expanded until it is required;
13920 see [temp.inst]. */
13921 DECL_INITIAL (r) = NULL_TREE;
13922 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13923 if (VAR_P (r))
13924 {
13925 if (DECL_LANG_SPECIFIC (r))
13926 SET_DECL_DEPENDENT_INIT_P (r, false);
13927
13928 SET_DECL_MODE (r, VOIDmode);
13929
13930 /* Possibly limit visibility based on template args. */
13931 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13932 if (DECL_VISIBILITY_SPECIFIED (t))
13933 {
13934 DECL_VISIBILITY_SPECIFIED (r) = 0;
13935 DECL_ATTRIBUTES (r)
13936 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13937 }
13938 determine_visibility (r);
13939 }
13940
13941 if (!local_p)
13942 {
13943 /* A static data member declaration is always marked
13944 external when it is declared in-class, even if an
13945 initializer is present. We mimic the non-template
13946 processing here. */
13947 DECL_EXTERNAL (r) = 1;
13948 if (DECL_NAMESPACE_SCOPE_P (t))
13949 DECL_NOT_REALLY_EXTERN (r) = 1;
13950
13951 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13952 SET_DECL_IMPLICIT_INSTANTIATION (r);
13953 if (!error_operand_p (r) || (complain & tf_error))
13954 register_specialization (r, gen_tmpl, argvec, false, hash);
13955 }
13956 else
13957 {
13958 if (DECL_LANG_SPECIFIC (r))
13959 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13960 if (!cp_unevaluated_operand)
13961 register_local_specialization (r, t);
13962 }
13963
13964 DECL_CHAIN (r) = NULL_TREE;
13965
13966 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13967 /*flags=*/0,
13968 args, complain, in_decl);
13969
13970 /* Preserve a typedef that names a type. */
13971 if (is_typedef_decl (r) && type != error_mark_node)
13972 {
13973 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13974 set_underlying_type (r);
13975 if (TYPE_DECL_ALIAS_P (r))
13976 /* An alias template specialization can be dependent
13977 even if its underlying type is not. */
13978 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13979 }
13980
13981 layout_decl (r, 0);
13982 }
13983 break;
13984
13985 default:
13986 gcc_unreachable ();
13987 }
13988 #undef RETURN
13989
13990 out:
13991 /* Restore the file and line information. */
13992 input_location = saved_loc;
13993
13994 return r;
13995 }
13996
13997 /* Substitute into the ARG_TYPES of a function type.
13998 If END is a TREE_CHAIN, leave it and any following types
13999 un-substituted. */
14000
14001 static tree
14002 tsubst_arg_types (tree arg_types,
14003 tree args,
14004 tree end,
14005 tsubst_flags_t complain,
14006 tree in_decl)
14007 {
14008 tree remaining_arg_types;
14009 tree type = NULL_TREE;
14010 int i = 1;
14011 tree expanded_args = NULL_TREE;
14012 tree default_arg;
14013
14014 if (!arg_types || arg_types == void_list_node || arg_types == end)
14015 return arg_types;
14016
14017 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14018 args, end, complain, in_decl);
14019 if (remaining_arg_types == error_mark_node)
14020 return error_mark_node;
14021
14022 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14023 {
14024 /* For a pack expansion, perform substitution on the
14025 entire expression. Later on, we'll handle the arguments
14026 one-by-one. */
14027 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14028 args, complain, in_decl);
14029
14030 if (TREE_CODE (expanded_args) == TREE_VEC)
14031 /* So that we'll spin through the parameters, one by one. */
14032 i = TREE_VEC_LENGTH (expanded_args);
14033 else
14034 {
14035 /* We only partially substituted into the parameter
14036 pack. Our type is TYPE_PACK_EXPANSION. */
14037 type = expanded_args;
14038 expanded_args = NULL_TREE;
14039 }
14040 }
14041
14042 while (i > 0) {
14043 --i;
14044
14045 if (expanded_args)
14046 type = TREE_VEC_ELT (expanded_args, i);
14047 else if (!type)
14048 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14049
14050 if (type == error_mark_node)
14051 return error_mark_node;
14052 if (VOID_TYPE_P (type))
14053 {
14054 if (complain & tf_error)
14055 {
14056 error ("invalid parameter type %qT", type);
14057 if (in_decl)
14058 error ("in declaration %q+D", in_decl);
14059 }
14060 return error_mark_node;
14061 }
14062 /* DR 657. */
14063 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14064 return error_mark_node;
14065
14066 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14067 top-level qualifiers as required. */
14068 type = cv_unqualified (type_decays_to (type));
14069
14070 /* We do not substitute into default arguments here. The standard
14071 mandates that they be instantiated only when needed, which is
14072 done in build_over_call. */
14073 default_arg = TREE_PURPOSE (arg_types);
14074
14075 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14076 since the new op() won't have any associated template arguments for us
14077 to refer to later. */
14078 if (lambda_fn_in_template_p (in_decl))
14079 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14080 false/*fn*/, false/*constexpr*/);
14081
14082 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
14083 {
14084 /* We've instantiated a template before its default arguments
14085 have been parsed. This can happen for a nested template
14086 class, and is not an error unless we require the default
14087 argument in a call of this function. */
14088 remaining_arg_types =
14089 tree_cons (default_arg, type, remaining_arg_types);
14090 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
14091 }
14092 else
14093 remaining_arg_types =
14094 hash_tree_cons (default_arg, type, remaining_arg_types);
14095 }
14096
14097 return remaining_arg_types;
14098 }
14099
14100 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14101 *not* handle the exception-specification for FNTYPE, because the
14102 initial substitution of explicitly provided template parameters
14103 during argument deduction forbids substitution into the
14104 exception-specification:
14105
14106 [temp.deduct]
14107
14108 All references in the function type of the function template to the
14109 corresponding template parameters are replaced by the specified tem-
14110 plate argument values. If a substitution in a template parameter or
14111 in the function type of the function template results in an invalid
14112 type, type deduction fails. [Note: The equivalent substitution in
14113 exception specifications is done only when the function is instanti-
14114 ated, at which point a program is ill-formed if the substitution
14115 results in an invalid type.] */
14116
14117 static tree
14118 tsubst_function_type (tree t,
14119 tree args,
14120 tsubst_flags_t complain,
14121 tree in_decl)
14122 {
14123 tree return_type;
14124 tree arg_types = NULL_TREE;
14125 tree fntype;
14126
14127 /* The TYPE_CONTEXT is not used for function/method types. */
14128 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14129
14130 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14131 failure. */
14132 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14133
14134 if (late_return_type_p)
14135 {
14136 /* Substitute the argument types. */
14137 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14138 complain, in_decl);
14139 if (arg_types == error_mark_node)
14140 return error_mark_node;
14141
14142 tree save_ccp = current_class_ptr;
14143 tree save_ccr = current_class_ref;
14144 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14145 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14146 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14147 if (do_inject)
14148 {
14149 /* DR 1207: 'this' is in scope in the trailing return type. */
14150 inject_this_parameter (this_type, cp_type_quals (this_type));
14151 }
14152
14153 /* Substitute the return type. */
14154 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14155
14156 if (do_inject)
14157 {
14158 current_class_ptr = save_ccp;
14159 current_class_ref = save_ccr;
14160 }
14161 }
14162 else
14163 /* Substitute the return type. */
14164 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14165
14166 if (return_type == error_mark_node)
14167 return error_mark_node;
14168 /* DR 486 clarifies that creation of a function type with an
14169 invalid return type is a deduction failure. */
14170 if (TREE_CODE (return_type) == ARRAY_TYPE
14171 || TREE_CODE (return_type) == FUNCTION_TYPE)
14172 {
14173 if (complain & tf_error)
14174 {
14175 if (TREE_CODE (return_type) == ARRAY_TYPE)
14176 error ("function returning an array");
14177 else
14178 error ("function returning a function");
14179 }
14180 return error_mark_node;
14181 }
14182 /* And DR 657. */
14183 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14184 return error_mark_node;
14185
14186 if (!late_return_type_p)
14187 {
14188 /* Substitute the argument types. */
14189 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14190 complain, in_decl);
14191 if (arg_types == error_mark_node)
14192 return error_mark_node;
14193 }
14194
14195 /* Construct a new type node and return it. */
14196 if (TREE_CODE (t) == FUNCTION_TYPE)
14197 {
14198 fntype = build_function_type (return_type, arg_types);
14199 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14200 }
14201 else
14202 {
14203 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14204 /* Don't pick up extra function qualifiers from the basetype. */
14205 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14206 if (! MAYBE_CLASS_TYPE_P (r))
14207 {
14208 /* [temp.deduct]
14209
14210 Type deduction may fail for any of the following
14211 reasons:
14212
14213 -- Attempting to create "pointer to member of T" when T
14214 is not a class type. */
14215 if (complain & tf_error)
14216 error ("creating pointer to member function of non-class type %qT",
14217 r);
14218 return error_mark_node;
14219 }
14220
14221 fntype = build_method_type_directly (r, return_type,
14222 TREE_CHAIN (arg_types));
14223 }
14224 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14225
14226 /* See comment above. */
14227 tree raises = NULL_TREE;
14228 cp_ref_qualifier rqual = type_memfn_rqual (t);
14229 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14230
14231 return fntype;
14232 }
14233
14234 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14235 ARGS into that specification, and return the substituted
14236 specification. If there is no specification, return NULL_TREE. */
14237
14238 static tree
14239 tsubst_exception_specification (tree fntype,
14240 tree args,
14241 tsubst_flags_t complain,
14242 tree in_decl,
14243 bool defer_ok)
14244 {
14245 tree specs;
14246 tree new_specs;
14247
14248 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14249 new_specs = NULL_TREE;
14250 if (specs && TREE_PURPOSE (specs))
14251 {
14252 /* A noexcept-specifier. */
14253 tree expr = TREE_PURPOSE (specs);
14254 if (TREE_CODE (expr) == INTEGER_CST)
14255 new_specs = expr;
14256 else if (defer_ok)
14257 {
14258 /* Defer instantiation of noexcept-specifiers to avoid
14259 excessive instantiations (c++/49107). */
14260 new_specs = make_node (DEFERRED_NOEXCEPT);
14261 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14262 {
14263 /* We already partially instantiated this member template,
14264 so combine the new args with the old. */
14265 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14266 = DEFERRED_NOEXCEPT_PATTERN (expr);
14267 DEFERRED_NOEXCEPT_ARGS (new_specs)
14268 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14269 }
14270 else
14271 {
14272 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14273 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14274 }
14275 }
14276 else
14277 {
14278 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14279 {
14280 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14281 args);
14282 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14283 }
14284 new_specs = tsubst_copy_and_build
14285 (expr, args, complain, in_decl, /*function_p=*/false,
14286 /*integral_constant_expression_p=*/true);
14287 }
14288 new_specs = build_noexcept_spec (new_specs, complain);
14289 }
14290 else if (specs)
14291 {
14292 if (! TREE_VALUE (specs))
14293 new_specs = specs;
14294 else
14295 while (specs)
14296 {
14297 tree spec;
14298 int i, len = 1;
14299 tree expanded_specs = NULL_TREE;
14300
14301 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14302 {
14303 /* Expand the pack expansion type. */
14304 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14305 args, complain,
14306 in_decl);
14307
14308 if (expanded_specs == error_mark_node)
14309 return error_mark_node;
14310 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14311 len = TREE_VEC_LENGTH (expanded_specs);
14312 else
14313 {
14314 /* We're substituting into a member template, so
14315 we got a TYPE_PACK_EXPANSION back. Add that
14316 expansion and move on. */
14317 gcc_assert (TREE_CODE (expanded_specs)
14318 == TYPE_PACK_EXPANSION);
14319 new_specs = add_exception_specifier (new_specs,
14320 expanded_specs,
14321 complain);
14322 specs = TREE_CHAIN (specs);
14323 continue;
14324 }
14325 }
14326
14327 for (i = 0; i < len; ++i)
14328 {
14329 if (expanded_specs)
14330 spec = TREE_VEC_ELT (expanded_specs, i);
14331 else
14332 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14333 if (spec == error_mark_node)
14334 return spec;
14335 new_specs = add_exception_specifier (new_specs, spec,
14336 complain);
14337 }
14338
14339 specs = TREE_CHAIN (specs);
14340 }
14341 }
14342 return new_specs;
14343 }
14344
14345 /* Take the tree structure T and replace template parameters used
14346 therein with the argument vector ARGS. IN_DECL is an associated
14347 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14348 Issue error and warning messages under control of COMPLAIN. Note
14349 that we must be relatively non-tolerant of extensions here, in
14350 order to preserve conformance; if we allow substitutions that
14351 should not be allowed, we may allow argument deductions that should
14352 not succeed, and therefore report ambiguous overload situations
14353 where there are none. In theory, we could allow the substitution,
14354 but indicate that it should have failed, and allow our caller to
14355 make sure that the right thing happens, but we don't try to do this
14356 yet.
14357
14358 This function is used for dealing with types, decls and the like;
14359 for expressions, use tsubst_expr or tsubst_copy. */
14360
14361 tree
14362 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14363 {
14364 enum tree_code code;
14365 tree type, r = NULL_TREE;
14366
14367 if (t == NULL_TREE || t == error_mark_node
14368 || t == integer_type_node
14369 || t == void_type_node
14370 || t == char_type_node
14371 || t == unknown_type_node
14372 || TREE_CODE (t) == NAMESPACE_DECL
14373 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14374 return t;
14375
14376 if (DECL_P (t))
14377 return tsubst_decl (t, args, complain);
14378
14379 if (args == NULL_TREE)
14380 return t;
14381
14382 code = TREE_CODE (t);
14383
14384 if (code == IDENTIFIER_NODE)
14385 type = IDENTIFIER_TYPE_VALUE (t);
14386 else
14387 type = TREE_TYPE (t);
14388
14389 gcc_assert (type != unknown_type_node);
14390
14391 /* Reuse typedefs. We need to do this to handle dependent attributes,
14392 such as attribute aligned. */
14393 if (TYPE_P (t)
14394 && typedef_variant_p (t))
14395 {
14396 tree decl = TYPE_NAME (t);
14397
14398 if (alias_template_specialization_p (t))
14399 {
14400 /* DECL represents an alias template and we want to
14401 instantiate it. */
14402 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14403 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14404 r = instantiate_alias_template (tmpl, gen_args, complain);
14405 }
14406 else if (DECL_CLASS_SCOPE_P (decl)
14407 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14408 && uses_template_parms (DECL_CONTEXT (decl)))
14409 {
14410 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14411 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14412 r = retrieve_specialization (tmpl, gen_args, 0);
14413 }
14414 else if (DECL_FUNCTION_SCOPE_P (decl)
14415 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14416 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14417 r = retrieve_local_specialization (decl);
14418 else
14419 /* The typedef is from a non-template context. */
14420 return t;
14421
14422 if (r)
14423 {
14424 r = TREE_TYPE (r);
14425 r = cp_build_qualified_type_real
14426 (r, cp_type_quals (t) | cp_type_quals (r),
14427 complain | tf_ignore_bad_quals);
14428 return r;
14429 }
14430 else
14431 {
14432 /* We don't have an instantiation yet, so drop the typedef. */
14433 int quals = cp_type_quals (t);
14434 t = DECL_ORIGINAL_TYPE (decl);
14435 t = cp_build_qualified_type_real (t, quals,
14436 complain | tf_ignore_bad_quals);
14437 }
14438 }
14439
14440 bool fndecl_type = (complain & tf_fndecl_type);
14441 complain &= ~tf_fndecl_type;
14442
14443 if (type
14444 && code != TYPENAME_TYPE
14445 && code != TEMPLATE_TYPE_PARM
14446 && code != TEMPLATE_PARM_INDEX
14447 && code != IDENTIFIER_NODE
14448 && code != FUNCTION_TYPE
14449 && code != METHOD_TYPE)
14450 type = tsubst (type, args, complain, in_decl);
14451 if (type == error_mark_node)
14452 return error_mark_node;
14453
14454 switch (code)
14455 {
14456 case RECORD_TYPE:
14457 case UNION_TYPE:
14458 case ENUMERAL_TYPE:
14459 return tsubst_aggr_type (t, args, complain, in_decl,
14460 /*entering_scope=*/0);
14461
14462 case ERROR_MARK:
14463 case IDENTIFIER_NODE:
14464 case VOID_TYPE:
14465 case REAL_TYPE:
14466 case COMPLEX_TYPE:
14467 case VECTOR_TYPE:
14468 case BOOLEAN_TYPE:
14469 case NULLPTR_TYPE:
14470 case LANG_TYPE:
14471 return t;
14472
14473 case INTEGER_TYPE:
14474 if (t == integer_type_node)
14475 return t;
14476
14477 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14478 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14479 return t;
14480
14481 {
14482 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14483
14484 max = tsubst_expr (omax, args, complain, in_decl,
14485 /*integral_constant_expression_p=*/false);
14486
14487 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14488 needed. */
14489 if (TREE_CODE (max) == NOP_EXPR
14490 && TREE_SIDE_EFFECTS (omax)
14491 && !TREE_TYPE (max))
14492 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14493
14494 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14495 with TREE_SIDE_EFFECTS that indicates this is not an integral
14496 constant expression. */
14497 if (processing_template_decl
14498 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14499 {
14500 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14501 TREE_SIDE_EFFECTS (max) = 1;
14502 }
14503
14504 return compute_array_index_type (NULL_TREE, max, complain);
14505 }
14506
14507 case TEMPLATE_TYPE_PARM:
14508 case TEMPLATE_TEMPLATE_PARM:
14509 case BOUND_TEMPLATE_TEMPLATE_PARM:
14510 case TEMPLATE_PARM_INDEX:
14511 {
14512 int idx;
14513 int level;
14514 int levels;
14515 tree arg = NULL_TREE;
14516
14517 /* Early in template argument deduction substitution, we don't
14518 want to reduce the level of 'auto', or it will be confused
14519 with a normal template parm in subsequent deduction. */
14520 if (is_auto (t) && (complain & tf_partial))
14521 return t;
14522
14523 r = NULL_TREE;
14524
14525 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14526 template_parm_level_and_index (t, &level, &idx);
14527
14528 levels = TMPL_ARGS_DEPTH (args);
14529 if (level <= levels
14530 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14531 {
14532 arg = TMPL_ARG (args, level, idx);
14533
14534 /* See through ARGUMENT_PACK_SELECT arguments. */
14535 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14536 arg = argument_pack_select_arg (arg);
14537 }
14538
14539 if (arg == error_mark_node)
14540 return error_mark_node;
14541 else if (arg != NULL_TREE)
14542 {
14543 if (ARGUMENT_PACK_P (arg))
14544 /* If ARG is an argument pack, we don't actually want to
14545 perform a substitution here, because substitutions
14546 for argument packs are only done
14547 element-by-element. We can get to this point when
14548 substituting the type of a non-type template
14549 parameter pack, when that type actually contains
14550 template parameter packs from an outer template, e.g.,
14551
14552 template<typename... Types> struct A {
14553 template<Types... Values> struct B { };
14554 }; */
14555 return t;
14556
14557 if (code == TEMPLATE_TYPE_PARM)
14558 {
14559 int quals;
14560 gcc_assert (TYPE_P (arg));
14561
14562 quals = cp_type_quals (arg) | cp_type_quals (t);
14563
14564 return cp_build_qualified_type_real
14565 (arg, quals, complain | tf_ignore_bad_quals);
14566 }
14567 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14568 {
14569 /* We are processing a type constructed from a
14570 template template parameter. */
14571 tree argvec = tsubst (TYPE_TI_ARGS (t),
14572 args, complain, in_decl);
14573 if (argvec == error_mark_node)
14574 return error_mark_node;
14575
14576 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14577 || TREE_CODE (arg) == TEMPLATE_DECL
14578 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14579
14580 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14581 /* Consider this code:
14582
14583 template <template <class> class Template>
14584 struct Internal {
14585 template <class Arg> using Bind = Template<Arg>;
14586 };
14587
14588 template <template <class> class Template, class Arg>
14589 using Instantiate = Template<Arg>; //#0
14590
14591 template <template <class> class Template,
14592 class Argument>
14593 using Bind =
14594 Instantiate<Internal<Template>::template Bind,
14595 Argument>; //#1
14596
14597 When #1 is parsed, the
14598 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14599 parameter `Template' in #0 matches the
14600 UNBOUND_CLASS_TEMPLATE representing the argument
14601 `Internal<Template>::template Bind'; We then want
14602 to assemble the type `Bind<Argument>' that can't
14603 be fully created right now, because
14604 `Internal<Template>' not being complete, the Bind
14605 template cannot be looked up in that context. So
14606 we need to "store" `Bind<Argument>' for later
14607 when the context of Bind becomes complete. Let's
14608 store that in a TYPENAME_TYPE. */
14609 return make_typename_type (TYPE_CONTEXT (arg),
14610 build_nt (TEMPLATE_ID_EXPR,
14611 TYPE_IDENTIFIER (arg),
14612 argvec),
14613 typename_type,
14614 complain);
14615
14616 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14617 are resolving nested-types in the signature of a
14618 member function templates. Otherwise ARG is a
14619 TEMPLATE_DECL and is the real template to be
14620 instantiated. */
14621 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14622 arg = TYPE_NAME (arg);
14623
14624 r = lookup_template_class (arg,
14625 argvec, in_decl,
14626 DECL_CONTEXT (arg),
14627 /*entering_scope=*/0,
14628 complain);
14629 return cp_build_qualified_type_real
14630 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14631 }
14632 else if (code == TEMPLATE_TEMPLATE_PARM)
14633 return arg;
14634 else
14635 /* TEMPLATE_PARM_INDEX. */
14636 return convert_from_reference (unshare_expr (arg));
14637 }
14638
14639 if (level == 1)
14640 /* This can happen during the attempted tsubst'ing in
14641 unify. This means that we don't yet have any information
14642 about the template parameter in question. */
14643 return t;
14644
14645 /* If we get here, we must have been looking at a parm for a
14646 more deeply nested template. Make a new version of this
14647 template parameter, but with a lower level. */
14648 switch (code)
14649 {
14650 case TEMPLATE_TYPE_PARM:
14651 case TEMPLATE_TEMPLATE_PARM:
14652 case BOUND_TEMPLATE_TEMPLATE_PARM:
14653 if (cp_type_quals (t))
14654 {
14655 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14656 r = cp_build_qualified_type_real
14657 (r, cp_type_quals (t),
14658 complain | (code == TEMPLATE_TYPE_PARM
14659 ? tf_ignore_bad_quals : 0));
14660 }
14661 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14662 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14663 && (r = (TEMPLATE_PARM_DESCENDANTS
14664 (TEMPLATE_TYPE_PARM_INDEX (t))))
14665 && (r = TREE_TYPE (r))
14666 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14667 /* Break infinite recursion when substituting the constraints
14668 of a constrained placeholder. */;
14669 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14670 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14671 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14672 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14673 r = TEMPLATE_PARM_DESCENDANTS (arg))
14674 && (TEMPLATE_PARM_LEVEL (r)
14675 == TEMPLATE_PARM_LEVEL (arg) - levels))
14676 /* Cache the simple case of lowering a type parameter. */
14677 r = TREE_TYPE (r);
14678 else
14679 {
14680 r = copy_type (t);
14681 TEMPLATE_TYPE_PARM_INDEX (r)
14682 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14683 r, levels, args, complain);
14684 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14685 TYPE_MAIN_VARIANT (r) = r;
14686 TYPE_POINTER_TO (r) = NULL_TREE;
14687 TYPE_REFERENCE_TO (r) = NULL_TREE;
14688
14689 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14690 {
14691 /* Propagate constraints on placeholders. */
14692 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14693 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14694 = tsubst_constraint (constr, args, complain, in_decl);
14695 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14696 {
14697 pl = tsubst_copy (pl, args, complain, in_decl);
14698 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14699 }
14700 }
14701
14702 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14703 /* We have reduced the level of the template
14704 template parameter, but not the levels of its
14705 template parameters, so canonical_type_parameter
14706 will not be able to find the canonical template
14707 template parameter for this level. Thus, we
14708 require structural equality checking to compare
14709 TEMPLATE_TEMPLATE_PARMs. */
14710 SET_TYPE_STRUCTURAL_EQUALITY (r);
14711 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14712 SET_TYPE_STRUCTURAL_EQUALITY (r);
14713 else
14714 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14715
14716 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14717 {
14718 tree tinfo = TYPE_TEMPLATE_INFO (t);
14719 /* We might need to substitute into the types of non-type
14720 template parameters. */
14721 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14722 complain, in_decl);
14723 if (tmpl == error_mark_node)
14724 return error_mark_node;
14725 tree argvec = tsubst (TI_ARGS (tinfo), args,
14726 complain, in_decl);
14727 if (argvec == error_mark_node)
14728 return error_mark_node;
14729
14730 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14731 = build_template_info (tmpl, argvec);
14732 }
14733 }
14734 break;
14735
14736 case TEMPLATE_PARM_INDEX:
14737 /* OK, now substitute the type of the non-type parameter. We
14738 couldn't do it earlier because it might be an auto parameter,
14739 and we wouldn't need to if we had an argument. */
14740 type = tsubst (type, args, complain, in_decl);
14741 if (type == error_mark_node)
14742 return error_mark_node;
14743 r = reduce_template_parm_level (t, type, levels, args, complain);
14744 break;
14745
14746 default:
14747 gcc_unreachable ();
14748 }
14749
14750 return r;
14751 }
14752
14753 case TREE_LIST:
14754 {
14755 tree purpose, value, chain;
14756
14757 if (t == void_list_node)
14758 return t;
14759
14760 purpose = TREE_PURPOSE (t);
14761 if (purpose)
14762 {
14763 purpose = tsubst (purpose, args, complain, in_decl);
14764 if (purpose == error_mark_node)
14765 return error_mark_node;
14766 }
14767 value = TREE_VALUE (t);
14768 if (value)
14769 {
14770 value = tsubst (value, args, complain, in_decl);
14771 if (value == error_mark_node)
14772 return error_mark_node;
14773 }
14774 chain = TREE_CHAIN (t);
14775 if (chain && chain != void_type_node)
14776 {
14777 chain = tsubst (chain, args, complain, in_decl);
14778 if (chain == error_mark_node)
14779 return error_mark_node;
14780 }
14781 if (purpose == TREE_PURPOSE (t)
14782 && value == TREE_VALUE (t)
14783 && chain == TREE_CHAIN (t))
14784 return t;
14785 return hash_tree_cons (purpose, value, chain);
14786 }
14787
14788 case TREE_BINFO:
14789 /* We should never be tsubsting a binfo. */
14790 gcc_unreachable ();
14791
14792 case TREE_VEC:
14793 /* A vector of template arguments. */
14794 gcc_assert (!type);
14795 return tsubst_template_args (t, args, complain, in_decl);
14796
14797 case POINTER_TYPE:
14798 case REFERENCE_TYPE:
14799 {
14800 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14801 return t;
14802
14803 /* [temp.deduct]
14804
14805 Type deduction may fail for any of the following
14806 reasons:
14807
14808 -- Attempting to create a pointer to reference type.
14809 -- Attempting to create a reference to a reference type or
14810 a reference to void.
14811
14812 Core issue 106 says that creating a reference to a reference
14813 during instantiation is no longer a cause for failure. We
14814 only enforce this check in strict C++98 mode. */
14815 if ((TYPE_REF_P (type)
14816 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14817 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14818 {
14819 static location_t last_loc;
14820
14821 /* We keep track of the last time we issued this error
14822 message to avoid spewing a ton of messages during a
14823 single bad template instantiation. */
14824 if (complain & tf_error
14825 && last_loc != input_location)
14826 {
14827 if (VOID_TYPE_P (type))
14828 error ("forming reference to void");
14829 else if (code == POINTER_TYPE)
14830 error ("forming pointer to reference type %qT", type);
14831 else
14832 error ("forming reference to reference type %qT", type);
14833 last_loc = input_location;
14834 }
14835
14836 return error_mark_node;
14837 }
14838 else if (TREE_CODE (type) == FUNCTION_TYPE
14839 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14840 || type_memfn_rqual (type) != REF_QUAL_NONE))
14841 {
14842 if (complain & tf_error)
14843 {
14844 if (code == POINTER_TYPE)
14845 error ("forming pointer to qualified function type %qT",
14846 type);
14847 else
14848 error ("forming reference to qualified function type %qT",
14849 type);
14850 }
14851 return error_mark_node;
14852 }
14853 else if (code == POINTER_TYPE)
14854 {
14855 r = build_pointer_type (type);
14856 if (TREE_CODE (type) == METHOD_TYPE)
14857 r = build_ptrmemfunc_type (r);
14858 }
14859 else if (TYPE_REF_P (type))
14860 /* In C++0x, during template argument substitution, when there is an
14861 attempt to create a reference to a reference type, reference
14862 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14863
14864 "If a template-argument for a template-parameter T names a type
14865 that is a reference to a type A, an attempt to create the type
14866 'lvalue reference to cv T' creates the type 'lvalue reference to
14867 A,' while an attempt to create the type type rvalue reference to
14868 cv T' creates the type T"
14869 */
14870 r = cp_build_reference_type
14871 (TREE_TYPE (type),
14872 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14873 else
14874 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14875 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14876
14877 if (r != error_mark_node)
14878 /* Will this ever be needed for TYPE_..._TO values? */
14879 layout_type (r);
14880
14881 return r;
14882 }
14883 case OFFSET_TYPE:
14884 {
14885 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14886 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14887 {
14888 /* [temp.deduct]
14889
14890 Type deduction may fail for any of the following
14891 reasons:
14892
14893 -- Attempting to create "pointer to member of T" when T
14894 is not a class type. */
14895 if (complain & tf_error)
14896 error ("creating pointer to member of non-class type %qT", r);
14897 return error_mark_node;
14898 }
14899 if (TYPE_REF_P (type))
14900 {
14901 if (complain & tf_error)
14902 error ("creating pointer to member reference type %qT", type);
14903 return error_mark_node;
14904 }
14905 if (VOID_TYPE_P (type))
14906 {
14907 if (complain & tf_error)
14908 error ("creating pointer to member of type void");
14909 return error_mark_node;
14910 }
14911 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14912 if (TREE_CODE (type) == FUNCTION_TYPE)
14913 {
14914 /* The type of the implicit object parameter gets its
14915 cv-qualifiers from the FUNCTION_TYPE. */
14916 tree memptr;
14917 tree method_type
14918 = build_memfn_type (type, r, type_memfn_quals (type),
14919 type_memfn_rqual (type));
14920 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14921 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14922 complain);
14923 }
14924 else
14925 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14926 cp_type_quals (t),
14927 complain);
14928 }
14929 case FUNCTION_TYPE:
14930 case METHOD_TYPE:
14931 {
14932 tree fntype;
14933 tree specs;
14934 fntype = tsubst_function_type (t, args, complain, in_decl);
14935 if (fntype == error_mark_node)
14936 return error_mark_node;
14937
14938 /* Substitute the exception specification. */
14939 specs = tsubst_exception_specification (t, args, complain, in_decl,
14940 /*defer_ok*/fndecl_type);
14941 if (specs == error_mark_node)
14942 return error_mark_node;
14943 if (specs)
14944 fntype = build_exception_variant (fntype, specs);
14945 return fntype;
14946 }
14947 case ARRAY_TYPE:
14948 {
14949 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14950 if (domain == error_mark_node)
14951 return error_mark_node;
14952
14953 /* As an optimization, we avoid regenerating the array type if
14954 it will obviously be the same as T. */
14955 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14956 return t;
14957
14958 /* These checks should match the ones in create_array_type_for_decl.
14959
14960 [temp.deduct]
14961
14962 The deduction may fail for any of the following reasons:
14963
14964 -- Attempting to create an array with an element type that
14965 is void, a function type, or a reference type, or [DR337]
14966 an abstract class type. */
14967 if (VOID_TYPE_P (type)
14968 || TREE_CODE (type) == FUNCTION_TYPE
14969 || (TREE_CODE (type) == ARRAY_TYPE
14970 && TYPE_DOMAIN (type) == NULL_TREE)
14971 || TYPE_REF_P (type))
14972 {
14973 if (complain & tf_error)
14974 error ("creating array of %qT", type);
14975 return error_mark_node;
14976 }
14977
14978 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14979 return error_mark_node;
14980
14981 r = build_cplus_array_type (type, domain);
14982
14983 if (!valid_array_size_p (input_location, r, in_decl,
14984 (complain & tf_error)))
14985 return error_mark_node;
14986
14987 if (TYPE_USER_ALIGN (t))
14988 {
14989 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14990 TYPE_USER_ALIGN (r) = 1;
14991 }
14992
14993 return r;
14994 }
14995
14996 case TYPENAME_TYPE:
14997 {
14998 tree ctx = TYPE_CONTEXT (t);
14999 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15000 {
15001 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15002 if (ctx == error_mark_node
15003 || TREE_VEC_LENGTH (ctx) > 1)
15004 return error_mark_node;
15005 if (TREE_VEC_LENGTH (ctx) == 0)
15006 {
15007 if (complain & tf_error)
15008 error ("%qD is instantiated for an empty pack",
15009 TYPENAME_TYPE_FULLNAME (t));
15010 return error_mark_node;
15011 }
15012 ctx = TREE_VEC_ELT (ctx, 0);
15013 }
15014 else
15015 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15016 /*entering_scope=*/1);
15017 if (ctx == error_mark_node)
15018 return error_mark_node;
15019
15020 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15021 complain, in_decl);
15022 if (f == error_mark_node)
15023 return error_mark_node;
15024
15025 if (!MAYBE_CLASS_TYPE_P (ctx))
15026 {
15027 if (complain & tf_error)
15028 error ("%qT is not a class, struct, or union type", ctx);
15029 return error_mark_node;
15030 }
15031 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15032 {
15033 /* Normally, make_typename_type does not require that the CTX
15034 have complete type in order to allow things like:
15035
15036 template <class T> struct S { typename S<T>::X Y; };
15037
15038 But, such constructs have already been resolved by this
15039 point, so here CTX really should have complete type, unless
15040 it's a partial instantiation. */
15041 ctx = complete_type (ctx);
15042 if (!COMPLETE_TYPE_P (ctx))
15043 {
15044 if (complain & tf_error)
15045 cxx_incomplete_type_error (NULL_TREE, ctx);
15046 return error_mark_node;
15047 }
15048 }
15049
15050 f = make_typename_type (ctx, f, typename_type,
15051 complain | tf_keep_type_decl);
15052 if (f == error_mark_node)
15053 return f;
15054 if (TREE_CODE (f) == TYPE_DECL)
15055 {
15056 complain |= tf_ignore_bad_quals;
15057 f = TREE_TYPE (f);
15058 }
15059
15060 if (TREE_CODE (f) != TYPENAME_TYPE)
15061 {
15062 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15063 {
15064 if (complain & tf_error)
15065 error ("%qT resolves to %qT, which is not an enumeration type",
15066 t, f);
15067 else
15068 return error_mark_node;
15069 }
15070 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15071 {
15072 if (complain & tf_error)
15073 error ("%qT resolves to %qT, which is is not a class type",
15074 t, f);
15075 else
15076 return error_mark_node;
15077 }
15078 }
15079
15080 return cp_build_qualified_type_real
15081 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15082 }
15083
15084 case UNBOUND_CLASS_TEMPLATE:
15085 {
15086 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15087 in_decl, /*entering_scope=*/1);
15088 tree name = TYPE_IDENTIFIER (t);
15089 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15090
15091 if (ctx == error_mark_node || name == error_mark_node)
15092 return error_mark_node;
15093
15094 if (parm_list)
15095 parm_list = tsubst_template_parms (parm_list, args, complain);
15096 return make_unbound_class_template (ctx, name, parm_list, complain);
15097 }
15098
15099 case TYPEOF_TYPE:
15100 {
15101 tree type;
15102
15103 ++cp_unevaluated_operand;
15104 ++c_inhibit_evaluation_warnings;
15105
15106 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15107 complain, in_decl,
15108 /*integral_constant_expression_p=*/false);
15109
15110 --cp_unevaluated_operand;
15111 --c_inhibit_evaluation_warnings;
15112
15113 type = finish_typeof (type);
15114 return cp_build_qualified_type_real (type,
15115 cp_type_quals (t)
15116 | cp_type_quals (type),
15117 complain);
15118 }
15119
15120 case DECLTYPE_TYPE:
15121 {
15122 tree type;
15123
15124 ++cp_unevaluated_operand;
15125 ++c_inhibit_evaluation_warnings;
15126
15127 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15128 complain|tf_decltype, in_decl,
15129 /*function_p*/false,
15130 /*integral_constant_expression*/false);
15131
15132 if (DECLTYPE_FOR_INIT_CAPTURE (t))
15133 {
15134 if (type == NULL_TREE)
15135 {
15136 if (complain & tf_error)
15137 error ("empty initializer in lambda init-capture");
15138 type = error_mark_node;
15139 }
15140 else if (TREE_CODE (type) == TREE_LIST)
15141 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
15142 }
15143
15144 --cp_unevaluated_operand;
15145 --c_inhibit_evaluation_warnings;
15146
15147 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15148 type = lambda_capture_field_type (type,
15149 DECLTYPE_FOR_INIT_CAPTURE (t),
15150 DECLTYPE_FOR_REF_CAPTURE (t));
15151 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15152 type = lambda_proxy_type (type);
15153 else
15154 {
15155 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15156 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15157 && EXPR_P (type))
15158 /* In a template ~id could be either a complement expression
15159 or an unqualified-id naming a destructor; if instantiating
15160 it produces an expression, it's not an id-expression or
15161 member access. */
15162 id = false;
15163 type = finish_decltype_type (type, id, complain);
15164 }
15165 return cp_build_qualified_type_real (type,
15166 cp_type_quals (t)
15167 | cp_type_quals (type),
15168 complain | tf_ignore_bad_quals);
15169 }
15170
15171 case UNDERLYING_TYPE:
15172 {
15173 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15174 complain, in_decl);
15175 return finish_underlying_type (type);
15176 }
15177
15178 case TYPE_ARGUMENT_PACK:
15179 case NONTYPE_ARGUMENT_PACK:
15180 {
15181 tree r;
15182
15183 if (code == NONTYPE_ARGUMENT_PACK)
15184 r = make_node (code);
15185 else
15186 r = cxx_make_type (code);
15187
15188 tree pack_args = ARGUMENT_PACK_ARGS (t);
15189 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15190 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15191
15192 return r;
15193 }
15194
15195 case VOID_CST:
15196 case INTEGER_CST:
15197 case REAL_CST:
15198 case STRING_CST:
15199 case PLUS_EXPR:
15200 case MINUS_EXPR:
15201 case NEGATE_EXPR:
15202 case NOP_EXPR:
15203 case INDIRECT_REF:
15204 case ADDR_EXPR:
15205 case CALL_EXPR:
15206 case ARRAY_REF:
15207 case SCOPE_REF:
15208 /* We should use one of the expression tsubsts for these codes. */
15209 gcc_unreachable ();
15210
15211 default:
15212 sorry ("use of %qs in template", get_tree_code_name (code));
15213 return error_mark_node;
15214 }
15215 }
15216
15217 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15218 expression on the left-hand side of the "." or "->" operator. We
15219 only do the lookup if we had a dependent BASELINK. Otherwise we
15220 adjust it onto the instantiated heirarchy. */
15221
15222 static tree
15223 tsubst_baselink (tree baselink, tree object_type,
15224 tree args, tsubst_flags_t complain, tree in_decl)
15225 {
15226 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15227 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15228 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15229
15230 tree optype = BASELINK_OPTYPE (baselink);
15231 optype = tsubst (optype, args, complain, in_decl);
15232
15233 tree template_args = NULL_TREE;
15234 bool template_id_p = false;
15235 tree fns = BASELINK_FUNCTIONS (baselink);
15236 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15237 {
15238 template_id_p = true;
15239 template_args = TREE_OPERAND (fns, 1);
15240 fns = TREE_OPERAND (fns, 0);
15241 if (template_args)
15242 template_args = tsubst_template_args (template_args, args,
15243 complain, in_decl);
15244 }
15245
15246 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15247 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15248 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15249
15250 if (dependent_p)
15251 {
15252 tree name = OVL_NAME (fns);
15253 if (IDENTIFIER_CONV_OP_P (name))
15254 name = make_conv_op_name (optype);
15255
15256 if (name == complete_dtor_identifier)
15257 /* Treat as-if non-dependent below. */
15258 dependent_p = false;
15259
15260 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15261 if (!baselink)
15262 {
15263 if ((complain & tf_error)
15264 && constructor_name_p (name, qualifying_scope))
15265 error ("cannot call constructor %<%T::%D%> directly",
15266 qualifying_scope, name);
15267 return error_mark_node;
15268 }
15269
15270 if (BASELINK_P (baselink))
15271 fns = BASELINK_FUNCTIONS (baselink);
15272 }
15273 else
15274 /* We're going to overwrite pieces below, make a duplicate. */
15275 baselink = copy_node (baselink);
15276
15277 /* If lookup found a single function, mark it as used at this point.
15278 (If lookup found multiple functions the one selected later by
15279 overload resolution will be marked as used at that point.) */
15280 if (!template_id_p && !really_overloaded_fn (fns))
15281 {
15282 tree fn = OVL_FIRST (fns);
15283 bool ok = mark_used (fn, complain);
15284 if (!ok && !(complain & tf_error))
15285 return error_mark_node;
15286 if (ok && BASELINK_P (baselink))
15287 /* We might have instantiated an auto function. */
15288 TREE_TYPE (baselink) = TREE_TYPE (fn);
15289 }
15290
15291 if (BASELINK_P (baselink))
15292 {
15293 /* Add back the template arguments, if present. */
15294 if (template_id_p)
15295 BASELINK_FUNCTIONS (baselink)
15296 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15297
15298 /* Update the conversion operator type. */
15299 BASELINK_OPTYPE (baselink) = optype;
15300 }
15301
15302 if (!object_type)
15303 object_type = current_class_type;
15304
15305 if (qualified_p || !dependent_p)
15306 {
15307 baselink = adjust_result_of_qualified_name_lookup (baselink,
15308 qualifying_scope,
15309 object_type);
15310 if (!qualified_p)
15311 /* We need to call adjust_result_of_qualified_name_lookup in case the
15312 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15313 so that we still get virtual function binding. */
15314 BASELINK_QUALIFIED_P (baselink) = false;
15315 }
15316
15317 return baselink;
15318 }
15319
15320 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15321 true if the qualified-id will be a postfix-expression in-and-of
15322 itself; false if more of the postfix-expression follows the
15323 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15324 of "&". */
15325
15326 static tree
15327 tsubst_qualified_id (tree qualified_id, tree args,
15328 tsubst_flags_t complain, tree in_decl,
15329 bool done, bool address_p)
15330 {
15331 tree expr;
15332 tree scope;
15333 tree name;
15334 bool is_template;
15335 tree template_args;
15336 location_t loc = UNKNOWN_LOCATION;
15337
15338 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15339
15340 /* Figure out what name to look up. */
15341 name = TREE_OPERAND (qualified_id, 1);
15342 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15343 {
15344 is_template = true;
15345 loc = EXPR_LOCATION (name);
15346 template_args = TREE_OPERAND (name, 1);
15347 if (template_args)
15348 template_args = tsubst_template_args (template_args, args,
15349 complain, in_decl);
15350 if (template_args == error_mark_node)
15351 return error_mark_node;
15352 name = TREE_OPERAND (name, 0);
15353 }
15354 else
15355 {
15356 is_template = false;
15357 template_args = NULL_TREE;
15358 }
15359
15360 /* Substitute into the qualifying scope. When there are no ARGS, we
15361 are just trying to simplify a non-dependent expression. In that
15362 case the qualifying scope may be dependent, and, in any case,
15363 substituting will not help. */
15364 scope = TREE_OPERAND (qualified_id, 0);
15365 if (args)
15366 {
15367 scope = tsubst (scope, args, complain, in_decl);
15368 expr = tsubst_copy (name, args, complain, in_decl);
15369 }
15370 else
15371 expr = name;
15372
15373 if (dependent_scope_p (scope))
15374 {
15375 if (is_template)
15376 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15377 tree r = build_qualified_name (NULL_TREE, scope, expr,
15378 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15379 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15380 return r;
15381 }
15382
15383 if (!BASELINK_P (name) && !DECL_P (expr))
15384 {
15385 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15386 {
15387 /* A BIT_NOT_EXPR is used to represent a destructor. */
15388 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15389 {
15390 error ("qualifying type %qT does not match destructor name ~%qT",
15391 scope, TREE_OPERAND (expr, 0));
15392 expr = error_mark_node;
15393 }
15394 else
15395 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15396 /*is_type_p=*/0, false);
15397 }
15398 else
15399 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15400 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15401 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15402 {
15403 if (complain & tf_error)
15404 {
15405 error ("dependent-name %qE is parsed as a non-type, but "
15406 "instantiation yields a type", qualified_id);
15407 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15408 }
15409 return error_mark_node;
15410 }
15411 }
15412
15413 if (DECL_P (expr))
15414 {
15415 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15416 scope);
15417 /* Remember that there was a reference to this entity. */
15418 if (!mark_used (expr, complain) && !(complain & tf_error))
15419 return error_mark_node;
15420 }
15421
15422 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15423 {
15424 if (complain & tf_error)
15425 qualified_name_lookup_error (scope,
15426 TREE_OPERAND (qualified_id, 1),
15427 expr, input_location);
15428 return error_mark_node;
15429 }
15430
15431 if (is_template)
15432 {
15433 /* We may be repeating a check already done during parsing, but
15434 if it was well-formed and passed then, it will pass again
15435 now, and if it didn't, we wouldn't have got here. The case
15436 we want to catch is when we couldn't tell then, and can now,
15437 namely when templ prior to substitution was an
15438 identifier. */
15439 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15440 return error_mark_node;
15441
15442 if (variable_template_p (expr))
15443 expr = lookup_and_finish_template_variable (expr, template_args,
15444 complain);
15445 else
15446 expr = lookup_template_function (expr, template_args);
15447 }
15448
15449 if (expr == error_mark_node && complain & tf_error)
15450 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15451 expr, input_location);
15452 else if (TYPE_P (scope))
15453 {
15454 expr = (adjust_result_of_qualified_name_lookup
15455 (expr, scope, current_nonlambda_class_type ()));
15456 expr = (finish_qualified_id_expr
15457 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15458 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15459 /*template_arg_p=*/false, complain));
15460 }
15461
15462 /* Expressions do not generally have reference type. */
15463 if (TREE_CODE (expr) != SCOPE_REF
15464 /* However, if we're about to form a pointer-to-member, we just
15465 want the referenced member referenced. */
15466 && TREE_CODE (expr) != OFFSET_REF)
15467 expr = convert_from_reference (expr);
15468
15469 if (REF_PARENTHESIZED_P (qualified_id))
15470 expr = force_paren_expr (expr);
15471
15472 return expr;
15473 }
15474
15475 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15476 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15477 for tsubst. */
15478
15479 static tree
15480 tsubst_init (tree init, tree decl, tree args,
15481 tsubst_flags_t complain, tree in_decl)
15482 {
15483 if (!init)
15484 return NULL_TREE;
15485
15486 init = tsubst_expr (init, args, complain, in_decl, false);
15487
15488 tree type = TREE_TYPE (decl);
15489
15490 if (!init && type != error_mark_node)
15491 {
15492 if (tree auto_node = type_uses_auto (type))
15493 {
15494 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15495 {
15496 if (complain & tf_error)
15497 error ("initializer for %q#D expands to an empty list "
15498 "of expressions", decl);
15499 return error_mark_node;
15500 }
15501 }
15502 else if (!dependent_type_p (type))
15503 {
15504 /* If we had an initializer but it
15505 instantiated to nothing,
15506 value-initialize the object. This will
15507 only occur when the initializer was a
15508 pack expansion where the parameter packs
15509 used in that expansion were of length
15510 zero. */
15511 init = build_value_init (type, complain);
15512 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15513 init = get_target_expr_sfinae (init, complain);
15514 if (TREE_CODE (init) == TARGET_EXPR)
15515 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15516 }
15517 }
15518
15519 return init;
15520 }
15521
15522 /* Like tsubst, but deals with expressions. This function just replaces
15523 template parms; to finish processing the resultant expression, use
15524 tsubst_copy_and_build or tsubst_expr. */
15525
15526 static tree
15527 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15528 {
15529 enum tree_code code;
15530 tree r;
15531
15532 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15533 return t;
15534
15535 code = TREE_CODE (t);
15536
15537 switch (code)
15538 {
15539 case PARM_DECL:
15540 r = retrieve_local_specialization (t);
15541
15542 if (r == NULL_TREE)
15543 {
15544 /* We get here for a use of 'this' in an NSDMI. */
15545 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15546 return current_class_ptr;
15547
15548 /* This can happen for a parameter name used later in a function
15549 declaration (such as in a late-specified return type). Just
15550 make a dummy decl, since it's only used for its type. */
15551 gcc_assert (cp_unevaluated_operand != 0);
15552 r = tsubst_decl (t, args, complain);
15553 /* Give it the template pattern as its context; its true context
15554 hasn't been instantiated yet and this is good enough for
15555 mangling. */
15556 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15557 }
15558
15559 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15560 r = argument_pack_select_arg (r);
15561 if (!mark_used (r, complain) && !(complain & tf_error))
15562 return error_mark_node;
15563 return r;
15564
15565 case CONST_DECL:
15566 {
15567 tree enum_type;
15568 tree v;
15569
15570 if (DECL_TEMPLATE_PARM_P (t))
15571 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15572 /* There is no need to substitute into namespace-scope
15573 enumerators. */
15574 if (DECL_NAMESPACE_SCOPE_P (t))
15575 return t;
15576 /* If ARGS is NULL, then T is known to be non-dependent. */
15577 if (args == NULL_TREE)
15578 return scalar_constant_value (t);
15579
15580 /* Unfortunately, we cannot just call lookup_name here.
15581 Consider:
15582
15583 template <int I> int f() {
15584 enum E { a = I };
15585 struct S { void g() { E e = a; } };
15586 };
15587
15588 When we instantiate f<7>::S::g(), say, lookup_name is not
15589 clever enough to find f<7>::a. */
15590 enum_type
15591 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15592 /*entering_scope=*/0);
15593
15594 for (v = TYPE_VALUES (enum_type);
15595 v != NULL_TREE;
15596 v = TREE_CHAIN (v))
15597 if (TREE_PURPOSE (v) == DECL_NAME (t))
15598 return TREE_VALUE (v);
15599
15600 /* We didn't find the name. That should never happen; if
15601 name-lookup found it during preliminary parsing, we
15602 should find it again here during instantiation. */
15603 gcc_unreachable ();
15604 }
15605 return t;
15606
15607 case FIELD_DECL:
15608 if (DECL_CONTEXT (t))
15609 {
15610 tree ctx;
15611
15612 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15613 /*entering_scope=*/1);
15614 if (ctx != DECL_CONTEXT (t))
15615 {
15616 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15617 if (!r)
15618 {
15619 if (complain & tf_error)
15620 error ("using invalid field %qD", t);
15621 return error_mark_node;
15622 }
15623 return r;
15624 }
15625 }
15626
15627 return t;
15628
15629 case VAR_DECL:
15630 case FUNCTION_DECL:
15631 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15632 r = tsubst (t, args, complain, in_decl);
15633 else if (local_variable_p (t)
15634 && uses_template_parms (DECL_CONTEXT (t)))
15635 {
15636 r = retrieve_local_specialization (t);
15637 if (r == NULL_TREE)
15638 {
15639 /* First try name lookup to find the instantiation. */
15640 r = lookup_name (DECL_NAME (t));
15641 if (r)
15642 {
15643 if (!VAR_P (r))
15644 {
15645 /* During error-recovery we may find a non-variable,
15646 even an OVERLOAD: just bail out and avoid ICEs and
15647 duplicate diagnostics (c++/62207). */
15648 gcc_assert (seen_error ());
15649 return error_mark_node;
15650 }
15651 if (!is_capture_proxy (r))
15652 {
15653 /* Make sure the one we found is the one we want. */
15654 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15655 if (ctx != DECL_CONTEXT (r))
15656 r = NULL_TREE;
15657 }
15658 }
15659
15660 if (r)
15661 /* OK */;
15662 else
15663 {
15664 /* This can happen for a variable used in a
15665 late-specified return type of a local lambda, or for a
15666 local static or constant. Building a new VAR_DECL
15667 should be OK in all those cases. */
15668 r = tsubst_decl (t, args, complain);
15669 if (local_specializations)
15670 /* Avoid infinite recursion (79640). */
15671 register_local_specialization (r, t);
15672 if (decl_maybe_constant_var_p (r))
15673 {
15674 /* We can't call cp_finish_decl, so handle the
15675 initializer by hand. */
15676 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15677 complain, in_decl);
15678 if (!processing_template_decl)
15679 init = maybe_constant_init (init);
15680 if (processing_template_decl
15681 ? potential_constant_expression (init)
15682 : reduced_constant_expression_p (init))
15683 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15684 = TREE_CONSTANT (r) = true;
15685 DECL_INITIAL (r) = init;
15686 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15687 TREE_TYPE (r)
15688 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15689 complain, adc_variable_type);
15690 }
15691 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15692 || decl_constant_var_p (r)
15693 || seen_error ());
15694 if (!processing_template_decl
15695 && !TREE_STATIC (r))
15696 r = process_outer_var_ref (r, complain);
15697 }
15698 /* Remember this for subsequent uses. */
15699 if (local_specializations)
15700 register_local_specialization (r, t);
15701 }
15702 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15703 r = argument_pack_select_arg (r);
15704 }
15705 else
15706 r = t;
15707 if (!mark_used (r, complain))
15708 return error_mark_node;
15709 return r;
15710
15711 case NAMESPACE_DECL:
15712 return t;
15713
15714 case OVERLOAD:
15715 return t;
15716
15717 case BASELINK:
15718 return tsubst_baselink (t, current_nonlambda_class_type (),
15719 args, complain, in_decl);
15720
15721 case TEMPLATE_DECL:
15722 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15723 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15724 args, complain, in_decl);
15725 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15726 return tsubst (t, args, complain, in_decl);
15727 else if (DECL_CLASS_SCOPE_P (t)
15728 && uses_template_parms (DECL_CONTEXT (t)))
15729 {
15730 /* Template template argument like the following example need
15731 special treatment:
15732
15733 template <template <class> class TT> struct C {};
15734 template <class T> struct D {
15735 template <class U> struct E {};
15736 C<E> c; // #1
15737 };
15738 D<int> d; // #2
15739
15740 We are processing the template argument `E' in #1 for
15741 the template instantiation #2. Originally, `E' is a
15742 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15743 have to substitute this with one having context `D<int>'. */
15744
15745 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15746 if (dependent_scope_p (context))
15747 {
15748 /* When rewriting a constructor into a deduction guide, a
15749 non-dependent name can become dependent, so memtmpl<args>
15750 becomes context::template memtmpl<args>. */
15751 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15752 return build_qualified_name (type, context, DECL_NAME (t),
15753 /*template*/true);
15754 }
15755 return lookup_field (context, DECL_NAME(t), 0, false);
15756 }
15757 else
15758 /* Ordinary template template argument. */
15759 return t;
15760
15761 case NON_LVALUE_EXPR:
15762 case VIEW_CONVERT_EXPR:
15763 {
15764 /* Handle location wrappers by substituting the wrapped node
15765 first, *then* reusing the resulting type. Doing the type
15766 first ensures that we handle template parameters and
15767 parameter pack expansions. */
15768 if (location_wrapper_p (t))
15769 {
15770 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15771 complain, in_decl);
15772 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15773 }
15774 tree op = TREE_OPERAND (t, 0);
15775 if (code == VIEW_CONVERT_EXPR
15776 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15777 {
15778 /* Wrapper to make a C++20 template parameter object const. */
15779 op = tsubst_copy (op, args, complain, in_decl);
15780 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15781 {
15782 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15783 return build1 (code, type, op);
15784 }
15785 else
15786 {
15787 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15788 return op;
15789 }
15790 }
15791 /* We shouldn't see any other uses of these in templates. */
15792 gcc_unreachable ();
15793 }
15794
15795 case CAST_EXPR:
15796 case REINTERPRET_CAST_EXPR:
15797 case CONST_CAST_EXPR:
15798 case STATIC_CAST_EXPR:
15799 case DYNAMIC_CAST_EXPR:
15800 case IMPLICIT_CONV_EXPR:
15801 case CONVERT_EXPR:
15802 case NOP_EXPR:
15803 {
15804 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15805 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15806 return build1 (code, type, op0);
15807 }
15808
15809 case SIZEOF_EXPR:
15810 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15811 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15812 {
15813 tree expanded, op = TREE_OPERAND (t, 0);
15814 int len = 0;
15815
15816 if (SIZEOF_EXPR_TYPE_P (t))
15817 op = TREE_TYPE (op);
15818
15819 ++cp_unevaluated_operand;
15820 ++c_inhibit_evaluation_warnings;
15821 /* We only want to compute the number of arguments. */
15822 if (PACK_EXPANSION_P (op))
15823 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15824 else
15825 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15826 args, complain, in_decl);
15827 --cp_unevaluated_operand;
15828 --c_inhibit_evaluation_warnings;
15829
15830 if (TREE_CODE (expanded) == TREE_VEC)
15831 {
15832 len = TREE_VEC_LENGTH (expanded);
15833 /* Set TREE_USED for the benefit of -Wunused. */
15834 for (int i = 0; i < len; i++)
15835 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15836 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15837 }
15838
15839 if (expanded == error_mark_node)
15840 return error_mark_node;
15841 else if (PACK_EXPANSION_P (expanded)
15842 || (TREE_CODE (expanded) == TREE_VEC
15843 && pack_expansion_args_count (expanded)))
15844
15845 {
15846 if (PACK_EXPANSION_P (expanded))
15847 /* OK. */;
15848 else if (TREE_VEC_LENGTH (expanded) == 1)
15849 expanded = TREE_VEC_ELT (expanded, 0);
15850 else
15851 expanded = make_argument_pack (expanded);
15852
15853 if (TYPE_P (expanded))
15854 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15855 false,
15856 complain & tf_error);
15857 else
15858 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15859 complain & tf_error);
15860 }
15861 else
15862 return build_int_cst (size_type_node, len);
15863 }
15864 if (SIZEOF_EXPR_TYPE_P (t))
15865 {
15866 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15867 args, complain, in_decl);
15868 r = build1 (NOP_EXPR, r, error_mark_node);
15869 r = build1 (SIZEOF_EXPR,
15870 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15871 SIZEOF_EXPR_TYPE_P (r) = 1;
15872 return r;
15873 }
15874 /* Fall through */
15875
15876 case INDIRECT_REF:
15877 case NEGATE_EXPR:
15878 case TRUTH_NOT_EXPR:
15879 case BIT_NOT_EXPR:
15880 case ADDR_EXPR:
15881 case UNARY_PLUS_EXPR: /* Unary + */
15882 case ALIGNOF_EXPR:
15883 case AT_ENCODE_EXPR:
15884 case ARROW_EXPR:
15885 case THROW_EXPR:
15886 case TYPEID_EXPR:
15887 case REALPART_EXPR:
15888 case IMAGPART_EXPR:
15889 case PAREN_EXPR:
15890 {
15891 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15892 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15893 r = build1 (code, type, op0);
15894 if (code == ALIGNOF_EXPR)
15895 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15896 return r;
15897 }
15898
15899 case COMPONENT_REF:
15900 {
15901 tree object;
15902 tree name;
15903
15904 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15905 name = TREE_OPERAND (t, 1);
15906 if (TREE_CODE (name) == BIT_NOT_EXPR)
15907 {
15908 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15909 complain, in_decl);
15910 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15911 }
15912 else if (TREE_CODE (name) == SCOPE_REF
15913 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15914 {
15915 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15916 complain, in_decl);
15917 name = TREE_OPERAND (name, 1);
15918 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15919 complain, in_decl);
15920 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15921 name = build_qualified_name (/*type=*/NULL_TREE,
15922 base, name,
15923 /*template_p=*/false);
15924 }
15925 else if (BASELINK_P (name))
15926 name = tsubst_baselink (name,
15927 non_reference (TREE_TYPE (object)),
15928 args, complain,
15929 in_decl);
15930 else
15931 name = tsubst_copy (name, args, complain, in_decl);
15932 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15933 }
15934
15935 case PLUS_EXPR:
15936 case MINUS_EXPR:
15937 case MULT_EXPR:
15938 case TRUNC_DIV_EXPR:
15939 case CEIL_DIV_EXPR:
15940 case FLOOR_DIV_EXPR:
15941 case ROUND_DIV_EXPR:
15942 case EXACT_DIV_EXPR:
15943 case BIT_AND_EXPR:
15944 case BIT_IOR_EXPR:
15945 case BIT_XOR_EXPR:
15946 case TRUNC_MOD_EXPR:
15947 case FLOOR_MOD_EXPR:
15948 case TRUTH_ANDIF_EXPR:
15949 case TRUTH_ORIF_EXPR:
15950 case TRUTH_AND_EXPR:
15951 case TRUTH_OR_EXPR:
15952 case RSHIFT_EXPR:
15953 case LSHIFT_EXPR:
15954 case RROTATE_EXPR:
15955 case LROTATE_EXPR:
15956 case EQ_EXPR:
15957 case NE_EXPR:
15958 case MAX_EXPR:
15959 case MIN_EXPR:
15960 case LE_EXPR:
15961 case GE_EXPR:
15962 case LT_EXPR:
15963 case GT_EXPR:
15964 case COMPOUND_EXPR:
15965 case DOTSTAR_EXPR:
15966 case MEMBER_REF:
15967 case PREDECREMENT_EXPR:
15968 case PREINCREMENT_EXPR:
15969 case POSTDECREMENT_EXPR:
15970 case POSTINCREMENT_EXPR:
15971 {
15972 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15973 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15974 return build_nt (code, op0, op1);
15975 }
15976
15977 case SCOPE_REF:
15978 {
15979 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15980 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15981 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15982 QUALIFIED_NAME_IS_TEMPLATE (t));
15983 }
15984
15985 case ARRAY_REF:
15986 {
15987 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15988 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15989 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15990 }
15991
15992 case CALL_EXPR:
15993 {
15994 int n = VL_EXP_OPERAND_LENGTH (t);
15995 tree result = build_vl_exp (CALL_EXPR, n);
15996 int i;
15997 for (i = 0; i < n; i++)
15998 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15999 complain, in_decl);
16000 return result;
16001 }
16002
16003 case COND_EXPR:
16004 case MODOP_EXPR:
16005 case PSEUDO_DTOR_EXPR:
16006 case VEC_PERM_EXPR:
16007 {
16008 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16009 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16010 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16011 r = build_nt (code, op0, op1, op2);
16012 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16013 return r;
16014 }
16015
16016 case NEW_EXPR:
16017 {
16018 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16019 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16020 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16021 r = build_nt (code, op0, op1, op2);
16022 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16023 return r;
16024 }
16025
16026 case DELETE_EXPR:
16027 {
16028 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16029 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16030 r = build_nt (code, op0, op1);
16031 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16032 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16033 return r;
16034 }
16035
16036 case TEMPLATE_ID_EXPR:
16037 {
16038 /* Substituted template arguments */
16039 tree fn = TREE_OPERAND (t, 0);
16040 tree targs = TREE_OPERAND (t, 1);
16041
16042 fn = tsubst_copy (fn, args, complain, in_decl);
16043 if (targs)
16044 targs = tsubst_template_args (targs, args, complain, in_decl);
16045
16046 return lookup_template_function (fn, targs);
16047 }
16048
16049 case TREE_LIST:
16050 {
16051 tree purpose, value, chain;
16052
16053 if (t == void_list_node)
16054 return t;
16055
16056 purpose = TREE_PURPOSE (t);
16057 if (purpose)
16058 purpose = tsubst_copy (purpose, args, complain, in_decl);
16059 value = TREE_VALUE (t);
16060 if (value)
16061 value = tsubst_copy (value, args, complain, in_decl);
16062 chain = TREE_CHAIN (t);
16063 if (chain && chain != void_type_node)
16064 chain = tsubst_copy (chain, args, complain, in_decl);
16065 if (purpose == TREE_PURPOSE (t)
16066 && value == TREE_VALUE (t)
16067 && chain == TREE_CHAIN (t))
16068 return t;
16069 return tree_cons (purpose, value, chain);
16070 }
16071
16072 case RECORD_TYPE:
16073 case UNION_TYPE:
16074 case ENUMERAL_TYPE:
16075 case INTEGER_TYPE:
16076 case TEMPLATE_TYPE_PARM:
16077 case TEMPLATE_TEMPLATE_PARM:
16078 case BOUND_TEMPLATE_TEMPLATE_PARM:
16079 case TEMPLATE_PARM_INDEX:
16080 case POINTER_TYPE:
16081 case REFERENCE_TYPE:
16082 case OFFSET_TYPE:
16083 case FUNCTION_TYPE:
16084 case METHOD_TYPE:
16085 case ARRAY_TYPE:
16086 case TYPENAME_TYPE:
16087 case UNBOUND_CLASS_TEMPLATE:
16088 case TYPEOF_TYPE:
16089 case DECLTYPE_TYPE:
16090 case TYPE_DECL:
16091 return tsubst (t, args, complain, in_decl);
16092
16093 case USING_DECL:
16094 t = DECL_NAME (t);
16095 /* Fall through. */
16096 case IDENTIFIER_NODE:
16097 if (IDENTIFIER_CONV_OP_P (t))
16098 {
16099 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16100 return make_conv_op_name (new_type);
16101 }
16102 else
16103 return t;
16104
16105 case CONSTRUCTOR:
16106 /* This is handled by tsubst_copy_and_build. */
16107 gcc_unreachable ();
16108
16109 case VA_ARG_EXPR:
16110 {
16111 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16112 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16113 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16114 }
16115
16116 case CLEANUP_POINT_EXPR:
16117 /* We shouldn't have built any of these during initial template
16118 generation. Instead, they should be built during instantiation
16119 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16120 gcc_unreachable ();
16121
16122 case OFFSET_REF:
16123 {
16124 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16125 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16126 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16127 r = build2 (code, type, op0, op1);
16128 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16129 if (!mark_used (TREE_OPERAND (r, 1), complain)
16130 && !(complain & tf_error))
16131 return error_mark_node;
16132 return r;
16133 }
16134
16135 case EXPR_PACK_EXPANSION:
16136 error ("invalid use of pack expansion expression");
16137 return error_mark_node;
16138
16139 case NONTYPE_ARGUMENT_PACK:
16140 error ("use %<...%> to expand argument pack");
16141 return error_mark_node;
16142
16143 case VOID_CST:
16144 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16145 return t;
16146
16147 case INTEGER_CST:
16148 case REAL_CST:
16149 case STRING_CST:
16150 case COMPLEX_CST:
16151 {
16152 /* Instantiate any typedefs in the type. */
16153 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16154 r = fold_convert (type, t);
16155 gcc_assert (TREE_CODE (r) == code);
16156 return r;
16157 }
16158
16159 case PTRMEM_CST:
16160 /* These can sometimes show up in a partial instantiation, but never
16161 involve template parms. */
16162 gcc_assert (!uses_template_parms (t));
16163 return t;
16164
16165 case UNARY_LEFT_FOLD_EXPR:
16166 return tsubst_unary_left_fold (t, args, complain, in_decl);
16167 case UNARY_RIGHT_FOLD_EXPR:
16168 return tsubst_unary_right_fold (t, args, complain, in_decl);
16169 case BINARY_LEFT_FOLD_EXPR:
16170 return tsubst_binary_left_fold (t, args, complain, in_decl);
16171 case BINARY_RIGHT_FOLD_EXPR:
16172 return tsubst_binary_right_fold (t, args, complain, in_decl);
16173 case PREDICT_EXPR:
16174 return t;
16175
16176 case DEBUG_BEGIN_STMT:
16177 /* ??? There's no point in copying it for now, but maybe some
16178 day it will contain more information, such as a pointer back
16179 to the containing function, inlined copy or so. */
16180 return t;
16181
16182 default:
16183 /* We shouldn't get here, but keep going if !flag_checking. */
16184 if (flag_checking)
16185 gcc_unreachable ();
16186 return t;
16187 }
16188 }
16189
16190 /* Helper function for tsubst_omp_clauses, used for instantiation of
16191 OMP_CLAUSE_DECL of clauses. */
16192
16193 static tree
16194 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16195 tree in_decl, tree *iterator_cache)
16196 {
16197 if (decl == NULL_TREE)
16198 return NULL_TREE;
16199
16200 /* Handle OpenMP iterators. */
16201 if (TREE_CODE (decl) == TREE_LIST
16202 && TREE_PURPOSE (decl)
16203 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16204 {
16205 tree ret;
16206 if (iterator_cache[0] == TREE_PURPOSE (decl))
16207 ret = iterator_cache[1];
16208 else
16209 {
16210 tree *tp = &ret;
16211 begin_scope (sk_omp, NULL);
16212 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16213 {
16214 *tp = copy_node (it);
16215 TREE_VEC_ELT (*tp, 0)
16216 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16217 TREE_VEC_ELT (*tp, 1)
16218 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16219 /*integral_constant_expression_p=*/false);
16220 TREE_VEC_ELT (*tp, 2)
16221 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16222 /*integral_constant_expression_p=*/false);
16223 TREE_VEC_ELT (*tp, 3)
16224 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16225 /*integral_constant_expression_p=*/false);
16226 TREE_CHAIN (*tp) = NULL_TREE;
16227 tp = &TREE_CHAIN (*tp);
16228 }
16229 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16230 iterator_cache[0] = TREE_PURPOSE (decl);
16231 iterator_cache[1] = ret;
16232 }
16233 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16234 args, complain,
16235 in_decl, NULL));
16236 }
16237
16238 /* Handle an OpenMP array section represented as a TREE_LIST (or
16239 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16240 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16241 TREE_LIST. We can handle it exactly the same as an array section
16242 (purpose, value, and a chain), even though the nomenclature
16243 (low_bound, length, etc) is different. */
16244 if (TREE_CODE (decl) == TREE_LIST)
16245 {
16246 tree low_bound
16247 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16248 /*integral_constant_expression_p=*/false);
16249 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16250 /*integral_constant_expression_p=*/false);
16251 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16252 in_decl, NULL);
16253 if (TREE_PURPOSE (decl) == low_bound
16254 && TREE_VALUE (decl) == length
16255 && TREE_CHAIN (decl) == chain)
16256 return decl;
16257 tree ret = tree_cons (low_bound, length, chain);
16258 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16259 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16260 return ret;
16261 }
16262 tree ret = tsubst_expr (decl, args, complain, in_decl,
16263 /*integral_constant_expression_p=*/false);
16264 /* Undo convert_from_reference tsubst_expr could have called. */
16265 if (decl
16266 && REFERENCE_REF_P (ret)
16267 && !REFERENCE_REF_P (decl))
16268 ret = TREE_OPERAND (ret, 0);
16269 return ret;
16270 }
16271
16272 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16273
16274 static tree
16275 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16276 tree args, tsubst_flags_t complain, tree in_decl)
16277 {
16278 tree new_clauses = NULL_TREE, nc, oc;
16279 tree linear_no_step = NULL_TREE;
16280 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16281
16282 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16283 {
16284 nc = copy_node (oc);
16285 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16286 new_clauses = nc;
16287
16288 switch (OMP_CLAUSE_CODE (nc))
16289 {
16290 case OMP_CLAUSE_LASTPRIVATE:
16291 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16292 {
16293 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16294 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16295 in_decl, /*integral_constant_expression_p=*/false);
16296 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16297 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16298 }
16299 /* FALLTHRU */
16300 case OMP_CLAUSE_PRIVATE:
16301 case OMP_CLAUSE_SHARED:
16302 case OMP_CLAUSE_FIRSTPRIVATE:
16303 case OMP_CLAUSE_COPYIN:
16304 case OMP_CLAUSE_COPYPRIVATE:
16305 case OMP_CLAUSE_UNIFORM:
16306 case OMP_CLAUSE_DEPEND:
16307 case OMP_CLAUSE_FROM:
16308 case OMP_CLAUSE_TO:
16309 case OMP_CLAUSE_MAP:
16310 case OMP_CLAUSE_NONTEMPORAL:
16311 case OMP_CLAUSE_USE_DEVICE_PTR:
16312 case OMP_CLAUSE_IS_DEVICE_PTR:
16313 OMP_CLAUSE_DECL (nc)
16314 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16315 in_decl, iterator_cache);
16316 break;
16317 case OMP_CLAUSE_TILE:
16318 case OMP_CLAUSE_IF:
16319 case OMP_CLAUSE_NUM_THREADS:
16320 case OMP_CLAUSE_SCHEDULE:
16321 case OMP_CLAUSE_COLLAPSE:
16322 case OMP_CLAUSE_FINAL:
16323 case OMP_CLAUSE_DEVICE:
16324 case OMP_CLAUSE_DIST_SCHEDULE:
16325 case OMP_CLAUSE_NUM_TEAMS:
16326 case OMP_CLAUSE_THREAD_LIMIT:
16327 case OMP_CLAUSE_SAFELEN:
16328 case OMP_CLAUSE_SIMDLEN:
16329 case OMP_CLAUSE_NUM_TASKS:
16330 case OMP_CLAUSE_GRAINSIZE:
16331 case OMP_CLAUSE_PRIORITY:
16332 case OMP_CLAUSE_ORDERED:
16333 case OMP_CLAUSE_HINT:
16334 case OMP_CLAUSE_NUM_GANGS:
16335 case OMP_CLAUSE_NUM_WORKERS:
16336 case OMP_CLAUSE_VECTOR_LENGTH:
16337 case OMP_CLAUSE_WORKER:
16338 case OMP_CLAUSE_VECTOR:
16339 case OMP_CLAUSE_ASYNC:
16340 case OMP_CLAUSE_WAIT:
16341 OMP_CLAUSE_OPERAND (nc, 0)
16342 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16343 in_decl, /*integral_constant_expression_p=*/false);
16344 break;
16345 case OMP_CLAUSE_REDUCTION:
16346 case OMP_CLAUSE_IN_REDUCTION:
16347 case OMP_CLAUSE_TASK_REDUCTION:
16348 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16349 {
16350 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16351 if (TREE_CODE (placeholder) == SCOPE_REF)
16352 {
16353 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16354 complain, in_decl);
16355 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16356 = build_qualified_name (NULL_TREE, scope,
16357 TREE_OPERAND (placeholder, 1),
16358 false);
16359 }
16360 else
16361 gcc_assert (identifier_p (placeholder));
16362 }
16363 OMP_CLAUSE_DECL (nc)
16364 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16365 in_decl, NULL);
16366 break;
16367 case OMP_CLAUSE_GANG:
16368 case OMP_CLAUSE_ALIGNED:
16369 OMP_CLAUSE_DECL (nc)
16370 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16371 in_decl, NULL);
16372 OMP_CLAUSE_OPERAND (nc, 1)
16373 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16374 in_decl, /*integral_constant_expression_p=*/false);
16375 break;
16376 case OMP_CLAUSE_LINEAR:
16377 OMP_CLAUSE_DECL (nc)
16378 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16379 in_decl, NULL);
16380 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16381 {
16382 gcc_assert (!linear_no_step);
16383 linear_no_step = nc;
16384 }
16385 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16386 OMP_CLAUSE_LINEAR_STEP (nc)
16387 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16388 complain, in_decl, NULL);
16389 else
16390 OMP_CLAUSE_LINEAR_STEP (nc)
16391 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16392 in_decl,
16393 /*integral_constant_expression_p=*/false);
16394 break;
16395 case OMP_CLAUSE_NOWAIT:
16396 case OMP_CLAUSE_DEFAULT:
16397 case OMP_CLAUSE_UNTIED:
16398 case OMP_CLAUSE_MERGEABLE:
16399 case OMP_CLAUSE_INBRANCH:
16400 case OMP_CLAUSE_NOTINBRANCH:
16401 case OMP_CLAUSE_PROC_BIND:
16402 case OMP_CLAUSE_FOR:
16403 case OMP_CLAUSE_PARALLEL:
16404 case OMP_CLAUSE_SECTIONS:
16405 case OMP_CLAUSE_TASKGROUP:
16406 case OMP_CLAUSE_NOGROUP:
16407 case OMP_CLAUSE_THREADS:
16408 case OMP_CLAUSE_SIMD:
16409 case OMP_CLAUSE_DEFAULTMAP:
16410 case OMP_CLAUSE_INDEPENDENT:
16411 case OMP_CLAUSE_AUTO:
16412 case OMP_CLAUSE_SEQ:
16413 case OMP_CLAUSE_IF_PRESENT:
16414 case OMP_CLAUSE_FINALIZE:
16415 break;
16416 default:
16417 gcc_unreachable ();
16418 }
16419 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16420 switch (OMP_CLAUSE_CODE (nc))
16421 {
16422 case OMP_CLAUSE_SHARED:
16423 case OMP_CLAUSE_PRIVATE:
16424 case OMP_CLAUSE_FIRSTPRIVATE:
16425 case OMP_CLAUSE_LASTPRIVATE:
16426 case OMP_CLAUSE_COPYPRIVATE:
16427 case OMP_CLAUSE_LINEAR:
16428 case OMP_CLAUSE_REDUCTION:
16429 case OMP_CLAUSE_IN_REDUCTION:
16430 case OMP_CLAUSE_TASK_REDUCTION:
16431 case OMP_CLAUSE_USE_DEVICE_PTR:
16432 case OMP_CLAUSE_IS_DEVICE_PTR:
16433 /* tsubst_expr on SCOPE_REF results in returning
16434 finish_non_static_data_member result. Undo that here. */
16435 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16436 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16437 == IDENTIFIER_NODE))
16438 {
16439 tree t = OMP_CLAUSE_DECL (nc);
16440 tree v = t;
16441 while (v)
16442 switch (TREE_CODE (v))
16443 {
16444 case COMPONENT_REF:
16445 case MEM_REF:
16446 case INDIRECT_REF:
16447 CASE_CONVERT:
16448 case POINTER_PLUS_EXPR:
16449 v = TREE_OPERAND (v, 0);
16450 continue;
16451 case PARM_DECL:
16452 if (DECL_CONTEXT (v) == current_function_decl
16453 && DECL_ARTIFICIAL (v)
16454 && DECL_NAME (v) == this_identifier)
16455 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16456 /* FALLTHRU */
16457 default:
16458 v = NULL_TREE;
16459 break;
16460 }
16461 }
16462 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16463 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16464 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16465 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16466 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16467 {
16468 tree decl = OMP_CLAUSE_DECL (nc);
16469 if (VAR_P (decl))
16470 {
16471 retrofit_lang_decl (decl);
16472 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16473 }
16474 }
16475 break;
16476 default:
16477 break;
16478 }
16479 }
16480
16481 new_clauses = nreverse (new_clauses);
16482 if (ort != C_ORT_OMP_DECLARE_SIMD)
16483 {
16484 new_clauses = finish_omp_clauses (new_clauses, ort);
16485 if (linear_no_step)
16486 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16487 if (nc == linear_no_step)
16488 {
16489 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16490 break;
16491 }
16492 }
16493 return new_clauses;
16494 }
16495
16496 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16497
16498 static tree
16499 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16500 tree in_decl)
16501 {
16502 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16503
16504 tree purpose, value, chain;
16505
16506 if (t == NULL)
16507 return t;
16508
16509 if (TREE_CODE (t) != TREE_LIST)
16510 return tsubst_copy_and_build (t, args, complain, in_decl,
16511 /*function_p=*/false,
16512 /*integral_constant_expression_p=*/false);
16513
16514 if (t == void_list_node)
16515 return t;
16516
16517 purpose = TREE_PURPOSE (t);
16518 if (purpose)
16519 purpose = RECUR (purpose);
16520 value = TREE_VALUE (t);
16521 if (value)
16522 {
16523 if (TREE_CODE (value) != LABEL_DECL)
16524 value = RECUR (value);
16525 else
16526 {
16527 value = lookup_label (DECL_NAME (value));
16528 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16529 TREE_USED (value) = 1;
16530 }
16531 }
16532 chain = TREE_CHAIN (t);
16533 if (chain && chain != void_type_node)
16534 chain = RECUR (chain);
16535 return tree_cons (purpose, value, chain);
16536 #undef RECUR
16537 }
16538
16539 /* Used to temporarily communicate the list of #pragma omp parallel
16540 clauses to #pragma omp for instantiation if they are combined
16541 together. */
16542
16543 static tree *omp_parallel_combined_clauses;
16544
16545 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
16546 tree *, unsigned int *);
16547
16548 /* Substitute one OMP_FOR iterator. */
16549
16550 static bool
16551 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
16552 tree initv, tree condv, tree incrv, tree *clauses,
16553 tree args, tsubst_flags_t complain, tree in_decl,
16554 bool integral_constant_expression_p)
16555 {
16556 #define RECUR(NODE) \
16557 tsubst_expr ((NODE), args, complain, in_decl, \
16558 integral_constant_expression_p)
16559 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
16560 bool ret = false;
16561
16562 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16563 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16564
16565 decl = TREE_OPERAND (init, 0);
16566 init = TREE_OPERAND (init, 1);
16567 tree decl_expr = NULL_TREE;
16568 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
16569 if (range_for)
16570 {
16571 bool decomp = false;
16572 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
16573 {
16574 tree v = DECL_VALUE_EXPR (decl);
16575 if (TREE_CODE (v) == ARRAY_REF
16576 && VAR_P (TREE_OPERAND (v, 0))
16577 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
16578 {
16579 tree decomp_first = NULL_TREE;
16580 unsigned decomp_cnt = 0;
16581 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
16582 maybe_push_decl (d);
16583 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
16584 in_decl, &decomp_first, &decomp_cnt);
16585 decomp = true;
16586 if (d == error_mark_node)
16587 decl = error_mark_node;
16588 else
16589 for (unsigned int i = 0; i < decomp_cnt; i++)
16590 {
16591 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
16592 {
16593 tree v = build_nt (ARRAY_REF, d,
16594 size_int (decomp_cnt - i - 1),
16595 NULL_TREE, NULL_TREE);
16596 SET_DECL_VALUE_EXPR (decomp_first, v);
16597 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
16598 }
16599 fit_decomposition_lang_decl (decomp_first, d);
16600 decomp_first = DECL_CHAIN (decomp_first);
16601 }
16602 }
16603 }
16604 decl = tsubst_decl (decl, args, complain);
16605 if (!decomp)
16606 maybe_push_decl (decl);
16607 }
16608 else if (init && TREE_CODE (init) == DECL_EXPR)
16609 {
16610 /* We need to jump through some hoops to handle declarations in the
16611 init-statement, since we might need to handle auto deduction,
16612 but we need to keep control of initialization. */
16613 decl_expr = init;
16614 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16615 decl = tsubst_decl (decl, args, complain);
16616 }
16617 else
16618 {
16619 if (TREE_CODE (decl) == SCOPE_REF)
16620 {
16621 decl = RECUR (decl);
16622 if (TREE_CODE (decl) == COMPONENT_REF)
16623 {
16624 tree v = decl;
16625 while (v)
16626 switch (TREE_CODE (v))
16627 {
16628 case COMPONENT_REF:
16629 case MEM_REF:
16630 case INDIRECT_REF:
16631 CASE_CONVERT:
16632 case POINTER_PLUS_EXPR:
16633 v = TREE_OPERAND (v, 0);
16634 continue;
16635 case PARM_DECL:
16636 if (DECL_CONTEXT (v) == current_function_decl
16637 && DECL_ARTIFICIAL (v)
16638 && DECL_NAME (v) == this_identifier)
16639 {
16640 decl = TREE_OPERAND (decl, 1);
16641 decl = omp_privatize_field (decl, false);
16642 }
16643 /* FALLTHRU */
16644 default:
16645 v = NULL_TREE;
16646 break;
16647 }
16648 }
16649 }
16650 else
16651 decl = RECUR (decl);
16652 }
16653 init = RECUR (init);
16654
16655 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16656 {
16657 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16658 if (TREE_CODE (o) == TREE_LIST)
16659 TREE_VEC_ELT (orig_declv, i)
16660 = tree_cons (RECUR (TREE_PURPOSE (o)),
16661 RECUR (TREE_VALUE (o)),
16662 NULL_TREE);
16663 else
16664 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16665 }
16666
16667 if (range_for)
16668 {
16669 tree this_pre_body = NULL_TREE;
16670 tree orig_init = NULL_TREE;
16671 tree orig_decl = NULL_TREE;
16672 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
16673 orig_init, cond, incr);
16674 if (orig_decl)
16675 {
16676 if (orig_declv == NULL_TREE)
16677 orig_declv = copy_node (declv);
16678 TREE_VEC_ELT (orig_declv, i) = orig_decl;
16679 ret = true;
16680 }
16681 else if (orig_declv)
16682 TREE_VEC_ELT (orig_declv, i) = decl;
16683 }
16684
16685 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16686 if (!range_for && auto_node && init)
16687 TREE_TYPE (decl)
16688 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16689
16690 gcc_assert (!type_dependent_expression_p (decl));
16691
16692 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
16693 {
16694 if (decl_expr)
16695 {
16696 /* Declare the variable, but don't let that initialize it. */
16697 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16698 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16699 RECUR (decl_expr);
16700 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16701 }
16702
16703 if (!range_for)
16704 {
16705 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16706 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16707 if (TREE_CODE (incr) == MODIFY_EXPR)
16708 {
16709 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16710 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16711 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16712 NOP_EXPR, rhs, complain);
16713 }
16714 else
16715 incr = RECUR (incr);
16716 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16717 TREE_VEC_ELT (orig_declv, i) = decl;
16718 }
16719 TREE_VEC_ELT (declv, i) = decl;
16720 TREE_VEC_ELT (initv, i) = init;
16721 TREE_VEC_ELT (condv, i) = cond;
16722 TREE_VEC_ELT (incrv, i) = incr;
16723 return ret;
16724 }
16725
16726 if (decl_expr)
16727 {
16728 /* Declare and initialize the variable. */
16729 RECUR (decl_expr);
16730 init = NULL_TREE;
16731 }
16732 else if (init)
16733 {
16734 tree *pc;
16735 int j;
16736 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16737 {
16738 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16739 {
16740 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16741 && OMP_CLAUSE_DECL (*pc) == decl)
16742 break;
16743 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16744 && OMP_CLAUSE_DECL (*pc) == decl)
16745 {
16746 if (j)
16747 break;
16748 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16749 tree c = *pc;
16750 *pc = OMP_CLAUSE_CHAIN (c);
16751 OMP_CLAUSE_CHAIN (c) = *clauses;
16752 *clauses = c;
16753 }
16754 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16755 && OMP_CLAUSE_DECL (*pc) == decl)
16756 {
16757 error ("iteration variable %qD should not be firstprivate",
16758 decl);
16759 *pc = OMP_CLAUSE_CHAIN (*pc);
16760 }
16761 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16762 && OMP_CLAUSE_DECL (*pc) == decl)
16763 {
16764 error ("iteration variable %qD should not be reduction",
16765 decl);
16766 *pc = OMP_CLAUSE_CHAIN (*pc);
16767 }
16768 else
16769 pc = &OMP_CLAUSE_CHAIN (*pc);
16770 }
16771 if (*pc)
16772 break;
16773 }
16774 if (*pc == NULL_TREE)
16775 {
16776 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16777 OMP_CLAUSE_DECL (c) = decl;
16778 c = finish_omp_clauses (c, C_ORT_OMP);
16779 if (c)
16780 {
16781 OMP_CLAUSE_CHAIN (c) = *clauses;
16782 *clauses = c;
16783 }
16784 }
16785 }
16786 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16787 if (COMPARISON_CLASS_P (cond))
16788 {
16789 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16790 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16791 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16792 }
16793 else
16794 cond = RECUR (cond);
16795 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16796 switch (TREE_CODE (incr))
16797 {
16798 case PREINCREMENT_EXPR:
16799 case PREDECREMENT_EXPR:
16800 case POSTINCREMENT_EXPR:
16801 case POSTDECREMENT_EXPR:
16802 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16803 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16804 break;
16805 case MODIFY_EXPR:
16806 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16807 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16808 {
16809 tree rhs = TREE_OPERAND (incr, 1);
16810 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16811 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16812 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16813 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16814 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16815 rhs0, rhs1));
16816 }
16817 else
16818 incr = RECUR (incr);
16819 break;
16820 case MODOP_EXPR:
16821 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16822 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16823 {
16824 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16825 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16826 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16827 TREE_TYPE (decl), lhs,
16828 RECUR (TREE_OPERAND (incr, 2))));
16829 }
16830 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16831 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16832 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16833 {
16834 tree rhs = TREE_OPERAND (incr, 2);
16835 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16836 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16837 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16838 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16839 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16840 rhs0, rhs1));
16841 }
16842 else
16843 incr = RECUR (incr);
16844 break;
16845 default:
16846 incr = RECUR (incr);
16847 break;
16848 }
16849
16850 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16851 TREE_VEC_ELT (orig_declv, i) = decl;
16852 TREE_VEC_ELT (declv, i) = decl;
16853 TREE_VEC_ELT (initv, i) = init;
16854 TREE_VEC_ELT (condv, i) = cond;
16855 TREE_VEC_ELT (incrv, i) = incr;
16856 return false;
16857 #undef RECUR
16858 }
16859
16860 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16861 of OMP_TARGET's body. */
16862
16863 static tree
16864 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16865 {
16866 *walk_subtrees = 0;
16867 switch (TREE_CODE (*tp))
16868 {
16869 case OMP_TEAMS:
16870 return *tp;
16871 case BIND_EXPR:
16872 case STATEMENT_LIST:
16873 *walk_subtrees = 1;
16874 break;
16875 default:
16876 break;
16877 }
16878 return NULL_TREE;
16879 }
16880
16881 /* Helper function for tsubst_expr. For decomposition declaration
16882 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16883 also the corresponding decls representing the identifiers
16884 of the decomposition declaration. Return DECL if successful
16885 or error_mark_node otherwise, set *FIRST to the first decl
16886 in the list chained through DECL_CHAIN and *CNT to the number
16887 of such decls. */
16888
16889 static tree
16890 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16891 tsubst_flags_t complain, tree in_decl, tree *first,
16892 unsigned int *cnt)
16893 {
16894 tree decl2, decl3, prev = decl;
16895 *cnt = 0;
16896 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16897 for (decl2 = DECL_CHAIN (pattern_decl);
16898 decl2
16899 && VAR_P (decl2)
16900 && DECL_DECOMPOSITION_P (decl2)
16901 && DECL_NAME (decl2);
16902 decl2 = DECL_CHAIN (decl2))
16903 {
16904 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16905 {
16906 gcc_assert (errorcount);
16907 return error_mark_node;
16908 }
16909 (*cnt)++;
16910 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16911 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16912 tree v = DECL_VALUE_EXPR (decl2);
16913 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16914 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16915 decl3 = tsubst (decl2, args, complain, in_decl);
16916 SET_DECL_VALUE_EXPR (decl2, v);
16917 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16918 if (VAR_P (decl3))
16919 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16920 else
16921 {
16922 gcc_assert (errorcount);
16923 decl = error_mark_node;
16924 continue;
16925 }
16926 maybe_push_decl (decl3);
16927 if (error_operand_p (decl3))
16928 decl = error_mark_node;
16929 else if (decl != error_mark_node
16930 && DECL_CHAIN (decl3) != prev
16931 && decl != prev)
16932 {
16933 gcc_assert (errorcount);
16934 decl = error_mark_node;
16935 }
16936 else
16937 prev = decl3;
16938 }
16939 *first = prev;
16940 return decl;
16941 }
16942
16943 /* Return the proper local_specialization for init-capture pack DECL. */
16944
16945 static tree
16946 lookup_init_capture_pack (tree decl)
16947 {
16948 /* We handle normal pack captures by forwarding to the specialization of the
16949 captured parameter. We can't do that for pack init-captures; we need them
16950 to have their own local_specialization. We created the individual
16951 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16952 when we process the DECL_EXPR for the pack init-capture in the template.
16953 So, how do we find them? We don't know the capture proxy pack when
16954 building the individual resulting proxies, and we don't know the
16955 individual proxies when instantiating the pack. What we have in common is
16956 the FIELD_DECL.
16957
16958 So...when we instantiate the FIELD_DECL, we stick the result in
16959 local_specializations. Then at the DECL_EXPR we look up that result, see
16960 how many elements it has, synthesize the names, and look them up. */
16961
16962 tree cname = DECL_NAME (decl);
16963 tree val = DECL_VALUE_EXPR (decl);
16964 tree field = TREE_OPERAND (val, 1);
16965 gcc_assert (TREE_CODE (field) == FIELD_DECL);
16966 tree fpack = retrieve_local_specialization (field);
16967 if (fpack == error_mark_node)
16968 return error_mark_node;
16969
16970 int len = 1;
16971 tree vec = NULL_TREE;
16972 tree r = NULL_TREE;
16973 if (TREE_CODE (fpack) == TREE_VEC)
16974 {
16975 len = TREE_VEC_LENGTH (fpack);
16976 vec = make_tree_vec (len);
16977 r = make_node (NONTYPE_ARGUMENT_PACK);
16978 SET_ARGUMENT_PACK_ARGS (r, vec);
16979 }
16980 for (int i = 0; i < len; ++i)
16981 {
16982 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
16983 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
16984 if (vec)
16985 TREE_VEC_ELT (vec, i) = elt;
16986 else
16987 r = elt;
16988 }
16989 return r;
16990 }
16991
16992 /* Like tsubst_copy for expressions, etc. but also does semantic
16993 processing. */
16994
16995 tree
16996 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16997 bool integral_constant_expression_p)
16998 {
16999 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17000 #define RECUR(NODE) \
17001 tsubst_expr ((NODE), args, complain, in_decl, \
17002 integral_constant_expression_p)
17003
17004 tree stmt, tmp;
17005 tree r;
17006 location_t loc;
17007
17008 if (t == NULL_TREE || t == error_mark_node)
17009 return t;
17010
17011 loc = input_location;
17012 if (location_t eloc = cp_expr_location (t))
17013 input_location = eloc;
17014 if (STATEMENT_CODE_P (TREE_CODE (t)))
17015 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17016
17017 switch (TREE_CODE (t))
17018 {
17019 case STATEMENT_LIST:
17020 {
17021 tree_stmt_iterator i;
17022 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17023 RECUR (tsi_stmt (i));
17024 break;
17025 }
17026
17027 case CTOR_INITIALIZER:
17028 finish_mem_initializers (tsubst_initializer_list
17029 (TREE_OPERAND (t, 0), args));
17030 break;
17031
17032 case RETURN_EXPR:
17033 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17034 break;
17035
17036 case EXPR_STMT:
17037 tmp = RECUR (EXPR_STMT_EXPR (t));
17038 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17039 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17040 else
17041 finish_expr_stmt (tmp);
17042 break;
17043
17044 case USING_STMT:
17045 finish_local_using_directive (USING_STMT_NAMESPACE (t),
17046 /*attribs=*/NULL_TREE);
17047 break;
17048
17049 case DECL_EXPR:
17050 {
17051 tree decl, pattern_decl;
17052 tree init;
17053
17054 pattern_decl = decl = DECL_EXPR_DECL (t);
17055 if (TREE_CODE (decl) == LABEL_DECL)
17056 finish_label_decl (DECL_NAME (decl));
17057 else if (TREE_CODE (decl) == USING_DECL)
17058 {
17059 tree scope = USING_DECL_SCOPE (decl);
17060 tree name = DECL_NAME (decl);
17061
17062 scope = tsubst (scope, args, complain, in_decl);
17063 decl = lookup_qualified_name (scope, name,
17064 /*is_type_p=*/false,
17065 /*complain=*/false);
17066 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
17067 qualified_name_lookup_error (scope, name, decl, input_location);
17068 else
17069 finish_local_using_decl (decl, scope, name);
17070 }
17071 else if (is_capture_proxy (decl)
17072 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17073 {
17074 /* We're in tsubst_lambda_expr, we've already inserted a new
17075 capture proxy, so look it up and register it. */
17076 tree inst;
17077 if (!DECL_PACK_P (decl))
17078 {
17079 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17080 /*nonclass*/1, /*block_p=*/true,
17081 /*ns_only*/0, LOOKUP_HIDDEN);
17082 gcc_assert (inst != decl && is_capture_proxy (inst));
17083 }
17084 else if (is_normal_capture_proxy (decl))
17085 {
17086 inst = (retrieve_local_specialization
17087 (DECL_CAPTURED_VARIABLE (decl)));
17088 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17089 }
17090 else
17091 inst = lookup_init_capture_pack (decl);
17092
17093 register_local_specialization (inst, decl);
17094 break;
17095 }
17096 else if (DECL_PRETTY_FUNCTION_P (decl))
17097 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17098 DECL_NAME (decl),
17099 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17100 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17101 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17102 /* Don't copy the old closure; we'll create a new one in
17103 tsubst_lambda_expr. */
17104 break;
17105 else
17106 {
17107 init = DECL_INITIAL (decl);
17108 decl = tsubst (decl, args, complain, in_decl);
17109 if (decl != error_mark_node)
17110 {
17111 /* By marking the declaration as instantiated, we avoid
17112 trying to instantiate it. Since instantiate_decl can't
17113 handle local variables, and since we've already done
17114 all that needs to be done, that's the right thing to
17115 do. */
17116 if (VAR_P (decl))
17117 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17118 if (VAR_P (decl) && !DECL_NAME (decl)
17119 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17120 /* Anonymous aggregates are a special case. */
17121 finish_anon_union (decl);
17122 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17123 {
17124 DECL_CONTEXT (decl) = current_function_decl;
17125 if (DECL_NAME (decl) == this_identifier)
17126 {
17127 tree lam = DECL_CONTEXT (current_function_decl);
17128 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17129 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17130 }
17131 insert_capture_proxy (decl);
17132 }
17133 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17134 /* We already did a pushtag. */;
17135 else if (TREE_CODE (decl) == FUNCTION_DECL
17136 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17137 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17138 {
17139 DECL_CONTEXT (decl) = NULL_TREE;
17140 pushdecl (decl);
17141 DECL_CONTEXT (decl) = current_function_decl;
17142 cp_check_omp_declare_reduction (decl);
17143 }
17144 else
17145 {
17146 int const_init = false;
17147 unsigned int cnt = 0;
17148 tree first = NULL_TREE, ndecl = error_mark_node;
17149 maybe_push_decl (decl);
17150
17151 if (VAR_P (decl)
17152 && DECL_DECOMPOSITION_P (decl)
17153 && TREE_TYPE (pattern_decl) != error_mark_node)
17154 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17155 complain, in_decl, &first,
17156 &cnt);
17157
17158 init = tsubst_init (init, decl, args, complain, in_decl);
17159
17160 if (VAR_P (decl))
17161 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17162 (pattern_decl));
17163
17164 if (ndecl != error_mark_node)
17165 cp_maybe_mangle_decomp (ndecl, first, cnt);
17166
17167 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
17168
17169 if (ndecl != error_mark_node)
17170 cp_finish_decomp (ndecl, first, cnt);
17171 }
17172 }
17173 }
17174
17175 break;
17176 }
17177
17178 case FOR_STMT:
17179 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17180 RECUR (FOR_INIT_STMT (t));
17181 finish_init_stmt (stmt);
17182 tmp = RECUR (FOR_COND (t));
17183 finish_for_cond (tmp, stmt, false, 0);
17184 tmp = RECUR (FOR_EXPR (t));
17185 finish_for_expr (tmp, stmt);
17186 {
17187 bool prev = note_iteration_stmt_body_start ();
17188 RECUR (FOR_BODY (t));
17189 note_iteration_stmt_body_end (prev);
17190 }
17191 finish_for_stmt (stmt);
17192 break;
17193
17194 case RANGE_FOR_STMT:
17195 {
17196 /* Construct another range_for, if this is not a final
17197 substitution (for inside inside a generic lambda of a
17198 template). Otherwise convert to a regular for. */
17199 tree decl, expr;
17200 stmt = (processing_template_decl
17201 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17202 : begin_for_stmt (NULL_TREE, NULL_TREE));
17203 RECUR (RANGE_FOR_INIT_STMT (t));
17204 decl = RANGE_FOR_DECL (t);
17205 decl = tsubst (decl, args, complain, in_decl);
17206 maybe_push_decl (decl);
17207 expr = RECUR (RANGE_FOR_EXPR (t));
17208
17209 tree decomp_first = NULL_TREE;
17210 unsigned decomp_cnt = 0;
17211 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17212 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17213 complain, in_decl,
17214 &decomp_first, &decomp_cnt);
17215
17216 if (processing_template_decl)
17217 {
17218 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17219 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17220 finish_range_for_decl (stmt, decl, expr);
17221 if (decomp_first && decl != error_mark_node)
17222 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17223 }
17224 else
17225 {
17226 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17227 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17228 stmt = cp_convert_range_for (stmt, decl, expr,
17229 decomp_first, decomp_cnt,
17230 RANGE_FOR_IVDEP (t), unroll);
17231 }
17232
17233 bool prev = note_iteration_stmt_body_start ();
17234 RECUR (RANGE_FOR_BODY (t));
17235 note_iteration_stmt_body_end (prev);
17236 finish_for_stmt (stmt);
17237 }
17238 break;
17239
17240 case WHILE_STMT:
17241 stmt = begin_while_stmt ();
17242 tmp = RECUR (WHILE_COND (t));
17243 finish_while_stmt_cond (tmp, stmt, false, 0);
17244 {
17245 bool prev = note_iteration_stmt_body_start ();
17246 RECUR (WHILE_BODY (t));
17247 note_iteration_stmt_body_end (prev);
17248 }
17249 finish_while_stmt (stmt);
17250 break;
17251
17252 case DO_STMT:
17253 stmt = begin_do_stmt ();
17254 {
17255 bool prev = note_iteration_stmt_body_start ();
17256 RECUR (DO_BODY (t));
17257 note_iteration_stmt_body_end (prev);
17258 }
17259 finish_do_body (stmt);
17260 tmp = RECUR (DO_COND (t));
17261 finish_do_stmt (tmp, stmt, false, 0);
17262 break;
17263
17264 case IF_STMT:
17265 stmt = begin_if_stmt ();
17266 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17267 if (IF_STMT_CONSTEXPR_P (t))
17268 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17269 tmp = RECUR (IF_COND (t));
17270 tmp = finish_if_stmt_cond (tmp, stmt);
17271 if (IF_STMT_CONSTEXPR_P (t)
17272 && instantiation_dependent_expression_p (tmp))
17273 {
17274 /* We're partially instantiating a generic lambda, but the condition
17275 of the constexpr if is still dependent. Don't substitute into the
17276 branches now, just remember the template arguments. */
17277 do_poplevel (IF_SCOPE (stmt));
17278 IF_COND (stmt) = IF_COND (t);
17279 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17280 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17281 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17282 add_stmt (stmt);
17283 break;
17284 }
17285 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17286 /* Don't instantiate the THEN_CLAUSE. */;
17287 else
17288 {
17289 tree folded = fold_non_dependent_expr (tmp, complain);
17290 bool inhibit = integer_zerop (folded);
17291 if (inhibit)
17292 ++c_inhibit_evaluation_warnings;
17293 RECUR (THEN_CLAUSE (t));
17294 if (inhibit)
17295 --c_inhibit_evaluation_warnings;
17296 }
17297 finish_then_clause (stmt);
17298
17299 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17300 /* Don't instantiate the ELSE_CLAUSE. */;
17301 else if (ELSE_CLAUSE (t))
17302 {
17303 tree folded = fold_non_dependent_expr (tmp, complain);
17304 bool inhibit = integer_nonzerop (folded);
17305 begin_else_clause (stmt);
17306 if (inhibit)
17307 ++c_inhibit_evaluation_warnings;
17308 RECUR (ELSE_CLAUSE (t));
17309 if (inhibit)
17310 --c_inhibit_evaluation_warnings;
17311 finish_else_clause (stmt);
17312 }
17313
17314 finish_if_stmt (stmt);
17315 break;
17316
17317 case BIND_EXPR:
17318 if (BIND_EXPR_BODY_BLOCK (t))
17319 stmt = begin_function_body ();
17320 else
17321 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17322 ? BCS_TRY_BLOCK : 0);
17323
17324 RECUR (BIND_EXPR_BODY (t));
17325
17326 if (BIND_EXPR_BODY_BLOCK (t))
17327 finish_function_body (stmt);
17328 else
17329 finish_compound_stmt (stmt);
17330 break;
17331
17332 case BREAK_STMT:
17333 finish_break_stmt ();
17334 break;
17335
17336 case CONTINUE_STMT:
17337 finish_continue_stmt ();
17338 break;
17339
17340 case SWITCH_STMT:
17341 stmt = begin_switch_stmt ();
17342 tmp = RECUR (SWITCH_STMT_COND (t));
17343 finish_switch_cond (tmp, stmt);
17344 RECUR (SWITCH_STMT_BODY (t));
17345 finish_switch_stmt (stmt);
17346 break;
17347
17348 case CASE_LABEL_EXPR:
17349 {
17350 tree decl = CASE_LABEL (t);
17351 tree low = RECUR (CASE_LOW (t));
17352 tree high = RECUR (CASE_HIGH (t));
17353 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17354 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17355 {
17356 tree label = CASE_LABEL (l);
17357 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17358 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17359 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17360 }
17361 }
17362 break;
17363
17364 case LABEL_EXPR:
17365 {
17366 tree decl = LABEL_EXPR_LABEL (t);
17367 tree label;
17368
17369 label = finish_label_stmt (DECL_NAME (decl));
17370 if (TREE_CODE (label) == LABEL_DECL)
17371 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17372 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17373 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17374 }
17375 break;
17376
17377 case GOTO_EXPR:
17378 tmp = GOTO_DESTINATION (t);
17379 if (TREE_CODE (tmp) != LABEL_DECL)
17380 /* Computed goto's must be tsubst'd into. On the other hand,
17381 non-computed gotos must not be; the identifier in question
17382 will have no binding. */
17383 tmp = RECUR (tmp);
17384 else
17385 tmp = DECL_NAME (tmp);
17386 finish_goto_stmt (tmp);
17387 break;
17388
17389 case ASM_EXPR:
17390 {
17391 tree string = RECUR (ASM_STRING (t));
17392 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17393 complain, in_decl);
17394 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17395 complain, in_decl);
17396 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17397 complain, in_decl);
17398 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17399 complain, in_decl);
17400 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
17401 clobbers, labels, ASM_INLINE_P (t));
17402 tree asm_expr = tmp;
17403 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17404 asm_expr = TREE_OPERAND (asm_expr, 0);
17405 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17406 }
17407 break;
17408
17409 case TRY_BLOCK:
17410 if (CLEANUP_P (t))
17411 {
17412 stmt = begin_try_block ();
17413 RECUR (TRY_STMTS (t));
17414 finish_cleanup_try_block (stmt);
17415 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17416 }
17417 else
17418 {
17419 tree compound_stmt = NULL_TREE;
17420
17421 if (FN_TRY_BLOCK_P (t))
17422 stmt = begin_function_try_block (&compound_stmt);
17423 else
17424 stmt = begin_try_block ();
17425
17426 RECUR (TRY_STMTS (t));
17427
17428 if (FN_TRY_BLOCK_P (t))
17429 finish_function_try_block (stmt);
17430 else
17431 finish_try_block (stmt);
17432
17433 RECUR (TRY_HANDLERS (t));
17434 if (FN_TRY_BLOCK_P (t))
17435 finish_function_handler_sequence (stmt, compound_stmt);
17436 else
17437 finish_handler_sequence (stmt);
17438 }
17439 break;
17440
17441 case HANDLER:
17442 {
17443 tree decl = HANDLER_PARMS (t);
17444
17445 if (decl)
17446 {
17447 decl = tsubst (decl, args, complain, in_decl);
17448 /* Prevent instantiate_decl from trying to instantiate
17449 this variable. We've already done all that needs to be
17450 done. */
17451 if (decl != error_mark_node)
17452 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17453 }
17454 stmt = begin_handler ();
17455 finish_handler_parms (decl, stmt);
17456 RECUR (HANDLER_BODY (t));
17457 finish_handler (stmt);
17458 }
17459 break;
17460
17461 case TAG_DEFN:
17462 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17463 if (CLASS_TYPE_P (tmp))
17464 {
17465 /* Local classes are not independent templates; they are
17466 instantiated along with their containing function. And this
17467 way we don't have to deal with pushing out of one local class
17468 to instantiate a member of another local class. */
17469 /* Closures are handled by the LAMBDA_EXPR. */
17470 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17471 complete_type (tmp);
17472 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17473 if ((VAR_P (fld)
17474 || (TREE_CODE (fld) == FUNCTION_DECL
17475 && !DECL_ARTIFICIAL (fld)))
17476 && DECL_TEMPLATE_INSTANTIATION (fld))
17477 instantiate_decl (fld, /*defer_ok=*/false,
17478 /*expl_inst_class=*/false);
17479 }
17480 break;
17481
17482 case STATIC_ASSERT:
17483 {
17484 tree condition;
17485
17486 ++c_inhibit_evaluation_warnings;
17487 condition =
17488 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17489 args,
17490 complain, in_decl,
17491 /*integral_constant_expression_p=*/true);
17492 --c_inhibit_evaluation_warnings;
17493
17494 finish_static_assert (condition,
17495 STATIC_ASSERT_MESSAGE (t),
17496 STATIC_ASSERT_SOURCE_LOCATION (t),
17497 /*member_p=*/false);
17498 }
17499 break;
17500
17501 case OACC_KERNELS:
17502 case OACC_PARALLEL:
17503 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17504 in_decl);
17505 stmt = begin_omp_parallel ();
17506 RECUR (OMP_BODY (t));
17507 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17508 break;
17509
17510 case OMP_PARALLEL:
17511 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17512 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17513 complain, in_decl);
17514 if (OMP_PARALLEL_COMBINED (t))
17515 omp_parallel_combined_clauses = &tmp;
17516 stmt = begin_omp_parallel ();
17517 RECUR (OMP_PARALLEL_BODY (t));
17518 gcc_assert (omp_parallel_combined_clauses == NULL);
17519 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17520 = OMP_PARALLEL_COMBINED (t);
17521 pop_omp_privatization_clauses (r);
17522 break;
17523
17524 case OMP_TASK:
17525 if (OMP_TASK_BODY (t) == NULL_TREE)
17526 {
17527 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17528 complain, in_decl);
17529 t = copy_node (t);
17530 OMP_TASK_CLAUSES (t) = tmp;
17531 add_stmt (t);
17532 break;
17533 }
17534 r = push_omp_privatization_clauses (false);
17535 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17536 complain, in_decl);
17537 stmt = begin_omp_task ();
17538 RECUR (OMP_TASK_BODY (t));
17539 finish_omp_task (tmp, stmt);
17540 pop_omp_privatization_clauses (r);
17541 break;
17542
17543 case OMP_FOR:
17544 case OMP_SIMD:
17545 case OMP_DISTRIBUTE:
17546 case OMP_TASKLOOP:
17547 case OACC_LOOP:
17548 {
17549 tree clauses, body, pre_body;
17550 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17551 tree orig_declv = NULL_TREE;
17552 tree incrv = NULL_TREE;
17553 enum c_omp_region_type ort = C_ORT_OMP;
17554 bool any_range_for = false;
17555 int i;
17556
17557 if (TREE_CODE (t) == OACC_LOOP)
17558 ort = C_ORT_ACC;
17559
17560 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17561 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17562 in_decl);
17563 if (OMP_FOR_INIT (t) != NULL_TREE)
17564 {
17565 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17566 if (OMP_FOR_ORIG_DECLS (t))
17567 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17568 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17569 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17570 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17571 }
17572
17573 keep_next_level (true);
17574 stmt = begin_omp_structured_block ();
17575
17576 pre_body = push_stmt_list ();
17577 RECUR (OMP_FOR_PRE_BODY (t));
17578 pre_body = pop_stmt_list (pre_body);
17579
17580 if (OMP_FOR_INIT (t) != NULL_TREE)
17581 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17582 any_range_for
17583 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
17584 condv, incrv, &clauses, args,
17585 complain, in_decl,
17586 integral_constant_expression_p);
17587 omp_parallel_combined_clauses = NULL;
17588
17589 if (any_range_for)
17590 {
17591 gcc_assert (orig_declv);
17592 body = begin_omp_structured_block ();
17593 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17594 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
17595 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
17596 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
17597 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
17598 TREE_VEC_ELT (declv, i));
17599 }
17600 else
17601 body = push_stmt_list ();
17602 RECUR (OMP_FOR_BODY (t));
17603 if (any_range_for)
17604 body = finish_omp_structured_block (body);
17605 else
17606 body = pop_stmt_list (body);
17607
17608 if (OMP_FOR_INIT (t) != NULL_TREE)
17609 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17610 orig_declv, initv, condv, incrv, body, pre_body,
17611 NULL, clauses);
17612 else
17613 {
17614 t = make_node (TREE_CODE (t));
17615 TREE_TYPE (t) = void_type_node;
17616 OMP_FOR_BODY (t) = body;
17617 OMP_FOR_PRE_BODY (t) = pre_body;
17618 OMP_FOR_CLAUSES (t) = clauses;
17619 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17620 add_stmt (t);
17621 }
17622
17623 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
17624 t));
17625 pop_omp_privatization_clauses (r);
17626 }
17627 break;
17628
17629 case OMP_SECTIONS:
17630 omp_parallel_combined_clauses = NULL;
17631 /* FALLTHRU */
17632 case OMP_SINGLE:
17633 case OMP_TEAMS:
17634 case OMP_CRITICAL:
17635 case OMP_TASKGROUP:
17636 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17637 && OMP_TEAMS_COMBINED (t));
17638 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17639 in_decl);
17640 if (TREE_CODE (t) == OMP_TEAMS)
17641 {
17642 keep_next_level (true);
17643 stmt = begin_omp_structured_block ();
17644 RECUR (OMP_BODY (t));
17645 stmt = finish_omp_structured_block (stmt);
17646 }
17647 else
17648 {
17649 stmt = push_stmt_list ();
17650 RECUR (OMP_BODY (t));
17651 stmt = pop_stmt_list (stmt);
17652 }
17653
17654 t = copy_node (t);
17655 OMP_BODY (t) = stmt;
17656 OMP_CLAUSES (t) = tmp;
17657 add_stmt (t);
17658 pop_omp_privatization_clauses (r);
17659 break;
17660
17661 case OMP_DEPOBJ:
17662 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
17663 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
17664 {
17665 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17666 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
17667 {
17668 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
17669 args, complain, in_decl);
17670 if (tmp == NULL_TREE)
17671 tmp = error_mark_node;
17672 }
17673 else
17674 {
17675 kind = (enum omp_clause_depend_kind)
17676 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
17677 tmp = NULL_TREE;
17678 }
17679 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
17680 }
17681 else
17682 finish_omp_depobj (EXPR_LOCATION (t), r,
17683 OMP_CLAUSE_DEPEND_SOURCE,
17684 OMP_DEPOBJ_CLAUSES (t));
17685 break;
17686
17687 case OACC_DATA:
17688 case OMP_TARGET_DATA:
17689 case OMP_TARGET:
17690 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17691 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17692 in_decl);
17693 keep_next_level (true);
17694 stmt = begin_omp_structured_block ();
17695
17696 RECUR (OMP_BODY (t));
17697 stmt = finish_omp_structured_block (stmt);
17698
17699 t = copy_node (t);
17700 OMP_BODY (t) = stmt;
17701 OMP_CLAUSES (t) = tmp;
17702 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17703 {
17704 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17705 if (teams)
17706 {
17707 /* For combined target teams, ensure the num_teams and
17708 thread_limit clause expressions are evaluated on the host,
17709 before entering the target construct. */
17710 tree c;
17711 for (c = OMP_TEAMS_CLAUSES (teams);
17712 c; c = OMP_CLAUSE_CHAIN (c))
17713 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17714 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17715 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17716 {
17717 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17718 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17719 if (expr == error_mark_node)
17720 continue;
17721 tmp = TARGET_EXPR_SLOT (expr);
17722 add_stmt (expr);
17723 OMP_CLAUSE_OPERAND (c, 0) = expr;
17724 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17725 OMP_CLAUSE_FIRSTPRIVATE);
17726 OMP_CLAUSE_DECL (tc) = tmp;
17727 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17728 OMP_TARGET_CLAUSES (t) = tc;
17729 }
17730 }
17731 }
17732 add_stmt (t);
17733 break;
17734
17735 case OACC_DECLARE:
17736 t = copy_node (t);
17737 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17738 complain, in_decl);
17739 OACC_DECLARE_CLAUSES (t) = tmp;
17740 add_stmt (t);
17741 break;
17742
17743 case OMP_TARGET_UPDATE:
17744 case OMP_TARGET_ENTER_DATA:
17745 case OMP_TARGET_EXIT_DATA:
17746 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17747 complain, in_decl);
17748 t = copy_node (t);
17749 OMP_STANDALONE_CLAUSES (t) = tmp;
17750 add_stmt (t);
17751 break;
17752
17753 case OACC_ENTER_DATA:
17754 case OACC_EXIT_DATA:
17755 case OACC_UPDATE:
17756 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17757 complain, in_decl);
17758 t = copy_node (t);
17759 OMP_STANDALONE_CLAUSES (t) = tmp;
17760 add_stmt (t);
17761 break;
17762
17763 case OMP_ORDERED:
17764 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17765 complain, in_decl);
17766 stmt = push_stmt_list ();
17767 RECUR (OMP_BODY (t));
17768 stmt = pop_stmt_list (stmt);
17769
17770 t = copy_node (t);
17771 OMP_BODY (t) = stmt;
17772 OMP_ORDERED_CLAUSES (t) = tmp;
17773 add_stmt (t);
17774 break;
17775
17776 case OMP_SECTION:
17777 case OMP_MASTER:
17778 stmt = push_stmt_list ();
17779 RECUR (OMP_BODY (t));
17780 stmt = pop_stmt_list (stmt);
17781
17782 t = copy_node (t);
17783 OMP_BODY (t) = stmt;
17784 add_stmt (t);
17785 break;
17786
17787 case OMP_ATOMIC:
17788 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17789 tmp = NULL_TREE;
17790 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
17791 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
17792 complain, in_decl);
17793 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17794 {
17795 tree op1 = TREE_OPERAND (t, 1);
17796 tree rhs1 = NULL_TREE;
17797 tree lhs, rhs;
17798 if (TREE_CODE (op1) == COMPOUND_EXPR)
17799 {
17800 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17801 op1 = TREE_OPERAND (op1, 1);
17802 }
17803 lhs = RECUR (TREE_OPERAND (op1, 0));
17804 rhs = RECUR (TREE_OPERAND (op1, 1));
17805 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
17806 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
17807 OMP_ATOMIC_MEMORY_ORDER (t));
17808 }
17809 else
17810 {
17811 tree op1 = TREE_OPERAND (t, 1);
17812 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17813 tree rhs1 = NULL_TREE;
17814 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17815 enum tree_code opcode = NOP_EXPR;
17816 if (code == OMP_ATOMIC_READ)
17817 {
17818 v = RECUR (TREE_OPERAND (op1, 0));
17819 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17820 }
17821 else if (code == OMP_ATOMIC_CAPTURE_OLD
17822 || code == OMP_ATOMIC_CAPTURE_NEW)
17823 {
17824 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17825 v = RECUR (TREE_OPERAND (op1, 0));
17826 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17827 if (TREE_CODE (op11) == COMPOUND_EXPR)
17828 {
17829 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17830 op11 = TREE_OPERAND (op11, 1);
17831 }
17832 lhs = RECUR (TREE_OPERAND (op11, 0));
17833 rhs = RECUR (TREE_OPERAND (op11, 1));
17834 opcode = TREE_CODE (op11);
17835 if (opcode == MODIFY_EXPR)
17836 opcode = NOP_EXPR;
17837 }
17838 else
17839 {
17840 code = OMP_ATOMIC;
17841 lhs = RECUR (TREE_OPERAND (op1, 0));
17842 rhs = RECUR (TREE_OPERAND (op1, 1));
17843 }
17844 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
17845 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
17846 }
17847 break;
17848
17849 case TRANSACTION_EXPR:
17850 {
17851 int flags = 0;
17852 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17853 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17854
17855 if (TRANSACTION_EXPR_IS_STMT (t))
17856 {
17857 tree body = TRANSACTION_EXPR_BODY (t);
17858 tree noex = NULL_TREE;
17859 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17860 {
17861 noex = MUST_NOT_THROW_COND (body);
17862 if (noex == NULL_TREE)
17863 noex = boolean_true_node;
17864 body = TREE_OPERAND (body, 0);
17865 }
17866 stmt = begin_transaction_stmt (input_location, NULL, flags);
17867 RECUR (body);
17868 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17869 }
17870 else
17871 {
17872 stmt = build_transaction_expr (EXPR_LOCATION (t),
17873 RECUR (TRANSACTION_EXPR_BODY (t)),
17874 flags, NULL_TREE);
17875 RETURN (stmt);
17876 }
17877 }
17878 break;
17879
17880 case MUST_NOT_THROW_EXPR:
17881 {
17882 tree op0 = RECUR (TREE_OPERAND (t, 0));
17883 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17884 RETURN (build_must_not_throw_expr (op0, cond));
17885 }
17886
17887 case EXPR_PACK_EXPANSION:
17888 error ("invalid use of pack expansion expression");
17889 RETURN (error_mark_node);
17890
17891 case NONTYPE_ARGUMENT_PACK:
17892 error ("use %<...%> to expand argument pack");
17893 RETURN (error_mark_node);
17894
17895 case COMPOUND_EXPR:
17896 tmp = RECUR (TREE_OPERAND (t, 0));
17897 if (tmp == NULL_TREE)
17898 /* If the first operand was a statement, we're done with it. */
17899 RETURN (RECUR (TREE_OPERAND (t, 1)));
17900 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17901 RECUR (TREE_OPERAND (t, 1)),
17902 complain));
17903
17904 case ANNOTATE_EXPR:
17905 tmp = RECUR (TREE_OPERAND (t, 0));
17906 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17907 TREE_TYPE (tmp), tmp,
17908 RECUR (TREE_OPERAND (t, 1)),
17909 RECUR (TREE_OPERAND (t, 2))));
17910
17911 case PREDICT_EXPR:
17912 RETURN (add_stmt (copy_node (t)));
17913
17914 default:
17915 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17916
17917 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17918 /*function_p=*/false,
17919 integral_constant_expression_p));
17920 }
17921
17922 RETURN (NULL_TREE);
17923 out:
17924 input_location = loc;
17925 return r;
17926 #undef RECUR
17927 #undef RETURN
17928 }
17929
17930 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17931 function. For description of the body see comment above
17932 cp_parser_omp_declare_reduction_exprs. */
17933
17934 static void
17935 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17936 {
17937 if (t == NULL_TREE || t == error_mark_node)
17938 return;
17939
17940 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17941
17942 tree_stmt_iterator tsi;
17943 int i;
17944 tree stmts[7];
17945 memset (stmts, 0, sizeof stmts);
17946 for (i = 0, tsi = tsi_start (t);
17947 i < 7 && !tsi_end_p (tsi);
17948 i++, tsi_next (&tsi))
17949 stmts[i] = tsi_stmt (tsi);
17950 gcc_assert (tsi_end_p (tsi));
17951
17952 if (i >= 3)
17953 {
17954 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17955 && TREE_CODE (stmts[1]) == DECL_EXPR);
17956 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17957 args, complain, in_decl);
17958 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17959 args, complain, in_decl);
17960 DECL_CONTEXT (omp_out) = current_function_decl;
17961 DECL_CONTEXT (omp_in) = current_function_decl;
17962 keep_next_level (true);
17963 tree block = begin_omp_structured_block ();
17964 tsubst_expr (stmts[2], args, complain, in_decl, false);
17965 block = finish_omp_structured_block (block);
17966 block = maybe_cleanup_point_expr_void (block);
17967 add_decl_expr (omp_out);
17968 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17969 TREE_NO_WARNING (omp_out) = 1;
17970 add_decl_expr (omp_in);
17971 finish_expr_stmt (block);
17972 }
17973 if (i >= 6)
17974 {
17975 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17976 && TREE_CODE (stmts[4]) == DECL_EXPR);
17977 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17978 args, complain, in_decl);
17979 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17980 args, complain, in_decl);
17981 DECL_CONTEXT (omp_priv) = current_function_decl;
17982 DECL_CONTEXT (omp_orig) = current_function_decl;
17983 keep_next_level (true);
17984 tree block = begin_omp_structured_block ();
17985 tsubst_expr (stmts[5], args, complain, in_decl, false);
17986 block = finish_omp_structured_block (block);
17987 block = maybe_cleanup_point_expr_void (block);
17988 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17989 add_decl_expr (omp_priv);
17990 add_decl_expr (omp_orig);
17991 finish_expr_stmt (block);
17992 if (i == 7)
17993 add_decl_expr (omp_orig);
17994 }
17995 }
17996
17997 /* T is a postfix-expression that is not being used in a function
17998 call. Return the substituted version of T. */
17999
18000 static tree
18001 tsubst_non_call_postfix_expression (tree t, tree args,
18002 tsubst_flags_t complain,
18003 tree in_decl)
18004 {
18005 if (TREE_CODE (t) == SCOPE_REF)
18006 t = tsubst_qualified_id (t, args, complain, in_decl,
18007 /*done=*/false, /*address_p=*/false);
18008 else
18009 t = tsubst_copy_and_build (t, args, complain, in_decl,
18010 /*function_p=*/false,
18011 /*integral_constant_expression_p=*/false);
18012
18013 return t;
18014 }
18015
18016 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18017 instantiation context. Instantiating a pack expansion containing a lambda
18018 might result in multiple lambdas all based on the same lambda in the
18019 template. */
18020
18021 tree
18022 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18023 {
18024 tree oldfn = lambda_function (t);
18025 in_decl = oldfn;
18026
18027 /* If we have already specialized this lambda expr, reuse it. See
18028 PR c++/87322. */
18029 if (local_specializations)
18030 if (tree r = retrieve_local_specialization (t))
18031 return r;
18032
18033 tree r = build_lambda_expr ();
18034
18035 if (local_specializations)
18036 register_local_specialization (r, t);
18037
18038 LAMBDA_EXPR_LOCATION (r)
18039 = LAMBDA_EXPR_LOCATION (t);
18040 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18041 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18042 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18043 LAMBDA_EXPR_INSTANTIATED (r) = true;
18044
18045 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18046 /* A lambda in a default argument outside a class gets no
18047 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18048 tsubst_default_argument calls start_lambda_scope, so we need to
18049 specifically ignore it here, and use the global scope. */
18050 record_null_lambda_scope (r);
18051 else
18052 record_lambda_scope (r);
18053
18054 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18055 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18056
18057 vec<tree,va_gc>* field_packs = NULL;
18058
18059 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18060 cap = TREE_CHAIN (cap))
18061 {
18062 tree ofield = TREE_PURPOSE (cap);
18063 if (PACK_EXPANSION_P (ofield))
18064 ofield = PACK_EXPANSION_PATTERN (ofield);
18065 tree field = tsubst_decl (ofield, args, complain);
18066
18067 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18068 {
18069 /* Remember these for when we've pushed local_specializations. */
18070 vec_safe_push (field_packs, ofield);
18071 vec_safe_push (field_packs, field);
18072 }
18073
18074 if (field == error_mark_node)
18075 return error_mark_node;
18076
18077 tree init = TREE_VALUE (cap);
18078 if (PACK_EXPANSION_P (init))
18079 init = tsubst_pack_expansion (init, args, complain, in_decl);
18080 else
18081 init = tsubst_copy_and_build (init, args, complain, in_decl,
18082 /*fn*/false, /*constexpr*/false);
18083
18084 if (TREE_CODE (field) == TREE_VEC)
18085 {
18086 int len = TREE_VEC_LENGTH (field);
18087 gcc_assert (TREE_CODE (init) == TREE_VEC
18088 && TREE_VEC_LENGTH (init) == len);
18089 for (int i = 0; i < len; ++i)
18090 LAMBDA_EXPR_CAPTURE_LIST (r)
18091 = tree_cons (TREE_VEC_ELT (field, i),
18092 TREE_VEC_ELT (init, i),
18093 LAMBDA_EXPR_CAPTURE_LIST (r));
18094 }
18095 else
18096 {
18097 LAMBDA_EXPR_CAPTURE_LIST (r)
18098 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
18099
18100 if (id_equal (DECL_NAME (field), "__this"))
18101 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18102 }
18103 }
18104
18105 tree type = begin_lambda_type (r);
18106 if (type == error_mark_node)
18107 return error_mark_node;
18108
18109 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18110 determine_visibility (TYPE_NAME (type));
18111
18112 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18113
18114 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18115 ? DECL_TI_TEMPLATE (oldfn)
18116 : NULL_TREE);
18117
18118 tree fntype = static_fn_type (oldfn);
18119 if (oldtmpl)
18120 ++processing_template_decl;
18121 fntype = tsubst (fntype, args, complain, in_decl);
18122 if (oldtmpl)
18123 --processing_template_decl;
18124
18125 if (fntype == error_mark_node)
18126 r = error_mark_node;
18127 else
18128 {
18129 /* The body of a lambda-expression is not a subexpression of the
18130 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18131 which would be skipped if cp_unevaluated_operand. */
18132 cp_evaluated ev;
18133
18134 /* Fix the type of 'this'. */
18135 fntype = build_memfn_type (fntype, type,
18136 type_memfn_quals (fntype),
18137 type_memfn_rqual (fntype));
18138 tree fn, tmpl;
18139 if (oldtmpl)
18140 {
18141 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18142 fn = DECL_TEMPLATE_RESULT (tmpl);
18143 finish_member_declaration (tmpl);
18144 }
18145 else
18146 {
18147 tmpl = NULL_TREE;
18148 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18149 finish_member_declaration (fn);
18150 }
18151
18152 /* Let finish_function set this. */
18153 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18154
18155 bool nested = cfun;
18156 if (nested)
18157 push_function_context ();
18158 else
18159 /* Still increment function_depth so that we don't GC in the
18160 middle of an expression. */
18161 ++function_depth;
18162
18163 local_specialization_stack s (lss_copy);
18164
18165 tree body = start_lambda_function (fn, r);
18166
18167 /* Now record them for lookup_init_capture_pack. */
18168 int fplen = vec_safe_length (field_packs);
18169 for (int i = 0; i < fplen; )
18170 {
18171 tree pack = (*field_packs)[i++];
18172 tree inst = (*field_packs)[i++];
18173 register_local_specialization (inst, pack);
18174 }
18175 release_tree_vector (field_packs);
18176
18177 register_parameter_specializations (oldfn, fn);
18178
18179 if (oldtmpl)
18180 {
18181 /* We might not partially instantiate some parts of the function, so
18182 copy these flags from the original template. */
18183 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18184 current_function_returns_value = ol->returns_value;
18185 current_function_returns_null = ol->returns_null;
18186 current_function_returns_abnormally = ol->returns_abnormally;
18187 current_function_infinite_loop = ol->infinite_loop;
18188 }
18189
18190 /* [temp.deduct] A lambda-expression appearing in a function type or a
18191 template parameter is not considered part of the immediate context for
18192 the purposes of template argument deduction. */
18193 complain = tf_warning_or_error;
18194
18195 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18196 /*constexpr*/false);
18197
18198 finish_lambda_function (body);
18199
18200 if (nested)
18201 pop_function_context ();
18202 else
18203 --function_depth;
18204
18205 /* The capture list was built up in reverse order; fix that now. */
18206 LAMBDA_EXPR_CAPTURE_LIST (r)
18207 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18208
18209 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18210
18211 maybe_add_lambda_conv_op (type);
18212 }
18213
18214 finish_struct (type, /*attr*/NULL_TREE);
18215
18216 insert_pending_capture_proxies ();
18217
18218 return r;
18219 }
18220
18221 /* Like tsubst but deals with expressions and performs semantic
18222 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18223
18224 tree
18225 tsubst_copy_and_build (tree t,
18226 tree args,
18227 tsubst_flags_t complain,
18228 tree in_decl,
18229 bool function_p,
18230 bool integral_constant_expression_p)
18231 {
18232 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18233 #define RECUR(NODE) \
18234 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18235 /*function_p=*/false, \
18236 integral_constant_expression_p)
18237
18238 tree retval, op1;
18239 location_t loc;
18240
18241 if (t == NULL_TREE || t == error_mark_node)
18242 return t;
18243
18244 loc = input_location;
18245 if (location_t eloc = cp_expr_location (t))
18246 input_location = eloc;
18247
18248 /* N3276 decltype magic only applies to calls at the top level or on the
18249 right side of a comma. */
18250 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18251 complain &= ~tf_decltype;
18252
18253 switch (TREE_CODE (t))
18254 {
18255 case USING_DECL:
18256 t = DECL_NAME (t);
18257 /* Fall through. */
18258 case IDENTIFIER_NODE:
18259 {
18260 tree decl;
18261 cp_id_kind idk;
18262 bool non_integral_constant_expression_p;
18263 const char *error_msg;
18264
18265 if (IDENTIFIER_CONV_OP_P (t))
18266 {
18267 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18268 t = make_conv_op_name (new_type);
18269 }
18270
18271 /* Look up the name. */
18272 decl = lookup_name (t);
18273
18274 /* By convention, expressions use ERROR_MARK_NODE to indicate
18275 failure, not NULL_TREE. */
18276 if (decl == NULL_TREE)
18277 decl = error_mark_node;
18278
18279 decl = finish_id_expression (t, decl, NULL_TREE,
18280 &idk,
18281 integral_constant_expression_p,
18282 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18283 &non_integral_constant_expression_p,
18284 /*template_p=*/false,
18285 /*done=*/true,
18286 /*address_p=*/false,
18287 /*template_arg_p=*/false,
18288 &error_msg,
18289 input_location);
18290 if (error_msg)
18291 error (error_msg);
18292 if (!function_p && identifier_p (decl))
18293 {
18294 if (complain & tf_error)
18295 unqualified_name_lookup_error (decl);
18296 decl = error_mark_node;
18297 }
18298 RETURN (decl);
18299 }
18300
18301 case TEMPLATE_ID_EXPR:
18302 {
18303 tree object;
18304 tree templ = RECUR (TREE_OPERAND (t, 0));
18305 tree targs = TREE_OPERAND (t, 1);
18306
18307 if (targs)
18308 targs = tsubst_template_args (targs, args, complain, in_decl);
18309 if (targs == error_mark_node)
18310 RETURN (error_mark_node);
18311
18312 if (TREE_CODE (templ) == SCOPE_REF)
18313 {
18314 tree name = TREE_OPERAND (templ, 1);
18315 tree tid = lookup_template_function (name, targs);
18316 TREE_OPERAND (templ, 1) = tid;
18317 RETURN (templ);
18318 }
18319
18320 if (variable_template_p (templ))
18321 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18322
18323 if (TREE_CODE (templ) == COMPONENT_REF)
18324 {
18325 object = TREE_OPERAND (templ, 0);
18326 templ = TREE_OPERAND (templ, 1);
18327 }
18328 else
18329 object = NULL_TREE;
18330 templ = lookup_template_function (templ, targs);
18331
18332 if (object)
18333 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18334 object, templ, NULL_TREE));
18335 else
18336 RETURN (baselink_for_fns (templ));
18337 }
18338
18339 case INDIRECT_REF:
18340 {
18341 tree r = RECUR (TREE_OPERAND (t, 0));
18342
18343 if (REFERENCE_REF_P (t))
18344 {
18345 /* A type conversion to reference type will be enclosed in
18346 such an indirect ref, but the substitution of the cast
18347 will have also added such an indirect ref. */
18348 r = convert_from_reference (r);
18349 }
18350 else
18351 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18352 complain|decltype_flag);
18353
18354 if (REF_PARENTHESIZED_P (t))
18355 r = force_paren_expr (r);
18356
18357 RETURN (r);
18358 }
18359
18360 case NOP_EXPR:
18361 {
18362 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18363 tree op0 = RECUR (TREE_OPERAND (t, 0));
18364 RETURN (build_nop (type, op0));
18365 }
18366
18367 case IMPLICIT_CONV_EXPR:
18368 {
18369 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18370 tree expr = RECUR (TREE_OPERAND (t, 0));
18371 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18372 {
18373 retval = copy_node (t);
18374 TREE_TYPE (retval) = type;
18375 TREE_OPERAND (retval, 0) = expr;
18376 RETURN (retval);
18377 }
18378 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18379 /* We'll pass this to convert_nontype_argument again, we don't need
18380 to actually perform any conversion here. */
18381 RETURN (expr);
18382 int flags = LOOKUP_IMPLICIT;
18383 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18384 flags = LOOKUP_NORMAL;
18385 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18386 flags |= LOOKUP_NO_NARROWING;
18387 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18388 flags));
18389 }
18390
18391 case CONVERT_EXPR:
18392 {
18393 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18394 tree op0 = RECUR (TREE_OPERAND (t, 0));
18395 if (op0 == error_mark_node)
18396 RETURN (error_mark_node);
18397 RETURN (build1 (CONVERT_EXPR, type, op0));
18398 }
18399
18400 case CAST_EXPR:
18401 case REINTERPRET_CAST_EXPR:
18402 case CONST_CAST_EXPR:
18403 case DYNAMIC_CAST_EXPR:
18404 case STATIC_CAST_EXPR:
18405 {
18406 tree type;
18407 tree op, r = NULL_TREE;
18408
18409 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18410 if (integral_constant_expression_p
18411 && !cast_valid_in_integral_constant_expression_p (type))
18412 {
18413 if (complain & tf_error)
18414 error ("a cast to a type other than an integral or "
18415 "enumeration type cannot appear in a constant-expression");
18416 RETURN (error_mark_node);
18417 }
18418
18419 op = RECUR (TREE_OPERAND (t, 0));
18420
18421 warning_sentinel s(warn_useless_cast);
18422 warning_sentinel s2(warn_ignored_qualifiers);
18423 switch (TREE_CODE (t))
18424 {
18425 case CAST_EXPR:
18426 r = build_functional_cast (type, op, complain);
18427 break;
18428 case REINTERPRET_CAST_EXPR:
18429 r = build_reinterpret_cast (type, op, complain);
18430 break;
18431 case CONST_CAST_EXPR:
18432 r = build_const_cast (type, op, complain);
18433 break;
18434 case DYNAMIC_CAST_EXPR:
18435 r = build_dynamic_cast (type, op, complain);
18436 break;
18437 case STATIC_CAST_EXPR:
18438 r = build_static_cast (type, op, complain);
18439 break;
18440 default:
18441 gcc_unreachable ();
18442 }
18443
18444 RETURN (r);
18445 }
18446
18447 case POSTDECREMENT_EXPR:
18448 case POSTINCREMENT_EXPR:
18449 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18450 args, complain, in_decl);
18451 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18452 complain|decltype_flag));
18453
18454 case PREDECREMENT_EXPR:
18455 case PREINCREMENT_EXPR:
18456 case NEGATE_EXPR:
18457 case BIT_NOT_EXPR:
18458 case ABS_EXPR:
18459 case TRUTH_NOT_EXPR:
18460 case UNARY_PLUS_EXPR: /* Unary + */
18461 case REALPART_EXPR:
18462 case IMAGPART_EXPR:
18463 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18464 RECUR (TREE_OPERAND (t, 0)),
18465 complain|decltype_flag));
18466
18467 case FIX_TRUNC_EXPR:
18468 gcc_unreachable ();
18469
18470 case ADDR_EXPR:
18471 op1 = TREE_OPERAND (t, 0);
18472 if (TREE_CODE (op1) == LABEL_DECL)
18473 RETURN (finish_label_address_expr (DECL_NAME (op1),
18474 EXPR_LOCATION (op1)));
18475 if (TREE_CODE (op1) == SCOPE_REF)
18476 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18477 /*done=*/true, /*address_p=*/true);
18478 else
18479 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18480 in_decl);
18481 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18482 complain|decltype_flag));
18483
18484 case PLUS_EXPR:
18485 case MINUS_EXPR:
18486 case MULT_EXPR:
18487 case TRUNC_DIV_EXPR:
18488 case CEIL_DIV_EXPR:
18489 case FLOOR_DIV_EXPR:
18490 case ROUND_DIV_EXPR:
18491 case EXACT_DIV_EXPR:
18492 case BIT_AND_EXPR:
18493 case BIT_IOR_EXPR:
18494 case BIT_XOR_EXPR:
18495 case TRUNC_MOD_EXPR:
18496 case FLOOR_MOD_EXPR:
18497 case TRUTH_ANDIF_EXPR:
18498 case TRUTH_ORIF_EXPR:
18499 case TRUTH_AND_EXPR:
18500 case TRUTH_OR_EXPR:
18501 case RSHIFT_EXPR:
18502 case LSHIFT_EXPR:
18503 case RROTATE_EXPR:
18504 case LROTATE_EXPR:
18505 case EQ_EXPR:
18506 case NE_EXPR:
18507 case MAX_EXPR:
18508 case MIN_EXPR:
18509 case LE_EXPR:
18510 case GE_EXPR:
18511 case LT_EXPR:
18512 case GT_EXPR:
18513 case MEMBER_REF:
18514 case DOTSTAR_EXPR:
18515 {
18516 warning_sentinel s1(warn_type_limits);
18517 warning_sentinel s2(warn_div_by_zero);
18518 warning_sentinel s3(warn_logical_op);
18519 warning_sentinel s4(warn_tautological_compare);
18520 tree op0 = RECUR (TREE_OPERAND (t, 0));
18521 tree op1 = RECUR (TREE_OPERAND (t, 1));
18522 tree r = build_x_binary_op
18523 (input_location, TREE_CODE (t),
18524 op0,
18525 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18526 ? ERROR_MARK
18527 : TREE_CODE (TREE_OPERAND (t, 0))),
18528 op1,
18529 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18530 ? ERROR_MARK
18531 : TREE_CODE (TREE_OPERAND (t, 1))),
18532 /*overload=*/NULL,
18533 complain|decltype_flag);
18534 if (EXPR_P (r) && TREE_NO_WARNING (t))
18535 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18536
18537 RETURN (r);
18538 }
18539
18540 case POINTER_PLUS_EXPR:
18541 {
18542 tree op0 = RECUR (TREE_OPERAND (t, 0));
18543 tree op1 = RECUR (TREE_OPERAND (t, 1));
18544 RETURN (fold_build_pointer_plus (op0, op1));
18545 }
18546
18547 case SCOPE_REF:
18548 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18549 /*address_p=*/false));
18550 case ARRAY_REF:
18551 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18552 args, complain, in_decl);
18553 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18554 RECUR (TREE_OPERAND (t, 1)),
18555 complain|decltype_flag));
18556
18557 case SIZEOF_EXPR:
18558 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18559 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18560 RETURN (tsubst_copy (t, args, complain, in_decl));
18561 /* Fall through */
18562
18563 case ALIGNOF_EXPR:
18564 {
18565 tree r;
18566
18567 op1 = TREE_OPERAND (t, 0);
18568 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18569 op1 = TREE_TYPE (op1);
18570 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18571 && ALIGNOF_EXPR_STD_P (t));
18572 if (!args)
18573 {
18574 /* When there are no ARGS, we are trying to evaluate a
18575 non-dependent expression from the parser. Trying to do
18576 the substitutions may not work. */
18577 if (!TYPE_P (op1))
18578 op1 = TREE_TYPE (op1);
18579 }
18580 else
18581 {
18582 ++cp_unevaluated_operand;
18583 ++c_inhibit_evaluation_warnings;
18584 if (TYPE_P (op1))
18585 op1 = tsubst (op1, args, complain, in_decl);
18586 else
18587 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18588 /*function_p=*/false,
18589 /*integral_constant_expression_p=*/
18590 false);
18591 --cp_unevaluated_operand;
18592 --c_inhibit_evaluation_warnings;
18593 }
18594 if (TYPE_P (op1))
18595 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18596 complain & tf_error);
18597 else
18598 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18599 complain & tf_error);
18600 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18601 {
18602 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18603 {
18604 if (!processing_template_decl && TYPE_P (op1))
18605 {
18606 r = build_min (SIZEOF_EXPR, size_type_node,
18607 build1 (NOP_EXPR, op1, error_mark_node));
18608 SIZEOF_EXPR_TYPE_P (r) = 1;
18609 }
18610 else
18611 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18612 TREE_SIDE_EFFECTS (r) = 0;
18613 TREE_READONLY (r) = 1;
18614 }
18615 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18616 }
18617 RETURN (r);
18618 }
18619
18620 case AT_ENCODE_EXPR:
18621 {
18622 op1 = TREE_OPERAND (t, 0);
18623 ++cp_unevaluated_operand;
18624 ++c_inhibit_evaluation_warnings;
18625 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18626 /*function_p=*/false,
18627 /*integral_constant_expression_p=*/false);
18628 --cp_unevaluated_operand;
18629 --c_inhibit_evaluation_warnings;
18630 RETURN (objc_build_encode_expr (op1));
18631 }
18632
18633 case NOEXCEPT_EXPR:
18634 op1 = TREE_OPERAND (t, 0);
18635 ++cp_unevaluated_operand;
18636 ++c_inhibit_evaluation_warnings;
18637 ++cp_noexcept_operand;
18638 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18639 /*function_p=*/false,
18640 /*integral_constant_expression_p=*/false);
18641 --cp_unevaluated_operand;
18642 --c_inhibit_evaluation_warnings;
18643 --cp_noexcept_operand;
18644 RETURN (finish_noexcept_expr (op1, complain));
18645
18646 case MODOP_EXPR:
18647 {
18648 warning_sentinel s(warn_div_by_zero);
18649 tree lhs = RECUR (TREE_OPERAND (t, 0));
18650 tree rhs = RECUR (TREE_OPERAND (t, 2));
18651 tree r = build_x_modify_expr
18652 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18653 complain|decltype_flag);
18654 /* TREE_NO_WARNING must be set if either the expression was
18655 parenthesized or it uses an operator such as >>= rather
18656 than plain assignment. In the former case, it was already
18657 set and must be copied. In the latter case,
18658 build_x_modify_expr sets it and it must not be reset
18659 here. */
18660 if (TREE_NO_WARNING (t))
18661 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18662
18663 RETURN (r);
18664 }
18665
18666 case ARROW_EXPR:
18667 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18668 args, complain, in_decl);
18669 /* Remember that there was a reference to this entity. */
18670 if (DECL_P (op1)
18671 && !mark_used (op1, complain) && !(complain & tf_error))
18672 RETURN (error_mark_node);
18673 RETURN (build_x_arrow (input_location, op1, complain));
18674
18675 case NEW_EXPR:
18676 {
18677 tree placement = RECUR (TREE_OPERAND (t, 0));
18678 tree init = RECUR (TREE_OPERAND (t, 3));
18679 vec<tree, va_gc> *placement_vec;
18680 vec<tree, va_gc> *init_vec;
18681 tree ret;
18682
18683 if (placement == NULL_TREE)
18684 placement_vec = NULL;
18685 else
18686 {
18687 placement_vec = make_tree_vector ();
18688 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18689 vec_safe_push (placement_vec, TREE_VALUE (placement));
18690 }
18691
18692 /* If there was an initializer in the original tree, but it
18693 instantiated to an empty list, then we should pass a
18694 non-NULL empty vector to tell build_new that it was an
18695 empty initializer() rather than no initializer. This can
18696 only happen when the initializer is a pack expansion whose
18697 parameter packs are of length zero. */
18698 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18699 init_vec = NULL;
18700 else
18701 {
18702 init_vec = make_tree_vector ();
18703 if (init == void_node)
18704 gcc_assert (init_vec != NULL);
18705 else
18706 {
18707 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18708 vec_safe_push (init_vec, TREE_VALUE (init));
18709 }
18710 }
18711
18712 /* Avoid passing an enclosing decl to valid_array_size_p. */
18713 in_decl = NULL_TREE;
18714
18715 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18716 tree op2 = RECUR (TREE_OPERAND (t, 2));
18717 ret = build_new (&placement_vec, op1, op2, &init_vec,
18718 NEW_EXPR_USE_GLOBAL (t),
18719 complain);
18720
18721 if (placement_vec != NULL)
18722 release_tree_vector (placement_vec);
18723 if (init_vec != NULL)
18724 release_tree_vector (init_vec);
18725
18726 RETURN (ret);
18727 }
18728
18729 case DELETE_EXPR:
18730 {
18731 tree op0 = RECUR (TREE_OPERAND (t, 0));
18732 tree op1 = RECUR (TREE_OPERAND (t, 1));
18733 RETURN (delete_sanity (op0, op1,
18734 DELETE_EXPR_USE_VEC (t),
18735 DELETE_EXPR_USE_GLOBAL (t),
18736 complain));
18737 }
18738
18739 case COMPOUND_EXPR:
18740 {
18741 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18742 complain & ~tf_decltype, in_decl,
18743 /*function_p=*/false,
18744 integral_constant_expression_p);
18745 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18746 op0,
18747 RECUR (TREE_OPERAND (t, 1)),
18748 complain|decltype_flag));
18749 }
18750
18751 case CALL_EXPR:
18752 {
18753 tree function;
18754 vec<tree, va_gc> *call_args;
18755 unsigned int nargs, i;
18756 bool qualified_p;
18757 bool koenig_p;
18758 tree ret;
18759
18760 function = CALL_EXPR_FN (t);
18761 /* Internal function with no arguments. */
18762 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18763 RETURN (t);
18764
18765 /* When we parsed the expression, we determined whether or
18766 not Koenig lookup should be performed. */
18767 koenig_p = KOENIG_LOOKUP_P (t);
18768 if (function == NULL_TREE)
18769 {
18770 koenig_p = false;
18771 qualified_p = false;
18772 }
18773 else if (TREE_CODE (function) == SCOPE_REF)
18774 {
18775 qualified_p = true;
18776 function = tsubst_qualified_id (function, args, complain, in_decl,
18777 /*done=*/false,
18778 /*address_p=*/false);
18779 }
18780 else if (koenig_p && identifier_p (function))
18781 {
18782 /* Do nothing; calling tsubst_copy_and_build on an identifier
18783 would incorrectly perform unqualified lookup again.
18784
18785 Note that we can also have an IDENTIFIER_NODE if the earlier
18786 unqualified lookup found a member function; in that case
18787 koenig_p will be false and we do want to do the lookup
18788 again to find the instantiated member function.
18789
18790 FIXME but doing that causes c++/15272, so we need to stop
18791 using IDENTIFIER_NODE in that situation. */
18792 qualified_p = false;
18793 }
18794 else
18795 {
18796 if (TREE_CODE (function) == COMPONENT_REF)
18797 {
18798 tree op = TREE_OPERAND (function, 1);
18799
18800 qualified_p = (TREE_CODE (op) == SCOPE_REF
18801 || (BASELINK_P (op)
18802 && BASELINK_QUALIFIED_P (op)));
18803 }
18804 else
18805 qualified_p = false;
18806
18807 if (TREE_CODE (function) == ADDR_EXPR
18808 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18809 /* Avoid error about taking the address of a constructor. */
18810 function = TREE_OPERAND (function, 0);
18811
18812 function = tsubst_copy_and_build (function, args, complain,
18813 in_decl,
18814 !qualified_p,
18815 integral_constant_expression_p);
18816
18817 if (BASELINK_P (function))
18818 qualified_p = true;
18819 }
18820
18821 nargs = call_expr_nargs (t);
18822 call_args = make_tree_vector ();
18823 for (i = 0; i < nargs; ++i)
18824 {
18825 tree arg = CALL_EXPR_ARG (t, i);
18826
18827 if (!PACK_EXPANSION_P (arg))
18828 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18829 else
18830 {
18831 /* Expand the pack expansion and push each entry onto
18832 CALL_ARGS. */
18833 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18834 if (TREE_CODE (arg) == TREE_VEC)
18835 {
18836 unsigned int len, j;
18837
18838 len = TREE_VEC_LENGTH (arg);
18839 for (j = 0; j < len; ++j)
18840 {
18841 tree value = TREE_VEC_ELT (arg, j);
18842 if (value != NULL_TREE)
18843 value = convert_from_reference (value);
18844 vec_safe_push (call_args, value);
18845 }
18846 }
18847 else
18848 {
18849 /* A partial substitution. Add one entry. */
18850 vec_safe_push (call_args, arg);
18851 }
18852 }
18853 }
18854
18855 /* Stripped-down processing for a call in a thunk. Specifically, in
18856 the thunk template for a generic lambda. */
18857 if (CALL_FROM_THUNK_P (t))
18858 {
18859 tree thisarg = NULL_TREE;
18860 if (TREE_CODE (function) == COMPONENT_REF)
18861 {
18862 thisarg = TREE_OPERAND (function, 0);
18863 if (TREE_CODE (thisarg) == INDIRECT_REF)
18864 thisarg = TREE_OPERAND (thisarg, 0);
18865 function = TREE_OPERAND (function, 1);
18866 if (TREE_CODE (function) == BASELINK)
18867 function = BASELINK_FUNCTIONS (function);
18868 }
18869 /* We aren't going to do normal overload resolution, so force the
18870 template-id to resolve. */
18871 function = resolve_nondeduced_context (function, complain);
18872 for (unsigned i = 0; i < nargs; ++i)
18873 {
18874 /* In a thunk, pass through args directly, without any
18875 conversions. */
18876 tree arg = (*call_args)[i];
18877 while (TREE_CODE (arg) != PARM_DECL)
18878 arg = TREE_OPERAND (arg, 0);
18879 (*call_args)[i] = arg;
18880 }
18881 if (thisarg)
18882 {
18883 /* Shift the other args over to make room. */
18884 tree last = (*call_args)[nargs - 1];
18885 vec_safe_push (call_args, last);
18886 for (int i = nargs-1; i > 0; --i)
18887 (*call_args)[i] = (*call_args)[i-1];
18888 (*call_args)[0] = thisarg;
18889 }
18890 ret = build_call_a (function, call_args->length (),
18891 call_args->address ());
18892 /* The thunk location is not interesting. */
18893 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
18894 CALL_FROM_THUNK_P (ret) = true;
18895 if (CLASS_TYPE_P (TREE_TYPE (ret)))
18896 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
18897
18898 release_tree_vector (call_args);
18899 RETURN (ret);
18900 }
18901
18902 /* We do not perform argument-dependent lookup if normal
18903 lookup finds a non-function, in accordance with the
18904 expected resolution of DR 218. */
18905 if (koenig_p
18906 && ((is_overloaded_fn (function)
18907 /* If lookup found a member function, the Koenig lookup is
18908 not appropriate, even if an unqualified-name was used
18909 to denote the function. */
18910 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18911 || identifier_p (function))
18912 /* Only do this when substitution turns a dependent call
18913 into a non-dependent call. */
18914 && type_dependent_expression_p_push (t)
18915 && !any_type_dependent_arguments_p (call_args))
18916 function = perform_koenig_lookup (function, call_args, tf_none);
18917
18918 if (function != NULL_TREE
18919 && identifier_p (function)
18920 && !any_type_dependent_arguments_p (call_args))
18921 {
18922 if (koenig_p && (complain & tf_warning_or_error))
18923 {
18924 /* For backwards compatibility and good diagnostics, try
18925 the unqualified lookup again if we aren't in SFINAE
18926 context. */
18927 tree unq = (tsubst_copy_and_build
18928 (function, args, complain, in_decl, true,
18929 integral_constant_expression_p));
18930 if (unq == error_mark_node)
18931 {
18932 release_tree_vector (call_args);
18933 RETURN (error_mark_node);
18934 }
18935
18936 if (unq != function)
18937 {
18938 /* In a lambda fn, we have to be careful to not
18939 introduce new this captures. Legacy code can't
18940 be using lambdas anyway, so it's ok to be
18941 stricter. */
18942 bool in_lambda = (current_class_type
18943 && LAMBDA_TYPE_P (current_class_type));
18944 char const *const msg
18945 = G_("%qD was not declared in this scope, "
18946 "and no declarations were found by "
18947 "argument-dependent lookup at the point "
18948 "of instantiation");
18949
18950 bool diag = true;
18951 if (in_lambda)
18952 error_at (cp_expr_loc_or_loc (t, input_location),
18953 msg, function);
18954 else
18955 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18956 msg, function);
18957 if (diag)
18958 {
18959 tree fn = unq;
18960
18961 if (INDIRECT_REF_P (fn))
18962 fn = TREE_OPERAND (fn, 0);
18963 if (is_overloaded_fn (fn))
18964 fn = get_first_fn (fn);
18965
18966 if (!DECL_P (fn))
18967 /* Can't say anything more. */;
18968 else if (DECL_CLASS_SCOPE_P (fn))
18969 {
18970 location_t loc = cp_expr_loc_or_loc (t,
18971 input_location);
18972 inform (loc,
18973 "declarations in dependent base %qT are "
18974 "not found by unqualified lookup",
18975 DECL_CLASS_CONTEXT (fn));
18976 if (current_class_ptr)
18977 inform (loc,
18978 "use %<this->%D%> instead", function);
18979 else
18980 inform (loc,
18981 "use %<%T::%D%> instead",
18982 current_class_name, function);
18983 }
18984 else
18985 inform (DECL_SOURCE_LOCATION (fn),
18986 "%qD declared here, later in the "
18987 "translation unit", fn);
18988 if (in_lambda)
18989 {
18990 release_tree_vector (call_args);
18991 RETURN (error_mark_node);
18992 }
18993 }
18994
18995 function = unq;
18996 }
18997 }
18998 if (identifier_p (function))
18999 {
19000 if (complain & tf_error)
19001 unqualified_name_lookup_error (function);
19002 release_tree_vector (call_args);
19003 RETURN (error_mark_node);
19004 }
19005 }
19006
19007 /* Remember that there was a reference to this entity. */
19008 if (function != NULL_TREE
19009 && DECL_P (function)
19010 && !mark_used (function, complain) && !(complain & tf_error))
19011 {
19012 release_tree_vector (call_args);
19013 RETURN (error_mark_node);
19014 }
19015
19016 /* Put back tf_decltype for the actual call. */
19017 complain |= decltype_flag;
19018
19019 if (function == NULL_TREE)
19020 switch (CALL_EXPR_IFN (t))
19021 {
19022 case IFN_LAUNDER:
19023 gcc_assert (nargs == 1);
19024 if (vec_safe_length (call_args) != 1)
19025 {
19026 error_at (cp_expr_loc_or_loc (t, input_location),
19027 "wrong number of arguments to "
19028 "%<__builtin_launder%>");
19029 ret = error_mark_node;
19030 }
19031 else
19032 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
19033 input_location),
19034 (*call_args)[0], complain);
19035 break;
19036
19037 case IFN_VEC_CONVERT:
19038 gcc_assert (nargs == 1);
19039 if (vec_safe_length (call_args) != 1)
19040 {
19041 error_at (cp_expr_loc_or_loc (t, input_location),
19042 "wrong number of arguments to "
19043 "%<__builtin_convertvector%>");
19044 ret = error_mark_node;
19045 break;
19046 }
19047 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19048 tsubst (TREE_TYPE (t), args,
19049 complain, in_decl),
19050 complain);
19051 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19052 {
19053 release_tree_vector (call_args);
19054 RETURN (ret);
19055 }
19056 break;
19057
19058 default:
19059 /* Unsupported internal function with arguments. */
19060 gcc_unreachable ();
19061 }
19062 else if (TREE_CODE (function) == OFFSET_REF
19063 || TREE_CODE (function) == DOTSTAR_EXPR
19064 || TREE_CODE (function) == MEMBER_REF)
19065 ret = build_offset_ref_call_from_tree (function, &call_args,
19066 complain);
19067 else if (TREE_CODE (function) == COMPONENT_REF)
19068 {
19069 tree instance = TREE_OPERAND (function, 0);
19070 tree fn = TREE_OPERAND (function, 1);
19071
19072 if (processing_template_decl
19073 && (type_dependent_expression_p (instance)
19074 || (!BASELINK_P (fn)
19075 && TREE_CODE (fn) != FIELD_DECL)
19076 || type_dependent_expression_p (fn)
19077 || any_type_dependent_arguments_p (call_args)))
19078 ret = build_min_nt_call_vec (function, call_args);
19079 else if (!BASELINK_P (fn))
19080 ret = finish_call_expr (function, &call_args,
19081 /*disallow_virtual=*/false,
19082 /*koenig_p=*/false,
19083 complain);
19084 else
19085 ret = (build_new_method_call
19086 (instance, fn,
19087 &call_args, NULL_TREE,
19088 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19089 /*fn_p=*/NULL,
19090 complain));
19091 }
19092 else
19093 ret = finish_call_expr (function, &call_args,
19094 /*disallow_virtual=*/qualified_p,
19095 koenig_p,
19096 complain);
19097
19098 release_tree_vector (call_args);
19099
19100 if (ret != error_mark_node)
19101 {
19102 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19103 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19104 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19105 if (op || ord || rev)
19106 {
19107 function = extract_call_expr (ret);
19108 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19109 CALL_EXPR_ORDERED_ARGS (function) = ord;
19110 CALL_EXPR_REVERSE_ARGS (function) = rev;
19111 }
19112 }
19113
19114 RETURN (ret);
19115 }
19116
19117 case COND_EXPR:
19118 {
19119 tree cond = RECUR (TREE_OPERAND (t, 0));
19120 cond = mark_rvalue_use (cond);
19121 tree folded_cond = fold_non_dependent_expr (cond, complain);
19122 tree exp1, exp2;
19123
19124 if (TREE_CODE (folded_cond) == INTEGER_CST)
19125 {
19126 if (integer_zerop (folded_cond))
19127 {
19128 ++c_inhibit_evaluation_warnings;
19129 exp1 = RECUR (TREE_OPERAND (t, 1));
19130 --c_inhibit_evaluation_warnings;
19131 exp2 = RECUR (TREE_OPERAND (t, 2));
19132 }
19133 else
19134 {
19135 exp1 = RECUR (TREE_OPERAND (t, 1));
19136 ++c_inhibit_evaluation_warnings;
19137 exp2 = RECUR (TREE_OPERAND (t, 2));
19138 --c_inhibit_evaluation_warnings;
19139 }
19140 cond = folded_cond;
19141 }
19142 else
19143 {
19144 exp1 = RECUR (TREE_OPERAND (t, 1));
19145 exp2 = RECUR (TREE_OPERAND (t, 2));
19146 }
19147
19148 warning_sentinel s(warn_duplicated_branches);
19149 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19150 cond, exp1, exp2, complain));
19151 }
19152
19153 case PSEUDO_DTOR_EXPR:
19154 {
19155 tree op0 = RECUR (TREE_OPERAND (t, 0));
19156 tree op1 = RECUR (TREE_OPERAND (t, 1));
19157 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19158 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19159 input_location));
19160 }
19161
19162 case TREE_LIST:
19163 {
19164 tree purpose, value, chain;
19165
19166 if (t == void_list_node)
19167 RETURN (t);
19168
19169 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19170 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19171 {
19172 /* We have pack expansions, so expand those and
19173 create a new list out of it. */
19174 tree purposevec = NULL_TREE;
19175 tree valuevec = NULL_TREE;
19176 tree chain;
19177 int i, len = -1;
19178
19179 /* Expand the argument expressions. */
19180 if (TREE_PURPOSE (t))
19181 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19182 complain, in_decl);
19183 if (TREE_VALUE (t))
19184 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19185 complain, in_decl);
19186
19187 /* Build the rest of the list. */
19188 chain = TREE_CHAIN (t);
19189 if (chain && chain != void_type_node)
19190 chain = RECUR (chain);
19191
19192 /* Determine the number of arguments. */
19193 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19194 {
19195 len = TREE_VEC_LENGTH (purposevec);
19196 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19197 }
19198 else if (TREE_CODE (valuevec) == TREE_VEC)
19199 len = TREE_VEC_LENGTH (valuevec);
19200 else
19201 {
19202 /* Since we only performed a partial substitution into
19203 the argument pack, we only RETURN (a single list
19204 node. */
19205 if (purposevec == TREE_PURPOSE (t)
19206 && valuevec == TREE_VALUE (t)
19207 && chain == TREE_CHAIN (t))
19208 RETURN (t);
19209
19210 RETURN (tree_cons (purposevec, valuevec, chain));
19211 }
19212
19213 /* Convert the argument vectors into a TREE_LIST */
19214 i = len;
19215 while (i > 0)
19216 {
19217 /* Grab the Ith values. */
19218 i--;
19219 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19220 : NULL_TREE;
19221 value
19222 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19223 : NULL_TREE;
19224
19225 /* Build the list (backwards). */
19226 chain = tree_cons (purpose, value, chain);
19227 }
19228
19229 RETURN (chain);
19230 }
19231
19232 purpose = TREE_PURPOSE (t);
19233 if (purpose)
19234 purpose = RECUR (purpose);
19235 value = TREE_VALUE (t);
19236 if (value)
19237 value = RECUR (value);
19238 chain = TREE_CHAIN (t);
19239 if (chain && chain != void_type_node)
19240 chain = RECUR (chain);
19241 if (purpose == TREE_PURPOSE (t)
19242 && value == TREE_VALUE (t)
19243 && chain == TREE_CHAIN (t))
19244 RETURN (t);
19245 RETURN (tree_cons (purpose, value, chain));
19246 }
19247
19248 case COMPONENT_REF:
19249 {
19250 tree object;
19251 tree object_type;
19252 tree member;
19253 tree r;
19254
19255 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19256 args, complain, in_decl);
19257 /* Remember that there was a reference to this entity. */
19258 if (DECL_P (object)
19259 && !mark_used (object, complain) && !(complain & tf_error))
19260 RETURN (error_mark_node);
19261 object_type = TREE_TYPE (object);
19262
19263 member = TREE_OPERAND (t, 1);
19264 if (BASELINK_P (member))
19265 member = tsubst_baselink (member,
19266 non_reference (TREE_TYPE (object)),
19267 args, complain, in_decl);
19268 else
19269 member = tsubst_copy (member, args, complain, in_decl);
19270 if (member == error_mark_node)
19271 RETURN (error_mark_node);
19272
19273 if (TREE_CODE (member) == FIELD_DECL)
19274 {
19275 r = finish_non_static_data_member (member, object, NULL_TREE);
19276 if (TREE_CODE (r) == COMPONENT_REF)
19277 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19278 RETURN (r);
19279 }
19280 else if (type_dependent_expression_p (object))
19281 /* We can't do much here. */;
19282 else if (!CLASS_TYPE_P (object_type))
19283 {
19284 if (scalarish_type_p (object_type))
19285 {
19286 tree s = NULL_TREE;
19287 tree dtor = member;
19288
19289 if (TREE_CODE (dtor) == SCOPE_REF)
19290 {
19291 s = TREE_OPERAND (dtor, 0);
19292 dtor = TREE_OPERAND (dtor, 1);
19293 }
19294 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19295 {
19296 dtor = TREE_OPERAND (dtor, 0);
19297 if (TYPE_P (dtor))
19298 RETURN (finish_pseudo_destructor_expr
19299 (object, s, dtor, input_location));
19300 }
19301 }
19302 }
19303 else if (TREE_CODE (member) == SCOPE_REF
19304 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19305 {
19306 /* Lookup the template functions now that we know what the
19307 scope is. */
19308 tree scope = TREE_OPERAND (member, 0);
19309 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19310 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19311 member = lookup_qualified_name (scope, tmpl,
19312 /*is_type_p=*/false,
19313 /*complain=*/false);
19314 if (BASELINK_P (member))
19315 {
19316 BASELINK_FUNCTIONS (member)
19317 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19318 args);
19319 member = (adjust_result_of_qualified_name_lookup
19320 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19321 object_type));
19322 }
19323 else
19324 {
19325 qualified_name_lookup_error (scope, tmpl, member,
19326 input_location);
19327 RETURN (error_mark_node);
19328 }
19329 }
19330 else if (TREE_CODE (member) == SCOPE_REF
19331 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19332 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19333 {
19334 if (complain & tf_error)
19335 {
19336 if (TYPE_P (TREE_OPERAND (member, 0)))
19337 error ("%qT is not a class or namespace",
19338 TREE_OPERAND (member, 0));
19339 else
19340 error ("%qD is not a class or namespace",
19341 TREE_OPERAND (member, 0));
19342 }
19343 RETURN (error_mark_node);
19344 }
19345
19346 r = finish_class_member_access_expr (object, member,
19347 /*template_p=*/false,
19348 complain);
19349 if (TREE_CODE (r) == COMPONENT_REF)
19350 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19351 RETURN (r);
19352 }
19353
19354 case THROW_EXPR:
19355 RETURN (build_throw
19356 (RECUR (TREE_OPERAND (t, 0))));
19357
19358 case CONSTRUCTOR:
19359 {
19360 vec<constructor_elt, va_gc> *n;
19361 constructor_elt *ce;
19362 unsigned HOST_WIDE_INT idx;
19363 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19364 bool process_index_p;
19365 int newlen;
19366 bool need_copy_p = false;
19367 tree r;
19368
19369 if (type == error_mark_node)
19370 RETURN (error_mark_node);
19371
19372 /* We do not want to process the index of aggregate
19373 initializers as they are identifier nodes which will be
19374 looked up by digest_init. */
19375 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19376
19377 if (null_member_pointer_value_p (t))
19378 {
19379 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19380 RETURN (t);
19381 }
19382
19383 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19384 newlen = vec_safe_length (n);
19385 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19386 {
19387 if (ce->index && process_index_p
19388 /* An identifier index is looked up in the type
19389 being initialized, not the current scope. */
19390 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19391 ce->index = RECUR (ce->index);
19392
19393 if (PACK_EXPANSION_P (ce->value))
19394 {
19395 /* Substitute into the pack expansion. */
19396 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19397 in_decl);
19398
19399 if (ce->value == error_mark_node
19400 || PACK_EXPANSION_P (ce->value))
19401 ;
19402 else if (TREE_VEC_LENGTH (ce->value) == 1)
19403 /* Just move the argument into place. */
19404 ce->value = TREE_VEC_ELT (ce->value, 0);
19405 else
19406 {
19407 /* Update the length of the final CONSTRUCTOR
19408 arguments vector, and note that we will need to
19409 copy.*/
19410 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19411 need_copy_p = true;
19412 }
19413 }
19414 else
19415 ce->value = RECUR (ce->value);
19416 }
19417
19418 if (need_copy_p)
19419 {
19420 vec<constructor_elt, va_gc> *old_n = n;
19421
19422 vec_alloc (n, newlen);
19423 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19424 {
19425 if (TREE_CODE (ce->value) == TREE_VEC)
19426 {
19427 int i, len = TREE_VEC_LENGTH (ce->value);
19428 for (i = 0; i < len; ++i)
19429 CONSTRUCTOR_APPEND_ELT (n, 0,
19430 TREE_VEC_ELT (ce->value, i));
19431 }
19432 else
19433 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19434 }
19435 }
19436
19437 r = build_constructor (init_list_type_node, n);
19438 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19439 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
19440 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
19441
19442 if (TREE_HAS_CONSTRUCTOR (t))
19443 {
19444 fcl_t cl = fcl_functional;
19445 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
19446 cl = fcl_c99;
19447 RETURN (finish_compound_literal (type, r, complain, cl));
19448 }
19449
19450 TREE_TYPE (r) = type;
19451 RETURN (r);
19452 }
19453
19454 case TYPEID_EXPR:
19455 {
19456 tree operand_0 = TREE_OPERAND (t, 0);
19457 if (TYPE_P (operand_0))
19458 {
19459 operand_0 = tsubst (operand_0, args, complain, in_decl);
19460 RETURN (get_typeid (operand_0, complain));
19461 }
19462 else
19463 {
19464 operand_0 = RECUR (operand_0);
19465 RETURN (build_typeid (operand_0, complain));
19466 }
19467 }
19468
19469 case VAR_DECL:
19470 if (!args)
19471 RETURN (t);
19472 /* Fall through */
19473
19474 case PARM_DECL:
19475 {
19476 tree r = tsubst_copy (t, args, complain, in_decl);
19477 /* ??? We're doing a subset of finish_id_expression here. */
19478 if (tree wrap = maybe_get_tls_wrapper_call (r))
19479 /* Replace an evaluated use of the thread_local variable with
19480 a call to its wrapper. */
19481 r = wrap;
19482 else if (outer_automatic_var_p (r))
19483 r = process_outer_var_ref (r, complain);
19484
19485 if (!TYPE_REF_P (TREE_TYPE (t)))
19486 /* If the original type was a reference, we'll be wrapped in
19487 the appropriate INDIRECT_REF. */
19488 r = convert_from_reference (r);
19489 RETURN (r);
19490 }
19491
19492 case VA_ARG_EXPR:
19493 {
19494 tree op0 = RECUR (TREE_OPERAND (t, 0));
19495 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19496 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19497 }
19498
19499 case OFFSETOF_EXPR:
19500 {
19501 tree object_ptr
19502 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19503 in_decl, /*function_p=*/false,
19504 /*integral_constant_expression_p=*/false);
19505 RETURN (finish_offsetof (object_ptr,
19506 RECUR (TREE_OPERAND (t, 0)),
19507 EXPR_LOCATION (t)));
19508 }
19509
19510 case ADDRESSOF_EXPR:
19511 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19512 RECUR (TREE_OPERAND (t, 0)), complain));
19513
19514 case TRAIT_EXPR:
19515 {
19516 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19517 complain, in_decl);
19518
19519 tree type2 = TRAIT_EXPR_TYPE2 (t);
19520 if (type2 && TREE_CODE (type2) == TREE_LIST)
19521 type2 = RECUR (type2);
19522 else if (type2)
19523 type2 = tsubst (type2, args, complain, in_decl);
19524
19525 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
19526 }
19527
19528 case STMT_EXPR:
19529 {
19530 tree old_stmt_expr = cur_stmt_expr;
19531 tree stmt_expr = begin_stmt_expr ();
19532
19533 cur_stmt_expr = stmt_expr;
19534 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19535 integral_constant_expression_p);
19536 stmt_expr = finish_stmt_expr (stmt_expr, false);
19537 cur_stmt_expr = old_stmt_expr;
19538
19539 /* If the resulting list of expression statement is empty,
19540 fold it further into void_node. */
19541 if (empty_expr_stmt_p (stmt_expr))
19542 stmt_expr = void_node;
19543
19544 RETURN (stmt_expr);
19545 }
19546
19547 case LAMBDA_EXPR:
19548 {
19549 if (complain & tf_partial)
19550 {
19551 /* We don't have a full set of template arguments yet; don't touch
19552 the lambda at all. */
19553 gcc_assert (processing_template_decl);
19554 return t;
19555 }
19556 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19557
19558 RETURN (build_lambda_object (r));
19559 }
19560
19561 case TARGET_EXPR:
19562 /* We can get here for a constant initializer of non-dependent type.
19563 FIXME stop folding in cp_parser_initializer_clause. */
19564 {
19565 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19566 complain);
19567 RETURN (r);
19568 }
19569
19570 case TRANSACTION_EXPR:
19571 RETURN (tsubst_expr(t, args, complain, in_decl,
19572 integral_constant_expression_p));
19573
19574 case PAREN_EXPR:
19575 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19576
19577 case VEC_PERM_EXPR:
19578 {
19579 tree op0 = RECUR (TREE_OPERAND (t, 0));
19580 tree op1 = RECUR (TREE_OPERAND (t, 1));
19581 tree op2 = RECUR (TREE_OPERAND (t, 2));
19582 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19583 complain));
19584 }
19585
19586 case REQUIRES_EXPR:
19587 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19588
19589 case RANGE_EXPR:
19590 /* No need to substitute further, a RANGE_EXPR will always be built
19591 with constant operands. */
19592 RETURN (t);
19593
19594 case NON_LVALUE_EXPR:
19595 case VIEW_CONVERT_EXPR:
19596 if (location_wrapper_p (t))
19597 /* We need to do this here as well as in tsubst_copy so we get the
19598 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19599 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19600 EXPR_LOCATION (t)));
19601 /* fallthrough. */
19602
19603 default:
19604 /* Handle Objective-C++ constructs, if appropriate. */
19605 {
19606 tree subst
19607 = objcp_tsubst_copy_and_build (t, args, complain,
19608 in_decl, /*function_p=*/false);
19609 if (subst)
19610 RETURN (subst);
19611 }
19612 RETURN (tsubst_copy (t, args, complain, in_decl));
19613 }
19614
19615 #undef RECUR
19616 #undef RETURN
19617 out:
19618 input_location = loc;
19619 return retval;
19620 }
19621
19622 /* Verify that the instantiated ARGS are valid. For type arguments,
19623 make sure that the type's linkage is ok. For non-type arguments,
19624 make sure they are constants if they are integral or enumerations.
19625 Emit an error under control of COMPLAIN, and return TRUE on error. */
19626
19627 static bool
19628 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19629 {
19630 if (dependent_template_arg_p (t))
19631 return false;
19632 if (ARGUMENT_PACK_P (t))
19633 {
19634 tree vec = ARGUMENT_PACK_ARGS (t);
19635 int len = TREE_VEC_LENGTH (vec);
19636 bool result = false;
19637 int i;
19638
19639 for (i = 0; i < len; ++i)
19640 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19641 result = true;
19642 return result;
19643 }
19644 else if (TYPE_P (t))
19645 {
19646 /* [basic.link]: A name with no linkage (notably, the name
19647 of a class or enumeration declared in a local scope)
19648 shall not be used to declare an entity with linkage.
19649 This implies that names with no linkage cannot be used as
19650 template arguments
19651
19652 DR 757 relaxes this restriction for C++0x. */
19653 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19654 : no_linkage_check (t, /*relaxed_p=*/false));
19655
19656 if (nt)
19657 {
19658 /* DR 488 makes use of a type with no linkage cause
19659 type deduction to fail. */
19660 if (complain & tf_error)
19661 {
19662 if (TYPE_UNNAMED_P (nt))
19663 error ("%qT is/uses unnamed type", t);
19664 else
19665 error ("template argument for %qD uses local type %qT",
19666 tmpl, t);
19667 }
19668 return true;
19669 }
19670 /* In order to avoid all sorts of complications, we do not
19671 allow variably-modified types as template arguments. */
19672 else if (variably_modified_type_p (t, NULL_TREE))
19673 {
19674 if (complain & tf_error)
19675 error ("%qT is a variably modified type", t);
19676 return true;
19677 }
19678 }
19679 /* Class template and alias template arguments should be OK. */
19680 else if (DECL_TYPE_TEMPLATE_P (t))
19681 ;
19682 /* A non-type argument of integral or enumerated type must be a
19683 constant. */
19684 else if (TREE_TYPE (t)
19685 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19686 && !REFERENCE_REF_P (t)
19687 && !TREE_CONSTANT (t))
19688 {
19689 if (complain & tf_error)
19690 error ("integral expression %qE is not constant", t);
19691 return true;
19692 }
19693 return false;
19694 }
19695
19696 static bool
19697 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19698 {
19699 int ix, len = DECL_NTPARMS (tmpl);
19700 bool result = false;
19701
19702 for (ix = 0; ix != len; ix++)
19703 {
19704 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19705 result = true;
19706 }
19707 if (result && (complain & tf_error))
19708 error (" trying to instantiate %qD", tmpl);
19709 return result;
19710 }
19711
19712 /* We're out of SFINAE context now, so generate diagnostics for the access
19713 errors we saw earlier when instantiating D from TMPL and ARGS. */
19714
19715 static void
19716 recheck_decl_substitution (tree d, tree tmpl, tree args)
19717 {
19718 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19719 tree type = TREE_TYPE (pattern);
19720 location_t loc = input_location;
19721
19722 push_access_scope (d);
19723 push_deferring_access_checks (dk_no_deferred);
19724 input_location = DECL_SOURCE_LOCATION (pattern);
19725 tsubst (type, args, tf_warning_or_error, d);
19726 input_location = loc;
19727 pop_deferring_access_checks ();
19728 pop_access_scope (d);
19729 }
19730
19731 /* Instantiate the indicated variable, function, or alias template TMPL with
19732 the template arguments in TARG_PTR. */
19733
19734 static tree
19735 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19736 {
19737 tree targ_ptr = orig_args;
19738 tree fndecl;
19739 tree gen_tmpl;
19740 tree spec;
19741 bool access_ok = true;
19742
19743 if (tmpl == error_mark_node)
19744 return error_mark_node;
19745
19746 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19747
19748 /* If this function is a clone, handle it specially. */
19749 if (DECL_CLONED_FUNCTION_P (tmpl))
19750 {
19751 tree spec;
19752 tree clone;
19753
19754 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19755 DECL_CLONED_FUNCTION. */
19756 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19757 targ_ptr, complain);
19758 if (spec == error_mark_node)
19759 return error_mark_node;
19760
19761 /* Look for the clone. */
19762 FOR_EACH_CLONE (clone, spec)
19763 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19764 return clone;
19765 /* We should always have found the clone by now. */
19766 gcc_unreachable ();
19767 return NULL_TREE;
19768 }
19769
19770 if (targ_ptr == error_mark_node)
19771 return error_mark_node;
19772
19773 /* Check to see if we already have this specialization. */
19774 gen_tmpl = most_general_template (tmpl);
19775 if (TMPL_ARGS_DEPTH (targ_ptr)
19776 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19777 /* targ_ptr only has the innermost template args, so add the outer ones
19778 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19779 the case of a non-dependent call within a template definition). */
19780 targ_ptr = (add_outermost_template_args
19781 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19782 targ_ptr));
19783
19784 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19785 but it doesn't seem to be on the hot path. */
19786 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19787
19788 gcc_assert (tmpl == gen_tmpl
19789 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19790 == spec)
19791 || fndecl == NULL_TREE);
19792
19793 if (spec != NULL_TREE)
19794 {
19795 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19796 {
19797 if (complain & tf_error)
19798 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19799 return error_mark_node;
19800 }
19801 return spec;
19802 }
19803
19804 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19805 complain))
19806 return error_mark_node;
19807
19808 /* We are building a FUNCTION_DECL, during which the access of its
19809 parameters and return types have to be checked. However this
19810 FUNCTION_DECL which is the desired context for access checking
19811 is not built yet. We solve this chicken-and-egg problem by
19812 deferring all checks until we have the FUNCTION_DECL. */
19813 push_deferring_access_checks (dk_deferred);
19814
19815 /* Instantiation of the function happens in the context of the function
19816 template, not the context of the overload resolution we're doing. */
19817 push_to_top_level ();
19818 /* If there are dependent arguments, e.g. because we're doing partial
19819 ordering, make sure processing_template_decl stays set. */
19820 if (uses_template_parms (targ_ptr))
19821 ++processing_template_decl;
19822 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19823 {
19824 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19825 complain, gen_tmpl, true);
19826 push_nested_class (ctx);
19827 }
19828
19829 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19830
19831 fndecl = NULL_TREE;
19832 if (VAR_P (pattern))
19833 {
19834 /* We need to determine if we're using a partial or explicit
19835 specialization now, because the type of the variable could be
19836 different. */
19837 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19838 tree elt = most_specialized_partial_spec (tid, complain);
19839 if (elt == error_mark_node)
19840 pattern = error_mark_node;
19841 else if (elt)
19842 {
19843 tree partial_tmpl = TREE_VALUE (elt);
19844 tree partial_args = TREE_PURPOSE (elt);
19845 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19846 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19847 }
19848 }
19849
19850 /* Substitute template parameters to obtain the specialization. */
19851 if (fndecl == NULL_TREE)
19852 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19853 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19854 pop_nested_class ();
19855 pop_from_top_level ();
19856
19857 if (fndecl == error_mark_node)
19858 {
19859 pop_deferring_access_checks ();
19860 return error_mark_node;
19861 }
19862
19863 /* The DECL_TI_TEMPLATE should always be the immediate parent
19864 template, not the most general template. */
19865 DECL_TI_TEMPLATE (fndecl) = tmpl;
19866 DECL_TI_ARGS (fndecl) = targ_ptr;
19867
19868 /* Now we know the specialization, compute access previously
19869 deferred. Do no access control for inheriting constructors,
19870 as we already checked access for the inherited constructor. */
19871 if (!(flag_new_inheriting_ctors
19872 && DECL_INHERITED_CTOR (fndecl)))
19873 {
19874 push_access_scope (fndecl);
19875 if (!perform_deferred_access_checks (complain))
19876 access_ok = false;
19877 pop_access_scope (fndecl);
19878 }
19879 pop_deferring_access_checks ();
19880
19881 /* If we've just instantiated the main entry point for a function,
19882 instantiate all the alternate entry points as well. We do this
19883 by cloning the instantiation of the main entry point, not by
19884 instantiating the template clones. */
19885 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19886 clone_function_decl (fndecl, /*update_methods=*/false);
19887
19888 if (!access_ok)
19889 {
19890 if (!(complain & tf_error))
19891 {
19892 /* Remember to reinstantiate when we're out of SFINAE so the user
19893 can see the errors. */
19894 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19895 }
19896 return error_mark_node;
19897 }
19898 return fndecl;
19899 }
19900
19901 /* Wrapper for instantiate_template_1. */
19902
19903 tree
19904 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19905 {
19906 tree ret;
19907 timevar_push (TV_TEMPLATE_INST);
19908 ret = instantiate_template_1 (tmpl, orig_args, complain);
19909 timevar_pop (TV_TEMPLATE_INST);
19910 return ret;
19911 }
19912
19913 /* Instantiate the alias template TMPL with ARGS. Also push a template
19914 instantiation level, which instantiate_template doesn't do because
19915 functions and variables have sufficient context established by the
19916 callers. */
19917
19918 static tree
19919 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19920 {
19921 if (tmpl == error_mark_node || args == error_mark_node)
19922 return error_mark_node;
19923 if (!push_tinst_level (tmpl, args))
19924 return error_mark_node;
19925
19926 args =
19927 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19928 args, tmpl, complain,
19929 /*require_all_args=*/true,
19930 /*use_default_args=*/true);
19931
19932 tree r = instantiate_template (tmpl, args, complain);
19933 pop_tinst_level ();
19934
19935 return r;
19936 }
19937
19938 /* PARM is a template parameter pack for FN. Returns true iff
19939 PARM is used in a deducible way in the argument list of FN. */
19940
19941 static bool
19942 pack_deducible_p (tree parm, tree fn)
19943 {
19944 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19945 for (; t; t = TREE_CHAIN (t))
19946 {
19947 tree type = TREE_VALUE (t);
19948 tree packs;
19949 if (!PACK_EXPANSION_P (type))
19950 continue;
19951 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19952 packs; packs = TREE_CHAIN (packs))
19953 if (template_args_equal (TREE_VALUE (packs), parm))
19954 {
19955 /* The template parameter pack is used in a function parameter
19956 pack. If this is the end of the parameter list, the
19957 template parameter pack is deducible. */
19958 if (TREE_CHAIN (t) == void_list_node)
19959 return true;
19960 else
19961 /* Otherwise, not. Well, it could be deduced from
19962 a non-pack parameter, but doing so would end up with
19963 a deduction mismatch, so don't bother. */
19964 return false;
19965 }
19966 }
19967 /* The template parameter pack isn't used in any function parameter
19968 packs, but it might be used deeper, e.g. tuple<Args...>. */
19969 return true;
19970 }
19971
19972 /* Subroutine of fn_type_unification: check non-dependent parms for
19973 convertibility. */
19974
19975 static int
19976 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19977 tree fn, unification_kind_t strict, int flags,
19978 struct conversion **convs, bool explain_p)
19979 {
19980 /* Non-constructor methods need to leave a conversion for 'this', which
19981 isn't included in nargs here. */
19982 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19983 && !DECL_CONSTRUCTOR_P (fn));
19984
19985 for (unsigned ia = 0;
19986 parms && parms != void_list_node && ia < nargs; )
19987 {
19988 tree parm = TREE_VALUE (parms);
19989
19990 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19991 && (!TREE_CHAIN (parms)
19992 || TREE_CHAIN (parms) == void_list_node))
19993 /* For a function parameter pack that occurs at the end of the
19994 parameter-declaration-list, the type A of each remaining
19995 argument of the call is compared with the type P of the
19996 declarator-id of the function parameter pack. */
19997 break;
19998
19999 parms = TREE_CHAIN (parms);
20000
20001 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20002 /* For a function parameter pack that does not occur at the
20003 end of the parameter-declaration-list, the type of the
20004 parameter pack is a non-deduced context. */
20005 continue;
20006
20007 if (!uses_template_parms (parm))
20008 {
20009 tree arg = args[ia];
20010 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20011 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20012
20013 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20014 conv_p, explain_p))
20015 return 1;
20016 }
20017
20018 ++ia;
20019 }
20020
20021 return 0;
20022 }
20023
20024 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20025 NARGS elements of the arguments that are being used when calling
20026 it. TARGS is a vector into which the deduced template arguments
20027 are placed.
20028
20029 Returns either a FUNCTION_DECL for the matching specialization of FN or
20030 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20031 true, diagnostics will be printed to explain why it failed.
20032
20033 If FN is a conversion operator, or we are trying to produce a specific
20034 specialization, RETURN_TYPE is the return type desired.
20035
20036 The EXPLICIT_TARGS are explicit template arguments provided via a
20037 template-id.
20038
20039 The parameter STRICT is one of:
20040
20041 DEDUCE_CALL:
20042 We are deducing arguments for a function call, as in
20043 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20044 deducing arguments for a call to the result of a conversion
20045 function template, as in [over.call.object].
20046
20047 DEDUCE_CONV:
20048 We are deducing arguments for a conversion function, as in
20049 [temp.deduct.conv].
20050
20051 DEDUCE_EXACT:
20052 We are deducing arguments when doing an explicit instantiation
20053 as in [temp.explicit], when determining an explicit specialization
20054 as in [temp.expl.spec], or when taking the address of a function
20055 template, as in [temp.deduct.funcaddr]. */
20056
20057 tree
20058 fn_type_unification (tree fn,
20059 tree explicit_targs,
20060 tree targs,
20061 const tree *args,
20062 unsigned int nargs,
20063 tree return_type,
20064 unification_kind_t strict,
20065 int flags,
20066 struct conversion **convs,
20067 bool explain_p,
20068 bool decltype_p)
20069 {
20070 tree parms;
20071 tree fntype;
20072 tree decl = NULL_TREE;
20073 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20074 bool ok;
20075 static int deduction_depth;
20076 /* type_unification_real will pass back any access checks from default
20077 template argument substitution. */
20078 vec<deferred_access_check, va_gc> *checks = NULL;
20079 /* We don't have all the template args yet. */
20080 bool incomplete = true;
20081
20082 tree orig_fn = fn;
20083 if (flag_new_inheriting_ctors)
20084 fn = strip_inheriting_ctors (fn);
20085
20086 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20087 tree r = error_mark_node;
20088
20089 tree full_targs = targs;
20090 if (TMPL_ARGS_DEPTH (targs)
20091 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20092 full_targs = (add_outermost_template_args
20093 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20094 targs));
20095
20096 if (decltype_p)
20097 complain |= tf_decltype;
20098
20099 /* In C++0x, it's possible to have a function template whose type depends
20100 on itself recursively. This is most obvious with decltype, but can also
20101 occur with enumeration scope (c++/48969). So we need to catch infinite
20102 recursion and reject the substitution at deduction time; this function
20103 will return error_mark_node for any repeated substitution.
20104
20105 This also catches excessive recursion such as when f<N> depends on
20106 f<N-1> across all integers, and returns error_mark_node for all the
20107 substitutions back up to the initial one.
20108
20109 This is, of course, not reentrant. */
20110 if (excessive_deduction_depth)
20111 return error_mark_node;
20112 ++deduction_depth;
20113
20114 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20115
20116 fntype = TREE_TYPE (fn);
20117 if (explicit_targs)
20118 {
20119 /* [temp.deduct]
20120
20121 The specified template arguments must match the template
20122 parameters in kind (i.e., type, nontype, template), and there
20123 must not be more arguments than there are parameters;
20124 otherwise type deduction fails.
20125
20126 Nontype arguments must match the types of the corresponding
20127 nontype template parameters, or must be convertible to the
20128 types of the corresponding nontype parameters as specified in
20129 _temp.arg.nontype_, otherwise type deduction fails.
20130
20131 All references in the function type of the function template
20132 to the corresponding template parameters are replaced by the
20133 specified template argument values. If a substitution in a
20134 template parameter or in the function type of the function
20135 template results in an invalid type, type deduction fails. */
20136 int i, len = TREE_VEC_LENGTH (tparms);
20137 location_t loc = input_location;
20138 incomplete = false;
20139
20140 if (explicit_targs == error_mark_node)
20141 goto fail;
20142
20143 if (TMPL_ARGS_DEPTH (explicit_targs)
20144 < TMPL_ARGS_DEPTH (full_targs))
20145 explicit_targs = add_outermost_template_args (full_targs,
20146 explicit_targs);
20147
20148 /* Adjust any explicit template arguments before entering the
20149 substitution context. */
20150 explicit_targs
20151 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20152 complain|tf_partial,
20153 /*require_all_args=*/false,
20154 /*use_default_args=*/false));
20155 if (explicit_targs == error_mark_node)
20156 goto fail;
20157
20158 /* Substitute the explicit args into the function type. This is
20159 necessary so that, for instance, explicitly declared function
20160 arguments can match null pointed constants. If we were given
20161 an incomplete set of explicit args, we must not do semantic
20162 processing during substitution as we could create partial
20163 instantiations. */
20164 for (i = 0; i < len; i++)
20165 {
20166 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20167 bool parameter_pack = false;
20168 tree targ = TREE_VEC_ELT (explicit_targs, i);
20169
20170 /* Dig out the actual parm. */
20171 if (TREE_CODE (parm) == TYPE_DECL
20172 || TREE_CODE (parm) == TEMPLATE_DECL)
20173 {
20174 parm = TREE_TYPE (parm);
20175 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20176 }
20177 else if (TREE_CODE (parm) == PARM_DECL)
20178 {
20179 parm = DECL_INITIAL (parm);
20180 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20181 }
20182
20183 if (targ == NULL_TREE)
20184 /* No explicit argument for this template parameter. */
20185 incomplete = true;
20186 else if (parameter_pack && pack_deducible_p (parm, fn))
20187 {
20188 /* Mark the argument pack as "incomplete". We could
20189 still deduce more arguments during unification.
20190 We remove this mark in type_unification_real. */
20191 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20192 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20193 = ARGUMENT_PACK_ARGS (targ);
20194
20195 /* We have some incomplete argument packs. */
20196 incomplete = true;
20197 }
20198 }
20199
20200 if (incomplete)
20201 {
20202 if (!push_tinst_level (fn, explicit_targs))
20203 {
20204 excessive_deduction_depth = true;
20205 goto fail;
20206 }
20207 ++processing_template_decl;
20208 input_location = DECL_SOURCE_LOCATION (fn);
20209 /* Ignore any access checks; we'll see them again in
20210 instantiate_template and they might have the wrong
20211 access path at this point. */
20212 push_deferring_access_checks (dk_deferred);
20213 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20214 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20215 pop_deferring_access_checks ();
20216 input_location = loc;
20217 --processing_template_decl;
20218 pop_tinst_level ();
20219
20220 if (fntype == error_mark_node)
20221 goto fail;
20222 }
20223
20224 /* Place the explicitly specified arguments in TARGS. */
20225 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20226 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20227 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20228 if (!incomplete && CHECKING_P
20229 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20230 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20231 (targs, NUM_TMPL_ARGS (explicit_targs));
20232 }
20233
20234 if (return_type && strict != DEDUCE_CALL)
20235 {
20236 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20237 new_args[0] = return_type;
20238 memcpy (new_args + 1, args, nargs * sizeof (tree));
20239 args = new_args;
20240 ++nargs;
20241 }
20242
20243 if (!incomplete)
20244 goto deduced;
20245
20246 /* Never do unification on the 'this' parameter. */
20247 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20248
20249 if (return_type && strict == DEDUCE_CALL)
20250 {
20251 /* We're deducing for a call to the result of a template conversion
20252 function. The parms we really want are in return_type. */
20253 if (INDIRECT_TYPE_P (return_type))
20254 return_type = TREE_TYPE (return_type);
20255 parms = TYPE_ARG_TYPES (return_type);
20256 }
20257 else if (return_type)
20258 {
20259 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20260 }
20261
20262 /* We allow incomplete unification without an error message here
20263 because the standard doesn't seem to explicitly prohibit it. Our
20264 callers must be ready to deal with unification failures in any
20265 event. */
20266
20267 /* If we aren't explaining yet, push tinst context so we can see where
20268 any errors (e.g. from class instantiations triggered by instantiation
20269 of default template arguments) come from. If we are explaining, this
20270 context is redundant. */
20271 if (!explain_p && !push_tinst_level (fn, targs))
20272 {
20273 excessive_deduction_depth = true;
20274 goto fail;
20275 }
20276
20277 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20278 full_targs, parms, args, nargs, /*subr=*/0,
20279 strict, &checks, explain_p);
20280 if (!explain_p)
20281 pop_tinst_level ();
20282 if (!ok)
20283 goto fail;
20284
20285 /* Now that we have bindings for all of the template arguments,
20286 ensure that the arguments deduced for the template template
20287 parameters have compatible template parameter lists. We cannot
20288 check this property before we have deduced all template
20289 arguments, because the template parameter types of a template
20290 template parameter might depend on prior template parameters
20291 deduced after the template template parameter. The following
20292 ill-formed example illustrates this issue:
20293
20294 template<typename T, template<T> class C> void f(C<5>, T);
20295
20296 template<int N> struct X {};
20297
20298 void g() {
20299 f(X<5>(), 5l); // error: template argument deduction fails
20300 }
20301
20302 The template parameter list of 'C' depends on the template type
20303 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20304 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20305 time that we deduce 'C'. */
20306 if (!template_template_parm_bindings_ok_p
20307 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20308 {
20309 unify_inconsistent_template_template_parameters (explain_p);
20310 goto fail;
20311 }
20312
20313 /* DR 1391: All parameters have args, now check non-dependent parms for
20314 convertibility. */
20315 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20316 convs, explain_p))
20317 goto fail;
20318
20319 deduced:
20320 /* All is well so far. Now, check:
20321
20322 [temp.deduct]
20323
20324 When all template arguments have been deduced, all uses of
20325 template parameters in nondeduced contexts are replaced with
20326 the corresponding deduced argument values. If the
20327 substitution results in an invalid type, as described above,
20328 type deduction fails. */
20329 if (!push_tinst_level (fn, targs))
20330 {
20331 excessive_deduction_depth = true;
20332 goto fail;
20333 }
20334
20335 /* Also collect access checks from the instantiation. */
20336 reopen_deferring_access_checks (checks);
20337
20338 decl = instantiate_template (fn, targs, complain);
20339
20340 checks = get_deferred_access_checks ();
20341 pop_deferring_access_checks ();
20342
20343 pop_tinst_level ();
20344
20345 if (decl == error_mark_node)
20346 goto fail;
20347
20348 /* Now perform any access checks encountered during substitution. */
20349 push_access_scope (decl);
20350 ok = perform_access_checks (checks, complain);
20351 pop_access_scope (decl);
20352 if (!ok)
20353 goto fail;
20354
20355 /* If we're looking for an exact match, check that what we got
20356 is indeed an exact match. It might not be if some template
20357 parameters are used in non-deduced contexts. But don't check
20358 for an exact match if we have dependent template arguments;
20359 in that case we're doing partial ordering, and we already know
20360 that we have two candidates that will provide the actual type. */
20361 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20362 {
20363 tree substed = TREE_TYPE (decl);
20364 unsigned int i;
20365
20366 tree sarg
20367 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20368 if (return_type)
20369 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20370 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20371 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20372 {
20373 unify_type_mismatch (explain_p, args[i],
20374 TREE_VALUE (sarg));
20375 goto fail;
20376 }
20377 }
20378
20379 /* After doing deduction with the inherited constructor, actually return an
20380 instantiation of the inheriting constructor. */
20381 if (orig_fn != fn)
20382 decl = instantiate_template (orig_fn, targs, complain);
20383
20384 r = decl;
20385
20386 fail:
20387 --deduction_depth;
20388 if (excessive_deduction_depth)
20389 {
20390 if (deduction_depth == 0)
20391 /* Reset once we're all the way out. */
20392 excessive_deduction_depth = false;
20393 }
20394
20395 return r;
20396 }
20397
20398 /* Adjust types before performing type deduction, as described in
20399 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20400 sections are symmetric. PARM is the type of a function parameter
20401 or the return type of the conversion function. ARG is the type of
20402 the argument passed to the call, or the type of the value
20403 initialized with the result of the conversion function.
20404 ARG_EXPR is the original argument expression, which may be null. */
20405
20406 static int
20407 maybe_adjust_types_for_deduction (unification_kind_t strict,
20408 tree* parm,
20409 tree* arg,
20410 tree arg_expr)
20411 {
20412 int result = 0;
20413
20414 switch (strict)
20415 {
20416 case DEDUCE_CALL:
20417 break;
20418
20419 case DEDUCE_CONV:
20420 /* Swap PARM and ARG throughout the remainder of this
20421 function; the handling is precisely symmetric since PARM
20422 will initialize ARG rather than vice versa. */
20423 std::swap (parm, arg);
20424 break;
20425
20426 case DEDUCE_EXACT:
20427 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20428 too, but here handle it by stripping the reference from PARM
20429 rather than by adding it to ARG. */
20430 if (TYPE_REF_P (*parm)
20431 && TYPE_REF_IS_RVALUE (*parm)
20432 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20433 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20434 && TYPE_REF_P (*arg)
20435 && !TYPE_REF_IS_RVALUE (*arg))
20436 *parm = TREE_TYPE (*parm);
20437 /* Nothing else to do in this case. */
20438 return 0;
20439
20440 default:
20441 gcc_unreachable ();
20442 }
20443
20444 if (!TYPE_REF_P (*parm))
20445 {
20446 /* [temp.deduct.call]
20447
20448 If P is not a reference type:
20449
20450 --If A is an array type, the pointer type produced by the
20451 array-to-pointer standard conversion (_conv.array_) is
20452 used in place of A for type deduction; otherwise,
20453
20454 --If A is a function type, the pointer type produced by
20455 the function-to-pointer standard conversion
20456 (_conv.func_) is used in place of A for type deduction;
20457 otherwise,
20458
20459 --If A is a cv-qualified type, the top level
20460 cv-qualifiers of A's type are ignored for type
20461 deduction. */
20462 if (TREE_CODE (*arg) == ARRAY_TYPE)
20463 *arg = build_pointer_type (TREE_TYPE (*arg));
20464 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20465 *arg = build_pointer_type (*arg);
20466 else
20467 *arg = TYPE_MAIN_VARIANT (*arg);
20468 }
20469
20470 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20471 reference to a cv-unqualified template parameter that does not represent a
20472 template parameter of a class template (during class template argument
20473 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20474 an lvalue, the type "lvalue reference to A" is used in place of A for type
20475 deduction. */
20476 if (TYPE_REF_P (*parm)
20477 && TYPE_REF_IS_RVALUE (*parm)
20478 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20479 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20480 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20481 && (arg_expr ? lvalue_p (arg_expr)
20482 /* try_one_overload doesn't provide an arg_expr, but
20483 functions are always lvalues. */
20484 : TREE_CODE (*arg) == FUNCTION_TYPE))
20485 *arg = build_reference_type (*arg);
20486
20487 /* [temp.deduct.call]
20488
20489 If P is a cv-qualified type, the top level cv-qualifiers
20490 of P's type are ignored for type deduction. If P is a
20491 reference type, the type referred to by P is used for
20492 type deduction. */
20493 *parm = TYPE_MAIN_VARIANT (*parm);
20494 if (TYPE_REF_P (*parm))
20495 {
20496 *parm = TREE_TYPE (*parm);
20497 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20498 }
20499
20500 /* DR 322. For conversion deduction, remove a reference type on parm
20501 too (which has been swapped into ARG). */
20502 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20503 *arg = TREE_TYPE (*arg);
20504
20505 return result;
20506 }
20507
20508 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20509 template which doesn't contain any deducible template parameters; check if
20510 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20511 unify_one_argument. */
20512
20513 static int
20514 check_non_deducible_conversion (tree parm, tree arg, int strict,
20515 int flags, struct conversion **conv_p,
20516 bool explain_p)
20517 {
20518 tree type;
20519
20520 if (!TYPE_P (arg))
20521 type = TREE_TYPE (arg);
20522 else
20523 type = arg;
20524
20525 if (same_type_p (parm, type))
20526 return unify_success (explain_p);
20527
20528 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20529 if (strict == DEDUCE_CONV)
20530 {
20531 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20532 return unify_success (explain_p);
20533 }
20534 else if (strict != DEDUCE_EXACT)
20535 {
20536 bool ok = false;
20537 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20538 if (conv_p)
20539 /* Avoid recalculating this in add_function_candidate. */
20540 ok = (*conv_p
20541 = good_conversion (parm, type, conv_arg, flags, complain));
20542 else
20543 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20544 if (ok)
20545 return unify_success (explain_p);
20546 }
20547
20548 if (strict == DEDUCE_EXACT)
20549 return unify_type_mismatch (explain_p, parm, arg);
20550 else
20551 return unify_arg_conversion (explain_p, parm, type, arg);
20552 }
20553
20554 static bool uses_deducible_template_parms (tree type);
20555
20556 /* Returns true iff the expression EXPR is one from which a template
20557 argument can be deduced. In other words, if it's an undecorated
20558 use of a template non-type parameter. */
20559
20560 static bool
20561 deducible_expression (tree expr)
20562 {
20563 /* Strip implicit conversions. */
20564 while (CONVERT_EXPR_P (expr))
20565 expr = TREE_OPERAND (expr, 0);
20566 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20567 }
20568
20569 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20570 deducible way; that is, if it has a max value of <PARM> - 1. */
20571
20572 static bool
20573 deducible_array_bound (tree domain)
20574 {
20575 if (domain == NULL_TREE)
20576 return false;
20577
20578 tree max = TYPE_MAX_VALUE (domain);
20579 if (TREE_CODE (max) != MINUS_EXPR)
20580 return false;
20581
20582 return deducible_expression (TREE_OPERAND (max, 0));
20583 }
20584
20585 /* Returns true iff the template arguments ARGS use a template parameter
20586 in a deducible way. */
20587
20588 static bool
20589 deducible_template_args (tree args)
20590 {
20591 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20592 {
20593 bool deducible;
20594 tree elt = TREE_VEC_ELT (args, i);
20595 if (ARGUMENT_PACK_P (elt))
20596 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20597 else
20598 {
20599 if (PACK_EXPANSION_P (elt))
20600 elt = PACK_EXPANSION_PATTERN (elt);
20601 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20602 deducible = true;
20603 else if (TYPE_P (elt))
20604 deducible = uses_deducible_template_parms (elt);
20605 else
20606 deducible = deducible_expression (elt);
20607 }
20608 if (deducible)
20609 return true;
20610 }
20611 return false;
20612 }
20613
20614 /* Returns true iff TYPE contains any deducible references to template
20615 parameters, as per 14.8.2.5. */
20616
20617 static bool
20618 uses_deducible_template_parms (tree type)
20619 {
20620 if (PACK_EXPANSION_P (type))
20621 type = PACK_EXPANSION_PATTERN (type);
20622
20623 /* T
20624 cv-list T
20625 TT<T>
20626 TT<i>
20627 TT<> */
20628 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20629 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20630 return true;
20631
20632 /* T*
20633 T&
20634 T&& */
20635 if (INDIRECT_TYPE_P (type))
20636 return uses_deducible_template_parms (TREE_TYPE (type));
20637
20638 /* T[integer-constant ]
20639 type [i] */
20640 if (TREE_CODE (type) == ARRAY_TYPE)
20641 return (uses_deducible_template_parms (TREE_TYPE (type))
20642 || deducible_array_bound (TYPE_DOMAIN (type)));
20643
20644 /* T type ::*
20645 type T::*
20646 T T::*
20647 T (type ::*)()
20648 type (T::*)()
20649 type (type ::*)(T)
20650 type (T::*)(T)
20651 T (type ::*)(T)
20652 T (T::*)()
20653 T (T::*)(T) */
20654 if (TYPE_PTRMEM_P (type))
20655 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20656 || (uses_deducible_template_parms
20657 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20658
20659 /* template-name <T> (where template-name refers to a class template)
20660 template-name <i> (where template-name refers to a class template) */
20661 if (CLASS_TYPE_P (type)
20662 && CLASSTYPE_TEMPLATE_INFO (type)
20663 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20664 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20665 (CLASSTYPE_TI_ARGS (type)));
20666
20667 /* type (T)
20668 T()
20669 T(T) */
20670 if (TREE_CODE (type) == FUNCTION_TYPE
20671 || TREE_CODE (type) == METHOD_TYPE)
20672 {
20673 if (uses_deducible_template_parms (TREE_TYPE (type)))
20674 return true;
20675 tree parm = TYPE_ARG_TYPES (type);
20676 if (TREE_CODE (type) == METHOD_TYPE)
20677 parm = TREE_CHAIN (parm);
20678 for (; parm; parm = TREE_CHAIN (parm))
20679 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20680 return true;
20681 }
20682
20683 return false;
20684 }
20685
20686 /* Subroutine of type_unification_real and unify_pack_expansion to
20687 handle unification of a single P/A pair. Parameters are as
20688 for those functions. */
20689
20690 static int
20691 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20692 int subr, unification_kind_t strict,
20693 bool explain_p)
20694 {
20695 tree arg_expr = NULL_TREE;
20696 int arg_strict;
20697
20698 if (arg == error_mark_node || parm == error_mark_node)
20699 return unify_invalid (explain_p);
20700 if (arg == unknown_type_node)
20701 /* We can't deduce anything from this, but we might get all the
20702 template args from other function args. */
20703 return unify_success (explain_p);
20704
20705 /* Implicit conversions (Clause 4) will be performed on a function
20706 argument to convert it to the type of the corresponding function
20707 parameter if the parameter type contains no template-parameters that
20708 participate in template argument deduction. */
20709 if (strict != DEDUCE_EXACT
20710 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20711 /* For function parameters with no deducible template parameters,
20712 just return. We'll check non-dependent conversions later. */
20713 return unify_success (explain_p);
20714
20715 switch (strict)
20716 {
20717 case DEDUCE_CALL:
20718 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20719 | UNIFY_ALLOW_MORE_CV_QUAL
20720 | UNIFY_ALLOW_DERIVED);
20721 break;
20722
20723 case DEDUCE_CONV:
20724 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20725 break;
20726
20727 case DEDUCE_EXACT:
20728 arg_strict = UNIFY_ALLOW_NONE;
20729 break;
20730
20731 default:
20732 gcc_unreachable ();
20733 }
20734
20735 /* We only do these transformations if this is the top-level
20736 parameter_type_list in a call or declaration matching; in other
20737 situations (nested function declarators, template argument lists) we
20738 won't be comparing a type to an expression, and we don't do any type
20739 adjustments. */
20740 if (!subr)
20741 {
20742 if (!TYPE_P (arg))
20743 {
20744 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20745 if (type_unknown_p (arg))
20746 {
20747 /* [temp.deduct.type] A template-argument can be
20748 deduced from a pointer to function or pointer
20749 to member function argument if the set of
20750 overloaded functions does not contain function
20751 templates and at most one of a set of
20752 overloaded functions provides a unique
20753 match. */
20754 resolve_overloaded_unification (tparms, targs, parm,
20755 arg, strict,
20756 arg_strict, explain_p);
20757 /* If a unique match was not found, this is a
20758 non-deduced context, so we still succeed. */
20759 return unify_success (explain_p);
20760 }
20761
20762 arg_expr = arg;
20763 arg = unlowered_expr_type (arg);
20764 if (arg == error_mark_node)
20765 return unify_invalid (explain_p);
20766 }
20767
20768 arg_strict |=
20769 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20770 }
20771 else
20772 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20773 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20774 return unify_template_argument_mismatch (explain_p, parm, arg);
20775
20776 /* For deduction from an init-list we need the actual list. */
20777 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20778 arg = arg_expr;
20779 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20780 }
20781
20782 /* for_each_template_parm callback that always returns 0. */
20783
20784 static int
20785 zero_r (tree, void *)
20786 {
20787 return 0;
20788 }
20789
20790 /* for_each_template_parm any_fn callback to handle deduction of a template
20791 type argument from the type of an array bound. */
20792
20793 static int
20794 array_deduction_r (tree t, void *data)
20795 {
20796 tree_pair_p d = (tree_pair_p)data;
20797 tree &tparms = d->purpose;
20798 tree &targs = d->value;
20799
20800 if (TREE_CODE (t) == ARRAY_TYPE)
20801 if (tree dom = TYPE_DOMAIN (t))
20802 if (tree max = TYPE_MAX_VALUE (dom))
20803 {
20804 if (TREE_CODE (max) == MINUS_EXPR)
20805 max = TREE_OPERAND (max, 0);
20806 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20807 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20808 UNIFY_ALLOW_NONE, /*explain*/false);
20809 }
20810
20811 /* Keep walking. */
20812 return 0;
20813 }
20814
20815 /* Try to deduce any not-yet-deduced template type arguments from the type of
20816 an array bound. This is handled separately from unify because 14.8.2.5 says
20817 "The type of a type parameter is only deduced from an array bound if it is
20818 not otherwise deduced." */
20819
20820 static void
20821 try_array_deduction (tree tparms, tree targs, tree parm)
20822 {
20823 tree_pair_s data = { tparms, targs };
20824 hash_set<tree> visited;
20825 for_each_template_parm (parm, zero_r, &data, &visited,
20826 /*nondeduced*/false, array_deduction_r);
20827 }
20828
20829 /* Most parms like fn_type_unification.
20830
20831 If SUBR is 1, we're being called recursively (to unify the
20832 arguments of a function or method parameter of a function
20833 template).
20834
20835 CHECKS is a pointer to a vector of access checks encountered while
20836 substituting default template arguments. */
20837
20838 static int
20839 type_unification_real (tree tparms,
20840 tree full_targs,
20841 tree xparms,
20842 const tree *xargs,
20843 unsigned int xnargs,
20844 int subr,
20845 unification_kind_t strict,
20846 vec<deferred_access_check, va_gc> **checks,
20847 bool explain_p)
20848 {
20849 tree parm, arg;
20850 int i;
20851 int ntparms = TREE_VEC_LENGTH (tparms);
20852 int saw_undeduced = 0;
20853 tree parms;
20854 const tree *args;
20855 unsigned int nargs;
20856 unsigned int ia;
20857
20858 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20859 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20860 gcc_assert (ntparms > 0);
20861
20862 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20863
20864 /* Reset the number of non-defaulted template arguments contained
20865 in TARGS. */
20866 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20867
20868 again:
20869 parms = xparms;
20870 args = xargs;
20871 nargs = xnargs;
20872
20873 ia = 0;
20874 while (parms && parms != void_list_node
20875 && ia < nargs)
20876 {
20877 parm = TREE_VALUE (parms);
20878
20879 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20880 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20881 /* For a function parameter pack that occurs at the end of the
20882 parameter-declaration-list, the type A of each remaining
20883 argument of the call is compared with the type P of the
20884 declarator-id of the function parameter pack. */
20885 break;
20886
20887 parms = TREE_CHAIN (parms);
20888
20889 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20890 /* For a function parameter pack that does not occur at the
20891 end of the parameter-declaration-list, the type of the
20892 parameter pack is a non-deduced context. */
20893 continue;
20894
20895 arg = args[ia];
20896 ++ia;
20897
20898 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20899 explain_p))
20900 return 1;
20901 }
20902
20903 if (parms
20904 && parms != void_list_node
20905 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20906 {
20907 /* Unify the remaining arguments with the pack expansion type. */
20908 tree argvec;
20909 tree parmvec = make_tree_vec (1);
20910
20911 /* Allocate a TREE_VEC and copy in all of the arguments */
20912 argvec = make_tree_vec (nargs - ia);
20913 for (i = 0; ia < nargs; ++ia, ++i)
20914 TREE_VEC_ELT (argvec, i) = args[ia];
20915
20916 /* Copy the parameter into parmvec. */
20917 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20918 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20919 /*subr=*/subr, explain_p))
20920 return 1;
20921
20922 /* Advance to the end of the list of parameters. */
20923 parms = TREE_CHAIN (parms);
20924 }
20925
20926 /* Fail if we've reached the end of the parm list, and more args
20927 are present, and the parm list isn't variadic. */
20928 if (ia < nargs && parms == void_list_node)
20929 return unify_too_many_arguments (explain_p, nargs, ia);
20930 /* Fail if parms are left and they don't have default values and
20931 they aren't all deduced as empty packs (c++/57397). This is
20932 consistent with sufficient_parms_p. */
20933 if (parms && parms != void_list_node
20934 && TREE_PURPOSE (parms) == NULL_TREE)
20935 {
20936 unsigned int count = nargs;
20937 tree p = parms;
20938 bool type_pack_p;
20939 do
20940 {
20941 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20942 if (!type_pack_p)
20943 count++;
20944 p = TREE_CHAIN (p);
20945 }
20946 while (p && p != void_list_node);
20947 if (count != nargs)
20948 return unify_too_few_arguments (explain_p, ia, count,
20949 type_pack_p);
20950 }
20951
20952 if (!subr)
20953 {
20954 tsubst_flags_t complain = (explain_p
20955 ? tf_warning_or_error
20956 : tf_none);
20957 bool tried_array_deduction = (cxx_dialect < cxx17);
20958
20959 for (i = 0; i < ntparms; i++)
20960 {
20961 tree targ = TREE_VEC_ELT (targs, i);
20962 tree tparm = TREE_VEC_ELT (tparms, i);
20963
20964 /* Clear the "incomplete" flags on all argument packs now so that
20965 substituting them into later default arguments works. */
20966 if (targ && ARGUMENT_PACK_P (targ))
20967 {
20968 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20969 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20970 }
20971
20972 if (targ || tparm == error_mark_node)
20973 continue;
20974 tparm = TREE_VALUE (tparm);
20975
20976 if (TREE_CODE (tparm) == TYPE_DECL
20977 && !tried_array_deduction)
20978 {
20979 try_array_deduction (tparms, targs, xparms);
20980 tried_array_deduction = true;
20981 if (TREE_VEC_ELT (targs, i))
20982 continue;
20983 }
20984
20985 /* If this is an undeduced nontype parameter that depends on
20986 a type parameter, try another pass; its type may have been
20987 deduced from a later argument than the one from which
20988 this parameter can be deduced. */
20989 if (TREE_CODE (tparm) == PARM_DECL
20990 && uses_template_parms (TREE_TYPE (tparm))
20991 && saw_undeduced < 2)
20992 {
20993 saw_undeduced = 1;
20994 continue;
20995 }
20996
20997 /* Core issue #226 (C++0x) [temp.deduct]:
20998
20999 If a template argument has not been deduced, its
21000 default template argument, if any, is used.
21001
21002 When we are in C++98 mode, TREE_PURPOSE will either
21003 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21004 to explicitly check cxx_dialect here. */
21005 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21006 /* OK, there is a default argument. Wait until after the
21007 conversion check to do substitution. */
21008 continue;
21009
21010 /* If the type parameter is a parameter pack, then it will
21011 be deduced to an empty parameter pack. */
21012 if (template_parameter_pack_p (tparm))
21013 {
21014 tree arg;
21015
21016 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21017 {
21018 arg = make_node (NONTYPE_ARGUMENT_PACK);
21019 TREE_CONSTANT (arg) = 1;
21020 }
21021 else
21022 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21023
21024 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21025
21026 TREE_VEC_ELT (targs, i) = arg;
21027 continue;
21028 }
21029
21030 return unify_parameter_deduction_failure (explain_p, tparm);
21031 }
21032
21033 /* Now substitute into the default template arguments. */
21034 for (i = 0; i < ntparms; i++)
21035 {
21036 tree targ = TREE_VEC_ELT (targs, i);
21037 tree tparm = TREE_VEC_ELT (tparms, i);
21038
21039 if (targ || tparm == error_mark_node)
21040 continue;
21041 tree parm = TREE_VALUE (tparm);
21042 tree arg = TREE_PURPOSE (tparm);
21043 reopen_deferring_access_checks (*checks);
21044 location_t save_loc = input_location;
21045 if (DECL_P (parm))
21046 input_location = DECL_SOURCE_LOCATION (parm);
21047
21048 if (saw_undeduced == 1
21049 && TREE_CODE (parm) == PARM_DECL
21050 && uses_template_parms (TREE_TYPE (parm)))
21051 {
21052 /* The type of this non-type parameter depends on undeduced
21053 parameters. Don't try to use its default argument yet,
21054 since we might deduce an argument for it on the next pass,
21055 but do check whether the arguments we already have cause
21056 substitution failure, so that that happens before we try
21057 later default arguments (78489). */
21058 ++processing_template_decl;
21059 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21060 NULL_TREE);
21061 --processing_template_decl;
21062 if (type == error_mark_node)
21063 arg = error_mark_node;
21064 else
21065 arg = NULL_TREE;
21066 }
21067 else
21068 {
21069 /* Even if the call is happening in template context, getting
21070 here means it's non-dependent, and a default argument is
21071 considered a separate definition under [temp.decls], so we can
21072 do this substitution without processing_template_decl. This
21073 is important if the default argument contains something that
21074 might be instantiation-dependent like access (87480). */
21075 processing_template_decl_sentinel s;
21076 tree substed = NULL_TREE;
21077 if (saw_undeduced == 1)
21078 {
21079 /* First instatiate in template context, in case we still
21080 depend on undeduced template parameters. */
21081 ++processing_template_decl;
21082 substed = tsubst_template_arg (arg, full_targs, complain,
21083 NULL_TREE);
21084 --processing_template_decl;
21085 if (substed != error_mark_node
21086 && !uses_template_parms (substed))
21087 /* We replaced all the tparms, substitute again out of
21088 template context. */
21089 substed = NULL_TREE;
21090 }
21091 if (!substed)
21092 substed = tsubst_template_arg (arg, full_targs, complain,
21093 NULL_TREE);
21094
21095 if (!uses_template_parms (substed))
21096 arg = convert_template_argument (parm, substed, full_targs,
21097 complain, i, NULL_TREE);
21098 else if (saw_undeduced == 1)
21099 arg = NULL_TREE;
21100 else
21101 arg = error_mark_node;
21102 }
21103
21104 input_location = save_loc;
21105 *checks = get_deferred_access_checks ();
21106 pop_deferring_access_checks ();
21107
21108 if (arg == error_mark_node)
21109 return 1;
21110 else if (arg)
21111 {
21112 TREE_VEC_ELT (targs, i) = arg;
21113 /* The position of the first default template argument,
21114 is also the number of non-defaulted arguments in TARGS.
21115 Record that. */
21116 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21117 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21118 }
21119 }
21120
21121 if (saw_undeduced++ == 1)
21122 goto again;
21123 }
21124
21125 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21126 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21127
21128 return unify_success (explain_p);
21129 }
21130
21131 /* Subroutine of type_unification_real. Args are like the variables
21132 at the call site. ARG is an overloaded function (or template-id);
21133 we try deducing template args from each of the overloads, and if
21134 only one succeeds, we go with that. Modifies TARGS and returns
21135 true on success. */
21136
21137 static bool
21138 resolve_overloaded_unification (tree tparms,
21139 tree targs,
21140 tree parm,
21141 tree arg,
21142 unification_kind_t strict,
21143 int sub_strict,
21144 bool explain_p)
21145 {
21146 tree tempargs = copy_node (targs);
21147 int good = 0;
21148 tree goodfn = NULL_TREE;
21149 bool addr_p;
21150
21151 if (TREE_CODE (arg) == ADDR_EXPR)
21152 {
21153 arg = TREE_OPERAND (arg, 0);
21154 addr_p = true;
21155 }
21156 else
21157 addr_p = false;
21158
21159 if (TREE_CODE (arg) == COMPONENT_REF)
21160 /* Handle `&x' where `x' is some static or non-static member
21161 function name. */
21162 arg = TREE_OPERAND (arg, 1);
21163
21164 if (TREE_CODE (arg) == OFFSET_REF)
21165 arg = TREE_OPERAND (arg, 1);
21166
21167 /* Strip baselink information. */
21168 if (BASELINK_P (arg))
21169 arg = BASELINK_FUNCTIONS (arg);
21170
21171 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21172 {
21173 /* If we got some explicit template args, we need to plug them into
21174 the affected templates before we try to unify, in case the
21175 explicit args will completely resolve the templates in question. */
21176
21177 int ok = 0;
21178 tree expl_subargs = TREE_OPERAND (arg, 1);
21179 arg = TREE_OPERAND (arg, 0);
21180
21181 for (lkp_iterator iter (arg); iter; ++iter)
21182 {
21183 tree fn = *iter;
21184 tree subargs, elem;
21185
21186 if (TREE_CODE (fn) != TEMPLATE_DECL)
21187 continue;
21188
21189 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21190 expl_subargs, NULL_TREE, tf_none,
21191 /*require_all_args=*/true,
21192 /*use_default_args=*/true);
21193 if (subargs != error_mark_node
21194 && !any_dependent_template_arguments_p (subargs))
21195 {
21196 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21197 if (try_one_overload (tparms, targs, tempargs, parm,
21198 elem, strict, sub_strict, addr_p, explain_p)
21199 && (!goodfn || !same_type_p (goodfn, elem)))
21200 {
21201 goodfn = elem;
21202 ++good;
21203 }
21204 }
21205 else if (subargs)
21206 ++ok;
21207 }
21208 /* If no templates (or more than one) are fully resolved by the
21209 explicit arguments, this template-id is a non-deduced context; it
21210 could still be OK if we deduce all template arguments for the
21211 enclosing call through other arguments. */
21212 if (good != 1)
21213 good = ok;
21214 }
21215 else if (TREE_CODE (arg) != OVERLOAD
21216 && TREE_CODE (arg) != FUNCTION_DECL)
21217 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21218 -- but the deduction does not succeed because the expression is
21219 not just the function on its own. */
21220 return false;
21221 else
21222 for (lkp_iterator iter (arg); iter; ++iter)
21223 {
21224 tree fn = *iter;
21225 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21226 strict, sub_strict, addr_p, explain_p)
21227 && (!goodfn || !decls_match (goodfn, fn)))
21228 {
21229 goodfn = fn;
21230 ++good;
21231 }
21232 }
21233
21234 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21235 to function or pointer to member function argument if the set of
21236 overloaded functions does not contain function templates and at most
21237 one of a set of overloaded functions provides a unique match.
21238
21239 So if we found multiple possibilities, we return success but don't
21240 deduce anything. */
21241
21242 if (good == 1)
21243 {
21244 int i = TREE_VEC_LENGTH (targs);
21245 for (; i--; )
21246 if (TREE_VEC_ELT (tempargs, i))
21247 {
21248 tree old = TREE_VEC_ELT (targs, i);
21249 tree new_ = TREE_VEC_ELT (tempargs, i);
21250 if (new_ && old && ARGUMENT_PACK_P (old)
21251 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21252 /* Don't forget explicit template arguments in a pack. */
21253 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21254 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21255 TREE_VEC_ELT (targs, i) = new_;
21256 }
21257 }
21258 if (good)
21259 return true;
21260
21261 return false;
21262 }
21263
21264 /* Core DR 115: In contexts where deduction is done and fails, or in
21265 contexts where deduction is not done, if a template argument list is
21266 specified and it, along with any default template arguments, identifies
21267 a single function template specialization, then the template-id is an
21268 lvalue for the function template specialization. */
21269
21270 tree
21271 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21272 {
21273 tree expr, offset, baselink;
21274 bool addr;
21275
21276 if (!type_unknown_p (orig_expr))
21277 return orig_expr;
21278
21279 expr = orig_expr;
21280 addr = false;
21281 offset = NULL_TREE;
21282 baselink = NULL_TREE;
21283
21284 if (TREE_CODE (expr) == ADDR_EXPR)
21285 {
21286 expr = TREE_OPERAND (expr, 0);
21287 addr = true;
21288 }
21289 if (TREE_CODE (expr) == OFFSET_REF)
21290 {
21291 offset = expr;
21292 expr = TREE_OPERAND (expr, 1);
21293 }
21294 if (BASELINK_P (expr))
21295 {
21296 baselink = expr;
21297 expr = BASELINK_FUNCTIONS (expr);
21298 }
21299
21300 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21301 {
21302 int good = 0;
21303 tree goodfn = NULL_TREE;
21304
21305 /* If we got some explicit template args, we need to plug them into
21306 the affected templates before we try to unify, in case the
21307 explicit args will completely resolve the templates in question. */
21308
21309 tree expl_subargs = TREE_OPERAND (expr, 1);
21310 tree arg = TREE_OPERAND (expr, 0);
21311 tree badfn = NULL_TREE;
21312 tree badargs = NULL_TREE;
21313
21314 for (lkp_iterator iter (arg); iter; ++iter)
21315 {
21316 tree fn = *iter;
21317 tree subargs, elem;
21318
21319 if (TREE_CODE (fn) != TEMPLATE_DECL)
21320 continue;
21321
21322 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21323 expl_subargs, NULL_TREE, tf_none,
21324 /*require_all_args=*/true,
21325 /*use_default_args=*/true);
21326 if (subargs != error_mark_node
21327 && !any_dependent_template_arguments_p (subargs))
21328 {
21329 elem = instantiate_template (fn, subargs, tf_none);
21330 if (elem == error_mark_node)
21331 {
21332 badfn = fn;
21333 badargs = subargs;
21334 }
21335 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21336 {
21337 goodfn = elem;
21338 ++good;
21339 }
21340 }
21341 }
21342 if (good == 1)
21343 {
21344 mark_used (goodfn);
21345 expr = goodfn;
21346 if (baselink)
21347 expr = build_baselink (BASELINK_BINFO (baselink),
21348 BASELINK_ACCESS_BINFO (baselink),
21349 expr, BASELINK_OPTYPE (baselink));
21350 if (offset)
21351 {
21352 tree base
21353 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21354 expr = build_offset_ref (base, expr, addr, complain);
21355 }
21356 if (addr)
21357 expr = cp_build_addr_expr (expr, complain);
21358 return expr;
21359 }
21360 else if (good == 0 && badargs && (complain & tf_error))
21361 /* There were no good options and at least one bad one, so let the
21362 user know what the problem is. */
21363 instantiate_template (badfn, badargs, complain);
21364 }
21365 return orig_expr;
21366 }
21367
21368 /* As above, but error out if the expression remains overloaded. */
21369
21370 tree
21371 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21372 {
21373 exp = resolve_nondeduced_context (exp, complain);
21374 if (type_unknown_p (exp))
21375 {
21376 if (complain & tf_error)
21377 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21378 return error_mark_node;
21379 }
21380 return exp;
21381 }
21382
21383 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21384 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21385 different overloads deduce different arguments for a given parm.
21386 ADDR_P is true if the expression for which deduction is being
21387 performed was of the form "& fn" rather than simply "fn".
21388
21389 Returns 1 on success. */
21390
21391 static int
21392 try_one_overload (tree tparms,
21393 tree orig_targs,
21394 tree targs,
21395 tree parm,
21396 tree arg,
21397 unification_kind_t strict,
21398 int sub_strict,
21399 bool addr_p,
21400 bool explain_p)
21401 {
21402 int nargs;
21403 tree tempargs;
21404 int i;
21405
21406 if (arg == error_mark_node)
21407 return 0;
21408
21409 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21410 to function or pointer to member function argument if the set of
21411 overloaded functions does not contain function templates and at most
21412 one of a set of overloaded functions provides a unique match.
21413
21414 So if this is a template, just return success. */
21415
21416 if (uses_template_parms (arg))
21417 return 1;
21418
21419 if (TREE_CODE (arg) == METHOD_TYPE)
21420 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21421 else if (addr_p)
21422 arg = build_pointer_type (arg);
21423
21424 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21425
21426 /* We don't copy orig_targs for this because if we have already deduced
21427 some template args from previous args, unify would complain when we
21428 try to deduce a template parameter for the same argument, even though
21429 there isn't really a conflict. */
21430 nargs = TREE_VEC_LENGTH (targs);
21431 tempargs = make_tree_vec (nargs);
21432
21433 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21434 return 0;
21435
21436 /* First make sure we didn't deduce anything that conflicts with
21437 explicitly specified args. */
21438 for (i = nargs; i--; )
21439 {
21440 tree elt = TREE_VEC_ELT (tempargs, i);
21441 tree oldelt = TREE_VEC_ELT (orig_targs, i);
21442
21443 if (!elt)
21444 /*NOP*/;
21445 else if (uses_template_parms (elt))
21446 /* Since we're unifying against ourselves, we will fill in
21447 template args used in the function parm list with our own
21448 template parms. Discard them. */
21449 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
21450 else if (oldelt && ARGUMENT_PACK_P (oldelt))
21451 {
21452 /* Check that the argument at each index of the deduced argument pack
21453 is equivalent to the corresponding explicitly specified argument.
21454 We may have deduced more arguments than were explicitly specified,
21455 and that's OK. */
21456
21457 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21458 that's wrong if we deduce the same argument pack from multiple
21459 function arguments: it's only incomplete the first time. */
21460
21461 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
21462 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
21463
21464 if (TREE_VEC_LENGTH (deduced_pack)
21465 < TREE_VEC_LENGTH (explicit_pack))
21466 return 0;
21467
21468 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
21469 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
21470 TREE_VEC_ELT (deduced_pack, j)))
21471 return 0;
21472 }
21473 else if (oldelt && !template_args_equal (oldelt, elt))
21474 return 0;
21475 }
21476
21477 for (i = nargs; i--; )
21478 {
21479 tree elt = TREE_VEC_ELT (tempargs, i);
21480
21481 if (elt)
21482 TREE_VEC_ELT (targs, i) = elt;
21483 }
21484
21485 return 1;
21486 }
21487
21488 /* PARM is a template class (perhaps with unbound template
21489 parameters). ARG is a fully instantiated type. If ARG can be
21490 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21491 TARGS are as for unify. */
21492
21493 static tree
21494 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21495 bool explain_p)
21496 {
21497 tree copy_of_targs;
21498
21499 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21500 return NULL_TREE;
21501 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21502 /* Matches anything. */;
21503 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21504 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21505 return NULL_TREE;
21506
21507 /* We need to make a new template argument vector for the call to
21508 unify. If we used TARGS, we'd clutter it up with the result of
21509 the attempted unification, even if this class didn't work out.
21510 We also don't want to commit ourselves to all the unifications
21511 we've already done, since unification is supposed to be done on
21512 an argument-by-argument basis. In other words, consider the
21513 following pathological case:
21514
21515 template <int I, int J, int K>
21516 struct S {};
21517
21518 template <int I, int J>
21519 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21520
21521 template <int I, int J, int K>
21522 void f(S<I, J, K>, S<I, I, I>);
21523
21524 void g() {
21525 S<0, 0, 0> s0;
21526 S<0, 1, 2> s2;
21527
21528 f(s0, s2);
21529 }
21530
21531 Now, by the time we consider the unification involving `s2', we
21532 already know that we must have `f<0, 0, 0>'. But, even though
21533 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21534 because there are two ways to unify base classes of S<0, 1, 2>
21535 with S<I, I, I>. If we kept the already deduced knowledge, we
21536 would reject the possibility I=1. */
21537 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21538
21539 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21540 {
21541 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21542 return NULL_TREE;
21543 return arg;
21544 }
21545
21546 /* If unification failed, we're done. */
21547 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21548 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21549 return NULL_TREE;
21550
21551 return arg;
21552 }
21553
21554 /* Given a template type PARM and a class type ARG, find the unique
21555 base type in ARG that is an instance of PARM. We do not examine
21556 ARG itself; only its base-classes. If there is not exactly one
21557 appropriate base class, return NULL_TREE. PARM may be the type of
21558 a partial specialization, as well as a plain template type. Used
21559 by unify. */
21560
21561 static enum template_base_result
21562 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21563 bool explain_p, tree *result)
21564 {
21565 tree rval = NULL_TREE;
21566 tree binfo;
21567
21568 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21569
21570 binfo = TYPE_BINFO (complete_type (arg));
21571 if (!binfo)
21572 {
21573 /* The type could not be completed. */
21574 *result = NULL_TREE;
21575 return tbr_incomplete_type;
21576 }
21577
21578 /* Walk in inheritance graph order. The search order is not
21579 important, and this avoids multiple walks of virtual bases. */
21580 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21581 {
21582 tree r = try_class_unification (tparms, targs, parm,
21583 BINFO_TYPE (binfo), explain_p);
21584
21585 if (r)
21586 {
21587 /* If there is more than one satisfactory baseclass, then:
21588
21589 [temp.deduct.call]
21590
21591 If they yield more than one possible deduced A, the type
21592 deduction fails.
21593
21594 applies. */
21595 if (rval && !same_type_p (r, rval))
21596 {
21597 *result = NULL_TREE;
21598 return tbr_ambiguous_baseclass;
21599 }
21600
21601 rval = r;
21602 }
21603 }
21604
21605 *result = rval;
21606 return tbr_success;
21607 }
21608
21609 /* Returns the level of DECL, which declares a template parameter. */
21610
21611 static int
21612 template_decl_level (tree decl)
21613 {
21614 switch (TREE_CODE (decl))
21615 {
21616 case TYPE_DECL:
21617 case TEMPLATE_DECL:
21618 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21619
21620 case PARM_DECL:
21621 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21622
21623 default:
21624 gcc_unreachable ();
21625 }
21626 return 0;
21627 }
21628
21629 /* Decide whether ARG can be unified with PARM, considering only the
21630 cv-qualifiers of each type, given STRICT as documented for unify.
21631 Returns nonzero iff the unification is OK on that basis. */
21632
21633 static int
21634 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21635 {
21636 int arg_quals = cp_type_quals (arg);
21637 int parm_quals = cp_type_quals (parm);
21638
21639 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21640 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21641 {
21642 /* Although a CVR qualifier is ignored when being applied to a
21643 substituted template parameter ([8.3.2]/1 for example), that
21644 does not allow us to unify "const T" with "int&" because both
21645 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21646 It is ok when we're allowing additional CV qualifiers
21647 at the outer level [14.8.2.1]/3,1st bullet. */
21648 if ((TYPE_REF_P (arg)
21649 || TREE_CODE (arg) == FUNCTION_TYPE
21650 || TREE_CODE (arg) == METHOD_TYPE)
21651 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21652 return 0;
21653
21654 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21655 && (parm_quals & TYPE_QUAL_RESTRICT))
21656 return 0;
21657 }
21658
21659 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21660 && (arg_quals & parm_quals) != parm_quals)
21661 return 0;
21662
21663 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21664 && (parm_quals & arg_quals) != arg_quals)
21665 return 0;
21666
21667 return 1;
21668 }
21669
21670 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21671 void
21672 template_parm_level_and_index (tree parm, int* level, int* index)
21673 {
21674 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21675 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21676 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21677 {
21678 *index = TEMPLATE_TYPE_IDX (parm);
21679 *level = TEMPLATE_TYPE_LEVEL (parm);
21680 }
21681 else
21682 {
21683 *index = TEMPLATE_PARM_IDX (parm);
21684 *level = TEMPLATE_PARM_LEVEL (parm);
21685 }
21686 }
21687
21688 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21689 do { \
21690 if (unify (TP, TA, P, A, S, EP)) \
21691 return 1; \
21692 } while (0)
21693
21694 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21695 expansion at the end of PACKED_PARMS. Returns 0 if the type
21696 deduction succeeds, 1 otherwise. STRICT is the same as in
21697 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21698 function call argument list. We'll need to adjust the arguments to make them
21699 types. SUBR tells us if this is from a recursive call to
21700 type_unification_real, or for comparing two template argument
21701 lists. */
21702
21703 static int
21704 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21705 tree packed_args, unification_kind_t strict,
21706 bool subr, bool explain_p)
21707 {
21708 tree parm
21709 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21710 tree pattern = PACK_EXPANSION_PATTERN (parm);
21711 tree pack, packs = NULL_TREE;
21712 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21713
21714 /* Add in any args remembered from an earlier partial instantiation. */
21715 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21716 int levels = TMPL_ARGS_DEPTH (targs);
21717
21718 packed_args = expand_template_argument_pack (packed_args);
21719
21720 int len = TREE_VEC_LENGTH (packed_args);
21721
21722 /* Determine the parameter packs we will be deducing from the
21723 pattern, and record their current deductions. */
21724 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21725 pack; pack = TREE_CHAIN (pack))
21726 {
21727 tree parm_pack = TREE_VALUE (pack);
21728 int idx, level;
21729
21730 /* Only template parameter packs can be deduced, not e.g. function
21731 parameter packs or __bases or __integer_pack. */
21732 if (!TEMPLATE_PARM_P (parm_pack))
21733 continue;
21734
21735 /* Determine the index and level of this parameter pack. */
21736 template_parm_level_and_index (parm_pack, &level, &idx);
21737 if (level < levels)
21738 continue;
21739
21740 /* Keep track of the parameter packs and their corresponding
21741 argument packs. */
21742 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21743 TREE_TYPE (packs) = make_tree_vec (len - start);
21744 }
21745
21746 /* Loop through all of the arguments that have not yet been
21747 unified and unify each with the pattern. */
21748 for (i = start; i < len; i++)
21749 {
21750 tree parm;
21751 bool any_explicit = false;
21752 tree arg = TREE_VEC_ELT (packed_args, i);
21753
21754 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21755 or the element of its argument pack at the current index if
21756 this argument was explicitly specified. */
21757 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21758 {
21759 int idx, level;
21760 tree arg, pargs;
21761 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21762
21763 arg = NULL_TREE;
21764 if (TREE_VALUE (pack)
21765 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21766 && (i - start < TREE_VEC_LENGTH (pargs)))
21767 {
21768 any_explicit = true;
21769 arg = TREE_VEC_ELT (pargs, i - start);
21770 }
21771 TMPL_ARG (targs, level, idx) = arg;
21772 }
21773
21774 /* If we had explicit template arguments, substitute them into the
21775 pattern before deduction. */
21776 if (any_explicit)
21777 {
21778 /* Some arguments might still be unspecified or dependent. */
21779 bool dependent;
21780 ++processing_template_decl;
21781 dependent = any_dependent_template_arguments_p (targs);
21782 if (!dependent)
21783 --processing_template_decl;
21784 parm = tsubst (pattern, targs,
21785 explain_p ? tf_warning_or_error : tf_none,
21786 NULL_TREE);
21787 if (dependent)
21788 --processing_template_decl;
21789 if (parm == error_mark_node)
21790 return 1;
21791 }
21792 else
21793 parm = pattern;
21794
21795 /* Unify the pattern with the current argument. */
21796 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21797 explain_p))
21798 return 1;
21799
21800 /* For each parameter pack, collect the deduced value. */
21801 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21802 {
21803 int idx, level;
21804 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21805
21806 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21807 TMPL_ARG (targs, level, idx);
21808 }
21809 }
21810
21811 /* Verify that the results of unification with the parameter packs
21812 produce results consistent with what we've seen before, and make
21813 the deduced argument packs available. */
21814 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21815 {
21816 tree old_pack = TREE_VALUE (pack);
21817 tree new_args = TREE_TYPE (pack);
21818 int i, len = TREE_VEC_LENGTH (new_args);
21819 int idx, level;
21820 bool nondeduced_p = false;
21821
21822 /* By default keep the original deduced argument pack.
21823 If necessary, more specific code is going to update the
21824 resulting deduced argument later down in this function. */
21825 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21826 TMPL_ARG (targs, level, idx) = old_pack;
21827
21828 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21829 actually deduce anything. */
21830 for (i = 0; i < len && !nondeduced_p; ++i)
21831 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21832 nondeduced_p = true;
21833 if (nondeduced_p)
21834 continue;
21835
21836 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21837 {
21838 /* If we had fewer function args than explicit template args,
21839 just use the explicits. */
21840 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21841 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21842 if (len < explicit_len)
21843 new_args = explicit_args;
21844 }
21845
21846 if (!old_pack)
21847 {
21848 tree result;
21849 /* Build the deduced *_ARGUMENT_PACK. */
21850 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21851 {
21852 result = make_node (NONTYPE_ARGUMENT_PACK);
21853 TREE_CONSTANT (result) = 1;
21854 }
21855 else
21856 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21857
21858 SET_ARGUMENT_PACK_ARGS (result, new_args);
21859
21860 /* Note the deduced argument packs for this parameter
21861 pack. */
21862 TMPL_ARG (targs, level, idx) = result;
21863 }
21864 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21865 && (ARGUMENT_PACK_ARGS (old_pack)
21866 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21867 {
21868 /* We only had the explicitly-provided arguments before, but
21869 now we have a complete set of arguments. */
21870 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21871
21872 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21873 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21874 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21875 }
21876 else
21877 {
21878 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21879 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21880
21881 if (!comp_template_args (old_args, new_args,
21882 &bad_old_arg, &bad_new_arg))
21883 /* Inconsistent unification of this parameter pack. */
21884 return unify_parameter_pack_inconsistent (explain_p,
21885 bad_old_arg,
21886 bad_new_arg);
21887 }
21888 }
21889
21890 return unify_success (explain_p);
21891 }
21892
21893 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21894 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21895 parameters and return value are as for unify. */
21896
21897 static int
21898 unify_array_domain (tree tparms, tree targs,
21899 tree parm_dom, tree arg_dom,
21900 bool explain_p)
21901 {
21902 tree parm_max;
21903 tree arg_max;
21904 bool parm_cst;
21905 bool arg_cst;
21906
21907 /* Our representation of array types uses "N - 1" as the
21908 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21909 not an integer constant. We cannot unify arbitrarily
21910 complex expressions, so we eliminate the MINUS_EXPRs
21911 here. */
21912 parm_max = TYPE_MAX_VALUE (parm_dom);
21913 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21914 if (!parm_cst)
21915 {
21916 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21917 parm_max = TREE_OPERAND (parm_max, 0);
21918 }
21919 arg_max = TYPE_MAX_VALUE (arg_dom);
21920 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21921 if (!arg_cst)
21922 {
21923 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21924 trying to unify the type of a variable with the type
21925 of a template parameter. For example:
21926
21927 template <unsigned int N>
21928 void f (char (&) [N]);
21929 int g();
21930 void h(int i) {
21931 char a[g(i)];
21932 f(a);
21933 }
21934
21935 Here, the type of the ARG will be "int [g(i)]", and
21936 may be a SAVE_EXPR, etc. */
21937 if (TREE_CODE (arg_max) != MINUS_EXPR)
21938 return unify_vla_arg (explain_p, arg_dom);
21939 arg_max = TREE_OPERAND (arg_max, 0);
21940 }
21941
21942 /* If only one of the bounds used a MINUS_EXPR, compensate
21943 by adding one to the other bound. */
21944 if (parm_cst && !arg_cst)
21945 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21946 integer_type_node,
21947 parm_max,
21948 integer_one_node);
21949 else if (arg_cst && !parm_cst)
21950 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21951 integer_type_node,
21952 arg_max,
21953 integer_one_node);
21954
21955 return unify (tparms, targs, parm_max, arg_max,
21956 UNIFY_ALLOW_INTEGER, explain_p);
21957 }
21958
21959 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21960
21961 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21962
21963 static pa_kind_t
21964 pa_kind (tree t)
21965 {
21966 if (PACK_EXPANSION_P (t))
21967 t = PACK_EXPANSION_PATTERN (t);
21968 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21969 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21970 || DECL_TYPE_TEMPLATE_P (t))
21971 return pa_tmpl;
21972 else if (TYPE_P (t))
21973 return pa_type;
21974 else
21975 return pa_expr;
21976 }
21977
21978 /* Deduce the value of template parameters. TPARMS is the (innermost)
21979 set of template parameters to a template. TARGS is the bindings
21980 for those template parameters, as determined thus far; TARGS may
21981 include template arguments for outer levels of template parameters
21982 as well. PARM is a parameter to a template function, or a
21983 subcomponent of that parameter; ARG is the corresponding argument.
21984 This function attempts to match PARM with ARG in a manner
21985 consistent with the existing assignments in TARGS. If more values
21986 are deduced, then TARGS is updated.
21987
21988 Returns 0 if the type deduction succeeds, 1 otherwise. The
21989 parameter STRICT is a bitwise or of the following flags:
21990
21991 UNIFY_ALLOW_NONE:
21992 Require an exact match between PARM and ARG.
21993 UNIFY_ALLOW_MORE_CV_QUAL:
21994 Allow the deduced ARG to be more cv-qualified (by qualification
21995 conversion) than ARG.
21996 UNIFY_ALLOW_LESS_CV_QUAL:
21997 Allow the deduced ARG to be less cv-qualified than ARG.
21998 UNIFY_ALLOW_DERIVED:
21999 Allow the deduced ARG to be a template base class of ARG,
22000 or a pointer to a template base class of the type pointed to by
22001 ARG.
22002 UNIFY_ALLOW_INTEGER:
22003 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22004 case for more information.
22005 UNIFY_ALLOW_OUTER_LEVEL:
22006 This is the outermost level of a deduction. Used to determine validity
22007 of qualification conversions. A valid qualification conversion must
22008 have const qualified pointers leading up to the inner type which
22009 requires additional CV quals, except at the outer level, where const
22010 is not required [conv.qual]. It would be normal to set this flag in
22011 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22012 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22013 This is the outermost level of a deduction, and PARM can be more CV
22014 qualified at this point.
22015 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22016 This is the outermost level of a deduction, and PARM can be less CV
22017 qualified at this point. */
22018
22019 static int
22020 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22021 bool explain_p)
22022 {
22023 int idx;
22024 tree targ;
22025 tree tparm;
22026 int strict_in = strict;
22027 tsubst_flags_t complain = (explain_p
22028 ? tf_warning_or_error
22029 : tf_none);
22030
22031 /* I don't think this will do the right thing with respect to types.
22032 But the only case I've seen it in so far has been array bounds, where
22033 signedness is the only information lost, and I think that will be
22034 okay. */
22035 while (CONVERT_EXPR_P (parm))
22036 parm = TREE_OPERAND (parm, 0);
22037
22038 if (arg == error_mark_node)
22039 return unify_invalid (explain_p);
22040 if (arg == unknown_type_node
22041 || arg == init_list_type_node)
22042 /* We can't deduce anything from this, but we might get all the
22043 template args from other function args. */
22044 return unify_success (explain_p);
22045
22046 if (parm == any_targ_node || arg == any_targ_node)
22047 return unify_success (explain_p);
22048
22049 /* If PARM uses template parameters, then we can't bail out here,
22050 even if ARG == PARM, since we won't record unifications for the
22051 template parameters. We might need them if we're trying to
22052 figure out which of two things is more specialized. */
22053 if (arg == parm && !uses_template_parms (parm))
22054 return unify_success (explain_p);
22055
22056 /* Handle init lists early, so the rest of the function can assume
22057 we're dealing with a type. */
22058 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22059 {
22060 tree elt, elttype;
22061 unsigned i;
22062 tree orig_parm = parm;
22063
22064 /* Replace T with std::initializer_list<T> for deduction. */
22065 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22066 && flag_deduce_init_list)
22067 parm = listify (parm);
22068
22069 if (!is_std_init_list (parm)
22070 && TREE_CODE (parm) != ARRAY_TYPE)
22071 /* We can only deduce from an initializer list argument if the
22072 parameter is std::initializer_list or an array; otherwise this
22073 is a non-deduced context. */
22074 return unify_success (explain_p);
22075
22076 if (TREE_CODE (parm) == ARRAY_TYPE)
22077 elttype = TREE_TYPE (parm);
22078 else
22079 {
22080 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22081 /* Deduction is defined in terms of a single type, so just punt
22082 on the (bizarre) std::initializer_list<T...>. */
22083 if (PACK_EXPANSION_P (elttype))
22084 return unify_success (explain_p);
22085 }
22086
22087 if (strict != DEDUCE_EXACT
22088 && TYPE_P (elttype)
22089 && !uses_deducible_template_parms (elttype))
22090 /* If ELTTYPE has no deducible template parms, skip deduction from
22091 the list elements. */;
22092 else
22093 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22094 {
22095 int elt_strict = strict;
22096
22097 if (elt == error_mark_node)
22098 return unify_invalid (explain_p);
22099
22100 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22101 {
22102 tree type = TREE_TYPE (elt);
22103 if (type == error_mark_node)
22104 return unify_invalid (explain_p);
22105 /* It should only be possible to get here for a call. */
22106 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22107 elt_strict |= maybe_adjust_types_for_deduction
22108 (DEDUCE_CALL, &elttype, &type, elt);
22109 elt = type;
22110 }
22111
22112 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22113 explain_p);
22114 }
22115
22116 if (TREE_CODE (parm) == ARRAY_TYPE
22117 && deducible_array_bound (TYPE_DOMAIN (parm)))
22118 {
22119 /* Also deduce from the length of the initializer list. */
22120 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22121 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22122 if (idx == error_mark_node)
22123 return unify_invalid (explain_p);
22124 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22125 idx, explain_p);
22126 }
22127
22128 /* If the std::initializer_list<T> deduction worked, replace the
22129 deduced A with std::initializer_list<A>. */
22130 if (orig_parm != parm)
22131 {
22132 idx = TEMPLATE_TYPE_IDX (orig_parm);
22133 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22134 targ = listify (targ);
22135 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22136 }
22137 return unify_success (explain_p);
22138 }
22139
22140 /* If parm and arg aren't the same kind of thing (template, type, or
22141 expression), fail early. */
22142 if (pa_kind (parm) != pa_kind (arg))
22143 return unify_invalid (explain_p);
22144
22145 /* Immediately reject some pairs that won't unify because of
22146 cv-qualification mismatches. */
22147 if (TREE_CODE (arg) == TREE_CODE (parm)
22148 && TYPE_P (arg)
22149 /* It is the elements of the array which hold the cv quals of an array
22150 type, and the elements might be template type parms. We'll check
22151 when we recurse. */
22152 && TREE_CODE (arg) != ARRAY_TYPE
22153 /* We check the cv-qualifiers when unifying with template type
22154 parameters below. We want to allow ARG `const T' to unify with
22155 PARM `T' for example, when computing which of two templates
22156 is more specialized, for example. */
22157 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22158 && !check_cv_quals_for_unify (strict_in, arg, parm))
22159 return unify_cv_qual_mismatch (explain_p, parm, arg);
22160
22161 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22162 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22163 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22164 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22165 strict &= ~UNIFY_ALLOW_DERIVED;
22166 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22167 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22168
22169 switch (TREE_CODE (parm))
22170 {
22171 case TYPENAME_TYPE:
22172 case SCOPE_REF:
22173 case UNBOUND_CLASS_TEMPLATE:
22174 /* In a type which contains a nested-name-specifier, template
22175 argument values cannot be deduced for template parameters used
22176 within the nested-name-specifier. */
22177 return unify_success (explain_p);
22178
22179 case TEMPLATE_TYPE_PARM:
22180 case TEMPLATE_TEMPLATE_PARM:
22181 case BOUND_TEMPLATE_TEMPLATE_PARM:
22182 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22183 if (error_operand_p (tparm))
22184 return unify_invalid (explain_p);
22185
22186 if (TEMPLATE_TYPE_LEVEL (parm)
22187 != template_decl_level (tparm))
22188 /* The PARM is not one we're trying to unify. Just check
22189 to see if it matches ARG. */
22190 {
22191 if (TREE_CODE (arg) == TREE_CODE (parm)
22192 && (is_auto (parm) ? is_auto (arg)
22193 : same_type_p (parm, arg)))
22194 return unify_success (explain_p);
22195 else
22196 return unify_type_mismatch (explain_p, parm, arg);
22197 }
22198 idx = TEMPLATE_TYPE_IDX (parm);
22199 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22200 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22201 if (error_operand_p (tparm))
22202 return unify_invalid (explain_p);
22203
22204 /* Check for mixed types and values. */
22205 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22206 && TREE_CODE (tparm) != TYPE_DECL)
22207 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22208 && TREE_CODE (tparm) != TEMPLATE_DECL))
22209 gcc_unreachable ();
22210
22211 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22212 {
22213 if ((strict_in & UNIFY_ALLOW_DERIVED)
22214 && CLASS_TYPE_P (arg))
22215 {
22216 /* First try to match ARG directly. */
22217 tree t = try_class_unification (tparms, targs, parm, arg,
22218 explain_p);
22219 if (!t)
22220 {
22221 /* Otherwise, look for a suitable base of ARG, as below. */
22222 enum template_base_result r;
22223 r = get_template_base (tparms, targs, parm, arg,
22224 explain_p, &t);
22225 if (!t)
22226 return unify_no_common_base (explain_p, r, parm, arg);
22227 arg = t;
22228 }
22229 }
22230 /* ARG must be constructed from a template class or a template
22231 template parameter. */
22232 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22233 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22234 return unify_template_deduction_failure (explain_p, parm, arg);
22235
22236 /* Deduce arguments T, i from TT<T> or TT<i>. */
22237 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22238 return 1;
22239
22240 arg = TYPE_TI_TEMPLATE (arg);
22241
22242 /* Fall through to deduce template name. */
22243 }
22244
22245 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22246 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22247 {
22248 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22249
22250 /* Simple cases: Value already set, does match or doesn't. */
22251 if (targ != NULL_TREE && template_args_equal (targ, arg))
22252 return unify_success (explain_p);
22253 else if (targ)
22254 return unify_inconsistency (explain_p, parm, targ, arg);
22255 }
22256 else
22257 {
22258 /* If PARM is `const T' and ARG is only `int', we don't have
22259 a match unless we are allowing additional qualification.
22260 If ARG is `const int' and PARM is just `T' that's OK;
22261 that binds `const int' to `T'. */
22262 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22263 arg, parm))
22264 return unify_cv_qual_mismatch (explain_p, parm, arg);
22265
22266 /* Consider the case where ARG is `const volatile int' and
22267 PARM is `const T'. Then, T should be `volatile int'. */
22268 arg = cp_build_qualified_type_real
22269 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22270 if (arg == error_mark_node)
22271 return unify_invalid (explain_p);
22272
22273 /* Simple cases: Value already set, does match or doesn't. */
22274 if (targ != NULL_TREE && same_type_p (targ, arg))
22275 return unify_success (explain_p);
22276 else if (targ)
22277 return unify_inconsistency (explain_p, parm, targ, arg);
22278
22279 /* Make sure that ARG is not a variable-sized array. (Note
22280 that were talking about variable-sized arrays (like
22281 `int[n]'), rather than arrays of unknown size (like
22282 `int[]').) We'll get very confused by such a type since
22283 the bound of the array is not constant, and therefore
22284 not mangleable. Besides, such types are not allowed in
22285 ISO C++, so we can do as we please here. We do allow
22286 them for 'auto' deduction, since that isn't ABI-exposed. */
22287 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22288 return unify_vla_arg (explain_p, arg);
22289
22290 /* Strip typedefs as in convert_template_argument. */
22291 arg = canonicalize_type_argument (arg, tf_none);
22292 }
22293
22294 /* If ARG is a parameter pack or an expansion, we cannot unify
22295 against it unless PARM is also a parameter pack. */
22296 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22297 && !template_parameter_pack_p (parm))
22298 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22299
22300 /* If the argument deduction results is a METHOD_TYPE,
22301 then there is a problem.
22302 METHOD_TYPE doesn't map to any real C++ type the result of
22303 the deduction cannot be of that type. */
22304 if (TREE_CODE (arg) == METHOD_TYPE)
22305 return unify_method_type_error (explain_p, arg);
22306
22307 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22308 return unify_success (explain_p);
22309
22310 case TEMPLATE_PARM_INDEX:
22311 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22312 if (error_operand_p (tparm))
22313 return unify_invalid (explain_p);
22314
22315 if (TEMPLATE_PARM_LEVEL (parm)
22316 != template_decl_level (tparm))
22317 {
22318 /* The PARM is not one we're trying to unify. Just check
22319 to see if it matches ARG. */
22320 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22321 && cp_tree_equal (parm, arg));
22322 if (result)
22323 unify_expression_unequal (explain_p, parm, arg);
22324 return result;
22325 }
22326
22327 idx = TEMPLATE_PARM_IDX (parm);
22328 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22329
22330 if (targ)
22331 {
22332 if ((strict & UNIFY_ALLOW_INTEGER)
22333 && TREE_TYPE (targ) && TREE_TYPE (arg)
22334 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22335 /* We're deducing from an array bound, the type doesn't matter. */
22336 arg = fold_convert (TREE_TYPE (targ), arg);
22337 int x = !cp_tree_equal (targ, arg);
22338 if (x)
22339 unify_inconsistency (explain_p, parm, targ, arg);
22340 return x;
22341 }
22342
22343 /* [temp.deduct.type] If, in the declaration of a function template
22344 with a non-type template-parameter, the non-type
22345 template-parameter is used in an expression in the function
22346 parameter-list and, if the corresponding template-argument is
22347 deduced, the template-argument type shall match the type of the
22348 template-parameter exactly, except that a template-argument
22349 deduced from an array bound may be of any integral type.
22350 The non-type parameter might use already deduced type parameters. */
22351 tparm = TREE_TYPE (parm);
22352 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22353 /* We don't have enough levels of args to do any substitution. This
22354 can happen in the context of -fnew-ttp-matching. */;
22355 else
22356 {
22357 ++processing_template_decl;
22358 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22359 --processing_template_decl;
22360
22361 if (tree a = type_uses_auto (tparm))
22362 {
22363 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22364 if (tparm == error_mark_node)
22365 return 1;
22366 }
22367 }
22368
22369 if (!TREE_TYPE (arg))
22370 /* Template-parameter dependent expression. Just accept it for now.
22371 It will later be processed in convert_template_argument. */
22372 ;
22373 else if (same_type_p (non_reference (TREE_TYPE (arg)),
22374 non_reference (tparm)))
22375 /* OK */;
22376 else if ((strict & UNIFY_ALLOW_INTEGER)
22377 && CP_INTEGRAL_TYPE_P (tparm))
22378 /* Convert the ARG to the type of PARM; the deduced non-type
22379 template argument must exactly match the types of the
22380 corresponding parameter. */
22381 arg = fold (build_nop (tparm, arg));
22382 else if (uses_template_parms (tparm))
22383 {
22384 /* We haven't deduced the type of this parameter yet. */
22385 if (cxx_dialect >= cxx17
22386 /* We deduce from array bounds in try_array_deduction. */
22387 && !(strict & UNIFY_ALLOW_INTEGER))
22388 {
22389 /* Deduce it from the non-type argument. */
22390 tree atype = TREE_TYPE (arg);
22391 RECUR_AND_CHECK_FAILURE (tparms, targs,
22392 tparm, atype,
22393 UNIFY_ALLOW_NONE, explain_p);
22394 }
22395 else
22396 /* Try again later. */
22397 return unify_success (explain_p);
22398 }
22399 else
22400 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22401
22402 /* If ARG is a parameter pack or an expansion, we cannot unify
22403 against it unless PARM is also a parameter pack. */
22404 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22405 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22406 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22407
22408 {
22409 bool removed_attr = false;
22410 arg = strip_typedefs_expr (arg, &removed_attr);
22411 }
22412 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22413 return unify_success (explain_p);
22414
22415 case PTRMEM_CST:
22416 {
22417 /* A pointer-to-member constant can be unified only with
22418 another constant. */
22419 if (TREE_CODE (arg) != PTRMEM_CST)
22420 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22421
22422 /* Just unify the class member. It would be useless (and possibly
22423 wrong, depending on the strict flags) to unify also
22424 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22425 arg refer to the same variable, even if through different
22426 classes. For instance:
22427
22428 struct A { int x; };
22429 struct B : A { };
22430
22431 Unification of &A::x and &B::x must succeed. */
22432 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22433 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22434 }
22435
22436 case POINTER_TYPE:
22437 {
22438 if (!TYPE_PTR_P (arg))
22439 return unify_type_mismatch (explain_p, parm, arg);
22440
22441 /* [temp.deduct.call]
22442
22443 A can be another pointer or pointer to member type that can
22444 be converted to the deduced A via a qualification
22445 conversion (_conv.qual_).
22446
22447 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22448 This will allow for additional cv-qualification of the
22449 pointed-to types if appropriate. */
22450
22451 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
22452 /* The derived-to-base conversion only persists through one
22453 level of pointers. */
22454 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
22455
22456 return unify (tparms, targs, TREE_TYPE (parm),
22457 TREE_TYPE (arg), strict, explain_p);
22458 }
22459
22460 case REFERENCE_TYPE:
22461 if (!TYPE_REF_P (arg))
22462 return unify_type_mismatch (explain_p, parm, arg);
22463 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22464 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22465
22466 case ARRAY_TYPE:
22467 if (TREE_CODE (arg) != ARRAY_TYPE)
22468 return unify_type_mismatch (explain_p, parm, arg);
22469 if ((TYPE_DOMAIN (parm) == NULL_TREE)
22470 != (TYPE_DOMAIN (arg) == NULL_TREE))
22471 return unify_type_mismatch (explain_p, parm, arg);
22472 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22473 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22474 if (TYPE_DOMAIN (parm) != NULL_TREE)
22475 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22476 TYPE_DOMAIN (arg), explain_p);
22477 return unify_success (explain_p);
22478
22479 case REAL_TYPE:
22480 case COMPLEX_TYPE:
22481 case VECTOR_TYPE:
22482 case INTEGER_TYPE:
22483 case BOOLEAN_TYPE:
22484 case ENUMERAL_TYPE:
22485 case VOID_TYPE:
22486 case NULLPTR_TYPE:
22487 if (TREE_CODE (arg) != TREE_CODE (parm))
22488 return unify_type_mismatch (explain_p, parm, arg);
22489
22490 /* We have already checked cv-qualification at the top of the
22491 function. */
22492 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22493 return unify_type_mismatch (explain_p, parm, arg);
22494
22495 /* As far as unification is concerned, this wins. Later checks
22496 will invalidate it if necessary. */
22497 return unify_success (explain_p);
22498
22499 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22500 /* Type INTEGER_CST can come from ordinary constant template args. */
22501 case INTEGER_CST:
22502 while (CONVERT_EXPR_P (arg))
22503 arg = TREE_OPERAND (arg, 0);
22504
22505 if (TREE_CODE (arg) != INTEGER_CST)
22506 return unify_template_argument_mismatch (explain_p, parm, arg);
22507 return (tree_int_cst_equal (parm, arg)
22508 ? unify_success (explain_p)
22509 : unify_template_argument_mismatch (explain_p, parm, arg));
22510
22511 case TREE_VEC:
22512 {
22513 int i, len, argslen;
22514 int parm_variadic_p = 0;
22515
22516 if (TREE_CODE (arg) != TREE_VEC)
22517 return unify_template_argument_mismatch (explain_p, parm, arg);
22518
22519 len = TREE_VEC_LENGTH (parm);
22520 argslen = TREE_VEC_LENGTH (arg);
22521
22522 /* Check for pack expansions in the parameters. */
22523 for (i = 0; i < len; ++i)
22524 {
22525 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22526 {
22527 if (i == len - 1)
22528 /* We can unify against something with a trailing
22529 parameter pack. */
22530 parm_variadic_p = 1;
22531 else
22532 /* [temp.deduct.type]/9: If the template argument list of
22533 P contains a pack expansion that is not the last
22534 template argument, the entire template argument list
22535 is a non-deduced context. */
22536 return unify_success (explain_p);
22537 }
22538 }
22539
22540 /* If we don't have enough arguments to satisfy the parameters
22541 (not counting the pack expression at the end), or we have
22542 too many arguments for a parameter list that doesn't end in
22543 a pack expression, we can't unify. */
22544 if (parm_variadic_p
22545 ? argslen < len - parm_variadic_p
22546 : argslen != len)
22547 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22548
22549 /* Unify all of the parameters that precede the (optional)
22550 pack expression. */
22551 for (i = 0; i < len - parm_variadic_p; ++i)
22552 {
22553 RECUR_AND_CHECK_FAILURE (tparms, targs,
22554 TREE_VEC_ELT (parm, i),
22555 TREE_VEC_ELT (arg, i),
22556 UNIFY_ALLOW_NONE, explain_p);
22557 }
22558 if (parm_variadic_p)
22559 return unify_pack_expansion (tparms, targs, parm, arg,
22560 DEDUCE_EXACT,
22561 /*subr=*/true, explain_p);
22562 return unify_success (explain_p);
22563 }
22564
22565 case RECORD_TYPE:
22566 case UNION_TYPE:
22567 if (TREE_CODE (arg) != TREE_CODE (parm))
22568 return unify_type_mismatch (explain_p, parm, arg);
22569
22570 if (TYPE_PTRMEMFUNC_P (parm))
22571 {
22572 if (!TYPE_PTRMEMFUNC_P (arg))
22573 return unify_type_mismatch (explain_p, parm, arg);
22574
22575 return unify (tparms, targs,
22576 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22577 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22578 strict, explain_p);
22579 }
22580 else if (TYPE_PTRMEMFUNC_P (arg))
22581 return unify_type_mismatch (explain_p, parm, arg);
22582
22583 if (CLASSTYPE_TEMPLATE_INFO (parm))
22584 {
22585 tree t = NULL_TREE;
22586
22587 if (strict_in & UNIFY_ALLOW_DERIVED)
22588 {
22589 /* First, we try to unify the PARM and ARG directly. */
22590 t = try_class_unification (tparms, targs,
22591 parm, arg, explain_p);
22592
22593 if (!t)
22594 {
22595 /* Fallback to the special case allowed in
22596 [temp.deduct.call]:
22597
22598 If P is a class, and P has the form
22599 template-id, then A can be a derived class of
22600 the deduced A. Likewise, if P is a pointer to
22601 a class of the form template-id, A can be a
22602 pointer to a derived class pointed to by the
22603 deduced A. */
22604 enum template_base_result r;
22605 r = get_template_base (tparms, targs, parm, arg,
22606 explain_p, &t);
22607
22608 if (!t)
22609 {
22610 /* Don't give the derived diagnostic if we're
22611 already dealing with the same template. */
22612 bool same_template
22613 = (CLASSTYPE_TEMPLATE_INFO (arg)
22614 && (CLASSTYPE_TI_TEMPLATE (parm)
22615 == CLASSTYPE_TI_TEMPLATE (arg)));
22616 return unify_no_common_base (explain_p && !same_template,
22617 r, parm, arg);
22618 }
22619 }
22620 }
22621 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22622 && (CLASSTYPE_TI_TEMPLATE (parm)
22623 == CLASSTYPE_TI_TEMPLATE (arg)))
22624 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22625 Then, we should unify `int' and `U'. */
22626 t = arg;
22627 else
22628 /* There's no chance of unification succeeding. */
22629 return unify_type_mismatch (explain_p, parm, arg);
22630
22631 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22632 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22633 }
22634 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22635 return unify_type_mismatch (explain_p, parm, arg);
22636 return unify_success (explain_p);
22637
22638 case METHOD_TYPE:
22639 case FUNCTION_TYPE:
22640 {
22641 unsigned int nargs;
22642 tree *args;
22643 tree a;
22644 unsigned int i;
22645
22646 if (TREE_CODE (arg) != TREE_CODE (parm))
22647 return unify_type_mismatch (explain_p, parm, arg);
22648
22649 /* CV qualifications for methods can never be deduced, they must
22650 match exactly. We need to check them explicitly here,
22651 because type_unification_real treats them as any other
22652 cv-qualified parameter. */
22653 if (TREE_CODE (parm) == METHOD_TYPE
22654 && (!check_cv_quals_for_unify
22655 (UNIFY_ALLOW_NONE,
22656 class_of_this_parm (arg),
22657 class_of_this_parm (parm))))
22658 return unify_cv_qual_mismatch (explain_p, parm, arg);
22659 if (TREE_CODE (arg) == FUNCTION_TYPE
22660 && type_memfn_quals (parm) != type_memfn_quals (arg))
22661 return unify_cv_qual_mismatch (explain_p, parm, arg);
22662 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22663 return unify_type_mismatch (explain_p, parm, arg);
22664
22665 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22666 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22667
22668 nargs = list_length (TYPE_ARG_TYPES (arg));
22669 args = XALLOCAVEC (tree, nargs);
22670 for (a = TYPE_ARG_TYPES (arg), i = 0;
22671 a != NULL_TREE && a != void_list_node;
22672 a = TREE_CHAIN (a), ++i)
22673 args[i] = TREE_VALUE (a);
22674 nargs = i;
22675
22676 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22677 args, nargs, 1, DEDUCE_EXACT,
22678 NULL, explain_p))
22679 return 1;
22680
22681 if (flag_noexcept_type)
22682 {
22683 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22684 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22685 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22686 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22687 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22688 && uses_template_parms (TREE_PURPOSE (pspec)))
22689 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22690 TREE_PURPOSE (aspec),
22691 UNIFY_ALLOW_NONE, explain_p);
22692 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22693 return unify_type_mismatch (explain_p, parm, arg);
22694 }
22695
22696 return 0;
22697 }
22698
22699 case OFFSET_TYPE:
22700 /* Unify a pointer to member with a pointer to member function, which
22701 deduces the type of the member as a function type. */
22702 if (TYPE_PTRMEMFUNC_P (arg))
22703 {
22704 /* Check top-level cv qualifiers */
22705 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22706 return unify_cv_qual_mismatch (explain_p, parm, arg);
22707
22708 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22709 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22710 UNIFY_ALLOW_NONE, explain_p);
22711
22712 /* Determine the type of the function we are unifying against. */
22713 tree fntype = static_fn_type (arg);
22714
22715 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22716 }
22717
22718 if (TREE_CODE (arg) != OFFSET_TYPE)
22719 return unify_type_mismatch (explain_p, parm, arg);
22720 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22721 TYPE_OFFSET_BASETYPE (arg),
22722 UNIFY_ALLOW_NONE, explain_p);
22723 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22724 strict, explain_p);
22725
22726 case CONST_DECL:
22727 if (DECL_TEMPLATE_PARM_P (parm))
22728 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22729 if (arg != scalar_constant_value (parm))
22730 return unify_template_argument_mismatch (explain_p, parm, arg);
22731 return unify_success (explain_p);
22732
22733 case FIELD_DECL:
22734 case TEMPLATE_DECL:
22735 /* Matched cases are handled by the ARG == PARM test above. */
22736 return unify_template_argument_mismatch (explain_p, parm, arg);
22737
22738 case VAR_DECL:
22739 /* We might get a variable as a non-type template argument in parm if the
22740 corresponding parameter is type-dependent. Make any necessary
22741 adjustments based on whether arg is a reference. */
22742 if (CONSTANT_CLASS_P (arg))
22743 parm = fold_non_dependent_expr (parm, complain);
22744 else if (REFERENCE_REF_P (arg))
22745 {
22746 tree sub = TREE_OPERAND (arg, 0);
22747 STRIP_NOPS (sub);
22748 if (TREE_CODE (sub) == ADDR_EXPR)
22749 arg = TREE_OPERAND (sub, 0);
22750 }
22751 /* Now use the normal expression code to check whether they match. */
22752 goto expr;
22753
22754 case TYPE_ARGUMENT_PACK:
22755 case NONTYPE_ARGUMENT_PACK:
22756 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22757 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22758
22759 case TYPEOF_TYPE:
22760 case DECLTYPE_TYPE:
22761 case UNDERLYING_TYPE:
22762 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22763 or UNDERLYING_TYPE nodes. */
22764 return unify_success (explain_p);
22765
22766 case ERROR_MARK:
22767 /* Unification fails if we hit an error node. */
22768 return unify_invalid (explain_p);
22769
22770 case INDIRECT_REF:
22771 if (REFERENCE_REF_P (parm))
22772 {
22773 bool pexp = PACK_EXPANSION_P (arg);
22774 if (pexp)
22775 arg = PACK_EXPANSION_PATTERN (arg);
22776 if (REFERENCE_REF_P (arg))
22777 arg = TREE_OPERAND (arg, 0);
22778 if (pexp)
22779 arg = make_pack_expansion (arg, complain);
22780 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22781 strict, explain_p);
22782 }
22783 /* FALLTHRU */
22784
22785 default:
22786 /* An unresolved overload is a nondeduced context. */
22787 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22788 return unify_success (explain_p);
22789 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
22790 expr:
22791 /* We must be looking at an expression. This can happen with
22792 something like:
22793
22794 template <int I>
22795 void foo(S<I>, S<I + 2>);
22796
22797 This is a "nondeduced context":
22798
22799 [deduct.type]
22800
22801 The nondeduced contexts are:
22802
22803 --A type that is a template-id in which one or more of
22804 the template-arguments is an expression that references
22805 a template-parameter.
22806
22807 In these cases, we assume deduction succeeded, but don't
22808 actually infer any unifications. */
22809
22810 if (!uses_template_parms (parm)
22811 && !template_args_equal (parm, arg))
22812 return unify_expression_unequal (explain_p, parm, arg);
22813 else
22814 return unify_success (explain_p);
22815 }
22816 }
22817 #undef RECUR_AND_CHECK_FAILURE
22818 \f
22819 /* Note that DECL can be defined in this translation unit, if
22820 required. */
22821
22822 static void
22823 mark_definable (tree decl)
22824 {
22825 tree clone;
22826 DECL_NOT_REALLY_EXTERN (decl) = 1;
22827 FOR_EACH_CLONE (clone, decl)
22828 DECL_NOT_REALLY_EXTERN (clone) = 1;
22829 }
22830
22831 /* Called if RESULT is explicitly instantiated, or is a member of an
22832 explicitly instantiated class. */
22833
22834 void
22835 mark_decl_instantiated (tree result, int extern_p)
22836 {
22837 SET_DECL_EXPLICIT_INSTANTIATION (result);
22838
22839 /* If this entity has already been written out, it's too late to
22840 make any modifications. */
22841 if (TREE_ASM_WRITTEN (result))
22842 return;
22843
22844 /* For anonymous namespace we don't need to do anything. */
22845 if (decl_anon_ns_mem_p (result))
22846 {
22847 gcc_assert (!TREE_PUBLIC (result));
22848 return;
22849 }
22850
22851 if (TREE_CODE (result) != FUNCTION_DECL)
22852 /* The TREE_PUBLIC flag for function declarations will have been
22853 set correctly by tsubst. */
22854 TREE_PUBLIC (result) = 1;
22855
22856 /* This might have been set by an earlier implicit instantiation. */
22857 DECL_COMDAT (result) = 0;
22858
22859 if (extern_p)
22860 DECL_NOT_REALLY_EXTERN (result) = 0;
22861 else
22862 {
22863 mark_definable (result);
22864 mark_needed (result);
22865 /* Always make artificials weak. */
22866 if (DECL_ARTIFICIAL (result) && flag_weak)
22867 comdat_linkage (result);
22868 /* For WIN32 we also want to put explicit instantiations in
22869 linkonce sections. */
22870 else if (TREE_PUBLIC (result))
22871 maybe_make_one_only (result);
22872 if (TREE_CODE (result) == FUNCTION_DECL
22873 && DECL_TEMPLATE_INSTANTIATED (result))
22874 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22875 since start_preparsed_function wouldn't have if we had an earlier
22876 extern explicit instantiation. */
22877 DECL_EXTERNAL (result) = 0;
22878 }
22879
22880 /* If EXTERN_P, then this function will not be emitted -- unless
22881 followed by an explicit instantiation, at which point its linkage
22882 will be adjusted. If !EXTERN_P, then this function will be
22883 emitted here. In neither circumstance do we want
22884 import_export_decl to adjust the linkage. */
22885 DECL_INTERFACE_KNOWN (result) = 1;
22886 }
22887
22888 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22889 important template arguments. If any are missing, we check whether
22890 they're important by using error_mark_node for substituting into any
22891 args that were used for partial ordering (the ones between ARGS and END)
22892 and seeing if it bubbles up. */
22893
22894 static bool
22895 check_undeduced_parms (tree targs, tree args, tree end)
22896 {
22897 bool found = false;
22898 int i;
22899 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22900 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22901 {
22902 found = true;
22903 TREE_VEC_ELT (targs, i) = error_mark_node;
22904 }
22905 if (found)
22906 {
22907 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22908 if (substed == error_mark_node)
22909 return true;
22910 }
22911 return false;
22912 }
22913
22914 /* Given two function templates PAT1 and PAT2, return:
22915
22916 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22917 -1 if PAT2 is more specialized than PAT1.
22918 0 if neither is more specialized.
22919
22920 LEN indicates the number of parameters we should consider
22921 (defaulted parameters should not be considered).
22922
22923 The 1998 std underspecified function template partial ordering, and
22924 DR214 addresses the issue. We take pairs of arguments, one from
22925 each of the templates, and deduce them against each other. One of
22926 the templates will be more specialized if all the *other*
22927 template's arguments deduce against its arguments and at least one
22928 of its arguments *does* *not* deduce against the other template's
22929 corresponding argument. Deduction is done as for class templates.
22930 The arguments used in deduction have reference and top level cv
22931 qualifiers removed. Iff both arguments were originally reference
22932 types *and* deduction succeeds in both directions, an lvalue reference
22933 wins against an rvalue reference and otherwise the template
22934 with the more cv-qualified argument wins for that pairing (if
22935 neither is more cv-qualified, they both are equal). Unlike regular
22936 deduction, after all the arguments have been deduced in this way,
22937 we do *not* verify the deduced template argument values can be
22938 substituted into non-deduced contexts.
22939
22940 The logic can be a bit confusing here, because we look at deduce1 and
22941 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22942 can find template arguments for pat1 to make arg1 look like arg2, that
22943 means that arg2 is at least as specialized as arg1. */
22944
22945 int
22946 more_specialized_fn (tree pat1, tree pat2, int len)
22947 {
22948 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22949 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22950 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22951 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22952 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22953 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22954 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22955 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22956 tree origs1, origs2;
22957 bool lose1 = false;
22958 bool lose2 = false;
22959
22960 /* Remove the this parameter from non-static member functions. If
22961 one is a non-static member function and the other is not a static
22962 member function, remove the first parameter from that function
22963 also. This situation occurs for operator functions where we
22964 locate both a member function (with this pointer) and non-member
22965 operator (with explicit first operand). */
22966 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22967 {
22968 len--; /* LEN is the number of significant arguments for DECL1 */
22969 args1 = TREE_CHAIN (args1);
22970 if (!DECL_STATIC_FUNCTION_P (decl2))
22971 args2 = TREE_CHAIN (args2);
22972 }
22973 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22974 {
22975 args2 = TREE_CHAIN (args2);
22976 if (!DECL_STATIC_FUNCTION_P (decl1))
22977 {
22978 len--;
22979 args1 = TREE_CHAIN (args1);
22980 }
22981 }
22982
22983 /* If only one is a conversion operator, they are unordered. */
22984 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22985 return 0;
22986
22987 /* Consider the return type for a conversion function */
22988 if (DECL_CONV_FN_P (decl1))
22989 {
22990 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22991 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22992 len++;
22993 }
22994
22995 processing_template_decl++;
22996
22997 origs1 = args1;
22998 origs2 = args2;
22999
23000 while (len--
23001 /* Stop when an ellipsis is seen. */
23002 && args1 != NULL_TREE && args2 != NULL_TREE)
23003 {
23004 tree arg1 = TREE_VALUE (args1);
23005 tree arg2 = TREE_VALUE (args2);
23006 int deduce1, deduce2;
23007 int quals1 = -1;
23008 int quals2 = -1;
23009 int ref1 = 0;
23010 int ref2 = 0;
23011
23012 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23013 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23014 {
23015 /* When both arguments are pack expansions, we need only
23016 unify the patterns themselves. */
23017 arg1 = PACK_EXPANSION_PATTERN (arg1);
23018 arg2 = PACK_EXPANSION_PATTERN (arg2);
23019
23020 /* This is the last comparison we need to do. */
23021 len = 0;
23022 }
23023
23024 /* DR 1847: If a particular P contains no template-parameters that
23025 participate in template argument deduction, that P is not used to
23026 determine the ordering. */
23027 if (!uses_deducible_template_parms (arg1)
23028 && !uses_deducible_template_parms (arg2))
23029 goto next;
23030
23031 if (TYPE_REF_P (arg1))
23032 {
23033 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23034 arg1 = TREE_TYPE (arg1);
23035 quals1 = cp_type_quals (arg1);
23036 }
23037
23038 if (TYPE_REF_P (arg2))
23039 {
23040 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23041 arg2 = TREE_TYPE (arg2);
23042 quals2 = cp_type_quals (arg2);
23043 }
23044
23045 arg1 = TYPE_MAIN_VARIANT (arg1);
23046 arg2 = TYPE_MAIN_VARIANT (arg2);
23047
23048 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23049 {
23050 int i, len2 = remaining_arguments (args2);
23051 tree parmvec = make_tree_vec (1);
23052 tree argvec = make_tree_vec (len2);
23053 tree ta = args2;
23054
23055 /* Setup the parameter vector, which contains only ARG1. */
23056 TREE_VEC_ELT (parmvec, 0) = arg1;
23057
23058 /* Setup the argument vector, which contains the remaining
23059 arguments. */
23060 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23061 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23062
23063 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23064 argvec, DEDUCE_EXACT,
23065 /*subr=*/true, /*explain_p=*/false)
23066 == 0);
23067
23068 /* We cannot deduce in the other direction, because ARG1 is
23069 a pack expansion but ARG2 is not. */
23070 deduce2 = 0;
23071 }
23072 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23073 {
23074 int i, len1 = remaining_arguments (args1);
23075 tree parmvec = make_tree_vec (1);
23076 tree argvec = make_tree_vec (len1);
23077 tree ta = args1;
23078
23079 /* Setup the parameter vector, which contains only ARG1. */
23080 TREE_VEC_ELT (parmvec, 0) = arg2;
23081
23082 /* Setup the argument vector, which contains the remaining
23083 arguments. */
23084 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23085 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23086
23087 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23088 argvec, DEDUCE_EXACT,
23089 /*subr=*/true, /*explain_p=*/false)
23090 == 0);
23091
23092 /* We cannot deduce in the other direction, because ARG2 is
23093 a pack expansion but ARG1 is not.*/
23094 deduce1 = 0;
23095 }
23096
23097 else
23098 {
23099 /* The normal case, where neither argument is a pack
23100 expansion. */
23101 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23102 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23103 == 0);
23104 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23105 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23106 == 0);
23107 }
23108
23109 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23110 arg2, then arg2 is not as specialized as arg1. */
23111 if (!deduce1)
23112 lose2 = true;
23113 if (!deduce2)
23114 lose1 = true;
23115
23116 /* "If, for a given type, deduction succeeds in both directions
23117 (i.e., the types are identical after the transformations above)
23118 and both P and A were reference types (before being replaced with
23119 the type referred to above):
23120 - if the type from the argument template was an lvalue reference and
23121 the type from the parameter template was not, the argument type is
23122 considered to be more specialized than the other; otherwise,
23123 - if the type from the argument template is more cv-qualified
23124 than the type from the parameter template (as described above),
23125 the argument type is considered to be more specialized than the other;
23126 otherwise,
23127 - neither type is more specialized than the other." */
23128
23129 if (deduce1 && deduce2)
23130 {
23131 if (ref1 && ref2 && ref1 != ref2)
23132 {
23133 if (ref1 > ref2)
23134 lose1 = true;
23135 else
23136 lose2 = true;
23137 }
23138 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23139 {
23140 if ((quals1 & quals2) == quals2)
23141 lose2 = true;
23142 if ((quals1 & quals2) == quals1)
23143 lose1 = true;
23144 }
23145 }
23146
23147 if (lose1 && lose2)
23148 /* We've failed to deduce something in either direction.
23149 These must be unordered. */
23150 break;
23151
23152 next:
23153
23154 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23155 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23156 /* We have already processed all of the arguments in our
23157 handing of the pack expansion type. */
23158 len = 0;
23159
23160 args1 = TREE_CHAIN (args1);
23161 args2 = TREE_CHAIN (args2);
23162 }
23163
23164 /* "In most cases, all template parameters must have values in order for
23165 deduction to succeed, but for partial ordering purposes a template
23166 parameter may remain without a value provided it is not used in the
23167 types being used for partial ordering."
23168
23169 Thus, if we are missing any of the targs1 we need to substitute into
23170 origs1, then pat2 is not as specialized as pat1. This can happen when
23171 there is a nondeduced context. */
23172 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23173 lose2 = true;
23174 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23175 lose1 = true;
23176
23177 processing_template_decl--;
23178
23179 /* If both deductions succeed, the partial ordering selects the more
23180 constrained template. */
23181 if (!lose1 && !lose2)
23182 {
23183 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
23184 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
23185 lose1 = !subsumes_constraints (c1, c2);
23186 lose2 = !subsumes_constraints (c2, c1);
23187 }
23188
23189 /* All things being equal, if the next argument is a pack expansion
23190 for one function but not for the other, prefer the
23191 non-variadic function. FIXME this is bogus; see c++/41958. */
23192 if (lose1 == lose2
23193 && args1 && TREE_VALUE (args1)
23194 && args2 && TREE_VALUE (args2))
23195 {
23196 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23197 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23198 }
23199
23200 if (lose1 == lose2)
23201 return 0;
23202 else if (!lose1)
23203 return 1;
23204 else
23205 return -1;
23206 }
23207
23208 /* Determine which of two partial specializations of TMPL is more
23209 specialized.
23210
23211 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23212 to the first partial specialization. The TREE_PURPOSE is the
23213 innermost set of template parameters for the partial
23214 specialization. PAT2 is similar, but for the second template.
23215
23216 Return 1 if the first partial specialization is more specialized;
23217 -1 if the second is more specialized; 0 if neither is more
23218 specialized.
23219
23220 See [temp.class.order] for information about determining which of
23221 two templates is more specialized. */
23222
23223 static int
23224 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23225 {
23226 tree targs;
23227 int winner = 0;
23228 bool any_deductions = false;
23229
23230 tree tmpl1 = TREE_VALUE (pat1);
23231 tree tmpl2 = TREE_VALUE (pat2);
23232 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23233 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23234
23235 /* Just like what happens for functions, if we are ordering between
23236 different template specializations, we may encounter dependent
23237 types in the arguments, and we need our dependency check functions
23238 to behave correctly. */
23239 ++processing_template_decl;
23240 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23241 if (targs)
23242 {
23243 --winner;
23244 any_deductions = true;
23245 }
23246
23247 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23248 if (targs)
23249 {
23250 ++winner;
23251 any_deductions = true;
23252 }
23253 --processing_template_decl;
23254
23255 /* If both deductions succeed, the partial ordering selects the more
23256 constrained template. */
23257 if (!winner && any_deductions)
23258 return more_constrained (tmpl1, tmpl2);
23259
23260 /* In the case of a tie where at least one of the templates
23261 has a parameter pack at the end, the template with the most
23262 non-packed parameters wins. */
23263 if (winner == 0
23264 && any_deductions
23265 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23266 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23267 {
23268 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23269 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23270 int len1 = TREE_VEC_LENGTH (args1);
23271 int len2 = TREE_VEC_LENGTH (args2);
23272
23273 /* We don't count the pack expansion at the end. */
23274 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23275 --len1;
23276 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23277 --len2;
23278
23279 if (len1 > len2)
23280 return 1;
23281 else if (len1 < len2)
23282 return -1;
23283 }
23284
23285 return winner;
23286 }
23287
23288 /* Return the template arguments that will produce the function signature
23289 DECL from the function template FN, with the explicit template
23290 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23291 also match. Return NULL_TREE if no satisfactory arguments could be
23292 found. */
23293
23294 static tree
23295 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23296 {
23297 int ntparms = DECL_NTPARMS (fn);
23298 tree targs = make_tree_vec (ntparms);
23299 tree decl_type = TREE_TYPE (decl);
23300 tree decl_arg_types;
23301 tree *args;
23302 unsigned int nargs, ix;
23303 tree arg;
23304
23305 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23306
23307 /* Never do unification on the 'this' parameter. */
23308 decl_arg_types = skip_artificial_parms_for (decl,
23309 TYPE_ARG_TYPES (decl_type));
23310
23311 nargs = list_length (decl_arg_types);
23312 args = XALLOCAVEC (tree, nargs);
23313 for (arg = decl_arg_types, ix = 0;
23314 arg != NULL_TREE && arg != void_list_node;
23315 arg = TREE_CHAIN (arg), ++ix)
23316 args[ix] = TREE_VALUE (arg);
23317
23318 if (fn_type_unification (fn, explicit_args, targs,
23319 args, ix,
23320 (check_rettype || DECL_CONV_FN_P (fn)
23321 ? TREE_TYPE (decl_type) : NULL_TREE),
23322 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23323 /*explain_p=*/false,
23324 /*decltype*/false)
23325 == error_mark_node)
23326 return NULL_TREE;
23327
23328 return targs;
23329 }
23330
23331 /* Return the innermost template arguments that, when applied to a partial
23332 specialization SPEC_TMPL of TMPL, yield the ARGS.
23333
23334 For example, suppose we have:
23335
23336 template <class T, class U> struct S {};
23337 template <class T> struct S<T*, int> {};
23338
23339 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23340 partial specialization and the ARGS will be {double*, int}. The resulting
23341 vector will be {double}, indicating that `T' is bound to `double'. */
23342
23343 static tree
23344 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23345 {
23346 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23347 tree spec_args
23348 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23349 int i, ntparms = TREE_VEC_LENGTH (tparms);
23350 tree deduced_args;
23351 tree innermost_deduced_args;
23352
23353 innermost_deduced_args = make_tree_vec (ntparms);
23354 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23355 {
23356 deduced_args = copy_node (args);
23357 SET_TMPL_ARGS_LEVEL (deduced_args,
23358 TMPL_ARGS_DEPTH (deduced_args),
23359 innermost_deduced_args);
23360 }
23361 else
23362 deduced_args = innermost_deduced_args;
23363
23364 bool tried_array_deduction = (cxx_dialect < cxx17);
23365 again:
23366 if (unify (tparms, deduced_args,
23367 INNERMOST_TEMPLATE_ARGS (spec_args),
23368 INNERMOST_TEMPLATE_ARGS (args),
23369 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23370 return NULL_TREE;
23371
23372 for (i = 0; i < ntparms; ++i)
23373 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23374 {
23375 if (!tried_array_deduction)
23376 {
23377 try_array_deduction (tparms, innermost_deduced_args,
23378 INNERMOST_TEMPLATE_ARGS (spec_args));
23379 tried_array_deduction = true;
23380 if (TREE_VEC_ELT (innermost_deduced_args, i))
23381 goto again;
23382 }
23383 return NULL_TREE;
23384 }
23385
23386 if (!push_tinst_level (spec_tmpl, deduced_args))
23387 {
23388 excessive_deduction_depth = true;
23389 return NULL_TREE;
23390 }
23391
23392 /* Verify that nondeduced template arguments agree with the type
23393 obtained from argument deduction.
23394
23395 For example:
23396
23397 struct A { typedef int X; };
23398 template <class T, class U> struct C {};
23399 template <class T> struct C<T, typename T::X> {};
23400
23401 Then with the instantiation `C<A, int>', we can deduce that
23402 `T' is `A' but unify () does not check whether `typename T::X'
23403 is `int'. */
23404 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23405
23406 if (spec_args != error_mark_node)
23407 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23408 INNERMOST_TEMPLATE_ARGS (spec_args),
23409 tmpl, tf_none, false, false);
23410
23411 pop_tinst_level ();
23412
23413 if (spec_args == error_mark_node
23414 /* We only need to check the innermost arguments; the other
23415 arguments will always agree. */
23416 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23417 INNERMOST_TEMPLATE_ARGS (args)))
23418 return NULL_TREE;
23419
23420 /* Now that we have bindings for all of the template arguments,
23421 ensure that the arguments deduced for the template template
23422 parameters have compatible template parameter lists. See the use
23423 of template_template_parm_bindings_ok_p in fn_type_unification
23424 for more information. */
23425 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23426 return NULL_TREE;
23427
23428 return deduced_args;
23429 }
23430
23431 // Compare two function templates T1 and T2 by deducing bindings
23432 // from one against the other. If both deductions succeed, compare
23433 // constraints to see which is more constrained.
23434 static int
23435 more_specialized_inst (tree t1, tree t2)
23436 {
23437 int fate = 0;
23438 int count = 0;
23439
23440 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
23441 {
23442 --fate;
23443 ++count;
23444 }
23445
23446 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
23447 {
23448 ++fate;
23449 ++count;
23450 }
23451
23452 // If both deductions succeed, then one may be more constrained.
23453 if (count == 2 && fate == 0)
23454 fate = more_constrained (t1, t2);
23455
23456 return fate;
23457 }
23458
23459 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23460 Return the TREE_LIST node with the most specialized template, if
23461 any. If there is no most specialized template, the error_mark_node
23462 is returned.
23463
23464 Note that this function does not look at, or modify, the
23465 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23466 returned is one of the elements of INSTANTIATIONS, callers may
23467 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23468 and retrieve it from the value returned. */
23469
23470 tree
23471 most_specialized_instantiation (tree templates)
23472 {
23473 tree fn, champ;
23474
23475 ++processing_template_decl;
23476
23477 champ = templates;
23478 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
23479 {
23480 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
23481 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
23482 if (fate == -1)
23483 champ = fn;
23484 else if (!fate)
23485 {
23486 /* Equally specialized, move to next function. If there
23487 is no next function, nothing's most specialized. */
23488 fn = TREE_CHAIN (fn);
23489 champ = fn;
23490 if (!fn)
23491 break;
23492 }
23493 }
23494
23495 if (champ)
23496 /* Now verify that champ is better than everything earlier in the
23497 instantiation list. */
23498 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23499 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23500 {
23501 champ = NULL_TREE;
23502 break;
23503 }
23504 }
23505
23506 processing_template_decl--;
23507
23508 if (!champ)
23509 return error_mark_node;
23510
23511 return champ;
23512 }
23513
23514 /* If DECL is a specialization of some template, return the most
23515 general such template. Otherwise, returns NULL_TREE.
23516
23517 For example, given:
23518
23519 template <class T> struct S { template <class U> void f(U); };
23520
23521 if TMPL is `template <class U> void S<int>::f(U)' this will return
23522 the full template. This function will not trace past partial
23523 specializations, however. For example, given in addition:
23524
23525 template <class T> struct S<T*> { template <class U> void f(U); };
23526
23527 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23528 `template <class T> template <class U> S<T*>::f(U)'. */
23529
23530 tree
23531 most_general_template (tree decl)
23532 {
23533 if (TREE_CODE (decl) != TEMPLATE_DECL)
23534 {
23535 if (tree tinfo = get_template_info (decl))
23536 decl = TI_TEMPLATE (tinfo);
23537 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23538 template friend, or a FIELD_DECL for a capture pack. */
23539 if (TREE_CODE (decl) != TEMPLATE_DECL)
23540 return NULL_TREE;
23541 }
23542
23543 /* Look for more and more general templates. */
23544 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23545 {
23546 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23547 (See cp-tree.h for details.) */
23548 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23549 break;
23550
23551 if (CLASS_TYPE_P (TREE_TYPE (decl))
23552 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23553 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23554 break;
23555
23556 /* Stop if we run into an explicitly specialized class template. */
23557 if (!DECL_NAMESPACE_SCOPE_P (decl)
23558 && DECL_CONTEXT (decl)
23559 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23560 break;
23561
23562 decl = DECL_TI_TEMPLATE (decl);
23563 }
23564
23565 return decl;
23566 }
23567
23568 /* Return the most specialized of the template partial specializations
23569 which can produce TARGET, a specialization of some class or variable
23570 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23571 a TEMPLATE_DECL node corresponding to the partial specialization, while
23572 the TREE_PURPOSE is the set of template arguments that must be
23573 substituted into the template pattern in order to generate TARGET.
23574
23575 If the choice of partial specialization is ambiguous, a diagnostic
23576 is issued, and the error_mark_node is returned. If there are no
23577 partial specializations matching TARGET, then NULL_TREE is
23578 returned, indicating that the primary template should be used. */
23579
23580 static tree
23581 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23582 {
23583 tree list = NULL_TREE;
23584 tree t;
23585 tree champ;
23586 int fate;
23587 bool ambiguous_p;
23588 tree outer_args = NULL_TREE;
23589 tree tmpl, args;
23590
23591 if (TYPE_P (target))
23592 {
23593 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23594 tmpl = TI_TEMPLATE (tinfo);
23595 args = TI_ARGS (tinfo);
23596 }
23597 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23598 {
23599 tmpl = TREE_OPERAND (target, 0);
23600 args = TREE_OPERAND (target, 1);
23601 }
23602 else if (VAR_P (target))
23603 {
23604 tree tinfo = DECL_TEMPLATE_INFO (target);
23605 tmpl = TI_TEMPLATE (tinfo);
23606 args = TI_ARGS (tinfo);
23607 }
23608 else
23609 gcc_unreachable ();
23610
23611 tree main_tmpl = most_general_template (tmpl);
23612
23613 /* For determining which partial specialization to use, only the
23614 innermost args are interesting. */
23615 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23616 {
23617 outer_args = strip_innermost_template_args (args, 1);
23618 args = INNERMOST_TEMPLATE_ARGS (args);
23619 }
23620
23621 /* The caller hasn't called push_to_top_level yet, but we need
23622 get_partial_spec_bindings to be done in non-template context so that we'll
23623 fully resolve everything. */
23624 processing_template_decl_sentinel ptds;
23625
23626 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23627 {
23628 tree spec_args;
23629 tree spec_tmpl = TREE_VALUE (t);
23630
23631 if (outer_args)
23632 {
23633 /* Substitute in the template args from the enclosing class. */
23634 ++processing_template_decl;
23635 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23636 --processing_template_decl;
23637 }
23638
23639 if (spec_tmpl == error_mark_node)
23640 return error_mark_node;
23641
23642 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23643 if (spec_args)
23644 {
23645 if (outer_args)
23646 spec_args = add_to_template_args (outer_args, spec_args);
23647
23648 /* Keep the candidate only if the constraints are satisfied,
23649 or if we're not compiling with concepts. */
23650 if (!flag_concepts
23651 || constraints_satisfied_p (spec_tmpl, spec_args))
23652 {
23653 list = tree_cons (spec_args, TREE_VALUE (t), list);
23654 TREE_TYPE (list) = TREE_TYPE (t);
23655 }
23656 }
23657 }
23658
23659 if (! list)
23660 return NULL_TREE;
23661
23662 ambiguous_p = false;
23663 t = list;
23664 champ = t;
23665 t = TREE_CHAIN (t);
23666 for (; t; t = TREE_CHAIN (t))
23667 {
23668 fate = more_specialized_partial_spec (tmpl, champ, t);
23669 if (fate == 1)
23670 ;
23671 else
23672 {
23673 if (fate == 0)
23674 {
23675 t = TREE_CHAIN (t);
23676 if (! t)
23677 {
23678 ambiguous_p = true;
23679 break;
23680 }
23681 }
23682 champ = t;
23683 }
23684 }
23685
23686 if (!ambiguous_p)
23687 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23688 {
23689 fate = more_specialized_partial_spec (tmpl, champ, t);
23690 if (fate != 1)
23691 {
23692 ambiguous_p = true;
23693 break;
23694 }
23695 }
23696
23697 if (ambiguous_p)
23698 {
23699 const char *str;
23700 char *spaces = NULL;
23701 if (!(complain & tf_error))
23702 return error_mark_node;
23703 if (TYPE_P (target))
23704 error ("ambiguous template instantiation for %q#T", target);
23705 else
23706 error ("ambiguous template instantiation for %q#D", target);
23707 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23708 for (t = list; t; t = TREE_CHAIN (t))
23709 {
23710 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23711 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23712 "%s %#qS", spaces ? spaces : str, subst);
23713 spaces = spaces ? spaces : get_spaces (str);
23714 }
23715 free (spaces);
23716 return error_mark_node;
23717 }
23718
23719 return champ;
23720 }
23721
23722 /* Explicitly instantiate DECL. */
23723
23724 void
23725 do_decl_instantiation (tree decl, tree storage)
23726 {
23727 tree result = NULL_TREE;
23728 int extern_p = 0;
23729
23730 if (!decl || decl == error_mark_node)
23731 /* An error occurred, for which grokdeclarator has already issued
23732 an appropriate message. */
23733 return;
23734 else if (! DECL_LANG_SPECIFIC (decl))
23735 {
23736 error ("explicit instantiation of non-template %q#D", decl);
23737 return;
23738 }
23739 else if (DECL_DECLARED_CONCEPT_P (decl))
23740 {
23741 if (VAR_P (decl))
23742 error ("explicit instantiation of variable concept %q#D", decl);
23743 else
23744 error ("explicit instantiation of function concept %q#D", decl);
23745 return;
23746 }
23747
23748 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23749 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23750
23751 if (VAR_P (decl) && !var_templ)
23752 {
23753 /* There is an asymmetry here in the way VAR_DECLs and
23754 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23755 the latter, the DECL we get back will be marked as a
23756 template instantiation, and the appropriate
23757 DECL_TEMPLATE_INFO will be set up. This does not happen for
23758 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23759 should handle VAR_DECLs as it currently handles
23760 FUNCTION_DECLs. */
23761 if (!DECL_CLASS_SCOPE_P (decl))
23762 {
23763 error ("%qD is not a static data member of a class template", decl);
23764 return;
23765 }
23766 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23767 if (!result || !VAR_P (result))
23768 {
23769 error ("no matching template for %qD found", decl);
23770 return;
23771 }
23772 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23773 {
23774 error ("type %qT for explicit instantiation %qD does not match "
23775 "declared type %qT", TREE_TYPE (result), decl,
23776 TREE_TYPE (decl));
23777 return;
23778 }
23779 }
23780 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23781 {
23782 error ("explicit instantiation of %q#D", decl);
23783 return;
23784 }
23785 else
23786 result = decl;
23787
23788 /* Check for various error cases. Note that if the explicit
23789 instantiation is valid the RESULT will currently be marked as an
23790 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23791 until we get here. */
23792
23793 if (DECL_TEMPLATE_SPECIALIZATION (result))
23794 {
23795 /* DR 259 [temp.spec].
23796
23797 Both an explicit instantiation and a declaration of an explicit
23798 specialization shall not appear in a program unless the explicit
23799 instantiation follows a declaration of the explicit specialization.
23800
23801 For a given set of template parameters, if an explicit
23802 instantiation of a template appears after a declaration of an
23803 explicit specialization for that template, the explicit
23804 instantiation has no effect. */
23805 return;
23806 }
23807 else if (DECL_EXPLICIT_INSTANTIATION (result))
23808 {
23809 /* [temp.spec]
23810
23811 No program shall explicitly instantiate any template more
23812 than once.
23813
23814 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23815 the first instantiation was `extern' and the second is not,
23816 and EXTERN_P for the opposite case. */
23817 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23818 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23819 /* If an "extern" explicit instantiation follows an ordinary
23820 explicit instantiation, the template is instantiated. */
23821 if (extern_p)
23822 return;
23823 }
23824 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23825 {
23826 error ("no matching template for %qD found", result);
23827 return;
23828 }
23829 else if (!DECL_TEMPLATE_INFO (result))
23830 {
23831 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23832 return;
23833 }
23834
23835 if (storage == NULL_TREE)
23836 ;
23837 else if (storage == ridpointers[(int) RID_EXTERN])
23838 {
23839 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23840 pedwarn (input_location, OPT_Wpedantic,
23841 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23842 "instantiations");
23843 extern_p = 1;
23844 }
23845 else
23846 error ("storage class %qD applied to template instantiation", storage);
23847
23848 check_explicit_instantiation_namespace (result);
23849 mark_decl_instantiated (result, extern_p);
23850 if (! extern_p)
23851 instantiate_decl (result, /*defer_ok=*/true,
23852 /*expl_inst_class_mem_p=*/false);
23853 }
23854
23855 static void
23856 mark_class_instantiated (tree t, int extern_p)
23857 {
23858 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23859 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23860 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23861 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23862 if (! extern_p)
23863 {
23864 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23865 rest_of_type_compilation (t, 1);
23866 }
23867 }
23868
23869 /* Called from do_type_instantiation through binding_table_foreach to
23870 do recursive instantiation for the type bound in ENTRY. */
23871 static void
23872 bt_instantiate_type_proc (binding_entry entry, void *data)
23873 {
23874 tree storage = *(tree *) data;
23875
23876 if (MAYBE_CLASS_TYPE_P (entry->type)
23877 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23878 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23879 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23880 }
23881
23882 /* Perform an explicit instantiation of template class T. STORAGE, if
23883 non-null, is the RID for extern, inline or static. COMPLAIN is
23884 nonzero if this is called from the parser, zero if called recursively,
23885 since the standard is unclear (as detailed below). */
23886
23887 void
23888 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23889 {
23890 int extern_p = 0;
23891 int nomem_p = 0;
23892 int static_p = 0;
23893 int previous_instantiation_extern_p = 0;
23894
23895 if (TREE_CODE (t) == TYPE_DECL)
23896 t = TREE_TYPE (t);
23897
23898 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23899 {
23900 tree tmpl =
23901 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23902 if (tmpl)
23903 error ("explicit instantiation of non-class template %qD", tmpl);
23904 else
23905 error ("explicit instantiation of non-template type %qT", t);
23906 return;
23907 }
23908
23909 complete_type (t);
23910
23911 if (!COMPLETE_TYPE_P (t))
23912 {
23913 if (complain & tf_error)
23914 error ("explicit instantiation of %q#T before definition of template",
23915 t);
23916 return;
23917 }
23918
23919 if (storage != NULL_TREE)
23920 {
23921 if (!in_system_header_at (input_location))
23922 {
23923 if (storage == ridpointers[(int) RID_EXTERN])
23924 {
23925 if (cxx_dialect == cxx98)
23926 pedwarn (input_location, OPT_Wpedantic,
23927 "ISO C++ 1998 forbids the use of %<extern%> on "
23928 "explicit instantiations");
23929 }
23930 else
23931 pedwarn (input_location, OPT_Wpedantic,
23932 "ISO C++ forbids the use of %qE"
23933 " on explicit instantiations", storage);
23934 }
23935
23936 if (storage == ridpointers[(int) RID_INLINE])
23937 nomem_p = 1;
23938 else if (storage == ridpointers[(int) RID_EXTERN])
23939 extern_p = 1;
23940 else if (storage == ridpointers[(int) RID_STATIC])
23941 static_p = 1;
23942 else
23943 {
23944 error ("storage class %qD applied to template instantiation",
23945 storage);
23946 extern_p = 0;
23947 }
23948 }
23949
23950 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23951 {
23952 /* DR 259 [temp.spec].
23953
23954 Both an explicit instantiation and a declaration of an explicit
23955 specialization shall not appear in a program unless the explicit
23956 instantiation follows a declaration of the explicit specialization.
23957
23958 For a given set of template parameters, if an explicit
23959 instantiation of a template appears after a declaration of an
23960 explicit specialization for that template, the explicit
23961 instantiation has no effect. */
23962 return;
23963 }
23964 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23965 {
23966 /* [temp.spec]
23967
23968 No program shall explicitly instantiate any template more
23969 than once.
23970
23971 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23972 instantiation was `extern'. If EXTERN_P then the second is.
23973 These cases are OK. */
23974 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23975
23976 if (!previous_instantiation_extern_p && !extern_p
23977 && (complain & tf_error))
23978 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23979
23980 /* If we've already instantiated the template, just return now. */
23981 if (!CLASSTYPE_INTERFACE_ONLY (t))
23982 return;
23983 }
23984
23985 check_explicit_instantiation_namespace (TYPE_NAME (t));
23986 mark_class_instantiated (t, extern_p);
23987
23988 if (nomem_p)
23989 return;
23990
23991 /* In contrast to implicit instantiation, where only the
23992 declarations, and not the definitions, of members are
23993 instantiated, we have here:
23994
23995 [temp.explicit]
23996
23997 The explicit instantiation of a class template specialization
23998 implies the instantiation of all of its members not
23999 previously explicitly specialized in the translation unit
24000 containing the explicit instantiation.
24001
24002 Of course, we can't instantiate member template classes, since we
24003 don't have any arguments for them. Note that the standard is
24004 unclear on whether the instantiation of the members are
24005 *explicit* instantiations or not. However, the most natural
24006 interpretation is that it should be an explicit
24007 instantiation. */
24008 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24009 if ((VAR_P (fld)
24010 || (TREE_CODE (fld) == FUNCTION_DECL
24011 && !static_p
24012 && user_provided_p (fld)))
24013 && DECL_TEMPLATE_INSTANTIATION (fld))
24014 {
24015 mark_decl_instantiated (fld, extern_p);
24016 if (! extern_p)
24017 instantiate_decl (fld, /*defer_ok=*/true,
24018 /*expl_inst_class_mem_p=*/true);
24019 }
24020
24021 if (CLASSTYPE_NESTED_UTDS (t))
24022 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24023 bt_instantiate_type_proc, &storage);
24024 }
24025
24026 /* Given a function DECL, which is a specialization of TMPL, modify
24027 DECL to be a re-instantiation of TMPL with the same template
24028 arguments. TMPL should be the template into which tsubst'ing
24029 should occur for DECL, not the most general template.
24030
24031 One reason for doing this is a scenario like this:
24032
24033 template <class T>
24034 void f(const T&, int i);
24035
24036 void g() { f(3, 7); }
24037
24038 template <class T>
24039 void f(const T& t, const int i) { }
24040
24041 Note that when the template is first instantiated, with
24042 instantiate_template, the resulting DECL will have no name for the
24043 first parameter, and the wrong type for the second. So, when we go
24044 to instantiate the DECL, we regenerate it. */
24045
24046 static void
24047 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24048 {
24049 /* The arguments used to instantiate DECL, from the most general
24050 template. */
24051 tree code_pattern;
24052
24053 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24054
24055 /* Make sure that we can see identifiers, and compute access
24056 correctly. */
24057 push_access_scope (decl);
24058
24059 if (TREE_CODE (decl) == FUNCTION_DECL)
24060 {
24061 tree decl_parm;
24062 tree pattern_parm;
24063 tree specs;
24064 int args_depth;
24065 int parms_depth;
24066
24067 args_depth = TMPL_ARGS_DEPTH (args);
24068 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24069 if (args_depth > parms_depth)
24070 args = get_innermost_template_args (args, parms_depth);
24071
24072 /* Instantiate a dynamic exception-specification. noexcept will be
24073 handled below. */
24074 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24075 if (TREE_VALUE (raises))
24076 {
24077 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24078 args, tf_error, NULL_TREE,
24079 /*defer_ok*/false);
24080 if (specs && specs != error_mark_node)
24081 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24082 specs);
24083 }
24084
24085 /* Merge parameter declarations. */
24086 decl_parm = skip_artificial_parms_for (decl,
24087 DECL_ARGUMENTS (decl));
24088 pattern_parm
24089 = skip_artificial_parms_for (code_pattern,
24090 DECL_ARGUMENTS (code_pattern));
24091 while (decl_parm && !DECL_PACK_P (pattern_parm))
24092 {
24093 tree parm_type;
24094 tree attributes;
24095
24096 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24097 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24098 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24099 NULL_TREE);
24100 parm_type = type_decays_to (parm_type);
24101 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24102 TREE_TYPE (decl_parm) = parm_type;
24103 attributes = DECL_ATTRIBUTES (pattern_parm);
24104 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24105 {
24106 DECL_ATTRIBUTES (decl_parm) = attributes;
24107 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24108 }
24109 decl_parm = DECL_CHAIN (decl_parm);
24110 pattern_parm = DECL_CHAIN (pattern_parm);
24111 }
24112 /* Merge any parameters that match with the function parameter
24113 pack. */
24114 if (pattern_parm && DECL_PACK_P (pattern_parm))
24115 {
24116 int i, len;
24117 tree expanded_types;
24118 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24119 the parameters in this function parameter pack. */
24120 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24121 args, tf_error, NULL_TREE);
24122 len = TREE_VEC_LENGTH (expanded_types);
24123 for (i = 0; i < len; i++)
24124 {
24125 tree parm_type;
24126 tree attributes;
24127
24128 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24129 /* Rename the parameter to include the index. */
24130 DECL_NAME (decl_parm) =
24131 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24132 parm_type = TREE_VEC_ELT (expanded_types, i);
24133 parm_type = type_decays_to (parm_type);
24134 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24135 TREE_TYPE (decl_parm) = parm_type;
24136 attributes = DECL_ATTRIBUTES (pattern_parm);
24137 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24138 {
24139 DECL_ATTRIBUTES (decl_parm) = attributes;
24140 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24141 }
24142 decl_parm = DECL_CHAIN (decl_parm);
24143 }
24144 }
24145 /* Merge additional specifiers from the CODE_PATTERN. */
24146 if (DECL_DECLARED_INLINE_P (code_pattern)
24147 && !DECL_DECLARED_INLINE_P (decl))
24148 DECL_DECLARED_INLINE_P (decl) = 1;
24149
24150 maybe_instantiate_noexcept (decl, tf_error);
24151 }
24152 else if (VAR_P (decl))
24153 {
24154 start_lambda_scope (decl);
24155 DECL_INITIAL (decl) =
24156 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24157 tf_error, DECL_TI_TEMPLATE (decl));
24158 finish_lambda_scope ();
24159 if (VAR_HAD_UNKNOWN_BOUND (decl))
24160 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24161 tf_error, DECL_TI_TEMPLATE (decl));
24162 }
24163 else
24164 gcc_unreachable ();
24165
24166 pop_access_scope (decl);
24167 }
24168
24169 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24170 substituted to get DECL. */
24171
24172 tree
24173 template_for_substitution (tree decl)
24174 {
24175 tree tmpl = DECL_TI_TEMPLATE (decl);
24176
24177 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24178 for the instantiation. This is not always the most general
24179 template. Consider, for example:
24180
24181 template <class T>
24182 struct S { template <class U> void f();
24183 template <> void f<int>(); };
24184
24185 and an instantiation of S<double>::f<int>. We want TD to be the
24186 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24187 while (/* An instantiation cannot have a definition, so we need a
24188 more general template. */
24189 DECL_TEMPLATE_INSTANTIATION (tmpl)
24190 /* We must also deal with friend templates. Given:
24191
24192 template <class T> struct S {
24193 template <class U> friend void f() {};
24194 };
24195
24196 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24197 so far as the language is concerned, but that's still
24198 where we get the pattern for the instantiation from. On
24199 other hand, if the definition comes outside the class, say:
24200
24201 template <class T> struct S {
24202 template <class U> friend void f();
24203 };
24204 template <class U> friend void f() {}
24205
24206 we don't need to look any further. That's what the check for
24207 DECL_INITIAL is for. */
24208 || (TREE_CODE (decl) == FUNCTION_DECL
24209 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24210 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24211 {
24212 /* The present template, TD, should not be a definition. If it
24213 were a definition, we should be using it! Note that we
24214 cannot restructure the loop to just keep going until we find
24215 a template with a definition, since that might go too far if
24216 a specialization was declared, but not defined. */
24217
24218 /* Fetch the more general template. */
24219 tmpl = DECL_TI_TEMPLATE (tmpl);
24220 }
24221
24222 return tmpl;
24223 }
24224
24225 /* Returns true if we need to instantiate this template instance even if we
24226 know we aren't going to emit it. */
24227
24228 bool
24229 always_instantiate_p (tree decl)
24230 {
24231 /* We always instantiate inline functions so that we can inline them. An
24232 explicit instantiation declaration prohibits implicit instantiation of
24233 non-inline functions. With high levels of optimization, we would
24234 normally inline non-inline functions -- but we're not allowed to do
24235 that for "extern template" functions. Therefore, we check
24236 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24237 return ((TREE_CODE (decl) == FUNCTION_DECL
24238 && (DECL_DECLARED_INLINE_P (decl)
24239 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24240 /* And we need to instantiate static data members so that
24241 their initializers are available in integral constant
24242 expressions. */
24243 || (VAR_P (decl)
24244 && decl_maybe_constant_var_p (decl)));
24245 }
24246
24247 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24248 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24249 error, true otherwise. */
24250
24251 bool
24252 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24253 {
24254 tree fntype, spec, noex, clone;
24255
24256 /* Don't instantiate a noexcept-specification from template context. */
24257 if (processing_template_decl
24258 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24259 return true;
24260
24261 if (DECL_CLONED_FUNCTION_P (fn))
24262 fn = DECL_CLONED_FUNCTION (fn);
24263
24264 tree orig_fn = NULL_TREE;
24265 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24266 its FUNCTION_DECL for the rest of this function -- push_access_scope
24267 doesn't accept TEMPLATE_DECLs. */
24268 if (DECL_FUNCTION_TEMPLATE_P (fn))
24269 {
24270 orig_fn = fn;
24271 fn = DECL_TEMPLATE_RESULT (fn);
24272 }
24273
24274 fntype = TREE_TYPE (fn);
24275 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24276
24277 if (!spec || !TREE_PURPOSE (spec))
24278 return true;
24279
24280 noex = TREE_PURPOSE (spec);
24281
24282 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24283 {
24284 static hash_set<tree>* fns = new hash_set<tree>;
24285 bool added = false;
24286 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24287 {
24288 spec = get_defaulted_eh_spec (fn, complain);
24289 if (spec == error_mark_node)
24290 /* This might have failed because of an unparsed DMI, so
24291 let's try again later. */
24292 return false;
24293 }
24294 else if (!(added = !fns->add (fn)))
24295 {
24296 /* If hash_set::add returns true, the element was already there. */
24297 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24298 DECL_SOURCE_LOCATION (fn));
24299 error_at (loc,
24300 "exception specification of %qD depends on itself",
24301 fn);
24302 spec = noexcept_false_spec;
24303 }
24304 else if (push_tinst_level (fn))
24305 {
24306 push_access_scope (fn);
24307 push_deferring_access_checks (dk_no_deferred);
24308 input_location = DECL_SOURCE_LOCATION (fn);
24309
24310 tree save_ccp = current_class_ptr;
24311 tree save_ccr = current_class_ref;
24312 /* If needed, set current_class_ptr for the benefit of
24313 tsubst_copy/PARM_DECL. */
24314 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24315 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24316 {
24317 tree this_parm = DECL_ARGUMENTS (tdecl);
24318 current_class_ptr = NULL_TREE;
24319 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24320 current_class_ptr = this_parm;
24321 }
24322
24323 /* If this function is represented by a TEMPLATE_DECL, then
24324 the deferred noexcept-specification might still contain
24325 dependent types, even after substitution. And we need the
24326 dependency check functions to work in build_noexcept_spec. */
24327 if (orig_fn)
24328 ++processing_template_decl;
24329
24330 /* Do deferred instantiation of the noexcept-specifier. */
24331 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24332 DEFERRED_NOEXCEPT_ARGS (noex),
24333 tf_warning_or_error, fn,
24334 /*function_p=*/false,
24335 /*i_c_e_p=*/true);
24336
24337 current_class_ptr = save_ccp;
24338 current_class_ref = save_ccr;
24339
24340 /* Build up the noexcept-specification. */
24341 spec = build_noexcept_spec (noex, tf_warning_or_error);
24342
24343 if (orig_fn)
24344 --processing_template_decl;
24345
24346 pop_deferring_access_checks ();
24347 pop_access_scope (fn);
24348 pop_tinst_level ();
24349 }
24350 else
24351 spec = noexcept_false_spec;
24352
24353 if (added)
24354 fns->remove (fn);
24355
24356 if (spec == error_mark_node)
24357 {
24358 /* This failed with a hard error, so let's go with false. */
24359 gcc_assert (seen_error ());
24360 spec = noexcept_false_spec;
24361 }
24362
24363 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24364 if (orig_fn)
24365 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24366 }
24367
24368 FOR_EACH_CLONE (clone, fn)
24369 {
24370 if (TREE_TYPE (clone) == fntype)
24371 TREE_TYPE (clone) = TREE_TYPE (fn);
24372 else
24373 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24374 }
24375
24376 return true;
24377 }
24378
24379 /* We're starting to process the function INST, an instantiation of PATTERN;
24380 add their parameters to local_specializations. */
24381
24382 static void
24383 register_parameter_specializations (tree pattern, tree inst)
24384 {
24385 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24386 tree spec_parm = DECL_ARGUMENTS (inst);
24387 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24388 {
24389 register_local_specialization (spec_parm, tmpl_parm);
24390 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24391 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24392 }
24393 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24394 {
24395 if (!DECL_PACK_P (tmpl_parm))
24396 {
24397 register_local_specialization (spec_parm, tmpl_parm);
24398 spec_parm = DECL_CHAIN (spec_parm);
24399 }
24400 else
24401 {
24402 /* Register the (value) argument pack as a specialization of
24403 TMPL_PARM, then move on. */
24404 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24405 register_local_specialization (argpack, tmpl_parm);
24406 }
24407 }
24408 gcc_assert (!spec_parm);
24409 }
24410
24411 /* Produce the definition of D, a _DECL generated from a template. If
24412 DEFER_OK is true, then we don't have to actually do the
24413 instantiation now; we just have to do it sometime. Normally it is
24414 an error if this is an explicit instantiation but D is undefined.
24415 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24416 instantiated class template. */
24417
24418 tree
24419 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24420 {
24421 tree tmpl = DECL_TI_TEMPLATE (d);
24422 tree gen_args;
24423 tree args;
24424 tree td;
24425 tree code_pattern;
24426 tree spec;
24427 tree gen_tmpl;
24428 bool pattern_defined;
24429 location_t saved_loc = input_location;
24430 int saved_unevaluated_operand = cp_unevaluated_operand;
24431 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24432 bool external_p;
24433 bool deleted_p;
24434
24435 /* This function should only be used to instantiate templates for
24436 functions and static member variables. */
24437 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24438
24439 /* A concept is never instantiated. */
24440 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24441
24442 /* Variables are never deferred; if instantiation is required, they
24443 are instantiated right away. That allows for better code in the
24444 case that an expression refers to the value of the variable --
24445 if the variable has a constant value the referring expression can
24446 take advantage of that fact. */
24447 if (VAR_P (d))
24448 defer_ok = false;
24449
24450 /* Don't instantiate cloned functions. Instead, instantiate the
24451 functions they cloned. */
24452 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
24453 d = DECL_CLONED_FUNCTION (d);
24454
24455 if (DECL_TEMPLATE_INSTANTIATED (d)
24456 || (TREE_CODE (d) == FUNCTION_DECL
24457 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
24458 || DECL_TEMPLATE_SPECIALIZATION (d))
24459 /* D has already been instantiated or explicitly specialized, so
24460 there's nothing for us to do here.
24461
24462 It might seem reasonable to check whether or not D is an explicit
24463 instantiation, and, if so, stop here. But when an explicit
24464 instantiation is deferred until the end of the compilation,
24465 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24466 the instantiation. */
24467 return d;
24468
24469 /* Check to see whether we know that this template will be
24470 instantiated in some other file, as with "extern template"
24471 extension. */
24472 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
24473
24474 /* In general, we do not instantiate such templates. */
24475 if (external_p && !always_instantiate_p (d))
24476 return d;
24477
24478 gen_tmpl = most_general_template (tmpl);
24479 gen_args = DECL_TI_ARGS (d);
24480
24481 if (tmpl != gen_tmpl)
24482 /* We should already have the extra args. */
24483 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
24484 == TMPL_ARGS_DEPTH (gen_args));
24485 /* And what's in the hash table should match D. */
24486 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
24487 || spec == NULL_TREE);
24488
24489 /* This needs to happen before any tsubsting. */
24490 if (! push_tinst_level (d))
24491 return d;
24492
24493 timevar_push (TV_TEMPLATE_INST);
24494
24495 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24496 for the instantiation. */
24497 td = template_for_substitution (d);
24498 args = gen_args;
24499
24500 if (VAR_P (d))
24501 {
24502 /* Look up an explicit specialization, if any. */
24503 tree tid = lookup_template_variable (gen_tmpl, gen_args);
24504 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
24505 if (elt && elt != error_mark_node)
24506 {
24507 td = TREE_VALUE (elt);
24508 args = TREE_PURPOSE (elt);
24509 }
24510 }
24511
24512 code_pattern = DECL_TEMPLATE_RESULT (td);
24513
24514 /* We should never be trying to instantiate a member of a class
24515 template or partial specialization. */
24516 gcc_assert (d != code_pattern);
24517
24518 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
24519 || DECL_TEMPLATE_SPECIALIZATION (td))
24520 /* In the case of a friend template whose definition is provided
24521 outside the class, we may have too many arguments. Drop the
24522 ones we don't need. The same is true for specializations. */
24523 args = get_innermost_template_args
24524 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
24525
24526 if (TREE_CODE (d) == FUNCTION_DECL)
24527 {
24528 deleted_p = DECL_DELETED_FN (code_pattern);
24529 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
24530 && DECL_INITIAL (code_pattern) != error_mark_node)
24531 || DECL_DEFAULTED_FN (code_pattern)
24532 || deleted_p);
24533 }
24534 else
24535 {
24536 deleted_p = false;
24537 if (DECL_CLASS_SCOPE_P (code_pattern))
24538 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
24539 else
24540 pattern_defined = ! DECL_EXTERNAL (code_pattern);
24541 }
24542
24543 /* We may be in the middle of deferred access check. Disable it now. */
24544 push_deferring_access_checks (dk_no_deferred);
24545
24546 /* Unless an explicit instantiation directive has already determined
24547 the linkage of D, remember that a definition is available for
24548 this entity. */
24549 if (pattern_defined
24550 && !DECL_INTERFACE_KNOWN (d)
24551 && !DECL_NOT_REALLY_EXTERN (d))
24552 mark_definable (d);
24553
24554 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24555 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24556 input_location = DECL_SOURCE_LOCATION (d);
24557
24558 /* If D is a member of an explicitly instantiated class template,
24559 and no definition is available, treat it like an implicit
24560 instantiation. */
24561 if (!pattern_defined && expl_inst_class_mem_p
24562 && DECL_EXPLICIT_INSTANTIATION (d))
24563 {
24564 /* Leave linkage flags alone on instantiations with anonymous
24565 visibility. */
24566 if (TREE_PUBLIC (d))
24567 {
24568 DECL_NOT_REALLY_EXTERN (d) = 0;
24569 DECL_INTERFACE_KNOWN (d) = 0;
24570 }
24571 SET_DECL_IMPLICIT_INSTANTIATION (d);
24572 }
24573
24574 /* Defer all other templates, unless we have been explicitly
24575 forbidden from doing so. */
24576 if (/* If there is no definition, we cannot instantiate the
24577 template. */
24578 ! pattern_defined
24579 /* If it's OK to postpone instantiation, do so. */
24580 || defer_ok
24581 /* If this is a static data member that will be defined
24582 elsewhere, we don't want to instantiate the entire data
24583 member, but we do want to instantiate the initializer so that
24584 we can substitute that elsewhere. */
24585 || (external_p && VAR_P (d))
24586 /* Handle here a deleted function too, avoid generating
24587 its body (c++/61080). */
24588 || deleted_p)
24589 {
24590 /* The definition of the static data member is now required so
24591 we must substitute the initializer. */
24592 if (VAR_P (d)
24593 && !DECL_INITIAL (d)
24594 && DECL_INITIAL (code_pattern))
24595 {
24596 tree ns;
24597 tree init;
24598 bool const_init = false;
24599 bool enter_context = DECL_CLASS_SCOPE_P (d);
24600
24601 ns = decl_namespace_context (d);
24602 push_nested_namespace (ns);
24603 if (enter_context)
24604 push_nested_class (DECL_CONTEXT (d));
24605 init = tsubst_expr (DECL_INITIAL (code_pattern),
24606 args,
24607 tf_warning_or_error, NULL_TREE,
24608 /*integral_constant_expression_p=*/false);
24609 /* If instantiating the initializer involved instantiating this
24610 again, don't call cp_finish_decl twice. */
24611 if (!DECL_INITIAL (d))
24612 {
24613 /* Make sure the initializer is still constant, in case of
24614 circular dependency (template/instantiate6.C). */
24615 const_init
24616 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24617 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24618 /*asmspec_tree=*/NULL_TREE,
24619 LOOKUP_ONLYCONVERTING);
24620 }
24621 if (enter_context)
24622 pop_nested_class ();
24623 pop_nested_namespace (ns);
24624 }
24625
24626 /* We restore the source position here because it's used by
24627 add_pending_template. */
24628 input_location = saved_loc;
24629
24630 if (at_eof && !pattern_defined
24631 && DECL_EXPLICIT_INSTANTIATION (d)
24632 && DECL_NOT_REALLY_EXTERN (d))
24633 /* [temp.explicit]
24634
24635 The definition of a non-exported function template, a
24636 non-exported member function template, or a non-exported
24637 member function or static data member of a class template
24638 shall be present in every translation unit in which it is
24639 explicitly instantiated. */
24640 permerror (input_location, "explicit instantiation of %qD "
24641 "but no definition available", d);
24642
24643 /* If we're in unevaluated context, we just wanted to get the
24644 constant value; this isn't an odr use, so don't queue
24645 a full instantiation. */
24646 if (cp_unevaluated_operand != 0)
24647 goto out;
24648 /* ??? Historically, we have instantiated inline functions, even
24649 when marked as "extern template". */
24650 if (!(external_p && VAR_P (d)))
24651 add_pending_template (d);
24652 goto out;
24653 }
24654 /* Tell the repository that D is available in this translation unit
24655 -- and see if it is supposed to be instantiated here. */
24656 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
24657 {
24658 /* In a PCH file, despite the fact that the repository hasn't
24659 requested instantiation in the PCH it is still possible that
24660 an instantiation will be required in a file that includes the
24661 PCH. */
24662 if (pch_file)
24663 add_pending_template (d);
24664 /* Instantiate inline functions so that the inliner can do its
24665 job, even though we'll not be emitting a copy of this
24666 function. */
24667 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
24668 goto out;
24669 }
24670
24671 bool push_to_top, nested;
24672 tree fn_context;
24673 fn_context = decl_function_context (d);
24674 if (LAMBDA_FUNCTION_P (d))
24675 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24676 fn_context = NULL_TREE;
24677 nested = current_function_decl != NULL_TREE;
24678 push_to_top = !(nested && fn_context == current_function_decl);
24679
24680 vec<tree> omp_privatization_save;
24681 if (nested)
24682 save_omp_privatization_clauses (omp_privatization_save);
24683
24684 if (push_to_top)
24685 push_to_top_level ();
24686 else
24687 {
24688 gcc_assert (!processing_template_decl);
24689 push_function_context ();
24690 cp_unevaluated_operand = 0;
24691 c_inhibit_evaluation_warnings = 0;
24692 }
24693
24694 /* Mark D as instantiated so that recursive calls to
24695 instantiate_decl do not try to instantiate it again. */
24696 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24697
24698 /* Regenerate the declaration in case the template has been modified
24699 by a subsequent redeclaration. */
24700 regenerate_decl_from_template (d, td, args);
24701
24702 /* We already set the file and line above. Reset them now in case
24703 they changed as a result of calling regenerate_decl_from_template. */
24704 input_location = DECL_SOURCE_LOCATION (d);
24705
24706 if (VAR_P (d))
24707 {
24708 tree init;
24709 bool const_init = false;
24710
24711 /* Clear out DECL_RTL; whatever was there before may not be right
24712 since we've reset the type of the declaration. */
24713 SET_DECL_RTL (d, NULL);
24714 DECL_IN_AGGR_P (d) = 0;
24715
24716 /* The initializer is placed in DECL_INITIAL by
24717 regenerate_decl_from_template so we don't need to
24718 push/pop_access_scope again here. Pull it out so that
24719 cp_finish_decl can process it. */
24720 init = DECL_INITIAL (d);
24721 DECL_INITIAL (d) = NULL_TREE;
24722 DECL_INITIALIZED_P (d) = 0;
24723
24724 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24725 initializer. That function will defer actual emission until
24726 we have a chance to determine linkage. */
24727 DECL_EXTERNAL (d) = 0;
24728
24729 /* Enter the scope of D so that access-checking works correctly. */
24730 bool enter_context = DECL_CLASS_SCOPE_P (d);
24731 if (enter_context)
24732 push_nested_class (DECL_CONTEXT (d));
24733
24734 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24735 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24736
24737 if (enter_context)
24738 pop_nested_class ();
24739
24740 if (variable_template_p (gen_tmpl))
24741 note_variable_template_instantiation (d);
24742 }
24743 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24744 synthesize_method (d);
24745 else if (TREE_CODE (d) == FUNCTION_DECL)
24746 {
24747 /* Set up the list of local specializations. */
24748 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24749 tree block = NULL_TREE;
24750
24751 /* Set up context. */
24752 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24753 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24754 block = push_stmt_list ();
24755 else
24756 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24757
24758 /* Some typedefs referenced from within the template code need to be
24759 access checked at template instantiation time, i.e now. These
24760 types were added to the template at parsing time. Let's get those
24761 and perform the access checks then. */
24762 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24763 args);
24764
24765 /* Create substitution entries for the parameters. */
24766 register_parameter_specializations (code_pattern, d);
24767
24768 /* Substitute into the body of the function. */
24769 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24770 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24771 tf_warning_or_error, tmpl);
24772 else
24773 {
24774 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24775 tf_warning_or_error, tmpl,
24776 /*integral_constant_expression_p=*/false);
24777
24778 /* Set the current input_location to the end of the function
24779 so that finish_function knows where we are. */
24780 input_location
24781 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24782
24783 /* Remember if we saw an infinite loop in the template. */
24784 current_function_infinite_loop
24785 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24786 }
24787
24788 /* Finish the function. */
24789 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24790 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24791 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24792 else
24793 {
24794 d = finish_function (/*inline_p=*/false);
24795 expand_or_defer_fn (d);
24796 }
24797
24798 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24799 cp_check_omp_declare_reduction (d);
24800 }
24801
24802 /* We're not deferring instantiation any more. */
24803 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24804
24805 if (push_to_top)
24806 pop_from_top_level ();
24807 else
24808 pop_function_context ();
24809
24810 if (nested)
24811 restore_omp_privatization_clauses (omp_privatization_save);
24812
24813 out:
24814 pop_deferring_access_checks ();
24815 timevar_pop (TV_TEMPLATE_INST);
24816 pop_tinst_level ();
24817 input_location = saved_loc;
24818 cp_unevaluated_operand = saved_unevaluated_operand;
24819 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24820
24821 return d;
24822 }
24823
24824 /* Run through the list of templates that we wish we could
24825 instantiate, and instantiate any we can. RETRIES is the
24826 number of times we retry pending template instantiation. */
24827
24828 void
24829 instantiate_pending_templates (int retries)
24830 {
24831 int reconsider;
24832 location_t saved_loc = input_location;
24833
24834 /* Instantiating templates may trigger vtable generation. This in turn
24835 may require further template instantiations. We place a limit here
24836 to avoid infinite loop. */
24837 if (pending_templates && retries >= max_tinst_depth)
24838 {
24839 tree decl = pending_templates->tinst->maybe_get_node ();
24840
24841 fatal_error (input_location,
24842 "template instantiation depth exceeds maximum of %d"
24843 " instantiating %q+D, possibly from virtual table generation"
24844 " (use %<-ftemplate-depth=%> to increase the maximum)",
24845 max_tinst_depth, decl);
24846 if (TREE_CODE (decl) == FUNCTION_DECL)
24847 /* Pretend that we defined it. */
24848 DECL_INITIAL (decl) = error_mark_node;
24849 return;
24850 }
24851
24852 do
24853 {
24854 struct pending_template **t = &pending_templates;
24855 struct pending_template *last = NULL;
24856 reconsider = 0;
24857 while (*t)
24858 {
24859 tree instantiation = reopen_tinst_level ((*t)->tinst);
24860 bool complete = false;
24861
24862 if (TYPE_P (instantiation))
24863 {
24864 if (!COMPLETE_TYPE_P (instantiation))
24865 {
24866 instantiate_class_template (instantiation);
24867 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24868 for (tree fld = TYPE_FIELDS (instantiation);
24869 fld; fld = TREE_CHAIN (fld))
24870 if ((VAR_P (fld)
24871 || (TREE_CODE (fld) == FUNCTION_DECL
24872 && !DECL_ARTIFICIAL (fld)))
24873 && DECL_TEMPLATE_INSTANTIATION (fld))
24874 instantiate_decl (fld,
24875 /*defer_ok=*/false,
24876 /*expl_inst_class_mem_p=*/false);
24877
24878 if (COMPLETE_TYPE_P (instantiation))
24879 reconsider = 1;
24880 }
24881
24882 complete = COMPLETE_TYPE_P (instantiation);
24883 }
24884 else
24885 {
24886 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24887 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24888 {
24889 instantiation
24890 = instantiate_decl (instantiation,
24891 /*defer_ok=*/false,
24892 /*expl_inst_class_mem_p=*/false);
24893 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24894 reconsider = 1;
24895 }
24896
24897 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24898 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24899 }
24900
24901 if (complete)
24902 {
24903 /* If INSTANTIATION has been instantiated, then we don't
24904 need to consider it again in the future. */
24905 struct pending_template *drop = *t;
24906 *t = (*t)->next;
24907 set_refcount_ptr (drop->tinst);
24908 pending_template_freelist ().free (drop);
24909 }
24910 else
24911 {
24912 last = *t;
24913 t = &(*t)->next;
24914 }
24915 tinst_depth = 0;
24916 set_refcount_ptr (current_tinst_level);
24917 }
24918 last_pending_template = last;
24919 }
24920 while (reconsider);
24921
24922 input_location = saved_loc;
24923 }
24924
24925 /* Substitute ARGVEC into T, which is a list of initializers for
24926 either base class or a non-static data member. The TREE_PURPOSEs
24927 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24928 instantiate_decl. */
24929
24930 static tree
24931 tsubst_initializer_list (tree t, tree argvec)
24932 {
24933 tree inits = NULL_TREE;
24934 tree target_ctor = error_mark_node;
24935
24936 for (; t; t = TREE_CHAIN (t))
24937 {
24938 tree decl;
24939 tree init;
24940 tree expanded_bases = NULL_TREE;
24941 tree expanded_arguments = NULL_TREE;
24942 int i, len = 1;
24943
24944 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24945 {
24946 tree expr;
24947 tree arg;
24948
24949 /* Expand the base class expansion type into separate base
24950 classes. */
24951 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24952 tf_warning_or_error,
24953 NULL_TREE);
24954 if (expanded_bases == error_mark_node)
24955 continue;
24956
24957 /* We'll be building separate TREE_LISTs of arguments for
24958 each base. */
24959 len = TREE_VEC_LENGTH (expanded_bases);
24960 expanded_arguments = make_tree_vec (len);
24961 for (i = 0; i < len; i++)
24962 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24963
24964 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24965 expand each argument in the TREE_VALUE of t. */
24966 expr = make_node (EXPR_PACK_EXPANSION);
24967 PACK_EXPANSION_LOCAL_P (expr) = true;
24968 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24969 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24970
24971 if (TREE_VALUE (t) == void_type_node)
24972 /* VOID_TYPE_NODE is used to indicate
24973 value-initialization. */
24974 {
24975 for (i = 0; i < len; i++)
24976 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24977 }
24978 else
24979 {
24980 /* Substitute parameter packs into each argument in the
24981 TREE_LIST. */
24982 in_base_initializer = 1;
24983 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24984 {
24985 tree expanded_exprs;
24986
24987 /* Expand the argument. */
24988 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24989 expanded_exprs
24990 = tsubst_pack_expansion (expr, argvec,
24991 tf_warning_or_error,
24992 NULL_TREE);
24993 if (expanded_exprs == error_mark_node)
24994 continue;
24995
24996 /* Prepend each of the expanded expressions to the
24997 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24998 for (i = 0; i < len; i++)
24999 {
25000 TREE_VEC_ELT (expanded_arguments, i) =
25001 tree_cons (NULL_TREE,
25002 TREE_VEC_ELT (expanded_exprs, i),
25003 TREE_VEC_ELT (expanded_arguments, i));
25004 }
25005 }
25006 in_base_initializer = 0;
25007
25008 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25009 since we built them backwards. */
25010 for (i = 0; i < len; i++)
25011 {
25012 TREE_VEC_ELT (expanded_arguments, i) =
25013 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25014 }
25015 }
25016 }
25017
25018 for (i = 0; i < len; ++i)
25019 {
25020 if (expanded_bases)
25021 {
25022 decl = TREE_VEC_ELT (expanded_bases, i);
25023 decl = expand_member_init (decl);
25024 init = TREE_VEC_ELT (expanded_arguments, i);
25025 }
25026 else
25027 {
25028 tree tmp;
25029 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25030 tf_warning_or_error, NULL_TREE);
25031
25032 decl = expand_member_init (decl);
25033 if (decl && !DECL_P (decl))
25034 in_base_initializer = 1;
25035
25036 init = TREE_VALUE (t);
25037 tmp = init;
25038 if (init != void_type_node)
25039 init = tsubst_expr (init, argvec,
25040 tf_warning_or_error, NULL_TREE,
25041 /*integral_constant_expression_p=*/false);
25042 if (init == NULL_TREE && tmp != NULL_TREE)
25043 /* If we had an initializer but it instantiated to nothing,
25044 value-initialize the object. This will only occur when
25045 the initializer was a pack expansion where the parameter
25046 packs used in that expansion were of length zero. */
25047 init = void_type_node;
25048 in_base_initializer = 0;
25049 }
25050
25051 if (target_ctor != error_mark_node
25052 && init != error_mark_node)
25053 {
25054 error ("mem-initializer for %qD follows constructor delegation",
25055 decl);
25056 return inits;
25057 }
25058 /* Look for a target constructor. */
25059 if (init != error_mark_node
25060 && decl && CLASS_TYPE_P (decl)
25061 && same_type_p (decl, current_class_type))
25062 {
25063 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25064 if (inits)
25065 {
25066 error ("constructor delegation follows mem-initializer for %qD",
25067 TREE_PURPOSE (inits));
25068 continue;
25069 }
25070 target_ctor = init;
25071 }
25072
25073 if (decl)
25074 {
25075 init = build_tree_list (decl, init);
25076 TREE_CHAIN (init) = inits;
25077 inits = init;
25078 }
25079 }
25080 }
25081 return inits;
25082 }
25083
25084 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25085
25086 static void
25087 set_current_access_from_decl (tree decl)
25088 {
25089 if (TREE_PRIVATE (decl))
25090 current_access_specifier = access_private_node;
25091 else if (TREE_PROTECTED (decl))
25092 current_access_specifier = access_protected_node;
25093 else
25094 current_access_specifier = access_public_node;
25095 }
25096
25097 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25098 is the instantiation (which should have been created with
25099 start_enum) and ARGS are the template arguments to use. */
25100
25101 static void
25102 tsubst_enum (tree tag, tree newtag, tree args)
25103 {
25104 tree e;
25105
25106 if (SCOPED_ENUM_P (newtag))
25107 begin_scope (sk_scoped_enum, newtag);
25108
25109 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25110 {
25111 tree value;
25112 tree decl;
25113
25114 decl = TREE_VALUE (e);
25115 /* Note that in a template enum, the TREE_VALUE is the
25116 CONST_DECL, not the corresponding INTEGER_CST. */
25117 value = tsubst_expr (DECL_INITIAL (decl),
25118 args, tf_warning_or_error, NULL_TREE,
25119 /*integral_constant_expression_p=*/true);
25120
25121 /* Give this enumeration constant the correct access. */
25122 set_current_access_from_decl (decl);
25123
25124 /* Actually build the enumerator itself. Here we're assuming that
25125 enumerators can't have dependent attributes. */
25126 build_enumerator (DECL_NAME (decl), value, newtag,
25127 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25128 }
25129
25130 if (SCOPED_ENUM_P (newtag))
25131 finish_scope ();
25132
25133 finish_enum_value_list (newtag);
25134 finish_enum (newtag);
25135
25136 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25137 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25138 }
25139
25140 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25141 its type -- but without substituting the innermost set of template
25142 arguments. So, innermost set of template parameters will appear in
25143 the type. */
25144
25145 tree
25146 get_mostly_instantiated_function_type (tree decl)
25147 {
25148 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25149 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25150 }
25151
25152 /* Return truthvalue if we're processing a template different from
25153 the last one involved in diagnostics. */
25154 bool
25155 problematic_instantiation_changed (void)
25156 {
25157 return current_tinst_level != last_error_tinst_level;
25158 }
25159
25160 /* Remember current template involved in diagnostics. */
25161 void
25162 record_last_problematic_instantiation (void)
25163 {
25164 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25165 }
25166
25167 struct tinst_level *
25168 current_instantiation (void)
25169 {
25170 return current_tinst_level;
25171 }
25172
25173 /* Return TRUE if current_function_decl is being instantiated, false
25174 otherwise. */
25175
25176 bool
25177 instantiating_current_function_p (void)
25178 {
25179 return (current_instantiation ()
25180 && (current_instantiation ()->maybe_get_node ()
25181 == current_function_decl));
25182 }
25183
25184 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25185 type. Return false for ok, true for disallowed. Issue error and
25186 inform messages under control of COMPLAIN. */
25187
25188 static bool
25189 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25190 {
25191 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25192 return false;
25193 else if (TYPE_PTR_P (type))
25194 return false;
25195 else if (TYPE_REF_P (type)
25196 && !TYPE_REF_IS_RVALUE (type))
25197 return false;
25198 else if (TYPE_PTRMEM_P (type))
25199 return false;
25200 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25201 return false;
25202 else if (TREE_CODE (type) == TYPENAME_TYPE)
25203 return false;
25204 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25205 return false;
25206 else if (TREE_CODE (type) == NULLPTR_TYPE)
25207 return false;
25208 /* A bound template template parm could later be instantiated to have a valid
25209 nontype parm type via an alias template. */
25210 else if (cxx_dialect >= cxx11
25211 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25212 return false;
25213 else if (CLASS_TYPE_P (type))
25214 {
25215 if (cxx_dialect < cxx2a)
25216 {
25217 error ("non-type template parameters of class type only available "
25218 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25219 return true;
25220 }
25221 if (!complete_type_or_else (type, NULL_TREE))
25222 return true;
25223 if (!literal_type_p (type))
25224 {
25225 error ("%qT is not a valid type for a template non-type parameter "
25226 "because it is not literal", type);
25227 explain_non_literal_class (type);
25228 return true;
25229 }
25230 if (cp_has_mutable_p (type))
25231 {
25232 error ("%qT is not a valid type for a template non-type parameter "
25233 "because it has a mutable member", type);
25234 return true;
25235 }
25236 /* FIXME check op<=> and strong structural equality once spaceship is
25237 implemented. */
25238 return false;
25239 }
25240
25241 if (complain & tf_error)
25242 {
25243 if (type == error_mark_node)
25244 inform (input_location, "invalid template non-type parameter");
25245 else
25246 error ("%q#T is not a valid type for a template non-type parameter",
25247 type);
25248 }
25249 return true;
25250 }
25251
25252 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25253 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25254
25255 static bool
25256 dependent_type_p_r (tree type)
25257 {
25258 tree scope;
25259
25260 /* [temp.dep.type]
25261
25262 A type is dependent if it is:
25263
25264 -- a template parameter. Template template parameters are types
25265 for us (since TYPE_P holds true for them) so we handle
25266 them here. */
25267 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25268 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25269 return true;
25270 /* -- a qualified-id with a nested-name-specifier which contains a
25271 class-name that names a dependent type or whose unqualified-id
25272 names a dependent type. */
25273 if (TREE_CODE (type) == TYPENAME_TYPE)
25274 return true;
25275
25276 /* An alias template specialization can be dependent even if the
25277 resulting type is not. */
25278 if (dependent_alias_template_spec_p (type))
25279 return true;
25280
25281 /* -- a cv-qualified type where the cv-unqualified type is
25282 dependent.
25283 No code is necessary for this bullet; the code below handles
25284 cv-qualified types, and we don't want to strip aliases with
25285 TYPE_MAIN_VARIANT because of DR 1558. */
25286 /* -- a compound type constructed from any dependent type. */
25287 if (TYPE_PTRMEM_P (type))
25288 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25289 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25290 (type)));
25291 else if (INDIRECT_TYPE_P (type))
25292 return dependent_type_p (TREE_TYPE (type));
25293 else if (TREE_CODE (type) == FUNCTION_TYPE
25294 || TREE_CODE (type) == METHOD_TYPE)
25295 {
25296 tree arg_type;
25297
25298 if (dependent_type_p (TREE_TYPE (type)))
25299 return true;
25300 for (arg_type = TYPE_ARG_TYPES (type);
25301 arg_type;
25302 arg_type = TREE_CHAIN (arg_type))
25303 if (dependent_type_p (TREE_VALUE (arg_type)))
25304 return true;
25305 if (cxx_dialect >= cxx17)
25306 /* A value-dependent noexcept-specifier makes the type dependent. */
25307 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25308 if (tree noex = TREE_PURPOSE (spec))
25309 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25310 affect overload resolution and treating it as dependent breaks
25311 things. */
25312 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25313 && value_dependent_expression_p (noex))
25314 return true;
25315 return false;
25316 }
25317 /* -- an array type constructed from any dependent type or whose
25318 size is specified by a constant expression that is
25319 value-dependent.
25320
25321 We checked for type- and value-dependence of the bounds in
25322 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25323 if (TREE_CODE (type) == ARRAY_TYPE)
25324 {
25325 if (TYPE_DOMAIN (type)
25326 && dependent_type_p (TYPE_DOMAIN (type)))
25327 return true;
25328 return dependent_type_p (TREE_TYPE (type));
25329 }
25330
25331 /* -- a template-id in which either the template name is a template
25332 parameter ... */
25333 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25334 return true;
25335 /* ... or any of the template arguments is a dependent type or
25336 an expression that is type-dependent or value-dependent. */
25337 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25338 && (any_dependent_template_arguments_p
25339 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25340 return true;
25341
25342 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25343 dependent; if the argument of the `typeof' expression is not
25344 type-dependent, then it should already been have resolved. */
25345 if (TREE_CODE (type) == TYPEOF_TYPE
25346 || TREE_CODE (type) == DECLTYPE_TYPE
25347 || TREE_CODE (type) == UNDERLYING_TYPE)
25348 return true;
25349
25350 /* A template argument pack is dependent if any of its packed
25351 arguments are. */
25352 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25353 {
25354 tree args = ARGUMENT_PACK_ARGS (type);
25355 int i, len = TREE_VEC_LENGTH (args);
25356 for (i = 0; i < len; ++i)
25357 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25358 return true;
25359 }
25360
25361 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25362 be template parameters. */
25363 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25364 return true;
25365
25366 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25367 return true;
25368
25369 /* The standard does not specifically mention types that are local
25370 to template functions or local classes, but they should be
25371 considered dependent too. For example:
25372
25373 template <int I> void f() {
25374 enum E { a = I };
25375 S<sizeof (E)> s;
25376 }
25377
25378 The size of `E' cannot be known until the value of `I' has been
25379 determined. Therefore, `E' must be considered dependent. */
25380 scope = TYPE_CONTEXT (type);
25381 if (scope && TYPE_P (scope))
25382 return dependent_type_p (scope);
25383 /* Don't use type_dependent_expression_p here, as it can lead
25384 to infinite recursion trying to determine whether a lambda
25385 nested in a lambda is dependent (c++/47687). */
25386 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25387 && DECL_LANG_SPECIFIC (scope)
25388 && DECL_TEMPLATE_INFO (scope)
25389 && (any_dependent_template_arguments_p
25390 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25391 return true;
25392
25393 /* Other types are non-dependent. */
25394 return false;
25395 }
25396
25397 /* Returns TRUE if TYPE is dependent, in the sense of
25398 [temp.dep.type]. Note that a NULL type is considered dependent. */
25399
25400 bool
25401 dependent_type_p (tree type)
25402 {
25403 /* If there are no template parameters in scope, then there can't be
25404 any dependent types. */
25405 if (!processing_template_decl)
25406 {
25407 /* If we are not processing a template, then nobody should be
25408 providing us with a dependent type. */
25409 gcc_assert (type);
25410 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25411 return false;
25412 }
25413
25414 /* If the type is NULL, we have not computed a type for the entity
25415 in question; in that case, the type is dependent. */
25416 if (!type)
25417 return true;
25418
25419 /* Erroneous types can be considered non-dependent. */
25420 if (type == error_mark_node)
25421 return false;
25422
25423 /* Getting here with global_type_node means we improperly called this
25424 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25425 gcc_checking_assert (type != global_type_node);
25426
25427 /* If we have not already computed the appropriate value for TYPE,
25428 do so now. */
25429 if (!TYPE_DEPENDENT_P_VALID (type))
25430 {
25431 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25432 TYPE_DEPENDENT_P_VALID (type) = 1;
25433 }
25434
25435 return TYPE_DEPENDENT_P (type);
25436 }
25437
25438 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25439 lookup. In other words, a dependent type that is not the current
25440 instantiation. */
25441
25442 bool
25443 dependent_scope_p (tree scope)
25444 {
25445 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25446 && !currently_open_class (scope));
25447 }
25448
25449 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25450 an unknown base of 'this' (and is therefore instantiation-dependent). */
25451
25452 static bool
25453 unknown_base_ref_p (tree t)
25454 {
25455 if (!current_class_ptr)
25456 return false;
25457
25458 tree mem = TREE_OPERAND (t, 1);
25459 if (shared_member_p (mem))
25460 return false;
25461
25462 tree cur = current_nonlambda_class_type ();
25463 if (!any_dependent_bases_p (cur))
25464 return false;
25465
25466 tree ctx = TREE_OPERAND (t, 0);
25467 if (DERIVED_FROM_P (ctx, cur))
25468 return false;
25469
25470 return true;
25471 }
25472
25473 /* T is a SCOPE_REF; return whether we need to consider it
25474 instantiation-dependent so that we can check access at instantiation
25475 time even though we know which member it resolves to. */
25476
25477 static bool
25478 instantiation_dependent_scope_ref_p (tree t)
25479 {
25480 if (DECL_P (TREE_OPERAND (t, 1))
25481 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
25482 && !unknown_base_ref_p (t)
25483 && accessible_in_template_p (TREE_OPERAND (t, 0),
25484 TREE_OPERAND (t, 1)))
25485 return false;
25486 else
25487 return true;
25488 }
25489
25490 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25491 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25492 expression. */
25493
25494 /* Note that this predicate is not appropriate for general expressions;
25495 only constant expressions (that satisfy potential_constant_expression)
25496 can be tested for value dependence. */
25497
25498 bool
25499 value_dependent_expression_p (tree expression)
25500 {
25501 if (!processing_template_decl || expression == NULL_TREE)
25502 return false;
25503
25504 /* A type-dependent expression is also value-dependent. */
25505 if (type_dependent_expression_p (expression))
25506 return true;
25507
25508 switch (TREE_CODE (expression))
25509 {
25510 case BASELINK:
25511 /* A dependent member function of the current instantiation. */
25512 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
25513
25514 case FUNCTION_DECL:
25515 /* A dependent member function of the current instantiation. */
25516 if (DECL_CLASS_SCOPE_P (expression)
25517 && dependent_type_p (DECL_CONTEXT (expression)))
25518 return true;
25519 break;
25520
25521 case IDENTIFIER_NODE:
25522 /* A name that has not been looked up -- must be dependent. */
25523 return true;
25524
25525 case TEMPLATE_PARM_INDEX:
25526 /* A non-type template parm. */
25527 return true;
25528
25529 case CONST_DECL:
25530 /* A non-type template parm. */
25531 if (DECL_TEMPLATE_PARM_P (expression))
25532 return true;
25533 return value_dependent_expression_p (DECL_INITIAL (expression));
25534
25535 case VAR_DECL:
25536 /* A constant with literal type and is initialized
25537 with an expression that is value-dependent. */
25538 if (DECL_DEPENDENT_INIT_P (expression)
25539 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25540 || TYPE_REF_P (TREE_TYPE (expression)))
25541 return true;
25542 if (DECL_HAS_VALUE_EXPR_P (expression))
25543 {
25544 tree value_expr = DECL_VALUE_EXPR (expression);
25545 if (value_dependent_expression_p (value_expr))
25546 return true;
25547 }
25548 return false;
25549
25550 case DYNAMIC_CAST_EXPR:
25551 case STATIC_CAST_EXPR:
25552 case CONST_CAST_EXPR:
25553 case REINTERPRET_CAST_EXPR:
25554 case CAST_EXPR:
25555 case IMPLICIT_CONV_EXPR:
25556 /* These expressions are value-dependent if the type to which
25557 the cast occurs is dependent or the expression being casted
25558 is value-dependent. */
25559 {
25560 tree type = TREE_TYPE (expression);
25561
25562 if (dependent_type_p (type))
25563 return true;
25564
25565 /* A functional cast has a list of operands. */
25566 expression = TREE_OPERAND (expression, 0);
25567 if (!expression)
25568 {
25569 /* If there are no operands, it must be an expression such
25570 as "int()". This should not happen for aggregate types
25571 because it would form non-constant expressions. */
25572 gcc_assert (cxx_dialect >= cxx11
25573 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25574
25575 return false;
25576 }
25577
25578 if (TREE_CODE (expression) == TREE_LIST)
25579 return any_value_dependent_elements_p (expression);
25580
25581 return value_dependent_expression_p (expression);
25582 }
25583
25584 case SIZEOF_EXPR:
25585 if (SIZEOF_EXPR_TYPE_P (expression))
25586 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25587 /* FALLTHRU */
25588 case ALIGNOF_EXPR:
25589 case TYPEID_EXPR:
25590 /* A `sizeof' expression is value-dependent if the operand is
25591 type-dependent or is a pack expansion. */
25592 expression = TREE_OPERAND (expression, 0);
25593 if (PACK_EXPANSION_P (expression))
25594 return true;
25595 else if (TYPE_P (expression))
25596 return dependent_type_p (expression);
25597 return instantiation_dependent_uneval_expression_p (expression);
25598
25599 case AT_ENCODE_EXPR:
25600 /* An 'encode' expression is value-dependent if the operand is
25601 type-dependent. */
25602 expression = TREE_OPERAND (expression, 0);
25603 return dependent_type_p (expression);
25604
25605 case NOEXCEPT_EXPR:
25606 expression = TREE_OPERAND (expression, 0);
25607 return instantiation_dependent_uneval_expression_p (expression);
25608
25609 case SCOPE_REF:
25610 /* All instantiation-dependent expressions should also be considered
25611 value-dependent. */
25612 return instantiation_dependent_scope_ref_p (expression);
25613
25614 case COMPONENT_REF:
25615 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25616 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25617
25618 case NONTYPE_ARGUMENT_PACK:
25619 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25620 is value-dependent. */
25621 {
25622 tree values = ARGUMENT_PACK_ARGS (expression);
25623 int i, len = TREE_VEC_LENGTH (values);
25624
25625 for (i = 0; i < len; ++i)
25626 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25627 return true;
25628
25629 return false;
25630 }
25631
25632 case TRAIT_EXPR:
25633 {
25634 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25635
25636 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25637 return true;
25638
25639 if (!type2)
25640 return false;
25641
25642 if (TREE_CODE (type2) != TREE_LIST)
25643 return dependent_type_p (type2);
25644
25645 for (; type2; type2 = TREE_CHAIN (type2))
25646 if (dependent_type_p (TREE_VALUE (type2)))
25647 return true;
25648
25649 return false;
25650 }
25651
25652 case MODOP_EXPR:
25653 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25654 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25655
25656 case ARRAY_REF:
25657 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25658 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25659
25660 case ADDR_EXPR:
25661 {
25662 tree op = TREE_OPERAND (expression, 0);
25663 return (value_dependent_expression_p (op)
25664 || has_value_dependent_address (op));
25665 }
25666
25667 case REQUIRES_EXPR:
25668 /* Treat all requires-expressions as value-dependent so
25669 we don't try to fold them. */
25670 return true;
25671
25672 case TYPE_REQ:
25673 return dependent_type_p (TREE_OPERAND (expression, 0));
25674
25675 case CALL_EXPR:
25676 {
25677 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25678 return true;
25679 tree fn = get_callee_fndecl (expression);
25680 int i, nargs;
25681 nargs = call_expr_nargs (expression);
25682 for (i = 0; i < nargs; ++i)
25683 {
25684 tree op = CALL_EXPR_ARG (expression, i);
25685 /* In a call to a constexpr member function, look through the
25686 implicit ADDR_EXPR on the object argument so that it doesn't
25687 cause the call to be considered value-dependent. We also
25688 look through it in potential_constant_expression. */
25689 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25690 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25691 && TREE_CODE (op) == ADDR_EXPR)
25692 op = TREE_OPERAND (op, 0);
25693 if (value_dependent_expression_p (op))
25694 return true;
25695 }
25696 return false;
25697 }
25698
25699 case TEMPLATE_ID_EXPR:
25700 return variable_concept_p (TREE_OPERAND (expression, 0));
25701
25702 case CONSTRUCTOR:
25703 {
25704 unsigned ix;
25705 tree val;
25706 if (dependent_type_p (TREE_TYPE (expression)))
25707 return true;
25708 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25709 if (value_dependent_expression_p (val))
25710 return true;
25711 return false;
25712 }
25713
25714 case STMT_EXPR:
25715 /* Treat a GNU statement expression as dependent to avoid crashing
25716 under instantiate_non_dependent_expr; it can't be constant. */
25717 return true;
25718
25719 default:
25720 /* A constant expression is value-dependent if any subexpression is
25721 value-dependent. */
25722 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25723 {
25724 case tcc_reference:
25725 case tcc_unary:
25726 case tcc_comparison:
25727 case tcc_binary:
25728 case tcc_expression:
25729 case tcc_vl_exp:
25730 {
25731 int i, len = cp_tree_operand_length (expression);
25732
25733 for (i = 0; i < len; i++)
25734 {
25735 tree t = TREE_OPERAND (expression, i);
25736
25737 /* In some cases, some of the operands may be missing.
25738 (For example, in the case of PREDECREMENT_EXPR, the
25739 amount to increment by may be missing.) That doesn't
25740 make the expression dependent. */
25741 if (t && value_dependent_expression_p (t))
25742 return true;
25743 }
25744 }
25745 break;
25746 default:
25747 break;
25748 }
25749 break;
25750 }
25751
25752 /* The expression is not value-dependent. */
25753 return false;
25754 }
25755
25756 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25757 [temp.dep.expr]. Note that an expression with no type is
25758 considered dependent. Other parts of the compiler arrange for an
25759 expression with type-dependent subexpressions to have no type, so
25760 this function doesn't have to be fully recursive. */
25761
25762 bool
25763 type_dependent_expression_p (tree expression)
25764 {
25765 if (!processing_template_decl)
25766 return false;
25767
25768 if (expression == NULL_TREE || expression == error_mark_node)
25769 return false;
25770
25771 STRIP_ANY_LOCATION_WRAPPER (expression);
25772
25773 /* An unresolved name is always dependent. */
25774 if (identifier_p (expression)
25775 || TREE_CODE (expression) == USING_DECL
25776 || TREE_CODE (expression) == WILDCARD_DECL)
25777 return true;
25778
25779 /* A lambda-expression in template context is dependent. dependent_type_p is
25780 true for a lambda in the scope of a class or function template, but that
25781 doesn't cover all template contexts, like a default template argument. */
25782 if (TREE_CODE (expression) == LAMBDA_EXPR)
25783 return true;
25784
25785 /* A fold expression is type-dependent. */
25786 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25787 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25788 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25789 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25790 return true;
25791
25792 /* Some expression forms are never type-dependent. */
25793 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25794 || TREE_CODE (expression) == SIZEOF_EXPR
25795 || TREE_CODE (expression) == ALIGNOF_EXPR
25796 || TREE_CODE (expression) == AT_ENCODE_EXPR
25797 || TREE_CODE (expression) == NOEXCEPT_EXPR
25798 || TREE_CODE (expression) == TRAIT_EXPR
25799 || TREE_CODE (expression) == TYPEID_EXPR
25800 || TREE_CODE (expression) == DELETE_EXPR
25801 || TREE_CODE (expression) == VEC_DELETE_EXPR
25802 || TREE_CODE (expression) == THROW_EXPR
25803 || TREE_CODE (expression) == REQUIRES_EXPR)
25804 return false;
25805
25806 /* The types of these expressions depends only on the type to which
25807 the cast occurs. */
25808 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25809 || TREE_CODE (expression) == STATIC_CAST_EXPR
25810 || TREE_CODE (expression) == CONST_CAST_EXPR
25811 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25812 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25813 || TREE_CODE (expression) == CAST_EXPR)
25814 return dependent_type_p (TREE_TYPE (expression));
25815
25816 /* The types of these expressions depends only on the type created
25817 by the expression. */
25818 if (TREE_CODE (expression) == NEW_EXPR
25819 || TREE_CODE (expression) == VEC_NEW_EXPR)
25820 {
25821 /* For NEW_EXPR tree nodes created inside a template, either
25822 the object type itself or a TREE_LIST may appear as the
25823 operand 1. */
25824 tree type = TREE_OPERAND (expression, 1);
25825 if (TREE_CODE (type) == TREE_LIST)
25826 /* This is an array type. We need to check array dimensions
25827 as well. */
25828 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25829 || value_dependent_expression_p
25830 (TREE_OPERAND (TREE_VALUE (type), 1));
25831 else
25832 return dependent_type_p (type);
25833 }
25834
25835 if (TREE_CODE (expression) == SCOPE_REF)
25836 {
25837 tree scope = TREE_OPERAND (expression, 0);
25838 tree name = TREE_OPERAND (expression, 1);
25839
25840 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25841 contains an identifier associated by name lookup with one or more
25842 declarations declared with a dependent type, or...a
25843 nested-name-specifier or qualified-id that names a member of an
25844 unknown specialization. */
25845 return (type_dependent_expression_p (name)
25846 || dependent_scope_p (scope));
25847 }
25848
25849 if (TREE_CODE (expression) == TEMPLATE_DECL
25850 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25851 return uses_outer_template_parms (expression);
25852
25853 if (TREE_CODE (expression) == STMT_EXPR)
25854 expression = stmt_expr_value_expr (expression);
25855
25856 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25857 {
25858 tree elt;
25859 unsigned i;
25860
25861 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25862 {
25863 if (type_dependent_expression_p (elt))
25864 return true;
25865 }
25866 return false;
25867 }
25868
25869 /* A static data member of the current instantiation with incomplete
25870 array type is type-dependent, as the definition and specializations
25871 can have different bounds. */
25872 if (VAR_P (expression)
25873 && DECL_CLASS_SCOPE_P (expression)
25874 && dependent_type_p (DECL_CONTEXT (expression))
25875 && VAR_HAD_UNKNOWN_BOUND (expression))
25876 return true;
25877
25878 /* An array of unknown bound depending on a variadic parameter, eg:
25879
25880 template<typename... Args>
25881 void foo (Args... args)
25882 {
25883 int arr[] = { args... };
25884 }
25885
25886 template<int... vals>
25887 void bar ()
25888 {
25889 int arr[] = { vals... };
25890 }
25891
25892 If the array has no length and has an initializer, it must be that
25893 we couldn't determine its length in cp_complete_array_type because
25894 it is dependent. */
25895 if (VAR_P (expression)
25896 && TREE_TYPE (expression) != NULL_TREE
25897 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25898 && !TYPE_DOMAIN (TREE_TYPE (expression))
25899 && DECL_INITIAL (expression))
25900 return true;
25901
25902 /* A function or variable template-id is type-dependent if it has any
25903 dependent template arguments. */
25904 if (VAR_OR_FUNCTION_DECL_P (expression)
25905 && DECL_LANG_SPECIFIC (expression)
25906 && DECL_TEMPLATE_INFO (expression))
25907 {
25908 /* Consider the innermost template arguments, since those are the ones
25909 that come from the template-id; the template arguments for the
25910 enclosing class do not make it type-dependent unless they are used in
25911 the type of the decl. */
25912 if (instantiates_primary_template_p (expression)
25913 && (any_dependent_template_arguments_p
25914 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25915 return true;
25916 }
25917
25918 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25919 type-dependent. Checking this is important for functions with auto return
25920 type, which looks like a dependent type. */
25921 if (TREE_CODE (expression) == FUNCTION_DECL
25922 && !(DECL_CLASS_SCOPE_P (expression)
25923 && dependent_type_p (DECL_CONTEXT (expression)))
25924 && !(DECL_LANG_SPECIFIC (expression)
25925 && DECL_FRIEND_P (expression)
25926 && (!DECL_FRIEND_CONTEXT (expression)
25927 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25928 && !DECL_LOCAL_FUNCTION_P (expression))
25929 {
25930 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25931 || undeduced_auto_decl (expression));
25932 return false;
25933 }
25934
25935 /* Always dependent, on the number of arguments if nothing else. */
25936 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25937 return true;
25938
25939 if (TREE_TYPE (expression) == unknown_type_node)
25940 {
25941 if (TREE_CODE (expression) == ADDR_EXPR)
25942 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25943 if (TREE_CODE (expression) == COMPONENT_REF
25944 || TREE_CODE (expression) == OFFSET_REF)
25945 {
25946 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25947 return true;
25948 expression = TREE_OPERAND (expression, 1);
25949 if (identifier_p (expression))
25950 return false;
25951 }
25952 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25953 if (TREE_CODE (expression) == SCOPE_REF)
25954 return false;
25955
25956 if (BASELINK_P (expression))
25957 {
25958 if (BASELINK_OPTYPE (expression)
25959 && dependent_type_p (BASELINK_OPTYPE (expression)))
25960 return true;
25961 expression = BASELINK_FUNCTIONS (expression);
25962 }
25963
25964 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25965 {
25966 if (any_dependent_template_arguments_p
25967 (TREE_OPERAND (expression, 1)))
25968 return true;
25969 expression = TREE_OPERAND (expression, 0);
25970 if (identifier_p (expression))
25971 return true;
25972 }
25973
25974 gcc_assert (TREE_CODE (expression) == OVERLOAD
25975 || TREE_CODE (expression) == FUNCTION_DECL);
25976
25977 for (lkp_iterator iter (expression); iter; ++iter)
25978 if (type_dependent_expression_p (*iter))
25979 return true;
25980
25981 return false;
25982 }
25983
25984 /* The type of a non-type template parm declared with a placeholder type
25985 depends on the corresponding template argument, even though
25986 placeholders are not normally considered dependent. */
25987 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
25988 && is_auto (TREE_TYPE (expression)))
25989 return true;
25990
25991 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25992
25993 /* Dependent type attributes might not have made it from the decl to
25994 the type yet. */
25995 if (DECL_P (expression)
25996 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
25997 return true;
25998
25999 return (dependent_type_p (TREE_TYPE (expression)));
26000 }
26001
26002 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26003 type-dependent if the expression refers to a member of the current
26004 instantiation and the type of the referenced member is dependent, or the
26005 class member access expression refers to a member of an unknown
26006 specialization.
26007
26008 This function returns true if the OBJECT in such a class member access
26009 expression is of an unknown specialization. */
26010
26011 bool
26012 type_dependent_object_expression_p (tree object)
26013 {
26014 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26015 dependent. */
26016 if (TREE_CODE (object) == IDENTIFIER_NODE)
26017 return true;
26018 tree scope = TREE_TYPE (object);
26019 return (!scope || dependent_scope_p (scope));
26020 }
26021
26022 /* walk_tree callback function for instantiation_dependent_expression_p,
26023 below. Returns non-zero if a dependent subexpression is found. */
26024
26025 static tree
26026 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26027 void * /*data*/)
26028 {
26029 if (TYPE_P (*tp))
26030 {
26031 /* We don't have to worry about decltype currently because decltype
26032 of an instantiation-dependent expr is a dependent type. This
26033 might change depending on the resolution of DR 1172. */
26034 *walk_subtrees = false;
26035 return NULL_TREE;
26036 }
26037 enum tree_code code = TREE_CODE (*tp);
26038 switch (code)
26039 {
26040 /* Don't treat an argument list as dependent just because it has no
26041 TREE_TYPE. */
26042 case TREE_LIST:
26043 case TREE_VEC:
26044 case NONTYPE_ARGUMENT_PACK:
26045 return NULL_TREE;
26046
26047 case TEMPLATE_PARM_INDEX:
26048 if (dependent_type_p (TREE_TYPE (*tp)))
26049 return *tp;
26050 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26051 return *tp;
26052 /* We'll check value-dependence separately. */
26053 return NULL_TREE;
26054
26055 /* Handle expressions with type operands. */
26056 case SIZEOF_EXPR:
26057 case ALIGNOF_EXPR:
26058 case TYPEID_EXPR:
26059 case AT_ENCODE_EXPR:
26060 {
26061 tree op = TREE_OPERAND (*tp, 0);
26062 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26063 op = TREE_TYPE (op);
26064 if (TYPE_P (op))
26065 {
26066 if (dependent_type_p (op))
26067 return *tp;
26068 else
26069 {
26070 *walk_subtrees = false;
26071 return NULL_TREE;
26072 }
26073 }
26074 break;
26075 }
26076
26077 case COMPONENT_REF:
26078 if (identifier_p (TREE_OPERAND (*tp, 1)))
26079 /* In a template, finish_class_member_access_expr creates a
26080 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26081 type-dependent, so that we can check access control at
26082 instantiation time (PR 42277). See also Core issue 1273. */
26083 return *tp;
26084 break;
26085
26086 case SCOPE_REF:
26087 if (instantiation_dependent_scope_ref_p (*tp))
26088 return *tp;
26089 else
26090 break;
26091
26092 /* Treat statement-expressions as dependent. */
26093 case BIND_EXPR:
26094 return *tp;
26095
26096 /* Treat requires-expressions as dependent. */
26097 case REQUIRES_EXPR:
26098 return *tp;
26099
26100 case CALL_EXPR:
26101 /* Treat calls to function concepts as dependent. */
26102 if (function_concept_check_p (*tp))
26103 return *tp;
26104 break;
26105
26106 case TEMPLATE_ID_EXPR:
26107 /* And variable concepts. */
26108 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
26109 return *tp;
26110 break;
26111
26112 case CONSTRUCTOR:
26113 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26114 return *tp;
26115 break;
26116
26117 default:
26118 break;
26119 }
26120
26121 if (type_dependent_expression_p (*tp))
26122 return *tp;
26123 else
26124 return NULL_TREE;
26125 }
26126
26127 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26128 sense defined by the ABI:
26129
26130 "An expression is instantiation-dependent if it is type-dependent
26131 or value-dependent, or it has a subexpression that is type-dependent
26132 or value-dependent."
26133
26134 Except don't actually check value-dependence for unevaluated expressions,
26135 because in sizeof(i) we don't care about the value of i. Checking
26136 type-dependence will in turn check value-dependence of array bounds/template
26137 arguments as needed. */
26138
26139 bool
26140 instantiation_dependent_uneval_expression_p (tree expression)
26141 {
26142 tree result;
26143
26144 if (!processing_template_decl)
26145 return false;
26146
26147 if (expression == error_mark_node)
26148 return false;
26149
26150 result = cp_walk_tree_without_duplicates (&expression,
26151 instantiation_dependent_r, NULL);
26152 return result != NULL_TREE;
26153 }
26154
26155 /* As above, but also check value-dependence of the expression as a whole. */
26156
26157 bool
26158 instantiation_dependent_expression_p (tree expression)
26159 {
26160 return (instantiation_dependent_uneval_expression_p (expression)
26161 || value_dependent_expression_p (expression));
26162 }
26163
26164 /* Like type_dependent_expression_p, but it also works while not processing
26165 a template definition, i.e. during substitution or mangling. */
26166
26167 bool
26168 type_dependent_expression_p_push (tree expr)
26169 {
26170 bool b;
26171 ++processing_template_decl;
26172 b = type_dependent_expression_p (expr);
26173 --processing_template_decl;
26174 return b;
26175 }
26176
26177 /* Returns TRUE if ARGS contains a type-dependent expression. */
26178
26179 bool
26180 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26181 {
26182 unsigned int i;
26183 tree arg;
26184
26185 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26186 {
26187 if (type_dependent_expression_p (arg))
26188 return true;
26189 }
26190 return false;
26191 }
26192
26193 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26194 expressions) contains any type-dependent expressions. */
26195
26196 bool
26197 any_type_dependent_elements_p (const_tree list)
26198 {
26199 for (; list; list = TREE_CHAIN (list))
26200 if (type_dependent_expression_p (TREE_VALUE (list)))
26201 return true;
26202
26203 return false;
26204 }
26205
26206 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26207 expressions) contains any value-dependent expressions. */
26208
26209 bool
26210 any_value_dependent_elements_p (const_tree list)
26211 {
26212 for (; list; list = TREE_CHAIN (list))
26213 if (value_dependent_expression_p (TREE_VALUE (list)))
26214 return true;
26215
26216 return false;
26217 }
26218
26219 /* Returns TRUE if the ARG (a template argument) is dependent. */
26220
26221 bool
26222 dependent_template_arg_p (tree arg)
26223 {
26224 if (!processing_template_decl)
26225 return false;
26226
26227 /* Assume a template argument that was wrongly written by the user
26228 is dependent. This is consistent with what
26229 any_dependent_template_arguments_p [that calls this function]
26230 does. */
26231 if (!arg || arg == error_mark_node)
26232 return true;
26233
26234 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26235 arg = argument_pack_select_arg (arg);
26236
26237 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26238 return true;
26239 if (TREE_CODE (arg) == TEMPLATE_DECL)
26240 {
26241 if (DECL_TEMPLATE_PARM_P (arg))
26242 return true;
26243 /* A member template of a dependent class is not necessarily
26244 type-dependent, but it is a dependent template argument because it
26245 will be a member of an unknown specialization to that template. */
26246 tree scope = CP_DECL_CONTEXT (arg);
26247 return TYPE_P (scope) && dependent_type_p (scope);
26248 }
26249 else if (ARGUMENT_PACK_P (arg))
26250 {
26251 tree args = ARGUMENT_PACK_ARGS (arg);
26252 int i, len = TREE_VEC_LENGTH (args);
26253 for (i = 0; i < len; ++i)
26254 {
26255 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26256 return true;
26257 }
26258
26259 return false;
26260 }
26261 else if (TYPE_P (arg))
26262 return dependent_type_p (arg);
26263 else
26264 return (type_dependent_expression_p (arg)
26265 || value_dependent_expression_p (arg));
26266 }
26267
26268 /* Returns true if ARGS (a collection of template arguments) contains
26269 any types that require structural equality testing. */
26270
26271 bool
26272 any_template_arguments_need_structural_equality_p (tree args)
26273 {
26274 int i;
26275 int j;
26276
26277 if (!args)
26278 return false;
26279 if (args == error_mark_node)
26280 return true;
26281
26282 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26283 {
26284 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26285 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26286 {
26287 tree arg = TREE_VEC_ELT (level, j);
26288 tree packed_args = NULL_TREE;
26289 int k, len = 1;
26290
26291 if (ARGUMENT_PACK_P (arg))
26292 {
26293 /* Look inside the argument pack. */
26294 packed_args = ARGUMENT_PACK_ARGS (arg);
26295 len = TREE_VEC_LENGTH (packed_args);
26296 }
26297
26298 for (k = 0; k < len; ++k)
26299 {
26300 if (packed_args)
26301 arg = TREE_VEC_ELT (packed_args, k);
26302
26303 if (error_operand_p (arg))
26304 return true;
26305 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26306 continue;
26307 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26308 return true;
26309 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26310 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26311 return true;
26312 }
26313 }
26314 }
26315
26316 return false;
26317 }
26318
26319 /* Returns true if ARGS (a collection of template arguments) contains
26320 any dependent arguments. */
26321
26322 bool
26323 any_dependent_template_arguments_p (const_tree args)
26324 {
26325 int i;
26326 int j;
26327
26328 if (!args)
26329 return false;
26330 if (args == error_mark_node)
26331 return true;
26332
26333 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26334 {
26335 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26336 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26337 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26338 return true;
26339 }
26340
26341 return false;
26342 }
26343
26344 /* Returns true if ARGS contains any errors. */
26345
26346 bool
26347 any_erroneous_template_args_p (const_tree args)
26348 {
26349 int i;
26350 int j;
26351
26352 if (args == error_mark_node)
26353 return true;
26354
26355 if (args && TREE_CODE (args) != TREE_VEC)
26356 {
26357 if (tree ti = get_template_info (args))
26358 args = TI_ARGS (ti);
26359 else
26360 args = NULL_TREE;
26361 }
26362
26363 if (!args)
26364 return false;
26365
26366 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26367 {
26368 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26369 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26370 if (error_operand_p (TREE_VEC_ELT (level, j)))
26371 return true;
26372 }
26373
26374 return false;
26375 }
26376
26377 /* Returns TRUE if the template TMPL is type-dependent. */
26378
26379 bool
26380 dependent_template_p (tree tmpl)
26381 {
26382 if (TREE_CODE (tmpl) == OVERLOAD)
26383 {
26384 for (lkp_iterator iter (tmpl); iter; ++iter)
26385 if (dependent_template_p (*iter))
26386 return true;
26387 return false;
26388 }
26389
26390 /* Template template parameters are dependent. */
26391 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26392 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26393 return true;
26394 /* So are names that have not been looked up. */
26395 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26396 return true;
26397 return false;
26398 }
26399
26400 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26401
26402 bool
26403 dependent_template_id_p (tree tmpl, tree args)
26404 {
26405 return (dependent_template_p (tmpl)
26406 || any_dependent_template_arguments_p (args));
26407 }
26408
26409 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26410 are dependent. */
26411
26412 bool
26413 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26414 {
26415 int i;
26416
26417 if (!processing_template_decl)
26418 return false;
26419
26420 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26421 {
26422 tree decl = TREE_VEC_ELT (declv, i);
26423 tree init = TREE_VEC_ELT (initv, i);
26424 tree cond = TREE_VEC_ELT (condv, i);
26425 tree incr = TREE_VEC_ELT (incrv, i);
26426
26427 if (type_dependent_expression_p (decl)
26428 || TREE_CODE (decl) == SCOPE_REF)
26429 return true;
26430
26431 if (init && type_dependent_expression_p (init))
26432 return true;
26433
26434 if (cond == global_namespace)
26435 return true;
26436
26437 if (type_dependent_expression_p (cond))
26438 return true;
26439
26440 if (COMPARISON_CLASS_P (cond)
26441 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26442 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26443 return true;
26444
26445 if (TREE_CODE (incr) == MODOP_EXPR)
26446 {
26447 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26448 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26449 return true;
26450 }
26451 else if (type_dependent_expression_p (incr))
26452 return true;
26453 else if (TREE_CODE (incr) == MODIFY_EXPR)
26454 {
26455 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
26456 return true;
26457 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
26458 {
26459 tree t = TREE_OPERAND (incr, 1);
26460 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
26461 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
26462 return true;
26463
26464 /* If this loop has a class iterator with != comparison
26465 with increment other than i++/++i/i--/--i, make sure the
26466 increment is constant. */
26467 if (CLASS_TYPE_P (TREE_TYPE (decl))
26468 && TREE_CODE (cond) == NE_EXPR)
26469 {
26470 if (TREE_OPERAND (t, 0) == decl)
26471 t = TREE_OPERAND (t, 1);
26472 else
26473 t = TREE_OPERAND (t, 0);
26474 if (TREE_CODE (t) != INTEGER_CST)
26475 return true;
26476 }
26477 }
26478 }
26479 }
26480
26481 return false;
26482 }
26483
26484 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26485 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26486 no such TYPE can be found. Note that this function peers inside
26487 uninstantiated templates and therefore should be used only in
26488 extremely limited situations. ONLY_CURRENT_P restricts this
26489 peering to the currently open classes hierarchy (which is required
26490 when comparing types). */
26491
26492 tree
26493 resolve_typename_type (tree type, bool only_current_p)
26494 {
26495 tree scope;
26496 tree name;
26497 tree decl;
26498 int quals;
26499 tree pushed_scope;
26500 tree result;
26501
26502 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
26503
26504 scope = TYPE_CONTEXT (type);
26505 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26506 gcc_checking_assert (uses_template_parms (scope));
26507
26508 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26509 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26510 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26511 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26512 identifier of the TYPENAME_TYPE anymore.
26513 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26514 TYPENAME_TYPE instead, we avoid messing up with a possible
26515 typedef variant case. */
26516 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
26517
26518 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26519 it first before we can figure out what NAME refers to. */
26520 if (TREE_CODE (scope) == TYPENAME_TYPE)
26521 {
26522 if (TYPENAME_IS_RESOLVING_P (scope))
26523 /* Given a class template A with a dependent base with nested type C,
26524 typedef typename A::C::C C will land us here, as trying to resolve
26525 the initial A::C leads to the local C typedef, which leads back to
26526 A::C::C. So we break the recursion now. */
26527 return type;
26528 else
26529 scope = resolve_typename_type (scope, only_current_p);
26530 }
26531 /* If we don't know what SCOPE refers to, then we cannot resolve the
26532 TYPENAME_TYPE. */
26533 if (!CLASS_TYPE_P (scope))
26534 return type;
26535 /* If this is a typedef, we don't want to look inside (c++/11987). */
26536 if (typedef_variant_p (type))
26537 return type;
26538 /* If SCOPE isn't the template itself, it will not have a valid
26539 TYPE_FIELDS list. */
26540 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
26541 /* scope is either the template itself or a compatible instantiation
26542 like X<T>, so look up the name in the original template. */
26543 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
26544 /* If scope has no fields, it can't be a current instantiation. Check this
26545 before currently_open_class to avoid infinite recursion (71515). */
26546 if (!TYPE_FIELDS (scope))
26547 return type;
26548 /* If the SCOPE is not the current instantiation, there's no reason
26549 to look inside it. */
26550 if (only_current_p && !currently_open_class (scope))
26551 return type;
26552 /* Enter the SCOPE so that name lookup will be resolved as if we
26553 were in the class definition. In particular, SCOPE will no
26554 longer be considered a dependent type. */
26555 pushed_scope = push_scope (scope);
26556 /* Look up the declaration. */
26557 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
26558 tf_warning_or_error);
26559
26560 result = NULL_TREE;
26561
26562 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26563 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26564 tree fullname = TYPENAME_TYPE_FULLNAME (type);
26565 if (!decl)
26566 /*nop*/;
26567 else if (identifier_p (fullname)
26568 && TREE_CODE (decl) == TYPE_DECL)
26569 {
26570 result = TREE_TYPE (decl);
26571 if (result == error_mark_node)
26572 result = NULL_TREE;
26573 }
26574 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
26575 && DECL_CLASS_TEMPLATE_P (decl))
26576 {
26577 /* Obtain the template and the arguments. */
26578 tree tmpl = TREE_OPERAND (fullname, 0);
26579 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
26580 {
26581 /* We get here with a plain identifier because a previous tentative
26582 parse of the nested-name-specifier as part of a ptr-operator saw
26583 ::template X<A>. The use of ::template is necessary in a
26584 ptr-operator, but wrong in a declarator-id.
26585
26586 [temp.names]: In a qualified-id of a declarator-id, the keyword
26587 template shall not appear at the top level. */
26588 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
26589 "keyword %<template%> not allowed in declarator-id");
26590 tmpl = decl;
26591 }
26592 tree args = TREE_OPERAND (fullname, 1);
26593 /* Instantiate the template. */
26594 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26595 /*entering_scope=*/true,
26596 tf_error | tf_user);
26597 if (result == error_mark_node)
26598 result = NULL_TREE;
26599 }
26600
26601 /* Leave the SCOPE. */
26602 if (pushed_scope)
26603 pop_scope (pushed_scope);
26604
26605 /* If we failed to resolve it, return the original typename. */
26606 if (!result)
26607 return type;
26608
26609 /* If lookup found a typename type, resolve that too. */
26610 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26611 {
26612 /* Ill-formed programs can cause infinite recursion here, so we
26613 must catch that. */
26614 TYPENAME_IS_RESOLVING_P (result) = 1;
26615 result = resolve_typename_type (result, only_current_p);
26616 TYPENAME_IS_RESOLVING_P (result) = 0;
26617 }
26618
26619 /* Qualify the resulting type. */
26620 quals = cp_type_quals (type);
26621 if (quals)
26622 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26623
26624 return result;
26625 }
26626
26627 /* EXPR is an expression which is not type-dependent. Return a proxy
26628 for EXPR that can be used to compute the types of larger
26629 expressions containing EXPR. */
26630
26631 tree
26632 build_non_dependent_expr (tree expr)
26633 {
26634 tree orig_expr = expr;
26635 tree inner_expr;
26636
26637 /* When checking, try to get a constant value for all non-dependent
26638 expressions in order to expose bugs in *_dependent_expression_p
26639 and constexpr. This can affect code generation, see PR70704, so
26640 only do this for -fchecking=2. */
26641 if (flag_checking > 1
26642 && cxx_dialect >= cxx11
26643 /* Don't do this during nsdmi parsing as it can lead to
26644 unexpected recursive instantiations. */
26645 && !parsing_nsdmi ()
26646 /* Don't do this during concept expansion either and for
26647 the same reason. */
26648 && !expanding_concept ())
26649 fold_non_dependent_expr (expr, tf_none);
26650
26651 STRIP_ANY_LOCATION_WRAPPER (expr);
26652
26653 /* Preserve OVERLOADs; the functions must be available to resolve
26654 types. */
26655 inner_expr = expr;
26656 if (TREE_CODE (inner_expr) == STMT_EXPR)
26657 inner_expr = stmt_expr_value_expr (inner_expr);
26658 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26659 inner_expr = TREE_OPERAND (inner_expr, 0);
26660 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26661 inner_expr = TREE_OPERAND (inner_expr, 1);
26662 if (is_overloaded_fn (inner_expr)
26663 || TREE_CODE (inner_expr) == OFFSET_REF)
26664 return orig_expr;
26665 /* There is no need to return a proxy for a variable or enumerator. */
26666 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26667 return orig_expr;
26668 /* Preserve string constants; conversions from string constants to
26669 "char *" are allowed, even though normally a "const char *"
26670 cannot be used to initialize a "char *". */
26671 if (TREE_CODE (expr) == STRING_CST)
26672 return orig_expr;
26673 /* Preserve void and arithmetic constants, as an optimization -- there is no
26674 reason to create a new node. */
26675 if (TREE_CODE (expr) == VOID_CST
26676 || TREE_CODE (expr) == INTEGER_CST
26677 || TREE_CODE (expr) == REAL_CST)
26678 return orig_expr;
26679 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26680 There is at least one place where we want to know that a
26681 particular expression is a throw-expression: when checking a ?:
26682 expression, there are special rules if the second or third
26683 argument is a throw-expression. */
26684 if (TREE_CODE (expr) == THROW_EXPR)
26685 return orig_expr;
26686
26687 /* Don't wrap an initializer list, we need to be able to look inside. */
26688 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26689 return orig_expr;
26690
26691 /* Don't wrap a dummy object, we need to be able to test for it. */
26692 if (is_dummy_object (expr))
26693 return orig_expr;
26694
26695 if (TREE_CODE (expr) == COND_EXPR)
26696 return build3 (COND_EXPR,
26697 TREE_TYPE (expr),
26698 TREE_OPERAND (expr, 0),
26699 (TREE_OPERAND (expr, 1)
26700 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26701 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26702 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26703 if (TREE_CODE (expr) == COMPOUND_EXPR
26704 && !COMPOUND_EXPR_OVERLOADED (expr))
26705 return build2 (COMPOUND_EXPR,
26706 TREE_TYPE (expr),
26707 TREE_OPERAND (expr, 0),
26708 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26709
26710 /* If the type is unknown, it can't really be non-dependent */
26711 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26712
26713 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26714 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26715 TREE_TYPE (expr), expr);
26716 }
26717
26718 /* ARGS is a vector of expressions as arguments to a function call.
26719 Replace the arguments with equivalent non-dependent expressions.
26720 This modifies ARGS in place. */
26721
26722 void
26723 make_args_non_dependent (vec<tree, va_gc> *args)
26724 {
26725 unsigned int ix;
26726 tree arg;
26727
26728 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26729 {
26730 tree newarg = build_non_dependent_expr (arg);
26731 if (newarg != arg)
26732 (*args)[ix] = newarg;
26733 }
26734 }
26735
26736 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26737 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26738 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26739
26740 static tree
26741 make_auto_1 (tree name, bool set_canonical)
26742 {
26743 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26744 TYPE_NAME (au) = build_decl (input_location,
26745 TYPE_DECL, name, au);
26746 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26747 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26748 (0, processing_template_decl + 1, processing_template_decl + 1,
26749 TYPE_NAME (au), NULL_TREE);
26750 if (set_canonical)
26751 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26752 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26753 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26754
26755 return au;
26756 }
26757
26758 tree
26759 make_decltype_auto (void)
26760 {
26761 return make_auto_1 (decltype_auto_identifier, true);
26762 }
26763
26764 tree
26765 make_auto (void)
26766 {
26767 return make_auto_1 (auto_identifier, true);
26768 }
26769
26770 /* Return a C++17 deduction placeholder for class template TMPL. */
26771
26772 tree
26773 make_template_placeholder (tree tmpl)
26774 {
26775 tree t = make_auto_1 (auto_identifier, false);
26776 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26777 /* Our canonical type depends on the placeholder. */
26778 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26779 return t;
26780 }
26781
26782 /* True iff T is a C++17 class template deduction placeholder. */
26783
26784 bool
26785 template_placeholder_p (tree t)
26786 {
26787 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26788 }
26789
26790 /* Make a "constrained auto" type-specifier. This is an
26791 auto type with constraints that must be associated after
26792 deduction. The constraint is formed from the given
26793 CONC and its optional sequence of arguments, which are
26794 non-null if written as partial-concept-id. */
26795
26796 tree
26797 make_constrained_auto (tree con, tree args)
26798 {
26799 tree type = make_auto_1 (auto_identifier, false);
26800
26801 /* Build the constraint. */
26802 tree tmpl = DECL_TI_TEMPLATE (con);
26803 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26804 expr = build_concept_check (expr, type, args);
26805
26806 tree constr = normalize_expression (expr);
26807 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26808
26809 /* Our canonical type depends on the constraint. */
26810 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26811
26812 /* Attach the constraint to the type declaration. */
26813 tree decl = TYPE_NAME (type);
26814 return decl;
26815 }
26816
26817 /* Given type ARG, return std::initializer_list<ARG>. */
26818
26819 static tree
26820 listify (tree arg)
26821 {
26822 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26823
26824 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26825 {
26826 gcc_rich_location richloc (input_location);
26827 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26828 error_at (&richloc,
26829 "deducing from brace-enclosed initializer list"
26830 " requires %<#include <initializer_list>%>");
26831
26832 return error_mark_node;
26833 }
26834 tree argvec = make_tree_vec (1);
26835 TREE_VEC_ELT (argvec, 0) = arg;
26836
26837 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26838 NULL_TREE, 0, tf_warning_or_error);
26839 }
26840
26841 /* Replace auto in TYPE with std::initializer_list<auto>. */
26842
26843 static tree
26844 listify_autos (tree type, tree auto_node)
26845 {
26846 tree init_auto = listify (auto_node);
26847 tree argvec = make_tree_vec (1);
26848 TREE_VEC_ELT (argvec, 0) = init_auto;
26849 if (processing_template_decl)
26850 argvec = add_to_template_args (current_template_args (), argvec);
26851 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26852 }
26853
26854 /* Hash traits for hashing possibly constrained 'auto'
26855 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26856
26857 struct auto_hash : default_hash_traits<tree>
26858 {
26859 static inline hashval_t hash (tree);
26860 static inline bool equal (tree, tree);
26861 };
26862
26863 /* Hash the 'auto' T. */
26864
26865 inline hashval_t
26866 auto_hash::hash (tree t)
26867 {
26868 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26869 /* Matching constrained-type-specifiers denote the same template
26870 parameter, so hash the constraint. */
26871 return hash_placeholder_constraint (c);
26872 else
26873 /* But unconstrained autos are all separate, so just hash the pointer. */
26874 return iterative_hash_object (t, 0);
26875 }
26876
26877 /* Compare two 'auto's. */
26878
26879 inline bool
26880 auto_hash::equal (tree t1, tree t2)
26881 {
26882 if (t1 == t2)
26883 return true;
26884
26885 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26886 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26887
26888 /* Two unconstrained autos are distinct. */
26889 if (!c1 || !c2)
26890 return false;
26891
26892 return equivalent_placeholder_constraints (c1, c2);
26893 }
26894
26895 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26896 constrained) auto, add it to the vector. */
26897
26898 static int
26899 extract_autos_r (tree t, void *data)
26900 {
26901 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26902 if (is_auto (t))
26903 {
26904 /* All the autos were built with index 0; fix that up now. */
26905 tree *p = hash.find_slot (t, INSERT);
26906 unsigned idx;
26907 if (*p)
26908 /* If this is a repeated constrained-type-specifier, use the index we
26909 chose before. */
26910 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26911 else
26912 {
26913 /* Otherwise this is new, so use the current count. */
26914 *p = t;
26915 idx = hash.elements () - 1;
26916 }
26917 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26918 }
26919
26920 /* Always keep walking. */
26921 return 0;
26922 }
26923
26924 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26925 says they can appear anywhere in the type. */
26926
26927 static tree
26928 extract_autos (tree type)
26929 {
26930 hash_set<tree> visited;
26931 hash_table<auto_hash> hash (2);
26932
26933 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26934
26935 tree tree_vec = make_tree_vec (hash.elements());
26936 for (hash_table<auto_hash>::iterator iter = hash.begin();
26937 iter != hash.end(); ++iter)
26938 {
26939 tree elt = *iter;
26940 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26941 TREE_VEC_ELT (tree_vec, i)
26942 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26943 }
26944
26945 return tree_vec;
26946 }
26947
26948 /* The stem for deduction guide names. */
26949 const char *const dguide_base = "__dguide_";
26950
26951 /* Return the name for a deduction guide for class template TMPL. */
26952
26953 tree
26954 dguide_name (tree tmpl)
26955 {
26956 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26957 tree tname = TYPE_IDENTIFIER (type);
26958 char *buf = (char *) alloca (1 + strlen (dguide_base)
26959 + IDENTIFIER_LENGTH (tname));
26960 memcpy (buf, dguide_base, strlen (dguide_base));
26961 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26962 IDENTIFIER_LENGTH (tname) + 1);
26963 tree dname = get_identifier (buf);
26964 TREE_TYPE (dname) = type;
26965 return dname;
26966 }
26967
26968 /* True if NAME is the name of a deduction guide. */
26969
26970 bool
26971 dguide_name_p (tree name)
26972 {
26973 return (TREE_CODE (name) == IDENTIFIER_NODE
26974 && TREE_TYPE (name)
26975 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26976 strlen (dguide_base)));
26977 }
26978
26979 /* True if FN is a deduction guide. */
26980
26981 bool
26982 deduction_guide_p (const_tree fn)
26983 {
26984 if (DECL_P (fn))
26985 if (tree name = DECL_NAME (fn))
26986 return dguide_name_p (name);
26987 return false;
26988 }
26989
26990 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26991
26992 bool
26993 copy_guide_p (const_tree fn)
26994 {
26995 gcc_assert (deduction_guide_p (fn));
26996 if (!DECL_ARTIFICIAL (fn))
26997 return false;
26998 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
26999 return (TREE_CHAIN (parms) == void_list_node
27000 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27001 }
27002
27003 /* True if FN is a guide generated from a constructor template. */
27004
27005 bool
27006 template_guide_p (const_tree fn)
27007 {
27008 gcc_assert (deduction_guide_p (fn));
27009 if (!DECL_ARTIFICIAL (fn))
27010 return false;
27011 tree tmpl = DECL_TI_TEMPLATE (fn);
27012 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27013 return PRIMARY_TEMPLATE_P (org);
27014 return false;
27015 }
27016
27017 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27018 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27019 template parameter types. Note that the handling of template template
27020 parameters relies on current_template_parms being set appropriately for the
27021 new template. */
27022
27023 static tree
27024 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27025 tree tsubst_args, tsubst_flags_t complain)
27026 {
27027 if (olddecl == error_mark_node)
27028 return error_mark_node;
27029
27030 tree oldidx = get_template_parm_index (olddecl);
27031
27032 tree newtype;
27033 if (TREE_CODE (olddecl) == TYPE_DECL
27034 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27035 {
27036 tree oldtype = TREE_TYPE (olddecl);
27037 newtype = cxx_make_type (TREE_CODE (oldtype));
27038 TYPE_MAIN_VARIANT (newtype) = newtype;
27039 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27040 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27041 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27042 }
27043 else
27044 {
27045 newtype = TREE_TYPE (olddecl);
27046 if (type_uses_auto (newtype))
27047 {
27048 // Substitute once to fix references to other template parameters.
27049 newtype = tsubst (newtype, tsubst_args,
27050 complain|tf_partial, NULL_TREE);
27051 // Now substitute again to reduce the level of the auto.
27052 newtype = tsubst (newtype, current_template_args (),
27053 complain, NULL_TREE);
27054 }
27055 else
27056 newtype = tsubst (newtype, tsubst_args,
27057 complain, NULL_TREE);
27058 }
27059
27060 tree newdecl
27061 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27062 DECL_NAME (olddecl), newtype);
27063 SET_DECL_TEMPLATE_PARM_P (newdecl);
27064
27065 tree newidx;
27066 if (TREE_CODE (olddecl) == TYPE_DECL
27067 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27068 {
27069 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27070 = build_template_parm_index (index, level, level,
27071 newdecl, newtype);
27072 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27073 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27074 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27075 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27076
27077 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27078 {
27079 DECL_TEMPLATE_RESULT (newdecl)
27080 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27081 DECL_NAME (olddecl), newtype);
27082 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27083 // First create a copy (ttargs) of tsubst_args with an
27084 // additional level for the template template parameter's own
27085 // template parameters (ttparms).
27086 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27087 (DECL_TEMPLATE_PARMS (olddecl)));
27088 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27089 tree ttargs = make_tree_vec (depth + 1);
27090 for (int i = 0; i < depth; ++i)
27091 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27092 TREE_VEC_ELT (ttargs, depth)
27093 = template_parms_level_to_args (ttparms);
27094 // Substitute ttargs into ttparms to fix references to
27095 // other template parameters.
27096 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27097 complain|tf_partial);
27098 // Now substitute again with args based on tparms, to reduce
27099 // the level of the ttparms.
27100 ttargs = current_template_args ();
27101 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27102 complain);
27103 // Finally, tack the adjusted parms onto tparms.
27104 ttparms = tree_cons (size_int (depth), ttparms,
27105 current_template_parms);
27106 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27107 }
27108 }
27109 else
27110 {
27111 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27112 tree newconst
27113 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27114 TREE_CODE (oldconst),
27115 DECL_NAME (oldconst), newtype);
27116 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27117 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27118 SET_DECL_TEMPLATE_PARM_P (newconst);
27119 newidx = build_template_parm_index (index, level, level,
27120 newconst, newtype);
27121 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27122 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27123 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27124 }
27125
27126 return newdecl;
27127 }
27128
27129 /* Returns a C++17 class deduction guide template based on the constructor
27130 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27131 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27132
27133 static tree
27134 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
27135 {
27136 tree type, tparms, targs, fparms, fargs, ci;
27137 bool memtmpl = false;
27138 bool explicit_p;
27139 location_t loc;
27140 tree fn_tmpl = NULL_TREE;
27141
27142 if (TYPE_P (ctor))
27143 {
27144 type = ctor;
27145 bool copy_p = TYPE_REF_P (type);
27146 if (copy_p)
27147 {
27148 type = TREE_TYPE (type);
27149 fparms = tree_cons (NULL_TREE, type, void_list_node);
27150 }
27151 else
27152 fparms = void_list_node;
27153
27154 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27155 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27156 targs = CLASSTYPE_TI_ARGS (type);
27157 ci = NULL_TREE;
27158 fargs = NULL_TREE;
27159 loc = DECL_SOURCE_LOCATION (ctmpl);
27160 explicit_p = false;
27161 }
27162 else
27163 {
27164 ++processing_template_decl;
27165 bool ok = true;
27166
27167 fn_tmpl
27168 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27169 : DECL_TI_TEMPLATE (ctor));
27170 if (outer_args)
27171 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27172 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27173
27174 type = DECL_CONTEXT (ctor);
27175
27176 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27177 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27178 fully specialized args for the enclosing class. Strip those off, as
27179 the deduction guide won't have those template parameters. */
27180 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27181 TMPL_PARMS_DEPTH (tparms));
27182 /* Discard the 'this' parameter. */
27183 fparms = FUNCTION_ARG_CHAIN (ctor);
27184 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27185 ci = get_constraints (ctor);
27186 loc = DECL_SOURCE_LOCATION (ctor);
27187 explicit_p = DECL_NONCONVERTING_P (ctor);
27188
27189 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27190 {
27191 memtmpl = true;
27192
27193 /* For a member template constructor, we need to flatten the two
27194 template parameter lists into one, and then adjust the function
27195 signature accordingly. This gets...complicated. */
27196 tree save_parms = current_template_parms;
27197
27198 /* For a member template we should have two levels of parms/args, one
27199 for the class and one for the constructor. We stripped
27200 specialized args for further enclosing classes above. */
27201 const int depth = 2;
27202 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27203
27204 /* Template args for translating references to the two-level template
27205 parameters into references to the one-level template parameters we
27206 are creating. */
27207 tree tsubst_args = copy_node (targs);
27208 TMPL_ARGS_LEVEL (tsubst_args, depth)
27209 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27210
27211 /* Template parms for the constructor template. */
27212 tree ftparms = TREE_VALUE (tparms);
27213 unsigned flen = TREE_VEC_LENGTH (ftparms);
27214 /* Template parms for the class template. */
27215 tparms = TREE_CHAIN (tparms);
27216 tree ctparms = TREE_VALUE (tparms);
27217 unsigned clen = TREE_VEC_LENGTH (ctparms);
27218 /* Template parms for the deduction guide start as a copy of the
27219 template parms for the class. We set current_template_parms for
27220 lookup_template_class_1. */
27221 current_template_parms = tparms = copy_node (tparms);
27222 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27223 for (unsigned i = 0; i < clen; ++i)
27224 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27225
27226 /* Now we need to rewrite the constructor parms to append them to the
27227 class parms. */
27228 for (unsigned i = 0; i < flen; ++i)
27229 {
27230 unsigned index = i + clen;
27231 unsigned level = 1;
27232 tree oldelt = TREE_VEC_ELT (ftparms, i);
27233 tree olddecl = TREE_VALUE (oldelt);
27234 tree newdecl = rewrite_template_parm (olddecl, index, level,
27235 tsubst_args, complain);
27236 if (newdecl == error_mark_node)
27237 ok = false;
27238 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27239 tsubst_args, complain, ctor);
27240 tree list = build_tree_list (newdef, newdecl);
27241 TEMPLATE_PARM_CONSTRAINTS (list)
27242 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27243 tsubst_args, complain, ctor);
27244 TREE_VEC_ELT (new_vec, index) = list;
27245 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27246 }
27247
27248 /* Now we have a final set of template parms to substitute into the
27249 function signature. */
27250 targs = template_parms_to_args (tparms);
27251 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27252 complain, ctor);
27253 if (fparms == error_mark_node)
27254 ok = false;
27255 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27256 if (ci)
27257 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27258
27259 current_template_parms = save_parms;
27260 }
27261
27262 --processing_template_decl;
27263 if (!ok)
27264 return error_mark_node;
27265 }
27266
27267 if (!memtmpl)
27268 {
27269 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27270 tparms = copy_node (tparms);
27271 INNERMOST_TEMPLATE_PARMS (tparms)
27272 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27273 }
27274
27275 tree fntype = build_function_type (type, fparms);
27276 tree ded_fn = build_lang_decl_loc (loc,
27277 FUNCTION_DECL,
27278 dguide_name (type), fntype);
27279 DECL_ARGUMENTS (ded_fn) = fargs;
27280 DECL_ARTIFICIAL (ded_fn) = true;
27281 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27282 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27283 DECL_ARTIFICIAL (ded_tmpl) = true;
27284 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27285 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27286 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27287 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27288 if (DECL_P (ctor))
27289 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27290 if (ci)
27291 set_constraints (ded_tmpl, ci);
27292
27293 return ded_tmpl;
27294 }
27295
27296 /* Deduce template arguments for the class template placeholder PTYPE for
27297 template TMPL based on the initializer INIT, and return the resulting
27298 type. */
27299
27300 static tree
27301 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
27302 tsubst_flags_t complain)
27303 {
27304 if (!DECL_CLASS_TEMPLATE_P (tmpl))
27305 {
27306 /* We should have handled this in the caller. */
27307 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27308 return ptype;
27309 if (complain & tf_error)
27310 error ("non-class template %qT used without template arguments", tmpl);
27311 return error_mark_node;
27312 }
27313 if (init && TREE_TYPE (init) == ptype)
27314 /* Using the template parm as its own argument. */
27315 return ptype;
27316
27317 tree type = TREE_TYPE (tmpl);
27318
27319 bool try_list_ctor = false;
27320
27321 vec<tree,va_gc> *args;
27322 if (init == NULL_TREE
27323 || TREE_CODE (init) == TREE_LIST)
27324 args = make_tree_vector_from_list (init);
27325 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
27326 {
27327 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
27328 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
27329 {
27330 /* As an exception, the first phase in 16.3.1.7 (considering the
27331 initializer list as a single argument) is omitted if the
27332 initializer list consists of a single expression of type cv U,
27333 where U is a specialization of C or a class derived from a
27334 specialization of C. */
27335 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
27336 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
27337 {
27338 tree etype = TREE_TYPE (elt);
27339 tree tparms = (INNERMOST_TEMPLATE_PARMS
27340 (DECL_TEMPLATE_PARMS (tmpl)));
27341 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27342 int err = unify (tparms, targs, type, etype,
27343 UNIFY_ALLOW_DERIVED, /*explain*/false);
27344 if (err == 0)
27345 try_list_ctor = false;
27346 ggc_free (targs);
27347 }
27348 }
27349 if (try_list_ctor || is_std_init_list (type))
27350 args = make_tree_vector_single (init);
27351 else
27352 args = make_tree_vector_from_ctor (init);
27353 }
27354 else
27355 args = make_tree_vector_single (init);
27356
27357 tree dname = dguide_name (tmpl);
27358 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
27359 /*type*/false, /*complain*/false,
27360 /*hidden*/false);
27361 bool elided = false;
27362 if (cands == error_mark_node)
27363 cands = NULL_TREE;
27364
27365 /* Prune explicit deduction guides in copy-initialization context. */
27366 if (flags & LOOKUP_ONLYCONVERTING)
27367 {
27368 for (lkp_iterator iter (cands); !elided && iter; ++iter)
27369 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27370 elided = true;
27371
27372 if (elided)
27373 {
27374 /* Found a nonconverting guide, prune the candidates. */
27375 tree pruned = NULL_TREE;
27376 for (lkp_iterator iter (cands); iter; ++iter)
27377 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27378 pruned = lookup_add (*iter, pruned);
27379
27380 cands = pruned;
27381 }
27382 }
27383
27384 tree outer_args = NULL_TREE;
27385 if (DECL_CLASS_SCOPE_P (tmpl)
27386 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
27387 {
27388 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
27389 type = TREE_TYPE (most_general_template (tmpl));
27390 }
27391
27392 bool saw_ctor = false;
27393 // FIXME cache artificial deduction guides
27394 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
27395 {
27396 /* Skip inherited constructors. */
27397 if (iter.using_p ())
27398 continue;
27399
27400 tree guide = build_deduction_guide (*iter, outer_args, complain);
27401 if (guide == error_mark_node)
27402 return error_mark_node;
27403 if ((flags & LOOKUP_ONLYCONVERTING)
27404 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
27405 elided = true;
27406 else
27407 cands = lookup_add (guide, cands);
27408
27409 saw_ctor = true;
27410 }
27411
27412 tree call = error_mark_node;
27413
27414 /* If this is list-initialization and the class has a list constructor, first
27415 try deducing from the list as a single argument, as [over.match.list]. */
27416 tree list_cands = NULL_TREE;
27417 if (try_list_ctor && cands)
27418 for (lkp_iterator iter (cands); iter; ++iter)
27419 {
27420 tree dg = *iter;
27421 if (is_list_ctor (dg))
27422 list_cands = lookup_add (dg, list_cands);
27423 }
27424 if (list_cands)
27425 {
27426 ++cp_unevaluated_operand;
27427 call = build_new_function_call (list_cands, &args, tf_decltype);
27428 --cp_unevaluated_operand;
27429
27430 if (call == error_mark_node)
27431 {
27432 /* That didn't work, now try treating the list as a sequence of
27433 arguments. */
27434 release_tree_vector (args);
27435 args = make_tree_vector_from_ctor (init);
27436 }
27437 }
27438
27439 /* Maybe generate an implicit deduction guide. */
27440 if (call == error_mark_node && args->length () < 2)
27441 {
27442 tree gtype = NULL_TREE;
27443
27444 if (args->length () == 1)
27445 /* Generate a copy guide. */
27446 gtype = build_reference_type (type);
27447 else if (!saw_ctor)
27448 /* Generate a default guide. */
27449 gtype = type;
27450
27451 if (gtype)
27452 {
27453 tree guide = build_deduction_guide (gtype, outer_args, complain);
27454 if (guide == error_mark_node)
27455 return error_mark_node;
27456 cands = lookup_add (guide, cands);
27457 }
27458 }
27459
27460 if (elided && !cands)
27461 {
27462 error ("cannot deduce template arguments for copy-initialization"
27463 " of %qT, as it has no non-explicit deduction guides or "
27464 "user-declared constructors", type);
27465 return error_mark_node;
27466 }
27467 else if (!cands && call == error_mark_node)
27468 {
27469 error ("cannot deduce template arguments of %qT, as it has no viable "
27470 "deduction guides", type);
27471 return error_mark_node;
27472 }
27473
27474 if (call == error_mark_node)
27475 {
27476 ++cp_unevaluated_operand;
27477 call = build_new_function_call (cands, &args, tf_decltype);
27478 --cp_unevaluated_operand;
27479 }
27480
27481 if (call == error_mark_node && (complain & tf_warning_or_error))
27482 {
27483 error ("class template argument deduction failed:");
27484
27485 ++cp_unevaluated_operand;
27486 call = build_new_function_call (cands, &args, complain | tf_decltype);
27487 --cp_unevaluated_operand;
27488
27489 if (elided)
27490 inform (input_location, "explicit deduction guides not considered "
27491 "for copy-initialization");
27492 }
27493
27494 release_tree_vector (args);
27495
27496 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
27497 }
27498
27499 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27500 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27501 The CONTEXT determines the context in which auto deduction is performed
27502 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27503 OUTER_TARGS are used during template argument deduction
27504 (context == adc_unify) to properly substitute the result, and is ignored
27505 in other contexts.
27506
27507 For partial-concept-ids, extra args may be appended to the list of deduced
27508 template arguments prior to determining constraint satisfaction. */
27509
27510 tree
27511 do_auto_deduction (tree type, tree init, tree auto_node,
27512 tsubst_flags_t complain, auto_deduction_context context,
27513 tree outer_targs, int flags)
27514 {
27515 tree targs;
27516
27517 if (init == error_mark_node)
27518 return error_mark_node;
27519
27520 if (init && type_dependent_expression_p (init)
27521 && context != adc_unify)
27522 /* Defining a subset of type-dependent expressions that we can deduce
27523 from ahead of time isn't worth the trouble. */
27524 return type;
27525
27526 /* Similarly, we can't deduce from another undeduced decl. */
27527 if (init && undeduced_auto_decl (init))
27528 return type;
27529
27530 /* We may be doing a partial substitution, but we still want to replace
27531 auto_node. */
27532 complain &= ~tf_partial;
27533
27534 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
27535 /* C++17 class template argument deduction. */
27536 return do_class_deduction (type, tmpl, init, flags, complain);
27537
27538 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
27539 /* Nothing we can do with this, even in deduction context. */
27540 return type;
27541
27542 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27543 with either a new invented type template parameter U or, if the
27544 initializer is a braced-init-list (8.5.4), with
27545 std::initializer_list<U>. */
27546 if (BRACE_ENCLOSED_INITIALIZER_P (init))
27547 {
27548 if (!DIRECT_LIST_INIT_P (init))
27549 type = listify_autos (type, auto_node);
27550 else if (CONSTRUCTOR_NELTS (init) == 1)
27551 init = CONSTRUCTOR_ELT (init, 0)->value;
27552 else
27553 {
27554 if (complain & tf_warning_or_error)
27555 {
27556 if (permerror (input_location, "direct-list-initialization of "
27557 "%<auto%> requires exactly one element"))
27558 inform (input_location,
27559 "for deduction to %<std::initializer_list%>, use copy-"
27560 "list-initialization (i.e. add %<=%> before the %<{%>)");
27561 }
27562 type = listify_autos (type, auto_node);
27563 }
27564 }
27565
27566 if (type == error_mark_node)
27567 return error_mark_node;
27568
27569 init = resolve_nondeduced_context (init, complain);
27570
27571 if (context == adc_decomp_type
27572 && auto_node == type
27573 && init != error_mark_node
27574 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
27575 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27576 and initializer has array type, deduce cv-qualified array type. */
27577 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
27578 complain);
27579 else if (AUTO_IS_DECLTYPE (auto_node))
27580 {
27581 tree stripped_init = tree_strip_any_location_wrapper (init);
27582 bool id = (DECL_P (stripped_init)
27583 || ((TREE_CODE (init) == COMPONENT_REF
27584 || TREE_CODE (init) == SCOPE_REF)
27585 && !REF_PARENTHESIZED_P (init)));
27586 targs = make_tree_vec (1);
27587 TREE_VEC_ELT (targs, 0)
27588 = finish_decltype_type (init, id, tf_warning_or_error);
27589 if (type != auto_node)
27590 {
27591 if (complain & tf_error)
27592 error ("%qT as type rather than plain %<decltype(auto)%>", type);
27593 return error_mark_node;
27594 }
27595 }
27596 else
27597 {
27598 tree parms = build_tree_list (NULL_TREE, type);
27599 tree tparms;
27600
27601 if (flag_concepts)
27602 tparms = extract_autos (type);
27603 else
27604 {
27605 tparms = make_tree_vec (1);
27606 TREE_VEC_ELT (tparms, 0)
27607 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27608 }
27609
27610 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27611 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27612 DEDUCE_CALL,
27613 NULL, /*explain_p=*/false);
27614 if (val > 0)
27615 {
27616 if (processing_template_decl)
27617 /* Try again at instantiation time. */
27618 return type;
27619 if (type && type != error_mark_node
27620 && (complain & tf_error))
27621 /* If type is error_mark_node a diagnostic must have been
27622 emitted by now. Also, having a mention to '<type error>'
27623 in the diagnostic is not really useful to the user. */
27624 {
27625 if (cfun
27626 && FNDECL_USED_AUTO (current_function_decl)
27627 && (auto_node
27628 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
27629 && LAMBDA_FUNCTION_P (current_function_decl))
27630 error ("unable to deduce lambda return type from %qE", init);
27631 else
27632 error ("unable to deduce %qT from %qE", type, init);
27633 type_unification_real (tparms, targs, parms, &init, 1, 0,
27634 DEDUCE_CALL,
27635 NULL, /*explain_p=*/true);
27636 }
27637 return error_mark_node;
27638 }
27639 }
27640
27641 /* Check any placeholder constraints against the deduced type. */
27642 if (flag_concepts && !processing_template_decl)
27643 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27644 {
27645 /* Use the deduced type to check the associated constraints. If we
27646 have a partial-concept-id, rebuild the argument list so that
27647 we check using the extra arguments. */
27648 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27649 tree cargs = CHECK_CONSTR_ARGS (constr);
27650 if (TREE_VEC_LENGTH (cargs) > 1)
27651 {
27652 cargs = copy_node (cargs);
27653 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27654 }
27655 else
27656 cargs = targs;
27657 if (!constraints_satisfied_p (constr, cargs))
27658 {
27659 if (complain & tf_warning_or_error)
27660 {
27661 auto_diagnostic_group d;
27662 switch (context)
27663 {
27664 case adc_unspecified:
27665 case adc_unify:
27666 error("placeholder constraints not satisfied");
27667 break;
27668 case adc_variable_type:
27669 case adc_decomp_type:
27670 error ("deduced initializer does not satisfy "
27671 "placeholder constraints");
27672 break;
27673 case adc_return_type:
27674 error ("deduced return type does not satisfy "
27675 "placeholder constraints");
27676 break;
27677 case adc_requirement:
27678 error ("deduced expression type does not satisfy "
27679 "placeholder constraints");
27680 break;
27681 }
27682 diagnose_constraints (input_location, constr, targs);
27683 }
27684 return error_mark_node;
27685 }
27686 }
27687
27688 if (processing_template_decl && context != adc_unify)
27689 outer_targs = current_template_args ();
27690 targs = add_to_template_args (outer_targs, targs);
27691 return tsubst (type, targs, complain, NULL_TREE);
27692 }
27693
27694 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27695 result. */
27696
27697 tree
27698 splice_late_return_type (tree type, tree late_return_type)
27699 {
27700 if (is_auto (type))
27701 {
27702 if (late_return_type)
27703 return late_return_type;
27704
27705 tree idx = get_template_parm_index (type);
27706 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27707 /* In an abbreviated function template we didn't know we were dealing
27708 with a function template when we saw the auto return type, so update
27709 it to have the correct level. */
27710 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27711 }
27712 return type;
27713 }
27714
27715 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27716 'decltype(auto)' or a deduced class template. */
27717
27718 bool
27719 is_auto (const_tree type)
27720 {
27721 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27722 && (TYPE_IDENTIFIER (type) == auto_identifier
27723 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
27724 return true;
27725 else
27726 return false;
27727 }
27728
27729 /* for_each_template_parm callback for type_uses_auto. */
27730
27731 int
27732 is_auto_r (tree tp, void */*data*/)
27733 {
27734 return is_auto (tp);
27735 }
27736
27737 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27738 a use of `auto'. Returns NULL_TREE otherwise. */
27739
27740 tree
27741 type_uses_auto (tree type)
27742 {
27743 if (type == NULL_TREE)
27744 return NULL_TREE;
27745 else if (flag_concepts)
27746 {
27747 /* The Concepts TS allows multiple autos in one type-specifier; just
27748 return the first one we find, do_auto_deduction will collect all of
27749 them. */
27750 if (uses_template_parms (type))
27751 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27752 /*visited*/NULL, /*nondeduced*/false);
27753 else
27754 return NULL_TREE;
27755 }
27756 else
27757 return find_type_usage (type, is_auto);
27758 }
27759
27760 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27761 concepts are enabled, auto is acceptable in template arguments, but
27762 only when TEMPL identifies a template class. Return TRUE if any
27763 such errors were reported. */
27764
27765 bool
27766 check_auto_in_tmpl_args (tree tmpl, tree args)
27767 {
27768 /* If there were previous errors, nevermind. */
27769 if (!args || TREE_CODE (args) != TREE_VEC)
27770 return false;
27771
27772 /* If TMPL is an identifier, we're parsing and we can't tell yet
27773 whether TMPL is supposed to be a type, a function or a variable.
27774 We'll only be able to tell during template substitution, so we
27775 expect to be called again then. If concepts are enabled and we
27776 know we have a type, we're ok. */
27777 if (flag_concepts
27778 && (identifier_p (tmpl)
27779 || (DECL_P (tmpl)
27780 && (DECL_TYPE_TEMPLATE_P (tmpl)
27781 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27782 return false;
27783
27784 /* Quickly search for any occurrences of auto; usually there won't
27785 be any, and then we'll avoid allocating the vector. */
27786 if (!type_uses_auto (args))
27787 return false;
27788
27789 bool errors = false;
27790
27791 tree vec = extract_autos (args);
27792 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27793 {
27794 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27795 error_at (DECL_SOURCE_LOCATION (xauto),
27796 "invalid use of %qT in template argument", xauto);
27797 errors = true;
27798 }
27799
27800 return errors;
27801 }
27802
27803 /* For a given template T, return the vector of typedefs referenced
27804 in T for which access check is needed at T instantiation time.
27805 T is either a FUNCTION_DECL or a RECORD_TYPE.
27806 Those typedefs were added to T by the function
27807 append_type_to_template_for_access_check. */
27808
27809 vec<qualified_typedef_usage_t, va_gc> *
27810 get_types_needing_access_check (tree t)
27811 {
27812 tree ti;
27813 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27814
27815 if (!t || t == error_mark_node)
27816 return NULL;
27817
27818 if (!(ti = get_template_info (t)))
27819 return NULL;
27820
27821 if (CLASS_TYPE_P (t)
27822 || TREE_CODE (t) == FUNCTION_DECL)
27823 {
27824 if (!TI_TEMPLATE (ti))
27825 return NULL;
27826
27827 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27828 }
27829
27830 return result;
27831 }
27832
27833 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27834 tied to T. That list of typedefs will be access checked at
27835 T instantiation time.
27836 T is either a FUNCTION_DECL or a RECORD_TYPE.
27837 TYPE_DECL is a TYPE_DECL node representing a typedef.
27838 SCOPE is the scope through which TYPE_DECL is accessed.
27839 LOCATION is the location of the usage point of TYPE_DECL.
27840
27841 This function is a subroutine of
27842 append_type_to_template_for_access_check. */
27843
27844 static void
27845 append_type_to_template_for_access_check_1 (tree t,
27846 tree type_decl,
27847 tree scope,
27848 location_t location)
27849 {
27850 qualified_typedef_usage_t typedef_usage;
27851 tree ti;
27852
27853 if (!t || t == error_mark_node)
27854 return;
27855
27856 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27857 || CLASS_TYPE_P (t))
27858 && type_decl
27859 && TREE_CODE (type_decl) == TYPE_DECL
27860 && scope);
27861
27862 if (!(ti = get_template_info (t)))
27863 return;
27864
27865 gcc_assert (TI_TEMPLATE (ti));
27866
27867 typedef_usage.typedef_decl = type_decl;
27868 typedef_usage.context = scope;
27869 typedef_usage.locus = location;
27870
27871 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27872 }
27873
27874 /* Append TYPE_DECL to the template TEMPL.
27875 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27876 At TEMPL instanciation time, TYPE_DECL will be checked to see
27877 if it can be accessed through SCOPE.
27878 LOCATION is the location of the usage point of TYPE_DECL.
27879
27880 e.g. consider the following code snippet:
27881
27882 class C
27883 {
27884 typedef int myint;
27885 };
27886
27887 template<class U> struct S
27888 {
27889 C::myint mi; // <-- usage point of the typedef C::myint
27890 };
27891
27892 S<char> s;
27893
27894 At S<char> instantiation time, we need to check the access of C::myint
27895 In other words, we need to check the access of the myint typedef through
27896 the C scope. For that purpose, this function will add the myint typedef
27897 and the scope C through which its being accessed to a list of typedefs
27898 tied to the template S. That list will be walked at template instantiation
27899 time and access check performed on each typedefs it contains.
27900 Note that this particular code snippet should yield an error because
27901 myint is private to C. */
27902
27903 void
27904 append_type_to_template_for_access_check (tree templ,
27905 tree type_decl,
27906 tree scope,
27907 location_t location)
27908 {
27909 qualified_typedef_usage_t *iter;
27910 unsigned i;
27911
27912 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27913
27914 /* Make sure we don't append the type to the template twice. */
27915 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27916 if (iter->typedef_decl == type_decl && scope == iter->context)
27917 return;
27918
27919 append_type_to_template_for_access_check_1 (templ, type_decl,
27920 scope, location);
27921 }
27922
27923 /* Convert the generic type parameters in PARM that match the types given in the
27924 range [START_IDX, END_IDX) from the current_template_parms into generic type
27925 packs. */
27926
27927 tree
27928 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27929 {
27930 tree current = current_template_parms;
27931 int depth = TMPL_PARMS_DEPTH (current);
27932 current = INNERMOST_TEMPLATE_PARMS (current);
27933 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27934
27935 for (int i = 0; i < start_idx; ++i)
27936 TREE_VEC_ELT (replacement, i)
27937 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27938
27939 for (int i = start_idx; i < end_idx; ++i)
27940 {
27941 /* Create a distinct parameter pack type from the current parm and add it
27942 to the replacement args to tsubst below into the generic function
27943 parameter. */
27944
27945 tree o = TREE_TYPE (TREE_VALUE
27946 (TREE_VEC_ELT (current, i)));
27947 tree t = copy_type (o);
27948 TEMPLATE_TYPE_PARM_INDEX (t)
27949 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27950 o, 0, 0, tf_none);
27951 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27952 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27953 TYPE_MAIN_VARIANT (t) = t;
27954 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27955 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27956 TREE_VEC_ELT (replacement, i) = t;
27957 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27958 }
27959
27960 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27961 TREE_VEC_ELT (replacement, i)
27962 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27963
27964 /* If there are more levels then build up the replacement with the outer
27965 template parms. */
27966 if (depth > 1)
27967 replacement = add_to_template_args (template_parms_to_args
27968 (TREE_CHAIN (current_template_parms)),
27969 replacement);
27970
27971 return tsubst (parm, replacement, tf_none, NULL_TREE);
27972 }
27973
27974 /* Entries in the decl_constraint hash table. */
27975 struct GTY((for_user)) constr_entry
27976 {
27977 tree decl;
27978 tree ci;
27979 };
27980
27981 /* Hashing function and equality for constraint entries. */
27982 struct constr_hasher : ggc_ptr_hash<constr_entry>
27983 {
27984 static hashval_t hash (constr_entry *e)
27985 {
27986 return (hashval_t)DECL_UID (e->decl);
27987 }
27988
27989 static bool equal (constr_entry *e1, constr_entry *e2)
27990 {
27991 return e1->decl == e2->decl;
27992 }
27993 };
27994
27995 /* A mapping from declarations to constraint information. Note that
27996 both templates and their underlying declarations are mapped to the
27997 same constraint information.
27998
27999 FIXME: This is defined in pt.c because garbage collection
28000 code is not being generated for constraint.cc. */
28001
28002 static GTY (()) hash_table<constr_hasher> *decl_constraints;
28003
28004 /* Returns the template constraints of declaration T. If T is not
28005 constrained, return NULL_TREE. Note that T must be non-null. */
28006
28007 tree
28008 get_constraints (tree t)
28009 {
28010 if (!flag_concepts)
28011 return NULL_TREE;
28012
28013 gcc_assert (DECL_P (t));
28014 if (TREE_CODE (t) == TEMPLATE_DECL)
28015 t = DECL_TEMPLATE_RESULT (t);
28016 constr_entry elt = { t, NULL_TREE };
28017 constr_entry* found = decl_constraints->find (&elt);
28018 if (found)
28019 return found->ci;
28020 else
28021 return NULL_TREE;
28022 }
28023
28024 /* Associate the given constraint information CI with the declaration
28025 T. If T is a template, then the constraints are associated with
28026 its underlying declaration. Don't build associations if CI is
28027 NULL_TREE. */
28028
28029 void
28030 set_constraints (tree t, tree ci)
28031 {
28032 if (!ci)
28033 return;
28034 gcc_assert (t && flag_concepts);
28035 if (TREE_CODE (t) == TEMPLATE_DECL)
28036 t = DECL_TEMPLATE_RESULT (t);
28037 gcc_assert (!get_constraints (t));
28038 constr_entry elt = {t, ci};
28039 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
28040 constr_entry* entry = ggc_alloc<constr_entry> ();
28041 *entry = elt;
28042 *slot = entry;
28043 }
28044
28045 /* Remove the associated constraints of the declaration T. */
28046
28047 void
28048 remove_constraints (tree t)
28049 {
28050 gcc_assert (DECL_P (t));
28051 if (TREE_CODE (t) == TEMPLATE_DECL)
28052 t = DECL_TEMPLATE_RESULT (t);
28053
28054 constr_entry elt = {t, NULL_TREE};
28055 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
28056 if (slot)
28057 decl_constraints->clear_slot (slot);
28058 }
28059
28060 /* Memoized satisfaction results for declarations. This
28061 maps the pair (constraint_info, arguments) to the result computed
28062 by constraints_satisfied_p. */
28063
28064 struct GTY((for_user)) constraint_sat_entry
28065 {
28066 tree ci;
28067 tree args;
28068 tree result;
28069 };
28070
28071 /* Hashing function and equality for constraint entries. */
28072
28073 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
28074 {
28075 static hashval_t hash (constraint_sat_entry *e)
28076 {
28077 hashval_t val = iterative_hash_object(e->ci, 0);
28078 return iterative_hash_template_arg (e->args, val);
28079 }
28080
28081 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
28082 {
28083 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
28084 }
28085 };
28086
28087 /* Memoized satisfaction results for concept checks. */
28088
28089 struct GTY((for_user)) concept_spec_entry
28090 {
28091 tree tmpl;
28092 tree args;
28093 tree result;
28094 };
28095
28096 /* Hashing function and equality for constraint entries. */
28097
28098 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
28099 {
28100 static hashval_t hash (concept_spec_entry *e)
28101 {
28102 return hash_tmpl_and_args (e->tmpl, e->args);
28103 }
28104
28105 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
28106 {
28107 ++comparing_specializations;
28108 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
28109 --comparing_specializations;
28110 return eq;
28111 }
28112 };
28113
28114 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
28115 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
28116
28117 /* Search for a memoized satisfaction result. Returns one of the
28118 truth value nodes if previously memoized, or NULL_TREE otherwise. */
28119
28120 tree
28121 lookup_constraint_satisfaction (tree ci, tree args)
28122 {
28123 constraint_sat_entry elt = { ci, args, NULL_TREE };
28124 constraint_sat_entry* found = constraint_memos->find (&elt);
28125 if (found)
28126 return found->result;
28127 else
28128 return NULL_TREE;
28129 }
28130
28131 /* Memoize the result of a satisfication test. Returns the saved result. */
28132
28133 tree
28134 memoize_constraint_satisfaction (tree ci, tree args, tree result)
28135 {
28136 constraint_sat_entry elt = {ci, args, result};
28137 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
28138 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
28139 *entry = elt;
28140 *slot = entry;
28141 return result;
28142 }
28143
28144 /* Search for a memoized satisfaction result for a concept. */
28145
28146 tree
28147 lookup_concept_satisfaction (tree tmpl, tree args)
28148 {
28149 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28150 concept_spec_entry* found = concept_memos->find (&elt);
28151 if (found)
28152 return found->result;
28153 else
28154 return NULL_TREE;
28155 }
28156
28157 /* Memoize the result of a concept check. Returns the saved result. */
28158
28159 tree
28160 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
28161 {
28162 concept_spec_entry elt = {tmpl, args, result};
28163 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
28164 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28165 *entry = elt;
28166 *slot = entry;
28167 return result;
28168 }
28169
28170 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
28171
28172 /* Returns a prior concept specialization. This returns the substituted
28173 and normalized constraints defined by the concept. */
28174
28175 tree
28176 get_concept_expansion (tree tmpl, tree args)
28177 {
28178 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28179 concept_spec_entry* found = concept_expansions->find (&elt);
28180 if (found)
28181 return found->result;
28182 else
28183 return NULL_TREE;
28184 }
28185
28186 /* Save a concept expansion for later. */
28187
28188 tree
28189 save_concept_expansion (tree tmpl, tree args, tree def)
28190 {
28191 concept_spec_entry elt = {tmpl, args, def};
28192 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
28193 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28194 *entry = elt;
28195 *slot = entry;
28196 return def;
28197 }
28198
28199 static hashval_t
28200 hash_subsumption_args (tree t1, tree t2)
28201 {
28202 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
28203 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
28204 int val = 0;
28205 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
28206 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
28207 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
28208 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
28209 return val;
28210 }
28211
28212 /* Compare the constraints of two subsumption entries. The LEFT1 and
28213 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
28214 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
28215
28216 static bool
28217 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
28218 {
28219 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
28220 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
28221 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
28222 CHECK_CONSTR_ARGS (right1)))
28223 return comp_template_args (CHECK_CONSTR_ARGS (left2),
28224 CHECK_CONSTR_ARGS (right2));
28225 return false;
28226 }
28227
28228 /* Key/value pair for learning and memoizing subsumption results. This
28229 associates a pair of check constraints (including arguments) with
28230 a boolean value indicating the result. */
28231
28232 struct GTY((for_user)) subsumption_entry
28233 {
28234 tree t1;
28235 tree t2;
28236 bool result;
28237 };
28238
28239 /* Hashing function and equality for constraint entries. */
28240
28241 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
28242 {
28243 static hashval_t hash (subsumption_entry *e)
28244 {
28245 return hash_subsumption_args (e->t1, e->t2);
28246 }
28247
28248 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
28249 {
28250 ++comparing_specializations;
28251 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
28252 --comparing_specializations;
28253 return eq;
28254 }
28255 };
28256
28257 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
28258
28259 /* Search for a previously cached subsumption result. */
28260
28261 bool*
28262 lookup_subsumption_result (tree t1, tree t2)
28263 {
28264 subsumption_entry elt = { t1, t2, false };
28265 subsumption_entry* found = subsumption_table->find (&elt);
28266 if (found)
28267 return &found->result;
28268 else
28269 return 0;
28270 }
28271
28272 /* Save a subsumption result. */
28273
28274 bool
28275 save_subsumption_result (tree t1, tree t2, bool result)
28276 {
28277 subsumption_entry elt = {t1, t2, result};
28278 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
28279 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
28280 *entry = elt;
28281 *slot = entry;
28282 return result;
28283 }
28284
28285 /* Set up the hash table for constraint association. */
28286
28287 void
28288 init_constraint_processing (void)
28289 {
28290 if (!flag_concepts)
28291 return;
28292
28293 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
28294 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
28295 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
28296 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
28297 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
28298 }
28299
28300 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28301 0..N-1. */
28302
28303 void
28304 declare_integer_pack (void)
28305 {
28306 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28307 build_function_type_list (integer_type_node,
28308 integer_type_node,
28309 NULL_TREE),
28310 NULL_TREE, ECF_CONST);
28311 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28312 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
28313 DECL_FUNCTION_CODE (ipfn)
28314 = (enum built_in_function) (int) CP_BUILT_IN_INTEGER_PACK;
28315 }
28316
28317 /* Set up the hash tables for template instantiations. */
28318
28319 void
28320 init_template_processing (void)
28321 {
28322 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
28323 type_specializations = hash_table<spec_hasher>::create_ggc (37);
28324
28325 if (cxx_dialect >= cxx11)
28326 declare_integer_pack ();
28327 }
28328
28329 /* Print stats about the template hash tables for -fstats. */
28330
28331 void
28332 print_template_statistics (void)
28333 {
28334 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28335 "%f collisions\n", (long) decl_specializations->size (),
28336 (long) decl_specializations->elements (),
28337 decl_specializations->collisions ());
28338 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28339 "%f collisions\n", (long) type_specializations->size (),
28340 (long) type_specializations->elements (),
28341 type_specializations->collisions ());
28342 }
28343
28344 #if CHECKING_P
28345
28346 namespace selftest {
28347
28348 /* Verify that build_non_dependent_expr () works, for various expressions,
28349 and that location wrappers don't affect the results. */
28350
28351 static void
28352 test_build_non_dependent_expr ()
28353 {
28354 location_t loc = BUILTINS_LOCATION;
28355
28356 /* Verify constants, without and with location wrappers. */
28357 tree int_cst = build_int_cst (integer_type_node, 42);
28358 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28359
28360 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28361 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28362 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28363
28364 tree string_lit = build_string (4, "foo");
28365 TREE_TYPE (string_lit) = char_array_type_node;
28366 string_lit = fix_string_type (string_lit);
28367 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28368
28369 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28370 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28371 ASSERT_EQ (wrapped_string_lit,
28372 build_non_dependent_expr (wrapped_string_lit));
28373 }
28374
28375 /* Verify that type_dependent_expression_p () works correctly, even
28376 in the presence of location wrapper nodes. */
28377
28378 static void
28379 test_type_dependent_expression_p ()
28380 {
28381 location_t loc = BUILTINS_LOCATION;
28382
28383 tree name = get_identifier ("foo");
28384
28385 /* If no templates are involved, nothing is type-dependent. */
28386 gcc_assert (!processing_template_decl);
28387 ASSERT_FALSE (type_dependent_expression_p (name));
28388
28389 ++processing_template_decl;
28390
28391 /* Within a template, an unresolved name is always type-dependent. */
28392 ASSERT_TRUE (type_dependent_expression_p (name));
28393
28394 /* Ensure it copes with NULL_TREE and errors. */
28395 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28396 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28397
28398 /* A USING_DECL in a template should be type-dependent, even if wrapped
28399 with a location wrapper (PR c++/83799). */
28400 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28401 TREE_TYPE (using_decl) = integer_type_node;
28402 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28403 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28404 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28405 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28406
28407 --processing_template_decl;
28408 }
28409
28410 /* Run all of the selftests within this file. */
28411
28412 void
28413 cp_pt_c_tests ()
28414 {
28415 test_build_non_dependent_expr ();
28416 test_type_dependent_expression_p ();
28417 }
28418
28419 } // namespace selftest
28420
28421 #endif /* #if CHECKING_P */
28422
28423 #include "gt-cp-pt.h"