1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 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 #include "target.h"
46
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
50
51 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
55 {
56 struct pending_template *next;
57 struct tinst_level *tinst;
58 };
59
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
62
63 int processing_template_parmlist;
64 static int template_header_count;
65
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) vec<tree, va_gc> *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.
local_specialization_stack(lss_policy policy)82 local_specialization_stack::local_specialization_stack (lss_policy policy)
83 : saved (local_specializations)
84 {
85 if (policy == lss_nop)
86 ;
87 else if (policy == lss_blank || !saved)
88 local_specializations = new hash_map<tree, tree>;
89 else
90 local_specializations = new hash_map<tree, tree>(*saved);
91 }
92
~local_specialization_stack()93 local_specialization_stack::~local_specialization_stack ()
94 {
95 if (local_specializations != saved)
96 {
97 delete local_specializations;
98 local_specializations = saved;
99 }
100 }
101
102 /* True if we've recursed into fn_type_unification too many times. */
103 static bool excessive_deduction_depth;
104
105 struct GTY((for_user)) spec_entry
106 {
107 tree tmpl;
108 tree args;
109 tree spec;
110 };
111
112 struct spec_hasher : ggc_ptr_hash<spec_entry>
113 {
114 static hashval_t hash (spec_entry *);
115 static bool equal (spec_entry *, spec_entry *);
116 };
117
118 static GTY (()) hash_table<spec_hasher> *decl_specializations;
119
120 static GTY (()) hash_table<spec_hasher> *type_specializations;
121
122 /* Contains canonical template parameter types. The vector is indexed by
123 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
124 TREE_LIST, whose TREE_VALUEs contain the canonical template
125 parameters of various types and levels. */
126 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
127
128 #define UNIFY_ALLOW_NONE 0
129 #define UNIFY_ALLOW_MORE_CV_QUAL 1
130 #define UNIFY_ALLOW_LESS_CV_QUAL 2
131 #define UNIFY_ALLOW_DERIVED 4
132 #define UNIFY_ALLOW_INTEGER 8
133 #define UNIFY_ALLOW_OUTER_LEVEL 16
134 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
135 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
136
137 enum template_base_result {
138 tbr_incomplete_type,
139 tbr_ambiguous_baseclass,
140 tbr_success
141 };
142
143 static bool resolve_overloaded_unification (tree, tree, tree, tree,
144 unification_kind_t, int,
145 bool);
146 static int try_one_overload (tree, tree, tree, tree, tree,
147 unification_kind_t, int, bool, bool);
148 static int unify (tree, tree, tree, tree, int, bool);
149 static void add_pending_template (tree);
150 static tree reopen_tinst_level (struct tinst_level *);
151 static tree tsubst_initializer_list (tree, tree);
152 static tree get_partial_spec_bindings (tree, tree, tree);
153 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
154 bool, bool);
155 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
156 bool, bool);
157 static void tsubst_enum (tree, tree, tree);
158 static tree add_to_template_args (tree, tree);
159 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
160 static int check_non_deducible_conversion (tree, tree, int, int,
161 struct conversion **, bool);
162 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
163 tree);
164 static int type_unification_real (tree, tree, tree, const tree *,
165 unsigned int, int, unification_kind_t,
166 vec<deferred_access_check, va_gc> **,
167 bool);
168 static void note_template_header (int);
169 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
170 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
171 static tree convert_template_argument (tree, tree, tree,
172 tsubst_flags_t, int, tree);
173 static tree for_each_template_parm (tree, tree_fn_t, void*,
174 hash_set<tree> *, bool, tree_fn_t = NULL);
175 static tree expand_template_argument_pack (tree);
176 static tree build_template_parm_index (int, int, int, tree, tree);
177 static bool inline_needs_template_parms (tree, bool);
178 static void push_inline_template_parms_recursive (tree, int);
179 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
180 static int mark_template_parm (tree, void *);
181 static int template_parm_this_level_p (tree, void *);
182 static tree tsubst_friend_function (tree, tree);
183 static tree tsubst_friend_class (tree, tree);
184 static int can_complete_type_without_circularity (tree);
185 static tree get_bindings (tree, tree, tree, bool);
186 static int template_decl_level (tree);
187 static int check_cv_quals_for_unify (int, tree, tree);
188 static int unify_pack_expansion (tree, tree, tree,
189 tree, unification_kind_t, bool, bool);
190 static tree copy_template_args (tree);
191 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
192 tree most_specialized_partial_spec (tree, tsubst_flags_t);
193 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
194 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
195 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
196 static bool check_specialization_scope (void);
197 static tree process_partial_specialization (tree);
198 static void set_current_access_from_decl (tree);
199 static enum template_base_result get_template_base (tree, tree, tree, tree,
200 bool , tree *);
201 static tree try_class_unification (tree, tree, tree, tree, bool);
202 static bool class_nttp_const_wrapper_p (tree t);
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 get_underlying_template (tree);
225 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
226 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
227 static tree make_argument_pack (tree);
228 static void register_parameter_specializations (tree, tree);
229 static tree enclosing_instantiation_of (tree tctx);
230
231 /* Make the current scope suitable for access checking when we are
232 processing T. T can be FUNCTION_DECL for instantiated function
233 template, VAR_DECL for static member variable, or TYPE_DECL for
234 alias template (needed by instantiate_decl). */
235
236 void
push_access_scope(tree t)237 push_access_scope (tree t)
238 {
239 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
240 || TREE_CODE (t) == TYPE_DECL);
241
242 if (DECL_FRIEND_CONTEXT (t))
243 push_nested_class (DECL_FRIEND_CONTEXT (t));
244 else if (DECL_CLASS_SCOPE_P (t))
245 push_nested_class (DECL_CONTEXT (t));
246 else
247 push_to_top_level ();
248
249 if (TREE_CODE (t) == FUNCTION_DECL)
250 {
251 vec_safe_push (saved_access_scope, current_function_decl);
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 void
pop_access_scope(tree t)260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 current_function_decl = saved_access_scope->pop();
264
265 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
266 pop_nested_class ();
267 else
268 pop_from_top_level ();
269 }
270
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
275
276 tree
finish_member_template_decl(tree decl)277 finish_member_template_decl (tree decl)
278 {
279 if (decl == error_mark_node)
280 return error_mark_node;
281
282 gcc_assert (DECL_P (decl));
283
284 if (TREE_CODE (decl) == TYPE_DECL)
285 {
286 tree type;
287
288 type = TREE_TYPE (decl);
289 if (type == error_mark_node)
290 return error_mark_node;
291 if (MAYBE_CLASS_TYPE_P (type)
292 && CLASSTYPE_TEMPLATE_INFO (type)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
294 {
295 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
296 check_member_template (tmpl);
297 return tmpl;
298 }
299 return NULL_TREE;
300 }
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error_at (DECL_SOURCE_LOCATION (decl),
303 "data member %qD cannot be a member template", decl);
304 else if (DECL_TEMPLATE_INFO (decl))
305 {
306 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
307 {
308 check_member_template (DECL_TI_TEMPLATE (decl));
309 return DECL_TI_TEMPLATE (decl);
310 }
311 else
312 return decl;
313 }
314 else
315 error_at (DECL_SOURCE_LOCATION (decl),
316 "invalid member template declaration %qD", decl);
317
318 return error_mark_node;
319 }
320
321 /* Create a template info node. */
322
323 tree
build_template_info(tree template_decl,tree template_args)324 build_template_info (tree template_decl, tree template_args)
325 {
326 tree result = make_node (TEMPLATE_INFO);
327 TI_TEMPLATE (result) = template_decl;
328 TI_ARGS (result) = template_args;
329 return result;
330 }
331
332 /* Return the template info node corresponding to T, whatever T is. */
333
334 tree
get_template_info(const_tree t)335 get_template_info (const_tree t)
336 {
337 tree tinfo = NULL_TREE;
338
339 if (!t || t == error_mark_node)
340 return NULL;
341
342 if (TREE_CODE (t) == NAMESPACE_DECL
343 || TREE_CODE (t) == PARM_DECL)
344 return NULL;
345
346 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
347 tinfo = DECL_TEMPLATE_INFO (t);
348
349 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
350 t = TREE_TYPE (t);
351
352 if (OVERLOAD_TYPE_P (t))
353 tinfo = TYPE_TEMPLATE_INFO (t);
354 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
355 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
356
357 return tinfo;
358 }
359
360 /* Returns the template nesting level of the indicated class TYPE.
361
362 For example, in:
363 template <class T>
364 struct A
365 {
366 template <class U>
367 struct B {};
368 };
369
370 A<T>::B<U> has depth two, while A<T> has depth one.
371 Both A<T>::B<int> and A<int>::B<U> have depth one, if
372 they are instantiations, not specializations.
373
374 This function is guaranteed to return 0 if passed NULL_TREE so
375 that, for example, `template_class_depth (current_class_type)' is
376 always safe. */
377
378 int
template_class_depth(tree type)379 template_class_depth (tree type)
380 {
381 int depth;
382
383 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
384 {
385 tree tinfo = get_template_info (type);
386
387 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
388 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
389 ++depth;
390
391 if (DECL_P (type))
392 {
393 if (tree fctx = DECL_FRIEND_CONTEXT (type))
394 type = fctx;
395 else
396 type = CP_DECL_CONTEXT (type);
397 }
398 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
399 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
400 else
401 type = CP_TYPE_CONTEXT (type);
402 }
403
404 return depth;
405 }
406
407 /* Return TRUE if NODE instantiates a template that has arguments of
408 its own, be it directly a primary template or indirectly through a
409 partial specializations. */
410 static bool
instantiates_primary_template_p(tree node)411 instantiates_primary_template_p (tree node)
412 {
413 tree tinfo = get_template_info (node);
414 if (!tinfo)
415 return false;
416
417 tree tmpl = TI_TEMPLATE (tinfo);
418 if (PRIMARY_TEMPLATE_P (tmpl))
419 return true;
420
421 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
422 return false;
423
424 /* So now we know we have a specialization, but it could be a full
425 or a partial specialization. To tell which, compare the depth of
426 its template arguments with those of its context. */
427
428 tree ctxt = DECL_CONTEXT (tmpl);
429 tree ctinfo = get_template_info (ctxt);
430 if (!ctinfo)
431 return true;
432
433 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
434 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
435 }
436
437 /* Subroutine of maybe_begin_member_template_processing.
438 Returns true if processing DECL needs us to push template parms. */
439
440 static bool
inline_needs_template_parms(tree decl,bool nsdmi)441 inline_needs_template_parms (tree decl, bool nsdmi)
442 {
443 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
444 return false;
445
446 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
447 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
448 }
449
450 /* Subroutine of maybe_begin_member_template_processing.
451 Push the template parms in PARMS, starting from LEVELS steps into the
452 chain, and ending at the beginning, since template parms are listed
453 innermost first. */
454
455 static void
push_inline_template_parms_recursive(tree parmlist,int levels)456 push_inline_template_parms_recursive (tree parmlist, int levels)
457 {
458 tree parms = TREE_VALUE (parmlist);
459 int i;
460
461 if (levels > 1)
462 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
463
464 ++processing_template_decl;
465 current_template_parms
466 = tree_cons (size_int (processing_template_decl),
467 parms, current_template_parms);
468 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
469
470 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
471 NULL);
472 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
473 {
474 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
475
476 if (error_operand_p (parm))
477 continue;
478
479 gcc_assert (DECL_P (parm));
480
481 switch (TREE_CODE (parm))
482 {
483 case TYPE_DECL:
484 case TEMPLATE_DECL:
485 pushdecl (parm);
486 break;
487
488 case PARM_DECL:
489 /* Push the CONST_DECL. */
490 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
491 break;
492
493 default:
494 gcc_unreachable ();
495 }
496 }
497 }
498
499 /* Restore the template parameter context for a member template, a
500 friend template defined in a class definition, or a non-template
501 member of template class. */
502
503 void
maybe_begin_member_template_processing(tree decl)504 maybe_begin_member_template_processing (tree decl)
505 {
506 tree parms;
507 int levels = 0;
508 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
509
510 if (nsdmi)
511 {
512 tree ctx = DECL_CONTEXT (decl);
513 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
514 /* Disregard full specializations (c++/60999). */
515 && uses_template_parms (ctx)
516 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
517 }
518
519 if (inline_needs_template_parms (decl, nsdmi))
520 {
521 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
522 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
523
524 if (DECL_TEMPLATE_SPECIALIZATION (decl))
525 {
526 --levels;
527 parms = TREE_CHAIN (parms);
528 }
529
530 push_inline_template_parms_recursive (parms, levels);
531 }
532
533 /* Remember how many levels of template parameters we pushed so that
534 we can pop them later. */
535 inline_parm_levels.safe_push (levels);
536 }
537
538 /* Undo the effects of maybe_begin_member_template_processing. */
539
540 void
maybe_end_member_template_processing(void)541 maybe_end_member_template_processing (void)
542 {
543 int i;
544 int last;
545
546 if (inline_parm_levels.length () == 0)
547 return;
548
549 last = inline_parm_levels.pop ();
550 for (i = 0; i < last; ++i)
551 {
552 --processing_template_decl;
553 current_template_parms = TREE_CHAIN (current_template_parms);
554 poplevel (0, 0, 0);
555 }
556 }
557
558 /* Return a new template argument vector which contains all of ARGS,
559 but has as its innermost set of arguments the EXTRA_ARGS. */
560
561 static tree
add_to_template_args(tree args,tree extra_args)562 add_to_template_args (tree args, tree extra_args)
563 {
564 tree new_args;
565 int extra_depth;
566 int i;
567 int j;
568
569 if (args == NULL_TREE || extra_args == error_mark_node)
570 return extra_args;
571
572 extra_depth = TMPL_ARGS_DEPTH (extra_args);
573 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
574
575 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
576 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
577
578 for (j = 1; j <= extra_depth; ++j, ++i)
579 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
580
581 return new_args;
582 }
583
584 /* Like add_to_template_args, but only the outermost ARGS are added to
585 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
586 (EXTRA_ARGS) levels are added. This function is used to combine
587 the template arguments from a partial instantiation with the
588 template arguments used to attain the full instantiation from the
589 partial instantiation. */
590
591 tree
add_outermost_template_args(tree args,tree extra_args)592 add_outermost_template_args (tree args, tree extra_args)
593 {
594 tree new_args;
595
596 /* If there are more levels of EXTRA_ARGS than there are ARGS,
597 something very fishy is going on. */
598 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
599
600 /* If *all* the new arguments will be the EXTRA_ARGS, just return
601 them. */
602 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
603 return extra_args;
604
605 /* For the moment, we make ARGS look like it contains fewer levels. */
606 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
607
608 new_args = add_to_template_args (args, extra_args);
609
610 /* Now, we restore ARGS to its full dimensions. */
611 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
612
613 return new_args;
614 }
615
616 /* Return the N levels of innermost template arguments from the ARGS. */
617
618 tree
get_innermost_template_args(tree args,int n)619 get_innermost_template_args (tree args, int n)
620 {
621 tree new_args;
622 int extra_levels;
623 int i;
624
625 gcc_assert (n >= 0);
626
627 /* If N is 1, just return the innermost set of template arguments. */
628 if (n == 1)
629 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
630
631 /* If we're not removing anything, just return the arguments we were
632 given. */
633 extra_levels = TMPL_ARGS_DEPTH (args) - n;
634 gcc_assert (extra_levels >= 0);
635 if (extra_levels == 0)
636 return args;
637
638 /* Make a new set of arguments, not containing the outer arguments. */
639 new_args = make_tree_vec (n);
640 for (i = 1; i <= n; ++i)
641 SET_TMPL_ARGS_LEVEL (new_args, i,
642 TMPL_ARGS_LEVEL (args, i + extra_levels));
643
644 return new_args;
645 }
646
647 /* The inverse of get_innermost_template_args: Return all but the innermost
648 EXTRA_LEVELS levels of template arguments from the ARGS. */
649
650 static tree
strip_innermost_template_args(tree args,int extra_levels)651 strip_innermost_template_args (tree args, int extra_levels)
652 {
653 tree new_args;
654 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
655 int i;
656
657 gcc_assert (n >= 0);
658
659 /* If N is 1, just return the outermost set of template arguments. */
660 if (n == 1)
661 return TMPL_ARGS_LEVEL (args, 1);
662
663 /* If we're not removing anything, just return the arguments we were
664 given. */
665 gcc_assert (extra_levels >= 0);
666 if (extra_levels == 0)
667 return args;
668
669 /* Make a new set of arguments, not containing the inner arguments. */
670 new_args = make_tree_vec (n);
671 for (i = 1; i <= n; ++i)
672 SET_TMPL_ARGS_LEVEL (new_args, i,
673 TMPL_ARGS_LEVEL (args, i));
674
675 return new_args;
676 }
677
678 /* We've got a template header coming up; push to a new level for storing
679 the parms. */
680
681 void
begin_template_parm_list(void)682 begin_template_parm_list (void)
683 {
684 /* We use a non-tag-transparent scope here, which causes pushtag to
685 put tags in this scope, rather than in the enclosing class or
686 namespace scope. This is the right thing, since we want
687 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
688 global template class, push_template_decl handles putting the
689 TEMPLATE_DECL into top-level scope. For a nested template class,
690 e.g.:
691
692 template <class T> struct S1 {
693 template <class T> struct S2 {};
694 };
695
696 pushtag contains special code to insert the TEMPLATE_DECL for S2
697 at the right scope. */
698 begin_scope (sk_template_parms, NULL);
699 ++processing_template_decl;
700 ++processing_template_parmlist;
701 note_template_header (0);
702
703 /* Add a dummy parameter level while we process the parameter list. */
704 current_template_parms
705 = tree_cons (size_int (processing_template_decl),
706 make_tree_vec (0),
707 current_template_parms);
708 }
709
710 /* This routine is called when a specialization is declared. If it is
711 invalid to declare a specialization here, an error is reported and
712 false is returned, otherwise this routine will return true. */
713
714 static bool
check_specialization_scope(void)715 check_specialization_scope (void)
716 {
717 tree scope = current_scope ();
718
719 /* [temp.expl.spec]
720
721 An explicit specialization shall be declared in the namespace of
722 which the template is a member, or, for member templates, in the
723 namespace of which the enclosing class or enclosing class
724 template is a member. An explicit specialization of a member
725 function, member class or static data member of a class template
726 shall be declared in the namespace of which the class template
727 is a member. */
728 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
729 {
730 error ("explicit specialization in non-namespace scope %qD", scope);
731 return false;
732 }
733
734 /* [temp.expl.spec]
735
736 In an explicit specialization declaration for a member of a class
737 template or a member template that appears in namespace scope,
738 the member template and some of its enclosing class templates may
739 remain unspecialized, except that the declaration shall not
740 explicitly specialize a class member template if its enclosing
741 class templates are not explicitly specialized as well. */
742 if (current_template_parms)
743 {
744 error ("enclosing class templates are not explicitly specialized");
745 return false;
746 }
747
748 return true;
749 }
750
751 /* We've just seen template <>. */
752
753 bool
begin_specialization(void)754 begin_specialization (void)
755 {
756 begin_scope (sk_template_spec, NULL);
757 note_template_header (1);
758 return check_specialization_scope ();
759 }
760
761 /* Called at then end of processing a declaration preceded by
762 template<>. */
763
764 void
end_specialization(void)765 end_specialization (void)
766 {
767 finish_scope ();
768 reset_specialization ();
769 }
770
771 /* Any template <>'s that we have seen thus far are not referring to a
772 function specialization. */
773
774 void
reset_specialization(void)775 reset_specialization (void)
776 {
777 processing_specialization = 0;
778 template_header_count = 0;
779 }
780
781 /* We've just seen a template header. If SPECIALIZATION is nonzero,
782 it was of the form template <>. */
783
784 static void
note_template_header(int specialization)785 note_template_header (int specialization)
786 {
787 processing_specialization = specialization;
788 template_header_count++;
789 }
790
791 /* We're beginning an explicit instantiation. */
792
793 void
begin_explicit_instantiation(void)794 begin_explicit_instantiation (void)
795 {
796 gcc_assert (!processing_explicit_instantiation);
797 processing_explicit_instantiation = true;
798 }
799
800
801 void
end_explicit_instantiation(void)802 end_explicit_instantiation (void)
803 {
804 gcc_assert (processing_explicit_instantiation);
805 processing_explicit_instantiation = false;
806 }
807
808 /* An explicit specialization or partial specialization of TMPL is being
809 declared. Check that the namespace in which the specialization is
810 occurring is permissible. Returns false iff it is invalid to
811 specialize TMPL in the current namespace. */
812
813 static bool
check_specialization_namespace(tree tmpl)814 check_specialization_namespace (tree tmpl)
815 {
816 tree tpl_ns = decl_namespace_context (tmpl);
817
818 /* [tmpl.expl.spec]
819
820 An explicit specialization shall be declared in a namespace enclosing the
821 specialized template. An explicit specialization whose declarator-id is
822 not qualified shall be declared in the nearest enclosing namespace of the
823 template, or, if the namespace is inline (7.3.1), any namespace from its
824 enclosing namespace set. */
825 if (current_scope() != DECL_CONTEXT (tmpl)
826 && !at_namespace_scope_p ())
827 {
828 error ("specialization of %qD must appear at namespace scope", tmpl);
829 return false;
830 }
831
832 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
833 /* Same or enclosing namespace. */
834 return true;
835 else
836 {
837 auto_diagnostic_group d;
838 if (permerror (input_location,
839 "specialization of %qD in different namespace", tmpl))
840 inform (DECL_SOURCE_LOCATION (tmpl),
841 " from definition of %q#D", tmpl);
842 return false;
843 }
844 }
845
846 /* SPEC is an explicit instantiation. Check that it is valid to
847 perform this explicit instantiation in the current namespace. */
848
849 static void
check_explicit_instantiation_namespace(tree spec)850 check_explicit_instantiation_namespace (tree spec)
851 {
852 tree ns;
853
854 /* DR 275: An explicit instantiation shall appear in an enclosing
855 namespace of its template. */
856 ns = decl_namespace_context (spec);
857 if (!is_nested_namespace (current_namespace, ns))
858 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
859 "(which does not enclose namespace %qD)",
860 spec, current_namespace, ns);
861 }
862
863 /* Returns the type of a template specialization only if that
864 specialization needs to be defined. Otherwise (e.g., if the type has
865 already been defined), the function returns NULL_TREE. */
866
867 static tree
maybe_new_partial_specialization(tree type)868 maybe_new_partial_specialization (tree type)
869 {
870 /* An implicit instantiation of an incomplete type implies
871 the definition of a new class template.
872
873 template<typename T>
874 struct S;
875
876 template<typename T>
877 struct S<T*>;
878
879 Here, S<T*> is an implicit instantiation of S whose type
880 is incomplete. */
881 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
882 return type;
883
884 /* It can also be the case that TYPE is a completed specialization.
885 Continuing the previous example, suppose we also declare:
886
887 template<typename T>
888 requires Integral<T>
889 struct S<T*>;
890
891 Here, S<T*> refers to the specialization S<T*> defined
892 above. However, we need to differentiate definitions because
893 we intend to define a new partial specialization. In this case,
894 we rely on the fact that the constraints are different for
895 this declaration than that above.
896
897 Note that we also get here for injected class names and
898 late-parsed template definitions. We must ensure that we
899 do not create new type declarations for those cases. */
900 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
901 {
902 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
903 tree args = CLASSTYPE_TI_ARGS (type);
904
905 /* If there are no template parameters, this cannot be a new
906 partial template specialization? */
907 if (!current_template_parms)
908 return NULL_TREE;
909
910 /* The injected-class-name is not a new partial specialization. */
911 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
912 return NULL_TREE;
913
914 /* If the constraints are not the same as those of the primary
915 then, we can probably create a new specialization. */
916 tree type_constr = current_template_constraints ();
917
918 if (type == TREE_TYPE (tmpl))
919 {
920 tree main_constr = get_constraints (tmpl);
921 if (equivalent_constraints (type_constr, main_constr))
922 return NULL_TREE;
923 }
924
925 /* Also, if there's a pre-existing specialization with matching
926 constraints, then this also isn't new. */
927 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
928 while (specs)
929 {
930 tree spec_tmpl = TREE_VALUE (specs);
931 tree spec_args = TREE_PURPOSE (specs);
932 tree spec_constr = get_constraints (spec_tmpl);
933 if (comp_template_args (args, spec_args)
934 && equivalent_constraints (type_constr, spec_constr))
935 return NULL_TREE;
936 specs = TREE_CHAIN (specs);
937 }
938
939 /* Create a new type node (and corresponding type decl)
940 for the newly declared specialization. */
941 tree t = make_class_type (TREE_CODE (type));
942 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
943 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
944
945 /* We only need a separate type node for storing the definition of this
946 partial specialization; uses of S<T*> are unconstrained, so all are
947 equivalent. So keep TYPE_CANONICAL the same. */
948 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
949
950 /* Build the corresponding type decl. */
951 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
952 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
953 DECL_SOURCE_LOCATION (d) = input_location;
954 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
955 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
956
957 return t;
958 }
959
960 return NULL_TREE;
961 }
962
963 /* The TYPE is being declared. If it is a template type, that means it
964 is a partial specialization. Do appropriate error-checking. */
965
966 tree
maybe_process_partial_specialization(tree type)967 maybe_process_partial_specialization (tree type)
968 {
969 tree context;
970
971 if (type == error_mark_node)
972 return error_mark_node;
973
974 /* A lambda that appears in specialization context is not itself a
975 specialization. */
976 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
977 return type;
978
979 /* An injected-class-name is not a specialization. */
980 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
981 return type;
982
983 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
984 {
985 error ("name of class shadows template template parameter %qD",
986 TYPE_NAME (type));
987 return error_mark_node;
988 }
989
990 context = TYPE_CONTEXT (type);
991
992 if (TYPE_ALIAS_P (type))
993 {
994 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
995
996 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
997 error ("specialization of alias template %qD",
998 TI_TEMPLATE (tinfo));
999 else
1000 error ("explicit specialization of non-template %qT", type);
1001 return error_mark_node;
1002 }
1003 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1004 {
1005 /* This is for ordinary explicit specialization and partial
1006 specialization of a template class such as:
1007
1008 template <> class C<int>;
1009
1010 or:
1011
1012 template <class T> class C<T*>;
1013
1014 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1015
1016 if (tree t = maybe_new_partial_specialization (type))
1017 {
1018 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1019 && !at_namespace_scope_p ())
1020 return error_mark_node;
1021 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1022 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1023 if (processing_template_decl)
1024 {
1025 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1026 if (decl == error_mark_node)
1027 return error_mark_node;
1028 return TREE_TYPE (decl);
1029 }
1030 }
1031 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1032 error ("specialization of %qT after instantiation", type);
1033 else if (errorcount && !processing_specialization
1034 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1035 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1036 /* Trying to define a specialization either without a template<> header
1037 or in an inappropriate place. We've already given an error, so just
1038 bail now so we don't actually define the specialization. */
1039 return error_mark_node;
1040 }
1041 else if (CLASS_TYPE_P (type)
1042 && !CLASSTYPE_USE_TEMPLATE (type)
1043 && CLASSTYPE_TEMPLATE_INFO (type)
1044 && context && CLASS_TYPE_P (context)
1045 && CLASSTYPE_TEMPLATE_INFO (context))
1046 {
1047 /* This is for an explicit specialization of member class
1048 template according to [temp.expl.spec/18]:
1049
1050 template <> template <class U> class C<int>::D;
1051
1052 The context `C<int>' must be an implicit instantiation.
1053 Otherwise this is just a member class template declared
1054 earlier like:
1055
1056 template <> class C<int> { template <class U> class D; };
1057 template <> template <class U> class C<int>::D;
1058
1059 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1060 while in the second case, `C<int>::D' is a primary template
1061 and `C<T>::D' may not exist. */
1062
1063 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1064 && !COMPLETE_TYPE_P (type))
1065 {
1066 tree t;
1067 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1068
1069 if (current_namespace
1070 != decl_namespace_context (tmpl))
1071 {
1072 if (permerror (input_location,
1073 "specialization of %qD in different namespace",
1074 type))
1075 inform (DECL_SOURCE_LOCATION (tmpl),
1076 "from definition of %q#D", tmpl);
1077 }
1078
1079 /* Check for invalid specialization after instantiation:
1080
1081 template <> template <> class C<int>::D<int>;
1082 template <> template <class U> class C<int>::D; */
1083
1084 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1085 t; t = TREE_CHAIN (t))
1086 {
1087 tree inst = TREE_VALUE (t);
1088 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1089 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1090 {
1091 /* We already have a full specialization of this partial
1092 instantiation, or a full specialization has been
1093 looked up but not instantiated. Reassign it to the
1094 new member specialization template. */
1095 spec_entry elt;
1096 spec_entry *entry;
1097
1098 elt.tmpl = most_general_template (tmpl);
1099 elt.args = CLASSTYPE_TI_ARGS (inst);
1100 elt.spec = inst;
1101
1102 type_specializations->remove_elt (&elt);
1103
1104 elt.tmpl = tmpl;
1105 CLASSTYPE_TI_ARGS (inst)
1106 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1107
1108 spec_entry **slot
1109 = type_specializations->find_slot (&elt, INSERT);
1110 entry = ggc_alloc<spec_entry> ();
1111 *entry = elt;
1112 *slot = entry;
1113 }
1114 else
1115 /* But if we've had an implicit instantiation, that's a
1116 problem ([temp.expl.spec]/6). */
1117 error ("specialization %qT after instantiation %qT",
1118 type, inst);
1119 }
1120
1121 /* Mark TYPE as a specialization. And as a result, we only
1122 have one level of template argument for the innermost
1123 class template. */
1124 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1125 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1126 CLASSTYPE_TI_ARGS (type)
1127 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1128 }
1129 }
1130 else if (processing_specialization)
1131 {
1132 /* Someday C++0x may allow for enum template specialization. */
1133 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1134 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1135 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1136 "of %qD not allowed by ISO C++", type);
1137 else
1138 {
1139 error ("explicit specialization of non-template %qT", type);
1140 return error_mark_node;
1141 }
1142 }
1143
1144 return type;
1145 }
1146
1147 /* Returns nonzero if we can optimize the retrieval of specializations
1148 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1149 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1150
1151 static inline bool
optimize_specialization_lookup_p(tree tmpl)1152 optimize_specialization_lookup_p (tree tmpl)
1153 {
1154 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1155 && DECL_CLASS_SCOPE_P (tmpl)
1156 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1157 parameter. */
1158 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1159 /* The optimized lookup depends on the fact that the
1160 template arguments for the member function template apply
1161 purely to the containing class, which is not true if the
1162 containing class is an explicit or partial
1163 specialization. */
1164 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1165 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1166 && !DECL_CONV_FN_P (tmpl)
1167 /* It is possible to have a template that is not a member
1168 template and is not a member of a template class:
1169
1170 template <typename T>
1171 struct S { friend A::f(); };
1172
1173 Here, the friend function is a template, but the context does
1174 not have template information. The optimized lookup relies
1175 on having ARGS be the template arguments for both the class
1176 and the function template. */
1177 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1178 }
1179
1180 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1181 gone through coerce_template_parms by now. */
1182
1183 static void
verify_unstripped_args_1(tree inner)1184 verify_unstripped_args_1 (tree inner)
1185 {
1186 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1187 {
1188 tree arg = TREE_VEC_ELT (inner, i);
1189 if (TREE_CODE (arg) == TEMPLATE_DECL)
1190 /* OK */;
1191 else if (TYPE_P (arg))
1192 gcc_assert (strip_typedefs (arg, NULL) == arg);
1193 else if (ARGUMENT_PACK_P (arg))
1194 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1195 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1196 /* Allow typedefs on the type of a non-type argument, since a
1197 parameter can have them. */;
1198 else
1199 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1200 }
1201 }
1202
1203 static void
verify_unstripped_args(tree args)1204 verify_unstripped_args (tree args)
1205 {
1206 ++processing_template_decl;
1207 if (!any_dependent_template_arguments_p (args))
1208 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1209 --processing_template_decl;
1210 }
1211
1212 /* Retrieve the specialization (in the sense of [temp.spec] - a
1213 specialization is either an instantiation or an explicit
1214 specialization) of TMPL for the given template ARGS. If there is
1215 no such specialization, return NULL_TREE. The ARGS are a vector of
1216 arguments, or a vector of vectors of arguments, in the case of
1217 templates with more than one level of parameters.
1218
1219 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1220 then we search for a partial specialization matching ARGS. This
1221 parameter is ignored if TMPL is not a class template.
1222
1223 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1224 result is a NONTYPE_ARGUMENT_PACK. */
1225
1226 static tree
retrieve_specialization(tree tmpl,tree args,hashval_t hash)1227 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1228 {
1229 if (tmpl == NULL_TREE)
1230 return NULL_TREE;
1231
1232 if (args == error_mark_node)
1233 return NULL_TREE;
1234
1235 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1236 || TREE_CODE (tmpl) == FIELD_DECL);
1237
1238 /* There should be as many levels of arguments as there are
1239 levels of parameters. */
1240 gcc_assert (TMPL_ARGS_DEPTH (args)
1241 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1242 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1243 : template_class_depth (DECL_CONTEXT (tmpl))));
1244
1245 if (flag_checking)
1246 verify_unstripped_args (args);
1247
1248 /* Lambda functions in templates aren't instantiated normally, but through
1249 tsubst_lambda_expr. */
1250 if (lambda_fn_in_template_p (tmpl))
1251 return NULL_TREE;
1252
1253 if (optimize_specialization_lookup_p (tmpl))
1254 {
1255 /* The template arguments actually apply to the containing
1256 class. Find the class specialization with those
1257 arguments. */
1258 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1259 tree class_specialization
1260 = retrieve_specialization (class_template, args, 0);
1261 if (!class_specialization)
1262 return NULL_TREE;
1263
1264 /* Find the instance of TMPL. */
1265 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1266 for (ovl_iterator iter (fns); iter; ++iter)
1267 {
1268 tree fn = *iter;
1269 if (tree ti = get_template_info (fn))
1270 if (TI_TEMPLATE (ti) == tmpl
1271 /* using-declarations can bring in a different
1272 instantiation of tmpl as a member of a different
1273 instantiation of tmpl's class. We don't want those
1274 here. */
1275 && DECL_CONTEXT (fn) == class_specialization)
1276 return fn;
1277 }
1278 return NULL_TREE;
1279 }
1280 else
1281 {
1282 spec_entry *found;
1283 spec_entry elt;
1284 hash_table<spec_hasher> *specializations;
1285
1286 elt.tmpl = tmpl;
1287 elt.args = args;
1288 elt.spec = NULL_TREE;
1289
1290 if (DECL_CLASS_TEMPLATE_P (tmpl))
1291 specializations = type_specializations;
1292 else
1293 specializations = decl_specializations;
1294
1295 if (hash == 0)
1296 hash = spec_hasher::hash (&elt);
1297 found = specializations->find_with_hash (&elt, hash);
1298 if (found)
1299 return found->spec;
1300 }
1301
1302 return NULL_TREE;
1303 }
1304
1305 /* Like retrieve_specialization, but for local declarations. */
1306
1307 tree
retrieve_local_specialization(tree tmpl)1308 retrieve_local_specialization (tree tmpl)
1309 {
1310 if (local_specializations == NULL)
1311 return NULL_TREE;
1312
1313 tree *slot = local_specializations->get (tmpl);
1314 return slot ? *slot : NULL_TREE;
1315 }
1316
1317 /* Returns nonzero iff DECL is a specialization of TMPL. */
1318
1319 int
is_specialization_of(tree decl,tree tmpl)1320 is_specialization_of (tree decl, tree tmpl)
1321 {
1322 tree t;
1323
1324 if (TREE_CODE (decl) == FUNCTION_DECL)
1325 {
1326 for (t = decl;
1327 t != NULL_TREE;
1328 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1329 if (t == tmpl)
1330 return 1;
1331 }
1332 else
1333 {
1334 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1335
1336 for (t = TREE_TYPE (decl);
1337 t != NULL_TREE;
1338 t = CLASSTYPE_USE_TEMPLATE (t)
1339 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1340 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1341 return 1;
1342 }
1343
1344 return 0;
1345 }
1346
1347 /* Returns nonzero iff DECL is a specialization of friend declaration
1348 FRIEND_DECL according to [temp.friend]. */
1349
1350 bool
is_specialization_of_friend(tree decl,tree friend_decl)1351 is_specialization_of_friend (tree decl, tree friend_decl)
1352 {
1353 bool need_template = true;
1354 int template_depth;
1355
1356 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1357 || TREE_CODE (decl) == TYPE_DECL);
1358
1359 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1360 of a template class, we want to check if DECL is a specialization
1361 if this. */
1362 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1363 && DECL_TEMPLATE_INFO (friend_decl)
1364 && !DECL_USE_TEMPLATE (friend_decl))
1365 {
1366 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1367 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1368 need_template = false;
1369 }
1370 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1371 && !PRIMARY_TEMPLATE_P (friend_decl))
1372 need_template = false;
1373
1374 /* There is nothing to do if this is not a template friend. */
1375 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1376 return false;
1377
1378 if (is_specialization_of (decl, friend_decl))
1379 return true;
1380
1381 /* [temp.friend/6]
1382 A member of a class template may be declared to be a friend of a
1383 non-template class. In this case, the corresponding member of
1384 every specialization of the class template is a friend of the
1385 class granting friendship.
1386
1387 For example, given a template friend declaration
1388
1389 template <class T> friend void A<T>::f();
1390
1391 the member function below is considered a friend
1392
1393 template <> struct A<int> {
1394 void f();
1395 };
1396
1397 For this type of template friend, TEMPLATE_DEPTH below will be
1398 nonzero. To determine if DECL is a friend of FRIEND, we first
1399 check if the enclosing class is a specialization of another. */
1400
1401 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1402 if (template_depth
1403 && DECL_CLASS_SCOPE_P (decl)
1404 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1405 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1406 {
1407 /* Next, we check the members themselves. In order to handle
1408 a few tricky cases, such as when FRIEND_DECL's are
1409
1410 template <class T> friend void A<T>::g(T t);
1411 template <class T> template <T t> friend void A<T>::h();
1412
1413 and DECL's are
1414
1415 void A<int>::g(int);
1416 template <int> void A<int>::h();
1417
1418 we need to figure out ARGS, the template arguments from
1419 the context of DECL. This is required for template substitution
1420 of `T' in the function parameter of `g' and template parameter
1421 of `h' in the above examples. Here ARGS corresponds to `int'. */
1422
1423 tree context = DECL_CONTEXT (decl);
1424 tree args = NULL_TREE;
1425 int current_depth = 0;
1426
1427 while (current_depth < template_depth)
1428 {
1429 if (CLASSTYPE_TEMPLATE_INFO (context))
1430 {
1431 if (current_depth == 0)
1432 args = TYPE_TI_ARGS (context);
1433 else
1434 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1435 current_depth++;
1436 }
1437 context = TYPE_CONTEXT (context);
1438 }
1439
1440 if (TREE_CODE (decl) == FUNCTION_DECL)
1441 {
1442 bool is_template;
1443 tree friend_type;
1444 tree decl_type;
1445 tree friend_args_type;
1446 tree decl_args_type;
1447
1448 /* Make sure that both DECL and FRIEND_DECL are templates or
1449 non-templates. */
1450 is_template = DECL_TEMPLATE_INFO (decl)
1451 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1452 if (need_template ^ is_template)
1453 return false;
1454 else if (is_template)
1455 {
1456 /* If both are templates, check template parameter list. */
1457 tree friend_parms
1458 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1459 args, tf_none);
1460 if (!comp_template_parms
1461 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1462 friend_parms))
1463 return false;
1464
1465 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1466 }
1467 else
1468 decl_type = TREE_TYPE (decl);
1469
1470 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1471 tf_none, NULL_TREE);
1472 if (friend_type == error_mark_node)
1473 return false;
1474
1475 /* Check if return types match. */
1476 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1477 return false;
1478
1479 /* Check if function parameter types match, ignoring the
1480 `this' parameter. */
1481 friend_args_type = TYPE_ARG_TYPES (friend_type);
1482 decl_args_type = TYPE_ARG_TYPES (decl_type);
1483 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1484 friend_args_type = TREE_CHAIN (friend_args_type);
1485 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1486 decl_args_type = TREE_CHAIN (decl_args_type);
1487
1488 return compparms (decl_args_type, friend_args_type);
1489 }
1490 else
1491 {
1492 /* DECL is a TYPE_DECL */
1493 bool is_template;
1494 tree decl_type = TREE_TYPE (decl);
1495
1496 /* Make sure that both DECL and FRIEND_DECL are templates or
1497 non-templates. */
1498 is_template
1499 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1500 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1501
1502 if (need_template ^ is_template)
1503 return false;
1504 else if (is_template)
1505 {
1506 tree friend_parms;
1507 /* If both are templates, check the name of the two
1508 TEMPLATE_DECL's first because is_friend didn't. */
1509 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1510 != DECL_NAME (friend_decl))
1511 return false;
1512
1513 /* Now check template parameter list. */
1514 friend_parms
1515 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1516 args, tf_none);
1517 return comp_template_parms
1518 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1519 friend_parms);
1520 }
1521 else
1522 return (DECL_NAME (decl)
1523 == DECL_NAME (friend_decl));
1524 }
1525 }
1526 return false;
1527 }
1528
1529 /* Register the specialization SPEC as a specialization of TMPL with
1530 the indicated ARGS. IS_FRIEND indicates whether the specialization
1531 is actually just a friend declaration. ATTRLIST is the list of
1532 attributes that the specialization is declared with or NULL when
1533 it isn't. Returns SPEC, or an equivalent prior declaration, if
1534 available.
1535
1536 We also store instantiations of field packs in the hash table, even
1537 though they are not themselves templates, to make lookup easier. */
1538
1539 static tree
register_specialization(tree spec,tree tmpl,tree args,bool is_friend,hashval_t hash)1540 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1541 hashval_t hash)
1542 {
1543 tree fn;
1544 spec_entry **slot = NULL;
1545 spec_entry elt;
1546
1547 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1548 || (TREE_CODE (tmpl) == FIELD_DECL
1549 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1550
1551 if (TREE_CODE (spec) == FUNCTION_DECL
1552 && uses_template_parms (DECL_TI_ARGS (spec)))
1553 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1554 register it; we want the corresponding TEMPLATE_DECL instead.
1555 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1556 the more obvious `uses_template_parms (spec)' to avoid problems
1557 with default function arguments. In particular, given
1558 something like this:
1559
1560 template <class T> void f(T t1, T t = T())
1561
1562 the default argument expression is not substituted for in an
1563 instantiation unless and until it is actually needed. */
1564 return spec;
1565
1566 if (optimize_specialization_lookup_p (tmpl))
1567 /* We don't put these specializations in the hash table, but we might
1568 want to give an error about a mismatch. */
1569 fn = retrieve_specialization (tmpl, args, 0);
1570 else
1571 {
1572 elt.tmpl = tmpl;
1573 elt.args = args;
1574 elt.spec = spec;
1575
1576 if (hash == 0)
1577 hash = spec_hasher::hash (&elt);
1578
1579 slot =
1580 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1581 if (*slot)
1582 fn = ((spec_entry *) *slot)->spec;
1583 else
1584 fn = NULL_TREE;
1585 }
1586
1587 /* We can sometimes try to re-register a specialization that we've
1588 already got. In particular, regenerate_decl_from_template calls
1589 duplicate_decls which will update the specialization list. But,
1590 we'll still get called again here anyhow. It's more convenient
1591 to simply allow this than to try to prevent it. */
1592 if (fn == spec)
1593 return spec;
1594 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1595 {
1596 if (DECL_TEMPLATE_INSTANTIATION (fn))
1597 {
1598 if (DECL_ODR_USED (fn)
1599 || DECL_EXPLICIT_INSTANTIATION (fn))
1600 {
1601 error ("specialization of %qD after instantiation",
1602 fn);
1603 return error_mark_node;
1604 }
1605 else
1606 {
1607 tree clone;
1608 /* This situation should occur only if the first
1609 specialization is an implicit instantiation, the
1610 second is an explicit specialization, and the
1611 implicit instantiation has not yet been used. That
1612 situation can occur if we have implicitly
1613 instantiated a member function and then specialized
1614 it later.
1615
1616 We can also wind up here if a friend declaration that
1617 looked like an instantiation turns out to be a
1618 specialization:
1619
1620 template <class T> void foo(T);
1621 class S { friend void foo<>(int) };
1622 template <> void foo(int);
1623
1624 We transform the existing DECL in place so that any
1625 pointers to it become pointers to the updated
1626 declaration.
1627
1628 If there was a definition for the template, but not
1629 for the specialization, we want this to look as if
1630 there were no definition, and vice versa. */
1631 DECL_INITIAL (fn) = NULL_TREE;
1632 duplicate_decls (spec, fn, is_friend);
1633 /* The call to duplicate_decls will have applied
1634 [temp.expl.spec]:
1635
1636 An explicit specialization of a function template
1637 is inline only if it is explicitly declared to be,
1638 and independently of whether its function template
1639 is.
1640
1641 to the primary function; now copy the inline bits to
1642 the various clones. */
1643 FOR_EACH_CLONE (clone, fn)
1644 {
1645 DECL_DECLARED_INLINE_P (clone)
1646 = DECL_DECLARED_INLINE_P (fn);
1647 DECL_SOURCE_LOCATION (clone)
1648 = DECL_SOURCE_LOCATION (fn);
1649 DECL_DELETED_FN (clone)
1650 = DECL_DELETED_FN (fn);
1651 }
1652 check_specialization_namespace (tmpl);
1653
1654 return fn;
1655 }
1656 }
1657 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1658 {
1659 tree dd = duplicate_decls (spec, fn, is_friend);
1660 if (dd == error_mark_node)
1661 /* We've already complained in duplicate_decls. */
1662 return error_mark_node;
1663
1664 if (dd == NULL_TREE && DECL_INITIAL (spec))
1665 /* Dup decl failed, but this is a new definition. Set the
1666 line number so any errors match this new
1667 definition. */
1668 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1669
1670 return fn;
1671 }
1672 }
1673 else if (fn)
1674 return duplicate_decls (spec, fn, is_friend);
1675
1676 /* A specialization must be declared in the same namespace as the
1677 template it is specializing. */
1678 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1679 && !check_specialization_namespace (tmpl))
1680 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1681
1682 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1683 {
1684 spec_entry *entry = ggc_alloc<spec_entry> ();
1685 gcc_assert (tmpl && args && spec);
1686 *entry = elt;
1687 *slot = entry;
1688 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1689 && PRIMARY_TEMPLATE_P (tmpl)
1690 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1691 || variable_template_p (tmpl))
1692 /* If TMPL is a forward declaration of a template function, keep a list
1693 of all specializations in case we need to reassign them to a friend
1694 template later in tsubst_friend_function.
1695
1696 Also keep a list of all variable template instantiations so that
1697 process_partial_specialization can check whether a later partial
1698 specialization would have used it. */
1699 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1700 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1701 }
1702
1703 return spec;
1704 }
1705
1706 /* Returns true iff two spec_entry nodes are equivalent. */
1707
1708 int comparing_specializations;
1709
1710 bool
equal(spec_entry * e1,spec_entry * e2)1711 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1712 {
1713 int equal;
1714
1715 ++comparing_specializations;
1716 equal = (e1->tmpl == e2->tmpl
1717 && comp_template_args (e1->args, e2->args));
1718 if (equal && flag_concepts
1719 /* tmpl could be a FIELD_DECL for a capture pack. */
1720 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1721 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1722 && uses_template_parms (e1->args))
1723 {
1724 /* Partial specializations of a variable template can be distinguished by
1725 constraints. */
1726 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1727 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1728 equal = equivalent_constraints (c1, c2);
1729 }
1730 --comparing_specializations;
1731
1732 return equal;
1733 }
1734
1735 /* Returns a hash for a template TMPL and template arguments ARGS. */
1736
1737 static hashval_t
hash_tmpl_and_args(tree tmpl,tree args)1738 hash_tmpl_and_args (tree tmpl, tree args)
1739 {
1740 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1741 return iterative_hash_template_arg (args, val);
1742 }
1743
1744 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1745 ignoring SPEC. */
1746
1747 hashval_t
hash(spec_entry * e)1748 spec_hasher::hash (spec_entry *e)
1749 {
1750 return hash_tmpl_and_args (e->tmpl, e->args);
1751 }
1752
1753 /* Recursively calculate a hash value for a template argument ARG, for use
1754 in the hash tables of template specializations. We must be
1755 careful to (at least) skip the same entities template_args_equal
1756 does. */
1757
1758 hashval_t
iterative_hash_template_arg(tree arg,hashval_t val)1759 iterative_hash_template_arg (tree arg, hashval_t val)
1760 {
1761 if (arg == NULL_TREE)
1762 return iterative_hash_object (arg, val);
1763
1764 if (!TYPE_P (arg))
1765 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1766 while (CONVERT_EXPR_P (arg)
1767 || TREE_CODE (arg) == NON_LVALUE_EXPR
1768 || class_nttp_const_wrapper_p (arg))
1769 arg = TREE_OPERAND (arg, 0);
1770
1771 enum tree_code code = TREE_CODE (arg);
1772
1773 val = iterative_hash_object (code, val);
1774
1775 switch (code)
1776 {
1777 case ARGUMENT_PACK_SELECT:
1778 gcc_unreachable ();
1779
1780 case ERROR_MARK:
1781 return val;
1782
1783 case IDENTIFIER_NODE:
1784 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1785
1786 case TREE_VEC:
1787 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1788 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1789 return val;
1790
1791 case TYPE_PACK_EXPANSION:
1792 case EXPR_PACK_EXPANSION:
1793 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1794 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1795
1796 case TYPE_ARGUMENT_PACK:
1797 case NONTYPE_ARGUMENT_PACK:
1798 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1799
1800 case TREE_LIST:
1801 for (; arg; arg = TREE_CHAIN (arg))
1802 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1803 return val;
1804
1805 case OVERLOAD:
1806 for (lkp_iterator iter (arg); iter; ++iter)
1807 val = iterative_hash_template_arg (*iter, val);
1808 return val;
1809
1810 case CONSTRUCTOR:
1811 {
1812 tree field, value;
1813 unsigned i;
1814 iterative_hash_template_arg (TREE_TYPE (arg), val);
1815 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1816 {
1817 val = iterative_hash_template_arg (field, val);
1818 val = iterative_hash_template_arg (value, val);
1819 }
1820 return val;
1821 }
1822
1823 case PARM_DECL:
1824 if (!DECL_ARTIFICIAL (arg))
1825 {
1826 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1827 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1828 }
1829 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1830
1831 case TARGET_EXPR:
1832 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1833
1834 case PTRMEM_CST:
1835 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1836 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1837
1838 case TEMPLATE_PARM_INDEX:
1839 val = iterative_hash_template_arg
1840 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1841 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1842 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1843
1844 case TRAIT_EXPR:
1845 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1846 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1847 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1848
1849 case BASELINK:
1850 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1851 val);
1852 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1853 val);
1854
1855 case MODOP_EXPR:
1856 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1857 code = TREE_CODE (TREE_OPERAND (arg, 1));
1858 val = iterative_hash_object (code, val);
1859 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1860
1861 case LAMBDA_EXPR:
1862 /* [temp.over.link] Two lambda-expressions are never considered
1863 equivalent.
1864
1865 So just hash the closure type. */
1866 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1867
1868 case CAST_EXPR:
1869 case IMPLICIT_CONV_EXPR:
1870 case STATIC_CAST_EXPR:
1871 case REINTERPRET_CAST_EXPR:
1872 case CONST_CAST_EXPR:
1873 case DYNAMIC_CAST_EXPR:
1874 case NEW_EXPR:
1875 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1876 /* Now hash operands as usual. */
1877 break;
1878
1879 case CALL_EXPR:
1880 {
1881 tree fn = CALL_EXPR_FN (arg);
1882 if (tree name = dependent_name (fn))
1883 {
1884 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1885 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1886 fn = name;
1887 }
1888 val = iterative_hash_template_arg (fn, val);
1889 call_expr_arg_iterator ai;
1890 for (tree x = first_call_expr_arg (arg, &ai); x;
1891 x = next_call_expr_arg (&ai))
1892 val = iterative_hash_template_arg (x, val);
1893 return val;
1894 }
1895
1896 default:
1897 break;
1898 }
1899
1900 char tclass = TREE_CODE_CLASS (code);
1901 switch (tclass)
1902 {
1903 case tcc_type:
1904 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1905 {
1906 // We want an alias specialization that survived strip_typedefs
1907 // to hash differently from its TYPE_CANONICAL, to avoid hash
1908 // collisions that compare as different in template_args_equal.
1909 // These could be dependent specializations that strip_typedefs
1910 // left alone, or untouched specializations because
1911 // coerce_template_parms returns the unconverted template
1912 // arguments if it sees incomplete argument packs.
1913 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1914 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1915 }
1916
1917 switch (TREE_CODE (arg))
1918 {
1919 case TEMPLATE_TEMPLATE_PARM:
1920 {
1921 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1922
1923 /* Do not recurse with TPI directly, as that is unbounded
1924 recursion. */
1925 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1926 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1927 }
1928 break;
1929
1930 case DECLTYPE_TYPE:
1931 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1932 break;
1933
1934 default:
1935 if (tree canonical = TYPE_CANONICAL (arg))
1936 val = iterative_hash_object (TYPE_HASH (canonical), val);
1937 break;
1938 }
1939
1940 return val;
1941
1942 case tcc_declaration:
1943 case tcc_constant:
1944 return iterative_hash_expr (arg, val);
1945
1946 default:
1947 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1948 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1949 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1950 return val;
1951 }
1952
1953 gcc_unreachable ();
1954 return 0;
1955 }
1956
1957 /* Unregister the specialization SPEC as a specialization of TMPL.
1958 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1959 if the SPEC was listed as a specialization of TMPL.
1960
1961 Note that SPEC has been ggc_freed, so we can't look inside it. */
1962
1963 bool
reregister_specialization(tree spec,tree tinfo,tree new_spec)1964 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1965 {
1966 spec_entry *entry;
1967 spec_entry elt;
1968
1969 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1970 elt.args = TI_ARGS (tinfo);
1971 elt.spec = NULL_TREE;
1972
1973 entry = decl_specializations->find (&elt);
1974 if (entry != NULL)
1975 {
1976 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1977 gcc_assert (new_spec != NULL_TREE);
1978 entry->spec = new_spec;
1979 return 1;
1980 }
1981
1982 return 0;
1983 }
1984
1985 /* Like register_specialization, but for local declarations. We are
1986 registering SPEC, an instantiation of TMPL. */
1987
1988 void
register_local_specialization(tree spec,tree tmpl)1989 register_local_specialization (tree spec, tree tmpl)
1990 {
1991 gcc_assert (tmpl != spec);
1992 local_specializations->put (tmpl, spec);
1993 }
1994
1995 /* TYPE is a class type. Returns true if TYPE is an explicitly
1996 specialized class. */
1997
1998 bool
explicit_class_specialization_p(tree type)1999 explicit_class_specialization_p (tree type)
2000 {
2001 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2002 return false;
2003 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2004 }
2005
2006 /* Print the list of functions at FNS, going through all the overloads
2007 for each element of the list. Alternatively, FNS cannot be a
2008 TREE_LIST, in which case it will be printed together with all the
2009 overloads.
2010
2011 MORE and *STR should respectively be FALSE and NULL when the function
2012 is called from the outside. They are used internally on recursive
2013 calls. print_candidates manages the two parameters and leaves NULL
2014 in *STR when it ends. */
2015
2016 static void
2017 print_candidates_1 (tree fns, char **str, bool more = false)
2018 {
2019 if (TREE_CODE (fns) == TREE_LIST)
2020 for (; fns; fns = TREE_CHAIN (fns))
2021 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2022 else
2023 for (lkp_iterator iter (fns); iter;)
2024 {
2025 tree cand = *iter;
2026 ++iter;
2027
2028 const char *pfx = *str;
2029 if (!pfx)
2030 {
2031 if (more || iter)
2032 pfx = _("candidates are:");
2033 else
2034 pfx = _("candidate is:");
2035 *str = get_spaces (pfx);
2036 }
2037 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2038 }
2039 }
2040
2041 /* Print the list of candidate FNS in an error message. FNS can also
2042 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2043
2044 void
print_candidates(tree fns)2045 print_candidates (tree fns)
2046 {
2047 char *str = NULL;
2048 print_candidates_1 (fns, &str);
2049 free (str);
2050 }
2051
2052 /* Get a (possibly) constrained template declaration for the
2053 purpose of ordering candidates. */
2054 static tree
get_template_for_ordering(tree list)2055 get_template_for_ordering (tree list)
2056 {
2057 gcc_assert (TREE_CODE (list) == TREE_LIST);
2058 tree f = TREE_VALUE (list);
2059 if (tree ti = DECL_TEMPLATE_INFO (f))
2060 return TI_TEMPLATE (ti);
2061 return f;
2062 }
2063
2064 /* Among candidates having the same signature, return the
2065 most constrained or NULL_TREE if there is no best candidate.
2066 If the signatures of candidates vary (e.g., template
2067 specialization vs. member function), then there can be no
2068 most constrained.
2069
2070 Note that we don't compare constraints on the functions
2071 themselves, but rather those of their templates. */
2072 static tree
most_constrained_function(tree candidates)2073 most_constrained_function (tree candidates)
2074 {
2075 // Try to find the best candidate in a first pass.
2076 tree champ = candidates;
2077 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2078 {
2079 int winner = more_constrained (get_template_for_ordering (champ),
2080 get_template_for_ordering (c));
2081 if (winner == -1)
2082 champ = c; // The candidate is more constrained
2083 else if (winner == 0)
2084 return NULL_TREE; // Neither is more constrained
2085 }
2086
2087 // Verify that the champ is better than previous candidates.
2088 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2089 if (!more_constrained (get_template_for_ordering (champ),
2090 get_template_for_ordering (c)))
2091 return NULL_TREE;
2092 }
2093
2094 return champ;
2095 }
2096
2097
2098 /* Returns the template (one of the functions given by TEMPLATE_ID)
2099 which can be specialized to match the indicated DECL with the
2100 explicit template args given in TEMPLATE_ID. The DECL may be
2101 NULL_TREE if none is available. In that case, the functions in
2102 TEMPLATE_ID are non-members.
2103
2104 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2105 specialization of a member template.
2106
2107 The TEMPLATE_COUNT is the number of references to qualifying
2108 template classes that appeared in the name of the function. See
2109 check_explicit_specialization for a more accurate description.
2110
2111 TSK indicates what kind of template declaration (if any) is being
2112 declared. TSK_TEMPLATE indicates that the declaration given by
2113 DECL, though a FUNCTION_DECL, has template parameters, and is
2114 therefore a template function.
2115
2116 The template args (those explicitly specified and those deduced)
2117 are output in a newly created vector *TARGS_OUT.
2118
2119 If it is impossible to determine the result, an error message is
2120 issued. The error_mark_node is returned to indicate failure. */
2121
2122 static tree
determine_specialization(tree template_id,tree decl,tree * targs_out,int need_member_template,int template_count,tmpl_spec_kind tsk)2123 determine_specialization (tree template_id,
2124 tree decl,
2125 tree* targs_out,
2126 int need_member_template,
2127 int template_count,
2128 tmpl_spec_kind tsk)
2129 {
2130 tree fns;
2131 tree targs;
2132 tree explicit_targs;
2133 tree candidates = NULL_TREE;
2134
2135 /* A TREE_LIST of templates of which DECL may be a specialization.
2136 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2137 corresponding TREE_PURPOSE is the set of template arguments that,
2138 when used to instantiate the template, would produce a function
2139 with the signature of DECL. */
2140 tree templates = NULL_TREE;
2141 int header_count;
2142 cp_binding_level *b;
2143
2144 *targs_out = NULL_TREE;
2145
2146 if (template_id == error_mark_node || decl == error_mark_node)
2147 return error_mark_node;
2148
2149 /* We shouldn't be specializing a member template of an
2150 unspecialized class template; we already gave an error in
2151 check_specialization_scope, now avoid crashing. */
2152 if (!VAR_P (decl)
2153 && template_count && DECL_CLASS_SCOPE_P (decl)
2154 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2155 {
2156 gcc_assert (errorcount);
2157 return error_mark_node;
2158 }
2159
2160 fns = TREE_OPERAND (template_id, 0);
2161 explicit_targs = TREE_OPERAND (template_id, 1);
2162
2163 if (fns == error_mark_node)
2164 return error_mark_node;
2165
2166 /* Check for baselinks. */
2167 if (BASELINK_P (fns))
2168 fns = BASELINK_FUNCTIONS (fns);
2169
2170 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2171 {
2172 error_at (DECL_SOURCE_LOCATION (decl),
2173 "%qD is not a function template", fns);
2174 return error_mark_node;
2175 }
2176 else if (VAR_P (decl) && !variable_template_p (fns))
2177 {
2178 error ("%qD is not a variable template", fns);
2179 return error_mark_node;
2180 }
2181
2182 /* Count the number of template headers specified for this
2183 specialization. */
2184 header_count = 0;
2185 for (b = current_binding_level;
2186 b->kind == sk_template_parms;
2187 b = b->level_chain)
2188 ++header_count;
2189
2190 tree orig_fns = fns;
2191
2192 if (variable_template_p (fns))
2193 {
2194 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2195 targs = coerce_template_parms (parms, explicit_targs, fns,
2196 tf_warning_or_error,
2197 /*req_all*/true, /*use_defarg*/true);
2198 if (targs != error_mark_node)
2199 templates = tree_cons (targs, fns, templates);
2200 }
2201 else for (lkp_iterator iter (fns); iter; ++iter)
2202 {
2203 tree fn = *iter;
2204
2205 if (TREE_CODE (fn) == TEMPLATE_DECL)
2206 {
2207 tree decl_arg_types;
2208 tree fn_arg_types;
2209 tree insttype;
2210
2211 /* In case of explicit specialization, we need to check if
2212 the number of template headers appearing in the specialization
2213 is correct. This is usually done in check_explicit_specialization,
2214 but the check done there cannot be exhaustive when specializing
2215 member functions. Consider the following code:
2216
2217 template <> void A<int>::f(int);
2218 template <> template <> void A<int>::f(int);
2219
2220 Assuming that A<int> is not itself an explicit specialization
2221 already, the first line specializes "f" which is a non-template
2222 member function, whilst the second line specializes "f" which
2223 is a template member function. So both lines are syntactically
2224 correct, and check_explicit_specialization does not reject
2225 them.
2226
2227 Here, we can do better, as we are matching the specialization
2228 against the declarations. We count the number of template
2229 headers, and we check if they match TEMPLATE_COUNT + 1
2230 (TEMPLATE_COUNT is the number of qualifying template classes,
2231 plus there must be another header for the member template
2232 itself).
2233
2234 Notice that if header_count is zero, this is not a
2235 specialization but rather a template instantiation, so there
2236 is no check we can perform here. */
2237 if (header_count && header_count != template_count + 1)
2238 continue;
2239
2240 /* Check that the number of template arguments at the
2241 innermost level for DECL is the same as for FN. */
2242 if (current_binding_level->kind == sk_template_parms
2243 && !current_binding_level->explicit_spec_p
2244 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2245 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2246 (current_template_parms))))
2247 continue;
2248
2249 /* DECL might be a specialization of FN. */
2250 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2251 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2252
2253 /* For a non-static member function, we need to make sure
2254 that the const qualification is the same. Since
2255 get_bindings does not try to merge the "this" parameter,
2256 we must do the comparison explicitly. */
2257 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2258 {
2259 if (!same_type_p (TREE_VALUE (fn_arg_types),
2260 TREE_VALUE (decl_arg_types)))
2261 continue;
2262
2263 /* And the ref-qualification. */
2264 if (type_memfn_rqual (TREE_TYPE (decl))
2265 != type_memfn_rqual (TREE_TYPE (fn)))
2266 continue;
2267 }
2268
2269 /* Skip the "this" parameter and, for constructors of
2270 classes with virtual bases, the VTT parameter. A
2271 full specialization of a constructor will have a VTT
2272 parameter, but a template never will. */
2273 decl_arg_types
2274 = skip_artificial_parms_for (decl, decl_arg_types);
2275 fn_arg_types
2276 = skip_artificial_parms_for (fn, fn_arg_types);
2277
2278 /* Function templates cannot be specializations; there are
2279 no partial specializations of functions. Therefore, if
2280 the type of DECL does not match FN, there is no
2281 match.
2282
2283 Note that it should never be the case that we have both
2284 candidates added here, and for regular member functions
2285 below. */
2286 if (tsk == tsk_template)
2287 {
2288 if (compparms (fn_arg_types, decl_arg_types))
2289 candidates = tree_cons (NULL_TREE, fn, candidates);
2290 continue;
2291 }
2292
2293 /* See whether this function might be a specialization of this
2294 template. Suppress access control because we might be trying
2295 to make this specialization a friend, and we have already done
2296 access control for the declaration of the specialization. */
2297 push_deferring_access_checks (dk_no_check);
2298 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2299 pop_deferring_access_checks ();
2300
2301 if (!targs)
2302 /* We cannot deduce template arguments that when used to
2303 specialize TMPL will produce DECL. */
2304 continue;
2305
2306 if (uses_template_parms (targs))
2307 /* We deduced something involving 'auto', which isn't a valid
2308 template argument. */
2309 continue;
2310
2311 /* Remove, from the set of candidates, all those functions
2312 whose constraints are not satisfied. */
2313 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2314 continue;
2315
2316 // Then, try to form the new function type.
2317 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2318 if (insttype == error_mark_node)
2319 continue;
2320 fn_arg_types
2321 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2322 if (!compparms (fn_arg_types, decl_arg_types))
2323 continue;
2324
2325 /* Save this template, and the arguments deduced. */
2326 templates = tree_cons (targs, fn, templates);
2327 }
2328 else if (need_member_template)
2329 /* FN is an ordinary member function, and we need a
2330 specialization of a member template. */
2331 ;
2332 else if (TREE_CODE (fn) != FUNCTION_DECL)
2333 /* We can get IDENTIFIER_NODEs here in certain erroneous
2334 cases. */
2335 ;
2336 else if (!DECL_FUNCTION_MEMBER_P (fn))
2337 /* This is just an ordinary non-member function. Nothing can
2338 be a specialization of that. */
2339 ;
2340 else if (DECL_ARTIFICIAL (fn))
2341 /* Cannot specialize functions that are created implicitly. */
2342 ;
2343 else
2344 {
2345 tree decl_arg_types;
2346
2347 /* This is an ordinary member function. However, since
2348 we're here, we can assume its enclosing class is a
2349 template class. For example,
2350
2351 template <typename T> struct S { void f(); };
2352 template <> void S<int>::f() {}
2353
2354 Here, S<int>::f is a non-template, but S<int> is a
2355 template class. If FN has the same type as DECL, we
2356 might be in business. */
2357
2358 if (!DECL_TEMPLATE_INFO (fn))
2359 /* Its enclosing class is an explicit specialization
2360 of a template class. This is not a candidate. */
2361 continue;
2362
2363 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2364 TREE_TYPE (TREE_TYPE (fn))))
2365 /* The return types differ. */
2366 continue;
2367
2368 /* Adjust the type of DECL in case FN is a static member. */
2369 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2370 if (DECL_STATIC_FUNCTION_P (fn)
2371 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2372 decl_arg_types = TREE_CHAIN (decl_arg_types);
2373
2374 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2375 decl_arg_types))
2376 continue;
2377
2378 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2379 && (type_memfn_rqual (TREE_TYPE (decl))
2380 != type_memfn_rqual (TREE_TYPE (fn))))
2381 continue;
2382
2383 // If the deduced arguments do not satisfy the constraints,
2384 // this is not a candidate.
2385 if (flag_concepts && !constraints_satisfied_p (fn))
2386 continue;
2387
2388 // Add the candidate.
2389 candidates = tree_cons (NULL_TREE, fn, candidates);
2390 }
2391 }
2392
2393 if (templates && TREE_CHAIN (templates))
2394 {
2395 /* We have:
2396
2397 [temp.expl.spec]
2398
2399 It is possible for a specialization with a given function
2400 signature to be instantiated from more than one function
2401 template. In such cases, explicit specification of the
2402 template arguments must be used to uniquely identify the
2403 function template specialization being specialized.
2404
2405 Note that here, there's no suggestion that we're supposed to
2406 determine which of the candidate templates is most
2407 specialized. However, we, also have:
2408
2409 [temp.func.order]
2410
2411 Partial ordering of overloaded function template
2412 declarations is used in the following contexts to select
2413 the function template to which a function template
2414 specialization refers:
2415
2416 -- when an explicit specialization refers to a function
2417 template.
2418
2419 So, we do use the partial ordering rules, at least for now.
2420 This extension can only serve to make invalid programs valid,
2421 so it's safe. And, there is strong anecdotal evidence that
2422 the committee intended the partial ordering rules to apply;
2423 the EDG front end has that behavior, and John Spicer claims
2424 that the committee simply forgot to delete the wording in
2425 [temp.expl.spec]. */
2426 tree tmpl = most_specialized_instantiation (templates);
2427 if (tmpl != error_mark_node)
2428 {
2429 templates = tmpl;
2430 TREE_CHAIN (templates) = NULL_TREE;
2431 }
2432 }
2433
2434 // Concepts allows multiple declarations of member functions
2435 // with the same signature. Like above, we need to rely on
2436 // on the partial ordering of those candidates to determine which
2437 // is the best.
2438 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2439 {
2440 if (tree cand = most_constrained_function (candidates))
2441 {
2442 candidates = cand;
2443 TREE_CHAIN (cand) = NULL_TREE;
2444 }
2445 }
2446
2447 if (templates == NULL_TREE && candidates == NULL_TREE)
2448 {
2449 error ("template-id %qD for %q+D does not match any template "
2450 "declaration", template_id, decl);
2451 if (header_count && header_count != template_count + 1)
2452 inform (DECL_SOURCE_LOCATION (decl),
2453 "saw %d %<template<>%>, need %d for "
2454 "specializing a member function template",
2455 header_count, template_count + 1);
2456 else
2457 print_candidates (orig_fns);
2458 return error_mark_node;
2459 }
2460 else if ((templates && TREE_CHAIN (templates))
2461 || (candidates && TREE_CHAIN (candidates))
2462 || (templates && candidates))
2463 {
2464 error ("ambiguous template specialization %qD for %q+D",
2465 template_id, decl);
2466 candidates = chainon (candidates, templates);
2467 print_candidates (candidates);
2468 return error_mark_node;
2469 }
2470
2471 /* We have one, and exactly one, match. */
2472 if (candidates)
2473 {
2474 tree fn = TREE_VALUE (candidates);
2475 *targs_out = copy_node (DECL_TI_ARGS (fn));
2476
2477 /* Propagate the candidate's constraints to the declaration. */
2478 set_constraints (decl, get_constraints (fn));
2479
2480 /* DECL is a re-declaration or partial instantiation of a template
2481 function. */
2482 if (TREE_CODE (fn) == TEMPLATE_DECL)
2483 return fn;
2484 /* It was a specialization of an ordinary member function in a
2485 template class. */
2486 return DECL_TI_TEMPLATE (fn);
2487 }
2488
2489 /* It was a specialization of a template. */
2490 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2491 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2492 {
2493 *targs_out = copy_node (targs);
2494 SET_TMPL_ARGS_LEVEL (*targs_out,
2495 TMPL_ARGS_DEPTH (*targs_out),
2496 TREE_PURPOSE (templates));
2497 }
2498 else
2499 *targs_out = TREE_PURPOSE (templates);
2500 return TREE_VALUE (templates);
2501 }
2502
2503 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2504 but with the default argument values filled in from those in the
2505 TMPL_TYPES. */
2506
2507 static tree
copy_default_args_to_explicit_spec_1(tree spec_types,tree tmpl_types)2508 copy_default_args_to_explicit_spec_1 (tree spec_types,
2509 tree tmpl_types)
2510 {
2511 tree new_spec_types;
2512
2513 if (!spec_types)
2514 return NULL_TREE;
2515
2516 if (spec_types == void_list_node)
2517 return void_list_node;
2518
2519 /* Substitute into the rest of the list. */
2520 new_spec_types =
2521 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2522 TREE_CHAIN (tmpl_types));
2523
2524 /* Add the default argument for this parameter. */
2525 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2526 TREE_VALUE (spec_types),
2527 new_spec_types);
2528 }
2529
2530 /* DECL is an explicit specialization. Replicate default arguments
2531 from the template it specializes. (That way, code like:
2532
2533 template <class T> void f(T = 3);
2534 template <> void f(double);
2535 void g () { f (); }
2536
2537 works, as required.) An alternative approach would be to look up
2538 the correct default arguments at the call-site, but this approach
2539 is consistent with how implicit instantiations are handled. */
2540
2541 static void
copy_default_args_to_explicit_spec(tree decl)2542 copy_default_args_to_explicit_spec (tree decl)
2543 {
2544 tree tmpl;
2545 tree spec_types;
2546 tree tmpl_types;
2547 tree new_spec_types;
2548 tree old_type;
2549 tree new_type;
2550 tree t;
2551 tree object_type = NULL_TREE;
2552 tree in_charge = NULL_TREE;
2553 tree vtt = NULL_TREE;
2554
2555 /* See if there's anything we need to do. */
2556 tmpl = DECL_TI_TEMPLATE (decl);
2557 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2558 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2559 if (TREE_PURPOSE (t))
2560 break;
2561 if (!t)
2562 return;
2563
2564 old_type = TREE_TYPE (decl);
2565 spec_types = TYPE_ARG_TYPES (old_type);
2566
2567 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2568 {
2569 /* Remove the this pointer, but remember the object's type for
2570 CV quals. */
2571 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2572 spec_types = TREE_CHAIN (spec_types);
2573 tmpl_types = TREE_CHAIN (tmpl_types);
2574
2575 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2576 {
2577 /* DECL may contain more parameters than TMPL due to the extra
2578 in-charge parameter in constructors and destructors. */
2579 in_charge = spec_types;
2580 spec_types = TREE_CHAIN (spec_types);
2581 }
2582 if (DECL_HAS_VTT_PARM_P (decl))
2583 {
2584 vtt = spec_types;
2585 spec_types = TREE_CHAIN (spec_types);
2586 }
2587 }
2588
2589 /* Compute the merged default arguments. */
2590 new_spec_types =
2591 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2592
2593 /* Compute the new FUNCTION_TYPE. */
2594 if (object_type)
2595 {
2596 if (vtt)
2597 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2598 TREE_VALUE (vtt),
2599 new_spec_types);
2600
2601 if (in_charge)
2602 /* Put the in-charge parameter back. */
2603 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2604 TREE_VALUE (in_charge),
2605 new_spec_types);
2606
2607 new_type = build_method_type_directly (object_type,
2608 TREE_TYPE (old_type),
2609 new_spec_types);
2610 }
2611 else
2612 new_type = build_function_type (TREE_TYPE (old_type),
2613 new_spec_types);
2614 new_type = cp_build_type_attribute_variant (new_type,
2615 TYPE_ATTRIBUTES (old_type));
2616 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2617
2618 TREE_TYPE (decl) = new_type;
2619 }
2620
2621 /* Return the number of template headers we expect to see for a definition
2622 or specialization of CTYPE or one of its non-template members. */
2623
2624 int
num_template_headers_for_class(tree ctype)2625 num_template_headers_for_class (tree ctype)
2626 {
2627 int num_templates = 0;
2628
2629 while (ctype && CLASS_TYPE_P (ctype))
2630 {
2631 /* You're supposed to have one `template <...>' for every
2632 template class, but you don't need one for a full
2633 specialization. For example:
2634
2635 template <class T> struct S{};
2636 template <> struct S<int> { void f(); };
2637 void S<int>::f () {}
2638
2639 is correct; there shouldn't be a `template <>' for the
2640 definition of `S<int>::f'. */
2641 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2642 /* If CTYPE does not have template information of any
2643 kind, then it is not a template, nor is it nested
2644 within a template. */
2645 break;
2646 if (explicit_class_specialization_p (ctype))
2647 break;
2648 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2649 ++num_templates;
2650
2651 ctype = TYPE_CONTEXT (ctype);
2652 }
2653
2654 return num_templates;
2655 }
2656
2657 /* Do a simple sanity check on the template headers that precede the
2658 variable declaration DECL. */
2659
2660 void
check_template_variable(tree decl)2661 check_template_variable (tree decl)
2662 {
2663 tree ctx = CP_DECL_CONTEXT (decl);
2664 int wanted = num_template_headers_for_class (ctx);
2665 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2666 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2667 {
2668 if (cxx_dialect < cxx14)
2669 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2670 "variable templates only available with "
2671 "%<-std=c++14%> or %<-std=gnu++14%>");
2672
2673 // Namespace-scope variable templates should have a template header.
2674 ++wanted;
2675 }
2676 if (template_header_count > wanted)
2677 {
2678 auto_diagnostic_group d;
2679 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2680 "too many template headers for %qD "
2681 "(should be %d)",
2682 decl, wanted);
2683 if (warned && CLASS_TYPE_P (ctx)
2684 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2685 inform (DECL_SOURCE_LOCATION (decl),
2686 "members of an explicitly specialized class are defined "
2687 "without a template header");
2688 }
2689 }
2690
2691 /* An explicit specialization whose declarator-id or class-head-name is not
2692 qualified shall be declared in the nearest enclosing namespace of the
2693 template, or, if the namespace is inline (7.3.1), any namespace from its
2694 enclosing namespace set.
2695
2696 If the name declared in the explicit instantiation is an unqualified name,
2697 the explicit instantiation shall appear in the namespace where its template
2698 is declared or, if that namespace is inline (7.3.1), any namespace from its
2699 enclosing namespace set. */
2700
2701 void
check_unqualified_spec_or_inst(tree t,location_t loc)2702 check_unqualified_spec_or_inst (tree t, location_t loc)
2703 {
2704 tree tmpl = most_general_template (t);
2705 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2706 && !is_nested_namespace (current_namespace,
2707 CP_DECL_CONTEXT (tmpl), true))
2708 {
2709 if (processing_specialization)
2710 permerror (loc, "explicit specialization of %qD outside its "
2711 "namespace must use a nested-name-specifier", tmpl);
2712 else if (processing_explicit_instantiation
2713 && cxx_dialect >= cxx11)
2714 /* This was allowed in C++98, so only pedwarn. */
2715 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2716 "outside its namespace must use a nested-name-"
2717 "specifier", tmpl);
2718 }
2719 }
2720
2721 /* Warn for a template specialization SPEC that is missing some of a set
2722 of function or type attributes that the template TEMPL is declared with.
2723 ATTRLIST is a list of additional attributes that SPEC should be taken
2724 to ultimately be declared with. */
2725
2726 static void
warn_spec_missing_attributes(tree tmpl,tree spec,tree attrlist)2727 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2728 {
2729 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2730 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2731
2732 /* Avoid warning if the difference between the primary and
2733 the specialization is not in one of the attributes below. */
2734 const char* const blacklist[] = {
2735 "alloc_align", "alloc_size", "assume_aligned", "format",
2736 "format_arg", "malloc", "nonnull", NULL
2737 };
2738
2739 /* Put together a list of the black listed attributes that the primary
2740 template is declared with that the specialization is not, in case
2741 it's not apparent from the most recent declaration of the primary. */
2742 pretty_printer str;
2743 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2744 blacklist, &str);
2745
2746 if (!nattrs)
2747 return;
2748
2749 auto_diagnostic_group d;
2750 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2751 "explicit specialization %q#D may be missing attributes",
2752 spec))
2753 inform (DECL_SOURCE_LOCATION (tmpl),
2754 nattrs > 1
2755 ? G_("missing primary template attributes %s")
2756 : G_("missing primary template attribute %s"),
2757 pp_formatted_text (&str));
2758 }
2759
2760 /* Check to see if the function just declared, as indicated in
2761 DECLARATOR, and in DECL, is a specialization of a function
2762 template. We may also discover that the declaration is an explicit
2763 instantiation at this point.
2764
2765 Returns DECL, or an equivalent declaration that should be used
2766 instead if all goes well. Issues an error message if something is
2767 amiss. Returns error_mark_node if the error is not easily
2768 recoverable.
2769
2770 FLAGS is a bitmask consisting of the following flags:
2771
2772 2: The function has a definition.
2773 4: The function is a friend.
2774
2775 The TEMPLATE_COUNT is the number of references to qualifying
2776 template classes that appeared in the name of the function. For
2777 example, in
2778
2779 template <class T> struct S { void f(); };
2780 void S<int>::f();
2781
2782 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2783 classes are not counted in the TEMPLATE_COUNT, so that in
2784
2785 template <class T> struct S {};
2786 template <> struct S<int> { void f(); }
2787 template <> void S<int>::f();
2788
2789 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2790 invalid; there should be no template <>.)
2791
2792 If the function is a specialization, it is marked as such via
2793 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2794 is set up correctly, and it is added to the list of specializations
2795 for that template. */
2796
2797 tree
check_explicit_specialization(tree declarator,tree decl,int template_count,int flags,tree attrlist)2798 check_explicit_specialization (tree declarator,
2799 tree decl,
2800 int template_count,
2801 int flags,
2802 tree attrlist)
2803 {
2804 int have_def = flags & 2;
2805 int is_friend = flags & 4;
2806 bool is_concept = flags & 8;
2807 int specialization = 0;
2808 int explicit_instantiation = 0;
2809 int member_specialization = 0;
2810 tree ctype = DECL_CLASS_CONTEXT (decl);
2811 tree dname = DECL_NAME (decl);
2812 tmpl_spec_kind tsk;
2813
2814 if (is_friend)
2815 {
2816 if (!processing_specialization)
2817 tsk = tsk_none;
2818 else
2819 tsk = tsk_excessive_parms;
2820 }
2821 else
2822 tsk = current_tmpl_spec_kind (template_count);
2823
2824 switch (tsk)
2825 {
2826 case tsk_none:
2827 if (processing_specialization && !VAR_P (decl))
2828 {
2829 specialization = 1;
2830 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2831 }
2832 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2833 {
2834 if (is_friend)
2835 /* This could be something like:
2836
2837 template <class T> void f(T);
2838 class S { friend void f<>(int); } */
2839 specialization = 1;
2840 else
2841 {
2842 /* This case handles bogus declarations like template <>
2843 template <class T> void f<int>(); */
2844
2845 error_at (cp_expr_loc_or_input_loc (declarator),
2846 "template-id %qE in declaration of primary template",
2847 declarator);
2848 return decl;
2849 }
2850 }
2851 break;
2852
2853 case tsk_invalid_member_spec:
2854 /* The error has already been reported in
2855 check_specialization_scope. */
2856 return error_mark_node;
2857
2858 case tsk_invalid_expl_inst:
2859 error ("template parameter list used in explicit instantiation");
2860
2861 /* Fall through. */
2862
2863 case tsk_expl_inst:
2864 if (have_def)
2865 error ("definition provided for explicit instantiation");
2866
2867 explicit_instantiation = 1;
2868 break;
2869
2870 case tsk_excessive_parms:
2871 case tsk_insufficient_parms:
2872 if (tsk == tsk_excessive_parms)
2873 error ("too many template parameter lists in declaration of %qD",
2874 decl);
2875 else if (template_header_count)
2876 error("too few template parameter lists in declaration of %qD", decl);
2877 else
2878 error("explicit specialization of %qD must be introduced by "
2879 "%<template <>%>", decl);
2880
2881 /* Fall through. */
2882 case tsk_expl_spec:
2883 if (is_concept)
2884 error ("explicit specialization declared %<concept%>");
2885
2886 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2887 /* In cases like template<> constexpr bool v = true;
2888 We'll give an error in check_template_variable. */
2889 break;
2890
2891 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2892 if (ctype)
2893 member_specialization = 1;
2894 else
2895 specialization = 1;
2896 break;
2897
2898 case tsk_template:
2899 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2900 {
2901 /* This case handles bogus declarations like template <>
2902 template <class T> void f<int>(); */
2903
2904 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2905 error_at (cp_expr_loc_or_input_loc (declarator),
2906 "template-id %qE in declaration of primary template",
2907 declarator);
2908 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2909 {
2910 /* Partial specialization of variable template. */
2911 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2912 specialization = 1;
2913 goto ok;
2914 }
2915 else if (cxx_dialect < cxx14)
2916 error_at (cp_expr_loc_or_input_loc (declarator),
2917 "non-type partial specialization %qE "
2918 "is not allowed", declarator);
2919 else
2920 error_at (cp_expr_loc_or_input_loc (declarator),
2921 "non-class, non-variable partial specialization %qE "
2922 "is not allowed", declarator);
2923 return decl;
2924 ok:;
2925 }
2926
2927 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2928 /* This is a specialization of a member template, without
2929 specialization the containing class. Something like:
2930
2931 template <class T> struct S {
2932 template <class U> void f (U);
2933 };
2934 template <> template <class U> void S<int>::f(U) {}
2935
2936 That's a specialization -- but of the entire template. */
2937 specialization = 1;
2938 break;
2939
2940 default:
2941 gcc_unreachable ();
2942 }
2943
2944 if ((specialization || member_specialization)
2945 /* This doesn't apply to variable templates. */
2946 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2947 {
2948 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2949 for (; t; t = TREE_CHAIN (t))
2950 if (TREE_PURPOSE (t))
2951 {
2952 permerror (input_location,
2953 "default argument specified in explicit specialization");
2954 break;
2955 }
2956 }
2957
2958 if (specialization || member_specialization || explicit_instantiation)
2959 {
2960 tree tmpl = NULL_TREE;
2961 tree targs = NULL_TREE;
2962 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2963
2964 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2965 if (!was_template_id)
2966 {
2967 tree fns;
2968
2969 gcc_assert (identifier_p (declarator));
2970 if (ctype)
2971 fns = dname;
2972 else
2973 {
2974 /* If there is no class context, the explicit instantiation
2975 must be at namespace scope. */
2976 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2977
2978 /* Find the namespace binding, using the declaration
2979 context. */
2980 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2981 false, true);
2982 if (fns == error_mark_node)
2983 /* If lookup fails, look for a friend declaration so we can
2984 give a better diagnostic. */
2985 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2986 /*type*/false, /*complain*/true,
2987 /*hidden*/true);
2988
2989 if (fns == error_mark_node || !is_overloaded_fn (fns))
2990 {
2991 error ("%qD is not a template function", dname);
2992 fns = error_mark_node;
2993 }
2994 }
2995
2996 declarator = lookup_template_function (fns, NULL_TREE);
2997 }
2998
2999 if (declarator == error_mark_node)
3000 return error_mark_node;
3001
3002 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3003 {
3004 if (!explicit_instantiation)
3005 /* A specialization in class scope. This is invalid,
3006 but the error will already have been flagged by
3007 check_specialization_scope. */
3008 return error_mark_node;
3009 else
3010 {
3011 /* It's not valid to write an explicit instantiation in
3012 class scope, e.g.:
3013
3014 class C { template void f(); }
3015
3016 This case is caught by the parser. However, on
3017 something like:
3018
3019 template class C { void f(); };
3020
3021 (which is invalid) we can get here. The error will be
3022 issued later. */
3023 ;
3024 }
3025
3026 return decl;
3027 }
3028 else if (ctype != NULL_TREE
3029 && (identifier_p (TREE_OPERAND (declarator, 0))))
3030 {
3031 // We'll match variable templates in start_decl.
3032 if (VAR_P (decl))
3033 return decl;
3034
3035 /* Find the list of functions in ctype that have the same
3036 name as the declared function. */
3037 tree name = TREE_OPERAND (declarator, 0);
3038
3039 if (constructor_name_p (name, ctype))
3040 {
3041 if (DECL_CONSTRUCTOR_P (decl)
3042 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3043 : !CLASSTYPE_DESTRUCTOR (ctype))
3044 {
3045 /* From [temp.expl.spec]:
3046
3047 If such an explicit specialization for the member
3048 of a class template names an implicitly-declared
3049 special member function (clause _special_), the
3050 program is ill-formed.
3051
3052 Similar language is found in [temp.explicit]. */
3053 error ("specialization of implicitly-declared special member function");
3054 return error_mark_node;
3055 }
3056
3057 name = DECL_NAME (decl);
3058 }
3059
3060 /* For a type-conversion operator, We might be looking for
3061 `operator int' which will be a specialization of
3062 `operator T'. Grab all the conversion operators, and
3063 then select from them. */
3064 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3065 ? conv_op_identifier : name);
3066
3067 if (fns == NULL_TREE)
3068 {
3069 error ("no member function %qD declared in %qT", name, ctype);
3070 return error_mark_node;
3071 }
3072 else
3073 TREE_OPERAND (declarator, 0) = fns;
3074 }
3075
3076 /* Figure out what exactly is being specialized at this point.
3077 Note that for an explicit instantiation, even one for a
3078 member function, we cannot tell a priori whether the
3079 instantiation is for a member template, or just a member
3080 function of a template class. Even if a member template is
3081 being instantiated, the member template arguments may be
3082 elided if they can be deduced from the rest of the
3083 declaration. */
3084 tmpl = determine_specialization (declarator, decl,
3085 &targs,
3086 member_specialization,
3087 template_count,
3088 tsk);
3089
3090 if (!tmpl || tmpl == error_mark_node)
3091 /* We couldn't figure out what this declaration was
3092 specializing. */
3093 return error_mark_node;
3094 else
3095 {
3096 if (TREE_CODE (decl) == FUNCTION_DECL
3097 && DECL_HIDDEN_FRIEND_P (tmpl))
3098 {
3099 auto_diagnostic_group d;
3100 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3101 "friend declaration %qD is not visible to "
3102 "explicit specialization", tmpl))
3103 inform (DECL_SOURCE_LOCATION (tmpl),
3104 "friend declaration here");
3105 }
3106 else if (!ctype && !is_friend
3107 && CP_DECL_CONTEXT (decl) == current_namespace)
3108 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3109
3110 tree gen_tmpl = most_general_template (tmpl);
3111
3112 if (explicit_instantiation)
3113 {
3114 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3115 is done by do_decl_instantiation later. */
3116
3117 int arg_depth = TMPL_ARGS_DEPTH (targs);
3118 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3119
3120 if (arg_depth > parm_depth)
3121 {
3122 /* If TMPL is not the most general template (for
3123 example, if TMPL is a friend template that is
3124 injected into namespace scope), then there will
3125 be too many levels of TARGS. Remove some of them
3126 here. */
3127 int i;
3128 tree new_targs;
3129
3130 new_targs = make_tree_vec (parm_depth);
3131 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3132 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3133 = TREE_VEC_ELT (targs, i);
3134 targs = new_targs;
3135 }
3136
3137 return instantiate_template (tmpl, targs, tf_error);
3138 }
3139
3140 /* If we thought that the DECL was a member function, but it
3141 turns out to be specializing a static member function,
3142 make DECL a static member function as well. */
3143 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3144 && DECL_STATIC_FUNCTION_P (tmpl)
3145 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3146 revert_static_member_fn (decl);
3147
3148 /* If this is a specialization of a member template of a
3149 template class, we want to return the TEMPLATE_DECL, not
3150 the specialization of it. */
3151 if (tsk == tsk_template && !was_template_id)
3152 {
3153 tree result = DECL_TEMPLATE_RESULT (tmpl);
3154 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3155 DECL_INITIAL (result) = NULL_TREE;
3156 if (have_def)
3157 {
3158 tree parm;
3159 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3160 DECL_SOURCE_LOCATION (result)
3161 = DECL_SOURCE_LOCATION (decl);
3162 /* We want to use the argument list specified in the
3163 definition, not in the original declaration. */
3164 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3165 for (parm = DECL_ARGUMENTS (result); parm;
3166 parm = DECL_CHAIN (parm))
3167 DECL_CONTEXT (parm) = result;
3168 }
3169 return register_specialization (tmpl, gen_tmpl, targs,
3170 is_friend, 0);
3171 }
3172
3173 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3174 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3175
3176 if (was_template_id)
3177 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3178
3179 /* Inherit default function arguments from the template
3180 DECL is specializing. */
3181 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3182 copy_default_args_to_explicit_spec (decl);
3183
3184 /* This specialization has the same protection as the
3185 template it specializes. */
3186 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3187 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3188
3189 /* 7.1.1-1 [dcl.stc]
3190
3191 A storage-class-specifier shall not be specified in an
3192 explicit specialization...
3193
3194 The parser rejects these, so unless action is taken here,
3195 explicit function specializations will always appear with
3196 global linkage.
3197
3198 The action recommended by the C++ CWG in response to C++
3199 defect report 605 is to make the storage class and linkage
3200 of the explicit specialization match the templated function:
3201
3202 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3203 */
3204 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3205 {
3206 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3207 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3208
3209 /* A concept cannot be specialized. */
3210 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3211 {
3212 error ("explicit specialization of function concept %qD",
3213 gen_tmpl);
3214 return error_mark_node;
3215 }
3216
3217 /* This specialization has the same linkage and visibility as
3218 the function template it specializes. */
3219 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3220 if (! TREE_PUBLIC (decl))
3221 {
3222 DECL_INTERFACE_KNOWN (decl) = 1;
3223 DECL_NOT_REALLY_EXTERN (decl) = 1;
3224 }
3225 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3226 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3227 {
3228 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3229 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3230 }
3231 }
3232
3233 /* If DECL is a friend declaration, declared using an
3234 unqualified name, the namespace associated with DECL may
3235 have been set incorrectly. For example, in:
3236
3237 template <typename T> void f(T);
3238 namespace N {
3239 struct S { friend void f<int>(int); }
3240 }
3241
3242 we will have set the DECL_CONTEXT for the friend
3243 declaration to N, rather than to the global namespace. */
3244 if (DECL_NAMESPACE_SCOPE_P (decl))
3245 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3246
3247 if (is_friend && !have_def)
3248 /* This is not really a declaration of a specialization.
3249 It's just the name of an instantiation. But, it's not
3250 a request for an instantiation, either. */
3251 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3252 else if (TREE_CODE (decl) == FUNCTION_DECL)
3253 /* A specialization is not necessarily COMDAT. */
3254 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3255 && DECL_DECLARED_INLINE_P (decl));
3256 else if (VAR_P (decl))
3257 DECL_COMDAT (decl) = false;
3258
3259 /* If this is a full specialization, register it so that we can find
3260 it again. Partial specializations will be registered in
3261 process_partial_specialization. */
3262 if (!processing_template_decl)
3263 {
3264 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3265
3266 decl = register_specialization (decl, gen_tmpl, targs,
3267 is_friend, 0);
3268 }
3269
3270
3271 /* A 'structor should already have clones. */
3272 gcc_assert (decl == error_mark_node
3273 || variable_template_p (tmpl)
3274 || !(DECL_CONSTRUCTOR_P (decl)
3275 || DECL_DESTRUCTOR_P (decl))
3276 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3277 }
3278 }
3279
3280 return decl;
3281 }
3282
3283 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3284 parameters. These are represented in the same format used for
3285 DECL_TEMPLATE_PARMS. */
3286
3287 int
comp_template_parms(const_tree parms1,const_tree parms2)3288 comp_template_parms (const_tree parms1, const_tree parms2)
3289 {
3290 const_tree p1;
3291 const_tree p2;
3292
3293 if (parms1 == parms2)
3294 return 1;
3295
3296 for (p1 = parms1, p2 = parms2;
3297 p1 != NULL_TREE && p2 != NULL_TREE;
3298 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3299 {
3300 tree t1 = TREE_VALUE (p1);
3301 tree t2 = TREE_VALUE (p2);
3302 int i;
3303
3304 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3305 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3306
3307 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3308 return 0;
3309
3310 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3311 {
3312 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3313 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3314
3315 /* If either of the template parameters are invalid, assume
3316 they match for the sake of error recovery. */
3317 if (error_operand_p (parm1) || error_operand_p (parm2))
3318 return 1;
3319
3320 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3321 return 0;
3322
3323 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3324 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3325 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3326 continue;
3327 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3328 return 0;
3329 }
3330 }
3331
3332 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3333 /* One set of parameters has more parameters lists than the
3334 other. */
3335 return 0;
3336
3337 return 1;
3338 }
3339
3340 /* Returns true if two template parameters are declared with
3341 equivalent constraints. */
3342
3343 static bool
template_parameter_constraints_equivalent_p(const_tree parm1,const_tree parm2)3344 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3345 {
3346 tree req1 = TREE_TYPE (parm1);
3347 tree req2 = TREE_TYPE (parm2);
3348 if (!req1 != !req2)
3349 return false;
3350 if (req1)
3351 return cp_tree_equal (req1, req2);
3352 return true;
3353 }
3354
3355 /* Returns true when two template parameters are equivalent. */
3356
3357 static bool
template_parameters_equivalent_p(const_tree parm1,const_tree parm2)3358 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3359 {
3360 tree decl1 = TREE_VALUE (parm1);
3361 tree decl2 = TREE_VALUE (parm2);
3362
3363 /* If either of the template parameters are invalid, assume
3364 they match for the sake of error recovery. */
3365 if (error_operand_p (decl1) || error_operand_p (decl2))
3366 return true;
3367
3368 /* ... they declare parameters of the same kind. */
3369 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3370 return false;
3371
3372 /* ... one parameter was introduced by a parameter declaration, then
3373 both are. This case arises as a result of eagerly rewriting declarations
3374 during parsing. */
3375 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3376 return false;
3377
3378 /* ... if either declares a pack, they both do. */
3379 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3380 return false;
3381
3382 if (TREE_CODE (decl1) == PARM_DECL)
3383 {
3384 /* ... if they declare non-type parameters, the types are equivalent. */
3385 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3386 return false;
3387 }
3388 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3389 {
3390 /* ... if they declare template template parameters, their template
3391 parameter lists are equivalent. */
3392 if (!template_heads_equivalent_p (decl1, decl2))
3393 return false;
3394 }
3395
3396 /* ... if they are declared with a qualified-concept name, they both
3397 are, and those names are equivalent. */
3398 return template_parameter_constraints_equivalent_p (parm1, parm2);
3399 }
3400
3401 /* Returns true if two template parameters lists are equivalent.
3402 Two template parameter lists are equivalent if they have the
3403 same length and their corresponding parameters are equivalent.
3404
3405 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3406 data structure returned by DECL_TEMPLATE_PARMS.
3407
3408 This is generally the same implementation as comp_template_parms
3409 except that it also the concept names and arguments used to
3410 introduce parameters. */
3411
3412 static bool
template_parameter_lists_equivalent_p(const_tree parms1,const_tree parms2)3413 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3414 {
3415 if (parms1 == parms2)
3416 return true;
3417
3418 const_tree p1 = parms1;
3419 const_tree p2 = parms2;
3420 while (p1 != NULL_TREE && p2 != NULL_TREE)
3421 {
3422 tree list1 = TREE_VALUE (p1);
3423 tree list2 = TREE_VALUE (p2);
3424
3425 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3426 return 0;
3427
3428 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3429 {
3430 tree parm1 = TREE_VEC_ELT (list1, i);
3431 tree parm2 = TREE_VEC_ELT (list2, i);
3432 if (!template_parameters_equivalent_p (parm1, parm2))
3433 return false;
3434 }
3435
3436 p1 = TREE_CHAIN (p1);
3437 p2 = TREE_CHAIN (p2);
3438 }
3439
3440 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3441 return false;
3442
3443 return true;
3444 }
3445
3446 /* Return true if the requires-clause of the template parameter lists are
3447 equivalent and false otherwise. */
3448 static bool
template_requirements_equivalent_p(const_tree parms1,const_tree parms2)3449 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3450 {
3451 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3452 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3453 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3454 return false;
3455 if (!cp_tree_equal (req1, req2))
3456 return false;
3457 return true;
3458 }
3459
3460 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3461 Two template heads are equivalent if their template parameter
3462 lists are equivalent and their requires clauses are equivalent.
3463
3464 In pre-C++20, this is equivalent to calling comp_template_parms
3465 for the template parameters of TMPL1 and TMPL2. */
3466
3467 bool
template_heads_equivalent_p(const_tree tmpl1,const_tree tmpl2)3468 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3469 {
3470 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3471 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3472
3473 /* Don't change the matching rules for pre-C++20. */
3474 if (cxx_dialect < cxx2a)
3475 return comp_template_parms (parms1, parms2);
3476
3477 /* ... have the same number of template parameters, and their
3478 corresponding parameters are equivalent. */
3479 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3480 return false;
3481
3482 /* ... if either has a requires-clause, they both do and their
3483 corresponding constraint-expressions are equivalent. */
3484 return template_requirements_equivalent_p (parms1, parms2);
3485 }
3486
3487 /* Determine whether PARM is a parameter pack. */
3488
3489 bool
template_parameter_pack_p(const_tree parm)3490 template_parameter_pack_p (const_tree parm)
3491 {
3492 /* Determine if we have a non-type template parameter pack. */
3493 if (TREE_CODE (parm) == PARM_DECL)
3494 return (DECL_TEMPLATE_PARM_P (parm)
3495 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3496 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3497 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3498
3499 /* If this is a list of template parameters, we could get a
3500 TYPE_DECL or a TEMPLATE_DECL. */
3501 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3502 parm = TREE_TYPE (parm);
3503
3504 /* Otherwise it must be a type template parameter. */
3505 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3506 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3507 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3508 }
3509
3510 /* Determine if T is a function parameter pack. */
3511
3512 bool
function_parameter_pack_p(const_tree t)3513 function_parameter_pack_p (const_tree t)
3514 {
3515 if (t && TREE_CODE (t) == PARM_DECL)
3516 return DECL_PACK_P (t);
3517 return false;
3518 }
3519
3520 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3521 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3522
3523 tree
get_function_template_decl(const_tree primary_func_tmpl_inst)3524 get_function_template_decl (const_tree primary_func_tmpl_inst)
3525 {
3526 if (! primary_func_tmpl_inst
3527 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3528 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3529 return NULL;
3530
3531 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3532 }
3533
3534 /* Return true iff the function parameter PARAM_DECL was expanded
3535 from the function parameter pack PACK. */
3536
3537 bool
function_parameter_expanded_from_pack_p(tree param_decl,tree pack)3538 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3539 {
3540 if (DECL_ARTIFICIAL (param_decl)
3541 || !function_parameter_pack_p (pack))
3542 return false;
3543
3544 /* The parameter pack and its pack arguments have the same
3545 DECL_PARM_INDEX. */
3546 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3547 }
3548
3549 /* Determine whether ARGS describes a variadic template args list,
3550 i.e., one that is terminated by a template argument pack. */
3551
3552 static bool
template_args_variadic_p(tree args)3553 template_args_variadic_p (tree args)
3554 {
3555 int nargs;
3556 tree last_parm;
3557
3558 if (args == NULL_TREE)
3559 return false;
3560
3561 args = INNERMOST_TEMPLATE_ARGS (args);
3562 nargs = TREE_VEC_LENGTH (args);
3563
3564 if (nargs == 0)
3565 return false;
3566
3567 last_parm = TREE_VEC_ELT (args, nargs - 1);
3568
3569 return ARGUMENT_PACK_P (last_parm);
3570 }
3571
3572 /* Generate a new name for the parameter pack name NAME (an
3573 IDENTIFIER_NODE) that incorporates its */
3574
3575 static tree
make_ith_pack_parameter_name(tree name,int i)3576 make_ith_pack_parameter_name (tree name, int i)
3577 {
3578 /* Munge the name to include the parameter index. */
3579 #define NUMBUF_LEN 128
3580 char numbuf[NUMBUF_LEN];
3581 char* newname;
3582 int newname_len;
3583
3584 if (name == NULL_TREE)
3585 return name;
3586 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3587 newname_len = IDENTIFIER_LENGTH (name)
3588 + strlen (numbuf) + 2;
3589 newname = (char*)alloca (newname_len);
3590 snprintf (newname, newname_len,
3591 "%s#%i", IDENTIFIER_POINTER (name), i);
3592 return get_identifier (newname);
3593 }
3594
3595 /* Return true if T is a primary function, class or alias template
3596 specialization, not including the template pattern. */
3597
3598 bool
primary_template_specialization_p(const_tree t)3599 primary_template_specialization_p (const_tree t)
3600 {
3601 if (!t)
3602 return false;
3603
3604 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3605 return (DECL_LANG_SPECIFIC (t)
3606 && DECL_USE_TEMPLATE (t)
3607 && DECL_TEMPLATE_INFO (t)
3608 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3609 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3610 return (CLASSTYPE_TEMPLATE_INFO (t)
3611 && CLASSTYPE_USE_TEMPLATE (t)
3612 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3613 else if (alias_template_specialization_p (t, nt_transparent))
3614 return true;
3615 return false;
3616 }
3617
3618 /* Return true if PARM is a template template parameter. */
3619
3620 bool
template_template_parameter_p(const_tree parm)3621 template_template_parameter_p (const_tree parm)
3622 {
3623 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3624 }
3625
3626 /* Return true iff PARM is a DECL representing a type template
3627 parameter. */
3628
3629 bool
template_type_parameter_p(const_tree parm)3630 template_type_parameter_p (const_tree parm)
3631 {
3632 return (parm
3633 && (TREE_CODE (parm) == TYPE_DECL
3634 || TREE_CODE (parm) == TEMPLATE_DECL)
3635 && DECL_TEMPLATE_PARM_P (parm));
3636 }
3637
3638 /* Return the template parameters of T if T is a
3639 primary template instantiation, NULL otherwise. */
3640
3641 tree
get_primary_template_innermost_parameters(const_tree t)3642 get_primary_template_innermost_parameters (const_tree t)
3643 {
3644 tree parms = NULL, template_info = NULL;
3645
3646 if ((template_info = get_template_info (t))
3647 && primary_template_specialization_p (t))
3648 parms = INNERMOST_TEMPLATE_PARMS
3649 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3650
3651 return parms;
3652 }
3653
3654 /* Return the template parameters of the LEVELth level from the full list
3655 of template parameters PARMS. */
3656
3657 tree
get_template_parms_at_level(tree parms,int level)3658 get_template_parms_at_level (tree parms, int level)
3659 {
3660 tree p;
3661 if (!parms
3662 || TREE_CODE (parms) != TREE_LIST
3663 || level > TMPL_PARMS_DEPTH (parms))
3664 return NULL_TREE;
3665
3666 for (p = parms; p; p = TREE_CHAIN (p))
3667 if (TMPL_PARMS_DEPTH (p) == level)
3668 return p;
3669
3670 return NULL_TREE;
3671 }
3672
3673 /* Returns the template arguments of T if T is a template instantiation,
3674 NULL otherwise. */
3675
3676 tree
get_template_innermost_arguments(const_tree t)3677 get_template_innermost_arguments (const_tree t)
3678 {
3679 tree args = NULL, template_info = NULL;
3680
3681 if ((template_info = get_template_info (t))
3682 && TI_ARGS (template_info))
3683 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3684
3685 return args;
3686 }
3687
3688 /* Return the argument pack elements of T if T is a template argument pack,
3689 NULL otherwise. */
3690
3691 tree
get_template_argument_pack_elems(const_tree t)3692 get_template_argument_pack_elems (const_tree t)
3693 {
3694 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3695 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3696 return NULL;
3697
3698 return ARGUMENT_PACK_ARGS (t);
3699 }
3700
3701 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3702 ARGUMENT_PACK_SELECT represents. */
3703
3704 static tree
argument_pack_select_arg(tree t)3705 argument_pack_select_arg (tree t)
3706 {
3707 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3708 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3709
3710 /* If the selected argument is an expansion E, that most likely means we were
3711 called from gen_elem_of_pack_expansion_instantiation during the
3712 substituting of an argument pack (of which the Ith element is a pack
3713 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3714 In this case, the Ith element resulting from this substituting is going to
3715 be a pack expansion, which pattern is the pattern of E. Let's return the
3716 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3717 resulting pack expansion from it. */
3718 if (PACK_EXPANSION_P (arg))
3719 {
3720 /* Make sure we aren't throwing away arg info. */
3721 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3722 arg = PACK_EXPANSION_PATTERN (arg);
3723 }
3724
3725 return arg;
3726 }
3727
3728
3729 /* True iff FN is a function representing a built-in variadic parameter
3730 pack. */
3731
3732 bool
builtin_pack_fn_p(tree fn)3733 builtin_pack_fn_p (tree fn)
3734 {
3735 if (!fn
3736 || TREE_CODE (fn) != FUNCTION_DECL
3737 || !DECL_IS_BUILTIN (fn))
3738 return false;
3739
3740 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3741 return true;
3742
3743 return false;
3744 }
3745
3746 /* True iff CALL is a call to a function representing a built-in variadic
3747 parameter pack. */
3748
3749 static bool
builtin_pack_call_p(tree call)3750 builtin_pack_call_p (tree call)
3751 {
3752 if (TREE_CODE (call) != CALL_EXPR)
3753 return false;
3754 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3755 }
3756
3757 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3758
3759 static tree
expand_integer_pack(tree call,tree args,tsubst_flags_t complain,tree in_decl)3760 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3761 tree in_decl)
3762 {
3763 tree ohi = CALL_EXPR_ARG (call, 0);
3764 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3765 false/*fn*/, true/*int_cst*/);
3766
3767 if (value_dependent_expression_p (hi))
3768 {
3769 if (hi != ohi)
3770 {
3771 call = copy_node (call);
3772 CALL_EXPR_ARG (call, 0) = hi;
3773 }
3774 tree ex = make_pack_expansion (call, complain);
3775 tree vec = make_tree_vec (1);
3776 TREE_VEC_ELT (vec, 0) = ex;
3777 return vec;
3778 }
3779 else
3780 {
3781 hi = cxx_constant_value (hi);
3782 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3783
3784 /* Calculate the largest value of len that won't make the size of the vec
3785 overflow an int. The compiler will exceed resource limits long before
3786 this, but it seems a decent place to diagnose. */
3787 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3788
3789 if (len < 0 || len > max)
3790 {
3791 if ((complain & tf_error)
3792 && hi != error_mark_node)
3793 error ("argument to %<__integer_pack%> must be between 0 and %d",
3794 max);
3795 return error_mark_node;
3796 }
3797
3798 tree vec = make_tree_vec (len);
3799
3800 for (int i = 0; i < len; ++i)
3801 TREE_VEC_ELT (vec, i) = size_int (i);
3802
3803 return vec;
3804 }
3805 }
3806
3807 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3808 CALL. */
3809
3810 static tree
expand_builtin_pack_call(tree call,tree args,tsubst_flags_t complain,tree in_decl)3811 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3812 tree in_decl)
3813 {
3814 if (!builtin_pack_call_p (call))
3815 return NULL_TREE;
3816
3817 tree fn = CALL_EXPR_FN (call);
3818
3819 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3820 return expand_integer_pack (call, args, complain, in_decl);
3821
3822 return NULL_TREE;
3823 }
3824
3825 /* Structure used to track the progress of find_parameter_packs_r. */
3826 struct find_parameter_pack_data
3827 {
3828 /* TREE_LIST that will contain all of the parameter packs found by
3829 the traversal. */
3830 tree* parameter_packs;
3831
3832 /* Set of AST nodes that have been visited by the traversal. */
3833 hash_set<tree> *visited;
3834
3835 /* True iff we're making a type pack expansion. */
3836 bool type_pack_expansion_p;
3837 };
3838
3839 /* Identifies all of the argument packs that occur in a template
3840 argument and appends them to the TREE_LIST inside DATA, which is a
3841 find_parameter_pack_data structure. This is a subroutine of
3842 make_pack_expansion and uses_parameter_packs. */
3843 static tree
find_parameter_packs_r(tree * tp,int * walk_subtrees,void * data)3844 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3845 {
3846 tree t = *tp;
3847 struct find_parameter_pack_data* ppd =
3848 (struct find_parameter_pack_data*)data;
3849 bool parameter_pack_p = false;
3850
3851 /* Don't look through typedefs; we are interested in whether a
3852 parameter pack is actually written in the expression/type we're
3853 looking at, not the target type. */
3854 if (TYPE_P (t) && typedef_variant_p (t))
3855 {
3856 /* But do look at arguments for an alias template. */
3857 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3858 cp_walk_tree (&TI_ARGS (tinfo),
3859 &find_parameter_packs_r,
3860 ppd, ppd->visited);
3861 *walk_subtrees = 0;
3862 return NULL_TREE;
3863 }
3864
3865 /* Identify whether this is a parameter pack or not. */
3866 switch (TREE_CODE (t))
3867 {
3868 case TEMPLATE_PARM_INDEX:
3869 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3870 parameter_pack_p = true;
3871 break;
3872
3873 case TEMPLATE_TYPE_PARM:
3874 t = TYPE_MAIN_VARIANT (t);
3875 /* FALLTHRU */
3876 case TEMPLATE_TEMPLATE_PARM:
3877 /* If the placeholder appears in the decl-specifier-seq of a function
3878 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3879 is a pack expansion, the invented template parameter is a template
3880 parameter pack. */
3881 if (ppd->type_pack_expansion_p && is_auto (t))
3882 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3883 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3884 parameter_pack_p = true;
3885 break;
3886
3887 case FIELD_DECL:
3888 case PARM_DECL:
3889 if (DECL_PACK_P (t))
3890 {
3891 /* We don't want to walk into the type of a PARM_DECL,
3892 because we don't want to see the type parameter pack. */
3893 *walk_subtrees = 0;
3894 parameter_pack_p = true;
3895 }
3896 break;
3897
3898 case VAR_DECL:
3899 if (DECL_PACK_P (t))
3900 {
3901 /* We don't want to walk into the type of a variadic capture proxy,
3902 because we don't want to see the type parameter pack. */
3903 *walk_subtrees = 0;
3904 parameter_pack_p = true;
3905 }
3906 else if (variable_template_specialization_p (t))
3907 {
3908 cp_walk_tree (&DECL_TI_ARGS (t),
3909 find_parameter_packs_r,
3910 ppd, ppd->visited);
3911 *walk_subtrees = 0;
3912 }
3913 break;
3914
3915 case CALL_EXPR:
3916 if (builtin_pack_call_p (t))
3917 parameter_pack_p = true;
3918 break;
3919
3920 case BASES:
3921 parameter_pack_p = true;
3922 break;
3923 default:
3924 /* Not a parameter pack. */
3925 break;
3926 }
3927
3928 if (parameter_pack_p)
3929 {
3930 /* Add this parameter pack to the list. */
3931 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3932 }
3933
3934 if (TYPE_P (t))
3935 cp_walk_tree (&TYPE_CONTEXT (t),
3936 &find_parameter_packs_r, ppd, ppd->visited);
3937
3938 /* This switch statement will return immediately if we don't find a
3939 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3940 switch (TREE_CODE (t))
3941 {
3942 case BOUND_TEMPLATE_TEMPLATE_PARM:
3943 /* Check the template itself. */
3944 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3945 &find_parameter_packs_r, ppd, ppd->visited);
3946 return NULL_TREE;
3947
3948 case DECL_EXPR:
3949 {
3950 tree decl = DECL_EXPR_DECL (t);
3951 /* Ignore the declaration of a capture proxy for a parameter pack. */
3952 if (is_capture_proxy (decl))
3953 *walk_subtrees = 0;
3954 if (is_typedef_decl (decl))
3955 /* Since we stop at typedefs above, we need to look through them at
3956 the point of the DECL_EXPR. */
3957 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3958 &find_parameter_packs_r, ppd, ppd->visited);
3959 return NULL_TREE;
3960 }
3961
3962 case TEMPLATE_DECL:
3963 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3964 return NULL_TREE;
3965 cp_walk_tree (&TREE_TYPE (t),
3966 &find_parameter_packs_r, ppd, ppd->visited);
3967 return NULL_TREE;
3968
3969 case TYPENAME_TYPE:
3970 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3971 ppd, ppd->visited);
3972 *walk_subtrees = 0;
3973 return NULL_TREE;
3974
3975 case TYPE_PACK_EXPANSION:
3976 case EXPR_PACK_EXPANSION:
3977 *walk_subtrees = 0;
3978 return NULL_TREE;
3979
3980 case INTEGER_TYPE:
3981 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3982 ppd, ppd->visited);
3983 *walk_subtrees = 0;
3984 return NULL_TREE;
3985
3986 case IDENTIFIER_NODE:
3987 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3988 ppd->visited);
3989 *walk_subtrees = 0;
3990 return NULL_TREE;
3991
3992 case LAMBDA_EXPR:
3993 {
3994 /* Look at explicit captures. */
3995 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3996 cap; cap = TREE_CHAIN (cap))
3997 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3998 ppd->visited);
3999 /* Since we defer implicit capture, look in the parms and body. */
4000 tree fn = lambda_function (t);
4001 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4002 ppd->visited);
4003 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4004 ppd->visited);
4005 *walk_subtrees = 0;
4006 return NULL_TREE;
4007 }
4008
4009 case DECLTYPE_TYPE:
4010 {
4011 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4012 type_pack_expansion_p to false so that any placeholders
4013 within the expression don't get marked as parameter packs. */
4014 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4015 ppd->type_pack_expansion_p = false;
4016 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4017 ppd, ppd->visited);
4018 ppd->type_pack_expansion_p = type_pack_expansion_p;
4019 *walk_subtrees = 0;
4020 return NULL_TREE;
4021 }
4022
4023 case IF_STMT:
4024 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4025 ppd, ppd->visited);
4026 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4027 ppd, ppd->visited);
4028 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4029 ppd, ppd->visited);
4030 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4031 *walk_subtrees = 0;
4032 return NULL_TREE;
4033
4034 default:
4035 return NULL_TREE;
4036 }
4037
4038 return NULL_TREE;
4039 }
4040
4041 /* Determines if the expression or type T uses any parameter packs. */
4042 tree
uses_parameter_packs(tree t)4043 uses_parameter_packs (tree t)
4044 {
4045 tree parameter_packs = NULL_TREE;
4046 struct find_parameter_pack_data ppd;
4047 ppd.parameter_packs = ¶meter_packs;
4048 ppd.visited = new hash_set<tree>;
4049 ppd.type_pack_expansion_p = false;
4050 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4051 delete ppd.visited;
4052 return parameter_packs;
4053 }
4054
4055 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4056 representation a base-class initializer into a parameter pack
4057 expansion. If all goes well, the resulting node will be an
4058 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4059 respectively. */
4060 tree
make_pack_expansion(tree arg,tsubst_flags_t complain)4061 make_pack_expansion (tree arg, tsubst_flags_t complain)
4062 {
4063 tree result;
4064 tree parameter_packs = NULL_TREE;
4065 bool for_types = false;
4066 struct find_parameter_pack_data ppd;
4067
4068 if (!arg || arg == error_mark_node)
4069 return arg;
4070
4071 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4072 {
4073 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4074 class initializer. In this case, the TREE_PURPOSE will be a
4075 _TYPE node (representing the base class expansion we're
4076 initializing) and the TREE_VALUE will be a TREE_LIST
4077 containing the initialization arguments.
4078
4079 The resulting expansion looks somewhat different from most
4080 expansions. Rather than returning just one _EXPANSION, we
4081 return a TREE_LIST whose TREE_PURPOSE is a
4082 TYPE_PACK_EXPANSION containing the bases that will be
4083 initialized. The TREE_VALUE will be identical to the
4084 original TREE_VALUE, which is a list of arguments that will
4085 be passed to each base. We do not introduce any new pack
4086 expansion nodes into the TREE_VALUE (although it is possible
4087 that some already exist), because the TREE_PURPOSE and
4088 TREE_VALUE all need to be expanded together with the same
4089 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4090 resulting TREE_PURPOSE will mention the parameter packs in
4091 both the bases and the arguments to the bases. */
4092 tree purpose;
4093 tree value;
4094 tree parameter_packs = NULL_TREE;
4095
4096 /* Determine which parameter packs will be used by the base
4097 class expansion. */
4098 ppd.visited = new hash_set<tree>;
4099 ppd.parameter_packs = ¶meter_packs;
4100 ppd.type_pack_expansion_p = false;
4101 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4102 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4103 &ppd, ppd.visited);
4104
4105 if (parameter_packs == NULL_TREE)
4106 {
4107 if (complain & tf_error)
4108 error ("base initializer expansion %qT contains no parameter packs",
4109 arg);
4110 delete ppd.visited;
4111 return error_mark_node;
4112 }
4113
4114 if (TREE_VALUE (arg) != void_type_node)
4115 {
4116 /* Collect the sets of parameter packs used in each of the
4117 initialization arguments. */
4118 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4119 {
4120 /* Determine which parameter packs will be expanded in this
4121 argument. */
4122 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4123 &ppd, ppd.visited);
4124 }
4125 }
4126
4127 delete ppd.visited;
4128
4129 /* Create the pack expansion type for the base type. */
4130 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4131 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4132 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4133 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4134
4135 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4136 they will rarely be compared to anything. */
4137 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4138
4139 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4140 }
4141
4142 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4143 for_types = true;
4144
4145 /* Build the PACK_EXPANSION_* node. */
4146 result = for_types
4147 ? cxx_make_type (TYPE_PACK_EXPANSION)
4148 : make_node (EXPR_PACK_EXPANSION);
4149 SET_PACK_EXPANSION_PATTERN (result, arg);
4150 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4151 {
4152 /* Propagate type and const-expression information. */
4153 TREE_TYPE (result) = TREE_TYPE (arg);
4154 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4155 /* Mark this read now, since the expansion might be length 0. */
4156 mark_exp_read (arg);
4157 }
4158 else
4159 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4160 they will rarely be compared to anything. */
4161 SET_TYPE_STRUCTURAL_EQUALITY (result);
4162
4163 /* Determine which parameter packs will be expanded. */
4164 ppd.parameter_packs = ¶meter_packs;
4165 ppd.visited = new hash_set<tree>;
4166 ppd.type_pack_expansion_p = TYPE_P (arg);
4167 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4168 delete ppd.visited;
4169
4170 /* Make sure we found some parameter packs. */
4171 if (parameter_packs == NULL_TREE)
4172 {
4173 if (complain & tf_error)
4174 {
4175 if (TYPE_P (arg))
4176 error ("expansion pattern %qT contains no parameter packs", arg);
4177 else
4178 error ("expansion pattern %qE contains no parameter packs", arg);
4179 }
4180 return error_mark_node;
4181 }
4182 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4183
4184 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4185
4186 return result;
4187 }
4188
4189 /* Checks T for any "bare" parameter packs, which have not yet been
4190 expanded, and issues an error if any are found. This operation can
4191 only be done on full expressions or types (e.g., an expression
4192 statement, "if" condition, etc.), because we could have expressions like:
4193
4194 foo(f(g(h(args)))...)
4195
4196 where "args" is a parameter pack. check_for_bare_parameter_packs
4197 should not be called for the subexpressions args, h(args),
4198 g(h(args)), or f(g(h(args))), because we would produce erroneous
4199 error messages.
4200
4201 Returns TRUE and emits an error if there were bare parameter packs,
4202 returns FALSE otherwise. */
4203 bool
check_for_bare_parameter_packs(tree t,location_t loc)4204 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4205 {
4206 tree parameter_packs = NULL_TREE;
4207 struct find_parameter_pack_data ppd;
4208
4209 if (!processing_template_decl || !t || t == error_mark_node)
4210 return false;
4211
4212 /* A lambda might use a parameter pack from the containing context. */
4213 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4214 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4215 return false;
4216
4217 if (TREE_CODE (t) == TYPE_DECL)
4218 t = TREE_TYPE (t);
4219
4220 ppd.parameter_packs = ¶meter_packs;
4221 ppd.visited = new hash_set<tree>;
4222 ppd.type_pack_expansion_p = false;
4223 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4224 delete ppd.visited;
4225
4226 if (parameter_packs)
4227 {
4228 if (loc == UNKNOWN_LOCATION)
4229 loc = cp_expr_loc_or_input_loc (t);
4230 error_at (loc, "parameter packs not expanded with %<...%>:");
4231 while (parameter_packs)
4232 {
4233 tree pack = TREE_VALUE (parameter_packs);
4234 tree name = NULL_TREE;
4235
4236 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4237 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4238 name = TYPE_NAME (pack);
4239 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4240 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4241 else if (TREE_CODE (pack) == CALL_EXPR)
4242 name = DECL_NAME (CALL_EXPR_FN (pack));
4243 else
4244 name = DECL_NAME (pack);
4245
4246 if (name)
4247 inform (loc, " %qD", name);
4248 else
4249 inform (loc, " %s", "<anonymous>");
4250
4251 parameter_packs = TREE_CHAIN (parameter_packs);
4252 }
4253
4254 return true;
4255 }
4256
4257 return false;
4258 }
4259
4260 /* Expand any parameter packs that occur in the template arguments in
4261 ARGS. */
4262 tree
expand_template_argument_pack(tree args)4263 expand_template_argument_pack (tree args)
4264 {
4265 if (args == error_mark_node)
4266 return error_mark_node;
4267
4268 tree result_args = NULL_TREE;
4269 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4270 int num_result_args = -1;
4271 int non_default_args_count = -1;
4272
4273 /* First, determine if we need to expand anything, and the number of
4274 slots we'll need. */
4275 for (in_arg = 0; in_arg < nargs; ++in_arg)
4276 {
4277 tree arg = TREE_VEC_ELT (args, in_arg);
4278 if (arg == NULL_TREE)
4279 return args;
4280 if (ARGUMENT_PACK_P (arg))
4281 {
4282 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4283 if (num_result_args < 0)
4284 num_result_args = in_arg + num_packed;
4285 else
4286 num_result_args += num_packed;
4287 }
4288 else
4289 {
4290 if (num_result_args >= 0)
4291 num_result_args++;
4292 }
4293 }
4294
4295 /* If no expansion is necessary, we're done. */
4296 if (num_result_args < 0)
4297 return args;
4298
4299 /* Expand arguments. */
4300 result_args = make_tree_vec (num_result_args);
4301 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4302 non_default_args_count =
4303 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4304 for (in_arg = 0; in_arg < nargs; ++in_arg)
4305 {
4306 tree arg = TREE_VEC_ELT (args, in_arg);
4307 if (ARGUMENT_PACK_P (arg))
4308 {
4309 tree packed = ARGUMENT_PACK_ARGS (arg);
4310 int i, num_packed = TREE_VEC_LENGTH (packed);
4311 for (i = 0; i < num_packed; ++i, ++out_arg)
4312 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4313 if (non_default_args_count > 0)
4314 non_default_args_count += num_packed - 1;
4315 }
4316 else
4317 {
4318 TREE_VEC_ELT (result_args, out_arg) = arg;
4319 ++out_arg;
4320 }
4321 }
4322 if (non_default_args_count >= 0)
4323 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4324 return result_args;
4325 }
4326
4327 /* Checks if DECL shadows a template parameter.
4328
4329 [temp.local]: A template-parameter shall not be redeclared within its
4330 scope (including nested scopes).
4331
4332 Emits an error and returns TRUE if the DECL shadows a parameter,
4333 returns FALSE otherwise. */
4334
4335 bool
check_template_shadow(tree decl)4336 check_template_shadow (tree decl)
4337 {
4338 tree olddecl;
4339
4340 /* If we're not in a template, we can't possibly shadow a template
4341 parameter. */
4342 if (!current_template_parms)
4343 return true;
4344
4345 /* Figure out what we're shadowing. */
4346 decl = OVL_FIRST (decl);
4347 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4348
4349 /* If there's no previous binding for this name, we're not shadowing
4350 anything, let alone a template parameter. */
4351 if (!olddecl)
4352 return true;
4353
4354 /* If we're not shadowing a template parameter, we're done. Note
4355 that OLDDECL might be an OVERLOAD (or perhaps even an
4356 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4357 node. */
4358 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4359 return true;
4360
4361 /* We check for decl != olddecl to avoid bogus errors for using a
4362 name inside a class. We check TPFI to avoid duplicate errors for
4363 inline member templates. */
4364 if (decl == olddecl
4365 || (DECL_TEMPLATE_PARM_P (decl)
4366 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4367 return true;
4368
4369 /* Don't complain about the injected class name, as we've already
4370 complained about the class itself. */
4371 if (DECL_SELF_REFERENCE_P (decl))
4372 return false;
4373
4374 if (DECL_TEMPLATE_PARM_P (decl))
4375 error ("declaration of template parameter %q+D shadows "
4376 "template parameter", decl);
4377 else
4378 error ("declaration of %q+#D shadows template parameter", decl);
4379 inform (DECL_SOURCE_LOCATION (olddecl),
4380 "template parameter %qD declared here", olddecl);
4381 return false;
4382 }
4383
4384 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4385 ORIG_LEVEL, DECL, and TYPE. */
4386
4387 static tree
build_template_parm_index(int index,int level,int orig_level,tree decl,tree type)4388 build_template_parm_index (int index,
4389 int level,
4390 int orig_level,
4391 tree decl,
4392 tree type)
4393 {
4394 tree t = make_node (TEMPLATE_PARM_INDEX);
4395 TEMPLATE_PARM_IDX (t) = index;
4396 TEMPLATE_PARM_LEVEL (t) = level;
4397 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4398 TEMPLATE_PARM_DECL (t) = decl;
4399 TREE_TYPE (t) = type;
4400 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4401 TREE_READONLY (t) = TREE_READONLY (decl);
4402
4403 return t;
4404 }
4405
4406 /* Find the canonical type parameter for the given template type
4407 parameter. Returns the canonical type parameter, which may be TYPE
4408 if no such parameter existed. */
4409
4410 static tree
canonical_type_parameter(tree type)4411 canonical_type_parameter (tree type)
4412 {
4413 tree list;
4414 int idx = TEMPLATE_TYPE_IDX (type);
4415
4416 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4417
4418 if (!canonical_template_parms)
4419 vec_alloc (canonical_template_parms, idx + 1);
4420
4421 if (canonical_template_parms->length () <= (unsigned) idx)
4422 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4423
4424 list = (*canonical_template_parms)[idx];
4425 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4426 list = TREE_CHAIN (list);
4427
4428 if (list)
4429 return TREE_VALUE (list);
4430 else
4431 {
4432 (*canonical_template_parms)[idx]
4433 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4434 return type;
4435 }
4436 }
4437
4438 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4439 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4440 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4441 new one is created. */
4442
4443 static tree
reduce_template_parm_level(tree index,tree type,int levels,tree args,tsubst_flags_t complain)4444 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4445 tsubst_flags_t complain)
4446 {
4447 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4448 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4449 != TEMPLATE_PARM_LEVEL (index) - levels)
4450 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4451 {
4452 tree orig_decl = TEMPLATE_PARM_DECL (index);
4453
4454 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4455 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4456 type);
4457 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4458 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4459 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4460 DECL_ARTIFICIAL (decl) = 1;
4461 SET_DECL_TEMPLATE_PARM_P (decl);
4462
4463 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4464 TEMPLATE_PARM_LEVEL (index) - levels,
4465 TEMPLATE_PARM_ORIG_LEVEL (index),
4466 decl, type);
4467 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4468 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4469 = TEMPLATE_PARM_PARAMETER_PACK (index);
4470
4471 /* Template template parameters need this. */
4472 tree inner = decl;
4473 if (TREE_CODE (decl) == TEMPLATE_DECL)
4474 {
4475 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4476 TYPE_DECL, DECL_NAME (decl), type);
4477 DECL_TEMPLATE_RESULT (decl) = inner;
4478 DECL_ARTIFICIAL (inner) = true;
4479 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4480 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4481 }
4482
4483 /* Attach the TPI to the decl. */
4484 if (TREE_CODE (inner) == TYPE_DECL)
4485 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4486 else
4487 DECL_INITIAL (decl) = tpi;
4488 }
4489
4490 return TEMPLATE_PARM_DESCENDANTS (index);
4491 }
4492
4493 /* Process information from new template parameter PARM and append it
4494 to the LIST being built. This new parameter is a non-type
4495 parameter iff IS_NON_TYPE is true. This new parameter is a
4496 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4497 is in PARM_LOC. */
4498
4499 tree
process_template_parm(tree list,location_t parm_loc,tree parm,bool is_non_type,bool is_parameter_pack)4500 process_template_parm (tree list, location_t parm_loc, tree parm,
4501 bool is_non_type, bool is_parameter_pack)
4502 {
4503 tree decl = 0;
4504 int idx = 0;
4505
4506 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4507 tree defval = TREE_PURPOSE (parm);
4508 tree constr = TREE_TYPE (parm);
4509
4510 if (list)
4511 {
4512 tree p = tree_last (list);
4513
4514 if (p && TREE_VALUE (p) != error_mark_node)
4515 {
4516 p = TREE_VALUE (p);
4517 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4518 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4519 else
4520 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4521 }
4522
4523 ++idx;
4524 }
4525
4526 if (is_non_type)
4527 {
4528 parm = TREE_VALUE (parm);
4529
4530 SET_DECL_TEMPLATE_PARM_P (parm);
4531
4532 if (TREE_TYPE (parm) != error_mark_node)
4533 {
4534 /* [temp.param]
4535
4536 The top-level cv-qualifiers on the template-parameter are
4537 ignored when determining its type. */
4538 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4539 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4540 TREE_TYPE (parm) = error_mark_node;
4541 else if (uses_parameter_packs (TREE_TYPE (parm))
4542 && !is_parameter_pack
4543 /* If we're in a nested template parameter list, the template
4544 template parameter could be a parameter pack. */
4545 && processing_template_parmlist == 1)
4546 {
4547 /* This template parameter is not a parameter pack, but it
4548 should be. Complain about "bare" parameter packs. */
4549 check_for_bare_parameter_packs (TREE_TYPE (parm));
4550
4551 /* Recover by calling this a parameter pack. */
4552 is_parameter_pack = true;
4553 }
4554 }
4555
4556 /* A template parameter is not modifiable. */
4557 TREE_CONSTANT (parm) = 1;
4558 TREE_READONLY (parm) = 1;
4559 decl = build_decl (parm_loc,
4560 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4561 TREE_CONSTANT (decl) = 1;
4562 TREE_READONLY (decl) = 1;
4563 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4564 = build_template_parm_index (idx, processing_template_decl,
4565 processing_template_decl,
4566 decl, TREE_TYPE (parm));
4567
4568 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4569 = is_parameter_pack;
4570 }
4571 else
4572 {
4573 tree t;
4574 parm = TREE_VALUE (TREE_VALUE (parm));
4575
4576 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4577 {
4578 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4579 /* This is for distinguishing between real templates and template
4580 template parameters */
4581 TREE_TYPE (parm) = t;
4582
4583 /* any_template_parm_r expects to be able to get the targs of a
4584 DECL_TEMPLATE_RESULT. */
4585 tree result = DECL_TEMPLATE_RESULT (parm);
4586 TREE_TYPE (result) = t;
4587 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4588 tree tinfo = build_template_info (parm, args);
4589 retrofit_lang_decl (result);
4590 DECL_TEMPLATE_INFO (result) = tinfo;
4591
4592 decl = parm;
4593 }
4594 else
4595 {
4596 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4597 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4598 decl = build_decl (parm_loc,
4599 TYPE_DECL, parm, t);
4600 }
4601
4602 TYPE_NAME (t) = decl;
4603 TYPE_STUB_DECL (t) = decl;
4604 parm = decl;
4605 TEMPLATE_TYPE_PARM_INDEX (t)
4606 = build_template_parm_index (idx, processing_template_decl,
4607 processing_template_decl,
4608 decl, TREE_TYPE (parm));
4609 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4610 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4611 SET_TYPE_STRUCTURAL_EQUALITY (t);
4612 else
4613 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4614 }
4615 DECL_ARTIFICIAL (decl) = 1;
4616 SET_DECL_TEMPLATE_PARM_P (decl);
4617
4618 /* Build requirements for the type/template parameter.
4619 This must be done after SET_DECL_TEMPLATE_PARM_P or
4620 process_template_parm could fail. */
4621 tree reqs = finish_shorthand_constraint (parm, constr);
4622
4623 decl = pushdecl (decl);
4624 if (!is_non_type)
4625 parm = decl;
4626
4627 /* Build the parameter node linking the parameter declaration,
4628 its default argument (if any), and its constraints (if any). */
4629 parm = build_tree_list (defval, parm);
4630 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4631
4632 return chainon (list, parm);
4633 }
4634
4635 /* The end of a template parameter list has been reached. Process the
4636 tree list into a parameter vector, converting each parameter into a more
4637 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4638 as PARM_DECLs. */
4639
4640 tree
end_template_parm_list(tree parms)4641 end_template_parm_list (tree parms)
4642 {
4643 int nparms;
4644 tree parm, next;
4645 tree saved_parmlist = make_tree_vec (list_length (parms));
4646
4647 /* Pop the dummy parameter level and add the real one. */
4648 current_template_parms = TREE_CHAIN (current_template_parms);
4649
4650 current_template_parms
4651 = tree_cons (size_int (processing_template_decl),
4652 saved_parmlist, current_template_parms);
4653
4654 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4655 {
4656 next = TREE_CHAIN (parm);
4657 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4658 TREE_CHAIN (parm) = NULL_TREE;
4659 }
4660
4661 --processing_template_parmlist;
4662
4663 return saved_parmlist;
4664 }
4665
4666 // Explicitly indicate the end of the template parameter list. We assume
4667 // that the current template parameters have been constructed and/or
4668 // managed explicitly, as when creating new template template parameters
4669 // from a shorthand constraint.
4670 void
end_template_parm_list()4671 end_template_parm_list ()
4672 {
4673 --processing_template_parmlist;
4674 }
4675
4676 /* end_template_decl is called after a template declaration is seen. */
4677
4678 void
end_template_decl(void)4679 end_template_decl (void)
4680 {
4681 reset_specialization ();
4682
4683 if (! processing_template_decl)
4684 return;
4685
4686 /* This matches the pushlevel in begin_template_parm_list. */
4687 finish_scope ();
4688
4689 --processing_template_decl;
4690 current_template_parms = TREE_CHAIN (current_template_parms);
4691 }
4692
4693 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4694 thereof, and converts it into an argument suitable to be passed to
4695 the type substitution functions. Note that if the TREE_LIST contains
4696 an error_mark node, the returned argument is error_mark_node. */
4697
4698 tree
template_parm_to_arg(tree t)4699 template_parm_to_arg (tree t)
4700 {
4701 if (!t)
4702 return NULL_TREE;
4703
4704 if (TREE_CODE (t) == TREE_LIST)
4705 t = TREE_VALUE (t);
4706
4707 if (error_operand_p (t))
4708 return error_mark_node;
4709
4710 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4711 {
4712 if (TREE_CODE (t) == TYPE_DECL
4713 || TREE_CODE (t) == TEMPLATE_DECL)
4714 t = TREE_TYPE (t);
4715 else
4716 t = DECL_INITIAL (t);
4717 }
4718
4719 gcc_assert (TEMPLATE_PARM_P (t));
4720
4721 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4722 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4723 {
4724 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4725 {
4726 /* Turn this argument into a TYPE_ARGUMENT_PACK
4727 with a single element, which expands T. */
4728 tree vec = make_tree_vec (1);
4729 if (CHECKING_P)
4730 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4731
4732 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4733
4734 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4735 SET_ARGUMENT_PACK_ARGS (t, vec);
4736 }
4737 }
4738 else
4739 {
4740 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4741 {
4742 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4743 with a single element, which expands T. */
4744 tree vec = make_tree_vec (1);
4745 if (CHECKING_P)
4746 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4747
4748 t = convert_from_reference (t);
4749 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4750
4751 t = make_node (NONTYPE_ARGUMENT_PACK);
4752 SET_ARGUMENT_PACK_ARGS (t, vec);
4753 }
4754 else
4755 t = convert_from_reference (t);
4756 }
4757 return t;
4758 }
4759
4760 /* Given a single level of template parameters (a TREE_VEC), return it
4761 as a set of template arguments. */
4762
4763 tree
template_parms_level_to_args(tree parms)4764 template_parms_level_to_args (tree parms)
4765 {
4766 tree a = copy_node (parms);
4767 TREE_TYPE (a) = NULL_TREE;
4768 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4769 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4770
4771 if (CHECKING_P)
4772 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4773
4774 return a;
4775 }
4776
4777 /* Given a set of template parameters, return them as a set of template
4778 arguments. The template parameters are represented as a TREE_VEC, in
4779 the form documented in cp-tree.h for template arguments. */
4780
4781 tree
template_parms_to_args(tree parms)4782 template_parms_to_args (tree parms)
4783 {
4784 tree header;
4785 tree args = NULL_TREE;
4786 int length = TMPL_PARMS_DEPTH (parms);
4787 int l = length;
4788
4789 /* If there is only one level of template parameters, we do not
4790 create a TREE_VEC of TREE_VECs. Instead, we return a single
4791 TREE_VEC containing the arguments. */
4792 if (length > 1)
4793 args = make_tree_vec (length);
4794
4795 for (header = parms; header; header = TREE_CHAIN (header))
4796 {
4797 tree a = template_parms_level_to_args (TREE_VALUE (header));
4798
4799 if (length > 1)
4800 TREE_VEC_ELT (args, --l) = a;
4801 else
4802 args = a;
4803 }
4804
4805 return args;
4806 }
4807
4808 /* Within the declaration of a template, return the currently active
4809 template parameters as an argument TREE_VEC. */
4810
4811 static tree
current_template_args(void)4812 current_template_args (void)
4813 {
4814 return template_parms_to_args (current_template_parms);
4815 }
4816
4817 /* Return the fully generic arguments for of TMPL, i.e. what
4818 current_template_args would be while parsing it. */
4819
4820 tree
generic_targs_for(tree tmpl)4821 generic_targs_for (tree tmpl)
4822 {
4823 if (tmpl == NULL_TREE)
4824 return NULL_TREE;
4825 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4826 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4827 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4828 template parameter, it has no TEMPLATE_INFO; for a partial
4829 specialization, it has the arguments for the primary template, and we
4830 want the arguments for the partial specialization. */;
4831 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4832 if (tree ti = get_template_info (result))
4833 return TI_ARGS (ti);
4834 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4835 }
4836
4837 /* Update the declared TYPE by doing any lookups which were thought to be
4838 dependent, but are not now that we know the SCOPE of the declarator. */
4839
4840 tree
maybe_update_decl_type(tree orig_type,tree scope)4841 maybe_update_decl_type (tree orig_type, tree scope)
4842 {
4843 tree type = orig_type;
4844
4845 if (type == NULL_TREE)
4846 return type;
4847
4848 if (TREE_CODE (orig_type) == TYPE_DECL)
4849 type = TREE_TYPE (type);
4850
4851 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4852 && dependent_type_p (type)
4853 /* Don't bother building up the args in this case. */
4854 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4855 {
4856 /* tsubst in the args corresponding to the template parameters,
4857 including auto if present. Most things will be unchanged, but
4858 make_typename_type and tsubst_qualified_id will resolve
4859 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4860 tree args = current_template_args ();
4861 tree auto_node = type_uses_auto (type);
4862 tree pushed;
4863 if (auto_node)
4864 {
4865 tree auto_vec = make_tree_vec (1);
4866 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4867 args = add_to_template_args (args, auto_vec);
4868 }
4869 pushed = push_scope (scope);
4870 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4871 if (pushed)
4872 pop_scope (scope);
4873 }
4874
4875 if (type == error_mark_node)
4876 return orig_type;
4877
4878 if (TREE_CODE (orig_type) == TYPE_DECL)
4879 {
4880 if (same_type_p (type, TREE_TYPE (orig_type)))
4881 type = orig_type;
4882 else
4883 type = TYPE_NAME (type);
4884 }
4885 return type;
4886 }
4887
4888 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4889 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4890 the new template is a member template. */
4891
4892 static tree
build_template_decl(tree decl,tree parms,bool member_template_p)4893 build_template_decl (tree decl, tree parms, bool member_template_p)
4894 {
4895 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4896 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4897 DECL_TEMPLATE_PARMS (tmpl) = parms;
4898 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4899 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4900 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4901
4902 return tmpl;
4903 }
4904
4905 struct template_parm_data
4906 {
4907 /* The level of the template parameters we are currently
4908 processing. */
4909 int level;
4910
4911 /* The index of the specialization argument we are currently
4912 processing. */
4913 int current_arg;
4914
4915 /* An array whose size is the number of template parameters. The
4916 elements are nonzero if the parameter has been used in any one
4917 of the arguments processed so far. */
4918 int* parms;
4919
4920 /* An array whose size is the number of template arguments. The
4921 elements are nonzero if the argument makes use of template
4922 parameters of this level. */
4923 int* arg_uses_template_parms;
4924 };
4925
4926 /* Subroutine of push_template_decl used to see if each template
4927 parameter in a partial specialization is used in the explicit
4928 argument list. If T is of the LEVEL given in DATA (which is
4929 treated as a template_parm_data*), then DATA->PARMS is marked
4930 appropriately. */
4931
4932 static int
mark_template_parm(tree t,void * data)4933 mark_template_parm (tree t, void* data)
4934 {
4935 int level;
4936 int idx;
4937 struct template_parm_data* tpd = (struct template_parm_data*) data;
4938
4939 template_parm_level_and_index (t, &level, &idx);
4940
4941 if (level == tpd->level)
4942 {
4943 tpd->parms[idx] = 1;
4944 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4945 }
4946
4947 /* In C++17 the type of a non-type argument is a deduced context. */
4948 if (cxx_dialect >= cxx17
4949 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4950 for_each_template_parm (TREE_TYPE (t),
4951 &mark_template_parm,
4952 data,
4953 NULL,
4954 /*include_nondeduced_p=*/false);
4955
4956 /* Return zero so that for_each_template_parm will continue the
4957 traversal of the tree; we want to mark *every* template parm. */
4958 return 0;
4959 }
4960
4961 /* Process the partial specialization DECL. */
4962
4963 static tree
process_partial_specialization(tree decl)4964 process_partial_specialization (tree decl)
4965 {
4966 tree type = TREE_TYPE (decl);
4967 tree tinfo = get_template_info (decl);
4968 tree maintmpl = TI_TEMPLATE (tinfo);
4969 tree specargs = TI_ARGS (tinfo);
4970 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4971 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4972 tree inner_parms;
4973 tree inst;
4974 int nargs = TREE_VEC_LENGTH (inner_args);
4975 int ntparms;
4976 int i;
4977 bool did_error_intro = false;
4978 struct template_parm_data tpd;
4979 struct template_parm_data tpd2;
4980
4981 gcc_assert (current_template_parms);
4982
4983 /* A concept cannot be specialized. */
4984 if (flag_concepts && variable_concept_p (maintmpl))
4985 {
4986 error ("specialization of variable concept %q#D", maintmpl);
4987 return error_mark_node;
4988 }
4989
4990 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4991 ntparms = TREE_VEC_LENGTH (inner_parms);
4992
4993 /* We check that each of the template parameters given in the
4994 partial specialization is used in the argument list to the
4995 specialization. For example:
4996
4997 template <class T> struct S;
4998 template <class T> struct S<T*>;
4999
5000 The second declaration is OK because `T*' uses the template
5001 parameter T, whereas
5002
5003 template <class T> struct S<int>;
5004
5005 is no good. Even trickier is:
5006
5007 template <class T>
5008 struct S1
5009 {
5010 template <class U>
5011 struct S2;
5012 template <class U>
5013 struct S2<T>;
5014 };
5015
5016 The S2<T> declaration is actually invalid; it is a
5017 full-specialization. Of course,
5018
5019 template <class U>
5020 struct S2<T (*)(U)>;
5021
5022 or some such would have been OK. */
5023 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5024 tpd.parms = XALLOCAVEC (int, ntparms);
5025 memset (tpd.parms, 0, sizeof (int) * ntparms);
5026
5027 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5028 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5029 for (i = 0; i < nargs; ++i)
5030 {
5031 tpd.current_arg = i;
5032 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5033 &mark_template_parm,
5034 &tpd,
5035 NULL,
5036 /*include_nondeduced_p=*/false);
5037 }
5038 for (i = 0; i < ntparms; ++i)
5039 if (tpd.parms[i] == 0)
5040 {
5041 /* One of the template parms was not used in a deduced context in the
5042 specialization. */
5043 if (!did_error_intro)
5044 {
5045 error ("template parameters not deducible in "
5046 "partial specialization:");
5047 did_error_intro = true;
5048 }
5049
5050 inform (input_location, " %qD",
5051 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5052 }
5053
5054 if (did_error_intro)
5055 return error_mark_node;
5056
5057 /* [temp.class.spec]
5058
5059 The argument list of the specialization shall not be identical to
5060 the implicit argument list of the primary template. */
5061 tree main_args
5062 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5063 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5064 && (!flag_concepts
5065 || !strictly_subsumes (current_template_constraints (),
5066 main_args, maintmpl)))
5067 {
5068 if (!flag_concepts)
5069 error ("partial specialization %q+D does not specialize "
5070 "any template arguments; to define the primary template, "
5071 "remove the template argument list", decl);
5072 else
5073 error ("partial specialization %q+D does not specialize any "
5074 "template arguments and is not more constrained than "
5075 "the primary template; to define the primary template, "
5076 "remove the template argument list", decl);
5077 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5078 }
5079
5080 /* A partial specialization that replaces multiple parameters of the
5081 primary template with a pack expansion is less specialized for those
5082 parameters. */
5083 if (nargs < DECL_NTPARMS (maintmpl))
5084 {
5085 error ("partial specialization is not more specialized than the "
5086 "primary template because it replaces multiple parameters "
5087 "with a pack expansion");
5088 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5089 /* Avoid crash in process_partial_specialization. */
5090 return decl;
5091 }
5092
5093 else if (nargs > DECL_NTPARMS (maintmpl))
5094 {
5095 error ("too many arguments for partial specialization %qT", type);
5096 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5097 /* Avoid crash below. */
5098 return decl;
5099 }
5100
5101 /* If we aren't in a dependent class, we can actually try deduction. */
5102 else if (tpd.level == 1
5103 /* FIXME we should be able to handle a partial specialization of a
5104 partial instantiation, but currently we can't (c++/41727). */
5105 && TMPL_ARGS_DEPTH (specargs) == 1
5106 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5107 {
5108 auto_diagnostic_group d;
5109 if (permerror (input_location, "partial specialization %qD is not "
5110 "more specialized than", decl))
5111 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5112 maintmpl);
5113 }
5114
5115 /* [temp.class.spec]
5116
5117 A partially specialized non-type argument expression shall not
5118 involve template parameters of the partial specialization except
5119 when the argument expression is a simple identifier.
5120
5121 The type of a template parameter corresponding to a specialized
5122 non-type argument shall not be dependent on a parameter of the
5123 specialization.
5124
5125 Also, we verify that pack expansions only occur at the
5126 end of the argument list. */
5127 tpd2.parms = 0;
5128 for (i = 0; i < nargs; ++i)
5129 {
5130 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5131 tree arg = TREE_VEC_ELT (inner_args, i);
5132 tree packed_args = NULL_TREE;
5133 int j, len = 1;
5134
5135 if (ARGUMENT_PACK_P (arg))
5136 {
5137 /* Extract the arguments from the argument pack. We'll be
5138 iterating over these in the following loop. */
5139 packed_args = ARGUMENT_PACK_ARGS (arg);
5140 len = TREE_VEC_LENGTH (packed_args);
5141 }
5142
5143 for (j = 0; j < len; j++)
5144 {
5145 if (packed_args)
5146 /* Get the Jth argument in the parameter pack. */
5147 arg = TREE_VEC_ELT (packed_args, j);
5148
5149 if (PACK_EXPANSION_P (arg))
5150 {
5151 /* Pack expansions must come at the end of the
5152 argument list. */
5153 if ((packed_args && j < len - 1)
5154 || (!packed_args && i < nargs - 1))
5155 {
5156 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5157 error ("parameter pack argument %qE must be at the "
5158 "end of the template argument list", arg);
5159 else
5160 error ("parameter pack argument %qT must be at the "
5161 "end of the template argument list", arg);
5162 }
5163 }
5164
5165 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5166 /* We only care about the pattern. */
5167 arg = PACK_EXPANSION_PATTERN (arg);
5168
5169 if (/* These first two lines are the `non-type' bit. */
5170 !TYPE_P (arg)
5171 && TREE_CODE (arg) != TEMPLATE_DECL
5172 /* This next two lines are the `argument expression is not just a
5173 simple identifier' condition and also the `specialized
5174 non-type argument' bit. */
5175 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5176 && !((REFERENCE_REF_P (arg)
5177 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5178 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5179 {
5180 if ((!packed_args && tpd.arg_uses_template_parms[i])
5181 || (packed_args && uses_template_parms (arg)))
5182 error_at (cp_expr_loc_or_input_loc (arg),
5183 "template argument %qE involves template "
5184 "parameter(s)", arg);
5185 else
5186 {
5187 /* Look at the corresponding template parameter,
5188 marking which template parameters its type depends
5189 upon. */
5190 tree type = TREE_TYPE (parm);
5191
5192 if (!tpd2.parms)
5193 {
5194 /* We haven't yet initialized TPD2. Do so now. */
5195 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5196 /* The number of parameters here is the number in the
5197 main template, which, as checked in the assertion
5198 above, is NARGS. */
5199 tpd2.parms = XALLOCAVEC (int, nargs);
5200 tpd2.level =
5201 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5202 }
5203
5204 /* Mark the template parameters. But this time, we're
5205 looking for the template parameters of the main
5206 template, not in the specialization. */
5207 tpd2.current_arg = i;
5208 tpd2.arg_uses_template_parms[i] = 0;
5209 memset (tpd2.parms, 0, sizeof (int) * nargs);
5210 for_each_template_parm (type,
5211 &mark_template_parm,
5212 &tpd2,
5213 NULL,
5214 /*include_nondeduced_p=*/false);
5215
5216 if (tpd2.arg_uses_template_parms [i])
5217 {
5218 /* The type depended on some template parameters.
5219 If they are fully specialized in the
5220 specialization, that's OK. */
5221 int j;
5222 int count = 0;
5223 for (j = 0; j < nargs; ++j)
5224 if (tpd2.parms[j] != 0
5225 && tpd.arg_uses_template_parms [j])
5226 ++count;
5227 if (count != 0)
5228 error_n (input_location, count,
5229 "type %qT of template argument %qE depends "
5230 "on a template parameter",
5231 "type %qT of template argument %qE depends "
5232 "on template parameters",
5233 type,
5234 arg);
5235 }
5236 }
5237 }
5238 }
5239 }
5240
5241 /* We should only get here once. */
5242 if (TREE_CODE (decl) == TYPE_DECL)
5243 gcc_assert (!COMPLETE_TYPE_P (type));
5244
5245 // Build the template decl.
5246 tree tmpl = build_template_decl (decl, current_template_parms,
5247 DECL_MEMBER_TEMPLATE_P (maintmpl));
5248 TREE_TYPE (tmpl) = type;
5249 DECL_TEMPLATE_RESULT (tmpl) = decl;
5250 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5251 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5252 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5253
5254 /* Give template template parms a DECL_CONTEXT of the template
5255 for which they are a parameter. */
5256 for (i = 0; i < ntparms; ++i)
5257 {
5258 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5259 if (TREE_CODE (parm) == TEMPLATE_DECL)
5260 DECL_CONTEXT (parm) = tmpl;
5261 }
5262
5263 if (VAR_P (decl))
5264 /* We didn't register this in check_explicit_specialization so we could
5265 wait until the constraints were set. */
5266 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5267 else
5268 associate_classtype_constraints (type);
5269
5270 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5271 = tree_cons (specargs, tmpl,
5272 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5273 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5274
5275 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5276 inst = TREE_CHAIN (inst))
5277 {
5278 tree instance = TREE_VALUE (inst);
5279 if (TYPE_P (instance)
5280 ? (COMPLETE_TYPE_P (instance)
5281 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5282 : DECL_TEMPLATE_INSTANTIATION (instance))
5283 {
5284 tree spec = most_specialized_partial_spec (instance, tf_none);
5285 tree inst_decl = (DECL_P (instance)
5286 ? instance : TYPE_NAME (instance));
5287 if (!spec)
5288 /* OK */;
5289 else if (spec == error_mark_node)
5290 permerror (input_location,
5291 "declaration of %qD ambiguates earlier template "
5292 "instantiation for %qD", decl, inst_decl);
5293 else if (TREE_VALUE (spec) == tmpl)
5294 permerror (input_location,
5295 "partial specialization of %qD after instantiation "
5296 "of %qD", decl, inst_decl);
5297 }
5298 }
5299
5300 return decl;
5301 }
5302
5303 /* PARM is a template parameter of some form; return the corresponding
5304 TEMPLATE_PARM_INDEX. */
5305
5306 static tree
get_template_parm_index(tree parm)5307 get_template_parm_index (tree parm)
5308 {
5309 if (TREE_CODE (parm) == PARM_DECL
5310 || TREE_CODE (parm) == CONST_DECL)
5311 parm = DECL_INITIAL (parm);
5312 else if (TREE_CODE (parm) == TYPE_DECL
5313 || TREE_CODE (parm) == TEMPLATE_DECL)
5314 parm = TREE_TYPE (parm);
5315 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5316 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5317 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5318 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5319 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5320 return parm;
5321 }
5322
5323 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5324 parameter packs used by the template parameter PARM. */
5325
5326 static void
fixed_parameter_pack_p_1(tree parm,struct find_parameter_pack_data * ppd)5327 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5328 {
5329 /* A type parm can't refer to another parm. */
5330 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5331 return;
5332 else if (TREE_CODE (parm) == PARM_DECL)
5333 {
5334 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5335 ppd, ppd->visited);
5336 return;
5337 }
5338
5339 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5340
5341 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5342 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5343 {
5344 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5345 if (template_parameter_pack_p (p))
5346 /* Any packs in the type are expanded by this parameter. */;
5347 else
5348 fixed_parameter_pack_p_1 (p, ppd);
5349 }
5350 }
5351
5352 /* PARM is a template parameter pack. Return any parameter packs used in
5353 its type or the type of any of its template parameters. If there are
5354 any such packs, it will be instantiated into a fixed template parameter
5355 list by partial instantiation rather than be fully deduced. */
5356
5357 tree
fixed_parameter_pack_p(tree parm)5358 fixed_parameter_pack_p (tree parm)
5359 {
5360 /* This can only be true in a member template. */
5361 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5362 return NULL_TREE;
5363 /* This can only be true for a parameter pack. */
5364 if (!template_parameter_pack_p (parm))
5365 return NULL_TREE;
5366 /* A type parm can't refer to another parm. */
5367 if (TREE_CODE (parm) == TYPE_DECL)
5368 return NULL_TREE;
5369
5370 tree parameter_packs = NULL_TREE;
5371 struct find_parameter_pack_data ppd;
5372 ppd.parameter_packs = ¶meter_packs;
5373 ppd.visited = new hash_set<tree>;
5374 ppd.type_pack_expansion_p = false;
5375
5376 fixed_parameter_pack_p_1 (parm, &ppd);
5377
5378 delete ppd.visited;
5379 return parameter_packs;
5380 }
5381
5382 /* Check that a template declaration's use of default arguments and
5383 parameter packs is not invalid. Here, PARMS are the template
5384 parameters. IS_PRIMARY is true if DECL is the thing declared by
5385 a primary template. IS_PARTIAL is true if DECL is a partial
5386 specialization.
5387
5388 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5389 function template declaration or a friend class template
5390 declaration. In the function case, 1 indicates a declaration, 2
5391 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5392 emitted for extraneous default arguments.
5393
5394 Returns TRUE if there were no errors found, FALSE otherwise. */
5395
5396 bool
check_default_tmpl_args(tree decl,tree parms,bool is_primary,bool is_partial,int is_friend_decl)5397 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5398 bool is_partial, int is_friend_decl)
5399 {
5400 const char *msg;
5401 int last_level_to_check;
5402 tree parm_level;
5403 bool no_errors = true;
5404
5405 /* [temp.param]
5406
5407 A default template-argument shall not be specified in a
5408 function template declaration or a function template definition, nor
5409 in the template-parameter-list of the definition of a member of a
5410 class template. */
5411
5412 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5413 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5414 /* You can't have a function template declaration in a local
5415 scope, nor you can you define a member of a class template in a
5416 local scope. */
5417 return true;
5418
5419 if ((TREE_CODE (decl) == TYPE_DECL
5420 && TREE_TYPE (decl)
5421 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5422 || (TREE_CODE (decl) == FUNCTION_DECL
5423 && LAMBDA_FUNCTION_P (decl)))
5424 /* A lambda doesn't have an explicit declaration; don't complain
5425 about the parms of the enclosing class. */
5426 return true;
5427
5428 if (current_class_type
5429 && !TYPE_BEING_DEFINED (current_class_type)
5430 && DECL_LANG_SPECIFIC (decl)
5431 && DECL_DECLARES_FUNCTION_P (decl)
5432 /* If this is either a friend defined in the scope of the class
5433 or a member function. */
5434 && (DECL_FUNCTION_MEMBER_P (decl)
5435 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5436 : DECL_FRIEND_CONTEXT (decl)
5437 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5438 : false)
5439 /* And, if it was a member function, it really was defined in
5440 the scope of the class. */
5441 && (!DECL_FUNCTION_MEMBER_P (decl)
5442 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5443 /* We already checked these parameters when the template was
5444 declared, so there's no need to do it again now. This function
5445 was defined in class scope, but we're processing its body now
5446 that the class is complete. */
5447 return true;
5448
5449 /* Core issue 226 (C++0x only): the following only applies to class
5450 templates. */
5451 if (is_primary
5452 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5453 {
5454 /* [temp.param]
5455
5456 If a template-parameter has a default template-argument, all
5457 subsequent template-parameters shall have a default
5458 template-argument supplied. */
5459 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5460 {
5461 tree inner_parms = TREE_VALUE (parm_level);
5462 int ntparms = TREE_VEC_LENGTH (inner_parms);
5463 int seen_def_arg_p = 0;
5464 int i;
5465
5466 for (i = 0; i < ntparms; ++i)
5467 {
5468 tree parm = TREE_VEC_ELT (inner_parms, i);
5469
5470 if (parm == error_mark_node)
5471 continue;
5472
5473 if (TREE_PURPOSE (parm))
5474 seen_def_arg_p = 1;
5475 else if (seen_def_arg_p
5476 && !template_parameter_pack_p (TREE_VALUE (parm)))
5477 {
5478 error ("no default argument for %qD", TREE_VALUE (parm));
5479 /* For better subsequent error-recovery, we indicate that
5480 there should have been a default argument. */
5481 TREE_PURPOSE (parm) = error_mark_node;
5482 no_errors = false;
5483 }
5484 else if (!is_partial
5485 && !is_friend_decl
5486 /* Don't complain about an enclosing partial
5487 specialization. */
5488 && parm_level == parms
5489 && TREE_CODE (decl) == TYPE_DECL
5490 && i < ntparms - 1
5491 && template_parameter_pack_p (TREE_VALUE (parm))
5492 /* A fixed parameter pack will be partially
5493 instantiated into a fixed length list. */
5494 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5495 {
5496 /* A primary class template can only have one
5497 parameter pack, at the end of the template
5498 parameter list. */
5499
5500 error ("parameter pack %q+D must be at the end of the"
5501 " template parameter list", TREE_VALUE (parm));
5502
5503 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5504 = error_mark_node;
5505 no_errors = false;
5506 }
5507 }
5508 }
5509 }
5510
5511 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5512 || is_partial
5513 || !is_primary
5514 || is_friend_decl)
5515 /* For an ordinary class template, default template arguments are
5516 allowed at the innermost level, e.g.:
5517 template <class T = int>
5518 struct S {};
5519 but, in a partial specialization, they're not allowed even
5520 there, as we have in [temp.class.spec]:
5521
5522 The template parameter list of a specialization shall not
5523 contain default template argument values.
5524
5525 So, for a partial specialization, or for a function template
5526 (in C++98/C++03), we look at all of them. */
5527 ;
5528 else
5529 /* But, for a primary class template that is not a partial
5530 specialization we look at all template parameters except the
5531 innermost ones. */
5532 parms = TREE_CHAIN (parms);
5533
5534 /* Figure out what error message to issue. */
5535 if (is_friend_decl == 2)
5536 msg = G_("default template arguments may not be used in function template "
5537 "friend re-declaration");
5538 else if (is_friend_decl)
5539 msg = G_("default template arguments may not be used in template "
5540 "friend declarations");
5541 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5542 msg = G_("default template arguments may not be used in function templates "
5543 "without %<-std=c++11%> or %<-std=gnu++11%>");
5544 else if (is_partial)
5545 msg = G_("default template arguments may not be used in "
5546 "partial specializations");
5547 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5548 msg = G_("default argument for template parameter for class enclosing %qD");
5549 else
5550 /* Per [temp.param]/9, "A default template-argument shall not be
5551 specified in the template-parameter-lists of the definition of
5552 a member of a class template that appears outside of the member's
5553 class.", thus if we aren't handling a member of a class template
5554 there is no need to examine the parameters. */
5555 return true;
5556
5557 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5558 /* If we're inside a class definition, there's no need to
5559 examine the parameters to the class itself. On the one
5560 hand, they will be checked when the class is defined, and,
5561 on the other, default arguments are valid in things like:
5562 template <class T = double>
5563 struct S { template <class U> void f(U); };
5564 Here the default argument for `S' has no bearing on the
5565 declaration of `f'. */
5566 last_level_to_check = template_class_depth (current_class_type) + 1;
5567 else
5568 /* Check everything. */
5569 last_level_to_check = 0;
5570
5571 for (parm_level = parms;
5572 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5573 parm_level = TREE_CHAIN (parm_level))
5574 {
5575 tree inner_parms = TREE_VALUE (parm_level);
5576 int i;
5577 int ntparms;
5578
5579 ntparms = TREE_VEC_LENGTH (inner_parms);
5580 for (i = 0; i < ntparms; ++i)
5581 {
5582 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5583 continue;
5584
5585 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5586 {
5587 if (msg)
5588 {
5589 no_errors = false;
5590 if (is_friend_decl == 2)
5591 return no_errors;
5592
5593 error (msg, decl);
5594 msg = 0;
5595 }
5596
5597 /* Clear out the default argument so that we are not
5598 confused later. */
5599 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5600 }
5601 }
5602
5603 /* At this point, if we're still interested in issuing messages,
5604 they must apply to classes surrounding the object declared. */
5605 if (msg)
5606 msg = G_("default argument for template parameter for class "
5607 "enclosing %qD");
5608 }
5609
5610 return no_errors;
5611 }
5612
5613 /* Worker for push_template_decl_real, called via
5614 for_each_template_parm. DATA is really an int, indicating the
5615 level of the parameters we are interested in. If T is a template
5616 parameter of that level, return nonzero. */
5617
5618 static int
template_parm_this_level_p(tree t,void * data)5619 template_parm_this_level_p (tree t, void* data)
5620 {
5621 int this_level = *(int *)data;
5622 int level;
5623
5624 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5625 level = TEMPLATE_PARM_LEVEL (t);
5626 else
5627 level = TEMPLATE_TYPE_LEVEL (t);
5628 return level == this_level;
5629 }
5630
5631 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5632 DATA is really an int, indicating the innermost outer level of parameters.
5633 If T is a template parameter of that level or further out, return
5634 nonzero. */
5635
5636 static int
template_parm_outer_level(tree t,void * data)5637 template_parm_outer_level (tree t, void *data)
5638 {
5639 int this_level = *(int *)data;
5640 int level;
5641
5642 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5643 level = TEMPLATE_PARM_LEVEL (t);
5644 else
5645 level = TEMPLATE_TYPE_LEVEL (t);
5646 return level <= this_level;
5647 }
5648
5649 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5650 parameters given by current_template_args, or reuses a
5651 previously existing one, if appropriate. Returns the DECL, or an
5652 equivalent one, if it is replaced via a call to duplicate_decls.
5653
5654 If IS_FRIEND is true, DECL is a friend declaration. */
5655
5656 tree
push_template_decl_real(tree decl,bool is_friend)5657 push_template_decl_real (tree decl, bool is_friend)
5658 {
5659 tree tmpl;
5660 tree args;
5661 tree info;
5662 tree ctx;
5663 bool is_primary;
5664 bool is_partial;
5665 int new_template_p = 0;
5666 /* True if the template is a member template, in the sense of
5667 [temp.mem]. */
5668 bool member_template_p = false;
5669
5670 if (decl == error_mark_node || !current_template_parms)
5671 return error_mark_node;
5672
5673 /* See if this is a partial specialization. */
5674 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5675 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5676 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5677 || (VAR_P (decl)
5678 && DECL_LANG_SPECIFIC (decl)
5679 && DECL_TEMPLATE_SPECIALIZATION (decl)
5680 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5681
5682 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5683 is_friend = true;
5684
5685 if (is_friend)
5686 /* For a friend, we want the context of the friend, not
5687 the type of which it is a friend. */
5688 ctx = CP_DECL_CONTEXT (decl);
5689 else if (CP_DECL_CONTEXT (decl)
5690 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5691 /* In the case of a virtual function, we want the class in which
5692 it is defined. */
5693 ctx = CP_DECL_CONTEXT (decl);
5694 else
5695 /* Otherwise, if we're currently defining some class, the DECL
5696 is assumed to be a member of the class. */
5697 ctx = current_scope ();
5698
5699 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5700 ctx = NULL_TREE;
5701
5702 if (!DECL_CONTEXT (decl))
5703 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5704
5705 /* See if this is a primary template. */
5706 if (is_friend && ctx
5707 && uses_template_parms_level (ctx, processing_template_decl))
5708 /* A friend template that specifies a class context, i.e.
5709 template <typename T> friend void A<T>::f();
5710 is not primary. */
5711 is_primary = false;
5712 else if (TREE_CODE (decl) == TYPE_DECL
5713 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5714 is_primary = false;
5715 else
5716 is_primary = template_parm_scope_p ();
5717
5718 if (is_primary)
5719 {
5720 warning (OPT_Wtemplates, "template %qD declared", decl);
5721
5722 if (DECL_CLASS_SCOPE_P (decl))
5723 member_template_p = true;
5724 if (TREE_CODE (decl) == TYPE_DECL
5725 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5726 {
5727 error ("template class without a name");
5728 return error_mark_node;
5729 }
5730 else if (TREE_CODE (decl) == FUNCTION_DECL)
5731 {
5732 if (member_template_p)
5733 {
5734 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5735 error ("member template %qD may not have virt-specifiers", decl);
5736 }
5737 if (DECL_DESTRUCTOR_P (decl))
5738 {
5739 /* [temp.mem]
5740
5741 A destructor shall not be a member template. */
5742 error_at (DECL_SOURCE_LOCATION (decl),
5743 "destructor %qD declared as member template", decl);
5744 return error_mark_node;
5745 }
5746 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5747 && (!prototype_p (TREE_TYPE (decl))
5748 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5749 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5750 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5751 == void_list_node)))
5752 {
5753 /* [basic.stc.dynamic.allocation]
5754
5755 An allocation function can be a function
5756 template. ... Template allocation functions shall
5757 have two or more parameters. */
5758 error ("invalid template declaration of %qD", decl);
5759 return error_mark_node;
5760 }
5761 }
5762 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5763 && CLASS_TYPE_P (TREE_TYPE (decl)))
5764 {
5765 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5766 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5767 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5768 {
5769 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5770 if (TREE_CODE (t) == TYPE_DECL)
5771 t = TREE_TYPE (t);
5772 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5773 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5774 }
5775 }
5776 else if (TREE_CODE (decl) == TYPE_DECL
5777 && TYPE_DECL_ALIAS_P (decl))
5778 /* alias-declaration */
5779 gcc_assert (!DECL_ARTIFICIAL (decl));
5780 else if (VAR_P (decl))
5781 /* C++14 variable template. */;
5782 else if (TREE_CODE (decl) == CONCEPT_DECL)
5783 /* C++2a concept definitions. */;
5784 else
5785 {
5786 error ("template declaration of %q#D", decl);
5787 return error_mark_node;
5788 }
5789 }
5790
5791 /* Check to see that the rules regarding the use of default
5792 arguments are not being violated. We check args for a friend
5793 functions when we know whether it's a definition, introducing
5794 declaration or re-declaration. */
5795 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5796 check_default_tmpl_args (decl, current_template_parms,
5797 is_primary, is_partial, is_friend);
5798
5799 /* Ensure that there are no parameter packs in the type of this
5800 declaration that have not been expanded. */
5801 if (TREE_CODE (decl) == FUNCTION_DECL)
5802 {
5803 /* Check each of the arguments individually to see if there are
5804 any bare parameter packs. */
5805 tree type = TREE_TYPE (decl);
5806 tree arg = DECL_ARGUMENTS (decl);
5807 tree argtype = TYPE_ARG_TYPES (type);
5808
5809 while (arg && argtype)
5810 {
5811 if (!DECL_PACK_P (arg)
5812 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5813 {
5814 /* This is a PARM_DECL that contains unexpanded parameter
5815 packs. We have already complained about this in the
5816 check_for_bare_parameter_packs call, so just replace
5817 these types with ERROR_MARK_NODE. */
5818 TREE_TYPE (arg) = error_mark_node;
5819 TREE_VALUE (argtype) = error_mark_node;
5820 }
5821
5822 arg = DECL_CHAIN (arg);
5823 argtype = TREE_CHAIN (argtype);
5824 }
5825
5826 /* Check for bare parameter packs in the return type and the
5827 exception specifiers. */
5828 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5829 /* Errors were already issued, set return type to int
5830 as the frontend doesn't expect error_mark_node as
5831 the return type. */
5832 TREE_TYPE (type) = integer_type_node;
5833 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5834 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5835 }
5836 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5837 ? DECL_ORIGINAL_TYPE (decl)
5838 : TREE_TYPE (decl)))
5839 {
5840 TREE_TYPE (decl) = error_mark_node;
5841 return error_mark_node;
5842 }
5843
5844 if (is_partial)
5845 return process_partial_specialization (decl);
5846
5847 args = current_template_args ();
5848
5849 if (!ctx
5850 || TREE_CODE (ctx) == FUNCTION_DECL
5851 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5852 || (TREE_CODE (decl) == TYPE_DECL
5853 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5854 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5855 {
5856 if (DECL_LANG_SPECIFIC (decl)
5857 && DECL_TEMPLATE_INFO (decl)
5858 && DECL_TI_TEMPLATE (decl))
5859 tmpl = DECL_TI_TEMPLATE (decl);
5860 /* If DECL is a TYPE_DECL for a class-template, then there won't
5861 be DECL_LANG_SPECIFIC. The information equivalent to
5862 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5863 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5864 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5865 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5866 {
5867 /* Since a template declaration already existed for this
5868 class-type, we must be redeclaring it here. Make sure
5869 that the redeclaration is valid. */
5870 redeclare_class_template (TREE_TYPE (decl),
5871 current_template_parms,
5872 current_template_constraints ());
5873 /* We don't need to create a new TEMPLATE_DECL; just use the
5874 one we already had. */
5875 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5876 }
5877 else
5878 {
5879 tmpl = build_template_decl (decl, current_template_parms,
5880 member_template_p);
5881 new_template_p = 1;
5882
5883 if (DECL_LANG_SPECIFIC (decl)
5884 && DECL_TEMPLATE_SPECIALIZATION (decl))
5885 {
5886 /* A specialization of a member template of a template
5887 class. */
5888 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5889 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5890 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5891 }
5892 }
5893 }
5894 else
5895 {
5896 tree a, t, current, parms;
5897 int i;
5898 tree tinfo = get_template_info (decl);
5899
5900 if (!tinfo)
5901 {
5902 error ("template definition of non-template %q#D", decl);
5903 return error_mark_node;
5904 }
5905
5906 tmpl = TI_TEMPLATE (tinfo);
5907
5908 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5909 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5910 && DECL_TEMPLATE_SPECIALIZATION (decl)
5911 && DECL_MEMBER_TEMPLATE_P (tmpl))
5912 {
5913 tree new_tmpl;
5914
5915 /* The declaration is a specialization of a member
5916 template, declared outside the class. Therefore, the
5917 innermost template arguments will be NULL, so we
5918 replace them with the arguments determined by the
5919 earlier call to check_explicit_specialization. */
5920 args = DECL_TI_ARGS (decl);
5921
5922 new_tmpl
5923 = build_template_decl (decl, current_template_parms,
5924 member_template_p);
5925 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5926 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5927 DECL_TI_TEMPLATE (decl) = new_tmpl;
5928 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5929 DECL_TEMPLATE_INFO (new_tmpl)
5930 = build_template_info (tmpl, args);
5931
5932 register_specialization (new_tmpl,
5933 most_general_template (tmpl),
5934 args,
5935 is_friend, 0);
5936 return decl;
5937 }
5938
5939 /* Make sure the template headers we got make sense. */
5940
5941 parms = DECL_TEMPLATE_PARMS (tmpl);
5942 i = TMPL_PARMS_DEPTH (parms);
5943 if (TMPL_ARGS_DEPTH (args) != i)
5944 {
5945 error ("expected %d levels of template parms for %q#D, got %d",
5946 i, decl, TMPL_ARGS_DEPTH (args));
5947 DECL_INTERFACE_KNOWN (decl) = 1;
5948 return error_mark_node;
5949 }
5950 else
5951 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5952 {
5953 a = TMPL_ARGS_LEVEL (args, i);
5954 t = INNERMOST_TEMPLATE_PARMS (parms);
5955
5956 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5957 {
5958 if (current == decl)
5959 error ("got %d template parameters for %q#D",
5960 TREE_VEC_LENGTH (a), decl);
5961 else
5962 error ("got %d template parameters for %q#T",
5963 TREE_VEC_LENGTH (a), current);
5964 error (" but %d required", TREE_VEC_LENGTH (t));
5965 /* Avoid crash in import_export_decl. */
5966 DECL_INTERFACE_KNOWN (decl) = 1;
5967 return error_mark_node;
5968 }
5969
5970 if (current == decl)
5971 current = ctx;
5972 else if (current == NULL_TREE)
5973 /* Can happen in erroneous input. */
5974 break;
5975 else
5976 current = get_containing_scope (current);
5977 }
5978
5979 /* Check that the parms are used in the appropriate qualifying scopes
5980 in the declarator. */
5981 if (!comp_template_args
5982 (TI_ARGS (tinfo),
5983 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5984 {
5985 error ("template arguments to %qD do not match original "
5986 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5987 if (!uses_template_parms (TI_ARGS (tinfo)))
5988 inform (input_location, "use %<template<>%> for"
5989 " an explicit specialization");
5990 /* Avoid crash in import_export_decl. */
5991 DECL_INTERFACE_KNOWN (decl) = 1;
5992 return error_mark_node;
5993 }
5994 }
5995
5996 DECL_TEMPLATE_RESULT (tmpl) = decl;
5997 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5998
5999 /* Push template declarations for global functions and types. Note
6000 that we do not try to push a global template friend declared in a
6001 template class; such a thing may well depend on the template
6002 parameters of the class. */
6003 if (new_template_p && !ctx
6004 && !(is_friend && template_class_depth (current_class_type) > 0))
6005 {
6006 tmpl = pushdecl_namespace_level (tmpl, is_friend);
6007 if (tmpl == error_mark_node)
6008 return error_mark_node;
6009
6010 /* Hide template friend classes that haven't been declared yet. */
6011 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
6012 {
6013 DECL_ANTICIPATED (tmpl) = 1;
6014 DECL_FRIEND_P (tmpl) = 1;
6015 }
6016 }
6017
6018 if (is_primary)
6019 {
6020 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6021
6022 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6023
6024 /* Give template template parms a DECL_CONTEXT of the template
6025 for which they are a parameter. */
6026 parms = INNERMOST_TEMPLATE_PARMS (parms);
6027 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6028 {
6029 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6030 if (TREE_CODE (parm) == TEMPLATE_DECL)
6031 DECL_CONTEXT (parm) = tmpl;
6032 }
6033
6034 if (TREE_CODE (decl) == TYPE_DECL
6035 && TYPE_DECL_ALIAS_P (decl))
6036 {
6037 if (tree constr
6038 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6039 {
6040 /* ??? Why don't we do this here for all templates? */
6041 constr = build_constraints (constr, NULL_TREE);
6042 set_constraints (decl, constr);
6043 }
6044 if (complex_alias_template_p (tmpl))
6045 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6046 }
6047 }
6048
6049 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6050 back to its most general template. If TMPL is a specialization,
6051 ARGS may only have the innermost set of arguments. Add the missing
6052 argument levels if necessary. */
6053 if (DECL_TEMPLATE_INFO (tmpl))
6054 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6055
6056 info = build_template_info (tmpl, args);
6057
6058 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6059 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6060 else
6061 {
6062 if (is_primary)
6063 retrofit_lang_decl (decl);
6064 if (DECL_LANG_SPECIFIC (decl))
6065 DECL_TEMPLATE_INFO (decl) = info;
6066 }
6067
6068 if (flag_implicit_templates
6069 && !is_friend
6070 && TREE_PUBLIC (decl)
6071 && VAR_OR_FUNCTION_DECL_P (decl))
6072 /* Set DECL_COMDAT on template instantiations; if we force
6073 them to be emitted by explicit instantiation,
6074 mark_needed will tell cgraph to do the right thing. */
6075 DECL_COMDAT (decl) = true;
6076
6077 return DECL_TEMPLATE_RESULT (tmpl);
6078 }
6079
6080 tree
push_template_decl(tree decl)6081 push_template_decl (tree decl)
6082 {
6083 return push_template_decl_real (decl, false);
6084 }
6085
6086 /* FN is an inheriting constructor that inherits from the constructor
6087 template INHERITED; turn FN into a constructor template with a matching
6088 template header. */
6089
6090 tree
add_inherited_template_parms(tree fn,tree inherited)6091 add_inherited_template_parms (tree fn, tree inherited)
6092 {
6093 tree inner_parms
6094 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6095 inner_parms = copy_node (inner_parms);
6096 tree parms
6097 = tree_cons (size_int (processing_template_decl + 1),
6098 inner_parms, current_template_parms);
6099 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6100 tree args = template_parms_to_args (parms);
6101 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6102 TREE_TYPE (tmpl) = TREE_TYPE (fn);
6103 DECL_TEMPLATE_RESULT (tmpl) = fn;
6104 DECL_ARTIFICIAL (tmpl) = true;
6105 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6106 return tmpl;
6107 }
6108
6109 /* Called when a class template TYPE is redeclared with the indicated
6110 template PARMS, e.g.:
6111
6112 template <class T> struct S;
6113 template <class T> struct S {}; */
6114
6115 bool
redeclare_class_template(tree type,tree parms,tree cons)6116 redeclare_class_template (tree type, tree parms, tree cons)
6117 {
6118 tree tmpl;
6119 tree tmpl_parms;
6120 int i;
6121
6122 if (!TYPE_TEMPLATE_INFO (type))
6123 {
6124 error ("%qT is not a template type", type);
6125 return false;
6126 }
6127
6128 tmpl = TYPE_TI_TEMPLATE (type);
6129 if (!PRIMARY_TEMPLATE_P (tmpl))
6130 /* The type is nested in some template class. Nothing to worry
6131 about here; there are no new template parameters for the nested
6132 type. */
6133 return true;
6134
6135 if (!parms)
6136 {
6137 error ("template specifiers not specified in declaration of %qD",
6138 tmpl);
6139 return false;
6140 }
6141
6142 parms = INNERMOST_TEMPLATE_PARMS (parms);
6143 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6144
6145 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6146 {
6147 error_n (input_location, TREE_VEC_LENGTH (parms),
6148 "redeclared with %d template parameter",
6149 "redeclared with %d template parameters",
6150 TREE_VEC_LENGTH (parms));
6151 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6152 "previous declaration %qD used %d template parameter",
6153 "previous declaration %qD used %d template parameters",
6154 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6155 return false;
6156 }
6157
6158 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6159 {
6160 tree tmpl_parm;
6161 tree parm;
6162 tree tmpl_default;
6163 tree parm_default;
6164
6165 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6166 || TREE_VEC_ELT (parms, i) == error_mark_node)
6167 continue;
6168
6169 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6170 if (error_operand_p (tmpl_parm))
6171 return false;
6172
6173 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6174 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6175 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6176
6177 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6178 TEMPLATE_DECL. */
6179 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6180 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6181 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6182 || (TREE_CODE (tmpl_parm) != PARM_DECL
6183 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6184 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6185 || (TREE_CODE (tmpl_parm) == PARM_DECL
6186 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6187 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6188 {
6189 auto_diagnostic_group d;
6190 error ("template parameter %q+#D", tmpl_parm);
6191 inform (input_location, "redeclared here as %q#D", parm);
6192 return false;
6193 }
6194
6195 /* The parameters can be declared to introduce different
6196 constraints. */
6197 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6198 tree p2 = TREE_VEC_ELT (parms, i);
6199 if (!template_parameter_constraints_equivalent_p (p1, p2))
6200 {
6201 auto_diagnostic_group d;
6202 error ("declaration of template parameter %q+#D with different "
6203 "constraints", parm);
6204 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6205 "original declaration appeared here");
6206 return false;
6207 }
6208
6209 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6210 {
6211 /* We have in [temp.param]:
6212
6213 A template-parameter may not be given default arguments
6214 by two different declarations in the same scope. */
6215 auto_diagnostic_group d;
6216 error_at (input_location, "redefinition of default argument for %q#D", parm);
6217 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6218 "original definition appeared here");
6219 return false;
6220 }
6221
6222 if (parm_default != NULL_TREE)
6223 /* Update the previous template parameters (which are the ones
6224 that will really count) with the new default value. */
6225 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6226 else if (tmpl_default != NULL_TREE)
6227 /* Update the new parameters, too; they'll be used as the
6228 parameters for any members. */
6229 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6230
6231 /* Give each template template parm in this redeclaration a
6232 DECL_CONTEXT of the template for which they are a parameter. */
6233 if (TREE_CODE (parm) == TEMPLATE_DECL)
6234 {
6235 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6236 DECL_CONTEXT (parm) = tmpl;
6237 }
6238
6239 if (TREE_CODE (parm) == TYPE_DECL)
6240 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6241 }
6242
6243 tree ci = get_constraints (tmpl);
6244 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6245 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6246
6247 /* Two classes with different constraints declare different entities. */
6248 if (!cp_tree_equal (req1, req2))
6249 {
6250 auto_diagnostic_group d;
6251 error_at (input_location, "redeclaration %q#D with different "
6252 "constraints", tmpl);
6253 inform (DECL_SOURCE_LOCATION (tmpl),
6254 "original declaration appeared here");
6255 return false;
6256 }
6257
6258 return true;
6259 }
6260
6261 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6262 to be used when the caller has already checked
6263 (processing_template_decl
6264 && !instantiation_dependent_expression_p (expr)
6265 && potential_constant_expression (expr))
6266 and cleared processing_template_decl. */
6267
6268 tree
instantiate_non_dependent_expr_internal(tree expr,tsubst_flags_t complain)6269 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6270 {
6271 return tsubst_copy_and_build (expr,
6272 /*args=*/NULL_TREE,
6273 complain,
6274 /*in_decl=*/NULL_TREE,
6275 /*function_p=*/false,
6276 /*integral_constant_expression_p=*/true);
6277 }
6278
6279 /* Simplify EXPR if it is a non-dependent expression. Returns the
6280 (possibly simplified) expression. */
6281
6282 tree
instantiate_non_dependent_expr_sfinae(tree expr,tsubst_flags_t complain)6283 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6284 {
6285 if (expr == NULL_TREE)
6286 return NULL_TREE;
6287
6288 /* If we're in a template, but EXPR isn't value dependent, simplify
6289 it. We're supposed to treat:
6290
6291 template <typename T> void f(T[1 + 1]);
6292 template <typename T> void f(T[2]);
6293
6294 as two declarations of the same function, for example. */
6295 if (processing_template_decl
6296 && is_nondependent_constant_expression (expr))
6297 {
6298 processing_template_decl_sentinel s;
6299 expr = instantiate_non_dependent_expr_internal (expr, complain);
6300 }
6301 return expr;
6302 }
6303
6304 tree
instantiate_non_dependent_expr(tree expr)6305 instantiate_non_dependent_expr (tree expr)
6306 {
6307 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6308 }
6309
6310 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6311 an uninstantiated expression. */
6312
6313 tree
instantiate_non_dependent_or_null(tree expr)6314 instantiate_non_dependent_or_null (tree expr)
6315 {
6316 if (expr == NULL_TREE)
6317 return NULL_TREE;
6318 if (processing_template_decl)
6319 {
6320 if (!is_nondependent_constant_expression (expr))
6321 expr = NULL_TREE;
6322 else
6323 {
6324 processing_template_decl_sentinel s;
6325 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6326 }
6327 }
6328 return expr;
6329 }
6330
6331 /* True iff T is a specialization of a variable template. */
6332
6333 bool
variable_template_specialization_p(tree t)6334 variable_template_specialization_p (tree t)
6335 {
6336 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6337 return false;
6338 tree tmpl = DECL_TI_TEMPLATE (t);
6339 return variable_template_p (tmpl);
6340 }
6341
6342 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6343 template declaration, or a TYPE_DECL for an alias declaration. */
6344
6345 bool
alias_type_or_template_p(tree t)6346 alias_type_or_template_p (tree t)
6347 {
6348 if (t == NULL_TREE)
6349 return false;
6350 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6351 || (TYPE_P (t)
6352 && TYPE_NAME (t)
6353 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6354 || DECL_ALIAS_TEMPLATE_P (t));
6355 }
6356
6357 /* If T is a specialization of an alias template, return it; otherwise return
6358 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6359
6360 tree
alias_template_specialization_p(const_tree t,bool transparent_typedefs)6361 alias_template_specialization_p (const_tree t,
6362 bool transparent_typedefs)
6363 {
6364 if (!TYPE_P (t))
6365 return NULL_TREE;
6366
6367 /* It's an alias template specialization if it's an alias and its
6368 TYPE_NAME is a specialization of a primary template. */
6369 if (typedef_variant_p (t))
6370 {
6371 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6372 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6373 return CONST_CAST_TREE (t);
6374 if (transparent_typedefs)
6375 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6376 (TYPE_NAME (t)),
6377 transparent_typedefs);
6378 }
6379
6380 return NULL_TREE;
6381 }
6382
6383 /* An alias template is complex from a SFINAE perspective if a template-id
6384 using that alias can be ill-formed when the expansion is not, as with
6385 the void_t template. We determine this by checking whether the
6386 expansion for the alias template uses all its template parameters. */
6387
6388 struct uses_all_template_parms_data
6389 {
6390 int level;
6391 bool *seen;
6392 };
6393
6394 static int
uses_all_template_parms_r(tree t,void * data_)6395 uses_all_template_parms_r (tree t, void *data_)
6396 {
6397 struct uses_all_template_parms_data &data
6398 = *(struct uses_all_template_parms_data*)data_;
6399 tree idx = get_template_parm_index (t);
6400
6401 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6402 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6403 return 0;
6404 }
6405
6406 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6407
6408 static int
complex_pack_expansion_r(tree t,void * data_)6409 complex_pack_expansion_r (tree t, void *data_)
6410 {
6411 /* An alias template with a pack expansion that expands a pack from the
6412 enclosing class needs to be considered complex, to avoid confusion with
6413 the same pack being used as an argument to the alias's own template
6414 parameter (91966). */
6415 if (!PACK_EXPANSION_P (t))
6416 return 0;
6417 struct uses_all_template_parms_data &data
6418 = *(struct uses_all_template_parms_data*)data_;
6419 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6420 pack = TREE_CHAIN (pack))
6421 {
6422 tree parm_pack = TREE_VALUE (pack);
6423 if (!TEMPLATE_PARM_P (parm_pack))
6424 continue;
6425 int idx, level;
6426 template_parm_level_and_index (parm_pack, &level, &idx);
6427 if (level < data.level)
6428 return 1;
6429 }
6430 return 0;
6431 }
6432
6433 static bool
complex_alias_template_p(const_tree tmpl)6434 complex_alias_template_p (const_tree tmpl)
6435 {
6436 /* A renaming alias isn't complex. */
6437 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6438 return false;
6439
6440 /* Any other constrained alias is complex. */
6441 if (get_constraints (tmpl))
6442 return true;
6443
6444 struct uses_all_template_parms_data data;
6445 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6446 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6447 data.level = TMPL_PARMS_DEPTH (parms);
6448 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6449 data.seen = XALLOCAVEC (bool, len);
6450 for (int i = 0; i < len; ++i)
6451 data.seen[i] = false;
6452
6453 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6454 NULL, true, complex_pack_expansion_r))
6455 return true;
6456 for (int i = 0; i < len; ++i)
6457 if (!data.seen[i])
6458 return true;
6459 return false;
6460 }
6461
6462 /* If T is a specialization of a complex alias template with dependent
6463 template-arguments, return it; otherwise return NULL_TREE. If T is a
6464 typedef to such a specialization, return the specialization. */
6465
6466 tree
dependent_alias_template_spec_p(const_tree t,bool transparent_typedefs)6467 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6468 {
6469 if (!TYPE_P (t) || !typedef_variant_p (t))
6470 return NULL_TREE;
6471
6472 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6473 if (tinfo
6474 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6475 && (any_dependent_template_arguments_p
6476 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6477 return CONST_CAST_TREE (t);
6478
6479 if (transparent_typedefs)
6480 {
6481 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6482 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6483 }
6484
6485 return NULL_TREE;
6486 }
6487
6488 /* Return the number of innermost template parameters in TMPL. */
6489
6490 static int
num_innermost_template_parms(const_tree tmpl)6491 num_innermost_template_parms (const_tree tmpl)
6492 {
6493 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6494 return TREE_VEC_LENGTH (parms);
6495 }
6496
6497 /* Return either TMPL or another template that it is equivalent to under DR
6498 1286: An alias that just changes the name of a template is equivalent to
6499 the other template. */
6500
6501 static tree
get_underlying_template(tree tmpl)6502 get_underlying_template (tree tmpl)
6503 {
6504 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6505 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6506 {
6507 /* Determine if the alias is equivalent to an underlying template. */
6508 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6509 /* The underlying type may have been ill-formed. Don't proceed. */
6510 if (!orig_type)
6511 break;
6512 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6513 if (!tinfo)
6514 break;
6515
6516 tree underlying = TI_TEMPLATE (tinfo);
6517 if (!PRIMARY_TEMPLATE_P (underlying)
6518 || (num_innermost_template_parms (tmpl)
6519 != num_innermost_template_parms (underlying)))
6520 break;
6521
6522 /* Does the alias add cv-quals? */
6523 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
6524 break;
6525
6526 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6527 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6528 break;
6529
6530 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6531 it's appropriate to treat a less-constrained alias as equivalent. */
6532 if (!at_least_as_constrained (underlying, tmpl))
6533 break;
6534
6535 /* Alias is equivalent. Strip it and repeat. */
6536 tmpl = underlying;
6537 }
6538
6539 return tmpl;
6540 }
6541
6542 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6543 must be a reference-to-function or a pointer-to-function type, as specified
6544 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6545 and check that the resulting function has external linkage. */
6546
6547 static tree
convert_nontype_argument_function(tree type,tree expr,tsubst_flags_t complain)6548 convert_nontype_argument_function (tree type, tree expr,
6549 tsubst_flags_t complain)
6550 {
6551 tree fns = expr;
6552 tree fn, fn_no_ptr;
6553 linkage_kind linkage;
6554
6555 fn = instantiate_type (type, fns, tf_none);
6556 if (fn == error_mark_node)
6557 return error_mark_node;
6558
6559 if (value_dependent_expression_p (fn))
6560 goto accept;
6561
6562 fn_no_ptr = fn;
6563 if (REFERENCE_REF_P (fn_no_ptr))
6564 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6565 fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
6566 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6567 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6568 if (BASELINK_P (fn_no_ptr))
6569 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6570
6571 /* [temp.arg.nontype]/1
6572
6573 A template-argument for a non-type, non-template template-parameter
6574 shall be one of:
6575 [...]
6576 -- the address of an object or function with external [C++11: or
6577 internal] linkage. */
6578
6579 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6580 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6581 {
6582 if (complain & tf_error)
6583 {
6584 location_t loc = cp_expr_loc_or_input_loc (expr);
6585 error_at (loc, "%qE is not a valid template argument for type %qT",
6586 expr, type);
6587 if (TYPE_PTR_P (type))
6588 inform (loc, "it must be the address of a function "
6589 "with external linkage");
6590 else
6591 inform (loc, "it must be the name of a function with "
6592 "external linkage");
6593 }
6594 return NULL_TREE;
6595 }
6596
6597 linkage = decl_linkage (fn_no_ptr);
6598 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6599 {
6600 if (complain & tf_error)
6601 {
6602 location_t loc = cp_expr_loc_or_input_loc (expr);
6603 if (cxx_dialect >= cxx11)
6604 error_at (loc, "%qE is not a valid template argument for type "
6605 "%qT because %qD has no linkage",
6606 expr, type, fn_no_ptr);
6607 else
6608 error_at (loc, "%qE is not a valid template argument for type "
6609 "%qT because %qD does not have external linkage",
6610 expr, type, fn_no_ptr);
6611 }
6612 return NULL_TREE;
6613 }
6614
6615 accept:
6616 if (TYPE_REF_P (type))
6617 {
6618 if (REFERENCE_REF_P (fn))
6619 fn = TREE_OPERAND (fn, 0);
6620 else
6621 fn = build_address (fn);
6622 }
6623 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6624 fn = build_nop (type, fn);
6625
6626 return fn;
6627 }
6628
6629 /* Subroutine of convert_nontype_argument.
6630 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6631 Emit an error otherwise. */
6632
6633 static bool
check_valid_ptrmem_cst_expr(tree type,tree expr,tsubst_flags_t complain)6634 check_valid_ptrmem_cst_expr (tree type, tree expr,
6635 tsubst_flags_t complain)
6636 {
6637 tree orig_expr = expr;
6638 STRIP_NOPS (expr);
6639 if (null_ptr_cst_p (expr))
6640 return true;
6641 if (TREE_CODE (expr) == PTRMEM_CST
6642 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6643 PTRMEM_CST_CLASS (expr)))
6644 return true;
6645 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6646 return true;
6647 if (processing_template_decl
6648 && TREE_CODE (expr) == ADDR_EXPR
6649 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6650 return true;
6651 if (complain & tf_error)
6652 {
6653 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6654 error_at (loc, "%qE is not a valid template argument for type %qT",
6655 orig_expr, type);
6656 if (TREE_CODE (expr) != PTRMEM_CST)
6657 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6658 else
6659 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6660 }
6661 return false;
6662 }
6663
6664 /* Returns TRUE iff the address of OP is value-dependent.
6665
6666 14.6.2.4 [temp.dep.temp]:
6667 A non-integral non-type template-argument is dependent if its type is
6668 dependent or it has either of the following forms
6669 qualified-id
6670 & qualified-id
6671 and contains a nested-name-specifier which specifies a class-name that
6672 names a dependent type.
6673
6674 We generalize this to just say that the address of a member of a
6675 dependent class is value-dependent; the above doesn't cover the
6676 address of a static data member named with an unqualified-id. */
6677
6678 static bool
has_value_dependent_address(tree op)6679 has_value_dependent_address (tree op)
6680 {
6681 STRIP_ANY_LOCATION_WRAPPER (op);
6682
6683 /* We could use get_inner_reference here, but there's no need;
6684 this is only relevant for template non-type arguments, which
6685 can only be expressed as &id-expression. */
6686 if (DECL_P (op))
6687 {
6688 tree ctx = CP_DECL_CONTEXT (op);
6689 if (TYPE_P (ctx) && dependent_type_p (ctx))
6690 return true;
6691 }
6692
6693 return false;
6694 }
6695
6696 /* The next set of functions are used for providing helpful explanatory
6697 diagnostics for failed overload resolution. Their messages should be
6698 indented by two spaces for consistency with the messages in
6699 call.c */
6700
6701 static int
unify_success(bool)6702 unify_success (bool /*explain_p*/)
6703 {
6704 return 0;
6705 }
6706
6707 /* Other failure functions should call this one, to provide a single function
6708 for setting a breakpoint on. */
6709
6710 static int
unify_invalid(bool)6711 unify_invalid (bool /*explain_p*/)
6712 {
6713 return 1;
6714 }
6715
6716 static int
unify_parameter_deduction_failure(bool explain_p,tree parm)6717 unify_parameter_deduction_failure (bool explain_p, tree parm)
6718 {
6719 if (explain_p)
6720 inform (input_location,
6721 " couldn%'t deduce template parameter %qD", parm);
6722 return unify_invalid (explain_p);
6723 }
6724
6725 static int
unify_cv_qual_mismatch(bool explain_p,tree parm,tree arg)6726 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6727 {
6728 if (explain_p)
6729 inform (input_location,
6730 " types %qT and %qT have incompatible cv-qualifiers",
6731 parm, arg);
6732 return unify_invalid (explain_p);
6733 }
6734
6735 static int
unify_type_mismatch(bool explain_p,tree parm,tree arg)6736 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6737 {
6738 if (explain_p)
6739 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6740 return unify_invalid (explain_p);
6741 }
6742
6743 static int
unify_parameter_pack_mismatch(bool explain_p,tree parm,tree arg)6744 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6745 {
6746 if (explain_p)
6747 inform (input_location,
6748 " template parameter %qD is not a parameter pack, but "
6749 "argument %qD is",
6750 parm, arg);
6751 return unify_invalid (explain_p);
6752 }
6753
6754 static int
unify_ptrmem_cst_mismatch(bool explain_p,tree parm,tree arg)6755 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6756 {
6757 if (explain_p)
6758 inform (input_location,
6759 " template argument %qE does not match "
6760 "pointer-to-member constant %qE",
6761 arg, parm);
6762 return unify_invalid (explain_p);
6763 }
6764
6765 static int
unify_expression_unequal(bool explain_p,tree parm,tree arg)6766 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6767 {
6768 if (explain_p)
6769 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6770 return unify_invalid (explain_p);
6771 }
6772
6773 static int
unify_parameter_pack_inconsistent(bool explain_p,tree old_arg,tree new_arg)6774 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6775 {
6776 if (explain_p)
6777 inform (input_location,
6778 " inconsistent parameter pack deduction with %qT and %qT",
6779 old_arg, new_arg);
6780 return unify_invalid (explain_p);
6781 }
6782
6783 static int
unify_inconsistency(bool explain_p,tree parm,tree first,tree second)6784 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6785 {
6786 if (explain_p)
6787 {
6788 if (TYPE_P (parm))
6789 inform (input_location,
6790 " deduced conflicting types for parameter %qT (%qT and %qT)",
6791 parm, first, second);
6792 else
6793 inform (input_location,
6794 " deduced conflicting values for non-type parameter "
6795 "%qE (%qE and %qE)", parm, first, second);
6796 }
6797 return unify_invalid (explain_p);
6798 }
6799
6800 static int
unify_vla_arg(bool explain_p,tree arg)6801 unify_vla_arg (bool explain_p, tree arg)
6802 {
6803 if (explain_p)
6804 inform (input_location,
6805 " variable-sized array type %qT is not "
6806 "a valid template argument",
6807 arg);
6808 return unify_invalid (explain_p);
6809 }
6810
6811 static int
unify_method_type_error(bool explain_p,tree arg)6812 unify_method_type_error (bool explain_p, tree arg)
6813 {
6814 if (explain_p)
6815 inform (input_location,
6816 " member function type %qT is not a valid template argument",
6817 arg);
6818 return unify_invalid (explain_p);
6819 }
6820
6821 static int
6822 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6823 {
6824 if (explain_p)
6825 {
6826 if (least_p)
6827 inform_n (input_location, wanted,
6828 " candidate expects at least %d argument, %d provided",
6829 " candidate expects at least %d arguments, %d provided",
6830 wanted, have);
6831 else
6832 inform_n (input_location, wanted,
6833 " candidate expects %d argument, %d provided",
6834 " candidate expects %d arguments, %d provided",
6835 wanted, have);
6836 }
6837 return unify_invalid (explain_p);
6838 }
6839
6840 static int
unify_too_many_arguments(bool explain_p,int have,int wanted)6841 unify_too_many_arguments (bool explain_p, int have, int wanted)
6842 {
6843 return unify_arity (explain_p, have, wanted);
6844 }
6845
6846 static int
6847 unify_too_few_arguments (bool explain_p, int have, int wanted,
6848 bool least_p = false)
6849 {
6850 return unify_arity (explain_p, have, wanted, least_p);
6851 }
6852
6853 static int
unify_arg_conversion(bool explain_p,tree to_type,tree from_type,tree arg)6854 unify_arg_conversion (bool explain_p, tree to_type,
6855 tree from_type, tree arg)
6856 {
6857 if (explain_p)
6858 inform (cp_expr_loc_or_input_loc (arg),
6859 " cannot convert %qE (type %qT) to type %qT",
6860 arg, from_type, to_type);
6861 return unify_invalid (explain_p);
6862 }
6863
6864 static int
unify_no_common_base(bool explain_p,enum template_base_result r,tree parm,tree arg)6865 unify_no_common_base (bool explain_p, enum template_base_result r,
6866 tree parm, tree arg)
6867 {
6868 if (explain_p)
6869 switch (r)
6870 {
6871 case tbr_ambiguous_baseclass:
6872 inform (input_location, " %qT is an ambiguous base class of %qT",
6873 parm, arg);
6874 break;
6875 default:
6876 inform (input_location, " %qT is not derived from %qT", arg, parm);
6877 break;
6878 }
6879 return unify_invalid (explain_p);
6880 }
6881
6882 static int
unify_inconsistent_template_template_parameters(bool explain_p)6883 unify_inconsistent_template_template_parameters (bool explain_p)
6884 {
6885 if (explain_p)
6886 inform (input_location,
6887 " template parameters of a template template argument are "
6888 "inconsistent with other deduced template arguments");
6889 return unify_invalid (explain_p);
6890 }
6891
6892 static int
unify_template_deduction_failure(bool explain_p,tree parm,tree arg)6893 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6894 {
6895 if (explain_p)
6896 inform (input_location,
6897 " cannot deduce a template for %qT from non-template type %qT",
6898 parm, arg);
6899 return unify_invalid (explain_p);
6900 }
6901
6902 static int
unify_template_argument_mismatch(bool explain_p,tree parm,tree arg)6903 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6904 {
6905 if (explain_p)
6906 inform (input_location,
6907 " template argument %qE does not match %qE", arg, parm);
6908 return unify_invalid (explain_p);
6909 }
6910
6911 /* True if T is a C++20 template parameter object to store the argument for a
6912 template parameter of class type. */
6913
6914 bool
template_parm_object_p(const_tree t)6915 template_parm_object_p (const_tree t)
6916 {
6917 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6918 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6919 }
6920
6921 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6922 argument for TYPE, points to an unsuitable object. */
6923
6924 static bool
invalid_tparm_referent_p(tree type,tree expr,tsubst_flags_t complain)6925 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6926 {
6927 switch (TREE_CODE (expr))
6928 {
6929 CASE_CONVERT:
6930 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6931 complain);
6932
6933 case TARGET_EXPR:
6934 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6935 complain);
6936
6937 case CONSTRUCTOR:
6938 {
6939 unsigned i; tree elt;
6940 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6941 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6942 return true;
6943 }
6944 break;
6945
6946 case ADDR_EXPR:
6947 {
6948 tree decl = TREE_OPERAND (expr, 0);
6949
6950 if (!VAR_P (decl))
6951 {
6952 if (complain & tf_error)
6953 error_at (cp_expr_loc_or_input_loc (expr),
6954 "%qE is not a valid template argument of type %qT "
6955 "because %qE is not a variable", expr, type, decl);
6956 return true;
6957 }
6958 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6959 {
6960 if (complain & tf_error)
6961 error_at (cp_expr_loc_or_input_loc (expr),
6962 "%qE is not a valid template argument of type %qT "
6963 "in C++98 because %qD does not have external linkage",
6964 expr, type, decl);
6965 return true;
6966 }
6967 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6968 && decl_linkage (decl) == lk_none)
6969 {
6970 if (complain & tf_error)
6971 error_at (cp_expr_loc_or_input_loc (expr),
6972 "%qE is not a valid template argument of type %qT "
6973 "because %qD has no linkage", expr, type, decl);
6974 return true;
6975 }
6976 /* C++17: For a non-type template-parameter of reference or pointer
6977 type, the value of the constant expression shall not refer to (or
6978 for a pointer type, shall not be the address of):
6979 * a subobject (4.5),
6980 * a temporary object (15.2),
6981 * a string literal (5.13.5),
6982 * the result of a typeid expression (8.2.8), or
6983 * a predefined __func__ variable (11.4.1). */
6984 else if (DECL_ARTIFICIAL (decl))
6985 {
6986 if (complain & tf_error)
6987 error ("the address of %qD is not a valid template argument",
6988 decl);
6989 return true;
6990 }
6991 else if (!same_type_ignoring_top_level_qualifiers_p
6992 (strip_array_types (TREE_TYPE (type)),
6993 strip_array_types (TREE_TYPE (decl))))
6994 {
6995 if (complain & tf_error)
6996 error ("the address of the %qT subobject of %qD is not a "
6997 "valid template argument", TREE_TYPE (type), decl);
6998 return true;
6999 }
7000 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7001 {
7002 if (complain & tf_error)
7003 error ("the address of %qD is not a valid template argument "
7004 "because it does not have static storage duration",
7005 decl);
7006 return true;
7007 }
7008 }
7009 break;
7010
7011 default:
7012 if (!INDIRECT_TYPE_P (type))
7013 /* We're only concerned about pointers and references here. */;
7014 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7015 /* Null pointer values are OK in C++11. */;
7016 else
7017 {
7018 if (VAR_P (expr))
7019 {
7020 if (complain & tf_error)
7021 error ("%qD is not a valid template argument "
7022 "because %qD is a variable, not the address of "
7023 "a variable", expr, expr);
7024 return true;
7025 }
7026 else
7027 {
7028 if (complain & tf_error)
7029 error ("%qE is not a valid template argument for %qT "
7030 "because it is not the address of a variable",
7031 expr, type);
7032 return true;
7033 }
7034 }
7035 }
7036 return false;
7037
7038 }
7039
7040 /* The template arguments corresponding to template parameter objects of types
7041 that contain pointers to members. */
7042
7043 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7044
7045 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7046 template argument EXPR. */
7047
7048 static tree
get_template_parm_object(tree expr,tsubst_flags_t complain)7049 get_template_parm_object (tree expr, tsubst_flags_t complain)
7050 {
7051 if (TREE_CODE (expr) == TARGET_EXPR)
7052 expr = TARGET_EXPR_INITIAL (expr);
7053
7054 if (!TREE_CONSTANT (expr))
7055 {
7056 if ((complain & tf_error)
7057 && require_rvalue_constant_expression (expr))
7058 cxx_constant_value (expr);
7059 return error_mark_node;
7060 }
7061 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7062 return error_mark_node;
7063
7064 tree name = mangle_template_parm_object (expr);
7065 tree decl = get_global_binding (name);
7066 if (decl)
7067 return decl;
7068
7069 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7070 decl = create_temporary_var (type);
7071 TREE_STATIC (decl) = true;
7072 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7073 TREE_READONLY (decl) = true;
7074 DECL_NAME (decl) = name;
7075 SET_DECL_ASSEMBLER_NAME (decl, name);
7076 DECL_CONTEXT (decl) = global_namespace;
7077 comdat_linkage (decl);
7078
7079 if (!zero_init_p (type))
7080 {
7081 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7082 lower_var_init before we're done mangling. So store the original
7083 value elsewhere. */
7084 tree copy = unshare_constructor (expr);
7085 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7086 }
7087
7088 pushdecl_top_level_and_finish (decl, expr);
7089
7090 return decl;
7091 }
7092
7093 /* Return the actual template argument corresponding to template parameter
7094 object VAR. */
7095
7096 tree
tparm_object_argument(tree var)7097 tparm_object_argument (tree var)
7098 {
7099 if (zero_init_p (TREE_TYPE (var)))
7100 return DECL_INITIAL (var);
7101 return *(tparm_obj_values->get (var));
7102 }
7103
7104 /* Attempt to convert the non-type template parameter EXPR to the
7105 indicated TYPE. If the conversion is successful, return the
7106 converted value. If the conversion is unsuccessful, return
7107 NULL_TREE if we issued an error message, or error_mark_node if we
7108 did not. We issue error messages for out-and-out bad template
7109 parameters, but not simply because the conversion failed, since we
7110 might be just trying to do argument deduction. Both TYPE and EXPR
7111 must be non-dependent.
7112
7113 The conversion follows the special rules described in
7114 [temp.arg.nontype], and it is much more strict than an implicit
7115 conversion.
7116
7117 This function is called twice for each template argument (see
7118 lookup_template_class for a more accurate description of this
7119 problem). This means that we need to handle expressions which
7120 are not valid in a C++ source, but can be created from the
7121 first call (for instance, casts to perform conversions). These
7122 hacks can go away after we fix the double coercion problem. */
7123
7124 static tree
convert_nontype_argument(tree type,tree expr,tsubst_flags_t complain)7125 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7126 {
7127 tree expr_type;
7128 location_t loc = cp_expr_loc_or_input_loc (expr);
7129
7130 /* Detect immediately string literals as invalid non-type argument.
7131 This special-case is not needed for correctness (we would easily
7132 catch this later), but only to provide better diagnostic for this
7133 common user mistake. As suggested by DR 100, we do not mention
7134 linkage issues in the diagnostic as this is not the point. */
7135 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7136 {
7137 if (complain & tf_error)
7138 error ("%qE is not a valid template argument for type %qT "
7139 "because string literals can never be used in this context",
7140 expr, type);
7141 return NULL_TREE;
7142 }
7143
7144 /* Add the ADDR_EXPR now for the benefit of
7145 value_dependent_expression_p. */
7146 if (TYPE_PTROBV_P (type)
7147 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7148 {
7149 expr = decay_conversion (expr, complain);
7150 if (expr == error_mark_node)
7151 return error_mark_node;
7152 }
7153
7154 /* If we are in a template, EXPR may be non-dependent, but still
7155 have a syntactic, rather than semantic, form. For example, EXPR
7156 might be a SCOPE_REF, rather than the VAR_DECL to which the
7157 SCOPE_REF refers. Preserving the qualifying scope is necessary
7158 so that access checking can be performed when the template is
7159 instantiated -- but here we need the resolved form so that we can
7160 convert the argument. */
7161 bool non_dep = false;
7162 if (TYPE_REF_OBJ_P (type)
7163 && has_value_dependent_address (expr))
7164 /* If we want the address and it's value-dependent, don't fold. */;
7165 else if (processing_template_decl
7166 && is_nondependent_constant_expression (expr))
7167 non_dep = true;
7168 if (error_operand_p (expr))
7169 return error_mark_node;
7170 expr_type = TREE_TYPE (expr);
7171
7172 /* If the argument is non-dependent, perform any conversions in
7173 non-dependent context as well. */
7174 processing_template_decl_sentinel s (non_dep);
7175 if (non_dep)
7176 expr = instantiate_non_dependent_expr_internal (expr, complain);
7177
7178 const bool val_dep_p = value_dependent_expression_p (expr);
7179 if (val_dep_p)
7180 expr = canonicalize_expr_argument (expr, complain);
7181
7182 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7183 to a non-type argument of "nullptr". */
7184 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7185 expr = fold_simple (convert (type, expr));
7186
7187 /* In C++11, integral or enumeration non-type template arguments can be
7188 arbitrary constant expressions. Pointer and pointer to
7189 member arguments can be general constant expressions that evaluate
7190 to a null value, but otherwise still need to be of a specific form. */
7191 if (cxx_dialect >= cxx11)
7192 {
7193 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7194 /* A PTRMEM_CST is already constant, and a valid template
7195 argument for a parameter of pointer to member type, we just want
7196 to leave it in that form rather than lower it to a
7197 CONSTRUCTOR. */;
7198 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7199 || cxx_dialect >= cxx17)
7200 {
7201 /* C++17: A template-argument for a non-type template-parameter shall
7202 be a converted constant expression (8.20) of the type of the
7203 template-parameter. */
7204 expr = build_converted_constant_expr (type, expr, complain);
7205 if (expr == error_mark_node)
7206 /* Make sure we return NULL_TREE only if we have really issued
7207 an error, as described above. */
7208 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7209 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7210 {
7211 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7212 return expr;
7213 }
7214 expr = maybe_constant_value (expr, NULL_TREE,
7215 /*manifestly_const_eval=*/true);
7216 expr = convert_from_reference (expr);
7217 }
7218 else if (TYPE_PTR_OR_PTRMEM_P (type))
7219 {
7220 tree folded = maybe_constant_value (expr, NULL_TREE,
7221 /*manifestly_const_eval=*/true);
7222 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7223 : null_member_pointer_value_p (folded))
7224 expr = folded;
7225 }
7226 }
7227
7228 if (TYPE_REF_P (type))
7229 expr = mark_lvalue_use (expr);
7230 else
7231 expr = mark_rvalue_use (expr);
7232
7233 /* HACK: Due to double coercion, we can get a
7234 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7235 which is the tree that we built on the first call (see
7236 below when coercing to reference to object or to reference to
7237 function). We just strip everything and get to the arg.
7238 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7239 for examples. */
7240 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7241 {
7242 tree probe_type, probe = expr;
7243 if (REFERENCE_REF_P (probe))
7244 probe = TREE_OPERAND (probe, 0);
7245 probe_type = TREE_TYPE (probe);
7246 if (TREE_CODE (probe) == NOP_EXPR)
7247 {
7248 /* ??? Maybe we could use convert_from_reference here, but we
7249 would need to relax its constraints because the NOP_EXPR
7250 could actually change the type to something more cv-qualified,
7251 and this is not folded by convert_from_reference. */
7252 tree addr = TREE_OPERAND (probe, 0);
7253 if (TYPE_REF_P (probe_type)
7254 && TREE_CODE (addr) == ADDR_EXPR
7255 && TYPE_PTR_P (TREE_TYPE (addr))
7256 && (same_type_ignoring_top_level_qualifiers_p
7257 (TREE_TYPE (probe_type),
7258 TREE_TYPE (TREE_TYPE (addr)))))
7259 {
7260 expr = TREE_OPERAND (addr, 0);
7261 expr_type = TREE_TYPE (probe_type);
7262 }
7263 }
7264 }
7265
7266 /* [temp.arg.nontype]/5, bullet 1
7267
7268 For a non-type template-parameter of integral or enumeration type,
7269 integral promotions (_conv.prom_) and integral conversions
7270 (_conv.integral_) are applied. */
7271 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7272 {
7273 if (cxx_dialect < cxx11)
7274 {
7275 tree t = build_converted_constant_expr (type, expr, complain);
7276 t = maybe_constant_value (t);
7277 if (t != error_mark_node)
7278 expr = t;
7279 }
7280
7281 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7282 return error_mark_node;
7283
7284 /* Notice that there are constant expressions like '4 % 0' which
7285 do not fold into integer constants. */
7286 if (TREE_CODE (expr) != INTEGER_CST && !val_dep_p)
7287 {
7288 if (complain & tf_error)
7289 {
7290 int errs = errorcount, warns = warningcount + werrorcount;
7291 if (!require_potential_constant_expression (expr))
7292 expr = error_mark_node;
7293 else
7294 expr = cxx_constant_value (expr);
7295 if (errorcount > errs || warningcount + werrorcount > warns)
7296 inform (loc, "in template argument for type %qT", type);
7297 if (expr == error_mark_node)
7298 return NULL_TREE;
7299 /* else cxx_constant_value complained but gave us
7300 a real constant, so go ahead. */
7301 if (TREE_CODE (expr) != INTEGER_CST)
7302 {
7303 /* Some assemble time constant expressions like
7304 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7305 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7306 as we can emit them into .rodata initializers of
7307 variables, yet they can't fold into an INTEGER_CST at
7308 compile time. Refuse them here. */
7309 gcc_checking_assert (reduced_constant_expression_p (expr));
7310 error_at (loc, "template argument %qE for type %qT not "
7311 "a constant integer", expr, type);
7312 return NULL_TREE;
7313 }
7314 }
7315 else
7316 return NULL_TREE;
7317 }
7318
7319 /* Avoid typedef problems. */
7320 if (TREE_TYPE (expr) != type)
7321 expr = fold_convert (type, expr);
7322 }
7323 /* [temp.arg.nontype]/5, bullet 2
7324
7325 For a non-type template-parameter of type pointer to object,
7326 qualification conversions (_conv.qual_) and the array-to-pointer
7327 conversion (_conv.array_) are applied. */
7328 else if (TYPE_PTROBV_P (type))
7329 {
7330 tree decayed = expr;
7331
7332 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7333 decay_conversion or an explicit cast. If it's a problematic cast,
7334 we'll complain about it below. */
7335 if (TREE_CODE (expr) == NOP_EXPR)
7336 {
7337 tree probe = expr;
7338 STRIP_NOPS (probe);
7339 if (TREE_CODE (probe) == ADDR_EXPR
7340 && TYPE_PTR_P (TREE_TYPE (probe)))
7341 {
7342 expr = probe;
7343 expr_type = TREE_TYPE (expr);
7344 }
7345 }
7346
7347 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7348
7349 A template-argument for a non-type, non-template template-parameter
7350 shall be one of: [...]
7351
7352 -- the name of a non-type template-parameter;
7353 -- the address of an object or function with external linkage, [...]
7354 expressed as "& id-expression" where the & is optional if the name
7355 refers to a function or array, or if the corresponding
7356 template-parameter is a reference.
7357
7358 Here, we do not care about functions, as they are invalid anyway
7359 for a parameter of type pointer-to-object. */
7360
7361 if (val_dep_p)
7362 /* Non-type template parameters are OK. */
7363 ;
7364 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7365 /* Null pointer values are OK in C++11. */;
7366 else if (TREE_CODE (expr) != ADDR_EXPR
7367 && !INDIRECT_TYPE_P (expr_type))
7368 /* Other values, like integer constants, might be valid
7369 non-type arguments of some other type. */
7370 return error_mark_node;
7371 else if (invalid_tparm_referent_p (type, expr, complain))
7372 return NULL_TREE;
7373
7374 expr = decayed;
7375
7376 expr = perform_qualification_conversions (type, expr);
7377 if (expr == error_mark_node)
7378 return error_mark_node;
7379 }
7380 /* [temp.arg.nontype]/5, bullet 3
7381
7382 For a non-type template-parameter of type reference to object, no
7383 conversions apply. The type referred to by the reference may be more
7384 cv-qualified than the (otherwise identical) type of the
7385 template-argument. The template-parameter is bound directly to the
7386 template-argument, which must be an lvalue. */
7387 else if (TYPE_REF_OBJ_P (type))
7388 {
7389 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7390 expr_type))
7391 return error_mark_node;
7392
7393 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7394 {
7395 if (complain & tf_error)
7396 error ("%qE is not a valid template argument for type %qT "
7397 "because of conflicts in cv-qualification", expr, type);
7398 return NULL_TREE;
7399 }
7400
7401 if (!lvalue_p (expr))
7402 {
7403 if (complain & tf_error)
7404 error ("%qE is not a valid template argument for type %qT "
7405 "because it is not an lvalue", expr, type);
7406 return NULL_TREE;
7407 }
7408
7409 /* [temp.arg.nontype]/1
7410
7411 A template-argument for a non-type, non-template template-parameter
7412 shall be one of: [...]
7413
7414 -- the address of an object or function with external linkage. */
7415 if (INDIRECT_REF_P (expr)
7416 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7417 {
7418 expr = TREE_OPERAND (expr, 0);
7419 if (DECL_P (expr))
7420 {
7421 if (complain & tf_error)
7422 error ("%q#D is not a valid template argument for type %qT "
7423 "because a reference variable does not have a constant "
7424 "address", expr, type);
7425 return NULL_TREE;
7426 }
7427 }
7428
7429 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7430 /* OK, dependent reference. We don't want to ask whether a DECL is
7431 itself value-dependent, since what we want here is its address. */;
7432 else
7433 {
7434 expr = build_address (expr);
7435
7436 if (invalid_tparm_referent_p (type, expr, complain))
7437 return NULL_TREE;
7438 }
7439
7440 if (!same_type_p (type, TREE_TYPE (expr)))
7441 expr = build_nop (type, expr);
7442 }
7443 /* [temp.arg.nontype]/5, bullet 4
7444
7445 For a non-type template-parameter of type pointer to function, only
7446 the function-to-pointer conversion (_conv.func_) is applied. If the
7447 template-argument represents a set of overloaded functions (or a
7448 pointer to such), the matching function is selected from the set
7449 (_over.over_). */
7450 else if (TYPE_PTRFN_P (type))
7451 {
7452 /* If the argument is a template-id, we might not have enough
7453 context information to decay the pointer. */
7454 if (!type_unknown_p (expr_type))
7455 {
7456 expr = decay_conversion (expr, complain);
7457 if (expr == error_mark_node)
7458 return error_mark_node;
7459 }
7460
7461 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7462 /* Null pointer values are OK in C++11. */
7463 return perform_qualification_conversions (type, expr);
7464
7465 expr = convert_nontype_argument_function (type, expr, complain);
7466 if (!expr || expr == error_mark_node)
7467 return expr;
7468 }
7469 /* [temp.arg.nontype]/5, bullet 5
7470
7471 For a non-type template-parameter of type reference to function, no
7472 conversions apply. If the template-argument represents a set of
7473 overloaded functions, the matching function is selected from the set
7474 (_over.over_). */
7475 else if (TYPE_REFFN_P (type))
7476 {
7477 if (TREE_CODE (expr) == ADDR_EXPR)
7478 {
7479 if (complain & tf_error)
7480 {
7481 error ("%qE is not a valid template argument for type %qT "
7482 "because it is a pointer", expr, type);
7483 inform (input_location, "try using %qE instead",
7484 TREE_OPERAND (expr, 0));
7485 }
7486 return NULL_TREE;
7487 }
7488
7489 expr = convert_nontype_argument_function (type, expr, complain);
7490 if (!expr || expr == error_mark_node)
7491 return expr;
7492 }
7493 /* [temp.arg.nontype]/5, bullet 6
7494
7495 For a non-type template-parameter of type pointer to member function,
7496 no conversions apply. If the template-argument represents a set of
7497 overloaded member functions, the matching member function is selected
7498 from the set (_over.over_). */
7499 else if (TYPE_PTRMEMFUNC_P (type))
7500 {
7501 expr = instantiate_type (type, expr, tf_none);
7502 if (expr == error_mark_node)
7503 return error_mark_node;
7504
7505 /* [temp.arg.nontype] bullet 1 says the pointer to member
7506 expression must be a pointer-to-member constant. */
7507 if (!val_dep_p
7508 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7509 return NULL_TREE;
7510
7511 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7512 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7513 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7514 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7515 }
7516 /* [temp.arg.nontype]/5, bullet 7
7517
7518 For a non-type template-parameter of type pointer to data member,
7519 qualification conversions (_conv.qual_) are applied. */
7520 else if (TYPE_PTRDATAMEM_P (type))
7521 {
7522 /* [temp.arg.nontype] bullet 1 says the pointer to member
7523 expression must be a pointer-to-member constant. */
7524 if (!val_dep_p
7525 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7526 return NULL_TREE;
7527
7528 expr = perform_qualification_conversions (type, expr);
7529 if (expr == error_mark_node)
7530 return expr;
7531 }
7532 else if (NULLPTR_TYPE_P (type))
7533 {
7534 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7535 {
7536 if (complain & tf_error)
7537 error ("%qE is not a valid template argument for type %qT "
7538 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7539 return NULL_TREE;
7540 }
7541 return expr;
7542 }
7543 else if (CLASS_TYPE_P (type))
7544 {
7545 /* Replace the argument with a reference to the corresponding template
7546 parameter object. */
7547 if (!val_dep_p)
7548 expr = get_template_parm_object (expr, complain);
7549 if (expr == error_mark_node)
7550 return NULL_TREE;
7551 }
7552 /* A template non-type parameter must be one of the above. */
7553 else
7554 gcc_unreachable ();
7555
7556 /* Sanity check: did we actually convert the argument to the
7557 right type? */
7558 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7559 (type, TREE_TYPE (expr)));
7560 return convert_from_reference (expr);
7561 }
7562
7563 /* Subroutine of coerce_template_template_parms, which returns 1 if
7564 PARM_PARM and ARG_PARM match using the rule for the template
7565 parameters of template template parameters. Both PARM and ARG are
7566 template parameters; the rest of the arguments are the same as for
7567 coerce_template_template_parms.
7568 */
7569 static int
coerce_template_template_parm(tree parm,tree arg,tsubst_flags_t complain,tree in_decl,tree outer_args)7570 coerce_template_template_parm (tree parm,
7571 tree arg,
7572 tsubst_flags_t complain,
7573 tree in_decl,
7574 tree outer_args)
7575 {
7576 if (arg == NULL_TREE || error_operand_p (arg)
7577 || parm == NULL_TREE || error_operand_p (parm))
7578 return 0;
7579
7580 if (TREE_CODE (arg) != TREE_CODE (parm))
7581 return 0;
7582
7583 switch (TREE_CODE (parm))
7584 {
7585 case TEMPLATE_DECL:
7586 /* We encounter instantiations of templates like
7587 template <template <template <class> class> class TT>
7588 class C; */
7589 {
7590 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7591 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7592
7593 if (!coerce_template_template_parms
7594 (parmparm, argparm, complain, in_decl, outer_args))
7595 return 0;
7596 }
7597 /* Fall through. */
7598
7599 case TYPE_DECL:
7600 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7601 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7602 /* Argument is a parameter pack but parameter is not. */
7603 return 0;
7604 break;
7605
7606 case PARM_DECL:
7607 /* The tsubst call is used to handle cases such as
7608
7609 template <int> class C {};
7610 template <class T, template <T> class TT> class D {};
7611 D<int, C> d;
7612
7613 i.e. the parameter list of TT depends on earlier parameters. */
7614 if (!uses_template_parms (TREE_TYPE (arg)))
7615 {
7616 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7617 if (!uses_template_parms (t)
7618 && !same_type_p (t, TREE_TYPE (arg)))
7619 return 0;
7620 }
7621
7622 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7623 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7624 /* Argument is a parameter pack but parameter is not. */
7625 return 0;
7626
7627 break;
7628
7629 default:
7630 gcc_unreachable ();
7631 }
7632
7633 return 1;
7634 }
7635
7636 /* Coerce template argument list ARGLIST for use with template
7637 template-parameter TEMPL. */
7638
7639 static tree
coerce_template_args_for_ttp(tree templ,tree arglist,tsubst_flags_t complain)7640 coerce_template_args_for_ttp (tree templ, tree arglist,
7641 tsubst_flags_t complain)
7642 {
7643 /* Consider an example where a template template parameter declared as
7644
7645 template <class T, class U = std::allocator<T> > class TT
7646
7647 The template parameter level of T and U are one level larger than
7648 of TT. To proper process the default argument of U, say when an
7649 instantiation `TT<int>' is seen, we need to build the full
7650 arguments containing {int} as the innermost level. Outer levels,
7651 available when not appearing as default template argument, can be
7652 obtained from the arguments of the enclosing template.
7653
7654 Suppose that TT is later substituted with std::vector. The above
7655 instantiation is `TT<int, std::allocator<T> >' with TT at
7656 level 1, and T at level 2, while the template arguments at level 1
7657 becomes {std::vector} and the inner level 2 is {int}. */
7658
7659 tree outer = DECL_CONTEXT (templ);
7660 if (outer)
7661 outer = generic_targs_for (outer);
7662 else if (current_template_parms)
7663 {
7664 /* This is an argument of the current template, so we haven't set
7665 DECL_CONTEXT yet. */
7666 tree relevant_template_parms;
7667
7668 /* Parameter levels that are greater than the level of the given
7669 template template parm are irrelevant. */
7670 relevant_template_parms = current_template_parms;
7671 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7672 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7673 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7674
7675 outer = template_parms_to_args (relevant_template_parms);
7676 }
7677
7678 if (outer)
7679 arglist = add_to_template_args (outer, arglist);
7680
7681 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7682 return coerce_template_parms (parmlist, arglist, templ,
7683 complain,
7684 /*require_all_args=*/true,
7685 /*use_default_args=*/true);
7686 }
7687
7688 /* A cache of template template parameters with match-all default
7689 arguments. */
7690 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7691
7692 /* T is a bound template template-parameter. Copy its arguments into default
7693 arguments of the template template-parameter's template parameters. */
7694
7695 static tree
add_defaults_to_ttp(tree otmpl)7696 add_defaults_to_ttp (tree otmpl)
7697 {
7698 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7699 return *c;
7700
7701 tree ntmpl = copy_node (otmpl);
7702
7703 tree ntype = copy_node (TREE_TYPE (otmpl));
7704 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7705 TYPE_MAIN_VARIANT (ntype) = ntype;
7706 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7707 TYPE_NAME (ntype) = ntmpl;
7708 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7709
7710 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7711 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7712 TEMPLATE_PARM_DECL (idx) = ntmpl;
7713 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7714
7715 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7716 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7717 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7718 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7719 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7720 {
7721 tree o = TREE_VEC_ELT (vec, i);
7722 if (!template_parameter_pack_p (TREE_VALUE (o)))
7723 {
7724 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7725 TREE_PURPOSE (n) = any_targ_node;
7726 }
7727 }
7728
7729 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7730 return ntmpl;
7731 }
7732
7733 /* ARG is a bound potential template template-argument, and PARGS is a list
7734 of arguments for the corresponding template template-parameter. Adjust
7735 PARGS as appropriate for application to ARG's template, and if ARG is a
7736 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7737 arguments to the template template parameter. */
7738
7739 static tree
coerce_ttp_args_for_tta(tree & arg,tree pargs,tsubst_flags_t complain)7740 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7741 {
7742 ++processing_template_decl;
7743 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7744 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7745 {
7746 /* When comparing two template template-parameters in partial ordering,
7747 rewrite the one currently being used as an argument to have default
7748 arguments for all parameters. */
7749 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7750 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7751 if (pargs != error_mark_node)
7752 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7753 TYPE_TI_ARGS (arg));
7754 }
7755 else
7756 {
7757 tree aparms
7758 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7759 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7760 /*require_all*/true,
7761 /*use_default*/true);
7762 }
7763 --processing_template_decl;
7764 return pargs;
7765 }
7766
7767 /* Subroutine of unify for the case when PARM is a
7768 BOUND_TEMPLATE_TEMPLATE_PARM. */
7769
7770 static int
unify_bound_ttp_args(tree tparms,tree targs,tree parm,tree & arg,bool explain_p)7771 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7772 bool explain_p)
7773 {
7774 tree parmvec = TYPE_TI_ARGS (parm);
7775 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7776
7777 /* The template template parm might be variadic and the argument
7778 not, so flatten both argument lists. */
7779 parmvec = expand_template_argument_pack (parmvec);
7780 argvec = expand_template_argument_pack (argvec);
7781
7782 if (flag_new_ttp)
7783 {
7784 /* In keeping with P0522R0, adjust P's template arguments
7785 to apply to A's template; then flatten it again. */
7786 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7787 nparmvec = expand_template_argument_pack (nparmvec);
7788
7789 if (unify (tparms, targs, nparmvec, argvec,
7790 UNIFY_ALLOW_NONE, explain_p))
7791 return 1;
7792
7793 /* If the P0522 adjustment eliminated a pack expansion, deduce
7794 empty packs. */
7795 if (flag_new_ttp
7796 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7797 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7798 DEDUCE_EXACT, /*sub*/true, explain_p))
7799 return 1;
7800 }
7801 else
7802 {
7803 /* Deduce arguments T, i from TT<T> or TT<i>.
7804 We check each element of PARMVEC and ARGVEC individually
7805 rather than the whole TREE_VEC since they can have
7806 different number of elements, which is allowed under N2555. */
7807
7808 int len = TREE_VEC_LENGTH (parmvec);
7809
7810 /* Check if the parameters end in a pack, making them
7811 variadic. */
7812 int parm_variadic_p = 0;
7813 if (len > 0
7814 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7815 parm_variadic_p = 1;
7816
7817 for (int i = 0; i < len - parm_variadic_p; ++i)
7818 /* If the template argument list of P contains a pack
7819 expansion that is not the last template argument, the
7820 entire template argument list is a non-deduced
7821 context. */
7822 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7823 return unify_success (explain_p);
7824
7825 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7826 return unify_too_few_arguments (explain_p,
7827 TREE_VEC_LENGTH (argvec), len);
7828
7829 for (int i = 0; i < len - parm_variadic_p; ++i)
7830 if (unify (tparms, targs,
7831 TREE_VEC_ELT (parmvec, i),
7832 TREE_VEC_ELT (argvec, i),
7833 UNIFY_ALLOW_NONE, explain_p))
7834 return 1;
7835
7836 if (parm_variadic_p
7837 && unify_pack_expansion (tparms, targs,
7838 parmvec, argvec,
7839 DEDUCE_EXACT,
7840 /*subr=*/true, explain_p))
7841 return 1;
7842 }
7843
7844 return 0;
7845 }
7846
7847 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7848 template template parameters. Both PARM_PARMS and ARG_PARMS are
7849 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7850 or PARM_DECL.
7851
7852 Consider the example:
7853 template <class T> class A;
7854 template<template <class U> class TT> class B;
7855
7856 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7857 the parameters to A, and OUTER_ARGS contains A. */
7858
7859 static int
coerce_template_template_parms(tree parm_parms,tree arg_parms,tsubst_flags_t complain,tree in_decl,tree outer_args)7860 coerce_template_template_parms (tree parm_parms,
7861 tree arg_parms,
7862 tsubst_flags_t complain,
7863 tree in_decl,
7864 tree outer_args)
7865 {
7866 int nparms, nargs, i;
7867 tree parm, arg;
7868 int variadic_p = 0;
7869
7870 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7871 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7872
7873 nparms = TREE_VEC_LENGTH (parm_parms);
7874 nargs = TREE_VEC_LENGTH (arg_parms);
7875
7876 if (flag_new_ttp)
7877 {
7878 /* P0522R0: A template template-parameter P is at least as specialized as
7879 a template template-argument A if, given the following rewrite to two
7880 function templates, the function template corresponding to P is at
7881 least as specialized as the function template corresponding to A
7882 according to the partial ordering rules for function templates
7883 ([temp.func.order]). Given an invented class template X with the
7884 template parameter list of A (including default arguments):
7885
7886 * Each of the two function templates has the same template parameters,
7887 respectively, as P or A.
7888
7889 * Each function template has a single function parameter whose type is
7890 a specialization of X with template arguments corresponding to the
7891 template parameters from the respective function template where, for
7892 each template parameter PP in the template parameter list of the
7893 function template, a corresponding template argument AA is formed. If
7894 PP declares a parameter pack, then AA is the pack expansion
7895 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7896
7897 If the rewrite produces an invalid type, then P is not at least as
7898 specialized as A. */
7899
7900 /* So coerce P's args to apply to A's parms, and then deduce between A's
7901 args and the converted args. If that succeeds, A is at least as
7902 specialized as P, so they match.*/
7903 tree pargs = template_parms_level_to_args (parm_parms);
7904 pargs = add_outermost_template_args (outer_args, pargs);
7905 ++processing_template_decl;
7906 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7907 /*require_all*/true, /*use_default*/true);
7908 --processing_template_decl;
7909 if (pargs != error_mark_node)
7910 {
7911 tree targs = make_tree_vec (nargs);
7912 tree aargs = template_parms_level_to_args (arg_parms);
7913 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7914 /*explain*/false))
7915 return 1;
7916 }
7917 }
7918
7919 /* Determine whether we have a parameter pack at the end of the
7920 template template parameter's template parameter list. */
7921 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7922 {
7923 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7924
7925 if (error_operand_p (parm))
7926 return 0;
7927
7928 switch (TREE_CODE (parm))
7929 {
7930 case TEMPLATE_DECL:
7931 case TYPE_DECL:
7932 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7933 variadic_p = 1;
7934 break;
7935
7936 case PARM_DECL:
7937 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7938 variadic_p = 1;
7939 break;
7940
7941 default:
7942 gcc_unreachable ();
7943 }
7944 }
7945
7946 if (nargs != nparms
7947 && !(variadic_p && nargs >= nparms - 1))
7948 return 0;
7949
7950 /* Check all of the template parameters except the parameter pack at
7951 the end (if any). */
7952 for (i = 0; i < nparms - variadic_p; ++i)
7953 {
7954 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7955 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7956 continue;
7957
7958 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7959 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7960
7961 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7962 outer_args))
7963 return 0;
7964
7965 }
7966
7967 if (variadic_p)
7968 {
7969 /* Check each of the template parameters in the template
7970 argument against the template parameter pack at the end of
7971 the template template parameter. */
7972 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7973 return 0;
7974
7975 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7976
7977 for (; i < nargs; ++i)
7978 {
7979 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7980 continue;
7981
7982 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7983
7984 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7985 outer_args))
7986 return 0;
7987 }
7988 }
7989
7990 return 1;
7991 }
7992
7993 /* Verifies that the deduced template arguments (in TARGS) for the
7994 template template parameters (in TPARMS) represent valid bindings,
7995 by comparing the template parameter list of each template argument
7996 to the template parameter list of its corresponding template
7997 template parameter, in accordance with DR150. This
7998 routine can only be called after all template arguments have been
7999 deduced. It will return TRUE if all of the template template
8000 parameter bindings are okay, FALSE otherwise. */
8001 bool
template_template_parm_bindings_ok_p(tree tparms,tree targs)8002 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8003 {
8004 int i, ntparms = TREE_VEC_LENGTH (tparms);
8005 bool ret = true;
8006
8007 /* We're dealing with template parms in this process. */
8008 ++processing_template_decl;
8009
8010 targs = INNERMOST_TEMPLATE_ARGS (targs);
8011
8012 for (i = 0; i < ntparms; ++i)
8013 {
8014 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8015 tree targ = TREE_VEC_ELT (targs, i);
8016
8017 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8018 {
8019 tree packed_args = NULL_TREE;
8020 int idx, len = 1;
8021
8022 if (ARGUMENT_PACK_P (targ))
8023 {
8024 /* Look inside the argument pack. */
8025 packed_args = ARGUMENT_PACK_ARGS (targ);
8026 len = TREE_VEC_LENGTH (packed_args);
8027 }
8028
8029 for (idx = 0; idx < len; ++idx)
8030 {
8031 tree targ_parms = NULL_TREE;
8032
8033 if (packed_args)
8034 /* Extract the next argument from the argument
8035 pack. */
8036 targ = TREE_VEC_ELT (packed_args, idx);
8037
8038 if (PACK_EXPANSION_P (targ))
8039 /* Look at the pattern of the pack expansion. */
8040 targ = PACK_EXPANSION_PATTERN (targ);
8041
8042 /* Extract the template parameters from the template
8043 argument. */
8044 if (TREE_CODE (targ) == TEMPLATE_DECL)
8045 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8046 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8047 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8048
8049 /* Verify that we can coerce the template template
8050 parameters from the template argument to the template
8051 parameter. This requires an exact match. */
8052 if (targ_parms
8053 && !coerce_template_template_parms
8054 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8055 targ_parms,
8056 tf_none,
8057 tparm,
8058 targs))
8059 {
8060 ret = false;
8061 goto out;
8062 }
8063 }
8064 }
8065 }
8066
8067 out:
8068
8069 --processing_template_decl;
8070 return ret;
8071 }
8072
8073 /* Since type attributes aren't mangled, we need to strip them from
8074 template type arguments. */
8075
8076 tree
canonicalize_type_argument(tree arg,tsubst_flags_t complain)8077 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8078 {
8079 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8080 return arg;
8081 bool removed_attributes = false;
8082 tree canon = strip_typedefs (arg, &removed_attributes);
8083 if (removed_attributes
8084 && (complain & tf_warning))
8085 warning (OPT_Wignored_attributes,
8086 "ignoring attributes on template argument %qT", arg);
8087 return canon;
8088 }
8089
8090 /* And from inside dependent non-type arguments like sizeof(Type). */
8091
8092 static tree
canonicalize_expr_argument(tree arg,tsubst_flags_t complain)8093 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8094 {
8095 if (!arg || arg == error_mark_node)
8096 return arg;
8097 bool removed_attributes = false;
8098 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8099 if (removed_attributes
8100 && (complain & tf_warning))
8101 warning (OPT_Wignored_attributes,
8102 "ignoring attributes in template argument %qE", arg);
8103 return canon;
8104 }
8105
8106 /* A template declaration can be substituted for a constrained
8107 template template parameter only when the argument is no more
8108 constrained than the parameter. */
8109
8110 static bool
is_compatible_template_arg(tree parm,tree arg)8111 is_compatible_template_arg (tree parm, tree arg)
8112 {
8113 tree parm_cons = get_constraints (parm);
8114
8115 /* For now, allow constrained template template arguments
8116 and unconstrained template template parameters. */
8117 if (parm_cons == NULL_TREE)
8118 return true;
8119
8120 /* If the template parameter is constrained, we need to rewrite its
8121 constraints in terms of the ARG's template parameters. This ensures
8122 that all of the template parameter types will have the same depth.
8123
8124 Note that this is only valid when coerce_template_template_parm is
8125 true for the innermost template parameters of PARM and ARG. In other
8126 words, because coercion is successful, this conversion will be valid. */
8127 tree new_args = NULL_TREE;
8128 if (parm_cons)
8129 {
8130 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8131 new_args = template_parms_level_to_args (aparms);
8132 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8133 tf_none, NULL_TREE);
8134 if (parm_cons == error_mark_node)
8135 return false;
8136 }
8137
8138 return weakly_subsumes (parm_cons, new_args, arg);
8139 }
8140
8141 // Convert a placeholder argument into a binding to the original
8142 // parameter. The original parameter is saved as the TREE_TYPE of
8143 // ARG.
8144 static inline tree
convert_wildcard_argument(tree parm,tree arg)8145 convert_wildcard_argument (tree parm, tree arg)
8146 {
8147 TREE_TYPE (arg) = parm;
8148 return arg;
8149 }
8150
8151 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8152 because one of them is dependent. But we need to represent the
8153 conversion for the benefit of cp_tree_equal. */
8154
8155 static tree
maybe_convert_nontype_argument(tree type,tree arg)8156 maybe_convert_nontype_argument (tree type, tree arg)
8157 {
8158 /* Auto parms get no conversion. */
8159 if (type_uses_auto (type))
8160 return arg;
8161 /* We don't need or want to add this conversion now if we're going to use the
8162 argument for deduction. */
8163 if (value_dependent_expression_p (arg))
8164 return arg;
8165
8166 type = cv_unqualified (type);
8167 tree argtype = TREE_TYPE (arg);
8168 if (same_type_p (type, argtype))
8169 return arg;
8170
8171 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8172 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8173 return arg;
8174 }
8175
8176 /* Convert the indicated template ARG as necessary to match the
8177 indicated template PARM. Returns the converted ARG, or
8178 error_mark_node if the conversion was unsuccessful. Error and
8179 warning messages are issued under control of COMPLAIN. This
8180 conversion is for the Ith parameter in the parameter list. ARGS is
8181 the full set of template arguments deduced so far. */
8182
8183 static tree
convert_template_argument(tree parm,tree arg,tree args,tsubst_flags_t complain,int i,tree in_decl)8184 convert_template_argument (tree parm,
8185 tree arg,
8186 tree args,
8187 tsubst_flags_t complain,
8188 int i,
8189 tree in_decl)
8190 {
8191 tree orig_arg;
8192 tree val;
8193 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8194
8195 if (parm == error_mark_node || error_operand_p (arg))
8196 return error_mark_node;
8197
8198 /* Trivially convert placeholders. */
8199 if (TREE_CODE (arg) == WILDCARD_DECL)
8200 return convert_wildcard_argument (parm, arg);
8201
8202 if (arg == any_targ_node)
8203 return arg;
8204
8205 if (TREE_CODE (arg) == TREE_LIST
8206 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8207 {
8208 /* The template argument was the name of some
8209 member function. That's usually
8210 invalid, but static members are OK. In any
8211 case, grab the underlying fields/functions
8212 and issue an error later if required. */
8213 TREE_TYPE (arg) = unknown_type_node;
8214 }
8215
8216 orig_arg = arg;
8217
8218 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8219 requires_type = (TREE_CODE (parm) == TYPE_DECL
8220 || requires_tmpl_type);
8221
8222 /* When determining whether an argument pack expansion is a template,
8223 look at the pattern. */
8224 if (PACK_EXPANSION_P (arg))
8225 arg = PACK_EXPANSION_PATTERN (arg);
8226
8227 /* Deal with an injected-class-name used as a template template arg. */
8228 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8229 {
8230 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8231 if (TREE_CODE (t) == TEMPLATE_DECL)
8232 {
8233 if (cxx_dialect >= cxx11)
8234 /* OK under DR 1004. */;
8235 else if (complain & tf_warning_or_error)
8236 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8237 " used as template template argument", TYPE_NAME (arg));
8238 else if (flag_pedantic_errors)
8239 t = arg;
8240
8241 arg = t;
8242 }
8243 }
8244
8245 is_tmpl_type =
8246 ((TREE_CODE (arg) == TEMPLATE_DECL
8247 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8248 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8249 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8250 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8251
8252 if (is_tmpl_type
8253 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8254 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8255 arg = TYPE_STUB_DECL (arg);
8256
8257 is_type = TYPE_P (arg) || is_tmpl_type;
8258
8259 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8260 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8261 {
8262 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8263 {
8264 if (complain & tf_error)
8265 error ("invalid use of destructor %qE as a type", orig_arg);
8266 return error_mark_node;
8267 }
8268
8269 permerror (input_location,
8270 "to refer to a type member of a template parameter, "
8271 "use %<typename %E%>", orig_arg);
8272
8273 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8274 TREE_OPERAND (arg, 1),
8275 typename_type,
8276 complain);
8277 arg = orig_arg;
8278 is_type = 1;
8279 }
8280 if (is_type != requires_type)
8281 {
8282 if (in_decl)
8283 {
8284 if (complain & tf_error)
8285 {
8286 error ("type/value mismatch at argument %d in template "
8287 "parameter list for %qD",
8288 i + 1, in_decl);
8289 if (is_type)
8290 {
8291 /* The template argument is a type, but we're expecting
8292 an expression. */
8293 inform (input_location,
8294 " expected a constant of type %qT, got %qT",
8295 TREE_TYPE (parm),
8296 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8297 /* [temp.arg]/2: "In a template-argument, an ambiguity
8298 between a type-id and an expression is resolved to a
8299 type-id, regardless of the form of the corresponding
8300 template-parameter." So give the user a clue. */
8301 if (TREE_CODE (arg) == FUNCTION_TYPE)
8302 inform (input_location, " ambiguous template argument "
8303 "for non-type template parameter is treated as "
8304 "function type");
8305 }
8306 else if (requires_tmpl_type)
8307 inform (input_location,
8308 " expected a class template, got %qE", orig_arg);
8309 else
8310 inform (input_location,
8311 " expected a type, got %qE", orig_arg);
8312 }
8313 }
8314 return error_mark_node;
8315 }
8316 if (is_tmpl_type ^ requires_tmpl_type)
8317 {
8318 if (in_decl && (complain & tf_error))
8319 {
8320 error ("type/value mismatch at argument %d in template "
8321 "parameter list for %qD",
8322 i + 1, in_decl);
8323 if (is_tmpl_type)
8324 inform (input_location,
8325 " expected a type, got %qT", DECL_NAME (arg));
8326 else
8327 inform (input_location,
8328 " expected a class template, got %qT", orig_arg);
8329 }
8330 return error_mark_node;
8331 }
8332
8333 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8334 /* We already did the appropriate conversion when packing args. */
8335 val = orig_arg;
8336 else if (is_type)
8337 {
8338 if (requires_tmpl_type)
8339 {
8340 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8341 /* The number of argument required is not known yet.
8342 Just accept it for now. */
8343 val = orig_arg;
8344 else
8345 {
8346 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8347 tree argparm;
8348
8349 /* Strip alias templates that are equivalent to another
8350 template. */
8351 arg = get_underlying_template (arg);
8352 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8353
8354 if (coerce_template_template_parms (parmparm, argparm,
8355 complain, in_decl,
8356 args))
8357 {
8358 val = arg;
8359
8360 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8361 TEMPLATE_DECL. */
8362 if (val != error_mark_node)
8363 {
8364 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8365 val = TREE_TYPE (val);
8366 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8367 val = make_pack_expansion (val, complain);
8368 }
8369 }
8370 else
8371 {
8372 if (in_decl && (complain & tf_error))
8373 {
8374 error ("type/value mismatch at argument %d in "
8375 "template parameter list for %qD",
8376 i + 1, in_decl);
8377 inform (input_location,
8378 " expected a template of type %qD, got %qT",
8379 parm, orig_arg);
8380 }
8381
8382 val = error_mark_node;
8383 }
8384
8385 // Check that the constraints are compatible before allowing the
8386 // substitution.
8387 if (val != error_mark_node)
8388 if (!is_compatible_template_arg (parm, arg))
8389 {
8390 if (in_decl && (complain & tf_error))
8391 {
8392 error ("constraint mismatch at argument %d in "
8393 "template parameter list for %qD",
8394 i + 1, in_decl);
8395 inform (input_location, " expected %qD but got %qD",
8396 parm, arg);
8397 }
8398 val = error_mark_node;
8399 }
8400 }
8401 }
8402 else
8403 val = orig_arg;
8404 /* We only form one instance of each template specialization.
8405 Therefore, if we use a non-canonical variant (i.e., a
8406 typedef), any future messages referring to the type will use
8407 the typedef, which is confusing if those future uses do not
8408 themselves also use the typedef. */
8409 if (TYPE_P (val))
8410 val = canonicalize_type_argument (val, complain);
8411 }
8412 else
8413 {
8414 tree t = TREE_TYPE (parm);
8415
8416 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8417 > TMPL_ARGS_DEPTH (args))
8418 /* We don't have enough levels of args to do any substitution. This
8419 can happen in the context of -fnew-ttp-matching. */;
8420 else if (tree a = type_uses_auto (t))
8421 {
8422 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8423 if (t == error_mark_node)
8424 return error_mark_node;
8425 }
8426 else
8427 t = tsubst (t, args, complain, in_decl);
8428
8429 if (invalid_nontype_parm_type_p (t, complain))
8430 return error_mark_node;
8431
8432 if (t != TREE_TYPE (parm))
8433 t = canonicalize_type_argument (t, complain);
8434
8435 if (!type_dependent_expression_p (orig_arg)
8436 && !uses_template_parms (t))
8437 /* We used to call digest_init here. However, digest_init
8438 will report errors, which we don't want when complain
8439 is zero. More importantly, digest_init will try too
8440 hard to convert things: for example, `0' should not be
8441 converted to pointer type at this point according to
8442 the standard. Accepting this is not merely an
8443 extension, since deciding whether or not these
8444 conversions can occur is part of determining which
8445 function template to call, or whether a given explicit
8446 argument specification is valid. */
8447 val = convert_nontype_argument (t, orig_arg, complain);
8448 else
8449 {
8450 val = canonicalize_expr_argument (orig_arg, complain);
8451 val = maybe_convert_nontype_argument (t, val);
8452 }
8453
8454
8455 if (val == NULL_TREE)
8456 val = error_mark_node;
8457 else if (val == error_mark_node && (complain & tf_error))
8458 error_at (cp_expr_loc_or_input_loc (orig_arg),
8459 "could not convert template argument %qE from %qT to %qT",
8460 orig_arg, TREE_TYPE (orig_arg), t);
8461
8462 if (INDIRECT_REF_P (val))
8463 {
8464 /* Reject template arguments that are references to built-in
8465 functions with no library fallbacks. */
8466 const_tree inner = TREE_OPERAND (val, 0);
8467 const_tree innertype = TREE_TYPE (inner);
8468 if (innertype
8469 && TYPE_REF_P (innertype)
8470 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8471 && TREE_OPERAND_LENGTH (inner) > 0
8472 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8473 return error_mark_node;
8474 }
8475
8476 if (TREE_CODE (val) == SCOPE_REF)
8477 {
8478 /* Strip typedefs from the SCOPE_REF. */
8479 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8480 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8481 complain);
8482 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8483 QUALIFIED_NAME_IS_TEMPLATE (val));
8484 }
8485 }
8486
8487 return val;
8488 }
8489
8490 /* Coerces the remaining template arguments in INNER_ARGS (from
8491 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8492 Returns the coerced argument pack. PARM_IDX is the position of this
8493 parameter in the template parameter list. ARGS is the original
8494 template argument list. */
8495 static tree
coerce_template_parameter_pack(tree parms,int parm_idx,tree args,tree inner_args,int arg_idx,tree new_args,int * lost,tree in_decl,tsubst_flags_t complain)8496 coerce_template_parameter_pack (tree parms,
8497 int parm_idx,
8498 tree args,
8499 tree inner_args,
8500 int arg_idx,
8501 tree new_args,
8502 int* lost,
8503 tree in_decl,
8504 tsubst_flags_t complain)
8505 {
8506 tree parm = TREE_VEC_ELT (parms, parm_idx);
8507 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8508 tree packed_args;
8509 tree argument_pack;
8510 tree packed_parms = NULL_TREE;
8511
8512 if (arg_idx > nargs)
8513 arg_idx = nargs;
8514
8515 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8516 {
8517 /* When the template parameter is a non-type template parameter pack
8518 or template template parameter pack whose type or template
8519 parameters use parameter packs, we know exactly how many arguments
8520 we are looking for. Build a vector of the instantiated decls for
8521 these template parameters in PACKED_PARMS. */
8522 /* We can't use make_pack_expansion here because it would interpret a
8523 _DECL as a use rather than a declaration. */
8524 tree decl = TREE_VALUE (parm);
8525 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8526 SET_PACK_EXPANSION_PATTERN (exp, decl);
8527 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8528 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8529
8530 TREE_VEC_LENGTH (args)--;
8531 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8532 TREE_VEC_LENGTH (args)++;
8533
8534 if (packed_parms == error_mark_node)
8535 return error_mark_node;
8536
8537 /* If we're doing a partial instantiation of a member template,
8538 verify that all of the types used for the non-type
8539 template parameter pack are, in fact, valid for non-type
8540 template parameters. */
8541 if (arg_idx < nargs
8542 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8543 {
8544 int j, len = TREE_VEC_LENGTH (packed_parms);
8545 for (j = 0; j < len; ++j)
8546 {
8547 tree t = TREE_VEC_ELT (packed_parms, j);
8548 if (TREE_CODE (t) == PARM_DECL
8549 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8550 return error_mark_node;
8551 }
8552 /* We don't know how many args we have yet, just
8553 use the unconverted ones for now. */
8554 return NULL_TREE;
8555 }
8556
8557 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8558 }
8559 /* Check if we have a placeholder pack, which indicates we're
8560 in the context of a introduction list. In that case we want
8561 to match this pack to the single placeholder. */
8562 else if (arg_idx < nargs
8563 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8564 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8565 {
8566 nargs = arg_idx + 1;
8567 packed_args = make_tree_vec (1);
8568 }
8569 else
8570 packed_args = make_tree_vec (nargs - arg_idx);
8571
8572 /* Convert the remaining arguments, which will be a part of the
8573 parameter pack "parm". */
8574 int first_pack_arg = arg_idx;
8575 for (; arg_idx < nargs; ++arg_idx)
8576 {
8577 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8578 tree actual_parm = TREE_VALUE (parm);
8579 int pack_idx = arg_idx - first_pack_arg;
8580
8581 if (packed_parms)
8582 {
8583 /* Once we've packed as many args as we have types, stop. */
8584 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8585 break;
8586 else if (PACK_EXPANSION_P (arg))
8587 /* We don't know how many args we have yet, just
8588 use the unconverted ones for now. */
8589 return NULL_TREE;
8590 else
8591 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8592 }
8593
8594 if (arg == error_mark_node)
8595 {
8596 if (complain & tf_error)
8597 error ("template argument %d is invalid", arg_idx + 1);
8598 }
8599 else
8600 arg = convert_template_argument (actual_parm,
8601 arg, new_args, complain, parm_idx,
8602 in_decl);
8603 if (arg == error_mark_node)
8604 (*lost)++;
8605 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8606 }
8607
8608 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8609 && TREE_VEC_LENGTH (packed_args) > 0)
8610 {
8611 if (complain & tf_error)
8612 error ("wrong number of template arguments (%d, should be %d)",
8613 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8614 return error_mark_node;
8615 }
8616
8617 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8618 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8619 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8620 else
8621 {
8622 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8623 TREE_CONSTANT (argument_pack) = 1;
8624 }
8625
8626 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8627 if (CHECKING_P)
8628 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8629 TREE_VEC_LENGTH (packed_args));
8630 return argument_pack;
8631 }
8632
8633 /* Returns the number of pack expansions in the template argument vector
8634 ARGS. */
8635
8636 static int
pack_expansion_args_count(tree args)8637 pack_expansion_args_count (tree args)
8638 {
8639 int i;
8640 int count = 0;
8641 if (args)
8642 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8643 {
8644 tree elt = TREE_VEC_ELT (args, i);
8645 if (elt && PACK_EXPANSION_P (elt))
8646 ++count;
8647 }
8648 return count;
8649 }
8650
8651 /* Convert all template arguments to their appropriate types, and
8652 return a vector containing the innermost resulting template
8653 arguments. If any error occurs, return error_mark_node. Error and
8654 warning messages are issued under control of COMPLAIN.
8655
8656 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8657 for arguments not specified in ARGS. Otherwise, if
8658 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8659 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8660 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8661 ARGS. */
8662
8663 static tree
coerce_template_parms(tree parms,tree args,tree in_decl,tsubst_flags_t complain,bool require_all_args,bool use_default_args)8664 coerce_template_parms (tree parms,
8665 tree args,
8666 tree in_decl,
8667 tsubst_flags_t complain,
8668 bool require_all_args,
8669 bool use_default_args)
8670 {
8671 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8672 tree orig_inner_args;
8673 tree inner_args;
8674 tree new_args;
8675 tree new_inner_args;
8676
8677 /* When used as a boolean value, indicates whether this is a
8678 variadic template parameter list. Since it's an int, we can also
8679 subtract it from nparms to get the number of non-variadic
8680 parameters. */
8681 int variadic_p = 0;
8682 int variadic_args_p = 0;
8683 int post_variadic_parms = 0;
8684
8685 /* Adjustment to nparms for fixed parameter packs. */
8686 int fixed_pack_adjust = 0;
8687 int fixed_packs = 0;
8688 int missing = 0;
8689
8690 /* Likewise for parameters with default arguments. */
8691 int default_p = 0;
8692
8693 if (args == error_mark_node)
8694 return error_mark_node;
8695
8696 nparms = TREE_VEC_LENGTH (parms);
8697
8698 /* Determine if there are any parameter packs or default arguments. */
8699 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8700 {
8701 tree parm = TREE_VEC_ELT (parms, parm_idx);
8702 if (variadic_p)
8703 ++post_variadic_parms;
8704 if (template_parameter_pack_p (TREE_VALUE (parm)))
8705 ++variadic_p;
8706 if (TREE_PURPOSE (parm))
8707 ++default_p;
8708 }
8709
8710 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8711 /* If there are no parameters that follow a parameter pack, we need to
8712 expand any argument packs so that we can deduce a parameter pack from
8713 some non-packed args followed by an argument pack, as in variadic85.C.
8714 If there are such parameters, we need to leave argument packs intact
8715 so the arguments are assigned properly. This can happen when dealing
8716 with a nested class inside a partial specialization of a class
8717 template, as in variadic92.C, or when deducing a template parameter pack
8718 from a sub-declarator, as in variadic114.C. */
8719 if (!post_variadic_parms)
8720 inner_args = expand_template_argument_pack (inner_args);
8721
8722 /* Count any pack expansion args. */
8723 variadic_args_p = pack_expansion_args_count (inner_args);
8724
8725 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8726 if ((nargs - variadic_args_p > nparms && !variadic_p)
8727 || (nargs < nparms - variadic_p
8728 && require_all_args
8729 && !variadic_args_p
8730 && (!use_default_args
8731 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8732 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8733 {
8734 bad_nargs:
8735 if (complain & tf_error)
8736 {
8737 if (variadic_p || default_p)
8738 {
8739 nparms -= variadic_p + default_p;
8740 error ("wrong number of template arguments "
8741 "(%d, should be at least %d)", nargs, nparms);
8742 }
8743 else
8744 error ("wrong number of template arguments "
8745 "(%d, should be %d)", nargs, nparms);
8746
8747 if (in_decl)
8748 inform (DECL_SOURCE_LOCATION (in_decl),
8749 "provided for %qD", in_decl);
8750 }
8751
8752 return error_mark_node;
8753 }
8754 /* We can't pass a pack expansion to a non-pack parameter of an alias
8755 template (DR 1430). */
8756 else if (in_decl
8757 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8758 || concept_definition_p (in_decl))
8759 && variadic_args_p
8760 && nargs - variadic_args_p < nparms - variadic_p)
8761 {
8762 if (complain & tf_error)
8763 {
8764 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8765 {
8766 tree arg = TREE_VEC_ELT (inner_args, i);
8767 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8768
8769 if (PACK_EXPANSION_P (arg)
8770 && !template_parameter_pack_p (parm))
8771 {
8772 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8773 error_at (location_of (arg),
8774 "pack expansion argument for non-pack parameter "
8775 "%qD of alias template %qD", parm, in_decl);
8776 else
8777 error_at (location_of (arg),
8778 "pack expansion argument for non-pack parameter "
8779 "%qD of concept %qD", parm, in_decl);
8780 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8781 goto found;
8782 }
8783 }
8784 gcc_unreachable ();
8785 found:;
8786 }
8787 return error_mark_node;
8788 }
8789
8790 /* We need to evaluate the template arguments, even though this
8791 template-id may be nested within a "sizeof". */
8792 cp_evaluated ev;
8793
8794 new_inner_args = make_tree_vec (nparms);
8795 new_args = add_outermost_template_args (args, new_inner_args);
8796 int pack_adjust = 0;
8797 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8798 {
8799 tree arg;
8800 tree parm;
8801
8802 /* Get the Ith template parameter. */
8803 parm = TREE_VEC_ELT (parms, parm_idx);
8804
8805 if (parm == error_mark_node)
8806 {
8807 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8808 continue;
8809 }
8810
8811 /* Calculate the next argument. */
8812 if (arg_idx < nargs)
8813 arg = TREE_VEC_ELT (inner_args, arg_idx);
8814 else
8815 arg = NULL_TREE;
8816
8817 if (template_parameter_pack_p (TREE_VALUE (parm))
8818 && (arg || require_all_args || !(complain & tf_partial))
8819 && !(arg && ARGUMENT_PACK_P (arg)))
8820 {
8821 /* Some arguments will be placed in the
8822 template parameter pack PARM. */
8823 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8824 inner_args, arg_idx,
8825 new_args, &lost,
8826 in_decl, complain);
8827
8828 if (arg == NULL_TREE)
8829 {
8830 /* We don't know how many args we have yet, just use the
8831 unconverted (and still packed) ones for now. */
8832 new_inner_args = orig_inner_args;
8833 arg_idx = nargs;
8834 break;
8835 }
8836
8837 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8838
8839 /* Store this argument. */
8840 if (arg == error_mark_node)
8841 {
8842 lost++;
8843 /* We are done with all of the arguments. */
8844 arg_idx = nargs;
8845 break;
8846 }
8847 else
8848 {
8849 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8850 arg_idx += pack_adjust;
8851 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8852 {
8853 ++fixed_packs;
8854 fixed_pack_adjust += pack_adjust;
8855 }
8856 }
8857
8858 continue;
8859 }
8860 else if (arg)
8861 {
8862 if (PACK_EXPANSION_P (arg))
8863 {
8864 /* "If every valid specialization of a variadic template
8865 requires an empty template parameter pack, the template is
8866 ill-formed, no diagnostic required." So check that the
8867 pattern works with this parameter. */
8868 tree pattern = PACK_EXPANSION_PATTERN (arg);
8869 tree conv = convert_template_argument (TREE_VALUE (parm),
8870 pattern, new_args,
8871 complain, parm_idx,
8872 in_decl);
8873 if (conv == error_mark_node)
8874 {
8875 if (complain & tf_error)
8876 inform (input_location, "so any instantiation with a "
8877 "non-empty parameter pack would be ill-formed");
8878 ++lost;
8879 }
8880 else if (TYPE_P (conv) && !TYPE_P (pattern))
8881 /* Recover from missing typename. */
8882 TREE_VEC_ELT (inner_args, arg_idx)
8883 = make_pack_expansion (conv, complain);
8884
8885 /* We don't know how many args we have yet, just
8886 use the unconverted ones for now. */
8887 new_inner_args = inner_args;
8888 arg_idx = nargs;
8889 break;
8890 }
8891 }
8892 else if (require_all_args)
8893 {
8894 /* There must be a default arg in this case. */
8895 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8896 complain, in_decl);
8897 /* The position of the first default template argument,
8898 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8899 Record that. */
8900 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8901 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8902 arg_idx - pack_adjust);
8903 }
8904 else
8905 break;
8906
8907 if (arg == error_mark_node)
8908 {
8909 if (complain & tf_error)
8910 error ("template argument %d is invalid", arg_idx + 1);
8911 }
8912 else if (!arg)
8913 {
8914 /* This can occur if there was an error in the template
8915 parameter list itself (which we would already have
8916 reported) that we are trying to recover from, e.g., a class
8917 template with a parameter list such as
8918 template<typename..., typename> (cpp0x/variadic150.C). */
8919 ++lost;
8920
8921 /* This can also happen with a fixed parameter pack (71834). */
8922 if (arg_idx >= nargs)
8923 ++missing;
8924 }
8925 else
8926 arg = convert_template_argument (TREE_VALUE (parm),
8927 arg, new_args, complain,
8928 parm_idx, in_decl);
8929
8930 if (arg == error_mark_node)
8931 lost++;
8932
8933 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8934 }
8935
8936 if (missing || arg_idx < nargs - variadic_args_p)
8937 {
8938 /* If we had fixed parameter packs, we didn't know how many arguments we
8939 actually needed earlier; now we do. */
8940 nparms += fixed_pack_adjust;
8941 variadic_p -= fixed_packs;
8942 goto bad_nargs;
8943 }
8944
8945 if (arg_idx < nargs)
8946 {
8947 /* We had some pack expansion arguments that will only work if the packs
8948 are empty, but wait until instantiation time to complain.
8949 See variadic-ttp3.C. */
8950
8951 /* Except that we can't provide empty packs to alias templates or
8952 concepts when there are no corresponding parameters. Basically,
8953 we can get here with this:
8954
8955 template<typename T> concept C = true;
8956
8957 template<typename... Args>
8958 requires C<Args...>
8959 void f();
8960
8961 When parsing C<Args...>, we try to form a concept check of
8962 C<?, Args...>. Without the extra check for substituting an empty
8963 pack past the last parameter, we can accept the check as valid.
8964
8965 FIXME: This may be valid for alias templates (but I doubt it).
8966
8967 FIXME: The error could be better also. */
8968 if (in_decl && concept_definition_p (in_decl))
8969 {
8970 if (complain & tf_error)
8971 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8972 "too many arguments");
8973 return error_mark_node;
8974 }
8975
8976 int len = nparms + (nargs - arg_idx);
8977 tree args = make_tree_vec (len);
8978 int i = 0;
8979 for (; i < nparms; ++i)
8980 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8981 for (; i < len; ++i, ++arg_idx)
8982 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8983 arg_idx - pack_adjust);
8984 new_inner_args = args;
8985 }
8986
8987 if (lost)
8988 {
8989 gcc_assert (!(complain & tf_error) || seen_error ());
8990 return error_mark_node;
8991 }
8992
8993 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8994 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8995 TREE_VEC_LENGTH (new_inner_args));
8996
8997 return new_inner_args;
8998 }
8999
9000 /* Convert all template arguments to their appropriate types, and
9001 return a vector containing the innermost resulting template
9002 arguments. If any error occurs, return error_mark_node. Error and
9003 warning messages are not issued.
9004
9005 Note that no function argument deduction is performed, and default
9006 arguments are used to fill in unspecified arguments. */
9007 tree
coerce_template_parms(tree parms,tree args,tree in_decl)9008 coerce_template_parms (tree parms, tree args, tree in_decl)
9009 {
9010 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9011 }
9012
9013 /* Convert all template arguments to their appropriate type, and
9014 instantiate default arguments as needed. This returns a vector
9015 containing the innermost resulting template arguments, or
9016 error_mark_node if unsuccessful. */
9017 tree
coerce_template_parms(tree parms,tree args,tree in_decl,tsubst_flags_t complain)9018 coerce_template_parms (tree parms, tree args, tree in_decl,
9019 tsubst_flags_t complain)
9020 {
9021 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9022 }
9023
9024 /* Like coerce_template_parms. If PARMS represents all template
9025 parameters levels, this function returns a vector of vectors
9026 representing all the resulting argument levels. Note that in this
9027 case, only the innermost arguments are coerced because the
9028 outermost ones are supposed to have been coerced already.
9029
9030 Otherwise, if PARMS represents only (the innermost) vector of
9031 parameters, this function returns a vector containing just the
9032 innermost resulting arguments. */
9033
9034 static tree
coerce_innermost_template_parms(tree parms,tree args,tree in_decl,tsubst_flags_t complain,bool require_all_args,bool use_default_args)9035 coerce_innermost_template_parms (tree parms,
9036 tree args,
9037 tree in_decl,
9038 tsubst_flags_t complain,
9039 bool require_all_args,
9040 bool use_default_args)
9041 {
9042 int parms_depth = TMPL_PARMS_DEPTH (parms);
9043 int args_depth = TMPL_ARGS_DEPTH (args);
9044 tree coerced_args;
9045
9046 if (parms_depth > 1)
9047 {
9048 coerced_args = make_tree_vec (parms_depth);
9049 tree level;
9050 int cur_depth;
9051
9052 for (level = parms, cur_depth = parms_depth;
9053 parms_depth > 0 && level != NULL_TREE;
9054 level = TREE_CHAIN (level), --cur_depth)
9055 {
9056 tree l;
9057 if (cur_depth == args_depth)
9058 l = coerce_template_parms (TREE_VALUE (level),
9059 args, in_decl, complain,
9060 require_all_args,
9061 use_default_args);
9062 else
9063 l = TMPL_ARGS_LEVEL (args, cur_depth);
9064
9065 if (l == error_mark_node)
9066 return error_mark_node;
9067
9068 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9069 }
9070 }
9071 else
9072 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9073 args, in_decl, complain,
9074 require_all_args,
9075 use_default_args);
9076 return coerced_args;
9077 }
9078
9079 /* Returns true if T is a wrapper to make a C++20 template parameter
9080 object const. */
9081
9082 static bool
class_nttp_const_wrapper_p(tree t)9083 class_nttp_const_wrapper_p (tree t)
9084 {
9085 if (cxx_dialect < cxx2a)
9086 return false;
9087 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9088 && CP_TYPE_CONST_P (TREE_TYPE (t))
9089 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9090 }
9091
9092 /* Returns 1 if template args OT and NT are equivalent. */
9093
9094 int
template_args_equal(tree ot,tree nt,bool partial_order)9095 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9096 {
9097 if (nt == ot)
9098 return 1;
9099 if (nt == NULL_TREE || ot == NULL_TREE)
9100 return false;
9101 if (nt == any_targ_node || ot == any_targ_node)
9102 return true;
9103
9104 if (class_nttp_const_wrapper_p (nt))
9105 nt = TREE_OPERAND (nt, 0);
9106 if (class_nttp_const_wrapper_p (ot))
9107 ot = TREE_OPERAND (ot, 0);
9108
9109 if (TREE_CODE (nt) == TREE_VEC)
9110 /* For member templates */
9111 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
9112 else if (PACK_EXPANSION_P (ot))
9113 return (PACK_EXPANSION_P (nt)
9114 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9115 PACK_EXPANSION_PATTERN (nt))
9116 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9117 PACK_EXPANSION_EXTRA_ARGS (nt)));
9118 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9119 return cp_tree_equal (ot, nt);
9120 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9121 gcc_unreachable ();
9122 else if (TYPE_P (nt))
9123 {
9124 if (!TYPE_P (ot))
9125 return false;
9126 /* Don't treat an alias template specialization with dependent
9127 arguments as equivalent to its underlying type when used as a
9128 template argument; we need them to be distinct so that we
9129 substitute into the specialization arguments at instantiation
9130 time. And aliases can't be equivalent without being ==, so
9131 we don't need to look any deeper.
9132
9133 During partial ordering, however, we need to treat them normally so
9134 that we can order uses of the same alias with different
9135 cv-qualification (79960). */
9136 if (!partial_order
9137 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9138 return false;
9139 else
9140 return same_type_p (ot, nt);
9141 }
9142 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
9143 return 0;
9144 else
9145 {
9146 /* Try to treat a template non-type argument that has been converted
9147 to the parameter type as equivalent to one that hasn't yet. */
9148 for (enum tree_code code1 = TREE_CODE (ot);
9149 CONVERT_EXPR_CODE_P (code1)
9150 || code1 == NON_LVALUE_EXPR;
9151 code1 = TREE_CODE (ot))
9152 ot = TREE_OPERAND (ot, 0);
9153 for (enum tree_code code2 = TREE_CODE (nt);
9154 CONVERT_EXPR_CODE_P (code2)
9155 || code2 == NON_LVALUE_EXPR;
9156 code2 = TREE_CODE (nt))
9157 nt = TREE_OPERAND (nt, 0);
9158
9159 return cp_tree_equal (ot, nt);
9160 }
9161 }
9162
9163 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9164 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9165 NEWARG_PTR with the offending arguments if they are non-NULL. */
9166
9167 int
comp_template_args(tree oldargs,tree newargs,tree * oldarg_ptr,tree * newarg_ptr,bool partial_order)9168 comp_template_args (tree oldargs, tree newargs,
9169 tree *oldarg_ptr, tree *newarg_ptr,
9170 bool partial_order)
9171 {
9172 int i;
9173
9174 if (oldargs == newargs)
9175 return 1;
9176
9177 if (!oldargs || !newargs)
9178 return 0;
9179
9180 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9181 return 0;
9182
9183 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9184 {
9185 tree nt = TREE_VEC_ELT (newargs, i);
9186 tree ot = TREE_VEC_ELT (oldargs, i);
9187
9188 if (! template_args_equal (ot, nt, partial_order))
9189 {
9190 if (oldarg_ptr != NULL)
9191 *oldarg_ptr = ot;
9192 if (newarg_ptr != NULL)
9193 *newarg_ptr = nt;
9194 return 0;
9195 }
9196 }
9197 return 1;
9198 }
9199
9200 inline bool
comp_template_args_porder(tree oargs,tree nargs)9201 comp_template_args_porder (tree oargs, tree nargs)
9202 {
9203 return comp_template_args (oargs, nargs, NULL, NULL, true);
9204 }
9205
9206 /* Implement a freelist interface for objects of type T.
9207
9208 Head is a separate object, rather than a regular member, so that we
9209 can define it as a GTY deletable pointer, which is highly
9210 desirable. A data member could be declared that way, but then the
9211 containing object would implicitly get GTY((user)), which would
9212 prevent us from instantiating freelists as global objects.
9213 Although this way we can create freelist global objects, they're
9214 such thin wrappers that instantiating temporaries at every use
9215 loses nothing and saves permanent storage for the freelist object.
9216
9217 Member functions next, anew, poison and reinit have default
9218 implementations that work for most of the types we're interested
9219 in, but if they don't work for some type, they should be explicitly
9220 specialized. See the comments before them for requirements, and
9221 the example specializations for the tree_list_freelist. */
9222 template <typename T>
9223 class freelist
9224 {
9225 /* Return the next object in a chain. We could just do type
9226 punning, but if we access the object with its underlying type, we
9227 avoid strict-aliasing trouble. This needs only work between
9228 poison and reinit. */
next(T * obj)9229 static T *&next (T *obj) { return obj->next; }
9230
9231 /* Return a newly allocated, uninitialized or minimally-initialized
9232 object of type T. Any initialization performed by anew should
9233 either remain across the life of the object and the execution of
9234 poison, or be redone by reinit. */
anew()9235 static T *anew () { return ggc_alloc<T> (); }
9236
9237 /* Optionally scribble all over the bits holding the object, so that
9238 they become (mostly?) uninitialized memory. This is called while
9239 preparing to make the object part of the free list. */
poison(T * obj)9240 static void poison (T *obj) {
9241 T *p ATTRIBUTE_UNUSED = obj;
9242 T **q ATTRIBUTE_UNUSED = &next (obj);
9243
9244 #ifdef ENABLE_GC_CHECKING
9245 /* Poison the data, to indicate the data is garbage. */
9246 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9247 memset (p, 0xa5, sizeof (*p));
9248 #endif
9249 /* Let valgrind know the object is free. */
9250 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9251
9252 /* Let valgrind know the next portion of the object is available,
9253 but uninitialized. */
9254 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9255 }
9256
9257 /* Bring an object that underwent at least one lifecycle after anew
9258 and before the most recent free and poison, back to a usable
9259 state, reinitializing whatever is needed for it to be
9260 functionally equivalent to an object just allocated and returned
9261 by anew. This may poison or clear the next field, used by
9262 freelist housekeeping after poison was called. */
reinit(T * obj)9263 static void reinit (T *obj) {
9264 T **q ATTRIBUTE_UNUSED = &next (obj);
9265
9266 #ifdef ENABLE_GC_CHECKING
9267 memset (q, 0xa5, sizeof (*q));
9268 #endif
9269 /* Let valgrind know the entire object is available, but
9270 uninitialized. */
9271 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9272 }
9273
9274 /* Reference a GTY-deletable pointer that points to the first object
9275 in the free list proper. */
9276 T *&head;
9277 public:
9278 /* Construct a freelist object chaining objects off of HEAD. */
freelist(T * & head)9279 freelist (T *&head) : head(head) {}
9280
9281 /* Add OBJ to the free object list. The former head becomes OBJ's
9282 successor. */
free(T * obj)9283 void free (T *obj)
9284 {
9285 poison (obj);
9286 next (obj) = head;
9287 head = obj;
9288 }
9289
9290 /* Take an object from the free list, if one is available, or
9291 allocate a new one. Objects taken from the free list should be
9292 regarded as filled with garbage, except for bits that are
9293 configured to be preserved across free and alloc. */
alloc()9294 T *alloc ()
9295 {
9296 if (head)
9297 {
9298 T *obj = head;
9299 head = next (head);
9300 reinit (obj);
9301 return obj;
9302 }
9303 else
9304 return anew ();
9305 }
9306 };
9307
9308 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9309 want to allocate a TREE_LIST using the usual interface, and ensure
9310 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9311 build_tree_list logic in reinit, so this could go out of sync. */
9312 template <>
9313 inline tree &
next(tree obj)9314 freelist<tree_node>::next (tree obj)
9315 {
9316 return TREE_CHAIN (obj);
9317 }
9318 template <>
9319 inline tree
anew()9320 freelist<tree_node>::anew ()
9321 {
9322 return build_tree_list (NULL, NULL);
9323 }
9324 template <>
9325 inline void
poison(tree obj ATTRIBUTE_UNUSED)9326 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9327 {
9328 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9329 tree p ATTRIBUTE_UNUSED = obj;
9330 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9331 tree *q ATTRIBUTE_UNUSED = &next (obj);
9332
9333 #ifdef ENABLE_GC_CHECKING
9334 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9335
9336 /* Poison the data, to indicate the data is garbage. */
9337 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9338 memset (p, 0xa5, size);
9339 #endif
9340 /* Let valgrind know the object is free. */
9341 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9342 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9343 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9344 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9345
9346 #ifdef ENABLE_GC_CHECKING
9347 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9348 /* Keep TREE_CHAIN functional. */
9349 TREE_SET_CODE (obj, TREE_LIST);
9350 #else
9351 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9352 #endif
9353 }
9354 template <>
9355 inline void
reinit(tree obj ATTRIBUTE_UNUSED)9356 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9357 {
9358 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9359
9360 #ifdef ENABLE_GC_CHECKING
9361 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9362 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9363 memset (obj, 0, sizeof (tree_list));
9364 #endif
9365
9366 /* Let valgrind know the entire object is available, but
9367 uninitialized. */
9368 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9369
9370 #ifdef ENABLE_GC_CHECKING
9371 TREE_SET_CODE (obj, TREE_LIST);
9372 #else
9373 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9374 #endif
9375 }
9376
9377 /* Point to the first object in the TREE_LIST freelist. */
9378 static GTY((deletable)) tree tree_list_freelist_head;
9379 /* Return the/an actual TREE_LIST freelist. */
9380 static inline freelist<tree_node>
tree_list_freelist()9381 tree_list_freelist ()
9382 {
9383 return tree_list_freelist_head;
9384 }
9385
9386 /* Point to the first object in the tinst_level freelist. */
9387 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9388 /* Return the/an actual tinst_level freelist. */
9389 static inline freelist<tinst_level>
tinst_level_freelist()9390 tinst_level_freelist ()
9391 {
9392 return tinst_level_freelist_head;
9393 }
9394
9395 /* Point to the first object in the pending_template freelist. */
9396 static GTY((deletable)) pending_template *pending_template_freelist_head;
9397 /* Return the/an actual pending_template freelist. */
9398 static inline freelist<pending_template>
pending_template_freelist()9399 pending_template_freelist ()
9400 {
9401 return pending_template_freelist_head;
9402 }
9403
9404 /* Build the TREE_LIST object out of a split list, store it
9405 permanently, and return it. */
9406 tree
to_list()9407 tinst_level::to_list ()
9408 {
9409 gcc_assert (split_list_p ());
9410 tree ret = tree_list_freelist ().alloc ();
9411 TREE_PURPOSE (ret) = tldcl;
9412 TREE_VALUE (ret) = targs;
9413 tldcl = ret;
9414 targs = NULL;
9415 gcc_assert (tree_list_p ());
9416 return ret;
9417 }
9418
9419 const unsigned short tinst_level::refcount_infinity;
9420
9421 /* Increment OBJ's refcount unless it is already infinite. */
9422 static tinst_level *
inc_refcount_use(tinst_level * obj)9423 inc_refcount_use (tinst_level *obj)
9424 {
9425 if (obj && obj->refcount != tinst_level::refcount_infinity)
9426 ++obj->refcount;
9427 return obj;
9428 }
9429
9430 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9431 void
free(tinst_level * obj)9432 tinst_level::free (tinst_level *obj)
9433 {
9434 if (obj->tree_list_p ())
9435 tree_list_freelist ().free (obj->get_node ());
9436 tinst_level_freelist ().free (obj);
9437 }
9438
9439 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9440 OBJ's DECL and OBJ, and start over with the tinst_level object that
9441 used to be referenced by OBJ's NEXT. */
9442 static void
dec_refcount_use(tinst_level * obj)9443 dec_refcount_use (tinst_level *obj)
9444 {
9445 while (obj
9446 && obj->refcount != tinst_level::refcount_infinity
9447 && !--obj->refcount)
9448 {
9449 tinst_level *next = obj->next;
9450 tinst_level::free (obj);
9451 obj = next;
9452 }
9453 }
9454
9455 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9456 and of the former PTR. Omitting the second argument is equivalent
9457 to passing (T*)NULL; this is allowed because passing the
9458 zero-valued integral constant NULL confuses type deduction and/or
9459 overload resolution. */
9460 template <typename T>
9461 static void
9462 set_refcount_ptr (T *& ptr, T *obj = NULL)
9463 {
9464 T *save = ptr;
9465 ptr = inc_refcount_use (obj);
9466 dec_refcount_use (save);
9467 }
9468
9469 static void
add_pending_template(tree d)9470 add_pending_template (tree d)
9471 {
9472 tree ti = (TYPE_P (d)
9473 ? CLASSTYPE_TEMPLATE_INFO (d)
9474 : DECL_TEMPLATE_INFO (d));
9475 struct pending_template *pt;
9476 int level;
9477
9478 if (TI_PENDING_TEMPLATE_FLAG (ti))
9479 return;
9480
9481 /* We are called both from instantiate_decl, where we've already had a
9482 tinst_level pushed, and instantiate_template, where we haven't.
9483 Compensate. */
9484 gcc_assert (TREE_CODE (d) != TREE_LIST);
9485 level = !current_tinst_level
9486 || current_tinst_level->maybe_get_node () != d;
9487
9488 if (level)
9489 push_tinst_level (d);
9490
9491 pt = pending_template_freelist ().alloc ();
9492 pt->next = NULL;
9493 pt->tinst = NULL;
9494 set_refcount_ptr (pt->tinst, current_tinst_level);
9495 if (last_pending_template)
9496 last_pending_template->next = pt;
9497 else
9498 pending_templates = pt;
9499
9500 last_pending_template = pt;
9501
9502 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9503
9504 if (level)
9505 pop_tinst_level ();
9506 }
9507
9508
9509 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9510 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9511 documentation for TEMPLATE_ID_EXPR. */
9512
9513 tree
lookup_template_function(tree fns,tree arglist)9514 lookup_template_function (tree fns, tree arglist)
9515 {
9516 if (fns == error_mark_node || arglist == error_mark_node)
9517 return error_mark_node;
9518
9519 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9520
9521 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9522 {
9523 error ("%q#D is not a function template", fns);
9524 return error_mark_node;
9525 }
9526
9527 if (BASELINK_P (fns))
9528 {
9529 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9530 unknown_type_node,
9531 BASELINK_FUNCTIONS (fns),
9532 arglist);
9533 return fns;
9534 }
9535
9536 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9537 }
9538
9539 /* Within the scope of a template class S<T>, the name S gets bound
9540 (in build_self_reference) to a TYPE_DECL for the class, not a
9541 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9542 or one of its enclosing classes, and that type is a template,
9543 return the associated TEMPLATE_DECL. Otherwise, the original
9544 DECL is returned.
9545
9546 Also handle the case when DECL is a TREE_LIST of ambiguous
9547 injected-class-names from different bases. */
9548
9549 tree
maybe_get_template_decl_from_type_decl(tree decl)9550 maybe_get_template_decl_from_type_decl (tree decl)
9551 {
9552 if (decl == NULL_TREE)
9553 return decl;
9554
9555 /* DR 176: A lookup that finds an injected-class-name (10.2
9556 [class.member.lookup]) can result in an ambiguity in certain cases
9557 (for example, if it is found in more than one base class). If all of
9558 the injected-class-names that are found refer to specializations of
9559 the same class template, and if the name is followed by a
9560 template-argument-list, the reference refers to the class template
9561 itself and not a specialization thereof, and is not ambiguous. */
9562 if (TREE_CODE (decl) == TREE_LIST)
9563 {
9564 tree t, tmpl = NULL_TREE;
9565 for (t = decl; t; t = TREE_CHAIN (t))
9566 {
9567 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9568 if (!tmpl)
9569 tmpl = elt;
9570 else if (tmpl != elt)
9571 break;
9572 }
9573 if (tmpl && t == NULL_TREE)
9574 return tmpl;
9575 else
9576 return decl;
9577 }
9578
9579 return (decl != NULL_TREE
9580 && DECL_SELF_REFERENCE_P (decl)
9581 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9582 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9583 }
9584
9585 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9586 parameters, find the desired type.
9587
9588 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9589
9590 IN_DECL, if non-NULL, is the template declaration we are trying to
9591 instantiate.
9592
9593 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9594 the class we are looking up.
9595
9596 Issue error and warning messages under control of COMPLAIN.
9597
9598 If the template class is really a local class in a template
9599 function, then the FUNCTION_CONTEXT is the function in which it is
9600 being instantiated.
9601
9602 ??? Note that this function is currently called *twice* for each
9603 template-id: the first time from the parser, while creating the
9604 incomplete type (finish_template_type), and the second type during the
9605 real instantiation (instantiate_template_class). This is surely something
9606 that we want to avoid. It also causes some problems with argument
9607 coercion (see convert_nontype_argument for more information on this). */
9608
9609 static tree
lookup_template_class_1(tree d1,tree arglist,tree in_decl,tree context,int entering_scope,tsubst_flags_t complain)9610 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9611 int entering_scope, tsubst_flags_t complain)
9612 {
9613 tree templ = NULL_TREE, parmlist;
9614 tree t;
9615 spec_entry **slot;
9616 spec_entry *entry;
9617 spec_entry elt;
9618 hashval_t hash;
9619
9620 if (identifier_p (d1))
9621 {
9622 tree value = innermost_non_namespace_value (d1);
9623 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9624 templ = value;
9625 else
9626 {
9627 if (context)
9628 push_decl_namespace (context);
9629 templ = lookup_name (d1);
9630 templ = maybe_get_template_decl_from_type_decl (templ);
9631 if (context)
9632 pop_decl_namespace ();
9633 }
9634 if (templ)
9635 context = DECL_CONTEXT (templ);
9636 }
9637 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9638 {
9639 tree type = TREE_TYPE (d1);
9640
9641 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9642 an implicit typename for the second A. Deal with it. */
9643 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9644 type = TREE_TYPE (type);
9645
9646 if (CLASSTYPE_TEMPLATE_INFO (type))
9647 {
9648 templ = CLASSTYPE_TI_TEMPLATE (type);
9649 d1 = DECL_NAME (templ);
9650 }
9651 }
9652 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9653 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9654 {
9655 templ = TYPE_TI_TEMPLATE (d1);
9656 d1 = DECL_NAME (templ);
9657 }
9658 else if (DECL_TYPE_TEMPLATE_P (d1))
9659 {
9660 templ = d1;
9661 d1 = DECL_NAME (templ);
9662 context = DECL_CONTEXT (templ);
9663 }
9664 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9665 {
9666 templ = d1;
9667 d1 = DECL_NAME (templ);
9668 }
9669
9670 /* Issue an error message if we didn't find a template. */
9671 if (! templ)
9672 {
9673 if (complain & tf_error)
9674 error ("%qT is not a template", d1);
9675 return error_mark_node;
9676 }
9677
9678 if (TREE_CODE (templ) != TEMPLATE_DECL
9679 /* Make sure it's a user visible template, if it was named by
9680 the user. */
9681 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9682 && !PRIMARY_TEMPLATE_P (templ)))
9683 {
9684 if (complain & tf_error)
9685 {
9686 error ("non-template type %qT used as a template", d1);
9687 if (in_decl)
9688 error ("for template declaration %q+D", in_decl);
9689 }
9690 return error_mark_node;
9691 }
9692
9693 complain &= ~tf_user;
9694
9695 /* An alias that just changes the name of a template is equivalent to the
9696 other template, so if any of the arguments are pack expansions, strip
9697 the alias to avoid problems with a pack expansion passed to a non-pack
9698 alias template parameter (DR 1430). */
9699 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9700 templ = get_underlying_template (templ);
9701
9702 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9703 {
9704 tree parm;
9705 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9706 if (arglist2 == error_mark_node
9707 || (!uses_template_parms (arglist2)
9708 && check_instantiated_args (templ, arglist2, complain)))
9709 return error_mark_node;
9710
9711 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9712 return parm;
9713 }
9714 else
9715 {
9716 tree template_type = TREE_TYPE (templ);
9717 tree gen_tmpl;
9718 tree type_decl;
9719 tree found = NULL_TREE;
9720 int arg_depth;
9721 int parm_depth;
9722 int is_dependent_type;
9723 int use_partial_inst_tmpl = false;
9724
9725 if (template_type == error_mark_node)
9726 /* An error occurred while building the template TEMPL, and a
9727 diagnostic has most certainly been emitted for that
9728 already. Let's propagate that error. */
9729 return error_mark_node;
9730
9731 gen_tmpl = most_general_template (templ);
9732 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9733 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9734 arg_depth = TMPL_ARGS_DEPTH (arglist);
9735
9736 if (arg_depth == 1 && parm_depth > 1)
9737 {
9738 /* We've been given an incomplete set of template arguments.
9739 For example, given:
9740
9741 template <class T> struct S1 {
9742 template <class U> struct S2 {};
9743 template <class U> struct S2<U*> {};
9744 };
9745
9746 we will be called with an ARGLIST of `U*', but the
9747 TEMPLATE will be `template <class T> template
9748 <class U> struct S1<T>::S2'. We must fill in the missing
9749 arguments. */
9750 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9751 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9752 arg_depth = TMPL_ARGS_DEPTH (arglist);
9753 }
9754
9755 /* Now we should have enough arguments. */
9756 gcc_assert (parm_depth == arg_depth);
9757
9758 /* From here on, we're only interested in the most general
9759 template. */
9760
9761 /* Calculate the BOUND_ARGS. These will be the args that are
9762 actually tsubst'd into the definition to create the
9763 instantiation. */
9764 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9765 complain,
9766 /*require_all_args=*/true,
9767 /*use_default_args=*/true);
9768
9769 if (arglist == error_mark_node)
9770 /* We were unable to bind the arguments. */
9771 return error_mark_node;
9772
9773 /* In the scope of a template class, explicit references to the
9774 template class refer to the type of the template, not any
9775 instantiation of it. For example, in:
9776
9777 template <class T> class C { void f(C<T>); }
9778
9779 the `C<T>' is just the same as `C'. Outside of the
9780 class, however, such a reference is an instantiation. */
9781 if (entering_scope
9782 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9783 || currently_open_class (template_type))
9784 {
9785 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9786
9787 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9788 return template_type;
9789 }
9790
9791 /* If we already have this specialization, return it. */
9792 elt.tmpl = gen_tmpl;
9793 elt.args = arglist;
9794 elt.spec = NULL_TREE;
9795 hash = spec_hasher::hash (&elt);
9796 entry = type_specializations->find_with_hash (&elt, hash);
9797
9798 if (entry)
9799 return entry->spec;
9800
9801 /* If the template's constraints are not satisfied,
9802 then we cannot form a valid type.
9803
9804 Note that the check is deferred until after the hash
9805 lookup. This prevents redundant checks on previously
9806 instantiated specializations. */
9807 if (flag_concepts
9808 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9809 && !constraints_satisfied_p (gen_tmpl, arglist))
9810 {
9811 if (complain & tf_error)
9812 {
9813 auto_diagnostic_group d;
9814 error ("template constraint failure for %qD", gen_tmpl);
9815 diagnose_constraints (input_location, gen_tmpl, arglist);
9816 }
9817 return error_mark_node;
9818 }
9819
9820 is_dependent_type = uses_template_parms (arglist);
9821
9822 /* If the deduced arguments are invalid, then the binding
9823 failed. */
9824 if (!is_dependent_type
9825 && check_instantiated_args (gen_tmpl,
9826 INNERMOST_TEMPLATE_ARGS (arglist),
9827 complain))
9828 return error_mark_node;
9829
9830 if (!is_dependent_type
9831 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9832 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9833 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9834 {
9835 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9836 DECL_NAME (gen_tmpl),
9837 /*tag_scope=*/ts_global);
9838 return found;
9839 }
9840
9841 context = DECL_CONTEXT (gen_tmpl);
9842 if (context && TYPE_P (context))
9843 {
9844 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9845 context = complete_type (context);
9846 }
9847 else
9848 context = tsubst (context, arglist, complain, in_decl);
9849
9850 if (context == error_mark_node)
9851 return error_mark_node;
9852
9853 if (!context)
9854 context = global_namespace;
9855
9856 /* Create the type. */
9857 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9858 {
9859 /* The user referred to a specialization of an alias
9860 template represented by GEN_TMPL.
9861
9862 [temp.alias]/2 says:
9863
9864 When a template-id refers to the specialization of an
9865 alias template, it is equivalent to the associated
9866 type obtained by substitution of its
9867 template-arguments for the template-parameters in the
9868 type-id of the alias template. */
9869
9870 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9871 /* Note that the call above (by indirectly calling
9872 register_specialization in tsubst_decl) registers the
9873 TYPE_DECL representing the specialization of the alias
9874 template. So next time someone substitutes ARGLIST for
9875 the template parms into the alias template (GEN_TMPL),
9876 she'll get that TYPE_DECL back. */
9877
9878 if (t == error_mark_node)
9879 return t;
9880 }
9881 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9882 {
9883 if (!is_dependent_type)
9884 {
9885 set_current_access_from_decl (TYPE_NAME (template_type));
9886 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9887 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9888 arglist, complain, in_decl),
9889 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9890 arglist, complain, in_decl),
9891 SCOPED_ENUM_P (template_type), NULL);
9892
9893 if (t == error_mark_node)
9894 return t;
9895 }
9896 else
9897 {
9898 /* We don't want to call start_enum for this type, since
9899 the values for the enumeration constants may involve
9900 template parameters. And, no one should be interested
9901 in the enumeration constants for such a type. */
9902 t = cxx_make_type (ENUMERAL_TYPE);
9903 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9904 }
9905 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9906 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9907 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9908 }
9909 else if (CLASS_TYPE_P (template_type))
9910 {
9911 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9912 instantiated here. */
9913 gcc_assert (!LAMBDA_TYPE_P (template_type));
9914
9915 t = make_class_type (TREE_CODE (template_type));
9916 CLASSTYPE_DECLARED_CLASS (t)
9917 = CLASSTYPE_DECLARED_CLASS (template_type);
9918 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9919
9920 /* A local class. Make sure the decl gets registered properly. */
9921 if (context == current_function_decl)
9922 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9923 == error_mark_node)
9924 return error_mark_node;
9925
9926 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9927 /* This instantiation is another name for the primary
9928 template type. Set the TYPE_CANONICAL field
9929 appropriately. */
9930 TYPE_CANONICAL (t) = template_type;
9931 else if (any_template_arguments_need_structural_equality_p (arglist))
9932 /* Some of the template arguments require structural
9933 equality testing, so this template class requires
9934 structural equality testing. */
9935 SET_TYPE_STRUCTURAL_EQUALITY (t);
9936 }
9937 else
9938 gcc_unreachable ();
9939
9940 /* If we called start_enum or pushtag above, this information
9941 will already be set up. */
9942 if (!TYPE_NAME (t))
9943 {
9944 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9945
9946 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9947 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9948 DECL_SOURCE_LOCATION (type_decl)
9949 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9950 }
9951 else
9952 type_decl = TYPE_NAME (t);
9953
9954 if (CLASS_TYPE_P (template_type))
9955 {
9956 TREE_PRIVATE (type_decl)
9957 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9958 TREE_PROTECTED (type_decl)
9959 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9960 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9961 {
9962 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9963 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9964 }
9965 }
9966
9967 if (OVERLOAD_TYPE_P (t)
9968 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9969 {
9970 static const char *tags[] = {"abi_tag", "may_alias"};
9971
9972 for (unsigned ix = 0; ix != 2; ix++)
9973 {
9974 tree attributes
9975 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9976
9977 if (attributes)
9978 TYPE_ATTRIBUTES (t)
9979 = tree_cons (TREE_PURPOSE (attributes),
9980 TREE_VALUE (attributes),
9981 TYPE_ATTRIBUTES (t));
9982 }
9983 }
9984
9985 /* Let's consider the explicit specialization of a member
9986 of a class template specialization that is implicitly instantiated,
9987 e.g.:
9988 template<class T>
9989 struct S
9990 {
9991 template<class U> struct M {}; //#0
9992 };
9993
9994 template<>
9995 template<>
9996 struct S<int>::M<char> //#1
9997 {
9998 int i;
9999 };
10000 [temp.expl.spec]/4 says this is valid.
10001
10002 In this case, when we write:
10003 S<int>::M<char> m;
10004
10005 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10006 the one of #0.
10007
10008 When we encounter #1, we want to store the partial instantiation
10009 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10010
10011 For all cases other than this "explicit specialization of member of a
10012 class template", we just want to store the most general template into
10013 the CLASSTYPE_TI_TEMPLATE of M.
10014
10015 This case of "explicit specialization of member of a class template"
10016 only happens when:
10017 1/ the enclosing class is an instantiation of, and therefore not
10018 the same as, the context of the most general template, and
10019 2/ we aren't looking at the partial instantiation itself, i.e.
10020 the innermost arguments are not the same as the innermost parms of
10021 the most general template.
10022
10023 So it's only when 1/ and 2/ happens that we want to use the partial
10024 instantiation of the member template in lieu of its most general
10025 template. */
10026
10027 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10028 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10029 /* the enclosing class must be an instantiation... */
10030 && CLASS_TYPE_P (context)
10031 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10032 {
10033 TREE_VEC_LENGTH (arglist)--;
10034 ++processing_template_decl;
10035 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10036 tree partial_inst_args =
10037 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10038 arglist, complain, NULL_TREE);
10039 --processing_template_decl;
10040 TREE_VEC_LENGTH (arglist)++;
10041 if (partial_inst_args == error_mark_node)
10042 return error_mark_node;
10043 use_partial_inst_tmpl =
10044 /*...and we must not be looking at the partial instantiation
10045 itself. */
10046 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10047 partial_inst_args);
10048 }
10049
10050 if (!use_partial_inst_tmpl)
10051 /* This case is easy; there are no member templates involved. */
10052 found = gen_tmpl;
10053 else
10054 {
10055 /* This is a full instantiation of a member template. Find
10056 the partial instantiation of which this is an instance. */
10057
10058 /* Temporarily reduce by one the number of levels in the ARGLIST
10059 so as to avoid comparing the last set of arguments. */
10060 TREE_VEC_LENGTH (arglist)--;
10061 /* We don't use COMPLAIN in the following call because this isn't
10062 the immediate context of deduction. For instance, tf_partial
10063 could be set here as we might be at the beginning of template
10064 argument deduction when any explicitly specified template
10065 arguments are substituted into the function type. tf_partial
10066 could lead into trouble because we wouldn't find the partial
10067 instantiation that might have been created outside tf_partial
10068 context, because the levels of template parameters wouldn't
10069 match, because in a tf_partial context, tsubst doesn't reduce
10070 TEMPLATE_PARM_LEVEL. */
10071 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
10072 TREE_VEC_LENGTH (arglist)++;
10073 /* FOUND is either a proper class type, or an alias
10074 template specialization. In the later case, it's a
10075 TYPE_DECL, resulting from the substituting of arguments
10076 for parameters in the TYPE_DECL of the alias template
10077 done earlier. So be careful while getting the template
10078 of FOUND. */
10079 found = (TREE_CODE (found) == TEMPLATE_DECL
10080 ? found
10081 : (TREE_CODE (found) == TYPE_DECL
10082 ? DECL_TI_TEMPLATE (found)
10083 : CLASSTYPE_TI_TEMPLATE (found)));
10084
10085 if (DECL_CLASS_TEMPLATE_P (found)
10086 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10087 {
10088 /* If this partial instantiation is specialized, we want to
10089 use it for hash table lookup. */
10090 elt.tmpl = found;
10091 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10092 hash = spec_hasher::hash (&elt);
10093 }
10094 }
10095
10096 // Build template info for the new specialization.
10097 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10098
10099 elt.spec = t;
10100 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10101 gcc_checking_assert (*slot == NULL);
10102 entry = ggc_alloc<spec_entry> ();
10103 *entry = elt;
10104 *slot = entry;
10105
10106 /* Note this use of the partial instantiation so we can check it
10107 later in maybe_process_partial_specialization. */
10108 DECL_TEMPLATE_INSTANTIATIONS (found)
10109 = tree_cons (arglist, t,
10110 DECL_TEMPLATE_INSTANTIATIONS (found));
10111
10112 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10113 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10114 /* Now that the type has been registered on the instantiations
10115 list, we set up the enumerators. Because the enumeration
10116 constants may involve the enumeration type itself, we make
10117 sure to register the type first, and then create the
10118 constants. That way, doing tsubst_expr for the enumeration
10119 constants won't result in recursive calls here; we'll find
10120 the instantiation and exit above. */
10121 tsubst_enum (template_type, t, arglist);
10122
10123 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10124 /* If the type makes use of template parameters, the
10125 code that generates debugging information will crash. */
10126 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10127
10128 /* Possibly limit visibility based on template args. */
10129 TREE_PUBLIC (type_decl) = 1;
10130 determine_visibility (type_decl);
10131
10132 inherit_targ_abi_tags (t);
10133
10134 return t;
10135 }
10136 }
10137
10138 /* Wrapper for lookup_template_class_1. */
10139
10140 tree
lookup_template_class(tree d1,tree arglist,tree in_decl,tree context,int entering_scope,tsubst_flags_t complain)10141 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10142 int entering_scope, tsubst_flags_t complain)
10143 {
10144 tree ret;
10145 timevar_push (TV_TEMPLATE_INST);
10146 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10147 entering_scope, complain);
10148 timevar_pop (TV_TEMPLATE_INST);
10149 return ret;
10150 }
10151
10152 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10153
10154 tree
lookup_template_variable(tree templ,tree arglist)10155 lookup_template_variable (tree templ, tree arglist)
10156 {
10157 if (flag_concepts && variable_concept_p (templ))
10158 return build_concept_check (templ, arglist, tf_none);
10159
10160 /* The type of the expression is NULL_TREE since the template-id could refer
10161 to an explicit or partial specialization. */
10162 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10163 }
10164
10165 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10166
10167 tree
finish_template_variable(tree var,tsubst_flags_t complain)10168 finish_template_variable (tree var, tsubst_flags_t complain)
10169 {
10170 tree templ = TREE_OPERAND (var, 0);
10171 tree arglist = TREE_OPERAND (var, 1);
10172
10173 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10174 arglist = add_outermost_template_args (tmpl_args, arglist);
10175
10176 templ = most_general_template (templ);
10177 tree parms = DECL_TEMPLATE_PARMS (templ);
10178 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10179 /*req_all*/true,
10180 /*use_default*/true);
10181
10182 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10183 {
10184 if (complain & tf_error)
10185 {
10186 auto_diagnostic_group d;
10187 error ("use of invalid variable template %qE", var);
10188 diagnose_constraints (location_of (var), templ, arglist);
10189 }
10190 return error_mark_node;
10191 }
10192
10193 return instantiate_template (templ, arglist, complain);
10194 }
10195
10196 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10197 TARGS template args, and instantiate it if it's not dependent. */
10198
10199 tree
lookup_and_finish_template_variable(tree templ,tree targs,tsubst_flags_t complain)10200 lookup_and_finish_template_variable (tree templ, tree targs,
10201 tsubst_flags_t complain)
10202 {
10203 templ = lookup_template_variable (templ, targs);
10204 if (!any_dependent_template_arguments_p (targs))
10205 {
10206 templ = finish_template_variable (templ, complain);
10207 mark_used (templ);
10208 }
10209
10210 return convert_from_reference (templ);
10211 }
10212
10213
10214 struct pair_fn_data
10215 {
10216 tree_fn_t fn;
10217 tree_fn_t any_fn;
10218 void *data;
10219 /* True when we should also visit template parameters that occur in
10220 non-deduced contexts. */
10221 bool include_nondeduced_p;
10222 hash_set<tree> *visited;
10223 };
10224
10225 /* Called from for_each_template_parm via walk_tree. */
10226
10227 static tree
for_each_template_parm_r(tree * tp,int * walk_subtrees,void * d)10228 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10229 {
10230 tree t = *tp;
10231 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10232 tree_fn_t fn = pfd->fn;
10233 void *data = pfd->data;
10234 tree result = NULL_TREE;
10235
10236 #define WALK_SUBTREE(NODE) \
10237 do \
10238 { \
10239 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10240 pfd->include_nondeduced_p, \
10241 pfd->any_fn); \
10242 if (result) goto out; \
10243 } \
10244 while (0)
10245
10246 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10247 return t;
10248
10249 if (TYPE_P (t)
10250 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10251 WALK_SUBTREE (TYPE_CONTEXT (t));
10252
10253 switch (TREE_CODE (t))
10254 {
10255 case RECORD_TYPE:
10256 if (TYPE_PTRMEMFUNC_P (t))
10257 break;
10258 /* Fall through. */
10259
10260 case UNION_TYPE:
10261 case ENUMERAL_TYPE:
10262 if (!TYPE_TEMPLATE_INFO (t))
10263 *walk_subtrees = 0;
10264 else
10265 WALK_SUBTREE (TYPE_TI_ARGS (t));
10266 break;
10267
10268 case INTEGER_TYPE:
10269 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10270 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10271 break;
10272
10273 case METHOD_TYPE:
10274 /* Since we're not going to walk subtrees, we have to do this
10275 explicitly here. */
10276 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10277 /* Fall through. */
10278
10279 case FUNCTION_TYPE:
10280 /* Check the return type. */
10281 WALK_SUBTREE (TREE_TYPE (t));
10282
10283 /* Check the parameter types. Since default arguments are not
10284 instantiated until they are needed, the TYPE_ARG_TYPES may
10285 contain expressions that involve template parameters. But,
10286 no-one should be looking at them yet. And, once they're
10287 instantiated, they don't contain template parameters, so
10288 there's no point in looking at them then, either. */
10289 {
10290 tree parm;
10291
10292 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10293 WALK_SUBTREE (TREE_VALUE (parm));
10294
10295 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10296 want walk_tree walking into them itself. */
10297 *walk_subtrees = 0;
10298 }
10299
10300 if (flag_noexcept_type)
10301 {
10302 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10303 if (spec)
10304 WALK_SUBTREE (TREE_PURPOSE (spec));
10305 }
10306 break;
10307
10308 case TYPEOF_TYPE:
10309 case DECLTYPE_TYPE:
10310 case UNDERLYING_TYPE:
10311 if (pfd->include_nondeduced_p
10312 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10313 pfd->visited,
10314 pfd->include_nondeduced_p,
10315 pfd->any_fn))
10316 return error_mark_node;
10317 *walk_subtrees = false;
10318 break;
10319
10320 case FUNCTION_DECL:
10321 case VAR_DECL:
10322 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10323 WALK_SUBTREE (DECL_TI_ARGS (t));
10324 /* Fall through. */
10325
10326 case PARM_DECL:
10327 case CONST_DECL:
10328 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10329 WALK_SUBTREE (DECL_INITIAL (t));
10330 if (DECL_CONTEXT (t)
10331 && pfd->include_nondeduced_p)
10332 WALK_SUBTREE (DECL_CONTEXT (t));
10333 break;
10334
10335 case BOUND_TEMPLATE_TEMPLATE_PARM:
10336 /* Record template parameters such as `T' inside `TT<T>'. */
10337 WALK_SUBTREE (TYPE_TI_ARGS (t));
10338 /* Fall through. */
10339
10340 case TEMPLATE_TEMPLATE_PARM:
10341 case TEMPLATE_TYPE_PARM:
10342 case TEMPLATE_PARM_INDEX:
10343 if (fn && (*fn)(t, data))
10344 return t;
10345 else if (!fn)
10346 return t;
10347 break;
10348
10349 case TEMPLATE_DECL:
10350 /* A template template parameter is encountered. */
10351 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10352 WALK_SUBTREE (TREE_TYPE (t));
10353
10354 /* Already substituted template template parameter */
10355 *walk_subtrees = 0;
10356 break;
10357
10358 case TYPENAME_TYPE:
10359 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10360 partial instantiation. */
10361 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10362 break;
10363
10364 case CONSTRUCTOR:
10365 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10366 && pfd->include_nondeduced_p)
10367 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10368 break;
10369
10370 case INDIRECT_REF:
10371 case COMPONENT_REF:
10372 /* If there's no type, then this thing must be some expression
10373 involving template parameters. */
10374 if (!fn && !TREE_TYPE (t))
10375 return error_mark_node;
10376 break;
10377
10378 case MODOP_EXPR:
10379 case CAST_EXPR:
10380 case IMPLICIT_CONV_EXPR:
10381 case REINTERPRET_CAST_EXPR:
10382 case CONST_CAST_EXPR:
10383 case STATIC_CAST_EXPR:
10384 case DYNAMIC_CAST_EXPR:
10385 case ARROW_EXPR:
10386 case DOTSTAR_EXPR:
10387 case TYPEID_EXPR:
10388 case PSEUDO_DTOR_EXPR:
10389 if (!fn)
10390 return error_mark_node;
10391 break;
10392
10393 case SCOPE_REF:
10394 if (pfd->include_nondeduced_p)
10395 WALK_SUBTREE (TREE_OPERAND (t, 0));
10396 break;
10397
10398 case REQUIRES_EXPR:
10399 {
10400 if (!fn)
10401 return error_mark_node;
10402
10403 /* Recursively walk the type of each constraint variable. */
10404 tree p = TREE_OPERAND (t, 0);
10405 while (p)
10406 {
10407 WALK_SUBTREE (TREE_TYPE (p));
10408 p = TREE_CHAIN (p);
10409 }
10410 }
10411 break;
10412
10413 default:
10414 break;
10415 }
10416
10417 #undef WALK_SUBTREE
10418
10419 /* We didn't find any template parameters we liked. */
10420 out:
10421 return result;
10422 }
10423
10424 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10425 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10426 call FN with the parameter and the DATA.
10427 If FN returns nonzero, the iteration is terminated, and
10428 for_each_template_parm returns 1. Otherwise, the iteration
10429 continues. If FN never returns a nonzero value, the value
10430 returned by for_each_template_parm is 0. If FN is NULL, it is
10431 considered to be the function which always returns 1.
10432
10433 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10434 parameters that occur in non-deduced contexts. When false, only
10435 visits those template parameters that can be deduced. */
10436
10437 static tree
for_each_template_parm(tree t,tree_fn_t fn,void * data,hash_set<tree> * visited,bool include_nondeduced_p,tree_fn_t any_fn)10438 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10439 hash_set<tree> *visited,
10440 bool include_nondeduced_p,
10441 tree_fn_t any_fn)
10442 {
10443 struct pair_fn_data pfd;
10444 tree result;
10445
10446 /* Set up. */
10447 pfd.fn = fn;
10448 pfd.any_fn = any_fn;
10449 pfd.data = data;
10450 pfd.include_nondeduced_p = include_nondeduced_p;
10451
10452 /* Walk the tree. (Conceptually, we would like to walk without
10453 duplicates, but for_each_template_parm_r recursively calls
10454 for_each_template_parm, so we would need to reorganize a fair
10455 bit to use walk_tree_without_duplicates, so we keep our own
10456 visited list.) */
10457 if (visited)
10458 pfd.visited = visited;
10459 else
10460 pfd.visited = new hash_set<tree>;
10461 result = cp_walk_tree (&t,
10462 for_each_template_parm_r,
10463 &pfd,
10464 pfd.visited);
10465
10466 /* Clean up. */
10467 if (!visited)
10468 {
10469 delete pfd.visited;
10470 pfd.visited = 0;
10471 }
10472
10473 return result;
10474 }
10475
10476 struct find_template_parameter_info
10477 {
find_template_parameter_infofind_template_parameter_info10478 explicit find_template_parameter_info (tree ctx_parms)
10479 : parm_list (NULL_TREE),
10480 ctx_parms (ctx_parms),
10481 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10482 {}
10483
10484 hash_set<tree> visited;
10485 hash_set<tree> parms;
10486 tree parm_list;
10487 tree ctx_parms;
10488 int max_depth;
10489 };
10490
10491 /* Appends the declaration of T to the list in DATA. */
10492
10493 static int
keep_template_parm(tree t,void * data)10494 keep_template_parm (tree t, void* data)
10495 {
10496 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10497
10498 /* Template parameters declared within the expression are not part of
10499 the parameter mapping. For example, in this concept:
10500
10501 template<typename T>
10502 concept C = requires { <expr> } -> same_as<int>;
10503
10504 the return specifier same_as<int> declares a new decltype parameter
10505 that must not be part of the parameter mapping. The same is true
10506 for generic lambda parameters, lambda template parameters, etc. */
10507 int level;
10508 int index;
10509 template_parm_level_and_index (t, &level, &index);
10510 if (level > ftpi->max_depth)
10511 return 0;
10512
10513 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10514 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10515 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10516 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10517
10518 /* Arguments like const T yield parameters like const T. This means that
10519 a template-id like X<T, const T> would yield two distinct parameters:
10520 T and const T. Adjust types to their unqualified versions. */
10521 if (TYPE_P (t))
10522 t = TYPE_MAIN_VARIANT (t);
10523 if (!ftpi->parms.add (t))
10524 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10525
10526 return 0;
10527 }
10528
10529 /* Ensure that we recursively examine certain terms that are not normally
10530 visited in for_each_template_parm_r. */
10531
10532 static int
any_template_parm_r(tree t,void * data)10533 any_template_parm_r (tree t, void *data)
10534 {
10535 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10536
10537 #define WALK_SUBTREE(NODE) \
10538 do \
10539 { \
10540 for_each_template_parm (NODE, keep_template_parm, data, \
10541 &ftpi->visited, true, \
10542 any_template_parm_r); \
10543 } \
10544 while (0)
10545
10546 /* A mention of a member alias/typedef is a use of all of its template
10547 arguments, including those from the enclosing class, so we don't use
10548 alias_template_specialization_p here. */
10549 if (TYPE_P (t) && typedef_variant_p (t))
10550 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10551 WALK_SUBTREE (TI_ARGS (tinfo));
10552
10553 switch (TREE_CODE (t))
10554 {
10555 case TEMPLATE_TYPE_PARM:
10556 /* Type constraints of a placeholder type may contain parameters. */
10557 if (is_auto (t))
10558 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10559 WALK_SUBTREE (constr);
10560 break;
10561
10562 case TEMPLATE_ID_EXPR:
10563 /* Search through references to variable templates. */
10564 WALK_SUBTREE (TREE_OPERAND (t, 0));
10565 WALK_SUBTREE (TREE_OPERAND (t, 1));
10566 break;
10567
10568 case TEMPLATE_PARM_INDEX:
10569 case PARM_DECL:
10570 /* A parameter or constraint variable may also depend on a template
10571 parameter without explicitly naming it. */
10572 WALK_SUBTREE (TREE_TYPE (t));
10573 break;
10574
10575 case TEMPLATE_DECL:
10576 {
10577 /* If T is a member template that shares template parameters with
10578 ctx_parms, we need to mark all those parameters for mapping. */
10579 tree dparms = DECL_TEMPLATE_PARMS (t);
10580 tree cparms = ftpi->ctx_parms;
10581 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10582 dparms = TREE_CHAIN (dparms);
10583 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10584 cparms = TREE_CHAIN (cparms);
10585 while (dparms
10586 && (TREE_TYPE (TREE_VALUE (dparms))
10587 != TREE_TYPE (TREE_VALUE (cparms))))
10588 dparms = TREE_CHAIN (dparms),
10589 cparms = TREE_CHAIN (cparms);
10590 if (dparms)
10591 {
10592 int ddepth = TMPL_PARMS_DEPTH (dparms);
10593 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10594 for (int i = 0; i < ddepth; ++i)
10595 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10596 }
10597 }
10598 break;
10599
10600 case LAMBDA_EXPR:
10601 {
10602 /* Look in the parms and body. */
10603 tree fn = lambda_function (t);
10604 WALK_SUBTREE (TREE_TYPE (fn));
10605 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10606 }
10607 break;
10608
10609 case IDENTIFIER_NODE:
10610 if (IDENTIFIER_CONV_OP_P (t))
10611 /* The conversion-type-id of a conversion operator may be dependent. */
10612 WALK_SUBTREE (TREE_TYPE (t));
10613 break;
10614
10615 default:
10616 break;
10617 }
10618
10619 /* Keep walking. */
10620 return 0;
10621 }
10622
10623 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10624 are the template parameters in scope. */
10625
10626 tree
find_template_parameters(tree t,tree ctx_parms)10627 find_template_parameters (tree t, tree ctx_parms)
10628 {
10629 if (!ctx_parms)
10630 return NULL_TREE;
10631
10632 find_template_parameter_info ftpi (ctx_parms);
10633 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10634 /*include_nondeduced*/true, any_template_parm_r);
10635 return ftpi.parm_list;
10636 }
10637
10638 /* Returns true if T depends on any template parameter. */
10639
10640 int
uses_template_parms(tree t)10641 uses_template_parms (tree t)
10642 {
10643 if (t == NULL_TREE)
10644 return false;
10645
10646 bool dependent_p;
10647 int saved_processing_template_decl;
10648
10649 saved_processing_template_decl = processing_template_decl;
10650 if (!saved_processing_template_decl)
10651 processing_template_decl = 1;
10652 if (TYPE_P (t))
10653 dependent_p = dependent_type_p (t);
10654 else if (TREE_CODE (t) == TREE_VEC)
10655 dependent_p = any_dependent_template_arguments_p (t);
10656 else if (TREE_CODE (t) == TREE_LIST)
10657 dependent_p = (uses_template_parms (TREE_VALUE (t))
10658 || uses_template_parms (TREE_CHAIN (t)));
10659 else if (TREE_CODE (t) == TYPE_DECL)
10660 dependent_p = dependent_type_p (TREE_TYPE (t));
10661 else if (t == error_mark_node)
10662 dependent_p = false;
10663 else
10664 dependent_p = value_dependent_expression_p (t);
10665
10666 processing_template_decl = saved_processing_template_decl;
10667
10668 return dependent_p;
10669 }
10670
10671 /* Returns true iff current_function_decl is an incompletely instantiated
10672 template. Useful instead of processing_template_decl because the latter
10673 is set to 0 during instantiate_non_dependent_expr. */
10674
10675 bool
in_template_function(void)10676 in_template_function (void)
10677 {
10678 tree fn = current_function_decl;
10679 bool ret;
10680 ++processing_template_decl;
10681 ret = (fn && DECL_LANG_SPECIFIC (fn)
10682 && DECL_TEMPLATE_INFO (fn)
10683 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10684 --processing_template_decl;
10685 return ret;
10686 }
10687
10688 /* Returns true if T depends on any template parameter with level LEVEL. */
10689
10690 bool
uses_template_parms_level(tree t,int level)10691 uses_template_parms_level (tree t, int level)
10692 {
10693 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10694 /*include_nondeduced_p=*/true);
10695 }
10696
10697 /* Returns true if the signature of DECL depends on any template parameter from
10698 its enclosing class. */
10699
10700 bool
uses_outer_template_parms(tree decl)10701 uses_outer_template_parms (tree decl)
10702 {
10703 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10704 if (depth == 0)
10705 return false;
10706 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10707 &depth, NULL, /*include_nondeduced_p=*/true))
10708 return true;
10709 if (PRIMARY_TEMPLATE_P (decl)
10710 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10711 (DECL_TEMPLATE_PARMS (decl)),
10712 template_parm_outer_level,
10713 &depth, NULL, /*include_nondeduced_p=*/true))
10714 return true;
10715 tree ci = get_constraints (decl);
10716 if (ci)
10717 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10718 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10719 &depth, NULL, /*nondeduced*/true))
10720 return true;
10721 return false;
10722 }
10723
10724 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10725 ill-formed translation unit, i.e. a variable or function that isn't
10726 usable in a constant expression. */
10727
10728 static inline bool
neglectable_inst_p(tree d)10729 neglectable_inst_p (tree d)
10730 {
10731 return (d && DECL_P (d)
10732 && !undeduced_auto_decl (d)
10733 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10734 : decl_maybe_constant_var_p (d)));
10735 }
10736
10737 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10738 neglectable and instantiated from within an erroneous instantiation. */
10739
10740 static bool
limit_bad_template_recursion(tree decl)10741 limit_bad_template_recursion (tree decl)
10742 {
10743 struct tinst_level *lev = current_tinst_level;
10744 int errs = errorcount + sorrycount;
10745 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10746 return false;
10747
10748 for (; lev; lev = lev->next)
10749 if (neglectable_inst_p (lev->maybe_get_node ()))
10750 break;
10751
10752 return (lev && errs > lev->errors);
10753 }
10754
10755 static int tinst_depth;
10756 extern int max_tinst_depth;
10757 int depth_reached;
10758
10759 static GTY(()) struct tinst_level *last_error_tinst_level;
10760
10761 /* We're starting to instantiate D; record the template instantiation context
10762 at LOC for diagnostics and to restore it later. */
10763
10764 static bool
push_tinst_level_loc(tree tldcl,tree targs,location_t loc)10765 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10766 {
10767 struct tinst_level *new_level;
10768
10769 if (tinst_depth >= max_tinst_depth)
10770 {
10771 /* Tell error.c not to try to instantiate any templates. */
10772 at_eof = 2;
10773 fatal_error (input_location,
10774 "template instantiation depth exceeds maximum of %d"
10775 " (use %<-ftemplate-depth=%> to increase the maximum)",
10776 max_tinst_depth);
10777 return false;
10778 }
10779
10780 /* If the current instantiation caused problems, don't let it instantiate
10781 anything else. Do allow deduction substitution and decls usable in
10782 constant expressions. */
10783 if (!targs && limit_bad_template_recursion (tldcl))
10784 {
10785 /* Avoid no_linkage_errors and unused function warnings for this
10786 decl. */
10787 TREE_NO_WARNING (tldcl) = 1;
10788 return false;
10789 }
10790
10791 /* When not -quiet, dump template instantiations other than functions, since
10792 announce_function will take care of those. */
10793 if (!quiet_flag && !targs
10794 && TREE_CODE (tldcl) != TREE_LIST
10795 && TREE_CODE (tldcl) != FUNCTION_DECL)
10796 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10797
10798 new_level = tinst_level_freelist ().alloc ();
10799 new_level->tldcl = tldcl;
10800 new_level->targs = targs;
10801 new_level->locus = loc;
10802 new_level->errors = errorcount + sorrycount;
10803 new_level->next = NULL;
10804 new_level->refcount = 0;
10805 set_refcount_ptr (new_level->next, current_tinst_level);
10806 set_refcount_ptr (current_tinst_level, new_level);
10807
10808 ++tinst_depth;
10809 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10810 depth_reached = tinst_depth;
10811
10812 return true;
10813 }
10814
10815 /* We're starting substitution of TMPL<ARGS>; record the template
10816 substitution context for diagnostics and to restore it later. */
10817
10818 static bool
push_tinst_level(tree tmpl,tree args)10819 push_tinst_level (tree tmpl, tree args)
10820 {
10821 return push_tinst_level_loc (tmpl, args, input_location);
10822 }
10823
10824 /* We're starting to instantiate D; record INPUT_LOCATION and the
10825 template instantiation context for diagnostics and to restore it
10826 later. */
10827
10828 bool
push_tinst_level(tree d)10829 push_tinst_level (tree d)
10830 {
10831 return push_tinst_level_loc (d, input_location);
10832 }
10833
10834 /* Likewise, but record LOC as the program location. */
10835
10836 bool
push_tinst_level_loc(tree d,location_t loc)10837 push_tinst_level_loc (tree d, location_t loc)
10838 {
10839 gcc_assert (TREE_CODE (d) != TREE_LIST);
10840 return push_tinst_level_loc (d, NULL, loc);
10841 }
10842
10843 /* We're done instantiating this template; return to the instantiation
10844 context. */
10845
10846 void
pop_tinst_level(void)10847 pop_tinst_level (void)
10848 {
10849 /* Restore the filename and line number stashed away when we started
10850 this instantiation. */
10851 input_location = current_tinst_level->locus;
10852 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10853 --tinst_depth;
10854 }
10855
10856 /* We're instantiating a deferred template; restore the template
10857 instantiation context in which the instantiation was requested, which
10858 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10859
10860 static tree
reopen_tinst_level(struct tinst_level * level)10861 reopen_tinst_level (struct tinst_level *level)
10862 {
10863 struct tinst_level *t;
10864
10865 tinst_depth = 0;
10866 for (t = level; t; t = t->next)
10867 ++tinst_depth;
10868
10869 set_refcount_ptr (current_tinst_level, level);
10870 pop_tinst_level ();
10871 if (current_tinst_level)
10872 current_tinst_level->errors = errorcount+sorrycount;
10873 return level->maybe_get_node ();
10874 }
10875
10876 /* Returns the TINST_LEVEL which gives the original instantiation
10877 context. */
10878
10879 struct tinst_level *
outermost_tinst_level(void)10880 outermost_tinst_level (void)
10881 {
10882 struct tinst_level *level = current_tinst_level;
10883 if (level)
10884 while (level->next)
10885 level = level->next;
10886 return level;
10887 }
10888
10889 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10890 vector of template arguments, as for tsubst.
10891
10892 Returns an appropriate tsubst'd friend declaration. */
10893
10894 static tree
tsubst_friend_function(tree decl,tree args)10895 tsubst_friend_function (tree decl, tree args)
10896 {
10897 tree new_friend;
10898
10899 if (TREE_CODE (decl) == FUNCTION_DECL
10900 && DECL_TEMPLATE_INSTANTIATION (decl)
10901 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10902 /* This was a friend declared with an explicit template
10903 argument list, e.g.:
10904
10905 friend void f<>(T);
10906
10907 to indicate that f was a template instantiation, not a new
10908 function declaration. Now, we have to figure out what
10909 instantiation of what template. */
10910 {
10911 tree template_id, arglist, fns;
10912 tree new_args;
10913 tree tmpl;
10914 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10915
10916 /* Friend functions are looked up in the containing namespace scope.
10917 We must enter that scope, to avoid finding member functions of the
10918 current class with same name. */
10919 push_nested_namespace (ns);
10920 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10921 tf_warning_or_error, NULL_TREE,
10922 /*integral_constant_expression_p=*/false);
10923 pop_nested_namespace (ns);
10924 arglist = tsubst (DECL_TI_ARGS (decl), args,
10925 tf_warning_or_error, NULL_TREE);
10926 template_id = lookup_template_function (fns, arglist);
10927
10928 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10929 tmpl = determine_specialization (template_id, new_friend,
10930 &new_args,
10931 /*need_member_template=*/0,
10932 TREE_VEC_LENGTH (args),
10933 tsk_none);
10934 return instantiate_template (tmpl, new_args, tf_error);
10935 }
10936
10937 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10938
10939 /* The NEW_FRIEND will look like an instantiation, to the
10940 compiler, but is not an instantiation from the point of view of
10941 the language. For example, we might have had:
10942
10943 template <class T> struct S {
10944 template <class U> friend void f(T, U);
10945 };
10946
10947 Then, in S<int>, template <class U> void f(int, U) is not an
10948 instantiation of anything. */
10949 if (new_friend == error_mark_node)
10950 return error_mark_node;
10951
10952 DECL_USE_TEMPLATE (new_friend) = 0;
10953 if (TREE_CODE (decl) == TEMPLATE_DECL)
10954 {
10955 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10956 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10957 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10958
10959 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10960 match in decls_match. */
10961 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10962 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
10963 treqs = maybe_substitute_reqs_for (treqs, new_friend);
10964 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10965 }
10966
10967 /* The mangled name for the NEW_FRIEND is incorrect. The function
10968 is not a template instantiation and should not be mangled like
10969 one. Therefore, we forget the mangling here; we'll recompute it
10970 later if we need it. */
10971 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10972 {
10973 SET_DECL_RTL (new_friend, NULL);
10974 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10975 }
10976
10977 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10978 {
10979 tree old_decl;
10980 tree new_friend_template_info;
10981 tree new_friend_result_template_info;
10982 tree ns;
10983 int new_friend_is_defn;
10984
10985 /* We must save some information from NEW_FRIEND before calling
10986 duplicate decls since that function will free NEW_FRIEND if
10987 possible. */
10988 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10989 new_friend_is_defn =
10990 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10991 (template_for_substitution (new_friend)))
10992 != NULL_TREE);
10993 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10994 {
10995 /* This declaration is a `primary' template. */
10996 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10997
10998 new_friend_result_template_info
10999 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
11000 }
11001 else
11002 new_friend_result_template_info = NULL_TREE;
11003
11004 /* Inside pushdecl_namespace_level, we will push into the
11005 current namespace. However, the friend function should go
11006 into the namespace of the template. */
11007 ns = decl_namespace_context (new_friend);
11008 push_nested_namespace (ns);
11009 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
11010 pop_nested_namespace (ns);
11011
11012 if (old_decl == error_mark_node)
11013 return error_mark_node;
11014
11015 if (old_decl != new_friend)
11016 {
11017 /* This new friend declaration matched an existing
11018 declaration. For example, given:
11019
11020 template <class T> void f(T);
11021 template <class U> class C {
11022 template <class T> friend void f(T) {}
11023 };
11024
11025 the friend declaration actually provides the definition
11026 of `f', once C has been instantiated for some type. So,
11027 old_decl will be the out-of-class template declaration,
11028 while new_friend is the in-class definition.
11029
11030 But, if `f' was called before this point, the
11031 instantiation of `f' will have DECL_TI_ARGS corresponding
11032 to `T' but not to `U', references to which might appear
11033 in the definition of `f'. Previously, the most general
11034 template for an instantiation of `f' was the out-of-class
11035 version; now it is the in-class version. Therefore, we
11036 run through all specialization of `f', adding to their
11037 DECL_TI_ARGS appropriately. In particular, they need a
11038 new set of outer arguments, corresponding to the
11039 arguments for this class instantiation.
11040
11041 The same situation can arise with something like this:
11042
11043 friend void f(int);
11044 template <class T> class C {
11045 friend void f(T) {}
11046 };
11047
11048 when `C<int>' is instantiated. Now, `f(int)' is defined
11049 in the class. */
11050
11051 if (!new_friend_is_defn)
11052 /* On the other hand, if the in-class declaration does
11053 *not* provide a definition, then we don't want to alter
11054 existing definitions. We can just leave everything
11055 alone. */
11056 ;
11057 else
11058 {
11059 tree new_template = TI_TEMPLATE (new_friend_template_info);
11060 tree new_args = TI_ARGS (new_friend_template_info);
11061
11062 /* Overwrite whatever template info was there before, if
11063 any, with the new template information pertaining to
11064 the declaration. */
11065 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11066
11067 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11068 {
11069 /* We should have called reregister_specialization in
11070 duplicate_decls. */
11071 gcc_assert (retrieve_specialization (new_template,
11072 new_args, 0)
11073 == old_decl);
11074
11075 /* Instantiate it if the global has already been used. */
11076 if (DECL_ODR_USED (old_decl))
11077 instantiate_decl (old_decl, /*defer_ok=*/true,
11078 /*expl_inst_class_mem_p=*/false);
11079 }
11080 else
11081 {
11082 tree t;
11083
11084 /* Indicate that the old function template is a partial
11085 instantiation. */
11086 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11087 = new_friend_result_template_info;
11088
11089 gcc_assert (new_template
11090 == most_general_template (new_template));
11091 gcc_assert (new_template != old_decl);
11092
11093 /* Reassign any specializations already in the hash table
11094 to the new more general template, and add the
11095 additional template args. */
11096 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11097 t != NULL_TREE;
11098 t = TREE_CHAIN (t))
11099 {
11100 tree spec = TREE_VALUE (t);
11101 spec_entry elt;
11102
11103 elt.tmpl = old_decl;
11104 elt.args = DECL_TI_ARGS (spec);
11105 elt.spec = NULL_TREE;
11106
11107 decl_specializations->remove_elt (&elt);
11108
11109 DECL_TI_ARGS (spec)
11110 = add_outermost_template_args (new_args,
11111 DECL_TI_ARGS (spec));
11112
11113 register_specialization
11114 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11115
11116 }
11117 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11118 }
11119 }
11120
11121 /* The information from NEW_FRIEND has been merged into OLD_DECL
11122 by duplicate_decls. */
11123 new_friend = old_decl;
11124 }
11125 }
11126 else
11127 {
11128 tree context = DECL_CONTEXT (new_friend);
11129 bool dependent_p;
11130
11131 /* In the code
11132 template <class T> class C {
11133 template <class U> friend void C1<U>::f (); // case 1
11134 friend void C2<T>::f (); // case 2
11135 };
11136 we only need to make sure CONTEXT is a complete type for
11137 case 2. To distinguish between the two cases, we note that
11138 CONTEXT of case 1 remains dependent type after tsubst while
11139 this isn't true for case 2. */
11140 ++processing_template_decl;
11141 dependent_p = dependent_type_p (context);
11142 --processing_template_decl;
11143
11144 if (!dependent_p
11145 && !complete_type_or_else (context, NULL_TREE))
11146 return error_mark_node;
11147
11148 if (COMPLETE_TYPE_P (context))
11149 {
11150 tree fn = new_friend;
11151 /* do_friend adds the TEMPLATE_DECL for any member friend
11152 template even if it isn't a member template, i.e.
11153 template <class T> friend A<T>::f();
11154 Look through it in that case. */
11155 if (TREE_CODE (fn) == TEMPLATE_DECL
11156 && !PRIMARY_TEMPLATE_P (fn))
11157 fn = DECL_TEMPLATE_RESULT (fn);
11158 /* Check to see that the declaration is really present, and,
11159 possibly obtain an improved declaration. */
11160 fn = check_classfn (context, fn, NULL_TREE);
11161
11162 if (fn)
11163 new_friend = fn;
11164 }
11165 }
11166
11167 return new_friend;
11168 }
11169
11170 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11171 template arguments, as for tsubst.
11172
11173 Returns an appropriate tsubst'd friend type or error_mark_node on
11174 failure. */
11175
11176 static tree
tsubst_friend_class(tree friend_tmpl,tree args)11177 tsubst_friend_class (tree friend_tmpl, tree args)
11178 {
11179 tree tmpl;
11180
11181 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11182 {
11183 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11184 return TREE_TYPE (tmpl);
11185 }
11186
11187 tree context = CP_DECL_CONTEXT (friend_tmpl);
11188 if (TREE_CODE (context) == NAMESPACE_DECL)
11189 push_nested_namespace (context);
11190 else
11191 {
11192 context = tsubst (context, args, tf_error, NULL_TREE);
11193 push_nested_class (context);
11194 }
11195
11196 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11197 /*non_class=*/false, /*block_p=*/false,
11198 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11199
11200 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11201 {
11202 /* The friend template has already been declared. Just
11203 check to see that the declarations match, and install any new
11204 default parameters. We must tsubst the default parameters,
11205 of course. We only need the innermost template parameters
11206 because that is all that redeclare_class_template will look
11207 at. */
11208 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11209 > TMPL_ARGS_DEPTH (args))
11210 {
11211 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11212 args, tf_warning_or_error);
11213 location_t saved_input_location = input_location;
11214 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11215 tree cons = get_constraints (tmpl);
11216 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11217 input_location = saved_input_location;
11218 }
11219 }
11220 else
11221 {
11222 /* The friend template has not already been declared. In this
11223 case, the instantiation of the template class will cause the
11224 injection of this template into the namespace scope. */
11225 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11226
11227 if (tmpl != error_mark_node)
11228 {
11229 /* The new TMPL is not an instantiation of anything, so we
11230 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11231 for the new type because that is supposed to be the
11232 corresponding template decl, i.e., TMPL. */
11233 DECL_USE_TEMPLATE (tmpl) = 0;
11234 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11235 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11236 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11237 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11238
11239 /* It is hidden. */
11240 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11241 DECL_ANTICIPATED (tmpl)
11242 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11243
11244 /* Substitute into and set the constraints on the new declaration. */
11245 if (tree ci = get_constraints (friend_tmpl))
11246 {
11247 ++processing_template_decl;
11248 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11249 DECL_FRIEND_CONTEXT (friend_tmpl));
11250 --processing_template_decl;
11251 set_constraints (tmpl, ci);
11252 }
11253
11254 /* Inject this template into the enclosing namspace scope. */
11255 tmpl = pushdecl_namespace_level (tmpl, true);
11256 }
11257 }
11258
11259 if (TREE_CODE (context) == NAMESPACE_DECL)
11260 pop_nested_namespace (context);
11261 else
11262 pop_nested_class ();
11263
11264 return TREE_TYPE (tmpl);
11265 }
11266
11267 /* Returns zero if TYPE cannot be completed later due to circularity.
11268 Otherwise returns one. */
11269
11270 static int
can_complete_type_without_circularity(tree type)11271 can_complete_type_without_circularity (tree type)
11272 {
11273 if (type == NULL_TREE || type == error_mark_node)
11274 return 0;
11275 else if (COMPLETE_TYPE_P (type))
11276 return 1;
11277 else if (TREE_CODE (type) == ARRAY_TYPE)
11278 return can_complete_type_without_circularity (TREE_TYPE (type));
11279 else if (CLASS_TYPE_P (type)
11280 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11281 return 0;
11282 else
11283 return 1;
11284 }
11285
11286 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11287 tsubst_flags_t, tree);
11288
11289 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11290 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11291
11292 static tree
tsubst_attribute(tree t,tree * decl_p,tree args,tsubst_flags_t complain,tree in_decl)11293 tsubst_attribute (tree t, tree *decl_p, tree args,
11294 tsubst_flags_t complain, tree in_decl)
11295 {
11296 gcc_assert (ATTR_IS_DEPENDENT (t));
11297
11298 tree val = TREE_VALUE (t);
11299 if (val == NULL_TREE)
11300 /* Nothing to do. */;
11301 else if ((flag_openmp || flag_openmp_simd)
11302 && is_attribute_p ("omp declare simd",
11303 get_attribute_name (t)))
11304 {
11305 tree clauses = TREE_VALUE (val);
11306 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11307 complain, in_decl);
11308 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11309 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11310 tree parms = DECL_ARGUMENTS (*decl_p);
11311 clauses
11312 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11313 if (clauses)
11314 val = build_tree_list (NULL_TREE, clauses);
11315 else
11316 val = NULL_TREE;
11317 }
11318 else if (flag_openmp
11319 && is_attribute_p ("omp declare variant base",
11320 get_attribute_name (t)))
11321 {
11322 ++cp_unevaluated_operand;
11323 tree varid
11324 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11325 in_decl, /*integral_constant_expression_p=*/false);
11326 --cp_unevaluated_operand;
11327 tree chain = TREE_CHAIN (val);
11328 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11329 tree ctx = copy_list (TREE_VALUE (val));
11330 tree simd = get_identifier ("simd");
11331 tree score = get_identifier (" score");
11332 tree condition = get_identifier ("condition");
11333 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11334 {
11335 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11336 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11337 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11338 {
11339 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11340 {
11341 tree clauses = TREE_VALUE (t2);
11342 clauses = tsubst_omp_clauses (clauses,
11343 C_ORT_OMP_DECLARE_SIMD, args,
11344 complain, in_decl);
11345 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11346 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11347 TREE_VALUE (t2) = clauses;
11348 }
11349 else
11350 {
11351 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11352 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11353 if (TREE_VALUE (t3))
11354 {
11355 bool allow_string
11356 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11357 && TREE_PURPOSE (t3) != score);
11358 tree v = TREE_VALUE (t3);
11359 if (TREE_CODE (v) == STRING_CST && allow_string)
11360 continue;
11361 v = tsubst_expr (v, args, complain, in_decl, true);
11362 v = fold_non_dependent_expr (v);
11363 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11364 || (TREE_PURPOSE (t3) == score
11365 ? TREE_CODE (v) != INTEGER_CST
11366 : !tree_fits_shwi_p (v)))
11367 {
11368 location_t loc
11369 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11370 match_loc);
11371 if (TREE_PURPOSE (t3) == score)
11372 error_at (loc, "score argument must be "
11373 "constant integer expression");
11374 else if (allow_string)
11375 error_at (loc, "property must be constant "
11376 "integer expression or string "
11377 "literal");
11378 else
11379 error_at (loc, "property must be constant "
11380 "integer expression");
11381 return NULL_TREE;
11382 }
11383 else if (TREE_PURPOSE (t3) == score
11384 && tree_int_cst_sgn (v) < 0)
11385 {
11386 location_t loc
11387 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11388 match_loc);
11389 error_at (loc, "score argument must be "
11390 "non-negative");
11391 return NULL_TREE;
11392 }
11393 TREE_VALUE (t3) = v;
11394 }
11395 }
11396 }
11397 }
11398 val = tree_cons (varid, ctx, chain);
11399 }
11400 /* If the first attribute argument is an identifier, don't
11401 pass it through tsubst. Attributes like mode, format,
11402 cleanup and several target specific attributes expect it
11403 unmodified. */
11404 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11405 {
11406 tree chain
11407 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11408 /*integral_constant_expression_p=*/false);
11409 if (chain != TREE_CHAIN (val))
11410 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11411 }
11412 else if (PACK_EXPANSION_P (val))
11413 {
11414 /* An attribute pack expansion. */
11415 tree purp = TREE_PURPOSE (t);
11416 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11417 if (pack == error_mark_node)
11418 return error_mark_node;
11419 int len = TREE_VEC_LENGTH (pack);
11420 tree list = NULL_TREE;
11421 tree *q = &list;
11422 for (int i = 0; i < len; ++i)
11423 {
11424 tree elt = TREE_VEC_ELT (pack, i);
11425 *q = build_tree_list (purp, elt);
11426 q = &TREE_CHAIN (*q);
11427 }
11428 return list;
11429 }
11430 else
11431 val = tsubst_expr (val, args, complain, in_decl,
11432 /*integral_constant_expression_p=*/false);
11433
11434 if (val != TREE_VALUE (t))
11435 return build_tree_list (TREE_PURPOSE (t), val);
11436 return t;
11437 }
11438
11439 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11440 unchanged or a new TREE_LIST chain. */
11441
11442 static tree
tsubst_attributes(tree attributes,tree args,tsubst_flags_t complain,tree in_decl)11443 tsubst_attributes (tree attributes, tree args,
11444 tsubst_flags_t complain, tree in_decl)
11445 {
11446 tree last_dep = NULL_TREE;
11447
11448 for (tree t = attributes; t; t = TREE_CHAIN (t))
11449 if (ATTR_IS_DEPENDENT (t))
11450 {
11451 last_dep = t;
11452 attributes = copy_list (attributes);
11453 break;
11454 }
11455
11456 if (last_dep)
11457 for (tree *p = &attributes; *p; )
11458 {
11459 tree t = *p;
11460 if (ATTR_IS_DEPENDENT (t))
11461 {
11462 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11463 if (subst != t)
11464 {
11465 *p = subst;
11466 while (*p)
11467 p = &TREE_CHAIN (*p);
11468 *p = TREE_CHAIN (t);
11469 continue;
11470 }
11471 }
11472 p = &TREE_CHAIN (*p);
11473 }
11474
11475 return attributes;
11476 }
11477
11478 /* Apply any attributes which had to be deferred until instantiation
11479 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11480 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11481
11482 static void
apply_late_template_attributes(tree * decl_p,tree attributes,int attr_flags,tree args,tsubst_flags_t complain,tree in_decl)11483 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11484 tree args, tsubst_flags_t complain, tree in_decl)
11485 {
11486 tree last_dep = NULL_TREE;
11487 tree t;
11488 tree *p;
11489
11490 if (attributes == NULL_TREE)
11491 return;
11492
11493 if (DECL_P (*decl_p))
11494 {
11495 if (TREE_TYPE (*decl_p) == error_mark_node)
11496 return;
11497 p = &DECL_ATTRIBUTES (*decl_p);
11498 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11499 to our attributes parameter. */
11500 gcc_assert (*p == attributes);
11501 }
11502 else
11503 {
11504 p = &TYPE_ATTRIBUTES (*decl_p);
11505 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11506 lookup_template_class_1, and should be preserved. */
11507 gcc_assert (*p != attributes);
11508 while (*p)
11509 p = &TREE_CHAIN (*p);
11510 }
11511
11512 for (t = attributes; t; t = TREE_CHAIN (t))
11513 if (ATTR_IS_DEPENDENT (t))
11514 {
11515 last_dep = t;
11516 attributes = copy_list (attributes);
11517 break;
11518 }
11519
11520 *p = attributes;
11521 if (last_dep)
11522 {
11523 tree late_attrs = NULL_TREE;
11524 tree *q = &late_attrs;
11525
11526 for (; *p; )
11527 {
11528 t = *p;
11529 if (ATTR_IS_DEPENDENT (t))
11530 {
11531 *p = TREE_CHAIN (t);
11532 TREE_CHAIN (t) = NULL_TREE;
11533 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11534 while (*q)
11535 q = &TREE_CHAIN (*q);
11536 }
11537 else
11538 p = &TREE_CHAIN (t);
11539 }
11540
11541 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11542 }
11543 }
11544
11545 /* Perform (or defer) access check for typedefs that were referenced
11546 from within the template TMPL code.
11547 This is a subroutine of instantiate_decl and instantiate_class_template.
11548 TMPL is the template to consider and TARGS is the list of arguments of
11549 that template. */
11550
11551 static void
perform_typedefs_access_check(tree tmpl,tree targs)11552 perform_typedefs_access_check (tree tmpl, tree targs)
11553 {
11554 unsigned i;
11555 qualified_typedef_usage_t *iter;
11556
11557 if (!tmpl
11558 || (!CLASS_TYPE_P (tmpl)
11559 && TREE_CODE (tmpl) != FUNCTION_DECL))
11560 return;
11561
11562 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
11563 {
11564 tree type_decl = iter->typedef_decl;
11565 tree type_scope = iter->context;
11566
11567 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11568 continue;
11569
11570 if (uses_template_parms (type_decl))
11571 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11572 if (uses_template_parms (type_scope))
11573 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11574
11575 /* Make access check error messages point to the location
11576 of the use of the typedef. */
11577 iloc_sentinel ils (iter->locus);
11578 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11579 type_decl, type_decl,
11580 tf_warning_or_error);
11581 }
11582 }
11583
11584 static tree
instantiate_class_template_1(tree type)11585 instantiate_class_template_1 (tree type)
11586 {
11587 tree templ, args, pattern, t, member;
11588 tree typedecl;
11589 tree pbinfo;
11590 tree base_list;
11591 unsigned int saved_maximum_field_alignment;
11592 tree fn_context;
11593
11594 if (type == error_mark_node)
11595 return error_mark_node;
11596
11597 if (COMPLETE_OR_OPEN_TYPE_P (type)
11598 || uses_template_parms (type))
11599 return type;
11600
11601 /* Figure out which template is being instantiated. */
11602 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11603 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11604
11605 /* Mark the type as in the process of being defined. */
11606 TYPE_BEING_DEFINED (type) = 1;
11607
11608 /* We may be in the middle of deferred access check. Disable
11609 it now. */
11610 deferring_access_check_sentinel acs (dk_no_deferred);
11611
11612 /* Determine what specialization of the original template to
11613 instantiate. */
11614 t = most_specialized_partial_spec (type, tf_warning_or_error);
11615 if (t == error_mark_node)
11616 return error_mark_node;
11617 else if (t)
11618 {
11619 /* This TYPE is actually an instantiation of a partial
11620 specialization. We replace the innermost set of ARGS with
11621 the arguments appropriate for substitution. For example,
11622 given:
11623
11624 template <class T> struct S {};
11625 template <class T> struct S<T*> {};
11626
11627 and supposing that we are instantiating S<int*>, ARGS will
11628 presently be {int*} -- but we need {int}. */
11629 pattern = TREE_TYPE (t);
11630 args = TREE_PURPOSE (t);
11631 }
11632 else
11633 {
11634 pattern = TREE_TYPE (templ);
11635 args = CLASSTYPE_TI_ARGS (type);
11636 }
11637
11638 /* If the template we're instantiating is incomplete, then clearly
11639 there's nothing we can do. */
11640 if (!COMPLETE_TYPE_P (pattern))
11641 {
11642 /* We can try again later. */
11643 TYPE_BEING_DEFINED (type) = 0;
11644 return type;
11645 }
11646
11647 /* If we've recursively instantiated too many templates, stop. */
11648 if (! push_tinst_level (type))
11649 return type;
11650
11651 int saved_unevaluated_operand = cp_unevaluated_operand;
11652 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11653
11654 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11655 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11656 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11657 fn_context = error_mark_node;
11658 if (!fn_context)
11659 push_to_top_level ();
11660 else
11661 {
11662 cp_unevaluated_operand = 0;
11663 c_inhibit_evaluation_warnings = 0;
11664 }
11665 /* Use #pragma pack from the template context. */
11666 saved_maximum_field_alignment = maximum_field_alignment;
11667 maximum_field_alignment = TYPE_PRECISION (pattern);
11668
11669 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11670
11671 /* Set the input location to the most specialized template definition.
11672 This is needed if tsubsting causes an error. */
11673 typedecl = TYPE_MAIN_DECL (pattern);
11674 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11675 DECL_SOURCE_LOCATION (typedecl);
11676
11677 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11678 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11679 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11680 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11681 if (ANON_AGGR_TYPE_P (pattern))
11682 SET_ANON_AGGR_TYPE_P (type);
11683 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11684 {
11685 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11686 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11687 /* Adjust visibility for template arguments. */
11688 determine_visibility (TYPE_MAIN_DECL (type));
11689 }
11690 if (CLASS_TYPE_P (type))
11691 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11692
11693 pbinfo = TYPE_BINFO (pattern);
11694
11695 /* We should never instantiate a nested class before its enclosing
11696 class; we need to look up the nested class by name before we can
11697 instantiate it, and that lookup should instantiate the enclosing
11698 class. */
11699 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11700 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11701
11702 base_list = NULL_TREE;
11703 if (BINFO_N_BASE_BINFOS (pbinfo))
11704 {
11705 tree pbase_binfo;
11706 tree pushed_scope;
11707 int i;
11708
11709 /* We must enter the scope containing the type, as that is where
11710 the accessibility of types named in dependent bases are
11711 looked up from. */
11712 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11713
11714 /* Substitute into each of the bases to determine the actual
11715 basetypes. */
11716 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11717 {
11718 tree base;
11719 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11720 tree expanded_bases = NULL_TREE;
11721 int idx, len = 1;
11722
11723 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11724 {
11725 expanded_bases =
11726 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11727 args, tf_error, NULL_TREE);
11728 if (expanded_bases == error_mark_node)
11729 continue;
11730
11731 len = TREE_VEC_LENGTH (expanded_bases);
11732 }
11733
11734 for (idx = 0; idx < len; idx++)
11735 {
11736 if (expanded_bases)
11737 /* Extract the already-expanded base class. */
11738 base = TREE_VEC_ELT (expanded_bases, idx);
11739 else
11740 /* Substitute to figure out the base class. */
11741 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11742 NULL_TREE);
11743
11744 if (base == error_mark_node)
11745 continue;
11746
11747 base_list = tree_cons (access, base, base_list);
11748 if (BINFO_VIRTUAL_P (pbase_binfo))
11749 TREE_TYPE (base_list) = integer_type_node;
11750 }
11751 }
11752
11753 /* The list is now in reverse order; correct that. */
11754 base_list = nreverse (base_list);
11755
11756 if (pushed_scope)
11757 pop_scope (pushed_scope);
11758 }
11759 /* Now call xref_basetypes to set up all the base-class
11760 information. */
11761 xref_basetypes (type, base_list);
11762
11763 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11764 (int) ATTR_FLAG_TYPE_IN_PLACE,
11765 args, tf_error, NULL_TREE);
11766 fixup_attribute_variants (type);
11767
11768 /* Now that our base classes are set up, enter the scope of the
11769 class, so that name lookups into base classes, etc. will work
11770 correctly. This is precisely analogous to what we do in
11771 begin_class_definition when defining an ordinary non-template
11772 class, except we also need to push the enclosing classes. */
11773 push_nested_class (type);
11774
11775 /* A vector to hold members marked with attribute used. */
11776 auto_vec<tree> used;
11777
11778 /* Now members are processed in the order of declaration. */
11779 for (member = CLASSTYPE_DECL_LIST (pattern);
11780 member; member = TREE_CHAIN (member))
11781 {
11782 tree t = TREE_VALUE (member);
11783
11784 if (TREE_PURPOSE (member))
11785 {
11786 if (TYPE_P (t))
11787 {
11788 if (LAMBDA_TYPE_P (t))
11789 /* A closure type for a lambda in an NSDMI or default argument.
11790 Ignore it; it will be regenerated when needed. */
11791 continue;
11792
11793 /* Build new CLASSTYPE_NESTED_UTDS. */
11794
11795 tree newtag;
11796 bool class_template_p;
11797
11798 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11799 && TYPE_LANG_SPECIFIC (t)
11800 && CLASSTYPE_IS_TEMPLATE (t));
11801 /* If the member is a class template, then -- even after
11802 substitution -- there may be dependent types in the
11803 template argument list for the class. We increment
11804 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11805 that function will assume that no types are dependent
11806 when outside of a template. */
11807 if (class_template_p)
11808 ++processing_template_decl;
11809 newtag = tsubst (t, args, tf_error, NULL_TREE);
11810 if (class_template_p)
11811 --processing_template_decl;
11812 if (newtag == error_mark_node)
11813 continue;
11814
11815 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11816 {
11817 tree name = TYPE_IDENTIFIER (t);
11818
11819 if (class_template_p)
11820 /* Unfortunately, lookup_template_class sets
11821 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11822 instantiation (i.e., for the type of a member
11823 template class nested within a template class.)
11824 This behavior is required for
11825 maybe_process_partial_specialization to work
11826 correctly, but is not accurate in this case;
11827 the TAG is not an instantiation of anything.
11828 (The corresponding TEMPLATE_DECL is an
11829 instantiation, but the TYPE is not.) */
11830 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11831
11832 /* Now, we call pushtag to put this NEWTAG into the scope of
11833 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11834 pushtag calling push_template_decl. We don't have to do
11835 this for enums because it will already have been done in
11836 tsubst_enum. */
11837 if (name)
11838 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11839 pushtag (name, newtag, /*tag_scope=*/ts_current);
11840 }
11841 }
11842 else if (DECL_DECLARES_FUNCTION_P (t))
11843 {
11844 tree r;
11845
11846 if (TREE_CODE (t) == TEMPLATE_DECL)
11847 ++processing_template_decl;
11848 r = tsubst (t, args, tf_error, NULL_TREE);
11849 if (TREE_CODE (t) == TEMPLATE_DECL)
11850 --processing_template_decl;
11851 set_current_access_from_decl (r);
11852 finish_member_declaration (r);
11853 /* Instantiate members marked with attribute used. */
11854 if (r != error_mark_node && DECL_PRESERVE_P (r))
11855 used.safe_push (r);
11856 if (TREE_CODE (r) == FUNCTION_DECL
11857 && DECL_OMP_DECLARE_REDUCTION_P (r))
11858 cp_check_omp_declare_reduction (r);
11859 }
11860 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11861 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11862 /* A closure type for a lambda in an NSDMI or default argument.
11863 Ignore it; it will be regenerated when needed. */;
11864 else
11865 {
11866 /* Build new TYPE_FIELDS. */
11867 if (TREE_CODE (t) == STATIC_ASSERT)
11868 {
11869 tree condition;
11870
11871 ++c_inhibit_evaluation_warnings;
11872 condition =
11873 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11874 tf_warning_or_error, NULL_TREE,
11875 /*integral_constant_expression_p=*/true);
11876 --c_inhibit_evaluation_warnings;
11877
11878 finish_static_assert (condition,
11879 STATIC_ASSERT_MESSAGE (t),
11880 STATIC_ASSERT_SOURCE_LOCATION (t),
11881 /*member_p=*/true);
11882 }
11883 else if (TREE_CODE (t) != CONST_DECL)
11884 {
11885 tree r;
11886 tree vec = NULL_TREE;
11887 int len = 1;
11888
11889 /* The file and line for this declaration, to
11890 assist in error message reporting. Since we
11891 called push_tinst_level above, we don't need to
11892 restore these. */
11893 input_location = DECL_SOURCE_LOCATION (t);
11894
11895 if (TREE_CODE (t) == TEMPLATE_DECL)
11896 ++processing_template_decl;
11897 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11898 if (TREE_CODE (t) == TEMPLATE_DECL)
11899 --processing_template_decl;
11900
11901 if (TREE_CODE (r) == TREE_VEC)
11902 {
11903 /* A capture pack became multiple fields. */
11904 vec = r;
11905 len = TREE_VEC_LENGTH (vec);
11906 }
11907
11908 for (int i = 0; i < len; ++i)
11909 {
11910 if (vec)
11911 r = TREE_VEC_ELT (vec, i);
11912 if (VAR_P (r))
11913 {
11914 /* In [temp.inst]:
11915
11916 [t]he initialization (and any associated
11917 side-effects) of a static data member does
11918 not occur unless the static data member is
11919 itself used in a way that requires the
11920 definition of the static data member to
11921 exist.
11922
11923 Therefore, we do not substitute into the
11924 initialized for the static data member here. */
11925 finish_static_data_member_decl
11926 (r,
11927 /*init=*/NULL_TREE,
11928 /*init_const_expr_p=*/false,
11929 /*asmspec_tree=*/NULL_TREE,
11930 /*flags=*/0);
11931 /* Instantiate members marked with attribute used. */
11932 if (r != error_mark_node && DECL_PRESERVE_P (r))
11933 used.safe_push (r);
11934 }
11935 else if (TREE_CODE (r) == FIELD_DECL)
11936 {
11937 /* Determine whether R has a valid type and can be
11938 completed later. If R is invalid, then its type
11939 is replaced by error_mark_node. */
11940 tree rtype = TREE_TYPE (r);
11941 if (can_complete_type_without_circularity (rtype))
11942 complete_type (rtype);
11943
11944 if (!complete_or_array_type_p (rtype))
11945 {
11946 /* If R's type couldn't be completed and
11947 it isn't a flexible array member (whose
11948 type is incomplete by definition) give
11949 an error. */
11950 cxx_incomplete_type_error (r, rtype);
11951 TREE_TYPE (r) = error_mark_node;
11952 }
11953 else if (TREE_CODE (rtype) == ARRAY_TYPE
11954 && TYPE_DOMAIN (rtype) == NULL_TREE
11955 && (TREE_CODE (type) == UNION_TYPE
11956 || TREE_CODE (type) == QUAL_UNION_TYPE))
11957 {
11958 error ("flexible array member %qD in union", r);
11959 TREE_TYPE (r) = error_mark_node;
11960 }
11961 else if (!verify_type_context (input_location,
11962 TCTX_FIELD, rtype))
11963 TREE_TYPE (r) = error_mark_node;
11964 }
11965
11966 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11967 such a thing will already have been added to the field
11968 list by tsubst_enum in finish_member_declaration in the
11969 CLASSTYPE_NESTED_UTDS case above. */
11970 if (!(TREE_CODE (r) == TYPE_DECL
11971 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11972 && DECL_ARTIFICIAL (r)))
11973 {
11974 set_current_access_from_decl (r);
11975 finish_member_declaration (r);
11976 }
11977 }
11978 }
11979 }
11980 }
11981 else
11982 {
11983 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11984 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11985 {
11986 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11987
11988 tree friend_type = t;
11989 bool adjust_processing_template_decl = false;
11990
11991 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11992 {
11993 /* template <class T> friend class C; */
11994 friend_type = tsubst_friend_class (friend_type, args);
11995 adjust_processing_template_decl = true;
11996 }
11997 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11998 {
11999 /* template <class T> friend class C::D; */
12000 friend_type = tsubst (friend_type, args,
12001 tf_warning_or_error, NULL_TREE);
12002 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12003 friend_type = TREE_TYPE (friend_type);
12004 adjust_processing_template_decl = true;
12005 }
12006 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12007 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12008 {
12009 /* This could be either
12010
12011 friend class T::C;
12012
12013 when dependent_type_p is false or
12014
12015 template <class U> friend class T::C;
12016
12017 otherwise. */
12018 /* Bump processing_template_decl in case this is something like
12019 template <class T> friend struct A<T>::B. */
12020 ++processing_template_decl;
12021 friend_type = tsubst (friend_type, args,
12022 tf_warning_or_error, NULL_TREE);
12023 if (dependent_type_p (friend_type))
12024 adjust_processing_template_decl = true;
12025 --processing_template_decl;
12026 }
12027 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
12028 && !CLASSTYPE_USE_TEMPLATE (friend_type)
12029 && TYPE_HIDDEN_P (friend_type))
12030 {
12031 /* friend class C;
12032
12033 where C hasn't been declared yet. Let's lookup name
12034 from namespace scope directly, bypassing any name that
12035 come from dependent base class. */
12036 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
12037
12038 /* The call to xref_tag_from_type does injection for friend
12039 classes. */
12040 push_nested_namespace (ns);
12041 friend_type =
12042 xref_tag_from_type (friend_type, NULL_TREE,
12043 /*tag_scope=*/ts_current);
12044 pop_nested_namespace (ns);
12045 }
12046 else if (uses_template_parms (friend_type))
12047 /* friend class C<T>; */
12048 friend_type = tsubst (friend_type, args,
12049 tf_warning_or_error, NULL_TREE);
12050 /* Otherwise it's
12051
12052 friend class C;
12053
12054 where C is already declared or
12055
12056 friend class C<int>;
12057
12058 We don't have to do anything in these cases. */
12059
12060 if (adjust_processing_template_decl)
12061 /* Trick make_friend_class into realizing that the friend
12062 we're adding is a template, not an ordinary class. It's
12063 important that we use make_friend_class since it will
12064 perform some error-checking and output cross-reference
12065 information. */
12066 ++processing_template_decl;
12067
12068 if (friend_type != error_mark_node)
12069 make_friend_class (type, friend_type, /*complain=*/false);
12070
12071 if (adjust_processing_template_decl)
12072 --processing_template_decl;
12073 }
12074 else
12075 {
12076 /* Build new DECL_FRIENDLIST. */
12077 tree r;
12078
12079 /* The file and line for this declaration, to
12080 assist in error message reporting. Since we
12081 called push_tinst_level above, we don't need to
12082 restore these. */
12083 input_location = DECL_SOURCE_LOCATION (t);
12084
12085 if (TREE_CODE (t) == TEMPLATE_DECL)
12086 {
12087 ++processing_template_decl;
12088 push_deferring_access_checks (dk_no_check);
12089 }
12090
12091 r = tsubst_friend_function (t, args);
12092 add_friend (type, r, /*complain=*/false);
12093 if (TREE_CODE (t) == TEMPLATE_DECL)
12094 {
12095 pop_deferring_access_checks ();
12096 --processing_template_decl;
12097 }
12098 }
12099 }
12100 }
12101
12102 if (fn_context)
12103 {
12104 /* Restore these before substituting into the lambda capture
12105 initializers. */
12106 cp_unevaluated_operand = saved_unevaluated_operand;
12107 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12108 }
12109
12110 /* Set the file and line number information to whatever is given for
12111 the class itself. This puts error messages involving generated
12112 implicit functions at a predictable point, and the same point
12113 that would be used for non-template classes. */
12114 input_location = DECL_SOURCE_LOCATION (typedecl);
12115
12116 unreverse_member_declarations (type);
12117 finish_struct_1 (type);
12118 TYPE_BEING_DEFINED (type) = 0;
12119
12120 /* We don't instantiate default arguments for member functions. 14.7.1:
12121
12122 The implicit instantiation of a class template specialization causes
12123 the implicit instantiation of the declarations, but not of the
12124 definitions or default arguments, of the class member functions,
12125 member classes, static data members and member templates.... */
12126
12127 /* Some typedefs referenced from within the template code need to be access
12128 checked at template instantiation time, i.e now. These types were
12129 added to the template at parsing time. Let's get those and perform
12130 the access checks then. */
12131 perform_typedefs_access_check (pattern, args);
12132 perform_deferred_access_checks (tf_warning_or_error);
12133 pop_nested_class ();
12134 maximum_field_alignment = saved_maximum_field_alignment;
12135 if (!fn_context)
12136 pop_from_top_level ();
12137 pop_tinst_level ();
12138
12139 /* The vtable for a template class can be emitted in any translation
12140 unit in which the class is instantiated. When there is no key
12141 method, however, finish_struct_1 will already have added TYPE to
12142 the keyed_classes. */
12143 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12144 vec_safe_push (keyed_classes, type);
12145
12146 /* Now that we've gone through all the members, instantiate those
12147 marked with attribute used. */
12148 unsigned int i;
12149 tree x;
12150 FOR_EACH_VEC_ELT (used, i, x)
12151 mark_used (x);
12152
12153 return type;
12154 }
12155
12156 /* Wrapper for instantiate_class_template_1. */
12157
12158 tree
instantiate_class_template(tree type)12159 instantiate_class_template (tree type)
12160 {
12161 tree ret;
12162 timevar_push (TV_TEMPLATE_INST);
12163 ret = instantiate_class_template_1 (type);
12164 timevar_pop (TV_TEMPLATE_INST);
12165 return ret;
12166 }
12167
12168 tree
tsubst_template_arg(tree t,tree args,tsubst_flags_t complain,tree in_decl)12169 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12170 {
12171 tree r;
12172
12173 if (!t)
12174 r = t;
12175 else if (TYPE_P (t))
12176 r = tsubst (t, args, complain, in_decl);
12177 else
12178 {
12179 if (!(complain & tf_warning))
12180 ++c_inhibit_evaluation_warnings;
12181 r = tsubst_expr (t, args, complain, in_decl,
12182 /*integral_constant_expression_p=*/true);
12183 if (!(complain & tf_warning))
12184 --c_inhibit_evaluation_warnings;
12185 }
12186
12187 return r;
12188 }
12189
12190 /* Given a function parameter pack TMPL_PARM and some function parameters
12191 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12192 and set *SPEC_P to point at the next point in the list. */
12193
12194 tree
extract_fnparm_pack(tree tmpl_parm,tree * spec_p)12195 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12196 {
12197 /* Collect all of the extra "packed" parameters into an
12198 argument pack. */
12199 tree argpack;
12200 tree spec_parm = *spec_p;
12201 int len;
12202
12203 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12204 if (tmpl_parm
12205 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12206 break;
12207
12208 spec_parm = *spec_p;
12209 if (len == 1 && DECL_PACK_P (spec_parm))
12210 {
12211 /* The instantiation is still a parameter pack; don't wrap it in a
12212 NONTYPE_ARGUMENT_PACK. */
12213 argpack = spec_parm;
12214 spec_parm = DECL_CHAIN (spec_parm);
12215 }
12216 else
12217 {
12218 /* Fill in PARMVEC with all of the parameters. */
12219 tree parmvec = make_tree_vec (len);
12220 argpack = make_node (NONTYPE_ARGUMENT_PACK);
12221 for (int i = 0; i < len; i++)
12222 {
12223 tree elt = spec_parm;
12224 if (DECL_PACK_P (elt))
12225 elt = make_pack_expansion (elt);
12226 TREE_VEC_ELT (parmvec, i) = elt;
12227 spec_parm = DECL_CHAIN (spec_parm);
12228 }
12229
12230 /* Build the argument packs. */
12231 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12232 }
12233 *spec_p = spec_parm;
12234
12235 return argpack;
12236 }
12237
12238 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12239 NONTYPE_ARGUMENT_PACK. */
12240
12241 static tree
make_fnparm_pack(tree spec_parm)12242 make_fnparm_pack (tree spec_parm)
12243 {
12244 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12245 }
12246
12247 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12248 pack expansion with no extra args, 2 if it has extra args, or 0
12249 if it is not a pack expansion. */
12250
12251 static int
argument_pack_element_is_expansion_p(tree arg_pack,int i)12252 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12253 {
12254 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12255 /* We're being called before this happens in tsubst_pack_expansion. */
12256 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12257 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12258 if (i >= TREE_VEC_LENGTH (vec))
12259 return 0;
12260 tree elt = TREE_VEC_ELT (vec, i);
12261 if (DECL_P (elt))
12262 /* A decl pack is itself an expansion. */
12263 elt = TREE_TYPE (elt);
12264 if (!PACK_EXPANSION_P (elt))
12265 return 0;
12266 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12267 return 2;
12268 return 1;
12269 }
12270
12271
12272 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12273
12274 static tree
make_argument_pack_select(tree arg_pack,unsigned index)12275 make_argument_pack_select (tree arg_pack, unsigned index)
12276 {
12277 tree aps = make_node (ARGUMENT_PACK_SELECT);
12278
12279 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12280 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12281
12282 return aps;
12283 }
12284
12285 /* This is a subroutine of tsubst_pack_expansion.
12286
12287 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12288 mechanism to store the (non complete list of) arguments of the
12289 substitution and return a non substituted pack expansion, in order
12290 to wait for when we have enough arguments to really perform the
12291 substitution. */
12292
12293 static bool
use_pack_expansion_extra_args_p(tree parm_packs,int arg_pack_len,bool has_empty_arg)12294 use_pack_expansion_extra_args_p (tree parm_packs,
12295 int arg_pack_len,
12296 bool has_empty_arg)
12297 {
12298 /* If one pack has an expansion and another pack has a normal
12299 argument or if one pack has an empty argument and an another
12300 one hasn't then tsubst_pack_expansion cannot perform the
12301 substitution and need to fall back on the
12302 PACK_EXPANSION_EXTRA mechanism. */
12303 if (parm_packs == NULL_TREE)
12304 return false;
12305 else if (has_empty_arg)
12306 {
12307 /* If all the actual packs are pack expansions, we can still
12308 subsitute directly. */
12309 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12310 {
12311 tree a = TREE_VALUE (p);
12312 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12313 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12314 a = ARGUMENT_PACK_ARGS (a);
12315 if (TREE_VEC_LENGTH (a) == 1)
12316 a = TREE_VEC_ELT (a, 0);
12317 if (PACK_EXPANSION_P (a))
12318 continue;
12319 return true;
12320 }
12321 return false;
12322 }
12323
12324 for (int i = 0 ; i < arg_pack_len; ++i)
12325 {
12326 bool has_expansion_arg = false;
12327 bool has_non_expansion_arg = false;
12328 for (tree parm_pack = parm_packs;
12329 parm_pack;
12330 parm_pack = TREE_CHAIN (parm_pack))
12331 {
12332 tree arg = TREE_VALUE (parm_pack);
12333
12334 int exp = argument_pack_element_is_expansion_p (arg, i);
12335 if (exp == 2)
12336 /* We can't substitute a pack expansion with extra args into
12337 our pattern. */
12338 return true;
12339 else if (exp)
12340 has_expansion_arg = true;
12341 else
12342 has_non_expansion_arg = true;
12343 }
12344
12345 if (has_expansion_arg && has_non_expansion_arg)
12346 {
12347 gcc_checking_assert (false);
12348 return true;
12349 }
12350 }
12351 return false;
12352 }
12353
12354 /* [temp.variadic]/6 says that:
12355
12356 The instantiation of a pack expansion [...]
12357 produces a list E1,E2, ..., En, where N is the number of elements
12358 in the pack expansion parameters.
12359
12360 This subroutine of tsubst_pack_expansion produces one of these Ei.
12361
12362 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12363 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12364 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12365 INDEX is the index 'i' of the element Ei to produce. ARGS,
12366 COMPLAIN, and IN_DECL are the same parameters as for the
12367 tsubst_pack_expansion function.
12368
12369 The function returns the resulting Ei upon successful completion,
12370 or error_mark_node.
12371
12372 Note that this function possibly modifies the ARGS parameter, so
12373 it's the responsibility of the caller to restore it. */
12374
12375 static tree
gen_elem_of_pack_expansion_instantiation(tree pattern,tree parm_packs,unsigned index,tree args,tsubst_flags_t complain,tree in_decl)12376 gen_elem_of_pack_expansion_instantiation (tree pattern,
12377 tree parm_packs,
12378 unsigned index,
12379 tree args /* This parm gets
12380 modified. */,
12381 tsubst_flags_t complain,
12382 tree in_decl)
12383 {
12384 tree t;
12385 bool ith_elem_is_expansion = false;
12386
12387 /* For each parameter pack, change the substitution of the parameter
12388 pack to the ith argument in its argument pack, then expand the
12389 pattern. */
12390 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12391 {
12392 tree parm = TREE_PURPOSE (pack);
12393 tree arg_pack = TREE_VALUE (pack);
12394 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12395
12396 ith_elem_is_expansion |=
12397 argument_pack_element_is_expansion_p (arg_pack, index);
12398
12399 /* Select the Ith argument from the pack. */
12400 if (TREE_CODE (parm) == PARM_DECL
12401 || VAR_P (parm)
12402 || TREE_CODE (parm) == FIELD_DECL)
12403 {
12404 if (index == 0)
12405 {
12406 aps = make_argument_pack_select (arg_pack, index);
12407 if (!mark_used (parm, complain) && !(complain & tf_error))
12408 return error_mark_node;
12409 register_local_specialization (aps, parm);
12410 }
12411 else
12412 aps = retrieve_local_specialization (parm);
12413 }
12414 else
12415 {
12416 int idx, level;
12417 template_parm_level_and_index (parm, &level, &idx);
12418
12419 if (index == 0)
12420 {
12421 aps = make_argument_pack_select (arg_pack, index);
12422 /* Update the corresponding argument. */
12423 TMPL_ARG (args, level, idx) = aps;
12424 }
12425 else
12426 /* Re-use the ARGUMENT_PACK_SELECT. */
12427 aps = TMPL_ARG (args, level, idx);
12428 }
12429 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12430 }
12431
12432 /* Substitute into the PATTERN with the (possibly altered)
12433 arguments. */
12434 if (pattern == in_decl)
12435 /* Expanding a fixed parameter pack from
12436 coerce_template_parameter_pack. */
12437 t = tsubst_decl (pattern, args, complain);
12438 else if (pattern == error_mark_node)
12439 t = error_mark_node;
12440 else if (!TYPE_P (pattern))
12441 t = tsubst_expr (pattern, args, complain, in_decl,
12442 /*integral_constant_expression_p=*/false);
12443 else
12444 t = tsubst (pattern, args, complain, in_decl);
12445
12446 /* If the Ith argument pack element is a pack expansion, then
12447 the Ith element resulting from the substituting is going to
12448 be a pack expansion as well. */
12449 if (ith_elem_is_expansion)
12450 t = make_pack_expansion (t, complain);
12451
12452 return t;
12453 }
12454
12455 /* When the unexpanded parameter pack in a fold expression expands to an empty
12456 sequence, the value of the expression is as follows; the program is
12457 ill-formed if the operator is not listed in this table.
12458
12459 && true
12460 || false
12461 , void() */
12462
12463 tree
expand_empty_fold(tree t,tsubst_flags_t complain)12464 expand_empty_fold (tree t, tsubst_flags_t complain)
12465 {
12466 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12467 if (!FOLD_EXPR_MODIFY_P (t))
12468 switch (code)
12469 {
12470 case TRUTH_ANDIF_EXPR:
12471 return boolean_true_node;
12472 case TRUTH_ORIF_EXPR:
12473 return boolean_false_node;
12474 case COMPOUND_EXPR:
12475 return void_node;
12476 default:
12477 break;
12478 }
12479
12480 if (complain & tf_error)
12481 error_at (location_of (t),
12482 "fold of empty expansion over %O", code);
12483 return error_mark_node;
12484 }
12485
12486 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12487 form an expression that combines the two terms using the
12488 operator of T. */
12489
12490 static tree
fold_expression(tree t,tree left,tree right,tsubst_flags_t complain)12491 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12492 {
12493 tree op = FOLD_EXPR_OP (t);
12494 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12495
12496 // Handle compound assignment operators.
12497 if (FOLD_EXPR_MODIFY_P (t))
12498 return build_x_modify_expr (input_location, left, code, right, complain);
12499
12500 warning_sentinel s(warn_parentheses);
12501 switch (code)
12502 {
12503 case COMPOUND_EXPR:
12504 return build_x_compound_expr (input_location, left, right, complain);
12505 default:
12506 return build_x_binary_op (input_location, code,
12507 left, TREE_CODE (left),
12508 right, TREE_CODE (right),
12509 /*overload=*/NULL,
12510 complain);
12511 }
12512 }
12513
12514 /* Substitute ARGS into the pack of a fold expression T. */
12515
12516 static inline tree
tsubst_fold_expr_pack(tree t,tree args,tsubst_flags_t complain,tree in_decl)12517 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12518 {
12519 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12520 }
12521
12522 /* Substitute ARGS into the pack of a fold expression T. */
12523
12524 static inline tree
tsubst_fold_expr_init(tree t,tree args,tsubst_flags_t complain,tree in_decl)12525 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12526 {
12527 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12528 }
12529
12530 /* Expand a PACK of arguments into a grouped as left fold.
12531 Given a pack containing elements A0, A1, ..., An and an
12532 operator @, this builds the expression:
12533
12534 ((A0 @ A1) @ A2) ... @ An
12535
12536 Note that PACK must not be empty.
12537
12538 The operator is defined by the original fold expression T. */
12539
12540 static tree
expand_left_fold(tree t,tree pack,tsubst_flags_t complain)12541 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12542 {
12543 tree left = TREE_VEC_ELT (pack, 0);
12544 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12545 {
12546 tree right = TREE_VEC_ELT (pack, i);
12547 left = fold_expression (t, left, right, complain);
12548 }
12549 return left;
12550 }
12551
12552 /* Substitute into a unary left fold expression. */
12553
12554 static tree
tsubst_unary_left_fold(tree t,tree args,tsubst_flags_t complain,tree in_decl)12555 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12556 tree in_decl)
12557 {
12558 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12559 if (pack == error_mark_node)
12560 return error_mark_node;
12561 if (PACK_EXPANSION_P (pack))
12562 {
12563 tree r = copy_node (t);
12564 FOLD_EXPR_PACK (r) = pack;
12565 return r;
12566 }
12567 if (TREE_VEC_LENGTH (pack) == 0)
12568 return expand_empty_fold (t, complain);
12569 else
12570 return expand_left_fold (t, pack, complain);
12571 }
12572
12573 /* Substitute into a binary left fold expression.
12574
12575 Do ths by building a single (non-empty) vector of argumnts and
12576 building the expression from those elements. */
12577
12578 static tree
tsubst_binary_left_fold(tree t,tree args,tsubst_flags_t complain,tree in_decl)12579 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12580 tree in_decl)
12581 {
12582 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12583 if (pack == error_mark_node)
12584 return error_mark_node;
12585 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12586 if (init == error_mark_node)
12587 return error_mark_node;
12588
12589 if (PACK_EXPANSION_P (pack))
12590 {
12591 tree r = copy_node (t);
12592 FOLD_EXPR_PACK (r) = pack;
12593 FOLD_EXPR_INIT (r) = init;
12594 return r;
12595 }
12596
12597 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12598 TREE_VEC_ELT (vec, 0) = init;
12599 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12600 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12601
12602 return expand_left_fold (t, vec, complain);
12603 }
12604
12605 /* Expand a PACK of arguments into a grouped as right fold.
12606 Given a pack containing elementns A0, A1, ..., and an
12607 operator @, this builds the expression:
12608
12609 A0@ ... (An-2 @ (An-1 @ An))
12610
12611 Note that PACK must not be empty.
12612
12613 The operator is defined by the original fold expression T. */
12614
12615 tree
expand_right_fold(tree t,tree pack,tsubst_flags_t complain)12616 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12617 {
12618 // Build the expression.
12619 int n = TREE_VEC_LENGTH (pack);
12620 tree right = TREE_VEC_ELT (pack, n - 1);
12621 for (--n; n != 0; --n)
12622 {
12623 tree left = TREE_VEC_ELT (pack, n - 1);
12624 right = fold_expression (t, left, right, complain);
12625 }
12626 return right;
12627 }
12628
12629 /* Substitute into a unary right fold expression. */
12630
12631 static tree
tsubst_unary_right_fold(tree t,tree args,tsubst_flags_t complain,tree in_decl)12632 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12633 tree in_decl)
12634 {
12635 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12636 if (pack == error_mark_node)
12637 return error_mark_node;
12638 if (PACK_EXPANSION_P (pack))
12639 {
12640 tree r = copy_node (t);
12641 FOLD_EXPR_PACK (r) = pack;
12642 return r;
12643 }
12644 if (TREE_VEC_LENGTH (pack) == 0)
12645 return expand_empty_fold (t, complain);
12646 else
12647 return expand_right_fold (t, pack, complain);
12648 }
12649
12650 /* Substitute into a binary right fold expression.
12651
12652 Do ths by building a single (non-empty) vector of arguments and
12653 building the expression from those elements. */
12654
12655 static tree
tsubst_binary_right_fold(tree t,tree args,tsubst_flags_t complain,tree in_decl)12656 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12657 tree in_decl)
12658 {
12659 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12660 if (pack == error_mark_node)
12661 return error_mark_node;
12662 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12663 if (init == error_mark_node)
12664 return error_mark_node;
12665
12666 if (PACK_EXPANSION_P (pack))
12667 {
12668 tree r = copy_node (t);
12669 FOLD_EXPR_PACK (r) = pack;
12670 FOLD_EXPR_INIT (r) = init;
12671 return r;
12672 }
12673
12674 int n = TREE_VEC_LENGTH (pack);
12675 tree vec = make_tree_vec (n + 1);
12676 for (int i = 0; i < n; ++i)
12677 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12678 TREE_VEC_ELT (vec, n) = init;
12679
12680 return expand_right_fold (t, vec, complain);
12681 }
12682
12683 /* Walk through the pattern of a pack expansion, adding everything in
12684 local_specializations to a list. */
12685
12686 class el_data
12687 {
12688 public:
12689 /* Set of variables declared within the pattern. */
12690 hash_set<tree> internal;
12691 /* Set of AST nodes that have been visited by the traversal. */
12692 hash_set<tree> visited;
12693 /* List of local_specializations used within the pattern. */
12694 tree extra;
12695 tsubst_flags_t complain;
12696
el_data(tsubst_flags_t c)12697 el_data (tsubst_flags_t c)
12698 : extra (NULL_TREE), complain (c) {}
12699 };
12700 static tree
extract_locals_r(tree * tp,int *,void * data_)12701 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12702 {
12703 el_data &data = *reinterpret_cast<el_data*>(data_);
12704 tree *extra = &data.extra;
12705 tsubst_flags_t complain = data.complain;
12706
12707 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12708 /* Remember local typedefs (85214). */
12709 tp = &TYPE_NAME (*tp);
12710
12711 if (TREE_CODE (*tp) == DECL_EXPR)
12712 {
12713 tree decl = DECL_EXPR_DECL (*tp);
12714 data.internal.add (decl);
12715 if (VAR_P (decl)
12716 && DECL_DECOMPOSITION_P (decl)
12717 && TREE_TYPE (decl) != error_mark_node)
12718 {
12719 gcc_assert (DECL_NAME (decl) == NULL_TREE);
12720 for (tree decl2 = DECL_CHAIN (decl);
12721 decl2
12722 && VAR_P (decl2)
12723 && DECL_DECOMPOSITION_P (decl2)
12724 && DECL_NAME (decl2)
12725 && TREE_TYPE (decl2) != error_mark_node;
12726 decl2 = DECL_CHAIN (decl2))
12727 {
12728 gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
12729 data.internal.add (decl2);
12730 }
12731 }
12732 }
12733 else if (TREE_CODE (*tp) == LAMBDA_EXPR)
12734 {
12735 /* Since we defer implicit capture, look in the parms and body. */
12736 tree fn = lambda_function (*tp);
12737 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
12738 &data.visited);
12739 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
12740 &data.visited);
12741 }
12742 else if (tree spec = retrieve_local_specialization (*tp))
12743 {
12744 if (data.internal.contains (*tp))
12745 /* Don't mess with variables declared within the pattern. */
12746 return NULL_TREE;
12747 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12748 {
12749 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12750 tree args = ARGUMENT_PACK_ARGS (spec);
12751 if (TREE_VEC_LENGTH (args) == 1)
12752 {
12753 tree elt = TREE_VEC_ELT (args, 0);
12754 if (PACK_EXPANSION_P (elt))
12755 elt = PACK_EXPANSION_PATTERN (elt);
12756 if (DECL_PACK_P (elt))
12757 spec = elt;
12758 }
12759 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12760 {
12761 /* Handle lambda capture here, since we aren't doing any
12762 substitution now, and so tsubst_copy won't call
12763 process_outer_var_ref. */
12764 tree args = ARGUMENT_PACK_ARGS (spec);
12765 int len = TREE_VEC_LENGTH (args);
12766 for (int i = 0; i < len; ++i)
12767 {
12768 tree arg = TREE_VEC_ELT (args, i);
12769 tree carg = arg;
12770 if (outer_automatic_var_p (arg))
12771 carg = process_outer_var_ref (arg, complain);
12772 if (carg != arg)
12773 {
12774 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12775 proxies. */
12776 if (i == 0)
12777 {
12778 spec = copy_node (spec);
12779 args = copy_node (args);
12780 SET_ARGUMENT_PACK_ARGS (spec, args);
12781 register_local_specialization (spec, *tp);
12782 }
12783 TREE_VEC_ELT (args, i) = carg;
12784 }
12785 }
12786 }
12787 }
12788 if (outer_automatic_var_p (spec))
12789 spec = process_outer_var_ref (spec, complain);
12790 *extra = tree_cons (*tp, spec, *extra);
12791 }
12792 return NULL_TREE;
12793 }
12794 static tree
extract_local_specs(tree pattern,tsubst_flags_t complain)12795 extract_local_specs (tree pattern, tsubst_flags_t complain)
12796 {
12797 el_data data (complain);
12798 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
12799 return data.extra;
12800 }
12801
12802 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12803 for use in PACK_EXPANSION_EXTRA_ARGS. */
12804
12805 tree
build_extra_args(tree pattern,tree args,tsubst_flags_t complain)12806 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12807 {
12808 tree extra = args;
12809 if (local_specializations)
12810 if (tree locals = extract_local_specs (pattern, complain))
12811 extra = tree_cons (NULL_TREE, extra, locals);
12812 return extra;
12813 }
12814
12815 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12816 normal template args to ARGS. */
12817
12818 tree
add_extra_args(tree extra,tree args)12819 add_extra_args (tree extra, tree args)
12820 {
12821 if (extra && TREE_CODE (extra) == TREE_LIST)
12822 {
12823 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12824 {
12825 /* The partial instantiation involved local declarations collected in
12826 extract_local_specs; map from the general template to our local
12827 context. */
12828 tree gen = TREE_PURPOSE (elt);
12829 tree inst = TREE_VALUE (elt);
12830 if (DECL_P (inst))
12831 if (tree local = retrieve_local_specialization (inst))
12832 inst = local;
12833 /* else inst is already a full instantiation of the pack. */
12834 register_local_specialization (inst, gen);
12835 }
12836 gcc_assert (!TREE_PURPOSE (extra));
12837 extra = TREE_VALUE (extra);
12838 }
12839 #if 1
12840 /* I think we should always be able to substitute dependent args into the
12841 pattern. If that turns out to be incorrect in some cases, enable the
12842 alternate code (and add complain/in_decl parms to this function). */
12843 gcc_checking_assert (!uses_template_parms (extra));
12844 #else
12845 if (!uses_template_parms (extra))
12846 {
12847 gcc_unreachable ();
12848 extra = tsubst_template_args (extra, args, complain, in_decl);
12849 args = add_outermost_template_args (args, extra);
12850 }
12851 else
12852 #endif
12853 args = add_to_template_args (extra, args);
12854 return args;
12855 }
12856
12857 /* Substitute ARGS into T, which is an pack expansion
12858 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12859 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12860 (if only a partial substitution could be performed) or
12861 ERROR_MARK_NODE if there was an error. */
12862 tree
tsubst_pack_expansion(tree t,tree args,tsubst_flags_t complain,tree in_decl)12863 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12864 tree in_decl)
12865 {
12866 tree pattern;
12867 tree pack, packs = NULL_TREE;
12868 bool unsubstituted_packs = false;
12869 int i, len = -1;
12870 tree result;
12871 bool need_local_specializations = false;
12872 int levels;
12873
12874 gcc_assert (PACK_EXPANSION_P (t));
12875 pattern = PACK_EXPANSION_PATTERN (t);
12876
12877 /* Add in any args remembered from an earlier partial instantiation. */
12878 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12879
12880 levels = TMPL_ARGS_DEPTH (args);
12881
12882 /* Determine the argument packs that will instantiate the parameter
12883 packs used in the expansion expression. While we're at it,
12884 compute the number of arguments to be expanded and make sure it
12885 is consistent. */
12886 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12887 pack = TREE_CHAIN (pack))
12888 {
12889 tree parm_pack = TREE_VALUE (pack);
12890 tree arg_pack = NULL_TREE;
12891 tree orig_arg = NULL_TREE;
12892 int level = 0;
12893
12894 if (TREE_CODE (parm_pack) == BASES)
12895 {
12896 gcc_assert (parm_pack == pattern);
12897 if (BASES_DIRECT (parm_pack))
12898 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12899 args, complain,
12900 in_decl, false),
12901 complain);
12902 else
12903 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12904 args, complain, in_decl,
12905 false), complain);
12906 }
12907 else if (builtin_pack_call_p (parm_pack))
12908 {
12909 if (parm_pack != pattern)
12910 {
12911 if (complain & tf_error)
12912 sorry ("%qE is not the entire pattern of the pack expansion",
12913 parm_pack);
12914 return error_mark_node;
12915 }
12916 return expand_builtin_pack_call (parm_pack, args,
12917 complain, in_decl);
12918 }
12919 else if (TREE_CODE (parm_pack) == PARM_DECL)
12920 {
12921 /* We know we have correct local_specializations if this
12922 expansion is at function scope, or if we're dealing with a
12923 local parameter in a requires expression; for the latter,
12924 tsubst_requires_expr set it up appropriately. */
12925 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12926 arg_pack = retrieve_local_specialization (parm_pack);
12927 else
12928 /* We can't rely on local_specializations for a parameter
12929 name used later in a function declaration (such as in a
12930 late-specified return type). Even if it exists, it might
12931 have the wrong value for a recursive call. */
12932 need_local_specializations = true;
12933
12934 if (!arg_pack)
12935 {
12936 /* This parameter pack was used in an unevaluated context. Just
12937 make a dummy decl, since it's only used for its type. */
12938 ++cp_unevaluated_operand;
12939 arg_pack = tsubst_decl (parm_pack, args, complain);
12940 --cp_unevaluated_operand;
12941 if (arg_pack && DECL_PACK_P (arg_pack))
12942 /* Partial instantiation of the parm_pack, we can't build
12943 up an argument pack yet. */
12944 arg_pack = NULL_TREE;
12945 else
12946 arg_pack = make_fnparm_pack (arg_pack);
12947 }
12948 else if (DECL_PACK_P (arg_pack))
12949 /* This argument pack isn't fully instantiated yet. */
12950 arg_pack = NULL_TREE;
12951 }
12952 else if (is_capture_proxy (parm_pack))
12953 {
12954 arg_pack = retrieve_local_specialization (parm_pack);
12955 if (DECL_PACK_P (arg_pack))
12956 arg_pack = NULL_TREE;
12957 }
12958 else
12959 {
12960 int idx;
12961 template_parm_level_and_index (parm_pack, &level, &idx);
12962 if (level <= levels)
12963 arg_pack = TMPL_ARG (args, level, idx);
12964
12965 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12966 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12967 arg_pack = NULL_TREE;
12968 }
12969
12970 orig_arg = arg_pack;
12971 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12972 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12973
12974 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12975 /* This can only happen if we forget to expand an argument
12976 pack somewhere else. Just return an error, silently. */
12977 {
12978 result = make_tree_vec (1);
12979 TREE_VEC_ELT (result, 0) = error_mark_node;
12980 return result;
12981 }
12982
12983 if (arg_pack)
12984 {
12985 int my_len =
12986 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12987
12988 /* Don't bother trying to do a partial substitution with
12989 incomplete packs; we'll try again after deduction. */
12990 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12991 return t;
12992
12993 if (len < 0)
12994 len = my_len;
12995 else if (len != my_len)
12996 {
12997 if (!(complain & tf_error))
12998 /* Fail quietly. */;
12999 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13000 error ("mismatched argument pack lengths while expanding %qT",
13001 pattern);
13002 else
13003 error ("mismatched argument pack lengths while expanding %qE",
13004 pattern);
13005 return error_mark_node;
13006 }
13007
13008 /* Keep track of the parameter packs and their corresponding
13009 argument packs. */
13010 packs = tree_cons (parm_pack, arg_pack, packs);
13011 TREE_TYPE (packs) = orig_arg;
13012 }
13013 else
13014 {
13015 /* We can't substitute for this parameter pack. We use a flag as
13016 well as the missing_level counter because function parameter
13017 packs don't have a level. */
13018 gcc_assert (processing_template_decl || is_auto (parm_pack));
13019 unsubstituted_packs = true;
13020 }
13021 }
13022
13023 /* If the expansion is just T..., return the matching argument pack, unless
13024 we need to call convert_from_reference on all the elements. This is an
13025 important optimization; see c++/68422. */
13026 if (!unsubstituted_packs
13027 && TREE_PURPOSE (packs) == pattern)
13028 {
13029 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13030
13031 /* If the argument pack is a single pack expansion, pull it out. */
13032 if (TREE_VEC_LENGTH (args) == 1
13033 && pack_expansion_args_count (args))
13034 return TREE_VEC_ELT (args, 0);
13035
13036 /* Types need no adjustment, nor does sizeof..., and if we still have
13037 some pack expansion args we won't do anything yet. */
13038 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13039 || PACK_EXPANSION_SIZEOF_P (t)
13040 || pack_expansion_args_count (args))
13041 return args;
13042 /* Also optimize expression pack expansions if we can tell that the
13043 elements won't have reference type. */
13044 tree type = TREE_TYPE (pattern);
13045 if (type && !TYPE_REF_P (type)
13046 && !PACK_EXPANSION_P (type)
13047 && !WILDCARD_TYPE_P (type))
13048 return args;
13049 /* Otherwise use the normal path so we get convert_from_reference. */
13050 }
13051
13052 /* We cannot expand this expansion expression, because we don't have
13053 all of the argument packs we need. */
13054 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13055 {
13056 /* We got some full packs, but we can't substitute them in until we
13057 have values for all the packs. So remember these until then. */
13058
13059 t = make_pack_expansion (pattern, complain);
13060 PACK_EXPANSION_EXTRA_ARGS (t)
13061 = build_extra_args (pattern, args, complain);
13062 return t;
13063 }
13064
13065 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13066 type, so create our own local specializations map; the current map is
13067 either NULL or (in the case of recursive unification) might have
13068 bindings that we don't want to use or alter. */
13069 local_specialization_stack lss (need_local_specializations
13070 ? lss_blank : lss_nop);
13071
13072 if (unsubstituted_packs)
13073 {
13074 /* There were no real arguments, we're just replacing a parameter
13075 pack with another version of itself. Substitute into the
13076 pattern and return a PACK_EXPANSION_*. The caller will need to
13077 deal with that. */
13078 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13079 result = tsubst_expr (pattern, args, complain, in_decl,
13080 /*integral_constant_expression_p=*/false);
13081 else
13082 result = tsubst (pattern, args, complain, in_decl);
13083 result = make_pack_expansion (result, complain);
13084 if (PACK_EXPANSION_AUTO_P (t))
13085 {
13086 /* This is a fake auto... pack expansion created in add_capture with
13087 _PACKS that don't appear in the pattern. Copy one over. */
13088 packs = PACK_EXPANSION_PARAMETER_PACKS (t);
13089 pack = retrieve_local_specialization (TREE_VALUE (packs));
13090 gcc_checking_assert (DECL_PACK_P (pack));
13091 PACK_EXPANSION_PARAMETER_PACKS (result)
13092 = build_tree_list (NULL_TREE, pack);
13093 PACK_EXPANSION_AUTO_P (result) = true;
13094 }
13095 return result;
13096 }
13097
13098 gcc_assert (len >= 0);
13099
13100 /* For each argument in each argument pack, substitute into the
13101 pattern. */
13102 result = make_tree_vec (len);
13103 tree elem_args = copy_template_args (args);
13104 for (i = 0; i < len; ++i)
13105 {
13106 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13107 i,
13108 elem_args, complain,
13109 in_decl);
13110 TREE_VEC_ELT (result, i) = t;
13111 if (t == error_mark_node)
13112 {
13113 result = error_mark_node;
13114 break;
13115 }
13116 }
13117
13118 /* Update ARGS to restore the substitution from parameter packs to
13119 their argument packs. */
13120 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13121 {
13122 tree parm = TREE_PURPOSE (pack);
13123
13124 if (TREE_CODE (parm) == PARM_DECL
13125 || VAR_P (parm)
13126 || TREE_CODE (parm) == FIELD_DECL)
13127 register_local_specialization (TREE_TYPE (pack), parm);
13128 else
13129 {
13130 int idx, level;
13131
13132 if (TREE_VALUE (pack) == NULL_TREE)
13133 continue;
13134
13135 template_parm_level_and_index (parm, &level, &idx);
13136
13137 /* Update the corresponding argument. */
13138 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13139 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13140 TREE_TYPE (pack);
13141 else
13142 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13143 }
13144 }
13145
13146 /* If the dependent pack arguments were such that we end up with only a
13147 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13148 if (len == 1 && TREE_CODE (result) == TREE_VEC
13149 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13150 return TREE_VEC_ELT (result, 0);
13151
13152 return result;
13153 }
13154
13155 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13156 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13157 parameter packs; all parms generated from a function parameter pack will
13158 have the same DECL_PARM_INDEX. */
13159
13160 tree
get_pattern_parm(tree parm,tree tmpl)13161 get_pattern_parm (tree parm, tree tmpl)
13162 {
13163 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13164 tree patparm;
13165
13166 if (DECL_ARTIFICIAL (parm))
13167 {
13168 for (patparm = DECL_ARGUMENTS (pattern);
13169 patparm; patparm = DECL_CHAIN (patparm))
13170 if (DECL_ARTIFICIAL (patparm)
13171 && DECL_NAME (parm) == DECL_NAME (patparm))
13172 break;
13173 }
13174 else
13175 {
13176 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13177 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13178 gcc_assert (DECL_PARM_INDEX (patparm)
13179 == DECL_PARM_INDEX (parm));
13180 }
13181
13182 return patparm;
13183 }
13184
13185 /* Make an argument pack out of the TREE_VEC VEC. */
13186
13187 static tree
make_argument_pack(tree vec)13188 make_argument_pack (tree vec)
13189 {
13190 tree pack;
13191 tree elt = TREE_VEC_ELT (vec, 0);
13192 if (TYPE_P (elt))
13193 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13194 else
13195 {
13196 pack = make_node (NONTYPE_ARGUMENT_PACK);
13197 TREE_CONSTANT (pack) = 1;
13198 }
13199 SET_ARGUMENT_PACK_ARGS (pack, vec);
13200 return pack;
13201 }
13202
13203 /* Return an exact copy of template args T that can be modified
13204 independently. */
13205
13206 static tree
copy_template_args(tree t)13207 copy_template_args (tree t)
13208 {
13209 if (t == error_mark_node)
13210 return t;
13211
13212 int len = TREE_VEC_LENGTH (t);
13213 tree new_vec = make_tree_vec (len);
13214
13215 for (int i = 0; i < len; ++i)
13216 {
13217 tree elt = TREE_VEC_ELT (t, i);
13218 if (elt && TREE_CODE (elt) == TREE_VEC)
13219 elt = copy_template_args (elt);
13220 TREE_VEC_ELT (new_vec, i) = elt;
13221 }
13222
13223 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13224 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13225
13226 return new_vec;
13227 }
13228
13229 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13230
13231 tree
tsubst_argument_pack(tree orig_arg,tree args,tsubst_flags_t complain,tree in_decl)13232 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13233 tree in_decl)
13234 {
13235 /* Substitute into each of the arguments. */
13236 tree new_arg = TYPE_P (orig_arg)
13237 ? cxx_make_type (TREE_CODE (orig_arg))
13238 : make_node (TREE_CODE (orig_arg));
13239
13240 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13241 args, complain, in_decl);
13242 if (pack_args == error_mark_node)
13243 new_arg = error_mark_node;
13244 else
13245 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13246
13247 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13248 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13249
13250 return new_arg;
13251 }
13252
13253 /* Substitute ARGS into the vector or list of template arguments T. */
13254
13255 tree
tsubst_template_args(tree t,tree args,tsubst_flags_t complain,tree in_decl)13256 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13257 {
13258 tree orig_t = t;
13259 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13260 tree *elts;
13261
13262 if (t == error_mark_node)
13263 return error_mark_node;
13264
13265 len = TREE_VEC_LENGTH (t);
13266 elts = XALLOCAVEC (tree, len);
13267
13268 for (i = 0; i < len; i++)
13269 {
13270 tree orig_arg = TREE_VEC_ELT (t, i);
13271 tree new_arg;
13272
13273 if (TREE_CODE (orig_arg) == TREE_VEC)
13274 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13275 else if (PACK_EXPANSION_P (orig_arg))
13276 {
13277 /* Substitute into an expansion expression. */
13278 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13279
13280 if (TREE_CODE (new_arg) == TREE_VEC)
13281 /* Add to the expanded length adjustment the number of
13282 expanded arguments. We subtract one from this
13283 measurement, because the argument pack expression
13284 itself is already counted as 1 in
13285 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13286 the argument pack is empty. */
13287 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13288 }
13289 else if (ARGUMENT_PACK_P (orig_arg))
13290 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13291 else
13292 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13293
13294 if (new_arg == error_mark_node)
13295 return error_mark_node;
13296
13297 elts[i] = new_arg;
13298 if (new_arg != orig_arg)
13299 need_new = 1;
13300 }
13301
13302 if (!need_new)
13303 return t;
13304
13305 /* Make space for the expanded arguments coming from template
13306 argument packs. */
13307 t = make_tree_vec (len + expanded_len_adjust);
13308 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13309 arguments for a member template.
13310 In that case each TREE_VEC in ORIG_T represents a level of template
13311 arguments, and ORIG_T won't carry any non defaulted argument count.
13312 It will rather be the nested TREE_VECs that will carry one.
13313 In other words, ORIG_T carries a non defaulted argument count only
13314 if it doesn't contain any nested TREE_VEC. */
13315 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13316 {
13317 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13318 count += expanded_len_adjust;
13319 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13320 }
13321 for (i = 0, out = 0; i < len; i++)
13322 {
13323 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13324 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13325 && TREE_CODE (elts[i]) == TREE_VEC)
13326 {
13327 int idx;
13328
13329 /* Now expand the template argument pack "in place". */
13330 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13331 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13332 }
13333 else
13334 {
13335 TREE_VEC_ELT (t, out) = elts[i];
13336 out++;
13337 }
13338 }
13339
13340 return t;
13341 }
13342
13343 /* Substitute ARGS into one level PARMS of template parameters. */
13344
13345 static tree
tsubst_template_parms_level(tree parms,tree args,tsubst_flags_t complain)13346 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13347 {
13348 if (parms == error_mark_node)
13349 return error_mark_node;
13350
13351 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13352
13353 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13354 {
13355 tree tuple = TREE_VEC_ELT (parms, i);
13356
13357 if (tuple == error_mark_node)
13358 continue;
13359
13360 TREE_VEC_ELT (new_vec, i) =
13361 tsubst_template_parm (tuple, args, complain);
13362 }
13363
13364 return new_vec;
13365 }
13366
13367 /* Return the result of substituting ARGS into the template parameters
13368 given by PARMS. If there are m levels of ARGS and m + n levels of
13369 PARMS, then the result will contain n levels of PARMS. For
13370 example, if PARMS is `template <class T> template <class U>
13371 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13372 result will be `template <int*, double, class V>'. */
13373
13374 static tree
tsubst_template_parms(tree parms,tree args,tsubst_flags_t complain)13375 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13376 {
13377 tree r = NULL_TREE;
13378 tree* new_parms;
13379
13380 /* When substituting into a template, we must set
13381 PROCESSING_TEMPLATE_DECL as the template parameters may be
13382 dependent if they are based on one-another, and the dependency
13383 predicates are short-circuit outside of templates. */
13384 ++processing_template_decl;
13385
13386 for (new_parms = &r;
13387 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13388 new_parms = &(TREE_CHAIN (*new_parms)),
13389 parms = TREE_CHAIN (parms))
13390 {
13391 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13392 args, complain);
13393 *new_parms =
13394 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13395 - TMPL_ARGS_DEPTH (args)),
13396 new_vec, NULL_TREE);
13397 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13398 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13399 }
13400
13401 --processing_template_decl;
13402
13403 return r;
13404 }
13405
13406 /* Return the result of substituting ARGS into one template parameter
13407 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13408 parameter and which TREE_PURPOSE is the default argument of the
13409 template parameter. */
13410
13411 static tree
tsubst_template_parm(tree t,tree args,tsubst_flags_t complain)13412 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13413 {
13414 tree default_value, parm_decl;
13415
13416 if (args == NULL_TREE
13417 || t == NULL_TREE
13418 || t == error_mark_node)
13419 return t;
13420
13421 gcc_assert (TREE_CODE (t) == TREE_LIST);
13422
13423 default_value = TREE_PURPOSE (t);
13424 parm_decl = TREE_VALUE (t);
13425 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13426
13427 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13428 if (TREE_CODE (parm_decl) == PARM_DECL
13429 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13430 parm_decl = error_mark_node;
13431 default_value = tsubst_template_arg (default_value, args,
13432 complain, NULL_TREE);
13433 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13434
13435 tree r = build_tree_list (default_value, parm_decl);
13436 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13437 return r;
13438 }
13439
13440 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13441 type T. If T is not an aggregate or enumeration type, it is
13442 handled as if by tsubst. IN_DECL is as for tsubst. If
13443 ENTERING_SCOPE is nonzero, T is the context for a template which
13444 we are presently tsubst'ing. Return the substituted value. */
13445
13446 static tree
tsubst_aggr_type(tree t,tree args,tsubst_flags_t complain,tree in_decl,int entering_scope)13447 tsubst_aggr_type (tree t,
13448 tree args,
13449 tsubst_flags_t complain,
13450 tree in_decl,
13451 int entering_scope)
13452 {
13453 if (t == NULL_TREE)
13454 return NULL_TREE;
13455
13456 switch (TREE_CODE (t))
13457 {
13458 case RECORD_TYPE:
13459 if (TYPE_PTRMEMFUNC_P (t))
13460 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13461
13462 /* Fall through. */
13463 case ENUMERAL_TYPE:
13464 case UNION_TYPE:
13465 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13466 {
13467 tree argvec;
13468 tree context;
13469 tree r;
13470
13471 /* In "sizeof(X<I>)" we need to evaluate "I". */
13472 cp_evaluated ev;
13473
13474 /* First, determine the context for the type we are looking
13475 up. */
13476 context = TYPE_CONTEXT (t);
13477 if (context && TYPE_P (context))
13478 {
13479 context = tsubst_aggr_type (context, args, complain,
13480 in_decl, /*entering_scope=*/1);
13481 /* If context is a nested class inside a class template,
13482 it may still need to be instantiated (c++/33959). */
13483 context = complete_type (context);
13484 }
13485
13486 /* Then, figure out what arguments are appropriate for the
13487 type we are trying to find. For example, given:
13488
13489 template <class T> struct S;
13490 template <class T, class U> void f(T, U) { S<U> su; }
13491
13492 and supposing that we are instantiating f<int, double>,
13493 then our ARGS will be {int, double}, but, when looking up
13494 S we only want {double}. */
13495 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13496 complain, in_decl);
13497 if (argvec == error_mark_node)
13498 r = error_mark_node;
13499 else if (!entering_scope
13500 && cxx_dialect >= cxx2a && dependent_scope_p (context))
13501 {
13502 /* See maybe_dependent_member_ref. */
13503 tree name = TYPE_IDENTIFIER (t);
13504 tree fullname = name;
13505 if (instantiates_primary_template_p (t))
13506 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13507 INNERMOST_TEMPLATE_ARGS (argvec));
13508 return build_typename_type (context, name, fullname,
13509 typename_type);
13510 }
13511 else
13512 {
13513 r = lookup_template_class (t, argvec, in_decl, context,
13514 entering_scope, complain);
13515 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13516 }
13517
13518 return r;
13519 }
13520 else
13521 /* This is not a template type, so there's nothing to do. */
13522 return t;
13523
13524 default:
13525 return tsubst (t, args, complain, in_decl);
13526 }
13527 }
13528
13529 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13530
13531 /* Substitute into the default argument ARG (a default argument for
13532 FN), which has the indicated TYPE. */
13533
13534 tree
tsubst_default_argument(tree fn,int parmnum,tree type,tree arg,tsubst_flags_t complain)13535 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13536 tsubst_flags_t complain)
13537 {
13538 int errs = errorcount + sorrycount;
13539
13540 /* This can happen in invalid code. */
13541 if (TREE_CODE (arg) == DEFERRED_PARSE)
13542 return arg;
13543
13544 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13545 parm = chain_index (parmnum, parm);
13546 tree parmtype = TREE_TYPE (parm);
13547 if (DECL_BY_REFERENCE (parm))
13548 parmtype = TREE_TYPE (parmtype);
13549 if (parmtype == error_mark_node)
13550 return error_mark_node;
13551
13552 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13553
13554 tree *slot;
13555 if (defarg_inst && (slot = defarg_inst->get (parm)))
13556 return *slot;
13557
13558 /* This default argument came from a template. Instantiate the
13559 default argument here, not in tsubst. In the case of
13560 something like:
13561
13562 template <class T>
13563 struct S {
13564 static T t();
13565 void f(T = t());
13566 };
13567
13568 we must be careful to do name lookup in the scope of S<T>,
13569 rather than in the current class. */
13570 push_to_top_level ();
13571 push_access_scope (fn);
13572 push_deferring_access_checks (dk_no_deferred);
13573 start_lambda_scope (parm);
13574
13575 /* The default argument expression may cause implicitly defined
13576 member functions to be synthesized, which will result in garbage
13577 collection. We must treat this situation as if we were within
13578 the body of function so as to avoid collecting live data on the
13579 stack. */
13580 ++function_depth;
13581 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13582 complain, NULL_TREE,
13583 /*integral_constant_expression_p=*/false);
13584 --function_depth;
13585
13586 finish_lambda_scope ();
13587
13588 /* Make sure the default argument is reasonable. */
13589 arg = check_default_argument (type, arg, complain);
13590
13591 if (errorcount+sorrycount > errs
13592 && (complain & tf_warning_or_error))
13593 inform (input_location,
13594 " when instantiating default argument for call to %qD", fn);
13595
13596 pop_deferring_access_checks ();
13597 pop_access_scope (fn);
13598 pop_from_top_level ();
13599
13600 if (arg != error_mark_node && !cp_unevaluated_operand)
13601 {
13602 if (!defarg_inst)
13603 defarg_inst = decl_tree_cache_map::create_ggc (37);
13604 defarg_inst->put (parm, arg);
13605 }
13606
13607 return arg;
13608 }
13609
13610 /* Substitute into all the default arguments for FN. */
13611
13612 static void
tsubst_default_arguments(tree fn,tsubst_flags_t complain)13613 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13614 {
13615 tree arg;
13616 tree tmpl_args;
13617
13618 tmpl_args = DECL_TI_ARGS (fn);
13619
13620 /* If this function is not yet instantiated, we certainly don't need
13621 its default arguments. */
13622 if (uses_template_parms (tmpl_args))
13623 return;
13624 /* Don't do this again for clones. */
13625 if (DECL_CLONED_FUNCTION_P (fn))
13626 return;
13627
13628 int i = 0;
13629 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13630 arg;
13631 arg = TREE_CHAIN (arg), ++i)
13632 if (TREE_PURPOSE (arg))
13633 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13634 TREE_VALUE (arg),
13635 TREE_PURPOSE (arg),
13636 complain);
13637 }
13638
13639 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13640 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13641
13642 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13643
13644 void
store_explicit_specifier(tree v,tree t)13645 store_explicit_specifier (tree v, tree t)
13646 {
13647 if (!explicit_specifier_map)
13648 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13649 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13650 explicit_specifier_map->put (v, t);
13651 }
13652
13653 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13654
13655 static tree
lookup_explicit_specifier(tree v)13656 lookup_explicit_specifier (tree v)
13657 {
13658 return *explicit_specifier_map->get (v);
13659 }
13660
13661 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13662 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13663 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13664 identical to T. */
13665
13666 static tree
rebuild_function_or_method_type(tree t,tree return_type,tree arg_types,tree raises,tsubst_flags_t complain)13667 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13668 tree raises, tsubst_flags_t complain)
13669 {
13670 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13671
13672 tree new_type;
13673 if (TREE_CODE (t) == FUNCTION_TYPE)
13674 {
13675 new_type = build_function_type (return_type, arg_types);
13676 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13677 }
13678 else
13679 {
13680 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13681 /* Don't pick up extra function qualifiers from the basetype. */
13682 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13683 if (! MAYBE_CLASS_TYPE_P (r))
13684 {
13685 /* [temp.deduct]
13686
13687 Type deduction may fail for any of the following
13688 reasons:
13689
13690 -- Attempting to create "pointer to member of T" when T
13691 is not a class type. */
13692 if (complain & tf_error)
13693 error ("creating pointer to member function of non-class type %qT",
13694 r);
13695 return error_mark_node;
13696 }
13697
13698 new_type = build_method_type_directly (r, return_type,
13699 TREE_CHAIN (arg_types));
13700 }
13701 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13702
13703 cp_ref_qualifier rqual = type_memfn_rqual (t);
13704 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13705 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13706 }
13707
13708 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13709 each of its formal parameters. If there is a disagreement then rebuild
13710 DECL's function type according to its formal parameter types, as part of a
13711 resolution for Core issues 1001/1322. */
13712
13713 static void
maybe_rebuild_function_decl_type(tree decl)13714 maybe_rebuild_function_decl_type (tree decl)
13715 {
13716 bool function_type_needs_rebuilding = false;
13717 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13718 {
13719 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13720 while (parm_type_list && parm_type_list != void_list_node)
13721 {
13722 tree parm_type = TREE_VALUE (parm_type_list);
13723 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13724 if (!same_type_p (parm_type, formal_parm_type_unqual))
13725 {
13726 function_type_needs_rebuilding = true;
13727 break;
13728 }
13729
13730 parm_list = DECL_CHAIN (parm_list);
13731 parm_type_list = TREE_CHAIN (parm_type_list);
13732 }
13733 }
13734
13735 if (!function_type_needs_rebuilding)
13736 return;
13737
13738 const tree fntype = TREE_TYPE (decl);
13739 tree parm_list = DECL_ARGUMENTS (decl);
13740 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13741 tree new_parm_type_list = NULL_TREE;
13742 tree *q = &new_parm_type_list;
13743 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13744 {
13745 *q = copy_node (old_parm_type_list);
13746 parm_list = DECL_CHAIN (parm_list);
13747 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13748 q = &TREE_CHAIN (*q);
13749 }
13750 while (old_parm_type_list && old_parm_type_list != void_list_node)
13751 {
13752 *q = copy_node (old_parm_type_list);
13753 tree *new_parm_type = &TREE_VALUE (*q);
13754 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13755 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13756 *new_parm_type = formal_parm_type_unqual;
13757
13758 parm_list = DECL_CHAIN (parm_list);
13759 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13760 q = &TREE_CHAIN (*q);
13761 }
13762 if (old_parm_type_list == void_list_node)
13763 *q = void_list_node;
13764
13765 TREE_TYPE (decl)
13766 = rebuild_function_or_method_type (fntype,
13767 TREE_TYPE (fntype), new_parm_type_list,
13768 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13769 }
13770
13771 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13772
13773 static tree
tsubst_function_decl(tree t,tree args,tsubst_flags_t complain,tree lambda_fntype)13774 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13775 tree lambda_fntype)
13776 {
13777 tree gen_tmpl, argvec;
13778 hashval_t hash = 0;
13779 tree in_decl = t;
13780
13781 /* Nobody should be tsubst'ing into non-template functions. */
13782 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13783
13784 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13785 {
13786 /* If T is not dependent, just return it. */
13787 if (!uses_template_parms (DECL_TI_ARGS (t))
13788 && !LAMBDA_FUNCTION_P (t))
13789 return t;
13790
13791 /* Calculate the most general template of which R is a
13792 specialization. */
13793 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13794
13795 /* We're substituting a lambda function under tsubst_lambda_expr but not
13796 directly from it; find the matching function we're already inside.
13797 But don't do this if T is a generic lambda with a single level of
13798 template parms, as in that case we're doing a normal instantiation. */
13799 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13800 && (!generic_lambda_fn_p (t)
13801 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13802 return enclosing_instantiation_of (t);
13803
13804 /* Calculate the complete set of arguments used to
13805 specialize R. */
13806 argvec = tsubst_template_args (DECL_TI_ARGS
13807 (DECL_TEMPLATE_RESULT
13808 (DECL_TI_TEMPLATE (t))),
13809 args, complain, in_decl);
13810 if (argvec == error_mark_node)
13811 return error_mark_node;
13812
13813 /* Check to see if we already have this specialization. */
13814 if (!lambda_fntype)
13815 {
13816 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13817 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13818 return spec;
13819 }
13820 }
13821 else
13822 {
13823 /* This special case arises when we have something like this:
13824
13825 template <class T> struct S {
13826 friend void f<int>(int, double);
13827 };
13828
13829 Here, the DECL_TI_TEMPLATE for the friend declaration
13830 will be an IDENTIFIER_NODE. We are being called from
13831 tsubst_friend_function, and we want only to create a
13832 new decl (R) with appropriate types so that we can call
13833 determine_specialization. */
13834 gen_tmpl = NULL_TREE;
13835 argvec = NULL_TREE;
13836 }
13837
13838 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13839 : NULL_TREE);
13840 tree ctx = closure ? closure : DECL_CONTEXT (t);
13841 bool member = ctx && TYPE_P (ctx);
13842
13843 if (member && !closure)
13844 ctx = tsubst_aggr_type (ctx, args,
13845 complain, t, /*entering_scope=*/1);
13846
13847 tree type = (lambda_fntype ? lambda_fntype
13848 : tsubst (TREE_TYPE (t), args,
13849 complain | tf_fndecl_type, in_decl));
13850 if (type == error_mark_node)
13851 return error_mark_node;
13852
13853 /* If we hit excessive deduction depth, the type is bogus even if
13854 it isn't error_mark_node, so don't build a decl. */
13855 if (excessive_deduction_depth)
13856 return error_mark_node;
13857
13858 /* We do NOT check for matching decls pushed separately at this
13859 point, as they may not represent instantiations of this
13860 template, and in any case are considered separate under the
13861 discrete model. */
13862 tree r = copy_decl (t);
13863 DECL_USE_TEMPLATE (r) = 0;
13864 TREE_TYPE (r) = type;
13865 /* Clear out the mangled name and RTL for the instantiation. */
13866 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13867 SET_DECL_RTL (r, NULL);
13868 /* Leave DECL_INITIAL set on deleted instantiations. */
13869 if (!DECL_DELETED_FN (r))
13870 DECL_INITIAL (r) = NULL_TREE;
13871 DECL_CONTEXT (r) = ctx;
13872
13873 /* Handle explicit(dependent-expr). */
13874 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13875 {
13876 tree spec = lookup_explicit_specifier (t);
13877 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13878 /*function_p=*/false,
13879 /*i_c_e_p=*/true);
13880 spec = build_explicit_specifier (spec, complain);
13881 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13882 }
13883
13884 /* OpenMP UDRs have the only argument a reference to the declared
13885 type. We want to diagnose if the declared type is a reference,
13886 which is invalid, but as references to references are usually
13887 quietly merged, diagnose it here. */
13888 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13889 {
13890 tree argtype
13891 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13892 argtype = tsubst (argtype, args, complain, in_decl);
13893 if (TYPE_REF_P (argtype))
13894 error_at (DECL_SOURCE_LOCATION (t),
13895 "reference type %qT in "
13896 "%<#pragma omp declare reduction%>", argtype);
13897 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13898 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13899 argtype);
13900 }
13901
13902 if (member && DECL_CONV_FN_P (r))
13903 /* Type-conversion operator. Reconstruct the name, in
13904 case it's the name of one of the template's parameters. */
13905 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13906
13907 tree parms = DECL_ARGUMENTS (t);
13908 if (closure)
13909 parms = DECL_CHAIN (parms);
13910 parms = tsubst (parms, args, complain, t);
13911 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13912 DECL_CONTEXT (parm) = r;
13913 if (closure)
13914 {
13915 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13916 DECL_CHAIN (tparm) = parms;
13917 parms = tparm;
13918 }
13919 DECL_ARGUMENTS (r) = parms;
13920 DECL_RESULT (r) = NULL_TREE;
13921
13922 maybe_rebuild_function_decl_type (r);
13923
13924 TREE_STATIC (r) = 0;
13925 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13926 DECL_EXTERNAL (r) = 1;
13927 /* If this is an instantiation of a function with internal
13928 linkage, we already know what object file linkage will be
13929 assigned to the instantiation. */
13930 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13931 DECL_DEFER_OUTPUT (r) = 0;
13932 DECL_CHAIN (r) = NULL_TREE;
13933 DECL_PENDING_INLINE_INFO (r) = 0;
13934 DECL_PENDING_INLINE_P (r) = 0;
13935 DECL_SAVED_TREE (r) = NULL_TREE;
13936 DECL_STRUCT_FUNCTION (r) = NULL;
13937 TREE_USED (r) = 0;
13938 /* We'll re-clone as appropriate in instantiate_template. */
13939 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13940
13941 /* If we aren't complaining now, return on error before we register
13942 the specialization so that we'll complain eventually. */
13943 if ((complain & tf_error) == 0
13944 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13945 && !grok_op_properties (r, /*complain=*/false))
13946 return error_mark_node;
13947
13948 /* Associate the constraints directly with the instantiation. We
13949 don't substitute through the constraints; that's only done when
13950 they are checked. */
13951 if (tree ci = get_constraints (t))
13952 /* Unless we're regenerating a lambda, in which case we'll set the
13953 lambda's constraints in tsubst_lambda_expr. */
13954 if (!lambda_fntype)
13955 set_constraints (r, ci);
13956
13957 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13958 SET_DECL_FRIEND_CONTEXT (r,
13959 tsubst (DECL_FRIEND_CONTEXT (t),
13960 args, complain, in_decl));
13961
13962 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13963 this in the special friend case mentioned above where
13964 GEN_TMPL is NULL. */
13965 if (gen_tmpl && !closure)
13966 {
13967 DECL_TEMPLATE_INFO (r)
13968 = build_template_info (gen_tmpl, argvec);
13969 SET_DECL_IMPLICIT_INSTANTIATION (r);
13970
13971 tree new_r
13972 = register_specialization (r, gen_tmpl, argvec, false, hash);
13973 if (new_r != r)
13974 /* We instantiated this while substituting into
13975 the type earlier (template/friend54.C). */
13976 return new_r;
13977
13978 /* We're not supposed to instantiate default arguments
13979 until they are called, for a template. But, for a
13980 declaration like:
13981
13982 template <class T> void f ()
13983 { extern void g(int i = T()); }
13984
13985 we should do the substitution when the template is
13986 instantiated. We handle the member function case in
13987 instantiate_class_template since the default arguments
13988 might refer to other members of the class. */
13989 if (!member
13990 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13991 && !uses_template_parms (argvec))
13992 tsubst_default_arguments (r, complain);
13993 }
13994 else
13995 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13996
13997 /* Copy the list of befriending classes. */
13998 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13999 *friends;
14000 friends = &TREE_CHAIN (*friends))
14001 {
14002 *friends = copy_node (*friends);
14003 TREE_VALUE (*friends)
14004 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14005 }
14006
14007 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14008 {
14009 maybe_retrofit_in_chrg (r);
14010 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14011 return error_mark_node;
14012 /* If this is an instantiation of a member template, clone it.
14013 If it isn't, that'll be handled by
14014 clone_constructors_and_destructors. */
14015 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14016 clone_function_decl (r, /*update_methods=*/false);
14017 }
14018 else if ((complain & tf_error) != 0
14019 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14020 && !grok_op_properties (r, /*complain=*/true))
14021 return error_mark_node;
14022
14023 /* Possibly limit visibility based on template args. */
14024 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14025 if (DECL_VISIBILITY_SPECIFIED (t))
14026 {
14027 DECL_VISIBILITY_SPECIFIED (r) = 0;
14028 DECL_ATTRIBUTES (r)
14029 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14030 }
14031 determine_visibility (r);
14032 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14033 && !processing_template_decl)
14034 defaulted_late_check (r);
14035
14036 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14037 args, complain, in_decl);
14038 if (flag_openmp)
14039 if (tree attr = lookup_attribute ("omp declare variant base",
14040 DECL_ATTRIBUTES (r)))
14041 omp_declare_variant_finalize (r, attr);
14042
14043 return r;
14044 }
14045
14046 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14047
14048 static tree
tsubst_template_decl(tree t,tree args,tsubst_flags_t complain,tree lambda_fntype)14049 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14050 tree lambda_fntype)
14051 {
14052 /* We can get here when processing a member function template,
14053 member class template, or template template parameter. */
14054 tree decl = DECL_TEMPLATE_RESULT (t);
14055 tree in_decl = t;
14056 tree spec;
14057 tree tmpl_args;
14058 tree full_args;
14059 tree r;
14060 hashval_t hash = 0;
14061
14062 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14063 {
14064 /* Template template parameter is treated here. */
14065 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14066 if (new_type == error_mark_node)
14067 r = error_mark_node;
14068 /* If we get a real template back, return it. This can happen in
14069 the context of most_specialized_partial_spec. */
14070 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14071 r = new_type;
14072 else
14073 /* The new TEMPLATE_DECL was built in
14074 reduce_template_parm_level. */
14075 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14076 return r;
14077 }
14078
14079 if (!lambda_fntype)
14080 {
14081 /* We might already have an instance of this template.
14082 The ARGS are for the surrounding class type, so the
14083 full args contain the tsubst'd args for the context,
14084 plus the innermost args from the template decl. */
14085 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14086 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14087 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14088 /* Because this is a template, the arguments will still be
14089 dependent, even after substitution. If
14090 PROCESSING_TEMPLATE_DECL is not set, the dependency
14091 predicates will short-circuit. */
14092 ++processing_template_decl;
14093 full_args = tsubst_template_args (tmpl_args, args,
14094 complain, in_decl);
14095 --processing_template_decl;
14096 if (full_args == error_mark_node)
14097 return error_mark_node;
14098
14099 /* If this is a default template template argument,
14100 tsubst might not have changed anything. */
14101 if (full_args == tmpl_args)
14102 return t;
14103
14104 hash = hash_tmpl_and_args (t, full_args);
14105 spec = retrieve_specialization (t, full_args, hash);
14106 if (spec != NULL_TREE)
14107 {
14108 if (TYPE_P (spec))
14109 /* Type partial instantiations are stored as the type by
14110 lookup_template_class_1, not here as the template. */
14111 spec = CLASSTYPE_TI_TEMPLATE (spec);
14112 return spec;
14113 }
14114 }
14115
14116 /* Make a new template decl. It will be similar to the
14117 original, but will record the current template arguments.
14118 We also create a new function declaration, which is just
14119 like the old one, but points to this new template, rather
14120 than the old one. */
14121 r = copy_decl (t);
14122 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14123 DECL_CHAIN (r) = NULL_TREE;
14124
14125 // Build new template info linking to the original template decl.
14126 if (!lambda_fntype)
14127 {
14128 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14129 SET_DECL_IMPLICIT_INSTANTIATION (r);
14130 }
14131 else
14132 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14133
14134 /* The template parameters for this new template are all the
14135 template parameters for the old template, except the
14136 outermost level of parameters. */
14137 DECL_TEMPLATE_PARMS (r)
14138 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14139 complain);
14140
14141 if (TREE_CODE (decl) == TYPE_DECL
14142 && !TYPE_DECL_ALIAS_P (decl))
14143 {
14144 tree new_type;
14145 ++processing_template_decl;
14146 if (CLASS_TYPE_P (TREE_TYPE (t)))
14147 new_type = tsubst_aggr_type (TREE_TYPE (t), args, complain,
14148 in_decl, /*entering*/1);
14149 else
14150 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14151 --processing_template_decl;
14152 if (new_type == error_mark_node)
14153 return error_mark_node;
14154
14155 TREE_TYPE (r) = new_type;
14156 /* For a partial specialization, we need to keep pointing to
14157 the primary template. */
14158 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14159 CLASSTYPE_TI_TEMPLATE (new_type) = r;
14160 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
14161 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
14162 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
14163 }
14164 else
14165 {
14166 tree new_decl;
14167 ++processing_template_decl;
14168 if (TREE_CODE (decl) == FUNCTION_DECL)
14169 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
14170 else
14171 new_decl = tsubst (decl, args, complain, in_decl);
14172 --processing_template_decl;
14173 if (new_decl == error_mark_node)
14174 return error_mark_node;
14175
14176 DECL_TEMPLATE_RESULT (r) = new_decl;
14177 TREE_TYPE (r) = TREE_TYPE (new_decl);
14178 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
14179 if (lambda_fntype)
14180 {
14181 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14182 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
14183 }
14184 else
14185 {
14186 DECL_TI_TEMPLATE (new_decl) = r;
14187 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
14188 }
14189 }
14190
14191 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14192 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14193
14194 if (PRIMARY_TEMPLATE_P (t))
14195 DECL_PRIMARY_TEMPLATE (r) = r;
14196
14197 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14198 && !lambda_fntype)
14199 /* Record this non-type partial instantiation. */
14200 register_specialization (r, t,
14201 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14202 false, hash);
14203
14204 return r;
14205 }
14206
14207 /* True if FN is the op() for a lambda in an uninstantiated template. */
14208
14209 bool
lambda_fn_in_template_p(tree fn)14210 lambda_fn_in_template_p (tree fn)
14211 {
14212 if (!fn || !LAMBDA_FUNCTION_P (fn))
14213 return false;
14214 tree closure = DECL_CONTEXT (fn);
14215 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14216 }
14217
14218 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14219 which the above is true. */
14220
14221 bool
instantiated_lambda_fn_p(tree fn)14222 instantiated_lambda_fn_p (tree fn)
14223 {
14224 if (!fn || !LAMBDA_FUNCTION_P (fn))
14225 return false;
14226 tree closure = DECL_CONTEXT (fn);
14227 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14228 return LAMBDA_EXPR_INSTANTIATED (lam);
14229 }
14230
14231 /* We're instantiating a variable from template function TCTX. Return the
14232 corresponding current enclosing scope. This gets complicated because lambda
14233 functions in templates are regenerated rather than instantiated, but generic
14234 lambda functions are subsequently instantiated. */
14235
14236 static tree
enclosing_instantiation_of(tree otctx)14237 enclosing_instantiation_of (tree otctx)
14238 {
14239 tree tctx = otctx;
14240 tree fn = current_function_decl;
14241 int lambda_count = 0;
14242
14243 for (; tctx && (lambda_fn_in_template_p (tctx)
14244 || instantiated_lambda_fn_p (tctx));
14245 tctx = decl_function_context (tctx))
14246 ++lambda_count;
14247
14248 if (!tctx)
14249 {
14250 /* Match using DECL_SOURCE_LOCATION, which is unique for all lambdas.
14251
14252 For GCC 11 the above condition limits this to the previously failing
14253 case where all enclosing functions are lambdas (95870). FIXME. */
14254 for (tree ofn = fn; ofn; ofn = decl_function_context (ofn))
14255 if (DECL_SOURCE_LOCATION (ofn) == DECL_SOURCE_LOCATION (otctx))
14256 return ofn;
14257 gcc_unreachable ();
14258 }
14259
14260 for (; fn; fn = decl_function_context (fn))
14261 {
14262 tree ofn = fn;
14263 int flambda_count = 0;
14264 for (; fn && instantiated_lambda_fn_p (fn);
14265 fn = decl_function_context (fn))
14266 ++flambda_count;
14267 if ((fn && DECL_TEMPLATE_INFO (fn))
14268 ? most_general_template (fn) != most_general_template (tctx)
14269 : fn != tctx)
14270 continue;
14271 if (flambda_count != lambda_count)
14272 {
14273 gcc_assert (flambda_count > lambda_count);
14274 for (; flambda_count > lambda_count; --flambda_count)
14275 ofn = decl_function_context (ofn);
14276 }
14277 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14278 || DECL_CONV_FN_P (ofn));
14279 return ofn;
14280 }
14281 gcc_unreachable ();
14282 }
14283
14284 /* Substitute the ARGS into the T, which is a _DECL. Return the
14285 result of the substitution. Issue error and warning messages under
14286 control of COMPLAIN. */
14287
14288 static tree
tsubst_decl(tree t,tree args,tsubst_flags_t complain)14289 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14290 {
14291 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14292 location_t saved_loc;
14293 tree r = NULL_TREE;
14294 tree in_decl = t;
14295 hashval_t hash = 0;
14296
14297 /* Set the filename and linenumber to improve error-reporting. */
14298 saved_loc = input_location;
14299 input_location = DECL_SOURCE_LOCATION (t);
14300
14301 switch (TREE_CODE (t))
14302 {
14303 case TEMPLATE_DECL:
14304 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14305 break;
14306
14307 case FUNCTION_DECL:
14308 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14309 break;
14310
14311 case PARM_DECL:
14312 {
14313 tree type = NULL_TREE;
14314 int i, len = 1;
14315 tree expanded_types = NULL_TREE;
14316 tree prev_r = NULL_TREE;
14317 tree first_r = NULL_TREE;
14318
14319 if (DECL_PACK_P (t))
14320 {
14321 /* If there is a local specialization that isn't a
14322 parameter pack, it means that we're doing a "simple"
14323 substitution from inside tsubst_pack_expansion. Just
14324 return the local specialization (which will be a single
14325 parm). */
14326 tree spec = retrieve_local_specialization (t);
14327 if (spec
14328 && TREE_CODE (spec) == PARM_DECL
14329 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14330 RETURN (spec);
14331
14332 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14333 the parameters in this function parameter pack. */
14334 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14335 complain, in_decl);
14336 if (TREE_CODE (expanded_types) == TREE_VEC)
14337 {
14338 len = TREE_VEC_LENGTH (expanded_types);
14339
14340 /* Zero-length parameter packs are boring. Just substitute
14341 into the chain. */
14342 if (len == 0 && !cp_unevaluated_operand)
14343 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14344 TREE_CHAIN (t)));
14345 }
14346 else
14347 {
14348 /* All we did was update the type. Make a note of that. */
14349 type = expanded_types;
14350 expanded_types = NULL_TREE;
14351 }
14352 }
14353
14354 /* Loop through all of the parameters we'll build. When T is
14355 a function parameter pack, LEN is the number of expanded
14356 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14357 r = NULL_TREE;
14358 for (i = 0; i < len; ++i)
14359 {
14360 prev_r = r;
14361 r = copy_node (t);
14362 if (DECL_TEMPLATE_PARM_P (t))
14363 SET_DECL_TEMPLATE_PARM_P (r);
14364
14365 if (expanded_types)
14366 /* We're on the Ith parameter of the function parameter
14367 pack. */
14368 {
14369 /* Get the Ith type. */
14370 type = TREE_VEC_ELT (expanded_types, i);
14371
14372 /* Rename the parameter to include the index. */
14373 DECL_NAME (r)
14374 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14375 }
14376 else if (!type)
14377 /* We're dealing with a normal parameter. */
14378 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14379
14380 type = type_decays_to (type);
14381 TREE_TYPE (r) = type;
14382 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14383
14384 if (DECL_INITIAL (r))
14385 {
14386 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14387 DECL_INITIAL (r) = TREE_TYPE (r);
14388 else
14389 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14390 complain, in_decl);
14391 }
14392
14393 DECL_CONTEXT (r) = NULL_TREE;
14394
14395 if (!DECL_TEMPLATE_PARM_P (r))
14396 DECL_ARG_TYPE (r) = type_passed_as (type);
14397
14398 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14399 args, complain, in_decl);
14400
14401 /* Keep track of the first new parameter we
14402 generate. That's what will be returned to the
14403 caller. */
14404 if (!first_r)
14405 first_r = r;
14406
14407 /* Build a proper chain of parameters when substituting
14408 into a function parameter pack. */
14409 if (prev_r)
14410 DECL_CHAIN (prev_r) = r;
14411 }
14412
14413 /* If cp_unevaluated_operand is set, we're just looking for a
14414 single dummy parameter, so don't keep going. */
14415 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14416 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14417 complain, DECL_CHAIN (t));
14418
14419 /* FIRST_R contains the start of the chain we've built. */
14420 r = first_r;
14421 }
14422 break;
14423
14424 case FIELD_DECL:
14425 {
14426 tree type = NULL_TREE;
14427 tree vec = NULL_TREE;
14428 tree expanded_types = NULL_TREE;
14429 int len = 1;
14430
14431 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14432 {
14433 /* This field is a lambda capture pack. Return a TREE_VEC of
14434 the expanded fields to instantiate_class_template_1. */
14435 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14436 complain, in_decl);
14437 if (TREE_CODE (expanded_types) == TREE_VEC)
14438 {
14439 len = TREE_VEC_LENGTH (expanded_types);
14440 vec = make_tree_vec (len);
14441 }
14442 else
14443 {
14444 /* All we did was update the type. Make a note of that. */
14445 type = expanded_types;
14446 expanded_types = NULL_TREE;
14447 }
14448 }
14449
14450 for (int i = 0; i < len; ++i)
14451 {
14452 r = copy_decl (t);
14453 if (expanded_types)
14454 {
14455 type = TREE_VEC_ELT (expanded_types, i);
14456 DECL_NAME (r)
14457 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14458 }
14459 else if (!type)
14460 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14461
14462 if (type == error_mark_node)
14463 RETURN (error_mark_node);
14464 TREE_TYPE (r) = type;
14465 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14466
14467 if (DECL_C_BIT_FIELD (r))
14468 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14469 number of bits. */
14470 DECL_BIT_FIELD_REPRESENTATIVE (r)
14471 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14472 complain, in_decl,
14473 /*integral_constant_expression_p=*/true);
14474 if (DECL_INITIAL (t))
14475 {
14476 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14477 NSDMI in perform_member_init. Still set DECL_INITIAL
14478 so that we know there is one. */
14479 DECL_INITIAL (r) = void_node;
14480 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14481 retrofit_lang_decl (r);
14482 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14483 }
14484 /* We don't have to set DECL_CONTEXT here; it is set by
14485 finish_member_declaration. */
14486 DECL_CHAIN (r) = NULL_TREE;
14487
14488 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14489 args, complain, in_decl);
14490
14491 if (vec)
14492 TREE_VEC_ELT (vec, i) = r;
14493 }
14494
14495 if (vec)
14496 r = vec;
14497 }
14498 break;
14499
14500 case USING_DECL:
14501 /* We reach here only for member using decls. We also need to check
14502 uses_template_parms because DECL_DEPENDENT_P is not set for a
14503 using-declaration that designates a member of the current
14504 instantiation (c++/53549). */
14505 if (DECL_DEPENDENT_P (t)
14506 || uses_template_parms (USING_DECL_SCOPE (t)))
14507 {
14508 tree scope = USING_DECL_SCOPE (t);
14509 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14510 if (PACK_EXPANSION_P (scope))
14511 {
14512 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14513 int len = TREE_VEC_LENGTH (vec);
14514 r = make_tree_vec (len);
14515 for (int i = 0; i < len; ++i)
14516 {
14517 tree escope = TREE_VEC_ELT (vec, i);
14518 tree elt = do_class_using_decl (escope, name);
14519 if (!elt)
14520 {
14521 r = error_mark_node;
14522 break;
14523 }
14524 else
14525 {
14526 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14527 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14528 }
14529 TREE_VEC_ELT (r, i) = elt;
14530 }
14531 }
14532 else
14533 {
14534 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14535 complain, in_decl);
14536 r = do_class_using_decl (inst_scope, name);
14537 if (!r)
14538 r = error_mark_node;
14539 else
14540 {
14541 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14542 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14543 }
14544 }
14545 }
14546 else
14547 {
14548 r = copy_node (t);
14549 DECL_CHAIN (r) = NULL_TREE;
14550 }
14551 break;
14552
14553 case TYPE_DECL:
14554 case VAR_DECL:
14555 {
14556 tree argvec = NULL_TREE;
14557 tree gen_tmpl = NULL_TREE;
14558 tree spec;
14559 tree tmpl = NULL_TREE;
14560 tree ctx;
14561 tree type = NULL_TREE;
14562 bool local_p;
14563
14564 if (TREE_TYPE (t) == error_mark_node)
14565 RETURN (error_mark_node);
14566
14567 if (TREE_CODE (t) == TYPE_DECL
14568 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14569 {
14570 /* If this is the canonical decl, we don't have to
14571 mess with instantiations, and often we can't (for
14572 typename, template type parms and such). Note that
14573 TYPE_NAME is not correct for the above test if
14574 we've copied the type for a typedef. */
14575 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14576 if (type == error_mark_node)
14577 RETURN (error_mark_node);
14578 r = TYPE_NAME (type);
14579 break;
14580 }
14581
14582 /* Check to see if we already have the specialization we
14583 need. */
14584 spec = NULL_TREE;
14585 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14586 {
14587 /* T is a static data member or namespace-scope entity.
14588 We have to substitute into namespace-scope variables
14589 (not just variable templates) because of cases like:
14590
14591 template <class T> void f() { extern T t; }
14592
14593 where the entity referenced is not known until
14594 instantiation time. */
14595 local_p = false;
14596 ctx = DECL_CONTEXT (t);
14597 if (DECL_CLASS_SCOPE_P (t))
14598 {
14599 ctx = tsubst_aggr_type (ctx, args,
14600 complain,
14601 in_decl, /*entering_scope=*/1);
14602 /* If CTX is unchanged, then T is in fact the
14603 specialization we want. That situation occurs when
14604 referencing a static data member within in its own
14605 class. We can use pointer equality, rather than
14606 same_type_p, because DECL_CONTEXT is always
14607 canonical... */
14608 if (ctx == DECL_CONTEXT (t)
14609 /* ... unless T is a member template; in which
14610 case our caller can be willing to create a
14611 specialization of that template represented
14612 by T. */
14613 && !(DECL_TI_TEMPLATE (t)
14614 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14615 spec = t;
14616 }
14617
14618 if (!spec)
14619 {
14620 tmpl = DECL_TI_TEMPLATE (t);
14621 gen_tmpl = most_general_template (tmpl);
14622 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14623 if (argvec != error_mark_node)
14624 argvec = (coerce_innermost_template_parms
14625 (DECL_TEMPLATE_PARMS (gen_tmpl),
14626 argvec, t, complain,
14627 /*all*/true, /*defarg*/true));
14628 if (argvec == error_mark_node)
14629 RETURN (error_mark_node);
14630 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14631 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14632 }
14633 }
14634 else
14635 {
14636 /* A local variable. */
14637 local_p = true;
14638 /* Subsequent calls to pushdecl will fill this in. */
14639 ctx = NULL_TREE;
14640 /* Unless this is a reference to a static variable from an
14641 enclosing function, in which case we need to fill it in now. */
14642 if (TREE_STATIC (t))
14643 {
14644 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14645 if (fn != current_function_decl)
14646 ctx = fn;
14647 }
14648 spec = retrieve_local_specialization (t);
14649 }
14650 /* If we already have the specialization we need, there is
14651 nothing more to do. */
14652 if (spec)
14653 {
14654 r = spec;
14655 break;
14656 }
14657
14658 /* Create a new node for the specialization we need. */
14659 if (type == NULL_TREE)
14660 {
14661 if (is_typedef_decl (t))
14662 type = DECL_ORIGINAL_TYPE (t);
14663 else
14664 type = TREE_TYPE (t);
14665 if (VAR_P (t)
14666 && VAR_HAD_UNKNOWN_BOUND (t)
14667 && type != error_mark_node)
14668 type = strip_array_domain (type);
14669 tree sub_args = args;
14670 if (tree auto_node = type_uses_auto (type))
14671 {
14672 /* Mask off any template args past the variable's context so we
14673 don't replace the auto with an unrelated argument. */
14674 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14675 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14676 if (extra > 0)
14677 /* This should never happen with the new lambda instantiation
14678 model, but keep the handling just in case. */
14679 gcc_assert (!CHECKING_P),
14680 sub_args = strip_innermost_template_args (args, extra);
14681 }
14682 type = tsubst (type, sub_args, complain, in_decl);
14683 /* Substituting the type might have recursively instantiated this
14684 same alias (c++/86171). */
14685 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14686 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14687 {
14688 r = spec;
14689 break;
14690 }
14691 }
14692 r = copy_decl (t);
14693 if (VAR_P (r))
14694 {
14695 DECL_INITIALIZED_P (r) = 0;
14696 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14697 if (type == error_mark_node)
14698 RETURN (error_mark_node);
14699 if (TREE_CODE (type) == FUNCTION_TYPE)
14700 {
14701 /* It may seem that this case cannot occur, since:
14702
14703 typedef void f();
14704 void g() { f x; }
14705
14706 declares a function, not a variable. However:
14707
14708 typedef void f();
14709 template <typename T> void g() { T t; }
14710 template void g<f>();
14711
14712 is an attempt to declare a variable with function
14713 type. */
14714 error ("variable %qD has function type",
14715 /* R is not yet sufficiently initialized, so we
14716 just use its name. */
14717 DECL_NAME (r));
14718 RETURN (error_mark_node);
14719 }
14720 type = complete_type (type);
14721 /* Wait until cp_finish_decl to set this again, to handle
14722 circular dependency (template/instantiate6.C). */
14723 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14724 type = check_var_type (DECL_NAME (r), type,
14725 DECL_SOURCE_LOCATION (r));
14726 if (DECL_HAS_VALUE_EXPR_P (t))
14727 {
14728 tree ve = DECL_VALUE_EXPR (t);
14729 /* If the DECL_VALUE_EXPR is converted to the declared type,
14730 preserve the identity so that gimplify_type_sizes works. */
14731 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14732 if (nop)
14733 ve = TREE_OPERAND (ve, 0);
14734 ve = tsubst_expr (ve, args, complain, in_decl,
14735 /*constant_expression_p=*/false);
14736 if (REFERENCE_REF_P (ve))
14737 {
14738 gcc_assert (TYPE_REF_P (type));
14739 ve = TREE_OPERAND (ve, 0);
14740 }
14741 if (nop)
14742 ve = build_nop (type, ve);
14743 else
14744 gcc_checking_assert (TREE_TYPE (ve) == type);
14745 SET_DECL_VALUE_EXPR (r, ve);
14746 }
14747 if (CP_DECL_THREAD_LOCAL_P (r)
14748 && !processing_template_decl)
14749 set_decl_tls_model (r, decl_default_tls_model (r));
14750 }
14751 else if (DECL_SELF_REFERENCE_P (t))
14752 SET_DECL_SELF_REFERENCE_P (r);
14753 TREE_TYPE (r) = type;
14754 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14755 DECL_CONTEXT (r) = ctx;
14756 /* Clear out the mangled name and RTL for the instantiation. */
14757 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14758 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14759 SET_DECL_RTL (r, NULL);
14760 /* The initializer must not be expanded until it is required;
14761 see [temp.inst]. */
14762 DECL_INITIAL (r) = NULL_TREE;
14763 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14764 if (VAR_P (r))
14765 {
14766 if (DECL_LANG_SPECIFIC (r))
14767 SET_DECL_DEPENDENT_INIT_P (r, false);
14768
14769 SET_DECL_MODE (r, VOIDmode);
14770
14771 /* Possibly limit visibility based on template args. */
14772 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14773 if (DECL_VISIBILITY_SPECIFIED (t))
14774 {
14775 DECL_VISIBILITY_SPECIFIED (r) = 0;
14776 DECL_ATTRIBUTES (r)
14777 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14778 }
14779 determine_visibility (r);
14780 }
14781
14782 if (!local_p)
14783 {
14784 /* A static data member declaration is always marked
14785 external when it is declared in-class, even if an
14786 initializer is present. We mimic the non-template
14787 processing here. */
14788 DECL_EXTERNAL (r) = 1;
14789 if (DECL_NAMESPACE_SCOPE_P (t))
14790 DECL_NOT_REALLY_EXTERN (r) = 1;
14791
14792 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14793 SET_DECL_IMPLICIT_INSTANTIATION (r);
14794 /* Remember whether we require constant initialization of
14795 a non-constant template variable. */
14796 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14797 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14798 if (!error_operand_p (r) || (complain & tf_error))
14799 register_specialization (r, gen_tmpl, argvec, false, hash);
14800 }
14801 else
14802 {
14803 if (DECL_LANG_SPECIFIC (r))
14804 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14805 if (!cp_unevaluated_operand)
14806 register_local_specialization (r, t);
14807 }
14808
14809 DECL_CHAIN (r) = NULL_TREE;
14810
14811 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14812 /*flags=*/0,
14813 args, complain, in_decl);
14814
14815 /* Preserve a typedef that names a type. */
14816 if (is_typedef_decl (r) && type != error_mark_node)
14817 {
14818 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14819 set_underlying_type (r);
14820
14821 /* common_handle_aligned_attribute doesn't apply the alignment
14822 to DECL_ORIGINAL_TYPE. */
14823 if (TYPE_USER_ALIGN (TREE_TYPE (t)))
14824 TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
14825 TYPE_ALIGN (TREE_TYPE (t)));
14826
14827 if (TYPE_DECL_ALIAS_P (r))
14828 /* An alias template specialization can be dependent
14829 even if its underlying type is not. */
14830 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14831 }
14832
14833 layout_decl (r, 0);
14834 }
14835 break;
14836
14837 default:
14838 gcc_unreachable ();
14839 }
14840 #undef RETURN
14841
14842 out:
14843 /* Restore the file and line information. */
14844 input_location = saved_loc;
14845
14846 return r;
14847 }
14848
14849 /* Substitute into the complete parameter type list PARMS. */
14850
14851 tree
tsubst_function_parms(tree parms,tree args,tsubst_flags_t complain,tree in_decl)14852 tsubst_function_parms (tree parms,
14853 tree args,
14854 tsubst_flags_t complain,
14855 tree in_decl)
14856 {
14857 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14858 }
14859
14860 /* Substitute into the ARG_TYPES of a function type.
14861 If END is a TREE_CHAIN, leave it and any following types
14862 un-substituted. */
14863
14864 static tree
tsubst_arg_types(tree arg_types,tree args,tree end,tsubst_flags_t complain,tree in_decl)14865 tsubst_arg_types (tree arg_types,
14866 tree args,
14867 tree end,
14868 tsubst_flags_t complain,
14869 tree in_decl)
14870 {
14871 tree remaining_arg_types;
14872 tree type = NULL_TREE;
14873 int i = 1;
14874 tree expanded_args = NULL_TREE;
14875 tree default_arg;
14876
14877 if (!arg_types || arg_types == void_list_node || arg_types == end)
14878 return arg_types;
14879
14880 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14881 args, end, complain, in_decl);
14882 if (remaining_arg_types == error_mark_node)
14883 return error_mark_node;
14884
14885 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14886 {
14887 /* For a pack expansion, perform substitution on the
14888 entire expression. Later on, we'll handle the arguments
14889 one-by-one. */
14890 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14891 args, complain, in_decl);
14892
14893 if (TREE_CODE (expanded_args) == TREE_VEC)
14894 /* So that we'll spin through the parameters, one by one. */
14895 i = TREE_VEC_LENGTH (expanded_args);
14896 else
14897 {
14898 /* We only partially substituted into the parameter
14899 pack. Our type is TYPE_PACK_EXPANSION. */
14900 type = expanded_args;
14901 expanded_args = NULL_TREE;
14902 }
14903 }
14904
14905 while (i > 0) {
14906 --i;
14907
14908 if (expanded_args)
14909 type = TREE_VEC_ELT (expanded_args, i);
14910 else if (!type)
14911 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14912
14913 if (type == error_mark_node)
14914 return error_mark_node;
14915 if (VOID_TYPE_P (type))
14916 {
14917 if (complain & tf_error)
14918 {
14919 error ("invalid parameter type %qT", type);
14920 if (in_decl)
14921 error ("in declaration %q+D", in_decl);
14922 }
14923 return error_mark_node;
14924 }
14925 /* DR 657. */
14926 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14927 return error_mark_node;
14928
14929 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14930 top-level qualifiers as required. */
14931 type = cv_unqualified (type_decays_to (type));
14932
14933 /* We do not substitute into default arguments here. The standard
14934 mandates that they be instantiated only when needed, which is
14935 done in build_over_call. */
14936 default_arg = TREE_PURPOSE (arg_types);
14937
14938 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14939 since the new op() won't have any associated template arguments for us
14940 to refer to later. */
14941 if (lambda_fn_in_template_p (in_decl))
14942 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14943 false/*fn*/, false/*constexpr*/);
14944
14945 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14946 {
14947 /* We've instantiated a template before its default arguments
14948 have been parsed. This can happen for a nested template
14949 class, and is not an error unless we require the default
14950 argument in a call of this function. */
14951 remaining_arg_types =
14952 tree_cons (default_arg, type, remaining_arg_types);
14953 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14954 remaining_arg_types);
14955 }
14956 else
14957 remaining_arg_types =
14958 hash_tree_cons (default_arg, type, remaining_arg_types);
14959 }
14960
14961 return remaining_arg_types;
14962 }
14963
14964 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14965 *not* handle the exception-specification for FNTYPE, because the
14966 initial substitution of explicitly provided template parameters
14967 during argument deduction forbids substitution into the
14968 exception-specification:
14969
14970 [temp.deduct]
14971
14972 All references in the function type of the function template to the
14973 corresponding template parameters are replaced by the specified tem-
14974 plate argument values. If a substitution in a template parameter or
14975 in the function type of the function template results in an invalid
14976 type, type deduction fails. [Note: The equivalent substitution in
14977 exception specifications is done only when the function is instanti-
14978 ated, at which point a program is ill-formed if the substitution
14979 results in an invalid type.] */
14980
14981 static tree
tsubst_function_type(tree t,tree args,tsubst_flags_t complain,tree in_decl)14982 tsubst_function_type (tree t,
14983 tree args,
14984 tsubst_flags_t complain,
14985 tree in_decl)
14986 {
14987 tree return_type;
14988 tree arg_types = NULL_TREE;
14989
14990 /* The TYPE_CONTEXT is not used for function/method types. */
14991 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14992
14993 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14994 failure. */
14995 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14996
14997 if (late_return_type_p)
14998 {
14999 /* Substitute the argument types. */
15000 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15001 complain, in_decl);
15002 if (arg_types == error_mark_node)
15003 return error_mark_node;
15004
15005 tree save_ccp = current_class_ptr;
15006 tree save_ccr = current_class_ref;
15007 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15008 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15009 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15010 if (do_inject)
15011 {
15012 /* DR 1207: 'this' is in scope in the trailing return type. */
15013 inject_this_parameter (this_type, cp_type_quals (this_type));
15014 }
15015
15016 /* Substitute the return type. */
15017 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15018
15019 if (do_inject)
15020 {
15021 current_class_ptr = save_ccp;
15022 current_class_ref = save_ccr;
15023 }
15024 }
15025 else
15026 /* Substitute the return type. */
15027 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15028
15029 if (return_type == error_mark_node)
15030 return error_mark_node;
15031 /* DR 486 clarifies that creation of a function type with an
15032 invalid return type is a deduction failure. */
15033 if (TREE_CODE (return_type) == ARRAY_TYPE
15034 || TREE_CODE (return_type) == FUNCTION_TYPE)
15035 {
15036 if (complain & tf_error)
15037 {
15038 if (TREE_CODE (return_type) == ARRAY_TYPE)
15039 error ("function returning an array");
15040 else
15041 error ("function returning a function");
15042 }
15043 return error_mark_node;
15044 }
15045 /* And DR 657. */
15046 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
15047 return error_mark_node;
15048
15049 if (!late_return_type_p)
15050 {
15051 /* Substitute the argument types. */
15052 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15053 complain, in_decl);
15054 if (arg_types == error_mark_node)
15055 return error_mark_node;
15056 }
15057
15058 /* Construct a new type node and return it. */
15059 return rebuild_function_or_method_type (t, return_type, arg_types,
15060 /*raises=*/NULL_TREE, complain);
15061 }
15062
15063 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15064 ARGS into that specification, and return the substituted
15065 specification. If there is no specification, return NULL_TREE. */
15066
15067 static tree
tsubst_exception_specification(tree fntype,tree args,tsubst_flags_t complain,tree in_decl,bool defer_ok)15068 tsubst_exception_specification (tree fntype,
15069 tree args,
15070 tsubst_flags_t complain,
15071 tree in_decl,
15072 bool defer_ok)
15073 {
15074 tree specs;
15075 tree new_specs;
15076
15077 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15078 new_specs = NULL_TREE;
15079 if (specs && TREE_PURPOSE (specs))
15080 {
15081 /* A noexcept-specifier. */
15082 tree expr = TREE_PURPOSE (specs);
15083 if (TREE_CODE (expr) == INTEGER_CST)
15084 new_specs = expr;
15085 else if (defer_ok)
15086 {
15087 /* Defer instantiation of noexcept-specifiers to avoid
15088 excessive instantiations (c++/49107). */
15089 new_specs = make_node (DEFERRED_NOEXCEPT);
15090 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15091 {
15092 /* We already partially instantiated this member template,
15093 so combine the new args with the old. */
15094 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15095 = DEFERRED_NOEXCEPT_PATTERN (expr);
15096 DEFERRED_NOEXCEPT_ARGS (new_specs)
15097 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15098 }
15099 else
15100 {
15101 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15102 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15103 }
15104 }
15105 else
15106 {
15107 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15108 {
15109 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15110 args);
15111 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15112 }
15113 new_specs = tsubst_copy_and_build
15114 (expr, args, complain, in_decl, /*function_p=*/false,
15115 /*integral_constant_expression_p=*/true);
15116 }
15117 new_specs = build_noexcept_spec (new_specs, complain);
15118 }
15119 else if (specs)
15120 {
15121 if (! TREE_VALUE (specs))
15122 new_specs = specs;
15123 else
15124 while (specs)
15125 {
15126 tree spec;
15127 int i, len = 1;
15128 tree expanded_specs = NULL_TREE;
15129
15130 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15131 {
15132 /* Expand the pack expansion type. */
15133 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15134 args, complain,
15135 in_decl);
15136
15137 if (expanded_specs == error_mark_node)
15138 return error_mark_node;
15139 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15140 len = TREE_VEC_LENGTH (expanded_specs);
15141 else
15142 {
15143 /* We're substituting into a member template, so
15144 we got a TYPE_PACK_EXPANSION back. Add that
15145 expansion and move on. */
15146 gcc_assert (TREE_CODE (expanded_specs)
15147 == TYPE_PACK_EXPANSION);
15148 new_specs = add_exception_specifier (new_specs,
15149 expanded_specs,
15150 complain);
15151 specs = TREE_CHAIN (specs);
15152 continue;
15153 }
15154 }
15155
15156 for (i = 0; i < len; ++i)
15157 {
15158 if (expanded_specs)
15159 spec = TREE_VEC_ELT (expanded_specs, i);
15160 else
15161 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15162 if (spec == error_mark_node)
15163 return spec;
15164 new_specs = add_exception_specifier (new_specs, spec,
15165 complain);
15166 }
15167
15168 specs = TREE_CHAIN (specs);
15169 }
15170 }
15171 return new_specs;
15172 }
15173
15174 /* Take the tree structure T and replace template parameters used
15175 therein with the argument vector ARGS. IN_DECL is an associated
15176 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15177 Issue error and warning messages under control of COMPLAIN. Note
15178 that we must be relatively non-tolerant of extensions here, in
15179 order to preserve conformance; if we allow substitutions that
15180 should not be allowed, we may allow argument deductions that should
15181 not succeed, and therefore report ambiguous overload situations
15182 where there are none. In theory, we could allow the substitution,
15183 but indicate that it should have failed, and allow our caller to
15184 make sure that the right thing happens, but we don't try to do this
15185 yet.
15186
15187 This function is used for dealing with types, decls and the like;
15188 for expressions, use tsubst_expr or tsubst_copy. */
15189
15190 tree
tsubst(tree t,tree args,tsubst_flags_t complain,tree in_decl)15191 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15192 {
15193 enum tree_code code;
15194 tree type, r = NULL_TREE;
15195
15196 if (t == NULL_TREE || t == error_mark_node
15197 || t == integer_type_node
15198 || t == void_type_node
15199 || t == char_type_node
15200 || t == unknown_type_node
15201 || TREE_CODE (t) == NAMESPACE_DECL
15202 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15203 return t;
15204
15205 if (DECL_P (t))
15206 return tsubst_decl (t, args, complain);
15207
15208 if (args == NULL_TREE)
15209 return t;
15210
15211 code = TREE_CODE (t);
15212
15213 if (code == IDENTIFIER_NODE)
15214 type = IDENTIFIER_TYPE_VALUE (t);
15215 else
15216 type = TREE_TYPE (t);
15217
15218 gcc_assert (type != unknown_type_node);
15219
15220 /* Reuse typedefs. We need to do this to handle dependent attributes,
15221 such as attribute aligned. */
15222 if (TYPE_P (t)
15223 && typedef_variant_p (t))
15224 {
15225 tree decl = TYPE_NAME (t);
15226
15227 if (alias_template_specialization_p (t, nt_opaque))
15228 {
15229 /* DECL represents an alias template and we want to
15230 instantiate it. */
15231 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15232 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15233 r = instantiate_alias_template (tmpl, gen_args, complain);
15234 }
15235 else if (DECL_CLASS_SCOPE_P (decl)
15236 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15237 && uses_template_parms (DECL_CONTEXT (decl)))
15238 {
15239 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15240 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15241 r = retrieve_specialization (tmpl, gen_args, 0);
15242 }
15243 else if (DECL_FUNCTION_SCOPE_P (decl)
15244 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15245 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15246 r = retrieve_local_specialization (decl);
15247 else
15248 /* The typedef is from a non-template context. */
15249 return t;
15250
15251 if (r)
15252 {
15253 r = TREE_TYPE (r);
15254 r = cp_build_qualified_type_real
15255 (r, cp_type_quals (t) | cp_type_quals (r),
15256 complain | tf_ignore_bad_quals);
15257 return r;
15258 }
15259 else
15260 {
15261 /* We don't have an instantiation yet, so drop the typedef. */
15262 int quals = cp_type_quals (t);
15263 t = DECL_ORIGINAL_TYPE (decl);
15264 t = cp_build_qualified_type_real (t, quals,
15265 complain | tf_ignore_bad_quals);
15266 }
15267 }
15268
15269 bool fndecl_type = (complain & tf_fndecl_type);
15270 complain &= ~tf_fndecl_type;
15271
15272 if (type
15273 && code != TYPENAME_TYPE
15274 && code != TEMPLATE_TYPE_PARM
15275 && code != TEMPLATE_PARM_INDEX
15276 && code != IDENTIFIER_NODE
15277 && code != FUNCTION_TYPE
15278 && code != METHOD_TYPE)
15279 type = tsubst (type, args, complain, in_decl);
15280 if (type == error_mark_node)
15281 return error_mark_node;
15282
15283 switch (code)
15284 {
15285 case RECORD_TYPE:
15286 case UNION_TYPE:
15287 case ENUMERAL_TYPE:
15288 return tsubst_aggr_type (t, args, complain, in_decl,
15289 /*entering_scope=*/0);
15290
15291 case ERROR_MARK:
15292 case IDENTIFIER_NODE:
15293 case VOID_TYPE:
15294 case REAL_TYPE:
15295 case COMPLEX_TYPE:
15296 case VECTOR_TYPE:
15297 case BOOLEAN_TYPE:
15298 case NULLPTR_TYPE:
15299 case LANG_TYPE:
15300 return t;
15301
15302 case INTEGER_TYPE:
15303 if (t == integer_type_node)
15304 return t;
15305
15306 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15307 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15308 return t;
15309
15310 {
15311 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15312
15313 max = tsubst_expr (omax, args, complain, in_decl,
15314 /*integral_constant_expression_p=*/false);
15315
15316 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15317 needed. */
15318 if (TREE_CODE (max) == NOP_EXPR
15319 && TREE_SIDE_EFFECTS (omax)
15320 && !TREE_TYPE (max))
15321 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15322
15323 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15324 with TREE_SIDE_EFFECTS that indicates this is not an integral
15325 constant expression. */
15326 if (processing_template_decl
15327 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15328 {
15329 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15330 TREE_SIDE_EFFECTS (max) = 1;
15331 }
15332
15333 return compute_array_index_type (NULL_TREE, max, complain);
15334 }
15335
15336 case TEMPLATE_TYPE_PARM:
15337 case TEMPLATE_TEMPLATE_PARM:
15338 case BOUND_TEMPLATE_TEMPLATE_PARM:
15339 case TEMPLATE_PARM_INDEX:
15340 {
15341 int idx;
15342 int level;
15343 int levels;
15344 tree arg = NULL_TREE;
15345
15346 r = NULL_TREE;
15347
15348 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15349 template_parm_level_and_index (t, &level, &idx);
15350
15351 levels = TMPL_ARGS_DEPTH (args);
15352 if (level <= levels
15353 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15354 {
15355 arg = TMPL_ARG (args, level, idx);
15356
15357 /* See through ARGUMENT_PACK_SELECT arguments. */
15358 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15359 arg = argument_pack_select_arg (arg);
15360 }
15361
15362 if (arg == error_mark_node)
15363 return error_mark_node;
15364 else if (arg != NULL_TREE)
15365 {
15366 if (ARGUMENT_PACK_P (arg))
15367 /* If ARG is an argument pack, we don't actually want to
15368 perform a substitution here, because substitutions
15369 for argument packs are only done
15370 element-by-element. We can get to this point when
15371 substituting the type of a non-type template
15372 parameter pack, when that type actually contains
15373 template parameter packs from an outer template, e.g.,
15374
15375 template<typename... Types> struct A {
15376 template<Types... Values> struct B { };
15377 }; */
15378 return t;
15379
15380 if (code == TEMPLATE_TYPE_PARM)
15381 {
15382 int quals;
15383
15384 /* When building concept checks for the purpose of
15385 deducing placeholders, we can end up with wildcards
15386 where types are expected. Adjust this to the deduced
15387 value. */
15388 if (TREE_CODE (arg) == WILDCARD_DECL)
15389 arg = TREE_TYPE (TREE_TYPE (arg));
15390
15391 gcc_assert (TYPE_P (arg));
15392
15393 quals = cp_type_quals (arg) | cp_type_quals (t);
15394
15395 return cp_build_qualified_type_real
15396 (arg, quals, complain | tf_ignore_bad_quals);
15397 }
15398 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15399 {
15400 /* We are processing a type constructed from a
15401 template template parameter. */
15402 tree argvec = tsubst (TYPE_TI_ARGS (t),
15403 args, complain, in_decl);
15404 if (argvec == error_mark_node)
15405 return error_mark_node;
15406
15407 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15408 || TREE_CODE (arg) == TEMPLATE_DECL
15409 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15410
15411 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15412 /* Consider this code:
15413
15414 template <template <class> class Template>
15415 struct Internal {
15416 template <class Arg> using Bind = Template<Arg>;
15417 };
15418
15419 template <template <class> class Template, class Arg>
15420 using Instantiate = Template<Arg>; //#0
15421
15422 template <template <class> class Template,
15423 class Argument>
15424 using Bind =
15425 Instantiate<Internal<Template>::template Bind,
15426 Argument>; //#1
15427
15428 When #1 is parsed, the
15429 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15430 parameter `Template' in #0 matches the
15431 UNBOUND_CLASS_TEMPLATE representing the argument
15432 `Internal<Template>::template Bind'; We then want
15433 to assemble the type `Bind<Argument>' that can't
15434 be fully created right now, because
15435 `Internal<Template>' not being complete, the Bind
15436 template cannot be looked up in that context. So
15437 we need to "store" `Bind<Argument>' for later
15438 when the context of Bind becomes complete. Let's
15439 store that in a TYPENAME_TYPE. */
15440 return make_typename_type (TYPE_CONTEXT (arg),
15441 build_nt (TEMPLATE_ID_EXPR,
15442 TYPE_IDENTIFIER (arg),
15443 argvec),
15444 typename_type,
15445 complain);
15446
15447 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15448 are resolving nested-types in the signature of a
15449 member function templates. Otherwise ARG is a
15450 TEMPLATE_DECL and is the real template to be
15451 instantiated. */
15452 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15453 arg = TYPE_NAME (arg);
15454
15455 r = lookup_template_class (arg,
15456 argvec, in_decl,
15457 DECL_CONTEXT (arg),
15458 /*entering_scope=*/0,
15459 complain);
15460 return cp_build_qualified_type_real
15461 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15462 }
15463 else if (code == TEMPLATE_TEMPLATE_PARM)
15464 return arg;
15465 else
15466 /* TEMPLATE_PARM_INDEX. */
15467 return convert_from_reference (unshare_expr (arg));
15468 }
15469
15470 if (level == 1)
15471 /* This can happen during the attempted tsubst'ing in
15472 unify. This means that we don't yet have any information
15473 about the template parameter in question. */
15474 return t;
15475
15476 /* Early in template argument deduction substitution, we don't
15477 want to reduce the level of 'auto', or it will be confused
15478 with a normal template parm in subsequent deduction.
15479 Similarly, don't reduce the level of template parameters to
15480 avoid mismatches when deducing their types. */
15481 if (complain & tf_partial)
15482 return t;
15483
15484 /* If we get here, we must have been looking at a parm for a
15485 more deeply nested template. Make a new version of this
15486 template parameter, but with a lower level. */
15487 switch (code)
15488 {
15489 case TEMPLATE_TYPE_PARM:
15490 case TEMPLATE_TEMPLATE_PARM:
15491 case BOUND_TEMPLATE_TEMPLATE_PARM:
15492 if (cp_type_quals (t))
15493 {
15494 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15495 r = cp_build_qualified_type_real
15496 (r, cp_type_quals (t),
15497 complain | (code == TEMPLATE_TYPE_PARM
15498 ? tf_ignore_bad_quals : 0));
15499 }
15500 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15501 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15502 && (r = (TEMPLATE_PARM_DESCENDANTS
15503 (TEMPLATE_TYPE_PARM_INDEX (t))))
15504 && (r = TREE_TYPE (r))
15505 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15506 /* Break infinite recursion when substituting the constraints
15507 of a constrained placeholder. */;
15508 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15509 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15510 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15511 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15512 r = TEMPLATE_PARM_DESCENDANTS (arg))
15513 && (TEMPLATE_PARM_LEVEL (r)
15514 == TEMPLATE_PARM_LEVEL (arg) - levels))
15515 /* Cache the simple case of lowering a type parameter. */
15516 r = TREE_TYPE (r);
15517 else
15518 {
15519 r = copy_type (t);
15520 TEMPLATE_TYPE_PARM_INDEX (r)
15521 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15522 r, levels, args, complain);
15523 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15524 TYPE_MAIN_VARIANT (r) = r;
15525 TYPE_POINTER_TO (r) = NULL_TREE;
15526 TYPE_REFERENCE_TO (r) = NULL_TREE;
15527
15528 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15529 {
15530 /* Propagate constraints on placeholders since they are
15531 only instantiated during satisfaction. */
15532 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15533 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15534 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15535 {
15536 pl = tsubst_copy (pl, args, complain, in_decl);
15537 if (TREE_CODE (pl) == TEMPLATE_TEMPLATE_PARM)
15538 pl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (pl);
15539 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15540 }
15541 }
15542
15543 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15544 /* We have reduced the level of the template
15545 template parameter, but not the levels of its
15546 template parameters, so canonical_type_parameter
15547 will not be able to find the canonical template
15548 template parameter for this level. Thus, we
15549 require structural equality checking to compare
15550 TEMPLATE_TEMPLATE_PARMs. */
15551 SET_TYPE_STRUCTURAL_EQUALITY (r);
15552 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15553 SET_TYPE_STRUCTURAL_EQUALITY (r);
15554 else
15555 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15556
15557 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15558 {
15559 tree tinfo = TYPE_TEMPLATE_INFO (t);
15560 /* We might need to substitute into the types of non-type
15561 template parameters. */
15562 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15563 complain, in_decl);
15564 if (tmpl == error_mark_node)
15565 return error_mark_node;
15566 tree argvec = tsubst (TI_ARGS (tinfo), args,
15567 complain, in_decl);
15568 if (argvec == error_mark_node)
15569 return error_mark_node;
15570
15571 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15572 = build_template_info (tmpl, argvec);
15573 }
15574 }
15575 break;
15576
15577 case TEMPLATE_PARM_INDEX:
15578 /* OK, now substitute the type of the non-type parameter. We
15579 couldn't do it earlier because it might be an auto parameter,
15580 and we wouldn't need to if we had an argument. */
15581 type = tsubst (type, args, complain, in_decl);
15582 if (type == error_mark_node)
15583 return error_mark_node;
15584 r = reduce_template_parm_level (t, type, levels, args, complain);
15585 break;
15586
15587 default:
15588 gcc_unreachable ();
15589 }
15590
15591 return r;
15592 }
15593
15594 case TREE_LIST:
15595 {
15596 tree purpose, value, chain;
15597
15598 if (t == void_list_node)
15599 return t;
15600
15601 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15602 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15603 {
15604 /* We have pack expansions, so expand those and
15605 create a new list out of it. */
15606
15607 /* Expand the argument expressions. */
15608 tree purposevec = NULL_TREE;
15609 if (TREE_PURPOSE (t))
15610 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15611 complain, in_decl);
15612 if (purposevec == error_mark_node)
15613 return error_mark_node;
15614
15615 tree valuevec = NULL_TREE;
15616 if (TREE_VALUE (t))
15617 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15618 complain, in_decl);
15619 if (valuevec == error_mark_node)
15620 return error_mark_node;
15621
15622 /* Build the rest of the list. */
15623 tree chain = TREE_CHAIN (t);
15624 if (chain && chain != void_type_node)
15625 chain = tsubst (chain, args, complain, in_decl);
15626 if (chain == error_mark_node)
15627 return error_mark_node;
15628
15629 /* Determine the number of arguments. */
15630 int len = -1;
15631 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15632 {
15633 len = TREE_VEC_LENGTH (purposevec);
15634 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15635 }
15636 else if (TREE_CODE (valuevec) == TREE_VEC)
15637 len = TREE_VEC_LENGTH (valuevec);
15638 else
15639 {
15640 /* Since we only performed a partial substitution into
15641 the argument pack, we only RETURN (a single list
15642 node. */
15643 if (purposevec == TREE_PURPOSE (t)
15644 && valuevec == TREE_VALUE (t)
15645 && chain == TREE_CHAIN (t))
15646 return t;
15647
15648 return tree_cons (purposevec, valuevec, chain);
15649 }
15650
15651 /* Convert the argument vectors into a TREE_LIST. */
15652 for (int i = len; i-- > 0; )
15653 {
15654 purpose = (purposevec ? TREE_VEC_ELT (purposevec, i)
15655 : NULL_TREE);
15656 value = (valuevec ? TREE_VEC_ELT (valuevec, i)
15657 : NULL_TREE);
15658
15659 /* Build the list (backwards). */
15660 chain = hash_tree_cons (purpose, value, chain);
15661 }
15662
15663 return chain;
15664 }
15665
15666 purpose = TREE_PURPOSE (t);
15667 if (purpose)
15668 {
15669 purpose = tsubst (purpose, args, complain, in_decl);
15670 if (purpose == error_mark_node)
15671 return error_mark_node;
15672 }
15673 value = TREE_VALUE (t);
15674 if (value)
15675 {
15676 value = tsubst (value, args, complain, in_decl);
15677 if (value == error_mark_node)
15678 return error_mark_node;
15679 }
15680 chain = TREE_CHAIN (t);
15681 if (chain && chain != void_type_node)
15682 {
15683 chain = tsubst (chain, args, complain, in_decl);
15684 if (chain == error_mark_node)
15685 return error_mark_node;
15686 }
15687 if (purpose == TREE_PURPOSE (t)
15688 && value == TREE_VALUE (t)
15689 && chain == TREE_CHAIN (t))
15690 return t;
15691 return hash_tree_cons (purpose, value, chain);
15692 }
15693
15694 case TREE_BINFO:
15695 /* We should never be tsubsting a binfo. */
15696 gcc_unreachable ();
15697
15698 case TREE_VEC:
15699 /* A vector of template arguments. */
15700 gcc_assert (!type);
15701 return tsubst_template_args (t, args, complain, in_decl);
15702
15703 case POINTER_TYPE:
15704 case REFERENCE_TYPE:
15705 {
15706 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15707 return t;
15708
15709 /* [temp.deduct]
15710
15711 Type deduction may fail for any of the following
15712 reasons:
15713
15714 -- Attempting to create a pointer to reference type.
15715 -- Attempting to create a reference to a reference type or
15716 a reference to void.
15717
15718 Core issue 106 says that creating a reference to a reference
15719 during instantiation is no longer a cause for failure. We
15720 only enforce this check in strict C++98 mode. */
15721 if ((TYPE_REF_P (type)
15722 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15723 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15724 {
15725 static location_t last_loc;
15726
15727 /* We keep track of the last time we issued this error
15728 message to avoid spewing a ton of messages during a
15729 single bad template instantiation. */
15730 if (complain & tf_error
15731 && last_loc != input_location)
15732 {
15733 if (VOID_TYPE_P (type))
15734 error ("forming reference to void");
15735 else if (code == POINTER_TYPE)
15736 error ("forming pointer to reference type %qT", type);
15737 else
15738 error ("forming reference to reference type %qT", type);
15739 last_loc = input_location;
15740 }
15741
15742 return error_mark_node;
15743 }
15744 else if (TREE_CODE (type) == FUNCTION_TYPE
15745 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15746 || type_memfn_rqual (type) != REF_QUAL_NONE))
15747 {
15748 if (complain & tf_error)
15749 {
15750 if (code == POINTER_TYPE)
15751 error ("forming pointer to qualified function type %qT",
15752 type);
15753 else
15754 error ("forming reference to qualified function type %qT",
15755 type);
15756 }
15757 return error_mark_node;
15758 }
15759 else if (code == POINTER_TYPE)
15760 {
15761 r = build_pointer_type (type);
15762 if (TREE_CODE (type) == METHOD_TYPE)
15763 r = build_ptrmemfunc_type (r);
15764 }
15765 else if (TYPE_REF_P (type))
15766 /* In C++0x, during template argument substitution, when there is an
15767 attempt to create a reference to a reference type, reference
15768 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15769
15770 "If a template-argument for a template-parameter T names a type
15771 that is a reference to a type A, an attempt to create the type
15772 'lvalue reference to cv T' creates the type 'lvalue reference to
15773 A,' while an attempt to create the type type rvalue reference to
15774 cv T' creates the type T"
15775 */
15776 r = cp_build_reference_type
15777 (TREE_TYPE (type),
15778 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15779 else
15780 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15781 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15782
15783 if (r != error_mark_node)
15784 /* Will this ever be needed for TYPE_..._TO values? */
15785 layout_type (r);
15786
15787 return r;
15788 }
15789 case OFFSET_TYPE:
15790 {
15791 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15792 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15793 {
15794 /* [temp.deduct]
15795
15796 Type deduction may fail for any of the following
15797 reasons:
15798
15799 -- Attempting to create "pointer to member of T" when T
15800 is not a class type. */
15801 if (complain & tf_error)
15802 error ("creating pointer to member of non-class type %qT", r);
15803 return error_mark_node;
15804 }
15805 if (TYPE_REF_P (type))
15806 {
15807 if (complain & tf_error)
15808 error ("creating pointer to member reference type %qT", type);
15809 return error_mark_node;
15810 }
15811 if (VOID_TYPE_P (type))
15812 {
15813 if (complain & tf_error)
15814 error ("creating pointer to member of type void");
15815 return error_mark_node;
15816 }
15817 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15818 if (TREE_CODE (type) == FUNCTION_TYPE)
15819 {
15820 /* The type of the implicit object parameter gets its
15821 cv-qualifiers from the FUNCTION_TYPE. */
15822 tree memptr;
15823 tree method_type
15824 = build_memfn_type (type, r, type_memfn_quals (type),
15825 type_memfn_rqual (type));
15826 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15827 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15828 complain);
15829 }
15830 else
15831 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15832 cp_type_quals (t),
15833 complain);
15834 }
15835 case FUNCTION_TYPE:
15836 case METHOD_TYPE:
15837 {
15838 tree fntype;
15839 tree specs;
15840 fntype = tsubst_function_type (t, args, complain, in_decl);
15841 if (fntype == error_mark_node)
15842 return error_mark_node;
15843
15844 /* Substitute the exception specification. */
15845 specs = tsubst_exception_specification (t, args, complain, in_decl,
15846 /*defer_ok*/fndecl_type);
15847 if (specs == error_mark_node)
15848 return error_mark_node;
15849 if (specs)
15850 fntype = build_exception_variant (fntype, specs);
15851 return fntype;
15852 }
15853 case ARRAY_TYPE:
15854 {
15855 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15856 if (domain == error_mark_node)
15857 return error_mark_node;
15858
15859 /* As an optimization, we avoid regenerating the array type if
15860 it will obviously be the same as T. */
15861 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15862 return t;
15863
15864 /* These checks should match the ones in create_array_type_for_decl.
15865
15866 [temp.deduct]
15867
15868 The deduction may fail for any of the following reasons:
15869
15870 -- Attempting to create an array with an element type that
15871 is void, a function type, or a reference type, or [DR337]
15872 an abstract class type. */
15873 if (VOID_TYPE_P (type)
15874 || TREE_CODE (type) == FUNCTION_TYPE
15875 || (TREE_CODE (type) == ARRAY_TYPE
15876 && TYPE_DOMAIN (type) == NULL_TREE)
15877 || TYPE_REF_P (type))
15878 {
15879 if (complain & tf_error)
15880 error ("creating array of %qT", type);
15881 return error_mark_node;
15882 }
15883
15884 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
15885 !(complain & tf_error)))
15886 return error_mark_node;
15887
15888 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15889 return error_mark_node;
15890
15891 r = build_cplus_array_type (type, domain);
15892
15893 if (!valid_array_size_p (input_location, r, in_decl,
15894 (complain & tf_error)))
15895 return error_mark_node;
15896
15897 if (TYPE_USER_ALIGN (t))
15898 {
15899 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15900 TYPE_USER_ALIGN (r) = 1;
15901 }
15902
15903 return r;
15904 }
15905
15906 case TYPENAME_TYPE:
15907 {
15908 tree ctx = TYPE_CONTEXT (t);
15909 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15910 {
15911 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15912 if (ctx == error_mark_node
15913 || TREE_VEC_LENGTH (ctx) > 1)
15914 return error_mark_node;
15915 if (TREE_VEC_LENGTH (ctx) == 0)
15916 {
15917 if (complain & tf_error)
15918 error ("%qD is instantiated for an empty pack",
15919 TYPENAME_TYPE_FULLNAME (t));
15920 return error_mark_node;
15921 }
15922 ctx = TREE_VEC_ELT (ctx, 0);
15923 }
15924 else
15925 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15926 /*entering_scope=*/1);
15927 if (ctx == error_mark_node)
15928 return error_mark_node;
15929
15930 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15931 complain, in_decl);
15932 if (f == error_mark_node)
15933 return error_mark_node;
15934
15935 if (!MAYBE_CLASS_TYPE_P (ctx))
15936 {
15937 if (complain & tf_error)
15938 error ("%qT is not a class, struct, or union type", ctx);
15939 return error_mark_node;
15940 }
15941 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15942 {
15943 /* Normally, make_typename_type does not require that the CTX
15944 have complete type in order to allow things like:
15945
15946 template <class T> struct S { typename S<T>::X Y; };
15947
15948 But, such constructs have already been resolved by this
15949 point, so here CTX really should have complete type, unless
15950 it's a partial instantiation. */
15951 ctx = complete_type (ctx);
15952 if (!COMPLETE_TYPE_P (ctx))
15953 {
15954 if (complain & tf_error)
15955 cxx_incomplete_type_error (NULL_TREE, ctx);
15956 return error_mark_node;
15957 }
15958 }
15959
15960 f = make_typename_type (ctx, f, typename_type,
15961 complain | tf_keep_type_decl);
15962 if (f == error_mark_node)
15963 return f;
15964 if (TREE_CODE (f) == TYPE_DECL)
15965 {
15966 complain |= tf_ignore_bad_quals;
15967 f = TREE_TYPE (f);
15968 }
15969
15970 if (TREE_CODE (f) != TYPENAME_TYPE)
15971 {
15972 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15973 {
15974 if (complain & tf_error)
15975 error ("%qT resolves to %qT, which is not an enumeration type",
15976 t, f);
15977 else
15978 return error_mark_node;
15979 }
15980 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15981 {
15982 if (complain & tf_error)
15983 error ("%qT resolves to %qT, which is not a class type",
15984 t, f);
15985 else
15986 return error_mark_node;
15987 }
15988 }
15989
15990 return cp_build_qualified_type_real
15991 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15992 }
15993
15994 case UNBOUND_CLASS_TEMPLATE:
15995 {
15996 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15997 in_decl, /*entering_scope=*/1);
15998 tree name = TYPE_IDENTIFIER (t);
15999 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16000
16001 if (ctx == error_mark_node || name == error_mark_node)
16002 return error_mark_node;
16003
16004 if (parm_list)
16005 parm_list = tsubst_template_parms (parm_list, args, complain);
16006 return make_unbound_class_template (ctx, name, parm_list, complain);
16007 }
16008
16009 case TYPEOF_TYPE:
16010 {
16011 tree type;
16012
16013 ++cp_unevaluated_operand;
16014 ++c_inhibit_evaluation_warnings;
16015
16016 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
16017 complain, in_decl,
16018 /*integral_constant_expression_p=*/false);
16019
16020 --cp_unevaluated_operand;
16021 --c_inhibit_evaluation_warnings;
16022
16023 type = finish_typeof (type);
16024 return cp_build_qualified_type_real (type,
16025 cp_type_quals (t)
16026 | cp_type_quals (type),
16027 complain);
16028 }
16029
16030 case DECLTYPE_TYPE:
16031 {
16032 tree type;
16033
16034 ++cp_unevaluated_operand;
16035 ++c_inhibit_evaluation_warnings;
16036
16037 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16038 complain|tf_decltype, in_decl,
16039 /*function_p*/false,
16040 /*integral_constant_expression*/false);
16041
16042 --cp_unevaluated_operand;
16043 --c_inhibit_evaluation_warnings;
16044
16045 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16046 type = lambda_capture_field_type (type,
16047 false /*explicit_init*/,
16048 DECLTYPE_FOR_REF_CAPTURE (t));
16049 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16050 type = lambda_proxy_type (type);
16051 else
16052 {
16053 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16054 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16055 && EXPR_P (type))
16056 /* In a template ~id could be either a complement expression
16057 or an unqualified-id naming a destructor; if instantiating
16058 it produces an expression, it's not an id-expression or
16059 member access. */
16060 id = false;
16061 type = finish_decltype_type (type, id, complain);
16062 }
16063 return cp_build_qualified_type_real (type,
16064 cp_type_quals (t)
16065 | cp_type_quals (type),
16066 complain | tf_ignore_bad_quals);
16067 }
16068
16069 case UNDERLYING_TYPE:
16070 {
16071 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16072 complain, in_decl);
16073 return finish_underlying_type (type);
16074 }
16075
16076 case TYPE_ARGUMENT_PACK:
16077 case NONTYPE_ARGUMENT_PACK:
16078 {
16079 tree r;
16080
16081 if (code == NONTYPE_ARGUMENT_PACK)
16082 r = make_node (code);
16083 else
16084 r = cxx_make_type (code);
16085
16086 tree pack_args = ARGUMENT_PACK_ARGS (t);
16087 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
16088 SET_ARGUMENT_PACK_ARGS (r, pack_args);
16089
16090 return r;
16091 }
16092
16093 case VOID_CST:
16094 case INTEGER_CST:
16095 case REAL_CST:
16096 case STRING_CST:
16097 case PLUS_EXPR:
16098 case MINUS_EXPR:
16099 case NEGATE_EXPR:
16100 case NOP_EXPR:
16101 case INDIRECT_REF:
16102 case ADDR_EXPR:
16103 case CALL_EXPR:
16104 case ARRAY_REF:
16105 case SCOPE_REF:
16106 /* We should use one of the expression tsubsts for these codes. */
16107 gcc_unreachable ();
16108
16109 default:
16110 sorry ("use of %qs in template", get_tree_code_name (code));
16111 return error_mark_node;
16112 }
16113 }
16114
16115 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16116 expression on the left-hand side of the "." or "->" operator. We
16117 only do the lookup if we had a dependent BASELINK. Otherwise we
16118 adjust it onto the instantiated heirarchy. */
16119
16120 static tree
tsubst_baselink(tree baselink,tree object_type,tree args,tsubst_flags_t complain,tree in_decl)16121 tsubst_baselink (tree baselink, tree object_type,
16122 tree args, tsubst_flags_t complain, tree in_decl)
16123 {
16124 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16125 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16126 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16127
16128 tree optype = BASELINK_OPTYPE (baselink);
16129 optype = tsubst (optype, args, complain, in_decl);
16130
16131 tree template_args = NULL_TREE;
16132 bool template_id_p = false;
16133 tree fns = BASELINK_FUNCTIONS (baselink);
16134 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16135 {
16136 template_id_p = true;
16137 template_args = TREE_OPERAND (fns, 1);
16138 fns = TREE_OPERAND (fns, 0);
16139 if (template_args)
16140 template_args = tsubst_template_args (template_args, args,
16141 complain, in_decl);
16142 }
16143
16144 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16145 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16146 bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
16147 || optype != BASELINK_OPTYPE (baselink));
16148
16149 if (dependent_p)
16150 {
16151 tree name = OVL_NAME (fns);
16152 if (IDENTIFIER_CONV_OP_P (name))
16153 name = make_conv_op_name (optype);
16154
16155 if (name == complete_dtor_identifier)
16156 /* Treat as-if non-dependent below. */
16157 dependent_p = false;
16158
16159 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
16160 if (!baselink)
16161 {
16162 if ((complain & tf_error)
16163 && constructor_name_p (name, qualifying_scope))
16164 error ("cannot call constructor %<%T::%D%> directly",
16165 qualifying_scope, name);
16166 return error_mark_node;
16167 }
16168
16169 if (BASELINK_P (baselink))
16170 fns = BASELINK_FUNCTIONS (baselink);
16171 }
16172 else
16173 {
16174 /* We're going to overwrite pieces below, make a duplicate. */
16175 baselink = copy_node (baselink);
16176
16177 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
16178 {
16179 /* The decl we found was from non-dependent scope, but we still need
16180 to update the binfos for the instantiated qualifying_scope. */
16181 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
16182 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
16183 ba_unique, NULL, complain);
16184 }
16185 }
16186
16187 /* If lookup found a single function, mark it as used at this point.
16188 (If lookup found multiple functions the one selected later by
16189 overload resolution will be marked as used at that point.) */
16190 if (!template_id_p && !really_overloaded_fn (fns))
16191 {
16192 tree fn = OVL_FIRST (fns);
16193 bool ok = mark_used (fn, complain);
16194 if (!ok && !(complain & tf_error))
16195 return error_mark_node;
16196 if (ok && BASELINK_P (baselink))
16197 /* We might have instantiated an auto function. */
16198 TREE_TYPE (baselink) = TREE_TYPE (fn);
16199 }
16200
16201 if (BASELINK_P (baselink))
16202 {
16203 /* Add back the template arguments, if present. */
16204 if (template_id_p)
16205 BASELINK_FUNCTIONS (baselink)
16206 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16207
16208 /* Update the conversion operator type. */
16209 BASELINK_OPTYPE (baselink) = optype;
16210 }
16211
16212 if (!object_type)
16213 object_type = current_class_type;
16214
16215 if (qualified_p || !dependent_p)
16216 {
16217 baselink = adjust_result_of_qualified_name_lookup (baselink,
16218 qualifying_scope,
16219 object_type);
16220 if (!qualified_p)
16221 /* We need to call adjust_result_of_qualified_name_lookup in case the
16222 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16223 so that we still get virtual function binding. */
16224 BASELINK_QUALIFIED_P (baselink) = false;
16225 }
16226
16227 return baselink;
16228 }
16229
16230 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16231 true if the qualified-id will be a postfix-expression in-and-of
16232 itself; false if more of the postfix-expression follows the
16233 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16234 of "&". */
16235
16236 static tree
tsubst_qualified_id(tree qualified_id,tree args,tsubst_flags_t complain,tree in_decl,bool done,bool address_p)16237 tsubst_qualified_id (tree qualified_id, tree args,
16238 tsubst_flags_t complain, tree in_decl,
16239 bool done, bool address_p)
16240 {
16241 tree expr;
16242 tree scope;
16243 tree name;
16244 bool is_template;
16245 tree template_args;
16246 location_t loc = UNKNOWN_LOCATION;
16247
16248 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16249
16250 /* Figure out what name to look up. */
16251 name = TREE_OPERAND (qualified_id, 1);
16252 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16253 {
16254 is_template = true;
16255 loc = EXPR_LOCATION (name);
16256 template_args = TREE_OPERAND (name, 1);
16257 if (template_args)
16258 template_args = tsubst_template_args (template_args, args,
16259 complain, in_decl);
16260 if (template_args == error_mark_node)
16261 return error_mark_node;
16262 name = TREE_OPERAND (name, 0);
16263 }
16264 else
16265 {
16266 is_template = false;
16267 template_args = NULL_TREE;
16268 }
16269
16270 /* Substitute into the qualifying scope. When there are no ARGS, we
16271 are just trying to simplify a non-dependent expression. In that
16272 case the qualifying scope may be dependent, and, in any case,
16273 substituting will not help. */
16274 scope = TREE_OPERAND (qualified_id, 0);
16275 if (args)
16276 {
16277 scope = tsubst (scope, args, complain, in_decl);
16278 expr = tsubst_copy (name, args, complain, in_decl);
16279 }
16280 else
16281 expr = name;
16282
16283 if (dependent_scope_p (scope))
16284 {
16285 if (is_template)
16286 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16287 tree r = build_qualified_name (NULL_TREE, scope, expr,
16288 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16289 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16290 return r;
16291 }
16292
16293 if (!BASELINK_P (name) && !DECL_P (expr))
16294 {
16295 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16296 {
16297 /* A BIT_NOT_EXPR is used to represent a destructor. */
16298 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16299 {
16300 error ("qualifying type %qT does not match destructor name ~%qT",
16301 scope, TREE_OPERAND (expr, 0));
16302 expr = error_mark_node;
16303 }
16304 else
16305 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16306 /*is_type_p=*/0, false);
16307 }
16308 else
16309 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
16310 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16311 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16312 {
16313 if (complain & tf_error)
16314 {
16315 error ("dependent-name %qE is parsed as a non-type, but "
16316 "instantiation yields a type", qualified_id);
16317 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16318 }
16319 return error_mark_node;
16320 }
16321 }
16322
16323 if (DECL_P (expr))
16324 {
16325 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16326 scope);
16327 /* Remember that there was a reference to this entity. */
16328 if (!mark_used (expr, complain) && !(complain & tf_error))
16329 return error_mark_node;
16330 }
16331
16332 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16333 {
16334 if (complain & tf_error)
16335 qualified_name_lookup_error (scope,
16336 TREE_OPERAND (qualified_id, 1),
16337 expr, input_location);
16338 return error_mark_node;
16339 }
16340
16341 if (is_template)
16342 {
16343 /* We may be repeating a check already done during parsing, but
16344 if it was well-formed and passed then, it will pass again
16345 now, and if it didn't, we wouldn't have got here. The case
16346 we want to catch is when we couldn't tell then, and can now,
16347 namely when templ prior to substitution was an
16348 identifier. */
16349 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16350 return error_mark_node;
16351
16352 if (variable_template_p (expr))
16353 expr = lookup_and_finish_template_variable (expr, template_args,
16354 complain);
16355 else
16356 expr = lookup_template_function (expr, template_args);
16357 }
16358
16359 if (expr == error_mark_node && complain & tf_error)
16360 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16361 expr, input_location);
16362 else if (TYPE_P (scope))
16363 {
16364 expr = (adjust_result_of_qualified_name_lookup
16365 (expr, scope, current_nonlambda_class_type ()));
16366 expr = (finish_qualified_id_expr
16367 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16368 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16369 /*template_arg_p=*/false, complain));
16370 }
16371
16372 /* Expressions do not generally have reference type. */
16373 if (TREE_CODE (expr) != SCOPE_REF
16374 /* However, if we're about to form a pointer-to-member, we just
16375 want the referenced member referenced. */
16376 && TREE_CODE (expr) != OFFSET_REF)
16377 expr = convert_from_reference (expr);
16378
16379 if (REF_PARENTHESIZED_P (qualified_id))
16380 expr = force_paren_expr (expr);
16381
16382 return expr;
16383 }
16384
16385 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16386 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16387 for tsubst. */
16388
16389 static tree
tsubst_init(tree init,tree decl,tree args,tsubst_flags_t complain,tree in_decl)16390 tsubst_init (tree init, tree decl, tree args,
16391 tsubst_flags_t complain, tree in_decl)
16392 {
16393 if (!init)
16394 return NULL_TREE;
16395
16396 init = tsubst_expr (init, args, complain, in_decl, false);
16397
16398 tree type = TREE_TYPE (decl);
16399
16400 if (!init && type != error_mark_node)
16401 {
16402 if (tree auto_node = type_uses_auto (type))
16403 {
16404 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16405 {
16406 if (complain & tf_error)
16407 error ("initializer for %q#D expands to an empty list "
16408 "of expressions", decl);
16409 return error_mark_node;
16410 }
16411 }
16412 else if (!dependent_type_p (type))
16413 {
16414 /* If we had an initializer but it
16415 instantiated to nothing,
16416 value-initialize the object. This will
16417 only occur when the initializer was a
16418 pack expansion where the parameter packs
16419 used in that expansion were of length
16420 zero. */
16421 init = build_value_init (type, complain);
16422 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16423 init = get_target_expr_sfinae (init, complain);
16424 if (TREE_CODE (init) == TARGET_EXPR)
16425 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16426 }
16427 }
16428
16429 return init;
16430 }
16431
16432 /* If T is a reference to a dependent member of the current instantiation C and
16433 we are trying to refer to that member in a partial instantiation of C,
16434 return a SCOPE_REF; otherwise, return NULL_TREE.
16435
16436 This can happen when forming a C++20 alias template deduction guide, as in
16437 PR96199. */
16438
16439 static tree
maybe_dependent_member_ref(tree t,tree args,tsubst_flags_t complain,tree in_decl)16440 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16441 tree in_decl)
16442 {
16443 if (cxx_dialect < cxx2a)
16444 return NULL_TREE;
16445
16446 tree ctx = context_for_name_lookup (t);
16447 if (!CLASS_TYPE_P (ctx))
16448 return NULL_TREE;
16449
16450 ctx = tsubst (ctx, args, complain, in_decl);
16451 if (dependent_scope_p (ctx))
16452 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16453 /*template_p=*/false);
16454
16455 return NULL_TREE;
16456 }
16457
16458 /* Like tsubst, but deals with expressions. This function just replaces
16459 template parms; to finish processing the resultant expression, use
16460 tsubst_copy_and_build or tsubst_expr. */
16461
16462 static tree
tsubst_copy(tree t,tree args,tsubst_flags_t complain,tree in_decl)16463 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16464 {
16465 enum tree_code code;
16466 tree r;
16467
16468 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16469 return t;
16470
16471 code = TREE_CODE (t);
16472
16473 switch (code)
16474 {
16475 case PARM_DECL:
16476 r = retrieve_local_specialization (t);
16477
16478 if (r == NULL_TREE)
16479 {
16480 /* We get here for a use of 'this' in an NSDMI. */
16481 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16482 return current_class_ptr;
16483
16484 /* This can happen for a parameter name used later in a function
16485 declaration (such as in a late-specified return type). Just
16486 make a dummy decl, since it's only used for its type. */
16487 gcc_assert (cp_unevaluated_operand != 0);
16488 r = tsubst_decl (t, args, complain);
16489 /* Give it the template pattern as its context; its true context
16490 hasn't been instantiated yet and this is good enough for
16491 mangling. */
16492 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16493 }
16494
16495 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16496 r = argument_pack_select_arg (r);
16497 if (!mark_used (r, complain) && !(complain & tf_error))
16498 return error_mark_node;
16499 return r;
16500
16501 case CONST_DECL:
16502 {
16503 tree enum_type;
16504 tree v;
16505
16506 if (DECL_TEMPLATE_PARM_P (t))
16507 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16508 /* There is no need to substitute into namespace-scope
16509 enumerators. */
16510 if (DECL_NAMESPACE_SCOPE_P (t))
16511 return t;
16512 /* If ARGS is NULL, then T is known to be non-dependent. */
16513 if (args == NULL_TREE)
16514 return scalar_constant_value (t);
16515
16516 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16517 return ref;
16518
16519 /* Unfortunately, we cannot just call lookup_name here.
16520 Consider:
16521
16522 template <int I> int f() {
16523 enum E { a = I };
16524 struct S { void g() { E e = a; } };
16525 };
16526
16527 When we instantiate f<7>::S::g(), say, lookup_name is not
16528 clever enough to find f<7>::a. */
16529 enum_type
16530 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16531 /*entering_scope=*/0);
16532
16533 for (v = TYPE_VALUES (enum_type);
16534 v != NULL_TREE;
16535 v = TREE_CHAIN (v))
16536 if (TREE_PURPOSE (v) == DECL_NAME (t))
16537 return TREE_VALUE (v);
16538
16539 /* We didn't find the name. That should never happen; if
16540 name-lookup found it during preliminary parsing, we
16541 should find it again here during instantiation. */
16542 gcc_unreachable ();
16543 }
16544 return t;
16545
16546 case FIELD_DECL:
16547 if (DECL_CONTEXT (t))
16548 {
16549 tree ctx;
16550
16551 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16552 /*entering_scope=*/1);
16553 if (ctx != DECL_CONTEXT (t))
16554 {
16555 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16556 if (!r)
16557 {
16558 if (complain & tf_error)
16559 error ("using invalid field %qD", t);
16560 return error_mark_node;
16561 }
16562 return r;
16563 }
16564 }
16565
16566 return t;
16567
16568 case VAR_DECL:
16569 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16570 return ref;
16571 gcc_fallthrough();
16572 case FUNCTION_DECL:
16573 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16574 r = tsubst (t, args, complain, in_decl);
16575 else if (local_variable_p (t)
16576 && uses_template_parms (DECL_CONTEXT (t)))
16577 {
16578 r = retrieve_local_specialization (t);
16579 if (r == NULL_TREE)
16580 {
16581 /* First try name lookup to find the instantiation. */
16582 r = lookup_name (DECL_NAME (t));
16583 if (r)
16584 {
16585 if (!VAR_P (r))
16586 {
16587 /* During error-recovery we may find a non-variable,
16588 even an OVERLOAD: just bail out and avoid ICEs and
16589 duplicate diagnostics (c++/62207). */
16590 gcc_assert (seen_error ());
16591 return error_mark_node;
16592 }
16593 if (!is_capture_proxy (r))
16594 {
16595 /* Make sure the one we found is the one we want. */
16596 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16597 if (ctx != DECL_CONTEXT (r))
16598 r = NULL_TREE;
16599 }
16600 }
16601
16602 if (r)
16603 /* OK */;
16604 else
16605 {
16606 /* This can happen for a variable used in a
16607 late-specified return type of a local lambda, or for a
16608 local static or constant. Building a new VAR_DECL
16609 should be OK in all those cases. */
16610 r = tsubst_decl (t, args, complain);
16611 if (local_specializations)
16612 /* Avoid infinite recursion (79640). */
16613 register_local_specialization (r, t);
16614 if (decl_maybe_constant_var_p (r))
16615 {
16616 /* We can't call cp_finish_decl, so handle the
16617 initializer by hand. */
16618 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16619 complain, in_decl);
16620 if (!processing_template_decl)
16621 init = maybe_constant_init (init);
16622 if (processing_template_decl
16623 ? potential_constant_expression (init)
16624 : reduced_constant_expression_p (init))
16625 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16626 = TREE_CONSTANT (r) = true;
16627 DECL_INITIAL (r) = init;
16628 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16629 TREE_TYPE (r)
16630 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16631 complain, adc_variable_type);
16632 }
16633 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16634 || decl_constant_var_p (r)
16635 || seen_error ());
16636 if (!processing_template_decl
16637 && !TREE_STATIC (r))
16638 r = process_outer_var_ref (r, complain);
16639 }
16640 /* Remember this for subsequent uses. */
16641 if (local_specializations)
16642 register_local_specialization (r, t);
16643 }
16644 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16645 r = argument_pack_select_arg (r);
16646 }
16647 else
16648 r = t;
16649 if (!mark_used (r, complain))
16650 return error_mark_node;
16651 return r;
16652
16653 case NAMESPACE_DECL:
16654 return t;
16655
16656 case OVERLOAD:
16657 return t;
16658
16659 case BASELINK:
16660 return tsubst_baselink (t, current_nonlambda_class_type (),
16661 args, complain, in_decl);
16662
16663 case TEMPLATE_DECL:
16664 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16665 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16666 args, complain, in_decl);
16667 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16668 return tsubst (t, args, complain, in_decl);
16669 else if (DECL_CLASS_SCOPE_P (t)
16670 && uses_template_parms (DECL_CONTEXT (t)))
16671 {
16672 /* Template template argument like the following example need
16673 special treatment:
16674
16675 template <template <class> class TT> struct C {};
16676 template <class T> struct D {
16677 template <class U> struct E {};
16678 C<E> c; // #1
16679 };
16680 D<int> d; // #2
16681
16682 We are processing the template argument `E' in #1 for
16683 the template instantiation #2. Originally, `E' is a
16684 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16685 have to substitute this with one having context `D<int>'. */
16686
16687 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16688 if (dependent_scope_p (context))
16689 {
16690 /* When rewriting a constructor into a deduction guide, a
16691 non-dependent name can become dependent, so memtmpl<args>
16692 becomes context::template memtmpl<args>. */
16693 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16694 return build_qualified_name (type, context, DECL_NAME (t),
16695 /*template*/true);
16696 }
16697 return lookup_field (context, DECL_NAME(t), 0, false);
16698 }
16699 else
16700 /* Ordinary template template argument. */
16701 return t;
16702
16703 case NON_LVALUE_EXPR:
16704 case VIEW_CONVERT_EXPR:
16705 {
16706 /* Handle location wrappers by substituting the wrapped node
16707 first, *then* reusing the resulting type. Doing the type
16708 first ensures that we handle template parameters and
16709 parameter pack expansions. */
16710 if (location_wrapper_p (t))
16711 {
16712 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16713 complain, in_decl);
16714 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16715 }
16716 tree op = TREE_OPERAND (t, 0);
16717 if (code == VIEW_CONVERT_EXPR
16718 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16719 {
16720 /* Wrapper to make a C++20 template parameter object const. */
16721 op = tsubst_copy (op, args, complain, in_decl);
16722 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16723 {
16724 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16725 return build1 (code, type, op);
16726 }
16727 else if (!CP_TYPE_CONST_P (TREE_TYPE (op)))
16728 {
16729 /* The template argument is not const, presumably because
16730 it is still dependent, and so not the const template parm
16731 object. */
16732 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16733 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
16734 (type, TREE_TYPE (op)));
16735 if (TREE_CODE (op) == CONSTRUCTOR
16736 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
16737 {
16738 /* Don't add a wrapper to these. */
16739 op = copy_node (op);
16740 TREE_TYPE (op) = type;
16741 }
16742 else
16743 /* Do add a wrapper otherwise. */
16744 op = build1 (code, type, op);
16745 }
16746 return op;
16747 }
16748 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16749 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16750 {
16751 op = tsubst_copy (op, args, complain, in_decl);
16752 op = build1 (code, TREE_TYPE (op), op);
16753 REF_PARENTHESIZED_P (op) = true;
16754 return op;
16755 }
16756 /* We shouldn't see any other uses of these in templates. */
16757 gcc_unreachable ();
16758 }
16759
16760 case CAST_EXPR:
16761 case REINTERPRET_CAST_EXPR:
16762 case CONST_CAST_EXPR:
16763 case STATIC_CAST_EXPR:
16764 case DYNAMIC_CAST_EXPR:
16765 case IMPLICIT_CONV_EXPR:
16766 case CONVERT_EXPR:
16767 case NOP_EXPR:
16768 {
16769 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16770 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16771 return build1 (code, type, op0);
16772 }
16773
16774 case SIZEOF_EXPR:
16775 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16776 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16777 {
16778 tree expanded, op = TREE_OPERAND (t, 0);
16779 int len = 0;
16780
16781 if (SIZEOF_EXPR_TYPE_P (t))
16782 op = TREE_TYPE (op);
16783
16784 ++cp_unevaluated_operand;
16785 ++c_inhibit_evaluation_warnings;
16786 /* We only want to compute the number of arguments. */
16787 if (PACK_EXPANSION_P (op))
16788 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16789 else
16790 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16791 args, complain, in_decl);
16792 --cp_unevaluated_operand;
16793 --c_inhibit_evaluation_warnings;
16794
16795 if (TREE_CODE (expanded) == TREE_VEC)
16796 {
16797 len = TREE_VEC_LENGTH (expanded);
16798 /* Set TREE_USED for the benefit of -Wunused. */
16799 for (int i = 0; i < len; i++)
16800 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16801 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16802 }
16803
16804 if (expanded == error_mark_node)
16805 return error_mark_node;
16806 else if (PACK_EXPANSION_P (expanded)
16807 || (TREE_CODE (expanded) == TREE_VEC
16808 && pack_expansion_args_count (expanded)))
16809
16810 {
16811 if (PACK_EXPANSION_P (expanded))
16812 /* OK. */;
16813 else if (TREE_VEC_LENGTH (expanded) == 1)
16814 expanded = TREE_VEC_ELT (expanded, 0);
16815 else
16816 expanded = make_argument_pack (expanded);
16817
16818 if (TYPE_P (expanded))
16819 return cxx_sizeof_or_alignof_type (input_location,
16820 expanded, SIZEOF_EXPR,
16821 false,
16822 complain & tf_error);
16823 else
16824 return cxx_sizeof_or_alignof_expr (input_location,
16825 expanded, SIZEOF_EXPR,
16826 complain & tf_error);
16827 }
16828 else
16829 return build_int_cst (size_type_node, len);
16830 }
16831 if (SIZEOF_EXPR_TYPE_P (t))
16832 {
16833 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16834 args, complain, in_decl);
16835 r = build1 (NOP_EXPR, r, error_mark_node);
16836 r = build1 (SIZEOF_EXPR,
16837 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16838 SIZEOF_EXPR_TYPE_P (r) = 1;
16839 return r;
16840 }
16841 /* Fall through */
16842
16843 case INDIRECT_REF:
16844 case NEGATE_EXPR:
16845 case TRUTH_NOT_EXPR:
16846 case BIT_NOT_EXPR:
16847 case ADDR_EXPR:
16848 case UNARY_PLUS_EXPR: /* Unary + */
16849 case ALIGNOF_EXPR:
16850 case AT_ENCODE_EXPR:
16851 case ARROW_EXPR:
16852 case THROW_EXPR:
16853 case TYPEID_EXPR:
16854 case REALPART_EXPR:
16855 case IMAGPART_EXPR:
16856 case PAREN_EXPR:
16857 {
16858 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16859 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16860 r = build1 (code, type, op0);
16861 if (code == ALIGNOF_EXPR)
16862 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16863 return r;
16864 }
16865
16866 case COMPONENT_REF:
16867 {
16868 tree object;
16869 tree name;
16870
16871 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16872 name = TREE_OPERAND (t, 1);
16873 if (TREE_CODE (name) == BIT_NOT_EXPR)
16874 {
16875 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16876 complain, in_decl);
16877 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16878 }
16879 else if (TREE_CODE (name) == SCOPE_REF
16880 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16881 {
16882 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16883 complain, in_decl);
16884 name = TREE_OPERAND (name, 1);
16885 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16886 complain, in_decl);
16887 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16888 name = build_qualified_name (/*type=*/NULL_TREE,
16889 base, name,
16890 /*template_p=*/false);
16891 }
16892 else if (BASELINK_P (name))
16893 name = tsubst_baselink (name,
16894 non_reference (TREE_TYPE (object)),
16895 args, complain,
16896 in_decl);
16897 else
16898 name = tsubst_copy (name, args, complain, in_decl);
16899 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16900 }
16901
16902 case PLUS_EXPR:
16903 case MINUS_EXPR:
16904 case MULT_EXPR:
16905 case TRUNC_DIV_EXPR:
16906 case CEIL_DIV_EXPR:
16907 case FLOOR_DIV_EXPR:
16908 case ROUND_DIV_EXPR:
16909 case EXACT_DIV_EXPR:
16910 case BIT_AND_EXPR:
16911 case BIT_IOR_EXPR:
16912 case BIT_XOR_EXPR:
16913 case TRUNC_MOD_EXPR:
16914 case FLOOR_MOD_EXPR:
16915 case TRUTH_ANDIF_EXPR:
16916 case TRUTH_ORIF_EXPR:
16917 case TRUTH_AND_EXPR:
16918 case TRUTH_OR_EXPR:
16919 case RSHIFT_EXPR:
16920 case LSHIFT_EXPR:
16921 case EQ_EXPR:
16922 case NE_EXPR:
16923 case MAX_EXPR:
16924 case MIN_EXPR:
16925 case LE_EXPR:
16926 case GE_EXPR:
16927 case LT_EXPR:
16928 case GT_EXPR:
16929 case COMPOUND_EXPR:
16930 case DOTSTAR_EXPR:
16931 case MEMBER_REF:
16932 case PREDECREMENT_EXPR:
16933 case PREINCREMENT_EXPR:
16934 case POSTDECREMENT_EXPR:
16935 case POSTINCREMENT_EXPR:
16936 {
16937 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16938 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16939 return build_nt (code, op0, op1);
16940 }
16941
16942 case SCOPE_REF:
16943 {
16944 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16945 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16946 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16947 QUALIFIED_NAME_IS_TEMPLATE (t));
16948 }
16949
16950 case ARRAY_REF:
16951 {
16952 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16953 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16954 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16955 }
16956
16957 case CALL_EXPR:
16958 {
16959 int n = VL_EXP_OPERAND_LENGTH (t);
16960 tree result = build_vl_exp (CALL_EXPR, n);
16961 int i;
16962 for (i = 0; i < n; i++)
16963 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16964 complain, in_decl);
16965 return result;
16966 }
16967
16968 case COND_EXPR:
16969 case MODOP_EXPR:
16970 case PSEUDO_DTOR_EXPR:
16971 case VEC_PERM_EXPR:
16972 {
16973 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16974 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16975 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16976 r = build_nt (code, op0, op1, op2);
16977 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16978 return r;
16979 }
16980
16981 case NEW_EXPR:
16982 {
16983 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16984 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16985 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16986 r = build_nt (code, op0, op1, op2);
16987 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16988 return r;
16989 }
16990
16991 case DELETE_EXPR:
16992 {
16993 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16994 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16995 r = build_nt (code, op0, op1);
16996 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16997 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16998 return r;
16999 }
17000
17001 case TEMPLATE_ID_EXPR:
17002 {
17003 /* Substituted template arguments */
17004 tree fn = TREE_OPERAND (t, 0);
17005 tree targs = TREE_OPERAND (t, 1);
17006
17007 fn = tsubst_copy (fn, args, complain, in_decl);
17008 if (targs)
17009 targs = tsubst_template_args (targs, args, complain, in_decl);
17010
17011 return lookup_template_function (fn, targs);
17012 }
17013
17014 case TREE_LIST:
17015 {
17016 tree purpose, value, chain;
17017
17018 if (t == void_list_node)
17019 return t;
17020
17021 purpose = TREE_PURPOSE (t);
17022 if (purpose)
17023 purpose = tsubst_copy (purpose, args, complain, in_decl);
17024 value = TREE_VALUE (t);
17025 if (value)
17026 value = tsubst_copy (value, args, complain, in_decl);
17027 chain = TREE_CHAIN (t);
17028 if (chain && chain != void_type_node)
17029 chain = tsubst_copy (chain, args, complain, in_decl);
17030 if (purpose == TREE_PURPOSE (t)
17031 && value == TREE_VALUE (t)
17032 && chain == TREE_CHAIN (t))
17033 return t;
17034 return tree_cons (purpose, value, chain);
17035 }
17036
17037 case RECORD_TYPE:
17038 case UNION_TYPE:
17039 case ENUMERAL_TYPE:
17040 case INTEGER_TYPE:
17041 case TEMPLATE_TYPE_PARM:
17042 case TEMPLATE_TEMPLATE_PARM:
17043 case BOUND_TEMPLATE_TEMPLATE_PARM:
17044 case TEMPLATE_PARM_INDEX:
17045 case POINTER_TYPE:
17046 case REFERENCE_TYPE:
17047 case OFFSET_TYPE:
17048 case FUNCTION_TYPE:
17049 case METHOD_TYPE:
17050 case ARRAY_TYPE:
17051 case TYPENAME_TYPE:
17052 case UNBOUND_CLASS_TEMPLATE:
17053 case TYPEOF_TYPE:
17054 case DECLTYPE_TYPE:
17055 case TYPE_DECL:
17056 return tsubst (t, args, complain, in_decl);
17057
17058 case USING_DECL:
17059 t = DECL_NAME (t);
17060 /* Fall through. */
17061 case IDENTIFIER_NODE:
17062 if (IDENTIFIER_CONV_OP_P (t))
17063 {
17064 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17065 return make_conv_op_name (new_type);
17066 }
17067 else
17068 return t;
17069
17070 case CONSTRUCTOR:
17071 /* This is handled by tsubst_copy_and_build. */
17072 gcc_unreachable ();
17073
17074 case VA_ARG_EXPR:
17075 {
17076 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17077 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17078 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17079 }
17080
17081 case CLEANUP_POINT_EXPR:
17082 /* We shouldn't have built any of these during initial template
17083 generation. Instead, they should be built during instantiation
17084 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17085 gcc_unreachable ();
17086
17087 case OFFSET_REF:
17088 {
17089 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17090 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17091 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17092 r = build2 (code, type, op0, op1);
17093 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17094 if (!mark_used (TREE_OPERAND (r, 1), complain)
17095 && !(complain & tf_error))
17096 return error_mark_node;
17097 return r;
17098 }
17099
17100 case EXPR_PACK_EXPANSION:
17101 error ("invalid use of pack expansion expression");
17102 return error_mark_node;
17103
17104 case NONTYPE_ARGUMENT_PACK:
17105 error ("use %<...%> to expand argument pack");
17106 return error_mark_node;
17107
17108 case VOID_CST:
17109 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17110 return t;
17111
17112 case INTEGER_CST:
17113 case REAL_CST:
17114 case COMPLEX_CST:
17115 {
17116 /* Instantiate any typedefs in the type. */
17117 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17118 r = fold_convert (type, t);
17119 gcc_assert (TREE_CODE (r) == code);
17120 return r;
17121 }
17122
17123 case STRING_CST:
17124 {
17125 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17126 r = t;
17127 if (type != TREE_TYPE (t))
17128 {
17129 r = copy_node (t);
17130 TREE_TYPE (r) = type;
17131 }
17132 return r;
17133 }
17134
17135 case PTRMEM_CST:
17136 /* These can sometimes show up in a partial instantiation, but never
17137 involve template parms. */
17138 gcc_assert (!uses_template_parms (t));
17139 return t;
17140
17141 case UNARY_LEFT_FOLD_EXPR:
17142 return tsubst_unary_left_fold (t, args, complain, in_decl);
17143 case UNARY_RIGHT_FOLD_EXPR:
17144 return tsubst_unary_right_fold (t, args, complain, in_decl);
17145 case BINARY_LEFT_FOLD_EXPR:
17146 return tsubst_binary_left_fold (t, args, complain, in_decl);
17147 case BINARY_RIGHT_FOLD_EXPR:
17148 return tsubst_binary_right_fold (t, args, complain, in_decl);
17149 case PREDICT_EXPR:
17150 return t;
17151
17152 case DEBUG_BEGIN_STMT:
17153 /* ??? There's no point in copying it for now, but maybe some
17154 day it will contain more information, such as a pointer back
17155 to the containing function, inlined copy or so. */
17156 return t;
17157
17158 case CO_AWAIT_EXPR:
17159 return tsubst_expr (t, args, complain, in_decl,
17160 /*integral_constant_expression_p=*/false);
17161 break;
17162
17163 default:
17164 /* We shouldn't get here, but keep going if !flag_checking. */
17165 if (flag_checking)
17166 gcc_unreachable ();
17167 return t;
17168 }
17169 }
17170
17171 /* Helper function for tsubst_omp_clauses, used for instantiation of
17172 OMP_CLAUSE_DECL of clauses. */
17173
17174 static tree
tsubst_omp_clause_decl(tree decl,tree args,tsubst_flags_t complain,tree in_decl,tree * iterator_cache)17175 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17176 tree in_decl, tree *iterator_cache)
17177 {
17178 if (decl == NULL_TREE)
17179 return NULL_TREE;
17180
17181 /* Handle OpenMP iterators. */
17182 if (TREE_CODE (decl) == TREE_LIST
17183 && TREE_PURPOSE (decl)
17184 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17185 {
17186 tree ret;
17187 if (iterator_cache[0] == TREE_PURPOSE (decl))
17188 ret = iterator_cache[1];
17189 else
17190 {
17191 tree *tp = &ret;
17192 begin_scope (sk_omp, NULL);
17193 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17194 {
17195 *tp = copy_node (it);
17196 TREE_VEC_ELT (*tp, 0)
17197 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17198 TREE_VEC_ELT (*tp, 1)
17199 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17200 /*integral_constant_expression_p=*/false);
17201 TREE_VEC_ELT (*tp, 2)
17202 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17203 /*integral_constant_expression_p=*/false);
17204 TREE_VEC_ELT (*tp, 3)
17205 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17206 /*integral_constant_expression_p=*/false);
17207 TREE_CHAIN (*tp) = NULL_TREE;
17208 tp = &TREE_CHAIN (*tp);
17209 }
17210 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17211 iterator_cache[0] = TREE_PURPOSE (decl);
17212 iterator_cache[1] = ret;
17213 }
17214 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17215 args, complain,
17216 in_decl, NULL));
17217 }
17218
17219 /* Handle an OpenMP array section represented as a TREE_LIST (or
17220 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17221 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17222 TREE_LIST. We can handle it exactly the same as an array section
17223 (purpose, value, and a chain), even though the nomenclature
17224 (low_bound, length, etc) is different. */
17225 if (TREE_CODE (decl) == TREE_LIST)
17226 {
17227 tree low_bound
17228 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17229 /*integral_constant_expression_p=*/false);
17230 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17231 /*integral_constant_expression_p=*/false);
17232 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17233 in_decl, NULL);
17234 if (TREE_PURPOSE (decl) == low_bound
17235 && TREE_VALUE (decl) == length
17236 && TREE_CHAIN (decl) == chain)
17237 return decl;
17238 tree ret = tree_cons (low_bound, length, chain);
17239 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17240 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17241 return ret;
17242 }
17243 tree ret = tsubst_expr (decl, args, complain, in_decl,
17244 /*integral_constant_expression_p=*/false);
17245 /* Undo convert_from_reference tsubst_expr could have called. */
17246 if (decl
17247 && REFERENCE_REF_P (ret)
17248 && !REFERENCE_REF_P (decl))
17249 ret = TREE_OPERAND (ret, 0);
17250 return ret;
17251 }
17252
17253 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17254
17255 static tree
tsubst_omp_clauses(tree clauses,enum c_omp_region_type ort,tree args,tsubst_flags_t complain,tree in_decl)17256 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17257 tree args, tsubst_flags_t complain, tree in_decl)
17258 {
17259 tree new_clauses = NULL_TREE, nc, oc;
17260 tree linear_no_step = NULL_TREE;
17261 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17262
17263 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17264 {
17265 nc = copy_node (oc);
17266 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17267 new_clauses = nc;
17268
17269 switch (OMP_CLAUSE_CODE (nc))
17270 {
17271 case OMP_CLAUSE_LASTPRIVATE:
17272 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17273 {
17274 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17275 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17276 in_decl, /*integral_constant_expression_p=*/false);
17277 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17278 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17279 }
17280 /* FALLTHRU */
17281 case OMP_CLAUSE_PRIVATE:
17282 case OMP_CLAUSE_SHARED:
17283 case OMP_CLAUSE_FIRSTPRIVATE:
17284 case OMP_CLAUSE_COPYIN:
17285 case OMP_CLAUSE_COPYPRIVATE:
17286 case OMP_CLAUSE_UNIFORM:
17287 case OMP_CLAUSE_DEPEND:
17288 case OMP_CLAUSE_FROM:
17289 case OMP_CLAUSE_TO:
17290 case OMP_CLAUSE_MAP:
17291 case OMP_CLAUSE__CACHE_:
17292 case OMP_CLAUSE_NONTEMPORAL:
17293 case OMP_CLAUSE_USE_DEVICE_PTR:
17294 case OMP_CLAUSE_USE_DEVICE_ADDR:
17295 case OMP_CLAUSE_IS_DEVICE_PTR:
17296 case OMP_CLAUSE_INCLUSIVE:
17297 case OMP_CLAUSE_EXCLUSIVE:
17298 OMP_CLAUSE_DECL (nc)
17299 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17300 in_decl, iterator_cache);
17301 break;
17302 case OMP_CLAUSE_TILE:
17303 case OMP_CLAUSE_IF:
17304 case OMP_CLAUSE_NUM_THREADS:
17305 case OMP_CLAUSE_SCHEDULE:
17306 case OMP_CLAUSE_COLLAPSE:
17307 case OMP_CLAUSE_FINAL:
17308 case OMP_CLAUSE_DEVICE:
17309 case OMP_CLAUSE_DIST_SCHEDULE:
17310 case OMP_CLAUSE_NUM_TEAMS:
17311 case OMP_CLAUSE_THREAD_LIMIT:
17312 case OMP_CLAUSE_SAFELEN:
17313 case OMP_CLAUSE_SIMDLEN:
17314 case OMP_CLAUSE_NUM_TASKS:
17315 case OMP_CLAUSE_GRAINSIZE:
17316 case OMP_CLAUSE_PRIORITY:
17317 case OMP_CLAUSE_ORDERED:
17318 case OMP_CLAUSE_HINT:
17319 case OMP_CLAUSE_NUM_GANGS:
17320 case OMP_CLAUSE_NUM_WORKERS:
17321 case OMP_CLAUSE_VECTOR_LENGTH:
17322 case OMP_CLAUSE_WORKER:
17323 case OMP_CLAUSE_VECTOR:
17324 case OMP_CLAUSE_ASYNC:
17325 case OMP_CLAUSE_WAIT:
17326 OMP_CLAUSE_OPERAND (nc, 0)
17327 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17328 in_decl, /*integral_constant_expression_p=*/false);
17329 break;
17330 case OMP_CLAUSE_REDUCTION:
17331 case OMP_CLAUSE_IN_REDUCTION:
17332 case OMP_CLAUSE_TASK_REDUCTION:
17333 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17334 {
17335 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17336 if (TREE_CODE (placeholder) == SCOPE_REF)
17337 {
17338 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17339 complain, in_decl);
17340 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17341 = build_qualified_name (NULL_TREE, scope,
17342 TREE_OPERAND (placeholder, 1),
17343 false);
17344 }
17345 else
17346 gcc_assert (identifier_p (placeholder));
17347 }
17348 OMP_CLAUSE_DECL (nc)
17349 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17350 in_decl, NULL);
17351 break;
17352 case OMP_CLAUSE_GANG:
17353 case OMP_CLAUSE_ALIGNED:
17354 OMP_CLAUSE_DECL (nc)
17355 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17356 in_decl, NULL);
17357 OMP_CLAUSE_OPERAND (nc, 1)
17358 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17359 in_decl, /*integral_constant_expression_p=*/false);
17360 break;
17361 case OMP_CLAUSE_LINEAR:
17362 OMP_CLAUSE_DECL (nc)
17363 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17364 in_decl, NULL);
17365 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17366 {
17367 gcc_assert (!linear_no_step);
17368 linear_no_step = nc;
17369 }
17370 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17371 OMP_CLAUSE_LINEAR_STEP (nc)
17372 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17373 complain, in_decl, NULL);
17374 else
17375 OMP_CLAUSE_LINEAR_STEP (nc)
17376 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17377 in_decl,
17378 /*integral_constant_expression_p=*/false);
17379 break;
17380 case OMP_CLAUSE_NOWAIT:
17381 case OMP_CLAUSE_DEFAULT:
17382 case OMP_CLAUSE_UNTIED:
17383 case OMP_CLAUSE_MERGEABLE:
17384 case OMP_CLAUSE_INBRANCH:
17385 case OMP_CLAUSE_NOTINBRANCH:
17386 case OMP_CLAUSE_PROC_BIND:
17387 case OMP_CLAUSE_FOR:
17388 case OMP_CLAUSE_PARALLEL:
17389 case OMP_CLAUSE_SECTIONS:
17390 case OMP_CLAUSE_TASKGROUP:
17391 case OMP_CLAUSE_NOGROUP:
17392 case OMP_CLAUSE_THREADS:
17393 case OMP_CLAUSE_SIMD:
17394 case OMP_CLAUSE_DEFAULTMAP:
17395 case OMP_CLAUSE_ORDER:
17396 case OMP_CLAUSE_BIND:
17397 case OMP_CLAUSE_INDEPENDENT:
17398 case OMP_CLAUSE_AUTO:
17399 case OMP_CLAUSE_SEQ:
17400 case OMP_CLAUSE_IF_PRESENT:
17401 case OMP_CLAUSE_FINALIZE:
17402 break;
17403 default:
17404 gcc_unreachable ();
17405 }
17406 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17407 switch (OMP_CLAUSE_CODE (nc))
17408 {
17409 case OMP_CLAUSE_SHARED:
17410 case OMP_CLAUSE_PRIVATE:
17411 case OMP_CLAUSE_FIRSTPRIVATE:
17412 case OMP_CLAUSE_LASTPRIVATE:
17413 case OMP_CLAUSE_COPYPRIVATE:
17414 case OMP_CLAUSE_LINEAR:
17415 case OMP_CLAUSE_REDUCTION:
17416 case OMP_CLAUSE_IN_REDUCTION:
17417 case OMP_CLAUSE_TASK_REDUCTION:
17418 case OMP_CLAUSE_USE_DEVICE_PTR:
17419 case OMP_CLAUSE_USE_DEVICE_ADDR:
17420 case OMP_CLAUSE_IS_DEVICE_PTR:
17421 case OMP_CLAUSE_INCLUSIVE:
17422 case OMP_CLAUSE_EXCLUSIVE:
17423 /* tsubst_expr on SCOPE_REF results in returning
17424 finish_non_static_data_member result. Undo that here. */
17425 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17426 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17427 == IDENTIFIER_NODE))
17428 {
17429 tree t = OMP_CLAUSE_DECL (nc);
17430 tree v = t;
17431 while (v)
17432 switch (TREE_CODE (v))
17433 {
17434 case COMPONENT_REF:
17435 case MEM_REF:
17436 case INDIRECT_REF:
17437 CASE_CONVERT:
17438 case POINTER_PLUS_EXPR:
17439 v = TREE_OPERAND (v, 0);
17440 continue;
17441 case PARM_DECL:
17442 if (DECL_CONTEXT (v) == current_function_decl
17443 && DECL_ARTIFICIAL (v)
17444 && DECL_NAME (v) == this_identifier)
17445 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17446 /* FALLTHRU */
17447 default:
17448 v = NULL_TREE;
17449 break;
17450 }
17451 }
17452 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17453 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17454 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17455 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17456 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17457 {
17458 tree decl = OMP_CLAUSE_DECL (nc);
17459 if (VAR_P (decl))
17460 {
17461 retrofit_lang_decl (decl);
17462 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17463 }
17464 }
17465 break;
17466 default:
17467 break;
17468 }
17469 }
17470
17471 new_clauses = nreverse (new_clauses);
17472 if (ort != C_ORT_OMP_DECLARE_SIMD)
17473 {
17474 new_clauses = finish_omp_clauses (new_clauses, ort);
17475 if (linear_no_step)
17476 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17477 if (nc == linear_no_step)
17478 {
17479 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17480 break;
17481 }
17482 }
17483 return new_clauses;
17484 }
17485
17486 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17487
17488 static tree
tsubst_copy_asm_operands(tree t,tree args,tsubst_flags_t complain,tree in_decl)17489 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17490 tree in_decl)
17491 {
17492 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17493
17494 tree purpose, value, chain;
17495
17496 if (t == NULL)
17497 return t;
17498
17499 if (TREE_CODE (t) != TREE_LIST)
17500 return tsubst_copy_and_build (t, args, complain, in_decl,
17501 /*function_p=*/false,
17502 /*integral_constant_expression_p=*/false);
17503
17504 if (t == void_list_node)
17505 return t;
17506
17507 purpose = TREE_PURPOSE (t);
17508 if (purpose)
17509 purpose = RECUR (purpose);
17510 value = TREE_VALUE (t);
17511 if (value)
17512 {
17513 if (TREE_CODE (value) != LABEL_DECL)
17514 value = RECUR (value);
17515 else
17516 {
17517 value = lookup_label (DECL_NAME (value));
17518 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17519 TREE_USED (value) = 1;
17520 }
17521 }
17522 chain = TREE_CHAIN (t);
17523 if (chain && chain != void_type_node)
17524 chain = RECUR (chain);
17525 return tree_cons (purpose, value, chain);
17526 #undef RECUR
17527 }
17528
17529 /* Used to temporarily communicate the list of #pragma omp parallel
17530 clauses to #pragma omp for instantiation if they are combined
17531 together. */
17532
17533 static tree *omp_parallel_combined_clauses;
17534
17535 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17536 tree *, unsigned int *);
17537
17538 /* Substitute one OMP_FOR iterator. */
17539
17540 static bool
tsubst_omp_for_iterator(tree t,int i,tree declv,tree & orig_declv,tree initv,tree condv,tree incrv,tree * clauses,tree args,tsubst_flags_t complain,tree in_decl,bool integral_constant_expression_p)17541 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17542 tree initv, tree condv, tree incrv, tree *clauses,
17543 tree args, tsubst_flags_t complain, tree in_decl,
17544 bool integral_constant_expression_p)
17545 {
17546 #define RECUR(NODE) \
17547 tsubst_expr ((NODE), args, complain, in_decl, \
17548 integral_constant_expression_p)
17549 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17550 bool ret = false;
17551
17552 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17553 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17554
17555 decl = TREE_OPERAND (init, 0);
17556 init = TREE_OPERAND (init, 1);
17557 tree decl_expr = NULL_TREE;
17558 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17559 if (range_for)
17560 {
17561 bool decomp = false;
17562 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17563 {
17564 tree v = DECL_VALUE_EXPR (decl);
17565 if (TREE_CODE (v) == ARRAY_REF
17566 && VAR_P (TREE_OPERAND (v, 0))
17567 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17568 {
17569 tree decomp_first = NULL_TREE;
17570 unsigned decomp_cnt = 0;
17571 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17572 maybe_push_decl (d);
17573 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17574 in_decl, &decomp_first, &decomp_cnt);
17575 decomp = true;
17576 if (d == error_mark_node)
17577 decl = error_mark_node;
17578 else
17579 for (unsigned int i = 0; i < decomp_cnt; i++)
17580 {
17581 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17582 {
17583 tree v = build_nt (ARRAY_REF, d,
17584 size_int (decomp_cnt - i - 1),
17585 NULL_TREE, NULL_TREE);
17586 SET_DECL_VALUE_EXPR (decomp_first, v);
17587 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17588 }
17589 fit_decomposition_lang_decl (decomp_first, d);
17590 decomp_first = DECL_CHAIN (decomp_first);
17591 }
17592 }
17593 }
17594 decl = tsubst_decl (decl, args, complain);
17595 if (!decomp)
17596 maybe_push_decl (decl);
17597 }
17598 else if (init && TREE_CODE (init) == DECL_EXPR)
17599 {
17600 /* We need to jump through some hoops to handle declarations in the
17601 init-statement, since we might need to handle auto deduction,
17602 but we need to keep control of initialization. */
17603 decl_expr = init;
17604 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17605 decl = tsubst_decl (decl, args, complain);
17606 }
17607 else
17608 {
17609 if (TREE_CODE (decl) == SCOPE_REF)
17610 {
17611 decl = RECUR (decl);
17612 if (TREE_CODE (decl) == COMPONENT_REF)
17613 {
17614 tree v = decl;
17615 while (v)
17616 switch (TREE_CODE (v))
17617 {
17618 case COMPONENT_REF:
17619 case MEM_REF:
17620 case INDIRECT_REF:
17621 CASE_CONVERT:
17622 case POINTER_PLUS_EXPR:
17623 v = TREE_OPERAND (v, 0);
17624 continue;
17625 case PARM_DECL:
17626 if (DECL_CONTEXT (v) == current_function_decl
17627 && DECL_ARTIFICIAL (v)
17628 && DECL_NAME (v) == this_identifier)
17629 {
17630 decl = TREE_OPERAND (decl, 1);
17631 decl = omp_privatize_field (decl, false);
17632 }
17633 /* FALLTHRU */
17634 default:
17635 v = NULL_TREE;
17636 break;
17637 }
17638 }
17639 }
17640 else
17641 decl = RECUR (decl);
17642 }
17643 init = RECUR (init);
17644
17645 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17646 {
17647 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17648 if (TREE_CODE (o) == TREE_LIST)
17649 TREE_VEC_ELT (orig_declv, i)
17650 = tree_cons (RECUR (TREE_PURPOSE (o)),
17651 RECUR (TREE_VALUE (o)),
17652 NULL_TREE);
17653 else
17654 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17655 }
17656
17657 if (range_for)
17658 {
17659 tree this_pre_body = NULL_TREE;
17660 tree orig_init = NULL_TREE;
17661 tree orig_decl = NULL_TREE;
17662 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17663 orig_init, cond, incr);
17664 if (orig_decl)
17665 {
17666 if (orig_declv == NULL_TREE)
17667 orig_declv = copy_node (declv);
17668 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17669 ret = true;
17670 }
17671 else if (orig_declv)
17672 TREE_VEC_ELT (orig_declv, i) = decl;
17673 }
17674
17675 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17676 if (!range_for && auto_node && init)
17677 TREE_TYPE (decl)
17678 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17679
17680 gcc_assert (!type_dependent_expression_p (decl));
17681
17682 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17683 {
17684 if (decl_expr)
17685 {
17686 /* Declare the variable, but don't let that initialize it. */
17687 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17688 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17689 RECUR (decl_expr);
17690 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17691 }
17692
17693 if (!range_for)
17694 {
17695 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17696 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17697 if (TREE_CODE (incr) == MODIFY_EXPR)
17698 {
17699 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17700 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17701 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17702 NOP_EXPR, rhs, complain);
17703 }
17704 else
17705 incr = RECUR (incr);
17706 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17707 TREE_VEC_ELT (orig_declv, i) = decl;
17708 }
17709 TREE_VEC_ELT (declv, i) = decl;
17710 TREE_VEC_ELT (initv, i) = init;
17711 TREE_VEC_ELT (condv, i) = cond;
17712 TREE_VEC_ELT (incrv, i) = incr;
17713 return ret;
17714 }
17715
17716 if (decl_expr)
17717 {
17718 /* Declare and initialize the variable. */
17719 RECUR (decl_expr);
17720 init = NULL_TREE;
17721 }
17722 else if (init)
17723 {
17724 tree *pc;
17725 int j;
17726 for (j = ((omp_parallel_combined_clauses == NULL
17727 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17728 {
17729 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17730 {
17731 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17732 && OMP_CLAUSE_DECL (*pc) == decl)
17733 break;
17734 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17735 && OMP_CLAUSE_DECL (*pc) == decl)
17736 {
17737 if (j)
17738 break;
17739 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17740 tree c = *pc;
17741 *pc = OMP_CLAUSE_CHAIN (c);
17742 OMP_CLAUSE_CHAIN (c) = *clauses;
17743 *clauses = c;
17744 }
17745 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17746 && OMP_CLAUSE_DECL (*pc) == decl)
17747 {
17748 error ("iteration variable %qD should not be firstprivate",
17749 decl);
17750 *pc = OMP_CLAUSE_CHAIN (*pc);
17751 }
17752 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17753 && OMP_CLAUSE_DECL (*pc) == decl)
17754 {
17755 error ("iteration variable %qD should not be reduction",
17756 decl);
17757 *pc = OMP_CLAUSE_CHAIN (*pc);
17758 }
17759 else
17760 pc = &OMP_CLAUSE_CHAIN (*pc);
17761 }
17762 if (*pc)
17763 break;
17764 }
17765 if (*pc == NULL_TREE)
17766 {
17767 tree c = build_omp_clause (input_location,
17768 TREE_CODE (t) == OMP_LOOP
17769 ? OMP_CLAUSE_LASTPRIVATE
17770 : OMP_CLAUSE_PRIVATE);
17771 OMP_CLAUSE_DECL (c) = decl;
17772 c = finish_omp_clauses (c, C_ORT_OMP);
17773 if (c)
17774 {
17775 OMP_CLAUSE_CHAIN (c) = *clauses;
17776 *clauses = c;
17777 }
17778 }
17779 }
17780 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17781 if (COMPARISON_CLASS_P (cond))
17782 {
17783 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17784 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17785 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17786 }
17787 else
17788 cond = RECUR (cond);
17789 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17790 switch (TREE_CODE (incr))
17791 {
17792 case PREINCREMENT_EXPR:
17793 case PREDECREMENT_EXPR:
17794 case POSTINCREMENT_EXPR:
17795 case POSTDECREMENT_EXPR:
17796 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17797 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17798 break;
17799 case MODIFY_EXPR:
17800 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17801 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17802 {
17803 tree rhs = TREE_OPERAND (incr, 1);
17804 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17805 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17806 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17807 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17808 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17809 rhs0, rhs1));
17810 }
17811 else
17812 incr = RECUR (incr);
17813 break;
17814 case MODOP_EXPR:
17815 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17816 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17817 {
17818 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17819 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17820 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17821 TREE_TYPE (decl), lhs,
17822 RECUR (TREE_OPERAND (incr, 2))));
17823 }
17824 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17825 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17826 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17827 {
17828 tree rhs = TREE_OPERAND (incr, 2);
17829 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17830 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17831 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17832 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17833 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17834 rhs0, rhs1));
17835 }
17836 else
17837 incr = RECUR (incr);
17838 break;
17839 default:
17840 incr = RECUR (incr);
17841 break;
17842 }
17843
17844 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17845 TREE_VEC_ELT (orig_declv, i) = decl;
17846 TREE_VEC_ELT (declv, i) = decl;
17847 TREE_VEC_ELT (initv, i) = init;
17848 TREE_VEC_ELT (condv, i) = cond;
17849 TREE_VEC_ELT (incrv, i) = incr;
17850 return false;
17851 #undef RECUR
17852 }
17853
17854 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17855 of OMP_TARGET's body. */
17856
17857 static tree
tsubst_find_omp_teams(tree * tp,int * walk_subtrees,void *)17858 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17859 {
17860 *walk_subtrees = 0;
17861 switch (TREE_CODE (*tp))
17862 {
17863 case OMP_TEAMS:
17864 return *tp;
17865 case BIND_EXPR:
17866 case STATEMENT_LIST:
17867 *walk_subtrees = 1;
17868 break;
17869 default:
17870 break;
17871 }
17872 return NULL_TREE;
17873 }
17874
17875 /* Helper function for tsubst_expr. For decomposition declaration
17876 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17877 also the corresponding decls representing the identifiers
17878 of the decomposition declaration. Return DECL if successful
17879 or error_mark_node otherwise, set *FIRST to the first decl
17880 in the list chained through DECL_CHAIN and *CNT to the number
17881 of such decls. */
17882
17883 static tree
tsubst_decomp_names(tree decl,tree pattern_decl,tree args,tsubst_flags_t complain,tree in_decl,tree * first,unsigned int * cnt)17884 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17885 tsubst_flags_t complain, tree in_decl, tree *first,
17886 unsigned int *cnt)
17887 {
17888 tree decl2, decl3, prev = decl;
17889 *cnt = 0;
17890 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17891 for (decl2 = DECL_CHAIN (pattern_decl);
17892 decl2
17893 && VAR_P (decl2)
17894 && DECL_DECOMPOSITION_P (decl2)
17895 && DECL_NAME (decl2);
17896 decl2 = DECL_CHAIN (decl2))
17897 {
17898 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17899 {
17900 gcc_assert (errorcount);
17901 return error_mark_node;
17902 }
17903 (*cnt)++;
17904 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17905 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17906 tree v = DECL_VALUE_EXPR (decl2);
17907 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17908 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17909 decl3 = tsubst (decl2, args, complain, in_decl);
17910 SET_DECL_VALUE_EXPR (decl2, v);
17911 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17912 if (VAR_P (decl3))
17913 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17914 else
17915 {
17916 gcc_assert (errorcount);
17917 decl = error_mark_node;
17918 continue;
17919 }
17920 maybe_push_decl (decl3);
17921 if (error_operand_p (decl3))
17922 decl = error_mark_node;
17923 else if (decl != error_mark_node
17924 && DECL_CHAIN (decl3) != prev
17925 && decl != prev)
17926 {
17927 gcc_assert (errorcount);
17928 decl = error_mark_node;
17929 }
17930 else
17931 prev = decl3;
17932 }
17933 *first = prev;
17934 return decl;
17935 }
17936
17937 /* Return the proper local_specialization for init-capture pack DECL. */
17938
17939 static tree
lookup_init_capture_pack(tree decl)17940 lookup_init_capture_pack (tree decl)
17941 {
17942 /* We handle normal pack captures by forwarding to the specialization of the
17943 captured parameter. We can't do that for pack init-captures; we need them
17944 to have their own local_specialization. We created the individual
17945 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17946 when we process the DECL_EXPR for the pack init-capture in the template.
17947 So, how do we find them? We don't know the capture proxy pack when
17948 building the individual resulting proxies, and we don't know the
17949 individual proxies when instantiating the pack. What we have in common is
17950 the FIELD_DECL.
17951
17952 So...when we instantiate the FIELD_DECL, we stick the result in
17953 local_specializations. Then at the DECL_EXPR we look up that result, see
17954 how many elements it has, synthesize the names, and look them up. */
17955
17956 tree cname = DECL_NAME (decl);
17957 tree val = DECL_VALUE_EXPR (decl);
17958 tree field = TREE_OPERAND (val, 1);
17959 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17960 tree fpack = retrieve_local_specialization (field);
17961 if (fpack == error_mark_node)
17962 return error_mark_node;
17963
17964 int len = 1;
17965 tree vec = NULL_TREE;
17966 tree r = NULL_TREE;
17967 if (TREE_CODE (fpack) == TREE_VEC)
17968 {
17969 len = TREE_VEC_LENGTH (fpack);
17970 vec = make_tree_vec (len);
17971 r = make_node (NONTYPE_ARGUMENT_PACK);
17972 SET_ARGUMENT_PACK_ARGS (r, vec);
17973 }
17974 for (int i = 0; i < len; ++i)
17975 {
17976 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17977 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17978 if (vec)
17979 TREE_VEC_ELT (vec, i) = elt;
17980 else
17981 r = elt;
17982 }
17983 return r;
17984 }
17985
17986 /* Like tsubst_copy for expressions, etc. but also does semantic
17987 processing. */
17988
17989 tree
tsubst_expr(tree t,tree args,tsubst_flags_t complain,tree in_decl,bool integral_constant_expression_p)17990 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17991 bool integral_constant_expression_p)
17992 {
17993 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17994 #define RECUR(NODE) \
17995 tsubst_expr ((NODE), args, complain, in_decl, \
17996 integral_constant_expression_p)
17997
17998 tree stmt, tmp;
17999 tree r;
18000 location_t loc;
18001
18002 if (t == NULL_TREE || t == error_mark_node)
18003 return t;
18004
18005 loc = input_location;
18006 if (location_t eloc = cp_expr_location (t))
18007 input_location = eloc;
18008 if (STATEMENT_CODE_P (TREE_CODE (t)))
18009 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18010
18011 switch (TREE_CODE (t))
18012 {
18013 case STATEMENT_LIST:
18014 {
18015 tree_stmt_iterator i;
18016 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
18017 RECUR (tsi_stmt (i));
18018 break;
18019 }
18020
18021 case CTOR_INITIALIZER:
18022 finish_mem_initializers (tsubst_initializer_list
18023 (TREE_OPERAND (t, 0), args));
18024 break;
18025
18026 case RETURN_EXPR:
18027 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18028 break;
18029
18030 case CO_RETURN_EXPR:
18031 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18032 break;
18033
18034 case CO_YIELD_EXPR:
18035 stmt = finish_co_yield_expr (input_location,
18036 RECUR (TREE_OPERAND (t, 0)));
18037 RETURN (stmt);
18038 break;
18039
18040 case CO_AWAIT_EXPR:
18041 stmt = finish_co_await_expr (input_location,
18042 RECUR (TREE_OPERAND (t, 0)));
18043 RETURN (stmt);
18044 break;
18045
18046 case EXPR_STMT:
18047 tmp = RECUR (EXPR_STMT_EXPR (t));
18048 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18049 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18050 else
18051 finish_expr_stmt (tmp);
18052 break;
18053
18054 case USING_STMT:
18055 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18056 break;
18057
18058 case DECL_EXPR:
18059 {
18060 tree decl, pattern_decl;
18061 tree init;
18062
18063 pattern_decl = decl = DECL_EXPR_DECL (t);
18064 if (TREE_CODE (decl) == LABEL_DECL)
18065 finish_label_decl (DECL_NAME (decl));
18066 else if (TREE_CODE (decl) == USING_DECL)
18067 {
18068 tree scope = USING_DECL_SCOPE (decl);
18069 tree name = DECL_NAME (decl);
18070
18071 scope = tsubst (scope, args, complain, in_decl);
18072 finish_nonmember_using_decl (scope, name);
18073 }
18074 else if (is_capture_proxy (decl)
18075 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18076 {
18077 /* We're in tsubst_lambda_expr, we've already inserted a new
18078 capture proxy, so look it up and register it. */
18079 tree inst;
18080 if (!DECL_PACK_P (decl))
18081 {
18082 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
18083 /*nonclass*/1, /*block_p=*/true,
18084 /*ns_only*/0, LOOKUP_HIDDEN);
18085 gcc_assert (inst != decl && is_capture_proxy (inst));
18086 }
18087 else if (is_normal_capture_proxy (decl))
18088 {
18089 inst = (retrieve_local_specialization
18090 (DECL_CAPTURED_VARIABLE (decl)));
18091 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18092 || DECL_PACK_P (inst));
18093 }
18094 else
18095 inst = lookup_init_capture_pack (decl);
18096
18097 register_local_specialization (inst, decl);
18098 break;
18099 }
18100 else if (DECL_PRETTY_FUNCTION_P (decl))
18101 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18102 DECL_NAME (decl),
18103 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18104 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18105 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18106 /* Don't copy the old closure; we'll create a new one in
18107 tsubst_lambda_expr. */
18108 break;
18109 else
18110 {
18111 init = DECL_INITIAL (decl);
18112 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
18113 for local variables, so save if DECL was declared constinit. */
18114 const bool constinit_p
18115 = (VAR_P (decl)
18116 && DECL_LANG_SPECIFIC (decl)
18117 && DECL_TEMPLATE_INFO (decl)
18118 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
18119 decl = tsubst (decl, args, complain, in_decl);
18120 if (decl != error_mark_node)
18121 {
18122 /* By marking the declaration as instantiated, we avoid
18123 trying to instantiate it. Since instantiate_decl can't
18124 handle local variables, and since we've already done
18125 all that needs to be done, that's the right thing to
18126 do. */
18127 if (VAR_P (decl))
18128 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18129 if (VAR_P (decl) && !DECL_NAME (decl)
18130 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18131 /* Anonymous aggregates are a special case. */
18132 finish_anon_union (decl);
18133 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18134 {
18135 DECL_CONTEXT (decl) = current_function_decl;
18136 if (DECL_NAME (decl) == this_identifier)
18137 {
18138 tree lam = DECL_CONTEXT (current_function_decl);
18139 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18140 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18141 }
18142 insert_capture_proxy (decl);
18143 }
18144 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18145 /* We already did a pushtag. */;
18146 else if (TREE_CODE (decl) == FUNCTION_DECL
18147 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18148 && DECL_FUNCTION_SCOPE_P (pattern_decl))
18149 {
18150 DECL_CONTEXT (decl) = NULL_TREE;
18151 pushdecl (decl);
18152 DECL_CONTEXT (decl) = current_function_decl;
18153 cp_check_omp_declare_reduction (decl);
18154 }
18155 else
18156 {
18157 bool const_init = false;
18158 unsigned int cnt = 0;
18159 tree first = NULL_TREE, ndecl = error_mark_node;
18160 tree asmspec_tree = NULL_TREE;
18161 maybe_push_decl (decl);
18162
18163 if (VAR_P (decl)
18164 && DECL_LANG_SPECIFIC (decl)
18165 && DECL_OMP_PRIVATIZED_MEMBER (decl))
18166 break;
18167
18168 if (VAR_P (decl)
18169 && DECL_DECOMPOSITION_P (decl)
18170 && TREE_TYPE (pattern_decl) != error_mark_node)
18171 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18172 complain, in_decl, &first,
18173 &cnt);
18174
18175 init = tsubst_init (init, decl, args, complain, in_decl);
18176
18177 if (VAR_P (decl))
18178 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18179 (pattern_decl));
18180
18181 if (ndecl != error_mark_node)
18182 cp_maybe_mangle_decomp (ndecl, first, cnt);
18183
18184 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
18185 {
18186 tree id = DECL_ASSEMBLER_NAME (pattern_decl);
18187 const char *asmspec = IDENTIFIER_POINTER (id);
18188 gcc_assert (asmspec[0] == '*');
18189 asmspec_tree
18190 = build_string (IDENTIFIER_LENGTH (id) - 1,
18191 asmspec + 1);
18192 TREE_TYPE (asmspec_tree) = char_array_type_node;
18193 }
18194
18195 cp_finish_decl (decl, init, const_init, asmspec_tree,
18196 constinit_p ? LOOKUP_CONSTINIT : 0);
18197
18198 if (ndecl != error_mark_node)
18199 cp_finish_decomp (ndecl, first, cnt);
18200 }
18201 }
18202 }
18203
18204 break;
18205 }
18206
18207 case FOR_STMT:
18208 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18209 RECUR (FOR_INIT_STMT (t));
18210 finish_init_stmt (stmt);
18211 tmp = RECUR (FOR_COND (t));
18212 finish_for_cond (tmp, stmt, false, 0);
18213 tmp = RECUR (FOR_EXPR (t));
18214 finish_for_expr (tmp, stmt);
18215 {
18216 bool prev = note_iteration_stmt_body_start ();
18217 RECUR (FOR_BODY (t));
18218 note_iteration_stmt_body_end (prev);
18219 }
18220 finish_for_stmt (stmt);
18221 break;
18222
18223 case RANGE_FOR_STMT:
18224 {
18225 /* Construct another range_for, if this is not a final
18226 substitution (for inside a generic lambda of a
18227 template). Otherwise convert to a regular for. */
18228 tree decl, expr;
18229 stmt = (processing_template_decl
18230 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18231 : begin_for_stmt (NULL_TREE, NULL_TREE));
18232 RECUR (RANGE_FOR_INIT_STMT (t));
18233 decl = RANGE_FOR_DECL (t);
18234 decl = tsubst (decl, args, complain, in_decl);
18235 maybe_push_decl (decl);
18236 expr = RECUR (RANGE_FOR_EXPR (t));
18237
18238 tree decomp_first = NULL_TREE;
18239 unsigned decomp_cnt = 0;
18240 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18241 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18242 complain, in_decl,
18243 &decomp_first, &decomp_cnt);
18244
18245 if (processing_template_decl)
18246 {
18247 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18248 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18249 finish_range_for_decl (stmt, decl, expr);
18250 if (decomp_first && decl != error_mark_node)
18251 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18252 }
18253 else
18254 {
18255 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18256 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18257 stmt = cp_convert_range_for (stmt, decl, expr,
18258 decomp_first, decomp_cnt,
18259 RANGE_FOR_IVDEP (t), unroll);
18260 }
18261
18262 bool prev = note_iteration_stmt_body_start ();
18263 RECUR (RANGE_FOR_BODY (t));
18264 note_iteration_stmt_body_end (prev);
18265 finish_for_stmt (stmt);
18266 }
18267 break;
18268
18269 case WHILE_STMT:
18270 stmt = begin_while_stmt ();
18271 tmp = RECUR (WHILE_COND (t));
18272 finish_while_stmt_cond (tmp, stmt, false, 0);
18273 {
18274 bool prev = note_iteration_stmt_body_start ();
18275 RECUR (WHILE_BODY (t));
18276 note_iteration_stmt_body_end (prev);
18277 }
18278 finish_while_stmt (stmt);
18279 break;
18280
18281 case DO_STMT:
18282 stmt = begin_do_stmt ();
18283 {
18284 bool prev = note_iteration_stmt_body_start ();
18285 RECUR (DO_BODY (t));
18286 note_iteration_stmt_body_end (prev);
18287 }
18288 finish_do_body (stmt);
18289 tmp = RECUR (DO_COND (t));
18290 finish_do_stmt (tmp, stmt, false, 0);
18291 break;
18292
18293 case IF_STMT:
18294 stmt = begin_if_stmt ();
18295 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18296 if (IF_STMT_CONSTEXPR_P (t))
18297 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18298 tmp = RECUR (IF_COND (t));
18299 tmp = finish_if_stmt_cond (tmp, stmt);
18300 if (IF_STMT_CONSTEXPR_P (t)
18301 && instantiation_dependent_expression_p (tmp))
18302 {
18303 /* We're partially instantiating a generic lambda, but the condition
18304 of the constexpr if is still dependent. Don't substitute into the
18305 branches now, just remember the template arguments. */
18306 do_poplevel (IF_SCOPE (stmt));
18307 IF_COND (stmt) = IF_COND (t);
18308 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18309 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18310 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18311 add_stmt (stmt);
18312 break;
18313 }
18314 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18315 /* Don't instantiate the THEN_CLAUSE. */;
18316 else
18317 {
18318 tree folded = fold_non_dependent_expr (tmp, complain);
18319 bool inhibit = integer_zerop (folded);
18320 if (inhibit)
18321 ++c_inhibit_evaluation_warnings;
18322 RECUR (THEN_CLAUSE (t));
18323 if (inhibit)
18324 --c_inhibit_evaluation_warnings;
18325 }
18326 finish_then_clause (stmt);
18327
18328 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18329 /* Don't instantiate the ELSE_CLAUSE. */;
18330 else if (ELSE_CLAUSE (t))
18331 {
18332 tree folded = fold_non_dependent_expr (tmp, complain);
18333 bool inhibit = integer_nonzerop (folded);
18334 begin_else_clause (stmt);
18335 if (inhibit)
18336 ++c_inhibit_evaluation_warnings;
18337 RECUR (ELSE_CLAUSE (t));
18338 if (inhibit)
18339 --c_inhibit_evaluation_warnings;
18340 finish_else_clause (stmt);
18341 }
18342
18343 finish_if_stmt (stmt);
18344 break;
18345
18346 case BIND_EXPR:
18347 if (BIND_EXPR_BODY_BLOCK (t))
18348 stmt = begin_function_body ();
18349 else
18350 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18351 ? BCS_TRY_BLOCK : 0);
18352
18353 RECUR (BIND_EXPR_BODY (t));
18354
18355 if (BIND_EXPR_BODY_BLOCK (t))
18356 finish_function_body (stmt);
18357 else
18358 finish_compound_stmt (stmt);
18359 break;
18360
18361 case BREAK_STMT:
18362 finish_break_stmt ();
18363 break;
18364
18365 case CONTINUE_STMT:
18366 finish_continue_stmt ();
18367 break;
18368
18369 case SWITCH_STMT:
18370 stmt = begin_switch_stmt ();
18371 tmp = RECUR (SWITCH_STMT_COND (t));
18372 finish_switch_cond (tmp, stmt);
18373 RECUR (SWITCH_STMT_BODY (t));
18374 finish_switch_stmt (stmt);
18375 break;
18376
18377 case CASE_LABEL_EXPR:
18378 {
18379 tree decl = CASE_LABEL (t);
18380 tree low = RECUR (CASE_LOW (t));
18381 tree high = RECUR (CASE_HIGH (t));
18382 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18383 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18384 {
18385 tree label = CASE_LABEL (l);
18386 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18387 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18388 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18389 }
18390 }
18391 break;
18392
18393 case LABEL_EXPR:
18394 {
18395 tree decl = LABEL_EXPR_LABEL (t);
18396 tree label;
18397
18398 label = finish_label_stmt (DECL_NAME (decl));
18399 if (TREE_CODE (label) == LABEL_DECL)
18400 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18401 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18402 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18403 }
18404 break;
18405
18406 case GOTO_EXPR:
18407 tmp = GOTO_DESTINATION (t);
18408 if (TREE_CODE (tmp) != LABEL_DECL)
18409 /* Computed goto's must be tsubst'd into. On the other hand,
18410 non-computed gotos must not be; the identifier in question
18411 will have no binding. */
18412 tmp = RECUR (tmp);
18413 else
18414 tmp = DECL_NAME (tmp);
18415 finish_goto_stmt (tmp);
18416 break;
18417
18418 case ASM_EXPR:
18419 {
18420 tree string = RECUR (ASM_STRING (t));
18421 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18422 complain, in_decl);
18423 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18424 complain, in_decl);
18425 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18426 complain, in_decl);
18427 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18428 complain, in_decl);
18429 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18430 outputs, inputs, clobbers, labels,
18431 ASM_INLINE_P (t));
18432 tree asm_expr = tmp;
18433 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18434 asm_expr = TREE_OPERAND (asm_expr, 0);
18435 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18436 }
18437 break;
18438
18439 case TRY_BLOCK:
18440 if (CLEANUP_P (t))
18441 {
18442 stmt = begin_try_block ();
18443 RECUR (TRY_STMTS (t));
18444 finish_cleanup_try_block (stmt);
18445 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18446 }
18447 else
18448 {
18449 tree compound_stmt = NULL_TREE;
18450
18451 if (FN_TRY_BLOCK_P (t))
18452 stmt = begin_function_try_block (&compound_stmt);
18453 else
18454 stmt = begin_try_block ();
18455
18456 RECUR (TRY_STMTS (t));
18457
18458 if (FN_TRY_BLOCK_P (t))
18459 finish_function_try_block (stmt);
18460 else
18461 finish_try_block (stmt);
18462
18463 RECUR (TRY_HANDLERS (t));
18464 if (FN_TRY_BLOCK_P (t))
18465 finish_function_handler_sequence (stmt, compound_stmt);
18466 else
18467 finish_handler_sequence (stmt);
18468 }
18469 break;
18470
18471 case HANDLER:
18472 {
18473 tree decl = HANDLER_PARMS (t);
18474
18475 if (decl)
18476 {
18477 decl = tsubst (decl, args, complain, in_decl);
18478 /* Prevent instantiate_decl from trying to instantiate
18479 this variable. We've already done all that needs to be
18480 done. */
18481 if (decl != error_mark_node)
18482 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18483 }
18484 stmt = begin_handler ();
18485 finish_handler_parms (decl, stmt);
18486 RECUR (HANDLER_BODY (t));
18487 finish_handler (stmt);
18488 }
18489 break;
18490
18491 case TAG_DEFN:
18492 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18493 if (CLASS_TYPE_P (tmp))
18494 {
18495 /* Local classes are not independent templates; they are
18496 instantiated along with their containing function. And this
18497 way we don't have to deal with pushing out of one local class
18498 to instantiate a member of another local class. */
18499 /* Closures are handled by the LAMBDA_EXPR. */
18500 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18501 complete_type (tmp);
18502 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18503 if ((VAR_P (fld)
18504 || (TREE_CODE (fld) == FUNCTION_DECL
18505 && !DECL_ARTIFICIAL (fld)))
18506 && DECL_TEMPLATE_INSTANTIATION (fld))
18507 instantiate_decl (fld, /*defer_ok=*/false,
18508 /*expl_inst_class=*/false);
18509 }
18510 break;
18511
18512 case STATIC_ASSERT:
18513 {
18514 tree condition;
18515
18516 ++c_inhibit_evaluation_warnings;
18517 condition =
18518 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18519 args,
18520 complain, in_decl,
18521 /*integral_constant_expression_p=*/true);
18522 --c_inhibit_evaluation_warnings;
18523
18524 finish_static_assert (condition,
18525 STATIC_ASSERT_MESSAGE (t),
18526 STATIC_ASSERT_SOURCE_LOCATION (t),
18527 /*member_p=*/false);
18528 }
18529 break;
18530
18531 case OACC_KERNELS:
18532 case OACC_PARALLEL:
18533 case OACC_SERIAL:
18534 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18535 in_decl);
18536 stmt = begin_omp_parallel ();
18537 RECUR (OMP_BODY (t));
18538 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18539 break;
18540
18541 case OMP_PARALLEL:
18542 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18543 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18544 complain, in_decl);
18545 if (OMP_PARALLEL_COMBINED (t))
18546 omp_parallel_combined_clauses = &tmp;
18547 stmt = begin_omp_parallel ();
18548 RECUR (OMP_PARALLEL_BODY (t));
18549 gcc_assert (omp_parallel_combined_clauses == NULL);
18550 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18551 = OMP_PARALLEL_COMBINED (t);
18552 pop_omp_privatization_clauses (r);
18553 break;
18554
18555 case OMP_TASK:
18556 if (OMP_TASK_BODY (t) == NULL_TREE)
18557 {
18558 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18559 complain, in_decl);
18560 t = copy_node (t);
18561 OMP_TASK_CLAUSES (t) = tmp;
18562 add_stmt (t);
18563 break;
18564 }
18565 r = push_omp_privatization_clauses (false);
18566 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18567 complain, in_decl);
18568 stmt = begin_omp_task ();
18569 RECUR (OMP_TASK_BODY (t));
18570 finish_omp_task (tmp, stmt);
18571 pop_omp_privatization_clauses (r);
18572 break;
18573
18574 case OMP_FOR:
18575 case OMP_LOOP:
18576 case OMP_SIMD:
18577 case OMP_DISTRIBUTE:
18578 case OMP_TASKLOOP:
18579 case OACC_LOOP:
18580 {
18581 tree clauses, body, pre_body;
18582 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18583 tree orig_declv = NULL_TREE;
18584 tree incrv = NULL_TREE;
18585 enum c_omp_region_type ort = C_ORT_OMP;
18586 bool any_range_for = false;
18587 int i;
18588
18589 if (TREE_CODE (t) == OACC_LOOP)
18590 ort = C_ORT_ACC;
18591
18592 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18593 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18594 in_decl);
18595 if (OMP_FOR_INIT (t) != NULL_TREE)
18596 {
18597 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18598 if (OMP_FOR_ORIG_DECLS (t))
18599 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18600 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18601 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18602 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18603 }
18604
18605 keep_next_level (true);
18606 stmt = begin_omp_structured_block ();
18607
18608 pre_body = push_stmt_list ();
18609 RECUR (OMP_FOR_PRE_BODY (t));
18610 pre_body = pop_stmt_list (pre_body);
18611
18612 if (OMP_FOR_INIT (t) != NULL_TREE)
18613 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18614 any_range_for
18615 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18616 condv, incrv, &clauses, args,
18617 complain, in_decl,
18618 integral_constant_expression_p);
18619 omp_parallel_combined_clauses = NULL;
18620
18621 if (any_range_for)
18622 {
18623 gcc_assert (orig_declv);
18624 body = begin_omp_structured_block ();
18625 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18626 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18627 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18628 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18629 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18630 TREE_VEC_ELT (declv, i));
18631 }
18632 else
18633 body = push_stmt_list ();
18634 RECUR (OMP_FOR_BODY (t));
18635 if (any_range_for)
18636 body = finish_omp_structured_block (body);
18637 else
18638 body = pop_stmt_list (body);
18639
18640 if (OMP_FOR_INIT (t) != NULL_TREE)
18641 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18642 orig_declv, initv, condv, incrv, body, pre_body,
18643 NULL, clauses);
18644 else
18645 {
18646 t = make_node (TREE_CODE (t));
18647 TREE_TYPE (t) = void_type_node;
18648 OMP_FOR_BODY (t) = body;
18649 OMP_FOR_PRE_BODY (t) = pre_body;
18650 OMP_FOR_CLAUSES (t) = clauses;
18651 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18652 add_stmt (t);
18653 }
18654
18655 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18656 t));
18657 pop_omp_privatization_clauses (r);
18658 }
18659 break;
18660
18661 case OMP_SECTIONS:
18662 omp_parallel_combined_clauses = NULL;
18663 /* FALLTHRU */
18664 case OMP_SINGLE:
18665 case OMP_TEAMS:
18666 case OMP_CRITICAL:
18667 case OMP_TASKGROUP:
18668 case OMP_SCAN:
18669 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18670 && OMP_TEAMS_COMBINED (t));
18671 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18672 in_decl);
18673 if (TREE_CODE (t) == OMP_TEAMS)
18674 {
18675 keep_next_level (true);
18676 stmt = begin_omp_structured_block ();
18677 RECUR (OMP_BODY (t));
18678 stmt = finish_omp_structured_block (stmt);
18679 }
18680 else
18681 {
18682 stmt = push_stmt_list ();
18683 RECUR (OMP_BODY (t));
18684 stmt = pop_stmt_list (stmt);
18685 }
18686
18687 t = copy_node (t);
18688 OMP_BODY (t) = stmt;
18689 OMP_CLAUSES (t) = tmp;
18690 add_stmt (t);
18691 pop_omp_privatization_clauses (r);
18692 break;
18693
18694 case OMP_DEPOBJ:
18695 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18696 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18697 {
18698 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18699 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18700 {
18701 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18702 args, complain, in_decl);
18703 if (tmp == NULL_TREE)
18704 tmp = error_mark_node;
18705 }
18706 else
18707 {
18708 kind = (enum omp_clause_depend_kind)
18709 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18710 tmp = NULL_TREE;
18711 }
18712 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18713 }
18714 else
18715 finish_omp_depobj (EXPR_LOCATION (t), r,
18716 OMP_CLAUSE_DEPEND_SOURCE,
18717 OMP_DEPOBJ_CLAUSES (t));
18718 break;
18719
18720 case OACC_DATA:
18721 case OMP_TARGET_DATA:
18722 case OMP_TARGET:
18723 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18724 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18725 in_decl);
18726 keep_next_level (true);
18727 stmt = begin_omp_structured_block ();
18728
18729 RECUR (OMP_BODY (t));
18730 stmt = finish_omp_structured_block (stmt);
18731
18732 t = copy_node (t);
18733 OMP_BODY (t) = stmt;
18734 OMP_CLAUSES (t) = tmp;
18735 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18736 {
18737 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18738 if (teams)
18739 {
18740 /* For combined target teams, ensure the num_teams and
18741 thread_limit clause expressions are evaluated on the host,
18742 before entering the target construct. */
18743 tree c;
18744 for (c = OMP_TEAMS_CLAUSES (teams);
18745 c; c = OMP_CLAUSE_CHAIN (c))
18746 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18747 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18748 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18749 {
18750 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18751 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18752 if (expr == error_mark_node)
18753 continue;
18754 tmp = TARGET_EXPR_SLOT (expr);
18755 add_stmt (expr);
18756 OMP_CLAUSE_OPERAND (c, 0) = expr;
18757 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18758 OMP_CLAUSE_FIRSTPRIVATE);
18759 OMP_CLAUSE_DECL (tc) = tmp;
18760 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18761 OMP_TARGET_CLAUSES (t) = tc;
18762 }
18763 }
18764 }
18765 add_stmt (t);
18766 break;
18767
18768 case OACC_DECLARE:
18769 t = copy_node (t);
18770 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18771 complain, in_decl);
18772 OACC_DECLARE_CLAUSES (t) = tmp;
18773 add_stmt (t);
18774 break;
18775
18776 case OMP_TARGET_UPDATE:
18777 case OMP_TARGET_ENTER_DATA:
18778 case OMP_TARGET_EXIT_DATA:
18779 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18780 complain, in_decl);
18781 t = copy_node (t);
18782 OMP_STANDALONE_CLAUSES (t) = tmp;
18783 add_stmt (t);
18784 break;
18785
18786 case OACC_CACHE:
18787 case OACC_ENTER_DATA:
18788 case OACC_EXIT_DATA:
18789 case OACC_UPDATE:
18790 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18791 complain, in_decl);
18792 t = copy_node (t);
18793 OMP_STANDALONE_CLAUSES (t) = tmp;
18794 add_stmt (t);
18795 break;
18796
18797 case OMP_ORDERED:
18798 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18799 complain, in_decl);
18800 stmt = push_stmt_list ();
18801 RECUR (OMP_BODY (t));
18802 stmt = pop_stmt_list (stmt);
18803
18804 t = copy_node (t);
18805 OMP_BODY (t) = stmt;
18806 OMP_ORDERED_CLAUSES (t) = tmp;
18807 add_stmt (t);
18808 break;
18809
18810 case OMP_MASTER:
18811 omp_parallel_combined_clauses = NULL;
18812 /* FALLTHRU */
18813 case OMP_SECTION:
18814 stmt = push_stmt_list ();
18815 RECUR (OMP_BODY (t));
18816 stmt = pop_stmt_list (stmt);
18817
18818 t = copy_node (t);
18819 OMP_BODY (t) = stmt;
18820 add_stmt (t);
18821 break;
18822
18823 case OMP_ATOMIC:
18824 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18825 tmp = NULL_TREE;
18826 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18827 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18828 complain, in_decl);
18829 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18830 {
18831 tree op1 = TREE_OPERAND (t, 1);
18832 tree rhs1 = NULL_TREE;
18833 tree lhs, rhs;
18834 if (TREE_CODE (op1) == COMPOUND_EXPR)
18835 {
18836 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18837 op1 = TREE_OPERAND (op1, 1);
18838 }
18839 lhs = RECUR (TREE_OPERAND (op1, 0));
18840 rhs = RECUR (TREE_OPERAND (op1, 1));
18841 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18842 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18843 OMP_ATOMIC_MEMORY_ORDER (t));
18844 }
18845 else
18846 {
18847 tree op1 = TREE_OPERAND (t, 1);
18848 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18849 tree rhs1 = NULL_TREE;
18850 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18851 enum tree_code opcode = NOP_EXPR;
18852 if (code == OMP_ATOMIC_READ)
18853 {
18854 v = RECUR (TREE_OPERAND (op1, 0));
18855 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18856 }
18857 else if (code == OMP_ATOMIC_CAPTURE_OLD
18858 || code == OMP_ATOMIC_CAPTURE_NEW)
18859 {
18860 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18861 v = RECUR (TREE_OPERAND (op1, 0));
18862 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18863 if (TREE_CODE (op11) == COMPOUND_EXPR)
18864 {
18865 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18866 op11 = TREE_OPERAND (op11, 1);
18867 }
18868 lhs = RECUR (TREE_OPERAND (op11, 0));
18869 rhs = RECUR (TREE_OPERAND (op11, 1));
18870 opcode = TREE_CODE (op11);
18871 if (opcode == MODIFY_EXPR)
18872 opcode = NOP_EXPR;
18873 }
18874 else
18875 {
18876 code = OMP_ATOMIC;
18877 lhs = RECUR (TREE_OPERAND (op1, 0));
18878 rhs = RECUR (TREE_OPERAND (op1, 1));
18879 }
18880 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18881 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18882 }
18883 break;
18884
18885 case TRANSACTION_EXPR:
18886 {
18887 int flags = 0;
18888 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18889 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18890
18891 if (TRANSACTION_EXPR_IS_STMT (t))
18892 {
18893 tree body = TRANSACTION_EXPR_BODY (t);
18894 tree noex = NULL_TREE;
18895 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18896 {
18897 noex = MUST_NOT_THROW_COND (body);
18898 if (noex == NULL_TREE)
18899 noex = boolean_true_node;
18900 body = TREE_OPERAND (body, 0);
18901 }
18902 stmt = begin_transaction_stmt (input_location, NULL, flags);
18903 RECUR (body);
18904 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18905 }
18906 else
18907 {
18908 stmt = build_transaction_expr (EXPR_LOCATION (t),
18909 RECUR (TRANSACTION_EXPR_BODY (t)),
18910 flags, NULL_TREE);
18911 RETURN (stmt);
18912 }
18913 }
18914 break;
18915
18916 case MUST_NOT_THROW_EXPR:
18917 {
18918 tree op0 = RECUR (TREE_OPERAND (t, 0));
18919 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18920 RETURN (build_must_not_throw_expr (op0, cond));
18921 }
18922
18923 case EXPR_PACK_EXPANSION:
18924 error ("invalid use of pack expansion expression");
18925 RETURN (error_mark_node);
18926
18927 case NONTYPE_ARGUMENT_PACK:
18928 error ("use %<...%> to expand argument pack");
18929 RETURN (error_mark_node);
18930
18931 case COMPOUND_EXPR:
18932 tmp = RECUR (TREE_OPERAND (t, 0));
18933 if (tmp == NULL_TREE)
18934 /* If the first operand was a statement, we're done with it. */
18935 RETURN (RECUR (TREE_OPERAND (t, 1)));
18936 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18937 RECUR (TREE_OPERAND (t, 1)),
18938 complain));
18939
18940 case ANNOTATE_EXPR:
18941 tmp = RECUR (TREE_OPERAND (t, 0));
18942 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18943 TREE_TYPE (tmp), tmp,
18944 RECUR (TREE_OPERAND (t, 1)),
18945 RECUR (TREE_OPERAND (t, 2))));
18946
18947 case PREDICT_EXPR:
18948 RETURN (add_stmt (copy_node (t)));
18949
18950 default:
18951 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18952
18953 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18954 /*function_p=*/false,
18955 integral_constant_expression_p));
18956 }
18957
18958 RETURN (NULL_TREE);
18959 out:
18960 input_location = loc;
18961 return r;
18962 #undef RECUR
18963 #undef RETURN
18964 }
18965
18966 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18967 function. For description of the body see comment above
18968 cp_parser_omp_declare_reduction_exprs. */
18969
18970 static void
tsubst_omp_udr(tree t,tree args,tsubst_flags_t complain,tree in_decl)18971 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18972 {
18973 if (t == NULL_TREE || t == error_mark_node)
18974 return;
18975
18976 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18977
18978 tree_stmt_iterator tsi;
18979 int i;
18980 tree stmts[7];
18981 memset (stmts, 0, sizeof stmts);
18982 for (i = 0, tsi = tsi_start (t);
18983 i < 7 && !tsi_end_p (tsi);
18984 i++, tsi_next (&tsi))
18985 stmts[i] = tsi_stmt (tsi);
18986 gcc_assert (tsi_end_p (tsi));
18987
18988 if (i >= 3)
18989 {
18990 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18991 && TREE_CODE (stmts[1]) == DECL_EXPR);
18992 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18993 args, complain, in_decl);
18994 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18995 args, complain, in_decl);
18996 DECL_CONTEXT (omp_out) = current_function_decl;
18997 DECL_CONTEXT (omp_in) = current_function_decl;
18998 keep_next_level (true);
18999 tree block = begin_omp_structured_block ();
19000 tsubst_expr (stmts[2], args, complain, in_decl, false);
19001 block = finish_omp_structured_block (block);
19002 block = maybe_cleanup_point_expr_void (block);
19003 add_decl_expr (omp_out);
19004 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
19005 TREE_NO_WARNING (omp_out) = 1;
19006 add_decl_expr (omp_in);
19007 finish_expr_stmt (block);
19008 }
19009 if (i >= 6)
19010 {
19011 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19012 && TREE_CODE (stmts[4]) == DECL_EXPR);
19013 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19014 args, complain, in_decl);
19015 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19016 args, complain, in_decl);
19017 DECL_CONTEXT (omp_priv) = current_function_decl;
19018 DECL_CONTEXT (omp_orig) = current_function_decl;
19019 keep_next_level (true);
19020 tree block = begin_omp_structured_block ();
19021 tsubst_expr (stmts[5], args, complain, in_decl, false);
19022 block = finish_omp_structured_block (block);
19023 block = maybe_cleanup_point_expr_void (block);
19024 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19025 add_decl_expr (omp_priv);
19026 add_decl_expr (omp_orig);
19027 finish_expr_stmt (block);
19028 if (i == 7)
19029 add_decl_expr (omp_orig);
19030 }
19031 }
19032
19033 /* T is a postfix-expression that is not being used in a function
19034 call. Return the substituted version of T. */
19035
19036 static tree
tsubst_non_call_postfix_expression(tree t,tree args,tsubst_flags_t complain,tree in_decl)19037 tsubst_non_call_postfix_expression (tree t, tree args,
19038 tsubst_flags_t complain,
19039 tree in_decl)
19040 {
19041 if (TREE_CODE (t) == SCOPE_REF)
19042 t = tsubst_qualified_id (t, args, complain, in_decl,
19043 /*done=*/false, /*address_p=*/false);
19044 else
19045 t = tsubst_copy_and_build (t, args, complain, in_decl,
19046 /*function_p=*/false,
19047 /*integral_constant_expression_p=*/false);
19048
19049 return t;
19050 }
19051
19052 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19053 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19054 dependent init-capture. */
19055
19056 static void
prepend_one_capture(tree field,tree init,tree & list,tsubst_flags_t complain)19057 prepend_one_capture (tree field, tree init, tree &list,
19058 tsubst_flags_t complain)
19059 {
19060 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19061 {
19062 tree type = NULL_TREE;
19063 if (!init)
19064 {
19065 if (complain & tf_error)
19066 error ("empty initializer in lambda init-capture");
19067 init = error_mark_node;
19068 }
19069 else if (TREE_CODE (init) == TREE_LIST)
19070 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19071 if (!type)
19072 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19073 TREE_TYPE (field) = type;
19074 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19075 }
19076 list = tree_cons (field, init, list);
19077 }
19078
19079 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19080 instantiation context. Instantiating a pack expansion containing a lambda
19081 might result in multiple lambdas all based on the same lambda in the
19082 template. */
19083
19084 tree
tsubst_lambda_expr(tree t,tree args,tsubst_flags_t complain,tree in_decl)19085 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19086 {
19087 tree oldfn = lambda_function (t);
19088 in_decl = oldfn;
19089
19090 tree r = build_lambda_expr ();
19091
19092 LAMBDA_EXPR_LOCATION (r)
19093 = LAMBDA_EXPR_LOCATION (t);
19094 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19095 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19096 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19097 LAMBDA_EXPR_INSTANTIATED (r) = true;
19098
19099 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19100 /* A lambda in a default argument outside a class gets no
19101 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19102 tsubst_default_argument calls start_lambda_scope, so we need to
19103 specifically ignore it here, and use the global scope. */
19104 record_null_lambda_scope (r);
19105 else
19106 record_lambda_scope (r);
19107
19108 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19109 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19110
19111 vec<tree,va_gc>* field_packs = NULL;
19112
19113 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19114 cap = TREE_CHAIN (cap))
19115 {
19116 tree ofield = TREE_PURPOSE (cap);
19117 tree init = TREE_VALUE (cap);
19118 if (PACK_EXPANSION_P (init))
19119 init = tsubst_pack_expansion (init, args, complain, in_decl);
19120 else
19121 init = tsubst_copy_and_build (init, args, complain, in_decl,
19122 /*fn*/false, /*constexpr*/false);
19123
19124 if (init == error_mark_node)
19125 return error_mark_node;
19126
19127 if (init && TREE_CODE (init) == TREE_LIST)
19128 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19129
19130 if (!processing_template_decl
19131 && init && TREE_CODE (init) != TREE_VEC
19132 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19133 {
19134 /* For a VLA, simply tsubsting the field type won't work, we need to
19135 go through add_capture again. XXX do we want to do this for all
19136 captures? */
19137 tree name = (get_identifier
19138 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19139 tree ftype = TREE_TYPE (ofield);
19140 bool by_ref = (TYPE_REF_P (ftype)
19141 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19142 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19143 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19144 continue;
19145 }
19146
19147 if (PACK_EXPANSION_P (ofield))
19148 ofield = PACK_EXPANSION_PATTERN (ofield);
19149 tree field = tsubst_decl (ofield, args, complain);
19150
19151 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19152 {
19153 /* Remember these for when we've pushed local_specializations. */
19154 vec_safe_push (field_packs, ofield);
19155 vec_safe_push (field_packs, field);
19156 }
19157
19158 if (field == error_mark_node)
19159 return error_mark_node;
19160
19161 if (TREE_CODE (field) == TREE_VEC)
19162 {
19163 int len = TREE_VEC_LENGTH (field);
19164 gcc_assert (TREE_CODE (init) == TREE_VEC
19165 && TREE_VEC_LENGTH (init) == len);
19166 for (int i = 0; i < len; ++i)
19167 prepend_one_capture (TREE_VEC_ELT (field, i),
19168 TREE_VEC_ELT (init, i),
19169 LAMBDA_EXPR_CAPTURE_LIST (r),
19170 complain);
19171 }
19172 else
19173 {
19174 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19175 complain);
19176
19177 if (id_equal (DECL_NAME (field), "__this"))
19178 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19179 }
19180 }
19181
19182 tree type = begin_lambda_type (r);
19183 if (type == error_mark_node)
19184 return error_mark_node;
19185
19186 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19187 determine_visibility (TYPE_NAME (type));
19188
19189 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19190
19191 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19192 ? DECL_TI_TEMPLATE (oldfn)
19193 : NULL_TREE);
19194
19195 tree fntype = static_fn_type (oldfn);
19196 if (oldtmpl)
19197 ++processing_template_decl;
19198 fntype = tsubst (fntype, args, complain, in_decl);
19199 if (oldtmpl)
19200 --processing_template_decl;
19201
19202 if (fntype == error_mark_node)
19203 r = error_mark_node;
19204 else
19205 {
19206 /* The body of a lambda-expression is not a subexpression of the
19207 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19208 which would be skipped if cp_unevaluated_operand. */
19209 cp_evaluated ev;
19210
19211 /* Fix the type of 'this'. */
19212 fntype = build_memfn_type (fntype, type,
19213 type_memfn_quals (fntype),
19214 type_memfn_rqual (fntype));
19215 tree fn, tmpl;
19216 if (oldtmpl)
19217 {
19218 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19219 if (tmpl == error_mark_node)
19220 {
19221 r = error_mark_node;
19222 goto out;
19223 }
19224 fn = DECL_TEMPLATE_RESULT (tmpl);
19225 finish_member_declaration (tmpl);
19226 }
19227 else
19228 {
19229 tmpl = NULL_TREE;
19230 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19231 if (fn == error_mark_node)
19232 {
19233 r = error_mark_node;
19234 goto out;
19235 }
19236 finish_member_declaration (fn);
19237 }
19238
19239 if (tree ci = get_constraints (oldfn))
19240 {
19241 /* Substitute into the lambda's constraints. */
19242 if (oldtmpl)
19243 ++processing_template_decl;
19244 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19245 if (oldtmpl)
19246 --processing_template_decl;
19247 set_constraints (fn, ci);
19248 }
19249
19250 /* Let finish_function set this. */
19251 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19252
19253 bool nested = cfun;
19254 if (nested)
19255 push_function_context ();
19256 else
19257 /* Still increment function_depth so that we don't GC in the
19258 middle of an expression. */
19259 ++function_depth;
19260
19261 local_specialization_stack s (lss_copy);
19262
19263 tree body = start_lambda_function (fn, r);
19264
19265 /* Now record them for lookup_init_capture_pack. */
19266 int fplen = vec_safe_length (field_packs);
19267 for (int i = 0; i < fplen; )
19268 {
19269 tree pack = (*field_packs)[i++];
19270 tree inst = (*field_packs)[i++];
19271 register_local_specialization (inst, pack);
19272 }
19273 release_tree_vector (field_packs);
19274
19275 register_parameter_specializations (oldfn, fn);
19276
19277 if (oldtmpl)
19278 {
19279 /* We might not partially instantiate some parts of the function, so
19280 copy these flags from the original template. */
19281 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19282 current_function_returns_value = ol->returns_value;
19283 current_function_returns_null = ol->returns_null;
19284 current_function_returns_abnormally = ol->returns_abnormally;
19285 current_function_infinite_loop = ol->infinite_loop;
19286 }
19287
19288 /* [temp.deduct] A lambda-expression appearing in a function type or a
19289 template parameter is not considered part of the immediate context for
19290 the purposes of template argument deduction. */
19291 complain = tf_warning_or_error;
19292
19293 tree saved = DECL_SAVED_TREE (oldfn);
19294 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
19295 /* We already have a body block from start_lambda_function, we don't
19296 need another to confuse NRV (91217). */
19297 saved = BIND_EXPR_BODY (saved);
19298
19299 tsubst_expr (saved, args, complain, r, /*constexpr*/false);
19300
19301 finish_lambda_function (body);
19302
19303 if (nested)
19304 pop_function_context ();
19305 else
19306 --function_depth;
19307
19308 /* The capture list was built up in reverse order; fix that now. */
19309 LAMBDA_EXPR_CAPTURE_LIST (r)
19310 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19311
19312 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19313
19314 maybe_add_lambda_conv_op (type);
19315 }
19316
19317 out:
19318 finish_struct (type, /*attr*/NULL_TREE);
19319
19320 insert_pending_capture_proxies ();
19321
19322 return r;
19323 }
19324
19325 /* Like tsubst but deals with expressions and performs semantic
19326 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19327 "F<TARGS> (ARGS)". */
19328
19329 tree
tsubst_copy_and_build(tree t,tree args,tsubst_flags_t complain,tree in_decl,bool function_p,bool integral_constant_expression_p)19330 tsubst_copy_and_build (tree t,
19331 tree args,
19332 tsubst_flags_t complain,
19333 tree in_decl,
19334 bool function_p,
19335 bool integral_constant_expression_p)
19336 {
19337 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19338 #define RECUR(NODE) \
19339 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19340 /*function_p=*/false, \
19341 integral_constant_expression_p)
19342
19343 tree retval, op1;
19344 location_t save_loc;
19345
19346 if (t == NULL_TREE || t == error_mark_node)
19347 return t;
19348
19349 save_loc = input_location;
19350 if (location_t eloc = cp_expr_location (t))
19351 input_location = eloc;
19352
19353 /* N3276 decltype magic only applies to calls at the top level or on the
19354 right side of a comma. */
19355 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19356 complain &= ~tf_decltype;
19357
19358 switch (TREE_CODE (t))
19359 {
19360 case USING_DECL:
19361 t = DECL_NAME (t);
19362 /* Fall through. */
19363 case IDENTIFIER_NODE:
19364 {
19365 tree decl;
19366 cp_id_kind idk;
19367 bool non_integral_constant_expression_p;
19368 const char *error_msg;
19369
19370 if (IDENTIFIER_CONV_OP_P (t))
19371 {
19372 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19373 t = make_conv_op_name (new_type);
19374 }
19375
19376 /* Look up the name. */
19377 decl = lookup_name (t);
19378
19379 /* By convention, expressions use ERROR_MARK_NODE to indicate
19380 failure, not NULL_TREE. */
19381 if (decl == NULL_TREE)
19382 decl = error_mark_node;
19383
19384 decl = finish_id_expression (t, decl, NULL_TREE,
19385 &idk,
19386 integral_constant_expression_p,
19387 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19388 &non_integral_constant_expression_p,
19389 /*template_p=*/false,
19390 /*done=*/true,
19391 /*address_p=*/false,
19392 /*template_arg_p=*/false,
19393 &error_msg,
19394 input_location);
19395 if (error_msg)
19396 error (error_msg);
19397 if (!function_p && identifier_p (decl))
19398 {
19399 if (complain & tf_error)
19400 unqualified_name_lookup_error (decl);
19401 decl = error_mark_node;
19402 }
19403 RETURN (decl);
19404 }
19405
19406 case TEMPLATE_ID_EXPR:
19407 {
19408 tree object;
19409 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19410 complain, in_decl,
19411 function_p,
19412 integral_constant_expression_p);
19413 tree targs = TREE_OPERAND (t, 1);
19414
19415 if (targs)
19416 targs = tsubst_template_args (targs, args, complain, in_decl);
19417 if (targs == error_mark_node)
19418 RETURN (error_mark_node);
19419
19420 if (TREE_CODE (templ) == SCOPE_REF)
19421 {
19422 tree name = TREE_OPERAND (templ, 1);
19423 tree tid = lookup_template_function (name, targs);
19424 TREE_OPERAND (templ, 1) = tid;
19425 RETURN (templ);
19426 }
19427
19428 if (concept_definition_p (templ))
19429 {
19430 tree check = build_concept_check (templ, targs, complain);
19431 if (check == error_mark_node)
19432 RETURN (error_mark_node);
19433
19434 tree id = unpack_concept_check (check);
19435
19436 /* If we built a function concept check, return the underlying
19437 template-id. So we can evaluate it as a function call. */
19438 if (function_concept_p (TREE_OPERAND (id, 0)))
19439 RETURN (id);
19440
19441 RETURN (check);
19442 }
19443
19444 if (variable_template_p (templ))
19445 {
19446 tree r = lookup_and_finish_template_variable (templ, targs,
19447 complain);
19448 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19449 RETURN (r);
19450 }
19451
19452 if (TREE_CODE (templ) == COMPONENT_REF)
19453 {
19454 object = TREE_OPERAND (templ, 0);
19455 templ = TREE_OPERAND (templ, 1);
19456 }
19457 else
19458 object = NULL_TREE;
19459
19460 tree tid = lookup_template_function (templ, targs);
19461
19462 if (object)
19463 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
19464 object, tid, NULL_TREE));
19465 else if (identifier_p (templ))
19466 {
19467 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19468 name lookup found nothing when parsing the template name. */
19469 gcc_assert (cxx_dialect >= cxx2a || seen_error ());
19470 RETURN (tid);
19471 }
19472 else
19473 RETURN (baselink_for_fns (tid));
19474 }
19475
19476 case INDIRECT_REF:
19477 {
19478 tree r = RECUR (TREE_OPERAND (t, 0));
19479
19480 if (REFERENCE_REF_P (t))
19481 {
19482 /* A type conversion to reference type will be enclosed in
19483 such an indirect ref, but the substitution of the cast
19484 will have also added such an indirect ref. */
19485 r = convert_from_reference (r);
19486 }
19487 else
19488 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19489 complain|decltype_flag);
19490
19491 if (REF_PARENTHESIZED_P (t))
19492 r = force_paren_expr (r);
19493
19494 RETURN (r);
19495 }
19496
19497 case NOP_EXPR:
19498 {
19499 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19500 tree op0 = RECUR (TREE_OPERAND (t, 0));
19501 RETURN (build_nop (type, op0));
19502 }
19503
19504 case IMPLICIT_CONV_EXPR:
19505 {
19506 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19507 tree expr = RECUR (TREE_OPERAND (t, 0));
19508 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19509 {
19510 retval = copy_node (t);
19511 TREE_TYPE (retval) = type;
19512 TREE_OPERAND (retval, 0) = expr;
19513 RETURN (retval);
19514 }
19515 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19516 /* We'll pass this to convert_nontype_argument again, we don't need
19517 to actually perform any conversion here. */
19518 RETURN (expr);
19519 int flags = LOOKUP_IMPLICIT;
19520 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19521 flags = LOOKUP_NORMAL;
19522 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19523 flags |= LOOKUP_NO_NARROWING;
19524 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19525 flags));
19526 }
19527
19528 case CONVERT_EXPR:
19529 {
19530 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19531 tree op0 = RECUR (TREE_OPERAND (t, 0));
19532 if (op0 == error_mark_node)
19533 RETURN (error_mark_node);
19534 RETURN (build1 (CONVERT_EXPR, type, op0));
19535 }
19536
19537 case CAST_EXPR:
19538 case REINTERPRET_CAST_EXPR:
19539 case CONST_CAST_EXPR:
19540 case DYNAMIC_CAST_EXPR:
19541 case STATIC_CAST_EXPR:
19542 {
19543 tree type;
19544 tree op, r = NULL_TREE;
19545
19546 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19547 if (integral_constant_expression_p
19548 && !cast_valid_in_integral_constant_expression_p (type))
19549 {
19550 if (complain & tf_error)
19551 error ("a cast to a type other than an integral or "
19552 "enumeration type cannot appear in a constant-expression");
19553 RETURN (error_mark_node);
19554 }
19555
19556 op = RECUR (TREE_OPERAND (t, 0));
19557
19558 warning_sentinel s(warn_useless_cast);
19559 warning_sentinel s2(warn_ignored_qualifiers);
19560 switch (TREE_CODE (t))
19561 {
19562 case CAST_EXPR:
19563 r = build_functional_cast (input_location, type, op, complain);
19564 break;
19565 case REINTERPRET_CAST_EXPR:
19566 r = build_reinterpret_cast (input_location, type, op, complain);
19567 break;
19568 case CONST_CAST_EXPR:
19569 r = build_const_cast (input_location, type, op, complain);
19570 break;
19571 case DYNAMIC_CAST_EXPR:
19572 r = build_dynamic_cast (input_location, type, op, complain);
19573 break;
19574 case STATIC_CAST_EXPR:
19575 r = build_static_cast (input_location, type, op, complain);
19576 break;
19577 default:
19578 gcc_unreachable ();
19579 }
19580
19581 RETURN (r);
19582 }
19583
19584 case POSTDECREMENT_EXPR:
19585 case POSTINCREMENT_EXPR:
19586 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19587 args, complain, in_decl);
19588 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19589 complain|decltype_flag));
19590
19591 case PREDECREMENT_EXPR:
19592 case PREINCREMENT_EXPR:
19593 case NEGATE_EXPR:
19594 case BIT_NOT_EXPR:
19595 case ABS_EXPR:
19596 case TRUTH_NOT_EXPR:
19597 case UNARY_PLUS_EXPR: /* Unary + */
19598 case REALPART_EXPR:
19599 case IMAGPART_EXPR:
19600 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19601 RECUR (TREE_OPERAND (t, 0)),
19602 complain|decltype_flag));
19603
19604 case FIX_TRUNC_EXPR:
19605 gcc_unreachable ();
19606
19607 case ADDR_EXPR:
19608 op1 = TREE_OPERAND (t, 0);
19609 if (TREE_CODE (op1) == LABEL_DECL)
19610 RETURN (finish_label_address_expr (DECL_NAME (op1),
19611 EXPR_LOCATION (op1)));
19612 if (TREE_CODE (op1) == SCOPE_REF)
19613 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19614 /*done=*/true, /*address_p=*/true);
19615 else
19616 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19617 in_decl);
19618 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19619 complain|decltype_flag));
19620
19621 case PLUS_EXPR:
19622 case MINUS_EXPR:
19623 case MULT_EXPR:
19624 case TRUNC_DIV_EXPR:
19625 case CEIL_DIV_EXPR:
19626 case FLOOR_DIV_EXPR:
19627 case ROUND_DIV_EXPR:
19628 case EXACT_DIV_EXPR:
19629 case BIT_AND_EXPR:
19630 case BIT_IOR_EXPR:
19631 case BIT_XOR_EXPR:
19632 case TRUNC_MOD_EXPR:
19633 case FLOOR_MOD_EXPR:
19634 case TRUTH_ANDIF_EXPR:
19635 case TRUTH_ORIF_EXPR:
19636 case TRUTH_AND_EXPR:
19637 case TRUTH_OR_EXPR:
19638 case RSHIFT_EXPR:
19639 case LSHIFT_EXPR:
19640 case EQ_EXPR:
19641 case NE_EXPR:
19642 case MAX_EXPR:
19643 case MIN_EXPR:
19644 case LE_EXPR:
19645 case GE_EXPR:
19646 case LT_EXPR:
19647 case GT_EXPR:
19648 case SPACESHIP_EXPR:
19649 case MEMBER_REF:
19650 case DOTSTAR_EXPR:
19651 {
19652 /* If T was type-dependent, suppress warnings that depend on the range
19653 of the types involved. */
19654 ++processing_template_decl;
19655 const bool was_dep = (potential_constant_expression (t)
19656 ? value_dependent_expression_p (t)
19657 : type_dependent_expression_p (t));
19658 --processing_template_decl;
19659 tree op0 = RECUR (TREE_OPERAND (t, 0));
19660 tree op1 = RECUR (TREE_OPERAND (t, 1));
19661
19662 warning_sentinel s1(warn_type_limits, was_dep);
19663 warning_sentinel s2(warn_div_by_zero, was_dep);
19664 warning_sentinel s3(warn_logical_op, was_dep);
19665 warning_sentinel s4(warn_tautological_compare, was_dep);
19666
19667 tree r = build_x_binary_op
19668 (input_location, TREE_CODE (t),
19669 op0,
19670 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19671 ? ERROR_MARK
19672 : TREE_CODE (TREE_OPERAND (t, 0))),
19673 op1,
19674 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19675 ? ERROR_MARK
19676 : TREE_CODE (TREE_OPERAND (t, 1))),
19677 /*overload=*/NULL,
19678 complain|decltype_flag);
19679 if (EXPR_P (r) && TREE_NO_WARNING (t))
19680 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19681
19682 RETURN (r);
19683 }
19684
19685 case POINTER_PLUS_EXPR:
19686 {
19687 tree op0 = RECUR (TREE_OPERAND (t, 0));
19688 if (op0 == error_mark_node)
19689 RETURN (error_mark_node);
19690 tree op1 = RECUR (TREE_OPERAND (t, 1));
19691 if (op1 == error_mark_node)
19692 RETURN (error_mark_node);
19693 RETURN (fold_build_pointer_plus (op0, op1));
19694 }
19695
19696 case SCOPE_REF:
19697 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19698 /*address_p=*/false));
19699
19700 case BASELINK:
19701 RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
19702 args, complain, in_decl));
19703
19704 case ARRAY_REF:
19705 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19706 args, complain, in_decl);
19707 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19708 RECUR (TREE_OPERAND (t, 1)),
19709 complain|decltype_flag));
19710
19711 case SIZEOF_EXPR:
19712 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19713 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19714 RETURN (tsubst_copy (t, args, complain, in_decl));
19715 /* Fall through */
19716
19717 case ALIGNOF_EXPR:
19718 {
19719 tree r;
19720
19721 op1 = TREE_OPERAND (t, 0);
19722 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19723 op1 = TREE_TYPE (op1);
19724 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19725 && ALIGNOF_EXPR_STD_P (t));
19726 if (!args)
19727 {
19728 /* When there are no ARGS, we are trying to evaluate a
19729 non-dependent expression from the parser. Trying to do
19730 the substitutions may not work. */
19731 if (!TYPE_P (op1))
19732 op1 = TREE_TYPE (op1);
19733 }
19734 else
19735 {
19736 ++cp_unevaluated_operand;
19737 ++c_inhibit_evaluation_warnings;
19738 if (TYPE_P (op1))
19739 op1 = tsubst (op1, args, complain, in_decl);
19740 else
19741 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19742 /*function_p=*/false,
19743 /*integral_constant_expression_p=*/
19744 false);
19745 --cp_unevaluated_operand;
19746 --c_inhibit_evaluation_warnings;
19747 }
19748 if (TYPE_P (op1))
19749 r = cxx_sizeof_or_alignof_type (input_location,
19750 op1, TREE_CODE (t), std_alignof,
19751 complain & tf_error);
19752 else
19753 r = cxx_sizeof_or_alignof_expr (input_location,
19754 op1, TREE_CODE (t),
19755 complain & tf_error);
19756 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19757 {
19758 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19759 {
19760 if (!processing_template_decl && TYPE_P (op1))
19761 {
19762 r = build_min (SIZEOF_EXPR, size_type_node,
19763 build1 (NOP_EXPR, op1, error_mark_node));
19764 SIZEOF_EXPR_TYPE_P (r) = 1;
19765 }
19766 else
19767 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19768 TREE_SIDE_EFFECTS (r) = 0;
19769 TREE_READONLY (r) = 1;
19770 }
19771 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19772 }
19773 RETURN (r);
19774 }
19775
19776 case AT_ENCODE_EXPR:
19777 {
19778 op1 = TREE_OPERAND (t, 0);
19779 ++cp_unevaluated_operand;
19780 ++c_inhibit_evaluation_warnings;
19781 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19782 /*function_p=*/false,
19783 /*integral_constant_expression_p=*/false);
19784 --cp_unevaluated_operand;
19785 --c_inhibit_evaluation_warnings;
19786 RETURN (objc_build_encode_expr (op1));
19787 }
19788
19789 case NOEXCEPT_EXPR:
19790 op1 = TREE_OPERAND (t, 0);
19791 ++cp_unevaluated_operand;
19792 ++c_inhibit_evaluation_warnings;
19793 ++cp_noexcept_operand;
19794 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19795 /*function_p=*/false,
19796 /*integral_constant_expression_p=*/false);
19797 --cp_unevaluated_operand;
19798 --c_inhibit_evaluation_warnings;
19799 --cp_noexcept_operand;
19800 RETURN (finish_noexcept_expr (op1, complain));
19801
19802 case MODOP_EXPR:
19803 {
19804 warning_sentinel s(warn_div_by_zero);
19805 tree lhs = RECUR (TREE_OPERAND (t, 0));
19806 tree rhs = RECUR (TREE_OPERAND (t, 2));
19807 tree r = build_x_modify_expr
19808 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19809 complain|decltype_flag);
19810 /* TREE_NO_WARNING must be set if either the expression was
19811 parenthesized or it uses an operator such as >>= rather
19812 than plain assignment. In the former case, it was already
19813 set and must be copied. In the latter case,
19814 build_x_modify_expr sets it and it must not be reset
19815 here. */
19816 if (TREE_NO_WARNING (t))
19817 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19818
19819 RETURN (r);
19820 }
19821
19822 case ARROW_EXPR:
19823 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19824 args, complain, in_decl);
19825 /* Remember that there was a reference to this entity. */
19826 if (DECL_P (op1)
19827 && !mark_used (op1, complain) && !(complain & tf_error))
19828 RETURN (error_mark_node);
19829 RETURN (build_x_arrow (input_location, op1, complain));
19830
19831 case NEW_EXPR:
19832 {
19833 tree placement = RECUR (TREE_OPERAND (t, 0));
19834 tree init = RECUR (TREE_OPERAND (t, 3));
19835 vec<tree, va_gc> *placement_vec;
19836 vec<tree, va_gc> *init_vec;
19837 tree ret;
19838 location_t loc = EXPR_LOCATION (t);
19839
19840 if (placement == NULL_TREE)
19841 placement_vec = NULL;
19842 else
19843 {
19844 placement_vec = make_tree_vector ();
19845 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19846 vec_safe_push (placement_vec, TREE_VALUE (placement));
19847 }
19848
19849 /* If there was an initializer in the original tree, but it
19850 instantiated to an empty list, then we should pass a
19851 non-NULL empty vector to tell build_new that it was an
19852 empty initializer() rather than no initializer. This can
19853 only happen when the initializer is a pack expansion whose
19854 parameter packs are of length zero. */
19855 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19856 init_vec = NULL;
19857 else
19858 {
19859 init_vec = make_tree_vector ();
19860 if (init == void_node)
19861 gcc_assert (init_vec != NULL);
19862 else
19863 {
19864 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19865 vec_safe_push (init_vec, TREE_VALUE (init));
19866 }
19867 }
19868
19869 /* Avoid passing an enclosing decl to valid_array_size_p. */
19870 in_decl = NULL_TREE;
19871
19872 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19873 tree op2 = RECUR (TREE_OPERAND (t, 2));
19874 ret = build_new (loc, &placement_vec, op1, op2,
19875 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19876 complain);
19877
19878 if (placement_vec != NULL)
19879 release_tree_vector (placement_vec);
19880 if (init_vec != NULL)
19881 release_tree_vector (init_vec);
19882
19883 RETURN (ret);
19884 }
19885
19886 case DELETE_EXPR:
19887 {
19888 tree op0 = RECUR (TREE_OPERAND (t, 0));
19889 tree op1 = RECUR (TREE_OPERAND (t, 1));
19890 RETURN (delete_sanity (input_location, op0, op1,
19891 DELETE_EXPR_USE_VEC (t),
19892 DELETE_EXPR_USE_GLOBAL (t),
19893 complain));
19894 }
19895
19896 case COMPOUND_EXPR:
19897 {
19898 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19899 complain & ~tf_decltype, in_decl,
19900 /*function_p=*/false,
19901 integral_constant_expression_p);
19902 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19903 op0,
19904 RECUR (TREE_OPERAND (t, 1)),
19905 complain|decltype_flag));
19906 }
19907
19908 case CALL_EXPR:
19909 {
19910 tree function;
19911 unsigned int nargs, i;
19912 bool qualified_p;
19913 bool koenig_p;
19914 tree ret;
19915
19916 function = CALL_EXPR_FN (t);
19917 /* Internal function with no arguments. */
19918 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19919 RETURN (t);
19920
19921 /* When we parsed the expression, we determined whether or
19922 not Koenig lookup should be performed. */
19923 koenig_p = KOENIG_LOOKUP_P (t);
19924 if (function == NULL_TREE)
19925 {
19926 koenig_p = false;
19927 qualified_p = false;
19928 }
19929 else if (TREE_CODE (function) == SCOPE_REF)
19930 {
19931 qualified_p = true;
19932 function = tsubst_qualified_id (function, args, complain, in_decl,
19933 /*done=*/false,
19934 /*address_p=*/false);
19935 }
19936 else if (koenig_p && identifier_p (function))
19937 {
19938 /* Do nothing; calling tsubst_copy_and_build on an identifier
19939 would incorrectly perform unqualified lookup again.
19940
19941 Note that we can also have an IDENTIFIER_NODE if the earlier
19942 unqualified lookup found a member function; in that case
19943 koenig_p will be false and we do want to do the lookup
19944 again to find the instantiated member function.
19945
19946 FIXME but doing that causes c++/15272, so we need to stop
19947 using IDENTIFIER_NODE in that situation. */
19948 qualified_p = false;
19949 }
19950 else
19951 {
19952 if (TREE_CODE (function) == COMPONENT_REF)
19953 {
19954 tree op = TREE_OPERAND (function, 1);
19955
19956 qualified_p = (TREE_CODE (op) == SCOPE_REF
19957 || (BASELINK_P (op)
19958 && BASELINK_QUALIFIED_P (op)));
19959 }
19960 else
19961 qualified_p = false;
19962
19963 if (TREE_CODE (function) == ADDR_EXPR
19964 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19965 /* Avoid error about taking the address of a constructor. */
19966 function = TREE_OPERAND (function, 0);
19967
19968 function = tsubst_copy_and_build (function, args, complain,
19969 in_decl,
19970 !qualified_p,
19971 integral_constant_expression_p);
19972
19973 if (BASELINK_P (function))
19974 qualified_p = true;
19975 }
19976
19977 nargs = call_expr_nargs (t);
19978 releasing_vec call_args;
19979 for (i = 0; i < nargs; ++i)
19980 {
19981 tree arg = CALL_EXPR_ARG (t, i);
19982
19983 if (!PACK_EXPANSION_P (arg))
19984 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19985 else
19986 {
19987 /* Expand the pack expansion and push each entry onto
19988 CALL_ARGS. */
19989 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19990 if (TREE_CODE (arg) == TREE_VEC)
19991 {
19992 unsigned int len, j;
19993
19994 len = TREE_VEC_LENGTH (arg);
19995 for (j = 0; j < len; ++j)
19996 {
19997 tree value = TREE_VEC_ELT (arg, j);
19998 if (value != NULL_TREE)
19999 value = convert_from_reference (value);
20000 vec_safe_push (call_args, value);
20001 }
20002 }
20003 else
20004 {
20005 /* A partial substitution. Add one entry. */
20006 vec_safe_push (call_args, arg);
20007 }
20008 }
20009 }
20010
20011 /* Stripped-down processing for a call in a thunk. Specifically, in
20012 the thunk template for a generic lambda. */
20013 if (CALL_FROM_THUNK_P (t))
20014 {
20015 /* Now that we've expanded any packs, the number of call args
20016 might be different. */
20017 unsigned int cargs = call_args->length ();
20018 tree thisarg = NULL_TREE;
20019 if (TREE_CODE (function) == COMPONENT_REF)
20020 {
20021 thisarg = TREE_OPERAND (function, 0);
20022 if (TREE_CODE (thisarg) == INDIRECT_REF)
20023 thisarg = TREE_OPERAND (thisarg, 0);
20024 function = TREE_OPERAND (function, 1);
20025 if (TREE_CODE (function) == BASELINK)
20026 function = BASELINK_FUNCTIONS (function);
20027 }
20028 /* We aren't going to do normal overload resolution, so force the
20029 template-id to resolve. */
20030 function = resolve_nondeduced_context (function, complain);
20031 for (unsigned i = 0; i < cargs; ++i)
20032 {
20033 /* In a thunk, pass through args directly, without any
20034 conversions. */
20035 tree arg = (*call_args)[i];
20036 while (TREE_CODE (arg) != PARM_DECL)
20037 arg = TREE_OPERAND (arg, 0);
20038 (*call_args)[i] = arg;
20039 }
20040 if (thisarg)
20041 {
20042 /* If there are no other args, just push 'this'. */
20043 if (cargs == 0)
20044 vec_safe_push (call_args, thisarg);
20045 else
20046 {
20047 /* Otherwise, shift the other args over to make room. */
20048 tree last = (*call_args)[cargs - 1];
20049 vec_safe_push (call_args, last);
20050 for (int i = cargs - 1; i > 0; --i)
20051 (*call_args)[i] = (*call_args)[i - 1];
20052 (*call_args)[0] = thisarg;
20053 }
20054 }
20055 ret = build_call_a (function, call_args->length (),
20056 call_args->address ());
20057 /* The thunk location is not interesting. */
20058 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20059 CALL_FROM_THUNK_P (ret) = true;
20060 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20061 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20062
20063 RETURN (ret);
20064 }
20065
20066 /* We do not perform argument-dependent lookup if normal
20067 lookup finds a non-function, in accordance with the
20068 resolution of DR 218. */
20069 if (koenig_p
20070 && ((is_overloaded_fn (function)
20071 /* If lookup found a member function, the Koenig lookup is
20072 not appropriate, even if an unqualified-name was used
20073 to denote the function. */
20074 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20075 || identifier_p (function)
20076 /* C++20 P0846: Lookup found nothing. */
20077 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20078 && identifier_p (TREE_OPERAND (function, 0))))
20079 /* Only do this when substitution turns a dependent call
20080 into a non-dependent call. */
20081 && type_dependent_expression_p_push (t)
20082 && !any_type_dependent_arguments_p (call_args))
20083 function = perform_koenig_lookup (function, call_args, tf_none);
20084
20085 if (function != NULL_TREE
20086 && (identifier_p (function)
20087 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20088 && identifier_p (TREE_OPERAND (function, 0))))
20089 && !any_type_dependent_arguments_p (call_args))
20090 {
20091 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20092 function = TREE_OPERAND (function, 0);
20093 if (koenig_p && (complain & tf_warning_or_error))
20094 {
20095 /* For backwards compatibility and good diagnostics, try
20096 the unqualified lookup again if we aren't in SFINAE
20097 context. */
20098 tree unq = (tsubst_copy_and_build
20099 (function, args, complain, in_decl, true,
20100 integral_constant_expression_p));
20101 if (unq == error_mark_node)
20102 RETURN (error_mark_node);
20103
20104 if (unq != function)
20105 {
20106 /* In a lambda fn, we have to be careful to not
20107 introduce new this captures. Legacy code can't
20108 be using lambdas anyway, so it's ok to be
20109 stricter. */
20110 bool in_lambda = (current_class_type
20111 && LAMBDA_TYPE_P (current_class_type));
20112 char const *const msg
20113 = G_("%qD was not declared in this scope, "
20114 "and no declarations were found by "
20115 "argument-dependent lookup at the point "
20116 "of instantiation");
20117
20118 bool diag = true;
20119 if (in_lambda)
20120 error_at (cp_expr_loc_or_input_loc (t),
20121 msg, function);
20122 else
20123 diag = permerror (cp_expr_loc_or_input_loc (t),
20124 msg, function);
20125 if (diag)
20126 {
20127 tree fn = unq;
20128
20129 if (INDIRECT_REF_P (fn))
20130 fn = TREE_OPERAND (fn, 0);
20131 if (is_overloaded_fn (fn))
20132 fn = get_first_fn (fn);
20133
20134 if (!DECL_P (fn))
20135 /* Can't say anything more. */;
20136 else if (DECL_CLASS_SCOPE_P (fn))
20137 {
20138 location_t loc = cp_expr_loc_or_input_loc (t);
20139 inform (loc,
20140 "declarations in dependent base %qT are "
20141 "not found by unqualified lookup",
20142 DECL_CLASS_CONTEXT (fn));
20143 if (current_class_ptr)
20144 inform (loc,
20145 "use %<this->%D%> instead", function);
20146 else
20147 inform (loc,
20148 "use %<%T::%D%> instead",
20149 current_class_name, function);
20150 }
20151 else
20152 inform (DECL_SOURCE_LOCATION (fn),
20153 "%qD declared here, later in the "
20154 "translation unit", fn);
20155 if (in_lambda)
20156 RETURN (error_mark_node);
20157 }
20158
20159 function = unq;
20160 }
20161 }
20162 if (identifier_p (function))
20163 {
20164 if (complain & tf_error)
20165 unqualified_name_lookup_error (function);
20166 RETURN (error_mark_node);
20167 }
20168 }
20169
20170 /* Remember that there was a reference to this entity. */
20171 if (function != NULL_TREE
20172 && DECL_P (function)
20173 && !mark_used (function, complain) && !(complain & tf_error))
20174 RETURN (error_mark_node);
20175
20176 /* Put back tf_decltype for the actual call. */
20177 complain |= decltype_flag;
20178
20179 if (function == NULL_TREE)
20180 switch (CALL_EXPR_IFN (t))
20181 {
20182 case IFN_LAUNDER:
20183 gcc_assert (nargs == 1);
20184 if (vec_safe_length (call_args) != 1)
20185 {
20186 error_at (cp_expr_loc_or_input_loc (t),
20187 "wrong number of arguments to "
20188 "%<__builtin_launder%>");
20189 ret = error_mark_node;
20190 }
20191 else
20192 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20193 (*call_args)[0], complain);
20194 break;
20195
20196 case IFN_VEC_CONVERT:
20197 gcc_assert (nargs == 1);
20198 if (vec_safe_length (call_args) != 1)
20199 {
20200 error_at (cp_expr_loc_or_input_loc (t),
20201 "wrong number of arguments to "
20202 "%<__builtin_convertvector%>");
20203 ret = error_mark_node;
20204 break;
20205 }
20206 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20207 tsubst (TREE_TYPE (t), args,
20208 complain, in_decl),
20209 complain);
20210 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20211 RETURN (ret);
20212 break;
20213
20214 default:
20215 /* Unsupported internal function with arguments. */
20216 gcc_unreachable ();
20217 }
20218 else if (TREE_CODE (function) == OFFSET_REF
20219 || TREE_CODE (function) == DOTSTAR_EXPR
20220 || TREE_CODE (function) == MEMBER_REF)
20221 ret = build_offset_ref_call_from_tree (function, &call_args,
20222 complain);
20223 else if (TREE_CODE (function) == COMPONENT_REF)
20224 {
20225 tree instance = TREE_OPERAND (function, 0);
20226 tree fn = TREE_OPERAND (function, 1);
20227
20228 if (processing_template_decl
20229 && (type_dependent_expression_p (instance)
20230 || (!BASELINK_P (fn)
20231 && TREE_CODE (fn) != FIELD_DECL)
20232 || type_dependent_expression_p (fn)
20233 || any_type_dependent_arguments_p (call_args)))
20234 ret = build_min_nt_call_vec (function, call_args);
20235 else if (!BASELINK_P (fn))
20236 ret = finish_call_expr (function, &call_args,
20237 /*disallow_virtual=*/false,
20238 /*koenig_p=*/false,
20239 complain);
20240 else
20241 ret = (build_new_method_call
20242 (instance, fn,
20243 &call_args, NULL_TREE,
20244 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20245 /*fn_p=*/NULL,
20246 complain));
20247 }
20248 else if (concept_check_p (function))
20249 {
20250 /* FUNCTION is a template-id referring to a concept definition. */
20251 tree id = unpack_concept_check (function);
20252 tree tmpl = TREE_OPERAND (id, 0);
20253 tree args = TREE_OPERAND (id, 1);
20254
20255 /* Calls to standard and variable concepts should have been
20256 previously diagnosed. */
20257 gcc_assert (function_concept_p (tmpl));
20258
20259 /* Ensure the result is wrapped as a call expression. */
20260 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20261 }
20262 else
20263 ret = finish_call_expr (function, &call_args,
20264 /*disallow_virtual=*/qualified_p,
20265 koenig_p,
20266 complain);
20267
20268 if (ret != error_mark_node)
20269 {
20270 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20271 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20272 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20273 if (op || ord || rev)
20274 {
20275 function = extract_call_expr (ret);
20276 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20277 CALL_EXPR_ORDERED_ARGS (function) = ord;
20278 CALL_EXPR_REVERSE_ARGS (function) = rev;
20279 }
20280 }
20281
20282 RETURN (ret);
20283 }
20284
20285 case COND_EXPR:
20286 {
20287 tree cond = RECUR (TREE_OPERAND (t, 0));
20288 cond = mark_rvalue_use (cond);
20289 tree folded_cond = fold_non_dependent_expr (cond, complain);
20290 tree exp1, exp2;
20291
20292 if (TREE_CODE (folded_cond) == INTEGER_CST)
20293 {
20294 if (integer_zerop (folded_cond))
20295 {
20296 ++c_inhibit_evaluation_warnings;
20297 exp1 = RECUR (TREE_OPERAND (t, 1));
20298 --c_inhibit_evaluation_warnings;
20299 exp2 = RECUR (TREE_OPERAND (t, 2));
20300 }
20301 else
20302 {
20303 exp1 = RECUR (TREE_OPERAND (t, 1));
20304 ++c_inhibit_evaluation_warnings;
20305 exp2 = RECUR (TREE_OPERAND (t, 2));
20306 --c_inhibit_evaluation_warnings;
20307 }
20308 cond = folded_cond;
20309 }
20310 else
20311 {
20312 exp1 = RECUR (TREE_OPERAND (t, 1));
20313 exp2 = RECUR (TREE_OPERAND (t, 2));
20314 }
20315
20316 warning_sentinel s(warn_duplicated_branches);
20317 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20318 cond, exp1, exp2, complain));
20319 }
20320
20321 case PSEUDO_DTOR_EXPR:
20322 {
20323 tree op0 = RECUR (TREE_OPERAND (t, 0));
20324 tree op1 = RECUR (TREE_OPERAND (t, 1));
20325 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20326 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20327 input_location));
20328 }
20329
20330 case TREE_LIST:
20331 {
20332 tree purpose, value, chain;
20333
20334 if (t == void_list_node)
20335 RETURN (t);
20336
20337 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
20338 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
20339 {
20340 /* We have pack expansions, so expand those and
20341 create a new list out of it. */
20342 tree purposevec = NULL_TREE;
20343 tree valuevec = NULL_TREE;
20344 tree chain;
20345 int i, len = -1;
20346
20347 /* Expand the argument expressions. */
20348 if (TREE_PURPOSE (t))
20349 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
20350 complain, in_decl);
20351 if (TREE_VALUE (t))
20352 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
20353 complain, in_decl);
20354
20355 /* Build the rest of the list. */
20356 chain = TREE_CHAIN (t);
20357 if (chain && chain != void_type_node)
20358 chain = RECUR (chain);
20359
20360 /* Determine the number of arguments. */
20361 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
20362 {
20363 len = TREE_VEC_LENGTH (purposevec);
20364 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
20365 }
20366 else if (TREE_CODE (valuevec) == TREE_VEC)
20367 len = TREE_VEC_LENGTH (valuevec);
20368 else
20369 {
20370 /* Since we only performed a partial substitution into
20371 the argument pack, we only RETURN (a single list
20372 node. */
20373 if (purposevec == TREE_PURPOSE (t)
20374 && valuevec == TREE_VALUE (t)
20375 && chain == TREE_CHAIN (t))
20376 RETURN (t);
20377
20378 RETURN (tree_cons (purposevec, valuevec, chain));
20379 }
20380
20381 /* Convert the argument vectors into a TREE_LIST */
20382 i = len;
20383 while (i > 0)
20384 {
20385 /* Grab the Ith values. */
20386 i--;
20387 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
20388 : NULL_TREE;
20389 value
20390 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
20391 : NULL_TREE;
20392
20393 /* Build the list (backwards). */
20394 chain = tree_cons (purpose, value, chain);
20395 }
20396
20397 RETURN (chain);
20398 }
20399
20400 purpose = TREE_PURPOSE (t);
20401 if (purpose)
20402 purpose = RECUR (purpose);
20403 value = TREE_VALUE (t);
20404 if (value)
20405 value = RECUR (value);
20406 chain = TREE_CHAIN (t);
20407 if (chain && chain != void_type_node)
20408 chain = RECUR (chain);
20409 if (purpose == TREE_PURPOSE (t)
20410 && value == TREE_VALUE (t)
20411 && chain == TREE_CHAIN (t))
20412 RETURN (t);
20413 RETURN (tree_cons (purpose, value, chain));
20414 }
20415
20416 case COMPONENT_REF:
20417 {
20418 tree object;
20419 tree object_type;
20420 tree member;
20421 tree r;
20422
20423 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20424 args, complain, in_decl);
20425 /* Remember that there was a reference to this entity. */
20426 if (DECL_P (object)
20427 && !mark_used (object, complain) && !(complain & tf_error))
20428 RETURN (error_mark_node);
20429 object_type = TREE_TYPE (object);
20430
20431 member = TREE_OPERAND (t, 1);
20432 if (BASELINK_P (member))
20433 member = tsubst_baselink (member,
20434 non_reference (TREE_TYPE (object)),
20435 args, complain, in_decl);
20436 else
20437 member = tsubst_copy (member, args, complain, in_decl);
20438 if (member == error_mark_node)
20439 RETURN (error_mark_node);
20440
20441 if (TREE_CODE (member) == FIELD_DECL)
20442 {
20443 r = finish_non_static_data_member (member, object, NULL_TREE);
20444 if (TREE_CODE (r) == COMPONENT_REF)
20445 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20446 RETURN (r);
20447 }
20448 else if (type_dependent_expression_p (object))
20449 /* We can't do much here. */;
20450 else if (!CLASS_TYPE_P (object_type))
20451 {
20452 if (scalarish_type_p (object_type))
20453 {
20454 tree s = NULL_TREE;
20455 tree dtor = member;
20456
20457 if (TREE_CODE (dtor) == SCOPE_REF)
20458 {
20459 s = TREE_OPERAND (dtor, 0);
20460 dtor = TREE_OPERAND (dtor, 1);
20461 }
20462 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20463 {
20464 dtor = TREE_OPERAND (dtor, 0);
20465 if (TYPE_P (dtor))
20466 RETURN (finish_pseudo_destructor_expr
20467 (object, s, dtor, input_location));
20468 }
20469 }
20470 }
20471 else if (TREE_CODE (member) == SCOPE_REF
20472 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20473 {
20474 /* Lookup the template functions now that we know what the
20475 scope is. */
20476 tree scope = TREE_OPERAND (member, 0);
20477 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20478 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20479 member = lookup_qualified_name (scope, tmpl,
20480 /*is_type_p=*/false,
20481 /*complain=*/false);
20482 if (BASELINK_P (member))
20483 {
20484 BASELINK_FUNCTIONS (member)
20485 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20486 args);
20487 member = (adjust_result_of_qualified_name_lookup
20488 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20489 object_type));
20490 }
20491 else
20492 {
20493 qualified_name_lookup_error (scope, tmpl, member,
20494 input_location);
20495 RETURN (error_mark_node);
20496 }
20497 }
20498 else if (TREE_CODE (member) == SCOPE_REF
20499 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20500 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20501 {
20502 if (complain & tf_error)
20503 {
20504 if (TYPE_P (TREE_OPERAND (member, 0)))
20505 error ("%qT is not a class or namespace",
20506 TREE_OPERAND (member, 0));
20507 else
20508 error ("%qD is not a class or namespace",
20509 TREE_OPERAND (member, 0));
20510 }
20511 RETURN (error_mark_node);
20512 }
20513
20514 r = finish_class_member_access_expr (object, member,
20515 /*template_p=*/false,
20516 complain);
20517 if (TREE_CODE (r) == COMPONENT_REF)
20518 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20519 RETURN (r);
20520 }
20521
20522 case THROW_EXPR:
20523 RETURN (build_throw
20524 (input_location, RECUR (TREE_OPERAND (t, 0))));
20525
20526 case CONSTRUCTOR:
20527 {
20528 vec<constructor_elt, va_gc> *n;
20529 constructor_elt *ce;
20530 unsigned HOST_WIDE_INT idx;
20531 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20532 bool process_index_p;
20533 int newlen;
20534 bool need_copy_p = false;
20535 tree r;
20536
20537 if (type == error_mark_node)
20538 RETURN (error_mark_node);
20539
20540 /* We do not want to process the index of aggregate
20541 initializers as they are identifier nodes which will be
20542 looked up by digest_init. */
20543 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20544
20545 if (null_member_pointer_value_p (t))
20546 {
20547 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20548 RETURN (t);
20549 }
20550
20551 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20552 newlen = vec_safe_length (n);
20553 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20554 {
20555 if (ce->index && process_index_p
20556 /* An identifier index is looked up in the type
20557 being initialized, not the current scope. */
20558 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20559 ce->index = RECUR (ce->index);
20560
20561 if (PACK_EXPANSION_P (ce->value))
20562 {
20563 /* Substitute into the pack expansion. */
20564 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20565 in_decl);
20566
20567 if (ce->value == error_mark_node
20568 || PACK_EXPANSION_P (ce->value))
20569 ;
20570 else if (TREE_VEC_LENGTH (ce->value) == 1)
20571 /* Just move the argument into place. */
20572 ce->value = TREE_VEC_ELT (ce->value, 0);
20573 else
20574 {
20575 /* Update the length of the final CONSTRUCTOR
20576 arguments vector, and note that we will need to
20577 copy.*/
20578 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20579 need_copy_p = true;
20580 }
20581 }
20582 else
20583 ce->value = RECUR (ce->value);
20584 }
20585
20586 if (need_copy_p)
20587 {
20588 vec<constructor_elt, va_gc> *old_n = n;
20589
20590 vec_alloc (n, newlen);
20591 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20592 {
20593 if (TREE_CODE (ce->value) == TREE_VEC)
20594 {
20595 int i, len = TREE_VEC_LENGTH (ce->value);
20596 for (i = 0; i < len; ++i)
20597 CONSTRUCTOR_APPEND_ELT (n, 0,
20598 TREE_VEC_ELT (ce->value, i));
20599 }
20600 else
20601 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20602 }
20603 }
20604
20605 r = build_constructor (init_list_type_node, n);
20606 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20607 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20608 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20609
20610 if (TREE_HAS_CONSTRUCTOR (t))
20611 {
20612 fcl_t cl = fcl_functional;
20613 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20614 cl = fcl_c99;
20615 RETURN (finish_compound_literal (type, r, complain, cl));
20616 }
20617
20618 TREE_TYPE (r) = type;
20619 RETURN (r);
20620 }
20621
20622 case TYPEID_EXPR:
20623 {
20624 tree operand_0 = TREE_OPERAND (t, 0);
20625 if (TYPE_P (operand_0))
20626 {
20627 operand_0 = tsubst (operand_0, args, complain, in_decl);
20628 RETURN (get_typeid (operand_0, complain));
20629 }
20630 else
20631 {
20632 operand_0 = RECUR (operand_0);
20633 RETURN (build_typeid (operand_0, complain));
20634 }
20635 }
20636
20637 case VAR_DECL:
20638 if (!args)
20639 RETURN (t);
20640 /* Fall through */
20641
20642 case PARM_DECL:
20643 {
20644 tree r = tsubst_copy (t, args, complain, in_decl);
20645 /* ??? We're doing a subset of finish_id_expression here. */
20646 if (tree wrap = maybe_get_tls_wrapper_call (r))
20647 /* Replace an evaluated use of the thread_local variable with
20648 a call to its wrapper. */
20649 r = wrap;
20650 else if (outer_automatic_var_p (r))
20651 r = process_outer_var_ref (r, complain);
20652
20653 if (!TYPE_REF_P (TREE_TYPE (t)))
20654 /* If the original type was a reference, we'll be wrapped in
20655 the appropriate INDIRECT_REF. */
20656 r = convert_from_reference (r);
20657 RETURN (r);
20658 }
20659
20660 case VA_ARG_EXPR:
20661 {
20662 tree op0 = RECUR (TREE_OPERAND (t, 0));
20663 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20664 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20665 }
20666
20667 case OFFSETOF_EXPR:
20668 {
20669 tree object_ptr
20670 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20671 in_decl, /*function_p=*/false,
20672 /*integral_constant_expression_p=*/false);
20673 RETURN (finish_offsetof (object_ptr,
20674 RECUR (TREE_OPERAND (t, 0)),
20675 EXPR_LOCATION (t)));
20676 }
20677
20678 case ADDRESSOF_EXPR:
20679 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20680 RECUR (TREE_OPERAND (t, 0)), complain));
20681
20682 case TRAIT_EXPR:
20683 {
20684 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20685 complain, in_decl);
20686 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20687 complain, in_decl);
20688 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20689 TRAIT_EXPR_KIND (t), type1, type2));
20690 }
20691
20692 case STMT_EXPR:
20693 {
20694 tree old_stmt_expr = cur_stmt_expr;
20695 tree stmt_expr = begin_stmt_expr ();
20696
20697 cur_stmt_expr = stmt_expr;
20698 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20699 integral_constant_expression_p);
20700 stmt_expr = finish_stmt_expr (stmt_expr, false);
20701 cur_stmt_expr = old_stmt_expr;
20702
20703 /* If the resulting list of expression statement is empty,
20704 fold it further into void_node. */
20705 if (empty_expr_stmt_p (stmt_expr))
20706 stmt_expr = void_node;
20707
20708 RETURN (stmt_expr);
20709 }
20710
20711 case LAMBDA_EXPR:
20712 {
20713 if (complain & tf_partial)
20714 {
20715 /* We don't have a full set of template arguments yet; don't touch
20716 the lambda at all. */
20717 gcc_assert (processing_template_decl);
20718 return t;
20719 }
20720 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20721
20722 RETURN (build_lambda_object (r));
20723 }
20724
20725 case TARGET_EXPR:
20726 /* We can get here for a constant initializer of non-dependent type.
20727 FIXME stop folding in cp_parser_initializer_clause. */
20728 {
20729 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20730 complain);
20731 RETURN (r);
20732 }
20733
20734 case TRANSACTION_EXPR:
20735 RETURN (tsubst_expr(t, args, complain, in_decl,
20736 integral_constant_expression_p));
20737
20738 case PAREN_EXPR:
20739 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20740
20741 case VEC_PERM_EXPR:
20742 {
20743 tree op0 = RECUR (TREE_OPERAND (t, 0));
20744 tree op1 = RECUR (TREE_OPERAND (t, 1));
20745 tree op2 = RECUR (TREE_OPERAND (t, 2));
20746 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20747 complain));
20748 }
20749
20750 case REQUIRES_EXPR:
20751 {
20752 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20753 RETURN (r);
20754 }
20755
20756 case RANGE_EXPR:
20757 /* No need to substitute further, a RANGE_EXPR will always be built
20758 with constant operands. */
20759 RETURN (t);
20760
20761 case NON_LVALUE_EXPR:
20762 case VIEW_CONVERT_EXPR:
20763 if (location_wrapper_p (t))
20764 /* We need to do this here as well as in tsubst_copy so we get the
20765 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20766 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20767 EXPR_LOCATION (t)));
20768 /* fallthrough. */
20769
20770 default:
20771 /* Handle Objective-C++ constructs, if appropriate. */
20772 {
20773 tree subst
20774 = objcp_tsubst_copy_and_build (t, args, complain,
20775 in_decl, /*function_p=*/false);
20776 if (subst)
20777 RETURN (subst);
20778 }
20779 RETURN (tsubst_copy (t, args, complain, in_decl));
20780 }
20781
20782 #undef RECUR
20783 #undef RETURN
20784 out:
20785 input_location = save_loc;
20786 return retval;
20787 }
20788
20789 /* Verify that the instantiated ARGS are valid. For type arguments,
20790 make sure that the type's linkage is ok. For non-type arguments,
20791 make sure they are constants if they are integral or enumerations.
20792 Emit an error under control of COMPLAIN, and return TRUE on error. */
20793
20794 static bool
check_instantiated_arg(tree tmpl,tree t,tsubst_flags_t complain)20795 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20796 {
20797 if (dependent_template_arg_p (t))
20798 return false;
20799 if (ARGUMENT_PACK_P (t))
20800 {
20801 tree vec = ARGUMENT_PACK_ARGS (t);
20802 int len = TREE_VEC_LENGTH (vec);
20803 bool result = false;
20804 int i;
20805
20806 for (i = 0; i < len; ++i)
20807 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20808 result = true;
20809 return result;
20810 }
20811 else if (TYPE_P (t))
20812 {
20813 /* [basic.link]: A name with no linkage (notably, the name
20814 of a class or enumeration declared in a local scope)
20815 shall not be used to declare an entity with linkage.
20816 This implies that names with no linkage cannot be used as
20817 template arguments
20818
20819 DR 757 relaxes this restriction for C++0x. */
20820 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20821 : no_linkage_check (t, /*relaxed_p=*/false));
20822
20823 if (nt)
20824 {
20825 /* DR 488 makes use of a type with no linkage cause
20826 type deduction to fail. */
20827 if (complain & tf_error)
20828 {
20829 if (TYPE_UNNAMED_P (nt))
20830 error ("%qT is/uses unnamed type", t);
20831 else
20832 error ("template argument for %qD uses local type %qT",
20833 tmpl, t);
20834 }
20835 return true;
20836 }
20837 /* In order to avoid all sorts of complications, we do not
20838 allow variably-modified types as template arguments. */
20839 else if (variably_modified_type_p (t, NULL_TREE))
20840 {
20841 if (complain & tf_error)
20842 error ("%qT is a variably modified type", t);
20843 return true;
20844 }
20845 }
20846 /* Class template and alias template arguments should be OK. */
20847 else if (DECL_TYPE_TEMPLATE_P (t))
20848 ;
20849 /* A non-type argument of integral or enumerated type must be a
20850 constant. */
20851 else if (TREE_TYPE (t)
20852 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20853 && !REFERENCE_REF_P (t)
20854 && !TREE_CONSTANT (t))
20855 {
20856 if (complain & tf_error)
20857 error ("integral expression %qE is not constant", t);
20858 return true;
20859 }
20860 return false;
20861 }
20862
20863 static bool
check_instantiated_args(tree tmpl,tree args,tsubst_flags_t complain)20864 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20865 {
20866 int ix, len = DECL_NTPARMS (tmpl);
20867 bool result = false;
20868
20869 for (ix = 0; ix != len; ix++)
20870 {
20871 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20872 result = true;
20873 }
20874 if (result && (complain & tf_error))
20875 error (" trying to instantiate %qD", tmpl);
20876 return result;
20877 }
20878
20879 /* We're out of SFINAE context now, so generate diagnostics for the access
20880 errors we saw earlier when instantiating D from TMPL and ARGS. */
20881
20882 static void
recheck_decl_substitution(tree d,tree tmpl,tree args)20883 recheck_decl_substitution (tree d, tree tmpl, tree args)
20884 {
20885 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20886 tree type = TREE_TYPE (pattern);
20887 location_t loc = input_location;
20888
20889 push_access_scope (d);
20890 push_deferring_access_checks (dk_no_deferred);
20891 input_location = DECL_SOURCE_LOCATION (pattern);
20892 tsubst (type, args, tf_warning_or_error, d);
20893 input_location = loc;
20894 pop_deferring_access_checks ();
20895 pop_access_scope (d);
20896 }
20897
20898 /* Instantiate the indicated variable, function, or alias template TMPL with
20899 the template arguments in TARG_PTR. */
20900
20901 static tree
instantiate_template_1(tree tmpl,tree orig_args,tsubst_flags_t complain)20902 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20903 {
20904 tree targ_ptr = orig_args;
20905 tree fndecl;
20906 tree gen_tmpl;
20907 tree spec;
20908 bool access_ok = true;
20909
20910 if (tmpl == error_mark_node)
20911 return error_mark_node;
20912
20913 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20914
20915 /* If this function is a clone, handle it specially. */
20916 if (DECL_CLONED_FUNCTION_P (tmpl))
20917 {
20918 tree spec;
20919 tree clone;
20920
20921 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20922 DECL_CLONED_FUNCTION. */
20923 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20924 targ_ptr, complain);
20925 if (spec == error_mark_node)
20926 return error_mark_node;
20927
20928 /* Look for the clone. */
20929 FOR_EACH_CLONE (clone, spec)
20930 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20931 return clone;
20932 /* We should always have found the clone by now. */
20933 gcc_unreachable ();
20934 return NULL_TREE;
20935 }
20936
20937 if (targ_ptr == error_mark_node)
20938 return error_mark_node;
20939
20940 /* Check to see if we already have this specialization. */
20941 gen_tmpl = most_general_template (tmpl);
20942 if (TMPL_ARGS_DEPTH (targ_ptr)
20943 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20944 /* targ_ptr only has the innermost template args, so add the outer ones
20945 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20946 the case of a non-dependent call within a template definition). */
20947 targ_ptr = (add_outermost_template_args
20948 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20949 targ_ptr));
20950
20951 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20952 but it doesn't seem to be on the hot path. */
20953 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20954
20955 gcc_assert (tmpl == gen_tmpl
20956 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
20957 == spec)
20958 || fndecl == NULL_TREE);
20959
20960 if (spec != NULL_TREE)
20961 {
20962 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20963 {
20964 if (complain & tf_error)
20965 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20966 return error_mark_node;
20967 }
20968 return spec;
20969 }
20970
20971 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20972 complain))
20973 return error_mark_node;
20974
20975 /* We are building a FUNCTION_DECL, during which the access of its
20976 parameters and return types have to be checked. However this
20977 FUNCTION_DECL which is the desired context for access checking
20978 is not built yet. We solve this chicken-and-egg problem by
20979 deferring all checks until we have the FUNCTION_DECL. */
20980 push_deferring_access_checks (dk_deferred);
20981
20982 /* Instantiation of the function happens in the context of the function
20983 template, not the context of the overload resolution we're doing. */
20984 push_to_top_level ();
20985 /* If there are dependent arguments, e.g. because we're doing partial
20986 ordering, make sure processing_template_decl stays set. */
20987 if (uses_template_parms (targ_ptr))
20988 ++processing_template_decl;
20989 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20990 {
20991 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20992 complain, gen_tmpl, true);
20993 push_nested_class (ctx);
20994 }
20995
20996 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20997
20998 fndecl = NULL_TREE;
20999 if (VAR_P (pattern))
21000 {
21001 /* We need to determine if we're using a partial or explicit
21002 specialization now, because the type of the variable could be
21003 different. */
21004 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
21005 tree elt = most_specialized_partial_spec (tid, complain);
21006 if (elt == error_mark_node)
21007 pattern = error_mark_node;
21008 else if (elt)
21009 {
21010 tree partial_tmpl = TREE_VALUE (elt);
21011 tree partial_args = TREE_PURPOSE (elt);
21012 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
21013 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
21014 }
21015 }
21016
21017 /* Substitute template parameters to obtain the specialization. */
21018 if (fndecl == NULL_TREE)
21019 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
21020 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21021 pop_nested_class ();
21022 pop_from_top_level ();
21023
21024 if (fndecl == error_mark_node)
21025 {
21026 pop_deferring_access_checks ();
21027 return error_mark_node;
21028 }
21029
21030 /* The DECL_TI_TEMPLATE should always be the immediate parent
21031 template, not the most general template. */
21032 DECL_TI_TEMPLATE (fndecl) = tmpl;
21033 DECL_TI_ARGS (fndecl) = targ_ptr;
21034
21035 /* Now we know the specialization, compute access previously
21036 deferred. Do no access control for inheriting constructors,
21037 as we already checked access for the inherited constructor. */
21038 if (!(flag_new_inheriting_ctors
21039 && DECL_INHERITED_CTOR (fndecl)))
21040 {
21041 push_access_scope (fndecl);
21042 if (!perform_deferred_access_checks (complain))
21043 access_ok = false;
21044 pop_access_scope (fndecl);
21045 }
21046 pop_deferring_access_checks ();
21047
21048 /* If we've just instantiated the main entry point for a function,
21049 instantiate all the alternate entry points as well. We do this
21050 by cloning the instantiation of the main entry point, not by
21051 instantiating the template clones. */
21052 if (tree chain = DECL_CHAIN (gen_tmpl))
21053 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
21054 clone_function_decl (fndecl, /*update_methods=*/false);
21055
21056 if (!access_ok)
21057 {
21058 if (!(complain & tf_error))
21059 {
21060 /* Remember to reinstantiate when we're out of SFINAE so the user
21061 can see the errors. */
21062 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
21063 }
21064 return error_mark_node;
21065 }
21066 return fndecl;
21067 }
21068
21069 /* Wrapper for instantiate_template_1. */
21070
21071 tree
instantiate_template(tree tmpl,tree orig_args,tsubst_flags_t complain)21072 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
21073 {
21074 tree ret;
21075 timevar_push (TV_TEMPLATE_INST);
21076 ret = instantiate_template_1 (tmpl, orig_args, complain);
21077 timevar_pop (TV_TEMPLATE_INST);
21078 return ret;
21079 }
21080
21081 /* Instantiate the alias template TMPL with ARGS. Also push a template
21082 instantiation level, which instantiate_template doesn't do because
21083 functions and variables have sufficient context established by the
21084 callers. */
21085
21086 static tree
instantiate_alias_template(tree tmpl,tree args,tsubst_flags_t complain)21087 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
21088 {
21089 if (tmpl == error_mark_node || args == error_mark_node)
21090 return error_mark_node;
21091
21092 args =
21093 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
21094 args, tmpl, complain,
21095 /*require_all_args=*/true,
21096 /*use_default_args=*/true);
21097
21098 /* FIXME check for satisfaction in check_instantiated_args. */
21099 if (flag_concepts
21100 && !any_dependent_template_arguments_p (args)
21101 && !constraints_satisfied_p (tmpl, args))
21102 {
21103 if (complain & tf_error)
21104 {
21105 auto_diagnostic_group d;
21106 error ("template constraint failure for %qD", tmpl);
21107 diagnose_constraints (input_location, tmpl, args);
21108 }
21109 return error_mark_node;
21110 }
21111
21112 if (!push_tinst_level (tmpl, args))
21113 return error_mark_node;
21114 tree r = instantiate_template (tmpl, args, complain);
21115 pop_tinst_level ();
21116
21117 return r;
21118 }
21119
21120 /* PARM is a template parameter pack for FN. Returns true iff
21121 PARM is used in a deducible way in the argument list of FN. */
21122
21123 static bool
pack_deducible_p(tree parm,tree fn)21124 pack_deducible_p (tree parm, tree fn)
21125 {
21126 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
21127 for (; t; t = TREE_CHAIN (t))
21128 {
21129 tree type = TREE_VALUE (t);
21130 tree packs;
21131 if (!PACK_EXPANSION_P (type))
21132 continue;
21133 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
21134 packs; packs = TREE_CHAIN (packs))
21135 if (template_args_equal (TREE_VALUE (packs), parm))
21136 {
21137 /* The template parameter pack is used in a function parameter
21138 pack. If this is the end of the parameter list, the
21139 template parameter pack is deducible. */
21140 if (TREE_CHAIN (t) == void_list_node)
21141 return true;
21142 else
21143 /* Otherwise, not. Well, it could be deduced from
21144 a non-pack parameter, but doing so would end up with
21145 a deduction mismatch, so don't bother. */
21146 return false;
21147 }
21148 }
21149 /* The template parameter pack isn't used in any function parameter
21150 packs, but it might be used deeper, e.g. tuple<Args...>. */
21151 return true;
21152 }
21153
21154 /* Subroutine of fn_type_unification: check non-dependent parms for
21155 convertibility. */
21156
21157 static int
check_non_deducible_conversions(tree parms,const tree * args,unsigned nargs,tree fn,unification_kind_t strict,int flags,struct conversion ** convs,bool explain_p)21158 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21159 tree fn, unification_kind_t strict, int flags,
21160 struct conversion **convs, bool explain_p)
21161 {
21162 /* Non-constructor methods need to leave a conversion for 'this', which
21163 isn't included in nargs here. */
21164 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21165 && !DECL_CONSTRUCTOR_P (fn));
21166
21167 for (unsigned ia = 0;
21168 parms && parms != void_list_node && ia < nargs; )
21169 {
21170 tree parm = TREE_VALUE (parms);
21171
21172 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21173 && (!TREE_CHAIN (parms)
21174 || TREE_CHAIN (parms) == void_list_node))
21175 /* For a function parameter pack that occurs at the end of the
21176 parameter-declaration-list, the type A of each remaining
21177 argument of the call is compared with the type P of the
21178 declarator-id of the function parameter pack. */
21179 break;
21180
21181 parms = TREE_CHAIN (parms);
21182
21183 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21184 /* For a function parameter pack that does not occur at the
21185 end of the parameter-declaration-list, the type of the
21186 parameter pack is a non-deduced context. */
21187 continue;
21188
21189 if (!uses_template_parms (parm))
21190 {
21191 tree arg = args[ia];
21192 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21193 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21194
21195 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21196 conv_p, explain_p))
21197 return 1;
21198 }
21199
21200 ++ia;
21201 }
21202
21203 return 0;
21204 }
21205
21206 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21207 NARGS elements of the arguments that are being used when calling
21208 it. TARGS is a vector into which the deduced template arguments
21209 are placed.
21210
21211 Returns either a FUNCTION_DECL for the matching specialization of FN or
21212 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21213 true, diagnostics will be printed to explain why it failed.
21214
21215 If FN is a conversion operator, or we are trying to produce a specific
21216 specialization, RETURN_TYPE is the return type desired.
21217
21218 The EXPLICIT_TARGS are explicit template arguments provided via a
21219 template-id.
21220
21221 The parameter STRICT is one of:
21222
21223 DEDUCE_CALL:
21224 We are deducing arguments for a function call, as in
21225 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21226 deducing arguments for a call to the result of a conversion
21227 function template, as in [over.call.object].
21228
21229 DEDUCE_CONV:
21230 We are deducing arguments for a conversion function, as in
21231 [temp.deduct.conv].
21232
21233 DEDUCE_EXACT:
21234 We are deducing arguments when doing an explicit instantiation
21235 as in [temp.explicit], when determining an explicit specialization
21236 as in [temp.expl.spec], or when taking the address of a function
21237 template, as in [temp.deduct.funcaddr]. */
21238
21239 tree
fn_type_unification(tree fn,tree explicit_targs,tree targs,const tree * args,unsigned int nargs,tree return_type,unification_kind_t strict,int flags,struct conversion ** convs,bool explain_p,bool decltype_p)21240 fn_type_unification (tree fn,
21241 tree explicit_targs,
21242 tree targs,
21243 const tree *args,
21244 unsigned int nargs,
21245 tree return_type,
21246 unification_kind_t strict,
21247 int flags,
21248 struct conversion **convs,
21249 bool explain_p,
21250 bool decltype_p)
21251 {
21252 tree parms;
21253 tree fntype;
21254 tree decl = NULL_TREE;
21255 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21256 bool ok;
21257 static int deduction_depth;
21258 /* type_unification_real will pass back any access checks from default
21259 template argument substitution. */
21260 vec<deferred_access_check, va_gc> *checks = NULL;
21261 /* We don't have all the template args yet. */
21262 bool incomplete = true;
21263
21264 tree orig_fn = fn;
21265 if (flag_new_inheriting_ctors)
21266 fn = strip_inheriting_ctors (fn);
21267
21268 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21269 tree r = error_mark_node;
21270
21271 tree full_targs = targs;
21272 if (TMPL_ARGS_DEPTH (targs)
21273 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21274 full_targs = (add_outermost_template_args
21275 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21276 targs));
21277
21278 if (decltype_p)
21279 complain |= tf_decltype;
21280
21281 /* In C++0x, it's possible to have a function template whose type depends
21282 on itself recursively. This is most obvious with decltype, but can also
21283 occur with enumeration scope (c++/48969). So we need to catch infinite
21284 recursion and reject the substitution at deduction time; this function
21285 will return error_mark_node for any repeated substitution.
21286
21287 This also catches excessive recursion such as when f<N> depends on
21288 f<N-1> across all integers, and returns error_mark_node for all the
21289 substitutions back up to the initial one.
21290
21291 This is, of course, not reentrant. */
21292 if (excessive_deduction_depth)
21293 return error_mark_node;
21294 ++deduction_depth;
21295
21296 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21297
21298 fntype = TREE_TYPE (fn);
21299 if (explicit_targs)
21300 {
21301 /* [temp.deduct]
21302
21303 The specified template arguments must match the template
21304 parameters in kind (i.e., type, nontype, template), and there
21305 must not be more arguments than there are parameters;
21306 otherwise type deduction fails.
21307
21308 Nontype arguments must match the types of the corresponding
21309 nontype template parameters, or must be convertible to the
21310 types of the corresponding nontype parameters as specified in
21311 _temp.arg.nontype_, otherwise type deduction fails.
21312
21313 All references in the function type of the function template
21314 to the corresponding template parameters are replaced by the
21315 specified template argument values. If a substitution in a
21316 template parameter or in the function type of the function
21317 template results in an invalid type, type deduction fails. */
21318 int i, len = TREE_VEC_LENGTH (tparms);
21319 location_t loc = input_location;
21320 incomplete = false;
21321
21322 if (explicit_targs == error_mark_node)
21323 goto fail;
21324
21325 if (TMPL_ARGS_DEPTH (explicit_targs)
21326 < TMPL_ARGS_DEPTH (full_targs))
21327 explicit_targs = add_outermost_template_args (full_targs,
21328 explicit_targs);
21329
21330 /* Adjust any explicit template arguments before entering the
21331 substitution context. */
21332 explicit_targs
21333 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
21334 complain|tf_partial,
21335 /*require_all_args=*/false,
21336 /*use_default_args=*/false));
21337 if (explicit_targs == error_mark_node)
21338 goto fail;
21339
21340 /* Substitute the explicit args into the function type. This is
21341 necessary so that, for instance, explicitly declared function
21342 arguments can match null pointed constants. If we were given
21343 an incomplete set of explicit args, we must not do semantic
21344 processing during substitution as we could create partial
21345 instantiations. */
21346 for (i = 0; i < len; i++)
21347 {
21348 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21349 bool parameter_pack = false;
21350 tree targ = TREE_VEC_ELT (explicit_targs, i);
21351
21352 /* Dig out the actual parm. */
21353 if (TREE_CODE (parm) == TYPE_DECL
21354 || TREE_CODE (parm) == TEMPLATE_DECL)
21355 {
21356 parm = TREE_TYPE (parm);
21357 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21358 }
21359 else if (TREE_CODE (parm) == PARM_DECL)
21360 {
21361 parm = DECL_INITIAL (parm);
21362 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21363 }
21364
21365 if (targ == NULL_TREE)
21366 /* No explicit argument for this template parameter. */
21367 incomplete = true;
21368 else if (parameter_pack && pack_deducible_p (parm, fn))
21369 {
21370 /* Mark the argument pack as "incomplete". We could
21371 still deduce more arguments during unification.
21372 We remove this mark in type_unification_real. */
21373 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21374 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21375 = ARGUMENT_PACK_ARGS (targ);
21376
21377 /* We have some incomplete argument packs. */
21378 incomplete = true;
21379 }
21380 }
21381
21382 if (incomplete)
21383 {
21384 if (!push_tinst_level (fn, explicit_targs))
21385 {
21386 excessive_deduction_depth = true;
21387 goto fail;
21388 }
21389 ++processing_template_decl;
21390 input_location = DECL_SOURCE_LOCATION (fn);
21391 /* Ignore any access checks; we'll see them again in
21392 instantiate_template and they might have the wrong
21393 access path at this point. */
21394 push_deferring_access_checks (dk_deferred);
21395 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21396 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21397 pop_deferring_access_checks ();
21398 input_location = loc;
21399 --processing_template_decl;
21400 pop_tinst_level ();
21401
21402 if (fntype == error_mark_node)
21403 goto fail;
21404 }
21405
21406 /* Place the explicitly specified arguments in TARGS. */
21407 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21408 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21409 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21410 if (!incomplete && CHECKING_P
21411 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21412 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21413 (targs, NUM_TMPL_ARGS (explicit_targs));
21414 }
21415
21416 if (return_type && strict != DEDUCE_CALL)
21417 {
21418 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21419 new_args[0] = return_type;
21420 memcpy (new_args + 1, args, nargs * sizeof (tree));
21421 args = new_args;
21422 ++nargs;
21423 }
21424
21425 if (!incomplete)
21426 goto deduced;
21427
21428 /* Never do unification on the 'this' parameter. */
21429 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21430
21431 if (return_type && strict == DEDUCE_CALL)
21432 {
21433 /* We're deducing for a call to the result of a template conversion
21434 function. The parms we really want are in return_type. */
21435 if (INDIRECT_TYPE_P (return_type))
21436 return_type = TREE_TYPE (return_type);
21437 parms = TYPE_ARG_TYPES (return_type);
21438 }
21439 else if (return_type)
21440 {
21441 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21442 }
21443
21444 /* We allow incomplete unification without an error message here
21445 because the standard doesn't seem to explicitly prohibit it. Our
21446 callers must be ready to deal with unification failures in any
21447 event. */
21448
21449 /* If we aren't explaining yet, push tinst context so we can see where
21450 any errors (e.g. from class instantiations triggered by instantiation
21451 of default template arguments) come from. If we are explaining, this
21452 context is redundant. */
21453 if (!explain_p && !push_tinst_level (fn, targs))
21454 {
21455 excessive_deduction_depth = true;
21456 goto fail;
21457 }
21458
21459 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21460 full_targs, parms, args, nargs, /*subr=*/0,
21461 strict, &checks, explain_p);
21462 if (!explain_p)
21463 pop_tinst_level ();
21464 if (!ok)
21465 goto fail;
21466
21467 /* Now that we have bindings for all of the template arguments,
21468 ensure that the arguments deduced for the template template
21469 parameters have compatible template parameter lists. We cannot
21470 check this property before we have deduced all template
21471 arguments, because the template parameter types of a template
21472 template parameter might depend on prior template parameters
21473 deduced after the template template parameter. The following
21474 ill-formed example illustrates this issue:
21475
21476 template<typename T, template<T> class C> void f(C<5>, T);
21477
21478 template<int N> struct X {};
21479
21480 void g() {
21481 f(X<5>(), 5l); // error: template argument deduction fails
21482 }
21483
21484 The template parameter list of 'C' depends on the template type
21485 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21486 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21487 time that we deduce 'C'. */
21488 if (!template_template_parm_bindings_ok_p
21489 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21490 {
21491 unify_inconsistent_template_template_parameters (explain_p);
21492 goto fail;
21493 }
21494
21495 /* DR 1391: All parameters have args, now check non-dependent parms for
21496 convertibility. */
21497 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21498 convs, explain_p))
21499 goto fail;
21500
21501 deduced:
21502 /* All is well so far. Now, check:
21503
21504 [temp.deduct]
21505
21506 When all template arguments have been deduced, all uses of
21507 template parameters in nondeduced contexts are replaced with
21508 the corresponding deduced argument values. If the
21509 substitution results in an invalid type, as described above,
21510 type deduction fails. */
21511 if (!push_tinst_level (fn, targs))
21512 {
21513 excessive_deduction_depth = true;
21514 goto fail;
21515 }
21516
21517 /* Also collect access checks from the instantiation. */
21518 reopen_deferring_access_checks (checks);
21519
21520 decl = instantiate_template (fn, targs, complain);
21521
21522 checks = get_deferred_access_checks ();
21523 pop_deferring_access_checks ();
21524
21525 pop_tinst_level ();
21526
21527 if (decl == error_mark_node)
21528 goto fail;
21529
21530 /* Now perform any access checks encountered during substitution. */
21531 push_access_scope (decl);
21532 ok = perform_access_checks (checks, complain);
21533 pop_access_scope (decl);
21534 if (!ok)
21535 goto fail;
21536
21537 /* If we're looking for an exact match, check that what we got
21538 is indeed an exact match. It might not be if some template
21539 parameters are used in non-deduced contexts. But don't check
21540 for an exact match if we have dependent template arguments;
21541 in that case we're doing partial ordering, and we already know
21542 that we have two candidates that will provide the actual type. */
21543 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21544 {
21545 tree substed = TREE_TYPE (decl);
21546 unsigned int i;
21547
21548 tree sarg
21549 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21550 if (return_type)
21551 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21552 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21553 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21554 {
21555 unify_type_mismatch (explain_p, args[i],
21556 TREE_VALUE (sarg));
21557 goto fail;
21558 }
21559 }
21560
21561 /* After doing deduction with the inherited constructor, actually return an
21562 instantiation of the inheriting constructor. */
21563 if (orig_fn != fn)
21564 decl = instantiate_template (orig_fn, targs, complain);
21565
21566 r = decl;
21567
21568 fail:
21569 --deduction_depth;
21570 if (excessive_deduction_depth)
21571 {
21572 if (deduction_depth == 0)
21573 /* Reset once we're all the way out. */
21574 excessive_deduction_depth = false;
21575 }
21576
21577 return r;
21578 }
21579
21580 /* Adjust types before performing type deduction, as described in
21581 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21582 sections are symmetric. PARM is the type of a function parameter
21583 or the return type of the conversion function. ARG is the type of
21584 the argument passed to the call, or the type of the value
21585 initialized with the result of the conversion function.
21586 ARG_EXPR is the original argument expression, which may be null. */
21587
21588 static int
maybe_adjust_types_for_deduction(unification_kind_t strict,tree * parm,tree * arg,tree arg_expr)21589 maybe_adjust_types_for_deduction (unification_kind_t strict,
21590 tree* parm,
21591 tree* arg,
21592 tree arg_expr)
21593 {
21594 int result = 0;
21595
21596 switch (strict)
21597 {
21598 case DEDUCE_CALL:
21599 break;
21600
21601 case DEDUCE_CONV:
21602 /* Swap PARM and ARG throughout the remainder of this
21603 function; the handling is precisely symmetric since PARM
21604 will initialize ARG rather than vice versa. */
21605 std::swap (parm, arg);
21606 break;
21607
21608 case DEDUCE_EXACT:
21609 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21610 too, but here handle it by stripping the reference from PARM
21611 rather than by adding it to ARG. */
21612 if (TYPE_REF_P (*parm)
21613 && TYPE_REF_IS_RVALUE (*parm)
21614 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21615 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21616 && TYPE_REF_P (*arg)
21617 && !TYPE_REF_IS_RVALUE (*arg))
21618 *parm = TREE_TYPE (*parm);
21619 /* Nothing else to do in this case. */
21620 return 0;
21621
21622 default:
21623 gcc_unreachable ();
21624 }
21625
21626 if (!TYPE_REF_P (*parm))
21627 {
21628 /* [temp.deduct.call]
21629
21630 If P is not a reference type:
21631
21632 --If A is an array type, the pointer type produced by the
21633 array-to-pointer standard conversion (_conv.array_) is
21634 used in place of A for type deduction; otherwise,
21635
21636 --If A is a function type, the pointer type produced by
21637 the function-to-pointer standard conversion
21638 (_conv.func_) is used in place of A for type deduction;
21639 otherwise,
21640
21641 --If A is a cv-qualified type, the top level
21642 cv-qualifiers of A's type are ignored for type
21643 deduction. */
21644 if (TREE_CODE (*arg) == ARRAY_TYPE)
21645 *arg = build_pointer_type (TREE_TYPE (*arg));
21646 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21647 *arg = build_pointer_type (*arg);
21648 else
21649 *arg = TYPE_MAIN_VARIANT (*arg);
21650 }
21651
21652 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21653 reference to a cv-unqualified template parameter that does not represent a
21654 template parameter of a class template (during class template argument
21655 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21656 an lvalue, the type "lvalue reference to A" is used in place of A for type
21657 deduction. */
21658 if (TYPE_REF_P (*parm)
21659 && TYPE_REF_IS_RVALUE (*parm)
21660 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21661 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21662 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21663 && (arg_expr ? lvalue_p (arg_expr)
21664 /* try_one_overload doesn't provide an arg_expr, but
21665 functions are always lvalues. */
21666 : TREE_CODE (*arg) == FUNCTION_TYPE))
21667 *arg = build_reference_type (*arg);
21668
21669 /* [temp.deduct.call]
21670
21671 If P is a cv-qualified type, the top level cv-qualifiers
21672 of P's type are ignored for type deduction. If P is a
21673 reference type, the type referred to by P is used for
21674 type deduction. */
21675 *parm = TYPE_MAIN_VARIANT (*parm);
21676 if (TYPE_REF_P (*parm))
21677 {
21678 *parm = TREE_TYPE (*parm);
21679 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21680 }
21681
21682 /* DR 322. For conversion deduction, remove a reference type on parm
21683 too (which has been swapped into ARG). */
21684 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21685 *arg = TREE_TYPE (*arg);
21686
21687 return result;
21688 }
21689
21690 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21691 template which doesn't contain any deducible template parameters; check if
21692 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21693 unify_one_argument. */
21694
21695 static int
check_non_deducible_conversion(tree parm,tree arg,int strict,int flags,struct conversion ** conv_p,bool explain_p)21696 check_non_deducible_conversion (tree parm, tree arg, int strict,
21697 int flags, struct conversion **conv_p,
21698 bool explain_p)
21699 {
21700 tree type;
21701
21702 if (!TYPE_P (arg))
21703 type = TREE_TYPE (arg);
21704 else
21705 type = arg;
21706
21707 if (same_type_p (parm, type))
21708 return unify_success (explain_p);
21709
21710 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21711 if (strict == DEDUCE_CONV)
21712 {
21713 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21714 return unify_success (explain_p);
21715 }
21716 else if (strict != DEDUCE_EXACT)
21717 {
21718 bool ok = false;
21719 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21720 if (conv_p)
21721 /* Avoid recalculating this in add_function_candidate. */
21722 ok = (*conv_p
21723 = good_conversion (parm, type, conv_arg, flags, complain));
21724 else
21725 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21726 if (ok)
21727 return unify_success (explain_p);
21728 }
21729
21730 if (strict == DEDUCE_EXACT)
21731 return unify_type_mismatch (explain_p, parm, arg);
21732 else
21733 return unify_arg_conversion (explain_p, parm, type, arg);
21734 }
21735
21736 static bool uses_deducible_template_parms (tree type);
21737
21738 /* Returns true iff the expression EXPR is one from which a template
21739 argument can be deduced. In other words, if it's an undecorated
21740 use of a template non-type parameter. */
21741
21742 static bool
deducible_expression(tree expr)21743 deducible_expression (tree expr)
21744 {
21745 /* Strip implicit conversions and implicit INDIRECT_REFs. */
21746 while (CONVERT_EXPR_P (expr)
21747 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
21748 || REFERENCE_REF_P (expr))
21749 expr = TREE_OPERAND (expr, 0);
21750 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21751 }
21752
21753 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21754 deducible way; that is, if it has a max value of <PARM> - 1. */
21755
21756 static bool
deducible_array_bound(tree domain)21757 deducible_array_bound (tree domain)
21758 {
21759 if (domain == NULL_TREE)
21760 return false;
21761
21762 tree max = TYPE_MAX_VALUE (domain);
21763 if (TREE_CODE (max) != MINUS_EXPR)
21764 return false;
21765
21766 return deducible_expression (TREE_OPERAND (max, 0));
21767 }
21768
21769 /* Returns true iff the template arguments ARGS use a template parameter
21770 in a deducible way. */
21771
21772 static bool
deducible_template_args(tree args)21773 deducible_template_args (tree args)
21774 {
21775 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21776 {
21777 bool deducible;
21778 tree elt = TREE_VEC_ELT (args, i);
21779 if (ARGUMENT_PACK_P (elt))
21780 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21781 else
21782 {
21783 if (PACK_EXPANSION_P (elt))
21784 elt = PACK_EXPANSION_PATTERN (elt);
21785 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21786 deducible = true;
21787 else if (TYPE_P (elt))
21788 deducible = uses_deducible_template_parms (elt);
21789 else
21790 deducible = deducible_expression (elt);
21791 }
21792 if (deducible)
21793 return true;
21794 }
21795 return false;
21796 }
21797
21798 /* Returns true iff TYPE contains any deducible references to template
21799 parameters, as per 14.8.2.5. */
21800
21801 static bool
uses_deducible_template_parms(tree type)21802 uses_deducible_template_parms (tree type)
21803 {
21804 if (PACK_EXPANSION_P (type))
21805 type = PACK_EXPANSION_PATTERN (type);
21806
21807 /* T
21808 cv-list T
21809 TT<T>
21810 TT<i>
21811 TT<> */
21812 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21813 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21814 return true;
21815
21816 /* T*
21817 T&
21818 T&& */
21819 if (INDIRECT_TYPE_P (type))
21820 return uses_deducible_template_parms (TREE_TYPE (type));
21821
21822 /* T[integer-constant ]
21823 type [i] */
21824 if (TREE_CODE (type) == ARRAY_TYPE)
21825 return (uses_deducible_template_parms (TREE_TYPE (type))
21826 || deducible_array_bound (TYPE_DOMAIN (type)));
21827
21828 /* T type ::*
21829 type T::*
21830 T T::*
21831 T (type ::*)()
21832 type (T::*)()
21833 type (type ::*)(T)
21834 type (T::*)(T)
21835 T (type ::*)(T)
21836 T (T::*)()
21837 T (T::*)(T) */
21838 if (TYPE_PTRMEM_P (type))
21839 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21840 || (uses_deducible_template_parms
21841 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21842
21843 /* template-name <T> (where template-name refers to a class template)
21844 template-name <i> (where template-name refers to a class template) */
21845 if (CLASS_TYPE_P (type)
21846 && CLASSTYPE_TEMPLATE_INFO (type)
21847 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21848 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21849 (CLASSTYPE_TI_ARGS (type)));
21850
21851 /* type (T)
21852 T()
21853 T(T) */
21854 if (FUNC_OR_METHOD_TYPE_P (type))
21855 {
21856 if (uses_deducible_template_parms (TREE_TYPE (type)))
21857 return true;
21858 tree parm = TYPE_ARG_TYPES (type);
21859 if (TREE_CODE (type) == METHOD_TYPE)
21860 parm = TREE_CHAIN (parm);
21861 for (; parm; parm = TREE_CHAIN (parm))
21862 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21863 return true;
21864 }
21865
21866 return false;
21867 }
21868
21869 /* Subroutine of type_unification_real and unify_pack_expansion to
21870 handle unification of a single P/A pair. Parameters are as
21871 for those functions. */
21872
21873 static int
unify_one_argument(tree tparms,tree targs,tree parm,tree arg,int subr,unification_kind_t strict,bool explain_p)21874 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21875 int subr, unification_kind_t strict,
21876 bool explain_p)
21877 {
21878 tree arg_expr = NULL_TREE;
21879 int arg_strict;
21880
21881 if (arg == error_mark_node || parm == error_mark_node)
21882 return unify_invalid (explain_p);
21883 if (arg == unknown_type_node)
21884 /* We can't deduce anything from this, but we might get all the
21885 template args from other function args. */
21886 return unify_success (explain_p);
21887
21888 /* Implicit conversions (Clause 4) will be performed on a function
21889 argument to convert it to the type of the corresponding function
21890 parameter if the parameter type contains no template-parameters that
21891 participate in template argument deduction. */
21892 if (strict != DEDUCE_EXACT
21893 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21894 /* For function parameters with no deducible template parameters,
21895 just return. We'll check non-dependent conversions later. */
21896 return unify_success (explain_p);
21897
21898 switch (strict)
21899 {
21900 case DEDUCE_CALL:
21901 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21902 | UNIFY_ALLOW_MORE_CV_QUAL
21903 | UNIFY_ALLOW_DERIVED);
21904 break;
21905
21906 case DEDUCE_CONV:
21907 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21908 break;
21909
21910 case DEDUCE_EXACT:
21911 arg_strict = UNIFY_ALLOW_NONE;
21912 break;
21913
21914 default:
21915 gcc_unreachable ();
21916 }
21917
21918 /* We only do these transformations if this is the top-level
21919 parameter_type_list in a call or declaration matching; in other
21920 situations (nested function declarators, template argument lists) we
21921 won't be comparing a type to an expression, and we don't do any type
21922 adjustments. */
21923 if (!subr)
21924 {
21925 if (!TYPE_P (arg))
21926 {
21927 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21928 if (type_unknown_p (arg))
21929 {
21930 /* [temp.deduct.type] A template-argument can be
21931 deduced from a pointer to function or pointer
21932 to member function argument if the set of
21933 overloaded functions does not contain function
21934 templates and at most one of a set of
21935 overloaded functions provides a unique
21936 match. */
21937 resolve_overloaded_unification (tparms, targs, parm,
21938 arg, strict,
21939 arg_strict, explain_p);
21940 /* If a unique match was not found, this is a
21941 non-deduced context, so we still succeed. */
21942 return unify_success (explain_p);
21943 }
21944
21945 arg_expr = arg;
21946 arg = unlowered_expr_type (arg);
21947 if (arg == error_mark_node)
21948 return unify_invalid (explain_p);
21949 }
21950
21951 arg_strict |=
21952 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21953 }
21954 else
21955 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21956 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21957 return unify_template_argument_mismatch (explain_p, parm, arg);
21958
21959 /* For deduction from an init-list we need the actual list. */
21960 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21961 arg = arg_expr;
21962 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21963 }
21964
21965 /* for_each_template_parm callback that always returns 0. */
21966
21967 static int
zero_r(tree,void *)21968 zero_r (tree, void *)
21969 {
21970 return 0;
21971 }
21972
21973 /* for_each_template_parm any_fn callback to handle deduction of a template
21974 type argument from the type of an array bound. */
21975
21976 static int
array_deduction_r(tree t,void * data)21977 array_deduction_r (tree t, void *data)
21978 {
21979 tree_pair_p d = (tree_pair_p)data;
21980 tree &tparms = d->purpose;
21981 tree &targs = d->value;
21982
21983 if (TREE_CODE (t) == ARRAY_TYPE)
21984 if (tree dom = TYPE_DOMAIN (t))
21985 if (tree max = TYPE_MAX_VALUE (dom))
21986 {
21987 if (TREE_CODE (max) == MINUS_EXPR)
21988 max = TREE_OPERAND (max, 0);
21989 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21990 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21991 UNIFY_ALLOW_NONE, /*explain*/false);
21992 }
21993
21994 /* Keep walking. */
21995 return 0;
21996 }
21997
21998 /* Try to deduce any not-yet-deduced template type arguments from the type of
21999 an array bound. This is handled separately from unify because 14.8.2.5 says
22000 "The type of a type parameter is only deduced from an array bound if it is
22001 not otherwise deduced." */
22002
22003 static void
try_array_deduction(tree tparms,tree targs,tree parm)22004 try_array_deduction (tree tparms, tree targs, tree parm)
22005 {
22006 tree_pair_s data = { tparms, targs };
22007 hash_set<tree> visited;
22008 for_each_template_parm (parm, zero_r, &data, &visited,
22009 /*nondeduced*/false, array_deduction_r);
22010 }
22011
22012 /* Most parms like fn_type_unification.
22013
22014 If SUBR is 1, we're being called recursively (to unify the
22015 arguments of a function or method parameter of a function
22016 template).
22017
22018 CHECKS is a pointer to a vector of access checks encountered while
22019 substituting default template arguments. */
22020
22021 static int
type_unification_real(tree tparms,tree full_targs,tree xparms,const tree * xargs,unsigned int xnargs,int subr,unification_kind_t strict,vec<deferred_access_check,va_gc> ** checks,bool explain_p)22022 type_unification_real (tree tparms,
22023 tree full_targs,
22024 tree xparms,
22025 const tree *xargs,
22026 unsigned int xnargs,
22027 int subr,
22028 unification_kind_t strict,
22029 vec<deferred_access_check, va_gc> **checks,
22030 bool explain_p)
22031 {
22032 tree parm, arg;
22033 int i;
22034 int ntparms = TREE_VEC_LENGTH (tparms);
22035 int saw_undeduced = 0;
22036 tree parms;
22037 const tree *args;
22038 unsigned int nargs;
22039 unsigned int ia;
22040
22041 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
22042 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
22043 gcc_assert (ntparms > 0);
22044
22045 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
22046
22047 /* Reset the number of non-defaulted template arguments contained
22048 in TARGS. */
22049 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
22050
22051 again:
22052 parms = xparms;
22053 args = xargs;
22054 nargs = xnargs;
22055
22056 ia = 0;
22057 while (parms && parms != void_list_node
22058 && ia < nargs)
22059 {
22060 parm = TREE_VALUE (parms);
22061
22062 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22063 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
22064 /* For a function parameter pack that occurs at the end of the
22065 parameter-declaration-list, the type A of each remaining
22066 argument of the call is compared with the type P of the
22067 declarator-id of the function parameter pack. */
22068 break;
22069
22070 parms = TREE_CHAIN (parms);
22071
22072 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22073 /* For a function parameter pack that does not occur at the
22074 end of the parameter-declaration-list, the type of the
22075 parameter pack is a non-deduced context. */
22076 continue;
22077
22078 arg = args[ia];
22079 ++ia;
22080
22081 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
22082 explain_p))
22083 return 1;
22084 }
22085
22086 if (parms
22087 && parms != void_list_node
22088 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
22089 {
22090 /* Unify the remaining arguments with the pack expansion type. */
22091 tree argvec;
22092 tree parmvec = make_tree_vec (1);
22093
22094 /* Allocate a TREE_VEC and copy in all of the arguments */
22095 argvec = make_tree_vec (nargs - ia);
22096 for (i = 0; ia < nargs; ++ia, ++i)
22097 TREE_VEC_ELT (argvec, i) = args[ia];
22098
22099 /* Copy the parameter into parmvec. */
22100 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
22101 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
22102 /*subr=*/subr, explain_p))
22103 return 1;
22104
22105 /* Advance to the end of the list of parameters. */
22106 parms = TREE_CHAIN (parms);
22107 }
22108
22109 /* Fail if we've reached the end of the parm list, and more args
22110 are present, and the parm list isn't variadic. */
22111 if (ia < nargs && parms == void_list_node)
22112 return unify_too_many_arguments (explain_p, nargs, ia);
22113 /* Fail if parms are left and they don't have default values and
22114 they aren't all deduced as empty packs (c++/57397). This is
22115 consistent with sufficient_parms_p. */
22116 if (parms && parms != void_list_node
22117 && TREE_PURPOSE (parms) == NULL_TREE)
22118 {
22119 unsigned int count = nargs;
22120 tree p = parms;
22121 bool type_pack_p;
22122 do
22123 {
22124 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
22125 if (!type_pack_p)
22126 count++;
22127 p = TREE_CHAIN (p);
22128 }
22129 while (p && p != void_list_node);
22130 if (count != nargs)
22131 return unify_too_few_arguments (explain_p, ia, count,
22132 type_pack_p);
22133 }
22134
22135 if (!subr)
22136 {
22137 tsubst_flags_t complain = (explain_p
22138 ? tf_warning_or_error
22139 : tf_none);
22140 bool tried_array_deduction = (cxx_dialect < cxx17);
22141
22142 for (i = 0; i < ntparms; i++)
22143 {
22144 tree targ = TREE_VEC_ELT (targs, i);
22145 tree tparm = TREE_VEC_ELT (tparms, i);
22146
22147 /* Clear the "incomplete" flags on all argument packs now so that
22148 substituting them into later default arguments works. */
22149 if (targ && ARGUMENT_PACK_P (targ))
22150 {
22151 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22152 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22153 }
22154
22155 if (targ || tparm == error_mark_node)
22156 continue;
22157 tparm = TREE_VALUE (tparm);
22158
22159 if (TREE_CODE (tparm) == TYPE_DECL
22160 && !tried_array_deduction)
22161 {
22162 try_array_deduction (tparms, targs, xparms);
22163 tried_array_deduction = true;
22164 if (TREE_VEC_ELT (targs, i))
22165 continue;
22166 }
22167
22168 /* If this is an undeduced nontype parameter that depends on
22169 a type parameter, try another pass; its type may have been
22170 deduced from a later argument than the one from which
22171 this parameter can be deduced. */
22172 if (TREE_CODE (tparm) == PARM_DECL
22173 && uses_template_parms (TREE_TYPE (tparm))
22174 && saw_undeduced < 2)
22175 {
22176 saw_undeduced = 1;
22177 continue;
22178 }
22179
22180 /* Core issue #226 (C++0x) [temp.deduct]:
22181
22182 If a template argument has not been deduced, its
22183 default template argument, if any, is used.
22184
22185 When we are in C++98 mode, TREE_PURPOSE will either
22186 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22187 to explicitly check cxx_dialect here. */
22188 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22189 /* OK, there is a default argument. Wait until after the
22190 conversion check to do substitution. */
22191 continue;
22192
22193 /* If the type parameter is a parameter pack, then it will
22194 be deduced to an empty parameter pack. */
22195 if (template_parameter_pack_p (tparm))
22196 {
22197 tree arg;
22198
22199 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22200 {
22201 arg = make_node (NONTYPE_ARGUMENT_PACK);
22202 TREE_CONSTANT (arg) = 1;
22203 }
22204 else
22205 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22206
22207 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22208
22209 TREE_VEC_ELT (targs, i) = arg;
22210 continue;
22211 }
22212
22213 return unify_parameter_deduction_failure (explain_p, tparm);
22214 }
22215
22216 /* Now substitute into the default template arguments. */
22217 for (i = 0; i < ntparms; i++)
22218 {
22219 tree targ = TREE_VEC_ELT (targs, i);
22220 tree tparm = TREE_VEC_ELT (tparms, i);
22221
22222 if (targ || tparm == error_mark_node)
22223 continue;
22224 tree parm = TREE_VALUE (tparm);
22225 tree arg = TREE_PURPOSE (tparm);
22226 reopen_deferring_access_checks (*checks);
22227 location_t save_loc = input_location;
22228 if (DECL_P (parm))
22229 input_location = DECL_SOURCE_LOCATION (parm);
22230
22231 if (saw_undeduced == 1
22232 && TREE_CODE (parm) == PARM_DECL
22233 && uses_template_parms (TREE_TYPE (parm)))
22234 {
22235 /* The type of this non-type parameter depends on undeduced
22236 parameters. Don't try to use its default argument yet,
22237 since we might deduce an argument for it on the next pass,
22238 but do check whether the arguments we already have cause
22239 substitution failure, so that that happens before we try
22240 later default arguments (78489). */
22241 ++processing_template_decl;
22242 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22243 NULL_TREE);
22244 --processing_template_decl;
22245 if (type == error_mark_node)
22246 arg = error_mark_node;
22247 else
22248 arg = NULL_TREE;
22249 }
22250 else
22251 {
22252 /* Even if the call is happening in template context, getting
22253 here means it's non-dependent, and a default argument is
22254 considered a separate definition under [temp.decls], so we can
22255 do this substitution without processing_template_decl. This
22256 is important if the default argument contains something that
22257 might be instantiation-dependent like access (87480). */
22258 processing_template_decl_sentinel s;
22259 tree substed = NULL_TREE;
22260 if (saw_undeduced == 1)
22261 {
22262 /* First instatiate in template context, in case we still
22263 depend on undeduced template parameters. */
22264 ++processing_template_decl;
22265 substed = tsubst_template_arg (arg, full_targs, complain,
22266 NULL_TREE);
22267 --processing_template_decl;
22268 if (substed != error_mark_node
22269 && !uses_template_parms (substed))
22270 /* We replaced all the tparms, substitute again out of
22271 template context. */
22272 substed = NULL_TREE;
22273 }
22274 if (!substed)
22275 substed = tsubst_template_arg (arg, full_targs, complain,
22276 NULL_TREE);
22277
22278 if (!uses_template_parms (substed))
22279 arg = convert_template_argument (parm, substed, full_targs,
22280 complain, i, NULL_TREE);
22281 else if (saw_undeduced == 1)
22282 arg = NULL_TREE;
22283 else
22284 arg = error_mark_node;
22285 }
22286
22287 input_location = save_loc;
22288 *checks = get_deferred_access_checks ();
22289 pop_deferring_access_checks ();
22290
22291 if (arg == error_mark_node)
22292 return 1;
22293 else if (arg)
22294 {
22295 TREE_VEC_ELT (targs, i) = arg;
22296 /* The position of the first default template argument,
22297 is also the number of non-defaulted arguments in TARGS.
22298 Record that. */
22299 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22300 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22301 }
22302 }
22303
22304 if (saw_undeduced++ == 1)
22305 goto again;
22306 }
22307
22308 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22309 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22310
22311 return unify_success (explain_p);
22312 }
22313
22314 /* Subroutine of type_unification_real. Args are like the variables
22315 at the call site. ARG is an overloaded function (or template-id);
22316 we try deducing template args from each of the overloads, and if
22317 only one succeeds, we go with that. Modifies TARGS and returns
22318 true on success. */
22319
22320 static bool
resolve_overloaded_unification(tree tparms,tree targs,tree parm,tree arg,unification_kind_t strict,int sub_strict,bool explain_p)22321 resolve_overloaded_unification (tree tparms,
22322 tree targs,
22323 tree parm,
22324 tree arg,
22325 unification_kind_t strict,
22326 int sub_strict,
22327 bool explain_p)
22328 {
22329 tree tempargs = copy_node (targs);
22330 int good = 0;
22331 tree goodfn = NULL_TREE;
22332 bool addr_p;
22333
22334 if (TREE_CODE (arg) == ADDR_EXPR)
22335 {
22336 arg = TREE_OPERAND (arg, 0);
22337 addr_p = true;
22338 }
22339 else
22340 addr_p = false;
22341
22342 if (TREE_CODE (arg) == COMPONENT_REF)
22343 /* Handle `&x' where `x' is some static or non-static member
22344 function name. */
22345 arg = TREE_OPERAND (arg, 1);
22346
22347 if (TREE_CODE (arg) == OFFSET_REF)
22348 arg = TREE_OPERAND (arg, 1);
22349
22350 /* Strip baselink information. */
22351 if (BASELINK_P (arg))
22352 arg = BASELINK_FUNCTIONS (arg);
22353
22354 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22355 {
22356 /* If we got some explicit template args, we need to plug them into
22357 the affected templates before we try to unify, in case the
22358 explicit args will completely resolve the templates in question. */
22359
22360 int ok = 0;
22361 tree expl_subargs = TREE_OPERAND (arg, 1);
22362 arg = TREE_OPERAND (arg, 0);
22363
22364 for (lkp_iterator iter (arg); iter; ++iter)
22365 {
22366 tree fn = *iter;
22367 tree subargs, elem;
22368
22369 if (TREE_CODE (fn) != TEMPLATE_DECL)
22370 continue;
22371
22372 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22373 expl_subargs, NULL_TREE, tf_none,
22374 /*require_all_args=*/true,
22375 /*use_default_args=*/true);
22376 if (subargs != error_mark_node
22377 && !any_dependent_template_arguments_p (subargs))
22378 {
22379 fn = instantiate_template (fn, subargs, tf_none);
22380 if (!constraints_satisfied_p (fn))
22381 continue;
22382 if (undeduced_auto_decl (fn))
22383 {
22384 /* Instantiate the function to deduce its return type. */
22385 ++function_depth;
22386 instantiate_decl (fn, /*defer*/false, /*class*/false);
22387 --function_depth;
22388 }
22389
22390 if (flag_noexcept_type)
22391 maybe_instantiate_noexcept (fn, tf_none);
22392
22393 elem = TREE_TYPE (fn);
22394 if (try_one_overload (tparms, targs, tempargs, parm,
22395 elem, strict, sub_strict, addr_p, explain_p)
22396 && (!goodfn || !same_type_p (goodfn, elem)))
22397 {
22398 goodfn = elem;
22399 ++good;
22400 }
22401 }
22402 else if (subargs)
22403 ++ok;
22404 }
22405 /* If no templates (or more than one) are fully resolved by the
22406 explicit arguments, this template-id is a non-deduced context; it
22407 could still be OK if we deduce all template arguments for the
22408 enclosing call through other arguments. */
22409 if (good != 1)
22410 good = ok;
22411 }
22412 else if (!OVL_P (arg))
22413 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22414 -- but the deduction does not succeed because the expression is
22415 not just the function on its own. */
22416 return false;
22417 else
22418 for (lkp_iterator iter (arg); iter; ++iter)
22419 {
22420 tree fn = *iter;
22421 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22422 strict, sub_strict, addr_p, explain_p)
22423 && (!goodfn || !decls_match (goodfn, fn)))
22424 {
22425 goodfn = fn;
22426 ++good;
22427 }
22428 }
22429
22430 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22431 to function or pointer to member function argument if the set of
22432 overloaded functions does not contain function templates and at most
22433 one of a set of overloaded functions provides a unique match.
22434
22435 So if we found multiple possibilities, we return success but don't
22436 deduce anything. */
22437
22438 if (good == 1)
22439 {
22440 int i = TREE_VEC_LENGTH (targs);
22441 for (; i--; )
22442 if (TREE_VEC_ELT (tempargs, i))
22443 {
22444 tree old = TREE_VEC_ELT (targs, i);
22445 tree new_ = TREE_VEC_ELT (tempargs, i);
22446 if (new_ && old && ARGUMENT_PACK_P (old)
22447 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22448 /* Don't forget explicit template arguments in a pack. */
22449 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22450 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22451 TREE_VEC_ELT (targs, i) = new_;
22452 }
22453 }
22454 if (good)
22455 return true;
22456
22457 return false;
22458 }
22459
22460 /* Core DR 115: In contexts where deduction is done and fails, or in
22461 contexts where deduction is not done, if a template argument list is
22462 specified and it, along with any default template arguments, identifies
22463 a single function template specialization, then the template-id is an
22464 lvalue for the function template specialization. */
22465
22466 tree
resolve_nondeduced_context(tree orig_expr,tsubst_flags_t complain)22467 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22468 {
22469 tree expr, offset, baselink;
22470 bool addr;
22471
22472 if (!type_unknown_p (orig_expr))
22473 return orig_expr;
22474
22475 expr = orig_expr;
22476 addr = false;
22477 offset = NULL_TREE;
22478 baselink = NULL_TREE;
22479
22480 if (TREE_CODE (expr) == ADDR_EXPR)
22481 {
22482 expr = TREE_OPERAND (expr, 0);
22483 addr = true;
22484 }
22485 if (TREE_CODE (expr) == OFFSET_REF)
22486 {
22487 offset = expr;
22488 expr = TREE_OPERAND (expr, 1);
22489 }
22490 if (BASELINK_P (expr))
22491 {
22492 baselink = expr;
22493 expr = BASELINK_FUNCTIONS (expr);
22494 }
22495
22496 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22497 {
22498 int good = 0;
22499 tree goodfn = NULL_TREE;
22500
22501 /* If we got some explicit template args, we need to plug them into
22502 the affected templates before we try to unify, in case the
22503 explicit args will completely resolve the templates in question. */
22504
22505 tree expl_subargs = TREE_OPERAND (expr, 1);
22506 tree arg = TREE_OPERAND (expr, 0);
22507 tree badfn = NULL_TREE;
22508 tree badargs = NULL_TREE;
22509
22510 for (lkp_iterator iter (arg); iter; ++iter)
22511 {
22512 tree fn = *iter;
22513 tree subargs, elem;
22514
22515 if (TREE_CODE (fn) != TEMPLATE_DECL)
22516 continue;
22517
22518 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22519 expl_subargs, NULL_TREE, tf_none,
22520 /*require_all_args=*/true,
22521 /*use_default_args=*/true);
22522 if (subargs != error_mark_node
22523 && !any_dependent_template_arguments_p (subargs))
22524 {
22525 elem = instantiate_template (fn, subargs, tf_none);
22526 if (elem == error_mark_node)
22527 {
22528 badfn = fn;
22529 badargs = subargs;
22530 }
22531 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22532 && constraints_satisfied_p (elem))
22533 {
22534 goodfn = elem;
22535 ++good;
22536 }
22537 }
22538 }
22539 if (good == 1)
22540 {
22541 mark_used (goodfn);
22542 expr = goodfn;
22543 if (baselink)
22544 expr = build_baselink (BASELINK_BINFO (baselink),
22545 BASELINK_ACCESS_BINFO (baselink),
22546 expr, BASELINK_OPTYPE (baselink));
22547 if (offset)
22548 {
22549 tree base
22550 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22551 expr = build_offset_ref (base, expr, addr, complain);
22552 }
22553 if (addr)
22554 expr = cp_build_addr_expr (expr, complain);
22555 return expr;
22556 }
22557 else if (good == 0 && badargs && (complain & tf_error))
22558 /* There were no good options and at least one bad one, so let the
22559 user know what the problem is. */
22560 instantiate_template (badfn, badargs, complain);
22561 }
22562 return orig_expr;
22563 }
22564
22565 /* As above, but error out if the expression remains overloaded. */
22566
22567 tree
resolve_nondeduced_context_or_error(tree exp,tsubst_flags_t complain)22568 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22569 {
22570 exp = resolve_nondeduced_context (exp, complain);
22571 if (type_unknown_p (exp))
22572 {
22573 if (complain & tf_error)
22574 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22575 return error_mark_node;
22576 }
22577 return exp;
22578 }
22579
22580 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22581 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22582 different overloads deduce different arguments for a given parm.
22583 ADDR_P is true if the expression for which deduction is being
22584 performed was of the form "& fn" rather than simply "fn".
22585
22586 Returns 1 on success. */
22587
22588 static int
try_one_overload(tree tparms,tree orig_targs,tree targs,tree parm,tree arg,unification_kind_t strict,int sub_strict,bool addr_p,bool explain_p)22589 try_one_overload (tree tparms,
22590 tree orig_targs,
22591 tree targs,
22592 tree parm,
22593 tree arg,
22594 unification_kind_t strict,
22595 int sub_strict,
22596 bool addr_p,
22597 bool explain_p)
22598 {
22599 int nargs;
22600 tree tempargs;
22601 int i;
22602
22603 if (arg == error_mark_node)
22604 return 0;
22605
22606 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22607 to function or pointer to member function argument if the set of
22608 overloaded functions does not contain function templates and at most
22609 one of a set of overloaded functions provides a unique match.
22610
22611 So if this is a template, just return success. */
22612
22613 if (uses_template_parms (arg))
22614 return 1;
22615
22616 if (TREE_CODE (arg) == METHOD_TYPE)
22617 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22618 else if (addr_p)
22619 arg = build_pointer_type (arg);
22620
22621 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22622
22623 /* We don't copy orig_targs for this because if we have already deduced
22624 some template args from previous args, unify would complain when we
22625 try to deduce a template parameter for the same argument, even though
22626 there isn't really a conflict. */
22627 nargs = TREE_VEC_LENGTH (targs);
22628 tempargs = make_tree_vec (nargs);
22629
22630 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22631 return 0;
22632
22633 /* First make sure we didn't deduce anything that conflicts with
22634 explicitly specified args. */
22635 for (i = nargs; i--; )
22636 {
22637 tree elt = TREE_VEC_ELT (tempargs, i);
22638 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22639
22640 if (!elt)
22641 /*NOP*/;
22642 else if (uses_template_parms (elt))
22643 /* Since we're unifying against ourselves, we will fill in
22644 template args used in the function parm list with our own
22645 template parms. Discard them. */
22646 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22647 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22648 {
22649 /* Check that the argument at each index of the deduced argument pack
22650 is equivalent to the corresponding explicitly specified argument.
22651 We may have deduced more arguments than were explicitly specified,
22652 and that's OK. */
22653
22654 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22655 that's wrong if we deduce the same argument pack from multiple
22656 function arguments: it's only incomplete the first time. */
22657
22658 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22659 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22660
22661 if (TREE_VEC_LENGTH (deduced_pack)
22662 < TREE_VEC_LENGTH (explicit_pack))
22663 return 0;
22664
22665 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22666 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22667 TREE_VEC_ELT (deduced_pack, j)))
22668 return 0;
22669 }
22670 else if (oldelt && !template_args_equal (oldelt, elt))
22671 return 0;
22672 }
22673
22674 for (i = nargs; i--; )
22675 {
22676 tree elt = TREE_VEC_ELT (tempargs, i);
22677
22678 if (elt)
22679 TREE_VEC_ELT (targs, i) = elt;
22680 }
22681
22682 return 1;
22683 }
22684
22685 /* PARM is a template class (perhaps with unbound template
22686 parameters). ARG is a fully instantiated type. If ARG can be
22687 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22688 TARGS are as for unify. */
22689
22690 static tree
try_class_unification(tree tparms,tree targs,tree parm,tree arg,bool explain_p)22691 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22692 bool explain_p)
22693 {
22694 tree copy_of_targs;
22695
22696 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22697 return NULL_TREE;
22698 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22699 /* Matches anything. */;
22700 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22701 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22702 return NULL_TREE;
22703
22704 /* We need to make a new template argument vector for the call to
22705 unify. If we used TARGS, we'd clutter it up with the result of
22706 the attempted unification, even if this class didn't work out.
22707 We also don't want to commit ourselves to all the unifications
22708 we've already done, since unification is supposed to be done on
22709 an argument-by-argument basis. In other words, consider the
22710 following pathological case:
22711
22712 template <int I, int J, int K>
22713 struct S {};
22714
22715 template <int I, int J>
22716 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22717
22718 template <int I, int J, int K>
22719 void f(S<I, J, K>, S<I, I, I>);
22720
22721 void g() {
22722 S<0, 0, 0> s0;
22723 S<0, 1, 2> s2;
22724
22725 f(s0, s2);
22726 }
22727
22728 Now, by the time we consider the unification involving `s2', we
22729 already know that we must have `f<0, 0, 0>'. But, even though
22730 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22731 because there are two ways to unify base classes of S<0, 1, 2>
22732 with S<I, I, I>. If we kept the already deduced knowledge, we
22733 would reject the possibility I=1. */
22734 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22735
22736 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22737 {
22738 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22739 return NULL_TREE;
22740 return arg;
22741 }
22742
22743 /* If unification failed, we're done. */
22744 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22745 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22746 return NULL_TREE;
22747
22748 return arg;
22749 }
22750
22751 /* Given a template type PARM and a class type ARG, find the unique
22752 base type in ARG that is an instance of PARM. We do not examine
22753 ARG itself; only its base-classes. If there is not exactly one
22754 appropriate base class, return NULL_TREE. PARM may be the type of
22755 a partial specialization, as well as a plain template type. Used
22756 by unify. */
22757
22758 static enum template_base_result
get_template_base(tree tparms,tree targs,tree parm,tree arg,bool explain_p,tree * result)22759 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22760 bool explain_p, tree *result)
22761 {
22762 tree rval = NULL_TREE;
22763 tree binfo;
22764
22765 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22766
22767 binfo = TYPE_BINFO (complete_type (arg));
22768 if (!binfo)
22769 {
22770 /* The type could not be completed. */
22771 *result = NULL_TREE;
22772 return tbr_incomplete_type;
22773 }
22774
22775 /* Walk in inheritance graph order. The search order is not
22776 important, and this avoids multiple walks of virtual bases. */
22777 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22778 {
22779 tree r = try_class_unification (tparms, targs, parm,
22780 BINFO_TYPE (binfo), explain_p);
22781
22782 if (r)
22783 {
22784 /* If there is more than one satisfactory baseclass, then:
22785
22786 [temp.deduct.call]
22787
22788 If they yield more than one possible deduced A, the type
22789 deduction fails.
22790
22791 applies. */
22792 if (rval && !same_type_p (r, rval))
22793 {
22794 *result = NULL_TREE;
22795 return tbr_ambiguous_baseclass;
22796 }
22797
22798 rval = r;
22799 }
22800 }
22801
22802 *result = rval;
22803 return tbr_success;
22804 }
22805
22806 /* Returns the level of DECL, which declares a template parameter. */
22807
22808 static int
template_decl_level(tree decl)22809 template_decl_level (tree decl)
22810 {
22811 switch (TREE_CODE (decl))
22812 {
22813 case TYPE_DECL:
22814 case TEMPLATE_DECL:
22815 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22816
22817 case PARM_DECL:
22818 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22819
22820 default:
22821 gcc_unreachable ();
22822 }
22823 return 0;
22824 }
22825
22826 /* Decide whether ARG can be unified with PARM, considering only the
22827 cv-qualifiers of each type, given STRICT as documented for unify.
22828 Returns nonzero iff the unification is OK on that basis. */
22829
22830 static int
check_cv_quals_for_unify(int strict,tree arg,tree parm)22831 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22832 {
22833 int arg_quals = cp_type_quals (arg);
22834 int parm_quals = cp_type_quals (parm);
22835
22836 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22837 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22838 {
22839 /* Although a CVR qualifier is ignored when being applied to a
22840 substituted template parameter ([8.3.2]/1 for example), that
22841 does not allow us to unify "const T" with "int&" because both
22842 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22843 It is ok when we're allowing additional CV qualifiers
22844 at the outer level [14.8.2.1]/3,1st bullet. */
22845 if ((TYPE_REF_P (arg)
22846 || FUNC_OR_METHOD_TYPE_P (arg))
22847 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22848 return 0;
22849
22850 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22851 && (parm_quals & TYPE_QUAL_RESTRICT))
22852 return 0;
22853 }
22854
22855 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22856 && (arg_quals & parm_quals) != parm_quals)
22857 return 0;
22858
22859 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22860 && (parm_quals & arg_quals) != arg_quals)
22861 return 0;
22862
22863 return 1;
22864 }
22865
22866 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22867 void
template_parm_level_and_index(tree parm,int * level,int * index)22868 template_parm_level_and_index (tree parm, int* level, int* index)
22869 {
22870 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22871 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22872 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22873 {
22874 *index = TEMPLATE_TYPE_IDX (parm);
22875 *level = TEMPLATE_TYPE_LEVEL (parm);
22876 }
22877 else
22878 {
22879 *index = TEMPLATE_PARM_IDX (parm);
22880 *level = TEMPLATE_PARM_LEVEL (parm);
22881 }
22882 }
22883
22884 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22885 do { \
22886 if (unify (TP, TA, P, A, S, EP)) \
22887 return 1; \
22888 } while (0)
22889
22890 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22891 expansion at the end of PACKED_PARMS. Returns 0 if the type
22892 deduction succeeds, 1 otherwise. STRICT is the same as in
22893 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22894 function call argument list. We'll need to adjust the arguments to make them
22895 types. SUBR tells us if this is from a recursive call to
22896 type_unification_real, or for comparing two template argument
22897 lists. */
22898
22899 static int
unify_pack_expansion(tree tparms,tree targs,tree packed_parms,tree packed_args,unification_kind_t strict,bool subr,bool explain_p)22900 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22901 tree packed_args, unification_kind_t strict,
22902 bool subr, bool explain_p)
22903 {
22904 tree parm
22905 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22906 tree pattern = PACK_EXPANSION_PATTERN (parm);
22907 tree pack, packs = NULL_TREE;
22908 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22909
22910 /* Add in any args remembered from an earlier partial instantiation. */
22911 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22912 int levels = TMPL_ARGS_DEPTH (targs);
22913
22914 packed_args = expand_template_argument_pack (packed_args);
22915
22916 int len = TREE_VEC_LENGTH (packed_args);
22917
22918 /* Determine the parameter packs we will be deducing from the
22919 pattern, and record their current deductions. */
22920 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22921 pack; pack = TREE_CHAIN (pack))
22922 {
22923 tree parm_pack = TREE_VALUE (pack);
22924 int idx, level;
22925
22926 /* Only template parameter packs can be deduced, not e.g. function
22927 parameter packs or __bases or __integer_pack. */
22928 if (!TEMPLATE_PARM_P (parm_pack))
22929 continue;
22930
22931 /* Determine the index and level of this parameter pack. */
22932 template_parm_level_and_index (parm_pack, &level, &idx);
22933 if (level < levels)
22934 continue;
22935
22936 /* Keep track of the parameter packs and their corresponding
22937 argument packs. */
22938 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22939 TREE_TYPE (packs) = make_tree_vec (len - start);
22940 }
22941
22942 /* Loop through all of the arguments that have not yet been
22943 unified and unify each with the pattern. */
22944 for (i = start; i < len; i++)
22945 {
22946 tree parm;
22947 bool any_explicit = false;
22948 tree arg = TREE_VEC_ELT (packed_args, i);
22949
22950 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22951 or the element of its argument pack at the current index if
22952 this argument was explicitly specified. */
22953 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22954 {
22955 int idx, level;
22956 tree arg, pargs;
22957 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22958
22959 arg = NULL_TREE;
22960 if (TREE_VALUE (pack)
22961 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22962 && (i - start < TREE_VEC_LENGTH (pargs)))
22963 {
22964 any_explicit = true;
22965 arg = TREE_VEC_ELT (pargs, i - start);
22966 }
22967 TMPL_ARG (targs, level, idx) = arg;
22968 }
22969
22970 /* If we had explicit template arguments, substitute them into the
22971 pattern before deduction. */
22972 if (any_explicit)
22973 {
22974 /* Some arguments might still be unspecified or dependent. */
22975 bool dependent;
22976 ++processing_template_decl;
22977 dependent = any_dependent_template_arguments_p (targs);
22978 if (!dependent)
22979 --processing_template_decl;
22980 parm = tsubst (pattern, targs,
22981 explain_p ? tf_warning_or_error : tf_none,
22982 NULL_TREE);
22983 if (dependent)
22984 --processing_template_decl;
22985 if (parm == error_mark_node)
22986 return 1;
22987 }
22988 else
22989 parm = pattern;
22990
22991 /* Unify the pattern with the current argument. */
22992 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22993 explain_p))
22994 return 1;
22995
22996 /* For each parameter pack, collect the deduced value. */
22997 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22998 {
22999 int idx, level;
23000 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23001
23002 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
23003 TMPL_ARG (targs, level, idx);
23004 }
23005 }
23006
23007 /* Verify that the results of unification with the parameter packs
23008 produce results consistent with what we've seen before, and make
23009 the deduced argument packs available. */
23010 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23011 {
23012 tree old_pack = TREE_VALUE (pack);
23013 tree new_args = TREE_TYPE (pack);
23014 int i, len = TREE_VEC_LENGTH (new_args);
23015 int idx, level;
23016 bool nondeduced_p = false;
23017
23018 /* By default keep the original deduced argument pack.
23019 If necessary, more specific code is going to update the
23020 resulting deduced argument later down in this function. */
23021 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23022 TMPL_ARG (targs, level, idx) = old_pack;
23023
23024 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23025 actually deduce anything. */
23026 for (i = 0; i < len && !nondeduced_p; ++i)
23027 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
23028 nondeduced_p = true;
23029 if (nondeduced_p)
23030 continue;
23031
23032 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
23033 {
23034 /* If we had fewer function args than explicit template args,
23035 just use the explicits. */
23036 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23037 int explicit_len = TREE_VEC_LENGTH (explicit_args);
23038 if (len < explicit_len)
23039 new_args = explicit_args;
23040 }
23041
23042 if (!old_pack)
23043 {
23044 tree result;
23045 /* Build the deduced *_ARGUMENT_PACK. */
23046 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
23047 {
23048 result = make_node (NONTYPE_ARGUMENT_PACK);
23049 TREE_CONSTANT (result) = 1;
23050 }
23051 else
23052 result = cxx_make_type (TYPE_ARGUMENT_PACK);
23053
23054 SET_ARGUMENT_PACK_ARGS (result, new_args);
23055
23056 /* Note the deduced argument packs for this parameter
23057 pack. */
23058 TMPL_ARG (targs, level, idx) = result;
23059 }
23060 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
23061 && (ARGUMENT_PACK_ARGS (old_pack)
23062 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
23063 {
23064 /* We only had the explicitly-provided arguments before, but
23065 now we have a complete set of arguments. */
23066 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23067
23068 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
23069 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
23070 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
23071 }
23072 else
23073 {
23074 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
23075 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
23076
23077 if (!comp_template_args (old_args, new_args,
23078 &bad_old_arg, &bad_new_arg))
23079 /* Inconsistent unification of this parameter pack. */
23080 return unify_parameter_pack_inconsistent (explain_p,
23081 bad_old_arg,
23082 bad_new_arg);
23083 }
23084 }
23085
23086 return unify_success (explain_p);
23087 }
23088
23089 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23090 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23091 parameters and return value are as for unify. */
23092
23093 static int
unify_array_domain(tree tparms,tree targs,tree parm_dom,tree arg_dom,bool explain_p)23094 unify_array_domain (tree tparms, tree targs,
23095 tree parm_dom, tree arg_dom,
23096 bool explain_p)
23097 {
23098 tree parm_max;
23099 tree arg_max;
23100 bool parm_cst;
23101 bool arg_cst;
23102
23103 /* Our representation of array types uses "N - 1" as the
23104 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23105 not an integer constant. We cannot unify arbitrarily
23106 complex expressions, so we eliminate the MINUS_EXPRs
23107 here. */
23108 parm_max = TYPE_MAX_VALUE (parm_dom);
23109 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
23110 if (!parm_cst)
23111 {
23112 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
23113 parm_max = TREE_OPERAND (parm_max, 0);
23114 }
23115 arg_max = TYPE_MAX_VALUE (arg_dom);
23116 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
23117 if (!arg_cst)
23118 {
23119 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23120 trying to unify the type of a variable with the type
23121 of a template parameter. For example:
23122
23123 template <unsigned int N>
23124 void f (char (&) [N]);
23125 int g();
23126 void h(int i) {
23127 char a[g(i)];
23128 f(a);
23129 }
23130
23131 Here, the type of the ARG will be "int [g(i)]", and
23132 may be a SAVE_EXPR, etc. */
23133 if (TREE_CODE (arg_max) != MINUS_EXPR)
23134 return unify_vla_arg (explain_p, arg_dom);
23135 arg_max = TREE_OPERAND (arg_max, 0);
23136 }
23137
23138 /* If only one of the bounds used a MINUS_EXPR, compensate
23139 by adding one to the other bound. */
23140 if (parm_cst && !arg_cst)
23141 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23142 integer_type_node,
23143 parm_max,
23144 integer_one_node);
23145 else if (arg_cst && !parm_cst)
23146 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23147 integer_type_node,
23148 arg_max,
23149 integer_one_node);
23150
23151 return unify (tparms, targs, parm_max, arg_max,
23152 UNIFY_ALLOW_INTEGER, explain_p);
23153 }
23154
23155 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23156
23157 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23158
23159 static pa_kind_t
pa_kind(tree t)23160 pa_kind (tree t)
23161 {
23162 if (PACK_EXPANSION_P (t))
23163 t = PACK_EXPANSION_PATTERN (t);
23164 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23165 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23166 || DECL_TYPE_TEMPLATE_P (t))
23167 return pa_tmpl;
23168 else if (TYPE_P (t))
23169 return pa_type;
23170 else
23171 return pa_expr;
23172 }
23173
23174 /* Deduce the value of template parameters. TPARMS is the (innermost)
23175 set of template parameters to a template. TARGS is the bindings
23176 for those template parameters, as determined thus far; TARGS may
23177 include template arguments for outer levels of template parameters
23178 as well. PARM is a parameter to a template function, or a
23179 subcomponent of that parameter; ARG is the corresponding argument.
23180 This function attempts to match PARM with ARG in a manner
23181 consistent with the existing assignments in TARGS. If more values
23182 are deduced, then TARGS is updated.
23183
23184 Returns 0 if the type deduction succeeds, 1 otherwise. The
23185 parameter STRICT is a bitwise or of the following flags:
23186
23187 UNIFY_ALLOW_NONE:
23188 Require an exact match between PARM and ARG.
23189 UNIFY_ALLOW_MORE_CV_QUAL:
23190 Allow the deduced ARG to be more cv-qualified (by qualification
23191 conversion) than ARG.
23192 UNIFY_ALLOW_LESS_CV_QUAL:
23193 Allow the deduced ARG to be less cv-qualified than ARG.
23194 UNIFY_ALLOW_DERIVED:
23195 Allow the deduced ARG to be a template base class of ARG,
23196 or a pointer to a template base class of the type pointed to by
23197 ARG.
23198 UNIFY_ALLOW_INTEGER:
23199 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23200 case for more information.
23201 UNIFY_ALLOW_OUTER_LEVEL:
23202 This is the outermost level of a deduction. Used to determine validity
23203 of qualification conversions. A valid qualification conversion must
23204 have const qualified pointers leading up to the inner type which
23205 requires additional CV quals, except at the outer level, where const
23206 is not required [conv.qual]. It would be normal to set this flag in
23207 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23208 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23209 This is the outermost level of a deduction, and PARM can be more CV
23210 qualified at this point.
23211 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23212 This is the outermost level of a deduction, and PARM can be less CV
23213 qualified at this point. */
23214
23215 static int
unify(tree tparms,tree targs,tree parm,tree arg,int strict,bool explain_p)23216 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23217 bool explain_p)
23218 {
23219 int idx;
23220 tree targ;
23221 tree tparm;
23222 int strict_in = strict;
23223 tsubst_flags_t complain = (explain_p
23224 ? tf_warning_or_error
23225 : tf_none);
23226
23227 /* I don't think this will do the right thing with respect to types.
23228 But the only case I've seen it in so far has been array bounds, where
23229 signedness is the only information lost, and I think that will be
23230 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23231 finish_id_expression_1, and are also OK. */
23232 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23233 parm = TREE_OPERAND (parm, 0);
23234
23235 if (arg == error_mark_node)
23236 return unify_invalid (explain_p);
23237 if (arg == unknown_type_node
23238 || arg == init_list_type_node)
23239 /* We can't deduce anything from this, but we might get all the
23240 template args from other function args. */
23241 return unify_success (explain_p);
23242
23243 if (parm == any_targ_node || arg == any_targ_node)
23244 return unify_success (explain_p);
23245
23246 /* If PARM uses template parameters, then we can't bail out here,
23247 even if ARG == PARM, since we won't record unifications for the
23248 template parameters. We might need them if we're trying to
23249 figure out which of two things is more specialized. */
23250 if (arg == parm && !uses_template_parms (parm))
23251 return unify_success (explain_p);
23252
23253 /* Handle init lists early, so the rest of the function can assume
23254 we're dealing with a type. */
23255 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23256 {
23257 tree elt, elttype;
23258 unsigned i;
23259 tree orig_parm = parm;
23260
23261 if (!is_std_init_list (parm)
23262 && TREE_CODE (parm) != ARRAY_TYPE)
23263 /* We can only deduce from an initializer list argument if the
23264 parameter is std::initializer_list or an array; otherwise this
23265 is a non-deduced context. */
23266 return unify_success (explain_p);
23267
23268 if (TREE_CODE (parm) == ARRAY_TYPE)
23269 elttype = TREE_TYPE (parm);
23270 else
23271 {
23272 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23273 /* Deduction is defined in terms of a single type, so just punt
23274 on the (bizarre) std::initializer_list<T...>. */
23275 if (PACK_EXPANSION_P (elttype))
23276 return unify_success (explain_p);
23277 }
23278
23279 if (strict != DEDUCE_EXACT
23280 && TYPE_P (elttype)
23281 && !uses_deducible_template_parms (elttype))
23282 /* If ELTTYPE has no deducible template parms, skip deduction from
23283 the list elements. */;
23284 else
23285 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23286 {
23287 int elt_strict = strict;
23288
23289 if (elt == error_mark_node)
23290 return unify_invalid (explain_p);
23291
23292 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23293 {
23294 tree type = TREE_TYPE (elt);
23295 if (type == error_mark_node)
23296 return unify_invalid (explain_p);
23297 /* It should only be possible to get here for a call. */
23298 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23299 elt_strict |= maybe_adjust_types_for_deduction
23300 (DEDUCE_CALL, &elttype, &type, elt);
23301 elt = type;
23302 }
23303
23304 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23305 explain_p);
23306 }
23307
23308 if (TREE_CODE (parm) == ARRAY_TYPE
23309 && deducible_array_bound (TYPE_DOMAIN (parm)))
23310 {
23311 /* Also deduce from the length of the initializer list. */
23312 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23313 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23314 if (idx == error_mark_node)
23315 return unify_invalid (explain_p);
23316 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23317 idx, explain_p);
23318 }
23319
23320 /* If the std::initializer_list<T> deduction worked, replace the
23321 deduced A with std::initializer_list<A>. */
23322 if (orig_parm != parm)
23323 {
23324 idx = TEMPLATE_TYPE_IDX (orig_parm);
23325 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23326 targ = listify (targ);
23327 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23328 }
23329 return unify_success (explain_p);
23330 }
23331
23332 /* If parm and arg aren't the same kind of thing (template, type, or
23333 expression), fail early. */
23334 if (pa_kind (parm) != pa_kind (arg))
23335 return unify_invalid (explain_p);
23336
23337 /* Immediately reject some pairs that won't unify because of
23338 cv-qualification mismatches. */
23339 if (TREE_CODE (arg) == TREE_CODE (parm)
23340 && TYPE_P (arg)
23341 /* It is the elements of the array which hold the cv quals of an array
23342 type, and the elements might be template type parms. We'll check
23343 when we recurse. */
23344 && TREE_CODE (arg) != ARRAY_TYPE
23345 /* We check the cv-qualifiers when unifying with template type
23346 parameters below. We want to allow ARG `const T' to unify with
23347 PARM `T' for example, when computing which of two templates
23348 is more specialized, for example. */
23349 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23350 && !check_cv_quals_for_unify (strict_in, arg, parm))
23351 return unify_cv_qual_mismatch (explain_p, parm, arg);
23352
23353 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23354 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23355 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23356 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23357 strict &= ~UNIFY_ALLOW_DERIVED;
23358 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23359 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23360
23361 switch (TREE_CODE (parm))
23362 {
23363 case TYPENAME_TYPE:
23364 case SCOPE_REF:
23365 case UNBOUND_CLASS_TEMPLATE:
23366 /* In a type which contains a nested-name-specifier, template
23367 argument values cannot be deduced for template parameters used
23368 within the nested-name-specifier. */
23369 return unify_success (explain_p);
23370
23371 case TEMPLATE_TYPE_PARM:
23372 case TEMPLATE_TEMPLATE_PARM:
23373 case BOUND_TEMPLATE_TEMPLATE_PARM:
23374 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23375 if (error_operand_p (tparm))
23376 return unify_invalid (explain_p);
23377
23378 if (TEMPLATE_TYPE_LEVEL (parm)
23379 != template_decl_level (tparm))
23380 /* The PARM is not one we're trying to unify. Just check
23381 to see if it matches ARG. */
23382 {
23383 if (TREE_CODE (arg) == TREE_CODE (parm)
23384 && (is_auto (parm) ? is_auto (arg)
23385 : same_type_p (parm, arg)))
23386 return unify_success (explain_p);
23387 else
23388 return unify_type_mismatch (explain_p, parm, arg);
23389 }
23390 idx = TEMPLATE_TYPE_IDX (parm);
23391 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23392 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23393 if (error_operand_p (tparm))
23394 return unify_invalid (explain_p);
23395
23396 /* Check for mixed types and values. */
23397 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23398 && TREE_CODE (tparm) != TYPE_DECL)
23399 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23400 && TREE_CODE (tparm) != TEMPLATE_DECL))
23401 gcc_unreachable ();
23402
23403 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23404 {
23405 if ((strict_in & UNIFY_ALLOW_DERIVED)
23406 && CLASS_TYPE_P (arg))
23407 {
23408 /* First try to match ARG directly. */
23409 tree t = try_class_unification (tparms, targs, parm, arg,
23410 explain_p);
23411 if (!t)
23412 {
23413 /* Otherwise, look for a suitable base of ARG, as below. */
23414 enum template_base_result r;
23415 r = get_template_base (tparms, targs, parm, arg,
23416 explain_p, &t);
23417 if (!t)
23418 return unify_no_common_base (explain_p, r, parm, arg);
23419 arg = t;
23420 }
23421 }
23422 /* ARG must be constructed from a template class or a template
23423 template parameter. */
23424 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23425 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23426 return unify_template_deduction_failure (explain_p, parm, arg);
23427
23428 /* Deduce arguments T, i from TT<T> or TT<i>. */
23429 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23430 return 1;
23431
23432 arg = TYPE_TI_TEMPLATE (arg);
23433
23434 /* Fall through to deduce template name. */
23435 }
23436
23437 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23438 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23439 {
23440 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23441
23442 /* Simple cases: Value already set, does match or doesn't. */
23443 if (targ != NULL_TREE && template_args_equal (targ, arg))
23444 return unify_success (explain_p);
23445 else if (targ)
23446 return unify_inconsistency (explain_p, parm, targ, arg);
23447 }
23448 else
23449 {
23450 /* If PARM is `const T' and ARG is only `int', we don't have
23451 a match unless we are allowing additional qualification.
23452 If ARG is `const int' and PARM is just `T' that's OK;
23453 that binds `const int' to `T'. */
23454 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23455 arg, parm))
23456 return unify_cv_qual_mismatch (explain_p, parm, arg);
23457
23458 /* Consider the case where ARG is `const volatile int' and
23459 PARM is `const T'. Then, T should be `volatile int'. */
23460 arg = cp_build_qualified_type_real
23461 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23462 if (arg == error_mark_node)
23463 return unify_invalid (explain_p);
23464
23465 /* Simple cases: Value already set, does match or doesn't. */
23466 if (targ != NULL_TREE && same_type_p (targ, arg))
23467 return unify_success (explain_p);
23468 else if (targ)
23469 return unify_inconsistency (explain_p, parm, targ, arg);
23470
23471 /* Make sure that ARG is not a variable-sized array. (Note
23472 that were talking about variable-sized arrays (like
23473 `int[n]'), rather than arrays of unknown size (like
23474 `int[]').) We'll get very confused by such a type since
23475 the bound of the array is not constant, and therefore
23476 not mangleable. Besides, such types are not allowed in
23477 ISO C++, so we can do as we please here. We do allow
23478 them for 'auto' deduction, since that isn't ABI-exposed. */
23479 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23480 return unify_vla_arg (explain_p, arg);
23481
23482 /* Strip typedefs as in convert_template_argument. */
23483 arg = canonicalize_type_argument (arg, tf_none);
23484 }
23485
23486 /* If ARG is a parameter pack or an expansion, we cannot unify
23487 against it unless PARM is also a parameter pack. */
23488 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23489 && !template_parameter_pack_p (parm))
23490 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23491
23492 /* If the argument deduction results is a METHOD_TYPE,
23493 then there is a problem.
23494 METHOD_TYPE doesn't map to any real C++ type the result of
23495 the deduction cannot be of that type. */
23496 if (TREE_CODE (arg) == METHOD_TYPE)
23497 return unify_method_type_error (explain_p, arg);
23498
23499 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23500 return unify_success (explain_p);
23501
23502 case TEMPLATE_PARM_INDEX:
23503 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23504 if (error_operand_p (tparm))
23505 return unify_invalid (explain_p);
23506
23507 if (TEMPLATE_PARM_LEVEL (parm)
23508 != template_decl_level (tparm))
23509 {
23510 /* The PARM is not one we're trying to unify. Just check
23511 to see if it matches ARG. */
23512 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23513 && cp_tree_equal (parm, arg));
23514 if (result)
23515 unify_expression_unequal (explain_p, parm, arg);
23516 return result;
23517 }
23518
23519 idx = TEMPLATE_PARM_IDX (parm);
23520 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23521
23522 if (targ)
23523 {
23524 if ((strict & UNIFY_ALLOW_INTEGER)
23525 && TREE_TYPE (targ) && TREE_TYPE (arg)
23526 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23527 /* We're deducing from an array bound, the type doesn't matter. */
23528 arg = fold_convert (TREE_TYPE (targ), arg);
23529 int x = !cp_tree_equal (targ, arg);
23530 if (x)
23531 unify_inconsistency (explain_p, parm, targ, arg);
23532 return x;
23533 }
23534
23535 /* [temp.deduct.type] If, in the declaration of a function template
23536 with a non-type template-parameter, the non-type
23537 template-parameter is used in an expression in the function
23538 parameter-list and, if the corresponding template-argument is
23539 deduced, the template-argument type shall match the type of the
23540 template-parameter exactly, except that a template-argument
23541 deduced from an array bound may be of any integral type.
23542 The non-type parameter might use already deduced type parameters. */
23543 tparm = TREE_TYPE (parm);
23544 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23545 /* We don't have enough levels of args to do any substitution. This
23546 can happen in the context of -fnew-ttp-matching. */;
23547 else
23548 {
23549 ++processing_template_decl;
23550 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23551 --processing_template_decl;
23552
23553 if (tree a = type_uses_auto (tparm))
23554 {
23555 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23556 if (tparm == error_mark_node)
23557 return 1;
23558 }
23559 }
23560
23561 if (!TREE_TYPE (arg))
23562 /* Template-parameter dependent expression. Just accept it for now.
23563 It will later be processed in convert_template_argument. */
23564 ;
23565 else if (same_type_ignoring_top_level_qualifiers_p
23566 (non_reference (TREE_TYPE (arg)),
23567 non_reference (tparm)))
23568 /* OK. Ignore top-level quals here because a class-type template
23569 parameter object is const. */;
23570 else if ((strict & UNIFY_ALLOW_INTEGER)
23571 && CP_INTEGRAL_TYPE_P (tparm))
23572 /* Convert the ARG to the type of PARM; the deduced non-type
23573 template argument must exactly match the types of the
23574 corresponding parameter. */
23575 arg = fold (build_nop (tparm, arg));
23576 else if (uses_template_parms (tparm))
23577 {
23578 /* We haven't deduced the type of this parameter yet. */
23579 if (cxx_dialect >= cxx17
23580 /* We deduce from array bounds in try_array_deduction. */
23581 && !(strict & UNIFY_ALLOW_INTEGER))
23582 {
23583 /* Deduce it from the non-type argument. */
23584 tree atype = TREE_TYPE (arg);
23585 RECUR_AND_CHECK_FAILURE (tparms, targs,
23586 tparm, atype,
23587 UNIFY_ALLOW_NONE, explain_p);
23588 }
23589 else
23590 /* Try again later. */
23591 return unify_success (explain_p);
23592 }
23593 else
23594 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23595
23596 /* If ARG is a parameter pack or an expansion, we cannot unify
23597 against it unless PARM is also a parameter pack. */
23598 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23599 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23600 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23601
23602 {
23603 bool removed_attr = false;
23604 arg = strip_typedefs_expr (arg, &removed_attr);
23605 }
23606 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23607 return unify_success (explain_p);
23608
23609 case PTRMEM_CST:
23610 {
23611 /* A pointer-to-member constant can be unified only with
23612 another constant. */
23613 if (TREE_CODE (arg) != PTRMEM_CST)
23614 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23615
23616 /* Just unify the class member. It would be useless (and possibly
23617 wrong, depending on the strict flags) to unify also
23618 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23619 arg refer to the same variable, even if through different
23620 classes. For instance:
23621
23622 struct A { int x; };
23623 struct B : A { };
23624
23625 Unification of &A::x and &B::x must succeed. */
23626 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23627 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23628 }
23629
23630 case POINTER_TYPE:
23631 {
23632 if (!TYPE_PTR_P (arg))
23633 return unify_type_mismatch (explain_p, parm, arg);
23634
23635 /* [temp.deduct.call]
23636
23637 A can be another pointer or pointer to member type that can
23638 be converted to the deduced A via a qualification
23639 conversion (_conv.qual_).
23640
23641 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23642 This will allow for additional cv-qualification of the
23643 pointed-to types if appropriate. */
23644
23645 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23646 /* The derived-to-base conversion only persists through one
23647 level of pointers. */
23648 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23649
23650 return unify (tparms, targs, TREE_TYPE (parm),
23651 TREE_TYPE (arg), strict, explain_p);
23652 }
23653
23654 case REFERENCE_TYPE:
23655 if (!TYPE_REF_P (arg))
23656 return unify_type_mismatch (explain_p, parm, arg);
23657 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23658 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23659
23660 case ARRAY_TYPE:
23661 if (TREE_CODE (arg) != ARRAY_TYPE)
23662 return unify_type_mismatch (explain_p, parm, arg);
23663 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23664 != (TYPE_DOMAIN (arg) == NULL_TREE))
23665 return unify_type_mismatch (explain_p, parm, arg);
23666 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23667 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23668 if (TYPE_DOMAIN (parm) != NULL_TREE)
23669 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23670 TYPE_DOMAIN (arg), explain_p);
23671 return unify_success (explain_p);
23672
23673 case REAL_TYPE:
23674 case COMPLEX_TYPE:
23675 case VECTOR_TYPE:
23676 case INTEGER_TYPE:
23677 case BOOLEAN_TYPE:
23678 case ENUMERAL_TYPE:
23679 case VOID_TYPE:
23680 case NULLPTR_TYPE:
23681 if (TREE_CODE (arg) != TREE_CODE (parm))
23682 return unify_type_mismatch (explain_p, parm, arg);
23683
23684 /* We have already checked cv-qualification at the top of the
23685 function. */
23686 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23687 return unify_type_mismatch (explain_p, parm, arg);
23688
23689 /* As far as unification is concerned, this wins. Later checks
23690 will invalidate it if necessary. */
23691 return unify_success (explain_p);
23692
23693 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23694 /* Type INTEGER_CST can come from ordinary constant template args. */
23695 case INTEGER_CST:
23696 while (CONVERT_EXPR_P (arg))
23697 arg = TREE_OPERAND (arg, 0);
23698
23699 if (TREE_CODE (arg) != INTEGER_CST)
23700 return unify_template_argument_mismatch (explain_p, parm, arg);
23701 return (tree_int_cst_equal (parm, arg)
23702 ? unify_success (explain_p)
23703 : unify_template_argument_mismatch (explain_p, parm, arg));
23704
23705 case TREE_VEC:
23706 {
23707 int i, len, argslen;
23708 int parm_variadic_p = 0;
23709
23710 if (TREE_CODE (arg) != TREE_VEC)
23711 return unify_template_argument_mismatch (explain_p, parm, arg);
23712
23713 len = TREE_VEC_LENGTH (parm);
23714 argslen = TREE_VEC_LENGTH (arg);
23715
23716 /* Check for pack expansions in the parameters. */
23717 for (i = 0; i < len; ++i)
23718 {
23719 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23720 {
23721 if (i == len - 1)
23722 /* We can unify against something with a trailing
23723 parameter pack. */
23724 parm_variadic_p = 1;
23725 else
23726 /* [temp.deduct.type]/9: If the template argument list of
23727 P contains a pack expansion that is not the last
23728 template argument, the entire template argument list
23729 is a non-deduced context. */
23730 return unify_success (explain_p);
23731 }
23732 }
23733
23734 /* If we don't have enough arguments to satisfy the parameters
23735 (not counting the pack expression at the end), or we have
23736 too many arguments for a parameter list that doesn't end in
23737 a pack expression, we can't unify. */
23738 if (parm_variadic_p
23739 ? argslen < len - parm_variadic_p
23740 : argslen != len)
23741 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23742
23743 /* Unify all of the parameters that precede the (optional)
23744 pack expression. */
23745 for (i = 0; i < len - parm_variadic_p; ++i)
23746 {
23747 RECUR_AND_CHECK_FAILURE (tparms, targs,
23748 TREE_VEC_ELT (parm, i),
23749 TREE_VEC_ELT (arg, i),
23750 UNIFY_ALLOW_NONE, explain_p);
23751 }
23752 if (parm_variadic_p)
23753 return unify_pack_expansion (tparms, targs, parm, arg,
23754 DEDUCE_EXACT,
23755 /*subr=*/true, explain_p);
23756 return unify_success (explain_p);
23757 }
23758
23759 case RECORD_TYPE:
23760 case UNION_TYPE:
23761 if (TREE_CODE (arg) != TREE_CODE (parm))
23762 return unify_type_mismatch (explain_p, parm, arg);
23763
23764 if (TYPE_PTRMEMFUNC_P (parm))
23765 {
23766 if (!TYPE_PTRMEMFUNC_P (arg))
23767 return unify_type_mismatch (explain_p, parm, arg);
23768
23769 return unify (tparms, targs,
23770 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23771 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23772 strict, explain_p);
23773 }
23774 else if (TYPE_PTRMEMFUNC_P (arg))
23775 return unify_type_mismatch (explain_p, parm, arg);
23776
23777 if (CLASSTYPE_TEMPLATE_INFO (parm))
23778 {
23779 tree t = NULL_TREE;
23780
23781 if (strict_in & UNIFY_ALLOW_DERIVED)
23782 {
23783 /* First, we try to unify the PARM and ARG directly. */
23784 t = try_class_unification (tparms, targs,
23785 parm, arg, explain_p);
23786
23787 if (!t)
23788 {
23789 /* Fallback to the special case allowed in
23790 [temp.deduct.call]:
23791
23792 If P is a class, and P has the form
23793 template-id, then A can be a derived class of
23794 the deduced A. Likewise, if P is a pointer to
23795 a class of the form template-id, A can be a
23796 pointer to a derived class pointed to by the
23797 deduced A. */
23798 enum template_base_result r;
23799 r = get_template_base (tparms, targs, parm, arg,
23800 explain_p, &t);
23801
23802 if (!t)
23803 {
23804 /* Don't give the derived diagnostic if we're
23805 already dealing with the same template. */
23806 bool same_template
23807 = (CLASSTYPE_TEMPLATE_INFO (arg)
23808 && (CLASSTYPE_TI_TEMPLATE (parm)
23809 == CLASSTYPE_TI_TEMPLATE (arg)));
23810 return unify_no_common_base (explain_p && !same_template,
23811 r, parm, arg);
23812 }
23813 }
23814 }
23815 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23816 && (CLASSTYPE_TI_TEMPLATE (parm)
23817 == CLASSTYPE_TI_TEMPLATE (arg)))
23818 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23819 Then, we should unify `int' and `U'. */
23820 t = arg;
23821 else
23822 /* There's no chance of unification succeeding. */
23823 return unify_type_mismatch (explain_p, parm, arg);
23824
23825 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23826 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23827 }
23828 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23829 return unify_type_mismatch (explain_p, parm, arg);
23830 return unify_success (explain_p);
23831
23832 case METHOD_TYPE:
23833 case FUNCTION_TYPE:
23834 {
23835 unsigned int nargs;
23836 tree *args;
23837 tree a;
23838 unsigned int i;
23839
23840 if (TREE_CODE (arg) != TREE_CODE (parm))
23841 return unify_type_mismatch (explain_p, parm, arg);
23842
23843 /* CV qualifications for methods can never be deduced, they must
23844 match exactly. We need to check them explicitly here,
23845 because type_unification_real treats them as any other
23846 cv-qualified parameter. */
23847 if (TREE_CODE (parm) == METHOD_TYPE
23848 && (!check_cv_quals_for_unify
23849 (UNIFY_ALLOW_NONE,
23850 class_of_this_parm (arg),
23851 class_of_this_parm (parm))))
23852 return unify_cv_qual_mismatch (explain_p, parm, arg);
23853 if (TREE_CODE (arg) == FUNCTION_TYPE
23854 && type_memfn_quals (parm) != type_memfn_quals (arg))
23855 return unify_cv_qual_mismatch (explain_p, parm, arg);
23856 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23857 return unify_type_mismatch (explain_p, parm, arg);
23858
23859 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23860 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23861
23862 nargs = list_length (TYPE_ARG_TYPES (arg));
23863 args = XALLOCAVEC (tree, nargs);
23864 for (a = TYPE_ARG_TYPES (arg), i = 0;
23865 a != NULL_TREE && a != void_list_node;
23866 a = TREE_CHAIN (a), ++i)
23867 args[i] = TREE_VALUE (a);
23868 nargs = i;
23869
23870 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23871 args, nargs, 1, DEDUCE_EXACT,
23872 NULL, explain_p))
23873 return 1;
23874
23875 if (flag_noexcept_type)
23876 {
23877 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23878 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23879 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23880 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23881 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23882 && uses_template_parms (TREE_PURPOSE (pspec)))
23883 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23884 TREE_PURPOSE (aspec),
23885 UNIFY_ALLOW_NONE, explain_p);
23886 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23887 return unify_type_mismatch (explain_p, parm, arg);
23888 }
23889
23890 return 0;
23891 }
23892
23893 case OFFSET_TYPE:
23894 /* Unify a pointer to member with a pointer to member function, which
23895 deduces the type of the member as a function type. */
23896 if (TYPE_PTRMEMFUNC_P (arg))
23897 {
23898 /* Check top-level cv qualifiers */
23899 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23900 return unify_cv_qual_mismatch (explain_p, parm, arg);
23901
23902 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23903 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23904 UNIFY_ALLOW_NONE, explain_p);
23905
23906 /* Determine the type of the function we are unifying against. */
23907 tree fntype = static_fn_type (arg);
23908
23909 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23910 }
23911
23912 if (TREE_CODE (arg) != OFFSET_TYPE)
23913 return unify_type_mismatch (explain_p, parm, arg);
23914 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23915 TYPE_OFFSET_BASETYPE (arg),
23916 UNIFY_ALLOW_NONE, explain_p);
23917 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23918 strict, explain_p);
23919
23920 case CONST_DECL:
23921 if (DECL_TEMPLATE_PARM_P (parm))
23922 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23923 if (arg != scalar_constant_value (parm))
23924 return unify_template_argument_mismatch (explain_p, parm, arg);
23925 return unify_success (explain_p);
23926
23927 case FIELD_DECL:
23928 case TEMPLATE_DECL:
23929 /* Matched cases are handled by the ARG == PARM test above. */
23930 return unify_template_argument_mismatch (explain_p, parm, arg);
23931
23932 case VAR_DECL:
23933 /* We might get a variable as a non-type template argument in parm if the
23934 corresponding parameter is type-dependent. Make any necessary
23935 adjustments based on whether arg is a reference. */
23936 if (CONSTANT_CLASS_P (arg))
23937 parm = fold_non_dependent_expr (parm, complain);
23938 else if (REFERENCE_REF_P (arg))
23939 {
23940 tree sub = TREE_OPERAND (arg, 0);
23941 STRIP_NOPS (sub);
23942 if (TREE_CODE (sub) == ADDR_EXPR)
23943 arg = TREE_OPERAND (sub, 0);
23944 }
23945 /* Now use the normal expression code to check whether they match. */
23946 goto expr;
23947
23948 case TYPE_ARGUMENT_PACK:
23949 case NONTYPE_ARGUMENT_PACK:
23950 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23951 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23952
23953 case TYPEOF_TYPE:
23954 case DECLTYPE_TYPE:
23955 case UNDERLYING_TYPE:
23956 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23957 or UNDERLYING_TYPE nodes. */
23958 return unify_success (explain_p);
23959
23960 case ERROR_MARK:
23961 /* Unification fails if we hit an error node. */
23962 return unify_invalid (explain_p);
23963
23964 case INDIRECT_REF:
23965 if (REFERENCE_REF_P (parm))
23966 {
23967 bool pexp = PACK_EXPANSION_P (arg);
23968 if (pexp)
23969 arg = PACK_EXPANSION_PATTERN (arg);
23970 if (REFERENCE_REF_P (arg))
23971 arg = TREE_OPERAND (arg, 0);
23972 if (pexp)
23973 arg = make_pack_expansion (arg, complain);
23974 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23975 strict, explain_p);
23976 }
23977 /* FALLTHRU */
23978
23979 default:
23980 /* An unresolved overload is a nondeduced context. */
23981 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23982 return unify_success (explain_p);
23983 gcc_assert (EXPR_P (parm)
23984 || COMPOUND_LITERAL_P (parm)
23985 || TREE_CODE (parm) == TRAIT_EXPR);
23986 expr:
23987 /* We must be looking at an expression. This can happen with
23988 something like:
23989
23990 template <int I>
23991 void foo(S<I>, S<I + 2>);
23992
23993 or
23994
23995 template<typename T>
23996 void foo(A<T, T{}>);
23997
23998 This is a "non-deduced context":
23999
24000 [deduct.type]
24001
24002 The non-deduced contexts are:
24003
24004 --A non-type template argument or an array bound in which
24005 a subexpression references a template parameter.
24006
24007 In these cases, we assume deduction succeeded, but don't
24008 actually infer any unifications. */
24009
24010 if (!uses_template_parms (parm)
24011 && !template_args_equal (parm, arg))
24012 return unify_expression_unequal (explain_p, parm, arg);
24013 else
24014 return unify_success (explain_p);
24015 }
24016 }
24017 #undef RECUR_AND_CHECK_FAILURE
24018
24019 /* Note that DECL can be defined in this translation unit, if
24020 required. */
24021
24022 static void
mark_definable(tree decl)24023 mark_definable (tree decl)
24024 {
24025 tree clone;
24026 DECL_NOT_REALLY_EXTERN (decl) = 1;
24027 FOR_EACH_CLONE (clone, decl)
24028 DECL_NOT_REALLY_EXTERN (clone) = 1;
24029 }
24030
24031 /* Called if RESULT is explicitly instantiated, or is a member of an
24032 explicitly instantiated class. */
24033
24034 void
mark_decl_instantiated(tree result,int extern_p)24035 mark_decl_instantiated (tree result, int extern_p)
24036 {
24037 SET_DECL_EXPLICIT_INSTANTIATION (result);
24038
24039 /* If this entity has already been written out, it's too late to
24040 make any modifications. */
24041 if (TREE_ASM_WRITTEN (result))
24042 return;
24043
24044 /* consteval functions are never emitted. */
24045 if (TREE_CODE (result) == FUNCTION_DECL
24046 && DECL_IMMEDIATE_FUNCTION_P (result))
24047 return;
24048
24049 /* For anonymous namespace we don't need to do anything. */
24050 if (decl_anon_ns_mem_p (result))
24051 {
24052 gcc_assert (!TREE_PUBLIC (result));
24053 return;
24054 }
24055
24056 if (TREE_CODE (result) != FUNCTION_DECL)
24057 /* The TREE_PUBLIC flag for function declarations will have been
24058 set correctly by tsubst. */
24059 TREE_PUBLIC (result) = 1;
24060
24061 /* This might have been set by an earlier implicit instantiation. */
24062 DECL_COMDAT (result) = 0;
24063
24064 if (extern_p)
24065 {
24066 DECL_EXTERNAL (result) = 1;
24067 DECL_NOT_REALLY_EXTERN (result) = 0;
24068 }
24069 else
24070 {
24071 mark_definable (result);
24072 mark_needed (result);
24073 /* Always make artificials weak. */
24074 if (DECL_ARTIFICIAL (result) && flag_weak)
24075 comdat_linkage (result);
24076 /* For WIN32 we also want to put explicit instantiations in
24077 linkonce sections. */
24078 else if (TREE_PUBLIC (result))
24079 maybe_make_one_only (result);
24080 if (TREE_CODE (result) == FUNCTION_DECL
24081 && DECL_TEMPLATE_INSTANTIATED (result))
24082 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24083 since start_preparsed_function wouldn't have if we had an earlier
24084 extern explicit instantiation. */
24085 DECL_EXTERNAL (result) = 0;
24086 }
24087
24088 /* If EXTERN_P, then this function will not be emitted -- unless
24089 followed by an explicit instantiation, at which point its linkage
24090 will be adjusted. If !EXTERN_P, then this function will be
24091 emitted here. In neither circumstance do we want
24092 import_export_decl to adjust the linkage. */
24093 DECL_INTERFACE_KNOWN (result) = 1;
24094 }
24095
24096 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24097 important template arguments. If any are missing, we check whether
24098 they're important by using error_mark_node for substituting into any
24099 args that were used for partial ordering (the ones between ARGS and END)
24100 and seeing if it bubbles up. */
24101
24102 static bool
check_undeduced_parms(tree targs,tree args,tree end)24103 check_undeduced_parms (tree targs, tree args, tree end)
24104 {
24105 bool found = false;
24106 int i;
24107 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
24108 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
24109 {
24110 found = true;
24111 TREE_VEC_ELT (targs, i) = error_mark_node;
24112 }
24113 if (found)
24114 {
24115 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
24116 if (substed == error_mark_node)
24117 return true;
24118 }
24119 return false;
24120 }
24121
24122 /* Given two function templates PAT1 and PAT2, return:
24123
24124 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24125 -1 if PAT2 is more specialized than PAT1.
24126 0 if neither is more specialized.
24127
24128 LEN indicates the number of parameters we should consider
24129 (defaulted parameters should not be considered).
24130
24131 The 1998 std underspecified function template partial ordering, and
24132 DR214 addresses the issue. We take pairs of arguments, one from
24133 each of the templates, and deduce them against each other. One of
24134 the templates will be more specialized if all the *other*
24135 template's arguments deduce against its arguments and at least one
24136 of its arguments *does* *not* deduce against the other template's
24137 corresponding argument. Deduction is done as for class templates.
24138 The arguments used in deduction have reference and top level cv
24139 qualifiers removed. Iff both arguments were originally reference
24140 types *and* deduction succeeds in both directions, an lvalue reference
24141 wins against an rvalue reference and otherwise the template
24142 with the more cv-qualified argument wins for that pairing (if
24143 neither is more cv-qualified, they both are equal). Unlike regular
24144 deduction, after all the arguments have been deduced in this way,
24145 we do *not* verify the deduced template argument values can be
24146 substituted into non-deduced contexts.
24147
24148 The logic can be a bit confusing here, because we look at deduce1 and
24149 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24150 can find template arguments for pat1 to make arg1 look like arg2, that
24151 means that arg2 is at least as specialized as arg1. */
24152
24153 int
more_specialized_fn(tree pat1,tree pat2,int len)24154 more_specialized_fn (tree pat1, tree pat2, int len)
24155 {
24156 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24157 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24158 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24159 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24160 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24161 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24162 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24163 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24164 tree origs1, origs2;
24165 bool lose1 = false;
24166 bool lose2 = false;
24167
24168 /* Remove the this parameter from non-static member functions. If
24169 one is a non-static member function and the other is not a static
24170 member function, remove the first parameter from that function
24171 also. This situation occurs for operator functions where we
24172 locate both a member function (with this pointer) and non-member
24173 operator (with explicit first operand). */
24174 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24175 {
24176 len--; /* LEN is the number of significant arguments for DECL1 */
24177 args1 = TREE_CHAIN (args1);
24178 if (!DECL_STATIC_FUNCTION_P (decl2))
24179 args2 = TREE_CHAIN (args2);
24180 }
24181 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24182 {
24183 args2 = TREE_CHAIN (args2);
24184 if (!DECL_STATIC_FUNCTION_P (decl1))
24185 {
24186 len--;
24187 args1 = TREE_CHAIN (args1);
24188 }
24189 }
24190
24191 /* If only one is a conversion operator, they are unordered. */
24192 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24193 return 0;
24194
24195 /* Consider the return type for a conversion function */
24196 if (DECL_CONV_FN_P (decl1))
24197 {
24198 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24199 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24200 len++;
24201 }
24202
24203 processing_template_decl++;
24204
24205 origs1 = args1;
24206 origs2 = args2;
24207
24208 while (len--
24209 /* Stop when an ellipsis is seen. */
24210 && args1 != NULL_TREE && args2 != NULL_TREE)
24211 {
24212 tree arg1 = TREE_VALUE (args1);
24213 tree arg2 = TREE_VALUE (args2);
24214 int deduce1, deduce2;
24215 int quals1 = -1;
24216 int quals2 = -1;
24217 int ref1 = 0;
24218 int ref2 = 0;
24219
24220 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24221 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24222 {
24223 /* When both arguments are pack expansions, we need only
24224 unify the patterns themselves. */
24225 arg1 = PACK_EXPANSION_PATTERN (arg1);
24226 arg2 = PACK_EXPANSION_PATTERN (arg2);
24227
24228 /* This is the last comparison we need to do. */
24229 len = 0;
24230 }
24231
24232 /* DR 1847: If a particular P contains no template-parameters that
24233 participate in template argument deduction, that P is not used to
24234 determine the ordering. */
24235 if (!uses_deducible_template_parms (arg1)
24236 && !uses_deducible_template_parms (arg2))
24237 goto next;
24238
24239 if (TYPE_REF_P (arg1))
24240 {
24241 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24242 arg1 = TREE_TYPE (arg1);
24243 quals1 = cp_type_quals (arg1);
24244 }
24245
24246 if (TYPE_REF_P (arg2))
24247 {
24248 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24249 arg2 = TREE_TYPE (arg2);
24250 quals2 = cp_type_quals (arg2);
24251 }
24252
24253 arg1 = TYPE_MAIN_VARIANT (arg1);
24254 arg2 = TYPE_MAIN_VARIANT (arg2);
24255
24256 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24257 {
24258 int i, len2 = remaining_arguments (args2);
24259 tree parmvec = make_tree_vec (1);
24260 tree argvec = make_tree_vec (len2);
24261 tree ta = args2;
24262
24263 /* Setup the parameter vector, which contains only ARG1. */
24264 TREE_VEC_ELT (parmvec, 0) = arg1;
24265
24266 /* Setup the argument vector, which contains the remaining
24267 arguments. */
24268 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24269 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24270
24271 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24272 argvec, DEDUCE_EXACT,
24273 /*subr=*/true, /*explain_p=*/false)
24274 == 0);
24275
24276 /* We cannot deduce in the other direction, because ARG1 is
24277 a pack expansion but ARG2 is not. */
24278 deduce2 = 0;
24279 }
24280 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24281 {
24282 int i, len1 = remaining_arguments (args1);
24283 tree parmvec = make_tree_vec (1);
24284 tree argvec = make_tree_vec (len1);
24285 tree ta = args1;
24286
24287 /* Setup the parameter vector, which contains only ARG1. */
24288 TREE_VEC_ELT (parmvec, 0) = arg2;
24289
24290 /* Setup the argument vector, which contains the remaining
24291 arguments. */
24292 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24293 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24294
24295 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24296 argvec, DEDUCE_EXACT,
24297 /*subr=*/true, /*explain_p=*/false)
24298 == 0);
24299
24300 /* We cannot deduce in the other direction, because ARG2 is
24301 a pack expansion but ARG1 is not.*/
24302 deduce1 = 0;
24303 }
24304
24305 else
24306 {
24307 /* The normal case, where neither argument is a pack
24308 expansion. */
24309 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24310 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24311 == 0);
24312 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24313 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24314 == 0);
24315 }
24316
24317 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24318 arg2, then arg2 is not as specialized as arg1. */
24319 if (!deduce1)
24320 lose2 = true;
24321 if (!deduce2)
24322 lose1 = true;
24323
24324 /* "If, for a given type, deduction succeeds in both directions
24325 (i.e., the types are identical after the transformations above)
24326 and both P and A were reference types (before being replaced with
24327 the type referred to above):
24328 - if the type from the argument template was an lvalue reference and
24329 the type from the parameter template was not, the argument type is
24330 considered to be more specialized than the other; otherwise,
24331 - if the type from the argument template is more cv-qualified
24332 than the type from the parameter template (as described above),
24333 the argument type is considered to be more specialized than the other;
24334 otherwise,
24335 - neither type is more specialized than the other." */
24336
24337 if (deduce1 && deduce2)
24338 {
24339 if (ref1 && ref2 && ref1 != ref2)
24340 {
24341 if (ref1 > ref2)
24342 lose1 = true;
24343 else
24344 lose2 = true;
24345 }
24346 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24347 {
24348 if ((quals1 & quals2) == quals2)
24349 lose2 = true;
24350 if ((quals1 & quals2) == quals1)
24351 lose1 = true;
24352 }
24353 }
24354
24355 if (lose1 && lose2)
24356 /* We've failed to deduce something in either direction.
24357 These must be unordered. */
24358 break;
24359
24360 next:
24361
24362 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24363 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24364 /* We have already processed all of the arguments in our
24365 handing of the pack expansion type. */
24366 len = 0;
24367
24368 args1 = TREE_CHAIN (args1);
24369 args2 = TREE_CHAIN (args2);
24370 }
24371
24372 /* "In most cases, all template parameters must have values in order for
24373 deduction to succeed, but for partial ordering purposes a template
24374 parameter may remain without a value provided it is not used in the
24375 types being used for partial ordering."
24376
24377 Thus, if we are missing any of the targs1 we need to substitute into
24378 origs1, then pat2 is not as specialized as pat1. This can happen when
24379 there is a nondeduced context. */
24380 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24381 lose2 = true;
24382 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24383 lose1 = true;
24384
24385 processing_template_decl--;
24386
24387 /* If both deductions succeed, the partial ordering selects the more
24388 constrained template. */
24389 /* P2113: If the corresponding template-parameters of the
24390 template-parameter-lists are not equivalent ([temp.over.link]) or if
24391 the function parameters that positionally correspond between the two
24392 templates are not of the same type, neither template is more
24393 specialized than the other. */
24394 if (!lose1 && !lose2
24395 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24396 DECL_TEMPLATE_PARMS (pat2))
24397 && compparms (origs1, origs2))
24398 {
24399 int winner = more_constrained (decl1, decl2);
24400 if (winner > 0)
24401 lose2 = true;
24402 else if (winner < 0)
24403 lose1 = true;
24404 }
24405
24406 /* All things being equal, if the next argument is a pack expansion
24407 for one function but not for the other, prefer the
24408 non-variadic function. FIXME this is bogus; see c++/41958. */
24409 if (lose1 == lose2
24410 && args1 && TREE_VALUE (args1)
24411 && args2 && TREE_VALUE (args2))
24412 {
24413 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24414 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24415 }
24416
24417 if (lose1 == lose2)
24418 return 0;
24419 else if (!lose1)
24420 return 1;
24421 else
24422 return -1;
24423 }
24424
24425 /* Determine which of two partial specializations of TMPL is more
24426 specialized.
24427
24428 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24429 to the first partial specialization. The TREE_PURPOSE is the
24430 innermost set of template parameters for the partial
24431 specialization. PAT2 is similar, but for the second template.
24432
24433 Return 1 if the first partial specialization is more specialized;
24434 -1 if the second is more specialized; 0 if neither is more
24435 specialized.
24436
24437 See [temp.class.order] for information about determining which of
24438 two templates is more specialized. */
24439
24440 static int
more_specialized_partial_spec(tree tmpl,tree pat1,tree pat2)24441 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24442 {
24443 tree targs;
24444 int winner = 0;
24445 bool any_deductions = false;
24446
24447 tree tmpl1 = TREE_VALUE (pat1);
24448 tree tmpl2 = TREE_VALUE (pat2);
24449 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24450 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24451
24452 /* Just like what happens for functions, if we are ordering between
24453 different template specializations, we may encounter dependent
24454 types in the arguments, and we need our dependency check functions
24455 to behave correctly. */
24456 ++processing_template_decl;
24457 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24458 if (targs)
24459 {
24460 --winner;
24461 any_deductions = true;
24462 }
24463
24464 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24465 if (targs)
24466 {
24467 ++winner;
24468 any_deductions = true;
24469 }
24470 --processing_template_decl;
24471
24472 /* If both deductions succeed, the partial ordering selects the more
24473 constrained template. */
24474 if (!winner && any_deductions)
24475 winner = more_constrained (tmpl1, tmpl2);
24476
24477 /* In the case of a tie where at least one of the templates
24478 has a parameter pack at the end, the template with the most
24479 non-packed parameters wins. */
24480 if (winner == 0
24481 && any_deductions
24482 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24483 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24484 {
24485 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24486 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24487 int len1 = TREE_VEC_LENGTH (args1);
24488 int len2 = TREE_VEC_LENGTH (args2);
24489
24490 /* We don't count the pack expansion at the end. */
24491 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24492 --len1;
24493 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24494 --len2;
24495
24496 if (len1 > len2)
24497 return 1;
24498 else if (len1 < len2)
24499 return -1;
24500 }
24501
24502 return winner;
24503 }
24504
24505 /* Return the template arguments that will produce the function signature
24506 DECL from the function template FN, with the explicit template
24507 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24508 also match. Return NULL_TREE if no satisfactory arguments could be
24509 found. */
24510
24511 static tree
get_bindings(tree fn,tree decl,tree explicit_args,bool check_rettype)24512 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24513 {
24514 int ntparms = DECL_NTPARMS (fn);
24515 tree targs = make_tree_vec (ntparms);
24516 tree decl_type = TREE_TYPE (decl);
24517 tree decl_arg_types;
24518 tree *args;
24519 unsigned int nargs, ix;
24520 tree arg;
24521
24522 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24523
24524 /* Never do unification on the 'this' parameter. */
24525 decl_arg_types = skip_artificial_parms_for (decl,
24526 TYPE_ARG_TYPES (decl_type));
24527
24528 nargs = list_length (decl_arg_types);
24529 args = XALLOCAVEC (tree, nargs);
24530 for (arg = decl_arg_types, ix = 0;
24531 arg != NULL_TREE && arg != void_list_node;
24532 arg = TREE_CHAIN (arg), ++ix)
24533 args[ix] = TREE_VALUE (arg);
24534
24535 if (fn_type_unification (fn, explicit_args, targs,
24536 args, ix,
24537 (check_rettype || DECL_CONV_FN_P (fn)
24538 ? TREE_TYPE (decl_type) : NULL_TREE),
24539 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24540 /*explain_p=*/false,
24541 /*decltype*/false)
24542 == error_mark_node)
24543 return NULL_TREE;
24544
24545 return targs;
24546 }
24547
24548 /* Return the innermost template arguments that, when applied to a partial
24549 specialization SPEC_TMPL of TMPL, yield the ARGS.
24550
24551 For example, suppose we have:
24552
24553 template <class T, class U> struct S {};
24554 template <class T> struct S<T*, int> {};
24555
24556 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24557 partial specialization and the ARGS will be {double*, int}. The resulting
24558 vector will be {double}, indicating that `T' is bound to `double'. */
24559
24560 static tree
get_partial_spec_bindings(tree tmpl,tree spec_tmpl,tree args)24561 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24562 {
24563 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24564 tree spec_args
24565 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24566 int i, ntparms = TREE_VEC_LENGTH (tparms);
24567 tree deduced_args;
24568 tree innermost_deduced_args;
24569
24570 innermost_deduced_args = make_tree_vec (ntparms);
24571 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24572 {
24573 deduced_args = copy_node (args);
24574 SET_TMPL_ARGS_LEVEL (deduced_args,
24575 TMPL_ARGS_DEPTH (deduced_args),
24576 innermost_deduced_args);
24577 }
24578 else
24579 deduced_args = innermost_deduced_args;
24580
24581 bool tried_array_deduction = (cxx_dialect < cxx17);
24582 again:
24583 if (unify (tparms, deduced_args,
24584 INNERMOST_TEMPLATE_ARGS (spec_args),
24585 INNERMOST_TEMPLATE_ARGS (args),
24586 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24587 return NULL_TREE;
24588
24589 for (i = 0; i < ntparms; ++i)
24590 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24591 {
24592 if (!tried_array_deduction)
24593 {
24594 try_array_deduction (tparms, innermost_deduced_args,
24595 INNERMOST_TEMPLATE_ARGS (spec_args));
24596 tried_array_deduction = true;
24597 if (TREE_VEC_ELT (innermost_deduced_args, i))
24598 goto again;
24599 }
24600 return NULL_TREE;
24601 }
24602
24603 if (!push_tinst_level (spec_tmpl, deduced_args))
24604 {
24605 excessive_deduction_depth = true;
24606 return NULL_TREE;
24607 }
24608
24609 /* Verify that nondeduced template arguments agree with the type
24610 obtained from argument deduction.
24611
24612 For example:
24613
24614 struct A { typedef int X; };
24615 template <class T, class U> struct C {};
24616 template <class T> struct C<T, typename T::X> {};
24617
24618 Then with the instantiation `C<A, int>', we can deduce that
24619 `T' is `A' but unify () does not check whether `typename T::X'
24620 is `int'. */
24621 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24622
24623 if (spec_args != error_mark_node)
24624 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24625 INNERMOST_TEMPLATE_ARGS (spec_args),
24626 tmpl, tf_none, false, false);
24627
24628 pop_tinst_level ();
24629
24630 if (spec_args == error_mark_node
24631 /* We only need to check the innermost arguments; the other
24632 arguments will always agree. */
24633 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24634 INNERMOST_TEMPLATE_ARGS (args)))
24635 return NULL_TREE;
24636
24637 /* Now that we have bindings for all of the template arguments,
24638 ensure that the arguments deduced for the template template
24639 parameters have compatible template parameter lists. See the use
24640 of template_template_parm_bindings_ok_p in fn_type_unification
24641 for more information. */
24642 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24643 return NULL_TREE;
24644
24645 return deduced_args;
24646 }
24647
24648 // Compare two function templates T1 and T2 by deducing bindings
24649 // from one against the other. If both deductions succeed, compare
24650 // constraints to see which is more constrained.
24651 static int
more_specialized_inst(tree t1,tree t2)24652 more_specialized_inst (tree t1, tree t2)
24653 {
24654 int fate = 0;
24655 int count = 0;
24656
24657 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24658 {
24659 --fate;
24660 ++count;
24661 }
24662
24663 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24664 {
24665 ++fate;
24666 ++count;
24667 }
24668
24669 // If both deductions succeed, then one may be more constrained.
24670 if (count == 2 && fate == 0)
24671 fate = more_constrained (t1, t2);
24672
24673 return fate;
24674 }
24675
24676 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24677 Return the TREE_LIST node with the most specialized template, if
24678 any. If there is no most specialized template, the error_mark_node
24679 is returned.
24680
24681 Note that this function does not look at, or modify, the
24682 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24683 returned is one of the elements of INSTANTIATIONS, callers may
24684 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24685 and retrieve it from the value returned. */
24686
24687 tree
most_specialized_instantiation(tree templates)24688 most_specialized_instantiation (tree templates)
24689 {
24690 tree fn, champ;
24691
24692 ++processing_template_decl;
24693
24694 champ = templates;
24695 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24696 {
24697 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24698 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24699 if (fate == -1)
24700 champ = fn;
24701 else if (!fate)
24702 {
24703 /* Equally specialized, move to next function. If there
24704 is no next function, nothing's most specialized. */
24705 fn = TREE_CHAIN (fn);
24706 champ = fn;
24707 if (!fn)
24708 break;
24709 }
24710 }
24711
24712 if (champ)
24713 /* Now verify that champ is better than everything earlier in the
24714 instantiation list. */
24715 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24716 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24717 {
24718 champ = NULL_TREE;
24719 break;
24720 }
24721 }
24722
24723 processing_template_decl--;
24724
24725 if (!champ)
24726 return error_mark_node;
24727
24728 return champ;
24729 }
24730
24731 /* If DECL is a specialization of some template, return the most
24732 general such template. Otherwise, returns NULL_TREE.
24733
24734 For example, given:
24735
24736 template <class T> struct S { template <class U> void f(U); };
24737
24738 if TMPL is `template <class U> void S<int>::f(U)' this will return
24739 the full template. This function will not trace past partial
24740 specializations, however. For example, given in addition:
24741
24742 template <class T> struct S<T*> { template <class U> void f(U); };
24743
24744 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24745 `template <class T> template <class U> S<T*>::f(U)'. */
24746
24747 tree
most_general_template(tree decl)24748 most_general_template (tree decl)
24749 {
24750 if (TREE_CODE (decl) != TEMPLATE_DECL)
24751 {
24752 if (tree tinfo = get_template_info (decl))
24753 decl = TI_TEMPLATE (tinfo);
24754 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24755 template friend, or a FIELD_DECL for a capture pack. */
24756 if (TREE_CODE (decl) != TEMPLATE_DECL)
24757 return NULL_TREE;
24758 }
24759
24760 /* Look for more and more general templates. */
24761 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24762 {
24763 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24764 (See cp-tree.h for details.) */
24765 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24766 break;
24767
24768 if (CLASS_TYPE_P (TREE_TYPE (decl))
24769 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24770 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24771 break;
24772
24773 /* Stop if we run into an explicitly specialized class template. */
24774 if (!DECL_NAMESPACE_SCOPE_P (decl)
24775 && DECL_CONTEXT (decl)
24776 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24777 break;
24778
24779 decl = DECL_TI_TEMPLATE (decl);
24780 }
24781
24782 return decl;
24783 }
24784
24785 /* Return the most specialized of the template partial specializations
24786 which can produce TARGET, a specialization of some class or variable
24787 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24788 a TEMPLATE_DECL node corresponding to the partial specialization, while
24789 the TREE_PURPOSE is the set of template arguments that must be
24790 substituted into the template pattern in order to generate TARGET.
24791
24792 If the choice of partial specialization is ambiguous, a diagnostic
24793 is issued, and the error_mark_node is returned. If there are no
24794 partial specializations matching TARGET, then NULL_TREE is
24795 returned, indicating that the primary template should be used. */
24796
24797 tree
most_specialized_partial_spec(tree target,tsubst_flags_t complain)24798 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24799 {
24800 tree list = NULL_TREE;
24801 tree t;
24802 tree champ;
24803 int fate;
24804 bool ambiguous_p;
24805 tree outer_args = NULL_TREE;
24806 tree tmpl, args;
24807
24808 if (TYPE_P (target))
24809 {
24810 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24811 tmpl = TI_TEMPLATE (tinfo);
24812 args = TI_ARGS (tinfo);
24813 }
24814 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24815 {
24816 tmpl = TREE_OPERAND (target, 0);
24817 args = TREE_OPERAND (target, 1);
24818 }
24819 else if (VAR_P (target))
24820 {
24821 tree tinfo = DECL_TEMPLATE_INFO (target);
24822 tmpl = TI_TEMPLATE (tinfo);
24823 args = TI_ARGS (tinfo);
24824 }
24825 else
24826 gcc_unreachable ();
24827
24828 tree main_tmpl = most_general_template (tmpl);
24829
24830 /* For determining which partial specialization to use, only the
24831 innermost args are interesting. */
24832 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24833 {
24834 outer_args = strip_innermost_template_args (args, 1);
24835 args = INNERMOST_TEMPLATE_ARGS (args);
24836 }
24837
24838 /* The caller hasn't called push_to_top_level yet, but we need
24839 get_partial_spec_bindings to be done in non-template context so that we'll
24840 fully resolve everything. */
24841 processing_template_decl_sentinel ptds;
24842
24843 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24844 {
24845 const tree ospec_tmpl = TREE_VALUE (t);
24846
24847 tree spec_tmpl;
24848 if (outer_args)
24849 {
24850 /* Substitute in the template args from the enclosing class. */
24851 ++processing_template_decl;
24852 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24853 --processing_template_decl;
24854 if (spec_tmpl == error_mark_node)
24855 return error_mark_node;
24856 }
24857 else
24858 spec_tmpl = ospec_tmpl;
24859
24860 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24861 if (spec_args)
24862 {
24863 if (outer_args)
24864 spec_args = add_to_template_args (outer_args, spec_args);
24865
24866 /* Keep the candidate only if the constraints are satisfied,
24867 or if we're not compiling with concepts. */
24868 if (!flag_concepts
24869 || constraints_satisfied_p (ospec_tmpl, spec_args))
24870 {
24871 list = tree_cons (spec_args, ospec_tmpl, list);
24872 TREE_TYPE (list) = TREE_TYPE (t);
24873 }
24874 }
24875 }
24876
24877 if (! list)
24878 return NULL_TREE;
24879
24880 ambiguous_p = false;
24881 t = list;
24882 champ = t;
24883 t = TREE_CHAIN (t);
24884 for (; t; t = TREE_CHAIN (t))
24885 {
24886 fate = more_specialized_partial_spec (tmpl, champ, t);
24887 if (fate == 1)
24888 ;
24889 else
24890 {
24891 if (fate == 0)
24892 {
24893 t = TREE_CHAIN (t);
24894 if (! t)
24895 {
24896 ambiguous_p = true;
24897 break;
24898 }
24899 }
24900 champ = t;
24901 }
24902 }
24903
24904 if (!ambiguous_p)
24905 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24906 {
24907 fate = more_specialized_partial_spec (tmpl, champ, t);
24908 if (fate != 1)
24909 {
24910 ambiguous_p = true;
24911 break;
24912 }
24913 }
24914
24915 if (ambiguous_p)
24916 {
24917 const char *str;
24918 char *spaces = NULL;
24919 if (!(complain & tf_error))
24920 return error_mark_node;
24921 if (TYPE_P (target))
24922 error ("ambiguous template instantiation for %q#T", target);
24923 else
24924 error ("ambiguous template instantiation for %q#D", target);
24925 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24926 for (t = list; t; t = TREE_CHAIN (t))
24927 {
24928 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24929 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24930 "%s %#qS", spaces ? spaces : str, subst);
24931 spaces = spaces ? spaces : get_spaces (str);
24932 }
24933 free (spaces);
24934 return error_mark_node;
24935 }
24936
24937 return champ;
24938 }
24939
24940 /* Explicitly instantiate DECL. */
24941
24942 void
do_decl_instantiation(tree decl,tree storage)24943 do_decl_instantiation (tree decl, tree storage)
24944 {
24945 tree result = NULL_TREE;
24946 int extern_p = 0;
24947
24948 if (!decl || decl == error_mark_node)
24949 /* An error occurred, for which grokdeclarator has already issued
24950 an appropriate message. */
24951 return;
24952 else if (! DECL_LANG_SPECIFIC (decl))
24953 {
24954 error ("explicit instantiation of non-template %q#D", decl);
24955 return;
24956 }
24957 else if (DECL_DECLARED_CONCEPT_P (decl))
24958 {
24959 if (VAR_P (decl))
24960 error ("explicit instantiation of variable concept %q#D", decl);
24961 else
24962 error ("explicit instantiation of function concept %q#D", decl);
24963 return;
24964 }
24965
24966 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24967 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24968
24969 if (VAR_P (decl) && !var_templ)
24970 {
24971 /* There is an asymmetry here in the way VAR_DECLs and
24972 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24973 the latter, the DECL we get back will be marked as a
24974 template instantiation, and the appropriate
24975 DECL_TEMPLATE_INFO will be set up. This does not happen for
24976 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24977 should handle VAR_DECLs as it currently handles
24978 FUNCTION_DECLs. */
24979 if (!DECL_CLASS_SCOPE_P (decl))
24980 {
24981 error ("%qD is not a static data member of a class template", decl);
24982 return;
24983 }
24984 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24985 if (!result || !VAR_P (result))
24986 {
24987 error ("no matching template for %qD found", decl);
24988 return;
24989 }
24990 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24991 {
24992 error ("type %qT for explicit instantiation %qD does not match "
24993 "declared type %qT", TREE_TYPE (result), decl,
24994 TREE_TYPE (decl));
24995 return;
24996 }
24997 }
24998 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24999 {
25000 error ("explicit instantiation of %q#D", decl);
25001 return;
25002 }
25003 else
25004 result = decl;
25005
25006 /* Check for various error cases. Note that if the explicit
25007 instantiation is valid the RESULT will currently be marked as an
25008 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25009 until we get here. */
25010
25011 if (DECL_TEMPLATE_SPECIALIZATION (result))
25012 {
25013 /* DR 259 [temp.spec].
25014
25015 Both an explicit instantiation and a declaration of an explicit
25016 specialization shall not appear in a program unless the explicit
25017 instantiation follows a declaration of the explicit specialization.
25018
25019 For a given set of template parameters, if an explicit
25020 instantiation of a template appears after a declaration of an
25021 explicit specialization for that template, the explicit
25022 instantiation has no effect. */
25023 return;
25024 }
25025 else if (DECL_EXPLICIT_INSTANTIATION (result))
25026 {
25027 /* [temp.spec]
25028
25029 No program shall explicitly instantiate any template more
25030 than once.
25031
25032 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25033 the first instantiation was `extern' and the second is not,
25034 and EXTERN_P for the opposite case. */
25035 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
25036 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
25037 /* If an "extern" explicit instantiation follows an ordinary
25038 explicit instantiation, the template is instantiated. */
25039 if (extern_p)
25040 return;
25041 }
25042 else if (!DECL_IMPLICIT_INSTANTIATION (result))
25043 {
25044 error ("no matching template for %qD found", result);
25045 return;
25046 }
25047 else if (!DECL_TEMPLATE_INFO (result))
25048 {
25049 permerror (input_location, "explicit instantiation of non-template %q#D", result);
25050 return;
25051 }
25052
25053 if (storage == NULL_TREE)
25054 ;
25055 else if (storage == ridpointers[(int) RID_EXTERN])
25056 {
25057 if (cxx_dialect == cxx98)
25058 pedwarn (input_location, OPT_Wpedantic,
25059 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25060 "instantiations");
25061 extern_p = 1;
25062 }
25063 else
25064 error ("storage class %qD applied to template instantiation", storage);
25065
25066 check_explicit_instantiation_namespace (result);
25067 mark_decl_instantiated (result, extern_p);
25068 if (! extern_p)
25069 instantiate_decl (result, /*defer_ok=*/true,
25070 /*expl_inst_class_mem_p=*/false);
25071 }
25072
25073 static void
mark_class_instantiated(tree t,int extern_p)25074 mark_class_instantiated (tree t, int extern_p)
25075 {
25076 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
25077 SET_CLASSTYPE_INTERFACE_KNOWN (t);
25078 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
25079 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
25080 if (! extern_p)
25081 {
25082 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
25083 rest_of_type_compilation (t, 1);
25084 }
25085 }
25086
25087 /* Called from do_type_instantiation through binding_table_foreach to
25088 do recursive instantiation for the type bound in ENTRY. */
25089 static void
bt_instantiate_type_proc(binding_entry entry,void * data)25090 bt_instantiate_type_proc (binding_entry entry, void *data)
25091 {
25092 tree storage = *(tree *) data;
25093
25094 if (MAYBE_CLASS_TYPE_P (entry->type)
25095 && CLASSTYPE_TEMPLATE_INFO (entry->type)
25096 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
25097 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
25098 }
25099
25100 /* Perform an explicit instantiation of template class T. STORAGE, if
25101 non-null, is the RID for extern, inline or static. COMPLAIN is
25102 nonzero if this is called from the parser, zero if called recursively,
25103 since the standard is unclear (as detailed below). */
25104
25105 void
do_type_instantiation(tree t,tree storage,tsubst_flags_t complain)25106 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
25107 {
25108 int extern_p = 0;
25109 int nomem_p = 0;
25110 int static_p = 0;
25111 int previous_instantiation_extern_p = 0;
25112
25113 if (TREE_CODE (t) == TYPE_DECL)
25114 t = TREE_TYPE (t);
25115
25116 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
25117 {
25118 tree tmpl =
25119 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
25120 if (tmpl)
25121 error ("explicit instantiation of non-class template %qD", tmpl);
25122 else
25123 error ("explicit instantiation of non-template type %qT", t);
25124 return;
25125 }
25126
25127 complete_type (t);
25128
25129 if (!COMPLETE_TYPE_P (t))
25130 {
25131 if (complain & tf_error)
25132 error ("explicit instantiation of %q#T before definition of template",
25133 t);
25134 return;
25135 }
25136
25137 if (storage != NULL_TREE)
25138 {
25139 if (storage == ridpointers[(int) RID_EXTERN])
25140 {
25141 if (cxx_dialect == cxx98)
25142 pedwarn (input_location, OPT_Wpedantic,
25143 "ISO C++ 1998 forbids the use of %<extern%> on "
25144 "explicit instantiations");
25145 }
25146 else
25147 pedwarn (input_location, OPT_Wpedantic,
25148 "ISO C++ forbids the use of %qE"
25149 " on explicit instantiations", storage);
25150
25151 if (storage == ridpointers[(int) RID_INLINE])
25152 nomem_p = 1;
25153 else if (storage == ridpointers[(int) RID_EXTERN])
25154 extern_p = 1;
25155 else if (storage == ridpointers[(int) RID_STATIC])
25156 static_p = 1;
25157 else
25158 {
25159 error ("storage class %qD applied to template instantiation",
25160 storage);
25161 extern_p = 0;
25162 }
25163 }
25164
25165 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25166 {
25167 /* DR 259 [temp.spec].
25168
25169 Both an explicit instantiation and a declaration of an explicit
25170 specialization shall not appear in a program unless the explicit
25171 instantiation follows a declaration of the explicit specialization.
25172
25173 For a given set of template parameters, if an explicit
25174 instantiation of a template appears after a declaration of an
25175 explicit specialization for that template, the explicit
25176 instantiation has no effect. */
25177 return;
25178 }
25179 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
25180 {
25181 /* [temp.spec]
25182
25183 No program shall explicitly instantiate any template more
25184 than once.
25185
25186 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
25187 instantiation was `extern'. If EXTERN_P then the second is.
25188 These cases are OK. */
25189 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
25190
25191 if (!previous_instantiation_extern_p && !extern_p
25192 && (complain & tf_error))
25193 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
25194
25195 /* If we've already instantiated the template, just return now. */
25196 if (!CLASSTYPE_INTERFACE_ONLY (t))
25197 return;
25198 }
25199
25200 check_explicit_instantiation_namespace (TYPE_NAME (t));
25201 mark_class_instantiated (t, extern_p);
25202
25203 if (nomem_p)
25204 return;
25205
25206 /* In contrast to implicit instantiation, where only the
25207 declarations, and not the definitions, of members are
25208 instantiated, we have here:
25209
25210 [temp.explicit]
25211
25212 An explicit instantiation that names a class template
25213 specialization is also an explicit instantiation of the same
25214 kind (declaration or definition) of each of its members (not
25215 including members inherited from base classes and members
25216 that are templates) that has not been previously explicitly
25217 specialized in the translation unit containing the explicit
25218 instantiation, provided that the associated constraints, if
25219 any, of that member are satisfied by the template arguments
25220 of the explicit instantiation. */
25221 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25222 if ((VAR_P (fld)
25223 || (TREE_CODE (fld) == FUNCTION_DECL
25224 && !static_p
25225 && user_provided_p (fld)))
25226 && DECL_TEMPLATE_INSTANTIATION (fld)
25227 && constraints_satisfied_p (fld))
25228 {
25229 mark_decl_instantiated (fld, extern_p);
25230 if (! extern_p)
25231 instantiate_decl (fld, /*defer_ok=*/true,
25232 /*expl_inst_class_mem_p=*/true);
25233 }
25234
25235 if (CLASSTYPE_NESTED_UTDS (t))
25236 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
25237 bt_instantiate_type_proc, &storage);
25238 }
25239
25240 /* Given a function DECL, which is a specialization of TMPL, modify
25241 DECL to be a re-instantiation of TMPL with the same template
25242 arguments. TMPL should be the template into which tsubst'ing
25243 should occur for DECL, not the most general template.
25244
25245 One reason for doing this is a scenario like this:
25246
25247 template <class T>
25248 void f(const T&, int i);
25249
25250 void g() { f(3, 7); }
25251
25252 template <class T>
25253 void f(const T& t, const int i) { }
25254
25255 Note that when the template is first instantiated, with
25256 instantiate_template, the resulting DECL will have no name for the
25257 first parameter, and the wrong type for the second. So, when we go
25258 to instantiate the DECL, we regenerate it. */
25259
25260 static void
regenerate_decl_from_template(tree decl,tree tmpl,tree args)25261 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25262 {
25263 /* The arguments used to instantiate DECL, from the most general
25264 template. */
25265 tree code_pattern;
25266
25267 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25268
25269 /* Make sure that we can see identifiers, and compute access
25270 correctly. */
25271 push_access_scope (decl);
25272
25273 if (TREE_CODE (decl) == FUNCTION_DECL)
25274 {
25275 tree decl_parm;
25276 tree pattern_parm;
25277 tree specs;
25278 int args_depth;
25279 int parms_depth;
25280
25281 args_depth = TMPL_ARGS_DEPTH (args);
25282 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25283 if (args_depth > parms_depth)
25284 args = get_innermost_template_args (args, parms_depth);
25285
25286 /* Instantiate a dynamic exception-specification. noexcept will be
25287 handled below. */
25288 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25289 if (TREE_VALUE (raises))
25290 {
25291 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25292 args, tf_error, NULL_TREE,
25293 /*defer_ok*/false);
25294 if (specs && specs != error_mark_node)
25295 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25296 specs);
25297 }
25298
25299 /* Merge parameter declarations. */
25300 decl_parm = skip_artificial_parms_for (decl,
25301 DECL_ARGUMENTS (decl));
25302 pattern_parm
25303 = skip_artificial_parms_for (code_pattern,
25304 DECL_ARGUMENTS (code_pattern));
25305 while (decl_parm && !DECL_PACK_P (pattern_parm))
25306 {
25307 tree parm_type;
25308 tree attributes;
25309
25310 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25311 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25312 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25313 NULL_TREE);
25314 parm_type = type_decays_to (parm_type);
25315 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25316 TREE_TYPE (decl_parm) = parm_type;
25317 attributes = DECL_ATTRIBUTES (pattern_parm);
25318 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25319 {
25320 DECL_ATTRIBUTES (decl_parm) = attributes;
25321 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25322 }
25323 decl_parm = DECL_CHAIN (decl_parm);
25324 pattern_parm = DECL_CHAIN (pattern_parm);
25325 }
25326 /* Merge any parameters that match with the function parameter
25327 pack. */
25328 if (pattern_parm && DECL_PACK_P (pattern_parm))
25329 {
25330 int i, len;
25331 tree expanded_types;
25332 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25333 the parameters in this function parameter pack. */
25334 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25335 args, tf_error, NULL_TREE);
25336 len = TREE_VEC_LENGTH (expanded_types);
25337 for (i = 0; i < len; i++)
25338 {
25339 tree parm_type;
25340 tree attributes;
25341
25342 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25343 /* Rename the parameter to include the index. */
25344 DECL_NAME (decl_parm) =
25345 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25346 parm_type = TREE_VEC_ELT (expanded_types, i);
25347 parm_type = type_decays_to (parm_type);
25348 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25349 TREE_TYPE (decl_parm) = parm_type;
25350 attributes = DECL_ATTRIBUTES (pattern_parm);
25351 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25352 {
25353 DECL_ATTRIBUTES (decl_parm) = attributes;
25354 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25355 }
25356 decl_parm = DECL_CHAIN (decl_parm);
25357 }
25358 }
25359 /* Merge additional specifiers from the CODE_PATTERN. */
25360 if (DECL_DECLARED_INLINE_P (code_pattern)
25361 && !DECL_DECLARED_INLINE_P (decl))
25362 DECL_DECLARED_INLINE_P (decl) = 1;
25363
25364 maybe_instantiate_noexcept (decl, tf_error);
25365 }
25366 else if (VAR_P (decl))
25367 {
25368 start_lambda_scope (decl);
25369 DECL_INITIAL (decl) =
25370 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25371 tf_error, DECL_TI_TEMPLATE (decl));
25372 finish_lambda_scope ();
25373 if (VAR_HAD_UNKNOWN_BOUND (decl))
25374 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25375 tf_error, DECL_TI_TEMPLATE (decl));
25376 }
25377 else
25378 gcc_unreachable ();
25379
25380 pop_access_scope (decl);
25381 }
25382
25383 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25384 substituted to get DECL. */
25385
25386 tree
template_for_substitution(tree decl)25387 template_for_substitution (tree decl)
25388 {
25389 tree tmpl = DECL_TI_TEMPLATE (decl);
25390
25391 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25392 for the instantiation. This is not always the most general
25393 template. Consider, for example:
25394
25395 template <class T>
25396 struct S { template <class U> void f();
25397 template <> void f<int>(); };
25398
25399 and an instantiation of S<double>::f<int>. We want TD to be the
25400 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25401 while (/* An instantiation cannot have a definition, so we need a
25402 more general template. */
25403 DECL_TEMPLATE_INSTANTIATION (tmpl)
25404 /* We must also deal with friend templates. Given:
25405
25406 template <class T> struct S {
25407 template <class U> friend void f() {};
25408 };
25409
25410 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25411 so far as the language is concerned, but that's still
25412 where we get the pattern for the instantiation from. On
25413 other hand, if the definition comes outside the class, say:
25414
25415 template <class T> struct S {
25416 template <class U> friend void f();
25417 };
25418 template <class U> friend void f() {}
25419
25420 we don't need to look any further. That's what the check for
25421 DECL_INITIAL is for. */
25422 || (TREE_CODE (decl) == FUNCTION_DECL
25423 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25424 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25425 {
25426 /* The present template, TD, should not be a definition. If it
25427 were a definition, we should be using it! Note that we
25428 cannot restructure the loop to just keep going until we find
25429 a template with a definition, since that might go too far if
25430 a specialization was declared, but not defined. */
25431
25432 /* Fetch the more general template. */
25433 tmpl = DECL_TI_TEMPLATE (tmpl);
25434 }
25435
25436 return tmpl;
25437 }
25438
25439 /* Returns true if we need to instantiate this template instance even if we
25440 know we aren't going to emit it. */
25441
25442 bool
always_instantiate_p(tree decl)25443 always_instantiate_p (tree decl)
25444 {
25445 /* We always instantiate inline functions so that we can inline them. An
25446 explicit instantiation declaration prohibits implicit instantiation of
25447 non-inline functions. With high levels of optimization, we would
25448 normally inline non-inline functions -- but we're not allowed to do
25449 that for "extern template" functions. Therefore, we check
25450 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25451 return ((TREE_CODE (decl) == FUNCTION_DECL
25452 && (DECL_DECLARED_INLINE_P (decl)
25453 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25454 /* And we need to instantiate static data members so that
25455 their initializers are available in integral constant
25456 expressions. */
25457 || (VAR_P (decl)
25458 && decl_maybe_constant_var_p (decl)));
25459 }
25460
25461 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25462 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25463 error, true otherwise. */
25464
25465 bool
maybe_instantiate_noexcept(tree fn,tsubst_flags_t complain)25466 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25467 {
25468 tree fntype, spec, noex, clone;
25469
25470 if (fn == error_mark_node)
25471 return false;
25472
25473 /* Don't instantiate a noexcept-specification from template context. */
25474 if (processing_template_decl
25475 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25476 return true;
25477
25478 if (DECL_MAYBE_DELETED (fn))
25479 {
25480 if (fn == current_function_decl)
25481 /* We're in start_preparsed_function, keep going. */
25482 return true;
25483
25484 ++function_depth;
25485 synthesize_method (fn);
25486 --function_depth;
25487 return !DECL_MAYBE_DELETED (fn);
25488 }
25489
25490 if (DECL_CLONED_FUNCTION_P (fn))
25491 fn = DECL_CLONED_FUNCTION (fn);
25492
25493 tree orig_fn = NULL_TREE;
25494 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25495 its FUNCTION_DECL for the rest of this function -- push_access_scope
25496 doesn't accept TEMPLATE_DECLs. */
25497 if (DECL_FUNCTION_TEMPLATE_P (fn))
25498 {
25499 orig_fn = fn;
25500 fn = DECL_TEMPLATE_RESULT (fn);
25501 }
25502
25503 fntype = TREE_TYPE (fn);
25504 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25505
25506 if (!spec || !TREE_PURPOSE (spec))
25507 return true;
25508
25509 noex = TREE_PURPOSE (spec);
25510
25511 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25512 {
25513 static hash_set<tree>* fns = new hash_set<tree>;
25514 bool added = false;
25515 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25516 {
25517 spec = get_defaulted_eh_spec (fn, complain);
25518 if (spec == error_mark_node)
25519 /* This might have failed because of an unparsed DMI, so
25520 let's try again later. */
25521 return false;
25522 }
25523 else if (!(added = !fns->add (fn)))
25524 {
25525 /* If hash_set::add returns true, the element was already there. */
25526 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25527 DECL_SOURCE_LOCATION (fn));
25528 error_at (loc,
25529 "exception specification of %qD depends on itself",
25530 fn);
25531 spec = noexcept_false_spec;
25532 }
25533 else if (push_tinst_level (fn))
25534 {
25535 push_to_top_level ();
25536 push_access_scope (fn);
25537 push_deferring_access_checks (dk_no_deferred);
25538 input_location = DECL_SOURCE_LOCATION (fn);
25539
25540 /* If needed, set current_class_ptr for the benefit of
25541 tsubst_copy/PARM_DECL. */
25542 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25543 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25544 {
25545 tree this_parm = DECL_ARGUMENTS (tdecl);
25546 current_class_ptr = NULL_TREE;
25547 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25548 current_class_ptr = this_parm;
25549 }
25550
25551 /* If this function is represented by a TEMPLATE_DECL, then
25552 the deferred noexcept-specification might still contain
25553 dependent types, even after substitution. And we need the
25554 dependency check functions to work in build_noexcept_spec. */
25555 if (orig_fn)
25556 ++processing_template_decl;
25557
25558 /* Do deferred instantiation of the noexcept-specifier. */
25559 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25560 DEFERRED_NOEXCEPT_ARGS (noex),
25561 tf_warning_or_error, fn,
25562 /*function_p=*/false,
25563 /*i_c_e_p=*/true);
25564
25565 /* Build up the noexcept-specification. */
25566 spec = build_noexcept_spec (noex, tf_warning_or_error);
25567
25568 if (orig_fn)
25569 --processing_template_decl;
25570
25571 pop_deferring_access_checks ();
25572 pop_access_scope (fn);
25573 pop_tinst_level ();
25574 pop_from_top_level ();
25575 }
25576 else
25577 spec = noexcept_false_spec;
25578
25579 if (added)
25580 fns->remove (fn);
25581
25582 if (spec == error_mark_node)
25583 {
25584 /* This failed with a hard error, so let's go with false. */
25585 gcc_assert (seen_error ());
25586 spec = noexcept_false_spec;
25587 }
25588
25589 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25590 if (orig_fn)
25591 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25592 }
25593
25594 FOR_EACH_CLONE (clone, fn)
25595 {
25596 if (TREE_TYPE (clone) == fntype)
25597 TREE_TYPE (clone) = TREE_TYPE (fn);
25598 else
25599 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
25600 }
25601
25602 return true;
25603 }
25604
25605 /* We're starting to process the function INST, an instantiation of PATTERN;
25606 add their parameters to local_specializations. */
25607
25608 static void
register_parameter_specializations(tree pattern,tree inst)25609 register_parameter_specializations (tree pattern, tree inst)
25610 {
25611 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25612 tree spec_parm = DECL_ARGUMENTS (inst);
25613 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25614 {
25615 register_local_specialization (spec_parm, tmpl_parm);
25616 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25617 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25618 }
25619 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25620 {
25621 if (!DECL_PACK_P (tmpl_parm))
25622 {
25623 register_local_specialization (spec_parm, tmpl_parm);
25624 spec_parm = DECL_CHAIN (spec_parm);
25625 }
25626 else
25627 {
25628 /* Register the (value) argument pack as a specialization of
25629 TMPL_PARM, then move on. */
25630 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25631 register_local_specialization (argpack, tmpl_parm);
25632 }
25633 }
25634 gcc_assert (!spec_parm);
25635 }
25636
25637 /* Produce the definition of D, a _DECL generated from a template. If
25638 DEFER_OK is true, then we don't have to actually do the
25639 instantiation now; we just have to do it sometime. Normally it is
25640 an error if this is an explicit instantiation but D is undefined.
25641 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25642 instantiated class template. */
25643
25644 tree
instantiate_decl(tree d,bool defer_ok,bool expl_inst_class_mem_p)25645 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25646 {
25647 tree tmpl = DECL_TI_TEMPLATE (d);
25648 tree gen_args;
25649 tree args;
25650 tree td;
25651 tree code_pattern;
25652 tree spec;
25653 tree gen_tmpl;
25654 bool pattern_defined;
25655 location_t saved_loc = input_location;
25656 int saved_unevaluated_operand = cp_unevaluated_operand;
25657 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25658 bool external_p;
25659 bool deleted_p;
25660
25661 /* This function should only be used to instantiate templates for
25662 functions and static member variables. */
25663 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25664
25665 /* A concept is never instantiated. */
25666 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25667
25668 /* Variables are never deferred; if instantiation is required, they
25669 are instantiated right away. That allows for better code in the
25670 case that an expression refers to the value of the variable --
25671 if the variable has a constant value the referring expression can
25672 take advantage of that fact. */
25673 if (VAR_P (d))
25674 defer_ok = false;
25675
25676 /* Don't instantiate cloned functions. Instead, instantiate the
25677 functions they cloned. */
25678 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25679 d = DECL_CLONED_FUNCTION (d);
25680
25681 if (DECL_TEMPLATE_INSTANTIATED (d)
25682 || (TREE_CODE (d) == FUNCTION_DECL
25683 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25684 || DECL_TEMPLATE_SPECIALIZATION (d))
25685 /* D has already been instantiated or explicitly specialized, so
25686 there's nothing for us to do here.
25687
25688 It might seem reasonable to check whether or not D is an explicit
25689 instantiation, and, if so, stop here. But when an explicit
25690 instantiation is deferred until the end of the compilation,
25691 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25692 the instantiation. */
25693 return d;
25694
25695 /* Check to see whether we know that this template will be
25696 instantiated in some other file, as with "extern template"
25697 extension. */
25698 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25699
25700 /* In general, we do not instantiate such templates. */
25701 if (external_p && !always_instantiate_p (d))
25702 return d;
25703
25704 gen_tmpl = most_general_template (tmpl);
25705 gen_args = DECL_TI_ARGS (d);
25706
25707 if (tmpl != gen_tmpl)
25708 /* We should already have the extra args. */
25709 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25710 == TMPL_ARGS_DEPTH (gen_args));
25711 /* And what's in the hash table should match D. */
25712 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
25713 || spec == NULL_TREE);
25714
25715 /* This needs to happen before any tsubsting. */
25716 if (! push_tinst_level (d))
25717 return d;
25718
25719 timevar_push (TV_TEMPLATE_INST);
25720
25721 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25722 for the instantiation. */
25723 td = template_for_substitution (d);
25724 args = gen_args;
25725
25726 if (VAR_P (d))
25727 {
25728 /* Look up an explicit specialization, if any. */
25729 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25730 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25731 if (elt && elt != error_mark_node)
25732 {
25733 td = TREE_VALUE (elt);
25734 args = TREE_PURPOSE (elt);
25735 }
25736 }
25737
25738 code_pattern = DECL_TEMPLATE_RESULT (td);
25739
25740 /* We should never be trying to instantiate a member of a class
25741 template or partial specialization. */
25742 gcc_assert (d != code_pattern);
25743
25744 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25745 || DECL_TEMPLATE_SPECIALIZATION (td))
25746 /* In the case of a friend template whose definition is provided
25747 outside the class, we may have too many arguments. Drop the
25748 ones we don't need. The same is true for specializations. */
25749 args = get_innermost_template_args
25750 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25751
25752 if (TREE_CODE (d) == FUNCTION_DECL)
25753 {
25754 deleted_p = DECL_DELETED_FN (code_pattern);
25755 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25756 && DECL_INITIAL (code_pattern) != error_mark_node)
25757 || DECL_DEFAULTED_FN (code_pattern)
25758 || deleted_p);
25759 }
25760 else
25761 {
25762 deleted_p = false;
25763 if (DECL_CLASS_SCOPE_P (code_pattern))
25764 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25765 else
25766 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25767 }
25768
25769 /* We may be in the middle of deferred access check. Disable it now. */
25770 push_deferring_access_checks (dk_no_deferred);
25771
25772 /* Unless an explicit instantiation directive has already determined
25773 the linkage of D, remember that a definition is available for
25774 this entity. */
25775 if (pattern_defined
25776 && !DECL_INTERFACE_KNOWN (d)
25777 && !DECL_NOT_REALLY_EXTERN (d))
25778 mark_definable (d);
25779
25780 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25781 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25782 input_location = DECL_SOURCE_LOCATION (d);
25783
25784 /* If D is a member of an explicitly instantiated class template,
25785 and no definition is available, treat it like an implicit
25786 instantiation. */
25787 if (!pattern_defined && expl_inst_class_mem_p
25788 && DECL_EXPLICIT_INSTANTIATION (d))
25789 {
25790 /* Leave linkage flags alone on instantiations with anonymous
25791 visibility. */
25792 if (TREE_PUBLIC (d))
25793 {
25794 DECL_NOT_REALLY_EXTERN (d) = 0;
25795 DECL_INTERFACE_KNOWN (d) = 0;
25796 }
25797 SET_DECL_IMPLICIT_INSTANTIATION (d);
25798 }
25799
25800 /* Defer all other templates, unless we have been explicitly
25801 forbidden from doing so. */
25802 if (/* If there is no definition, we cannot instantiate the
25803 template. */
25804 ! pattern_defined
25805 /* If it's OK to postpone instantiation, do so. */
25806 || defer_ok
25807 /* If this is a static data member that will be defined
25808 elsewhere, we don't want to instantiate the entire data
25809 member, but we do want to instantiate the initializer so that
25810 we can substitute that elsewhere. */
25811 || (external_p && VAR_P (d))
25812 /* Handle here a deleted function too, avoid generating
25813 its body (c++/61080). */
25814 || deleted_p)
25815 {
25816 /* The definition of the static data member is now required so
25817 we must substitute the initializer. */
25818 if (VAR_P (d)
25819 && !DECL_INITIAL (d)
25820 && DECL_INITIAL (code_pattern))
25821 {
25822 tree ns;
25823 tree init;
25824 bool const_init = false;
25825 bool enter_context = DECL_CLASS_SCOPE_P (d);
25826
25827 ns = decl_namespace_context (d);
25828 push_nested_namespace (ns);
25829 if (enter_context)
25830 push_nested_class (DECL_CONTEXT (d));
25831 init = tsubst_expr (DECL_INITIAL (code_pattern),
25832 args,
25833 tf_warning_or_error, NULL_TREE,
25834 /*integral_constant_expression_p=*/false);
25835 /* If instantiating the initializer involved instantiating this
25836 again, don't call cp_finish_decl twice. */
25837 if (!DECL_INITIAL (d))
25838 {
25839 /* Make sure the initializer is still constant, in case of
25840 circular dependency (template/instantiate6.C). */
25841 const_init
25842 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25843 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25844 /*asmspec_tree=*/NULL_TREE,
25845 LOOKUP_ONLYCONVERTING);
25846 }
25847 if (enter_context)
25848 pop_nested_class ();
25849 pop_nested_namespace (ns);
25850 }
25851
25852 /* We restore the source position here because it's used by
25853 add_pending_template. */
25854 input_location = saved_loc;
25855
25856 if (at_eof && !pattern_defined
25857 && DECL_EXPLICIT_INSTANTIATION (d)
25858 && DECL_NOT_REALLY_EXTERN (d))
25859 /* [temp.explicit]
25860
25861 The definition of a non-exported function template, a
25862 non-exported member function template, or a non-exported
25863 member function or static data member of a class template
25864 shall be present in every translation unit in which it is
25865 explicitly instantiated. */
25866 permerror (input_location, "explicit instantiation of %qD "
25867 "but no definition available", d);
25868
25869 /* If we're in unevaluated context, we just wanted to get the
25870 constant value; this isn't an odr use, so don't queue
25871 a full instantiation. */
25872 if (cp_unevaluated_operand != 0)
25873 goto out;
25874 /* ??? Historically, we have instantiated inline functions, even
25875 when marked as "extern template". */
25876 if (!(external_p && VAR_P (d)))
25877 add_pending_template (d);
25878 goto out;
25879 }
25880
25881 bool push_to_top, nested;
25882 tree fn_context;
25883 fn_context = decl_function_context (d);
25884 if (LAMBDA_FUNCTION_P (d))
25885 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25886 fn_context = NULL_TREE;
25887 nested = current_function_decl != NULL_TREE;
25888 push_to_top = !(nested && fn_context == current_function_decl);
25889
25890 vec<tree> omp_privatization_save;
25891 if (nested)
25892 save_omp_privatization_clauses (omp_privatization_save);
25893
25894 if (push_to_top)
25895 push_to_top_level ();
25896 else
25897 {
25898 gcc_assert (!processing_template_decl);
25899 push_function_context ();
25900 cp_unevaluated_operand = 0;
25901 c_inhibit_evaluation_warnings = 0;
25902 }
25903
25904 if (VAR_P (d))
25905 {
25906 /* The variable might be a lambda's extra scope, and that
25907 lambda's visibility depends on D's. */
25908 maybe_commonize_var (d);
25909 determine_visibility (d);
25910 }
25911
25912 /* Mark D as instantiated so that recursive calls to
25913 instantiate_decl do not try to instantiate it again. */
25914 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25915
25916 /* Regenerate the declaration in case the template has been modified
25917 by a subsequent redeclaration. */
25918 regenerate_decl_from_template (d, td, args);
25919
25920 /* We already set the file and line above. Reset them now in case
25921 they changed as a result of calling regenerate_decl_from_template. */
25922 input_location = DECL_SOURCE_LOCATION (d);
25923
25924 if (VAR_P (d))
25925 {
25926 tree init;
25927 bool const_init = false;
25928
25929 /* Clear out DECL_RTL; whatever was there before may not be right
25930 since we've reset the type of the declaration. */
25931 SET_DECL_RTL (d, NULL);
25932 DECL_IN_AGGR_P (d) = 0;
25933
25934 /* The initializer is placed in DECL_INITIAL by
25935 regenerate_decl_from_template so we don't need to
25936 push/pop_access_scope again here. Pull it out so that
25937 cp_finish_decl can process it. */
25938 init = DECL_INITIAL (d);
25939 DECL_INITIAL (d) = NULL_TREE;
25940 DECL_INITIALIZED_P (d) = 0;
25941
25942 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25943 initializer. That function will defer actual emission until
25944 we have a chance to determine linkage. */
25945 DECL_EXTERNAL (d) = 0;
25946
25947 /* Enter the scope of D so that access-checking works correctly. */
25948 bool enter_context = DECL_CLASS_SCOPE_P (d);
25949 if (enter_context)
25950 push_nested_class (DECL_CONTEXT (d));
25951
25952 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25953 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25954 ? LOOKUP_CONSTINIT : 0);
25955 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25956
25957 if (enter_context)
25958 pop_nested_class ();
25959
25960 if (variable_template_p (gen_tmpl))
25961 note_variable_template_instantiation (d);
25962 }
25963 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25964 synthesize_method (d);
25965 else if (TREE_CODE (d) == FUNCTION_DECL)
25966 {
25967 /* Set up the list of local specializations. */
25968 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25969 tree block = NULL_TREE;
25970
25971 /* Set up context. */
25972 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25973 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25974 block = push_stmt_list ();
25975 else
25976 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25977
25978 /* Some typedefs referenced from within the template code need to be
25979 access checked at template instantiation time, i.e now. These
25980 types were added to the template at parsing time. Let's get those
25981 and perform the access checks then. */
25982 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25983 args);
25984
25985 /* Create substitution entries for the parameters. */
25986 register_parameter_specializations (code_pattern, d);
25987
25988 /* Substitute into the body of the function. */
25989 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25990 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25991 tf_warning_or_error, tmpl);
25992 else
25993 {
25994 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25995 tf_warning_or_error, tmpl,
25996 /*integral_constant_expression_p=*/false);
25997
25998 /* Set the current input_location to the end of the function
25999 so that finish_function knows where we are. */
26000 input_location
26001 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
26002
26003 /* Remember if we saw an infinite loop in the template. */
26004 current_function_infinite_loop
26005 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
26006 }
26007
26008 /* Finish the function. */
26009 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
26010 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
26011 DECL_SAVED_TREE (d) = pop_stmt_list (block);
26012 else
26013 {
26014 d = finish_function (/*inline_p=*/false);
26015 expand_or_defer_fn (d);
26016 }
26017
26018 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
26019 cp_check_omp_declare_reduction (d);
26020 }
26021
26022 /* We're not deferring instantiation any more. */
26023 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
26024
26025 if (push_to_top)
26026 pop_from_top_level ();
26027 else
26028 pop_function_context ();
26029
26030 if (nested)
26031 restore_omp_privatization_clauses (omp_privatization_save);
26032
26033 out:
26034 pop_deferring_access_checks ();
26035 timevar_pop (TV_TEMPLATE_INST);
26036 pop_tinst_level ();
26037 input_location = saved_loc;
26038 cp_unevaluated_operand = saved_unevaluated_operand;
26039 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26040
26041 return d;
26042 }
26043
26044 /* Run through the list of templates that we wish we could
26045 instantiate, and instantiate any we can. RETRIES is the
26046 number of times we retry pending template instantiation. */
26047
26048 void
instantiate_pending_templates(int retries)26049 instantiate_pending_templates (int retries)
26050 {
26051 int reconsider;
26052 location_t saved_loc = input_location;
26053
26054 /* Instantiating templates may trigger vtable generation. This in turn
26055 may require further template instantiations. We place a limit here
26056 to avoid infinite loop. */
26057 if (pending_templates && retries >= max_tinst_depth)
26058 {
26059 tree decl = pending_templates->tinst->maybe_get_node ();
26060
26061 fatal_error (input_location,
26062 "template instantiation depth exceeds maximum of %d"
26063 " instantiating %q+D, possibly from virtual table generation"
26064 " (use %<-ftemplate-depth=%> to increase the maximum)",
26065 max_tinst_depth, decl);
26066 if (TREE_CODE (decl) == FUNCTION_DECL)
26067 /* Pretend that we defined it. */
26068 DECL_INITIAL (decl) = error_mark_node;
26069 return;
26070 }
26071
26072 do
26073 {
26074 struct pending_template **t = &pending_templates;
26075 struct pending_template *last = NULL;
26076 reconsider = 0;
26077 while (*t)
26078 {
26079 tree instantiation = reopen_tinst_level ((*t)->tinst);
26080 bool complete = false;
26081
26082 if (TYPE_P (instantiation))
26083 {
26084 if (!COMPLETE_TYPE_P (instantiation))
26085 {
26086 instantiate_class_template (instantiation);
26087 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
26088 for (tree fld = TYPE_FIELDS (instantiation);
26089 fld; fld = TREE_CHAIN (fld))
26090 if ((VAR_P (fld)
26091 || (TREE_CODE (fld) == FUNCTION_DECL
26092 && !DECL_ARTIFICIAL (fld)))
26093 && DECL_TEMPLATE_INSTANTIATION (fld))
26094 instantiate_decl (fld,
26095 /*defer_ok=*/false,
26096 /*expl_inst_class_mem_p=*/false);
26097
26098 if (COMPLETE_TYPE_P (instantiation))
26099 reconsider = 1;
26100 }
26101
26102 complete = COMPLETE_TYPE_P (instantiation);
26103 }
26104 else
26105 {
26106 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
26107 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
26108 {
26109 instantiation
26110 = instantiate_decl (instantiation,
26111 /*defer_ok=*/false,
26112 /*expl_inst_class_mem_p=*/false);
26113 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
26114 reconsider = 1;
26115 }
26116
26117 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
26118 || DECL_TEMPLATE_INSTANTIATED (instantiation));
26119 }
26120
26121 if (complete)
26122 {
26123 /* If INSTANTIATION has been instantiated, then we don't
26124 need to consider it again in the future. */
26125 struct pending_template *drop = *t;
26126 *t = (*t)->next;
26127 set_refcount_ptr (drop->tinst);
26128 pending_template_freelist ().free (drop);
26129 }
26130 else
26131 {
26132 last = *t;
26133 t = &(*t)->next;
26134 }
26135 tinst_depth = 0;
26136 set_refcount_ptr (current_tinst_level);
26137 }
26138 last_pending_template = last;
26139 }
26140 while (reconsider);
26141
26142 input_location = saved_loc;
26143 }
26144
26145 /* Substitute ARGVEC into T, which is a list of initializers for
26146 either base class or a non-static data member. The TREE_PURPOSEs
26147 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26148 instantiate_decl. */
26149
26150 static tree
tsubst_initializer_list(tree t,tree argvec)26151 tsubst_initializer_list (tree t, tree argvec)
26152 {
26153 tree inits = NULL_TREE;
26154 tree target_ctor = error_mark_node;
26155
26156 for (; t; t = TREE_CHAIN (t))
26157 {
26158 tree decl;
26159 tree init;
26160 tree expanded_bases = NULL_TREE;
26161 tree expanded_arguments = NULL_TREE;
26162 int i, len = 1;
26163
26164 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26165 {
26166 tree expr;
26167 tree arg;
26168
26169 /* Expand the base class expansion type into separate base
26170 classes. */
26171 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26172 tf_warning_or_error,
26173 NULL_TREE);
26174 if (expanded_bases == error_mark_node)
26175 continue;
26176
26177 /* We'll be building separate TREE_LISTs of arguments for
26178 each base. */
26179 len = TREE_VEC_LENGTH (expanded_bases);
26180 expanded_arguments = make_tree_vec (len);
26181 for (i = 0; i < len; i++)
26182 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26183
26184 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26185 expand each argument in the TREE_VALUE of t. */
26186 expr = make_node (EXPR_PACK_EXPANSION);
26187 PACK_EXPANSION_LOCAL_P (expr) = true;
26188 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26189 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26190
26191 if (TREE_VALUE (t) == void_type_node)
26192 /* VOID_TYPE_NODE is used to indicate
26193 value-initialization. */
26194 {
26195 for (i = 0; i < len; i++)
26196 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26197 }
26198 else
26199 {
26200 /* Substitute parameter packs into each argument in the
26201 TREE_LIST. */
26202 in_base_initializer = 1;
26203 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26204 {
26205 tree expanded_exprs;
26206
26207 /* Expand the argument. */
26208 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26209 expanded_exprs
26210 = tsubst_pack_expansion (expr, argvec,
26211 tf_warning_or_error,
26212 NULL_TREE);
26213 if (expanded_exprs == error_mark_node)
26214 continue;
26215
26216 /* Prepend each of the expanded expressions to the
26217 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26218 for (i = 0; i < len; i++)
26219 {
26220 TREE_VEC_ELT (expanded_arguments, i) =
26221 tree_cons (NULL_TREE,
26222 TREE_VEC_ELT (expanded_exprs, i),
26223 TREE_VEC_ELT (expanded_arguments, i));
26224 }
26225 }
26226 in_base_initializer = 0;
26227
26228 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26229 since we built them backwards. */
26230 for (i = 0; i < len; i++)
26231 {
26232 TREE_VEC_ELT (expanded_arguments, i) =
26233 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26234 }
26235 }
26236 }
26237
26238 for (i = 0; i < len; ++i)
26239 {
26240 if (expanded_bases)
26241 {
26242 decl = TREE_VEC_ELT (expanded_bases, i);
26243 decl = expand_member_init (decl);
26244 init = TREE_VEC_ELT (expanded_arguments, i);
26245 }
26246 else
26247 {
26248 tree tmp;
26249 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26250 tf_warning_or_error, NULL_TREE);
26251
26252 decl = expand_member_init (decl);
26253 if (decl && !DECL_P (decl))
26254 in_base_initializer = 1;
26255
26256 init = TREE_VALUE (t);
26257 tmp = init;
26258 if (init != void_type_node)
26259 init = tsubst_expr (init, argvec,
26260 tf_warning_or_error, NULL_TREE,
26261 /*integral_constant_expression_p=*/false);
26262 if (init == NULL_TREE && tmp != NULL_TREE)
26263 /* If we had an initializer but it instantiated to nothing,
26264 value-initialize the object. This will only occur when
26265 the initializer was a pack expansion where the parameter
26266 packs used in that expansion were of length zero. */
26267 init = void_type_node;
26268 in_base_initializer = 0;
26269 }
26270
26271 if (target_ctor != error_mark_node
26272 && init != error_mark_node)
26273 {
26274 error ("mem-initializer for %qD follows constructor delegation",
26275 decl);
26276 return inits;
26277 }
26278 /* Look for a target constructor. */
26279 if (init != error_mark_node
26280 && decl && CLASS_TYPE_P (decl)
26281 && same_type_p (decl, current_class_type))
26282 {
26283 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26284 if (inits)
26285 {
26286 error ("constructor delegation follows mem-initializer for %qD",
26287 TREE_PURPOSE (inits));
26288 continue;
26289 }
26290 target_ctor = init;
26291 }
26292
26293 if (decl)
26294 {
26295 init = build_tree_list (decl, init);
26296 TREE_CHAIN (init) = inits;
26297 inits = init;
26298 }
26299 }
26300 }
26301 return inits;
26302 }
26303
26304 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26305
26306 static void
set_current_access_from_decl(tree decl)26307 set_current_access_from_decl (tree decl)
26308 {
26309 if (TREE_PRIVATE (decl))
26310 current_access_specifier = access_private_node;
26311 else if (TREE_PROTECTED (decl))
26312 current_access_specifier = access_protected_node;
26313 else
26314 current_access_specifier = access_public_node;
26315 }
26316
26317 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26318 is the instantiation (which should have been created with
26319 start_enum) and ARGS are the template arguments to use. */
26320
26321 static void
tsubst_enum(tree tag,tree newtag,tree args)26322 tsubst_enum (tree tag, tree newtag, tree args)
26323 {
26324 tree e;
26325
26326 if (SCOPED_ENUM_P (newtag))
26327 begin_scope (sk_scoped_enum, newtag);
26328
26329 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26330 {
26331 tree value;
26332 tree decl;
26333
26334 decl = TREE_VALUE (e);
26335 /* Note that in a template enum, the TREE_VALUE is the
26336 CONST_DECL, not the corresponding INTEGER_CST. */
26337 value = tsubst_expr (DECL_INITIAL (decl),
26338 args, tf_warning_or_error, NULL_TREE,
26339 /*integral_constant_expression_p=*/true);
26340
26341 /* Give this enumeration constant the correct access. */
26342 set_current_access_from_decl (decl);
26343
26344 /* Actually build the enumerator itself. Here we're assuming that
26345 enumerators can't have dependent attributes. */
26346 build_enumerator (DECL_NAME (decl), value, newtag,
26347 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26348 }
26349
26350 if (SCOPED_ENUM_P (newtag))
26351 finish_scope ();
26352
26353 finish_enum_value_list (newtag);
26354 finish_enum (newtag);
26355
26356 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26357 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26358 }
26359
26360 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26361 its type -- but without substituting the innermost set of template
26362 arguments. So, innermost set of template parameters will appear in
26363 the type. */
26364
26365 tree
get_mostly_instantiated_function_type(tree decl)26366 get_mostly_instantiated_function_type (tree decl)
26367 {
26368 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26369 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26370 }
26371
26372 /* Return truthvalue if we're processing a template different from
26373 the last one involved in diagnostics. */
26374 bool
problematic_instantiation_changed(void)26375 problematic_instantiation_changed (void)
26376 {
26377 return current_tinst_level != last_error_tinst_level;
26378 }
26379
26380 /* Remember current template involved in diagnostics. */
26381 void
record_last_problematic_instantiation(void)26382 record_last_problematic_instantiation (void)
26383 {
26384 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26385 }
26386
26387 struct tinst_level *
current_instantiation(void)26388 current_instantiation (void)
26389 {
26390 return current_tinst_level;
26391 }
26392
26393 /* Return TRUE if current_function_decl is being instantiated, false
26394 otherwise. */
26395
26396 bool
instantiating_current_function_p(void)26397 instantiating_current_function_p (void)
26398 {
26399 return (current_instantiation ()
26400 && (current_instantiation ()->maybe_get_node ()
26401 == current_function_decl));
26402 }
26403
26404 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26405 type. Return false for ok, true for disallowed. Issue error and
26406 inform messages under control of COMPLAIN. */
26407
26408 static bool
invalid_nontype_parm_type_p(tree type,tsubst_flags_t complain)26409 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26410 {
26411 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26412 return false;
26413 else if (TYPE_PTR_P (type))
26414 return false;
26415 else if (TYPE_REF_P (type)
26416 && !TYPE_REF_IS_RVALUE (type))
26417 return false;
26418 else if (TYPE_PTRMEM_P (type))
26419 return false;
26420 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26421 {
26422 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx2a)
26423 {
26424 if (complain & tf_error)
26425 error ("non-type template parameters of deduced class type only "
26426 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
26427 return true;
26428 }
26429 return false;
26430 }
26431 else if (TREE_CODE (type) == TYPENAME_TYPE)
26432 return false;
26433 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26434 return false;
26435 else if (TREE_CODE (type) == NULLPTR_TYPE)
26436 return false;
26437 /* A bound template template parm could later be instantiated to have a valid
26438 nontype parm type via an alias template. */
26439 else if (cxx_dialect >= cxx11
26440 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26441 return false;
26442 else if (CLASS_TYPE_P (type))
26443 {
26444 if (cxx_dialect < cxx2a)
26445 {
26446 if (complain & tf_error)
26447 error ("non-type template parameters of class type only available "
26448 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
26449 return true;
26450 }
26451 if (dependent_type_p (type))
26452 return false;
26453 if (!complete_type_or_else (type, NULL_TREE))
26454 return true;
26455 if (!structural_type_p (type))
26456 {
26457 if (complain & tf_error)
26458 {
26459 auto_diagnostic_group d;
26460 error ("%qT is not a valid type for a template non-type "
26461 "parameter because it is not structural", type);
26462 structural_type_p (type, true);
26463 }
26464 return true;
26465 }
26466 return false;
26467 }
26468
26469 if (complain & tf_error)
26470 {
26471 if (type == error_mark_node)
26472 inform (input_location, "invalid template non-type parameter");
26473 else
26474 error ("%q#T is not a valid type for a template non-type parameter",
26475 type);
26476 }
26477 return true;
26478 }
26479
26480 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26481 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26482
26483 static bool
dependent_type_p_r(tree type)26484 dependent_type_p_r (tree type)
26485 {
26486 tree scope;
26487
26488 /* [temp.dep.type]
26489
26490 A type is dependent if it is:
26491
26492 -- a template parameter. Template template parameters are types
26493 for us (since TYPE_P holds true for them) so we handle
26494 them here. */
26495 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26496 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26497 return true;
26498 /* -- a qualified-id with a nested-name-specifier which contains a
26499 class-name that names a dependent type or whose unqualified-id
26500 names a dependent type. */
26501 if (TREE_CODE (type) == TYPENAME_TYPE)
26502 return true;
26503
26504 /* An alias template specialization can be dependent even if the
26505 resulting type is not. */
26506 if (dependent_alias_template_spec_p (type, nt_transparent))
26507 return true;
26508
26509 /* -- a cv-qualified type where the cv-unqualified type is
26510 dependent.
26511 No code is necessary for this bullet; the code below handles
26512 cv-qualified types, and we don't want to strip aliases with
26513 TYPE_MAIN_VARIANT because of DR 1558. */
26514 /* -- a compound type constructed from any dependent type. */
26515 if (TYPE_PTRMEM_P (type))
26516 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26517 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26518 (type)));
26519 else if (INDIRECT_TYPE_P (type))
26520 return dependent_type_p (TREE_TYPE (type));
26521 else if (FUNC_OR_METHOD_TYPE_P (type))
26522 {
26523 tree arg_type;
26524
26525 if (dependent_type_p (TREE_TYPE (type)))
26526 return true;
26527 for (arg_type = TYPE_ARG_TYPES (type);
26528 arg_type;
26529 arg_type = TREE_CHAIN (arg_type))
26530 if (dependent_type_p (TREE_VALUE (arg_type)))
26531 return true;
26532 if (cxx_dialect >= cxx17)
26533 /* A value-dependent noexcept-specifier makes the type dependent. */
26534 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26535 if (tree noex = TREE_PURPOSE (spec))
26536 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26537 affect overload resolution and treating it as dependent breaks
26538 things. Same for an unparsed noexcept expression. */
26539 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26540 && TREE_CODE (noex) != DEFERRED_PARSE
26541 && value_dependent_expression_p (noex))
26542 return true;
26543 return false;
26544 }
26545 /* -- an array type constructed from any dependent type or whose
26546 size is specified by a constant expression that is
26547 value-dependent.
26548
26549 We checked for type- and value-dependence of the bounds in
26550 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26551 if (TREE_CODE (type) == ARRAY_TYPE)
26552 {
26553 if (TYPE_DOMAIN (type)
26554 && dependent_type_p (TYPE_DOMAIN (type)))
26555 return true;
26556 return dependent_type_p (TREE_TYPE (type));
26557 }
26558
26559 /* -- a template-id in which either the template name is a template
26560 parameter ... */
26561 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26562 return true;
26563 /* ... or any of the template arguments is a dependent type or
26564 an expression that is type-dependent or value-dependent. */
26565 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26566 && (any_dependent_template_arguments_p
26567 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26568 return true;
26569
26570 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26571 dependent; if the argument of the `typeof' expression is not
26572 type-dependent, then it should already been have resolved. */
26573 if (TREE_CODE (type) == TYPEOF_TYPE
26574 || TREE_CODE (type) == DECLTYPE_TYPE
26575 || TREE_CODE (type) == UNDERLYING_TYPE)
26576 return true;
26577
26578 /* A template argument pack is dependent if any of its packed
26579 arguments are. */
26580 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26581 {
26582 tree args = ARGUMENT_PACK_ARGS (type);
26583 int i, len = TREE_VEC_LENGTH (args);
26584 for (i = 0; i < len; ++i)
26585 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26586 return true;
26587 }
26588
26589 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26590 be template parameters. */
26591 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26592 return true;
26593
26594 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26595 return true;
26596
26597 /* The standard does not specifically mention types that are local
26598 to template functions or local classes, but they should be
26599 considered dependent too. For example:
26600
26601 template <int I> void f() {
26602 enum E { a = I };
26603 S<sizeof (E)> s;
26604 }
26605
26606 The size of `E' cannot be known until the value of `I' has been
26607 determined. Therefore, `E' must be considered dependent. */
26608 scope = TYPE_CONTEXT (type);
26609 if (scope && TYPE_P (scope))
26610 return dependent_type_p (scope);
26611 /* Don't use type_dependent_expression_p here, as it can lead
26612 to infinite recursion trying to determine whether a lambda
26613 nested in a lambda is dependent (c++/47687). */
26614 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26615 && DECL_LANG_SPECIFIC (scope)
26616 && DECL_TEMPLATE_INFO (scope)
26617 && (any_dependent_template_arguments_p
26618 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26619 return true;
26620
26621 /* Other types are non-dependent. */
26622 return false;
26623 }
26624
26625 /* Returns TRUE if TYPE is dependent, in the sense of
26626 [temp.dep.type]. Note that a NULL type is considered dependent. */
26627
26628 bool
dependent_type_p(tree type)26629 dependent_type_p (tree type)
26630 {
26631 /* If there are no template parameters in scope, then there can't be
26632 any dependent types. */
26633 if (!processing_template_decl)
26634 {
26635 /* If we are not processing a template, then nobody should be
26636 providing us with a dependent type. */
26637 gcc_assert (type);
26638 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26639 return false;
26640 }
26641
26642 /* If the type is NULL, we have not computed a type for the entity
26643 in question; in that case, the type is dependent. */
26644 if (!type)
26645 return true;
26646
26647 /* Erroneous types can be considered non-dependent. */
26648 if (type == error_mark_node)
26649 return false;
26650
26651 /* Getting here with global_type_node means we improperly called this
26652 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26653 gcc_checking_assert (type != global_type_node);
26654
26655 /* If we have not already computed the appropriate value for TYPE,
26656 do so now. */
26657 if (!TYPE_DEPENDENT_P_VALID (type))
26658 {
26659 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26660 TYPE_DEPENDENT_P_VALID (type) = 1;
26661 }
26662
26663 return TYPE_DEPENDENT_P (type);
26664 }
26665
26666 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26667 lookup. In other words, a dependent type that is not the current
26668 instantiation. */
26669
26670 bool
dependent_scope_p(tree scope)26671 dependent_scope_p (tree scope)
26672 {
26673 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26674 && !currently_open_class (scope));
26675 }
26676
26677 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26678 an unknown base of 'this' (and is therefore instantiation-dependent). */
26679
26680 static bool
unknown_base_ref_p(tree t)26681 unknown_base_ref_p (tree t)
26682 {
26683 if (!current_class_ptr)
26684 return false;
26685
26686 tree mem = TREE_OPERAND (t, 1);
26687 if (shared_member_p (mem))
26688 return false;
26689
26690 tree cur = current_nonlambda_class_type ();
26691 if (!any_dependent_bases_p (cur))
26692 return false;
26693
26694 tree ctx = TREE_OPERAND (t, 0);
26695 if (DERIVED_FROM_P (ctx, cur))
26696 return false;
26697
26698 return true;
26699 }
26700
26701 /* T is a SCOPE_REF; return whether we need to consider it
26702 instantiation-dependent so that we can check access at instantiation
26703 time even though we know which member it resolves to. */
26704
26705 static bool
instantiation_dependent_scope_ref_p(tree t)26706 instantiation_dependent_scope_ref_p (tree t)
26707 {
26708 if (DECL_P (TREE_OPERAND (t, 1))
26709 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26710 && !unknown_base_ref_p (t)
26711 && accessible_in_template_p (TREE_OPERAND (t, 0),
26712 TREE_OPERAND (t, 1)))
26713 return false;
26714 else
26715 return true;
26716 }
26717
26718 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26719 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26720 expression. */
26721
26722 /* Note that this predicate is not appropriate for general expressions;
26723 only constant expressions (that satisfy potential_constant_expression)
26724 can be tested for value dependence. */
26725
26726 bool
value_dependent_expression_p(tree expression)26727 value_dependent_expression_p (tree expression)
26728 {
26729 if (!processing_template_decl || expression == NULL_TREE)
26730 return false;
26731
26732 /* A type-dependent expression is also value-dependent. */
26733 if (type_dependent_expression_p (expression))
26734 return true;
26735
26736 switch (TREE_CODE (expression))
26737 {
26738 case BASELINK:
26739 /* A dependent member function of the current instantiation. */
26740 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26741
26742 case FUNCTION_DECL:
26743 /* A dependent member function of the current instantiation. */
26744 if (DECL_CLASS_SCOPE_P (expression)
26745 && dependent_type_p (DECL_CONTEXT (expression)))
26746 return true;
26747 break;
26748
26749 case IDENTIFIER_NODE:
26750 /* A name that has not been looked up -- must be dependent. */
26751 return true;
26752
26753 case TEMPLATE_PARM_INDEX:
26754 /* A non-type template parm. */
26755 return true;
26756
26757 case CONST_DECL:
26758 /* A non-type template parm. */
26759 if (DECL_TEMPLATE_PARM_P (expression))
26760 return true;
26761 return value_dependent_expression_p (DECL_INITIAL (expression));
26762
26763 case VAR_DECL:
26764 /* A constant with literal type and is initialized
26765 with an expression that is value-dependent. */
26766 if (DECL_DEPENDENT_INIT_P (expression)
26767 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26768 || TYPE_REF_P (TREE_TYPE (expression)))
26769 return true;
26770 if (DECL_HAS_VALUE_EXPR_P (expression))
26771 {
26772 tree value_expr = DECL_VALUE_EXPR (expression);
26773 if (value_dependent_expression_p (value_expr)
26774 /* __PRETTY_FUNCTION__ inside a template function is dependent
26775 on the name of the function. */
26776 || (DECL_PRETTY_FUNCTION_P (expression)
26777 /* It might be used in a template, but not a template
26778 function, in which case its DECL_VALUE_EXPR will be
26779 "top level". */
26780 && value_expr == error_mark_node))
26781 return true;
26782 }
26783 return false;
26784
26785 case DYNAMIC_CAST_EXPR:
26786 case STATIC_CAST_EXPR:
26787 case CONST_CAST_EXPR:
26788 case REINTERPRET_CAST_EXPR:
26789 case CAST_EXPR:
26790 case IMPLICIT_CONV_EXPR:
26791 /* These expressions are value-dependent if the type to which
26792 the cast occurs is dependent or the expression being casted
26793 is value-dependent. */
26794 {
26795 tree type = TREE_TYPE (expression);
26796
26797 if (dependent_type_p (type))
26798 return true;
26799
26800 /* A functional cast has a list of operands. */
26801 expression = TREE_OPERAND (expression, 0);
26802 if (!expression)
26803 {
26804 /* If there are no operands, it must be an expression such
26805 as "int()". This should not happen for aggregate types
26806 because it would form non-constant expressions. */
26807 gcc_assert (cxx_dialect >= cxx11
26808 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26809
26810 return false;
26811 }
26812
26813 if (TREE_CODE (expression) == TREE_LIST)
26814 return any_value_dependent_elements_p (expression);
26815
26816 return value_dependent_expression_p (expression);
26817 }
26818
26819 case SIZEOF_EXPR:
26820 if (SIZEOF_EXPR_TYPE_P (expression))
26821 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26822 /* FALLTHRU */
26823 case ALIGNOF_EXPR:
26824 case TYPEID_EXPR:
26825 /* A `sizeof' expression is value-dependent if the operand is
26826 type-dependent or is a pack expansion. */
26827 expression = TREE_OPERAND (expression, 0);
26828 if (PACK_EXPANSION_P (expression))
26829 return true;
26830 else if (TYPE_P (expression))
26831 return dependent_type_p (expression);
26832 return instantiation_dependent_uneval_expression_p (expression);
26833
26834 case AT_ENCODE_EXPR:
26835 /* An 'encode' expression is value-dependent if the operand is
26836 type-dependent. */
26837 expression = TREE_OPERAND (expression, 0);
26838 return dependent_type_p (expression);
26839
26840 case NOEXCEPT_EXPR:
26841 expression = TREE_OPERAND (expression, 0);
26842 return instantiation_dependent_uneval_expression_p (expression);
26843
26844 case SCOPE_REF:
26845 /* All instantiation-dependent expressions should also be considered
26846 value-dependent. */
26847 return instantiation_dependent_scope_ref_p (expression);
26848
26849 case COMPONENT_REF:
26850 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26851 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26852
26853 case NONTYPE_ARGUMENT_PACK:
26854 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26855 is value-dependent. */
26856 {
26857 tree values = ARGUMENT_PACK_ARGS (expression);
26858 int i, len = TREE_VEC_LENGTH (values);
26859
26860 for (i = 0; i < len; ++i)
26861 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26862 return true;
26863
26864 return false;
26865 }
26866
26867 case TRAIT_EXPR:
26868 {
26869 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26870
26871 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26872 return true;
26873
26874 if (!type2)
26875 return false;
26876
26877 if (TREE_CODE (type2) != TREE_LIST)
26878 return dependent_type_p (type2);
26879
26880 for (; type2; type2 = TREE_CHAIN (type2))
26881 if (dependent_type_p (TREE_VALUE (type2)))
26882 return true;
26883
26884 return false;
26885 }
26886
26887 case MODOP_EXPR:
26888 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26889 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26890
26891 case ARRAY_REF:
26892 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26893 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26894
26895 case ADDR_EXPR:
26896 {
26897 tree op = TREE_OPERAND (expression, 0);
26898 return (value_dependent_expression_p (op)
26899 || has_value_dependent_address (op));
26900 }
26901
26902 case REQUIRES_EXPR:
26903 /* Treat all requires-expressions as value-dependent so
26904 we don't try to fold them. */
26905 return true;
26906
26907 case TYPE_REQ:
26908 return dependent_type_p (TREE_OPERAND (expression, 0));
26909
26910 case CALL_EXPR:
26911 {
26912 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26913 return true;
26914 tree fn = get_callee_fndecl (expression);
26915 int i, nargs;
26916 nargs = call_expr_nargs (expression);
26917 for (i = 0; i < nargs; ++i)
26918 {
26919 tree op = CALL_EXPR_ARG (expression, i);
26920 /* In a call to a constexpr member function, look through the
26921 implicit ADDR_EXPR on the object argument so that it doesn't
26922 cause the call to be considered value-dependent. We also
26923 look through it in potential_constant_expression. */
26924 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26925 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26926 && TREE_CODE (op) == ADDR_EXPR)
26927 op = TREE_OPERAND (op, 0);
26928 if (value_dependent_expression_p (op))
26929 return true;
26930 }
26931 return false;
26932 }
26933
26934 case TEMPLATE_ID_EXPR:
26935 return concept_definition_p (TREE_OPERAND (expression, 0));
26936
26937 case CONSTRUCTOR:
26938 {
26939 unsigned ix;
26940 tree val;
26941 if (dependent_type_p (TREE_TYPE (expression)))
26942 return true;
26943 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26944 if (value_dependent_expression_p (val))
26945 return true;
26946 return false;
26947 }
26948
26949 case STMT_EXPR:
26950 /* Treat a GNU statement expression as dependent to avoid crashing
26951 under instantiate_non_dependent_expr; it can't be constant. */
26952 return true;
26953
26954 default:
26955 /* A constant expression is value-dependent if any subexpression is
26956 value-dependent. */
26957 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26958 {
26959 case tcc_reference:
26960 case tcc_unary:
26961 case tcc_comparison:
26962 case tcc_binary:
26963 case tcc_expression:
26964 case tcc_vl_exp:
26965 {
26966 int i, len = cp_tree_operand_length (expression);
26967
26968 for (i = 0; i < len; i++)
26969 {
26970 tree t = TREE_OPERAND (expression, i);
26971
26972 /* In some cases, some of the operands may be missing.
26973 (For example, in the case of PREDECREMENT_EXPR, the
26974 amount to increment by may be missing.) That doesn't
26975 make the expression dependent. */
26976 if (t && value_dependent_expression_p (t))
26977 return true;
26978 }
26979 }
26980 break;
26981 default:
26982 break;
26983 }
26984 break;
26985 }
26986
26987 /* The expression is not value-dependent. */
26988 return false;
26989 }
26990
26991 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26992 [temp.dep.expr]. Note that an expression with no type is
26993 considered dependent. Other parts of the compiler arrange for an
26994 expression with type-dependent subexpressions to have no type, so
26995 this function doesn't have to be fully recursive. */
26996
26997 bool
type_dependent_expression_p(tree expression)26998 type_dependent_expression_p (tree expression)
26999 {
27000 if (!processing_template_decl)
27001 return false;
27002
27003 if (expression == NULL_TREE || expression == error_mark_node)
27004 return false;
27005
27006 STRIP_ANY_LOCATION_WRAPPER (expression);
27007
27008 /* An unresolved name is always dependent. */
27009 if (identifier_p (expression)
27010 || TREE_CODE (expression) == USING_DECL
27011 || TREE_CODE (expression) == WILDCARD_DECL)
27012 return true;
27013
27014 /* A lambda-expression in template context is dependent. dependent_type_p is
27015 true for a lambda in the scope of a class or function template, but that
27016 doesn't cover all template contexts, like a default template argument. */
27017 if (TREE_CODE (expression) == LAMBDA_EXPR)
27018 return true;
27019
27020 /* A fold expression is type-dependent. */
27021 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
27022 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
27023 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
27024 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
27025 return true;
27026
27027 /* Some expression forms are never type-dependent. */
27028 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
27029 || TREE_CODE (expression) == SIZEOF_EXPR
27030 || TREE_CODE (expression) == ALIGNOF_EXPR
27031 || TREE_CODE (expression) == AT_ENCODE_EXPR
27032 || TREE_CODE (expression) == NOEXCEPT_EXPR
27033 || TREE_CODE (expression) == TRAIT_EXPR
27034 || TREE_CODE (expression) == TYPEID_EXPR
27035 || TREE_CODE (expression) == DELETE_EXPR
27036 || TREE_CODE (expression) == VEC_DELETE_EXPR
27037 || TREE_CODE (expression) == THROW_EXPR
27038 || TREE_CODE (expression) == REQUIRES_EXPR)
27039 return false;
27040
27041 /* The types of these expressions depends only on the type to which
27042 the cast occurs. */
27043 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
27044 || TREE_CODE (expression) == STATIC_CAST_EXPR
27045 || TREE_CODE (expression) == CONST_CAST_EXPR
27046 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
27047 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
27048 || TREE_CODE (expression) == CAST_EXPR)
27049 return dependent_type_p (TREE_TYPE (expression));
27050
27051 /* The types of these expressions depends only on the type created
27052 by the expression. */
27053 if (TREE_CODE (expression) == NEW_EXPR
27054 || TREE_CODE (expression) == VEC_NEW_EXPR)
27055 {
27056 /* For NEW_EXPR tree nodes created inside a template, either
27057 the object type itself or a TREE_LIST may appear as the
27058 operand 1. */
27059 tree type = TREE_OPERAND (expression, 1);
27060 if (TREE_CODE (type) == TREE_LIST)
27061 /* This is an array type. We need to check array dimensions
27062 as well. */
27063 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
27064 || value_dependent_expression_p
27065 (TREE_OPERAND (TREE_VALUE (type), 1));
27066 else
27067 return dependent_type_p (type);
27068 }
27069
27070 if (TREE_CODE (expression) == SCOPE_REF)
27071 {
27072 tree scope = TREE_OPERAND (expression, 0);
27073 tree name = TREE_OPERAND (expression, 1);
27074
27075 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27076 contains an identifier associated by name lookup with one or more
27077 declarations declared with a dependent type, or...a
27078 nested-name-specifier or qualified-id that names a member of an
27079 unknown specialization. */
27080 return (type_dependent_expression_p (name)
27081 || dependent_scope_p (scope));
27082 }
27083
27084 if (TREE_CODE (expression) == TEMPLATE_DECL
27085 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
27086 return uses_outer_template_parms (expression);
27087
27088 if (TREE_CODE (expression) == STMT_EXPR)
27089 expression = stmt_expr_value_expr (expression);
27090
27091 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
27092 {
27093 tree elt;
27094 unsigned i;
27095
27096 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
27097 {
27098 if (type_dependent_expression_p (elt))
27099 return true;
27100 }
27101 return false;
27102 }
27103
27104 /* A static data member of the current instantiation with incomplete
27105 array type is type-dependent, as the definition and specializations
27106 can have different bounds. */
27107 if (VAR_P (expression)
27108 && DECL_CLASS_SCOPE_P (expression)
27109 && dependent_type_p (DECL_CONTEXT (expression))
27110 && VAR_HAD_UNKNOWN_BOUND (expression))
27111 return true;
27112
27113 /* An array of unknown bound depending on a variadic parameter, eg:
27114
27115 template<typename... Args>
27116 void foo (Args... args)
27117 {
27118 int arr[] = { args... };
27119 }
27120
27121 template<int... vals>
27122 void bar ()
27123 {
27124 int arr[] = { vals... };
27125 }
27126
27127 If the array has no length and has an initializer, it must be that
27128 we couldn't determine its length in cp_complete_array_type because
27129 it is dependent. */
27130 if (VAR_P (expression)
27131 && TREE_TYPE (expression) != NULL_TREE
27132 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
27133 && !TYPE_DOMAIN (TREE_TYPE (expression))
27134 && DECL_INITIAL (expression))
27135 return true;
27136
27137 /* A function or variable template-id is type-dependent if it has any
27138 dependent template arguments. */
27139 if (VAR_OR_FUNCTION_DECL_P (expression)
27140 && DECL_LANG_SPECIFIC (expression)
27141 && DECL_TEMPLATE_INFO (expression))
27142 {
27143 /* Consider the innermost template arguments, since those are the ones
27144 that come from the template-id; the template arguments for the
27145 enclosing class do not make it type-dependent unless they are used in
27146 the type of the decl. */
27147 if (instantiates_primary_template_p (expression)
27148 && (any_dependent_template_arguments_p
27149 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27150 return true;
27151 }
27152
27153 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27154 type-dependent. Checking this is important for functions with auto return
27155 type, which looks like a dependent type. */
27156 if (TREE_CODE (expression) == FUNCTION_DECL
27157 && !(DECL_CLASS_SCOPE_P (expression)
27158 && dependent_type_p (DECL_CONTEXT (expression)))
27159 && !(DECL_LANG_SPECIFIC (expression)
27160 && DECL_FRIEND_P (expression)
27161 && (!DECL_FRIEND_CONTEXT (expression)
27162 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27163 && !DECL_LOCAL_FUNCTION_P (expression))
27164 {
27165 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27166 || undeduced_auto_decl (expression));
27167 return false;
27168 }
27169
27170 /* Always dependent, on the number of arguments if nothing else. */
27171 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27172 return true;
27173
27174 if (TREE_TYPE (expression) == unknown_type_node)
27175 {
27176 if (TREE_CODE (expression) == ADDR_EXPR)
27177 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27178 if (TREE_CODE (expression) == COMPONENT_REF
27179 || TREE_CODE (expression) == OFFSET_REF)
27180 {
27181 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27182 return true;
27183 expression = TREE_OPERAND (expression, 1);
27184 if (identifier_p (expression))
27185 return false;
27186 }
27187 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27188 if (TREE_CODE (expression) == SCOPE_REF)
27189 return false;
27190
27191 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27192 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27193 || TREE_CODE (expression) == CO_YIELD_EXPR)
27194 return true;
27195
27196 if (BASELINK_P (expression))
27197 {
27198 if (BASELINK_OPTYPE (expression)
27199 && dependent_type_p (BASELINK_OPTYPE (expression)))
27200 return true;
27201 expression = BASELINK_FUNCTIONS (expression);
27202 }
27203
27204 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27205 {
27206 if (any_dependent_template_arguments_p
27207 (TREE_OPERAND (expression, 1)))
27208 return true;
27209 expression = TREE_OPERAND (expression, 0);
27210 if (identifier_p (expression))
27211 return true;
27212 }
27213
27214 gcc_assert (OVL_P (expression));
27215
27216 for (lkp_iterator iter (expression); iter; ++iter)
27217 if (type_dependent_expression_p (*iter))
27218 return true;
27219
27220 return false;
27221 }
27222
27223 /* The type of a non-type template parm declared with a placeholder type
27224 depends on the corresponding template argument, even though
27225 placeholders are not normally considered dependent. */
27226 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27227 && is_auto (TREE_TYPE (expression)))
27228 return true;
27229
27230 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27231
27232 /* Dependent type attributes might not have made it from the decl to
27233 the type yet. */
27234 if (DECL_P (expression)
27235 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27236 return true;
27237
27238 return (dependent_type_p (TREE_TYPE (expression)));
27239 }
27240
27241 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27242 type-dependent if the expression refers to a member of the current
27243 instantiation and the type of the referenced member is dependent, or the
27244 class member access expression refers to a member of an unknown
27245 specialization.
27246
27247 This function returns true if the OBJECT in such a class member access
27248 expression is of an unknown specialization. */
27249
27250 bool
type_dependent_object_expression_p(tree object)27251 type_dependent_object_expression_p (tree object)
27252 {
27253 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27254 dependent. */
27255 if (TREE_CODE (object) == IDENTIFIER_NODE)
27256 return true;
27257 tree scope = TREE_TYPE (object);
27258 return (!scope || dependent_scope_p (scope));
27259 }
27260
27261 /* walk_tree callback function for instantiation_dependent_expression_p,
27262 below. Returns non-zero if a dependent subexpression is found. */
27263
27264 static tree
instantiation_dependent_r(tree * tp,int * walk_subtrees,void *)27265 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27266 void * /*data*/)
27267 {
27268 if (TYPE_P (*tp))
27269 {
27270 /* We don't have to worry about decltype currently because decltype
27271 of an instantiation-dependent expr is a dependent type. This
27272 might change depending on the resolution of DR 1172. */
27273 *walk_subtrees = false;
27274 return NULL_TREE;
27275 }
27276 enum tree_code code = TREE_CODE (*tp);
27277 switch (code)
27278 {
27279 /* Don't treat an argument list as dependent just because it has no
27280 TREE_TYPE. */
27281 case TREE_LIST:
27282 case TREE_VEC:
27283 case NONTYPE_ARGUMENT_PACK:
27284 return NULL_TREE;
27285
27286 case TEMPLATE_PARM_INDEX:
27287 if (dependent_type_p (TREE_TYPE (*tp)))
27288 return *tp;
27289 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27290 return *tp;
27291 /* We'll check value-dependence separately. */
27292 return NULL_TREE;
27293
27294 /* Handle expressions with type operands. */
27295 case SIZEOF_EXPR:
27296 case ALIGNOF_EXPR:
27297 case TYPEID_EXPR:
27298 case AT_ENCODE_EXPR:
27299 {
27300 tree op = TREE_OPERAND (*tp, 0);
27301 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27302 op = TREE_TYPE (op);
27303 if (TYPE_P (op))
27304 {
27305 if (dependent_type_p (op))
27306 return *tp;
27307 else
27308 {
27309 *walk_subtrees = false;
27310 return NULL_TREE;
27311 }
27312 }
27313 break;
27314 }
27315
27316 case COMPONENT_REF:
27317 if (identifier_p (TREE_OPERAND (*tp, 1)))
27318 /* In a template, finish_class_member_access_expr creates a
27319 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27320 type-dependent, so that we can check access control at
27321 instantiation time (PR 42277). See also Core issue 1273. */
27322 return *tp;
27323 break;
27324
27325 case SCOPE_REF:
27326 if (instantiation_dependent_scope_ref_p (*tp))
27327 return *tp;
27328 else
27329 break;
27330
27331 /* Treat statement-expressions as dependent. */
27332 case BIND_EXPR:
27333 return *tp;
27334
27335 /* Treat requires-expressions as dependent. */
27336 case REQUIRES_EXPR:
27337 return *tp;
27338
27339 case CALL_EXPR:
27340 /* Treat concept checks as dependent. */
27341 if (concept_check_p (*tp))
27342 return *tp;
27343 break;
27344
27345 case TEMPLATE_ID_EXPR:
27346 /* Treat concept checks as dependent. */
27347 if (concept_check_p (*tp))
27348 return *tp;
27349 break;
27350
27351 case CONSTRUCTOR:
27352 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27353 return *tp;
27354 break;
27355
27356 default:
27357 break;
27358 }
27359
27360 if (type_dependent_expression_p (*tp))
27361 return *tp;
27362 else
27363 return NULL_TREE;
27364 }
27365
27366 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27367 sense defined by the ABI:
27368
27369 "An expression is instantiation-dependent if it is type-dependent
27370 or value-dependent, or it has a subexpression that is type-dependent
27371 or value-dependent."
27372
27373 Except don't actually check value-dependence for unevaluated expressions,
27374 because in sizeof(i) we don't care about the value of i. Checking
27375 type-dependence will in turn check value-dependence of array bounds/template
27376 arguments as needed. */
27377
27378 bool
instantiation_dependent_uneval_expression_p(tree expression)27379 instantiation_dependent_uneval_expression_p (tree expression)
27380 {
27381 tree result;
27382
27383 if (!processing_template_decl)
27384 return false;
27385
27386 if (expression == error_mark_node)
27387 return false;
27388
27389 result = cp_walk_tree_without_duplicates (&expression,
27390 instantiation_dependent_r, NULL);
27391 return result != NULL_TREE;
27392 }
27393
27394 /* As above, but also check value-dependence of the expression as a whole. */
27395
27396 bool
instantiation_dependent_expression_p(tree expression)27397 instantiation_dependent_expression_p (tree expression)
27398 {
27399 return (instantiation_dependent_uneval_expression_p (expression)
27400 || value_dependent_expression_p (expression));
27401 }
27402
27403 /* Like type_dependent_expression_p, but it also works while not processing
27404 a template definition, i.e. during substitution or mangling. */
27405
27406 bool
type_dependent_expression_p_push(tree expr)27407 type_dependent_expression_p_push (tree expr)
27408 {
27409 bool b;
27410 ++processing_template_decl;
27411 b = type_dependent_expression_p (expr);
27412 --processing_template_decl;
27413 return b;
27414 }
27415
27416 /* Returns TRUE if ARGS contains a type-dependent expression. */
27417
27418 bool
any_type_dependent_arguments_p(const vec<tree,va_gc> * args)27419 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27420 {
27421 unsigned int i;
27422 tree arg;
27423
27424 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27425 {
27426 if (type_dependent_expression_p (arg))
27427 return true;
27428 }
27429 return false;
27430 }
27431
27432 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27433 expressions) contains any type-dependent expressions. */
27434
27435 bool
any_type_dependent_elements_p(const_tree list)27436 any_type_dependent_elements_p (const_tree list)
27437 {
27438 for (; list; list = TREE_CHAIN (list))
27439 if (type_dependent_expression_p (TREE_VALUE (list)))
27440 return true;
27441
27442 return false;
27443 }
27444
27445 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27446 expressions) contains any value-dependent expressions. */
27447
27448 bool
any_value_dependent_elements_p(const_tree list)27449 any_value_dependent_elements_p (const_tree list)
27450 {
27451 for (; list; list = TREE_CHAIN (list))
27452 if (value_dependent_expression_p (TREE_VALUE (list)))
27453 return true;
27454
27455 return false;
27456 }
27457
27458 /* Returns TRUE if the ARG (a template argument) is dependent. */
27459
27460 bool
dependent_template_arg_p(tree arg)27461 dependent_template_arg_p (tree arg)
27462 {
27463 if (!processing_template_decl)
27464 return false;
27465
27466 /* Assume a template argument that was wrongly written by the user
27467 is dependent. This is consistent with what
27468 any_dependent_template_arguments_p [that calls this function]
27469 does. */
27470 if (!arg || arg == error_mark_node)
27471 return true;
27472
27473 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27474 arg = argument_pack_select_arg (arg);
27475
27476 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27477 return true;
27478 if (TREE_CODE (arg) == TEMPLATE_DECL)
27479 {
27480 if (DECL_TEMPLATE_PARM_P (arg))
27481 return true;
27482 /* A member template of a dependent class is not necessarily
27483 type-dependent, but it is a dependent template argument because it
27484 will be a member of an unknown specialization to that template. */
27485 tree scope = CP_DECL_CONTEXT (arg);
27486 return TYPE_P (scope) && dependent_type_p (scope);
27487 }
27488 else if (ARGUMENT_PACK_P (arg))
27489 {
27490 tree args = ARGUMENT_PACK_ARGS (arg);
27491 int i, len = TREE_VEC_LENGTH (args);
27492 for (i = 0; i < len; ++i)
27493 {
27494 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27495 return true;
27496 }
27497
27498 return false;
27499 }
27500 else if (TYPE_P (arg))
27501 return dependent_type_p (arg);
27502 else
27503 return value_dependent_expression_p (arg);
27504 }
27505
27506 /* Returns true if ARGS (a collection of template arguments) contains
27507 any types that require structural equality testing. */
27508
27509 bool
any_template_arguments_need_structural_equality_p(tree args)27510 any_template_arguments_need_structural_equality_p (tree args)
27511 {
27512 int i;
27513 int j;
27514
27515 if (!args)
27516 return false;
27517 if (args == error_mark_node)
27518 return true;
27519
27520 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27521 {
27522 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27523 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27524 {
27525 tree arg = TREE_VEC_ELT (level, j);
27526 tree packed_args = NULL_TREE;
27527 int k, len = 1;
27528
27529 if (ARGUMENT_PACK_P (arg))
27530 {
27531 /* Look inside the argument pack. */
27532 packed_args = ARGUMENT_PACK_ARGS (arg);
27533 len = TREE_VEC_LENGTH (packed_args);
27534 }
27535
27536 for (k = 0; k < len; ++k)
27537 {
27538 if (packed_args)
27539 arg = TREE_VEC_ELT (packed_args, k);
27540
27541 if (error_operand_p (arg))
27542 return true;
27543 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27544 continue;
27545 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27546 return true;
27547 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27548 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27549 return true;
27550 }
27551 }
27552 }
27553
27554 return false;
27555 }
27556
27557 /* Returns true if ARGS (a collection of template arguments) contains
27558 any dependent arguments. */
27559
27560 bool
any_dependent_template_arguments_p(const_tree args)27561 any_dependent_template_arguments_p (const_tree args)
27562 {
27563 int i;
27564 int j;
27565
27566 if (!args)
27567 return false;
27568 if (args == error_mark_node)
27569 return true;
27570
27571 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27572 {
27573 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27574 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27575 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27576 return true;
27577 }
27578
27579 return false;
27580 }
27581
27582 /* Returns true if ARGS contains any errors. */
27583
27584 bool
any_erroneous_template_args_p(const_tree args)27585 any_erroneous_template_args_p (const_tree args)
27586 {
27587 int i;
27588 int j;
27589
27590 if (args == error_mark_node)
27591 return true;
27592
27593 if (args && TREE_CODE (args) != TREE_VEC)
27594 {
27595 if (tree ti = get_template_info (args))
27596 args = TI_ARGS (ti);
27597 else
27598 args = NULL_TREE;
27599 }
27600
27601 if (!args)
27602 return false;
27603
27604 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27605 {
27606 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27607 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27608 if (error_operand_p (TREE_VEC_ELT (level, j)))
27609 return true;
27610 }
27611
27612 return false;
27613 }
27614
27615 /* Returns TRUE if the template TMPL is type-dependent. */
27616
27617 bool
dependent_template_p(tree tmpl)27618 dependent_template_p (tree tmpl)
27619 {
27620 if (TREE_CODE (tmpl) == OVERLOAD)
27621 {
27622 for (lkp_iterator iter (tmpl); iter; ++iter)
27623 if (dependent_template_p (*iter))
27624 return true;
27625 return false;
27626 }
27627
27628 /* Template template parameters are dependent. */
27629 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27630 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27631 return true;
27632 /* So are names that have not been looked up. */
27633 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27634 return true;
27635 return false;
27636 }
27637
27638 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27639
27640 bool
dependent_template_id_p(tree tmpl,tree args)27641 dependent_template_id_p (tree tmpl, tree args)
27642 {
27643 return (dependent_template_p (tmpl)
27644 || any_dependent_template_arguments_p (args));
27645 }
27646
27647 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27648 are dependent. */
27649
27650 bool
dependent_omp_for_p(tree declv,tree initv,tree condv,tree incrv)27651 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27652 {
27653 int i;
27654
27655 if (!processing_template_decl)
27656 return false;
27657
27658 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27659 {
27660 tree decl = TREE_VEC_ELT (declv, i);
27661 tree init = TREE_VEC_ELT (initv, i);
27662 tree cond = TREE_VEC_ELT (condv, i);
27663 tree incr = TREE_VEC_ELT (incrv, i);
27664
27665 if (type_dependent_expression_p (decl)
27666 || TREE_CODE (decl) == SCOPE_REF)
27667 return true;
27668
27669 if (init && type_dependent_expression_p (init))
27670 return true;
27671
27672 if (cond == global_namespace)
27673 return true;
27674
27675 if (type_dependent_expression_p (cond))
27676 return true;
27677
27678 if (COMPARISON_CLASS_P (cond)
27679 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27680 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27681 return true;
27682
27683 if (TREE_CODE (incr) == MODOP_EXPR)
27684 {
27685 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27686 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27687 return true;
27688 }
27689 else if (type_dependent_expression_p (incr))
27690 return true;
27691 else if (TREE_CODE (incr) == MODIFY_EXPR)
27692 {
27693 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27694 return true;
27695 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27696 {
27697 tree t = TREE_OPERAND (incr, 1);
27698 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27699 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27700 return true;
27701
27702 /* If this loop has a class iterator with != comparison
27703 with increment other than i++/++i/i--/--i, make sure the
27704 increment is constant. */
27705 if (CLASS_TYPE_P (TREE_TYPE (decl))
27706 && TREE_CODE (cond) == NE_EXPR)
27707 {
27708 if (TREE_OPERAND (t, 0) == decl)
27709 t = TREE_OPERAND (t, 1);
27710 else
27711 t = TREE_OPERAND (t, 0);
27712 if (TREE_CODE (t) != INTEGER_CST)
27713 return true;
27714 }
27715 }
27716 }
27717 }
27718
27719 return false;
27720 }
27721
27722 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27723 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27724 no such TYPE can be found. Note that this function peers inside
27725 uninstantiated templates and therefore should be used only in
27726 extremely limited situations. ONLY_CURRENT_P restricts this
27727 peering to the currently open classes hierarchy (which is required
27728 when comparing types). */
27729
27730 tree
resolve_typename_type(tree type,bool only_current_p)27731 resolve_typename_type (tree type, bool only_current_p)
27732 {
27733 tree scope;
27734 tree name;
27735 tree decl;
27736 int quals;
27737 tree pushed_scope;
27738 tree result;
27739
27740 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27741
27742 scope = TYPE_CONTEXT (type);
27743 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27744 gcc_checking_assert (uses_template_parms (scope));
27745
27746 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27747 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27748 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27749 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27750 identifier of the TYPENAME_TYPE anymore.
27751 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27752 TYPENAME_TYPE instead, we avoid messing up with a possible
27753 typedef variant case. */
27754 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27755
27756 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27757 it first before we can figure out what NAME refers to. */
27758 if (TREE_CODE (scope) == TYPENAME_TYPE)
27759 {
27760 if (TYPENAME_IS_RESOLVING_P (scope))
27761 /* Given a class template A with a dependent base with nested type C,
27762 typedef typename A::C::C C will land us here, as trying to resolve
27763 the initial A::C leads to the local C typedef, which leads back to
27764 A::C::C. So we break the recursion now. */
27765 return type;
27766 else
27767 scope = resolve_typename_type (scope, only_current_p);
27768 }
27769 /* If we don't know what SCOPE refers to, then we cannot resolve the
27770 TYPENAME_TYPE. */
27771 if (!CLASS_TYPE_P (scope))
27772 return type;
27773 /* If this is a typedef, we don't want to look inside (c++/11987). */
27774 if (typedef_variant_p (type))
27775 return type;
27776 /* If SCOPE isn't the template itself, it will not have a valid
27777 TYPE_FIELDS list. */
27778 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27779 /* scope is either the template itself or a compatible instantiation
27780 like X<T>, so look up the name in the original template. */
27781 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27782 /* If scope has no fields, it can't be a current instantiation. Check this
27783 before currently_open_class to avoid infinite recursion (71515). */
27784 if (!TYPE_FIELDS (scope))
27785 return type;
27786 /* If the SCOPE is not the current instantiation, there's no reason
27787 to look inside it. */
27788 if (only_current_p && !currently_open_class (scope))
27789 return type;
27790 /* Enter the SCOPE so that name lookup will be resolved as if we
27791 were in the class definition. In particular, SCOPE will no
27792 longer be considered a dependent type. */
27793 pushed_scope = push_scope (scope);
27794 /* Look up the declaration. */
27795 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27796 tf_warning_or_error);
27797
27798 result = NULL_TREE;
27799
27800 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27801 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27802 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27803 if (!decl)
27804 /*nop*/;
27805 else if (identifier_p (fullname)
27806 && TREE_CODE (decl) == TYPE_DECL)
27807 {
27808 result = TREE_TYPE (decl);
27809 if (result == error_mark_node)
27810 result = NULL_TREE;
27811 }
27812 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27813 && DECL_CLASS_TEMPLATE_P (decl))
27814 {
27815 /* Obtain the template and the arguments. */
27816 tree tmpl = TREE_OPERAND (fullname, 0);
27817 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27818 {
27819 /* We get here with a plain identifier because a previous tentative
27820 parse of the nested-name-specifier as part of a ptr-operator saw
27821 ::template X<A>. The use of ::template is necessary in a
27822 ptr-operator, but wrong in a declarator-id.
27823
27824 [temp.names]: In a qualified-id of a declarator-id, the keyword
27825 template shall not appear at the top level. */
27826 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27827 "keyword %<template%> not allowed in declarator-id");
27828 tmpl = decl;
27829 }
27830 tree args = TREE_OPERAND (fullname, 1);
27831 /* Instantiate the template. */
27832 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27833 /*entering_scope=*/true,
27834 tf_error | tf_user);
27835 if (result == error_mark_node)
27836 result = NULL_TREE;
27837 }
27838
27839 /* Leave the SCOPE. */
27840 if (pushed_scope)
27841 pop_scope (pushed_scope);
27842
27843 /* If we failed to resolve it, return the original typename. */
27844 if (!result)
27845 return type;
27846
27847 /* If lookup found a typename type, resolve that too. */
27848 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27849 {
27850 /* Ill-formed programs can cause infinite recursion here, so we
27851 must catch that. */
27852 TYPENAME_IS_RESOLVING_P (result) = 1;
27853 result = resolve_typename_type (result, only_current_p);
27854 TYPENAME_IS_RESOLVING_P (result) = 0;
27855 }
27856
27857 /* Qualify the resulting type. */
27858 quals = cp_type_quals (type);
27859 if (quals)
27860 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27861
27862 return result;
27863 }
27864
27865 /* EXPR is an expression which is not type-dependent. Return a proxy
27866 for EXPR that can be used to compute the types of larger
27867 expressions containing EXPR. */
27868
27869 tree
build_non_dependent_expr(tree expr)27870 build_non_dependent_expr (tree expr)
27871 {
27872 tree orig_expr = expr;
27873 tree inner_expr;
27874
27875 /* When checking, try to get a constant value for all non-dependent
27876 expressions in order to expose bugs in *_dependent_expression_p
27877 and constexpr. This can affect code generation, see PR70704, so
27878 only do this for -fchecking=2. */
27879 if (flag_checking > 1
27880 && cxx_dialect >= cxx11
27881 /* Don't do this during nsdmi parsing as it can lead to
27882 unexpected recursive instantiations. */
27883 && !parsing_nsdmi ()
27884 /* Don't do this during concept processing either and for
27885 the same reason. */
27886 && !processing_constraint_expression_p ())
27887 fold_non_dependent_expr (expr, tf_none);
27888
27889 STRIP_ANY_LOCATION_WRAPPER (expr);
27890
27891 /* Preserve OVERLOADs; the functions must be available to resolve
27892 types. */
27893 inner_expr = expr;
27894 if (TREE_CODE (inner_expr) == STMT_EXPR)
27895 inner_expr = stmt_expr_value_expr (inner_expr);
27896 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27897 inner_expr = TREE_OPERAND (inner_expr, 0);
27898 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27899 inner_expr = TREE_OPERAND (inner_expr, 1);
27900 if (is_overloaded_fn (inner_expr)
27901 || TREE_CODE (inner_expr) == OFFSET_REF)
27902 return orig_expr;
27903 /* There is no need to return a proxy for a variable or enumerator. */
27904 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27905 return orig_expr;
27906 /* Preserve string constants; conversions from string constants to
27907 "char *" are allowed, even though normally a "const char *"
27908 cannot be used to initialize a "char *". */
27909 if (TREE_CODE (expr) == STRING_CST)
27910 return orig_expr;
27911 /* Preserve void and arithmetic constants, as an optimization -- there is no
27912 reason to create a new node. */
27913 if (TREE_CODE (expr) == VOID_CST
27914 || TREE_CODE (expr) == INTEGER_CST
27915 || TREE_CODE (expr) == REAL_CST)
27916 return orig_expr;
27917 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27918 There is at least one place where we want to know that a
27919 particular expression is a throw-expression: when checking a ?:
27920 expression, there are special rules if the second or third
27921 argument is a throw-expression. */
27922 if (TREE_CODE (expr) == THROW_EXPR)
27923 return orig_expr;
27924
27925 /* Don't wrap an initializer list, we need to be able to look inside. */
27926 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27927 return orig_expr;
27928
27929 /* Don't wrap a dummy object, we need to be able to test for it. */
27930 if (is_dummy_object (expr))
27931 return orig_expr;
27932
27933 if (TREE_CODE (expr) == COND_EXPR)
27934 return build3 (COND_EXPR,
27935 TREE_TYPE (expr),
27936 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27937 (TREE_OPERAND (expr, 1)
27938 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27939 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27940 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27941 if (TREE_CODE (expr) == COMPOUND_EXPR
27942 && !COMPOUND_EXPR_OVERLOADED (expr))
27943 return build2 (COMPOUND_EXPR,
27944 TREE_TYPE (expr),
27945 TREE_OPERAND (expr, 0),
27946 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27947
27948 /* If the type is unknown, it can't really be non-dependent */
27949 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27950
27951 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27952 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27953 TREE_TYPE (expr), expr);
27954 }
27955
27956 /* ARGS is a vector of expressions as arguments to a function call.
27957 Replace the arguments with equivalent non-dependent expressions.
27958 This modifies ARGS in place. */
27959
27960 void
make_args_non_dependent(vec<tree,va_gc> * args)27961 make_args_non_dependent (vec<tree, va_gc> *args)
27962 {
27963 unsigned int ix;
27964 tree arg;
27965
27966 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27967 {
27968 tree newarg = build_non_dependent_expr (arg);
27969 if (newarg != arg)
27970 (*args)[ix] = newarg;
27971 }
27972 }
27973
27974 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27975 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27976 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27977
27978 static tree
make_auto_1(tree name,bool set_canonical)27979 make_auto_1 (tree name, bool set_canonical)
27980 {
27981 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27982 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27983 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27984 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27985 (0, processing_template_decl + 1, processing_template_decl + 1,
27986 TYPE_NAME (au), NULL_TREE);
27987 if (set_canonical)
27988 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27989 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27990 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27991 if (name == decltype_auto_identifier)
27992 AUTO_IS_DECLTYPE (au) = true;
27993
27994 return au;
27995 }
27996
27997 tree
make_decltype_auto(void)27998 make_decltype_auto (void)
27999 {
28000 return make_auto_1 (decltype_auto_identifier, true);
28001 }
28002
28003 tree
make_auto(void)28004 make_auto (void)
28005 {
28006 return make_auto_1 (auto_identifier, true);
28007 }
28008
28009 /* Return a C++17 deduction placeholder for class template TMPL. */
28010
28011 tree
make_template_placeholder(tree tmpl)28012 make_template_placeholder (tree tmpl)
28013 {
28014 tree t = make_auto_1 (auto_identifier, false);
28015 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
28016 /* Our canonical type depends on the placeholder. */
28017 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28018 return t;
28019 }
28020
28021 /* True iff T is a C++17 class template deduction placeholder. */
28022
28023 bool
template_placeholder_p(tree t)28024 template_placeholder_p (tree t)
28025 {
28026 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
28027 }
28028
28029 /* Make a "constrained auto" type-specifier. This is an auto or
28030 decltype(auto) type with constraints that must be associated after
28031 deduction. The constraint is formed from the given concept CON
28032 and its optional sequence of template arguments ARGS.
28033
28034 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28035
28036 static tree
make_constrained_placeholder_type(tree type,tree con,tree args)28037 make_constrained_placeholder_type (tree type, tree con, tree args)
28038 {
28039 /* Build the constraint. */
28040 tree tmpl = DECL_TI_TEMPLATE (con);
28041 tree expr = tmpl;
28042 if (TREE_CODE (con) == FUNCTION_DECL)
28043 expr = ovl_make (tmpl);
28044 ++processing_template_decl;
28045 expr = build_concept_check (expr, type, args, tf_warning_or_error);
28046 --processing_template_decl;
28047
28048 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
28049
28050 /* Our canonical type depends on the constraint. */
28051 TYPE_CANONICAL (type) = canonical_type_parameter (type);
28052
28053 /* Attach the constraint to the type declaration. */
28054 return TYPE_NAME (type);
28055 }
28056
28057 /* Make a "constrained auto" type-specifier. */
28058
28059 tree
make_constrained_auto(tree con,tree args)28060 make_constrained_auto (tree con, tree args)
28061 {
28062 tree type = make_auto_1 (auto_identifier, false);
28063 return make_constrained_placeholder_type (type, con, args);
28064 }
28065
28066 /* Make a "constrained decltype(auto)" type-specifier. */
28067
28068 tree
make_constrained_decltype_auto(tree con,tree args)28069 make_constrained_decltype_auto (tree con, tree args)
28070 {
28071 tree type = make_auto_1 (decltype_auto_identifier, false);
28072 return make_constrained_placeholder_type (type, con, args);
28073 }
28074
28075 /* Build and return a concept definition. Like other templates, the
28076 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28077 the TEMPLATE_DECL. */
28078
28079 tree
finish_concept_definition(cp_expr id,tree init)28080 finish_concept_definition (cp_expr id, tree init)
28081 {
28082 gcc_assert (identifier_p (id));
28083 gcc_assert (processing_template_decl);
28084
28085 location_t loc = id.get_location();
28086
28087 /* A concept-definition shall not have associated constraints. */
28088 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
28089 {
28090 error_at (loc, "a concept cannot be constrained");
28091 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
28092 }
28093
28094 /* A concept-definition shall appear in namespace scope. Templates
28095 aren't allowed in block scope, so we only need to check for class
28096 scope. */
28097 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28098 {
28099 error_at (loc, "concept %qE not in namespace scope", *id);
28100 return error_mark_node;
28101 }
28102
28103 /* Initially build the concept declaration; it's type is bool. */
28104 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
28105 DECL_CONTEXT (decl) = current_scope ();
28106 DECL_INITIAL (decl) = init;
28107
28108 /* Push the enclosing template. */
28109 return push_template_decl (decl);
28110 }
28111
28112 /* Given type ARG, return std::initializer_list<ARG>. */
28113
28114 static tree
listify(tree arg)28115 listify (tree arg)
28116 {
28117 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
28118
28119 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
28120 {
28121 gcc_rich_location richloc (input_location);
28122 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
28123 error_at (&richloc,
28124 "deducing from brace-enclosed initializer list"
28125 " requires %<#include <initializer_list>%>");
28126
28127 return error_mark_node;
28128 }
28129 tree argvec = make_tree_vec (1);
28130 TREE_VEC_ELT (argvec, 0) = arg;
28131
28132 return lookup_template_class (std_init_list, argvec, NULL_TREE,
28133 NULL_TREE, 0, tf_warning_or_error);
28134 }
28135
28136 /* Replace auto in TYPE with std::initializer_list<auto>. */
28137
28138 static tree
listify_autos(tree type,tree auto_node)28139 listify_autos (tree type, tree auto_node)
28140 {
28141 tree init_auto = listify (strip_top_quals (auto_node));
28142 tree argvec = make_tree_vec (1);
28143 TREE_VEC_ELT (argvec, 0) = init_auto;
28144 if (processing_template_decl)
28145 argvec = add_to_template_args (current_template_args (), argvec);
28146 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28147 }
28148
28149 /* Hash traits for hashing possibly constrained 'auto'
28150 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28151
28152 struct auto_hash : default_hash_traits<tree>
28153 {
28154 static inline hashval_t hash (tree);
28155 static inline bool equal (tree, tree);
28156 };
28157
28158 /* Hash the 'auto' T. */
28159
28160 inline hashval_t
hash(tree t)28161 auto_hash::hash (tree t)
28162 {
28163 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28164 /* Matching constrained-type-specifiers denote the same template
28165 parameter, so hash the constraint. */
28166 return hash_placeholder_constraint (c);
28167 else
28168 /* But unconstrained autos are all separate, so just hash the pointer. */
28169 return iterative_hash_object (t, 0);
28170 }
28171
28172 /* Compare two 'auto's. */
28173
28174 inline bool
equal(tree t1,tree t2)28175 auto_hash::equal (tree t1, tree t2)
28176 {
28177 if (t1 == t2)
28178 return true;
28179
28180 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28181 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28182
28183 /* Two unconstrained autos are distinct. */
28184 if (!c1 || !c2)
28185 return false;
28186
28187 return equivalent_placeholder_constraints (c1, c2);
28188 }
28189
28190 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28191 constrained) auto, add it to the vector. */
28192
28193 static int
extract_autos_r(tree t,void * data)28194 extract_autos_r (tree t, void *data)
28195 {
28196 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28197 if (is_auto (t))
28198 {
28199 /* All the autos were built with index 0; fix that up now. */
28200 tree *p = hash.find_slot (t, INSERT);
28201 unsigned idx;
28202 if (*p)
28203 /* If this is a repeated constrained-type-specifier, use the index we
28204 chose before. */
28205 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28206 else
28207 {
28208 /* Otherwise this is new, so use the current count. */
28209 *p = t;
28210 idx = hash.elements () - 1;
28211 }
28212 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28213 }
28214
28215 /* Always keep walking. */
28216 return 0;
28217 }
28218
28219 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28220 says they can appear anywhere in the type. */
28221
28222 static tree
extract_autos(tree type)28223 extract_autos (tree type)
28224 {
28225 hash_set<tree> visited;
28226 hash_table<auto_hash> hash (2);
28227
28228 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28229
28230 tree tree_vec = make_tree_vec (hash.elements());
28231 for (hash_table<auto_hash>::iterator iter = hash.begin();
28232 iter != hash.end(); ++iter)
28233 {
28234 tree elt = *iter;
28235 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28236 TREE_VEC_ELT (tree_vec, i)
28237 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28238 }
28239
28240 return tree_vec;
28241 }
28242
28243 /* The stem for deduction guide names. */
28244 const char *const dguide_base = "__dguide_";
28245
28246 /* Return the name for a deduction guide for class template TMPL. */
28247
28248 tree
dguide_name(tree tmpl)28249 dguide_name (tree tmpl)
28250 {
28251 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28252 tree tname = TYPE_IDENTIFIER (type);
28253 char *buf = (char *) alloca (1 + strlen (dguide_base)
28254 + IDENTIFIER_LENGTH (tname));
28255 memcpy (buf, dguide_base, strlen (dguide_base));
28256 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28257 IDENTIFIER_LENGTH (tname) + 1);
28258 tree dname = get_identifier (buf);
28259 TREE_TYPE (dname) = type;
28260 return dname;
28261 }
28262
28263 /* True if NAME is the name of a deduction guide. */
28264
28265 bool
dguide_name_p(tree name)28266 dguide_name_p (tree name)
28267 {
28268 return (TREE_CODE (name) == IDENTIFIER_NODE
28269 && TREE_TYPE (name)
28270 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28271 strlen (dguide_base)));
28272 }
28273
28274 /* True if FN is a deduction guide. */
28275
28276 bool
deduction_guide_p(const_tree fn)28277 deduction_guide_p (const_tree fn)
28278 {
28279 if (DECL_P (fn))
28280 if (tree name = DECL_NAME (fn))
28281 return dguide_name_p (name);
28282 return false;
28283 }
28284
28285 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28286
28287 bool
copy_guide_p(const_tree fn)28288 copy_guide_p (const_tree fn)
28289 {
28290 gcc_assert (deduction_guide_p (fn));
28291 if (!DECL_ARTIFICIAL (fn))
28292 return false;
28293 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28294 return (TREE_CHAIN (parms) == void_list_node
28295 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28296 }
28297
28298 /* True if FN is a guide generated from a constructor template. */
28299
28300 bool
template_guide_p(const_tree fn)28301 template_guide_p (const_tree fn)
28302 {
28303 gcc_assert (deduction_guide_p (fn));
28304 if (!DECL_ARTIFICIAL (fn))
28305 return false;
28306 tree tmpl = DECL_TI_TEMPLATE (fn);
28307 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28308 return PRIMARY_TEMPLATE_P (org);
28309 return false;
28310 }
28311
28312 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28313 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28314 template parameter types. Note that the handling of template template
28315 parameters relies on current_template_parms being set appropriately for the
28316 new template. */
28317
28318 static tree
rewrite_template_parm(tree olddecl,unsigned index,unsigned level,tree tsubst_args,tsubst_flags_t complain)28319 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28320 tree tsubst_args, tsubst_flags_t complain)
28321 {
28322 if (olddecl == error_mark_node)
28323 return error_mark_node;
28324
28325 tree oldidx = get_template_parm_index (olddecl);
28326
28327 tree newtype;
28328 if (TREE_CODE (olddecl) == TYPE_DECL
28329 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28330 {
28331 tree oldtype = TREE_TYPE (olddecl);
28332 newtype = cxx_make_type (TREE_CODE (oldtype));
28333 TYPE_MAIN_VARIANT (newtype) = newtype;
28334 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28335 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28336 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28337 }
28338 else
28339 {
28340 newtype = TREE_TYPE (olddecl);
28341 if (type_uses_auto (newtype))
28342 {
28343 // Substitute once to fix references to other template parameters.
28344 newtype = tsubst (newtype, tsubst_args,
28345 complain|tf_partial, NULL_TREE);
28346 // Now substitute again to reduce the level of the auto.
28347 newtype = tsubst (newtype, current_template_args (),
28348 complain, NULL_TREE);
28349 }
28350 else
28351 newtype = tsubst (newtype, tsubst_args,
28352 complain, NULL_TREE);
28353 }
28354
28355 tree newdecl
28356 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28357 DECL_NAME (olddecl), newtype);
28358 SET_DECL_TEMPLATE_PARM_P (newdecl);
28359
28360 tree newidx;
28361 if (TREE_CODE (olddecl) == TYPE_DECL
28362 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28363 {
28364 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28365 = build_template_parm_index (index, level, level,
28366 newdecl, newtype);
28367 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28368 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28369 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28370 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28371 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28372 else
28373 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28374
28375 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28376 {
28377 DECL_TEMPLATE_RESULT (newdecl)
28378 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28379 DECL_NAME (olddecl), newtype);
28380 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28381 // First create a copy (ttargs) of tsubst_args with an
28382 // additional level for the template template parameter's own
28383 // template parameters (ttparms).
28384 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28385 (DECL_TEMPLATE_PARMS (olddecl)));
28386 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28387 tree ttargs = make_tree_vec (depth + 1);
28388 for (int i = 0; i < depth; ++i)
28389 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28390 TREE_VEC_ELT (ttargs, depth)
28391 = template_parms_level_to_args (ttparms);
28392 // Substitute ttargs into ttparms to fix references to
28393 // other template parameters.
28394 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28395 complain|tf_partial);
28396 // Now substitute again with args based on tparms, to reduce
28397 // the level of the ttparms.
28398 ttargs = current_template_args ();
28399 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28400 complain);
28401 // Finally, tack the adjusted parms onto tparms.
28402 ttparms = tree_cons (size_int (depth), ttparms,
28403 current_template_parms);
28404 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28405 }
28406 }
28407 else
28408 {
28409 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28410 tree newconst
28411 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28412 TREE_CODE (oldconst),
28413 DECL_NAME (oldconst), newtype);
28414 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28415 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28416 SET_DECL_TEMPLATE_PARM_P (newconst);
28417 newidx = build_template_parm_index (index, level, level,
28418 newconst, newtype);
28419 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28420 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28421 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28422 }
28423
28424 return newdecl;
28425 }
28426
28427 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28428 template parameter. */
28429
28430 static tree
rewrite_tparm_list(tree oldelt,unsigned index,unsigned level,tree targs,unsigned targs_index,tsubst_flags_t complain)28431 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28432 tree targs, unsigned targs_index, tsubst_flags_t complain)
28433 {
28434 tree olddecl = TREE_VALUE (oldelt);
28435 tree newdecl = rewrite_template_parm (olddecl, index, level,
28436 targs, complain);
28437 if (newdecl == error_mark_node)
28438 return error_mark_node;
28439 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28440 targs, complain, NULL_TREE);
28441 tree list = build_tree_list (newdef, newdecl);
28442 TEMPLATE_PARM_CONSTRAINTS (list)
28443 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28444 targs, complain, NULL_TREE);
28445 int depth = TMPL_ARGS_DEPTH (targs);
28446 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28447 return list;
28448 }
28449
28450 /* Returns a C++17 class deduction guide template based on the constructor
28451 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28452 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28453 aggregate initialization guide. */
28454
28455 static tree
build_deduction_guide(tree type,tree ctor,tree outer_args,tsubst_flags_t complain)28456 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28457 {
28458 tree tparms, targs, fparms, fargs, ci;
28459 bool memtmpl = false;
28460 bool explicit_p;
28461 location_t loc;
28462 tree fn_tmpl = NULL_TREE;
28463
28464 if (outer_args)
28465 {
28466 ++processing_template_decl;
28467 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28468 --processing_template_decl;
28469 }
28470
28471 if (!DECL_DECLARES_FUNCTION_P (ctor))
28472 {
28473 if (TYPE_P (ctor))
28474 {
28475 bool copy_p = TYPE_REF_P (ctor);
28476 if (copy_p)
28477 fparms = tree_cons (NULL_TREE, type, void_list_node);
28478 else
28479 fparms = void_list_node;
28480 }
28481 else if (TREE_CODE (ctor) == TREE_LIST)
28482 fparms = ctor;
28483 else
28484 gcc_unreachable ();
28485
28486 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28487 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28488 targs = CLASSTYPE_TI_ARGS (type);
28489 ci = NULL_TREE;
28490 fargs = NULL_TREE;
28491 loc = DECL_SOURCE_LOCATION (ctmpl);
28492 explicit_p = false;
28493 }
28494 else
28495 {
28496 ++processing_template_decl;
28497 bool ok = true;
28498
28499 fn_tmpl
28500 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28501 : DECL_TI_TEMPLATE (ctor));
28502 if (outer_args)
28503 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28504 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28505
28506 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28507 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28508 fully specialized args for the enclosing class. Strip those off, as
28509 the deduction guide won't have those template parameters. */
28510 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28511 TMPL_PARMS_DEPTH (tparms));
28512 /* Discard the 'this' parameter. */
28513 fparms = FUNCTION_ARG_CHAIN (ctor);
28514 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28515 ci = get_constraints (ctor);
28516 loc = DECL_SOURCE_LOCATION (ctor);
28517 explicit_p = DECL_NONCONVERTING_P (ctor);
28518
28519 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28520 {
28521 memtmpl = true;
28522
28523 /* For a member template constructor, we need to flatten the two
28524 template parameter lists into one, and then adjust the function
28525 signature accordingly. This gets...complicated. */
28526 tree save_parms = current_template_parms;
28527
28528 /* For a member template we should have two levels of parms/args, one
28529 for the class and one for the constructor. We stripped
28530 specialized args for further enclosing classes above. */
28531 const int depth = 2;
28532 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28533
28534 /* Template args for translating references to the two-level template
28535 parameters into references to the one-level template parameters we
28536 are creating. */
28537 tree tsubst_args = copy_node (targs);
28538 TMPL_ARGS_LEVEL (tsubst_args, depth)
28539 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28540
28541 /* Template parms for the constructor template. */
28542 tree ftparms = TREE_VALUE (tparms);
28543 unsigned flen = TREE_VEC_LENGTH (ftparms);
28544 /* Template parms for the class template. */
28545 tparms = TREE_CHAIN (tparms);
28546 tree ctparms = TREE_VALUE (tparms);
28547 unsigned clen = TREE_VEC_LENGTH (ctparms);
28548 /* Template parms for the deduction guide start as a copy of the
28549 template parms for the class. We set current_template_parms for
28550 lookup_template_class_1. */
28551 current_template_parms = tparms = copy_node (tparms);
28552 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28553 for (unsigned i = 0; i < clen; ++i)
28554 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28555
28556 /* Now we need to rewrite the constructor parms to append them to the
28557 class parms. */
28558 for (unsigned i = 0; i < flen; ++i)
28559 {
28560 unsigned index = i + clen;
28561 unsigned level = 1;
28562 tree oldelt = TREE_VEC_ELT (ftparms, i);
28563 tree newelt
28564 = rewrite_tparm_list (oldelt, index, level,
28565 tsubst_args, i, complain);
28566 if (newelt == error_mark_node)
28567 ok = false;
28568 TREE_VEC_ELT (new_vec, index) = newelt;
28569 }
28570
28571 /* Now we have a final set of template parms to substitute into the
28572 function signature. */
28573 targs = template_parms_to_args (tparms);
28574 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28575 complain, ctor);
28576 if (fparms == error_mark_node)
28577 ok = false;
28578 if (ci)
28579 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28580
28581 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28582 cp_unevaluated_operand. */
28583 cp_evaluated ev;
28584 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28585 current_template_parms = save_parms;
28586 }
28587
28588 --processing_template_decl;
28589 if (!ok)
28590 return error_mark_node;
28591 }
28592
28593 if (!memtmpl)
28594 {
28595 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28596 tparms = copy_node (tparms);
28597 INNERMOST_TEMPLATE_PARMS (tparms)
28598 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28599 }
28600
28601 tree fntype = build_function_type (type, fparms);
28602 tree ded_fn = build_lang_decl_loc (loc,
28603 FUNCTION_DECL,
28604 dguide_name (type), fntype);
28605 DECL_ARGUMENTS (ded_fn) = fargs;
28606 DECL_ARTIFICIAL (ded_fn) = true;
28607 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28608 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28609 DECL_ARTIFICIAL (ded_tmpl) = true;
28610 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
28611 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
28612 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28613 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28614 if (DECL_P (ctor))
28615 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28616 if (ci)
28617 set_constraints (ded_tmpl, ci);
28618
28619 return ded_tmpl;
28620 }
28621
28622 /* Add to LIST the member types for the reshaped initializer CTOR. */
28623
28624 static tree
collect_ctor_idx_types(tree ctor,tree list)28625 collect_ctor_idx_types (tree ctor, tree list)
28626 {
28627 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28628 tree idx, val; unsigned i;
28629 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28630 {
28631 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28632 && CONSTRUCTOR_NELTS (val))
28633 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
28634 if (TREE_CODE (subidx) == FIELD_DECL)
28635 {
28636 list = collect_ctor_idx_types (val, list);
28637 continue;
28638 }
28639 tree ftype = finish_decltype_type (idx, true, tf_none);
28640 list = tree_cons (NULL_TREE, ftype, list);
28641 }
28642
28643 return list;
28644 }
28645
28646 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28647
28648 static bool
is_spec_or_derived(tree etype,tree tmpl)28649 is_spec_or_derived (tree etype, tree tmpl)
28650 {
28651 if (!etype || !CLASS_TYPE_P (etype))
28652 return false;
28653
28654 tree type = TREE_TYPE (tmpl);
28655 tree tparms = (INNERMOST_TEMPLATE_PARMS
28656 (DECL_TEMPLATE_PARMS (tmpl)));
28657 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28658 int err = unify (tparms, targs, type, etype,
28659 UNIFY_ALLOW_DERIVED, /*explain*/false);
28660 ggc_free (targs);
28661 return !err;
28662 }
28663
28664 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28665 INIT. */
28666
28667 static tree
maybe_aggr_guide(tree tmpl,tree init,vec<tree,va_gc> * args)28668 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28669 {
28670 if (cxx_dialect < cxx2a)
28671 return NULL_TREE;
28672
28673 if (init == NULL_TREE)
28674 return NULL_TREE;
28675
28676 tree type = TREE_TYPE (tmpl);
28677 if (!CP_AGGREGATE_TYPE_P (type))
28678 return NULL_TREE;
28679
28680 /* No aggregate candidate for copy-initialization. */
28681 if (args->length() == 1)
28682 {
28683 tree val = (*args)[0];
28684 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28685 return NULL_TREE;
28686 }
28687
28688 /* If we encounter a problem, we just won't add the candidate. */
28689 tsubst_flags_t complain = tf_none;
28690
28691 tree parms = NULL_TREE;
28692 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28693 {
28694 init = reshape_init (type, init, complain);
28695 if (init == error_mark_node)
28696 return NULL_TREE;
28697 parms = collect_ctor_idx_types (init, parms);
28698 }
28699 else if (TREE_CODE (init) == TREE_LIST)
28700 {
28701 int len = list_length (init);
28702 for (tree field = TYPE_FIELDS (type);
28703 len;
28704 --len, field = DECL_CHAIN (field))
28705 {
28706 field = next_initializable_field (field);
28707 if (!field)
28708 return NULL_TREE;
28709 tree ftype = finish_decltype_type (field, true, complain);
28710 parms = tree_cons (NULL_TREE, ftype, parms);
28711 }
28712 }
28713 else
28714 /* Aggregate initialization doesn't apply to an initializer expression. */
28715 return NULL_TREE;
28716
28717 if (parms)
28718 {
28719 tree last = parms;
28720 parms = nreverse (parms);
28721 TREE_CHAIN (last) = void_list_node;
28722 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28723 return guide;
28724 }
28725
28726 return NULL_TREE;
28727 }
28728
28729 /* UGUIDES are the deduction guides for the underlying template of alias
28730 template TMPL; adjust them to be deduction guides for TMPL. */
28731
28732 static tree
alias_ctad_tweaks(tree tmpl,tree uguides)28733 alias_ctad_tweaks (tree tmpl, tree uguides)
28734 {
28735 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28736 class type (9.2.8.2) where the template-name names an alias template A,
28737 the defining-type-id of A must be of the form
28738
28739 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28740
28741 as specified in 9.2.8.2. The guides of A are the set of functions or
28742 function templates formed as follows. For each function or function
28743 template f in the guides of the template named by the simple-template-id
28744 of the defining-type-id, the template arguments of the return type of f
28745 are deduced from the defining-type-id of A according to the process in
28746 13.10.2.5 with the exception that deduction does not fail if not all
28747 template arguments are deduced. Let g denote the result of substituting
28748 these deductions into f. If substitution succeeds, form a function or
28749 function template f' with the following properties and add it to the set
28750 of guides of A:
28751
28752 * The function type of f' is the function type of g.
28753
28754 * If f is a function template, f' is a function template whose template
28755 parameter list consists of all the template parameters of A (including
28756 their default template arguments) that appear in the above deductions or
28757 (recursively) in their default template arguments, followed by the
28758 template parameters of f that were not deduced (including their default
28759 template arguments), otherwise f' is not a function template.
28760
28761 * The associated constraints (13.5.2) are the conjunction of the
28762 associated constraints of g and a constraint that is satisfied if and only
28763 if the arguments of A are deducible (see below) from the return type.
28764
28765 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28766 be so as well.
28767
28768 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28769 considered to be so as well.
28770
28771 * The explicit-specifier of f' is the explicit-specifier of g (if
28772 any). */
28773
28774 /* This implementation differs from the above in two significant ways:
28775
28776 1) We include all template parameters of A, not just some.
28777 2) The added constraint is same_type instead of deducible.
28778
28779 I believe that while it's probably possible to construct a testcase that
28780 behaves differently with this simplification, it should have the same
28781 effect for real uses. Including all template parameters means that we
28782 deduce all parameters of A when resolving the call, so when we're in the
28783 constraint we don't need to deduce them again, we can just check whether
28784 the deduction produced the desired result. */
28785
28786 tsubst_flags_t complain = tf_warning_or_error;
28787 tree atype = TREE_TYPE (tmpl);
28788 tree aguides = NULL_TREE;
28789 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28790 unsigned natparms = TREE_VEC_LENGTH (atparms);
28791 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28792 for (ovl_iterator iter (uguides); iter; ++iter)
28793 {
28794 tree f = *iter;
28795 tree in_decl = f;
28796 location_t loc = DECL_SOURCE_LOCATION (f);
28797 tree ret = TREE_TYPE (TREE_TYPE (f));
28798 tree fprime = f;
28799 if (TREE_CODE (f) == TEMPLATE_DECL)
28800 {
28801 processing_template_decl_sentinel ptds (/*reset*/false);
28802 ++processing_template_decl;
28803
28804 /* Deduce template arguments for f from the type-id of A. */
28805 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28806 unsigned len = TREE_VEC_LENGTH (ftparms);
28807 tree targs = make_tree_vec (len);
28808 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28809 if (err)
28810 continue;
28811
28812 /* The number of parms for f' is the number of parms for A plus
28813 non-deduced parms of f. */
28814 unsigned ndlen = 0;
28815 unsigned j;
28816 for (unsigned i = 0; i < len; ++i)
28817 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28818 ++ndlen;
28819 tree gtparms = make_tree_vec (natparms + ndlen);
28820
28821 /* First copy over the parms of A. */
28822 for (j = 0; j < natparms; ++j)
28823 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28824 /* Now rewrite the non-deduced parms of f. */
28825 for (unsigned i = 0; ndlen && i < len; ++i)
28826 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28827 {
28828 --ndlen;
28829 unsigned index = j++;
28830 unsigned level = 1;
28831 tree oldlist = TREE_VEC_ELT (ftparms, i);
28832 tree list = rewrite_tparm_list (oldlist, index, level,
28833 targs, i, complain);
28834 TREE_VEC_ELT (gtparms, index) = list;
28835 }
28836 gtparms = build_tree_list (size_one_node, gtparms);
28837
28838 /* Substitute the deduced arguments plus the rewritten template
28839 parameters into f to get g. This covers the type, copyness,
28840 guideness, and explicit-specifier. */
28841 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28842 if (g == error_mark_node)
28843 continue;
28844 DECL_USE_TEMPLATE (g) = 0;
28845 fprime = build_template_decl (g, gtparms, false);
28846 DECL_TEMPLATE_RESULT (fprime) = g;
28847 TREE_TYPE (fprime) = TREE_TYPE (g);
28848 tree gtargs = template_parms_to_args (gtparms);
28849 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28850 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28851
28852 /* Substitute the associated constraints. */
28853 tree ci = get_constraints (f);
28854 if (ci)
28855 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28856 if (ci == error_mark_node)
28857 continue;
28858
28859 /* Add a constraint that the return type matches the instantiation of
28860 A with the same template arguments. */
28861 ret = TREE_TYPE (TREE_TYPE (fprime));
28862 if (!same_type_p (atype, ret)
28863 /* FIXME this should mean they don't compare as equivalent. */
28864 || dependent_alias_template_spec_p (atype, nt_opaque))
28865 {
28866 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28867 ci = append_constraint (ci, same);
28868 }
28869
28870 if (ci)
28871 {
28872 remove_constraints (fprime);
28873 set_constraints (fprime, ci);
28874 }
28875 }
28876 else
28877 {
28878 /* For a non-template deduction guide, if the arguments of A aren't
28879 deducible from the return type, don't add the candidate. */
28880 tree targs = make_tree_vec (natparms);
28881 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28882 for (unsigned i = 0; !err && i < natparms; ++i)
28883 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28884 err = true;
28885 if (err)
28886 continue;
28887 }
28888
28889 aguides = lookup_add (fprime, aguides);
28890 }
28891
28892 return aguides;
28893 }
28894
28895 /* Return artificial deduction guides built from the constructors of class
28896 template TMPL. */
28897
28898 static tree
ctor_deduction_guides_for(tree tmpl,tsubst_flags_t complain)28899 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28900 {
28901 tree type = TREE_TYPE (tmpl);
28902 tree outer_args = NULL_TREE;
28903 if (DECL_CLASS_SCOPE_P (tmpl)
28904 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28905 {
28906 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28907 type = TREE_TYPE (most_general_template (tmpl));
28908 }
28909
28910 tree cands = NULL_TREE;
28911
28912 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28913 {
28914 /* Skip inherited constructors. */
28915 if (iter.using_p ())
28916 continue;
28917
28918 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28919 cands = lookup_add (guide, cands);
28920 }
28921
28922 /* Add implicit default constructor deduction guide. */
28923 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28924 {
28925 tree guide = build_deduction_guide (type, type, outer_args,
28926 complain);
28927 cands = lookup_add (guide, cands);
28928 }
28929
28930 /* Add copy guide. */
28931 {
28932 tree gtype = build_reference_type (type);
28933 tree guide = build_deduction_guide (type, gtype, outer_args,
28934 complain);
28935 cands = lookup_add (guide, cands);
28936 }
28937
28938 return cands;
28939 }
28940
28941 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28942
28943 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28944 aggregate candidate is added separately because it depends on the
28945 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
28946 guide. */
28947
28948 static tree
deduction_guides_for(tree tmpl,bool & any_dguides_p,tsubst_flags_t complain)28949 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
28950 {
28951 tree guides = NULL_TREE;
28952 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28953 {
28954 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28955 tree tinfo = get_template_info (under);
28956 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
28957 complain);
28958 }
28959 else
28960 {
28961 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28962 dguide_name (tmpl),
28963 /*type*/false, /*complain*/false,
28964 /*hidden*/false);
28965 if (guides == error_mark_node)
28966 guides = NULL_TREE;
28967 else
28968 any_dguides_p = true;
28969 }
28970
28971 /* Cache the deduction guides for a template. We also remember the result of
28972 lookup, and rebuild everything if it changes; should be very rare. */
28973 tree_pair_p cache = NULL;
28974 if (tree_pair_p &r
28975 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28976 {
28977 cache = r;
28978 if (cache->purpose == guides)
28979 return cache->value;
28980 }
28981 else
28982 {
28983 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28984 cache->purpose = guides;
28985 }
28986
28987 tree cands = NULL_TREE;
28988 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28989 cands = alias_ctad_tweaks (tmpl, guides);
28990 else
28991 {
28992 cands = ctor_deduction_guides_for (tmpl, complain);
28993 for (ovl_iterator it (guides); it; ++it)
28994 cands = lookup_add (*it, cands);
28995 }
28996
28997 cache->value = cands;
28998 return cands;
28999 }
29000
29001 /* Return whether TMPL is a (class template argument-) deducible template. */
29002
29003 bool
ctad_template_p(tree tmpl)29004 ctad_template_p (tree tmpl)
29005 {
29006 /* A deducible template is either a class template or is an alias template
29007 whose defining-type-id is of the form
29008
29009 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29010
29011 where the nested-name-specifier (if any) is non-dependent and the
29012 template-name of the simple-template-id names a deducible template. */
29013
29014 if (DECL_CLASS_TEMPLATE_P (tmpl)
29015 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29016 return true;
29017 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
29018 return false;
29019 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29020 if (tree tinfo = get_template_info (orig))
29021 return ctad_template_p (TI_TEMPLATE (tinfo));
29022 return false;
29023 }
29024
29025 /* Deduce template arguments for the class template placeholder PTYPE for
29026 template TMPL based on the initializer INIT, and return the resulting
29027 type. */
29028
29029 static tree
do_class_deduction(tree ptype,tree tmpl,tree init,int flags,tsubst_flags_t complain)29030 do_class_deduction (tree ptype, tree tmpl, tree init,
29031 int flags, tsubst_flags_t complain)
29032 {
29033 /* We should have handled this in the caller. */
29034 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29035 return ptype;
29036
29037 /* Initializing one placeholder from another. */
29038 if (init && TREE_CODE (init) == TEMPLATE_PARM_INDEX
29039 && is_auto (TREE_TYPE (init))
29040 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
29041 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
29042
29043 /* Look through alias templates that just rename another template. */
29044 tmpl = get_underlying_template (tmpl);
29045 if (!ctad_template_p (tmpl))
29046 {
29047 if (complain & tf_error)
29048 error ("non-deducible template %qT used without template arguments", tmpl);
29049 return error_mark_node;
29050 }
29051 else if (cxx_dialect < cxx2a && DECL_ALIAS_TEMPLATE_P (tmpl))
29052 {
29053 /* This doesn't affect conforming C++17 code, so just pedwarn. */
29054 if (complain & tf_warning_or_error)
29055 pedwarn (input_location, 0, "alias template deduction only available "
29056 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
29057 }
29058
29059 tree type = TREE_TYPE (tmpl);
29060
29061 bool try_list_ctor = false;
29062
29063 releasing_vec rv_args = NULL;
29064 vec<tree,va_gc> *&args = *&rv_args;
29065 if (init == NULL_TREE)
29066 args = make_tree_vector ();
29067 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
29068 {
29069 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
29070 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
29071 {
29072 /* As an exception, the first phase in 16.3.1.7 (considering the
29073 initializer list as a single argument) is omitted if the
29074 initializer list consists of a single expression of type cv U,
29075 where U is a specialization of C or a class derived from a
29076 specialization of C. */
29077 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
29078 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
29079 try_list_ctor = false;
29080 }
29081 if (try_list_ctor || is_std_init_list (type))
29082 args = make_tree_vector_single (init);
29083 else
29084 args = make_tree_vector_from_ctor (init);
29085 }
29086 else if (TREE_CODE (init) == TREE_LIST)
29087 args = make_tree_vector_from_list (init);
29088 else
29089 args = make_tree_vector_single (init);
29090
29091 /* Do this now to avoid problems with erroneous args later on. */
29092 args = resolve_args (args, complain);
29093 if (args == NULL)
29094 return error_mark_node;
29095
29096 bool any_dguides_p = false;
29097 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29098 if (cands == error_mark_node)
29099 return error_mark_node;
29100
29101 /* Prune explicit deduction guides in copy-initialization context. */
29102 bool elided = false;
29103 if (flags & LOOKUP_ONLYCONVERTING)
29104 {
29105 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29106 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29107 elided = true;
29108
29109 if (elided)
29110 {
29111 /* Found a nonconverting guide, prune the candidates. */
29112 tree pruned = NULL_TREE;
29113 for (lkp_iterator iter (cands); iter; ++iter)
29114 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29115 pruned = lookup_add (*iter, pruned);
29116
29117 cands = pruned;
29118 }
29119 }
29120
29121 if (!any_dguides_p)
29122 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29123 cands = lookup_add (guide, cands);
29124
29125 tree call = error_mark_node;
29126
29127 /* If this is list-initialization and the class has a list constructor, first
29128 try deducing from the list as a single argument, as [over.match.list]. */
29129 tree list_cands = NULL_TREE;
29130 if (try_list_ctor && cands)
29131 for (lkp_iterator iter (cands); iter; ++iter)
29132 {
29133 tree dg = *iter;
29134 if (is_list_ctor (dg))
29135 list_cands = lookup_add (dg, list_cands);
29136 }
29137 if (list_cands)
29138 {
29139 ++cp_unevaluated_operand;
29140 call = build_new_function_call (list_cands, &args, tf_decltype);
29141 --cp_unevaluated_operand;
29142
29143 if (call == error_mark_node)
29144 {
29145 /* That didn't work, now try treating the list as a sequence of
29146 arguments. */
29147 release_tree_vector (args);
29148 args = make_tree_vector_from_ctor (init);
29149 }
29150 }
29151
29152 if (elided && !cands)
29153 {
29154 error ("cannot deduce template arguments for copy-initialization"
29155 " of %qT, as it has no non-explicit deduction guides or "
29156 "user-declared constructors", type);
29157 return error_mark_node;
29158 }
29159 else if (!cands && call == error_mark_node)
29160 {
29161 error ("cannot deduce template arguments of %qT, as it has no viable "
29162 "deduction guides", type);
29163 return error_mark_node;
29164 }
29165
29166 if (call == error_mark_node)
29167 {
29168 ++cp_unevaluated_operand;
29169 call = build_new_function_call (cands, &args, tf_decltype);
29170 --cp_unevaluated_operand;
29171 }
29172
29173 if (call == error_mark_node
29174 && (complain & tf_warning_or_error))
29175 {
29176 error ("class template argument deduction failed:");
29177
29178 ++cp_unevaluated_operand;
29179 call = build_new_function_call (cands, &args, complain | tf_decltype);
29180 --cp_unevaluated_operand;
29181
29182 if (elided)
29183 inform (input_location, "explicit deduction guides not considered "
29184 "for copy-initialization");
29185 }
29186
29187 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29188 }
29189
29190 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29191 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29192 The CONTEXT determines the context in which auto deduction is performed
29193 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29194 OUTER_TARGS are used during template argument deduction
29195 (context == adc_unify) to properly substitute the result, and is ignored
29196 in other contexts.
29197
29198 For partial-concept-ids, extra args may be appended to the list of deduced
29199 template arguments prior to determining constraint satisfaction. */
29200
29201 tree
do_auto_deduction(tree type,tree init,tree auto_node,tsubst_flags_t complain,auto_deduction_context context,tree outer_targs,int flags)29202 do_auto_deduction (tree type, tree init, tree auto_node,
29203 tsubst_flags_t complain, auto_deduction_context context,
29204 tree outer_targs, int flags)
29205 {
29206 tree targs;
29207
29208 if (init == error_mark_node)
29209 return error_mark_node;
29210
29211 if (init && type_dependent_expression_p (init)
29212 && context != adc_unify)
29213 /* Defining a subset of type-dependent expressions that we can deduce
29214 from ahead of time isn't worth the trouble. */
29215 return type;
29216
29217 /* Similarly, we can't deduce from another undeduced decl. */
29218 if (init && undeduced_auto_decl (init))
29219 return type;
29220
29221 /* We may be doing a partial substitution, but we still want to replace
29222 auto_node. */
29223 complain &= ~tf_partial;
29224
29225 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29226 /* C++17 class template argument deduction. */
29227 return do_class_deduction (type, tmpl, init, flags, complain);
29228
29229 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29230 /* Nothing we can do with this, even in deduction context. */
29231 return type;
29232
29233 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29234 with either a new invented type template parameter U or, if the
29235 initializer is a braced-init-list (8.5.4), with
29236 std::initializer_list<U>. */
29237 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29238 {
29239 if (!DIRECT_LIST_INIT_P (init))
29240 type = listify_autos (type, auto_node);
29241 else if (CONSTRUCTOR_NELTS (init) == 1)
29242 init = CONSTRUCTOR_ELT (init, 0)->value;
29243 else
29244 {
29245 if (complain & tf_warning_or_error)
29246 {
29247 if (permerror (input_location, "direct-list-initialization of "
29248 "%<auto%> requires exactly one element"))
29249 inform (input_location,
29250 "for deduction to %<std::initializer_list%>, use copy-"
29251 "list-initialization (i.e. add %<=%> before the %<{%>)");
29252 }
29253 type = listify_autos (type, auto_node);
29254 }
29255 }
29256
29257 if (type == error_mark_node)
29258 return error_mark_node;
29259
29260 init = resolve_nondeduced_context (init, complain);
29261
29262 if (context == adc_decomp_type
29263 && auto_node == type
29264 && init != error_mark_node
29265 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29266 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29267 and initializer has array type, deduce cv-qualified array type. */
29268 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29269 complain);
29270 else if (AUTO_IS_DECLTYPE (auto_node))
29271 {
29272 tree stripped_init = tree_strip_any_location_wrapper (init);
29273 bool id = (DECL_P (stripped_init)
29274 || ((TREE_CODE (init) == COMPONENT_REF
29275 || TREE_CODE (init) == SCOPE_REF)
29276 && !REF_PARENTHESIZED_P (init)));
29277 targs = make_tree_vec (1);
29278 TREE_VEC_ELT (targs, 0)
29279 = finish_decltype_type (init, id, tf_warning_or_error);
29280 if (type != auto_node)
29281 {
29282 if (complain & tf_error)
29283 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29284 return error_mark_node;
29285 }
29286 }
29287 else
29288 {
29289 if (error_operand_p (init))
29290 return error_mark_node;
29291
29292 tree parms = build_tree_list (NULL_TREE, type);
29293 tree tparms;
29294
29295 if (flag_concepts)
29296 tparms = extract_autos (type);
29297 else
29298 {
29299 tparms = make_tree_vec (1);
29300 TREE_VEC_ELT (tparms, 0)
29301 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29302 }
29303
29304 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29305 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29306 DEDUCE_CALL,
29307 NULL, /*explain_p=*/false);
29308 if (val > 0)
29309 {
29310 if (processing_template_decl)
29311 /* Try again at instantiation time. */
29312 return type;
29313 if (type && type != error_mark_node
29314 && (complain & tf_error))
29315 /* If type is error_mark_node a diagnostic must have been
29316 emitted by now. Also, having a mention to '<type error>'
29317 in the diagnostic is not really useful to the user. */
29318 {
29319 if (cfun
29320 && FNDECL_USED_AUTO (current_function_decl)
29321 && (auto_node
29322 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29323 && LAMBDA_FUNCTION_P (current_function_decl))
29324 error ("unable to deduce lambda return type from %qE", init);
29325 else
29326 error ("unable to deduce %qT from %qE", type, init);
29327 type_unification_real (tparms, targs, parms, &init, 1, 0,
29328 DEDUCE_CALL,
29329 NULL, /*explain_p=*/true);
29330 }
29331 return error_mark_node;
29332 }
29333 }
29334
29335 /* Check any placeholder constraints against the deduced type. */
29336 if (flag_concepts && !processing_template_decl)
29337 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29338 {
29339 /* Use the deduced type to check the associated constraints. If we
29340 have a partial-concept-id, rebuild the argument list so that
29341 we check using the extra arguments. */
29342 check = unpack_concept_check (check);
29343 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29344 tree cdecl = TREE_OPERAND (check, 0);
29345 if (OVL_P (cdecl))
29346 cdecl = OVL_FIRST (cdecl);
29347 tree cargs = TREE_OPERAND (check, 1);
29348 if (TREE_VEC_LENGTH (cargs) > 1)
29349 {
29350 cargs = copy_node (cargs);
29351 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29352 }
29353 else
29354 cargs = targs;
29355
29356 /* Rebuild the check using the deduced arguments. */
29357 check = build_concept_check (cdecl, cargs, tf_none);
29358
29359 if (!constraints_satisfied_p (check))
29360 {
29361 if (complain & tf_warning_or_error)
29362 {
29363 auto_diagnostic_group d;
29364 switch (context)
29365 {
29366 case adc_unspecified:
29367 case adc_unify:
29368 error("placeholder constraints not satisfied");
29369 break;
29370 case adc_variable_type:
29371 case adc_decomp_type:
29372 error ("deduced initializer does not satisfy "
29373 "placeholder constraints");
29374 break;
29375 case adc_return_type:
29376 error ("deduced return type does not satisfy "
29377 "placeholder constraints");
29378 break;
29379 case adc_requirement:
29380 error ("deduced expression type does not satisfy "
29381 "placeholder constraints");
29382 break;
29383 }
29384 diagnose_constraints (input_location, check, targs);
29385 }
29386 return error_mark_node;
29387 }
29388 }
29389
29390 if (processing_template_decl && context != adc_unify)
29391 outer_targs = current_template_args ();
29392 targs = add_to_template_args (outer_targs, targs);
29393 return tsubst (type, targs, complain, NULL_TREE);
29394 }
29395
29396 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29397 result. */
29398
29399 tree
splice_late_return_type(tree type,tree late_return_type)29400 splice_late_return_type (tree type, tree late_return_type)
29401 {
29402 if (late_return_type)
29403 {
29404 gcc_assert (is_auto (type) || seen_error ());
29405 return late_return_type;
29406 }
29407
29408 if (tree auto_node = find_type_usage (type, is_auto))
29409 if (TEMPLATE_TYPE_LEVEL (auto_node) <= processing_template_decl)
29410 {
29411 /* In an abbreviated function template we didn't know we were dealing
29412 with a function template when we saw the auto return type, so rebuild
29413 the return type using an auto with the correct level. */
29414 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
29415 tree auto_vec = make_tree_vec (1);
29416 TREE_VEC_ELT (auto_vec, 0) = new_auto;
29417 tree targs = add_outermost_template_args (current_template_args (),
29418 auto_vec);
29419 /* FIXME: We should also rebuild the constraint to refer to the new
29420 auto. */
29421 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29422 = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node);
29423 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29424 return tsubst (type, targs, tf_none, NULL_TREE);
29425 }
29426 return type;
29427 }
29428
29429 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29430 'decltype(auto)' or a deduced class template. */
29431
29432 bool
is_auto(const_tree type)29433 is_auto (const_tree type)
29434 {
29435 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29436 && (TYPE_IDENTIFIER (type) == auto_identifier
29437 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29438 return true;
29439 else
29440 return false;
29441 }
29442
29443 /* for_each_template_parm callback for type_uses_auto. */
29444
29445 int
is_auto_r(tree tp,void *)29446 is_auto_r (tree tp, void */*data*/)
29447 {
29448 return is_auto (tp);
29449 }
29450
29451 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29452 a use of `auto'. Returns NULL_TREE otherwise. */
29453
29454 tree
type_uses_auto(tree type)29455 type_uses_auto (tree type)
29456 {
29457 if (type == NULL_TREE)
29458 return NULL_TREE;
29459 else if (flag_concepts)
29460 {
29461 /* The Concepts TS allows multiple autos in one type-specifier; just
29462 return the first one we find, do_auto_deduction will collect all of
29463 them. */
29464 if (uses_template_parms (type))
29465 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29466 /*visited*/NULL, /*nondeduced*/false);
29467 else
29468 return NULL_TREE;
29469 }
29470 else
29471 return find_type_usage (type, is_auto);
29472 }
29473
29474 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29475 concepts are enabled, auto is acceptable in template arguments, but
29476 only when TEMPL identifies a template class. Return TRUE if any
29477 such errors were reported. */
29478
29479 bool
check_auto_in_tmpl_args(tree tmpl,tree args)29480 check_auto_in_tmpl_args (tree tmpl, tree args)
29481 {
29482 /* If there were previous errors, nevermind. */
29483 if (!args || TREE_CODE (args) != TREE_VEC)
29484 return false;
29485
29486 /* If TMPL is an identifier, we're parsing and we can't tell yet
29487 whether TMPL is supposed to be a type, a function or a variable.
29488 We'll only be able to tell during template substitution, so we
29489 expect to be called again then. If concepts are enabled and we
29490 know we have a type, we're ok. */
29491 if (flag_concepts
29492 && (identifier_p (tmpl)
29493 || (DECL_P (tmpl)
29494 && (DECL_TYPE_TEMPLATE_P (tmpl)
29495 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29496 return false;
29497
29498 /* Quickly search for any occurrences of auto; usually there won't
29499 be any, and then we'll avoid allocating the vector. */
29500 if (!type_uses_auto (args))
29501 return false;
29502
29503 bool errors = false;
29504
29505 tree vec = extract_autos (args);
29506 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29507 {
29508 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29509 error_at (DECL_SOURCE_LOCATION (xauto),
29510 "invalid use of %qT in template argument", xauto);
29511 errors = true;
29512 }
29513
29514 return errors;
29515 }
29516
29517 /* For a given template T, return the vector of typedefs referenced
29518 in T for which access check is needed at T instantiation time.
29519 T is either a FUNCTION_DECL or a RECORD_TYPE.
29520 Those typedefs were added to T by the function
29521 append_type_to_template_for_access_check. */
29522
29523 vec<qualified_typedef_usage_t, va_gc> *
get_types_needing_access_check(tree t)29524 get_types_needing_access_check (tree t)
29525 {
29526 tree ti;
29527 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
29528
29529 if (!t || t == error_mark_node)
29530 return NULL;
29531
29532 if (!(ti = get_template_info (t)))
29533 return NULL;
29534
29535 if (CLASS_TYPE_P (t)
29536 || TREE_CODE (t) == FUNCTION_DECL)
29537 {
29538 if (!TI_TEMPLATE (ti))
29539 return NULL;
29540
29541 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
29542 }
29543
29544 return result;
29545 }
29546
29547 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
29548 tied to T. That list of typedefs will be access checked at
29549 T instantiation time.
29550 T is either a FUNCTION_DECL or a RECORD_TYPE.
29551 TYPE_DECL is a TYPE_DECL node representing a typedef.
29552 SCOPE is the scope through which TYPE_DECL is accessed.
29553 LOCATION is the location of the usage point of TYPE_DECL.
29554
29555 This function is a subroutine of
29556 append_type_to_template_for_access_check. */
29557
29558 static void
append_type_to_template_for_access_check_1(tree t,tree type_decl,tree scope,location_t location)29559 append_type_to_template_for_access_check_1 (tree t,
29560 tree type_decl,
29561 tree scope,
29562 location_t location)
29563 {
29564 qualified_typedef_usage_t typedef_usage;
29565 tree ti;
29566
29567 if (!t || t == error_mark_node)
29568 return;
29569
29570 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
29571 || CLASS_TYPE_P (t))
29572 && type_decl
29573 && TREE_CODE (type_decl) == TYPE_DECL
29574 && scope);
29575
29576 if (!(ti = get_template_info (t)))
29577 return;
29578
29579 gcc_assert (TI_TEMPLATE (ti));
29580
29581 typedef_usage.typedef_decl = type_decl;
29582 typedef_usage.context = scope;
29583 typedef_usage.locus = location;
29584
29585 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
29586 }
29587
29588 /* Append TYPE_DECL to the template TEMPL.
29589 TEMPL is either a class type, a FUNCTION_DECL or a TEMPLATE_DECL.
29590 At TEMPL instanciation time, TYPE_DECL will be checked to see
29591 if it can be accessed through SCOPE.
29592 LOCATION is the location of the usage point of TYPE_DECL.
29593
29594 e.g. consider the following code snippet:
29595
29596 class C
29597 {
29598 typedef int myint;
29599 };
29600
29601 template<class U> struct S
29602 {
29603 C::myint mi; // <-- usage point of the typedef C::myint
29604 };
29605
29606 S<char> s;
29607
29608 At S<char> instantiation time, we need to check the access of C::myint
29609 In other words, we need to check the access of the myint typedef through
29610 the C scope. For that purpose, this function will add the myint typedef
29611 and the scope C through which its being accessed to a list of typedefs
29612 tied to the template S. That list will be walked at template instantiation
29613 time and access check performed on each typedefs it contains.
29614 Note that this particular code snippet should yield an error because
29615 myint is private to C. */
29616
29617 void
append_type_to_template_for_access_check(tree templ,tree type_decl,tree scope,location_t location)29618 append_type_to_template_for_access_check (tree templ,
29619 tree type_decl,
29620 tree scope,
29621 location_t location)
29622 {
29623 qualified_typedef_usage_t *iter;
29624 unsigned i;
29625
29626 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
29627
29628 /* Make sure we don't append the type to the template twice. */
29629 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
29630 if (iter->typedef_decl == type_decl && scope == iter->context)
29631 return;
29632
29633 append_type_to_template_for_access_check_1 (templ, type_decl,
29634 scope, location);
29635 }
29636
29637 /* Recursively walk over && expressions searching for EXPR. Return a reference
29638 to that expression. */
29639
find_template_requirement(tree * t,tree key)29640 static tree *find_template_requirement (tree *t, tree key)
29641 {
29642 if (*t == key)
29643 return t;
29644 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29645 {
29646 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29647 return p;
29648 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29649 return p;
29650 }
29651 return 0;
29652 }
29653
29654 /* Convert the generic type parameters in PARM that match the types given in the
29655 range [START_IDX, END_IDX) from the current_template_parms into generic type
29656 packs. */
29657
29658 tree
convert_generic_types_to_packs(tree parm,int start_idx,int end_idx)29659 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29660 {
29661 tree current = current_template_parms;
29662 int depth = TMPL_PARMS_DEPTH (current);
29663 current = INNERMOST_TEMPLATE_PARMS (current);
29664 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29665
29666 for (int i = 0; i < start_idx; ++i)
29667 TREE_VEC_ELT (replacement, i)
29668 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29669
29670 for (int i = start_idx; i < end_idx; ++i)
29671 {
29672 /* Create a distinct parameter pack type from the current parm and add it
29673 to the replacement args to tsubst below into the generic function
29674 parameter. */
29675 tree node = TREE_VEC_ELT (current, i);
29676 tree o = TREE_TYPE (TREE_VALUE (node));
29677 tree t = copy_type (o);
29678 TEMPLATE_TYPE_PARM_INDEX (t)
29679 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29680 t, 0, 0, tf_none);
29681 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29682 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29683 TYPE_MAIN_VARIANT (t) = t;
29684 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29685 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29686 TREE_VEC_ELT (replacement, i) = t;
29687
29688 /* Replace the current template parameter with new pack. */
29689 TREE_VALUE (node) = TREE_CHAIN (t);
29690
29691 /* Surgically adjust the associated constraint of adjusted parameter
29692 and it's corresponding contribution to the current template
29693 requirements. */
29694 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29695 {
29696 tree id = unpack_concept_check (constr);
29697 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29698 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29699 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29700
29701 /* If there was a constraint, we also need to replace that in
29702 the template requirements, which we've already built. */
29703 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29704 reqs = find_template_requirement (reqs, constr);
29705 *reqs = fold;
29706 }
29707 }
29708
29709 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29710 TREE_VEC_ELT (replacement, i)
29711 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29712
29713 /* If there are more levels then build up the replacement with the outer
29714 template parms. */
29715 if (depth > 1)
29716 replacement = add_to_template_args (template_parms_to_args
29717 (TREE_CHAIN (current_template_parms)),
29718 replacement);
29719
29720 return tsubst (parm, replacement, tf_none, NULL_TREE);
29721 }
29722
29723 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29724 0..N-1. */
29725
29726 void
declare_integer_pack(void)29727 declare_integer_pack (void)
29728 {
29729 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29730 build_function_type_list (integer_type_node,
29731 integer_type_node,
29732 NULL_TREE),
29733 NULL_TREE, ECF_CONST);
29734 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29735 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29736 CP_BUILT_IN_INTEGER_PACK);
29737 }
29738
29739 /* Set up the hash tables for template instantiations. */
29740
29741 void
init_template_processing(void)29742 init_template_processing (void)
29743 {
29744 /* FIXME: enable sanitization (PR87847) */
29745 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29746 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29747
29748 if (cxx_dialect >= cxx11)
29749 declare_integer_pack ();
29750 }
29751
29752 /* Print stats about the template hash tables for -fstats. */
29753
29754 void
print_template_statistics(void)29755 print_template_statistics (void)
29756 {
29757 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29758 "%f collisions\n", (long) decl_specializations->size (),
29759 (long) decl_specializations->elements (),
29760 decl_specializations->collisions ());
29761 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29762 "%f collisions\n", (long) type_specializations->size (),
29763 (long) type_specializations->elements (),
29764 type_specializations->collisions ());
29765 }
29766
29767 #if CHECKING_P
29768
29769 namespace selftest {
29770
29771 /* Verify that build_non_dependent_expr () works, for various expressions,
29772 and that location wrappers don't affect the results. */
29773
29774 static void
test_build_non_dependent_expr()29775 test_build_non_dependent_expr ()
29776 {
29777 location_t loc = BUILTINS_LOCATION;
29778
29779 /* Verify constants, without and with location wrappers. */
29780 tree int_cst = build_int_cst (integer_type_node, 42);
29781 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29782
29783 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29784 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29785 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29786
29787 tree string_lit = build_string (4, "foo");
29788 TREE_TYPE (string_lit) = char_array_type_node;
29789 string_lit = fix_string_type (string_lit);
29790 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29791
29792 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29793 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29794 ASSERT_EQ (wrapped_string_lit,
29795 build_non_dependent_expr (wrapped_string_lit));
29796 }
29797
29798 /* Verify that type_dependent_expression_p () works correctly, even
29799 in the presence of location wrapper nodes. */
29800
29801 static void
test_type_dependent_expression_p()29802 test_type_dependent_expression_p ()
29803 {
29804 location_t loc = BUILTINS_LOCATION;
29805
29806 tree name = get_identifier ("foo");
29807
29808 /* If no templates are involved, nothing is type-dependent. */
29809 gcc_assert (!processing_template_decl);
29810 ASSERT_FALSE (type_dependent_expression_p (name));
29811
29812 ++processing_template_decl;
29813
29814 /* Within a template, an unresolved name is always type-dependent. */
29815 ASSERT_TRUE (type_dependent_expression_p (name));
29816
29817 /* Ensure it copes with NULL_TREE and errors. */
29818 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29819 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29820
29821 /* A USING_DECL in a template should be type-dependent, even if wrapped
29822 with a location wrapper (PR c++/83799). */
29823 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29824 TREE_TYPE (using_decl) = integer_type_node;
29825 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29826 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29827 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29828 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29829
29830 --processing_template_decl;
29831 }
29832
29833 /* Run all of the selftests within this file. */
29834
29835 void
cp_pt_c_tests()29836 cp_pt_c_tests ()
29837 {
29838 test_build_non_dependent_expr ();
29839 test_type_dependent_expression_p ();
29840 }
29841
29842 } // namespace selftest
29843
29844 #endif /* #if CHECKING_P */
29845
29846 #include "gt-cp-pt.h"
29847