]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
PR c++/82565 - ICE with concepts and generic lambda.
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template {
54 struct pending_template *next;
55 struct tinst_level *tinst;
56 };
57
58 static GTY(()) struct pending_template *pending_templates;
59 static GTY(()) struct pending_template *last_pending_template;
60
61 int processing_template_parmlist;
62 static int template_header_count;
63
64 static GTY(()) tree saved_trees;
65 static vec<int> inline_parm_levels;
66
67 static GTY(()) struct tinst_level *current_tinst_level;
68
69 static GTY(()) tree saved_access_scope;
70
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr;
75
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
78 //
79 // Implementation of the RAII helper for creating new local
80 // specializations.
81 local_specialization_stack::local_specialization_stack (lss_policy policy)
82 : saved (local_specializations)
83 {
84 if (policy == lss_blank || !saved)
85 local_specializations = new hash_map<tree, tree>;
86 else
87 local_specializations = new hash_map<tree, tree>(*saved);
88 }
89
90 local_specialization_stack::~local_specialization_stack ()
91 {
92 delete local_specializations;
93 local_specializations = saved;
94 }
95
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth;
98
99 struct GTY((for_user)) spec_entry
100 {
101 tree tmpl;
102 tree args;
103 tree spec;
104 };
105
106 struct spec_hasher : ggc_ptr_hash<spec_entry>
107 {
108 static hashval_t hash (spec_entry *);
109 static bool equal (spec_entry *, spec_entry *);
110 };
111
112 static GTY (()) hash_table<spec_hasher> *decl_specializations;
113
114 static GTY (()) hash_table<spec_hasher> *type_specializations;
115
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
121
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
130
131 enum template_base_result {
132 tbr_incomplete_type,
133 tbr_ambiguous_baseclass,
134 tbr_success
135 };
136
137 static void push_access_scope (tree);
138 static void pop_access_scope (tree);
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static tree add_to_template_args (tree, tree);
155 static tree add_outermost_template_args (tree, tree);
156 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
157 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
158 tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t, int,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static void template_parm_level_and_index (tree, int*, int*);
184 static int unify_pack_expansion (tree, tree, tree,
185 tree, unification_kind_t, bool, bool);
186 static tree copy_template_args (tree);
187 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
190 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
191 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
192 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
193 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
194 static bool check_specialization_scope (void);
195 static tree process_partial_specialization (tree);
196 static void set_current_access_from_decl (tree);
197 static enum template_base_result get_template_base (tree, tree, tree, tree,
198 bool , tree *);
199 static tree try_class_unification (tree, tree, tree, tree, bool);
200 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
201 tree, tree);
202 static bool template_template_parm_bindings_ok_p (tree, tree);
203 static void tsubst_default_arguments (tree, tsubst_flags_t);
204 static tree for_each_template_parm_r (tree *, int *, void *);
205 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
206 static void copy_default_args_to_explicit_spec (tree);
207 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
208 static bool dependent_template_arg_p (tree);
209 static bool any_template_arguments_need_structural_equality_p (tree);
210 static bool dependent_type_p_r (tree);
211 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
212 static tree tsubst_decl (tree, tree, tsubst_flags_t);
213 static void perform_typedefs_access_check (tree tmpl, tree targs);
214 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
215 location_t);
216 static tree listify (tree);
217 static tree listify_autos (tree, tree);
218 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
219 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
220 static bool complex_alias_template_p (const_tree tmpl);
221 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
222 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
223 static tree make_argument_pack (tree);
224 static void register_parameter_specializations (tree, tree);
225 static tree enclosing_instantiation_of (tree tctx);
226
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
231
232 static void
233 push_access_scope (tree t)
234 {
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
236 || TREE_CODE (t) == TYPE_DECL);
237
238 if (DECL_FRIEND_CONTEXT (t))
239 push_nested_class (DECL_FRIEND_CONTEXT (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else
243 push_to_top_level ();
244
245 if (TREE_CODE (t) == FUNCTION_DECL)
246 {
247 saved_access_scope = tree_cons
248 (NULL_TREE, current_function_decl, saved_access_scope);
249 current_function_decl = t;
250 }
251 }
252
253 /* Restore the scope set up by push_access_scope. T is the node we
254 are processing. */
255
256 static void
257 pop_access_scope (tree t)
258 {
259 if (TREE_CODE (t) == FUNCTION_DECL)
260 {
261 current_function_decl = TREE_VALUE (saved_access_scope);
262 saved_access_scope = TREE_CHAIN (saved_access_scope);
263 }
264
265 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
266 pop_nested_class ();
267 else
268 pop_from_top_level ();
269 }
270
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
275
276 tree
277 finish_member_template_decl (tree decl)
278 {
279 if (decl == error_mark_node)
280 return error_mark_node;
281
282 gcc_assert (DECL_P (decl));
283
284 if (TREE_CODE (decl) == TYPE_DECL)
285 {
286 tree type;
287
288 type = TREE_TYPE (decl);
289 if (type == error_mark_node)
290 return error_mark_node;
291 if (MAYBE_CLASS_TYPE_P (type)
292 && CLASSTYPE_TEMPLATE_INFO (type)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
294 {
295 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
296 check_member_template (tmpl);
297 return tmpl;
298 }
299 return NULL_TREE;
300 }
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error ("data member %qD cannot be a member template", decl);
303 else if (DECL_TEMPLATE_INFO (decl))
304 {
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
306 {
307 check_member_template (DECL_TI_TEMPLATE (decl));
308 return DECL_TI_TEMPLATE (decl);
309 }
310 else
311 return decl;
312 }
313 else
314 error ("invalid member template declaration %qD", decl);
315
316 return error_mark_node;
317 }
318
319 /* Create a template info node. */
320
321 tree
322 build_template_info (tree template_decl, tree template_args)
323 {
324 tree result = make_node (TEMPLATE_INFO);
325 TI_TEMPLATE (result) = template_decl;
326 TI_ARGS (result) = template_args;
327 return result;
328 }
329
330 /* Return the template info node corresponding to T, whatever T is. */
331
332 tree
333 get_template_info (const_tree t)
334 {
335 tree tinfo = NULL_TREE;
336
337 if (!t || t == error_mark_node)
338 return NULL;
339
340 if (TREE_CODE (t) == NAMESPACE_DECL
341 || TREE_CODE (t) == PARM_DECL)
342 return NULL;
343
344 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
345 tinfo = DECL_TEMPLATE_INFO (t);
346
347 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
348 t = TREE_TYPE (t);
349
350 if (OVERLOAD_TYPE_P (t))
351 tinfo = TYPE_TEMPLATE_INFO (t);
352 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
353 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
354
355 return tinfo;
356 }
357
358 /* Returns the template nesting level of the indicated class TYPE.
359
360 For example, in:
361 template <class T>
362 struct A
363 {
364 template <class U>
365 struct B {};
366 };
367
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
371
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
374 always safe. */
375
376 int
377 template_class_depth (tree type)
378 {
379 int depth;
380
381 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
382 {
383 tree tinfo = get_template_info (type);
384
385 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
387 ++depth;
388
389 if (DECL_P (type))
390 type = CP_DECL_CONTEXT (type);
391 else if (LAMBDA_TYPE_P (type))
392 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
393 else
394 type = CP_TYPE_CONTEXT (type);
395 }
396
397 return depth;
398 }
399
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
402
403 static bool
404 inline_needs_template_parms (tree decl, bool nsdmi)
405 {
406 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
407 return false;
408
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
410 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
411 }
412
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
416 innermost first. */
417
418 static void
419 push_inline_template_parms_recursive (tree parmlist, int levels)
420 {
421 tree parms = TREE_VALUE (parmlist);
422 int i;
423
424 if (levels > 1)
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
426
427 ++processing_template_decl;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl),
430 parms, current_template_parms);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
432
433 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
434 NULL);
435 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
436 {
437 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
438
439 if (error_operand_p (parm))
440 continue;
441
442 gcc_assert (DECL_P (parm));
443
444 switch (TREE_CODE (parm))
445 {
446 case TYPE_DECL:
447 case TEMPLATE_DECL:
448 pushdecl (parm);
449 break;
450
451 case PARM_DECL:
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
454 break;
455
456 default:
457 gcc_unreachable ();
458 }
459 }
460 }
461
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
465
466 void
467 maybe_begin_member_template_processing (tree decl)
468 {
469 tree parms;
470 int levels = 0;
471 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
472
473 if (nsdmi)
474 {
475 tree ctx = DECL_CONTEXT (decl);
476 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx)
479 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
480 }
481
482 if (inline_needs_template_parms (decl, nsdmi))
483 {
484 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
485 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
486
487 if (DECL_TEMPLATE_SPECIALIZATION (decl))
488 {
489 --levels;
490 parms = TREE_CHAIN (parms);
491 }
492
493 push_inline_template_parms_recursive (parms, levels);
494 }
495
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels.safe_push (levels);
499 }
500
501 /* Undo the effects of maybe_begin_member_template_processing. */
502
503 void
504 maybe_end_member_template_processing (void)
505 {
506 int i;
507 int last;
508
509 if (inline_parm_levels.length () == 0)
510 return;
511
512 last = inline_parm_levels.pop ();
513 for (i = 0; i < last; ++i)
514 {
515 --processing_template_decl;
516 current_template_parms = TREE_CHAIN (current_template_parms);
517 poplevel (0, 0, 0);
518 }
519 }
520
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
523
524 static tree
525 add_to_template_args (tree args, tree extra_args)
526 {
527 tree new_args;
528 int extra_depth;
529 int i;
530 int j;
531
532 if (args == NULL_TREE || extra_args == error_mark_node)
533 return extra_args;
534
535 extra_depth = TMPL_ARGS_DEPTH (extra_args);
536 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
537
538 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
539 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
540
541 for (j = 1; j <= extra_depth; ++j, ++i)
542 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
543
544 return new_args;
545 }
546
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
553
554 static tree
555 add_outermost_template_args (tree args, tree extra_args)
556 {
557 tree new_args;
558
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
562
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
564 them. */
565 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
566 return extra_args;
567
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
570
571 new_args = add_to_template_args (args, extra_args);
572
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
575
576 return new_args;
577 }
578
579 /* Return the N levels of innermost template arguments from the ARGS. */
580
581 tree
582 get_innermost_template_args (tree args, int n)
583 {
584 tree new_args;
585 int extra_levels;
586 int i;
587
588 gcc_assert (n >= 0);
589
590 /* If N is 1, just return the innermost set of template arguments. */
591 if (n == 1)
592 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
593
594 /* If we're not removing anything, just return the arguments we were
595 given. */
596 extra_levels = TMPL_ARGS_DEPTH (args) - n;
597 gcc_assert (extra_levels >= 0);
598 if (extra_levels == 0)
599 return args;
600
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args = make_tree_vec (n);
603 for (i = 1; i <= n; ++i)
604 SET_TMPL_ARGS_LEVEL (new_args, i,
605 TMPL_ARGS_LEVEL (args, i + extra_levels));
606
607 return new_args;
608 }
609
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
612
613 static tree
614 strip_innermost_template_args (tree args, int extra_levels)
615 {
616 tree new_args;
617 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
618 int i;
619
620 gcc_assert (n >= 0);
621
622 /* If N is 1, just return the outermost set of template arguments. */
623 if (n == 1)
624 return TMPL_ARGS_LEVEL (args, 1);
625
626 /* If we're not removing anything, just return the arguments we were
627 given. */
628 gcc_assert (extra_levels >= 0);
629 if (extra_levels == 0)
630 return args;
631
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args = make_tree_vec (n);
634 for (i = 1; i <= n; ++i)
635 SET_TMPL_ARGS_LEVEL (new_args, i,
636 TMPL_ARGS_LEVEL (args, i));
637
638 return new_args;
639 }
640
641 /* We've got a template header coming up; push to a new level for storing
642 the parms. */
643
644 void
645 begin_template_parm_list (void)
646 {
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
653 e.g.:
654
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
657 };
658
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms, NULL);
662 ++processing_template_decl;
663 ++processing_template_parmlist;
664 note_template_header (0);
665
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl),
669 make_tree_vec (0),
670 current_template_parms);
671 }
672
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
676
677 static bool
678 check_specialization_scope (void)
679 {
680 tree scope = current_scope ();
681
682 /* [temp.expl.spec]
683
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
690 is a member. */
691 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
692 {
693 error ("explicit specialization in non-namespace scope %qD", scope);
694 return false;
695 }
696
697 /* [temp.expl.spec]
698
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms)
706 {
707 error ("enclosing class templates are not explicitly specialized");
708 return false;
709 }
710
711 return true;
712 }
713
714 /* We've just seen template <>. */
715
716 bool
717 begin_specialization (void)
718 {
719 begin_scope (sk_template_spec, NULL);
720 note_template_header (1);
721 return check_specialization_scope ();
722 }
723
724 /* Called at then end of processing a declaration preceded by
725 template<>. */
726
727 void
728 end_specialization (void)
729 {
730 finish_scope ();
731 reset_specialization ();
732 }
733
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
736
737 void
738 reset_specialization (void)
739 {
740 processing_specialization = 0;
741 template_header_count = 0;
742 }
743
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
746
747 static void
748 note_template_header (int specialization)
749 {
750 processing_specialization = specialization;
751 template_header_count++;
752 }
753
754 /* We're beginning an explicit instantiation. */
755
756 void
757 begin_explicit_instantiation (void)
758 {
759 gcc_assert (!processing_explicit_instantiation);
760 processing_explicit_instantiation = true;
761 }
762
763
764 void
765 end_explicit_instantiation (void)
766 {
767 gcc_assert (processing_explicit_instantiation);
768 processing_explicit_instantiation = false;
769 }
770
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
775
776 static bool
777 check_specialization_namespace (tree tmpl)
778 {
779 tree tpl_ns = decl_namespace_context (tmpl);
780
781 /* [tmpl.expl.spec]
782
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl)
789 && !at_namespace_scope_p ())
790 {
791 error ("specialization of %qD must appear at namespace scope", tmpl);
792 return false;
793 }
794
795 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
796 /* Same or enclosing namespace. */
797 return true;
798 else
799 {
800 permerror (input_location,
801 "specialization of %qD in different namespace", tmpl);
802 inform (DECL_SOURCE_LOCATION (tmpl),
803 " from definition of %q#D", tmpl);
804 return false;
805 }
806 }
807
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
810
811 static void
812 check_explicit_instantiation_namespace (tree spec)
813 {
814 tree ns;
815
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns = decl_namespace_context (spec);
819 if (!is_nested_namespace (current_namespace, ns))
820 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec, current_namespace, ns);
823 }
824
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
828 static tree
829 maybe_new_partial_specialization (tree type)
830 {
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
833 //
834 // template<typename T>
835 // struct S;
836 //
837 // template<typename T>
838 // struct S<T*>;
839 //
840 // Here, S<T*> is an implicit instantiation of S whose type
841 // is incomplete.
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
843 return type;
844
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
847 //
848 // template<typename T>
849 // requires Integral<T>
850 // struct S<T*>;
851 //
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
857 //
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
862 {
863 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
864 tree args = CLASSTYPE_TI_ARGS (type);
865
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms)
869 return NULL_TREE;
870
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
873 return NULL_TREE;
874
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr = current_template_constraints ();
878
879 if (type == TREE_TYPE (tmpl))
880 {
881 tree main_constr = get_constraints (tmpl);
882 if (equivalent_constraints (type_constr, main_constr))
883 return NULL_TREE;
884 }
885
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
889 while (specs)
890 {
891 tree spec_tmpl = TREE_VALUE (specs);
892 tree spec_args = TREE_PURPOSE (specs);
893 tree spec_constr = get_constraints (spec_tmpl);
894 if (comp_template_args (args, spec_args)
895 && equivalent_constraints (type_constr, spec_constr))
896 return NULL_TREE;
897 specs = TREE_CHAIN (specs);
898 }
899
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t = make_class_type (TREE_CODE (type));
903 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
904 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
905
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
910
911 // Build the corresponding type decl.
912 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
913 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
914 DECL_SOURCE_LOCATION (d) = input_location;
915
916 return t;
917 }
918
919 return NULL_TREE;
920 }
921
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
924
925 tree
926 maybe_process_partial_specialization (tree type)
927 {
928 tree context;
929
930 if (type == error_mark_node)
931 return error_mark_node;
932
933 /* A lambda that appears in specialization context is not itself a
934 specialization. */
935 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
936 return type;
937
938 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
939 {
940 error ("name of class shadows template template parameter %qD",
941 TYPE_NAME (type));
942 return error_mark_node;
943 }
944
945 context = TYPE_CONTEXT (type);
946
947 if (TYPE_ALIAS_P (type))
948 {
949 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
950
951 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo));
954 else
955 error ("explicit specialization of non-template %qT", type);
956 return error_mark_node;
957 }
958 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
959 {
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
962
963 template <> class C<int>;
964
965 or:
966
967 template <class T> class C<T*>;
968
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
970
971 if (tree t = maybe_new_partial_specialization (type))
972 {
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
974 && !at_namespace_scope_p ())
975 return error_mark_node;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
978 if (processing_template_decl)
979 {
980 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
981 if (decl == error_mark_node)
982 return error_mark_node;
983 return TREE_TYPE (decl);
984 }
985 }
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
987 error ("specialization of %qT after instantiation", type);
988 else if (errorcount && !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node;
995 }
996 else if (CLASS_TYPE_P (type)
997 && !CLASSTYPE_USE_TEMPLATE (type)
998 && CLASSTYPE_TEMPLATE_INFO (type)
999 && context && CLASS_TYPE_P (context)
1000 && CLASSTYPE_TEMPLATE_INFO (context))
1001 {
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1004
1005 template <> template <class U> class C<int>::D;
1006
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1009 earlier like:
1010
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1013
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1017
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1019 && !COMPLETE_TYPE_P (type))
1020 {
1021 tree t;
1022 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1023
1024 if (current_namespace
1025 != decl_namespace_context (tmpl))
1026 {
1027 permerror (input_location,
1028 "specializing %q#T in different namespace", type);
1029 permerror (DECL_SOURCE_LOCATION (tmpl),
1030 " from definition of %q#D", tmpl);
1031 }
1032
1033 /* Check for invalid specialization after instantiation:
1034
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1037
1038 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1039 t; t = TREE_CHAIN (t))
1040 {
1041 tree inst = TREE_VALUE (t);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1044 {
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1049 spec_entry elt;
1050 spec_entry *entry;
1051
1052 elt.tmpl = most_general_template (tmpl);
1053 elt.args = CLASSTYPE_TI_ARGS (inst);
1054 elt.spec = inst;
1055
1056 type_specializations->remove_elt (&elt);
1057
1058 elt.tmpl = tmpl;
1059 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1060
1061 spec_entry **slot
1062 = type_specializations->find_slot (&elt, INSERT);
1063 entry = ggc_alloc<spec_entry> ();
1064 *entry = elt;
1065 *slot = entry;
1066 }
1067 else
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1071 type, inst);
1072 }
1073
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1076 class template. */
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1079 CLASSTYPE_TI_ARGS (type)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1081 }
1082 }
1083 else if (processing_specialization)
1084 {
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1088 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1089 "of %qD not allowed by ISO C++", type);
1090 else
1091 {
1092 error ("explicit specialization of non-template %qT", type);
1093 return error_mark_node;
1094 }
1095 }
1096
1097 return type;
1098 }
1099
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1103
1104 static inline bool
1105 optimize_specialization_lookup_p (tree tmpl)
1106 {
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1108 && DECL_CLASS_SCOPE_P (tmpl)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1110 parameter. */
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1116 specialization. */
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1119 && !DECL_CONV_FN_P (tmpl)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1122
1123 template <typename T>
1124 struct S { friend A::f(); };
1125
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1131 }
1132
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1135
1136 static void
1137 verify_unstripped_args_1 (tree inner)
1138 {
1139 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1140 {
1141 tree arg = TREE_VEC_ELT (inner, i);
1142 if (TREE_CODE (arg) == TEMPLATE_DECL)
1143 /* OK */;
1144 else if (TYPE_P (arg))
1145 gcc_assert (strip_typedefs (arg, NULL) == arg);
1146 else if (ARGUMENT_PACK_P (arg))
1147 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1148 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1151 else
1152 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1153 }
1154 }
1155
1156 static void
1157 verify_unstripped_args (tree args)
1158 {
1159 ++processing_template_decl;
1160 if (!any_dependent_template_arguments_p (args))
1161 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1162 --processing_template_decl;
1163 }
1164
1165 /* Retrieve the specialization (in the sense of [temp.spec] - a
1166 specialization is either an instantiation or an explicit
1167 specialization) of TMPL for the given template ARGS. If there is
1168 no such specialization, return NULL_TREE. The ARGS are a vector of
1169 arguments, or a vector of vectors of arguments, in the case of
1170 templates with more than one level of parameters.
1171
1172 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1173 then we search for a partial specialization matching ARGS. This
1174 parameter is ignored if TMPL is not a class template.
1175
1176 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1177 result is a NONTYPE_ARGUMENT_PACK. */
1178
1179 static tree
1180 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1181 {
1182 if (tmpl == NULL_TREE)
1183 return NULL_TREE;
1184
1185 if (args == error_mark_node)
1186 return NULL_TREE;
1187
1188 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1189 || TREE_CODE (tmpl) == FIELD_DECL);
1190
1191 /* There should be as many levels of arguments as there are
1192 levels of parameters. */
1193 gcc_assert (TMPL_ARGS_DEPTH (args)
1194 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1195 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1196 : template_class_depth (DECL_CONTEXT (tmpl))));
1197
1198 if (flag_checking)
1199 verify_unstripped_args (args);
1200
1201 /* Lambda functions in templates aren't instantiated normally, but through
1202 tsubst_lambda_expr. */
1203 if (lambda_fn_in_template_p (tmpl))
1204 return NULL_TREE;
1205
1206 if (optimize_specialization_lookup_p (tmpl))
1207 {
1208 /* The template arguments actually apply to the containing
1209 class. Find the class specialization with those
1210 arguments. */
1211 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1212 tree class_specialization
1213 = retrieve_specialization (class_template, args, 0);
1214 if (!class_specialization)
1215 return NULL_TREE;
1216
1217 /* Find the instance of TMPL. */
1218 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1219 for (ovl_iterator iter (fns); iter; ++iter)
1220 {
1221 tree fn = *iter;
1222 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1223 /* using-declarations can add base methods to the method vec,
1224 and we don't want those here. */
1225 && DECL_CONTEXT (fn) == class_specialization)
1226 return fn;
1227 }
1228 return NULL_TREE;
1229 }
1230 else
1231 {
1232 spec_entry *found;
1233 spec_entry elt;
1234 hash_table<spec_hasher> *specializations;
1235
1236 elt.tmpl = tmpl;
1237 elt.args = args;
1238 elt.spec = NULL_TREE;
1239
1240 if (DECL_CLASS_TEMPLATE_P (tmpl))
1241 specializations = type_specializations;
1242 else
1243 specializations = decl_specializations;
1244
1245 if (hash == 0)
1246 hash = spec_hasher::hash (&elt);
1247 found = specializations->find_with_hash (&elt, hash);
1248 if (found)
1249 return found->spec;
1250 }
1251
1252 return NULL_TREE;
1253 }
1254
1255 /* Like retrieve_specialization, but for local declarations. */
1256
1257 tree
1258 retrieve_local_specialization (tree tmpl)
1259 {
1260 if (local_specializations == NULL)
1261 return NULL_TREE;
1262
1263 tree *slot = local_specializations->get (tmpl);
1264 return slot ? *slot : NULL_TREE;
1265 }
1266
1267 /* Returns nonzero iff DECL is a specialization of TMPL. */
1268
1269 int
1270 is_specialization_of (tree decl, tree tmpl)
1271 {
1272 tree t;
1273
1274 if (TREE_CODE (decl) == FUNCTION_DECL)
1275 {
1276 for (t = decl;
1277 t != NULL_TREE;
1278 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1279 if (t == tmpl)
1280 return 1;
1281 }
1282 else
1283 {
1284 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1285
1286 for (t = TREE_TYPE (decl);
1287 t != NULL_TREE;
1288 t = CLASSTYPE_USE_TEMPLATE (t)
1289 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1290 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1291 return 1;
1292 }
1293
1294 return 0;
1295 }
1296
1297 /* Returns nonzero iff DECL is a specialization of friend declaration
1298 FRIEND_DECL according to [temp.friend]. */
1299
1300 bool
1301 is_specialization_of_friend (tree decl, tree friend_decl)
1302 {
1303 bool need_template = true;
1304 int template_depth;
1305
1306 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1307 || TREE_CODE (decl) == TYPE_DECL);
1308
1309 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1310 of a template class, we want to check if DECL is a specialization
1311 if this. */
1312 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1313 && DECL_TEMPLATE_INFO (friend_decl)
1314 && !DECL_USE_TEMPLATE (friend_decl))
1315 {
1316 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1317 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1318 need_template = false;
1319 }
1320 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1321 && !PRIMARY_TEMPLATE_P (friend_decl))
1322 need_template = false;
1323
1324 /* There is nothing to do if this is not a template friend. */
1325 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1326 return false;
1327
1328 if (is_specialization_of (decl, friend_decl))
1329 return true;
1330
1331 /* [temp.friend/6]
1332 A member of a class template may be declared to be a friend of a
1333 non-template class. In this case, the corresponding member of
1334 every specialization of the class template is a friend of the
1335 class granting friendship.
1336
1337 For example, given a template friend declaration
1338
1339 template <class T> friend void A<T>::f();
1340
1341 the member function below is considered a friend
1342
1343 template <> struct A<int> {
1344 void f();
1345 };
1346
1347 For this type of template friend, TEMPLATE_DEPTH below will be
1348 nonzero. To determine if DECL is a friend of FRIEND, we first
1349 check if the enclosing class is a specialization of another. */
1350
1351 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1352 if (template_depth
1353 && DECL_CLASS_SCOPE_P (decl)
1354 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1355 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1356 {
1357 /* Next, we check the members themselves. In order to handle
1358 a few tricky cases, such as when FRIEND_DECL's are
1359
1360 template <class T> friend void A<T>::g(T t);
1361 template <class T> template <T t> friend void A<T>::h();
1362
1363 and DECL's are
1364
1365 void A<int>::g(int);
1366 template <int> void A<int>::h();
1367
1368 we need to figure out ARGS, the template arguments from
1369 the context of DECL. This is required for template substitution
1370 of `T' in the function parameter of `g' and template parameter
1371 of `h' in the above examples. Here ARGS corresponds to `int'. */
1372
1373 tree context = DECL_CONTEXT (decl);
1374 tree args = NULL_TREE;
1375 int current_depth = 0;
1376
1377 while (current_depth < template_depth)
1378 {
1379 if (CLASSTYPE_TEMPLATE_INFO (context))
1380 {
1381 if (current_depth == 0)
1382 args = TYPE_TI_ARGS (context);
1383 else
1384 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1385 current_depth++;
1386 }
1387 context = TYPE_CONTEXT (context);
1388 }
1389
1390 if (TREE_CODE (decl) == FUNCTION_DECL)
1391 {
1392 bool is_template;
1393 tree friend_type;
1394 tree decl_type;
1395 tree friend_args_type;
1396 tree decl_args_type;
1397
1398 /* Make sure that both DECL and FRIEND_DECL are templates or
1399 non-templates. */
1400 is_template = DECL_TEMPLATE_INFO (decl)
1401 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1402 if (need_template ^ is_template)
1403 return false;
1404 else if (is_template)
1405 {
1406 /* If both are templates, check template parameter list. */
1407 tree friend_parms
1408 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1409 args, tf_none);
1410 if (!comp_template_parms
1411 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1412 friend_parms))
1413 return false;
1414
1415 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1416 }
1417 else
1418 decl_type = TREE_TYPE (decl);
1419
1420 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1421 tf_none, NULL_TREE);
1422 if (friend_type == error_mark_node)
1423 return false;
1424
1425 /* Check if return types match. */
1426 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1427 return false;
1428
1429 /* Check if function parameter types match, ignoring the
1430 `this' parameter. */
1431 friend_args_type = TYPE_ARG_TYPES (friend_type);
1432 decl_args_type = TYPE_ARG_TYPES (decl_type);
1433 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1434 friend_args_type = TREE_CHAIN (friend_args_type);
1435 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1436 decl_args_type = TREE_CHAIN (decl_args_type);
1437
1438 return compparms (decl_args_type, friend_args_type);
1439 }
1440 else
1441 {
1442 /* DECL is a TYPE_DECL */
1443 bool is_template;
1444 tree decl_type = TREE_TYPE (decl);
1445
1446 /* Make sure that both DECL and FRIEND_DECL are templates or
1447 non-templates. */
1448 is_template
1449 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1451
1452 if (need_template ^ is_template)
1453 return false;
1454 else if (is_template)
1455 {
1456 tree friend_parms;
1457 /* If both are templates, check the name of the two
1458 TEMPLATE_DECL's first because is_friend didn't. */
1459 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1460 != DECL_NAME (friend_decl))
1461 return false;
1462
1463 /* Now check template parameter list. */
1464 friend_parms
1465 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1466 args, tf_none);
1467 return comp_template_parms
1468 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1469 friend_parms);
1470 }
1471 else
1472 return (DECL_NAME (decl)
1473 == DECL_NAME (friend_decl));
1474 }
1475 }
1476 return false;
1477 }
1478
1479 /* Register the specialization SPEC as a specialization of TMPL with
1480 the indicated ARGS. IS_FRIEND indicates whether the specialization
1481 is actually just a friend declaration. ATTRLIST is the list of
1482 attributes that the specialization is declared with or NULL when
1483 it isn't. Returns SPEC, or an equivalent prior declaration, if
1484 available.
1485
1486 We also store instantiations of field packs in the hash table, even
1487 though they are not themselves templates, to make lookup easier. */
1488
1489 static tree
1490 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1491 hashval_t hash)
1492 {
1493 tree fn;
1494 spec_entry **slot = NULL;
1495 spec_entry elt;
1496
1497 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1498 || (TREE_CODE (tmpl) == FIELD_DECL
1499 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1500
1501 if (TREE_CODE (spec) == FUNCTION_DECL
1502 && uses_template_parms (DECL_TI_ARGS (spec)))
1503 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1504 register it; we want the corresponding TEMPLATE_DECL instead.
1505 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1506 the more obvious `uses_template_parms (spec)' to avoid problems
1507 with default function arguments. In particular, given
1508 something like this:
1509
1510 template <class T> void f(T t1, T t = T())
1511
1512 the default argument expression is not substituted for in an
1513 instantiation unless and until it is actually needed. */
1514 return spec;
1515
1516 if (optimize_specialization_lookup_p (tmpl))
1517 /* We don't put these specializations in the hash table, but we might
1518 want to give an error about a mismatch. */
1519 fn = retrieve_specialization (tmpl, args, 0);
1520 else
1521 {
1522 elt.tmpl = tmpl;
1523 elt.args = args;
1524 elt.spec = spec;
1525
1526 if (hash == 0)
1527 hash = spec_hasher::hash (&elt);
1528
1529 slot =
1530 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1531 if (*slot)
1532 fn = ((spec_entry *) *slot)->spec;
1533 else
1534 fn = NULL_TREE;
1535 }
1536
1537 /* We can sometimes try to re-register a specialization that we've
1538 already got. In particular, regenerate_decl_from_template calls
1539 duplicate_decls which will update the specialization list. But,
1540 we'll still get called again here anyhow. It's more convenient
1541 to simply allow this than to try to prevent it. */
1542 if (fn == spec)
1543 return spec;
1544 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1545 {
1546 if (DECL_TEMPLATE_INSTANTIATION (fn))
1547 {
1548 if (DECL_ODR_USED (fn)
1549 || DECL_EXPLICIT_INSTANTIATION (fn))
1550 {
1551 error ("specialization of %qD after instantiation",
1552 fn);
1553 return error_mark_node;
1554 }
1555 else
1556 {
1557 tree clone;
1558 /* This situation should occur only if the first
1559 specialization is an implicit instantiation, the
1560 second is an explicit specialization, and the
1561 implicit instantiation has not yet been used. That
1562 situation can occur if we have implicitly
1563 instantiated a member function and then specialized
1564 it later.
1565
1566 We can also wind up here if a friend declaration that
1567 looked like an instantiation turns out to be a
1568 specialization:
1569
1570 template <class T> void foo(T);
1571 class S { friend void foo<>(int) };
1572 template <> void foo(int);
1573
1574 We transform the existing DECL in place so that any
1575 pointers to it become pointers to the updated
1576 declaration.
1577
1578 If there was a definition for the template, but not
1579 for the specialization, we want this to look as if
1580 there were no definition, and vice versa. */
1581 DECL_INITIAL (fn) = NULL_TREE;
1582 duplicate_decls (spec, fn, is_friend);
1583 /* The call to duplicate_decls will have applied
1584 [temp.expl.spec]:
1585
1586 An explicit specialization of a function template
1587 is inline only if it is explicitly declared to be,
1588 and independently of whether its function template
1589 is.
1590
1591 to the primary function; now copy the inline bits to
1592 the various clones. */
1593 FOR_EACH_CLONE (clone, fn)
1594 {
1595 DECL_DECLARED_INLINE_P (clone)
1596 = DECL_DECLARED_INLINE_P (fn);
1597 DECL_SOURCE_LOCATION (clone)
1598 = DECL_SOURCE_LOCATION (fn);
1599 DECL_DELETED_FN (clone)
1600 = DECL_DELETED_FN (fn);
1601 }
1602 check_specialization_namespace (tmpl);
1603
1604 return fn;
1605 }
1606 }
1607 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1608 {
1609 tree dd = duplicate_decls (spec, fn, is_friend);
1610 if (dd == error_mark_node)
1611 /* We've already complained in duplicate_decls. */
1612 return error_mark_node;
1613
1614 if (dd == NULL_TREE && DECL_INITIAL (spec))
1615 /* Dup decl failed, but this is a new definition. Set the
1616 line number so any errors match this new
1617 definition. */
1618 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1619
1620 return fn;
1621 }
1622 }
1623 else if (fn)
1624 return duplicate_decls (spec, fn, is_friend);
1625
1626 /* A specialization must be declared in the same namespace as the
1627 template it is specializing. */
1628 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1629 && !check_specialization_namespace (tmpl))
1630 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1631
1632 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1633 {
1634 spec_entry *entry = ggc_alloc<spec_entry> ();
1635 gcc_assert (tmpl && args && spec);
1636 *entry = elt;
1637 *slot = entry;
1638 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1639 && PRIMARY_TEMPLATE_P (tmpl)
1640 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1641 || variable_template_p (tmpl))
1642 /* If TMPL is a forward declaration of a template function, keep a list
1643 of all specializations in case we need to reassign them to a friend
1644 template later in tsubst_friend_function.
1645
1646 Also keep a list of all variable template instantiations so that
1647 process_partial_specialization can check whether a later partial
1648 specialization would have used it. */
1649 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1650 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1651 }
1652
1653 return spec;
1654 }
1655
1656 /* Returns true iff two spec_entry nodes are equivalent. */
1657
1658 int comparing_specializations;
1659
1660 bool
1661 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1662 {
1663 int equal;
1664
1665 ++comparing_specializations;
1666 equal = (e1->tmpl == e2->tmpl
1667 && comp_template_args (e1->args, e2->args));
1668 if (equal && flag_concepts
1669 /* tmpl could be a FIELD_DECL for a capture pack. */
1670 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1671 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1672 && uses_template_parms (e1->args))
1673 {
1674 /* Partial specializations of a variable template can be distinguished by
1675 constraints. */
1676 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1677 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1678 equal = equivalent_constraints (c1, c2);
1679 }
1680 --comparing_specializations;
1681
1682 return equal;
1683 }
1684
1685 /* Returns a hash for a template TMPL and template arguments ARGS. */
1686
1687 static hashval_t
1688 hash_tmpl_and_args (tree tmpl, tree args)
1689 {
1690 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1691 return iterative_hash_template_arg (args, val);
1692 }
1693
1694 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1695 ignoring SPEC. */
1696
1697 hashval_t
1698 spec_hasher::hash (spec_entry *e)
1699 {
1700 return hash_tmpl_and_args (e->tmpl, e->args);
1701 }
1702
1703 /* Recursively calculate a hash value for a template argument ARG, for use
1704 in the hash tables of template specializations. */
1705
1706 hashval_t
1707 iterative_hash_template_arg (tree arg, hashval_t val)
1708 {
1709 unsigned HOST_WIDE_INT i;
1710 enum tree_code code;
1711 char tclass;
1712
1713 if (arg == NULL_TREE)
1714 return iterative_hash_object (arg, val);
1715
1716 if (!TYPE_P (arg))
1717 STRIP_NOPS (arg);
1718
1719 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1720 gcc_unreachable ();
1721
1722 code = TREE_CODE (arg);
1723 tclass = TREE_CODE_CLASS (code);
1724
1725 val = iterative_hash_object (code, val);
1726
1727 switch (code)
1728 {
1729 case ERROR_MARK:
1730 return val;
1731
1732 case IDENTIFIER_NODE:
1733 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1734
1735 case TREE_VEC:
1736 {
1737 int i, len = TREE_VEC_LENGTH (arg);
1738 for (i = 0; i < len; ++i)
1739 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1740 return val;
1741 }
1742
1743 case TYPE_PACK_EXPANSION:
1744 case EXPR_PACK_EXPANSION:
1745 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1746 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1747
1748 case TYPE_ARGUMENT_PACK:
1749 case NONTYPE_ARGUMENT_PACK:
1750 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1751
1752 case TREE_LIST:
1753 for (; arg; arg = TREE_CHAIN (arg))
1754 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1755 return val;
1756
1757 case OVERLOAD:
1758 for (lkp_iterator iter (arg); iter; ++iter)
1759 val = iterative_hash_template_arg (*iter, val);
1760 return val;
1761
1762 case CONSTRUCTOR:
1763 {
1764 tree field, value;
1765 iterative_hash_template_arg (TREE_TYPE (arg), val);
1766 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1767 {
1768 val = iterative_hash_template_arg (field, val);
1769 val = iterative_hash_template_arg (value, val);
1770 }
1771 return val;
1772 }
1773
1774 case PARM_DECL:
1775 if (!DECL_ARTIFICIAL (arg))
1776 {
1777 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1778 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1779 }
1780 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1781
1782 case TARGET_EXPR:
1783 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1784
1785 case PTRMEM_CST:
1786 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1787 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1788
1789 case TEMPLATE_PARM_INDEX:
1790 val = iterative_hash_template_arg
1791 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1792 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1793 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1794
1795 case TRAIT_EXPR:
1796 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1797 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1798 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1799
1800 case BASELINK:
1801 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1802 val);
1803 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1804 val);
1805
1806 case MODOP_EXPR:
1807 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1808 code = TREE_CODE (TREE_OPERAND (arg, 1));
1809 val = iterative_hash_object (code, val);
1810 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1811
1812 case LAMBDA_EXPR:
1813 /* A lambda can't appear in a template arg, but don't crash on
1814 erroneous input. */
1815 gcc_assert (seen_error ());
1816 return val;
1817
1818 case CAST_EXPR:
1819 case IMPLICIT_CONV_EXPR:
1820 case STATIC_CAST_EXPR:
1821 case REINTERPRET_CAST_EXPR:
1822 case CONST_CAST_EXPR:
1823 case DYNAMIC_CAST_EXPR:
1824 case NEW_EXPR:
1825 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1826 /* Now hash operands as usual. */
1827 break;
1828
1829 default:
1830 break;
1831 }
1832
1833 switch (tclass)
1834 {
1835 case tcc_type:
1836 if (alias_template_specialization_p (arg))
1837 {
1838 // We want an alias specialization that survived strip_typedefs
1839 // to hash differently from its TYPE_CANONICAL, to avoid hash
1840 // collisions that compare as different in template_args_equal.
1841 // These could be dependent specializations that strip_typedefs
1842 // left alone, or untouched specializations because
1843 // coerce_template_parms returns the unconverted template
1844 // arguments if it sees incomplete argument packs.
1845 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1846 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1847 }
1848 if (TYPE_CANONICAL (arg))
1849 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1850 val);
1851 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1852 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1853 /* Otherwise just compare the types during lookup. */
1854 return val;
1855
1856 case tcc_declaration:
1857 case tcc_constant:
1858 return iterative_hash_expr (arg, val);
1859
1860 default:
1861 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1862 {
1863 unsigned n = cp_tree_operand_length (arg);
1864 for (i = 0; i < n; ++i)
1865 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1866 return val;
1867 }
1868 }
1869 gcc_unreachable ();
1870 return 0;
1871 }
1872
1873 /* Unregister the specialization SPEC as a specialization of TMPL.
1874 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1875 if the SPEC was listed as a specialization of TMPL.
1876
1877 Note that SPEC has been ggc_freed, so we can't look inside it. */
1878
1879 bool
1880 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1881 {
1882 spec_entry *entry;
1883 spec_entry elt;
1884
1885 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1886 elt.args = TI_ARGS (tinfo);
1887 elt.spec = NULL_TREE;
1888
1889 entry = decl_specializations->find (&elt);
1890 if (entry != NULL)
1891 {
1892 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1893 gcc_assert (new_spec != NULL_TREE);
1894 entry->spec = new_spec;
1895 return 1;
1896 }
1897
1898 return 0;
1899 }
1900
1901 /* Like register_specialization, but for local declarations. We are
1902 registering SPEC, an instantiation of TMPL. */
1903
1904 void
1905 register_local_specialization (tree spec, tree tmpl)
1906 {
1907 gcc_assert (tmpl != spec);
1908 local_specializations->put (tmpl, spec);
1909 }
1910
1911 /* TYPE is a class type. Returns true if TYPE is an explicitly
1912 specialized class. */
1913
1914 bool
1915 explicit_class_specialization_p (tree type)
1916 {
1917 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1918 return false;
1919 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1920 }
1921
1922 /* Print the list of functions at FNS, going through all the overloads
1923 for each element of the list. Alternatively, FNS can not be a
1924 TREE_LIST, in which case it will be printed together with all the
1925 overloads.
1926
1927 MORE and *STR should respectively be FALSE and NULL when the function
1928 is called from the outside. They are used internally on recursive
1929 calls. print_candidates manages the two parameters and leaves NULL
1930 in *STR when it ends. */
1931
1932 static void
1933 print_candidates_1 (tree fns, char **str, bool more = false)
1934 {
1935 if (TREE_CODE (fns) == TREE_LIST)
1936 for (; fns; fns = TREE_CHAIN (fns))
1937 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1938 else
1939 for (lkp_iterator iter (fns); iter;)
1940 {
1941 tree cand = *iter;
1942 ++iter;
1943
1944 const char *pfx = *str;
1945 if (!pfx)
1946 {
1947 if (more || iter)
1948 pfx = _("candidates are:");
1949 else
1950 pfx = _("candidate is:");
1951 *str = get_spaces (pfx);
1952 }
1953 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1954 }
1955 }
1956
1957 /* Print the list of candidate FNS in an error message. FNS can also
1958 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1959
1960 void
1961 print_candidates (tree fns)
1962 {
1963 char *str = NULL;
1964 print_candidates_1 (fns, &str);
1965 free (str);
1966 }
1967
1968 /* Get a (possibly) constrained template declaration for the
1969 purpose of ordering candidates. */
1970 static tree
1971 get_template_for_ordering (tree list)
1972 {
1973 gcc_assert (TREE_CODE (list) == TREE_LIST);
1974 tree f = TREE_VALUE (list);
1975 if (tree ti = DECL_TEMPLATE_INFO (f))
1976 return TI_TEMPLATE (ti);
1977 return f;
1978 }
1979
1980 /* Among candidates having the same signature, return the
1981 most constrained or NULL_TREE if there is no best candidate.
1982 If the signatures of candidates vary (e.g., template
1983 specialization vs. member function), then there can be no
1984 most constrained.
1985
1986 Note that we don't compare constraints on the functions
1987 themselves, but rather those of their templates. */
1988 static tree
1989 most_constrained_function (tree candidates)
1990 {
1991 // Try to find the best candidate in a first pass.
1992 tree champ = candidates;
1993 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1994 {
1995 int winner = more_constrained (get_template_for_ordering (champ),
1996 get_template_for_ordering (c));
1997 if (winner == -1)
1998 champ = c; // The candidate is more constrained
1999 else if (winner == 0)
2000 return NULL_TREE; // Neither is more constrained
2001 }
2002
2003 // Verify that the champ is better than previous candidates.
2004 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2005 if (!more_constrained (get_template_for_ordering (champ),
2006 get_template_for_ordering (c)))
2007 return NULL_TREE;
2008 }
2009
2010 return champ;
2011 }
2012
2013
2014 /* Returns the template (one of the functions given by TEMPLATE_ID)
2015 which can be specialized to match the indicated DECL with the
2016 explicit template args given in TEMPLATE_ID. The DECL may be
2017 NULL_TREE if none is available. In that case, the functions in
2018 TEMPLATE_ID are non-members.
2019
2020 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2021 specialization of a member template.
2022
2023 The TEMPLATE_COUNT is the number of references to qualifying
2024 template classes that appeared in the name of the function. See
2025 check_explicit_specialization for a more accurate description.
2026
2027 TSK indicates what kind of template declaration (if any) is being
2028 declared. TSK_TEMPLATE indicates that the declaration given by
2029 DECL, though a FUNCTION_DECL, has template parameters, and is
2030 therefore a template function.
2031
2032 The template args (those explicitly specified and those deduced)
2033 are output in a newly created vector *TARGS_OUT.
2034
2035 If it is impossible to determine the result, an error message is
2036 issued. The error_mark_node is returned to indicate failure. */
2037
2038 static tree
2039 determine_specialization (tree template_id,
2040 tree decl,
2041 tree* targs_out,
2042 int need_member_template,
2043 int template_count,
2044 tmpl_spec_kind tsk)
2045 {
2046 tree fns;
2047 tree targs;
2048 tree explicit_targs;
2049 tree candidates = NULL_TREE;
2050
2051 /* A TREE_LIST of templates of which DECL may be a specialization.
2052 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2053 corresponding TREE_PURPOSE is the set of template arguments that,
2054 when used to instantiate the template, would produce a function
2055 with the signature of DECL. */
2056 tree templates = NULL_TREE;
2057 int header_count;
2058 cp_binding_level *b;
2059
2060 *targs_out = NULL_TREE;
2061
2062 if (template_id == error_mark_node || decl == error_mark_node)
2063 return error_mark_node;
2064
2065 /* We shouldn't be specializing a member template of an
2066 unspecialized class template; we already gave an error in
2067 check_specialization_scope, now avoid crashing. */
2068 if (!VAR_P (decl)
2069 && template_count && DECL_CLASS_SCOPE_P (decl)
2070 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2071 {
2072 gcc_assert (errorcount);
2073 return error_mark_node;
2074 }
2075
2076 fns = TREE_OPERAND (template_id, 0);
2077 explicit_targs = TREE_OPERAND (template_id, 1);
2078
2079 if (fns == error_mark_node)
2080 return error_mark_node;
2081
2082 /* Check for baselinks. */
2083 if (BASELINK_P (fns))
2084 fns = BASELINK_FUNCTIONS (fns);
2085
2086 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2087 {
2088 error ("%qD is not a function template", fns);
2089 return error_mark_node;
2090 }
2091 else if (VAR_P (decl) && !variable_template_p (fns))
2092 {
2093 error ("%qD is not a variable template", fns);
2094 return error_mark_node;
2095 }
2096
2097 /* Count the number of template headers specified for this
2098 specialization. */
2099 header_count = 0;
2100 for (b = current_binding_level;
2101 b->kind == sk_template_parms;
2102 b = b->level_chain)
2103 ++header_count;
2104
2105 tree orig_fns = fns;
2106
2107 if (variable_template_p (fns))
2108 {
2109 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2110 targs = coerce_template_parms (parms, explicit_targs, fns,
2111 tf_warning_or_error,
2112 /*req_all*/true, /*use_defarg*/true);
2113 if (targs != error_mark_node)
2114 templates = tree_cons (targs, fns, templates);
2115 }
2116 else for (lkp_iterator iter (fns); iter; ++iter)
2117 {
2118 tree fn = *iter;
2119
2120 if (TREE_CODE (fn) == TEMPLATE_DECL)
2121 {
2122 tree decl_arg_types;
2123 tree fn_arg_types;
2124 tree insttype;
2125
2126 /* In case of explicit specialization, we need to check if
2127 the number of template headers appearing in the specialization
2128 is correct. This is usually done in check_explicit_specialization,
2129 but the check done there cannot be exhaustive when specializing
2130 member functions. Consider the following code:
2131
2132 template <> void A<int>::f(int);
2133 template <> template <> void A<int>::f(int);
2134
2135 Assuming that A<int> is not itself an explicit specialization
2136 already, the first line specializes "f" which is a non-template
2137 member function, whilst the second line specializes "f" which
2138 is a template member function. So both lines are syntactically
2139 correct, and check_explicit_specialization does not reject
2140 them.
2141
2142 Here, we can do better, as we are matching the specialization
2143 against the declarations. We count the number of template
2144 headers, and we check if they match TEMPLATE_COUNT + 1
2145 (TEMPLATE_COUNT is the number of qualifying template classes,
2146 plus there must be another header for the member template
2147 itself).
2148
2149 Notice that if header_count is zero, this is not a
2150 specialization but rather a template instantiation, so there
2151 is no check we can perform here. */
2152 if (header_count && header_count != template_count + 1)
2153 continue;
2154
2155 /* Check that the number of template arguments at the
2156 innermost level for DECL is the same as for FN. */
2157 if (current_binding_level->kind == sk_template_parms
2158 && !current_binding_level->explicit_spec_p
2159 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2160 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2161 (current_template_parms))))
2162 continue;
2163
2164 /* DECL might be a specialization of FN. */
2165 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2166 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2167
2168 /* For a non-static member function, we need to make sure
2169 that the const qualification is the same. Since
2170 get_bindings does not try to merge the "this" parameter,
2171 we must do the comparison explicitly. */
2172 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2173 {
2174 if (!same_type_p (TREE_VALUE (fn_arg_types),
2175 TREE_VALUE (decl_arg_types)))
2176 continue;
2177
2178 /* And the ref-qualification. */
2179 if (type_memfn_rqual (TREE_TYPE (decl))
2180 != type_memfn_rqual (TREE_TYPE (fn)))
2181 continue;
2182 }
2183
2184 /* Skip the "this" parameter and, for constructors of
2185 classes with virtual bases, the VTT parameter. A
2186 full specialization of a constructor will have a VTT
2187 parameter, but a template never will. */
2188 decl_arg_types
2189 = skip_artificial_parms_for (decl, decl_arg_types);
2190 fn_arg_types
2191 = skip_artificial_parms_for (fn, fn_arg_types);
2192
2193 /* Function templates cannot be specializations; there are
2194 no partial specializations of functions. Therefore, if
2195 the type of DECL does not match FN, there is no
2196 match.
2197
2198 Note that it should never be the case that we have both
2199 candidates added here, and for regular member functions
2200 below. */
2201 if (tsk == tsk_template)
2202 {
2203 if (compparms (fn_arg_types, decl_arg_types))
2204 candidates = tree_cons (NULL_TREE, fn, candidates);
2205 continue;
2206 }
2207
2208 /* See whether this function might be a specialization of this
2209 template. Suppress access control because we might be trying
2210 to make this specialization a friend, and we have already done
2211 access control for the declaration of the specialization. */
2212 push_deferring_access_checks (dk_no_check);
2213 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2214 pop_deferring_access_checks ();
2215
2216 if (!targs)
2217 /* We cannot deduce template arguments that when used to
2218 specialize TMPL will produce DECL. */
2219 continue;
2220
2221 if (uses_template_parms (targs))
2222 /* We deduced something involving 'auto', which isn't a valid
2223 template argument. */
2224 continue;
2225
2226 /* Remove, from the set of candidates, all those functions
2227 whose constraints are not satisfied. */
2228 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2229 continue;
2230
2231 // Then, try to form the new function type.
2232 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2233 if (insttype == error_mark_node)
2234 continue;
2235 fn_arg_types
2236 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2237 if (!compparms (fn_arg_types, decl_arg_types))
2238 continue;
2239
2240 /* Save this template, and the arguments deduced. */
2241 templates = tree_cons (targs, fn, templates);
2242 }
2243 else if (need_member_template)
2244 /* FN is an ordinary member function, and we need a
2245 specialization of a member template. */
2246 ;
2247 else if (TREE_CODE (fn) != FUNCTION_DECL)
2248 /* We can get IDENTIFIER_NODEs here in certain erroneous
2249 cases. */
2250 ;
2251 else if (!DECL_FUNCTION_MEMBER_P (fn))
2252 /* This is just an ordinary non-member function. Nothing can
2253 be a specialization of that. */
2254 ;
2255 else if (DECL_ARTIFICIAL (fn))
2256 /* Cannot specialize functions that are created implicitly. */
2257 ;
2258 else
2259 {
2260 tree decl_arg_types;
2261
2262 /* This is an ordinary member function. However, since
2263 we're here, we can assume its enclosing class is a
2264 template class. For example,
2265
2266 template <typename T> struct S { void f(); };
2267 template <> void S<int>::f() {}
2268
2269 Here, S<int>::f is a non-template, but S<int> is a
2270 template class. If FN has the same type as DECL, we
2271 might be in business. */
2272
2273 if (!DECL_TEMPLATE_INFO (fn))
2274 /* Its enclosing class is an explicit specialization
2275 of a template class. This is not a candidate. */
2276 continue;
2277
2278 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2279 TREE_TYPE (TREE_TYPE (fn))))
2280 /* The return types differ. */
2281 continue;
2282
2283 /* Adjust the type of DECL in case FN is a static member. */
2284 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2285 if (DECL_STATIC_FUNCTION_P (fn)
2286 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2287 decl_arg_types = TREE_CHAIN (decl_arg_types);
2288
2289 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2290 decl_arg_types))
2291 continue;
2292
2293 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2294 && (type_memfn_rqual (TREE_TYPE (decl))
2295 != type_memfn_rqual (TREE_TYPE (fn))))
2296 continue;
2297
2298 // If the deduced arguments do not satisfy the constraints,
2299 // this is not a candidate.
2300 if (flag_concepts && !constraints_satisfied_p (fn))
2301 continue;
2302
2303 // Add the candidate.
2304 candidates = tree_cons (NULL_TREE, fn, candidates);
2305 }
2306 }
2307
2308 if (templates && TREE_CHAIN (templates))
2309 {
2310 /* We have:
2311
2312 [temp.expl.spec]
2313
2314 It is possible for a specialization with a given function
2315 signature to be instantiated from more than one function
2316 template. In such cases, explicit specification of the
2317 template arguments must be used to uniquely identify the
2318 function template specialization being specialized.
2319
2320 Note that here, there's no suggestion that we're supposed to
2321 determine which of the candidate templates is most
2322 specialized. However, we, also have:
2323
2324 [temp.func.order]
2325
2326 Partial ordering of overloaded function template
2327 declarations is used in the following contexts to select
2328 the function template to which a function template
2329 specialization refers:
2330
2331 -- when an explicit specialization refers to a function
2332 template.
2333
2334 So, we do use the partial ordering rules, at least for now.
2335 This extension can only serve to make invalid programs valid,
2336 so it's safe. And, there is strong anecdotal evidence that
2337 the committee intended the partial ordering rules to apply;
2338 the EDG front end has that behavior, and John Spicer claims
2339 that the committee simply forgot to delete the wording in
2340 [temp.expl.spec]. */
2341 tree tmpl = most_specialized_instantiation (templates);
2342 if (tmpl != error_mark_node)
2343 {
2344 templates = tmpl;
2345 TREE_CHAIN (templates) = NULL_TREE;
2346 }
2347 }
2348
2349 // Concepts allows multiple declarations of member functions
2350 // with the same signature. Like above, we need to rely on
2351 // on the partial ordering of those candidates to determine which
2352 // is the best.
2353 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2354 {
2355 if (tree cand = most_constrained_function (candidates))
2356 {
2357 candidates = cand;
2358 TREE_CHAIN (cand) = NULL_TREE;
2359 }
2360 }
2361
2362 if (templates == NULL_TREE && candidates == NULL_TREE)
2363 {
2364 error ("template-id %qD for %q+D does not match any template "
2365 "declaration", template_id, decl);
2366 if (header_count && header_count != template_count + 1)
2367 inform (input_location, "saw %d %<template<>%>, need %d for "
2368 "specializing a member function template",
2369 header_count, template_count + 1);
2370 else
2371 print_candidates (orig_fns);
2372 return error_mark_node;
2373 }
2374 else if ((templates && TREE_CHAIN (templates))
2375 || (candidates && TREE_CHAIN (candidates))
2376 || (templates && candidates))
2377 {
2378 error ("ambiguous template specialization %qD for %q+D",
2379 template_id, decl);
2380 candidates = chainon (candidates, templates);
2381 print_candidates (candidates);
2382 return error_mark_node;
2383 }
2384
2385 /* We have one, and exactly one, match. */
2386 if (candidates)
2387 {
2388 tree fn = TREE_VALUE (candidates);
2389 *targs_out = copy_node (DECL_TI_ARGS (fn));
2390
2391 // Propagate the candidate's constraints to the declaration.
2392 set_constraints (decl, get_constraints (fn));
2393
2394 /* DECL is a re-declaration or partial instantiation of a template
2395 function. */
2396 if (TREE_CODE (fn) == TEMPLATE_DECL)
2397 return fn;
2398 /* It was a specialization of an ordinary member function in a
2399 template class. */
2400 return DECL_TI_TEMPLATE (fn);
2401 }
2402
2403 /* It was a specialization of a template. */
2404 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2405 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2406 {
2407 *targs_out = copy_node (targs);
2408 SET_TMPL_ARGS_LEVEL (*targs_out,
2409 TMPL_ARGS_DEPTH (*targs_out),
2410 TREE_PURPOSE (templates));
2411 }
2412 else
2413 *targs_out = TREE_PURPOSE (templates);
2414 return TREE_VALUE (templates);
2415 }
2416
2417 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2418 but with the default argument values filled in from those in the
2419 TMPL_TYPES. */
2420
2421 static tree
2422 copy_default_args_to_explicit_spec_1 (tree spec_types,
2423 tree tmpl_types)
2424 {
2425 tree new_spec_types;
2426
2427 if (!spec_types)
2428 return NULL_TREE;
2429
2430 if (spec_types == void_list_node)
2431 return void_list_node;
2432
2433 /* Substitute into the rest of the list. */
2434 new_spec_types =
2435 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2436 TREE_CHAIN (tmpl_types));
2437
2438 /* Add the default argument for this parameter. */
2439 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2440 TREE_VALUE (spec_types),
2441 new_spec_types);
2442 }
2443
2444 /* DECL is an explicit specialization. Replicate default arguments
2445 from the template it specializes. (That way, code like:
2446
2447 template <class T> void f(T = 3);
2448 template <> void f(double);
2449 void g () { f (); }
2450
2451 works, as required.) An alternative approach would be to look up
2452 the correct default arguments at the call-site, but this approach
2453 is consistent with how implicit instantiations are handled. */
2454
2455 static void
2456 copy_default_args_to_explicit_spec (tree decl)
2457 {
2458 tree tmpl;
2459 tree spec_types;
2460 tree tmpl_types;
2461 tree new_spec_types;
2462 tree old_type;
2463 tree new_type;
2464 tree t;
2465 tree object_type = NULL_TREE;
2466 tree in_charge = NULL_TREE;
2467 tree vtt = NULL_TREE;
2468
2469 /* See if there's anything we need to do. */
2470 tmpl = DECL_TI_TEMPLATE (decl);
2471 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2472 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2473 if (TREE_PURPOSE (t))
2474 break;
2475 if (!t)
2476 return;
2477
2478 old_type = TREE_TYPE (decl);
2479 spec_types = TYPE_ARG_TYPES (old_type);
2480
2481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2482 {
2483 /* Remove the this pointer, but remember the object's type for
2484 CV quals. */
2485 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2486 spec_types = TREE_CHAIN (spec_types);
2487 tmpl_types = TREE_CHAIN (tmpl_types);
2488
2489 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2490 {
2491 /* DECL may contain more parameters than TMPL due to the extra
2492 in-charge parameter in constructors and destructors. */
2493 in_charge = spec_types;
2494 spec_types = TREE_CHAIN (spec_types);
2495 }
2496 if (DECL_HAS_VTT_PARM_P (decl))
2497 {
2498 vtt = spec_types;
2499 spec_types = TREE_CHAIN (spec_types);
2500 }
2501 }
2502
2503 /* Compute the merged default arguments. */
2504 new_spec_types =
2505 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2506
2507 /* Compute the new FUNCTION_TYPE. */
2508 if (object_type)
2509 {
2510 if (vtt)
2511 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2512 TREE_VALUE (vtt),
2513 new_spec_types);
2514
2515 if (in_charge)
2516 /* Put the in-charge parameter back. */
2517 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2518 TREE_VALUE (in_charge),
2519 new_spec_types);
2520
2521 new_type = build_method_type_directly (object_type,
2522 TREE_TYPE (old_type),
2523 new_spec_types);
2524 }
2525 else
2526 new_type = build_function_type (TREE_TYPE (old_type),
2527 new_spec_types);
2528 new_type = cp_build_type_attribute_variant (new_type,
2529 TYPE_ATTRIBUTES (old_type));
2530 new_type = build_exception_variant (new_type,
2531 TYPE_RAISES_EXCEPTIONS (old_type));
2532
2533 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2534 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2535
2536 TREE_TYPE (decl) = new_type;
2537 }
2538
2539 /* Return the number of template headers we expect to see for a definition
2540 or specialization of CTYPE or one of its non-template members. */
2541
2542 int
2543 num_template_headers_for_class (tree ctype)
2544 {
2545 int num_templates = 0;
2546
2547 while (ctype && CLASS_TYPE_P (ctype))
2548 {
2549 /* You're supposed to have one `template <...>' for every
2550 template class, but you don't need one for a full
2551 specialization. For example:
2552
2553 template <class T> struct S{};
2554 template <> struct S<int> { void f(); };
2555 void S<int>::f () {}
2556
2557 is correct; there shouldn't be a `template <>' for the
2558 definition of `S<int>::f'. */
2559 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2560 /* If CTYPE does not have template information of any
2561 kind, then it is not a template, nor is it nested
2562 within a template. */
2563 break;
2564 if (explicit_class_specialization_p (ctype))
2565 break;
2566 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2567 ++num_templates;
2568
2569 ctype = TYPE_CONTEXT (ctype);
2570 }
2571
2572 return num_templates;
2573 }
2574
2575 /* Do a simple sanity check on the template headers that precede the
2576 variable declaration DECL. */
2577
2578 void
2579 check_template_variable (tree decl)
2580 {
2581 tree ctx = CP_DECL_CONTEXT (decl);
2582 int wanted = num_template_headers_for_class (ctx);
2583 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2584 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2585 {
2586 if (cxx_dialect < cxx14)
2587 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2588 "variable templates only available with "
2589 "-std=c++14 or -std=gnu++14");
2590
2591 // Namespace-scope variable templates should have a template header.
2592 ++wanted;
2593 }
2594 if (template_header_count > wanted)
2595 {
2596 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2597 "too many template headers for %qD "
2598 "(should be %d)",
2599 decl, wanted);
2600 if (warned && CLASS_TYPE_P (ctx)
2601 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2602 inform (DECL_SOURCE_LOCATION (decl),
2603 "members of an explicitly specialized class are defined "
2604 "without a template header");
2605 }
2606 }
2607
2608 /* An explicit specialization whose declarator-id or class-head-name is not
2609 qualified shall be declared in the nearest enclosing namespace of the
2610 template, or, if the namespace is inline (7.3.1), any namespace from its
2611 enclosing namespace set.
2612
2613 If the name declared in the explicit instantiation is an unqualified name,
2614 the explicit instantiation shall appear in the namespace where its template
2615 is declared or, if that namespace is inline (7.3.1), any namespace from its
2616 enclosing namespace set. */
2617
2618 void
2619 check_unqualified_spec_or_inst (tree t, location_t loc)
2620 {
2621 tree tmpl = most_general_template (t);
2622 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2623 && !is_nested_namespace (current_namespace,
2624 CP_DECL_CONTEXT (tmpl), true))
2625 {
2626 if (processing_specialization)
2627 permerror (loc, "explicit specialization of %qD outside its "
2628 "namespace must use a nested-name-specifier", tmpl);
2629 else if (processing_explicit_instantiation
2630 && cxx_dialect >= cxx11)
2631 /* This was allowed in C++98, so only pedwarn. */
2632 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2633 "outside its namespace must use a nested-name-"
2634 "specifier", tmpl);
2635 }
2636 }
2637
2638 /* Warn for a template specialization SPEC that is missing some of a set
2639 of function or type attributes that the template TEMPL is declared with.
2640 ATTRLIST is a list of additional attributes that SPEC should be taken
2641 to ultimately be declared with. */
2642
2643 static void
2644 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2645 {
2646 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2647 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2648
2649 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2650 return;
2651
2652 /* Avoid warning if either declaration or its type is deprecated. */
2653 if (TREE_DEPRECATED (tmpl)
2654 || TREE_DEPRECATED (spec))
2655 return;
2656
2657 tree tmpl_type = TREE_TYPE (tmpl);
2658 tree spec_type = TREE_TYPE (spec);
2659
2660 if (TREE_DEPRECATED (tmpl_type)
2661 || TREE_DEPRECATED (spec_type)
2662 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2663 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2664 return;
2665
2666 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2667 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2668
2669 if (!spec_attrs[0])
2670 spec_attrs[0] = attrlist;
2671 else if (!spec_attrs[1])
2672 spec_attrs[1] = attrlist;
2673
2674 /* Avoid warning if the primary has no attributes. */
2675 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2676 return;
2677
2678 /* Avoid warning if either declaration contains an attribute on
2679 the white list below. */
2680 const char* const whitelist[] = {
2681 "error", "warning"
2682 };
2683
2684 for (unsigned i = 0; i != 2; ++i)
2685 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2686 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2687 || lookup_attribute (whitelist[j], spec_attrs[i]))
2688 return;
2689
2690 /* Avoid warning if the difference between the primary and
2691 the specialization is not in one of the attributes below. */
2692 const char* const blacklist[] = {
2693 "alloc_align", "alloc_size", "assume_aligned", "format",
2694 "format_arg", "malloc", "nonnull"
2695 };
2696
2697 /* Put together a list of the black listed attributes that the primary
2698 template is declared with that the specialization is not, in case
2699 it's not apparent from the most recent declaration of the primary. */
2700 unsigned nattrs = 0;
2701 pretty_printer str;
2702
2703 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2704 {
2705 for (unsigned j = 0; j != 2; ++j)
2706 {
2707 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2708 continue;
2709
2710 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2711 {
2712 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2713 break;
2714
2715 if (nattrs)
2716 pp_string (&str, ", ");
2717 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2718 pp_string (&str, blacklist[i]);
2719 pp_end_quote (&str, pp_show_color (global_dc->printer));
2720 ++nattrs;
2721 }
2722 }
2723 }
2724
2725 if (!nattrs)
2726 return;
2727
2728 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2729 "explicit specialization %q#D may be missing attributes",
2730 spec))
2731 inform (DECL_SOURCE_LOCATION (tmpl),
2732 nattrs > 1
2733 ? G_("missing primary template attributes %s")
2734 : G_("missing primary template attribute %s"),
2735 pp_formatted_text (&str));
2736 }
2737
2738 /* Check to see if the function just declared, as indicated in
2739 DECLARATOR, and in DECL, is a specialization of a function
2740 template. We may also discover that the declaration is an explicit
2741 instantiation at this point.
2742
2743 Returns DECL, or an equivalent declaration that should be used
2744 instead if all goes well. Issues an error message if something is
2745 amiss. Returns error_mark_node if the error is not easily
2746 recoverable.
2747
2748 FLAGS is a bitmask consisting of the following flags:
2749
2750 2: The function has a definition.
2751 4: The function is a friend.
2752
2753 The TEMPLATE_COUNT is the number of references to qualifying
2754 template classes that appeared in the name of the function. For
2755 example, in
2756
2757 template <class T> struct S { void f(); };
2758 void S<int>::f();
2759
2760 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2761 classes are not counted in the TEMPLATE_COUNT, so that in
2762
2763 template <class T> struct S {};
2764 template <> struct S<int> { void f(); }
2765 template <> void S<int>::f();
2766
2767 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2768 invalid; there should be no template <>.)
2769
2770 If the function is a specialization, it is marked as such via
2771 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2772 is set up correctly, and it is added to the list of specializations
2773 for that template. */
2774
2775 tree
2776 check_explicit_specialization (tree declarator,
2777 tree decl,
2778 int template_count,
2779 int flags,
2780 tree attrlist)
2781 {
2782 int have_def = flags & 2;
2783 int is_friend = flags & 4;
2784 bool is_concept = flags & 8;
2785 int specialization = 0;
2786 int explicit_instantiation = 0;
2787 int member_specialization = 0;
2788 tree ctype = DECL_CLASS_CONTEXT (decl);
2789 tree dname = DECL_NAME (decl);
2790 tmpl_spec_kind tsk;
2791
2792 if (is_friend)
2793 {
2794 if (!processing_specialization)
2795 tsk = tsk_none;
2796 else
2797 tsk = tsk_excessive_parms;
2798 }
2799 else
2800 tsk = current_tmpl_spec_kind (template_count);
2801
2802 switch (tsk)
2803 {
2804 case tsk_none:
2805 if (processing_specialization && !VAR_P (decl))
2806 {
2807 specialization = 1;
2808 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2809 }
2810 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2811 {
2812 if (is_friend)
2813 /* This could be something like:
2814
2815 template <class T> void f(T);
2816 class S { friend void f<>(int); } */
2817 specialization = 1;
2818 else
2819 {
2820 /* This case handles bogus declarations like template <>
2821 template <class T> void f<int>(); */
2822
2823 error ("template-id %qD in declaration of primary template",
2824 declarator);
2825 return decl;
2826 }
2827 }
2828 break;
2829
2830 case tsk_invalid_member_spec:
2831 /* The error has already been reported in
2832 check_specialization_scope. */
2833 return error_mark_node;
2834
2835 case tsk_invalid_expl_inst:
2836 error ("template parameter list used in explicit instantiation");
2837
2838 /* Fall through. */
2839
2840 case tsk_expl_inst:
2841 if (have_def)
2842 error ("definition provided for explicit instantiation");
2843
2844 explicit_instantiation = 1;
2845 break;
2846
2847 case tsk_excessive_parms:
2848 case tsk_insufficient_parms:
2849 if (tsk == tsk_excessive_parms)
2850 error ("too many template parameter lists in declaration of %qD",
2851 decl);
2852 else if (template_header_count)
2853 error("too few template parameter lists in declaration of %qD", decl);
2854 else
2855 error("explicit specialization of %qD must be introduced by "
2856 "%<template <>%>", decl);
2857
2858 /* Fall through. */
2859 case tsk_expl_spec:
2860 if (is_concept)
2861 error ("explicit specialization declared %<concept%>");
2862
2863 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2864 /* In cases like template<> constexpr bool v = true;
2865 We'll give an error in check_template_variable. */
2866 break;
2867
2868 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2869 if (ctype)
2870 member_specialization = 1;
2871 else
2872 specialization = 1;
2873 break;
2874
2875 case tsk_template:
2876 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2877 {
2878 /* This case handles bogus declarations like template <>
2879 template <class T> void f<int>(); */
2880
2881 if (!uses_template_parms (declarator))
2882 error ("template-id %qD in declaration of primary template",
2883 declarator);
2884 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2885 {
2886 /* Partial specialization of variable template. */
2887 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2888 specialization = 1;
2889 goto ok;
2890 }
2891 else if (cxx_dialect < cxx14)
2892 error ("non-type partial specialization %qD "
2893 "is not allowed", declarator);
2894 else
2895 error ("non-class, non-variable partial specialization %qD "
2896 "is not allowed", declarator);
2897 return decl;
2898 ok:;
2899 }
2900
2901 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2902 /* This is a specialization of a member template, without
2903 specialization the containing class. Something like:
2904
2905 template <class T> struct S {
2906 template <class U> void f (U);
2907 };
2908 template <> template <class U> void S<int>::f(U) {}
2909
2910 That's a specialization -- but of the entire template. */
2911 specialization = 1;
2912 break;
2913
2914 default:
2915 gcc_unreachable ();
2916 }
2917
2918 if ((specialization || member_specialization)
2919 /* This doesn't apply to variable templates. */
2920 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2921 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2922 {
2923 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2924 for (; t; t = TREE_CHAIN (t))
2925 if (TREE_PURPOSE (t))
2926 {
2927 permerror (input_location,
2928 "default argument specified in explicit specialization");
2929 break;
2930 }
2931 }
2932
2933 if (specialization || member_specialization || explicit_instantiation)
2934 {
2935 tree tmpl = NULL_TREE;
2936 tree targs = NULL_TREE;
2937 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2938
2939 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2940 if (!was_template_id)
2941 {
2942 tree fns;
2943
2944 gcc_assert (identifier_p (declarator));
2945 if (ctype)
2946 fns = dname;
2947 else
2948 {
2949 /* If there is no class context, the explicit instantiation
2950 must be at namespace scope. */
2951 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2952
2953 /* Find the namespace binding, using the declaration
2954 context. */
2955 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2956 false, true);
2957 if (fns == error_mark_node)
2958 /* If lookup fails, look for a friend declaration so we can
2959 give a better diagnostic. */
2960 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2961 /*type*/false, /*complain*/true,
2962 /*hidden*/true);
2963
2964 if (fns == error_mark_node || !is_overloaded_fn (fns))
2965 {
2966 error ("%qD is not a template function", dname);
2967 fns = error_mark_node;
2968 }
2969 }
2970
2971 declarator = lookup_template_function (fns, NULL_TREE);
2972 }
2973
2974 if (declarator == error_mark_node)
2975 return error_mark_node;
2976
2977 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2978 {
2979 if (!explicit_instantiation)
2980 /* A specialization in class scope. This is invalid,
2981 but the error will already have been flagged by
2982 check_specialization_scope. */
2983 return error_mark_node;
2984 else
2985 {
2986 /* It's not valid to write an explicit instantiation in
2987 class scope, e.g.:
2988
2989 class C { template void f(); }
2990
2991 This case is caught by the parser. However, on
2992 something like:
2993
2994 template class C { void f(); };
2995
2996 (which is invalid) we can get here. The error will be
2997 issued later. */
2998 ;
2999 }
3000
3001 return decl;
3002 }
3003 else if (ctype != NULL_TREE
3004 && (identifier_p (TREE_OPERAND (declarator, 0))))
3005 {
3006 // We'll match variable templates in start_decl.
3007 if (VAR_P (decl))
3008 return decl;
3009
3010 /* Find the list of functions in ctype that have the same
3011 name as the declared function. */
3012 tree name = TREE_OPERAND (declarator, 0);
3013
3014 if (constructor_name_p (name, ctype))
3015 {
3016 if (DECL_CONSTRUCTOR_P (decl)
3017 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3018 : !CLASSTYPE_DESTRUCTOR (ctype))
3019 {
3020 /* From [temp.expl.spec]:
3021
3022 If such an explicit specialization for the member
3023 of a class template names an implicitly-declared
3024 special member function (clause _special_), the
3025 program is ill-formed.
3026
3027 Similar language is found in [temp.explicit]. */
3028 error ("specialization of implicitly-declared special member function");
3029 return error_mark_node;
3030 }
3031
3032 name = DECL_NAME (decl);
3033 }
3034
3035 /* For a type-conversion operator, We might be looking for
3036 `operator int' which will be a specialization of
3037 `operator T'. Grab all the conversion operators, and
3038 then select from them. */
3039 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3040 ? conv_op_identifier : name);
3041
3042 if (fns == NULL_TREE)
3043 {
3044 error ("no member function %qD declared in %qT", name, ctype);
3045 return error_mark_node;
3046 }
3047 else
3048 TREE_OPERAND (declarator, 0) = fns;
3049 }
3050
3051 /* Figure out what exactly is being specialized at this point.
3052 Note that for an explicit instantiation, even one for a
3053 member function, we cannot tell a priori whether the
3054 instantiation is for a member template, or just a member
3055 function of a template class. Even if a member template is
3056 being instantiated, the member template arguments may be
3057 elided if they can be deduced from the rest of the
3058 declaration. */
3059 tmpl = determine_specialization (declarator, decl,
3060 &targs,
3061 member_specialization,
3062 template_count,
3063 tsk);
3064
3065 if (!tmpl || tmpl == error_mark_node)
3066 /* We couldn't figure out what this declaration was
3067 specializing. */
3068 return error_mark_node;
3069 else
3070 {
3071 if (TREE_CODE (decl) == FUNCTION_DECL
3072 && DECL_HIDDEN_FRIEND_P (tmpl))
3073 {
3074 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3075 "friend declaration %qD is not visible to "
3076 "explicit specialization", tmpl))
3077 inform (DECL_SOURCE_LOCATION (tmpl),
3078 "friend declaration here");
3079 }
3080 else if (!ctype && !is_friend
3081 && CP_DECL_CONTEXT (decl) == current_namespace)
3082 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3083
3084 tree gen_tmpl = most_general_template (tmpl);
3085
3086 if (explicit_instantiation)
3087 {
3088 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3089 is done by do_decl_instantiation later. */
3090
3091 int arg_depth = TMPL_ARGS_DEPTH (targs);
3092 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3093
3094 if (arg_depth > parm_depth)
3095 {
3096 /* If TMPL is not the most general template (for
3097 example, if TMPL is a friend template that is
3098 injected into namespace scope), then there will
3099 be too many levels of TARGS. Remove some of them
3100 here. */
3101 int i;
3102 tree new_targs;
3103
3104 new_targs = make_tree_vec (parm_depth);
3105 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3106 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3107 = TREE_VEC_ELT (targs, i);
3108 targs = new_targs;
3109 }
3110
3111 return instantiate_template (tmpl, targs, tf_error);
3112 }
3113
3114 /* If we thought that the DECL was a member function, but it
3115 turns out to be specializing a static member function,
3116 make DECL a static member function as well. */
3117 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3118 && DECL_STATIC_FUNCTION_P (tmpl)
3119 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3120 revert_static_member_fn (decl);
3121
3122 /* If this is a specialization of a member template of a
3123 template class, we want to return the TEMPLATE_DECL, not
3124 the specialization of it. */
3125 if (tsk == tsk_template && !was_template_id)
3126 {
3127 tree result = DECL_TEMPLATE_RESULT (tmpl);
3128 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3129 DECL_INITIAL (result) = NULL_TREE;
3130 if (have_def)
3131 {
3132 tree parm;
3133 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3134 DECL_SOURCE_LOCATION (result)
3135 = DECL_SOURCE_LOCATION (decl);
3136 /* We want to use the argument list specified in the
3137 definition, not in the original declaration. */
3138 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3139 for (parm = DECL_ARGUMENTS (result); parm;
3140 parm = DECL_CHAIN (parm))
3141 DECL_CONTEXT (parm) = result;
3142 }
3143 return register_specialization (tmpl, gen_tmpl, targs,
3144 is_friend, 0);
3145 }
3146
3147 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3148 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3149
3150 if (was_template_id)
3151 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3152
3153 /* Inherit default function arguments from the template
3154 DECL is specializing. */
3155 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3156 copy_default_args_to_explicit_spec (decl);
3157
3158 /* This specialization has the same protection as the
3159 template it specializes. */
3160 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3161 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3162
3163 /* 7.1.1-1 [dcl.stc]
3164
3165 A storage-class-specifier shall not be specified in an
3166 explicit specialization...
3167
3168 The parser rejects these, so unless action is taken here,
3169 explicit function specializations will always appear with
3170 global linkage.
3171
3172 The action recommended by the C++ CWG in response to C++
3173 defect report 605 is to make the storage class and linkage
3174 of the explicit specialization match the templated function:
3175
3176 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3177 */
3178 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3179 {
3180 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3181 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3182
3183 /* A concept cannot be specialized. */
3184 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3185 {
3186 error ("explicit specialization of function concept %qD",
3187 gen_tmpl);
3188 return error_mark_node;
3189 }
3190
3191 /* This specialization has the same linkage and visibility as
3192 the function template it specializes. */
3193 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3194 if (! TREE_PUBLIC (decl))
3195 {
3196 DECL_INTERFACE_KNOWN (decl) = 1;
3197 DECL_NOT_REALLY_EXTERN (decl) = 1;
3198 }
3199 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3200 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3201 {
3202 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3203 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3204 }
3205 }
3206
3207 /* If DECL is a friend declaration, declared using an
3208 unqualified name, the namespace associated with DECL may
3209 have been set incorrectly. For example, in:
3210
3211 template <typename T> void f(T);
3212 namespace N {
3213 struct S { friend void f<int>(int); }
3214 }
3215
3216 we will have set the DECL_CONTEXT for the friend
3217 declaration to N, rather than to the global namespace. */
3218 if (DECL_NAMESPACE_SCOPE_P (decl))
3219 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3220
3221 if (is_friend && !have_def)
3222 /* This is not really a declaration of a specialization.
3223 It's just the name of an instantiation. But, it's not
3224 a request for an instantiation, either. */
3225 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3226 else if (TREE_CODE (decl) == FUNCTION_DECL)
3227 /* A specialization is not necessarily COMDAT. */
3228 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3229 && DECL_DECLARED_INLINE_P (decl));
3230 else if (VAR_P (decl))
3231 DECL_COMDAT (decl) = false;
3232
3233 /* If this is a full specialization, register it so that we can find
3234 it again. Partial specializations will be registered in
3235 process_partial_specialization. */
3236 if (!processing_template_decl)
3237 {
3238 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3239
3240 decl = register_specialization (decl, gen_tmpl, targs,
3241 is_friend, 0);
3242 }
3243
3244
3245 /* A 'structor should already have clones. */
3246 gcc_assert (decl == error_mark_node
3247 || variable_template_p (tmpl)
3248 || !(DECL_CONSTRUCTOR_P (decl)
3249 || DECL_DESTRUCTOR_P (decl))
3250 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3251 }
3252 }
3253
3254 return decl;
3255 }
3256
3257 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3258 parameters. These are represented in the same format used for
3259 DECL_TEMPLATE_PARMS. */
3260
3261 int
3262 comp_template_parms (const_tree parms1, const_tree parms2)
3263 {
3264 const_tree p1;
3265 const_tree p2;
3266
3267 if (parms1 == parms2)
3268 return 1;
3269
3270 for (p1 = parms1, p2 = parms2;
3271 p1 != NULL_TREE && p2 != NULL_TREE;
3272 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3273 {
3274 tree t1 = TREE_VALUE (p1);
3275 tree t2 = TREE_VALUE (p2);
3276 int i;
3277
3278 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3279 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3280
3281 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3282 return 0;
3283
3284 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3285 {
3286 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3287 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3288
3289 /* If either of the template parameters are invalid, assume
3290 they match for the sake of error recovery. */
3291 if (error_operand_p (parm1) || error_operand_p (parm2))
3292 return 1;
3293
3294 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3295 return 0;
3296
3297 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3298 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3299 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3300 continue;
3301 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3302 return 0;
3303 }
3304 }
3305
3306 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3307 /* One set of parameters has more parameters lists than the
3308 other. */
3309 return 0;
3310
3311 return 1;
3312 }
3313
3314 /* Determine whether PARM is a parameter pack. */
3315
3316 bool
3317 template_parameter_pack_p (const_tree parm)
3318 {
3319 /* Determine if we have a non-type template parameter pack. */
3320 if (TREE_CODE (parm) == PARM_DECL)
3321 return (DECL_TEMPLATE_PARM_P (parm)
3322 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3323 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3324 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3325
3326 /* If this is a list of template parameters, we could get a
3327 TYPE_DECL or a TEMPLATE_DECL. */
3328 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3329 parm = TREE_TYPE (parm);
3330
3331 /* Otherwise it must be a type template parameter. */
3332 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3333 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3334 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3335 }
3336
3337 /* Determine if T is a function parameter pack. */
3338
3339 bool
3340 function_parameter_pack_p (const_tree t)
3341 {
3342 if (t && TREE_CODE (t) == PARM_DECL)
3343 return DECL_PACK_P (t);
3344 return false;
3345 }
3346
3347 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3348 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3349
3350 tree
3351 get_function_template_decl (const_tree primary_func_tmpl_inst)
3352 {
3353 if (! primary_func_tmpl_inst
3354 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3355 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3356 return NULL;
3357
3358 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3359 }
3360
3361 /* Return true iff the function parameter PARAM_DECL was expanded
3362 from the function parameter pack PACK. */
3363
3364 bool
3365 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3366 {
3367 if (DECL_ARTIFICIAL (param_decl)
3368 || !function_parameter_pack_p (pack))
3369 return false;
3370
3371 /* The parameter pack and its pack arguments have the same
3372 DECL_PARM_INDEX. */
3373 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3374 }
3375
3376 /* Determine whether ARGS describes a variadic template args list,
3377 i.e., one that is terminated by a template argument pack. */
3378
3379 static bool
3380 template_args_variadic_p (tree args)
3381 {
3382 int nargs;
3383 tree last_parm;
3384
3385 if (args == NULL_TREE)
3386 return false;
3387
3388 args = INNERMOST_TEMPLATE_ARGS (args);
3389 nargs = TREE_VEC_LENGTH (args);
3390
3391 if (nargs == 0)
3392 return false;
3393
3394 last_parm = TREE_VEC_ELT (args, nargs - 1);
3395
3396 return ARGUMENT_PACK_P (last_parm);
3397 }
3398
3399 /* Generate a new name for the parameter pack name NAME (an
3400 IDENTIFIER_NODE) that incorporates its */
3401
3402 static tree
3403 make_ith_pack_parameter_name (tree name, int i)
3404 {
3405 /* Munge the name to include the parameter index. */
3406 #define NUMBUF_LEN 128
3407 char numbuf[NUMBUF_LEN];
3408 char* newname;
3409 int newname_len;
3410
3411 if (name == NULL_TREE)
3412 return name;
3413 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3414 newname_len = IDENTIFIER_LENGTH (name)
3415 + strlen (numbuf) + 2;
3416 newname = (char*)alloca (newname_len);
3417 snprintf (newname, newname_len,
3418 "%s#%i", IDENTIFIER_POINTER (name), i);
3419 return get_identifier (newname);
3420 }
3421
3422 /* Return true if T is a primary function, class or alias template
3423 specialization, not including the template pattern. */
3424
3425 bool
3426 primary_template_specialization_p (const_tree t)
3427 {
3428 if (!t)
3429 return false;
3430
3431 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3432 return (DECL_LANG_SPECIFIC (t)
3433 && DECL_USE_TEMPLATE (t)
3434 && DECL_TEMPLATE_INFO (t)
3435 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3436 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3437 return (CLASSTYPE_TEMPLATE_INFO (t)
3438 && CLASSTYPE_USE_TEMPLATE (t)
3439 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3440 else if (alias_template_specialization_p (t))
3441 return true;
3442 return false;
3443 }
3444
3445 /* Return true if PARM is a template template parameter. */
3446
3447 bool
3448 template_template_parameter_p (const_tree parm)
3449 {
3450 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3451 }
3452
3453 /* Return true iff PARM is a DECL representing a type template
3454 parameter. */
3455
3456 bool
3457 template_type_parameter_p (const_tree parm)
3458 {
3459 return (parm
3460 && (TREE_CODE (parm) == TYPE_DECL
3461 || TREE_CODE (parm) == TEMPLATE_DECL)
3462 && DECL_TEMPLATE_PARM_P (parm));
3463 }
3464
3465 /* Return the template parameters of T if T is a
3466 primary template instantiation, NULL otherwise. */
3467
3468 tree
3469 get_primary_template_innermost_parameters (const_tree t)
3470 {
3471 tree parms = NULL, template_info = NULL;
3472
3473 if ((template_info = get_template_info (t))
3474 && primary_template_specialization_p (t))
3475 parms = INNERMOST_TEMPLATE_PARMS
3476 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3477
3478 return parms;
3479 }
3480
3481 /* Return the template parameters of the LEVELth level from the full list
3482 of template parameters PARMS. */
3483
3484 tree
3485 get_template_parms_at_level (tree parms, int level)
3486 {
3487 tree p;
3488 if (!parms
3489 || TREE_CODE (parms) != TREE_LIST
3490 || level > TMPL_PARMS_DEPTH (parms))
3491 return NULL_TREE;
3492
3493 for (p = parms; p; p = TREE_CHAIN (p))
3494 if (TMPL_PARMS_DEPTH (p) == level)
3495 return p;
3496
3497 return NULL_TREE;
3498 }
3499
3500 /* Returns the template arguments of T if T is a template instantiation,
3501 NULL otherwise. */
3502
3503 tree
3504 get_template_innermost_arguments (const_tree t)
3505 {
3506 tree args = NULL, template_info = NULL;
3507
3508 if ((template_info = get_template_info (t))
3509 && TI_ARGS (template_info))
3510 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3511
3512 return args;
3513 }
3514
3515 /* Return the argument pack elements of T if T is a template argument pack,
3516 NULL otherwise. */
3517
3518 tree
3519 get_template_argument_pack_elems (const_tree t)
3520 {
3521 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3522 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3523 return NULL;
3524
3525 return ARGUMENT_PACK_ARGS (t);
3526 }
3527
3528 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3529 ARGUMENT_PACK_SELECT represents. */
3530
3531 static tree
3532 argument_pack_select_arg (tree t)
3533 {
3534 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3535 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3536
3537 /* If the selected argument is an expansion E, that most likely means we were
3538 called from gen_elem_of_pack_expansion_instantiation during the
3539 substituting of an argument pack (of which the Ith element is a pack
3540 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3541 In this case, the Ith element resulting from this substituting is going to
3542 be a pack expansion, which pattern is the pattern of E. Let's return the
3543 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3544 resulting pack expansion from it. */
3545 if (PACK_EXPANSION_P (arg))
3546 {
3547 /* Make sure we aren't throwing away arg info. */
3548 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3549 arg = PACK_EXPANSION_PATTERN (arg);
3550 }
3551
3552 return arg;
3553 }
3554
3555
3556 /* True iff FN is a function representing a built-in variadic parameter
3557 pack. */
3558
3559 bool
3560 builtin_pack_fn_p (tree fn)
3561 {
3562 if (!fn
3563 || TREE_CODE (fn) != FUNCTION_DECL
3564 || !DECL_IS_BUILTIN (fn))
3565 return false;
3566
3567 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3568 return true;
3569
3570 return false;
3571 }
3572
3573 /* True iff CALL is a call to a function representing a built-in variadic
3574 parameter pack. */
3575
3576 static bool
3577 builtin_pack_call_p (tree call)
3578 {
3579 if (TREE_CODE (call) != CALL_EXPR)
3580 return false;
3581 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3582 }
3583
3584 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3585
3586 static tree
3587 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3588 tree in_decl)
3589 {
3590 tree ohi = CALL_EXPR_ARG (call, 0);
3591 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3592 false/*fn*/, true/*int_cst*/);
3593
3594 if (value_dependent_expression_p (hi))
3595 {
3596 if (hi != ohi)
3597 {
3598 call = copy_node (call);
3599 CALL_EXPR_ARG (call, 0) = hi;
3600 }
3601 tree ex = make_pack_expansion (call, complain);
3602 tree vec = make_tree_vec (1);
3603 TREE_VEC_ELT (vec, 0) = ex;
3604 return vec;
3605 }
3606 else
3607 {
3608 hi = cxx_constant_value (hi);
3609 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3610
3611 /* Calculate the largest value of len that won't make the size of the vec
3612 overflow an int. The compiler will exceed resource limits long before
3613 this, but it seems a decent place to diagnose. */
3614 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3615
3616 if (len < 0 || len > max)
3617 {
3618 if ((complain & tf_error)
3619 && hi != error_mark_node)
3620 error ("argument to __integer_pack must be between 0 and %d", max);
3621 return error_mark_node;
3622 }
3623
3624 tree vec = make_tree_vec (len);
3625
3626 for (int i = 0; i < len; ++i)
3627 TREE_VEC_ELT (vec, i) = size_int (i);
3628
3629 return vec;
3630 }
3631 }
3632
3633 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3634 CALL. */
3635
3636 static tree
3637 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3638 tree in_decl)
3639 {
3640 if (!builtin_pack_call_p (call))
3641 return NULL_TREE;
3642
3643 tree fn = CALL_EXPR_FN (call);
3644
3645 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3646 return expand_integer_pack (call, args, complain, in_decl);
3647
3648 return NULL_TREE;
3649 }
3650
3651 /* Structure used to track the progress of find_parameter_packs_r. */
3652 struct find_parameter_pack_data
3653 {
3654 /* TREE_LIST that will contain all of the parameter packs found by
3655 the traversal. */
3656 tree* parameter_packs;
3657
3658 /* Set of AST nodes that have been visited by the traversal. */
3659 hash_set<tree> *visited;
3660
3661 /* True iff we're making a type pack expansion. */
3662 bool type_pack_expansion_p;
3663 };
3664
3665 /* Identifies all of the argument packs that occur in a template
3666 argument and appends them to the TREE_LIST inside DATA, which is a
3667 find_parameter_pack_data structure. This is a subroutine of
3668 make_pack_expansion and uses_parameter_packs. */
3669 static tree
3670 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3671 {
3672 tree t = *tp;
3673 struct find_parameter_pack_data* ppd =
3674 (struct find_parameter_pack_data*)data;
3675 bool parameter_pack_p = false;
3676
3677 /* Handle type aliases/typedefs. */
3678 if (TYPE_ALIAS_P (t))
3679 {
3680 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3681 cp_walk_tree (&TI_ARGS (tinfo),
3682 &find_parameter_packs_r,
3683 ppd, ppd->visited);
3684 *walk_subtrees = 0;
3685 return NULL_TREE;
3686 }
3687
3688 /* Identify whether this is a parameter pack or not. */
3689 switch (TREE_CODE (t))
3690 {
3691 case TEMPLATE_PARM_INDEX:
3692 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3693 parameter_pack_p = true;
3694 break;
3695
3696 case TEMPLATE_TYPE_PARM:
3697 t = TYPE_MAIN_VARIANT (t);
3698 /* FALLTHRU */
3699 case TEMPLATE_TEMPLATE_PARM:
3700 /* If the placeholder appears in the decl-specifier-seq of a function
3701 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3702 is a pack expansion, the invented template parameter is a template
3703 parameter pack. */
3704 if (ppd->type_pack_expansion_p && is_auto (t))
3705 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3706 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3707 parameter_pack_p = true;
3708 break;
3709
3710 case FIELD_DECL:
3711 case PARM_DECL:
3712 if (DECL_PACK_P (t))
3713 {
3714 /* We don't want to walk into the type of a PARM_DECL,
3715 because we don't want to see the type parameter pack. */
3716 *walk_subtrees = 0;
3717 parameter_pack_p = true;
3718 }
3719 break;
3720
3721 case VAR_DECL:
3722 if (DECL_PACK_P (t))
3723 {
3724 /* We don't want to walk into the type of a variadic capture proxy,
3725 because we don't want to see the type parameter pack. */
3726 *walk_subtrees = 0;
3727 parameter_pack_p = true;
3728 }
3729 else if (variable_template_specialization_p (t))
3730 {
3731 cp_walk_tree (&DECL_TI_ARGS (t),
3732 find_parameter_packs_r,
3733 ppd, ppd->visited);
3734 *walk_subtrees = 0;
3735 }
3736 break;
3737
3738 case CALL_EXPR:
3739 if (builtin_pack_call_p (t))
3740 parameter_pack_p = true;
3741 break;
3742
3743 case BASES:
3744 parameter_pack_p = true;
3745 break;
3746 default:
3747 /* Not a parameter pack. */
3748 break;
3749 }
3750
3751 if (parameter_pack_p)
3752 {
3753 /* Add this parameter pack to the list. */
3754 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3755 }
3756
3757 if (TYPE_P (t))
3758 cp_walk_tree (&TYPE_CONTEXT (t),
3759 &find_parameter_packs_r, ppd, ppd->visited);
3760
3761 /* This switch statement will return immediately if we don't find a
3762 parameter pack. */
3763 switch (TREE_CODE (t))
3764 {
3765 case TEMPLATE_PARM_INDEX:
3766 return NULL_TREE;
3767
3768 case BOUND_TEMPLATE_TEMPLATE_PARM:
3769 /* Check the template itself. */
3770 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3771 &find_parameter_packs_r, ppd, ppd->visited);
3772 /* Check the template arguments. */
3773 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3774 ppd->visited);
3775 *walk_subtrees = 0;
3776 return NULL_TREE;
3777
3778 case TEMPLATE_TYPE_PARM:
3779 case TEMPLATE_TEMPLATE_PARM:
3780 return NULL_TREE;
3781
3782 case PARM_DECL:
3783 return NULL_TREE;
3784
3785 case DECL_EXPR:
3786 /* Ignore the declaration of a capture proxy for a parameter pack. */
3787 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3788 *walk_subtrees = 0;
3789 return NULL_TREE;
3790
3791 case RECORD_TYPE:
3792 if (TYPE_PTRMEMFUNC_P (t))
3793 return NULL_TREE;
3794 /* Fall through. */
3795
3796 case UNION_TYPE:
3797 case ENUMERAL_TYPE:
3798 if (TYPE_TEMPLATE_INFO (t))
3799 cp_walk_tree (&TYPE_TI_ARGS (t),
3800 &find_parameter_packs_r, ppd, ppd->visited);
3801
3802 *walk_subtrees = 0;
3803 return NULL_TREE;
3804
3805 case TEMPLATE_DECL:
3806 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3807 return NULL_TREE;
3808 gcc_fallthrough();
3809
3810 case CONSTRUCTOR:
3811 cp_walk_tree (&TREE_TYPE (t),
3812 &find_parameter_packs_r, ppd, ppd->visited);
3813 return NULL_TREE;
3814
3815 case TYPENAME_TYPE:
3816 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3817 ppd, ppd->visited);
3818 *walk_subtrees = 0;
3819 return NULL_TREE;
3820
3821 case TYPE_PACK_EXPANSION:
3822 case EXPR_PACK_EXPANSION:
3823 *walk_subtrees = 0;
3824 return NULL_TREE;
3825
3826 case INTEGER_TYPE:
3827 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3828 ppd, ppd->visited);
3829 *walk_subtrees = 0;
3830 return NULL_TREE;
3831
3832 case IDENTIFIER_NODE:
3833 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3834 ppd->visited);
3835 *walk_subtrees = 0;
3836 return NULL_TREE;
3837
3838 case LAMBDA_EXPR:
3839 {
3840 /* Look at explicit captures. */
3841 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3842 cap; cap = TREE_CHAIN (cap))
3843 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3844 ppd->visited);
3845 /* Since we defer implicit capture, look in the body as well. */
3846 tree fn = lambda_function (t);
3847 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3848 ppd->visited);
3849 *walk_subtrees = 0;
3850 return NULL_TREE;
3851 }
3852
3853 case DECLTYPE_TYPE:
3854 {
3855 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3856 type_pack_expansion_p to false so that any placeholders
3857 within the expression don't get marked as parameter packs. */
3858 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3859 ppd->type_pack_expansion_p = false;
3860 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3861 ppd, ppd->visited);
3862 ppd->type_pack_expansion_p = type_pack_expansion_p;
3863 *walk_subtrees = 0;
3864 return NULL_TREE;
3865 }
3866
3867 default:
3868 return NULL_TREE;
3869 }
3870
3871 return NULL_TREE;
3872 }
3873
3874 /* Determines if the expression or type T uses any parameter packs. */
3875 bool
3876 uses_parameter_packs (tree t)
3877 {
3878 tree parameter_packs = NULL_TREE;
3879 struct find_parameter_pack_data ppd;
3880 ppd.parameter_packs = &parameter_packs;
3881 ppd.visited = new hash_set<tree>;
3882 ppd.type_pack_expansion_p = false;
3883 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3884 delete ppd.visited;
3885 return parameter_packs != NULL_TREE;
3886 }
3887
3888 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3889 representation a base-class initializer into a parameter pack
3890 expansion. If all goes well, the resulting node will be an
3891 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3892 respectively. */
3893 tree
3894 make_pack_expansion (tree arg, tsubst_flags_t complain)
3895 {
3896 tree result;
3897 tree parameter_packs = NULL_TREE;
3898 bool for_types = false;
3899 struct find_parameter_pack_data ppd;
3900
3901 if (!arg || arg == error_mark_node)
3902 return arg;
3903
3904 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3905 {
3906 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3907 class initializer. In this case, the TREE_PURPOSE will be a
3908 _TYPE node (representing the base class expansion we're
3909 initializing) and the TREE_VALUE will be a TREE_LIST
3910 containing the initialization arguments.
3911
3912 The resulting expansion looks somewhat different from most
3913 expansions. Rather than returning just one _EXPANSION, we
3914 return a TREE_LIST whose TREE_PURPOSE is a
3915 TYPE_PACK_EXPANSION containing the bases that will be
3916 initialized. The TREE_VALUE will be identical to the
3917 original TREE_VALUE, which is a list of arguments that will
3918 be passed to each base. We do not introduce any new pack
3919 expansion nodes into the TREE_VALUE (although it is possible
3920 that some already exist), because the TREE_PURPOSE and
3921 TREE_VALUE all need to be expanded together with the same
3922 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3923 resulting TREE_PURPOSE will mention the parameter packs in
3924 both the bases and the arguments to the bases. */
3925 tree purpose;
3926 tree value;
3927 tree parameter_packs = NULL_TREE;
3928
3929 /* Determine which parameter packs will be used by the base
3930 class expansion. */
3931 ppd.visited = new hash_set<tree>;
3932 ppd.parameter_packs = &parameter_packs;
3933 ppd.type_pack_expansion_p = true;
3934 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3935 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3936 &ppd, ppd.visited);
3937
3938 if (parameter_packs == NULL_TREE)
3939 {
3940 if (complain & tf_error)
3941 error ("base initializer expansion %qT contains no parameter packs",
3942 arg);
3943 delete ppd.visited;
3944 return error_mark_node;
3945 }
3946
3947 if (TREE_VALUE (arg) != void_type_node)
3948 {
3949 /* Collect the sets of parameter packs used in each of the
3950 initialization arguments. */
3951 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3952 {
3953 /* Determine which parameter packs will be expanded in this
3954 argument. */
3955 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3956 &ppd, ppd.visited);
3957 }
3958 }
3959
3960 delete ppd.visited;
3961
3962 /* Create the pack expansion type for the base type. */
3963 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3964 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3965 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3966 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3967
3968 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3969 they will rarely be compared to anything. */
3970 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3971
3972 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3973 }
3974
3975 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3976 for_types = true;
3977
3978 /* Build the PACK_EXPANSION_* node. */
3979 result = for_types
3980 ? cxx_make_type (TYPE_PACK_EXPANSION)
3981 : make_node (EXPR_PACK_EXPANSION);
3982 SET_PACK_EXPANSION_PATTERN (result, arg);
3983 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3984 {
3985 /* Propagate type and const-expression information. */
3986 TREE_TYPE (result) = TREE_TYPE (arg);
3987 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3988 /* Mark this read now, since the expansion might be length 0. */
3989 mark_exp_read (arg);
3990 }
3991 else
3992 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3993 they will rarely be compared to anything. */
3994 SET_TYPE_STRUCTURAL_EQUALITY (result);
3995
3996 /* Determine which parameter packs will be expanded. */
3997 ppd.parameter_packs = &parameter_packs;
3998 ppd.visited = new hash_set<tree>;
3999 ppd.type_pack_expansion_p = TYPE_P (arg);
4000 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4001 delete ppd.visited;
4002
4003 /* Make sure we found some parameter packs. */
4004 if (parameter_packs == NULL_TREE)
4005 {
4006 if (complain & tf_error)
4007 {
4008 if (TYPE_P (arg))
4009 error ("expansion pattern %qT contains no argument packs", arg);
4010 else
4011 error ("expansion pattern %qE contains no argument packs", arg);
4012 }
4013 return error_mark_node;
4014 }
4015 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4016
4017 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4018
4019 return result;
4020 }
4021
4022 /* Checks T for any "bare" parameter packs, which have not yet been
4023 expanded, and issues an error if any are found. This operation can
4024 only be done on full expressions or types (e.g., an expression
4025 statement, "if" condition, etc.), because we could have expressions like:
4026
4027 foo(f(g(h(args)))...)
4028
4029 where "args" is a parameter pack. check_for_bare_parameter_packs
4030 should not be called for the subexpressions args, h(args),
4031 g(h(args)), or f(g(h(args))), because we would produce erroneous
4032 error messages.
4033
4034 Returns TRUE and emits an error if there were bare parameter packs,
4035 returns FALSE otherwise. */
4036 bool
4037 check_for_bare_parameter_packs (tree t)
4038 {
4039 tree parameter_packs = NULL_TREE;
4040 struct find_parameter_pack_data ppd;
4041
4042 if (!processing_template_decl || !t || t == error_mark_node)
4043 return false;
4044
4045 /* A lambda might use a parameter pack from the containing context. */
4046 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
4047 return false;
4048
4049 if (TREE_CODE (t) == TYPE_DECL)
4050 t = TREE_TYPE (t);
4051
4052 ppd.parameter_packs = &parameter_packs;
4053 ppd.visited = new hash_set<tree>;
4054 ppd.type_pack_expansion_p = false;
4055 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4056 delete ppd.visited;
4057
4058 if (parameter_packs)
4059 {
4060 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
4061 error_at (loc, "parameter packs not expanded with %<...%>:");
4062 while (parameter_packs)
4063 {
4064 tree pack = TREE_VALUE (parameter_packs);
4065 tree name = NULL_TREE;
4066
4067 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4068 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4069 name = TYPE_NAME (pack);
4070 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4071 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4072 else if (TREE_CODE (pack) == CALL_EXPR)
4073 name = DECL_NAME (CALL_EXPR_FN (pack));
4074 else
4075 name = DECL_NAME (pack);
4076
4077 if (name)
4078 inform (loc, " %qD", name);
4079 else
4080 inform (loc, " <anonymous>");
4081
4082 parameter_packs = TREE_CHAIN (parameter_packs);
4083 }
4084
4085 return true;
4086 }
4087
4088 return false;
4089 }
4090
4091 /* Expand any parameter packs that occur in the template arguments in
4092 ARGS. */
4093 tree
4094 expand_template_argument_pack (tree args)
4095 {
4096 if (args == error_mark_node)
4097 return error_mark_node;
4098
4099 tree result_args = NULL_TREE;
4100 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4101 int num_result_args = -1;
4102 int non_default_args_count = -1;
4103
4104 /* First, determine if we need to expand anything, and the number of
4105 slots we'll need. */
4106 for (in_arg = 0; in_arg < nargs; ++in_arg)
4107 {
4108 tree arg = TREE_VEC_ELT (args, in_arg);
4109 if (arg == NULL_TREE)
4110 return args;
4111 if (ARGUMENT_PACK_P (arg))
4112 {
4113 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4114 if (num_result_args < 0)
4115 num_result_args = in_arg + num_packed;
4116 else
4117 num_result_args += num_packed;
4118 }
4119 else
4120 {
4121 if (num_result_args >= 0)
4122 num_result_args++;
4123 }
4124 }
4125
4126 /* If no expansion is necessary, we're done. */
4127 if (num_result_args < 0)
4128 return args;
4129
4130 /* Expand arguments. */
4131 result_args = make_tree_vec (num_result_args);
4132 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4133 non_default_args_count =
4134 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4135 for (in_arg = 0; in_arg < nargs; ++in_arg)
4136 {
4137 tree arg = TREE_VEC_ELT (args, in_arg);
4138 if (ARGUMENT_PACK_P (arg))
4139 {
4140 tree packed = ARGUMENT_PACK_ARGS (arg);
4141 int i, num_packed = TREE_VEC_LENGTH (packed);
4142 for (i = 0; i < num_packed; ++i, ++out_arg)
4143 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4144 if (non_default_args_count > 0)
4145 non_default_args_count += num_packed - 1;
4146 }
4147 else
4148 {
4149 TREE_VEC_ELT (result_args, out_arg) = arg;
4150 ++out_arg;
4151 }
4152 }
4153 if (non_default_args_count >= 0)
4154 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4155 return result_args;
4156 }
4157
4158 /* Checks if DECL shadows a template parameter.
4159
4160 [temp.local]: A template-parameter shall not be redeclared within its
4161 scope (including nested scopes).
4162
4163 Emits an error and returns TRUE if the DECL shadows a parameter,
4164 returns FALSE otherwise. */
4165
4166 bool
4167 check_template_shadow (tree decl)
4168 {
4169 tree olddecl;
4170
4171 /* If we're not in a template, we can't possibly shadow a template
4172 parameter. */
4173 if (!current_template_parms)
4174 return true;
4175
4176 /* Figure out what we're shadowing. */
4177 decl = OVL_FIRST (decl);
4178 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4179
4180 /* If there's no previous binding for this name, we're not shadowing
4181 anything, let alone a template parameter. */
4182 if (!olddecl)
4183 return true;
4184
4185 /* If we're not shadowing a template parameter, we're done. Note
4186 that OLDDECL might be an OVERLOAD (or perhaps even an
4187 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4188 node. */
4189 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4190 return true;
4191
4192 /* We check for decl != olddecl to avoid bogus errors for using a
4193 name inside a class. We check TPFI to avoid duplicate errors for
4194 inline member templates. */
4195 if (decl == olddecl
4196 || (DECL_TEMPLATE_PARM_P (decl)
4197 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4198 return true;
4199
4200 /* Don't complain about the injected class name, as we've already
4201 complained about the class itself. */
4202 if (DECL_SELF_REFERENCE_P (decl))
4203 return false;
4204
4205 if (DECL_TEMPLATE_PARM_P (decl))
4206 error ("declaration of template parameter %q+D shadows "
4207 "template parameter", decl);
4208 else
4209 error ("declaration of %q+#D shadows template parameter", decl);
4210 inform (DECL_SOURCE_LOCATION (olddecl),
4211 "template parameter %qD declared here", olddecl);
4212 return false;
4213 }
4214
4215 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4216 ORIG_LEVEL, DECL, and TYPE. */
4217
4218 static tree
4219 build_template_parm_index (int index,
4220 int level,
4221 int orig_level,
4222 tree decl,
4223 tree type)
4224 {
4225 tree t = make_node (TEMPLATE_PARM_INDEX);
4226 TEMPLATE_PARM_IDX (t) = index;
4227 TEMPLATE_PARM_LEVEL (t) = level;
4228 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4229 TEMPLATE_PARM_DECL (t) = decl;
4230 TREE_TYPE (t) = type;
4231 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4232 TREE_READONLY (t) = TREE_READONLY (decl);
4233
4234 return t;
4235 }
4236
4237 /* Find the canonical type parameter for the given template type
4238 parameter. Returns the canonical type parameter, which may be TYPE
4239 if no such parameter existed. */
4240
4241 static tree
4242 canonical_type_parameter (tree type)
4243 {
4244 tree list;
4245 int idx = TEMPLATE_TYPE_IDX (type);
4246 if (!canonical_template_parms)
4247 vec_alloc (canonical_template_parms, idx + 1);
4248
4249 if (canonical_template_parms->length () <= (unsigned) idx)
4250 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4251
4252 list = (*canonical_template_parms)[idx];
4253 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4254 list = TREE_CHAIN (list);
4255
4256 if (list)
4257 return TREE_VALUE (list);
4258 else
4259 {
4260 (*canonical_template_parms)[idx]
4261 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4262 return type;
4263 }
4264 }
4265
4266 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4267 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4268 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4269 new one is created. */
4270
4271 static tree
4272 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4273 tsubst_flags_t complain)
4274 {
4275 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4276 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4277 != TEMPLATE_PARM_LEVEL (index) - levels)
4278 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4279 {
4280 tree orig_decl = TEMPLATE_PARM_DECL (index);
4281 tree decl, t;
4282
4283 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4284 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4285 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4286 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4287 DECL_ARTIFICIAL (decl) = 1;
4288 SET_DECL_TEMPLATE_PARM_P (decl);
4289
4290 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4291 TEMPLATE_PARM_LEVEL (index) - levels,
4292 TEMPLATE_PARM_ORIG_LEVEL (index),
4293 decl, type);
4294 TEMPLATE_PARM_DESCENDANTS (index) = t;
4295 TEMPLATE_PARM_PARAMETER_PACK (t)
4296 = TEMPLATE_PARM_PARAMETER_PACK (index);
4297
4298 /* Template template parameters need this. */
4299 if (TREE_CODE (decl) == TEMPLATE_DECL)
4300 {
4301 DECL_TEMPLATE_RESULT (decl)
4302 = build_decl (DECL_SOURCE_LOCATION (decl),
4303 TYPE_DECL, DECL_NAME (decl), type);
4304 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4305 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4306 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4307 }
4308 }
4309
4310 return TEMPLATE_PARM_DESCENDANTS (index);
4311 }
4312
4313 /* Process information from new template parameter PARM and append it
4314 to the LIST being built. This new parameter is a non-type
4315 parameter iff IS_NON_TYPE is true. This new parameter is a
4316 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4317 is in PARM_LOC. */
4318
4319 tree
4320 process_template_parm (tree list, location_t parm_loc, tree parm,
4321 bool is_non_type, bool is_parameter_pack)
4322 {
4323 tree decl = 0;
4324 int idx = 0;
4325
4326 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4327 tree defval = TREE_PURPOSE (parm);
4328 tree constr = TREE_TYPE (parm);
4329
4330 if (list)
4331 {
4332 tree p = tree_last (list);
4333
4334 if (p && TREE_VALUE (p) != error_mark_node)
4335 {
4336 p = TREE_VALUE (p);
4337 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4338 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4339 else
4340 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4341 }
4342
4343 ++idx;
4344 }
4345
4346 if (is_non_type)
4347 {
4348 parm = TREE_VALUE (parm);
4349
4350 SET_DECL_TEMPLATE_PARM_P (parm);
4351
4352 if (TREE_TYPE (parm) != error_mark_node)
4353 {
4354 /* [temp.param]
4355
4356 The top-level cv-qualifiers on the template-parameter are
4357 ignored when determining its type. */
4358 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4359 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4360 TREE_TYPE (parm) = error_mark_node;
4361 else if (uses_parameter_packs (TREE_TYPE (parm))
4362 && !is_parameter_pack
4363 /* If we're in a nested template parameter list, the template
4364 template parameter could be a parameter pack. */
4365 && processing_template_parmlist == 1)
4366 {
4367 /* This template parameter is not a parameter pack, but it
4368 should be. Complain about "bare" parameter packs. */
4369 check_for_bare_parameter_packs (TREE_TYPE (parm));
4370
4371 /* Recover by calling this a parameter pack. */
4372 is_parameter_pack = true;
4373 }
4374 }
4375
4376 /* A template parameter is not modifiable. */
4377 TREE_CONSTANT (parm) = 1;
4378 TREE_READONLY (parm) = 1;
4379 decl = build_decl (parm_loc,
4380 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4381 TREE_CONSTANT (decl) = 1;
4382 TREE_READONLY (decl) = 1;
4383 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4384 = build_template_parm_index (idx, processing_template_decl,
4385 processing_template_decl,
4386 decl, TREE_TYPE (parm));
4387
4388 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4389 = is_parameter_pack;
4390 }
4391 else
4392 {
4393 tree t;
4394 parm = TREE_VALUE (TREE_VALUE (parm));
4395
4396 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4397 {
4398 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4399 /* This is for distinguishing between real templates and template
4400 template parameters */
4401 TREE_TYPE (parm) = t;
4402 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4403 decl = parm;
4404 }
4405 else
4406 {
4407 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4408 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4409 decl = build_decl (parm_loc,
4410 TYPE_DECL, parm, t);
4411 }
4412
4413 TYPE_NAME (t) = decl;
4414 TYPE_STUB_DECL (t) = decl;
4415 parm = decl;
4416 TEMPLATE_TYPE_PARM_INDEX (t)
4417 = build_template_parm_index (idx, processing_template_decl,
4418 processing_template_decl,
4419 decl, TREE_TYPE (parm));
4420 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4421 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4422 }
4423 DECL_ARTIFICIAL (decl) = 1;
4424 SET_DECL_TEMPLATE_PARM_P (decl);
4425
4426 /* Build requirements for the type/template parameter.
4427 This must be done after SET_DECL_TEMPLATE_PARM_P or
4428 process_template_parm could fail. */
4429 tree reqs = finish_shorthand_constraint (parm, constr);
4430
4431 pushdecl (decl);
4432
4433 if (defval && TREE_CODE (defval) == OVERLOAD)
4434 lookup_keep (defval, true);
4435
4436 /* Build the parameter node linking the parameter declaration,
4437 its default argument (if any), and its constraints (if any). */
4438 parm = build_tree_list (defval, parm);
4439 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4440
4441 return chainon (list, parm);
4442 }
4443
4444 /* The end of a template parameter list has been reached. Process the
4445 tree list into a parameter vector, converting each parameter into a more
4446 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4447 as PARM_DECLs. */
4448
4449 tree
4450 end_template_parm_list (tree parms)
4451 {
4452 int nparms;
4453 tree parm, next;
4454 tree saved_parmlist = make_tree_vec (list_length (parms));
4455
4456 /* Pop the dummy parameter level and add the real one. */
4457 current_template_parms = TREE_CHAIN (current_template_parms);
4458
4459 current_template_parms
4460 = tree_cons (size_int (processing_template_decl),
4461 saved_parmlist, current_template_parms);
4462
4463 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4464 {
4465 next = TREE_CHAIN (parm);
4466 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4467 TREE_CHAIN (parm) = NULL_TREE;
4468 }
4469
4470 --processing_template_parmlist;
4471
4472 return saved_parmlist;
4473 }
4474
4475 // Explicitly indicate the end of the template parameter list. We assume
4476 // that the current template parameters have been constructed and/or
4477 // managed explicitly, as when creating new template template parameters
4478 // from a shorthand constraint.
4479 void
4480 end_template_parm_list ()
4481 {
4482 --processing_template_parmlist;
4483 }
4484
4485 /* end_template_decl is called after a template declaration is seen. */
4486
4487 void
4488 end_template_decl (void)
4489 {
4490 reset_specialization ();
4491
4492 if (! processing_template_decl)
4493 return;
4494
4495 /* This matches the pushlevel in begin_template_parm_list. */
4496 finish_scope ();
4497
4498 --processing_template_decl;
4499 current_template_parms = TREE_CHAIN (current_template_parms);
4500 }
4501
4502 /* Takes a TREE_LIST representing a template parameter and convert it
4503 into an argument suitable to be passed to the type substitution
4504 functions. Note that If the TREE_LIST contains an error_mark
4505 node, the returned argument is error_mark_node. */
4506
4507 tree
4508 template_parm_to_arg (tree t)
4509 {
4510
4511 if (t == NULL_TREE
4512 || TREE_CODE (t) != TREE_LIST)
4513 return t;
4514
4515 if (error_operand_p (TREE_VALUE (t)))
4516 return error_mark_node;
4517
4518 t = TREE_VALUE (t);
4519
4520 if (TREE_CODE (t) == TYPE_DECL
4521 || TREE_CODE (t) == TEMPLATE_DECL)
4522 {
4523 t = TREE_TYPE (t);
4524
4525 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4526 {
4527 /* Turn this argument into a TYPE_ARGUMENT_PACK
4528 with a single element, which expands T. */
4529 tree vec = make_tree_vec (1);
4530 if (CHECKING_P)
4531 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4532
4533 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4534
4535 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4536 SET_ARGUMENT_PACK_ARGS (t, vec);
4537 }
4538 }
4539 else
4540 {
4541 t = DECL_INITIAL (t);
4542
4543 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4544 {
4545 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4546 with a single element, which expands T. */
4547 tree vec = make_tree_vec (1);
4548 if (CHECKING_P)
4549 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4550
4551 t = convert_from_reference (t);
4552 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4553
4554 t = make_node (NONTYPE_ARGUMENT_PACK);
4555 SET_ARGUMENT_PACK_ARGS (t, vec);
4556 }
4557 else
4558 t = convert_from_reference (t);
4559 }
4560 return t;
4561 }
4562
4563 /* Given a single level of template parameters (a TREE_VEC), return it
4564 as a set of template arguments. */
4565
4566 static tree
4567 template_parms_level_to_args (tree parms)
4568 {
4569 tree a = copy_node (parms);
4570 TREE_TYPE (a) = NULL_TREE;
4571 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4572 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4573
4574 if (CHECKING_P)
4575 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4576
4577 return a;
4578 }
4579
4580 /* Given a set of template parameters, return them as a set of template
4581 arguments. The template parameters are represented as a TREE_VEC, in
4582 the form documented in cp-tree.h for template arguments. */
4583
4584 static tree
4585 template_parms_to_args (tree parms)
4586 {
4587 tree header;
4588 tree args = NULL_TREE;
4589 int length = TMPL_PARMS_DEPTH (parms);
4590 int l = length;
4591
4592 /* If there is only one level of template parameters, we do not
4593 create a TREE_VEC of TREE_VECs. Instead, we return a single
4594 TREE_VEC containing the arguments. */
4595 if (length > 1)
4596 args = make_tree_vec (length);
4597
4598 for (header = parms; header; header = TREE_CHAIN (header))
4599 {
4600 tree a = template_parms_level_to_args (TREE_VALUE (header));
4601
4602 if (length > 1)
4603 TREE_VEC_ELT (args, --l) = a;
4604 else
4605 args = a;
4606 }
4607
4608 return args;
4609 }
4610
4611 /* Within the declaration of a template, return the currently active
4612 template parameters as an argument TREE_VEC. */
4613
4614 static tree
4615 current_template_args (void)
4616 {
4617 return template_parms_to_args (current_template_parms);
4618 }
4619
4620 /* Update the declared TYPE by doing any lookups which were thought to be
4621 dependent, but are not now that we know the SCOPE of the declarator. */
4622
4623 tree
4624 maybe_update_decl_type (tree orig_type, tree scope)
4625 {
4626 tree type = orig_type;
4627
4628 if (type == NULL_TREE)
4629 return type;
4630
4631 if (TREE_CODE (orig_type) == TYPE_DECL)
4632 type = TREE_TYPE (type);
4633
4634 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4635 && dependent_type_p (type)
4636 /* Don't bother building up the args in this case. */
4637 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4638 {
4639 /* tsubst in the args corresponding to the template parameters,
4640 including auto if present. Most things will be unchanged, but
4641 make_typename_type and tsubst_qualified_id will resolve
4642 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4643 tree args = current_template_args ();
4644 tree auto_node = type_uses_auto (type);
4645 tree pushed;
4646 if (auto_node)
4647 {
4648 tree auto_vec = make_tree_vec (1);
4649 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4650 args = add_to_template_args (args, auto_vec);
4651 }
4652 pushed = push_scope (scope);
4653 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4654 if (pushed)
4655 pop_scope (scope);
4656 }
4657
4658 if (type == error_mark_node)
4659 return orig_type;
4660
4661 if (TREE_CODE (orig_type) == TYPE_DECL)
4662 {
4663 if (same_type_p (type, TREE_TYPE (orig_type)))
4664 type = orig_type;
4665 else
4666 type = TYPE_NAME (type);
4667 }
4668 return type;
4669 }
4670
4671 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4672 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4673 the new template is a member template. */
4674
4675 tree
4676 build_template_decl (tree decl, tree parms, bool member_template_p)
4677 {
4678 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4679 DECL_TEMPLATE_PARMS (tmpl) = parms;
4680 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4681 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4682 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4683
4684 return tmpl;
4685 }
4686
4687 struct template_parm_data
4688 {
4689 /* The level of the template parameters we are currently
4690 processing. */
4691 int level;
4692
4693 /* The index of the specialization argument we are currently
4694 processing. */
4695 int current_arg;
4696
4697 /* An array whose size is the number of template parameters. The
4698 elements are nonzero if the parameter has been used in any one
4699 of the arguments processed so far. */
4700 int* parms;
4701
4702 /* An array whose size is the number of template arguments. The
4703 elements are nonzero if the argument makes use of template
4704 parameters of this level. */
4705 int* arg_uses_template_parms;
4706 };
4707
4708 /* Subroutine of push_template_decl used to see if each template
4709 parameter in a partial specialization is used in the explicit
4710 argument list. If T is of the LEVEL given in DATA (which is
4711 treated as a template_parm_data*), then DATA->PARMS is marked
4712 appropriately. */
4713
4714 static int
4715 mark_template_parm (tree t, void* data)
4716 {
4717 int level;
4718 int idx;
4719 struct template_parm_data* tpd = (struct template_parm_data*) data;
4720
4721 template_parm_level_and_index (t, &level, &idx);
4722
4723 if (level == tpd->level)
4724 {
4725 tpd->parms[idx] = 1;
4726 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4727 }
4728
4729 /* In C++17 the type of a non-type argument is a deduced context. */
4730 if (cxx_dialect >= cxx17
4731 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4732 for_each_template_parm (TREE_TYPE (t),
4733 &mark_template_parm,
4734 data,
4735 NULL,
4736 /*include_nondeduced_p=*/false);
4737
4738 /* Return zero so that for_each_template_parm will continue the
4739 traversal of the tree; we want to mark *every* template parm. */
4740 return 0;
4741 }
4742
4743 /* Process the partial specialization DECL. */
4744
4745 static tree
4746 process_partial_specialization (tree decl)
4747 {
4748 tree type = TREE_TYPE (decl);
4749 tree tinfo = get_template_info (decl);
4750 tree maintmpl = TI_TEMPLATE (tinfo);
4751 tree specargs = TI_ARGS (tinfo);
4752 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4753 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4754 tree inner_parms;
4755 tree inst;
4756 int nargs = TREE_VEC_LENGTH (inner_args);
4757 int ntparms;
4758 int i;
4759 bool did_error_intro = false;
4760 struct template_parm_data tpd;
4761 struct template_parm_data tpd2;
4762
4763 gcc_assert (current_template_parms);
4764
4765 /* A concept cannot be specialized. */
4766 if (flag_concepts && variable_concept_p (maintmpl))
4767 {
4768 error ("specialization of variable concept %q#D", maintmpl);
4769 return error_mark_node;
4770 }
4771
4772 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4773 ntparms = TREE_VEC_LENGTH (inner_parms);
4774
4775 /* We check that each of the template parameters given in the
4776 partial specialization is used in the argument list to the
4777 specialization. For example:
4778
4779 template <class T> struct S;
4780 template <class T> struct S<T*>;
4781
4782 The second declaration is OK because `T*' uses the template
4783 parameter T, whereas
4784
4785 template <class T> struct S<int>;
4786
4787 is no good. Even trickier is:
4788
4789 template <class T>
4790 struct S1
4791 {
4792 template <class U>
4793 struct S2;
4794 template <class U>
4795 struct S2<T>;
4796 };
4797
4798 The S2<T> declaration is actually invalid; it is a
4799 full-specialization. Of course,
4800
4801 template <class U>
4802 struct S2<T (*)(U)>;
4803
4804 or some such would have been OK. */
4805 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4806 tpd.parms = XALLOCAVEC (int, ntparms);
4807 memset (tpd.parms, 0, sizeof (int) * ntparms);
4808
4809 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4810 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4811 for (i = 0; i < nargs; ++i)
4812 {
4813 tpd.current_arg = i;
4814 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4815 &mark_template_parm,
4816 &tpd,
4817 NULL,
4818 /*include_nondeduced_p=*/false);
4819 }
4820 for (i = 0; i < ntparms; ++i)
4821 if (tpd.parms[i] == 0)
4822 {
4823 /* One of the template parms was not used in a deduced context in the
4824 specialization. */
4825 if (!did_error_intro)
4826 {
4827 error ("template parameters not deducible in "
4828 "partial specialization:");
4829 did_error_intro = true;
4830 }
4831
4832 inform (input_location, " %qD",
4833 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4834 }
4835
4836 if (did_error_intro)
4837 return error_mark_node;
4838
4839 /* [temp.class.spec]
4840
4841 The argument list of the specialization shall not be identical to
4842 the implicit argument list of the primary template. */
4843 tree main_args
4844 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4845 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4846 && (!flag_concepts
4847 || !strictly_subsumes (current_template_constraints (),
4848 get_constraints (maintmpl))))
4849 {
4850 if (!flag_concepts)
4851 error ("partial specialization %q+D does not specialize "
4852 "any template arguments; to define the primary template, "
4853 "remove the template argument list", decl);
4854 else
4855 error ("partial specialization %q+D does not specialize any "
4856 "template arguments and is not more constrained than "
4857 "the primary template; to define the primary template, "
4858 "remove the template argument list", decl);
4859 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4860 }
4861
4862 /* A partial specialization that replaces multiple parameters of the
4863 primary template with a pack expansion is less specialized for those
4864 parameters. */
4865 if (nargs < DECL_NTPARMS (maintmpl))
4866 {
4867 error ("partial specialization is not more specialized than the "
4868 "primary template because it replaces multiple parameters "
4869 "with a pack expansion");
4870 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4871 /* Avoid crash in process_partial_specialization. */
4872 return decl;
4873 }
4874
4875 /* If we aren't in a dependent class, we can actually try deduction. */
4876 else if (tpd.level == 1
4877 /* FIXME we should be able to handle a partial specialization of a
4878 partial instantiation, but currently we can't (c++/41727). */
4879 && TMPL_ARGS_DEPTH (specargs) == 1
4880 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4881 {
4882 if (permerror (input_location, "partial specialization %qD is not "
4883 "more specialized than", decl))
4884 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4885 maintmpl);
4886 }
4887
4888 /* [temp.class.spec]
4889
4890 A partially specialized non-type argument expression shall not
4891 involve template parameters of the partial specialization except
4892 when the argument expression is a simple identifier.
4893
4894 The type of a template parameter corresponding to a specialized
4895 non-type argument shall not be dependent on a parameter of the
4896 specialization.
4897
4898 Also, we verify that pack expansions only occur at the
4899 end of the argument list. */
4900 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4901 tpd2.parms = 0;
4902 for (i = 0; i < nargs; ++i)
4903 {
4904 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4905 tree arg = TREE_VEC_ELT (inner_args, i);
4906 tree packed_args = NULL_TREE;
4907 int j, len = 1;
4908
4909 if (ARGUMENT_PACK_P (arg))
4910 {
4911 /* Extract the arguments from the argument pack. We'll be
4912 iterating over these in the following loop. */
4913 packed_args = ARGUMENT_PACK_ARGS (arg);
4914 len = TREE_VEC_LENGTH (packed_args);
4915 }
4916
4917 for (j = 0; j < len; j++)
4918 {
4919 if (packed_args)
4920 /* Get the Jth argument in the parameter pack. */
4921 arg = TREE_VEC_ELT (packed_args, j);
4922
4923 if (PACK_EXPANSION_P (arg))
4924 {
4925 /* Pack expansions must come at the end of the
4926 argument list. */
4927 if ((packed_args && j < len - 1)
4928 || (!packed_args && i < nargs - 1))
4929 {
4930 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4931 error ("parameter pack argument %qE must be at the "
4932 "end of the template argument list", arg);
4933 else
4934 error ("parameter pack argument %qT must be at the "
4935 "end of the template argument list", arg);
4936 }
4937 }
4938
4939 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4940 /* We only care about the pattern. */
4941 arg = PACK_EXPANSION_PATTERN (arg);
4942
4943 if (/* These first two lines are the `non-type' bit. */
4944 !TYPE_P (arg)
4945 && TREE_CODE (arg) != TEMPLATE_DECL
4946 /* This next two lines are the `argument expression is not just a
4947 simple identifier' condition and also the `specialized
4948 non-type argument' bit. */
4949 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4950 && !(REFERENCE_REF_P (arg)
4951 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4952 {
4953 if ((!packed_args && tpd.arg_uses_template_parms[i])
4954 || (packed_args && uses_template_parms (arg)))
4955 error ("template argument %qE involves template parameter(s)",
4956 arg);
4957 else
4958 {
4959 /* Look at the corresponding template parameter,
4960 marking which template parameters its type depends
4961 upon. */
4962 tree type = TREE_TYPE (parm);
4963
4964 if (!tpd2.parms)
4965 {
4966 /* We haven't yet initialized TPD2. Do so now. */
4967 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4968 /* The number of parameters here is the number in the
4969 main template, which, as checked in the assertion
4970 above, is NARGS. */
4971 tpd2.parms = XALLOCAVEC (int, nargs);
4972 tpd2.level =
4973 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4974 }
4975
4976 /* Mark the template parameters. But this time, we're
4977 looking for the template parameters of the main
4978 template, not in the specialization. */
4979 tpd2.current_arg = i;
4980 tpd2.arg_uses_template_parms[i] = 0;
4981 memset (tpd2.parms, 0, sizeof (int) * nargs);
4982 for_each_template_parm (type,
4983 &mark_template_parm,
4984 &tpd2,
4985 NULL,
4986 /*include_nondeduced_p=*/false);
4987
4988 if (tpd2.arg_uses_template_parms [i])
4989 {
4990 /* The type depended on some template parameters.
4991 If they are fully specialized in the
4992 specialization, that's OK. */
4993 int j;
4994 int count = 0;
4995 for (j = 0; j < nargs; ++j)
4996 if (tpd2.parms[j] != 0
4997 && tpd.arg_uses_template_parms [j])
4998 ++count;
4999 if (count != 0)
5000 error_n (input_location, count,
5001 "type %qT of template argument %qE depends "
5002 "on a template parameter",
5003 "type %qT of template argument %qE depends "
5004 "on template parameters",
5005 type,
5006 arg);
5007 }
5008 }
5009 }
5010 }
5011 }
5012
5013 /* We should only get here once. */
5014 if (TREE_CODE (decl) == TYPE_DECL)
5015 gcc_assert (!COMPLETE_TYPE_P (type));
5016
5017 // Build the template decl.
5018 tree tmpl = build_template_decl (decl, current_template_parms,
5019 DECL_MEMBER_TEMPLATE_P (maintmpl));
5020 TREE_TYPE (tmpl) = type;
5021 DECL_TEMPLATE_RESULT (tmpl) = decl;
5022 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5023 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5024 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5025
5026 /* Give template template parms a DECL_CONTEXT of the template
5027 for which they are a parameter. */
5028 for (i = 0; i < ntparms; ++i)
5029 {
5030 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5031 if (TREE_CODE (parm) == TEMPLATE_DECL)
5032 DECL_CONTEXT (parm) = tmpl;
5033 }
5034
5035 if (VAR_P (decl))
5036 /* We didn't register this in check_explicit_specialization so we could
5037 wait until the constraints were set. */
5038 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5039 else
5040 associate_classtype_constraints (type);
5041
5042 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5043 = tree_cons (specargs, tmpl,
5044 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5045 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5046
5047 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5048 inst = TREE_CHAIN (inst))
5049 {
5050 tree instance = TREE_VALUE (inst);
5051 if (TYPE_P (instance)
5052 ? (COMPLETE_TYPE_P (instance)
5053 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5054 : DECL_TEMPLATE_INSTANTIATION (instance))
5055 {
5056 tree spec = most_specialized_partial_spec (instance, tf_none);
5057 tree inst_decl = (DECL_P (instance)
5058 ? instance : TYPE_NAME (instance));
5059 if (!spec)
5060 /* OK */;
5061 else if (spec == error_mark_node)
5062 permerror (input_location,
5063 "declaration of %qD ambiguates earlier template "
5064 "instantiation for %qD", decl, inst_decl);
5065 else if (TREE_VALUE (spec) == tmpl)
5066 permerror (input_location,
5067 "partial specialization of %qD after instantiation "
5068 "of %qD", decl, inst_decl);
5069 }
5070 }
5071
5072 return decl;
5073 }
5074
5075 /* PARM is a template parameter of some form; return the corresponding
5076 TEMPLATE_PARM_INDEX. */
5077
5078 static tree
5079 get_template_parm_index (tree parm)
5080 {
5081 if (TREE_CODE (parm) == PARM_DECL
5082 || TREE_CODE (parm) == CONST_DECL)
5083 parm = DECL_INITIAL (parm);
5084 else if (TREE_CODE (parm) == TYPE_DECL
5085 || TREE_CODE (parm) == TEMPLATE_DECL)
5086 parm = TREE_TYPE (parm);
5087 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5088 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5089 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5090 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5091 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5092 return parm;
5093 }
5094
5095 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5096 parameter packs used by the template parameter PARM. */
5097
5098 static void
5099 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5100 {
5101 /* A type parm can't refer to another parm. */
5102 if (TREE_CODE (parm) == TYPE_DECL)
5103 return;
5104 else if (TREE_CODE (parm) == PARM_DECL)
5105 {
5106 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5107 ppd, ppd->visited);
5108 return;
5109 }
5110
5111 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5112
5113 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5114 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5115 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5116 }
5117
5118 /* PARM is a template parameter pack. Return any parameter packs used in
5119 its type or the type of any of its template parameters. If there are
5120 any such packs, it will be instantiated into a fixed template parameter
5121 list by partial instantiation rather than be fully deduced. */
5122
5123 tree
5124 fixed_parameter_pack_p (tree parm)
5125 {
5126 /* This can only be true in a member template. */
5127 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5128 return NULL_TREE;
5129 /* This can only be true for a parameter pack. */
5130 if (!template_parameter_pack_p (parm))
5131 return NULL_TREE;
5132 /* A type parm can't refer to another parm. */
5133 if (TREE_CODE (parm) == TYPE_DECL)
5134 return NULL_TREE;
5135
5136 tree parameter_packs = NULL_TREE;
5137 struct find_parameter_pack_data ppd;
5138 ppd.parameter_packs = &parameter_packs;
5139 ppd.visited = new hash_set<tree>;
5140 ppd.type_pack_expansion_p = false;
5141
5142 fixed_parameter_pack_p_1 (parm, &ppd);
5143
5144 delete ppd.visited;
5145 return parameter_packs;
5146 }
5147
5148 /* Check that a template declaration's use of default arguments and
5149 parameter packs is not invalid. Here, PARMS are the template
5150 parameters. IS_PRIMARY is true if DECL is the thing declared by
5151 a primary template. IS_PARTIAL is true if DECL is a partial
5152 specialization.
5153
5154 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5155 function template declaration or a friend class template
5156 declaration. In the function case, 1 indicates a declaration, 2
5157 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5158 emitted for extraneous default arguments.
5159
5160 Returns TRUE if there were no errors found, FALSE otherwise. */
5161
5162 bool
5163 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5164 bool is_partial, int is_friend_decl)
5165 {
5166 const char *msg;
5167 int last_level_to_check;
5168 tree parm_level;
5169 bool no_errors = true;
5170
5171 /* [temp.param]
5172
5173 A default template-argument shall not be specified in a
5174 function template declaration or a function template definition, nor
5175 in the template-parameter-list of the definition of a member of a
5176 class template. */
5177
5178 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5179 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5180 /* You can't have a function template declaration in a local
5181 scope, nor you can you define a member of a class template in a
5182 local scope. */
5183 return true;
5184
5185 if ((TREE_CODE (decl) == TYPE_DECL
5186 && TREE_TYPE (decl)
5187 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5188 || (TREE_CODE (decl) == FUNCTION_DECL
5189 && LAMBDA_FUNCTION_P (decl)))
5190 /* A lambda doesn't have an explicit declaration; don't complain
5191 about the parms of the enclosing class. */
5192 return true;
5193
5194 if (current_class_type
5195 && !TYPE_BEING_DEFINED (current_class_type)
5196 && DECL_LANG_SPECIFIC (decl)
5197 && DECL_DECLARES_FUNCTION_P (decl)
5198 /* If this is either a friend defined in the scope of the class
5199 or a member function. */
5200 && (DECL_FUNCTION_MEMBER_P (decl)
5201 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5202 : DECL_FRIEND_CONTEXT (decl)
5203 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5204 : false)
5205 /* And, if it was a member function, it really was defined in
5206 the scope of the class. */
5207 && (!DECL_FUNCTION_MEMBER_P (decl)
5208 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5209 /* We already checked these parameters when the template was
5210 declared, so there's no need to do it again now. This function
5211 was defined in class scope, but we're processing its body now
5212 that the class is complete. */
5213 return true;
5214
5215 /* Core issue 226 (C++0x only): the following only applies to class
5216 templates. */
5217 if (is_primary
5218 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5219 {
5220 /* [temp.param]
5221
5222 If a template-parameter has a default template-argument, all
5223 subsequent template-parameters shall have a default
5224 template-argument supplied. */
5225 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5226 {
5227 tree inner_parms = TREE_VALUE (parm_level);
5228 int ntparms = TREE_VEC_LENGTH (inner_parms);
5229 int seen_def_arg_p = 0;
5230 int i;
5231
5232 for (i = 0; i < ntparms; ++i)
5233 {
5234 tree parm = TREE_VEC_ELT (inner_parms, i);
5235
5236 if (parm == error_mark_node)
5237 continue;
5238
5239 if (TREE_PURPOSE (parm))
5240 seen_def_arg_p = 1;
5241 else if (seen_def_arg_p
5242 && !template_parameter_pack_p (TREE_VALUE (parm)))
5243 {
5244 error ("no default argument for %qD", TREE_VALUE (parm));
5245 /* For better subsequent error-recovery, we indicate that
5246 there should have been a default argument. */
5247 TREE_PURPOSE (parm) = error_mark_node;
5248 no_errors = false;
5249 }
5250 else if (!is_partial
5251 && !is_friend_decl
5252 /* Don't complain about an enclosing partial
5253 specialization. */
5254 && parm_level == parms
5255 && TREE_CODE (decl) == TYPE_DECL
5256 && i < ntparms - 1
5257 && template_parameter_pack_p (TREE_VALUE (parm))
5258 /* A fixed parameter pack will be partially
5259 instantiated into a fixed length list. */
5260 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5261 {
5262 /* A primary class template can only have one
5263 parameter pack, at the end of the template
5264 parameter list. */
5265
5266 error ("parameter pack %q+D must be at the end of the"
5267 " template parameter list", TREE_VALUE (parm));
5268
5269 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5270 = error_mark_node;
5271 no_errors = false;
5272 }
5273 }
5274 }
5275 }
5276
5277 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5278 || is_partial
5279 || !is_primary
5280 || is_friend_decl)
5281 /* For an ordinary class template, default template arguments are
5282 allowed at the innermost level, e.g.:
5283 template <class T = int>
5284 struct S {};
5285 but, in a partial specialization, they're not allowed even
5286 there, as we have in [temp.class.spec]:
5287
5288 The template parameter list of a specialization shall not
5289 contain default template argument values.
5290
5291 So, for a partial specialization, or for a function template
5292 (in C++98/C++03), we look at all of them. */
5293 ;
5294 else
5295 /* But, for a primary class template that is not a partial
5296 specialization we look at all template parameters except the
5297 innermost ones. */
5298 parms = TREE_CHAIN (parms);
5299
5300 /* Figure out what error message to issue. */
5301 if (is_friend_decl == 2)
5302 msg = G_("default template arguments may not be used in function template "
5303 "friend re-declaration");
5304 else if (is_friend_decl)
5305 msg = G_("default template arguments may not be used in template "
5306 "friend declarations");
5307 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5308 msg = G_("default template arguments may not be used in function templates "
5309 "without -std=c++11 or -std=gnu++11");
5310 else if (is_partial)
5311 msg = G_("default template arguments may not be used in "
5312 "partial specializations");
5313 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5314 msg = G_("default argument for template parameter for class enclosing %qD");
5315 else
5316 /* Per [temp.param]/9, "A default template-argument shall not be
5317 specified in the template-parameter-lists of the definition of
5318 a member of a class template that appears outside of the member's
5319 class.", thus if we aren't handling a member of a class template
5320 there is no need to examine the parameters. */
5321 return true;
5322
5323 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5324 /* If we're inside a class definition, there's no need to
5325 examine the parameters to the class itself. On the one
5326 hand, they will be checked when the class is defined, and,
5327 on the other, default arguments are valid in things like:
5328 template <class T = double>
5329 struct S { template <class U> void f(U); };
5330 Here the default argument for `S' has no bearing on the
5331 declaration of `f'. */
5332 last_level_to_check = template_class_depth (current_class_type) + 1;
5333 else
5334 /* Check everything. */
5335 last_level_to_check = 0;
5336
5337 for (parm_level = parms;
5338 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5339 parm_level = TREE_CHAIN (parm_level))
5340 {
5341 tree inner_parms = TREE_VALUE (parm_level);
5342 int i;
5343 int ntparms;
5344
5345 ntparms = TREE_VEC_LENGTH (inner_parms);
5346 for (i = 0; i < ntparms; ++i)
5347 {
5348 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5349 continue;
5350
5351 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5352 {
5353 if (msg)
5354 {
5355 no_errors = false;
5356 if (is_friend_decl == 2)
5357 return no_errors;
5358
5359 error (msg, decl);
5360 msg = 0;
5361 }
5362
5363 /* Clear out the default argument so that we are not
5364 confused later. */
5365 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5366 }
5367 }
5368
5369 /* At this point, if we're still interested in issuing messages,
5370 they must apply to classes surrounding the object declared. */
5371 if (msg)
5372 msg = G_("default argument for template parameter for class "
5373 "enclosing %qD");
5374 }
5375
5376 return no_errors;
5377 }
5378
5379 /* Worker for push_template_decl_real, called via
5380 for_each_template_parm. DATA is really an int, indicating the
5381 level of the parameters we are interested in. If T is a template
5382 parameter of that level, return nonzero. */
5383
5384 static int
5385 template_parm_this_level_p (tree t, void* data)
5386 {
5387 int this_level = *(int *)data;
5388 int level;
5389
5390 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5391 level = TEMPLATE_PARM_LEVEL (t);
5392 else
5393 level = TEMPLATE_TYPE_LEVEL (t);
5394 return level == this_level;
5395 }
5396
5397 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5398 DATA is really an int, indicating the innermost outer level of parameters.
5399 If T is a template parameter of that level or further out, return
5400 nonzero. */
5401
5402 static int
5403 template_parm_outer_level (tree t, void *data)
5404 {
5405 int this_level = *(int *)data;
5406 int level;
5407
5408 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5409 level = TEMPLATE_PARM_LEVEL (t);
5410 else
5411 level = TEMPLATE_TYPE_LEVEL (t);
5412 return level <= this_level;
5413 }
5414
5415 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5416 parameters given by current_template_args, or reuses a
5417 previously existing one, if appropriate. Returns the DECL, or an
5418 equivalent one, if it is replaced via a call to duplicate_decls.
5419
5420 If IS_FRIEND is true, DECL is a friend declaration. */
5421
5422 tree
5423 push_template_decl_real (tree decl, bool is_friend)
5424 {
5425 tree tmpl;
5426 tree args;
5427 tree info;
5428 tree ctx;
5429 bool is_primary;
5430 bool is_partial;
5431 int new_template_p = 0;
5432 /* True if the template is a member template, in the sense of
5433 [temp.mem]. */
5434 bool member_template_p = false;
5435
5436 if (decl == error_mark_node || !current_template_parms)
5437 return error_mark_node;
5438
5439 /* See if this is a partial specialization. */
5440 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5441 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5442 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5443 || (VAR_P (decl)
5444 && DECL_LANG_SPECIFIC (decl)
5445 && DECL_TEMPLATE_SPECIALIZATION (decl)
5446 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5447
5448 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5449 is_friend = true;
5450
5451 if (is_friend)
5452 /* For a friend, we want the context of the friend, not
5453 the type of which it is a friend. */
5454 ctx = CP_DECL_CONTEXT (decl);
5455 else if (CP_DECL_CONTEXT (decl)
5456 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5457 /* In the case of a virtual function, we want the class in which
5458 it is defined. */
5459 ctx = CP_DECL_CONTEXT (decl);
5460 else
5461 /* Otherwise, if we're currently defining some class, the DECL
5462 is assumed to be a member of the class. */
5463 ctx = current_scope ();
5464
5465 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5466 ctx = NULL_TREE;
5467
5468 if (!DECL_CONTEXT (decl))
5469 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5470
5471 /* See if this is a primary template. */
5472 if (is_friend && ctx
5473 && uses_template_parms_level (ctx, processing_template_decl))
5474 /* A friend template that specifies a class context, i.e.
5475 template <typename T> friend void A<T>::f();
5476 is not primary. */
5477 is_primary = false;
5478 else if (TREE_CODE (decl) == TYPE_DECL
5479 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5480 is_primary = false;
5481 else
5482 is_primary = template_parm_scope_p ();
5483
5484 if (is_primary)
5485 {
5486 warning (OPT_Wtemplates, "template %qD declared", decl);
5487
5488 if (DECL_CLASS_SCOPE_P (decl))
5489 member_template_p = true;
5490 if (TREE_CODE (decl) == TYPE_DECL
5491 && anon_aggrname_p (DECL_NAME (decl)))
5492 {
5493 error ("template class without a name");
5494 return error_mark_node;
5495 }
5496 else if (TREE_CODE (decl) == FUNCTION_DECL)
5497 {
5498 if (member_template_p)
5499 {
5500 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5501 error ("member template %qD may not have virt-specifiers", decl);
5502 }
5503 if (DECL_DESTRUCTOR_P (decl))
5504 {
5505 /* [temp.mem]
5506
5507 A destructor shall not be a member template. */
5508 error ("destructor %qD declared as member template", decl);
5509 return error_mark_node;
5510 }
5511 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5512 && (!prototype_p (TREE_TYPE (decl))
5513 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5514 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5515 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5516 == void_list_node)))
5517 {
5518 /* [basic.stc.dynamic.allocation]
5519
5520 An allocation function can be a function
5521 template. ... Template allocation functions shall
5522 have two or more parameters. */
5523 error ("invalid template declaration of %qD", decl);
5524 return error_mark_node;
5525 }
5526 }
5527 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5528 && CLASS_TYPE_P (TREE_TYPE (decl)))
5529 {
5530 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5531 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5532 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5533 {
5534 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5535 if (TREE_CODE (t) == TYPE_DECL)
5536 t = TREE_TYPE (t);
5537 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5538 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5539 }
5540 }
5541 else if (TREE_CODE (decl) == TYPE_DECL
5542 && TYPE_DECL_ALIAS_P (decl))
5543 /* alias-declaration */
5544 gcc_assert (!DECL_ARTIFICIAL (decl));
5545 else if (VAR_P (decl))
5546 /* C++14 variable template. */;
5547 else
5548 {
5549 error ("template declaration of %q#D", decl);
5550 return error_mark_node;
5551 }
5552 }
5553
5554 /* Check to see that the rules regarding the use of default
5555 arguments are not being violated. We check args for a friend
5556 functions when we know whether it's a definition, introducing
5557 declaration or re-declaration. */
5558 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5559 check_default_tmpl_args (decl, current_template_parms,
5560 is_primary, is_partial, is_friend);
5561
5562 /* Ensure that there are no parameter packs in the type of this
5563 declaration that have not been expanded. */
5564 if (TREE_CODE (decl) == FUNCTION_DECL)
5565 {
5566 /* Check each of the arguments individually to see if there are
5567 any bare parameter packs. */
5568 tree type = TREE_TYPE (decl);
5569 tree arg = DECL_ARGUMENTS (decl);
5570 tree argtype = TYPE_ARG_TYPES (type);
5571
5572 while (arg && argtype)
5573 {
5574 if (!DECL_PACK_P (arg)
5575 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5576 {
5577 /* This is a PARM_DECL that contains unexpanded parameter
5578 packs. We have already complained about this in the
5579 check_for_bare_parameter_packs call, so just replace
5580 these types with ERROR_MARK_NODE. */
5581 TREE_TYPE (arg) = error_mark_node;
5582 TREE_VALUE (argtype) = error_mark_node;
5583 }
5584
5585 arg = DECL_CHAIN (arg);
5586 argtype = TREE_CHAIN (argtype);
5587 }
5588
5589 /* Check for bare parameter packs in the return type and the
5590 exception specifiers. */
5591 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5592 /* Errors were already issued, set return type to int
5593 as the frontend doesn't expect error_mark_node as
5594 the return type. */
5595 TREE_TYPE (type) = integer_type_node;
5596 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5597 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5598 }
5599 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5600 && TYPE_DECL_ALIAS_P (decl))
5601 ? DECL_ORIGINAL_TYPE (decl)
5602 : TREE_TYPE (decl)))
5603 {
5604 TREE_TYPE (decl) = error_mark_node;
5605 return error_mark_node;
5606 }
5607
5608 if (is_partial)
5609 return process_partial_specialization (decl);
5610
5611 args = current_template_args ();
5612
5613 if (!ctx
5614 || TREE_CODE (ctx) == FUNCTION_DECL
5615 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5616 || (TREE_CODE (decl) == TYPE_DECL
5617 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5618 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5619 {
5620 if (DECL_LANG_SPECIFIC (decl)
5621 && DECL_TEMPLATE_INFO (decl)
5622 && DECL_TI_TEMPLATE (decl))
5623 tmpl = DECL_TI_TEMPLATE (decl);
5624 /* If DECL is a TYPE_DECL for a class-template, then there won't
5625 be DECL_LANG_SPECIFIC. The information equivalent to
5626 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5627 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5628 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5629 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5630 {
5631 /* Since a template declaration already existed for this
5632 class-type, we must be redeclaring it here. Make sure
5633 that the redeclaration is valid. */
5634 redeclare_class_template (TREE_TYPE (decl),
5635 current_template_parms,
5636 current_template_constraints ());
5637 /* We don't need to create a new TEMPLATE_DECL; just use the
5638 one we already had. */
5639 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5640 }
5641 else
5642 {
5643 tmpl = build_template_decl (decl, current_template_parms,
5644 member_template_p);
5645 new_template_p = 1;
5646
5647 if (DECL_LANG_SPECIFIC (decl)
5648 && DECL_TEMPLATE_SPECIALIZATION (decl))
5649 {
5650 /* A specialization of a member template of a template
5651 class. */
5652 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5653 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5654 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5655 }
5656 }
5657 }
5658 else
5659 {
5660 tree a, t, current, parms;
5661 int i;
5662 tree tinfo = get_template_info (decl);
5663
5664 if (!tinfo)
5665 {
5666 error ("template definition of non-template %q#D", decl);
5667 return error_mark_node;
5668 }
5669
5670 tmpl = TI_TEMPLATE (tinfo);
5671
5672 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5673 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5674 && DECL_TEMPLATE_SPECIALIZATION (decl)
5675 && DECL_MEMBER_TEMPLATE_P (tmpl))
5676 {
5677 tree new_tmpl;
5678
5679 /* The declaration is a specialization of a member
5680 template, declared outside the class. Therefore, the
5681 innermost template arguments will be NULL, so we
5682 replace them with the arguments determined by the
5683 earlier call to check_explicit_specialization. */
5684 args = DECL_TI_ARGS (decl);
5685
5686 new_tmpl
5687 = build_template_decl (decl, current_template_parms,
5688 member_template_p);
5689 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5690 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5691 DECL_TI_TEMPLATE (decl) = new_tmpl;
5692 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5693 DECL_TEMPLATE_INFO (new_tmpl)
5694 = build_template_info (tmpl, args);
5695
5696 register_specialization (new_tmpl,
5697 most_general_template (tmpl),
5698 args,
5699 is_friend, 0);
5700 return decl;
5701 }
5702
5703 /* Make sure the template headers we got make sense. */
5704
5705 parms = DECL_TEMPLATE_PARMS (tmpl);
5706 i = TMPL_PARMS_DEPTH (parms);
5707 if (TMPL_ARGS_DEPTH (args) != i)
5708 {
5709 error ("expected %d levels of template parms for %q#D, got %d",
5710 i, decl, TMPL_ARGS_DEPTH (args));
5711 DECL_INTERFACE_KNOWN (decl) = 1;
5712 return error_mark_node;
5713 }
5714 else
5715 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5716 {
5717 a = TMPL_ARGS_LEVEL (args, i);
5718 t = INNERMOST_TEMPLATE_PARMS (parms);
5719
5720 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5721 {
5722 if (current == decl)
5723 error ("got %d template parameters for %q#D",
5724 TREE_VEC_LENGTH (a), decl);
5725 else
5726 error ("got %d template parameters for %q#T",
5727 TREE_VEC_LENGTH (a), current);
5728 error (" but %d required", TREE_VEC_LENGTH (t));
5729 /* Avoid crash in import_export_decl. */
5730 DECL_INTERFACE_KNOWN (decl) = 1;
5731 return error_mark_node;
5732 }
5733
5734 if (current == decl)
5735 current = ctx;
5736 else if (current == NULL_TREE)
5737 /* Can happen in erroneous input. */
5738 break;
5739 else
5740 current = get_containing_scope (current);
5741 }
5742
5743 /* Check that the parms are used in the appropriate qualifying scopes
5744 in the declarator. */
5745 if (!comp_template_args
5746 (TI_ARGS (tinfo),
5747 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5748 {
5749 error ("template arguments to %qD do not match original "
5750 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5751 if (!uses_template_parms (TI_ARGS (tinfo)))
5752 inform (input_location, "use %<template<>%> for"
5753 " an explicit specialization");
5754 /* Avoid crash in import_export_decl. */
5755 DECL_INTERFACE_KNOWN (decl) = 1;
5756 return error_mark_node;
5757 }
5758 }
5759
5760 DECL_TEMPLATE_RESULT (tmpl) = decl;
5761 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5762
5763 /* Push template declarations for global functions and types. Note
5764 that we do not try to push a global template friend declared in a
5765 template class; such a thing may well depend on the template
5766 parameters of the class. */
5767 if (new_template_p && !ctx
5768 && !(is_friend && template_class_depth (current_class_type) > 0))
5769 {
5770 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5771 if (tmpl == error_mark_node)
5772 return error_mark_node;
5773
5774 /* Hide template friend classes that haven't been declared yet. */
5775 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5776 {
5777 DECL_ANTICIPATED (tmpl) = 1;
5778 DECL_FRIEND_P (tmpl) = 1;
5779 }
5780 }
5781
5782 if (is_primary)
5783 {
5784 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5785
5786 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5787
5788 /* Give template template parms a DECL_CONTEXT of the template
5789 for which they are a parameter. */
5790 parms = INNERMOST_TEMPLATE_PARMS (parms);
5791 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5792 {
5793 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5794 if (TREE_CODE (parm) == TEMPLATE_DECL)
5795 DECL_CONTEXT (parm) = tmpl;
5796 }
5797
5798 if (TREE_CODE (decl) == TYPE_DECL
5799 && TYPE_DECL_ALIAS_P (decl)
5800 && complex_alias_template_p (tmpl))
5801 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5802 }
5803
5804 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5805 back to its most general template. If TMPL is a specialization,
5806 ARGS may only have the innermost set of arguments. Add the missing
5807 argument levels if necessary. */
5808 if (DECL_TEMPLATE_INFO (tmpl))
5809 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5810
5811 info = build_template_info (tmpl, args);
5812
5813 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5814 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5815 else
5816 {
5817 if (is_primary)
5818 retrofit_lang_decl (decl);
5819 if (DECL_LANG_SPECIFIC (decl))
5820 DECL_TEMPLATE_INFO (decl) = info;
5821 }
5822
5823 if (flag_implicit_templates
5824 && !is_friend
5825 && TREE_PUBLIC (decl)
5826 && VAR_OR_FUNCTION_DECL_P (decl))
5827 /* Set DECL_COMDAT on template instantiations; if we force
5828 them to be emitted by explicit instantiation or -frepo,
5829 mark_needed will tell cgraph to do the right thing. */
5830 DECL_COMDAT (decl) = true;
5831
5832 return DECL_TEMPLATE_RESULT (tmpl);
5833 }
5834
5835 tree
5836 push_template_decl (tree decl)
5837 {
5838 return push_template_decl_real (decl, false);
5839 }
5840
5841 /* FN is an inheriting constructor that inherits from the constructor
5842 template INHERITED; turn FN into a constructor template with a matching
5843 template header. */
5844
5845 tree
5846 add_inherited_template_parms (tree fn, tree inherited)
5847 {
5848 tree inner_parms
5849 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5850 inner_parms = copy_node (inner_parms);
5851 tree parms
5852 = tree_cons (size_int (processing_template_decl + 1),
5853 inner_parms, current_template_parms);
5854 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5855 tree args = template_parms_to_args (parms);
5856 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5857 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5858 DECL_TEMPLATE_RESULT (tmpl) = fn;
5859 DECL_ARTIFICIAL (tmpl) = true;
5860 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5861 return tmpl;
5862 }
5863
5864 /* Called when a class template TYPE is redeclared with the indicated
5865 template PARMS, e.g.:
5866
5867 template <class T> struct S;
5868 template <class T> struct S {}; */
5869
5870 bool
5871 redeclare_class_template (tree type, tree parms, tree cons)
5872 {
5873 tree tmpl;
5874 tree tmpl_parms;
5875 int i;
5876
5877 if (!TYPE_TEMPLATE_INFO (type))
5878 {
5879 error ("%qT is not a template type", type);
5880 return false;
5881 }
5882
5883 tmpl = TYPE_TI_TEMPLATE (type);
5884 if (!PRIMARY_TEMPLATE_P (tmpl))
5885 /* The type is nested in some template class. Nothing to worry
5886 about here; there are no new template parameters for the nested
5887 type. */
5888 return true;
5889
5890 if (!parms)
5891 {
5892 error ("template specifiers not specified in declaration of %qD",
5893 tmpl);
5894 return false;
5895 }
5896
5897 parms = INNERMOST_TEMPLATE_PARMS (parms);
5898 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5899
5900 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5901 {
5902 error_n (input_location, TREE_VEC_LENGTH (parms),
5903 "redeclared with %d template parameter",
5904 "redeclared with %d template parameters",
5905 TREE_VEC_LENGTH (parms));
5906 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5907 "previous declaration %qD used %d template parameter",
5908 "previous declaration %qD used %d template parameters",
5909 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5910 return false;
5911 }
5912
5913 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5914 {
5915 tree tmpl_parm;
5916 tree parm;
5917 tree tmpl_default;
5918 tree parm_default;
5919
5920 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5921 || TREE_VEC_ELT (parms, i) == error_mark_node)
5922 continue;
5923
5924 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5925 if (error_operand_p (tmpl_parm))
5926 return false;
5927
5928 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5929 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5930 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5931
5932 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5933 TEMPLATE_DECL. */
5934 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5935 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5936 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5937 || (TREE_CODE (tmpl_parm) != PARM_DECL
5938 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5939 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5940 || (TREE_CODE (tmpl_parm) == PARM_DECL
5941 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5942 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5943 {
5944 error ("template parameter %q+#D", tmpl_parm);
5945 error ("redeclared here as %q#D", parm);
5946 return false;
5947 }
5948
5949 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5950 {
5951 /* We have in [temp.param]:
5952
5953 A template-parameter may not be given default arguments
5954 by two different declarations in the same scope. */
5955 error_at (input_location, "redefinition of default argument for %q#D", parm);
5956 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5957 "original definition appeared here");
5958 return false;
5959 }
5960
5961 if (parm_default != NULL_TREE)
5962 /* Update the previous template parameters (which are the ones
5963 that will really count) with the new default value. */
5964 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5965 else if (tmpl_default != NULL_TREE)
5966 /* Update the new parameters, too; they'll be used as the
5967 parameters for any members. */
5968 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5969
5970 /* Give each template template parm in this redeclaration a
5971 DECL_CONTEXT of the template for which they are a parameter. */
5972 if (TREE_CODE (parm) == TEMPLATE_DECL)
5973 {
5974 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5975 DECL_CONTEXT (parm) = tmpl;
5976 }
5977
5978 if (TREE_CODE (parm) == TYPE_DECL)
5979 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5980 }
5981
5982 // Cannot redeclare a class template with a different set of constraints.
5983 if (!equivalent_constraints (get_constraints (tmpl), cons))
5984 {
5985 error_at (input_location, "redeclaration %q#D with different "
5986 "constraints", tmpl);
5987 inform (DECL_SOURCE_LOCATION (tmpl),
5988 "original declaration appeared here");
5989 }
5990
5991 return true;
5992 }
5993
5994 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5995 to be used when the caller has already checked
5996 (processing_template_decl
5997 && !instantiation_dependent_expression_p (expr)
5998 && potential_constant_expression (expr))
5999 and cleared processing_template_decl. */
6000
6001 tree
6002 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6003 {
6004 return tsubst_copy_and_build (expr,
6005 /*args=*/NULL_TREE,
6006 complain,
6007 /*in_decl=*/NULL_TREE,
6008 /*function_p=*/false,
6009 /*integral_constant_expression_p=*/true);
6010 }
6011
6012 /* Simplify EXPR if it is a non-dependent expression. Returns the
6013 (possibly simplified) expression. */
6014
6015 tree
6016 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6017 {
6018 if (expr == NULL_TREE)
6019 return NULL_TREE;
6020
6021 /* If we're in a template, but EXPR isn't value dependent, simplify
6022 it. We're supposed to treat:
6023
6024 template <typename T> void f(T[1 + 1]);
6025 template <typename T> void f(T[2]);
6026
6027 as two declarations of the same function, for example. */
6028 if (processing_template_decl
6029 && is_nondependent_constant_expression (expr))
6030 {
6031 processing_template_decl_sentinel s;
6032 expr = instantiate_non_dependent_expr_internal (expr, complain);
6033 }
6034 return expr;
6035 }
6036
6037 tree
6038 instantiate_non_dependent_expr (tree expr)
6039 {
6040 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6041 }
6042
6043 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6044 an uninstantiated expression. */
6045
6046 tree
6047 instantiate_non_dependent_or_null (tree expr)
6048 {
6049 if (expr == NULL_TREE)
6050 return NULL_TREE;
6051 if (processing_template_decl)
6052 {
6053 if (!is_nondependent_constant_expression (expr))
6054 expr = NULL_TREE;
6055 else
6056 {
6057 processing_template_decl_sentinel s;
6058 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6059 }
6060 }
6061 return expr;
6062 }
6063
6064 /* True iff T is a specialization of a variable template. */
6065
6066 bool
6067 variable_template_specialization_p (tree t)
6068 {
6069 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6070 return false;
6071 tree tmpl = DECL_TI_TEMPLATE (t);
6072 return variable_template_p (tmpl);
6073 }
6074
6075 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6076 template declaration, or a TYPE_DECL for an alias declaration. */
6077
6078 bool
6079 alias_type_or_template_p (tree t)
6080 {
6081 if (t == NULL_TREE)
6082 return false;
6083 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6084 || (TYPE_P (t)
6085 && TYPE_NAME (t)
6086 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6087 || DECL_ALIAS_TEMPLATE_P (t));
6088 }
6089
6090 /* Return TRUE iff T is a specialization of an alias template. */
6091
6092 bool
6093 alias_template_specialization_p (const_tree t)
6094 {
6095 /* It's an alias template specialization if it's an alias and its
6096 TYPE_NAME is a specialization of a primary template. */
6097 if (TYPE_ALIAS_P (t))
6098 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6099 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6100
6101 return false;
6102 }
6103
6104 /* An alias template is complex from a SFINAE perspective if a template-id
6105 using that alias can be ill-formed when the expansion is not, as with
6106 the void_t template. We determine this by checking whether the
6107 expansion for the alias template uses all its template parameters. */
6108
6109 struct uses_all_template_parms_data
6110 {
6111 int level;
6112 bool *seen;
6113 };
6114
6115 static int
6116 uses_all_template_parms_r (tree t, void *data_)
6117 {
6118 struct uses_all_template_parms_data &data
6119 = *(struct uses_all_template_parms_data*)data_;
6120 tree idx = get_template_parm_index (t);
6121
6122 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6123 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6124 return 0;
6125 }
6126
6127 static bool
6128 complex_alias_template_p (const_tree tmpl)
6129 {
6130 struct uses_all_template_parms_data data;
6131 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6132 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6133 data.level = TMPL_PARMS_DEPTH (parms);
6134 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6135 data.seen = XALLOCAVEC (bool, len);
6136 for (int i = 0; i < len; ++i)
6137 data.seen[i] = false;
6138
6139 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6140 for (int i = 0; i < len; ++i)
6141 if (!data.seen[i])
6142 return true;
6143 return false;
6144 }
6145
6146 /* Return TRUE iff T is a specialization of a complex alias template with
6147 dependent template-arguments. */
6148
6149 bool
6150 dependent_alias_template_spec_p (const_tree t)
6151 {
6152 if (!alias_template_specialization_p (t))
6153 return false;
6154
6155 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6156 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6157 return false;
6158
6159 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6160 if (!any_dependent_template_arguments_p (args))
6161 return false;
6162
6163 return true;
6164 }
6165
6166 /* Return the number of innermost template parameters in TMPL. */
6167
6168 static int
6169 num_innermost_template_parms (tree tmpl)
6170 {
6171 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6172 return TREE_VEC_LENGTH (parms);
6173 }
6174
6175 /* Return either TMPL or another template that it is equivalent to under DR
6176 1286: An alias that just changes the name of a template is equivalent to
6177 the other template. */
6178
6179 static tree
6180 get_underlying_template (tree tmpl)
6181 {
6182 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6183 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6184 {
6185 /* Determine if the alias is equivalent to an underlying template. */
6186 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6187 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6188 if (!tinfo)
6189 break;
6190
6191 tree underlying = TI_TEMPLATE (tinfo);
6192 if (!PRIMARY_TEMPLATE_P (underlying)
6193 || (num_innermost_template_parms (tmpl)
6194 != num_innermost_template_parms (underlying)))
6195 break;
6196
6197 tree alias_args = INNERMOST_TEMPLATE_ARGS
6198 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6199 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6200 break;
6201
6202 /* Alias is equivalent. Strip it and repeat. */
6203 tmpl = underlying;
6204 }
6205
6206 return tmpl;
6207 }
6208
6209 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6210 must be a reference-to-function or a pointer-to-function type, as specified
6211 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6212 and check that the resulting function has external linkage. */
6213
6214 static tree
6215 convert_nontype_argument_function (tree type, tree expr,
6216 tsubst_flags_t complain)
6217 {
6218 tree fns = expr;
6219 tree fn, fn_no_ptr;
6220 linkage_kind linkage;
6221
6222 fn = instantiate_type (type, fns, tf_none);
6223 if (fn == error_mark_node)
6224 return error_mark_node;
6225
6226 if (value_dependent_expression_p (fn))
6227 goto accept;
6228
6229 fn_no_ptr = strip_fnptr_conv (fn);
6230 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6231 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6232 if (BASELINK_P (fn_no_ptr))
6233 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6234
6235 /* [temp.arg.nontype]/1
6236
6237 A template-argument for a non-type, non-template template-parameter
6238 shall be one of:
6239 [...]
6240 -- the address of an object or function with external [C++11: or
6241 internal] linkage. */
6242
6243 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6244 {
6245 if (complain & tf_error)
6246 {
6247 error ("%qE is not a valid template argument for type %qT",
6248 expr, type);
6249 if (TYPE_PTR_P (type))
6250 inform (input_location, "it must be the address of a function "
6251 "with external linkage");
6252 else
6253 inform (input_location, "it must be the name of a function with "
6254 "external linkage");
6255 }
6256 return NULL_TREE;
6257 }
6258
6259 linkage = decl_linkage (fn_no_ptr);
6260 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6261 {
6262 if (complain & tf_error)
6263 {
6264 if (cxx_dialect >= cxx11)
6265 error ("%qE is not a valid template argument for type %qT "
6266 "because %qD has no linkage",
6267 expr, type, fn_no_ptr);
6268 else
6269 error ("%qE is not a valid template argument for type %qT "
6270 "because %qD does not have external linkage",
6271 expr, type, fn_no_ptr);
6272 }
6273 return NULL_TREE;
6274 }
6275
6276 accept:
6277 if (TREE_CODE (type) == REFERENCE_TYPE)
6278 {
6279 if (REFERENCE_REF_P (fn))
6280 fn = TREE_OPERAND (fn, 0);
6281 else
6282 fn = build_address (fn);
6283 }
6284 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6285 fn = build_nop (type, fn);
6286
6287 return fn;
6288 }
6289
6290 /* Subroutine of convert_nontype_argument.
6291 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6292 Emit an error otherwise. */
6293
6294 static bool
6295 check_valid_ptrmem_cst_expr (tree type, tree expr,
6296 tsubst_flags_t complain)
6297 {
6298 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6299 tree orig_expr = expr;
6300 STRIP_NOPS (expr);
6301 if (null_ptr_cst_p (expr))
6302 return true;
6303 if (TREE_CODE (expr) == PTRMEM_CST
6304 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6305 PTRMEM_CST_CLASS (expr)))
6306 return true;
6307 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6308 return true;
6309 if (processing_template_decl
6310 && TREE_CODE (expr) == ADDR_EXPR
6311 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6312 return true;
6313 if (complain & tf_error)
6314 {
6315 error_at (loc, "%qE is not a valid template argument for type %qT",
6316 orig_expr, type);
6317 if (TREE_CODE (expr) != PTRMEM_CST)
6318 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6319 else
6320 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6321 }
6322 return false;
6323 }
6324
6325 /* Returns TRUE iff the address of OP is value-dependent.
6326
6327 14.6.2.4 [temp.dep.temp]:
6328 A non-integral non-type template-argument is dependent if its type is
6329 dependent or it has either of the following forms
6330 qualified-id
6331 & qualified-id
6332 and contains a nested-name-specifier which specifies a class-name that
6333 names a dependent type.
6334
6335 We generalize this to just say that the address of a member of a
6336 dependent class is value-dependent; the above doesn't cover the
6337 address of a static data member named with an unqualified-id. */
6338
6339 static bool
6340 has_value_dependent_address (tree op)
6341 {
6342 /* We could use get_inner_reference here, but there's no need;
6343 this is only relevant for template non-type arguments, which
6344 can only be expressed as &id-expression. */
6345 if (DECL_P (op))
6346 {
6347 tree ctx = CP_DECL_CONTEXT (op);
6348 if (TYPE_P (ctx) && dependent_type_p (ctx))
6349 return true;
6350 }
6351
6352 return false;
6353 }
6354
6355 /* The next set of functions are used for providing helpful explanatory
6356 diagnostics for failed overload resolution. Their messages should be
6357 indented by two spaces for consistency with the messages in
6358 call.c */
6359
6360 static int
6361 unify_success (bool /*explain_p*/)
6362 {
6363 return 0;
6364 }
6365
6366 /* Other failure functions should call this one, to provide a single function
6367 for setting a breakpoint on. */
6368
6369 static int
6370 unify_invalid (bool /*explain_p*/)
6371 {
6372 return 1;
6373 }
6374
6375 static int
6376 unify_parameter_deduction_failure (bool explain_p, tree parm)
6377 {
6378 if (explain_p)
6379 inform (input_location,
6380 " couldn't deduce template parameter %qD", parm);
6381 return unify_invalid (explain_p);
6382 }
6383
6384 static int
6385 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6386 {
6387 if (explain_p)
6388 inform (input_location,
6389 " types %qT and %qT have incompatible cv-qualifiers",
6390 parm, arg);
6391 return unify_invalid (explain_p);
6392 }
6393
6394 static int
6395 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6396 {
6397 if (explain_p)
6398 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6399 return unify_invalid (explain_p);
6400 }
6401
6402 static int
6403 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6404 {
6405 if (explain_p)
6406 inform (input_location,
6407 " template parameter %qD is not a parameter pack, but "
6408 "argument %qD is",
6409 parm, arg);
6410 return unify_invalid (explain_p);
6411 }
6412
6413 static int
6414 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6415 {
6416 if (explain_p)
6417 inform (input_location,
6418 " template argument %qE does not match "
6419 "pointer-to-member constant %qE",
6420 arg, parm);
6421 return unify_invalid (explain_p);
6422 }
6423
6424 static int
6425 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6426 {
6427 if (explain_p)
6428 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6429 return unify_invalid (explain_p);
6430 }
6431
6432 static int
6433 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6434 {
6435 if (explain_p)
6436 inform (input_location,
6437 " inconsistent parameter pack deduction with %qT and %qT",
6438 old_arg, new_arg);
6439 return unify_invalid (explain_p);
6440 }
6441
6442 static int
6443 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6444 {
6445 if (explain_p)
6446 {
6447 if (TYPE_P (parm))
6448 inform (input_location,
6449 " deduced conflicting types for parameter %qT (%qT and %qT)",
6450 parm, first, second);
6451 else
6452 inform (input_location,
6453 " deduced conflicting values for non-type parameter "
6454 "%qE (%qE and %qE)", parm, first, second);
6455 }
6456 return unify_invalid (explain_p);
6457 }
6458
6459 static int
6460 unify_vla_arg (bool explain_p, tree arg)
6461 {
6462 if (explain_p)
6463 inform (input_location,
6464 " variable-sized array type %qT is not "
6465 "a valid template argument",
6466 arg);
6467 return unify_invalid (explain_p);
6468 }
6469
6470 static int
6471 unify_method_type_error (bool explain_p, tree arg)
6472 {
6473 if (explain_p)
6474 inform (input_location,
6475 " member function type %qT is not a valid template argument",
6476 arg);
6477 return unify_invalid (explain_p);
6478 }
6479
6480 static int
6481 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6482 {
6483 if (explain_p)
6484 {
6485 if (least_p)
6486 inform_n (input_location, wanted,
6487 " candidate expects at least %d argument, %d provided",
6488 " candidate expects at least %d arguments, %d provided",
6489 wanted, have);
6490 else
6491 inform_n (input_location, wanted,
6492 " candidate expects %d argument, %d provided",
6493 " candidate expects %d arguments, %d provided",
6494 wanted, have);
6495 }
6496 return unify_invalid (explain_p);
6497 }
6498
6499 static int
6500 unify_too_many_arguments (bool explain_p, int have, int wanted)
6501 {
6502 return unify_arity (explain_p, have, wanted);
6503 }
6504
6505 static int
6506 unify_too_few_arguments (bool explain_p, int have, int wanted,
6507 bool least_p = false)
6508 {
6509 return unify_arity (explain_p, have, wanted, least_p);
6510 }
6511
6512 static int
6513 unify_arg_conversion (bool explain_p, tree to_type,
6514 tree from_type, tree arg)
6515 {
6516 if (explain_p)
6517 inform (EXPR_LOC_OR_LOC (arg, input_location),
6518 " cannot convert %qE (type %qT) to type %qT",
6519 arg, from_type, to_type);
6520 return unify_invalid (explain_p);
6521 }
6522
6523 static int
6524 unify_no_common_base (bool explain_p, enum template_base_result r,
6525 tree parm, tree arg)
6526 {
6527 if (explain_p)
6528 switch (r)
6529 {
6530 case tbr_ambiguous_baseclass:
6531 inform (input_location, " %qT is an ambiguous base class of %qT",
6532 parm, arg);
6533 break;
6534 default:
6535 inform (input_location, " %qT is not derived from %qT", arg, parm);
6536 break;
6537 }
6538 return unify_invalid (explain_p);
6539 }
6540
6541 static int
6542 unify_inconsistent_template_template_parameters (bool explain_p)
6543 {
6544 if (explain_p)
6545 inform (input_location,
6546 " template parameters of a template template argument are "
6547 "inconsistent with other deduced template arguments");
6548 return unify_invalid (explain_p);
6549 }
6550
6551 static int
6552 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6553 {
6554 if (explain_p)
6555 inform (input_location,
6556 " can't deduce a template for %qT from non-template type %qT",
6557 parm, arg);
6558 return unify_invalid (explain_p);
6559 }
6560
6561 static int
6562 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6563 {
6564 if (explain_p)
6565 inform (input_location,
6566 " template argument %qE does not match %qE", arg, parm);
6567 return unify_invalid (explain_p);
6568 }
6569
6570 /* Attempt to convert the non-type template parameter EXPR to the
6571 indicated TYPE. If the conversion is successful, return the
6572 converted value. If the conversion is unsuccessful, return
6573 NULL_TREE if we issued an error message, or error_mark_node if we
6574 did not. We issue error messages for out-and-out bad template
6575 parameters, but not simply because the conversion failed, since we
6576 might be just trying to do argument deduction. Both TYPE and EXPR
6577 must be non-dependent.
6578
6579 The conversion follows the special rules described in
6580 [temp.arg.nontype], and it is much more strict than an implicit
6581 conversion.
6582
6583 This function is called twice for each template argument (see
6584 lookup_template_class for a more accurate description of this
6585 problem). This means that we need to handle expressions which
6586 are not valid in a C++ source, but can be created from the
6587 first call (for instance, casts to perform conversions). These
6588 hacks can go away after we fix the double coercion problem. */
6589
6590 static tree
6591 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6592 {
6593 tree expr_type;
6594 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6595 tree orig_expr = expr;
6596
6597 /* Detect immediately string literals as invalid non-type argument.
6598 This special-case is not needed for correctness (we would easily
6599 catch this later), but only to provide better diagnostic for this
6600 common user mistake. As suggested by DR 100, we do not mention
6601 linkage issues in the diagnostic as this is not the point. */
6602 /* FIXME we're making this OK. */
6603 if (TREE_CODE (expr) == STRING_CST)
6604 {
6605 if (complain & tf_error)
6606 error ("%qE is not a valid template argument for type %qT "
6607 "because string literals can never be used in this context",
6608 expr, type);
6609 return NULL_TREE;
6610 }
6611
6612 /* Add the ADDR_EXPR now for the benefit of
6613 value_dependent_expression_p. */
6614 if (TYPE_PTROBV_P (type)
6615 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6616 {
6617 expr = decay_conversion (expr, complain);
6618 if (expr == error_mark_node)
6619 return error_mark_node;
6620 }
6621
6622 /* If we are in a template, EXPR may be non-dependent, but still
6623 have a syntactic, rather than semantic, form. For example, EXPR
6624 might be a SCOPE_REF, rather than the VAR_DECL to which the
6625 SCOPE_REF refers. Preserving the qualifying scope is necessary
6626 so that access checking can be performed when the template is
6627 instantiated -- but here we need the resolved form so that we can
6628 convert the argument. */
6629 bool non_dep = false;
6630 if (TYPE_REF_OBJ_P (type)
6631 && has_value_dependent_address (expr))
6632 /* If we want the address and it's value-dependent, don't fold. */;
6633 else if (processing_template_decl
6634 && is_nondependent_constant_expression (expr))
6635 non_dep = true;
6636 if (error_operand_p (expr))
6637 return error_mark_node;
6638 expr_type = TREE_TYPE (expr);
6639
6640 /* If the argument is non-dependent, perform any conversions in
6641 non-dependent context as well. */
6642 processing_template_decl_sentinel s (non_dep);
6643 if (non_dep)
6644 expr = instantiate_non_dependent_expr_internal (expr, complain);
6645
6646 if (value_dependent_expression_p (expr))
6647 expr = canonicalize_expr_argument (expr, complain);
6648
6649 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6650 to a non-type argument of "nullptr". */
6651 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6652 expr = fold_simple (convert (type, expr));
6653
6654 /* In C++11, integral or enumeration non-type template arguments can be
6655 arbitrary constant expressions. Pointer and pointer to
6656 member arguments can be general constant expressions that evaluate
6657 to a null value, but otherwise still need to be of a specific form. */
6658 if (cxx_dialect >= cxx11)
6659 {
6660 if (TREE_CODE (expr) == PTRMEM_CST)
6661 /* A PTRMEM_CST is already constant, and a valid template
6662 argument for a parameter of pointer to member type, we just want
6663 to leave it in that form rather than lower it to a
6664 CONSTRUCTOR. */;
6665 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6666 || cxx_dialect >= cxx17)
6667 {
6668 /* C++17: A template-argument for a non-type template-parameter shall
6669 be a converted constant expression (8.20) of the type of the
6670 template-parameter. */
6671 expr = build_converted_constant_expr (type, expr, complain);
6672 if (expr == error_mark_node)
6673 return error_mark_node;
6674 expr = maybe_constant_value (expr);
6675 expr = convert_from_reference (expr);
6676 }
6677 else if (TYPE_PTR_OR_PTRMEM_P (type))
6678 {
6679 tree folded = maybe_constant_value (expr);
6680 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6681 : null_member_pointer_value_p (folded))
6682 expr = folded;
6683 }
6684 }
6685
6686 if (TREE_CODE (type) == REFERENCE_TYPE)
6687 expr = mark_lvalue_use (expr);
6688 else
6689 expr = mark_rvalue_use (expr);
6690
6691 /* HACK: Due to double coercion, we can get a
6692 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6693 which is the tree that we built on the first call (see
6694 below when coercing to reference to object or to reference to
6695 function). We just strip everything and get to the arg.
6696 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6697 for examples. */
6698 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6699 {
6700 tree probe_type, probe = expr;
6701 if (REFERENCE_REF_P (probe))
6702 probe = TREE_OPERAND (probe, 0);
6703 probe_type = TREE_TYPE (probe);
6704 if (TREE_CODE (probe) == NOP_EXPR)
6705 {
6706 /* ??? Maybe we could use convert_from_reference here, but we
6707 would need to relax its constraints because the NOP_EXPR
6708 could actually change the type to something more cv-qualified,
6709 and this is not folded by convert_from_reference. */
6710 tree addr = TREE_OPERAND (probe, 0);
6711 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6712 && TREE_CODE (addr) == ADDR_EXPR
6713 && TYPE_PTR_P (TREE_TYPE (addr))
6714 && (same_type_ignoring_top_level_qualifiers_p
6715 (TREE_TYPE (probe_type),
6716 TREE_TYPE (TREE_TYPE (addr)))))
6717 {
6718 expr = TREE_OPERAND (addr, 0);
6719 expr_type = TREE_TYPE (probe_type);
6720 }
6721 }
6722 }
6723
6724 /* [temp.arg.nontype]/5, bullet 1
6725
6726 For a non-type template-parameter of integral or enumeration type,
6727 integral promotions (_conv.prom_) and integral conversions
6728 (_conv.integral_) are applied. */
6729 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6730 {
6731 if (cxx_dialect < cxx11)
6732 {
6733 tree t = build_converted_constant_expr (type, expr, complain);
6734 t = maybe_constant_value (t);
6735 if (t != error_mark_node)
6736 expr = t;
6737 }
6738
6739 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6740 return error_mark_node;
6741
6742 /* Notice that there are constant expressions like '4 % 0' which
6743 do not fold into integer constants. */
6744 if (TREE_CODE (expr) != INTEGER_CST
6745 && !value_dependent_expression_p (expr))
6746 {
6747 if (complain & tf_error)
6748 {
6749 int errs = errorcount, warns = warningcount + werrorcount;
6750 if (!require_potential_constant_expression (expr))
6751 expr = error_mark_node;
6752 else
6753 expr = cxx_constant_value (expr);
6754 if (errorcount > errs || warningcount + werrorcount > warns)
6755 inform (loc, "in template argument for type %qT ", type);
6756 if (expr == error_mark_node)
6757 return NULL_TREE;
6758 /* else cxx_constant_value complained but gave us
6759 a real constant, so go ahead. */
6760 if (TREE_CODE (expr) != INTEGER_CST)
6761 {
6762 /* Some assemble time constant expressions like
6763 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6764 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6765 as we can emit them into .rodata initializers of
6766 variables, yet they can't fold into an INTEGER_CST at
6767 compile time. Refuse them here. */
6768 gcc_checking_assert (reduced_constant_expression_p (expr));
6769 error_at (loc, "template argument %qE for type %qT not "
6770 "a constant integer", expr, type);
6771 return NULL_TREE;
6772 }
6773 }
6774 else
6775 return NULL_TREE;
6776 }
6777
6778 /* Avoid typedef problems. */
6779 if (TREE_TYPE (expr) != type)
6780 expr = fold_convert (type, expr);
6781 }
6782 /* [temp.arg.nontype]/5, bullet 2
6783
6784 For a non-type template-parameter of type pointer to object,
6785 qualification conversions (_conv.qual_) and the array-to-pointer
6786 conversion (_conv.array_) are applied. */
6787 else if (TYPE_PTROBV_P (type))
6788 {
6789 tree decayed = expr;
6790
6791 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6792 decay_conversion or an explicit cast. If it's a problematic cast,
6793 we'll complain about it below. */
6794 if (TREE_CODE (expr) == NOP_EXPR)
6795 {
6796 tree probe = expr;
6797 STRIP_NOPS (probe);
6798 if (TREE_CODE (probe) == ADDR_EXPR
6799 && TYPE_PTR_P (TREE_TYPE (probe)))
6800 {
6801 expr = probe;
6802 expr_type = TREE_TYPE (expr);
6803 }
6804 }
6805
6806 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6807
6808 A template-argument for a non-type, non-template template-parameter
6809 shall be one of: [...]
6810
6811 -- the name of a non-type template-parameter;
6812 -- the address of an object or function with external linkage, [...]
6813 expressed as "& id-expression" where the & is optional if the name
6814 refers to a function or array, or if the corresponding
6815 template-parameter is a reference.
6816
6817 Here, we do not care about functions, as they are invalid anyway
6818 for a parameter of type pointer-to-object. */
6819
6820 if (value_dependent_expression_p (expr))
6821 /* Non-type template parameters are OK. */
6822 ;
6823 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6824 /* Null pointer values are OK in C++11. */;
6825 else if (TREE_CODE (expr) != ADDR_EXPR)
6826 {
6827 if (VAR_P (expr))
6828 {
6829 if (complain & tf_error)
6830 error ("%qD is not a valid template argument "
6831 "because %qD is a variable, not the address of "
6832 "a variable", orig_expr, expr);
6833 return NULL_TREE;
6834 }
6835 if (POINTER_TYPE_P (expr_type))
6836 {
6837 if (complain & tf_error)
6838 error ("%qE is not a valid template argument for %qT "
6839 "because it is not the address of a variable",
6840 orig_expr, type);
6841 return NULL_TREE;
6842 }
6843 /* Other values, like integer constants, might be valid
6844 non-type arguments of some other type. */
6845 return error_mark_node;
6846 }
6847 else
6848 {
6849 tree decl = TREE_OPERAND (expr, 0);
6850
6851 if (!VAR_P (decl))
6852 {
6853 if (complain & tf_error)
6854 error ("%qE is not a valid template argument of type %qT "
6855 "because %qE is not a variable", orig_expr, type, decl);
6856 return NULL_TREE;
6857 }
6858 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6859 {
6860 if (complain & tf_error)
6861 error ("%qE is not a valid template argument of type %qT "
6862 "because %qD does not have external linkage",
6863 orig_expr, type, decl);
6864 return NULL_TREE;
6865 }
6866 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6867 && decl_linkage (decl) == lk_none)
6868 {
6869 if (complain & tf_error)
6870 error ("%qE is not a valid template argument of type %qT "
6871 "because %qD has no linkage", orig_expr, type, decl);
6872 return NULL_TREE;
6873 }
6874 /* C++17: For a non-type template-parameter of reference or pointer
6875 type, the value of the constant expression shall not refer to (or
6876 for a pointer type, shall not be the address of):
6877 * a subobject (4.5),
6878 * a temporary object (15.2),
6879 * a string literal (5.13.5),
6880 * the result of a typeid expression (8.2.8), or
6881 * a predefined __func__ variable (11.4.1). */
6882 else if (DECL_ARTIFICIAL (decl))
6883 {
6884 if (complain & tf_error)
6885 error ("the address of %qD is not a valid template argument",
6886 decl);
6887 return NULL_TREE;
6888 }
6889 else if (!same_type_ignoring_top_level_qualifiers_p
6890 (strip_array_types (TREE_TYPE (type)),
6891 strip_array_types (TREE_TYPE (decl))))
6892 {
6893 if (complain & tf_error)
6894 error ("the address of the %qT subobject of %qD is not a "
6895 "valid template argument", TREE_TYPE (type), decl);
6896 return NULL_TREE;
6897 }
6898 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6899 {
6900 if (complain & tf_error)
6901 error ("the address of %qD is not a valid template argument "
6902 "because it does not have static storage duration",
6903 decl);
6904 return NULL_TREE;
6905 }
6906 }
6907
6908 expr = decayed;
6909
6910 expr = perform_qualification_conversions (type, expr);
6911 if (expr == error_mark_node)
6912 return error_mark_node;
6913 }
6914 /* [temp.arg.nontype]/5, bullet 3
6915
6916 For a non-type template-parameter of type reference to object, no
6917 conversions apply. The type referred to by the reference may be more
6918 cv-qualified than the (otherwise identical) type of the
6919 template-argument. The template-parameter is bound directly to the
6920 template-argument, which must be an lvalue. */
6921 else if (TYPE_REF_OBJ_P (type))
6922 {
6923 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6924 expr_type))
6925 return error_mark_node;
6926
6927 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6928 {
6929 if (complain & tf_error)
6930 error ("%qE is not a valid template argument for type %qT "
6931 "because of conflicts in cv-qualification", expr, type);
6932 return NULL_TREE;
6933 }
6934
6935 if (!glvalue_p (expr)
6936 || TYPE_REF_IS_RVALUE (type) != xvalue_p (expr))
6937 {
6938 if (complain & tf_error)
6939 {
6940 if (TYPE_REF_IS_RVALUE (type))
6941 error ("%qE is not a valid template argument for type %qT "
6942 "because it is not an xvalue", expr, type);
6943 else
6944 error ("%qE is not a valid template argument for type %qT "
6945 "because it is not an lvalue", expr, type);
6946 }
6947 return NULL_TREE;
6948 }
6949
6950 /* [temp.arg.nontype]/1
6951
6952 A template-argument for a non-type, non-template template-parameter
6953 shall be one of: [...]
6954
6955 -- the address of an object or function with external linkage. */
6956 if (INDIRECT_REF_P (expr)
6957 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6958 {
6959 expr = TREE_OPERAND (expr, 0);
6960 if (DECL_P (expr))
6961 {
6962 if (complain & tf_error)
6963 error ("%q#D is not a valid template argument for type %qT "
6964 "because a reference variable does not have a constant "
6965 "address", expr, type);
6966 return NULL_TREE;
6967 }
6968 }
6969
6970 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6971 && value_dependent_expression_p (expr))
6972 /* OK, dependent reference. We don't want to ask whether a DECL is
6973 itself value-dependent, since what we want here is its address. */;
6974 else
6975 {
6976 if (!DECL_P (expr))
6977 {
6978 if (complain & tf_error)
6979 error ("%qE is not a valid template argument for type %qT "
6980 "because it is not an object with linkage",
6981 expr, type);
6982 return NULL_TREE;
6983 }
6984
6985 /* DR 1155 allows internal linkage in C++11 and up. */
6986 linkage_kind linkage = decl_linkage (expr);
6987 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6988 {
6989 if (complain & tf_error)
6990 error ("%qE is not a valid template argument for type %qT "
6991 "because object %qD does not have linkage",
6992 expr, type, expr);
6993 return NULL_TREE;
6994 }
6995
6996 expr = build_address (expr);
6997 }
6998
6999 if (!same_type_p (type, TREE_TYPE (expr)))
7000 expr = build_nop (type, expr);
7001 }
7002 /* [temp.arg.nontype]/5, bullet 4
7003
7004 For a non-type template-parameter of type pointer to function, only
7005 the function-to-pointer conversion (_conv.func_) is applied. If the
7006 template-argument represents a set of overloaded functions (or a
7007 pointer to such), the matching function is selected from the set
7008 (_over.over_). */
7009 else if (TYPE_PTRFN_P (type))
7010 {
7011 /* If the argument is a template-id, we might not have enough
7012 context information to decay the pointer. */
7013 if (!type_unknown_p (expr_type))
7014 {
7015 expr = decay_conversion (expr, complain);
7016 if (expr == error_mark_node)
7017 return error_mark_node;
7018 }
7019
7020 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7021 /* Null pointer values are OK in C++11. */
7022 return perform_qualification_conversions (type, expr);
7023
7024 expr = convert_nontype_argument_function (type, expr, complain);
7025 if (!expr || expr == error_mark_node)
7026 return expr;
7027 }
7028 /* [temp.arg.nontype]/5, bullet 5
7029
7030 For a non-type template-parameter of type reference to function, no
7031 conversions apply. If the template-argument represents a set of
7032 overloaded functions, the matching function is selected from the set
7033 (_over.over_). */
7034 else if (TYPE_REFFN_P (type))
7035 {
7036 if (TREE_CODE (expr) == ADDR_EXPR)
7037 {
7038 if (complain & tf_error)
7039 {
7040 error ("%qE is not a valid template argument for type %qT "
7041 "because it is a pointer", expr, type);
7042 inform (input_location, "try using %qE instead",
7043 TREE_OPERAND (expr, 0));
7044 }
7045 return NULL_TREE;
7046 }
7047
7048 expr = convert_nontype_argument_function (type, expr, complain);
7049 if (!expr || expr == error_mark_node)
7050 return expr;
7051 }
7052 /* [temp.arg.nontype]/5, bullet 6
7053
7054 For a non-type template-parameter of type pointer to member function,
7055 no conversions apply. If the template-argument represents a set of
7056 overloaded member functions, the matching member function is selected
7057 from the set (_over.over_). */
7058 else if (TYPE_PTRMEMFUNC_P (type))
7059 {
7060 expr = instantiate_type (type, expr, tf_none);
7061 if (expr == error_mark_node)
7062 return error_mark_node;
7063
7064 /* [temp.arg.nontype] bullet 1 says the pointer to member
7065 expression must be a pointer-to-member constant. */
7066 if (!value_dependent_expression_p (expr)
7067 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7068 return NULL_TREE;
7069
7070 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7071 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7072 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7073 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7074 }
7075 /* [temp.arg.nontype]/5, bullet 7
7076
7077 For a non-type template-parameter of type pointer to data member,
7078 qualification conversions (_conv.qual_) are applied. */
7079 else if (TYPE_PTRDATAMEM_P (type))
7080 {
7081 /* [temp.arg.nontype] bullet 1 says the pointer to member
7082 expression must be a pointer-to-member constant. */
7083 if (!value_dependent_expression_p (expr)
7084 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7085 return NULL_TREE;
7086
7087 expr = perform_qualification_conversions (type, expr);
7088 if (expr == error_mark_node)
7089 return expr;
7090 }
7091 else if (NULLPTR_TYPE_P (type))
7092 {
7093 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7094 {
7095 if (complain & tf_error)
7096 error ("%qE is not a valid template argument for type %qT "
7097 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7098 return NULL_TREE;
7099 }
7100 return expr;
7101 }
7102 /* A template non-type parameter must be one of the above. */
7103 else
7104 gcc_unreachable ();
7105
7106 /* Sanity check: did we actually convert the argument to the
7107 right type? */
7108 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7109 (type, TREE_TYPE (expr)));
7110 return convert_from_reference (expr);
7111 }
7112
7113 /* Subroutine of coerce_template_template_parms, which returns 1 if
7114 PARM_PARM and ARG_PARM match using the rule for the template
7115 parameters of template template parameters. Both PARM and ARG are
7116 template parameters; the rest of the arguments are the same as for
7117 coerce_template_template_parms.
7118 */
7119 static int
7120 coerce_template_template_parm (tree parm,
7121 tree arg,
7122 tsubst_flags_t complain,
7123 tree in_decl,
7124 tree outer_args)
7125 {
7126 if (arg == NULL_TREE || error_operand_p (arg)
7127 || parm == NULL_TREE || error_operand_p (parm))
7128 return 0;
7129
7130 if (TREE_CODE (arg) != TREE_CODE (parm))
7131 return 0;
7132
7133 switch (TREE_CODE (parm))
7134 {
7135 case TEMPLATE_DECL:
7136 /* We encounter instantiations of templates like
7137 template <template <template <class> class> class TT>
7138 class C; */
7139 {
7140 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7141 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7142
7143 if (!coerce_template_template_parms
7144 (parmparm, argparm, complain, in_decl, outer_args))
7145 return 0;
7146 }
7147 /* Fall through. */
7148
7149 case TYPE_DECL:
7150 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7151 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7152 /* Argument is a parameter pack but parameter is not. */
7153 return 0;
7154 break;
7155
7156 case PARM_DECL:
7157 /* The tsubst call is used to handle cases such as
7158
7159 template <int> class C {};
7160 template <class T, template <T> class TT> class D {};
7161 D<int, C> d;
7162
7163 i.e. the parameter list of TT depends on earlier parameters. */
7164 if (!uses_template_parms (TREE_TYPE (arg)))
7165 {
7166 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7167 if (!uses_template_parms (t)
7168 && !same_type_p (t, TREE_TYPE (arg)))
7169 return 0;
7170 }
7171
7172 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7173 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7174 /* Argument is a parameter pack but parameter is not. */
7175 return 0;
7176
7177 break;
7178
7179 default:
7180 gcc_unreachable ();
7181 }
7182
7183 return 1;
7184 }
7185
7186 /* Coerce template argument list ARGLIST for use with template
7187 template-parameter TEMPL. */
7188
7189 static tree
7190 coerce_template_args_for_ttp (tree templ, tree arglist,
7191 tsubst_flags_t complain)
7192 {
7193 /* Consider an example where a template template parameter declared as
7194
7195 template <class T, class U = std::allocator<T> > class TT
7196
7197 The template parameter level of T and U are one level larger than
7198 of TT. To proper process the default argument of U, say when an
7199 instantiation `TT<int>' is seen, we need to build the full
7200 arguments containing {int} as the innermost level. Outer levels,
7201 available when not appearing as default template argument, can be
7202 obtained from the arguments of the enclosing template.
7203
7204 Suppose that TT is later substituted with std::vector. The above
7205 instantiation is `TT<int, std::allocator<T> >' with TT at
7206 level 1, and T at level 2, while the template arguments at level 1
7207 becomes {std::vector} and the inner level 2 is {int}. */
7208
7209 tree outer = DECL_CONTEXT (templ);
7210 if (outer)
7211 {
7212 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7213 /* We want arguments for the partial specialization, not arguments for
7214 the primary template. */
7215 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7216 else
7217 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7218 }
7219 else if (current_template_parms)
7220 {
7221 /* This is an argument of the current template, so we haven't set
7222 DECL_CONTEXT yet. */
7223 tree relevant_template_parms;
7224
7225 /* Parameter levels that are greater than the level of the given
7226 template template parm are irrelevant. */
7227 relevant_template_parms = current_template_parms;
7228 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7229 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7230 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7231
7232 outer = template_parms_to_args (relevant_template_parms);
7233 }
7234
7235 if (outer)
7236 arglist = add_to_template_args (outer, arglist);
7237
7238 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7239 return coerce_template_parms (parmlist, arglist, templ,
7240 complain,
7241 /*require_all_args=*/true,
7242 /*use_default_args=*/true);
7243 }
7244
7245 /* A cache of template template parameters with match-all default
7246 arguments. */
7247 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7248 static void
7249 store_defaulted_ttp (tree v, tree t)
7250 {
7251 if (!defaulted_ttp_cache)
7252 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7253 defaulted_ttp_cache->put (v, t);
7254 }
7255 static tree
7256 lookup_defaulted_ttp (tree v)
7257 {
7258 if (defaulted_ttp_cache)
7259 if (tree *p = defaulted_ttp_cache->get (v))
7260 return *p;
7261 return NULL_TREE;
7262 }
7263
7264 /* T is a bound template template-parameter. Copy its arguments into default
7265 arguments of the template template-parameter's template parameters. */
7266
7267 static tree
7268 add_defaults_to_ttp (tree otmpl)
7269 {
7270 if (tree c = lookup_defaulted_ttp (otmpl))
7271 return c;
7272
7273 tree ntmpl = copy_node (otmpl);
7274
7275 tree ntype = copy_node (TREE_TYPE (otmpl));
7276 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7277 TYPE_MAIN_VARIANT (ntype) = ntype;
7278 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7279 TYPE_NAME (ntype) = ntmpl;
7280 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7281
7282 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7283 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7284 TEMPLATE_PARM_DECL (idx) = ntmpl;
7285 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7286
7287 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7288 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7289 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7290 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7291 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7292 {
7293 tree o = TREE_VEC_ELT (vec, i);
7294 if (!template_parameter_pack_p (TREE_VALUE (o)))
7295 {
7296 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7297 TREE_PURPOSE (n) = any_targ_node;
7298 }
7299 }
7300
7301 store_defaulted_ttp (otmpl, ntmpl);
7302 return ntmpl;
7303 }
7304
7305 /* ARG is a bound potential template template-argument, and PARGS is a list
7306 of arguments for the corresponding template template-parameter. Adjust
7307 PARGS as appropriate for application to ARG's template, and if ARG is a
7308 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7309 arguments to the template template parameter. */
7310
7311 static tree
7312 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7313 {
7314 ++processing_template_decl;
7315 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7316 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7317 {
7318 /* When comparing two template template-parameters in partial ordering,
7319 rewrite the one currently being used as an argument to have default
7320 arguments for all parameters. */
7321 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7322 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7323 if (pargs != error_mark_node)
7324 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7325 TYPE_TI_ARGS (arg));
7326 }
7327 else
7328 {
7329 tree aparms
7330 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7331 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7332 /*require_all*/true,
7333 /*use_default*/true);
7334 }
7335 --processing_template_decl;
7336 return pargs;
7337 }
7338
7339 /* Subroutine of unify for the case when PARM is a
7340 BOUND_TEMPLATE_TEMPLATE_PARM. */
7341
7342 static int
7343 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7344 bool explain_p)
7345 {
7346 tree parmvec = TYPE_TI_ARGS (parm);
7347 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7348
7349 /* The template template parm might be variadic and the argument
7350 not, so flatten both argument lists. */
7351 parmvec = expand_template_argument_pack (parmvec);
7352 argvec = expand_template_argument_pack (argvec);
7353
7354 if (flag_new_ttp)
7355 {
7356 /* In keeping with P0522R0, adjust P's template arguments
7357 to apply to A's template; then flatten it again. */
7358 tree nparmvec = parmvec;
7359 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7360 nparmvec = expand_template_argument_pack (nparmvec);
7361
7362 if (unify (tparms, targs, nparmvec, argvec,
7363 UNIFY_ALLOW_NONE, explain_p))
7364 return 1;
7365
7366 /* If the P0522 adjustment eliminated a pack expansion, deduce
7367 empty packs. */
7368 if (flag_new_ttp
7369 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7370 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7371 DEDUCE_EXACT, /*sub*/true, explain_p))
7372 return 1;
7373 }
7374 else
7375 {
7376 /* Deduce arguments T, i from TT<T> or TT<i>.
7377 We check each element of PARMVEC and ARGVEC individually
7378 rather than the whole TREE_VEC since they can have
7379 different number of elements, which is allowed under N2555. */
7380
7381 int len = TREE_VEC_LENGTH (parmvec);
7382
7383 /* Check if the parameters end in a pack, making them
7384 variadic. */
7385 int parm_variadic_p = 0;
7386 if (len > 0
7387 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7388 parm_variadic_p = 1;
7389
7390 for (int i = 0; i < len - parm_variadic_p; ++i)
7391 /* If the template argument list of P contains a pack
7392 expansion that is not the last template argument, the
7393 entire template argument list is a non-deduced
7394 context. */
7395 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7396 return unify_success (explain_p);
7397
7398 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7399 return unify_too_few_arguments (explain_p,
7400 TREE_VEC_LENGTH (argvec), len);
7401
7402 for (int i = 0; i < len - parm_variadic_p; ++i)
7403 if (unify (tparms, targs,
7404 TREE_VEC_ELT (parmvec, i),
7405 TREE_VEC_ELT (argvec, i),
7406 UNIFY_ALLOW_NONE, explain_p))
7407 return 1;
7408
7409 if (parm_variadic_p
7410 && unify_pack_expansion (tparms, targs,
7411 parmvec, argvec,
7412 DEDUCE_EXACT,
7413 /*subr=*/true, explain_p))
7414 return 1;
7415 }
7416
7417 return 0;
7418 }
7419
7420 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7421 template template parameters. Both PARM_PARMS and ARG_PARMS are
7422 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7423 or PARM_DECL.
7424
7425 Consider the example:
7426 template <class T> class A;
7427 template<template <class U> class TT> class B;
7428
7429 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7430 the parameters to A, and OUTER_ARGS contains A. */
7431
7432 static int
7433 coerce_template_template_parms (tree parm_parms,
7434 tree arg_parms,
7435 tsubst_flags_t complain,
7436 tree in_decl,
7437 tree outer_args)
7438 {
7439 int nparms, nargs, i;
7440 tree parm, arg;
7441 int variadic_p = 0;
7442
7443 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7444 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7445
7446 nparms = TREE_VEC_LENGTH (parm_parms);
7447 nargs = TREE_VEC_LENGTH (arg_parms);
7448
7449 if (flag_new_ttp)
7450 {
7451 /* P0522R0: A template template-parameter P is at least as specialized as
7452 a template template-argument A if, given the following rewrite to two
7453 function templates, the function template corresponding to P is at
7454 least as specialized as the function template corresponding to A
7455 according to the partial ordering rules for function templates
7456 ([temp.func.order]). Given an invented class template X with the
7457 template parameter list of A (including default arguments):
7458
7459 * Each of the two function templates has the same template parameters,
7460 respectively, as P or A.
7461
7462 * Each function template has a single function parameter whose type is
7463 a specialization of X with template arguments corresponding to the
7464 template parameters from the respective function template where, for
7465 each template parameter PP in the template parameter list of the
7466 function template, a corresponding template argument AA is formed. If
7467 PP declares a parameter pack, then AA is the pack expansion
7468 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7469
7470 If the rewrite produces an invalid type, then P is not at least as
7471 specialized as A. */
7472
7473 /* So coerce P's args to apply to A's parms, and then deduce between A's
7474 args and the converted args. If that succeeds, A is at least as
7475 specialized as P, so they match.*/
7476 tree pargs = template_parms_level_to_args (parm_parms);
7477 ++processing_template_decl;
7478 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7479 /*require_all*/true, /*use_default*/true);
7480 --processing_template_decl;
7481 if (pargs != error_mark_node)
7482 {
7483 tree targs = make_tree_vec (nargs);
7484 tree aargs = template_parms_level_to_args (arg_parms);
7485 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7486 /*explain*/false))
7487 return 1;
7488 }
7489 }
7490
7491 /* Determine whether we have a parameter pack at the end of the
7492 template template parameter's template parameter list. */
7493 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7494 {
7495 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7496
7497 if (error_operand_p (parm))
7498 return 0;
7499
7500 switch (TREE_CODE (parm))
7501 {
7502 case TEMPLATE_DECL:
7503 case TYPE_DECL:
7504 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7505 variadic_p = 1;
7506 break;
7507
7508 case PARM_DECL:
7509 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7510 variadic_p = 1;
7511 break;
7512
7513 default:
7514 gcc_unreachable ();
7515 }
7516 }
7517
7518 if (nargs != nparms
7519 && !(variadic_p && nargs >= nparms - 1))
7520 return 0;
7521
7522 /* Check all of the template parameters except the parameter pack at
7523 the end (if any). */
7524 for (i = 0; i < nparms - variadic_p; ++i)
7525 {
7526 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7527 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7528 continue;
7529
7530 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7531 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7532
7533 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7534 outer_args))
7535 return 0;
7536
7537 }
7538
7539 if (variadic_p)
7540 {
7541 /* Check each of the template parameters in the template
7542 argument against the template parameter pack at the end of
7543 the template template parameter. */
7544 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7545 return 0;
7546
7547 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7548
7549 for (; i < nargs; ++i)
7550 {
7551 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7552 continue;
7553
7554 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7555
7556 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7557 outer_args))
7558 return 0;
7559 }
7560 }
7561
7562 return 1;
7563 }
7564
7565 /* Verifies that the deduced template arguments (in TARGS) for the
7566 template template parameters (in TPARMS) represent valid bindings,
7567 by comparing the template parameter list of each template argument
7568 to the template parameter list of its corresponding template
7569 template parameter, in accordance with DR150. This
7570 routine can only be called after all template arguments have been
7571 deduced. It will return TRUE if all of the template template
7572 parameter bindings are okay, FALSE otherwise. */
7573 bool
7574 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7575 {
7576 int i, ntparms = TREE_VEC_LENGTH (tparms);
7577 bool ret = true;
7578
7579 /* We're dealing with template parms in this process. */
7580 ++processing_template_decl;
7581
7582 targs = INNERMOST_TEMPLATE_ARGS (targs);
7583
7584 for (i = 0; i < ntparms; ++i)
7585 {
7586 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7587 tree targ = TREE_VEC_ELT (targs, i);
7588
7589 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7590 {
7591 tree packed_args = NULL_TREE;
7592 int idx, len = 1;
7593
7594 if (ARGUMENT_PACK_P (targ))
7595 {
7596 /* Look inside the argument pack. */
7597 packed_args = ARGUMENT_PACK_ARGS (targ);
7598 len = TREE_VEC_LENGTH (packed_args);
7599 }
7600
7601 for (idx = 0; idx < len; ++idx)
7602 {
7603 tree targ_parms = NULL_TREE;
7604
7605 if (packed_args)
7606 /* Extract the next argument from the argument
7607 pack. */
7608 targ = TREE_VEC_ELT (packed_args, idx);
7609
7610 if (PACK_EXPANSION_P (targ))
7611 /* Look at the pattern of the pack expansion. */
7612 targ = PACK_EXPANSION_PATTERN (targ);
7613
7614 /* Extract the template parameters from the template
7615 argument. */
7616 if (TREE_CODE (targ) == TEMPLATE_DECL)
7617 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7618 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7619 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7620
7621 /* Verify that we can coerce the template template
7622 parameters from the template argument to the template
7623 parameter. This requires an exact match. */
7624 if (targ_parms
7625 && !coerce_template_template_parms
7626 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7627 targ_parms,
7628 tf_none,
7629 tparm,
7630 targs))
7631 {
7632 ret = false;
7633 goto out;
7634 }
7635 }
7636 }
7637 }
7638
7639 out:
7640
7641 --processing_template_decl;
7642 return ret;
7643 }
7644
7645 /* Since type attributes aren't mangled, we need to strip them from
7646 template type arguments. */
7647
7648 static tree
7649 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7650 {
7651 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7652 return arg;
7653 bool removed_attributes = false;
7654 tree canon = strip_typedefs (arg, &removed_attributes);
7655 if (removed_attributes
7656 && (complain & tf_warning))
7657 warning (OPT_Wignored_attributes,
7658 "ignoring attributes on template argument %qT", arg);
7659 return canon;
7660 }
7661
7662 /* And from inside dependent non-type arguments like sizeof(Type). */
7663
7664 static tree
7665 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7666 {
7667 if (!arg || arg == error_mark_node)
7668 return arg;
7669 bool removed_attributes = false;
7670 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7671 if (removed_attributes
7672 && (complain & tf_warning))
7673 warning (OPT_Wignored_attributes,
7674 "ignoring attributes in template argument %qE", arg);
7675 return canon;
7676 }
7677
7678 // A template declaration can be substituted for a constrained
7679 // template template parameter only when the argument is more
7680 // constrained than the parameter.
7681 static bool
7682 is_compatible_template_arg (tree parm, tree arg)
7683 {
7684 tree parm_cons = get_constraints (parm);
7685
7686 /* For now, allow constrained template template arguments
7687 and unconstrained template template parameters. */
7688 if (parm_cons == NULL_TREE)
7689 return true;
7690
7691 tree arg_cons = get_constraints (arg);
7692
7693 // If the template parameter is constrained, we need to rewrite its
7694 // constraints in terms of the ARG's template parameters. This ensures
7695 // that all of the template parameter types will have the same depth.
7696 //
7697 // Note that this is only valid when coerce_template_template_parm is
7698 // true for the innermost template parameters of PARM and ARG. In other
7699 // words, because coercion is successful, this conversion will be valid.
7700 if (parm_cons)
7701 {
7702 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7703 parm_cons = tsubst_constraint_info (parm_cons,
7704 INNERMOST_TEMPLATE_ARGS (args),
7705 tf_none, NULL_TREE);
7706 if (parm_cons == error_mark_node)
7707 return false;
7708 }
7709
7710 return subsumes (parm_cons, arg_cons);
7711 }
7712
7713 // Convert a placeholder argument into a binding to the original
7714 // parameter. The original parameter is saved as the TREE_TYPE of
7715 // ARG.
7716 static inline tree
7717 convert_wildcard_argument (tree parm, tree arg)
7718 {
7719 TREE_TYPE (arg) = parm;
7720 return arg;
7721 }
7722
7723 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7724 because one of them is dependent. But we need to represent the
7725 conversion for the benefit of cp_tree_equal. */
7726
7727 static tree
7728 maybe_convert_nontype_argument (tree type, tree arg)
7729 {
7730 /* Auto parms get no conversion. */
7731 if (type_uses_auto (type))
7732 return arg;
7733 /* We don't need or want to add this conversion now if we're going to use the
7734 argument for deduction. */
7735 if (value_dependent_expression_p (arg))
7736 return arg;
7737
7738 type = cv_unqualified (type);
7739 tree argtype = TREE_TYPE (arg);
7740 if (same_type_p (type, argtype))
7741 return arg;
7742
7743 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7744 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7745 return arg;
7746 }
7747
7748 /* Convert the indicated template ARG as necessary to match the
7749 indicated template PARM. Returns the converted ARG, or
7750 error_mark_node if the conversion was unsuccessful. Error and
7751 warning messages are issued under control of COMPLAIN. This
7752 conversion is for the Ith parameter in the parameter list. ARGS is
7753 the full set of template arguments deduced so far. */
7754
7755 static tree
7756 convert_template_argument (tree parm,
7757 tree arg,
7758 tree args,
7759 tsubst_flags_t complain,
7760 int i,
7761 tree in_decl)
7762 {
7763 tree orig_arg;
7764 tree val;
7765 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7766
7767 if (parm == error_mark_node)
7768 return error_mark_node;
7769
7770 /* Trivially convert placeholders. */
7771 if (TREE_CODE (arg) == WILDCARD_DECL)
7772 return convert_wildcard_argument (parm, arg);
7773
7774 if (arg == any_targ_node)
7775 return arg;
7776
7777 if (TREE_CODE (arg) == TREE_LIST
7778 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7779 {
7780 /* The template argument was the name of some
7781 member function. That's usually
7782 invalid, but static members are OK. In any
7783 case, grab the underlying fields/functions
7784 and issue an error later if required. */
7785 orig_arg = TREE_VALUE (arg);
7786 TREE_TYPE (arg) = unknown_type_node;
7787 }
7788
7789 orig_arg = arg;
7790
7791 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7792 requires_type = (TREE_CODE (parm) == TYPE_DECL
7793 || requires_tmpl_type);
7794
7795 /* When determining whether an argument pack expansion is a template,
7796 look at the pattern. */
7797 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7798 arg = PACK_EXPANSION_PATTERN (arg);
7799
7800 /* Deal with an injected-class-name used as a template template arg. */
7801 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7802 {
7803 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7804 if (TREE_CODE (t) == TEMPLATE_DECL)
7805 {
7806 if (cxx_dialect >= cxx11)
7807 /* OK under DR 1004. */;
7808 else if (complain & tf_warning_or_error)
7809 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7810 " used as template template argument", TYPE_NAME (arg));
7811 else if (flag_pedantic_errors)
7812 t = arg;
7813
7814 arg = t;
7815 }
7816 }
7817
7818 is_tmpl_type =
7819 ((TREE_CODE (arg) == TEMPLATE_DECL
7820 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7821 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7822 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7823 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7824
7825 if (is_tmpl_type
7826 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7827 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7828 arg = TYPE_STUB_DECL (arg);
7829
7830 is_type = TYPE_P (arg) || is_tmpl_type;
7831
7832 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7833 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7834 {
7835 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7836 {
7837 if (complain & tf_error)
7838 error ("invalid use of destructor %qE as a type", orig_arg);
7839 return error_mark_node;
7840 }
7841
7842 permerror (input_location,
7843 "to refer to a type member of a template parameter, "
7844 "use %<typename %E%>", orig_arg);
7845
7846 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7847 TREE_OPERAND (arg, 1),
7848 typename_type,
7849 complain);
7850 arg = orig_arg;
7851 is_type = 1;
7852 }
7853 if (is_type != requires_type)
7854 {
7855 if (in_decl)
7856 {
7857 if (complain & tf_error)
7858 {
7859 error ("type/value mismatch at argument %d in template "
7860 "parameter list for %qD",
7861 i + 1, in_decl);
7862 if (is_type)
7863 inform (input_location,
7864 " expected a constant of type %qT, got %qT",
7865 TREE_TYPE (parm),
7866 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7867 else if (requires_tmpl_type)
7868 inform (input_location,
7869 " expected a class template, got %qE", orig_arg);
7870 else
7871 inform (input_location,
7872 " expected a type, got %qE", orig_arg);
7873 }
7874 }
7875 return error_mark_node;
7876 }
7877 if (is_tmpl_type ^ requires_tmpl_type)
7878 {
7879 if (in_decl && (complain & tf_error))
7880 {
7881 error ("type/value mismatch at argument %d in template "
7882 "parameter list for %qD",
7883 i + 1, in_decl);
7884 if (is_tmpl_type)
7885 inform (input_location,
7886 " expected a type, got %qT", DECL_NAME (arg));
7887 else
7888 inform (input_location,
7889 " expected a class template, got %qT", orig_arg);
7890 }
7891 return error_mark_node;
7892 }
7893
7894 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7895 /* We already did the appropriate conversion when packing args. */
7896 val = orig_arg;
7897 else if (is_type)
7898 {
7899 if (requires_tmpl_type)
7900 {
7901 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7902 /* The number of argument required is not known yet.
7903 Just accept it for now. */
7904 val = orig_arg;
7905 else
7906 {
7907 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7908 tree argparm;
7909
7910 /* Strip alias templates that are equivalent to another
7911 template. */
7912 arg = get_underlying_template (arg);
7913 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7914
7915 if (coerce_template_template_parms (parmparm, argparm,
7916 complain, in_decl,
7917 args))
7918 {
7919 val = arg;
7920
7921 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7922 TEMPLATE_DECL. */
7923 if (val != error_mark_node)
7924 {
7925 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7926 val = TREE_TYPE (val);
7927 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7928 val = make_pack_expansion (val, complain);
7929 }
7930 }
7931 else
7932 {
7933 if (in_decl && (complain & tf_error))
7934 {
7935 error ("type/value mismatch at argument %d in "
7936 "template parameter list for %qD",
7937 i + 1, in_decl);
7938 inform (input_location,
7939 " expected a template of type %qD, got %qT",
7940 parm, orig_arg);
7941 }
7942
7943 val = error_mark_node;
7944 }
7945
7946 // Check that the constraints are compatible before allowing the
7947 // substitution.
7948 if (val != error_mark_node)
7949 if (!is_compatible_template_arg (parm, arg))
7950 {
7951 if (in_decl && (complain & tf_error))
7952 {
7953 error ("constraint mismatch at argument %d in "
7954 "template parameter list for %qD",
7955 i + 1, in_decl);
7956 inform (input_location, " expected %qD but got %qD",
7957 parm, arg);
7958 }
7959 val = error_mark_node;
7960 }
7961 }
7962 }
7963 else
7964 val = orig_arg;
7965 /* We only form one instance of each template specialization.
7966 Therefore, if we use a non-canonical variant (i.e., a
7967 typedef), any future messages referring to the type will use
7968 the typedef, which is confusing if those future uses do not
7969 themselves also use the typedef. */
7970 if (TYPE_P (val))
7971 val = canonicalize_type_argument (val, complain);
7972 }
7973 else
7974 {
7975 tree t = TREE_TYPE (parm);
7976
7977 if (tree a = type_uses_auto (t))
7978 {
7979 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7980 if (t == error_mark_node)
7981 return error_mark_node;
7982 }
7983 else
7984 t = tsubst (t, args, complain, in_decl);
7985
7986 if (invalid_nontype_parm_type_p (t, complain))
7987 return error_mark_node;
7988
7989 if (!type_dependent_expression_p (orig_arg)
7990 && !uses_template_parms (t))
7991 /* We used to call digest_init here. However, digest_init
7992 will report errors, which we don't want when complain
7993 is zero. More importantly, digest_init will try too
7994 hard to convert things: for example, `0' should not be
7995 converted to pointer type at this point according to
7996 the standard. Accepting this is not merely an
7997 extension, since deciding whether or not these
7998 conversions can occur is part of determining which
7999 function template to call, or whether a given explicit
8000 argument specification is valid. */
8001 val = convert_nontype_argument (t, orig_arg, complain);
8002 else
8003 {
8004 val = canonicalize_expr_argument (orig_arg, complain);
8005 val = maybe_convert_nontype_argument (t, val);
8006 }
8007
8008
8009 if (val == NULL_TREE)
8010 val = error_mark_node;
8011 else if (val == error_mark_node && (complain & tf_error))
8012 error ("could not convert template argument %qE from %qT to %qT",
8013 orig_arg, TREE_TYPE (orig_arg), t);
8014
8015 if (INDIRECT_REF_P (val))
8016 {
8017 /* Reject template arguments that are references to built-in
8018 functions with no library fallbacks. */
8019 const_tree inner = TREE_OPERAND (val, 0);
8020 const_tree innertype = TREE_TYPE (inner);
8021 if (innertype
8022 && TREE_CODE (innertype) == REFERENCE_TYPE
8023 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8024 && TREE_OPERAND_LENGTH (inner) > 0
8025 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8026 return error_mark_node;
8027 }
8028
8029 if (TREE_CODE (val) == SCOPE_REF)
8030 {
8031 /* Strip typedefs from the SCOPE_REF. */
8032 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8033 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8034 complain);
8035 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8036 QUALIFIED_NAME_IS_TEMPLATE (val));
8037 }
8038 }
8039
8040 return val;
8041 }
8042
8043 /* Coerces the remaining template arguments in INNER_ARGS (from
8044 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8045 Returns the coerced argument pack. PARM_IDX is the position of this
8046 parameter in the template parameter list. ARGS is the original
8047 template argument list. */
8048 static tree
8049 coerce_template_parameter_pack (tree parms,
8050 int parm_idx,
8051 tree args,
8052 tree inner_args,
8053 int arg_idx,
8054 tree new_args,
8055 int* lost,
8056 tree in_decl,
8057 tsubst_flags_t complain)
8058 {
8059 tree parm = TREE_VEC_ELT (parms, parm_idx);
8060 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8061 tree packed_args;
8062 tree argument_pack;
8063 tree packed_parms = NULL_TREE;
8064
8065 if (arg_idx > nargs)
8066 arg_idx = nargs;
8067
8068 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8069 {
8070 /* When the template parameter is a non-type template parameter pack
8071 or template template parameter pack whose type or template
8072 parameters use parameter packs, we know exactly how many arguments
8073 we are looking for. Build a vector of the instantiated decls for
8074 these template parameters in PACKED_PARMS. */
8075 /* We can't use make_pack_expansion here because it would interpret a
8076 _DECL as a use rather than a declaration. */
8077 tree decl = TREE_VALUE (parm);
8078 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8079 SET_PACK_EXPANSION_PATTERN (exp, decl);
8080 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8081 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8082
8083 TREE_VEC_LENGTH (args)--;
8084 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8085 TREE_VEC_LENGTH (args)++;
8086
8087 if (packed_parms == error_mark_node)
8088 return error_mark_node;
8089
8090 /* If we're doing a partial instantiation of a member template,
8091 verify that all of the types used for the non-type
8092 template parameter pack are, in fact, valid for non-type
8093 template parameters. */
8094 if (arg_idx < nargs
8095 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8096 {
8097 int j, len = TREE_VEC_LENGTH (packed_parms);
8098 for (j = 0; j < len; ++j)
8099 {
8100 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8101 if (invalid_nontype_parm_type_p (t, complain))
8102 return error_mark_node;
8103 }
8104 /* We don't know how many args we have yet, just
8105 use the unconverted ones for now. */
8106 return NULL_TREE;
8107 }
8108
8109 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8110 }
8111 /* Check if we have a placeholder pack, which indicates we're
8112 in the context of a introduction list. In that case we want
8113 to match this pack to the single placeholder. */
8114 else if (arg_idx < nargs
8115 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8116 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8117 {
8118 nargs = arg_idx + 1;
8119 packed_args = make_tree_vec (1);
8120 }
8121 else
8122 packed_args = make_tree_vec (nargs - arg_idx);
8123
8124 /* Convert the remaining arguments, which will be a part of the
8125 parameter pack "parm". */
8126 int first_pack_arg = arg_idx;
8127 for (; arg_idx < nargs; ++arg_idx)
8128 {
8129 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8130 tree actual_parm = TREE_VALUE (parm);
8131 int pack_idx = arg_idx - first_pack_arg;
8132
8133 if (packed_parms)
8134 {
8135 /* Once we've packed as many args as we have types, stop. */
8136 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8137 break;
8138 else if (PACK_EXPANSION_P (arg))
8139 /* We don't know how many args we have yet, just
8140 use the unconverted ones for now. */
8141 return NULL_TREE;
8142 else
8143 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8144 }
8145
8146 if (arg == error_mark_node)
8147 {
8148 if (complain & tf_error)
8149 error ("template argument %d is invalid", arg_idx + 1);
8150 }
8151 else
8152 arg = convert_template_argument (actual_parm,
8153 arg, new_args, complain, parm_idx,
8154 in_decl);
8155 if (arg == error_mark_node)
8156 (*lost)++;
8157 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8158 }
8159
8160 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8161 && TREE_VEC_LENGTH (packed_args) > 0)
8162 {
8163 if (complain & tf_error)
8164 error ("wrong number of template arguments (%d, should be %d)",
8165 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8166 return error_mark_node;
8167 }
8168
8169 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8170 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8171 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8172 else
8173 {
8174 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8175 TREE_CONSTANT (argument_pack) = 1;
8176 }
8177
8178 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8179 if (CHECKING_P)
8180 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8181 TREE_VEC_LENGTH (packed_args));
8182 return argument_pack;
8183 }
8184
8185 /* Returns the number of pack expansions in the template argument vector
8186 ARGS. */
8187
8188 static int
8189 pack_expansion_args_count (tree args)
8190 {
8191 int i;
8192 int count = 0;
8193 if (args)
8194 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8195 {
8196 tree elt = TREE_VEC_ELT (args, i);
8197 if (elt && PACK_EXPANSION_P (elt))
8198 ++count;
8199 }
8200 return count;
8201 }
8202
8203 /* Convert all template arguments to their appropriate types, and
8204 return a vector containing the innermost resulting template
8205 arguments. If any error occurs, return error_mark_node. Error and
8206 warning messages are issued under control of COMPLAIN.
8207
8208 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8209 for arguments not specified in ARGS. Otherwise, if
8210 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8211 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8212 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8213 ARGS. */
8214
8215 static tree
8216 coerce_template_parms (tree parms,
8217 tree args,
8218 tree in_decl,
8219 tsubst_flags_t complain,
8220 bool require_all_args,
8221 bool use_default_args)
8222 {
8223 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8224 tree orig_inner_args;
8225 tree inner_args;
8226 tree new_args;
8227 tree new_inner_args;
8228 int saved_unevaluated_operand;
8229 int saved_inhibit_evaluation_warnings;
8230
8231 /* When used as a boolean value, indicates whether this is a
8232 variadic template parameter list. Since it's an int, we can also
8233 subtract it from nparms to get the number of non-variadic
8234 parameters. */
8235 int variadic_p = 0;
8236 int variadic_args_p = 0;
8237 int post_variadic_parms = 0;
8238
8239 /* Likewise for parameters with default arguments. */
8240 int default_p = 0;
8241
8242 if (args == error_mark_node)
8243 return error_mark_node;
8244
8245 nparms = TREE_VEC_LENGTH (parms);
8246
8247 /* Determine if there are any parameter packs or default arguments. */
8248 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8249 {
8250 tree parm = TREE_VEC_ELT (parms, parm_idx);
8251 if (variadic_p)
8252 ++post_variadic_parms;
8253 if (template_parameter_pack_p (TREE_VALUE (parm)))
8254 ++variadic_p;
8255 if (TREE_PURPOSE (parm))
8256 ++default_p;
8257 }
8258
8259 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8260 /* If there are no parameters that follow a parameter pack, we need to
8261 expand any argument packs so that we can deduce a parameter pack from
8262 some non-packed args followed by an argument pack, as in variadic85.C.
8263 If there are such parameters, we need to leave argument packs intact
8264 so the arguments are assigned properly. This can happen when dealing
8265 with a nested class inside a partial specialization of a class
8266 template, as in variadic92.C, or when deducing a template parameter pack
8267 from a sub-declarator, as in variadic114.C. */
8268 if (!post_variadic_parms)
8269 inner_args = expand_template_argument_pack (inner_args);
8270
8271 /* Count any pack expansion args. */
8272 variadic_args_p = pack_expansion_args_count (inner_args);
8273
8274 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8275 if ((nargs - variadic_args_p > nparms && !variadic_p)
8276 || (nargs < nparms - variadic_p
8277 && require_all_args
8278 && !variadic_args_p
8279 && (!use_default_args
8280 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8281 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8282 {
8283 if (complain & tf_error)
8284 {
8285 if (variadic_p || default_p)
8286 {
8287 nparms -= variadic_p + default_p;
8288 error ("wrong number of template arguments "
8289 "(%d, should be at least %d)", nargs, nparms);
8290 }
8291 else
8292 error ("wrong number of template arguments "
8293 "(%d, should be %d)", nargs, nparms);
8294
8295 if (in_decl)
8296 inform (DECL_SOURCE_LOCATION (in_decl),
8297 "provided for %qD", in_decl);
8298 }
8299
8300 return error_mark_node;
8301 }
8302 /* We can't pass a pack expansion to a non-pack parameter of an alias
8303 template (DR 1430). */
8304 else if (in_decl
8305 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8306 || concept_template_p (in_decl))
8307 && variadic_args_p
8308 && nargs - variadic_args_p < nparms - variadic_p)
8309 {
8310 if (complain & tf_error)
8311 {
8312 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8313 {
8314 tree arg = TREE_VEC_ELT (inner_args, i);
8315 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8316
8317 if (PACK_EXPANSION_P (arg)
8318 && !template_parameter_pack_p (parm))
8319 {
8320 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8321 error_at (location_of (arg),
8322 "pack expansion argument for non-pack parameter "
8323 "%qD of alias template %qD", parm, in_decl);
8324 else
8325 error_at (location_of (arg),
8326 "pack expansion argument for non-pack parameter "
8327 "%qD of concept %qD", parm, in_decl);
8328 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8329 goto found;
8330 }
8331 }
8332 gcc_unreachable ();
8333 found:;
8334 }
8335 return error_mark_node;
8336 }
8337
8338 /* We need to evaluate the template arguments, even though this
8339 template-id may be nested within a "sizeof". */
8340 saved_unevaluated_operand = cp_unevaluated_operand;
8341 cp_unevaluated_operand = 0;
8342 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8343 c_inhibit_evaluation_warnings = 0;
8344 new_inner_args = make_tree_vec (nparms);
8345 new_args = add_outermost_template_args (args, new_inner_args);
8346 int pack_adjust = 0;
8347 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8348 {
8349 tree arg;
8350 tree parm;
8351
8352 /* Get the Ith template parameter. */
8353 parm = TREE_VEC_ELT (parms, parm_idx);
8354
8355 if (parm == error_mark_node)
8356 {
8357 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8358 continue;
8359 }
8360
8361 /* Calculate the next argument. */
8362 if (arg_idx < nargs)
8363 arg = TREE_VEC_ELT (inner_args, arg_idx);
8364 else
8365 arg = NULL_TREE;
8366
8367 if (template_parameter_pack_p (TREE_VALUE (parm))
8368 && !(arg && ARGUMENT_PACK_P (arg)))
8369 {
8370 /* Some arguments will be placed in the
8371 template parameter pack PARM. */
8372 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8373 inner_args, arg_idx,
8374 new_args, &lost,
8375 in_decl, complain);
8376
8377 if (arg == NULL_TREE)
8378 {
8379 /* We don't know how many args we have yet, just use the
8380 unconverted (and still packed) ones for now. */
8381 new_inner_args = orig_inner_args;
8382 arg_idx = nargs;
8383 break;
8384 }
8385
8386 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8387
8388 /* Store this argument. */
8389 if (arg == error_mark_node)
8390 {
8391 lost++;
8392 /* We are done with all of the arguments. */
8393 arg_idx = nargs;
8394 }
8395 else
8396 {
8397 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8398 arg_idx += pack_adjust;
8399 }
8400
8401 continue;
8402 }
8403 else if (arg)
8404 {
8405 if (PACK_EXPANSION_P (arg))
8406 {
8407 /* "If every valid specialization of a variadic template
8408 requires an empty template parameter pack, the template is
8409 ill-formed, no diagnostic required." So check that the
8410 pattern works with this parameter. */
8411 tree pattern = PACK_EXPANSION_PATTERN (arg);
8412 tree conv = convert_template_argument (TREE_VALUE (parm),
8413 pattern, new_args,
8414 complain, parm_idx,
8415 in_decl);
8416 if (conv == error_mark_node)
8417 {
8418 if (complain & tf_error)
8419 inform (input_location, "so any instantiation with a "
8420 "non-empty parameter pack would be ill-formed");
8421 ++lost;
8422 }
8423 else if (TYPE_P (conv) && !TYPE_P (pattern))
8424 /* Recover from missing typename. */
8425 TREE_VEC_ELT (inner_args, arg_idx)
8426 = make_pack_expansion (conv, complain);
8427
8428 /* We don't know how many args we have yet, just
8429 use the unconverted ones for now. */
8430 new_inner_args = inner_args;
8431 arg_idx = nargs;
8432 break;
8433 }
8434 }
8435 else if (require_all_args)
8436 {
8437 /* There must be a default arg in this case. */
8438 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8439 complain, in_decl);
8440 /* The position of the first default template argument,
8441 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8442 Record that. */
8443 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8444 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8445 arg_idx - pack_adjust);
8446 }
8447 else
8448 break;
8449
8450 if (arg == error_mark_node)
8451 {
8452 if (complain & tf_error)
8453 error ("template argument %d is invalid", arg_idx + 1);
8454 }
8455 else if (!arg)
8456 /* This only occurs if there was an error in the template
8457 parameter list itself (which we would already have
8458 reported) that we are trying to recover from, e.g., a class
8459 template with a parameter list such as
8460 template<typename..., typename>. */
8461 ++lost;
8462 else
8463 arg = convert_template_argument (TREE_VALUE (parm),
8464 arg, new_args, complain,
8465 parm_idx, in_decl);
8466
8467 if (arg == error_mark_node)
8468 lost++;
8469 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8470 }
8471 cp_unevaluated_operand = saved_unevaluated_operand;
8472 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8473
8474 if (variadic_p && arg_idx < nargs)
8475 {
8476 if (complain & tf_error)
8477 {
8478 error ("wrong number of template arguments "
8479 "(%d, should be %d)", nargs, arg_idx);
8480 if (in_decl)
8481 error ("provided for %q+D", in_decl);
8482 }
8483 return error_mark_node;
8484 }
8485
8486 if (lost)
8487 return error_mark_node;
8488
8489 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8490 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8491 TREE_VEC_LENGTH (new_inner_args));
8492
8493 return new_inner_args;
8494 }
8495
8496 /* Convert all template arguments to their appropriate types, and
8497 return a vector containing the innermost resulting template
8498 arguments. If any error occurs, return error_mark_node. Error and
8499 warning messages are not issued.
8500
8501 Note that no function argument deduction is performed, and default
8502 arguments are used to fill in unspecified arguments. */
8503 tree
8504 coerce_template_parms (tree parms, tree args, tree in_decl)
8505 {
8506 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8507 }
8508
8509 /* Convert all template arguments to their appropriate type, and
8510 instantiate default arguments as needed. This returns a vector
8511 containing the innermost resulting template arguments, or
8512 error_mark_node if unsuccessful. */
8513 tree
8514 coerce_template_parms (tree parms, tree args, tree in_decl,
8515 tsubst_flags_t complain)
8516 {
8517 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8518 }
8519
8520 /* Like coerce_template_parms. If PARMS represents all template
8521 parameters levels, this function returns a vector of vectors
8522 representing all the resulting argument levels. Note that in this
8523 case, only the innermost arguments are coerced because the
8524 outermost ones are supposed to have been coerced already.
8525
8526 Otherwise, if PARMS represents only (the innermost) vector of
8527 parameters, this function returns a vector containing just the
8528 innermost resulting arguments. */
8529
8530 static tree
8531 coerce_innermost_template_parms (tree parms,
8532 tree args,
8533 tree in_decl,
8534 tsubst_flags_t complain,
8535 bool require_all_args,
8536 bool use_default_args)
8537 {
8538 int parms_depth = TMPL_PARMS_DEPTH (parms);
8539 int args_depth = TMPL_ARGS_DEPTH (args);
8540 tree coerced_args;
8541
8542 if (parms_depth > 1)
8543 {
8544 coerced_args = make_tree_vec (parms_depth);
8545 tree level;
8546 int cur_depth;
8547
8548 for (level = parms, cur_depth = parms_depth;
8549 parms_depth > 0 && level != NULL_TREE;
8550 level = TREE_CHAIN (level), --cur_depth)
8551 {
8552 tree l;
8553 if (cur_depth == args_depth)
8554 l = coerce_template_parms (TREE_VALUE (level),
8555 args, in_decl, complain,
8556 require_all_args,
8557 use_default_args);
8558 else
8559 l = TMPL_ARGS_LEVEL (args, cur_depth);
8560
8561 if (l == error_mark_node)
8562 return error_mark_node;
8563
8564 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8565 }
8566 }
8567 else
8568 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8569 args, in_decl, complain,
8570 require_all_args,
8571 use_default_args);
8572 return coerced_args;
8573 }
8574
8575 /* Returns 1 if template args OT and NT are equivalent. */
8576
8577 int
8578 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8579 {
8580 if (nt == ot)
8581 return 1;
8582 if (nt == NULL_TREE || ot == NULL_TREE)
8583 return false;
8584 if (nt == any_targ_node || ot == any_targ_node)
8585 return true;
8586
8587 if (TREE_CODE (nt) == TREE_VEC)
8588 /* For member templates */
8589 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8590 else if (PACK_EXPANSION_P (ot))
8591 return (PACK_EXPANSION_P (nt)
8592 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8593 PACK_EXPANSION_PATTERN (nt))
8594 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8595 PACK_EXPANSION_EXTRA_ARGS (nt)));
8596 else if (ARGUMENT_PACK_P (ot))
8597 {
8598 int i, len;
8599 tree opack, npack;
8600
8601 if (!ARGUMENT_PACK_P (nt))
8602 return 0;
8603
8604 opack = ARGUMENT_PACK_ARGS (ot);
8605 npack = ARGUMENT_PACK_ARGS (nt);
8606 len = TREE_VEC_LENGTH (opack);
8607 if (TREE_VEC_LENGTH (npack) != len)
8608 return 0;
8609 for (i = 0; i < len; ++i)
8610 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8611 TREE_VEC_ELT (npack, i)))
8612 return 0;
8613 return 1;
8614 }
8615 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8616 gcc_unreachable ();
8617 else if (TYPE_P (nt))
8618 {
8619 if (!TYPE_P (ot))
8620 return false;
8621 /* Don't treat an alias template specialization with dependent
8622 arguments as equivalent to its underlying type when used as a
8623 template argument; we need them to be distinct so that we
8624 substitute into the specialization arguments at instantiation
8625 time. And aliases can't be equivalent without being ==, so
8626 we don't need to look any deeper.
8627
8628 During partial ordering, however, we need to treat them normally so
8629 that we can order uses of the same alias with different
8630 cv-qualification (79960). */
8631 if (!partial_order
8632 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8633 return false;
8634 else
8635 return same_type_p (ot, nt);
8636 }
8637 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8638 return 0;
8639 else
8640 {
8641 /* Try to treat a template non-type argument that has been converted
8642 to the parameter type as equivalent to one that hasn't yet. */
8643 for (enum tree_code code1 = TREE_CODE (ot);
8644 CONVERT_EXPR_CODE_P (code1)
8645 || code1 == NON_LVALUE_EXPR;
8646 code1 = TREE_CODE (ot))
8647 ot = TREE_OPERAND (ot, 0);
8648 for (enum tree_code code2 = TREE_CODE (nt);
8649 CONVERT_EXPR_CODE_P (code2)
8650 || code2 == NON_LVALUE_EXPR;
8651 code2 = TREE_CODE (nt))
8652 nt = TREE_OPERAND (nt, 0);
8653
8654 return cp_tree_equal (ot, nt);
8655 }
8656 }
8657
8658 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8659 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8660 NEWARG_PTR with the offending arguments if they are non-NULL. */
8661
8662 int
8663 comp_template_args (tree oldargs, tree newargs,
8664 tree *oldarg_ptr, tree *newarg_ptr,
8665 bool partial_order)
8666 {
8667 int i;
8668
8669 if (oldargs == newargs)
8670 return 1;
8671
8672 if (!oldargs || !newargs)
8673 return 0;
8674
8675 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8676 return 0;
8677
8678 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8679 {
8680 tree nt = TREE_VEC_ELT (newargs, i);
8681 tree ot = TREE_VEC_ELT (oldargs, i);
8682
8683 if (! template_args_equal (ot, nt, partial_order))
8684 {
8685 if (oldarg_ptr != NULL)
8686 *oldarg_ptr = ot;
8687 if (newarg_ptr != NULL)
8688 *newarg_ptr = nt;
8689 return 0;
8690 }
8691 }
8692 return 1;
8693 }
8694
8695 inline bool
8696 comp_template_args_porder (tree oargs, tree nargs)
8697 {
8698 return comp_template_args (oargs, nargs, NULL, NULL, true);
8699 }
8700
8701 static void
8702 add_pending_template (tree d)
8703 {
8704 tree ti = (TYPE_P (d)
8705 ? CLASSTYPE_TEMPLATE_INFO (d)
8706 : DECL_TEMPLATE_INFO (d));
8707 struct pending_template *pt;
8708 int level;
8709
8710 if (TI_PENDING_TEMPLATE_FLAG (ti))
8711 return;
8712
8713 /* We are called both from instantiate_decl, where we've already had a
8714 tinst_level pushed, and instantiate_template, where we haven't.
8715 Compensate. */
8716 level = !current_tinst_level || current_tinst_level->decl != d;
8717
8718 if (level)
8719 push_tinst_level (d);
8720
8721 pt = ggc_alloc<pending_template> ();
8722 pt->next = NULL;
8723 pt->tinst = current_tinst_level;
8724 if (last_pending_template)
8725 last_pending_template->next = pt;
8726 else
8727 pending_templates = pt;
8728
8729 last_pending_template = pt;
8730
8731 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8732
8733 if (level)
8734 pop_tinst_level ();
8735 }
8736
8737
8738 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8739 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8740 documentation for TEMPLATE_ID_EXPR. */
8741
8742 tree
8743 lookup_template_function (tree fns, tree arglist)
8744 {
8745 tree type;
8746
8747 if (fns == error_mark_node || arglist == error_mark_node)
8748 return error_mark_node;
8749
8750 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8751
8752 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8753 {
8754 error ("%q#D is not a function template", fns);
8755 return error_mark_node;
8756 }
8757
8758 if (BASELINK_P (fns))
8759 {
8760 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8761 unknown_type_node,
8762 BASELINK_FUNCTIONS (fns),
8763 arglist);
8764 return fns;
8765 }
8766
8767 type = TREE_TYPE (fns);
8768 if (TREE_CODE (fns) == OVERLOAD || !type)
8769 type = unknown_type_node;
8770
8771 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8772 }
8773
8774 /* Within the scope of a template class S<T>, the name S gets bound
8775 (in build_self_reference) to a TYPE_DECL for the class, not a
8776 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8777 or one of its enclosing classes, and that type is a template,
8778 return the associated TEMPLATE_DECL. Otherwise, the original
8779 DECL is returned.
8780
8781 Also handle the case when DECL is a TREE_LIST of ambiguous
8782 injected-class-names from different bases. */
8783
8784 tree
8785 maybe_get_template_decl_from_type_decl (tree decl)
8786 {
8787 if (decl == NULL_TREE)
8788 return decl;
8789
8790 /* DR 176: A lookup that finds an injected-class-name (10.2
8791 [class.member.lookup]) can result in an ambiguity in certain cases
8792 (for example, if it is found in more than one base class). If all of
8793 the injected-class-names that are found refer to specializations of
8794 the same class template, and if the name is followed by a
8795 template-argument-list, the reference refers to the class template
8796 itself and not a specialization thereof, and is not ambiguous. */
8797 if (TREE_CODE (decl) == TREE_LIST)
8798 {
8799 tree t, tmpl = NULL_TREE;
8800 for (t = decl; t; t = TREE_CHAIN (t))
8801 {
8802 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8803 if (!tmpl)
8804 tmpl = elt;
8805 else if (tmpl != elt)
8806 break;
8807 }
8808 if (tmpl && t == NULL_TREE)
8809 return tmpl;
8810 else
8811 return decl;
8812 }
8813
8814 return (decl != NULL_TREE
8815 && DECL_SELF_REFERENCE_P (decl)
8816 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8817 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8818 }
8819
8820 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8821 parameters, find the desired type.
8822
8823 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8824
8825 IN_DECL, if non-NULL, is the template declaration we are trying to
8826 instantiate.
8827
8828 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8829 the class we are looking up.
8830
8831 Issue error and warning messages under control of COMPLAIN.
8832
8833 If the template class is really a local class in a template
8834 function, then the FUNCTION_CONTEXT is the function in which it is
8835 being instantiated.
8836
8837 ??? Note that this function is currently called *twice* for each
8838 template-id: the first time from the parser, while creating the
8839 incomplete type (finish_template_type), and the second type during the
8840 real instantiation (instantiate_template_class). This is surely something
8841 that we want to avoid. It also causes some problems with argument
8842 coercion (see convert_nontype_argument for more information on this). */
8843
8844 static tree
8845 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8846 int entering_scope, tsubst_flags_t complain)
8847 {
8848 tree templ = NULL_TREE, parmlist;
8849 tree t;
8850 spec_entry **slot;
8851 spec_entry *entry;
8852 spec_entry elt;
8853 hashval_t hash;
8854
8855 if (identifier_p (d1))
8856 {
8857 tree value = innermost_non_namespace_value (d1);
8858 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8859 templ = value;
8860 else
8861 {
8862 if (context)
8863 push_decl_namespace (context);
8864 templ = lookup_name (d1);
8865 templ = maybe_get_template_decl_from_type_decl (templ);
8866 if (context)
8867 pop_decl_namespace ();
8868 }
8869 if (templ)
8870 context = DECL_CONTEXT (templ);
8871 }
8872 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8873 {
8874 tree type = TREE_TYPE (d1);
8875
8876 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8877 an implicit typename for the second A. Deal with it. */
8878 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8879 type = TREE_TYPE (type);
8880
8881 if (CLASSTYPE_TEMPLATE_INFO (type))
8882 {
8883 templ = CLASSTYPE_TI_TEMPLATE (type);
8884 d1 = DECL_NAME (templ);
8885 }
8886 }
8887 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8888 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8889 {
8890 templ = TYPE_TI_TEMPLATE (d1);
8891 d1 = DECL_NAME (templ);
8892 }
8893 else if (DECL_TYPE_TEMPLATE_P (d1))
8894 {
8895 templ = d1;
8896 d1 = DECL_NAME (templ);
8897 context = DECL_CONTEXT (templ);
8898 }
8899 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8900 {
8901 templ = d1;
8902 d1 = DECL_NAME (templ);
8903 }
8904
8905 /* Issue an error message if we didn't find a template. */
8906 if (! templ)
8907 {
8908 if (complain & tf_error)
8909 error ("%qT is not a template", d1);
8910 return error_mark_node;
8911 }
8912
8913 if (TREE_CODE (templ) != TEMPLATE_DECL
8914 /* Make sure it's a user visible template, if it was named by
8915 the user. */
8916 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8917 && !PRIMARY_TEMPLATE_P (templ)))
8918 {
8919 if (complain & tf_error)
8920 {
8921 error ("non-template type %qT used as a template", d1);
8922 if (in_decl)
8923 error ("for template declaration %q+D", in_decl);
8924 }
8925 return error_mark_node;
8926 }
8927
8928 complain &= ~tf_user;
8929
8930 /* An alias that just changes the name of a template is equivalent to the
8931 other template, so if any of the arguments are pack expansions, strip
8932 the alias to avoid problems with a pack expansion passed to a non-pack
8933 alias template parameter (DR 1430). */
8934 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8935 templ = get_underlying_template (templ);
8936
8937 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8938 {
8939 tree parm;
8940 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8941 if (arglist2 == error_mark_node
8942 || (!uses_template_parms (arglist2)
8943 && check_instantiated_args (templ, arglist2, complain)))
8944 return error_mark_node;
8945
8946 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8947 return parm;
8948 }
8949 else
8950 {
8951 tree template_type = TREE_TYPE (templ);
8952 tree gen_tmpl;
8953 tree type_decl;
8954 tree found = NULL_TREE;
8955 int arg_depth;
8956 int parm_depth;
8957 int is_dependent_type;
8958 int use_partial_inst_tmpl = false;
8959
8960 if (template_type == error_mark_node)
8961 /* An error occurred while building the template TEMPL, and a
8962 diagnostic has most certainly been emitted for that
8963 already. Let's propagate that error. */
8964 return error_mark_node;
8965
8966 gen_tmpl = most_general_template (templ);
8967 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8968 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8969 arg_depth = TMPL_ARGS_DEPTH (arglist);
8970
8971 if (arg_depth == 1 && parm_depth > 1)
8972 {
8973 /* We've been given an incomplete set of template arguments.
8974 For example, given:
8975
8976 template <class T> struct S1 {
8977 template <class U> struct S2 {};
8978 template <class U> struct S2<U*> {};
8979 };
8980
8981 we will be called with an ARGLIST of `U*', but the
8982 TEMPLATE will be `template <class T> template
8983 <class U> struct S1<T>::S2'. We must fill in the missing
8984 arguments. */
8985 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8986 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8987 arg_depth = TMPL_ARGS_DEPTH (arglist);
8988 }
8989
8990 /* Now we should have enough arguments. */
8991 gcc_assert (parm_depth == arg_depth);
8992
8993 /* From here on, we're only interested in the most general
8994 template. */
8995
8996 /* Calculate the BOUND_ARGS. These will be the args that are
8997 actually tsubst'd into the definition to create the
8998 instantiation. */
8999 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9000 complain,
9001 /*require_all_args=*/true,
9002 /*use_default_args=*/true);
9003
9004 if (arglist == error_mark_node)
9005 /* We were unable to bind the arguments. */
9006 return error_mark_node;
9007
9008 /* In the scope of a template class, explicit references to the
9009 template class refer to the type of the template, not any
9010 instantiation of it. For example, in:
9011
9012 template <class T> class C { void f(C<T>); }
9013
9014 the `C<T>' is just the same as `C'. Outside of the
9015 class, however, such a reference is an instantiation. */
9016 if (entering_scope
9017 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9018 || currently_open_class (template_type))
9019 {
9020 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9021
9022 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9023 return template_type;
9024 }
9025
9026 /* If we already have this specialization, return it. */
9027 elt.tmpl = gen_tmpl;
9028 elt.args = arglist;
9029 elt.spec = NULL_TREE;
9030 hash = spec_hasher::hash (&elt);
9031 entry = type_specializations->find_with_hash (&elt, hash);
9032
9033 if (entry)
9034 return entry->spec;
9035
9036 /* If the the template's constraints are not satisfied,
9037 then we cannot form a valid type.
9038
9039 Note that the check is deferred until after the hash
9040 lookup. This prevents redundant checks on previously
9041 instantiated specializations. */
9042 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9043 {
9044 if (complain & tf_error)
9045 {
9046 error ("template constraint failure");
9047 diagnose_constraints (input_location, gen_tmpl, arglist);
9048 }
9049 return error_mark_node;
9050 }
9051
9052 is_dependent_type = uses_template_parms (arglist);
9053
9054 /* If the deduced arguments are invalid, then the binding
9055 failed. */
9056 if (!is_dependent_type
9057 && check_instantiated_args (gen_tmpl,
9058 INNERMOST_TEMPLATE_ARGS (arglist),
9059 complain))
9060 return error_mark_node;
9061
9062 if (!is_dependent_type
9063 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9064 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9065 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9066 {
9067 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9068 DECL_NAME (gen_tmpl),
9069 /*tag_scope=*/ts_global);
9070 return found;
9071 }
9072
9073 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
9074 complain, in_decl);
9075 if (context == error_mark_node)
9076 return error_mark_node;
9077
9078 if (!context)
9079 context = global_namespace;
9080
9081 /* Create the type. */
9082 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9083 {
9084 /* The user referred to a specialization of an alias
9085 template represented by GEN_TMPL.
9086
9087 [temp.alias]/2 says:
9088
9089 When a template-id refers to the specialization of an
9090 alias template, it is equivalent to the associated
9091 type obtained by substitution of its
9092 template-arguments for the template-parameters in the
9093 type-id of the alias template. */
9094
9095 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9096 /* Note that the call above (by indirectly calling
9097 register_specialization in tsubst_decl) registers the
9098 TYPE_DECL representing the specialization of the alias
9099 template. So next time someone substitutes ARGLIST for
9100 the template parms into the alias template (GEN_TMPL),
9101 she'll get that TYPE_DECL back. */
9102
9103 if (t == error_mark_node)
9104 return t;
9105 }
9106 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9107 {
9108 if (!is_dependent_type)
9109 {
9110 set_current_access_from_decl (TYPE_NAME (template_type));
9111 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9112 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9113 arglist, complain, in_decl),
9114 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9115 arglist, complain, in_decl),
9116 SCOPED_ENUM_P (template_type), NULL);
9117
9118 if (t == error_mark_node)
9119 return t;
9120 }
9121 else
9122 {
9123 /* We don't want to call start_enum for this type, since
9124 the values for the enumeration constants may involve
9125 template parameters. And, no one should be interested
9126 in the enumeration constants for such a type. */
9127 t = cxx_make_type (ENUMERAL_TYPE);
9128 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9129 }
9130 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9131 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9132 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9133 }
9134 else if (CLASS_TYPE_P (template_type))
9135 {
9136 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9137 instantiated here. */
9138 gcc_assert (!LAMBDA_TYPE_P (template_type));
9139
9140 t = make_class_type (TREE_CODE (template_type));
9141 CLASSTYPE_DECLARED_CLASS (t)
9142 = CLASSTYPE_DECLARED_CLASS (template_type);
9143 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9144
9145 /* A local class. Make sure the decl gets registered properly. */
9146 if (context == current_function_decl)
9147 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9148
9149 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9150 /* This instantiation is another name for the primary
9151 template type. Set the TYPE_CANONICAL field
9152 appropriately. */
9153 TYPE_CANONICAL (t) = template_type;
9154 else if (any_template_arguments_need_structural_equality_p (arglist))
9155 /* Some of the template arguments require structural
9156 equality testing, so this template class requires
9157 structural equality testing. */
9158 SET_TYPE_STRUCTURAL_EQUALITY (t);
9159 }
9160 else
9161 gcc_unreachable ();
9162
9163 /* If we called start_enum or pushtag above, this information
9164 will already be set up. */
9165 if (!TYPE_NAME (t))
9166 {
9167 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9168
9169 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9170 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9171 DECL_SOURCE_LOCATION (type_decl)
9172 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9173 }
9174 else
9175 type_decl = TYPE_NAME (t);
9176
9177 if (CLASS_TYPE_P (template_type))
9178 {
9179 TREE_PRIVATE (type_decl)
9180 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9181 TREE_PROTECTED (type_decl)
9182 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9183 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9184 {
9185 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9186 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9187 }
9188 }
9189
9190 if (OVERLOAD_TYPE_P (t)
9191 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9192 {
9193 static const char *tags[] = {"abi_tag", "may_alias"};
9194
9195 for (unsigned ix = 0; ix != 2; ix++)
9196 {
9197 tree attributes
9198 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9199
9200 if (attributes)
9201 TYPE_ATTRIBUTES (t)
9202 = tree_cons (TREE_PURPOSE (attributes),
9203 TREE_VALUE (attributes),
9204 TYPE_ATTRIBUTES (t));
9205 }
9206 }
9207
9208 /* Let's consider the explicit specialization of a member
9209 of a class template specialization that is implicitly instantiated,
9210 e.g.:
9211 template<class T>
9212 struct S
9213 {
9214 template<class U> struct M {}; //#0
9215 };
9216
9217 template<>
9218 template<>
9219 struct S<int>::M<char> //#1
9220 {
9221 int i;
9222 };
9223 [temp.expl.spec]/4 says this is valid.
9224
9225 In this case, when we write:
9226 S<int>::M<char> m;
9227
9228 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9229 the one of #0.
9230
9231 When we encounter #1, we want to store the partial instantiation
9232 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9233
9234 For all cases other than this "explicit specialization of member of a
9235 class template", we just want to store the most general template into
9236 the CLASSTYPE_TI_TEMPLATE of M.
9237
9238 This case of "explicit specialization of member of a class template"
9239 only happens when:
9240 1/ the enclosing class is an instantiation of, and therefore not
9241 the same as, the context of the most general template, and
9242 2/ we aren't looking at the partial instantiation itself, i.e.
9243 the innermost arguments are not the same as the innermost parms of
9244 the most general template.
9245
9246 So it's only when 1/ and 2/ happens that we want to use the partial
9247 instantiation of the member template in lieu of its most general
9248 template. */
9249
9250 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9251 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9252 /* the enclosing class must be an instantiation... */
9253 && CLASS_TYPE_P (context)
9254 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9255 {
9256 TREE_VEC_LENGTH (arglist)--;
9257 ++processing_template_decl;
9258 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9259 tree partial_inst_args =
9260 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9261 arglist, complain, NULL_TREE);
9262 --processing_template_decl;
9263 TREE_VEC_LENGTH (arglist)++;
9264 if (partial_inst_args == error_mark_node)
9265 return error_mark_node;
9266 use_partial_inst_tmpl =
9267 /*...and we must not be looking at the partial instantiation
9268 itself. */
9269 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9270 partial_inst_args);
9271 }
9272
9273 if (!use_partial_inst_tmpl)
9274 /* This case is easy; there are no member templates involved. */
9275 found = gen_tmpl;
9276 else
9277 {
9278 /* This is a full instantiation of a member template. Find
9279 the partial instantiation of which this is an instance. */
9280
9281 /* Temporarily reduce by one the number of levels in the ARGLIST
9282 so as to avoid comparing the last set of arguments. */
9283 TREE_VEC_LENGTH (arglist)--;
9284 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9285 TREE_VEC_LENGTH (arglist)++;
9286 /* FOUND is either a proper class type, or an alias
9287 template specialization. In the later case, it's a
9288 TYPE_DECL, resulting from the substituting of arguments
9289 for parameters in the TYPE_DECL of the alias template
9290 done earlier. So be careful while getting the template
9291 of FOUND. */
9292 found = (TREE_CODE (found) == TEMPLATE_DECL
9293 ? found
9294 : (TREE_CODE (found) == TYPE_DECL
9295 ? DECL_TI_TEMPLATE (found)
9296 : CLASSTYPE_TI_TEMPLATE (found)));
9297 }
9298
9299 // Build template info for the new specialization.
9300 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9301
9302 elt.spec = t;
9303 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9304 entry = ggc_alloc<spec_entry> ();
9305 *entry = elt;
9306 *slot = entry;
9307
9308 /* Note this use of the partial instantiation so we can check it
9309 later in maybe_process_partial_specialization. */
9310 DECL_TEMPLATE_INSTANTIATIONS (found)
9311 = tree_cons (arglist, t,
9312 DECL_TEMPLATE_INSTANTIATIONS (found));
9313
9314 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9315 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9316 /* Now that the type has been registered on the instantiations
9317 list, we set up the enumerators. Because the enumeration
9318 constants may involve the enumeration type itself, we make
9319 sure to register the type first, and then create the
9320 constants. That way, doing tsubst_expr for the enumeration
9321 constants won't result in recursive calls here; we'll find
9322 the instantiation and exit above. */
9323 tsubst_enum (template_type, t, arglist);
9324
9325 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9326 /* If the type makes use of template parameters, the
9327 code that generates debugging information will crash. */
9328 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9329
9330 /* Possibly limit visibility based on template args. */
9331 TREE_PUBLIC (type_decl) = 1;
9332 determine_visibility (type_decl);
9333
9334 inherit_targ_abi_tags (t);
9335
9336 return t;
9337 }
9338 }
9339
9340 /* Wrapper for lookup_template_class_1. */
9341
9342 tree
9343 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9344 int entering_scope, tsubst_flags_t complain)
9345 {
9346 tree ret;
9347 timevar_push (TV_TEMPLATE_INST);
9348 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9349 entering_scope, complain);
9350 timevar_pop (TV_TEMPLATE_INST);
9351 return ret;
9352 }
9353
9354 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9355
9356 tree
9357 lookup_template_variable (tree templ, tree arglist)
9358 {
9359 /* The type of the expression is NULL_TREE since the template-id could refer
9360 to an explicit or partial specialization. */
9361 tree type = NULL_TREE;
9362 if (flag_concepts && variable_concept_p (templ))
9363 /* Except that concepts are always bool. */
9364 type = boolean_type_node;
9365 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9366 }
9367
9368 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9369
9370 tree
9371 finish_template_variable (tree var, tsubst_flags_t complain)
9372 {
9373 tree templ = TREE_OPERAND (var, 0);
9374 tree arglist = TREE_OPERAND (var, 1);
9375
9376 /* We never want to return a VAR_DECL for a variable concept, since they
9377 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9378 bool concept_p = flag_concepts && variable_concept_p (templ);
9379 if (concept_p && processing_template_decl)
9380 return var;
9381
9382 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9383 arglist = add_outermost_template_args (tmpl_args, arglist);
9384
9385 templ = most_general_template (templ);
9386 tree parms = DECL_TEMPLATE_PARMS (templ);
9387 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9388 /*req_all*/true,
9389 /*use_default*/true);
9390
9391 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9392 {
9393 if (complain & tf_error)
9394 {
9395 error ("use of invalid variable template %qE", var);
9396 diagnose_constraints (location_of (var), templ, arglist);
9397 }
9398 return error_mark_node;
9399 }
9400
9401 /* If a template-id refers to a specialization of a variable
9402 concept, then the expression is true if and only if the
9403 concept's constraints are satisfied by the given template
9404 arguments.
9405
9406 NOTE: This is an extension of Concepts Lite TS that
9407 allows constraints to be used in expressions. */
9408 if (concept_p)
9409 {
9410 tree decl = DECL_TEMPLATE_RESULT (templ);
9411 return evaluate_variable_concept (decl, arglist);
9412 }
9413
9414 return instantiate_template (templ, arglist, complain);
9415 }
9416
9417 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9418 TARGS template args, and instantiate it if it's not dependent. */
9419
9420 tree
9421 lookup_and_finish_template_variable (tree templ, tree targs,
9422 tsubst_flags_t complain)
9423 {
9424 templ = lookup_template_variable (templ, targs);
9425 if (!any_dependent_template_arguments_p (targs))
9426 {
9427 templ = finish_template_variable (templ, complain);
9428 mark_used (templ);
9429 }
9430
9431 return convert_from_reference (templ);
9432 }
9433
9434 \f
9435 struct pair_fn_data
9436 {
9437 tree_fn_t fn;
9438 tree_fn_t any_fn;
9439 void *data;
9440 /* True when we should also visit template parameters that occur in
9441 non-deduced contexts. */
9442 bool include_nondeduced_p;
9443 hash_set<tree> *visited;
9444 };
9445
9446 /* Called from for_each_template_parm via walk_tree. */
9447
9448 static tree
9449 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9450 {
9451 tree t = *tp;
9452 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9453 tree_fn_t fn = pfd->fn;
9454 void *data = pfd->data;
9455 tree result = NULL_TREE;
9456
9457 #define WALK_SUBTREE(NODE) \
9458 do \
9459 { \
9460 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9461 pfd->include_nondeduced_p, \
9462 pfd->any_fn); \
9463 if (result) goto out; \
9464 } \
9465 while (0)
9466
9467 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9468 return t;
9469
9470 if (TYPE_P (t)
9471 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9472 WALK_SUBTREE (TYPE_CONTEXT (t));
9473
9474 switch (TREE_CODE (t))
9475 {
9476 case RECORD_TYPE:
9477 if (TYPE_PTRMEMFUNC_P (t))
9478 break;
9479 /* Fall through. */
9480
9481 case UNION_TYPE:
9482 case ENUMERAL_TYPE:
9483 if (!TYPE_TEMPLATE_INFO (t))
9484 *walk_subtrees = 0;
9485 else
9486 WALK_SUBTREE (TYPE_TI_ARGS (t));
9487 break;
9488
9489 case INTEGER_TYPE:
9490 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9491 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9492 break;
9493
9494 case METHOD_TYPE:
9495 /* Since we're not going to walk subtrees, we have to do this
9496 explicitly here. */
9497 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9498 /* Fall through. */
9499
9500 case FUNCTION_TYPE:
9501 /* Check the return type. */
9502 WALK_SUBTREE (TREE_TYPE (t));
9503
9504 /* Check the parameter types. Since default arguments are not
9505 instantiated until they are needed, the TYPE_ARG_TYPES may
9506 contain expressions that involve template parameters. But,
9507 no-one should be looking at them yet. And, once they're
9508 instantiated, they don't contain template parameters, so
9509 there's no point in looking at them then, either. */
9510 {
9511 tree parm;
9512
9513 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9514 WALK_SUBTREE (TREE_VALUE (parm));
9515
9516 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9517 want walk_tree walking into them itself. */
9518 *walk_subtrees = 0;
9519 }
9520
9521 if (flag_noexcept_type)
9522 {
9523 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9524 if (spec)
9525 WALK_SUBTREE (TREE_PURPOSE (spec));
9526 }
9527 break;
9528
9529 case TYPEOF_TYPE:
9530 case UNDERLYING_TYPE:
9531 if (pfd->include_nondeduced_p
9532 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9533 pfd->visited,
9534 pfd->include_nondeduced_p,
9535 pfd->any_fn))
9536 return error_mark_node;
9537 break;
9538
9539 case FUNCTION_DECL:
9540 case VAR_DECL:
9541 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9542 WALK_SUBTREE (DECL_TI_ARGS (t));
9543 /* Fall through. */
9544
9545 case PARM_DECL:
9546 case CONST_DECL:
9547 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9548 WALK_SUBTREE (DECL_INITIAL (t));
9549 if (DECL_CONTEXT (t)
9550 && pfd->include_nondeduced_p)
9551 WALK_SUBTREE (DECL_CONTEXT (t));
9552 break;
9553
9554 case BOUND_TEMPLATE_TEMPLATE_PARM:
9555 /* Record template parameters such as `T' inside `TT<T>'. */
9556 WALK_SUBTREE (TYPE_TI_ARGS (t));
9557 /* Fall through. */
9558
9559 case TEMPLATE_TEMPLATE_PARM:
9560 case TEMPLATE_TYPE_PARM:
9561 case TEMPLATE_PARM_INDEX:
9562 if (fn && (*fn)(t, data))
9563 return t;
9564 else if (!fn)
9565 return t;
9566 break;
9567
9568 case TEMPLATE_DECL:
9569 /* A template template parameter is encountered. */
9570 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9571 WALK_SUBTREE (TREE_TYPE (t));
9572
9573 /* Already substituted template template parameter */
9574 *walk_subtrees = 0;
9575 break;
9576
9577 case TYPENAME_TYPE:
9578 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9579 partial instantiation. */
9580 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9581 break;
9582
9583 case CONSTRUCTOR:
9584 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9585 && pfd->include_nondeduced_p)
9586 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9587 break;
9588
9589 case INDIRECT_REF:
9590 case COMPONENT_REF:
9591 /* If there's no type, then this thing must be some expression
9592 involving template parameters. */
9593 if (!fn && !TREE_TYPE (t))
9594 return error_mark_node;
9595 break;
9596
9597 case MODOP_EXPR:
9598 case CAST_EXPR:
9599 case IMPLICIT_CONV_EXPR:
9600 case REINTERPRET_CAST_EXPR:
9601 case CONST_CAST_EXPR:
9602 case STATIC_CAST_EXPR:
9603 case DYNAMIC_CAST_EXPR:
9604 case ARROW_EXPR:
9605 case DOTSTAR_EXPR:
9606 case TYPEID_EXPR:
9607 case PSEUDO_DTOR_EXPR:
9608 if (!fn)
9609 return error_mark_node;
9610 break;
9611
9612 default:
9613 break;
9614 }
9615
9616 #undef WALK_SUBTREE
9617
9618 /* We didn't find any template parameters we liked. */
9619 out:
9620 return result;
9621 }
9622
9623 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9624 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9625 call FN with the parameter and the DATA.
9626 If FN returns nonzero, the iteration is terminated, and
9627 for_each_template_parm returns 1. Otherwise, the iteration
9628 continues. If FN never returns a nonzero value, the value
9629 returned by for_each_template_parm is 0. If FN is NULL, it is
9630 considered to be the function which always returns 1.
9631
9632 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9633 parameters that occur in non-deduced contexts. When false, only
9634 visits those template parameters that can be deduced. */
9635
9636 static tree
9637 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9638 hash_set<tree> *visited,
9639 bool include_nondeduced_p,
9640 tree_fn_t any_fn)
9641 {
9642 struct pair_fn_data pfd;
9643 tree result;
9644
9645 /* Set up. */
9646 pfd.fn = fn;
9647 pfd.any_fn = any_fn;
9648 pfd.data = data;
9649 pfd.include_nondeduced_p = include_nondeduced_p;
9650
9651 /* Walk the tree. (Conceptually, we would like to walk without
9652 duplicates, but for_each_template_parm_r recursively calls
9653 for_each_template_parm, so we would need to reorganize a fair
9654 bit to use walk_tree_without_duplicates, so we keep our own
9655 visited list.) */
9656 if (visited)
9657 pfd.visited = visited;
9658 else
9659 pfd.visited = new hash_set<tree>;
9660 result = cp_walk_tree (&t,
9661 for_each_template_parm_r,
9662 &pfd,
9663 pfd.visited);
9664
9665 /* Clean up. */
9666 if (!visited)
9667 {
9668 delete pfd.visited;
9669 pfd.visited = 0;
9670 }
9671
9672 return result;
9673 }
9674
9675 /* Returns true if T depends on any template parameter. */
9676
9677 int
9678 uses_template_parms (tree t)
9679 {
9680 if (t == NULL_TREE)
9681 return false;
9682
9683 bool dependent_p;
9684 int saved_processing_template_decl;
9685
9686 saved_processing_template_decl = processing_template_decl;
9687 if (!saved_processing_template_decl)
9688 processing_template_decl = 1;
9689 if (TYPE_P (t))
9690 dependent_p = dependent_type_p (t);
9691 else if (TREE_CODE (t) == TREE_VEC)
9692 dependent_p = any_dependent_template_arguments_p (t);
9693 else if (TREE_CODE (t) == TREE_LIST)
9694 dependent_p = (uses_template_parms (TREE_VALUE (t))
9695 || uses_template_parms (TREE_CHAIN (t)));
9696 else if (TREE_CODE (t) == TYPE_DECL)
9697 dependent_p = dependent_type_p (TREE_TYPE (t));
9698 else if (DECL_P (t)
9699 || EXPR_P (t)
9700 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9701 || TREE_CODE (t) == OVERLOAD
9702 || BASELINK_P (t)
9703 || identifier_p (t)
9704 || TREE_CODE (t) == TRAIT_EXPR
9705 || TREE_CODE (t) == CONSTRUCTOR
9706 || CONSTANT_CLASS_P (t))
9707 dependent_p = (type_dependent_expression_p (t)
9708 || value_dependent_expression_p (t));
9709 else
9710 {
9711 gcc_assert (t == error_mark_node);
9712 dependent_p = false;
9713 }
9714
9715 processing_template_decl = saved_processing_template_decl;
9716
9717 return dependent_p;
9718 }
9719
9720 /* Returns true iff current_function_decl is an incompletely instantiated
9721 template. Useful instead of processing_template_decl because the latter
9722 is set to 0 during instantiate_non_dependent_expr. */
9723
9724 bool
9725 in_template_function (void)
9726 {
9727 tree fn = current_function_decl;
9728 bool ret;
9729 ++processing_template_decl;
9730 ret = (fn && DECL_LANG_SPECIFIC (fn)
9731 && DECL_TEMPLATE_INFO (fn)
9732 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9733 --processing_template_decl;
9734 return ret;
9735 }
9736
9737 /* Returns true if T depends on any template parameter with level LEVEL. */
9738
9739 bool
9740 uses_template_parms_level (tree t, int level)
9741 {
9742 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9743 /*include_nondeduced_p=*/true);
9744 }
9745
9746 /* Returns true if the signature of DECL depends on any template parameter from
9747 its enclosing class. */
9748
9749 bool
9750 uses_outer_template_parms (tree decl)
9751 {
9752 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9753 if (depth == 0)
9754 return false;
9755 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9756 &depth, NULL, /*include_nondeduced_p=*/true))
9757 return true;
9758 if (PRIMARY_TEMPLATE_P (decl)
9759 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9760 (DECL_TEMPLATE_PARMS (decl)),
9761 template_parm_outer_level,
9762 &depth, NULL, /*include_nondeduced_p=*/true))
9763 return true;
9764 tree ci = get_constraints (decl);
9765 if (ci)
9766 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9767 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9768 &depth, NULL, /*nondeduced*/true))
9769 return true;
9770 return false;
9771 }
9772
9773 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9774 ill-formed translation unit, i.e. a variable or function that isn't
9775 usable in a constant expression. */
9776
9777 static inline bool
9778 neglectable_inst_p (tree d)
9779 {
9780 return (DECL_P (d)
9781 && !undeduced_auto_decl (d)
9782 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9783 : decl_maybe_constant_var_p (d)));
9784 }
9785
9786 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9787 neglectable and instantiated from within an erroneous instantiation. */
9788
9789 static bool
9790 limit_bad_template_recursion (tree decl)
9791 {
9792 struct tinst_level *lev = current_tinst_level;
9793 int errs = errorcount + sorrycount;
9794 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9795 return false;
9796
9797 for (; lev; lev = lev->next)
9798 if (neglectable_inst_p (lev->decl))
9799 break;
9800
9801 return (lev && errs > lev->errors);
9802 }
9803
9804 static int tinst_depth;
9805 extern int max_tinst_depth;
9806 int depth_reached;
9807
9808 static GTY(()) struct tinst_level *last_error_tinst_level;
9809
9810 /* We're starting to instantiate D; record the template instantiation context
9811 for diagnostics and to restore it later. */
9812
9813 bool
9814 push_tinst_level (tree d)
9815 {
9816 return push_tinst_level_loc (d, input_location);
9817 }
9818
9819 /* We're starting to instantiate D; record the template instantiation context
9820 at LOC for diagnostics and to restore it later. */
9821
9822 bool
9823 push_tinst_level_loc (tree d, location_t loc)
9824 {
9825 struct tinst_level *new_level;
9826
9827 if (tinst_depth >= max_tinst_depth)
9828 {
9829 /* Tell error.c not to try to instantiate any templates. */
9830 at_eof = 2;
9831 fatal_error (input_location,
9832 "template instantiation depth exceeds maximum of %d"
9833 " (use -ftemplate-depth= to increase the maximum)",
9834 max_tinst_depth);
9835 return false;
9836 }
9837
9838 /* If the current instantiation caused problems, don't let it instantiate
9839 anything else. Do allow deduction substitution and decls usable in
9840 constant expressions. */
9841 if (limit_bad_template_recursion (d))
9842 return false;
9843
9844 /* When not -quiet, dump template instantiations other than functions, since
9845 announce_function will take care of those. */
9846 if (!quiet_flag
9847 && TREE_CODE (d) != TREE_LIST
9848 && TREE_CODE (d) != FUNCTION_DECL)
9849 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9850
9851 new_level = ggc_alloc<tinst_level> ();
9852 new_level->decl = d;
9853 new_level->locus = loc;
9854 new_level->errors = errorcount+sorrycount;
9855 new_level->in_system_header_p = in_system_header_at (input_location);
9856 new_level->next = current_tinst_level;
9857 current_tinst_level = new_level;
9858
9859 ++tinst_depth;
9860 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9861 depth_reached = tinst_depth;
9862
9863 return true;
9864 }
9865
9866 /* We're done instantiating this template; return to the instantiation
9867 context. */
9868
9869 void
9870 pop_tinst_level (void)
9871 {
9872 /* Restore the filename and line number stashed away when we started
9873 this instantiation. */
9874 input_location = current_tinst_level->locus;
9875 current_tinst_level = current_tinst_level->next;
9876 --tinst_depth;
9877 }
9878
9879 /* We're instantiating a deferred template; restore the template
9880 instantiation context in which the instantiation was requested, which
9881 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9882
9883 static tree
9884 reopen_tinst_level (struct tinst_level *level)
9885 {
9886 struct tinst_level *t;
9887
9888 tinst_depth = 0;
9889 for (t = level; t; t = t->next)
9890 ++tinst_depth;
9891
9892 current_tinst_level = level;
9893 pop_tinst_level ();
9894 if (current_tinst_level)
9895 current_tinst_level->errors = errorcount+sorrycount;
9896 return level->decl;
9897 }
9898
9899 /* Returns the TINST_LEVEL which gives the original instantiation
9900 context. */
9901
9902 struct tinst_level *
9903 outermost_tinst_level (void)
9904 {
9905 struct tinst_level *level = current_tinst_level;
9906 if (level)
9907 while (level->next)
9908 level = level->next;
9909 return level;
9910 }
9911
9912 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9913 vector of template arguments, as for tsubst.
9914
9915 Returns an appropriate tsubst'd friend declaration. */
9916
9917 static tree
9918 tsubst_friend_function (tree decl, tree args)
9919 {
9920 tree new_friend;
9921
9922 if (TREE_CODE (decl) == FUNCTION_DECL
9923 && DECL_TEMPLATE_INSTANTIATION (decl)
9924 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9925 /* This was a friend declared with an explicit template
9926 argument list, e.g.:
9927
9928 friend void f<>(T);
9929
9930 to indicate that f was a template instantiation, not a new
9931 function declaration. Now, we have to figure out what
9932 instantiation of what template. */
9933 {
9934 tree template_id, arglist, fns;
9935 tree new_args;
9936 tree tmpl;
9937 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9938
9939 /* Friend functions are looked up in the containing namespace scope.
9940 We must enter that scope, to avoid finding member functions of the
9941 current class with same name. */
9942 push_nested_namespace (ns);
9943 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9944 tf_warning_or_error, NULL_TREE,
9945 /*integral_constant_expression_p=*/false);
9946 pop_nested_namespace (ns);
9947 arglist = tsubst (DECL_TI_ARGS (decl), args,
9948 tf_warning_or_error, NULL_TREE);
9949 template_id = lookup_template_function (fns, arglist);
9950
9951 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9952 tmpl = determine_specialization (template_id, new_friend,
9953 &new_args,
9954 /*need_member_template=*/0,
9955 TREE_VEC_LENGTH (args),
9956 tsk_none);
9957 return instantiate_template (tmpl, new_args, tf_error);
9958 }
9959
9960 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9961
9962 /* The NEW_FRIEND will look like an instantiation, to the
9963 compiler, but is not an instantiation from the point of view of
9964 the language. For example, we might have had:
9965
9966 template <class T> struct S {
9967 template <class U> friend void f(T, U);
9968 };
9969
9970 Then, in S<int>, template <class U> void f(int, U) is not an
9971 instantiation of anything. */
9972 if (new_friend == error_mark_node)
9973 return error_mark_node;
9974
9975 DECL_USE_TEMPLATE (new_friend) = 0;
9976 if (TREE_CODE (decl) == TEMPLATE_DECL)
9977 {
9978 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9979 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9980 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9981 }
9982
9983 /* The mangled name for the NEW_FRIEND is incorrect. The function
9984 is not a template instantiation and should not be mangled like
9985 one. Therefore, we forget the mangling here; we'll recompute it
9986 later if we need it. */
9987 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9988 {
9989 SET_DECL_RTL (new_friend, NULL);
9990 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9991 }
9992
9993 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9994 {
9995 tree old_decl;
9996 tree new_friend_template_info;
9997 tree new_friend_result_template_info;
9998 tree ns;
9999 int new_friend_is_defn;
10000
10001 /* We must save some information from NEW_FRIEND before calling
10002 duplicate decls since that function will free NEW_FRIEND if
10003 possible. */
10004 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10005 new_friend_is_defn =
10006 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10007 (template_for_substitution (new_friend)))
10008 != NULL_TREE);
10009 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10010 {
10011 /* This declaration is a `primary' template. */
10012 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10013
10014 new_friend_result_template_info
10015 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10016 }
10017 else
10018 new_friend_result_template_info = NULL_TREE;
10019
10020 /* Inside pushdecl_namespace_level, we will push into the
10021 current namespace. However, the friend function should go
10022 into the namespace of the template. */
10023 ns = decl_namespace_context (new_friend);
10024 push_nested_namespace (ns);
10025 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10026 pop_nested_namespace (ns);
10027
10028 if (old_decl == error_mark_node)
10029 return error_mark_node;
10030
10031 if (old_decl != new_friend)
10032 {
10033 /* This new friend declaration matched an existing
10034 declaration. For example, given:
10035
10036 template <class T> void f(T);
10037 template <class U> class C {
10038 template <class T> friend void f(T) {}
10039 };
10040
10041 the friend declaration actually provides the definition
10042 of `f', once C has been instantiated for some type. So,
10043 old_decl will be the out-of-class template declaration,
10044 while new_friend is the in-class definition.
10045
10046 But, if `f' was called before this point, the
10047 instantiation of `f' will have DECL_TI_ARGS corresponding
10048 to `T' but not to `U', references to which might appear
10049 in the definition of `f'. Previously, the most general
10050 template for an instantiation of `f' was the out-of-class
10051 version; now it is the in-class version. Therefore, we
10052 run through all specialization of `f', adding to their
10053 DECL_TI_ARGS appropriately. In particular, they need a
10054 new set of outer arguments, corresponding to the
10055 arguments for this class instantiation.
10056
10057 The same situation can arise with something like this:
10058
10059 friend void f(int);
10060 template <class T> class C {
10061 friend void f(T) {}
10062 };
10063
10064 when `C<int>' is instantiated. Now, `f(int)' is defined
10065 in the class. */
10066
10067 if (!new_friend_is_defn)
10068 /* On the other hand, if the in-class declaration does
10069 *not* provide a definition, then we don't want to alter
10070 existing definitions. We can just leave everything
10071 alone. */
10072 ;
10073 else
10074 {
10075 tree new_template = TI_TEMPLATE (new_friend_template_info);
10076 tree new_args = TI_ARGS (new_friend_template_info);
10077
10078 /* Overwrite whatever template info was there before, if
10079 any, with the new template information pertaining to
10080 the declaration. */
10081 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10082
10083 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10084 {
10085 /* We should have called reregister_specialization in
10086 duplicate_decls. */
10087 gcc_assert (retrieve_specialization (new_template,
10088 new_args, 0)
10089 == old_decl);
10090
10091 /* Instantiate it if the global has already been used. */
10092 if (DECL_ODR_USED (old_decl))
10093 instantiate_decl (old_decl, /*defer_ok=*/true,
10094 /*expl_inst_class_mem_p=*/false);
10095 }
10096 else
10097 {
10098 tree t;
10099
10100 /* Indicate that the old function template is a partial
10101 instantiation. */
10102 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10103 = new_friend_result_template_info;
10104
10105 gcc_assert (new_template
10106 == most_general_template (new_template));
10107 gcc_assert (new_template != old_decl);
10108
10109 /* Reassign any specializations already in the hash table
10110 to the new more general template, and add the
10111 additional template args. */
10112 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10113 t != NULL_TREE;
10114 t = TREE_CHAIN (t))
10115 {
10116 tree spec = TREE_VALUE (t);
10117 spec_entry elt;
10118
10119 elt.tmpl = old_decl;
10120 elt.args = DECL_TI_ARGS (spec);
10121 elt.spec = NULL_TREE;
10122
10123 decl_specializations->remove_elt (&elt);
10124
10125 DECL_TI_ARGS (spec)
10126 = add_outermost_template_args (new_args,
10127 DECL_TI_ARGS (spec));
10128
10129 register_specialization
10130 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10131
10132 }
10133 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10134 }
10135 }
10136
10137 /* The information from NEW_FRIEND has been merged into OLD_DECL
10138 by duplicate_decls. */
10139 new_friend = old_decl;
10140 }
10141 }
10142 else
10143 {
10144 tree context = DECL_CONTEXT (new_friend);
10145 bool dependent_p;
10146
10147 /* In the code
10148 template <class T> class C {
10149 template <class U> friend void C1<U>::f (); // case 1
10150 friend void C2<T>::f (); // case 2
10151 };
10152 we only need to make sure CONTEXT is a complete type for
10153 case 2. To distinguish between the two cases, we note that
10154 CONTEXT of case 1 remains dependent type after tsubst while
10155 this isn't true for case 2. */
10156 ++processing_template_decl;
10157 dependent_p = dependent_type_p (context);
10158 --processing_template_decl;
10159
10160 if (!dependent_p
10161 && !complete_type_or_else (context, NULL_TREE))
10162 return error_mark_node;
10163
10164 if (COMPLETE_TYPE_P (context))
10165 {
10166 tree fn = new_friend;
10167 /* do_friend adds the TEMPLATE_DECL for any member friend
10168 template even if it isn't a member template, i.e.
10169 template <class T> friend A<T>::f();
10170 Look through it in that case. */
10171 if (TREE_CODE (fn) == TEMPLATE_DECL
10172 && !PRIMARY_TEMPLATE_P (fn))
10173 fn = DECL_TEMPLATE_RESULT (fn);
10174 /* Check to see that the declaration is really present, and,
10175 possibly obtain an improved declaration. */
10176 fn = check_classfn (context, fn, NULL_TREE);
10177
10178 if (fn)
10179 new_friend = fn;
10180 }
10181 }
10182
10183 return new_friend;
10184 }
10185
10186 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10187 template arguments, as for tsubst.
10188
10189 Returns an appropriate tsubst'd friend type or error_mark_node on
10190 failure. */
10191
10192 static tree
10193 tsubst_friend_class (tree friend_tmpl, tree args)
10194 {
10195 tree tmpl;
10196
10197 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10198 {
10199 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10200 return TREE_TYPE (tmpl);
10201 }
10202
10203 tree context = CP_DECL_CONTEXT (friend_tmpl);
10204 if (TREE_CODE (context) == NAMESPACE_DECL)
10205 push_nested_namespace (context);
10206 else
10207 push_nested_class (context);
10208
10209 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10210 /*non_class=*/false, /*block_p=*/false,
10211 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10212
10213 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10214 {
10215 /* The friend template has already been declared. Just
10216 check to see that the declarations match, and install any new
10217 default parameters. We must tsubst the default parameters,
10218 of course. We only need the innermost template parameters
10219 because that is all that redeclare_class_template will look
10220 at. */
10221 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10222 > TMPL_ARGS_DEPTH (args))
10223 {
10224 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10225 args, tf_warning_or_error);
10226 location_t saved_input_location = input_location;
10227 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10228 tree cons = get_constraints (tmpl);
10229 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10230 input_location = saved_input_location;
10231 }
10232 }
10233 else
10234 {
10235 /* The friend template has not already been declared. In this
10236 case, the instantiation of the template class will cause the
10237 injection of this template into the namespace scope. */
10238 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10239
10240 if (tmpl != error_mark_node)
10241 {
10242 /* The new TMPL is not an instantiation of anything, so we
10243 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10244 for the new type because that is supposed to be the
10245 corresponding template decl, i.e., TMPL. */
10246 DECL_USE_TEMPLATE (tmpl) = 0;
10247 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10248 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10249 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10250 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10251
10252 /* It is hidden. */
10253 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10254 DECL_ANTICIPATED (tmpl)
10255 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10256
10257 /* Inject this template into the enclosing namspace scope. */
10258 tmpl = pushdecl_namespace_level (tmpl, true);
10259 }
10260 }
10261
10262 if (TREE_CODE (context) == NAMESPACE_DECL)
10263 pop_nested_namespace (context);
10264 else
10265 pop_nested_class ();
10266
10267 return TREE_TYPE (tmpl);
10268 }
10269
10270 /* Returns zero if TYPE cannot be completed later due to circularity.
10271 Otherwise returns one. */
10272
10273 static int
10274 can_complete_type_without_circularity (tree type)
10275 {
10276 if (type == NULL_TREE || type == error_mark_node)
10277 return 0;
10278 else if (COMPLETE_TYPE_P (type))
10279 return 1;
10280 else if (TREE_CODE (type) == ARRAY_TYPE)
10281 return can_complete_type_without_circularity (TREE_TYPE (type));
10282 else if (CLASS_TYPE_P (type)
10283 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10284 return 0;
10285 else
10286 return 1;
10287 }
10288
10289 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10290 tsubst_flags_t, tree);
10291
10292 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10293 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10294
10295 static tree
10296 tsubst_attribute (tree t, tree *decl_p, tree args,
10297 tsubst_flags_t complain, tree in_decl)
10298 {
10299 gcc_assert (ATTR_IS_DEPENDENT (t));
10300
10301 tree val = TREE_VALUE (t);
10302 if (val == NULL_TREE)
10303 /* Nothing to do. */;
10304 else if ((flag_openmp || flag_openmp_simd)
10305 && is_attribute_p ("omp declare simd",
10306 get_attribute_name (t)))
10307 {
10308 tree clauses = TREE_VALUE (val);
10309 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10310 complain, in_decl);
10311 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10312 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10313 tree parms = DECL_ARGUMENTS (*decl_p);
10314 clauses
10315 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10316 if (clauses)
10317 val = build_tree_list (NULL_TREE, clauses);
10318 else
10319 val = NULL_TREE;
10320 }
10321 /* If the first attribute argument is an identifier, don't
10322 pass it through tsubst. Attributes like mode, format,
10323 cleanup and several target specific attributes expect it
10324 unmodified. */
10325 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10326 {
10327 tree chain
10328 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10329 /*integral_constant_expression_p=*/false);
10330 if (chain != TREE_CHAIN (val))
10331 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10332 }
10333 else if (PACK_EXPANSION_P (val))
10334 {
10335 /* An attribute pack expansion. */
10336 tree purp = TREE_PURPOSE (t);
10337 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10338 if (pack == error_mark_node)
10339 return error_mark_node;
10340 int len = TREE_VEC_LENGTH (pack);
10341 tree list = NULL_TREE;
10342 tree *q = &list;
10343 for (int i = 0; i < len; ++i)
10344 {
10345 tree elt = TREE_VEC_ELT (pack, i);
10346 *q = build_tree_list (purp, elt);
10347 q = &TREE_CHAIN (*q);
10348 }
10349 return list;
10350 }
10351 else
10352 val = tsubst_expr (val, args, complain, in_decl,
10353 /*integral_constant_expression_p=*/false);
10354
10355 if (val != TREE_VALUE (t))
10356 return build_tree_list (TREE_PURPOSE (t), val);
10357 return t;
10358 }
10359
10360 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10361 unchanged or a new TREE_LIST chain. */
10362
10363 static tree
10364 tsubst_attributes (tree attributes, tree args,
10365 tsubst_flags_t complain, tree in_decl)
10366 {
10367 tree last_dep = NULL_TREE;
10368
10369 for (tree t = attributes; t; t = TREE_CHAIN (t))
10370 if (ATTR_IS_DEPENDENT (t))
10371 {
10372 last_dep = t;
10373 attributes = copy_list (attributes);
10374 break;
10375 }
10376
10377 if (last_dep)
10378 for (tree *p = &attributes; *p; )
10379 {
10380 tree t = *p;
10381 if (ATTR_IS_DEPENDENT (t))
10382 {
10383 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10384 if (subst != t)
10385 {
10386 *p = subst;
10387 while (*p)
10388 p = &TREE_CHAIN (*p);
10389 *p = TREE_CHAIN (t);
10390 continue;
10391 }
10392 }
10393 p = &TREE_CHAIN (*p);
10394 }
10395
10396 return attributes;
10397 }
10398
10399 /* Apply any attributes which had to be deferred until instantiation
10400 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10401 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10402
10403 static void
10404 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10405 tree args, tsubst_flags_t complain, tree in_decl)
10406 {
10407 tree last_dep = NULL_TREE;
10408 tree t;
10409 tree *p;
10410
10411 if (attributes == NULL_TREE)
10412 return;
10413
10414 if (DECL_P (*decl_p))
10415 {
10416 if (TREE_TYPE (*decl_p) == error_mark_node)
10417 return;
10418 p = &DECL_ATTRIBUTES (*decl_p);
10419 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10420 to our attributes parameter. */
10421 gcc_assert (*p == attributes);
10422 }
10423 else
10424 {
10425 p = &TYPE_ATTRIBUTES (*decl_p);
10426 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10427 lookup_template_class_1, and should be preserved. */
10428 gcc_assert (*p != attributes);
10429 while (*p)
10430 p = &TREE_CHAIN (*p);
10431 }
10432
10433 for (t = attributes; t; t = TREE_CHAIN (t))
10434 if (ATTR_IS_DEPENDENT (t))
10435 {
10436 last_dep = t;
10437 attributes = copy_list (attributes);
10438 break;
10439 }
10440
10441 *p = attributes;
10442 if (last_dep)
10443 {
10444 tree late_attrs = NULL_TREE;
10445 tree *q = &late_attrs;
10446
10447 for (; *p; )
10448 {
10449 t = *p;
10450 if (ATTR_IS_DEPENDENT (t))
10451 {
10452 *p = TREE_CHAIN (t);
10453 TREE_CHAIN (t) = NULL_TREE;
10454 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10455 while (*q)
10456 q = &TREE_CHAIN (*q);
10457 }
10458 else
10459 p = &TREE_CHAIN (t);
10460 }
10461
10462 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10463 }
10464 }
10465
10466 /* Perform (or defer) access check for typedefs that were referenced
10467 from within the template TMPL code.
10468 This is a subroutine of instantiate_decl and instantiate_class_template.
10469 TMPL is the template to consider and TARGS is the list of arguments of
10470 that template. */
10471
10472 static void
10473 perform_typedefs_access_check (tree tmpl, tree targs)
10474 {
10475 location_t saved_location;
10476 unsigned i;
10477 qualified_typedef_usage_t *iter;
10478
10479 if (!tmpl
10480 || (!CLASS_TYPE_P (tmpl)
10481 && TREE_CODE (tmpl) != FUNCTION_DECL))
10482 return;
10483
10484 saved_location = input_location;
10485 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10486 {
10487 tree type_decl = iter->typedef_decl;
10488 tree type_scope = iter->context;
10489
10490 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10491 continue;
10492
10493 if (uses_template_parms (type_decl))
10494 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10495 if (uses_template_parms (type_scope))
10496 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10497
10498 /* Make access check error messages point to the location
10499 of the use of the typedef. */
10500 input_location = iter->locus;
10501 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10502 type_decl, type_decl,
10503 tf_warning_or_error);
10504 }
10505 input_location = saved_location;
10506 }
10507
10508 static tree
10509 instantiate_class_template_1 (tree type)
10510 {
10511 tree templ, args, pattern, t, member;
10512 tree typedecl;
10513 tree pbinfo;
10514 tree base_list;
10515 unsigned int saved_maximum_field_alignment;
10516 tree fn_context;
10517
10518 if (type == error_mark_node)
10519 return error_mark_node;
10520
10521 if (COMPLETE_OR_OPEN_TYPE_P (type)
10522 || uses_template_parms (type))
10523 return type;
10524
10525 /* Figure out which template is being instantiated. */
10526 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10527 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10528
10529 /* Mark the type as in the process of being defined. */
10530 TYPE_BEING_DEFINED (type) = 1;
10531
10532 /* Determine what specialization of the original template to
10533 instantiate. */
10534 t = most_specialized_partial_spec (type, tf_warning_or_error);
10535 if (t == error_mark_node)
10536 return error_mark_node;
10537 else if (t)
10538 {
10539 /* This TYPE is actually an instantiation of a partial
10540 specialization. We replace the innermost set of ARGS with
10541 the arguments appropriate for substitution. For example,
10542 given:
10543
10544 template <class T> struct S {};
10545 template <class T> struct S<T*> {};
10546
10547 and supposing that we are instantiating S<int*>, ARGS will
10548 presently be {int*} -- but we need {int}. */
10549 pattern = TREE_TYPE (t);
10550 args = TREE_PURPOSE (t);
10551 }
10552 else
10553 {
10554 pattern = TREE_TYPE (templ);
10555 args = CLASSTYPE_TI_ARGS (type);
10556 }
10557
10558 /* If the template we're instantiating is incomplete, then clearly
10559 there's nothing we can do. */
10560 if (!COMPLETE_TYPE_P (pattern))
10561 {
10562 /* We can try again later. */
10563 TYPE_BEING_DEFINED (type) = 0;
10564 return type;
10565 }
10566
10567 /* If we've recursively instantiated too many templates, stop. */
10568 if (! push_tinst_level (type))
10569 return type;
10570
10571 /* We may be in the middle of deferred access check. Disable
10572 it now. */
10573 push_deferring_access_checks (dk_no_deferred);
10574
10575 int saved_unevaluated_operand = cp_unevaluated_operand;
10576 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10577
10578 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10579 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10580 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10581 fn_context = error_mark_node;
10582 if (!fn_context)
10583 push_to_top_level ();
10584 else
10585 {
10586 cp_unevaluated_operand = 0;
10587 c_inhibit_evaluation_warnings = 0;
10588 }
10589 /* Use #pragma pack from the template context. */
10590 saved_maximum_field_alignment = maximum_field_alignment;
10591 maximum_field_alignment = TYPE_PRECISION (pattern);
10592
10593 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10594
10595 /* Set the input location to the most specialized template definition.
10596 This is needed if tsubsting causes an error. */
10597 typedecl = TYPE_MAIN_DECL (pattern);
10598 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10599 DECL_SOURCE_LOCATION (typedecl);
10600
10601 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10602 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10603 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10604 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10605 if (ANON_AGGR_TYPE_P (pattern))
10606 SET_ANON_AGGR_TYPE_P (type);
10607 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10608 {
10609 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10610 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10611 /* Adjust visibility for template arguments. */
10612 determine_visibility (TYPE_MAIN_DECL (type));
10613 }
10614 if (CLASS_TYPE_P (type))
10615 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10616
10617 pbinfo = TYPE_BINFO (pattern);
10618
10619 /* We should never instantiate a nested class before its enclosing
10620 class; we need to look up the nested class by name before we can
10621 instantiate it, and that lookup should instantiate the enclosing
10622 class. */
10623 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10624 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10625
10626 base_list = NULL_TREE;
10627 if (BINFO_N_BASE_BINFOS (pbinfo))
10628 {
10629 tree pbase_binfo;
10630 tree pushed_scope;
10631 int i;
10632
10633 /* We must enter the scope containing the type, as that is where
10634 the accessibility of types named in dependent bases are
10635 looked up from. */
10636 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10637
10638 /* Substitute into each of the bases to determine the actual
10639 basetypes. */
10640 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10641 {
10642 tree base;
10643 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10644 tree expanded_bases = NULL_TREE;
10645 int idx, len = 1;
10646
10647 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10648 {
10649 expanded_bases =
10650 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10651 args, tf_error, NULL_TREE);
10652 if (expanded_bases == error_mark_node)
10653 continue;
10654
10655 len = TREE_VEC_LENGTH (expanded_bases);
10656 }
10657
10658 for (idx = 0; idx < len; idx++)
10659 {
10660 if (expanded_bases)
10661 /* Extract the already-expanded base class. */
10662 base = TREE_VEC_ELT (expanded_bases, idx);
10663 else
10664 /* Substitute to figure out the base class. */
10665 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10666 NULL_TREE);
10667
10668 if (base == error_mark_node)
10669 continue;
10670
10671 base_list = tree_cons (access, base, base_list);
10672 if (BINFO_VIRTUAL_P (pbase_binfo))
10673 TREE_TYPE (base_list) = integer_type_node;
10674 }
10675 }
10676
10677 /* The list is now in reverse order; correct that. */
10678 base_list = nreverse (base_list);
10679
10680 if (pushed_scope)
10681 pop_scope (pushed_scope);
10682 }
10683 /* Now call xref_basetypes to set up all the base-class
10684 information. */
10685 xref_basetypes (type, base_list);
10686
10687 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10688 (int) ATTR_FLAG_TYPE_IN_PLACE,
10689 args, tf_error, NULL_TREE);
10690 fixup_attribute_variants (type);
10691
10692 /* Now that our base classes are set up, enter the scope of the
10693 class, so that name lookups into base classes, etc. will work
10694 correctly. This is precisely analogous to what we do in
10695 begin_class_definition when defining an ordinary non-template
10696 class, except we also need to push the enclosing classes. */
10697 push_nested_class (type);
10698
10699 /* Now members are processed in the order of declaration. */
10700 for (member = CLASSTYPE_DECL_LIST (pattern);
10701 member; member = TREE_CHAIN (member))
10702 {
10703 tree t = TREE_VALUE (member);
10704
10705 if (TREE_PURPOSE (member))
10706 {
10707 if (TYPE_P (t))
10708 {
10709 if (LAMBDA_TYPE_P (t))
10710 /* A closure type for a lambda in an NSDMI or default argument.
10711 Ignore it; it will be regenerated when needed. */
10712 continue;
10713
10714 /* Build new CLASSTYPE_NESTED_UTDS. */
10715
10716 tree newtag;
10717 bool class_template_p;
10718
10719 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10720 && TYPE_LANG_SPECIFIC (t)
10721 && CLASSTYPE_IS_TEMPLATE (t));
10722 /* If the member is a class template, then -- even after
10723 substitution -- there may be dependent types in the
10724 template argument list for the class. We increment
10725 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10726 that function will assume that no types are dependent
10727 when outside of a template. */
10728 if (class_template_p)
10729 ++processing_template_decl;
10730 newtag = tsubst (t, args, tf_error, NULL_TREE);
10731 if (class_template_p)
10732 --processing_template_decl;
10733 if (newtag == error_mark_node)
10734 continue;
10735
10736 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10737 {
10738 tree name = TYPE_IDENTIFIER (t);
10739
10740 if (class_template_p)
10741 /* Unfortunately, lookup_template_class sets
10742 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10743 instantiation (i.e., for the type of a member
10744 template class nested within a template class.)
10745 This behavior is required for
10746 maybe_process_partial_specialization to work
10747 correctly, but is not accurate in this case;
10748 the TAG is not an instantiation of anything.
10749 (The corresponding TEMPLATE_DECL is an
10750 instantiation, but the TYPE is not.) */
10751 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10752
10753 /* Now, we call pushtag to put this NEWTAG into the scope of
10754 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10755 pushtag calling push_template_decl. We don't have to do
10756 this for enums because it will already have been done in
10757 tsubst_enum. */
10758 if (name)
10759 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10760 pushtag (name, newtag, /*tag_scope=*/ts_current);
10761 }
10762 }
10763 else if (DECL_DECLARES_FUNCTION_P (t))
10764 {
10765 tree r;
10766
10767 if (TREE_CODE (t) == TEMPLATE_DECL)
10768 ++processing_template_decl;
10769 r = tsubst (t, args, tf_error, NULL_TREE);
10770 if (TREE_CODE (t) == TEMPLATE_DECL)
10771 --processing_template_decl;
10772 set_current_access_from_decl (r);
10773 finish_member_declaration (r);
10774 /* Instantiate members marked with attribute used. */
10775 if (r != error_mark_node && DECL_PRESERVE_P (r))
10776 mark_used (r);
10777 if (TREE_CODE (r) == FUNCTION_DECL
10778 && DECL_OMP_DECLARE_REDUCTION_P (r))
10779 cp_check_omp_declare_reduction (r);
10780 }
10781 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10782 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10783 /* A closure type for a lambda in an NSDMI or default argument.
10784 Ignore it; it will be regenerated when needed. */;
10785 else
10786 {
10787 /* Build new TYPE_FIELDS. */
10788 if (TREE_CODE (t) == STATIC_ASSERT)
10789 {
10790 tree condition;
10791
10792 ++c_inhibit_evaluation_warnings;
10793 condition =
10794 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10795 tf_warning_or_error, NULL_TREE,
10796 /*integral_constant_expression_p=*/true);
10797 --c_inhibit_evaluation_warnings;
10798
10799 finish_static_assert (condition,
10800 STATIC_ASSERT_MESSAGE (t),
10801 STATIC_ASSERT_SOURCE_LOCATION (t),
10802 /*member_p=*/true);
10803 }
10804 else if (TREE_CODE (t) != CONST_DECL)
10805 {
10806 tree r;
10807 tree vec = NULL_TREE;
10808 int len = 1;
10809
10810 /* The file and line for this declaration, to
10811 assist in error message reporting. Since we
10812 called push_tinst_level above, we don't need to
10813 restore these. */
10814 input_location = DECL_SOURCE_LOCATION (t);
10815
10816 if (TREE_CODE (t) == TEMPLATE_DECL)
10817 ++processing_template_decl;
10818 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10819 if (TREE_CODE (t) == TEMPLATE_DECL)
10820 --processing_template_decl;
10821
10822 if (TREE_CODE (r) == TREE_VEC)
10823 {
10824 /* A capture pack became multiple fields. */
10825 vec = r;
10826 len = TREE_VEC_LENGTH (vec);
10827 }
10828
10829 for (int i = 0; i < len; ++i)
10830 {
10831 if (vec)
10832 r = TREE_VEC_ELT (vec, i);
10833 if (VAR_P (r))
10834 {
10835 /* In [temp.inst]:
10836
10837 [t]he initialization (and any associated
10838 side-effects) of a static data member does
10839 not occur unless the static data member is
10840 itself used in a way that requires the
10841 definition of the static data member to
10842 exist.
10843
10844 Therefore, we do not substitute into the
10845 initialized for the static data member here. */
10846 finish_static_data_member_decl
10847 (r,
10848 /*init=*/NULL_TREE,
10849 /*init_const_expr_p=*/false,
10850 /*asmspec_tree=*/NULL_TREE,
10851 /*flags=*/0);
10852 /* Instantiate members marked with attribute used. */
10853 if (r != error_mark_node && DECL_PRESERVE_P (r))
10854 mark_used (r);
10855 }
10856 else if (TREE_CODE (r) == FIELD_DECL)
10857 {
10858 /* Determine whether R has a valid type and can be
10859 completed later. If R is invalid, then its type
10860 is replaced by error_mark_node. */
10861 tree rtype = TREE_TYPE (r);
10862 if (can_complete_type_without_circularity (rtype))
10863 complete_type (rtype);
10864
10865 if (!complete_or_array_type_p (rtype))
10866 {
10867 /* If R's type couldn't be completed and
10868 it isn't a flexible array member (whose
10869 type is incomplete by definition) give
10870 an error. */
10871 cxx_incomplete_type_error (r, rtype);
10872 TREE_TYPE (r) = error_mark_node;
10873 }
10874 }
10875
10876 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10877 such a thing will already have been added to the field
10878 list by tsubst_enum in finish_member_declaration in the
10879 CLASSTYPE_NESTED_UTDS case above. */
10880 if (!(TREE_CODE (r) == TYPE_DECL
10881 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10882 && DECL_ARTIFICIAL (r)))
10883 {
10884 set_current_access_from_decl (r);
10885 finish_member_declaration (r);
10886 }
10887 }
10888 }
10889 }
10890 }
10891 else
10892 {
10893 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10894 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10895 {
10896 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10897
10898 tree friend_type = t;
10899 bool adjust_processing_template_decl = false;
10900
10901 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10902 {
10903 /* template <class T> friend class C; */
10904 friend_type = tsubst_friend_class (friend_type, args);
10905 adjust_processing_template_decl = true;
10906 }
10907 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10908 {
10909 /* template <class T> friend class C::D; */
10910 friend_type = tsubst (friend_type, args,
10911 tf_warning_or_error, NULL_TREE);
10912 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10913 friend_type = TREE_TYPE (friend_type);
10914 adjust_processing_template_decl = true;
10915 }
10916 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10917 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10918 {
10919 /* This could be either
10920
10921 friend class T::C;
10922
10923 when dependent_type_p is false or
10924
10925 template <class U> friend class T::C;
10926
10927 otherwise. */
10928 /* Bump processing_template_decl in case this is something like
10929 template <class T> friend struct A<T>::B. */
10930 ++processing_template_decl;
10931 friend_type = tsubst (friend_type, args,
10932 tf_warning_or_error, NULL_TREE);
10933 if (dependent_type_p (friend_type))
10934 adjust_processing_template_decl = true;
10935 --processing_template_decl;
10936 }
10937 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10938 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10939 && TYPE_HIDDEN_P (friend_type))
10940 {
10941 /* friend class C;
10942
10943 where C hasn't been declared yet. Let's lookup name
10944 from namespace scope directly, bypassing any name that
10945 come from dependent base class. */
10946 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10947
10948 /* The call to xref_tag_from_type does injection for friend
10949 classes. */
10950 push_nested_namespace (ns);
10951 friend_type =
10952 xref_tag_from_type (friend_type, NULL_TREE,
10953 /*tag_scope=*/ts_current);
10954 pop_nested_namespace (ns);
10955 }
10956 else if (uses_template_parms (friend_type))
10957 /* friend class C<T>; */
10958 friend_type = tsubst (friend_type, args,
10959 tf_warning_or_error, NULL_TREE);
10960 /* Otherwise it's
10961
10962 friend class C;
10963
10964 where C is already declared or
10965
10966 friend class C<int>;
10967
10968 We don't have to do anything in these cases. */
10969
10970 if (adjust_processing_template_decl)
10971 /* Trick make_friend_class into realizing that the friend
10972 we're adding is a template, not an ordinary class. It's
10973 important that we use make_friend_class since it will
10974 perform some error-checking and output cross-reference
10975 information. */
10976 ++processing_template_decl;
10977
10978 if (friend_type != error_mark_node)
10979 make_friend_class (type, friend_type, /*complain=*/false);
10980
10981 if (adjust_processing_template_decl)
10982 --processing_template_decl;
10983 }
10984 else
10985 {
10986 /* Build new DECL_FRIENDLIST. */
10987 tree r;
10988
10989 /* The file and line for this declaration, to
10990 assist in error message reporting. Since we
10991 called push_tinst_level above, we don't need to
10992 restore these. */
10993 input_location = DECL_SOURCE_LOCATION (t);
10994
10995 if (TREE_CODE (t) == TEMPLATE_DECL)
10996 {
10997 ++processing_template_decl;
10998 push_deferring_access_checks (dk_no_check);
10999 }
11000
11001 r = tsubst_friend_function (t, args);
11002 add_friend (type, r, /*complain=*/false);
11003 if (TREE_CODE (t) == TEMPLATE_DECL)
11004 {
11005 pop_deferring_access_checks ();
11006 --processing_template_decl;
11007 }
11008 }
11009 }
11010 }
11011
11012 if (fn_context)
11013 {
11014 /* Restore these before substituting into the lambda capture
11015 initializers. */
11016 cp_unevaluated_operand = saved_unevaluated_operand;
11017 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11018 }
11019
11020 /* Set the file and line number information to whatever is given for
11021 the class itself. This puts error messages involving generated
11022 implicit functions at a predictable point, and the same point
11023 that would be used for non-template classes. */
11024 input_location = DECL_SOURCE_LOCATION (typedecl);
11025
11026 unreverse_member_declarations (type);
11027 finish_struct_1 (type);
11028 TYPE_BEING_DEFINED (type) = 0;
11029
11030 /* We don't instantiate default arguments for member functions. 14.7.1:
11031
11032 The implicit instantiation of a class template specialization causes
11033 the implicit instantiation of the declarations, but not of the
11034 definitions or default arguments, of the class member functions,
11035 member classes, static data members and member templates.... */
11036
11037 /* Some typedefs referenced from within the template code need to be access
11038 checked at template instantiation time, i.e now. These types were
11039 added to the template at parsing time. Let's get those and perform
11040 the access checks then. */
11041 perform_typedefs_access_check (pattern, args);
11042 perform_deferred_access_checks (tf_warning_or_error);
11043 pop_nested_class ();
11044 maximum_field_alignment = saved_maximum_field_alignment;
11045 if (!fn_context)
11046 pop_from_top_level ();
11047 pop_deferring_access_checks ();
11048 pop_tinst_level ();
11049
11050 /* The vtable for a template class can be emitted in any translation
11051 unit in which the class is instantiated. When there is no key
11052 method, however, finish_struct_1 will already have added TYPE to
11053 the keyed_classes. */
11054 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11055 vec_safe_push (keyed_classes, type);
11056
11057 return type;
11058 }
11059
11060 /* Wrapper for instantiate_class_template_1. */
11061
11062 tree
11063 instantiate_class_template (tree type)
11064 {
11065 tree ret;
11066 timevar_push (TV_TEMPLATE_INST);
11067 ret = instantiate_class_template_1 (type);
11068 timevar_pop (TV_TEMPLATE_INST);
11069 return ret;
11070 }
11071
11072 static tree
11073 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11074 {
11075 tree r;
11076
11077 if (!t)
11078 r = t;
11079 else if (TYPE_P (t))
11080 r = tsubst (t, args, complain, in_decl);
11081 else
11082 {
11083 if (!(complain & tf_warning))
11084 ++c_inhibit_evaluation_warnings;
11085 r = tsubst_expr (t, args, complain, in_decl,
11086 /*integral_constant_expression_p=*/true);
11087 if (!(complain & tf_warning))
11088 --c_inhibit_evaluation_warnings;
11089 }
11090 return r;
11091 }
11092
11093 /* Given a function parameter pack TMPL_PARM and some function parameters
11094 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11095 and set *SPEC_P to point at the next point in the list. */
11096
11097 tree
11098 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11099 {
11100 /* Collect all of the extra "packed" parameters into an
11101 argument pack. */
11102 tree parmvec;
11103 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11104 tree spec_parm = *spec_p;
11105 int i, len;
11106
11107 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11108 if (tmpl_parm
11109 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11110 break;
11111
11112 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11113 parmvec = make_tree_vec (len);
11114 spec_parm = *spec_p;
11115 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11116 {
11117 tree elt = spec_parm;
11118 if (DECL_PACK_P (elt))
11119 elt = make_pack_expansion (elt);
11120 TREE_VEC_ELT (parmvec, i) = elt;
11121 }
11122
11123 /* Build the argument packs. */
11124 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11125 *spec_p = spec_parm;
11126
11127 return argpack;
11128 }
11129
11130 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11131 NONTYPE_ARGUMENT_PACK. */
11132
11133 static tree
11134 make_fnparm_pack (tree spec_parm)
11135 {
11136 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11137 }
11138
11139 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11140 pack expansion with no extra args, 2 if it has extra args, or 0
11141 if it is not a pack expansion. */
11142
11143 static int
11144 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11145 {
11146 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11147 if (i >= TREE_VEC_LENGTH (vec))
11148 return 0;
11149 tree elt = TREE_VEC_ELT (vec, i);
11150 if (DECL_P (elt))
11151 /* A decl pack is itself an expansion. */
11152 elt = TREE_TYPE (elt);
11153 if (!PACK_EXPANSION_P (elt))
11154 return 0;
11155 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11156 return 2;
11157 return 1;
11158 }
11159
11160
11161 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11162
11163 static tree
11164 make_argument_pack_select (tree arg_pack, unsigned index)
11165 {
11166 tree aps = make_node (ARGUMENT_PACK_SELECT);
11167
11168 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11169 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11170
11171 return aps;
11172 }
11173
11174 /* This is a subroutine of tsubst_pack_expansion.
11175
11176 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11177 mechanism to store the (non complete list of) arguments of the
11178 substitution and return a non substituted pack expansion, in order
11179 to wait for when we have enough arguments to really perform the
11180 substitution. */
11181
11182 static bool
11183 use_pack_expansion_extra_args_p (tree parm_packs,
11184 int arg_pack_len,
11185 bool has_empty_arg)
11186 {
11187 /* If one pack has an expansion and another pack has a normal
11188 argument or if one pack has an empty argument and an another
11189 one hasn't then tsubst_pack_expansion cannot perform the
11190 substitution and need to fall back on the
11191 PACK_EXPANSION_EXTRA mechanism. */
11192 if (parm_packs == NULL_TREE)
11193 return false;
11194 else if (has_empty_arg)
11195 return true;
11196
11197 bool has_expansion_arg = false;
11198 for (int i = 0 ; i < arg_pack_len; ++i)
11199 {
11200 bool has_non_expansion_arg = false;
11201 for (tree parm_pack = parm_packs;
11202 parm_pack;
11203 parm_pack = TREE_CHAIN (parm_pack))
11204 {
11205 tree arg = TREE_VALUE (parm_pack);
11206
11207 int exp = argument_pack_element_is_expansion_p (arg, i);
11208 if (exp == 2)
11209 /* We can't substitute a pack expansion with extra args into
11210 our pattern. */
11211 return true;
11212 else if (exp)
11213 has_expansion_arg = true;
11214 else
11215 has_non_expansion_arg = true;
11216 }
11217
11218 if (has_expansion_arg && has_non_expansion_arg)
11219 return true;
11220 }
11221 return false;
11222 }
11223
11224 /* [temp.variadic]/6 says that:
11225
11226 The instantiation of a pack expansion [...]
11227 produces a list E1,E2, ..., En, where N is the number of elements
11228 in the pack expansion parameters.
11229
11230 This subroutine of tsubst_pack_expansion produces one of these Ei.
11231
11232 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11233 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11234 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11235 INDEX is the index 'i' of the element Ei to produce. ARGS,
11236 COMPLAIN, and IN_DECL are the same parameters as for the
11237 tsubst_pack_expansion function.
11238
11239 The function returns the resulting Ei upon successful completion,
11240 or error_mark_node.
11241
11242 Note that this function possibly modifies the ARGS parameter, so
11243 it's the responsibility of the caller to restore it. */
11244
11245 static tree
11246 gen_elem_of_pack_expansion_instantiation (tree pattern,
11247 tree parm_packs,
11248 unsigned index,
11249 tree args /* This parm gets
11250 modified. */,
11251 tsubst_flags_t complain,
11252 tree in_decl)
11253 {
11254 tree t;
11255 bool ith_elem_is_expansion = false;
11256
11257 /* For each parameter pack, change the substitution of the parameter
11258 pack to the ith argument in its argument pack, then expand the
11259 pattern. */
11260 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11261 {
11262 tree parm = TREE_PURPOSE (pack);
11263 tree arg_pack = TREE_VALUE (pack);
11264 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11265
11266 ith_elem_is_expansion |=
11267 argument_pack_element_is_expansion_p (arg_pack, index);
11268
11269 /* Select the Ith argument from the pack. */
11270 if (TREE_CODE (parm) == PARM_DECL
11271 || VAR_P (parm)
11272 || TREE_CODE (parm) == FIELD_DECL)
11273 {
11274 if (index == 0)
11275 {
11276 aps = make_argument_pack_select (arg_pack, index);
11277 if (!mark_used (parm, complain) && !(complain & tf_error))
11278 return error_mark_node;
11279 register_local_specialization (aps, parm);
11280 }
11281 else
11282 aps = retrieve_local_specialization (parm);
11283 }
11284 else
11285 {
11286 int idx, level;
11287 template_parm_level_and_index (parm, &level, &idx);
11288
11289 if (index == 0)
11290 {
11291 aps = make_argument_pack_select (arg_pack, index);
11292 /* Update the corresponding argument. */
11293 TMPL_ARG (args, level, idx) = aps;
11294 }
11295 else
11296 /* Re-use the ARGUMENT_PACK_SELECT. */
11297 aps = TMPL_ARG (args, level, idx);
11298 }
11299 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11300 }
11301
11302 /* Substitute into the PATTERN with the (possibly altered)
11303 arguments. */
11304 if (pattern == in_decl)
11305 /* Expanding a fixed parameter pack from
11306 coerce_template_parameter_pack. */
11307 t = tsubst_decl (pattern, args, complain);
11308 else if (pattern == error_mark_node)
11309 t = error_mark_node;
11310 else if (constraint_p (pattern))
11311 {
11312 if (processing_template_decl)
11313 t = tsubst_constraint (pattern, args, complain, in_decl);
11314 else
11315 t = (constraints_satisfied_p (pattern, args)
11316 ? boolean_true_node : boolean_false_node);
11317 }
11318 else if (!TYPE_P (pattern))
11319 t = tsubst_expr (pattern, args, complain, in_decl,
11320 /*integral_constant_expression_p=*/false);
11321 else
11322 t = tsubst (pattern, args, complain, in_decl);
11323
11324 /* If the Ith argument pack element is a pack expansion, then
11325 the Ith element resulting from the substituting is going to
11326 be a pack expansion as well. */
11327 if (ith_elem_is_expansion)
11328 t = make_pack_expansion (t, complain);
11329
11330 return t;
11331 }
11332
11333 /* When the unexpanded parameter pack in a fold expression expands to an empty
11334 sequence, the value of the expression is as follows; the program is
11335 ill-formed if the operator is not listed in this table.
11336
11337 && true
11338 || false
11339 , void() */
11340
11341 tree
11342 expand_empty_fold (tree t, tsubst_flags_t complain)
11343 {
11344 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11345 if (!FOLD_EXPR_MODIFY_P (t))
11346 switch (code)
11347 {
11348 case TRUTH_ANDIF_EXPR:
11349 return boolean_true_node;
11350 case TRUTH_ORIF_EXPR:
11351 return boolean_false_node;
11352 case COMPOUND_EXPR:
11353 return void_node;
11354 default:
11355 break;
11356 }
11357
11358 if (complain & tf_error)
11359 error_at (location_of (t),
11360 "fold of empty expansion over %O", code);
11361 return error_mark_node;
11362 }
11363
11364 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11365 form an expression that combines the two terms using the
11366 operator of T. */
11367
11368 static tree
11369 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11370 {
11371 tree op = FOLD_EXPR_OP (t);
11372 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11373
11374 // Handle compound assignment operators.
11375 if (FOLD_EXPR_MODIFY_P (t))
11376 return build_x_modify_expr (input_location, left, code, right, complain);
11377
11378 switch (code)
11379 {
11380 case COMPOUND_EXPR:
11381 return build_x_compound_expr (input_location, left, right, complain);
11382 case DOTSTAR_EXPR:
11383 return build_m_component_ref (left, right, complain);
11384 default:
11385 return build_x_binary_op (input_location, code,
11386 left, TREE_CODE (left),
11387 right, TREE_CODE (right),
11388 /*overload=*/NULL,
11389 complain);
11390 }
11391 }
11392
11393 /* Substitute ARGS into the pack of a fold expression T. */
11394
11395 static inline tree
11396 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11397 {
11398 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11399 }
11400
11401 /* Substitute ARGS into the pack of a fold expression T. */
11402
11403 static inline tree
11404 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11405 {
11406 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11407 }
11408
11409 /* Expand a PACK of arguments into a grouped as left fold.
11410 Given a pack containing elements A0, A1, ..., An and an
11411 operator @, this builds the expression:
11412
11413 ((A0 @ A1) @ A2) ... @ An
11414
11415 Note that PACK must not be empty.
11416
11417 The operator is defined by the original fold expression T. */
11418
11419 static tree
11420 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11421 {
11422 tree left = TREE_VEC_ELT (pack, 0);
11423 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11424 {
11425 tree right = TREE_VEC_ELT (pack, i);
11426 left = fold_expression (t, left, right, complain);
11427 }
11428 return left;
11429 }
11430
11431 /* Substitute into a unary left fold expression. */
11432
11433 static tree
11434 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11435 tree in_decl)
11436 {
11437 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11438 if (pack == error_mark_node)
11439 return error_mark_node;
11440 if (PACK_EXPANSION_P (pack))
11441 {
11442 tree r = copy_node (t);
11443 FOLD_EXPR_PACK (r) = pack;
11444 return r;
11445 }
11446 if (TREE_VEC_LENGTH (pack) == 0)
11447 return expand_empty_fold (t, complain);
11448 else
11449 return expand_left_fold (t, pack, complain);
11450 }
11451
11452 /* Substitute into a binary left fold expression.
11453
11454 Do ths by building a single (non-empty) vector of argumnts and
11455 building the expression from those elements. */
11456
11457 static tree
11458 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11459 tree in_decl)
11460 {
11461 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11462 if (pack == error_mark_node)
11463 return error_mark_node;
11464 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11465 if (init == error_mark_node)
11466 return error_mark_node;
11467
11468 if (PACK_EXPANSION_P (pack))
11469 {
11470 tree r = copy_node (t);
11471 FOLD_EXPR_PACK (r) = pack;
11472 FOLD_EXPR_INIT (r) = init;
11473 return r;
11474 }
11475
11476 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11477 TREE_VEC_ELT (vec, 0) = init;
11478 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11479 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11480
11481 return expand_left_fold (t, vec, complain);
11482 }
11483
11484 /* Expand a PACK of arguments into a grouped as right fold.
11485 Given a pack containing elementns A0, A1, ..., and an
11486 operator @, this builds the expression:
11487
11488 A0@ ... (An-2 @ (An-1 @ An))
11489
11490 Note that PACK must not be empty.
11491
11492 The operator is defined by the original fold expression T. */
11493
11494 tree
11495 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11496 {
11497 // Build the expression.
11498 int n = TREE_VEC_LENGTH (pack);
11499 tree right = TREE_VEC_ELT (pack, n - 1);
11500 for (--n; n != 0; --n)
11501 {
11502 tree left = TREE_VEC_ELT (pack, n - 1);
11503 right = fold_expression (t, left, right, complain);
11504 }
11505 return right;
11506 }
11507
11508 /* Substitute into a unary right fold expression. */
11509
11510 static tree
11511 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11512 tree in_decl)
11513 {
11514 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11515 if (pack == error_mark_node)
11516 return error_mark_node;
11517 if (PACK_EXPANSION_P (pack))
11518 {
11519 tree r = copy_node (t);
11520 FOLD_EXPR_PACK (r) = pack;
11521 return r;
11522 }
11523 if (TREE_VEC_LENGTH (pack) == 0)
11524 return expand_empty_fold (t, complain);
11525 else
11526 return expand_right_fold (t, pack, complain);
11527 }
11528
11529 /* Substitute into a binary right fold expression.
11530
11531 Do ths by building a single (non-empty) vector of arguments and
11532 building the expression from those elements. */
11533
11534 static tree
11535 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11536 tree in_decl)
11537 {
11538 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11539 if (pack == error_mark_node)
11540 return error_mark_node;
11541 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11542 if (init == error_mark_node)
11543 return error_mark_node;
11544
11545 if (PACK_EXPANSION_P (pack))
11546 {
11547 tree r = copy_node (t);
11548 FOLD_EXPR_PACK (r) = pack;
11549 FOLD_EXPR_INIT (r) = init;
11550 return r;
11551 }
11552
11553 int n = TREE_VEC_LENGTH (pack);
11554 tree vec = make_tree_vec (n + 1);
11555 for (int i = 0; i < n; ++i)
11556 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11557 TREE_VEC_ELT (vec, n) = init;
11558
11559 return expand_right_fold (t, vec, complain);
11560 }
11561
11562 /* Walk through the pattern of a pack expansion, adding everything in
11563 local_specializations to a list. */
11564
11565 struct el_data
11566 {
11567 tree extra;
11568 tsubst_flags_t complain;
11569 };
11570 static tree
11571 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11572 {
11573 el_data &data = *reinterpret_cast<el_data*>(data_);
11574 tree *extra = &data.extra;
11575 tsubst_flags_t complain = data.complain;
11576 if (tree spec = retrieve_local_specialization (*tp))
11577 {
11578 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11579 {
11580 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11581 tree args = ARGUMENT_PACK_ARGS (spec);
11582 if (TREE_VEC_LENGTH (args) == 1)
11583 {
11584 tree elt = TREE_VEC_ELT (args, 0);
11585 if (PACK_EXPANSION_P (elt))
11586 elt = PACK_EXPANSION_PATTERN (elt);
11587 if (DECL_PACK_P (elt))
11588 spec = elt;
11589 }
11590 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11591 {
11592 /* Handle lambda capture here, since we aren't doing any
11593 substitution now, and so tsubst_copy won't call
11594 process_outer_var_ref. */
11595 tree args = ARGUMENT_PACK_ARGS (spec);
11596 int len = TREE_VEC_LENGTH (args);
11597 for (int i = 0; i < len; ++i)
11598 {
11599 tree arg = TREE_VEC_ELT (args, i);
11600 tree carg = arg;
11601 if (outer_automatic_var_p (arg))
11602 carg = process_outer_var_ref (arg, complain);
11603 if (carg != arg)
11604 {
11605 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11606 proxies. */
11607 if (i == 0)
11608 {
11609 spec = copy_node (spec);
11610 args = copy_node (args);
11611 SET_ARGUMENT_PACK_ARGS (spec, args);
11612 register_local_specialization (spec, *tp);
11613 }
11614 TREE_VEC_ELT (args, i) = carg;
11615 }
11616 }
11617 }
11618 }
11619 if (outer_automatic_var_p (spec))
11620 spec = process_outer_var_ref (spec, complain);
11621 *extra = tree_cons (*tp, spec, *extra);
11622 }
11623 return NULL_TREE;
11624 }
11625 static tree
11626 extract_local_specs (tree pattern, tsubst_flags_t complain)
11627 {
11628 el_data data = { NULL_TREE, complain };
11629 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11630 return data.extra;
11631 }
11632
11633 /* Substitute ARGS into T, which is an pack expansion
11634 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11635 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11636 (if only a partial substitution could be performed) or
11637 ERROR_MARK_NODE if there was an error. */
11638 tree
11639 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11640 tree in_decl)
11641 {
11642 tree pattern;
11643 tree pack, packs = NULL_TREE;
11644 bool unsubstituted_packs = false;
11645 bool unsubstituted_fn_pack = false;
11646 int i, len = -1;
11647 tree result;
11648 hash_map<tree, tree> *saved_local_specializations = NULL;
11649 bool need_local_specializations = false;
11650 int levels;
11651
11652 gcc_assert (PACK_EXPANSION_P (t));
11653 pattern = PACK_EXPANSION_PATTERN (t);
11654
11655 /* Add in any args remembered from an earlier partial instantiation. */
11656 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11657 if (extra && TREE_CODE (extra) == TREE_LIST)
11658 {
11659 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11660 {
11661 /* The partial instantiation involved local declarations collected in
11662 extract_local_specs; map from the general template to our local
11663 context. */
11664 tree gen = TREE_PURPOSE (elt);
11665 tree inst = TREE_VALUE (elt);
11666 if (DECL_P (inst))
11667 if (tree local = retrieve_local_specialization (inst))
11668 inst = local;
11669 /* else inst is already a full instantiation of the pack. */
11670 register_local_specialization (inst, gen);
11671 }
11672 gcc_assert (!TREE_PURPOSE (extra));
11673 extra = TREE_VALUE (extra);
11674 }
11675 args = add_to_template_args (extra, args);
11676
11677 levels = TMPL_ARGS_DEPTH (args);
11678
11679 /* Determine the argument packs that will instantiate the parameter
11680 packs used in the expansion expression. While we're at it,
11681 compute the number of arguments to be expanded and make sure it
11682 is consistent. */
11683 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11684 pack = TREE_CHAIN (pack))
11685 {
11686 tree parm_pack = TREE_VALUE (pack);
11687 tree arg_pack = NULL_TREE;
11688 tree orig_arg = NULL_TREE;
11689 int level = 0;
11690
11691 if (TREE_CODE (parm_pack) == BASES)
11692 {
11693 gcc_assert (parm_pack == pattern);
11694 if (BASES_DIRECT (parm_pack))
11695 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11696 args, complain, in_decl, false));
11697 else
11698 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11699 args, complain, in_decl, false));
11700 }
11701 else if (builtin_pack_call_p (parm_pack))
11702 {
11703 /* ??? Support use in other patterns. */
11704 gcc_assert (parm_pack == pattern);
11705 return expand_builtin_pack_call (parm_pack, args,
11706 complain, in_decl);
11707 }
11708 else if (TREE_CODE (parm_pack) == PARM_DECL)
11709 {
11710 /* We know we have correct local_specializations if this
11711 expansion is at function scope, or if we're dealing with a
11712 local parameter in a requires expression; for the latter,
11713 tsubst_requires_expr set it up appropriately. */
11714 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11715 arg_pack = retrieve_local_specialization (parm_pack);
11716 else
11717 /* We can't rely on local_specializations for a parameter
11718 name used later in a function declaration (such as in a
11719 late-specified return type). Even if it exists, it might
11720 have the wrong value for a recursive call. */
11721 need_local_specializations = true;
11722
11723 if (!arg_pack)
11724 {
11725 /* This parameter pack was used in an unevaluated context. Just
11726 make a dummy decl, since it's only used for its type. */
11727 ++cp_unevaluated_operand;
11728 arg_pack = tsubst_decl (parm_pack, args, complain);
11729 --cp_unevaluated_operand;
11730 if (arg_pack && DECL_PACK_P (arg_pack))
11731 /* Partial instantiation of the parm_pack, we can't build
11732 up an argument pack yet. */
11733 arg_pack = NULL_TREE;
11734 else
11735 arg_pack = make_fnparm_pack (arg_pack);
11736 }
11737 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11738 /* This argument pack isn't fully instantiated yet. We set this
11739 flag rather than clear arg_pack because we do want to do the
11740 optimization below, and we don't want to substitute directly
11741 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11742 where it isn't expected). */
11743 unsubstituted_fn_pack = true;
11744 }
11745 else if (is_normal_capture_proxy (parm_pack))
11746 {
11747 arg_pack = retrieve_local_specialization (parm_pack);
11748 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11749 unsubstituted_fn_pack = true;
11750 }
11751 else
11752 {
11753 int idx;
11754 template_parm_level_and_index (parm_pack, &level, &idx);
11755
11756 if (level <= levels)
11757 arg_pack = TMPL_ARG (args, level, idx);
11758 }
11759
11760 orig_arg = arg_pack;
11761 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11762 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11763
11764 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11765 /* This can only happen if we forget to expand an argument
11766 pack somewhere else. Just return an error, silently. */
11767 {
11768 result = make_tree_vec (1);
11769 TREE_VEC_ELT (result, 0) = error_mark_node;
11770 return result;
11771 }
11772
11773 if (arg_pack)
11774 {
11775 int my_len =
11776 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11777
11778 /* Don't bother trying to do a partial substitution with
11779 incomplete packs; we'll try again after deduction. */
11780 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11781 return t;
11782
11783 if (len < 0)
11784 len = my_len;
11785 else if (len != my_len
11786 && !unsubstituted_fn_pack)
11787 {
11788 if (!(complain & tf_error))
11789 /* Fail quietly. */;
11790 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11791 error ("mismatched argument pack lengths while expanding %qT",
11792 pattern);
11793 else
11794 error ("mismatched argument pack lengths while expanding %qE",
11795 pattern);
11796 return error_mark_node;
11797 }
11798
11799 /* Keep track of the parameter packs and their corresponding
11800 argument packs. */
11801 packs = tree_cons (parm_pack, arg_pack, packs);
11802 TREE_TYPE (packs) = orig_arg;
11803 }
11804 else
11805 {
11806 /* We can't substitute for this parameter pack. We use a flag as
11807 well as the missing_level counter because function parameter
11808 packs don't have a level. */
11809 gcc_assert (processing_template_decl);
11810 unsubstituted_packs = true;
11811 }
11812 }
11813
11814 /* If the expansion is just T..., return the matching argument pack, unless
11815 we need to call convert_from_reference on all the elements. This is an
11816 important optimization; see c++/68422. */
11817 if (!unsubstituted_packs
11818 && TREE_PURPOSE (packs) == pattern)
11819 {
11820 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11821
11822 /* If the argument pack is a single pack expansion, pull it out. */
11823 if (TREE_VEC_LENGTH (args) == 1
11824 && pack_expansion_args_count (args))
11825 return TREE_VEC_ELT (args, 0);
11826
11827 /* Types need no adjustment, nor does sizeof..., and if we still have
11828 some pack expansion args we won't do anything yet. */
11829 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11830 || PACK_EXPANSION_SIZEOF_P (t)
11831 || pack_expansion_args_count (args))
11832 return args;
11833 /* Also optimize expression pack expansions if we can tell that the
11834 elements won't have reference type. */
11835 tree type = TREE_TYPE (pattern);
11836 if (type && TREE_CODE (type) != REFERENCE_TYPE
11837 && !PACK_EXPANSION_P (type)
11838 && !WILDCARD_TYPE_P (type))
11839 return args;
11840 /* Otherwise use the normal path so we get convert_from_reference. */
11841 }
11842
11843 /* We cannot expand this expansion expression, because we don't have
11844 all of the argument packs we need. */
11845 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11846 {
11847 /* We got some full packs, but we can't substitute them in until we
11848 have values for all the packs. So remember these until then. */
11849
11850 t = make_pack_expansion (pattern, complain);
11851 tree extra = args;
11852 if (local_specializations)
11853 if (tree locals = extract_local_specs (pattern, complain))
11854 extra = tree_cons (NULL_TREE, extra, locals);
11855 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11856 return t;
11857 }
11858 else if (unsubstituted_packs)
11859 {
11860 /* There were no real arguments, we're just replacing a parameter
11861 pack with another version of itself. Substitute into the
11862 pattern and return a PACK_EXPANSION_*. The caller will need to
11863 deal with that. */
11864 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11865 t = tsubst_expr (pattern, args, complain, in_decl,
11866 /*integral_constant_expression_p=*/false);
11867 else
11868 t = tsubst (pattern, args, complain, in_decl);
11869 t = make_pack_expansion (t, complain);
11870 return t;
11871 }
11872
11873 gcc_assert (len >= 0);
11874
11875 if (need_local_specializations)
11876 {
11877 /* We're in a late-specified return type, so create our own local
11878 specializations map; the current map is either NULL or (in the
11879 case of recursive unification) might have bindings that we don't
11880 want to use or alter. */
11881 saved_local_specializations = local_specializations;
11882 local_specializations = new hash_map<tree, tree>;
11883 }
11884
11885 /* For each argument in each argument pack, substitute into the
11886 pattern. */
11887 result = make_tree_vec (len);
11888 tree elem_args = copy_template_args (args);
11889 for (i = 0; i < len; ++i)
11890 {
11891 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11892 i,
11893 elem_args, complain,
11894 in_decl);
11895 TREE_VEC_ELT (result, i) = t;
11896 if (t == error_mark_node)
11897 {
11898 result = error_mark_node;
11899 break;
11900 }
11901 }
11902
11903 /* Update ARGS to restore the substitution from parameter packs to
11904 their argument packs. */
11905 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11906 {
11907 tree parm = TREE_PURPOSE (pack);
11908
11909 if (TREE_CODE (parm) == PARM_DECL
11910 || VAR_P (parm)
11911 || TREE_CODE (parm) == FIELD_DECL)
11912 register_local_specialization (TREE_TYPE (pack), parm);
11913 else
11914 {
11915 int idx, level;
11916
11917 if (TREE_VALUE (pack) == NULL_TREE)
11918 continue;
11919
11920 template_parm_level_and_index (parm, &level, &idx);
11921
11922 /* Update the corresponding argument. */
11923 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11924 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11925 TREE_TYPE (pack);
11926 else
11927 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11928 }
11929 }
11930
11931 if (need_local_specializations)
11932 {
11933 delete local_specializations;
11934 local_specializations = saved_local_specializations;
11935 }
11936
11937 /* If the dependent pack arguments were such that we end up with only a
11938 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11939 if (len == 1 && TREE_CODE (result) == TREE_VEC
11940 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11941 return TREE_VEC_ELT (result, 0);
11942
11943 return result;
11944 }
11945
11946 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11947 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11948 parameter packs; all parms generated from a function parameter pack will
11949 have the same DECL_PARM_INDEX. */
11950
11951 tree
11952 get_pattern_parm (tree parm, tree tmpl)
11953 {
11954 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11955 tree patparm;
11956
11957 if (DECL_ARTIFICIAL (parm))
11958 {
11959 for (patparm = DECL_ARGUMENTS (pattern);
11960 patparm; patparm = DECL_CHAIN (patparm))
11961 if (DECL_ARTIFICIAL (patparm)
11962 && DECL_NAME (parm) == DECL_NAME (patparm))
11963 break;
11964 }
11965 else
11966 {
11967 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11968 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11969 gcc_assert (DECL_PARM_INDEX (patparm)
11970 == DECL_PARM_INDEX (parm));
11971 }
11972
11973 return patparm;
11974 }
11975
11976 /* Make an argument pack out of the TREE_VEC VEC. */
11977
11978 static tree
11979 make_argument_pack (tree vec)
11980 {
11981 tree pack;
11982 tree elt = TREE_VEC_ELT (vec, 0);
11983 if (TYPE_P (elt))
11984 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11985 else
11986 {
11987 pack = make_node (NONTYPE_ARGUMENT_PACK);
11988 TREE_CONSTANT (pack) = 1;
11989 }
11990 SET_ARGUMENT_PACK_ARGS (pack, vec);
11991 return pack;
11992 }
11993
11994 /* Return an exact copy of template args T that can be modified
11995 independently. */
11996
11997 static tree
11998 copy_template_args (tree t)
11999 {
12000 if (t == error_mark_node)
12001 return t;
12002
12003 int len = TREE_VEC_LENGTH (t);
12004 tree new_vec = make_tree_vec (len);
12005
12006 for (int i = 0; i < len; ++i)
12007 {
12008 tree elt = TREE_VEC_ELT (t, i);
12009 if (elt && TREE_CODE (elt) == TREE_VEC)
12010 elt = copy_template_args (elt);
12011 TREE_VEC_ELT (new_vec, i) = elt;
12012 }
12013
12014 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12015 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12016
12017 return new_vec;
12018 }
12019
12020 /* Substitute ARGS into the vector or list of template arguments T. */
12021
12022 static tree
12023 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12024 {
12025 tree orig_t = t;
12026 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12027 tree *elts;
12028
12029 if (t == error_mark_node)
12030 return error_mark_node;
12031
12032 len = TREE_VEC_LENGTH (t);
12033 elts = XALLOCAVEC (tree, len);
12034
12035 for (i = 0; i < len; i++)
12036 {
12037 tree orig_arg = TREE_VEC_ELT (t, i);
12038 tree new_arg;
12039
12040 if (TREE_CODE (orig_arg) == TREE_VEC)
12041 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12042 else if (PACK_EXPANSION_P (orig_arg))
12043 {
12044 /* Substitute into an expansion expression. */
12045 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12046
12047 if (TREE_CODE (new_arg) == TREE_VEC)
12048 /* Add to the expanded length adjustment the number of
12049 expanded arguments. We subtract one from this
12050 measurement, because the argument pack expression
12051 itself is already counted as 1 in
12052 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12053 the argument pack is empty. */
12054 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12055 }
12056 else if (ARGUMENT_PACK_P (orig_arg))
12057 {
12058 /* Substitute into each of the arguments. */
12059 new_arg = TYPE_P (orig_arg)
12060 ? cxx_make_type (TREE_CODE (orig_arg))
12061 : make_node (TREE_CODE (orig_arg));
12062
12063 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12064 args, complain, in_decl);
12065 if (pack_args == error_mark_node)
12066 new_arg = error_mark_node;
12067 else
12068 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12069
12070 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12071 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12072 }
12073 else
12074 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12075
12076 if (new_arg == error_mark_node)
12077 return error_mark_node;
12078
12079 elts[i] = new_arg;
12080 if (new_arg != orig_arg)
12081 need_new = 1;
12082 }
12083
12084 if (!need_new)
12085 return t;
12086
12087 /* Make space for the expanded arguments coming from template
12088 argument packs. */
12089 t = make_tree_vec (len + expanded_len_adjust);
12090 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12091 arguments for a member template.
12092 In that case each TREE_VEC in ORIG_T represents a level of template
12093 arguments, and ORIG_T won't carry any non defaulted argument count.
12094 It will rather be the nested TREE_VECs that will carry one.
12095 In other words, ORIG_T carries a non defaulted argument count only
12096 if it doesn't contain any nested TREE_VEC. */
12097 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12098 {
12099 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12100 count += expanded_len_adjust;
12101 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12102 }
12103 for (i = 0, out = 0; i < len; i++)
12104 {
12105 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12106 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12107 && TREE_CODE (elts[i]) == TREE_VEC)
12108 {
12109 int idx;
12110
12111 /* Now expand the template argument pack "in place". */
12112 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12113 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12114 }
12115 else
12116 {
12117 TREE_VEC_ELT (t, out) = elts[i];
12118 out++;
12119 }
12120 }
12121
12122 return t;
12123 }
12124
12125 /* Substitute ARGS into one level PARMS of template parameters. */
12126
12127 static tree
12128 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12129 {
12130 if (parms == error_mark_node)
12131 return error_mark_node;
12132
12133 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12134
12135 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12136 {
12137 tree tuple = TREE_VEC_ELT (parms, i);
12138
12139 if (tuple == error_mark_node)
12140 continue;
12141
12142 TREE_VEC_ELT (new_vec, i) =
12143 tsubst_template_parm (tuple, args, complain);
12144 }
12145
12146 return new_vec;
12147 }
12148
12149 /* Return the result of substituting ARGS into the template parameters
12150 given by PARMS. If there are m levels of ARGS and m + n levels of
12151 PARMS, then the result will contain n levels of PARMS. For
12152 example, if PARMS is `template <class T> template <class U>
12153 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12154 result will be `template <int*, double, class V>'. */
12155
12156 static tree
12157 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12158 {
12159 tree r = NULL_TREE;
12160 tree* new_parms;
12161
12162 /* When substituting into a template, we must set
12163 PROCESSING_TEMPLATE_DECL as the template parameters may be
12164 dependent if they are based on one-another, and the dependency
12165 predicates are short-circuit outside of templates. */
12166 ++processing_template_decl;
12167
12168 for (new_parms = &r;
12169 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12170 new_parms = &(TREE_CHAIN (*new_parms)),
12171 parms = TREE_CHAIN (parms))
12172 {
12173 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12174 args, complain);
12175 *new_parms =
12176 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12177 - TMPL_ARGS_DEPTH (args)),
12178 new_vec, NULL_TREE);
12179 }
12180
12181 --processing_template_decl;
12182
12183 return r;
12184 }
12185
12186 /* Return the result of substituting ARGS into one template parameter
12187 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12188 parameter and which TREE_PURPOSE is the default argument of the
12189 template parameter. */
12190
12191 static tree
12192 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12193 {
12194 tree default_value, parm_decl;
12195
12196 if (args == NULL_TREE
12197 || t == NULL_TREE
12198 || t == error_mark_node)
12199 return t;
12200
12201 gcc_assert (TREE_CODE (t) == TREE_LIST);
12202
12203 default_value = TREE_PURPOSE (t);
12204 parm_decl = TREE_VALUE (t);
12205
12206 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12207 if (TREE_CODE (parm_decl) == PARM_DECL
12208 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12209 parm_decl = error_mark_node;
12210 default_value = tsubst_template_arg (default_value, args,
12211 complain, NULL_TREE);
12212
12213 return build_tree_list (default_value, parm_decl);
12214 }
12215
12216 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12217 type T. If T is not an aggregate or enumeration type, it is
12218 handled as if by tsubst. IN_DECL is as for tsubst. If
12219 ENTERING_SCOPE is nonzero, T is the context for a template which
12220 we are presently tsubst'ing. Return the substituted value. */
12221
12222 static tree
12223 tsubst_aggr_type (tree t,
12224 tree args,
12225 tsubst_flags_t complain,
12226 tree in_decl,
12227 int entering_scope)
12228 {
12229 if (t == NULL_TREE)
12230 return NULL_TREE;
12231
12232 switch (TREE_CODE (t))
12233 {
12234 case RECORD_TYPE:
12235 if (TYPE_PTRMEMFUNC_P (t))
12236 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12237
12238 /* Fall through. */
12239 case ENUMERAL_TYPE:
12240 case UNION_TYPE:
12241 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12242 {
12243 tree argvec;
12244 tree context;
12245 tree r;
12246 int saved_unevaluated_operand;
12247 int saved_inhibit_evaluation_warnings;
12248
12249 /* In "sizeof(X<I>)" we need to evaluate "I". */
12250 saved_unevaluated_operand = cp_unevaluated_operand;
12251 cp_unevaluated_operand = 0;
12252 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12253 c_inhibit_evaluation_warnings = 0;
12254
12255 /* First, determine the context for the type we are looking
12256 up. */
12257 context = TYPE_CONTEXT (t);
12258 if (context && TYPE_P (context))
12259 {
12260 context = tsubst_aggr_type (context, args, complain,
12261 in_decl, /*entering_scope=*/1);
12262 /* If context is a nested class inside a class template,
12263 it may still need to be instantiated (c++/33959). */
12264 context = complete_type (context);
12265 }
12266
12267 /* Then, figure out what arguments are appropriate for the
12268 type we are trying to find. For example, given:
12269
12270 template <class T> struct S;
12271 template <class T, class U> void f(T, U) { S<U> su; }
12272
12273 and supposing that we are instantiating f<int, double>,
12274 then our ARGS will be {int, double}, but, when looking up
12275 S we only want {double}. */
12276 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12277 complain, in_decl);
12278 if (argvec == error_mark_node)
12279 r = error_mark_node;
12280 else
12281 {
12282 r = lookup_template_class (t, argvec, in_decl, context,
12283 entering_scope, complain);
12284 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12285 }
12286
12287 cp_unevaluated_operand = saved_unevaluated_operand;
12288 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12289
12290 return r;
12291 }
12292 else
12293 /* This is not a template type, so there's nothing to do. */
12294 return t;
12295
12296 default:
12297 return tsubst (t, args, complain, in_decl);
12298 }
12299 }
12300
12301 static GTY((cache)) tree_cache_map *defarg_inst;
12302
12303 /* Substitute into the default argument ARG (a default argument for
12304 FN), which has the indicated TYPE. */
12305
12306 tree
12307 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12308 tsubst_flags_t complain)
12309 {
12310 tree saved_class_ptr = NULL_TREE;
12311 tree saved_class_ref = NULL_TREE;
12312 int errs = errorcount + sorrycount;
12313
12314 /* This can happen in invalid code. */
12315 if (TREE_CODE (arg) == DEFAULT_ARG)
12316 return arg;
12317
12318 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12319 parm = chain_index (parmnum, parm);
12320 tree parmtype = TREE_TYPE (parm);
12321 if (DECL_BY_REFERENCE (parm))
12322 parmtype = TREE_TYPE (parmtype);
12323 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12324
12325 tree *slot;
12326 if (defarg_inst && (slot = defarg_inst->get (parm)))
12327 return *slot;
12328
12329 /* This default argument came from a template. Instantiate the
12330 default argument here, not in tsubst. In the case of
12331 something like:
12332
12333 template <class T>
12334 struct S {
12335 static T t();
12336 void f(T = t());
12337 };
12338
12339 we must be careful to do name lookup in the scope of S<T>,
12340 rather than in the current class. */
12341 push_access_scope (fn);
12342 /* The "this" pointer is not valid in a default argument. */
12343 if (cfun)
12344 {
12345 saved_class_ptr = current_class_ptr;
12346 cp_function_chain->x_current_class_ptr = NULL_TREE;
12347 saved_class_ref = current_class_ref;
12348 cp_function_chain->x_current_class_ref = NULL_TREE;
12349 }
12350
12351 start_lambda_scope (parm);
12352
12353 push_deferring_access_checks(dk_no_deferred);
12354 /* The default argument expression may cause implicitly defined
12355 member functions to be synthesized, which will result in garbage
12356 collection. We must treat this situation as if we were within
12357 the body of function so as to avoid collecting live data on the
12358 stack. */
12359 ++function_depth;
12360 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12361 complain, NULL_TREE,
12362 /*integral_constant_expression_p=*/false);
12363 --function_depth;
12364 pop_deferring_access_checks();
12365
12366 finish_lambda_scope ();
12367
12368 /* Restore the "this" pointer. */
12369 if (cfun)
12370 {
12371 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12372 cp_function_chain->x_current_class_ref = saved_class_ref;
12373 }
12374
12375 if (errorcount+sorrycount > errs
12376 && (complain & tf_warning_or_error))
12377 inform (input_location,
12378 " when instantiating default argument for call to %qD", fn);
12379
12380 /* Make sure the default argument is reasonable. */
12381 arg = check_default_argument (type, arg, complain);
12382
12383 pop_access_scope (fn);
12384
12385 if (arg != error_mark_node && !cp_unevaluated_operand)
12386 {
12387 if (!defarg_inst)
12388 defarg_inst = tree_cache_map::create_ggc (37);
12389 defarg_inst->put (parm, arg);
12390 }
12391
12392 return arg;
12393 }
12394
12395 /* Substitute into all the default arguments for FN. */
12396
12397 static void
12398 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12399 {
12400 tree arg;
12401 tree tmpl_args;
12402
12403 tmpl_args = DECL_TI_ARGS (fn);
12404
12405 /* If this function is not yet instantiated, we certainly don't need
12406 its default arguments. */
12407 if (uses_template_parms (tmpl_args))
12408 return;
12409 /* Don't do this again for clones. */
12410 if (DECL_CLONED_FUNCTION_P (fn))
12411 return;
12412
12413 int i = 0;
12414 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12415 arg;
12416 arg = TREE_CHAIN (arg), ++i)
12417 if (TREE_PURPOSE (arg))
12418 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12419 TREE_VALUE (arg),
12420 TREE_PURPOSE (arg),
12421 complain);
12422 }
12423
12424 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12425
12426 static tree
12427 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12428 tree lambda_fntype)
12429 {
12430 tree gen_tmpl, argvec;
12431 hashval_t hash = 0;
12432 tree in_decl = t;
12433
12434 /* Nobody should be tsubst'ing into non-template functions. */
12435 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12436
12437 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12438 {
12439 /* If T is not dependent, just return it. */
12440 if (!uses_template_parms (DECL_TI_ARGS (t)))
12441 return t;
12442
12443 /* Calculate the most general template of which R is a
12444 specialization. */
12445 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12446
12447 /* We're substituting a lambda function under tsubst_lambda_expr but not
12448 directly from it; find the matching function we're already inside.
12449 But don't do this if T is a generic lambda with a single level of
12450 template parms, as in that case we're doing a normal instantiation. */
12451 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12452 && (!generic_lambda_fn_p (t)
12453 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12454 return enclosing_instantiation_of (t);
12455
12456 /* Calculate the complete set of arguments used to
12457 specialize R. */
12458 argvec = tsubst_template_args (DECL_TI_ARGS
12459 (DECL_TEMPLATE_RESULT
12460 (DECL_TI_TEMPLATE (t))),
12461 args, complain, in_decl);
12462 if (argvec == error_mark_node)
12463 return error_mark_node;
12464
12465 /* Check to see if we already have this specialization. */
12466 if (!lambda_fntype)
12467 {
12468 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12469 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12470 return spec;
12471 }
12472
12473 /* We can see more levels of arguments than parameters if
12474 there was a specialization of a member template, like
12475 this:
12476
12477 template <class T> struct S { template <class U> void f(); }
12478 template <> template <class U> void S<int>::f(U);
12479
12480 Here, we'll be substituting into the specialization,
12481 because that's where we can find the code we actually
12482 want to generate, but we'll have enough arguments for
12483 the most general template.
12484
12485 We also deal with the peculiar case:
12486
12487 template <class T> struct S {
12488 template <class U> friend void f();
12489 };
12490 template <class U> void f() {}
12491 template S<int>;
12492 template void f<double>();
12493
12494 Here, the ARGS for the instantiation of will be {int,
12495 double}. But, we only need as many ARGS as there are
12496 levels of template parameters in CODE_PATTERN. We are
12497 careful not to get fooled into reducing the ARGS in
12498 situations like:
12499
12500 template <class T> struct S { template <class U> void f(U); }
12501 template <class T> template <> void S<T>::f(int) {}
12502
12503 which we can spot because the pattern will be a
12504 specialization in this case. */
12505 int args_depth = TMPL_ARGS_DEPTH (args);
12506 int parms_depth =
12507 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12508
12509 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12510 args = get_innermost_template_args (args, parms_depth);
12511 }
12512 else
12513 {
12514 /* This special case arises when we have something like this:
12515
12516 template <class T> struct S {
12517 friend void f<int>(int, double);
12518 };
12519
12520 Here, the DECL_TI_TEMPLATE for the friend declaration
12521 will be an IDENTIFIER_NODE. We are being called from
12522 tsubst_friend_function, and we want only to create a
12523 new decl (R) with appropriate types so that we can call
12524 determine_specialization. */
12525 gen_tmpl = NULL_TREE;
12526 argvec = NULL_TREE;
12527 }
12528
12529 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12530 : NULL_TREE);
12531 tree ctx = closure ? closure : DECL_CONTEXT (t);
12532 bool member = ctx && TYPE_P (ctx);
12533
12534 if (member && !closure)
12535 ctx = tsubst_aggr_type (ctx, args,
12536 complain, t, /*entering_scope=*/1);
12537
12538 tree type = (lambda_fntype ? lambda_fntype
12539 : tsubst (TREE_TYPE (t), args,
12540 complain | tf_fndecl_type, in_decl));
12541 if (type == error_mark_node)
12542 return error_mark_node;
12543
12544 /* If we hit excessive deduction depth, the type is bogus even if
12545 it isn't error_mark_node, so don't build a decl. */
12546 if (excessive_deduction_depth)
12547 return error_mark_node;
12548
12549 /* We do NOT check for matching decls pushed separately at this
12550 point, as they may not represent instantiations of this
12551 template, and in any case are considered separate under the
12552 discrete model. */
12553 tree r = copy_decl (t);
12554 DECL_USE_TEMPLATE (r) = 0;
12555 TREE_TYPE (r) = type;
12556 /* Clear out the mangled name and RTL for the instantiation. */
12557 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12558 SET_DECL_RTL (r, NULL);
12559 /* Leave DECL_INITIAL set on deleted instantiations. */
12560 if (!DECL_DELETED_FN (r))
12561 DECL_INITIAL (r) = NULL_TREE;
12562 DECL_CONTEXT (r) = ctx;
12563
12564 /* OpenMP UDRs have the only argument a reference to the declared
12565 type. We want to diagnose if the declared type is a reference,
12566 which is invalid, but as references to references are usually
12567 quietly merged, diagnose it here. */
12568 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12569 {
12570 tree argtype
12571 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12572 argtype = tsubst (argtype, args, complain, in_decl);
12573 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12574 error_at (DECL_SOURCE_LOCATION (t),
12575 "reference type %qT in "
12576 "%<#pragma omp declare reduction%>", argtype);
12577 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12578 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12579 argtype);
12580 }
12581
12582 if (member && DECL_CONV_FN_P (r))
12583 /* Type-conversion operator. Reconstruct the name, in
12584 case it's the name of one of the template's parameters. */
12585 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12586
12587 tree parms = DECL_ARGUMENTS (t);
12588 if (closure)
12589 parms = DECL_CHAIN (parms);
12590 parms = tsubst (parms, args, complain, t);
12591 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12592 DECL_CONTEXT (parm) = r;
12593 if (closure)
12594 {
12595 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12596 DECL_CHAIN (tparm) = parms;
12597 parms = tparm;
12598 }
12599 DECL_ARGUMENTS (r) = parms;
12600 DECL_RESULT (r) = NULL_TREE;
12601
12602 TREE_STATIC (r) = 0;
12603 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12604 DECL_EXTERNAL (r) = 1;
12605 /* If this is an instantiation of a function with internal
12606 linkage, we already know what object file linkage will be
12607 assigned to the instantiation. */
12608 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12609 DECL_DEFER_OUTPUT (r) = 0;
12610 DECL_CHAIN (r) = NULL_TREE;
12611 DECL_PENDING_INLINE_INFO (r) = 0;
12612 DECL_PENDING_INLINE_P (r) = 0;
12613 DECL_SAVED_TREE (r) = NULL_TREE;
12614 DECL_STRUCT_FUNCTION (r) = NULL;
12615 TREE_USED (r) = 0;
12616 /* We'll re-clone as appropriate in instantiate_template. */
12617 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12618
12619 /* If we aren't complaining now, return on error before we register
12620 the specialization so that we'll complain eventually. */
12621 if ((complain & tf_error) == 0
12622 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12623 && !grok_op_properties (r, /*complain=*/false))
12624 return error_mark_node;
12625
12626 /* When instantiating a constrained member, substitute
12627 into the constraints to create a new constraint. */
12628 if (tree ci = get_constraints (t))
12629 if (member)
12630 {
12631 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12632 set_constraints (r, ci);
12633 }
12634
12635 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12636 this in the special friend case mentioned above where
12637 GEN_TMPL is NULL. */
12638 if (gen_tmpl && !closure)
12639 {
12640 DECL_TEMPLATE_INFO (r)
12641 = build_template_info (gen_tmpl, argvec);
12642 SET_DECL_IMPLICIT_INSTANTIATION (r);
12643
12644 tree new_r
12645 = register_specialization (r, gen_tmpl, argvec, false, hash);
12646 if (new_r != r)
12647 /* We instantiated this while substituting into
12648 the type earlier (template/friend54.C). */
12649 return new_r;
12650
12651 /* We're not supposed to instantiate default arguments
12652 until they are called, for a template. But, for a
12653 declaration like:
12654
12655 template <class T> void f ()
12656 { extern void g(int i = T()); }
12657
12658 we should do the substitution when the template is
12659 instantiated. We handle the member function case in
12660 instantiate_class_template since the default arguments
12661 might refer to other members of the class. */
12662 if (!member
12663 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12664 && !uses_template_parms (argvec))
12665 tsubst_default_arguments (r, complain);
12666 }
12667 else
12668 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12669
12670 /* Copy the list of befriending classes. */
12671 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12672 *friends;
12673 friends = &TREE_CHAIN (*friends))
12674 {
12675 *friends = copy_node (*friends);
12676 TREE_VALUE (*friends)
12677 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12678 }
12679
12680 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12681 {
12682 maybe_retrofit_in_chrg (r);
12683 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12684 return error_mark_node;
12685 /* If this is an instantiation of a member template, clone it.
12686 If it isn't, that'll be handled by
12687 clone_constructors_and_destructors. */
12688 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12689 clone_function_decl (r, /*update_methods=*/false);
12690 }
12691 else if ((complain & tf_error) != 0
12692 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12693 && !grok_op_properties (r, /*complain=*/true))
12694 return error_mark_node;
12695
12696 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12697 SET_DECL_FRIEND_CONTEXT (r,
12698 tsubst (DECL_FRIEND_CONTEXT (t),
12699 args, complain, in_decl));
12700
12701 /* Possibly limit visibility based on template args. */
12702 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12703 if (DECL_VISIBILITY_SPECIFIED (t))
12704 {
12705 DECL_VISIBILITY_SPECIFIED (r) = 0;
12706 DECL_ATTRIBUTES (r)
12707 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12708 }
12709 determine_visibility (r);
12710 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12711 && !processing_template_decl)
12712 defaulted_late_check (r);
12713
12714 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12715 args, complain, in_decl);
12716 return r;
12717 }
12718
12719 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12720
12721 static tree
12722 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12723 tree lambda_fntype)
12724 {
12725 /* We can get here when processing a member function template,
12726 member class template, or template template parameter. */
12727 tree decl = DECL_TEMPLATE_RESULT (t);
12728 tree in_decl = t;
12729 tree spec;
12730 tree tmpl_args;
12731 tree full_args;
12732 tree r;
12733 hashval_t hash = 0;
12734
12735 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12736 {
12737 /* Template template parameter is treated here. */
12738 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12739 if (new_type == error_mark_node)
12740 r = error_mark_node;
12741 /* If we get a real template back, return it. This can happen in
12742 the context of most_specialized_partial_spec. */
12743 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12744 r = new_type;
12745 else
12746 /* The new TEMPLATE_DECL was built in
12747 reduce_template_parm_level. */
12748 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12749 return r;
12750 }
12751
12752 if (!lambda_fntype)
12753 {
12754 /* We might already have an instance of this template.
12755 The ARGS are for the surrounding class type, so the
12756 full args contain the tsubst'd args for the context,
12757 plus the innermost args from the template decl. */
12758 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12759 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12760 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12761 /* Because this is a template, the arguments will still be
12762 dependent, even after substitution. If
12763 PROCESSING_TEMPLATE_DECL is not set, the dependency
12764 predicates will short-circuit. */
12765 ++processing_template_decl;
12766 full_args = tsubst_template_args (tmpl_args, args,
12767 complain, in_decl);
12768 --processing_template_decl;
12769 if (full_args == error_mark_node)
12770 return error_mark_node;
12771
12772 /* If this is a default template template argument,
12773 tsubst might not have changed anything. */
12774 if (full_args == tmpl_args)
12775 return t;
12776
12777 hash = hash_tmpl_and_args (t, full_args);
12778 spec = retrieve_specialization (t, full_args, hash);
12779 if (spec != NULL_TREE)
12780 return spec;
12781 }
12782
12783 /* Make a new template decl. It will be similar to the
12784 original, but will record the current template arguments.
12785 We also create a new function declaration, which is just
12786 like the old one, but points to this new template, rather
12787 than the old one. */
12788 r = copy_decl (t);
12789 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12790 DECL_CHAIN (r) = NULL_TREE;
12791
12792 // Build new template info linking to the original template decl.
12793 if (!lambda_fntype)
12794 {
12795 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12796 SET_DECL_IMPLICIT_INSTANTIATION (r);
12797 }
12798 else
12799 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12800
12801 /* The template parameters for this new template are all the
12802 template parameters for the old template, except the
12803 outermost level of parameters. */
12804 DECL_TEMPLATE_PARMS (r)
12805 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12806 complain);
12807
12808 if (TREE_CODE (decl) == TYPE_DECL
12809 && !TYPE_DECL_ALIAS_P (decl))
12810 {
12811 tree new_type;
12812 ++processing_template_decl;
12813 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12814 --processing_template_decl;
12815 if (new_type == error_mark_node)
12816 return error_mark_node;
12817
12818 TREE_TYPE (r) = new_type;
12819 /* For a partial specialization, we need to keep pointing to
12820 the primary template. */
12821 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12822 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12823 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12824 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12825 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12826 }
12827 else
12828 {
12829 tree new_decl;
12830 ++processing_template_decl;
12831 if (TREE_CODE (decl) == FUNCTION_DECL)
12832 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12833 else
12834 new_decl = tsubst (decl, args, complain, in_decl);
12835 --processing_template_decl;
12836 if (new_decl == error_mark_node)
12837 return error_mark_node;
12838
12839 DECL_TEMPLATE_RESULT (r) = new_decl;
12840 TREE_TYPE (r) = TREE_TYPE (new_decl);
12841 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12842 if (lambda_fntype)
12843 {
12844 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12845 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12846 }
12847 else
12848 {
12849 DECL_TI_TEMPLATE (new_decl) = r;
12850 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12851 }
12852 }
12853
12854 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12855 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12856
12857 if (PRIMARY_TEMPLATE_P (t))
12858 DECL_PRIMARY_TEMPLATE (r) = r;
12859
12860 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12861 && !lambda_fntype)
12862 /* Record this non-type partial instantiation. */
12863 register_specialization (r, t,
12864 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12865 false, hash);
12866
12867 return r;
12868 }
12869
12870 /* True if FN is the op() for a lambda in an uninstantiated template. */
12871
12872 bool
12873 lambda_fn_in_template_p (tree fn)
12874 {
12875 if (!fn || !LAMBDA_FUNCTION_P (fn))
12876 return false;
12877 tree closure = DECL_CONTEXT (fn);
12878 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12879 }
12880
12881 /* We're instantiating a variable from template function TCTX. Return the
12882 corresponding current enclosing scope. This gets complicated because lambda
12883 functions in templates are regenerated rather than instantiated, but generic
12884 lambda functions are subsequently instantiated. */
12885
12886 static tree
12887 enclosing_instantiation_of (tree otctx)
12888 {
12889 tree tctx = otctx;
12890 tree fn = current_function_decl;
12891 int lambda_count = 0;
12892
12893 for (; tctx && lambda_fn_in_template_p (tctx);
12894 tctx = decl_function_context (tctx))
12895 ++lambda_count;
12896 for (; fn; fn = decl_function_context (fn))
12897 {
12898 tree ofn = fn;
12899 int flambda_count = 0;
12900 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12901 fn = decl_function_context (fn))
12902 ++flambda_count;
12903 if (DECL_TEMPLATE_INFO (fn)
12904 ? most_general_template (fn) != most_general_template (tctx)
12905 : fn != tctx)
12906 continue;
12907 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12908 || DECL_CONV_FN_P (ofn));
12909 return ofn;
12910 }
12911 gcc_unreachable ();
12912 }
12913
12914 /* Substitute the ARGS into the T, which is a _DECL. Return the
12915 result of the substitution. Issue error and warning messages under
12916 control of COMPLAIN. */
12917
12918 static tree
12919 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12920 {
12921 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12922 location_t saved_loc;
12923 tree r = NULL_TREE;
12924 tree in_decl = t;
12925 hashval_t hash = 0;
12926
12927 /* Set the filename and linenumber to improve error-reporting. */
12928 saved_loc = input_location;
12929 input_location = DECL_SOURCE_LOCATION (t);
12930
12931 switch (TREE_CODE (t))
12932 {
12933 case TEMPLATE_DECL:
12934 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12935 break;
12936
12937 case FUNCTION_DECL:
12938 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12939 break;
12940
12941 case PARM_DECL:
12942 {
12943 tree type = NULL_TREE;
12944 int i, len = 1;
12945 tree expanded_types = NULL_TREE;
12946 tree prev_r = NULL_TREE;
12947 tree first_r = NULL_TREE;
12948
12949 if (DECL_PACK_P (t))
12950 {
12951 /* If there is a local specialization that isn't a
12952 parameter pack, it means that we're doing a "simple"
12953 substitution from inside tsubst_pack_expansion. Just
12954 return the local specialization (which will be a single
12955 parm). */
12956 tree spec = retrieve_local_specialization (t);
12957 if (spec
12958 && TREE_CODE (spec) == PARM_DECL
12959 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12960 RETURN (spec);
12961
12962 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12963 the parameters in this function parameter pack. */
12964 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12965 complain, in_decl);
12966 if (TREE_CODE (expanded_types) == TREE_VEC)
12967 {
12968 len = TREE_VEC_LENGTH (expanded_types);
12969
12970 /* Zero-length parameter packs are boring. Just substitute
12971 into the chain. */
12972 if (len == 0)
12973 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12974 TREE_CHAIN (t)));
12975 }
12976 else
12977 {
12978 /* All we did was update the type. Make a note of that. */
12979 type = expanded_types;
12980 expanded_types = NULL_TREE;
12981 }
12982 }
12983
12984 /* Loop through all of the parameters we'll build. When T is
12985 a function parameter pack, LEN is the number of expanded
12986 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12987 r = NULL_TREE;
12988 for (i = 0; i < len; ++i)
12989 {
12990 prev_r = r;
12991 r = copy_node (t);
12992 if (DECL_TEMPLATE_PARM_P (t))
12993 SET_DECL_TEMPLATE_PARM_P (r);
12994
12995 if (expanded_types)
12996 /* We're on the Ith parameter of the function parameter
12997 pack. */
12998 {
12999 /* Get the Ith type. */
13000 type = TREE_VEC_ELT (expanded_types, i);
13001
13002 /* Rename the parameter to include the index. */
13003 DECL_NAME (r)
13004 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13005 }
13006 else if (!type)
13007 /* We're dealing with a normal parameter. */
13008 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13009
13010 type = type_decays_to (type);
13011 TREE_TYPE (r) = type;
13012 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13013
13014 if (DECL_INITIAL (r))
13015 {
13016 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13017 DECL_INITIAL (r) = TREE_TYPE (r);
13018 else
13019 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13020 complain, in_decl);
13021 }
13022
13023 DECL_CONTEXT (r) = NULL_TREE;
13024
13025 if (!DECL_TEMPLATE_PARM_P (r))
13026 DECL_ARG_TYPE (r) = type_passed_as (type);
13027
13028 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13029 args, complain, in_decl);
13030
13031 /* Keep track of the first new parameter we
13032 generate. That's what will be returned to the
13033 caller. */
13034 if (!first_r)
13035 first_r = r;
13036
13037 /* Build a proper chain of parameters when substituting
13038 into a function parameter pack. */
13039 if (prev_r)
13040 DECL_CHAIN (prev_r) = r;
13041 }
13042
13043 /* If cp_unevaluated_operand is set, we're just looking for a
13044 single dummy parameter, so don't keep going. */
13045 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13046 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13047 complain, DECL_CHAIN (t));
13048
13049 /* FIRST_R contains the start of the chain we've built. */
13050 r = first_r;
13051 }
13052 break;
13053
13054 case FIELD_DECL:
13055 {
13056 tree type = NULL_TREE;
13057 tree vec = NULL_TREE;
13058 tree expanded_types = NULL_TREE;
13059 int len = 1;
13060
13061 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13062 {
13063 /* This field is a lambda capture pack. Return a TREE_VEC of
13064 the expanded fields to instantiate_class_template_1. */
13065 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13066 complain, in_decl);
13067 if (TREE_CODE (expanded_types) == TREE_VEC)
13068 {
13069 len = TREE_VEC_LENGTH (expanded_types);
13070 vec = make_tree_vec (len);
13071 }
13072 else
13073 {
13074 /* All we did was update the type. Make a note of that. */
13075 type = expanded_types;
13076 expanded_types = NULL_TREE;
13077 }
13078 }
13079
13080 for (int i = 0; i < len; ++i)
13081 {
13082 r = copy_decl (t);
13083 if (expanded_types)
13084 {
13085 type = TREE_VEC_ELT (expanded_types, i);
13086 DECL_NAME (r)
13087 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13088 }
13089 else if (!type)
13090 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13091
13092 if (type == error_mark_node)
13093 RETURN (error_mark_node);
13094 TREE_TYPE (r) = type;
13095 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13096
13097 if (DECL_C_BIT_FIELD (r))
13098 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13099 number of bits. */
13100 DECL_BIT_FIELD_REPRESENTATIVE (r)
13101 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13102 complain, in_decl,
13103 /*integral_constant_expression_p=*/true);
13104 if (DECL_INITIAL (t))
13105 {
13106 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13107 NSDMI in perform_member_init. Still set DECL_INITIAL
13108 so that we know there is one. */
13109 DECL_INITIAL (r) = void_node;
13110 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13111 retrofit_lang_decl (r);
13112 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13113 }
13114 /* We don't have to set DECL_CONTEXT here; it is set by
13115 finish_member_declaration. */
13116 DECL_CHAIN (r) = NULL_TREE;
13117
13118 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13119 args, complain, in_decl);
13120
13121 if (vec)
13122 TREE_VEC_ELT (vec, i) = r;
13123 }
13124
13125 if (vec)
13126 r = vec;
13127 }
13128 break;
13129
13130 case USING_DECL:
13131 /* We reach here only for member using decls. We also need to check
13132 uses_template_parms because DECL_DEPENDENT_P is not set for a
13133 using-declaration that designates a member of the current
13134 instantiation (c++/53549). */
13135 if (DECL_DEPENDENT_P (t)
13136 || uses_template_parms (USING_DECL_SCOPE (t)))
13137 {
13138 tree scope = USING_DECL_SCOPE (t);
13139 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13140 if (PACK_EXPANSION_P (scope))
13141 {
13142 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13143 int len = TREE_VEC_LENGTH (vec);
13144 r = make_tree_vec (len);
13145 for (int i = 0; i < len; ++i)
13146 {
13147 tree escope = TREE_VEC_ELT (vec, i);
13148 tree elt = do_class_using_decl (escope, name);
13149 if (!elt)
13150 {
13151 r = error_mark_node;
13152 break;
13153 }
13154 else
13155 {
13156 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13157 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13158 }
13159 TREE_VEC_ELT (r, i) = elt;
13160 }
13161 }
13162 else
13163 {
13164 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13165 complain, in_decl);
13166 r = do_class_using_decl (inst_scope, name);
13167 if (!r)
13168 r = error_mark_node;
13169 else
13170 {
13171 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13172 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13173 }
13174 }
13175 }
13176 else
13177 {
13178 r = copy_node (t);
13179 DECL_CHAIN (r) = NULL_TREE;
13180 }
13181 break;
13182
13183 case TYPE_DECL:
13184 case VAR_DECL:
13185 {
13186 tree argvec = NULL_TREE;
13187 tree gen_tmpl = NULL_TREE;
13188 tree spec;
13189 tree tmpl = NULL_TREE;
13190 tree ctx;
13191 tree type = NULL_TREE;
13192 bool local_p;
13193
13194 if (TREE_TYPE (t) == error_mark_node)
13195 RETURN (error_mark_node);
13196
13197 if (TREE_CODE (t) == TYPE_DECL
13198 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13199 {
13200 /* If this is the canonical decl, we don't have to
13201 mess with instantiations, and often we can't (for
13202 typename, template type parms and such). Note that
13203 TYPE_NAME is not correct for the above test if
13204 we've copied the type for a typedef. */
13205 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13206 if (type == error_mark_node)
13207 RETURN (error_mark_node);
13208 r = TYPE_NAME (type);
13209 break;
13210 }
13211
13212 /* Check to see if we already have the specialization we
13213 need. */
13214 spec = NULL_TREE;
13215 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13216 {
13217 /* T is a static data member or namespace-scope entity.
13218 We have to substitute into namespace-scope variables
13219 (not just variable templates) because of cases like:
13220
13221 template <class T> void f() { extern T t; }
13222
13223 where the entity referenced is not known until
13224 instantiation time. */
13225 local_p = false;
13226 ctx = DECL_CONTEXT (t);
13227 if (DECL_CLASS_SCOPE_P (t))
13228 {
13229 ctx = tsubst_aggr_type (ctx, args,
13230 complain,
13231 in_decl, /*entering_scope=*/1);
13232 /* If CTX is unchanged, then T is in fact the
13233 specialization we want. That situation occurs when
13234 referencing a static data member within in its own
13235 class. We can use pointer equality, rather than
13236 same_type_p, because DECL_CONTEXT is always
13237 canonical... */
13238 if (ctx == DECL_CONTEXT (t)
13239 /* ... unless T is a member template; in which
13240 case our caller can be willing to create a
13241 specialization of that template represented
13242 by T. */
13243 && !(DECL_TI_TEMPLATE (t)
13244 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13245 spec = t;
13246 }
13247
13248 if (!spec)
13249 {
13250 tmpl = DECL_TI_TEMPLATE (t);
13251 gen_tmpl = most_general_template (tmpl);
13252 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13253 if (argvec != error_mark_node)
13254 argvec = (coerce_innermost_template_parms
13255 (DECL_TEMPLATE_PARMS (gen_tmpl),
13256 argvec, t, complain,
13257 /*all*/true, /*defarg*/true));
13258 if (argvec == error_mark_node)
13259 RETURN (error_mark_node);
13260 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13261 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13262 }
13263 }
13264 else
13265 {
13266 /* A local variable. */
13267 local_p = true;
13268 /* Subsequent calls to pushdecl will fill this in. */
13269 ctx = NULL_TREE;
13270 /* Unless this is a reference to a static variable from an
13271 enclosing function, in which case we need to fill it in now. */
13272 if (TREE_STATIC (t))
13273 {
13274 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13275 if (fn != current_function_decl)
13276 ctx = fn;
13277 }
13278 spec = retrieve_local_specialization (t);
13279 }
13280 /* If we already have the specialization we need, there is
13281 nothing more to do. */
13282 if (spec)
13283 {
13284 r = spec;
13285 break;
13286 }
13287
13288 /* Create a new node for the specialization we need. */
13289 r = copy_decl (t);
13290 if (type == NULL_TREE)
13291 {
13292 if (is_typedef_decl (t))
13293 type = DECL_ORIGINAL_TYPE (t);
13294 else
13295 type = TREE_TYPE (t);
13296 if (VAR_P (t)
13297 && VAR_HAD_UNKNOWN_BOUND (t)
13298 && type != error_mark_node)
13299 type = strip_array_domain (type);
13300 tree sub_args = args;
13301 if (tree auto_node = type_uses_auto (type))
13302 {
13303 /* Mask off any template args past the variable's context so we
13304 don't replace the auto with an unrelated argument. */
13305 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13306 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13307 if (extra > 0)
13308 /* This should never happen with the new lambda instantiation
13309 model, but keep the handling just in case. */
13310 gcc_assert (!CHECKING_P),
13311 sub_args = strip_innermost_template_args (args, extra);
13312 }
13313 type = tsubst (type, sub_args, complain, in_decl);
13314 }
13315 if (VAR_P (r))
13316 {
13317 /* Even if the original location is out of scope, the
13318 newly substituted one is not. */
13319 DECL_DEAD_FOR_LOCAL (r) = 0;
13320 DECL_INITIALIZED_P (r) = 0;
13321 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13322 if (type == error_mark_node)
13323 RETURN (error_mark_node);
13324 if (TREE_CODE (type) == FUNCTION_TYPE)
13325 {
13326 /* It may seem that this case cannot occur, since:
13327
13328 typedef void f();
13329 void g() { f x; }
13330
13331 declares a function, not a variable. However:
13332
13333 typedef void f();
13334 template <typename T> void g() { T t; }
13335 template void g<f>();
13336
13337 is an attempt to declare a variable with function
13338 type. */
13339 error ("variable %qD has function type",
13340 /* R is not yet sufficiently initialized, so we
13341 just use its name. */
13342 DECL_NAME (r));
13343 RETURN (error_mark_node);
13344 }
13345 type = complete_type (type);
13346 /* Wait until cp_finish_decl to set this again, to handle
13347 circular dependency (template/instantiate6.C). */
13348 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13349 type = check_var_type (DECL_NAME (r), type);
13350
13351 if (DECL_HAS_VALUE_EXPR_P (t))
13352 {
13353 tree ve = DECL_VALUE_EXPR (t);
13354 ve = tsubst_expr (ve, args, complain, in_decl,
13355 /*constant_expression_p=*/false);
13356 if (REFERENCE_REF_P (ve))
13357 {
13358 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13359 ve = TREE_OPERAND (ve, 0);
13360 }
13361 SET_DECL_VALUE_EXPR (r, ve);
13362 }
13363 if (CP_DECL_THREAD_LOCAL_P (r)
13364 && !processing_template_decl)
13365 set_decl_tls_model (r, decl_default_tls_model (r));
13366 }
13367 else if (DECL_SELF_REFERENCE_P (t))
13368 SET_DECL_SELF_REFERENCE_P (r);
13369 TREE_TYPE (r) = type;
13370 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13371 DECL_CONTEXT (r) = ctx;
13372 /* Clear out the mangled name and RTL for the instantiation. */
13373 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13374 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13375 SET_DECL_RTL (r, NULL);
13376 /* The initializer must not be expanded until it is required;
13377 see [temp.inst]. */
13378 DECL_INITIAL (r) = NULL_TREE;
13379 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13380 if (VAR_P (r))
13381 {
13382 if (DECL_LANG_SPECIFIC (r))
13383 SET_DECL_DEPENDENT_INIT_P (r, false);
13384
13385 SET_DECL_MODE (r, VOIDmode);
13386
13387 /* Possibly limit visibility based on template args. */
13388 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13389 if (DECL_VISIBILITY_SPECIFIED (t))
13390 {
13391 DECL_VISIBILITY_SPECIFIED (r) = 0;
13392 DECL_ATTRIBUTES (r)
13393 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13394 }
13395 determine_visibility (r);
13396 }
13397
13398 if (!local_p)
13399 {
13400 /* A static data member declaration is always marked
13401 external when it is declared in-class, even if an
13402 initializer is present. We mimic the non-template
13403 processing here. */
13404 DECL_EXTERNAL (r) = 1;
13405 if (DECL_NAMESPACE_SCOPE_P (t))
13406 DECL_NOT_REALLY_EXTERN (r) = 1;
13407
13408 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13409 SET_DECL_IMPLICIT_INSTANTIATION (r);
13410 register_specialization (r, gen_tmpl, argvec, false, hash);
13411 }
13412 else
13413 {
13414 if (DECL_LANG_SPECIFIC (r))
13415 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13416 if (!cp_unevaluated_operand)
13417 register_local_specialization (r, t);
13418 }
13419
13420 DECL_CHAIN (r) = NULL_TREE;
13421
13422 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13423 /*flags=*/0,
13424 args, complain, in_decl);
13425
13426 /* Preserve a typedef that names a type. */
13427 if (is_typedef_decl (r) && type != error_mark_node)
13428 {
13429 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13430 set_underlying_type (r);
13431 if (TYPE_DECL_ALIAS_P (r))
13432 /* An alias template specialization can be dependent
13433 even if its underlying type is not. */
13434 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13435 }
13436
13437 layout_decl (r, 0);
13438 }
13439 break;
13440
13441 default:
13442 gcc_unreachable ();
13443 }
13444 #undef RETURN
13445
13446 out:
13447 /* Restore the file and line information. */
13448 input_location = saved_loc;
13449
13450 return r;
13451 }
13452
13453 /* Substitute into the ARG_TYPES of a function type.
13454 If END is a TREE_CHAIN, leave it and any following types
13455 un-substituted. */
13456
13457 static tree
13458 tsubst_arg_types (tree arg_types,
13459 tree args,
13460 tree end,
13461 tsubst_flags_t complain,
13462 tree in_decl)
13463 {
13464 tree remaining_arg_types;
13465 tree type = NULL_TREE;
13466 int i = 1;
13467 tree expanded_args = NULL_TREE;
13468 tree default_arg;
13469
13470 if (!arg_types || arg_types == void_list_node || arg_types == end)
13471 return arg_types;
13472
13473 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13474 args, end, complain, in_decl);
13475 if (remaining_arg_types == error_mark_node)
13476 return error_mark_node;
13477
13478 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13479 {
13480 /* For a pack expansion, perform substitution on the
13481 entire expression. Later on, we'll handle the arguments
13482 one-by-one. */
13483 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13484 args, complain, in_decl);
13485
13486 if (TREE_CODE (expanded_args) == TREE_VEC)
13487 /* So that we'll spin through the parameters, one by one. */
13488 i = TREE_VEC_LENGTH (expanded_args);
13489 else
13490 {
13491 /* We only partially substituted into the parameter
13492 pack. Our type is TYPE_PACK_EXPANSION. */
13493 type = expanded_args;
13494 expanded_args = NULL_TREE;
13495 }
13496 }
13497
13498 while (i > 0) {
13499 --i;
13500
13501 if (expanded_args)
13502 type = TREE_VEC_ELT (expanded_args, i);
13503 else if (!type)
13504 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13505
13506 if (type == error_mark_node)
13507 return error_mark_node;
13508 if (VOID_TYPE_P (type))
13509 {
13510 if (complain & tf_error)
13511 {
13512 error ("invalid parameter type %qT", type);
13513 if (in_decl)
13514 error ("in declaration %q+D", in_decl);
13515 }
13516 return error_mark_node;
13517 }
13518 /* DR 657. */
13519 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13520 return error_mark_node;
13521
13522 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13523 top-level qualifiers as required. */
13524 type = cv_unqualified (type_decays_to (type));
13525
13526 /* We do not substitute into default arguments here. The standard
13527 mandates that they be instantiated only when needed, which is
13528 done in build_over_call. */
13529 default_arg = TREE_PURPOSE (arg_types);
13530
13531 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13532 since the new op() won't have any associated template arguments for us
13533 to refer to later. */
13534 if (lambda_fn_in_template_p (in_decl))
13535 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13536 false/*fn*/, false/*constexpr*/);
13537
13538 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13539 {
13540 /* We've instantiated a template before its default arguments
13541 have been parsed. This can happen for a nested template
13542 class, and is not an error unless we require the default
13543 argument in a call of this function. */
13544 remaining_arg_types =
13545 tree_cons (default_arg, type, remaining_arg_types);
13546 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13547 }
13548 else
13549 remaining_arg_types =
13550 hash_tree_cons (default_arg, type, remaining_arg_types);
13551 }
13552
13553 return remaining_arg_types;
13554 }
13555
13556 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13557 *not* handle the exception-specification for FNTYPE, because the
13558 initial substitution of explicitly provided template parameters
13559 during argument deduction forbids substitution into the
13560 exception-specification:
13561
13562 [temp.deduct]
13563
13564 All references in the function type of the function template to the
13565 corresponding template parameters are replaced by the specified tem-
13566 plate argument values. If a substitution in a template parameter or
13567 in the function type of the function template results in an invalid
13568 type, type deduction fails. [Note: The equivalent substitution in
13569 exception specifications is done only when the function is instanti-
13570 ated, at which point a program is ill-formed if the substitution
13571 results in an invalid type.] */
13572
13573 static tree
13574 tsubst_function_type (tree t,
13575 tree args,
13576 tsubst_flags_t complain,
13577 tree in_decl)
13578 {
13579 tree return_type;
13580 tree arg_types = NULL_TREE;
13581 tree fntype;
13582
13583 /* The TYPE_CONTEXT is not used for function/method types. */
13584 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13585
13586 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13587 failure. */
13588 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13589
13590 if (late_return_type_p)
13591 {
13592 /* Substitute the argument types. */
13593 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13594 complain, in_decl);
13595 if (arg_types == error_mark_node)
13596 return error_mark_node;
13597
13598 tree save_ccp = current_class_ptr;
13599 tree save_ccr = current_class_ref;
13600 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13601 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13602 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13603 if (do_inject)
13604 {
13605 /* DR 1207: 'this' is in scope in the trailing return type. */
13606 inject_this_parameter (this_type, cp_type_quals (this_type));
13607 }
13608
13609 /* Substitute the return type. */
13610 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13611
13612 if (do_inject)
13613 {
13614 current_class_ptr = save_ccp;
13615 current_class_ref = save_ccr;
13616 }
13617 }
13618 else
13619 /* Substitute the return type. */
13620 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13621
13622 if (return_type == error_mark_node)
13623 return error_mark_node;
13624 /* DR 486 clarifies that creation of a function type with an
13625 invalid return type is a deduction failure. */
13626 if (TREE_CODE (return_type) == ARRAY_TYPE
13627 || TREE_CODE (return_type) == FUNCTION_TYPE)
13628 {
13629 if (complain & tf_error)
13630 {
13631 if (TREE_CODE (return_type) == ARRAY_TYPE)
13632 error ("function returning an array");
13633 else
13634 error ("function returning a function");
13635 }
13636 return error_mark_node;
13637 }
13638 /* And DR 657. */
13639 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13640 return error_mark_node;
13641
13642 if (!late_return_type_p)
13643 {
13644 /* Substitute the argument types. */
13645 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13646 complain, in_decl);
13647 if (arg_types == error_mark_node)
13648 return error_mark_node;
13649 }
13650
13651 /* Construct a new type node and return it. */
13652 if (TREE_CODE (t) == FUNCTION_TYPE)
13653 {
13654 fntype = build_function_type (return_type, arg_types);
13655 fntype = apply_memfn_quals (fntype,
13656 type_memfn_quals (t),
13657 type_memfn_rqual (t));
13658 }
13659 else
13660 {
13661 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13662 /* Don't pick up extra function qualifiers from the basetype. */
13663 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13664 if (! MAYBE_CLASS_TYPE_P (r))
13665 {
13666 /* [temp.deduct]
13667
13668 Type deduction may fail for any of the following
13669 reasons:
13670
13671 -- Attempting to create "pointer to member of T" when T
13672 is not a class type. */
13673 if (complain & tf_error)
13674 error ("creating pointer to member function of non-class type %qT",
13675 r);
13676 return error_mark_node;
13677 }
13678
13679 fntype = build_method_type_directly (r, return_type,
13680 TREE_CHAIN (arg_types));
13681 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13682 }
13683 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13684
13685 if (late_return_type_p)
13686 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13687
13688 return fntype;
13689 }
13690
13691 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13692 ARGS into that specification, and return the substituted
13693 specification. If there is no specification, return NULL_TREE. */
13694
13695 static tree
13696 tsubst_exception_specification (tree fntype,
13697 tree args,
13698 tsubst_flags_t complain,
13699 tree in_decl,
13700 bool defer_ok)
13701 {
13702 tree specs;
13703 tree new_specs;
13704
13705 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13706 new_specs = NULL_TREE;
13707 if (specs && TREE_PURPOSE (specs))
13708 {
13709 /* A noexcept-specifier. */
13710 tree expr = TREE_PURPOSE (specs);
13711 if (TREE_CODE (expr) == INTEGER_CST)
13712 new_specs = expr;
13713 else if (defer_ok)
13714 {
13715 /* Defer instantiation of noexcept-specifiers to avoid
13716 excessive instantiations (c++/49107). */
13717 new_specs = make_node (DEFERRED_NOEXCEPT);
13718 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13719 {
13720 /* We already partially instantiated this member template,
13721 so combine the new args with the old. */
13722 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13723 = DEFERRED_NOEXCEPT_PATTERN (expr);
13724 DEFERRED_NOEXCEPT_ARGS (new_specs)
13725 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13726 }
13727 else
13728 {
13729 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13730 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13731 }
13732 }
13733 else
13734 new_specs = tsubst_copy_and_build
13735 (expr, args, complain, in_decl, /*function_p=*/false,
13736 /*integral_constant_expression_p=*/true);
13737 new_specs = build_noexcept_spec (new_specs, complain);
13738 }
13739 else if (specs)
13740 {
13741 if (! TREE_VALUE (specs))
13742 new_specs = specs;
13743 else
13744 while (specs)
13745 {
13746 tree spec;
13747 int i, len = 1;
13748 tree expanded_specs = NULL_TREE;
13749
13750 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13751 {
13752 /* Expand the pack expansion type. */
13753 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13754 args, complain,
13755 in_decl);
13756
13757 if (expanded_specs == error_mark_node)
13758 return error_mark_node;
13759 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13760 len = TREE_VEC_LENGTH (expanded_specs);
13761 else
13762 {
13763 /* We're substituting into a member template, so
13764 we got a TYPE_PACK_EXPANSION back. Add that
13765 expansion and move on. */
13766 gcc_assert (TREE_CODE (expanded_specs)
13767 == TYPE_PACK_EXPANSION);
13768 new_specs = add_exception_specifier (new_specs,
13769 expanded_specs,
13770 complain);
13771 specs = TREE_CHAIN (specs);
13772 continue;
13773 }
13774 }
13775
13776 for (i = 0; i < len; ++i)
13777 {
13778 if (expanded_specs)
13779 spec = TREE_VEC_ELT (expanded_specs, i);
13780 else
13781 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13782 if (spec == error_mark_node)
13783 return spec;
13784 new_specs = add_exception_specifier (new_specs, spec,
13785 complain);
13786 }
13787
13788 specs = TREE_CHAIN (specs);
13789 }
13790 }
13791 return new_specs;
13792 }
13793
13794 /* Take the tree structure T and replace template parameters used
13795 therein with the argument vector ARGS. IN_DECL is an associated
13796 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13797 Issue error and warning messages under control of COMPLAIN. Note
13798 that we must be relatively non-tolerant of extensions here, in
13799 order to preserve conformance; if we allow substitutions that
13800 should not be allowed, we may allow argument deductions that should
13801 not succeed, and therefore report ambiguous overload situations
13802 where there are none. In theory, we could allow the substitution,
13803 but indicate that it should have failed, and allow our caller to
13804 make sure that the right thing happens, but we don't try to do this
13805 yet.
13806
13807 This function is used for dealing with types, decls and the like;
13808 for expressions, use tsubst_expr or tsubst_copy. */
13809
13810 tree
13811 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13812 {
13813 enum tree_code code;
13814 tree type, r = NULL_TREE;
13815
13816 if (t == NULL_TREE || t == error_mark_node
13817 || t == integer_type_node
13818 || t == void_type_node
13819 || t == char_type_node
13820 || t == unknown_type_node
13821 || TREE_CODE (t) == NAMESPACE_DECL
13822 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13823 return t;
13824
13825 if (DECL_P (t))
13826 return tsubst_decl (t, args, complain);
13827
13828 if (args == NULL_TREE)
13829 return t;
13830
13831 code = TREE_CODE (t);
13832
13833 if (code == IDENTIFIER_NODE)
13834 type = IDENTIFIER_TYPE_VALUE (t);
13835 else
13836 type = TREE_TYPE (t);
13837
13838 gcc_assert (type != unknown_type_node);
13839
13840 /* Reuse typedefs. We need to do this to handle dependent attributes,
13841 such as attribute aligned. */
13842 if (TYPE_P (t)
13843 && typedef_variant_p (t))
13844 {
13845 tree decl = TYPE_NAME (t);
13846
13847 if (alias_template_specialization_p (t))
13848 {
13849 /* DECL represents an alias template and we want to
13850 instantiate it. */
13851 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13852 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13853 r = instantiate_alias_template (tmpl, gen_args, complain);
13854 }
13855 else if (DECL_CLASS_SCOPE_P (decl)
13856 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13857 && uses_template_parms (DECL_CONTEXT (decl)))
13858 {
13859 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13860 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13861 r = retrieve_specialization (tmpl, gen_args, 0);
13862 }
13863 else if (DECL_FUNCTION_SCOPE_P (decl)
13864 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13865 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13866 r = retrieve_local_specialization (decl);
13867 else
13868 /* The typedef is from a non-template context. */
13869 return t;
13870
13871 if (r)
13872 {
13873 r = TREE_TYPE (r);
13874 r = cp_build_qualified_type_real
13875 (r, cp_type_quals (t) | cp_type_quals (r),
13876 complain | tf_ignore_bad_quals);
13877 return r;
13878 }
13879 else
13880 {
13881 /* We don't have an instantiation yet, so drop the typedef. */
13882 int quals = cp_type_quals (t);
13883 t = DECL_ORIGINAL_TYPE (decl);
13884 t = cp_build_qualified_type_real (t, quals,
13885 complain | tf_ignore_bad_quals);
13886 }
13887 }
13888
13889 bool fndecl_type = (complain & tf_fndecl_type);
13890 complain &= ~tf_fndecl_type;
13891
13892 if (type
13893 && code != TYPENAME_TYPE
13894 && code != TEMPLATE_TYPE_PARM
13895 && code != TEMPLATE_PARM_INDEX
13896 && code != IDENTIFIER_NODE
13897 && code != FUNCTION_TYPE
13898 && code != METHOD_TYPE)
13899 type = tsubst (type, args, complain, in_decl);
13900 if (type == error_mark_node)
13901 return error_mark_node;
13902
13903 switch (code)
13904 {
13905 case RECORD_TYPE:
13906 case UNION_TYPE:
13907 case ENUMERAL_TYPE:
13908 return tsubst_aggr_type (t, args, complain, in_decl,
13909 /*entering_scope=*/0);
13910
13911 case ERROR_MARK:
13912 case IDENTIFIER_NODE:
13913 case VOID_TYPE:
13914 case REAL_TYPE:
13915 case COMPLEX_TYPE:
13916 case VECTOR_TYPE:
13917 case BOOLEAN_TYPE:
13918 case NULLPTR_TYPE:
13919 case LANG_TYPE:
13920 return t;
13921
13922 case INTEGER_TYPE:
13923 if (t == integer_type_node)
13924 return t;
13925
13926 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13927 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13928 return t;
13929
13930 {
13931 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13932
13933 max = tsubst_expr (omax, args, complain, in_decl,
13934 /*integral_constant_expression_p=*/false);
13935
13936 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13937 needed. */
13938 if (TREE_CODE (max) == NOP_EXPR
13939 && TREE_SIDE_EFFECTS (omax)
13940 && !TREE_TYPE (max))
13941 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13942
13943 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13944 with TREE_SIDE_EFFECTS that indicates this is not an integral
13945 constant expression. */
13946 if (processing_template_decl
13947 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13948 {
13949 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13950 TREE_SIDE_EFFECTS (max) = 1;
13951 }
13952
13953 return compute_array_index_type (NULL_TREE, max, complain);
13954 }
13955
13956 case TEMPLATE_TYPE_PARM:
13957 case TEMPLATE_TEMPLATE_PARM:
13958 case BOUND_TEMPLATE_TEMPLATE_PARM:
13959 case TEMPLATE_PARM_INDEX:
13960 {
13961 int idx;
13962 int level;
13963 int levels;
13964 tree arg = NULL_TREE;
13965
13966 /* Early in template argument deduction substitution, we don't
13967 want to reduce the level of 'auto', or it will be confused
13968 with a normal template parm in subsequent deduction. */
13969 if (is_auto (t) && (complain & tf_partial))
13970 return t;
13971
13972 r = NULL_TREE;
13973
13974 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13975 template_parm_level_and_index (t, &level, &idx);
13976
13977 levels = TMPL_ARGS_DEPTH (args);
13978 if (level <= levels
13979 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13980 {
13981 arg = TMPL_ARG (args, level, idx);
13982
13983 /* See through ARGUMENT_PACK_SELECT arguments. */
13984 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13985 arg = argument_pack_select_arg (arg);
13986 }
13987
13988 if (arg == error_mark_node)
13989 return error_mark_node;
13990 else if (arg != NULL_TREE)
13991 {
13992 if (ARGUMENT_PACK_P (arg))
13993 /* If ARG is an argument pack, we don't actually want to
13994 perform a substitution here, because substitutions
13995 for argument packs are only done
13996 element-by-element. We can get to this point when
13997 substituting the type of a non-type template
13998 parameter pack, when that type actually contains
13999 template parameter packs from an outer template, e.g.,
14000
14001 template<typename... Types> struct A {
14002 template<Types... Values> struct B { };
14003 }; */
14004 return t;
14005
14006 if (code == TEMPLATE_TYPE_PARM)
14007 {
14008 int quals;
14009 gcc_assert (TYPE_P (arg));
14010
14011 quals = cp_type_quals (arg) | cp_type_quals (t);
14012
14013 return cp_build_qualified_type_real
14014 (arg, quals, complain | tf_ignore_bad_quals);
14015 }
14016 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14017 {
14018 /* We are processing a type constructed from a
14019 template template parameter. */
14020 tree argvec = tsubst (TYPE_TI_ARGS (t),
14021 args, complain, in_decl);
14022 if (argvec == error_mark_node)
14023 return error_mark_node;
14024
14025 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14026 || TREE_CODE (arg) == TEMPLATE_DECL
14027 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14028
14029 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14030 /* Consider this code:
14031
14032 template <template <class> class Template>
14033 struct Internal {
14034 template <class Arg> using Bind = Template<Arg>;
14035 };
14036
14037 template <template <class> class Template, class Arg>
14038 using Instantiate = Template<Arg>; //#0
14039
14040 template <template <class> class Template,
14041 class Argument>
14042 using Bind =
14043 Instantiate<Internal<Template>::template Bind,
14044 Argument>; //#1
14045
14046 When #1 is parsed, the
14047 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14048 parameter `Template' in #0 matches the
14049 UNBOUND_CLASS_TEMPLATE representing the argument
14050 `Internal<Template>::template Bind'; We then want
14051 to assemble the type `Bind<Argument>' that can't
14052 be fully created right now, because
14053 `Internal<Template>' not being complete, the Bind
14054 template cannot be looked up in that context. So
14055 we need to "store" `Bind<Argument>' for later
14056 when the context of Bind becomes complete. Let's
14057 store that in a TYPENAME_TYPE. */
14058 return make_typename_type (TYPE_CONTEXT (arg),
14059 build_nt (TEMPLATE_ID_EXPR,
14060 TYPE_IDENTIFIER (arg),
14061 argvec),
14062 typename_type,
14063 complain);
14064
14065 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14066 are resolving nested-types in the signature of a
14067 member function templates. Otherwise ARG is a
14068 TEMPLATE_DECL and is the real template to be
14069 instantiated. */
14070 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14071 arg = TYPE_NAME (arg);
14072
14073 r = lookup_template_class (arg,
14074 argvec, in_decl,
14075 DECL_CONTEXT (arg),
14076 /*entering_scope=*/0,
14077 complain);
14078 return cp_build_qualified_type_real
14079 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14080 }
14081 else if (code == TEMPLATE_TEMPLATE_PARM)
14082 return arg;
14083 else
14084 /* TEMPLATE_PARM_INDEX. */
14085 return convert_from_reference (unshare_expr (arg));
14086 }
14087
14088 if (level == 1)
14089 /* This can happen during the attempted tsubst'ing in
14090 unify. This means that we don't yet have any information
14091 about the template parameter in question. */
14092 return t;
14093
14094 /* If we get here, we must have been looking at a parm for a
14095 more deeply nested template. Make a new version of this
14096 template parameter, but with a lower level. */
14097 switch (code)
14098 {
14099 case TEMPLATE_TYPE_PARM:
14100 case TEMPLATE_TEMPLATE_PARM:
14101 case BOUND_TEMPLATE_TEMPLATE_PARM:
14102 if (cp_type_quals (t))
14103 {
14104 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14105 r = cp_build_qualified_type_real
14106 (r, cp_type_quals (t),
14107 complain | (code == TEMPLATE_TYPE_PARM
14108 ? tf_ignore_bad_quals : 0));
14109 }
14110 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14111 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14112 && (r = (TEMPLATE_PARM_DESCENDANTS
14113 (TEMPLATE_TYPE_PARM_INDEX (t))))
14114 && (r = TREE_TYPE (r))
14115 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14116 /* Break infinite recursion when substituting the constraints
14117 of a constrained placeholder. */;
14118 else
14119 {
14120 r = copy_type (t);
14121 TEMPLATE_TYPE_PARM_INDEX (r)
14122 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14123 r, levels, args, complain);
14124 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14125 TYPE_MAIN_VARIANT (r) = r;
14126 TYPE_POINTER_TO (r) = NULL_TREE;
14127 TYPE_REFERENCE_TO (r) = NULL_TREE;
14128
14129 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14130 {
14131 /* Propagate constraints on placeholders. */
14132 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14133 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14134 = tsubst_constraint (constr, args, complain, in_decl);
14135 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14136 {
14137 pl = tsubst_copy (pl, args, complain, in_decl);
14138 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14139 }
14140 }
14141
14142 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14143 /* We have reduced the level of the template
14144 template parameter, but not the levels of its
14145 template parameters, so canonical_type_parameter
14146 will not be able to find the canonical template
14147 template parameter for this level. Thus, we
14148 require structural equality checking to compare
14149 TEMPLATE_TEMPLATE_PARMs. */
14150 SET_TYPE_STRUCTURAL_EQUALITY (r);
14151 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14152 SET_TYPE_STRUCTURAL_EQUALITY (r);
14153 else
14154 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14155
14156 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14157 {
14158 tree tinfo = TYPE_TEMPLATE_INFO (t);
14159 /* We might need to substitute into the types of non-type
14160 template parameters. */
14161 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14162 complain, in_decl);
14163 if (tmpl == error_mark_node)
14164 return error_mark_node;
14165 tree argvec = tsubst (TI_ARGS (tinfo), args,
14166 complain, in_decl);
14167 if (argvec == error_mark_node)
14168 return error_mark_node;
14169
14170 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14171 = build_template_info (tmpl, argvec);
14172 }
14173 }
14174 break;
14175
14176 case TEMPLATE_PARM_INDEX:
14177 /* OK, now substitute the type of the non-type parameter. We
14178 couldn't do it earlier because it might be an auto parameter,
14179 and we wouldn't need to if we had an argument. */
14180 type = tsubst (type, args, complain, in_decl);
14181 if (type == error_mark_node)
14182 return error_mark_node;
14183 r = reduce_template_parm_level (t, type, levels, args, complain);
14184 break;
14185
14186 default:
14187 gcc_unreachable ();
14188 }
14189
14190 return r;
14191 }
14192
14193 case TREE_LIST:
14194 {
14195 tree purpose, value, chain;
14196
14197 if (t == void_list_node)
14198 return t;
14199
14200 purpose = TREE_PURPOSE (t);
14201 if (purpose)
14202 {
14203 purpose = tsubst (purpose, args, complain, in_decl);
14204 if (purpose == error_mark_node)
14205 return error_mark_node;
14206 }
14207 value = TREE_VALUE (t);
14208 if (value)
14209 {
14210 value = tsubst (value, args, complain, in_decl);
14211 if (value == error_mark_node)
14212 return error_mark_node;
14213 }
14214 chain = TREE_CHAIN (t);
14215 if (chain && chain != void_type_node)
14216 {
14217 chain = tsubst (chain, args, complain, in_decl);
14218 if (chain == error_mark_node)
14219 return error_mark_node;
14220 }
14221 if (purpose == TREE_PURPOSE (t)
14222 && value == TREE_VALUE (t)
14223 && chain == TREE_CHAIN (t))
14224 return t;
14225 return hash_tree_cons (purpose, value, chain);
14226 }
14227
14228 case TREE_BINFO:
14229 /* We should never be tsubsting a binfo. */
14230 gcc_unreachable ();
14231
14232 case TREE_VEC:
14233 /* A vector of template arguments. */
14234 gcc_assert (!type);
14235 return tsubst_template_args (t, args, complain, in_decl);
14236
14237 case POINTER_TYPE:
14238 case REFERENCE_TYPE:
14239 {
14240 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14241 return t;
14242
14243 /* [temp.deduct]
14244
14245 Type deduction may fail for any of the following
14246 reasons:
14247
14248 -- Attempting to create a pointer to reference type.
14249 -- Attempting to create a reference to a reference type or
14250 a reference to void.
14251
14252 Core issue 106 says that creating a reference to a reference
14253 during instantiation is no longer a cause for failure. We
14254 only enforce this check in strict C++98 mode. */
14255 if ((TREE_CODE (type) == REFERENCE_TYPE
14256 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14257 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14258 {
14259 static location_t last_loc;
14260
14261 /* We keep track of the last time we issued this error
14262 message to avoid spewing a ton of messages during a
14263 single bad template instantiation. */
14264 if (complain & tf_error
14265 && last_loc != input_location)
14266 {
14267 if (VOID_TYPE_P (type))
14268 error ("forming reference to void");
14269 else if (code == POINTER_TYPE)
14270 error ("forming pointer to reference type %qT", type);
14271 else
14272 error ("forming reference to reference type %qT", type);
14273 last_loc = input_location;
14274 }
14275
14276 return error_mark_node;
14277 }
14278 else if (TREE_CODE (type) == FUNCTION_TYPE
14279 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14280 || type_memfn_rqual (type) != REF_QUAL_NONE))
14281 {
14282 if (complain & tf_error)
14283 {
14284 if (code == POINTER_TYPE)
14285 error ("forming pointer to qualified function type %qT",
14286 type);
14287 else
14288 error ("forming reference to qualified function type %qT",
14289 type);
14290 }
14291 return error_mark_node;
14292 }
14293 else if (code == POINTER_TYPE)
14294 {
14295 r = build_pointer_type (type);
14296 if (TREE_CODE (type) == METHOD_TYPE)
14297 r = build_ptrmemfunc_type (r);
14298 }
14299 else if (TREE_CODE (type) == REFERENCE_TYPE)
14300 /* In C++0x, during template argument substitution, when there is an
14301 attempt to create a reference to a reference type, reference
14302 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14303
14304 "If a template-argument for a template-parameter T names a type
14305 that is a reference to a type A, an attempt to create the type
14306 'lvalue reference to cv T' creates the type 'lvalue reference to
14307 A,' while an attempt to create the type type rvalue reference to
14308 cv T' creates the type T"
14309 */
14310 r = cp_build_reference_type
14311 (TREE_TYPE (type),
14312 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14313 else
14314 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14315 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14316
14317 if (r != error_mark_node)
14318 /* Will this ever be needed for TYPE_..._TO values? */
14319 layout_type (r);
14320
14321 return r;
14322 }
14323 case OFFSET_TYPE:
14324 {
14325 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14326 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14327 {
14328 /* [temp.deduct]
14329
14330 Type deduction may fail for any of the following
14331 reasons:
14332
14333 -- Attempting to create "pointer to member of T" when T
14334 is not a class type. */
14335 if (complain & tf_error)
14336 error ("creating pointer to member of non-class type %qT", r);
14337 return error_mark_node;
14338 }
14339 if (TREE_CODE (type) == REFERENCE_TYPE)
14340 {
14341 if (complain & tf_error)
14342 error ("creating pointer to member reference type %qT", type);
14343 return error_mark_node;
14344 }
14345 if (VOID_TYPE_P (type))
14346 {
14347 if (complain & tf_error)
14348 error ("creating pointer to member of type void");
14349 return error_mark_node;
14350 }
14351 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14352 if (TREE_CODE (type) == FUNCTION_TYPE)
14353 {
14354 /* The type of the implicit object parameter gets its
14355 cv-qualifiers from the FUNCTION_TYPE. */
14356 tree memptr;
14357 tree method_type
14358 = build_memfn_type (type, r, type_memfn_quals (type),
14359 type_memfn_rqual (type));
14360 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14361 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14362 complain);
14363 }
14364 else
14365 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14366 cp_type_quals (t),
14367 complain);
14368 }
14369 case FUNCTION_TYPE:
14370 case METHOD_TYPE:
14371 {
14372 tree fntype;
14373 tree specs;
14374 fntype = tsubst_function_type (t, args, complain, in_decl);
14375 if (fntype == error_mark_node)
14376 return error_mark_node;
14377
14378 /* Substitute the exception specification. */
14379 specs = tsubst_exception_specification (t, args, complain, in_decl,
14380 /*defer_ok*/fndecl_type);
14381 if (specs == error_mark_node)
14382 return error_mark_node;
14383 if (specs)
14384 fntype = build_exception_variant (fntype, specs);
14385 return fntype;
14386 }
14387 case ARRAY_TYPE:
14388 {
14389 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14390 if (domain == error_mark_node)
14391 return error_mark_node;
14392
14393 /* As an optimization, we avoid regenerating the array type if
14394 it will obviously be the same as T. */
14395 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14396 return t;
14397
14398 /* These checks should match the ones in create_array_type_for_decl.
14399
14400 [temp.deduct]
14401
14402 The deduction may fail for any of the following reasons:
14403
14404 -- Attempting to create an array with an element type that
14405 is void, a function type, or a reference type, or [DR337]
14406 an abstract class type. */
14407 if (VOID_TYPE_P (type)
14408 || TREE_CODE (type) == FUNCTION_TYPE
14409 || (TREE_CODE (type) == ARRAY_TYPE
14410 && TYPE_DOMAIN (type) == NULL_TREE)
14411 || TREE_CODE (type) == REFERENCE_TYPE)
14412 {
14413 if (complain & tf_error)
14414 error ("creating array of %qT", type);
14415 return error_mark_node;
14416 }
14417
14418 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14419 return error_mark_node;
14420
14421 r = build_cplus_array_type (type, domain);
14422
14423 if (TYPE_USER_ALIGN (t))
14424 {
14425 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14426 TYPE_USER_ALIGN (r) = 1;
14427 }
14428
14429 return r;
14430 }
14431
14432 case TYPENAME_TYPE:
14433 {
14434 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14435 in_decl, /*entering_scope=*/1);
14436 if (ctx == error_mark_node)
14437 return error_mark_node;
14438
14439 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14440 complain, in_decl);
14441 if (f == error_mark_node)
14442 return error_mark_node;
14443
14444 if (!MAYBE_CLASS_TYPE_P (ctx))
14445 {
14446 if (complain & tf_error)
14447 error ("%qT is not a class, struct, or union type", ctx);
14448 return error_mark_node;
14449 }
14450 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14451 {
14452 /* Normally, make_typename_type does not require that the CTX
14453 have complete type in order to allow things like:
14454
14455 template <class T> struct S { typename S<T>::X Y; };
14456
14457 But, such constructs have already been resolved by this
14458 point, so here CTX really should have complete type, unless
14459 it's a partial instantiation. */
14460 ctx = complete_type (ctx);
14461 if (!COMPLETE_TYPE_P (ctx))
14462 {
14463 if (complain & tf_error)
14464 cxx_incomplete_type_error (NULL_TREE, ctx);
14465 return error_mark_node;
14466 }
14467 }
14468
14469 f = make_typename_type (ctx, f, typename_type,
14470 complain | tf_keep_type_decl);
14471 if (f == error_mark_node)
14472 return f;
14473 if (TREE_CODE (f) == TYPE_DECL)
14474 {
14475 complain |= tf_ignore_bad_quals;
14476 f = TREE_TYPE (f);
14477 }
14478
14479 if (TREE_CODE (f) != TYPENAME_TYPE)
14480 {
14481 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14482 {
14483 if (complain & tf_error)
14484 error ("%qT resolves to %qT, which is not an enumeration type",
14485 t, f);
14486 else
14487 return error_mark_node;
14488 }
14489 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14490 {
14491 if (complain & tf_error)
14492 error ("%qT resolves to %qT, which is is not a class type",
14493 t, f);
14494 else
14495 return error_mark_node;
14496 }
14497 }
14498
14499 return cp_build_qualified_type_real
14500 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14501 }
14502
14503 case UNBOUND_CLASS_TEMPLATE:
14504 {
14505 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14506 in_decl, /*entering_scope=*/1);
14507 tree name = TYPE_IDENTIFIER (t);
14508 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14509
14510 if (ctx == error_mark_node || name == error_mark_node)
14511 return error_mark_node;
14512
14513 if (parm_list)
14514 parm_list = tsubst_template_parms (parm_list, args, complain);
14515 return make_unbound_class_template (ctx, name, parm_list, complain);
14516 }
14517
14518 case TYPEOF_TYPE:
14519 {
14520 tree type;
14521
14522 ++cp_unevaluated_operand;
14523 ++c_inhibit_evaluation_warnings;
14524
14525 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14526 complain, in_decl,
14527 /*integral_constant_expression_p=*/false);
14528
14529 --cp_unevaluated_operand;
14530 --c_inhibit_evaluation_warnings;
14531
14532 type = finish_typeof (type);
14533 return cp_build_qualified_type_real (type,
14534 cp_type_quals (t)
14535 | cp_type_quals (type),
14536 complain);
14537 }
14538
14539 case DECLTYPE_TYPE:
14540 {
14541 tree type;
14542
14543 ++cp_unevaluated_operand;
14544 ++c_inhibit_evaluation_warnings;
14545
14546 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14547 complain|tf_decltype, in_decl,
14548 /*function_p*/false,
14549 /*integral_constant_expression*/false);
14550
14551 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14552 {
14553 if (type == NULL_TREE)
14554 {
14555 if (complain & tf_error)
14556 error ("empty initializer in lambda init-capture");
14557 type = error_mark_node;
14558 }
14559 else if (TREE_CODE (type) == TREE_LIST)
14560 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14561 }
14562
14563 --cp_unevaluated_operand;
14564 --c_inhibit_evaluation_warnings;
14565
14566 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14567 type = lambda_capture_field_type (type,
14568 DECLTYPE_FOR_INIT_CAPTURE (t),
14569 DECLTYPE_FOR_REF_CAPTURE (t));
14570 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14571 type = lambda_proxy_type (type);
14572 else
14573 {
14574 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14575 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14576 && EXPR_P (type))
14577 /* In a template ~id could be either a complement expression
14578 or an unqualified-id naming a destructor; if instantiating
14579 it produces an expression, it's not an id-expression or
14580 member access. */
14581 id = false;
14582 type = finish_decltype_type (type, id, complain);
14583 }
14584 return cp_build_qualified_type_real (type,
14585 cp_type_quals (t)
14586 | cp_type_quals (type),
14587 complain | tf_ignore_bad_quals);
14588 }
14589
14590 case UNDERLYING_TYPE:
14591 {
14592 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14593 complain, in_decl);
14594 return finish_underlying_type (type);
14595 }
14596
14597 case TYPE_ARGUMENT_PACK:
14598 case NONTYPE_ARGUMENT_PACK:
14599 {
14600 tree r;
14601
14602 if (code == NONTYPE_ARGUMENT_PACK)
14603 r = make_node (code);
14604 else
14605 r = cxx_make_type (code);
14606
14607 tree pack_args = ARGUMENT_PACK_ARGS (t);
14608 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14609 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14610
14611 return r;
14612 }
14613
14614 case VOID_CST:
14615 case INTEGER_CST:
14616 case REAL_CST:
14617 case STRING_CST:
14618 case PLUS_EXPR:
14619 case MINUS_EXPR:
14620 case NEGATE_EXPR:
14621 case NOP_EXPR:
14622 case INDIRECT_REF:
14623 case ADDR_EXPR:
14624 case CALL_EXPR:
14625 case ARRAY_REF:
14626 case SCOPE_REF:
14627 /* We should use one of the expression tsubsts for these codes. */
14628 gcc_unreachable ();
14629
14630 default:
14631 sorry ("use of %qs in template", get_tree_code_name (code));
14632 return error_mark_node;
14633 }
14634 }
14635
14636 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14637 expression on the left-hand side of the "." or "->" operator. We
14638 only do the lookup if we had a dependent BASELINK. Otherwise we
14639 adjust it onto the instantiated heirarchy. */
14640
14641 static tree
14642 tsubst_baselink (tree baselink, tree object_type,
14643 tree args, tsubst_flags_t complain, tree in_decl)
14644 {
14645 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14646 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14647 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14648
14649 tree optype = BASELINK_OPTYPE (baselink);
14650 optype = tsubst (optype, args, complain, in_decl);
14651
14652 tree template_args = NULL_TREE;
14653 bool template_id_p = false;
14654 tree fns = BASELINK_FUNCTIONS (baselink);
14655 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14656 {
14657 template_id_p = true;
14658 template_args = TREE_OPERAND (fns, 1);
14659 fns = TREE_OPERAND (fns, 0);
14660 if (template_args)
14661 template_args = tsubst_template_args (template_args, args,
14662 complain, in_decl);
14663 }
14664
14665 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14666 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14667 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14668
14669 if (dependent_p)
14670 {
14671 tree name = OVL_NAME (fns);
14672 if (IDENTIFIER_CONV_OP_P (name))
14673 name = make_conv_op_name (optype);
14674
14675 if (name == complete_dtor_identifier)
14676 /* Treat as-if non-dependent below. */
14677 dependent_p = false;
14678
14679 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14680 if (!baselink)
14681 {
14682 if ((complain & tf_error)
14683 && constructor_name_p (name, qualifying_scope))
14684 error ("cannot call constructor %<%T::%D%> directly",
14685 qualifying_scope, name);
14686 return error_mark_node;
14687 }
14688
14689 if (BASELINK_P (baselink))
14690 fns = BASELINK_FUNCTIONS (baselink);
14691 }
14692 else
14693 /* We're going to overwrite pieces below, make a duplicate. */
14694 baselink = copy_node (baselink);
14695
14696 /* If lookup found a single function, mark it as used at this point.
14697 (If lookup found multiple functions the one selected later by
14698 overload resolution will be marked as used at that point.) */
14699 if (!template_id_p && !really_overloaded_fn (fns)
14700 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14701 return error_mark_node;
14702
14703 if (BASELINK_P (baselink))
14704 {
14705 /* Add back the template arguments, if present. */
14706 if (template_id_p)
14707 BASELINK_FUNCTIONS (baselink)
14708 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14709
14710 /* Update the conversion operator type. */
14711 BASELINK_OPTYPE (baselink) = optype;
14712 }
14713
14714 if (!object_type)
14715 object_type = current_class_type;
14716
14717 if (qualified_p || !dependent_p)
14718 {
14719 baselink = adjust_result_of_qualified_name_lookup (baselink,
14720 qualifying_scope,
14721 object_type);
14722 if (!qualified_p)
14723 /* We need to call adjust_result_of_qualified_name_lookup in case the
14724 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14725 so that we still get virtual function binding. */
14726 BASELINK_QUALIFIED_P (baselink) = false;
14727 }
14728
14729 return baselink;
14730 }
14731
14732 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14733 true if the qualified-id will be a postfix-expression in-and-of
14734 itself; false if more of the postfix-expression follows the
14735 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14736 of "&". */
14737
14738 static tree
14739 tsubst_qualified_id (tree qualified_id, tree args,
14740 tsubst_flags_t complain, tree in_decl,
14741 bool done, bool address_p)
14742 {
14743 tree expr;
14744 tree scope;
14745 tree name;
14746 bool is_template;
14747 tree template_args;
14748 location_t loc = UNKNOWN_LOCATION;
14749
14750 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14751
14752 /* Figure out what name to look up. */
14753 name = TREE_OPERAND (qualified_id, 1);
14754 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14755 {
14756 is_template = true;
14757 loc = EXPR_LOCATION (name);
14758 template_args = TREE_OPERAND (name, 1);
14759 if (template_args)
14760 template_args = tsubst_template_args (template_args, args,
14761 complain, in_decl);
14762 if (template_args == error_mark_node)
14763 return error_mark_node;
14764 name = TREE_OPERAND (name, 0);
14765 }
14766 else
14767 {
14768 is_template = false;
14769 template_args = NULL_TREE;
14770 }
14771
14772 /* Substitute into the qualifying scope. When there are no ARGS, we
14773 are just trying to simplify a non-dependent expression. In that
14774 case the qualifying scope may be dependent, and, in any case,
14775 substituting will not help. */
14776 scope = TREE_OPERAND (qualified_id, 0);
14777 if (args)
14778 {
14779 scope = tsubst (scope, args, complain, in_decl);
14780 expr = tsubst_copy (name, args, complain, in_decl);
14781 }
14782 else
14783 expr = name;
14784
14785 if (dependent_scope_p (scope))
14786 {
14787 if (is_template)
14788 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14789 tree r = build_qualified_name (NULL_TREE, scope, expr,
14790 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14791 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14792 return r;
14793 }
14794
14795 if (!BASELINK_P (name) && !DECL_P (expr))
14796 {
14797 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14798 {
14799 /* A BIT_NOT_EXPR is used to represent a destructor. */
14800 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14801 {
14802 error ("qualifying type %qT does not match destructor name ~%qT",
14803 scope, TREE_OPERAND (expr, 0));
14804 expr = error_mark_node;
14805 }
14806 else
14807 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14808 /*is_type_p=*/0, false);
14809 }
14810 else
14811 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14812 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14813 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14814 {
14815 if (complain & tf_error)
14816 {
14817 error ("dependent-name %qE is parsed as a non-type, but "
14818 "instantiation yields a type", qualified_id);
14819 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14820 }
14821 return error_mark_node;
14822 }
14823 }
14824
14825 if (DECL_P (expr))
14826 {
14827 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14828 scope);
14829 /* Remember that there was a reference to this entity. */
14830 if (!mark_used (expr, complain) && !(complain & tf_error))
14831 return error_mark_node;
14832 }
14833
14834 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14835 {
14836 if (complain & tf_error)
14837 qualified_name_lookup_error (scope,
14838 TREE_OPERAND (qualified_id, 1),
14839 expr, input_location);
14840 return error_mark_node;
14841 }
14842
14843 if (is_template)
14844 {
14845 if (variable_template_p (expr))
14846 expr = lookup_and_finish_template_variable (expr, template_args,
14847 complain);
14848 else
14849 expr = lookup_template_function (expr, template_args);
14850 }
14851
14852 if (expr == error_mark_node && complain & tf_error)
14853 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14854 expr, input_location);
14855 else if (TYPE_P (scope))
14856 {
14857 expr = (adjust_result_of_qualified_name_lookup
14858 (expr, scope, current_nonlambda_class_type ()));
14859 expr = (finish_qualified_id_expr
14860 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14861 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14862 /*template_arg_p=*/false, complain));
14863 }
14864
14865 /* Expressions do not generally have reference type. */
14866 if (TREE_CODE (expr) != SCOPE_REF
14867 /* However, if we're about to form a pointer-to-member, we just
14868 want the referenced member referenced. */
14869 && TREE_CODE (expr) != OFFSET_REF)
14870 expr = convert_from_reference (expr);
14871
14872 if (REF_PARENTHESIZED_P (qualified_id))
14873 expr = force_paren_expr (expr);
14874
14875 return expr;
14876 }
14877
14878 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14879 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14880 for tsubst. */
14881
14882 static tree
14883 tsubst_init (tree init, tree decl, tree args,
14884 tsubst_flags_t complain, tree in_decl)
14885 {
14886 if (!init)
14887 return NULL_TREE;
14888
14889 init = tsubst_expr (init, args, complain, in_decl, false);
14890
14891 if (!init && TREE_TYPE (decl) != error_mark_node)
14892 {
14893 /* If we had an initializer but it
14894 instantiated to nothing,
14895 value-initialize the object. This will
14896 only occur when the initializer was a
14897 pack expansion where the parameter packs
14898 used in that expansion were of length
14899 zero. */
14900 init = build_value_init (TREE_TYPE (decl),
14901 complain);
14902 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14903 init = get_target_expr_sfinae (init, complain);
14904 if (TREE_CODE (init) == TARGET_EXPR)
14905 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14906 }
14907
14908 return init;
14909 }
14910
14911 /* Like tsubst, but deals with expressions. This function just replaces
14912 template parms; to finish processing the resultant expression, use
14913 tsubst_copy_and_build or tsubst_expr. */
14914
14915 static tree
14916 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14917 {
14918 enum tree_code code;
14919 tree r;
14920
14921 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14922 return t;
14923
14924 code = TREE_CODE (t);
14925
14926 switch (code)
14927 {
14928 case PARM_DECL:
14929 r = retrieve_local_specialization (t);
14930
14931 if (r == NULL_TREE)
14932 {
14933 /* We get here for a use of 'this' in an NSDMI. */
14934 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14935 return current_class_ptr;
14936
14937 /* This can happen for a parameter name used later in a function
14938 declaration (such as in a late-specified return type). Just
14939 make a dummy decl, since it's only used for its type. */
14940 gcc_assert (cp_unevaluated_operand != 0);
14941 r = tsubst_decl (t, args, complain);
14942 /* Give it the template pattern as its context; its true context
14943 hasn't been instantiated yet and this is good enough for
14944 mangling. */
14945 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14946 }
14947
14948 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14949 r = argument_pack_select_arg (r);
14950 if (!mark_used (r, complain) && !(complain & tf_error))
14951 return error_mark_node;
14952 return r;
14953
14954 case CONST_DECL:
14955 {
14956 tree enum_type;
14957 tree v;
14958
14959 if (DECL_TEMPLATE_PARM_P (t))
14960 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14961 /* There is no need to substitute into namespace-scope
14962 enumerators. */
14963 if (DECL_NAMESPACE_SCOPE_P (t))
14964 return t;
14965 /* If ARGS is NULL, then T is known to be non-dependent. */
14966 if (args == NULL_TREE)
14967 return scalar_constant_value (t);
14968
14969 /* Unfortunately, we cannot just call lookup_name here.
14970 Consider:
14971
14972 template <int I> int f() {
14973 enum E { a = I };
14974 struct S { void g() { E e = a; } };
14975 };
14976
14977 When we instantiate f<7>::S::g(), say, lookup_name is not
14978 clever enough to find f<7>::a. */
14979 enum_type
14980 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14981 /*entering_scope=*/0);
14982
14983 for (v = TYPE_VALUES (enum_type);
14984 v != NULL_TREE;
14985 v = TREE_CHAIN (v))
14986 if (TREE_PURPOSE (v) == DECL_NAME (t))
14987 return TREE_VALUE (v);
14988
14989 /* We didn't find the name. That should never happen; if
14990 name-lookup found it during preliminary parsing, we
14991 should find it again here during instantiation. */
14992 gcc_unreachable ();
14993 }
14994 return t;
14995
14996 case FIELD_DECL:
14997 if (DECL_CONTEXT (t))
14998 {
14999 tree ctx;
15000
15001 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15002 /*entering_scope=*/1);
15003 if (ctx != DECL_CONTEXT (t))
15004 {
15005 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15006 if (!r)
15007 {
15008 if (complain & tf_error)
15009 error ("using invalid field %qD", t);
15010 return error_mark_node;
15011 }
15012 return r;
15013 }
15014 }
15015
15016 return t;
15017
15018 case VAR_DECL:
15019 case FUNCTION_DECL:
15020 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15021 r = tsubst (t, args, complain, in_decl);
15022 else if (local_variable_p (t)
15023 && uses_template_parms (DECL_CONTEXT (t)))
15024 {
15025 r = retrieve_local_specialization (t);
15026 if (r == NULL_TREE)
15027 {
15028 /* First try name lookup to find the instantiation. */
15029 r = lookup_name (DECL_NAME (t));
15030 if (r && !is_capture_proxy (r))
15031 {
15032 /* Make sure that the one we found is the one we want. */
15033 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15034 if (ctx != DECL_CONTEXT (r))
15035 r = NULL_TREE;
15036 }
15037
15038 if (r)
15039 /* OK */;
15040 else
15041 {
15042 /* This can happen for a variable used in a
15043 late-specified return type of a local lambda, or for a
15044 local static or constant. Building a new VAR_DECL
15045 should be OK in all those cases. */
15046 r = tsubst_decl (t, args, complain);
15047 if (local_specializations)
15048 /* Avoid infinite recursion (79640). */
15049 register_local_specialization (r, t);
15050 if (decl_maybe_constant_var_p (r))
15051 {
15052 /* We can't call cp_finish_decl, so handle the
15053 initializer by hand. */
15054 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15055 complain, in_decl);
15056 if (!processing_template_decl)
15057 init = maybe_constant_init (init);
15058 if (processing_template_decl
15059 ? potential_constant_expression (init)
15060 : reduced_constant_expression_p (init))
15061 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15062 = TREE_CONSTANT (r) = true;
15063 DECL_INITIAL (r) = init;
15064 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15065 TREE_TYPE (r)
15066 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15067 complain, adc_variable_type);
15068 }
15069 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15070 || decl_constant_var_p (r)
15071 || errorcount || sorrycount);
15072 if (!processing_template_decl
15073 && !TREE_STATIC (r))
15074 r = process_outer_var_ref (r, complain);
15075 }
15076 /* Remember this for subsequent uses. */
15077 if (local_specializations)
15078 register_local_specialization (r, t);
15079 }
15080 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15081 r = argument_pack_select_arg (r);
15082 }
15083 else
15084 r = t;
15085 if (!mark_used (r, complain))
15086 return error_mark_node;
15087 return r;
15088
15089 case NAMESPACE_DECL:
15090 return t;
15091
15092 case OVERLOAD:
15093 /* An OVERLOAD will always be a non-dependent overload set; an
15094 overload set from function scope will just be represented with an
15095 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15096 gcc_assert (!uses_template_parms (t));
15097 /* We must have marked any lookups as persistent. */
15098 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15099 return t;
15100
15101 case BASELINK:
15102 return tsubst_baselink (t, current_nonlambda_class_type (),
15103 args, complain, in_decl);
15104
15105 case TEMPLATE_DECL:
15106 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15107 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15108 args, complain, in_decl);
15109 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15110 return tsubst (t, args, complain, in_decl);
15111 else if (DECL_CLASS_SCOPE_P (t)
15112 && uses_template_parms (DECL_CONTEXT (t)))
15113 {
15114 /* Template template argument like the following example need
15115 special treatment:
15116
15117 template <template <class> class TT> struct C {};
15118 template <class T> struct D {
15119 template <class U> struct E {};
15120 C<E> c; // #1
15121 };
15122 D<int> d; // #2
15123
15124 We are processing the template argument `E' in #1 for
15125 the template instantiation #2. Originally, `E' is a
15126 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15127 have to substitute this with one having context `D<int>'. */
15128
15129 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15130 if (dependent_scope_p (context))
15131 {
15132 /* When rewriting a constructor into a deduction guide, a
15133 non-dependent name can become dependent, so memtmpl<args>
15134 becomes context::template memtmpl<args>. */
15135 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15136 return build_qualified_name (type, context, DECL_NAME (t),
15137 /*template*/true);
15138 }
15139 return lookup_field (context, DECL_NAME(t), 0, false);
15140 }
15141 else
15142 /* Ordinary template template argument. */
15143 return t;
15144
15145 case NON_LVALUE_EXPR:
15146 case VIEW_CONVERT_EXPR:
15147 {
15148 /* Handle location wrappers by substituting the wrapped node
15149 first, *then* reusing the resulting type. Doing the type
15150 first ensures that we handle template parameters and
15151 parameter pack expansions. */
15152 gcc_assert (location_wrapper_p (t));
15153 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15154 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15155 }
15156
15157 case CAST_EXPR:
15158 case REINTERPRET_CAST_EXPR:
15159 case CONST_CAST_EXPR:
15160 case STATIC_CAST_EXPR:
15161 case DYNAMIC_CAST_EXPR:
15162 case IMPLICIT_CONV_EXPR:
15163 case CONVERT_EXPR:
15164 case NOP_EXPR:
15165 {
15166 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15167 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15168 return build1 (code, type, op0);
15169 }
15170
15171 case SIZEOF_EXPR:
15172 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15173 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15174 {
15175 tree expanded, op = TREE_OPERAND (t, 0);
15176 int len = 0;
15177
15178 if (SIZEOF_EXPR_TYPE_P (t))
15179 op = TREE_TYPE (op);
15180
15181 ++cp_unevaluated_operand;
15182 ++c_inhibit_evaluation_warnings;
15183 /* We only want to compute the number of arguments. */
15184 if (PACK_EXPANSION_P (op))
15185 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15186 else
15187 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15188 args, complain, in_decl);
15189 --cp_unevaluated_operand;
15190 --c_inhibit_evaluation_warnings;
15191
15192 if (TREE_CODE (expanded) == TREE_VEC)
15193 {
15194 len = TREE_VEC_LENGTH (expanded);
15195 /* Set TREE_USED for the benefit of -Wunused. */
15196 for (int i = 0; i < len; i++)
15197 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15198 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15199 }
15200
15201 if (expanded == error_mark_node)
15202 return error_mark_node;
15203 else if (PACK_EXPANSION_P (expanded)
15204 || (TREE_CODE (expanded) == TREE_VEC
15205 && pack_expansion_args_count (expanded)))
15206
15207 {
15208 if (PACK_EXPANSION_P (expanded))
15209 /* OK. */;
15210 else if (TREE_VEC_LENGTH (expanded) == 1)
15211 expanded = TREE_VEC_ELT (expanded, 0);
15212 else
15213 expanded = make_argument_pack (expanded);
15214
15215 if (TYPE_P (expanded))
15216 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15217 complain & tf_error);
15218 else
15219 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15220 complain & tf_error);
15221 }
15222 else
15223 return build_int_cst (size_type_node, len);
15224 }
15225 if (SIZEOF_EXPR_TYPE_P (t))
15226 {
15227 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15228 args, complain, in_decl);
15229 r = build1 (NOP_EXPR, r, error_mark_node);
15230 r = build1 (SIZEOF_EXPR,
15231 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15232 SIZEOF_EXPR_TYPE_P (r) = 1;
15233 return r;
15234 }
15235 /* Fall through */
15236
15237 case INDIRECT_REF:
15238 case NEGATE_EXPR:
15239 case TRUTH_NOT_EXPR:
15240 case BIT_NOT_EXPR:
15241 case ADDR_EXPR:
15242 case UNARY_PLUS_EXPR: /* Unary + */
15243 case ALIGNOF_EXPR:
15244 case AT_ENCODE_EXPR:
15245 case ARROW_EXPR:
15246 case THROW_EXPR:
15247 case TYPEID_EXPR:
15248 case REALPART_EXPR:
15249 case IMAGPART_EXPR:
15250 case PAREN_EXPR:
15251 {
15252 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15253 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15254 return build1 (code, type, op0);
15255 }
15256
15257 case COMPONENT_REF:
15258 {
15259 tree object;
15260 tree name;
15261
15262 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15263 name = TREE_OPERAND (t, 1);
15264 if (TREE_CODE (name) == BIT_NOT_EXPR)
15265 {
15266 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15267 complain, in_decl);
15268 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15269 }
15270 else if (TREE_CODE (name) == SCOPE_REF
15271 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15272 {
15273 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15274 complain, in_decl);
15275 name = TREE_OPERAND (name, 1);
15276 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15277 complain, in_decl);
15278 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15279 name = build_qualified_name (/*type=*/NULL_TREE,
15280 base, name,
15281 /*template_p=*/false);
15282 }
15283 else if (BASELINK_P (name))
15284 name = tsubst_baselink (name,
15285 non_reference (TREE_TYPE (object)),
15286 args, complain,
15287 in_decl);
15288 else
15289 name = tsubst_copy (name, args, complain, in_decl);
15290 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15291 }
15292
15293 case PLUS_EXPR:
15294 case MINUS_EXPR:
15295 case MULT_EXPR:
15296 case TRUNC_DIV_EXPR:
15297 case CEIL_DIV_EXPR:
15298 case FLOOR_DIV_EXPR:
15299 case ROUND_DIV_EXPR:
15300 case EXACT_DIV_EXPR:
15301 case BIT_AND_EXPR:
15302 case BIT_IOR_EXPR:
15303 case BIT_XOR_EXPR:
15304 case TRUNC_MOD_EXPR:
15305 case FLOOR_MOD_EXPR:
15306 case TRUTH_ANDIF_EXPR:
15307 case TRUTH_ORIF_EXPR:
15308 case TRUTH_AND_EXPR:
15309 case TRUTH_OR_EXPR:
15310 case RSHIFT_EXPR:
15311 case LSHIFT_EXPR:
15312 case RROTATE_EXPR:
15313 case LROTATE_EXPR:
15314 case EQ_EXPR:
15315 case NE_EXPR:
15316 case MAX_EXPR:
15317 case MIN_EXPR:
15318 case LE_EXPR:
15319 case GE_EXPR:
15320 case LT_EXPR:
15321 case GT_EXPR:
15322 case COMPOUND_EXPR:
15323 case DOTSTAR_EXPR:
15324 case MEMBER_REF:
15325 case PREDECREMENT_EXPR:
15326 case PREINCREMENT_EXPR:
15327 case POSTDECREMENT_EXPR:
15328 case POSTINCREMENT_EXPR:
15329 {
15330 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15331 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15332 return build_nt (code, op0, op1);
15333 }
15334
15335 case SCOPE_REF:
15336 {
15337 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15338 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15339 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15340 QUALIFIED_NAME_IS_TEMPLATE (t));
15341 }
15342
15343 case ARRAY_REF:
15344 {
15345 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15346 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15347 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15348 }
15349
15350 case CALL_EXPR:
15351 {
15352 int n = VL_EXP_OPERAND_LENGTH (t);
15353 tree result = build_vl_exp (CALL_EXPR, n);
15354 int i;
15355 for (i = 0; i < n; i++)
15356 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15357 complain, in_decl);
15358 return result;
15359 }
15360
15361 case COND_EXPR:
15362 case MODOP_EXPR:
15363 case PSEUDO_DTOR_EXPR:
15364 case VEC_PERM_EXPR:
15365 {
15366 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15367 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15368 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15369 r = build_nt (code, op0, op1, op2);
15370 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15371 return r;
15372 }
15373
15374 case NEW_EXPR:
15375 {
15376 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15377 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15378 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15379 r = build_nt (code, op0, op1, op2);
15380 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15381 return r;
15382 }
15383
15384 case DELETE_EXPR:
15385 {
15386 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15387 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15388 r = build_nt (code, op0, op1);
15389 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15390 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15391 return r;
15392 }
15393
15394 case TEMPLATE_ID_EXPR:
15395 {
15396 /* Substituted template arguments */
15397 tree fn = TREE_OPERAND (t, 0);
15398 tree targs = TREE_OPERAND (t, 1);
15399
15400 fn = tsubst_copy (fn, args, complain, in_decl);
15401 if (targs)
15402 targs = tsubst_template_args (targs, args, complain, in_decl);
15403
15404 return lookup_template_function (fn, targs);
15405 }
15406
15407 case TREE_LIST:
15408 {
15409 tree purpose, value, chain;
15410
15411 if (t == void_list_node)
15412 return t;
15413
15414 purpose = TREE_PURPOSE (t);
15415 if (purpose)
15416 purpose = tsubst_copy (purpose, args, complain, in_decl);
15417 value = TREE_VALUE (t);
15418 if (value)
15419 value = tsubst_copy (value, args, complain, in_decl);
15420 chain = TREE_CHAIN (t);
15421 if (chain && chain != void_type_node)
15422 chain = tsubst_copy (chain, args, complain, in_decl);
15423 if (purpose == TREE_PURPOSE (t)
15424 && value == TREE_VALUE (t)
15425 && chain == TREE_CHAIN (t))
15426 return t;
15427 return tree_cons (purpose, value, chain);
15428 }
15429
15430 case RECORD_TYPE:
15431 case UNION_TYPE:
15432 case ENUMERAL_TYPE:
15433 case INTEGER_TYPE:
15434 case TEMPLATE_TYPE_PARM:
15435 case TEMPLATE_TEMPLATE_PARM:
15436 case BOUND_TEMPLATE_TEMPLATE_PARM:
15437 case TEMPLATE_PARM_INDEX:
15438 case POINTER_TYPE:
15439 case REFERENCE_TYPE:
15440 case OFFSET_TYPE:
15441 case FUNCTION_TYPE:
15442 case METHOD_TYPE:
15443 case ARRAY_TYPE:
15444 case TYPENAME_TYPE:
15445 case UNBOUND_CLASS_TEMPLATE:
15446 case TYPEOF_TYPE:
15447 case DECLTYPE_TYPE:
15448 case TYPE_DECL:
15449 return tsubst (t, args, complain, in_decl);
15450
15451 case USING_DECL:
15452 t = DECL_NAME (t);
15453 /* Fall through. */
15454 case IDENTIFIER_NODE:
15455 if (IDENTIFIER_CONV_OP_P (t))
15456 {
15457 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15458 return make_conv_op_name (new_type);
15459 }
15460 else
15461 return t;
15462
15463 case CONSTRUCTOR:
15464 /* This is handled by tsubst_copy_and_build. */
15465 gcc_unreachable ();
15466
15467 case VA_ARG_EXPR:
15468 {
15469 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15470 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15471 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15472 }
15473
15474 case CLEANUP_POINT_EXPR:
15475 /* We shouldn't have built any of these during initial template
15476 generation. Instead, they should be built during instantiation
15477 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15478 gcc_unreachable ();
15479
15480 case OFFSET_REF:
15481 {
15482 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15483 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15484 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15485 r = build2 (code, type, op0, op1);
15486 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15487 if (!mark_used (TREE_OPERAND (r, 1), complain)
15488 && !(complain & tf_error))
15489 return error_mark_node;
15490 return r;
15491 }
15492
15493 case EXPR_PACK_EXPANSION:
15494 error ("invalid use of pack expansion expression");
15495 return error_mark_node;
15496
15497 case NONTYPE_ARGUMENT_PACK:
15498 error ("use %<...%> to expand argument pack");
15499 return error_mark_node;
15500
15501 case VOID_CST:
15502 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15503 return t;
15504
15505 case INTEGER_CST:
15506 case REAL_CST:
15507 case STRING_CST:
15508 case COMPLEX_CST:
15509 {
15510 /* Instantiate any typedefs in the type. */
15511 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15512 r = fold_convert (type, t);
15513 gcc_assert (TREE_CODE (r) == code);
15514 return r;
15515 }
15516
15517 case PTRMEM_CST:
15518 /* These can sometimes show up in a partial instantiation, but never
15519 involve template parms. */
15520 gcc_assert (!uses_template_parms (t));
15521 return t;
15522
15523 case UNARY_LEFT_FOLD_EXPR:
15524 return tsubst_unary_left_fold (t, args, complain, in_decl);
15525 case UNARY_RIGHT_FOLD_EXPR:
15526 return tsubst_unary_right_fold (t, args, complain, in_decl);
15527 case BINARY_LEFT_FOLD_EXPR:
15528 return tsubst_binary_left_fold (t, args, complain, in_decl);
15529 case BINARY_RIGHT_FOLD_EXPR:
15530 return tsubst_binary_right_fold (t, args, complain, in_decl);
15531 case PREDICT_EXPR:
15532 return t;
15533
15534 case DEBUG_BEGIN_STMT:
15535 /* ??? There's no point in copying it for now, but maybe some
15536 day it will contain more information, such as a pointer back
15537 to the containing function, inlined copy or so. */
15538 return t;
15539
15540 default:
15541 /* We shouldn't get here, but keep going if !flag_checking. */
15542 if (flag_checking)
15543 gcc_unreachable ();
15544 return t;
15545 }
15546 }
15547
15548 /* Helper function for tsubst_omp_clauses, used for instantiation of
15549 OMP_CLAUSE_DECL of clauses. */
15550
15551 static tree
15552 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15553 tree in_decl)
15554 {
15555 if (decl == NULL_TREE)
15556 return NULL_TREE;
15557
15558 /* Handle an OpenMP array section represented as a TREE_LIST (or
15559 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15560 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15561 TREE_LIST. We can handle it exactly the same as an array section
15562 (purpose, value, and a chain), even though the nomenclature
15563 (low_bound, length, etc) is different. */
15564 if (TREE_CODE (decl) == TREE_LIST)
15565 {
15566 tree low_bound
15567 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15568 /*integral_constant_expression_p=*/false);
15569 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15570 /*integral_constant_expression_p=*/false);
15571 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15572 in_decl);
15573 if (TREE_PURPOSE (decl) == low_bound
15574 && TREE_VALUE (decl) == length
15575 && TREE_CHAIN (decl) == chain)
15576 return decl;
15577 tree ret = tree_cons (low_bound, length, chain);
15578 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15579 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15580 return ret;
15581 }
15582 tree ret = tsubst_expr (decl, args, complain, in_decl,
15583 /*integral_constant_expression_p=*/false);
15584 /* Undo convert_from_reference tsubst_expr could have called. */
15585 if (decl
15586 && REFERENCE_REF_P (ret)
15587 && !REFERENCE_REF_P (decl))
15588 ret = TREE_OPERAND (ret, 0);
15589 return ret;
15590 }
15591
15592 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15593
15594 static tree
15595 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15596 tree args, tsubst_flags_t complain, tree in_decl)
15597 {
15598 tree new_clauses = NULL_TREE, nc, oc;
15599 tree linear_no_step = NULL_TREE;
15600
15601 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15602 {
15603 nc = copy_node (oc);
15604 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15605 new_clauses = nc;
15606
15607 switch (OMP_CLAUSE_CODE (nc))
15608 {
15609 case OMP_CLAUSE_LASTPRIVATE:
15610 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15611 {
15612 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15613 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15614 in_decl, /*integral_constant_expression_p=*/false);
15615 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15616 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15617 }
15618 /* FALLTHRU */
15619 case OMP_CLAUSE_PRIVATE:
15620 case OMP_CLAUSE_SHARED:
15621 case OMP_CLAUSE_FIRSTPRIVATE:
15622 case OMP_CLAUSE_COPYIN:
15623 case OMP_CLAUSE_COPYPRIVATE:
15624 case OMP_CLAUSE_UNIFORM:
15625 case OMP_CLAUSE_DEPEND:
15626 case OMP_CLAUSE_FROM:
15627 case OMP_CLAUSE_TO:
15628 case OMP_CLAUSE_MAP:
15629 case OMP_CLAUSE_USE_DEVICE_PTR:
15630 case OMP_CLAUSE_IS_DEVICE_PTR:
15631 OMP_CLAUSE_DECL (nc)
15632 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15633 in_decl);
15634 break;
15635 case OMP_CLAUSE_TILE:
15636 case OMP_CLAUSE_IF:
15637 case OMP_CLAUSE_NUM_THREADS:
15638 case OMP_CLAUSE_SCHEDULE:
15639 case OMP_CLAUSE_COLLAPSE:
15640 case OMP_CLAUSE_FINAL:
15641 case OMP_CLAUSE_DEVICE:
15642 case OMP_CLAUSE_DIST_SCHEDULE:
15643 case OMP_CLAUSE_NUM_TEAMS:
15644 case OMP_CLAUSE_THREAD_LIMIT:
15645 case OMP_CLAUSE_SAFELEN:
15646 case OMP_CLAUSE_SIMDLEN:
15647 case OMP_CLAUSE_NUM_TASKS:
15648 case OMP_CLAUSE_GRAINSIZE:
15649 case OMP_CLAUSE_PRIORITY:
15650 case OMP_CLAUSE_ORDERED:
15651 case OMP_CLAUSE_HINT:
15652 case OMP_CLAUSE_NUM_GANGS:
15653 case OMP_CLAUSE_NUM_WORKERS:
15654 case OMP_CLAUSE_VECTOR_LENGTH:
15655 case OMP_CLAUSE_WORKER:
15656 case OMP_CLAUSE_VECTOR:
15657 case OMP_CLAUSE_ASYNC:
15658 case OMP_CLAUSE_WAIT:
15659 OMP_CLAUSE_OPERAND (nc, 0)
15660 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15661 in_decl, /*integral_constant_expression_p=*/false);
15662 break;
15663 case OMP_CLAUSE_REDUCTION:
15664 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15665 {
15666 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15667 if (TREE_CODE (placeholder) == SCOPE_REF)
15668 {
15669 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15670 complain, in_decl);
15671 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15672 = build_qualified_name (NULL_TREE, scope,
15673 TREE_OPERAND (placeholder, 1),
15674 false);
15675 }
15676 else
15677 gcc_assert (identifier_p (placeholder));
15678 }
15679 OMP_CLAUSE_DECL (nc)
15680 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15681 in_decl);
15682 break;
15683 case OMP_CLAUSE_GANG:
15684 case OMP_CLAUSE_ALIGNED:
15685 OMP_CLAUSE_DECL (nc)
15686 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15687 in_decl);
15688 OMP_CLAUSE_OPERAND (nc, 1)
15689 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15690 in_decl, /*integral_constant_expression_p=*/false);
15691 break;
15692 case OMP_CLAUSE_LINEAR:
15693 OMP_CLAUSE_DECL (nc)
15694 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15695 in_decl);
15696 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15697 {
15698 gcc_assert (!linear_no_step);
15699 linear_no_step = nc;
15700 }
15701 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15702 OMP_CLAUSE_LINEAR_STEP (nc)
15703 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15704 complain, in_decl);
15705 else
15706 OMP_CLAUSE_LINEAR_STEP (nc)
15707 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15708 in_decl,
15709 /*integral_constant_expression_p=*/false);
15710 break;
15711 case OMP_CLAUSE_NOWAIT:
15712 case OMP_CLAUSE_DEFAULT:
15713 case OMP_CLAUSE_UNTIED:
15714 case OMP_CLAUSE_MERGEABLE:
15715 case OMP_CLAUSE_INBRANCH:
15716 case OMP_CLAUSE_NOTINBRANCH:
15717 case OMP_CLAUSE_PROC_BIND:
15718 case OMP_CLAUSE_FOR:
15719 case OMP_CLAUSE_PARALLEL:
15720 case OMP_CLAUSE_SECTIONS:
15721 case OMP_CLAUSE_TASKGROUP:
15722 case OMP_CLAUSE_NOGROUP:
15723 case OMP_CLAUSE_THREADS:
15724 case OMP_CLAUSE_SIMD:
15725 case OMP_CLAUSE_DEFAULTMAP:
15726 case OMP_CLAUSE_INDEPENDENT:
15727 case OMP_CLAUSE_AUTO:
15728 case OMP_CLAUSE_SEQ:
15729 break;
15730 default:
15731 gcc_unreachable ();
15732 }
15733 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15734 switch (OMP_CLAUSE_CODE (nc))
15735 {
15736 case OMP_CLAUSE_SHARED:
15737 case OMP_CLAUSE_PRIVATE:
15738 case OMP_CLAUSE_FIRSTPRIVATE:
15739 case OMP_CLAUSE_LASTPRIVATE:
15740 case OMP_CLAUSE_COPYPRIVATE:
15741 case OMP_CLAUSE_LINEAR:
15742 case OMP_CLAUSE_REDUCTION:
15743 case OMP_CLAUSE_USE_DEVICE_PTR:
15744 case OMP_CLAUSE_IS_DEVICE_PTR:
15745 /* tsubst_expr on SCOPE_REF results in returning
15746 finish_non_static_data_member result. Undo that here. */
15747 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15748 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15749 == IDENTIFIER_NODE))
15750 {
15751 tree t = OMP_CLAUSE_DECL (nc);
15752 tree v = t;
15753 while (v)
15754 switch (TREE_CODE (v))
15755 {
15756 case COMPONENT_REF:
15757 case MEM_REF:
15758 case INDIRECT_REF:
15759 CASE_CONVERT:
15760 case POINTER_PLUS_EXPR:
15761 v = TREE_OPERAND (v, 0);
15762 continue;
15763 case PARM_DECL:
15764 if (DECL_CONTEXT (v) == current_function_decl
15765 && DECL_ARTIFICIAL (v)
15766 && DECL_NAME (v) == this_identifier)
15767 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15768 /* FALLTHRU */
15769 default:
15770 v = NULL_TREE;
15771 break;
15772 }
15773 }
15774 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15775 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15776 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15777 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15778 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15779 {
15780 tree decl = OMP_CLAUSE_DECL (nc);
15781 if (VAR_P (decl))
15782 {
15783 retrofit_lang_decl (decl);
15784 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15785 }
15786 }
15787 break;
15788 default:
15789 break;
15790 }
15791 }
15792
15793 new_clauses = nreverse (new_clauses);
15794 if (ort != C_ORT_OMP_DECLARE_SIMD)
15795 {
15796 new_clauses = finish_omp_clauses (new_clauses, ort);
15797 if (linear_no_step)
15798 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15799 if (nc == linear_no_step)
15800 {
15801 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15802 break;
15803 }
15804 }
15805 return new_clauses;
15806 }
15807
15808 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15809
15810 static tree
15811 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15812 tree in_decl)
15813 {
15814 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15815
15816 tree purpose, value, chain;
15817
15818 if (t == NULL)
15819 return t;
15820
15821 if (TREE_CODE (t) != TREE_LIST)
15822 return tsubst_copy_and_build (t, args, complain, in_decl,
15823 /*function_p=*/false,
15824 /*integral_constant_expression_p=*/false);
15825
15826 if (t == void_list_node)
15827 return t;
15828
15829 purpose = TREE_PURPOSE (t);
15830 if (purpose)
15831 purpose = RECUR (purpose);
15832 value = TREE_VALUE (t);
15833 if (value)
15834 {
15835 if (TREE_CODE (value) != LABEL_DECL)
15836 value = RECUR (value);
15837 else
15838 {
15839 value = lookup_label (DECL_NAME (value));
15840 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15841 TREE_USED (value) = 1;
15842 }
15843 }
15844 chain = TREE_CHAIN (t);
15845 if (chain && chain != void_type_node)
15846 chain = RECUR (chain);
15847 return tree_cons (purpose, value, chain);
15848 #undef RECUR
15849 }
15850
15851 /* Used to temporarily communicate the list of #pragma omp parallel
15852 clauses to #pragma omp for instantiation if they are combined
15853 together. */
15854
15855 static tree *omp_parallel_combined_clauses;
15856
15857 /* Substitute one OMP_FOR iterator. */
15858
15859 static void
15860 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15861 tree initv, tree condv, tree incrv, tree *clauses,
15862 tree args, tsubst_flags_t complain, tree in_decl,
15863 bool integral_constant_expression_p)
15864 {
15865 #define RECUR(NODE) \
15866 tsubst_expr ((NODE), args, complain, in_decl, \
15867 integral_constant_expression_p)
15868 tree decl, init, cond, incr;
15869
15870 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15871 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15872
15873 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15874 {
15875 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15876 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15877 }
15878
15879 decl = TREE_OPERAND (init, 0);
15880 init = TREE_OPERAND (init, 1);
15881 tree decl_expr = NULL_TREE;
15882 if (init && TREE_CODE (init) == DECL_EXPR)
15883 {
15884 /* We need to jump through some hoops to handle declarations in the
15885 init-statement, since we might need to handle auto deduction,
15886 but we need to keep control of initialization. */
15887 decl_expr = init;
15888 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15889 decl = tsubst_decl (decl, args, complain);
15890 }
15891 else
15892 {
15893 if (TREE_CODE (decl) == SCOPE_REF)
15894 {
15895 decl = RECUR (decl);
15896 if (TREE_CODE (decl) == COMPONENT_REF)
15897 {
15898 tree v = decl;
15899 while (v)
15900 switch (TREE_CODE (v))
15901 {
15902 case COMPONENT_REF:
15903 case MEM_REF:
15904 case INDIRECT_REF:
15905 CASE_CONVERT:
15906 case POINTER_PLUS_EXPR:
15907 v = TREE_OPERAND (v, 0);
15908 continue;
15909 case PARM_DECL:
15910 if (DECL_CONTEXT (v) == current_function_decl
15911 && DECL_ARTIFICIAL (v)
15912 && DECL_NAME (v) == this_identifier)
15913 {
15914 decl = TREE_OPERAND (decl, 1);
15915 decl = omp_privatize_field (decl, false);
15916 }
15917 /* FALLTHRU */
15918 default:
15919 v = NULL_TREE;
15920 break;
15921 }
15922 }
15923 }
15924 else
15925 decl = RECUR (decl);
15926 }
15927 init = RECUR (init);
15928
15929 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15930 if (auto_node && init)
15931 TREE_TYPE (decl)
15932 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
15933
15934 gcc_assert (!type_dependent_expression_p (decl));
15935
15936 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15937 {
15938 if (decl_expr)
15939 {
15940 /* Declare the variable, but don't let that initialize it. */
15941 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15942 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15943 RECUR (decl_expr);
15944 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15945 }
15946
15947 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15948 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15949 if (TREE_CODE (incr) == MODIFY_EXPR)
15950 {
15951 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15952 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15953 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15954 NOP_EXPR, rhs, complain);
15955 }
15956 else
15957 incr = RECUR (incr);
15958 TREE_VEC_ELT (declv, i) = decl;
15959 TREE_VEC_ELT (initv, i) = init;
15960 TREE_VEC_ELT (condv, i) = cond;
15961 TREE_VEC_ELT (incrv, i) = incr;
15962 return;
15963 }
15964
15965 if (decl_expr)
15966 {
15967 /* Declare and initialize the variable. */
15968 RECUR (decl_expr);
15969 init = NULL_TREE;
15970 }
15971 else if (init)
15972 {
15973 tree *pc;
15974 int j;
15975 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15976 {
15977 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15978 {
15979 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15980 && OMP_CLAUSE_DECL (*pc) == decl)
15981 break;
15982 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15983 && OMP_CLAUSE_DECL (*pc) == decl)
15984 {
15985 if (j)
15986 break;
15987 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15988 tree c = *pc;
15989 *pc = OMP_CLAUSE_CHAIN (c);
15990 OMP_CLAUSE_CHAIN (c) = *clauses;
15991 *clauses = c;
15992 }
15993 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15994 && OMP_CLAUSE_DECL (*pc) == decl)
15995 {
15996 error ("iteration variable %qD should not be firstprivate",
15997 decl);
15998 *pc = OMP_CLAUSE_CHAIN (*pc);
15999 }
16000 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16001 && OMP_CLAUSE_DECL (*pc) == decl)
16002 {
16003 error ("iteration variable %qD should not be reduction",
16004 decl);
16005 *pc = OMP_CLAUSE_CHAIN (*pc);
16006 }
16007 else
16008 pc = &OMP_CLAUSE_CHAIN (*pc);
16009 }
16010 if (*pc)
16011 break;
16012 }
16013 if (*pc == NULL_TREE)
16014 {
16015 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16016 OMP_CLAUSE_DECL (c) = decl;
16017 c = finish_omp_clauses (c, C_ORT_OMP);
16018 if (c)
16019 {
16020 OMP_CLAUSE_CHAIN (c) = *clauses;
16021 *clauses = c;
16022 }
16023 }
16024 }
16025 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16026 if (COMPARISON_CLASS_P (cond))
16027 {
16028 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16029 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16030 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16031 }
16032 else
16033 cond = RECUR (cond);
16034 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16035 switch (TREE_CODE (incr))
16036 {
16037 case PREINCREMENT_EXPR:
16038 case PREDECREMENT_EXPR:
16039 case POSTINCREMENT_EXPR:
16040 case POSTDECREMENT_EXPR:
16041 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16042 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16043 break;
16044 case MODIFY_EXPR:
16045 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16046 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16047 {
16048 tree rhs = TREE_OPERAND (incr, 1);
16049 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16050 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16051 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16052 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16053 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16054 rhs0, rhs1));
16055 }
16056 else
16057 incr = RECUR (incr);
16058 break;
16059 case MODOP_EXPR:
16060 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16061 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16062 {
16063 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16064 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16065 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16066 TREE_TYPE (decl), lhs,
16067 RECUR (TREE_OPERAND (incr, 2))));
16068 }
16069 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16070 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16071 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16072 {
16073 tree rhs = TREE_OPERAND (incr, 2);
16074 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16075 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16076 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16077 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16078 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16079 rhs0, rhs1));
16080 }
16081 else
16082 incr = RECUR (incr);
16083 break;
16084 default:
16085 incr = RECUR (incr);
16086 break;
16087 }
16088
16089 TREE_VEC_ELT (declv, i) = decl;
16090 TREE_VEC_ELT (initv, i) = init;
16091 TREE_VEC_ELT (condv, i) = cond;
16092 TREE_VEC_ELT (incrv, i) = incr;
16093 #undef RECUR
16094 }
16095
16096 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16097 of OMP_TARGET's body. */
16098
16099 static tree
16100 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16101 {
16102 *walk_subtrees = 0;
16103 switch (TREE_CODE (*tp))
16104 {
16105 case OMP_TEAMS:
16106 return *tp;
16107 case BIND_EXPR:
16108 case STATEMENT_LIST:
16109 *walk_subtrees = 1;
16110 break;
16111 default:
16112 break;
16113 }
16114 return NULL_TREE;
16115 }
16116
16117 /* Helper function for tsubst_expr. For decomposition declaration
16118 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16119 also the corresponding decls representing the identifiers
16120 of the decomposition declaration. Return DECL if successful
16121 or error_mark_node otherwise, set *FIRST to the first decl
16122 in the list chained through DECL_CHAIN and *CNT to the number
16123 of such decls. */
16124
16125 static tree
16126 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16127 tsubst_flags_t complain, tree in_decl, tree *first,
16128 unsigned int *cnt)
16129 {
16130 tree decl2, decl3, prev = decl;
16131 *cnt = 0;
16132 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16133 for (decl2 = DECL_CHAIN (pattern_decl);
16134 decl2
16135 && VAR_P (decl2)
16136 && DECL_DECOMPOSITION_P (decl2)
16137 && DECL_NAME (decl2);
16138 decl2 = DECL_CHAIN (decl2))
16139 {
16140 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16141 {
16142 gcc_assert (errorcount);
16143 return error_mark_node;
16144 }
16145 (*cnt)++;
16146 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16147 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16148 tree v = DECL_VALUE_EXPR (decl2);
16149 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16150 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16151 decl3 = tsubst (decl2, args, complain, in_decl);
16152 SET_DECL_VALUE_EXPR (decl2, v);
16153 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16154 if (VAR_P (decl3))
16155 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16156 maybe_push_decl (decl3);
16157 if (error_operand_p (decl3))
16158 decl = error_mark_node;
16159 else if (decl != error_mark_node
16160 && DECL_CHAIN (decl3) != prev)
16161 {
16162 gcc_assert (errorcount);
16163 decl = error_mark_node;
16164 }
16165 else
16166 prev = decl3;
16167 }
16168 *first = prev;
16169 return decl;
16170 }
16171
16172 /* Like tsubst_copy for expressions, etc. but also does semantic
16173 processing. */
16174
16175 tree
16176 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16177 bool integral_constant_expression_p)
16178 {
16179 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16180 #define RECUR(NODE) \
16181 tsubst_expr ((NODE), args, complain, in_decl, \
16182 integral_constant_expression_p)
16183
16184 tree stmt, tmp;
16185 tree r;
16186 location_t loc;
16187
16188 if (t == NULL_TREE || t == error_mark_node)
16189 return t;
16190
16191 loc = input_location;
16192 if (EXPR_HAS_LOCATION (t))
16193 input_location = EXPR_LOCATION (t);
16194 if (STATEMENT_CODE_P (TREE_CODE (t)))
16195 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16196
16197 switch (TREE_CODE (t))
16198 {
16199 case STATEMENT_LIST:
16200 {
16201 tree_stmt_iterator i;
16202 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16203 RECUR (tsi_stmt (i));
16204 break;
16205 }
16206
16207 case CTOR_INITIALIZER:
16208 finish_mem_initializers (tsubst_initializer_list
16209 (TREE_OPERAND (t, 0), args));
16210 break;
16211
16212 case RETURN_EXPR:
16213 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16214 break;
16215
16216 case EXPR_STMT:
16217 tmp = RECUR (EXPR_STMT_EXPR (t));
16218 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16219 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16220 else
16221 finish_expr_stmt (tmp);
16222 break;
16223
16224 case USING_STMT:
16225 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16226 /*attribs=*/NULL_TREE);
16227 break;
16228
16229 case DECL_EXPR:
16230 {
16231 tree decl, pattern_decl;
16232 tree init;
16233
16234 pattern_decl = decl = DECL_EXPR_DECL (t);
16235 if (TREE_CODE (decl) == LABEL_DECL)
16236 finish_label_decl (DECL_NAME (decl));
16237 else if (TREE_CODE (decl) == USING_DECL)
16238 {
16239 tree scope = USING_DECL_SCOPE (decl);
16240 tree name = DECL_NAME (decl);
16241
16242 scope = tsubst (scope, args, complain, in_decl);
16243 decl = lookup_qualified_name (scope, name,
16244 /*is_type_p=*/false,
16245 /*complain=*/false);
16246 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16247 qualified_name_lookup_error (scope, name, decl, input_location);
16248 else
16249 finish_local_using_decl (decl, scope, name);
16250 }
16251 else if (is_capture_proxy (decl)
16252 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16253 {
16254 /* We're in tsubst_lambda_expr, we've already inserted a new
16255 capture proxy, so look it up and register it. */
16256 tree inst;
16257 if (DECL_PACK_P (decl))
16258 {
16259 inst = (retrieve_local_specialization
16260 (DECL_CAPTURED_VARIABLE (decl)));
16261 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16262 }
16263 else
16264 {
16265 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16266 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16267 gcc_assert (inst != decl && is_capture_proxy (inst));
16268 }
16269 register_local_specialization (inst, decl);
16270 break;
16271 }
16272 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16273 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16274 /* Don't copy the old closure; we'll create a new one in
16275 tsubst_lambda_expr. */
16276 break;
16277 else
16278 {
16279 init = DECL_INITIAL (decl);
16280 decl = tsubst (decl, args, complain, in_decl);
16281 if (decl != error_mark_node)
16282 {
16283 /* By marking the declaration as instantiated, we avoid
16284 trying to instantiate it. Since instantiate_decl can't
16285 handle local variables, and since we've already done
16286 all that needs to be done, that's the right thing to
16287 do. */
16288 if (VAR_P (decl))
16289 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16290 if (VAR_P (decl)
16291 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16292 /* Anonymous aggregates are a special case. */
16293 finish_anon_union (decl);
16294 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16295 {
16296 DECL_CONTEXT (decl) = current_function_decl;
16297 if (DECL_NAME (decl) == this_identifier)
16298 {
16299 tree lam = DECL_CONTEXT (current_function_decl);
16300 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16301 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16302 }
16303 insert_capture_proxy (decl);
16304 }
16305 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16306 /* We already did a pushtag. */;
16307 else if (TREE_CODE (decl) == FUNCTION_DECL
16308 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16309 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16310 {
16311 DECL_CONTEXT (decl) = NULL_TREE;
16312 pushdecl (decl);
16313 DECL_CONTEXT (decl) = current_function_decl;
16314 cp_check_omp_declare_reduction (decl);
16315 }
16316 else
16317 {
16318 int const_init = false;
16319 maybe_push_decl (decl);
16320 if (VAR_P (decl)
16321 && DECL_PRETTY_FUNCTION_P (decl))
16322 {
16323 /* For __PRETTY_FUNCTION__ we have to adjust the
16324 initializer. */
16325 const char *const name
16326 = cxx_printable_name (current_function_decl, 2);
16327 init = cp_fname_init (name, &TREE_TYPE (decl));
16328 }
16329 else
16330 init = tsubst_init (init, decl, args, complain, in_decl);
16331
16332 if (VAR_P (decl))
16333 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16334 (pattern_decl));
16335 if (VAR_P (decl)
16336 && DECL_DECOMPOSITION_P (decl)
16337 && TREE_TYPE (pattern_decl) != error_mark_node)
16338 {
16339 unsigned int cnt;
16340 tree first;
16341 tree ndecl
16342 = tsubst_decomp_names (decl, pattern_decl, args,
16343 complain, in_decl, &first, &cnt);
16344 if (ndecl != error_mark_node)
16345 cp_maybe_mangle_decomp (ndecl, first, cnt);
16346 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16347 if (ndecl != error_mark_node)
16348 cp_finish_decomp (ndecl, first, cnt);
16349 }
16350 else
16351 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16352 }
16353 }
16354 }
16355
16356 break;
16357 }
16358
16359 case FOR_STMT:
16360 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16361 RECUR (FOR_INIT_STMT (t));
16362 finish_init_stmt (stmt);
16363 tmp = RECUR (FOR_COND (t));
16364 finish_for_cond (tmp, stmt, false, 0);
16365 tmp = RECUR (FOR_EXPR (t));
16366 finish_for_expr (tmp, stmt);
16367 {
16368 bool prev = note_iteration_stmt_body_start ();
16369 RECUR (FOR_BODY (t));
16370 note_iteration_stmt_body_end (prev);
16371 }
16372 finish_for_stmt (stmt);
16373 break;
16374
16375 case RANGE_FOR_STMT:
16376 {
16377 /* Construct another range_for, if this is not a final
16378 substitution (for inside inside a generic lambda of a
16379 template). Otherwise convert to a regular for. */
16380 tree decl, expr;
16381 stmt = (processing_template_decl
16382 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16383 : begin_for_stmt (NULL_TREE, NULL_TREE));
16384 decl = RANGE_FOR_DECL (t);
16385 decl = tsubst (decl, args, complain, in_decl);
16386 maybe_push_decl (decl);
16387 expr = RECUR (RANGE_FOR_EXPR (t));
16388
16389 tree decomp_first = NULL_TREE;
16390 unsigned decomp_cnt = 0;
16391 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16392 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16393 complain, in_decl,
16394 &decomp_first, &decomp_cnt);
16395
16396 if (processing_template_decl)
16397 {
16398 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16399 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16400 finish_range_for_decl (stmt, decl, expr);
16401 }
16402 else
16403 {
16404 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16405 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16406 stmt = cp_convert_range_for (stmt, decl, expr,
16407 decomp_first, decomp_cnt,
16408 RANGE_FOR_IVDEP (t), unroll);
16409 }
16410
16411 bool prev = note_iteration_stmt_body_start ();
16412 RECUR (RANGE_FOR_BODY (t));
16413 note_iteration_stmt_body_end (prev);
16414 finish_for_stmt (stmt);
16415 }
16416 break;
16417
16418 case WHILE_STMT:
16419 stmt = begin_while_stmt ();
16420 tmp = RECUR (WHILE_COND (t));
16421 finish_while_stmt_cond (tmp, stmt, false, 0);
16422 {
16423 bool prev = note_iteration_stmt_body_start ();
16424 RECUR (WHILE_BODY (t));
16425 note_iteration_stmt_body_end (prev);
16426 }
16427 finish_while_stmt (stmt);
16428 break;
16429
16430 case DO_STMT:
16431 stmt = begin_do_stmt ();
16432 {
16433 bool prev = note_iteration_stmt_body_start ();
16434 RECUR (DO_BODY (t));
16435 note_iteration_stmt_body_end (prev);
16436 }
16437 finish_do_body (stmt);
16438 tmp = RECUR (DO_COND (t));
16439 finish_do_stmt (tmp, stmt, false, 0);
16440 break;
16441
16442 case IF_STMT:
16443 stmt = begin_if_stmt ();
16444 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16445 tmp = RECUR (IF_COND (t));
16446 tmp = finish_if_stmt_cond (tmp, stmt);
16447 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16448 /* Don't instantiate the THEN_CLAUSE. */;
16449 else
16450 {
16451 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16452 if (inhibit)
16453 ++c_inhibit_evaluation_warnings;
16454 RECUR (THEN_CLAUSE (t));
16455 if (inhibit)
16456 --c_inhibit_evaluation_warnings;
16457 }
16458 finish_then_clause (stmt);
16459
16460 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16461 /* Don't instantiate the ELSE_CLAUSE. */;
16462 else if (ELSE_CLAUSE (t))
16463 {
16464 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16465 begin_else_clause (stmt);
16466 if (inhibit)
16467 ++c_inhibit_evaluation_warnings;
16468 RECUR (ELSE_CLAUSE (t));
16469 if (inhibit)
16470 --c_inhibit_evaluation_warnings;
16471 finish_else_clause (stmt);
16472 }
16473
16474 finish_if_stmt (stmt);
16475 break;
16476
16477 case BIND_EXPR:
16478 if (BIND_EXPR_BODY_BLOCK (t))
16479 stmt = begin_function_body ();
16480 else
16481 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16482 ? BCS_TRY_BLOCK : 0);
16483
16484 RECUR (BIND_EXPR_BODY (t));
16485
16486 if (BIND_EXPR_BODY_BLOCK (t))
16487 finish_function_body (stmt);
16488 else
16489 finish_compound_stmt (stmt);
16490 break;
16491
16492 case BREAK_STMT:
16493 finish_break_stmt ();
16494 break;
16495
16496 case CONTINUE_STMT:
16497 finish_continue_stmt ();
16498 break;
16499
16500 case SWITCH_STMT:
16501 stmt = begin_switch_stmt ();
16502 tmp = RECUR (SWITCH_STMT_COND (t));
16503 finish_switch_cond (tmp, stmt);
16504 RECUR (SWITCH_STMT_BODY (t));
16505 finish_switch_stmt (stmt);
16506 break;
16507
16508 case CASE_LABEL_EXPR:
16509 {
16510 tree low = RECUR (CASE_LOW (t));
16511 tree high = RECUR (CASE_HIGH (t));
16512 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16513 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16514 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16515 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16516 }
16517 break;
16518
16519 case LABEL_EXPR:
16520 {
16521 tree decl = LABEL_EXPR_LABEL (t);
16522 tree label;
16523
16524 label = finish_label_stmt (DECL_NAME (decl));
16525 if (TREE_CODE (label) == LABEL_DECL)
16526 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16527 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16528 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16529 }
16530 break;
16531
16532 case GOTO_EXPR:
16533 tmp = GOTO_DESTINATION (t);
16534 if (TREE_CODE (tmp) != LABEL_DECL)
16535 /* Computed goto's must be tsubst'd into. On the other hand,
16536 non-computed gotos must not be; the identifier in question
16537 will have no binding. */
16538 tmp = RECUR (tmp);
16539 else
16540 tmp = DECL_NAME (tmp);
16541 finish_goto_stmt (tmp);
16542 break;
16543
16544 case ASM_EXPR:
16545 {
16546 tree string = RECUR (ASM_STRING (t));
16547 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16548 complain, in_decl);
16549 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16550 complain, in_decl);
16551 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16552 complain, in_decl);
16553 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16554 complain, in_decl);
16555 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16556 clobbers, labels);
16557 tree asm_expr = tmp;
16558 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16559 asm_expr = TREE_OPERAND (asm_expr, 0);
16560 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16561 }
16562 break;
16563
16564 case TRY_BLOCK:
16565 if (CLEANUP_P (t))
16566 {
16567 stmt = begin_try_block ();
16568 RECUR (TRY_STMTS (t));
16569 finish_cleanup_try_block (stmt);
16570 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16571 }
16572 else
16573 {
16574 tree compound_stmt = NULL_TREE;
16575
16576 if (FN_TRY_BLOCK_P (t))
16577 stmt = begin_function_try_block (&compound_stmt);
16578 else
16579 stmt = begin_try_block ();
16580
16581 RECUR (TRY_STMTS (t));
16582
16583 if (FN_TRY_BLOCK_P (t))
16584 finish_function_try_block (stmt);
16585 else
16586 finish_try_block (stmt);
16587
16588 RECUR (TRY_HANDLERS (t));
16589 if (FN_TRY_BLOCK_P (t))
16590 finish_function_handler_sequence (stmt, compound_stmt);
16591 else
16592 finish_handler_sequence (stmt);
16593 }
16594 break;
16595
16596 case HANDLER:
16597 {
16598 tree decl = HANDLER_PARMS (t);
16599
16600 if (decl)
16601 {
16602 decl = tsubst (decl, args, complain, in_decl);
16603 /* Prevent instantiate_decl from trying to instantiate
16604 this variable. We've already done all that needs to be
16605 done. */
16606 if (decl != error_mark_node)
16607 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16608 }
16609 stmt = begin_handler ();
16610 finish_handler_parms (decl, stmt);
16611 RECUR (HANDLER_BODY (t));
16612 finish_handler (stmt);
16613 }
16614 break;
16615
16616 case TAG_DEFN:
16617 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16618 if (CLASS_TYPE_P (tmp))
16619 {
16620 /* Local classes are not independent templates; they are
16621 instantiated along with their containing function. And this
16622 way we don't have to deal with pushing out of one local class
16623 to instantiate a member of another local class. */
16624 /* Closures are handled by the LAMBDA_EXPR. */
16625 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16626 complete_type (tmp);
16627 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16628 if ((VAR_P (fld)
16629 || (TREE_CODE (fld) == FUNCTION_DECL
16630 && !DECL_ARTIFICIAL (fld)))
16631 && DECL_TEMPLATE_INSTANTIATION (fld))
16632 instantiate_decl (fld, /*defer_ok=*/false,
16633 /*expl_inst_class=*/false);
16634 }
16635 break;
16636
16637 case STATIC_ASSERT:
16638 {
16639 tree condition;
16640
16641 ++c_inhibit_evaluation_warnings;
16642 condition =
16643 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16644 args,
16645 complain, in_decl,
16646 /*integral_constant_expression_p=*/true);
16647 --c_inhibit_evaluation_warnings;
16648
16649 finish_static_assert (condition,
16650 STATIC_ASSERT_MESSAGE (t),
16651 STATIC_ASSERT_SOURCE_LOCATION (t),
16652 /*member_p=*/false);
16653 }
16654 break;
16655
16656 case OACC_KERNELS:
16657 case OACC_PARALLEL:
16658 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16659 in_decl);
16660 stmt = begin_omp_parallel ();
16661 RECUR (OMP_BODY (t));
16662 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16663 break;
16664
16665 case OMP_PARALLEL:
16666 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16667 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16668 complain, in_decl);
16669 if (OMP_PARALLEL_COMBINED (t))
16670 omp_parallel_combined_clauses = &tmp;
16671 stmt = begin_omp_parallel ();
16672 RECUR (OMP_PARALLEL_BODY (t));
16673 gcc_assert (omp_parallel_combined_clauses == NULL);
16674 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16675 = OMP_PARALLEL_COMBINED (t);
16676 pop_omp_privatization_clauses (r);
16677 break;
16678
16679 case OMP_TASK:
16680 r = push_omp_privatization_clauses (false);
16681 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16682 complain, in_decl);
16683 stmt = begin_omp_task ();
16684 RECUR (OMP_TASK_BODY (t));
16685 finish_omp_task (tmp, stmt);
16686 pop_omp_privatization_clauses (r);
16687 break;
16688
16689 case OMP_FOR:
16690 case OMP_SIMD:
16691 case OMP_DISTRIBUTE:
16692 case OMP_TASKLOOP:
16693 case OACC_LOOP:
16694 {
16695 tree clauses, body, pre_body;
16696 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16697 tree orig_declv = NULL_TREE;
16698 tree incrv = NULL_TREE;
16699 enum c_omp_region_type ort = C_ORT_OMP;
16700 int i;
16701
16702 if (TREE_CODE (t) == OACC_LOOP)
16703 ort = C_ORT_ACC;
16704
16705 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16706 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16707 in_decl);
16708 if (OMP_FOR_INIT (t) != NULL_TREE)
16709 {
16710 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16711 if (OMP_FOR_ORIG_DECLS (t))
16712 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16713 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16714 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16715 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16716 }
16717
16718 stmt = begin_omp_structured_block ();
16719
16720 pre_body = push_stmt_list ();
16721 RECUR (OMP_FOR_PRE_BODY (t));
16722 pre_body = pop_stmt_list (pre_body);
16723
16724 if (OMP_FOR_INIT (t) != NULL_TREE)
16725 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16726 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16727 incrv, &clauses, args, complain, in_decl,
16728 integral_constant_expression_p);
16729 omp_parallel_combined_clauses = NULL;
16730
16731 body = push_stmt_list ();
16732 RECUR (OMP_FOR_BODY (t));
16733 body = pop_stmt_list (body);
16734
16735 if (OMP_FOR_INIT (t) != NULL_TREE)
16736 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16737 orig_declv, initv, condv, incrv, body, pre_body,
16738 NULL, clauses);
16739 else
16740 {
16741 t = make_node (TREE_CODE (t));
16742 TREE_TYPE (t) = void_type_node;
16743 OMP_FOR_BODY (t) = body;
16744 OMP_FOR_PRE_BODY (t) = pre_body;
16745 OMP_FOR_CLAUSES (t) = clauses;
16746 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16747 add_stmt (t);
16748 }
16749
16750 add_stmt (finish_omp_structured_block (stmt));
16751 pop_omp_privatization_clauses (r);
16752 }
16753 break;
16754
16755 case OMP_SECTIONS:
16756 omp_parallel_combined_clauses = NULL;
16757 /* FALLTHRU */
16758 case OMP_SINGLE:
16759 case OMP_TEAMS:
16760 case OMP_CRITICAL:
16761 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16762 && OMP_TEAMS_COMBINED (t));
16763 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16764 in_decl);
16765 stmt = push_stmt_list ();
16766 RECUR (OMP_BODY (t));
16767 stmt = pop_stmt_list (stmt);
16768
16769 t = copy_node (t);
16770 OMP_BODY (t) = stmt;
16771 OMP_CLAUSES (t) = tmp;
16772 add_stmt (t);
16773 pop_omp_privatization_clauses (r);
16774 break;
16775
16776 case OACC_DATA:
16777 case OMP_TARGET_DATA:
16778 case OMP_TARGET:
16779 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16780 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16781 in_decl);
16782 keep_next_level (true);
16783 stmt = begin_omp_structured_block ();
16784
16785 RECUR (OMP_BODY (t));
16786 stmt = finish_omp_structured_block (stmt);
16787
16788 t = copy_node (t);
16789 OMP_BODY (t) = stmt;
16790 OMP_CLAUSES (t) = tmp;
16791 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16792 {
16793 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16794 if (teams)
16795 {
16796 /* For combined target teams, ensure the num_teams and
16797 thread_limit clause expressions are evaluated on the host,
16798 before entering the target construct. */
16799 tree c;
16800 for (c = OMP_TEAMS_CLAUSES (teams);
16801 c; c = OMP_CLAUSE_CHAIN (c))
16802 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16803 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16804 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16805 {
16806 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16807 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16808 if (expr == error_mark_node)
16809 continue;
16810 tmp = TARGET_EXPR_SLOT (expr);
16811 add_stmt (expr);
16812 OMP_CLAUSE_OPERAND (c, 0) = expr;
16813 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16814 OMP_CLAUSE_FIRSTPRIVATE);
16815 OMP_CLAUSE_DECL (tc) = tmp;
16816 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16817 OMP_TARGET_CLAUSES (t) = tc;
16818 }
16819 }
16820 }
16821 add_stmt (t);
16822 break;
16823
16824 case OACC_DECLARE:
16825 t = copy_node (t);
16826 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16827 complain, in_decl);
16828 OACC_DECLARE_CLAUSES (t) = tmp;
16829 add_stmt (t);
16830 break;
16831
16832 case OMP_TARGET_UPDATE:
16833 case OMP_TARGET_ENTER_DATA:
16834 case OMP_TARGET_EXIT_DATA:
16835 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16836 complain, in_decl);
16837 t = copy_node (t);
16838 OMP_STANDALONE_CLAUSES (t) = tmp;
16839 add_stmt (t);
16840 break;
16841
16842 case OACC_ENTER_DATA:
16843 case OACC_EXIT_DATA:
16844 case OACC_UPDATE:
16845 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16846 complain, in_decl);
16847 t = copy_node (t);
16848 OMP_STANDALONE_CLAUSES (t) = tmp;
16849 add_stmt (t);
16850 break;
16851
16852 case OMP_ORDERED:
16853 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16854 complain, in_decl);
16855 stmt = push_stmt_list ();
16856 RECUR (OMP_BODY (t));
16857 stmt = pop_stmt_list (stmt);
16858
16859 t = copy_node (t);
16860 OMP_BODY (t) = stmt;
16861 OMP_ORDERED_CLAUSES (t) = tmp;
16862 add_stmt (t);
16863 break;
16864
16865 case OMP_SECTION:
16866 case OMP_MASTER:
16867 case OMP_TASKGROUP:
16868 stmt = push_stmt_list ();
16869 RECUR (OMP_BODY (t));
16870 stmt = pop_stmt_list (stmt);
16871
16872 t = copy_node (t);
16873 OMP_BODY (t) = stmt;
16874 add_stmt (t);
16875 break;
16876
16877 case OMP_ATOMIC:
16878 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16879 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16880 {
16881 tree op1 = TREE_OPERAND (t, 1);
16882 tree rhs1 = NULL_TREE;
16883 tree lhs, rhs;
16884 if (TREE_CODE (op1) == COMPOUND_EXPR)
16885 {
16886 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16887 op1 = TREE_OPERAND (op1, 1);
16888 }
16889 lhs = RECUR (TREE_OPERAND (op1, 0));
16890 rhs = RECUR (TREE_OPERAND (op1, 1));
16891 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16892 NULL_TREE, NULL_TREE, rhs1,
16893 OMP_ATOMIC_SEQ_CST (t));
16894 }
16895 else
16896 {
16897 tree op1 = TREE_OPERAND (t, 1);
16898 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16899 tree rhs1 = NULL_TREE;
16900 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16901 enum tree_code opcode = NOP_EXPR;
16902 if (code == OMP_ATOMIC_READ)
16903 {
16904 v = RECUR (TREE_OPERAND (op1, 0));
16905 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16906 }
16907 else if (code == OMP_ATOMIC_CAPTURE_OLD
16908 || code == OMP_ATOMIC_CAPTURE_NEW)
16909 {
16910 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16911 v = RECUR (TREE_OPERAND (op1, 0));
16912 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16913 if (TREE_CODE (op11) == COMPOUND_EXPR)
16914 {
16915 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16916 op11 = TREE_OPERAND (op11, 1);
16917 }
16918 lhs = RECUR (TREE_OPERAND (op11, 0));
16919 rhs = RECUR (TREE_OPERAND (op11, 1));
16920 opcode = TREE_CODE (op11);
16921 if (opcode == MODIFY_EXPR)
16922 opcode = NOP_EXPR;
16923 }
16924 else
16925 {
16926 code = OMP_ATOMIC;
16927 lhs = RECUR (TREE_OPERAND (op1, 0));
16928 rhs = RECUR (TREE_OPERAND (op1, 1));
16929 }
16930 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16931 OMP_ATOMIC_SEQ_CST (t));
16932 }
16933 break;
16934
16935 case TRANSACTION_EXPR:
16936 {
16937 int flags = 0;
16938 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16939 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16940
16941 if (TRANSACTION_EXPR_IS_STMT (t))
16942 {
16943 tree body = TRANSACTION_EXPR_BODY (t);
16944 tree noex = NULL_TREE;
16945 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16946 {
16947 noex = MUST_NOT_THROW_COND (body);
16948 if (noex == NULL_TREE)
16949 noex = boolean_true_node;
16950 body = TREE_OPERAND (body, 0);
16951 }
16952 stmt = begin_transaction_stmt (input_location, NULL, flags);
16953 RECUR (body);
16954 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16955 }
16956 else
16957 {
16958 stmt = build_transaction_expr (EXPR_LOCATION (t),
16959 RECUR (TRANSACTION_EXPR_BODY (t)),
16960 flags, NULL_TREE);
16961 RETURN (stmt);
16962 }
16963 }
16964 break;
16965
16966 case MUST_NOT_THROW_EXPR:
16967 {
16968 tree op0 = RECUR (TREE_OPERAND (t, 0));
16969 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16970 RETURN (build_must_not_throw_expr (op0, cond));
16971 }
16972
16973 case EXPR_PACK_EXPANSION:
16974 error ("invalid use of pack expansion expression");
16975 RETURN (error_mark_node);
16976
16977 case NONTYPE_ARGUMENT_PACK:
16978 error ("use %<...%> to expand argument pack");
16979 RETURN (error_mark_node);
16980
16981 case COMPOUND_EXPR:
16982 tmp = RECUR (TREE_OPERAND (t, 0));
16983 if (tmp == NULL_TREE)
16984 /* If the first operand was a statement, we're done with it. */
16985 RETURN (RECUR (TREE_OPERAND (t, 1)));
16986 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16987 RECUR (TREE_OPERAND (t, 1)),
16988 complain));
16989
16990 case ANNOTATE_EXPR:
16991 tmp = RECUR (TREE_OPERAND (t, 0));
16992 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16993 TREE_TYPE (tmp), tmp,
16994 RECUR (TREE_OPERAND (t, 1)),
16995 RECUR (TREE_OPERAND (t, 2))));
16996
16997 default:
16998 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16999
17000 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17001 /*function_p=*/false,
17002 integral_constant_expression_p));
17003 }
17004
17005 RETURN (NULL_TREE);
17006 out:
17007 input_location = loc;
17008 return r;
17009 #undef RECUR
17010 #undef RETURN
17011 }
17012
17013 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17014 function. For description of the body see comment above
17015 cp_parser_omp_declare_reduction_exprs. */
17016
17017 static void
17018 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17019 {
17020 if (t == NULL_TREE || t == error_mark_node)
17021 return;
17022
17023 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17024
17025 tree_stmt_iterator tsi;
17026 int i;
17027 tree stmts[7];
17028 memset (stmts, 0, sizeof stmts);
17029 for (i = 0, tsi = tsi_start (t);
17030 i < 7 && !tsi_end_p (tsi);
17031 i++, tsi_next (&tsi))
17032 stmts[i] = tsi_stmt (tsi);
17033 gcc_assert (tsi_end_p (tsi));
17034
17035 if (i >= 3)
17036 {
17037 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17038 && TREE_CODE (stmts[1]) == DECL_EXPR);
17039 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17040 args, complain, in_decl);
17041 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17042 args, complain, in_decl);
17043 DECL_CONTEXT (omp_out) = current_function_decl;
17044 DECL_CONTEXT (omp_in) = current_function_decl;
17045 keep_next_level (true);
17046 tree block = begin_omp_structured_block ();
17047 tsubst_expr (stmts[2], args, complain, in_decl, false);
17048 block = finish_omp_structured_block (block);
17049 block = maybe_cleanup_point_expr_void (block);
17050 add_decl_expr (omp_out);
17051 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17052 TREE_NO_WARNING (omp_out) = 1;
17053 add_decl_expr (omp_in);
17054 finish_expr_stmt (block);
17055 }
17056 if (i >= 6)
17057 {
17058 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17059 && TREE_CODE (stmts[4]) == DECL_EXPR);
17060 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17061 args, complain, in_decl);
17062 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17063 args, complain, in_decl);
17064 DECL_CONTEXT (omp_priv) = current_function_decl;
17065 DECL_CONTEXT (omp_orig) = current_function_decl;
17066 keep_next_level (true);
17067 tree block = begin_omp_structured_block ();
17068 tsubst_expr (stmts[5], args, complain, in_decl, false);
17069 block = finish_omp_structured_block (block);
17070 block = maybe_cleanup_point_expr_void (block);
17071 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17072 add_decl_expr (omp_priv);
17073 add_decl_expr (omp_orig);
17074 finish_expr_stmt (block);
17075 if (i == 7)
17076 add_decl_expr (omp_orig);
17077 }
17078 }
17079
17080 /* T is a postfix-expression that is not being used in a function
17081 call. Return the substituted version of T. */
17082
17083 static tree
17084 tsubst_non_call_postfix_expression (tree t, tree args,
17085 tsubst_flags_t complain,
17086 tree in_decl)
17087 {
17088 if (TREE_CODE (t) == SCOPE_REF)
17089 t = tsubst_qualified_id (t, args, complain, in_decl,
17090 /*done=*/false, /*address_p=*/false);
17091 else
17092 t = tsubst_copy_and_build (t, args, complain, in_decl,
17093 /*function_p=*/false,
17094 /*integral_constant_expression_p=*/false);
17095
17096 return t;
17097 }
17098
17099 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17100 instantiation context. Instantiating a pack expansion containing a lambda
17101 might result in multiple lambdas all based on the same lambda in the
17102 template. */
17103
17104 tree
17105 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17106 {
17107 tree oldfn = lambda_function (t);
17108 in_decl = oldfn;
17109
17110 tree r = build_lambda_expr ();
17111
17112 LAMBDA_EXPR_LOCATION (r)
17113 = LAMBDA_EXPR_LOCATION (t);
17114 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17115 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17116 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17117
17118 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17119 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
17120 else
17121 record_lambda_scope (r);
17122
17123 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17124 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17125
17126 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17127 cap = TREE_CHAIN (cap))
17128 {
17129 tree field = TREE_PURPOSE (cap);
17130 if (PACK_EXPANSION_P (field))
17131 field = PACK_EXPANSION_PATTERN (field);
17132 field = tsubst_decl (field, args, complain);
17133
17134 if (field == error_mark_node)
17135 return error_mark_node;
17136
17137 tree init = TREE_VALUE (cap);
17138 if (PACK_EXPANSION_P (init))
17139 init = tsubst_pack_expansion (init, args, complain, in_decl);
17140 else
17141 init = tsubst_copy_and_build (init, args, complain, in_decl,
17142 /*fn*/false, /*constexpr*/false);
17143
17144 if (TREE_CODE (field) == TREE_VEC)
17145 {
17146 int len = TREE_VEC_LENGTH (field);
17147 gcc_assert (TREE_CODE (init) == TREE_VEC
17148 && TREE_VEC_LENGTH (init) == len);
17149 for (int i = 0; i < len; ++i)
17150 LAMBDA_EXPR_CAPTURE_LIST (r)
17151 = tree_cons (TREE_VEC_ELT (field, i),
17152 TREE_VEC_ELT (init, i),
17153 LAMBDA_EXPR_CAPTURE_LIST (r));
17154 }
17155 else
17156 {
17157 LAMBDA_EXPR_CAPTURE_LIST (r)
17158 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17159
17160 if (id_equal (DECL_NAME (field), "__this"))
17161 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17162 }
17163 }
17164
17165 tree type = begin_lambda_type (r);
17166
17167 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17168 determine_visibility (TYPE_NAME (type));
17169
17170 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17171
17172 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17173 ? DECL_TI_TEMPLATE (oldfn)
17174 : NULL_TREE);
17175
17176 tree fntype = static_fn_type (oldfn);
17177 if (oldtmpl)
17178 ++processing_template_decl;
17179 fntype = tsubst (fntype, args, complain, in_decl);
17180 if (oldtmpl)
17181 --processing_template_decl;
17182
17183 if (fntype == error_mark_node)
17184 r = error_mark_node;
17185 else
17186 {
17187 /* Fix the type of 'this'. */
17188 fntype = build_memfn_type (fntype, type,
17189 type_memfn_quals (fntype),
17190 type_memfn_rqual (fntype));
17191 tree fn, tmpl;
17192 if (oldtmpl)
17193 {
17194 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17195 fn = DECL_TEMPLATE_RESULT (tmpl);
17196 finish_member_declaration (tmpl);
17197 }
17198 else
17199 {
17200 tmpl = NULL_TREE;
17201 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17202 finish_member_declaration (fn);
17203 }
17204
17205 /* Let finish_function set this. */
17206 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17207
17208 bool nested = cfun;
17209 if (nested)
17210 push_function_context ();
17211 else
17212 /* Still increment function_depth so that we don't GC in the
17213 middle of an expression. */
17214 ++function_depth;
17215
17216 local_specialization_stack s (lss_copy);
17217
17218 tree body = start_lambda_function (fn, r);
17219
17220 register_parameter_specializations (oldfn, fn);
17221
17222 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17223 /*constexpr*/false);
17224
17225 finish_lambda_function (body);
17226
17227 if (nested)
17228 pop_function_context ();
17229 else
17230 --function_depth;
17231
17232 /* The capture list was built up in reverse order; fix that now. */
17233 LAMBDA_EXPR_CAPTURE_LIST (r)
17234 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17235
17236 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17237
17238 maybe_add_lambda_conv_op (type);
17239 }
17240
17241 finish_struct (type, /*attr*/NULL_TREE);
17242
17243 insert_pending_capture_proxies ();
17244
17245 return r;
17246 }
17247
17248 /* Like tsubst but deals with expressions and performs semantic
17249 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17250
17251 tree
17252 tsubst_copy_and_build (tree t,
17253 tree args,
17254 tsubst_flags_t complain,
17255 tree in_decl,
17256 bool function_p,
17257 bool integral_constant_expression_p)
17258 {
17259 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17260 #define RECUR(NODE) \
17261 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17262 /*function_p=*/false, \
17263 integral_constant_expression_p)
17264
17265 tree retval, op1;
17266 location_t loc;
17267
17268 if (t == NULL_TREE || t == error_mark_node)
17269 return t;
17270
17271 loc = input_location;
17272 if (EXPR_HAS_LOCATION (t))
17273 input_location = EXPR_LOCATION (t);
17274
17275 /* N3276 decltype magic only applies to calls at the top level or on the
17276 right side of a comma. */
17277 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17278 complain &= ~tf_decltype;
17279
17280 switch (TREE_CODE (t))
17281 {
17282 case USING_DECL:
17283 t = DECL_NAME (t);
17284 /* Fall through. */
17285 case IDENTIFIER_NODE:
17286 {
17287 tree decl;
17288 cp_id_kind idk;
17289 bool non_integral_constant_expression_p;
17290 const char *error_msg;
17291
17292 if (IDENTIFIER_CONV_OP_P (t))
17293 {
17294 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17295 t = make_conv_op_name (new_type);
17296 }
17297
17298 /* Look up the name. */
17299 decl = lookup_name (t);
17300
17301 /* By convention, expressions use ERROR_MARK_NODE to indicate
17302 failure, not NULL_TREE. */
17303 if (decl == NULL_TREE)
17304 decl = error_mark_node;
17305
17306 decl = finish_id_expression (t, decl, NULL_TREE,
17307 &idk,
17308 integral_constant_expression_p,
17309 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17310 &non_integral_constant_expression_p,
17311 /*template_p=*/false,
17312 /*done=*/true,
17313 /*address_p=*/false,
17314 /*template_arg_p=*/false,
17315 &error_msg,
17316 input_location);
17317 if (error_msg)
17318 error (error_msg);
17319 if (!function_p && identifier_p (decl))
17320 {
17321 if (complain & tf_error)
17322 unqualified_name_lookup_error (decl);
17323 decl = error_mark_node;
17324 }
17325 RETURN (decl);
17326 }
17327
17328 case TEMPLATE_ID_EXPR:
17329 {
17330 tree object;
17331 tree templ = RECUR (TREE_OPERAND (t, 0));
17332 tree targs = TREE_OPERAND (t, 1);
17333
17334 if (targs)
17335 targs = tsubst_template_args (targs, args, complain, in_decl);
17336 if (targs == error_mark_node)
17337 RETURN (error_mark_node);
17338
17339 if (TREE_CODE (templ) == SCOPE_REF)
17340 {
17341 tree name = TREE_OPERAND (templ, 1);
17342 tree tid = lookup_template_function (name, targs);
17343 TREE_OPERAND (templ, 1) = tid;
17344 RETURN (templ);
17345 }
17346
17347 if (variable_template_p (templ))
17348 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17349
17350 if (TREE_CODE (templ) == COMPONENT_REF)
17351 {
17352 object = TREE_OPERAND (templ, 0);
17353 templ = TREE_OPERAND (templ, 1);
17354 }
17355 else
17356 object = NULL_TREE;
17357 templ = lookup_template_function (templ, targs);
17358
17359 if (object)
17360 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17361 object, templ, NULL_TREE));
17362 else
17363 RETURN (baselink_for_fns (templ));
17364 }
17365
17366 case INDIRECT_REF:
17367 {
17368 tree r = RECUR (TREE_OPERAND (t, 0));
17369
17370 if (REFERENCE_REF_P (t))
17371 {
17372 /* A type conversion to reference type will be enclosed in
17373 such an indirect ref, but the substitution of the cast
17374 will have also added such an indirect ref. */
17375 r = convert_from_reference (r);
17376 }
17377 else
17378 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17379 complain|decltype_flag);
17380
17381 if (REF_PARENTHESIZED_P (t))
17382 r = force_paren_expr (r);
17383
17384 RETURN (r);
17385 }
17386
17387 case NOP_EXPR:
17388 {
17389 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17390 tree op0 = RECUR (TREE_OPERAND (t, 0));
17391 RETURN (build_nop (type, op0));
17392 }
17393
17394 case IMPLICIT_CONV_EXPR:
17395 {
17396 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17397 tree expr = RECUR (TREE_OPERAND (t, 0));
17398 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17399 {
17400 retval = copy_node (t);
17401 TREE_TYPE (retval) = type;
17402 TREE_OPERAND (retval, 0) = expr;
17403 RETURN (retval);
17404 }
17405 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17406 /* We'll pass this to convert_nontype_argument again, we don't need
17407 to actually perform any conversion here. */
17408 RETURN (expr);
17409 int flags = LOOKUP_IMPLICIT;
17410 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17411 flags = LOOKUP_NORMAL;
17412 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17413 flags));
17414 }
17415
17416 case CONVERT_EXPR:
17417 {
17418 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17419 tree op0 = RECUR (TREE_OPERAND (t, 0));
17420 if (op0 == error_mark_node)
17421 RETURN (error_mark_node);
17422 RETURN (build1 (CONVERT_EXPR, type, op0));
17423 }
17424
17425 case CAST_EXPR:
17426 case REINTERPRET_CAST_EXPR:
17427 case CONST_CAST_EXPR:
17428 case DYNAMIC_CAST_EXPR:
17429 case STATIC_CAST_EXPR:
17430 {
17431 tree type;
17432 tree op, r = NULL_TREE;
17433
17434 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17435 if (integral_constant_expression_p
17436 && !cast_valid_in_integral_constant_expression_p (type))
17437 {
17438 if (complain & tf_error)
17439 error ("a cast to a type other than an integral or "
17440 "enumeration type cannot appear in a constant-expression");
17441 RETURN (error_mark_node);
17442 }
17443
17444 op = RECUR (TREE_OPERAND (t, 0));
17445
17446 warning_sentinel s(warn_useless_cast);
17447 warning_sentinel s2(warn_ignored_qualifiers);
17448 switch (TREE_CODE (t))
17449 {
17450 case CAST_EXPR:
17451 r = build_functional_cast (type, op, complain);
17452 break;
17453 case REINTERPRET_CAST_EXPR:
17454 r = build_reinterpret_cast (type, op, complain);
17455 break;
17456 case CONST_CAST_EXPR:
17457 r = build_const_cast (type, op, complain);
17458 break;
17459 case DYNAMIC_CAST_EXPR:
17460 r = build_dynamic_cast (type, op, complain);
17461 break;
17462 case STATIC_CAST_EXPR:
17463 r = build_static_cast (type, op, complain);
17464 break;
17465 default:
17466 gcc_unreachable ();
17467 }
17468
17469 RETURN (r);
17470 }
17471
17472 case POSTDECREMENT_EXPR:
17473 case POSTINCREMENT_EXPR:
17474 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17475 args, complain, in_decl);
17476 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17477 complain|decltype_flag));
17478
17479 case PREDECREMENT_EXPR:
17480 case PREINCREMENT_EXPR:
17481 case NEGATE_EXPR:
17482 case BIT_NOT_EXPR:
17483 case ABS_EXPR:
17484 case TRUTH_NOT_EXPR:
17485 case UNARY_PLUS_EXPR: /* Unary + */
17486 case REALPART_EXPR:
17487 case IMAGPART_EXPR:
17488 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17489 RECUR (TREE_OPERAND (t, 0)),
17490 complain|decltype_flag));
17491
17492 case FIX_TRUNC_EXPR:
17493 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17494 false, complain));
17495
17496 case ADDR_EXPR:
17497 op1 = TREE_OPERAND (t, 0);
17498 if (TREE_CODE (op1) == LABEL_DECL)
17499 RETURN (finish_label_address_expr (DECL_NAME (op1),
17500 EXPR_LOCATION (op1)));
17501 if (TREE_CODE (op1) == SCOPE_REF)
17502 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17503 /*done=*/true, /*address_p=*/true);
17504 else
17505 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17506 in_decl);
17507 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17508 complain|decltype_flag));
17509
17510 case PLUS_EXPR:
17511 case MINUS_EXPR:
17512 case MULT_EXPR:
17513 case TRUNC_DIV_EXPR:
17514 case CEIL_DIV_EXPR:
17515 case FLOOR_DIV_EXPR:
17516 case ROUND_DIV_EXPR:
17517 case EXACT_DIV_EXPR:
17518 case BIT_AND_EXPR:
17519 case BIT_IOR_EXPR:
17520 case BIT_XOR_EXPR:
17521 case TRUNC_MOD_EXPR:
17522 case FLOOR_MOD_EXPR:
17523 case TRUTH_ANDIF_EXPR:
17524 case TRUTH_ORIF_EXPR:
17525 case TRUTH_AND_EXPR:
17526 case TRUTH_OR_EXPR:
17527 case RSHIFT_EXPR:
17528 case LSHIFT_EXPR:
17529 case RROTATE_EXPR:
17530 case LROTATE_EXPR:
17531 case EQ_EXPR:
17532 case NE_EXPR:
17533 case MAX_EXPR:
17534 case MIN_EXPR:
17535 case LE_EXPR:
17536 case GE_EXPR:
17537 case LT_EXPR:
17538 case GT_EXPR:
17539 case MEMBER_REF:
17540 case DOTSTAR_EXPR:
17541 {
17542 warning_sentinel s1(warn_type_limits);
17543 warning_sentinel s2(warn_div_by_zero);
17544 warning_sentinel s3(warn_logical_op);
17545 warning_sentinel s4(warn_tautological_compare);
17546 tree op0 = RECUR (TREE_OPERAND (t, 0));
17547 tree op1 = RECUR (TREE_OPERAND (t, 1));
17548 tree r = build_x_binary_op
17549 (input_location, TREE_CODE (t),
17550 op0,
17551 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17552 ? ERROR_MARK
17553 : TREE_CODE (TREE_OPERAND (t, 0))),
17554 op1,
17555 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17556 ? ERROR_MARK
17557 : TREE_CODE (TREE_OPERAND (t, 1))),
17558 /*overload=*/NULL,
17559 complain|decltype_flag);
17560 if (EXPR_P (r) && TREE_NO_WARNING (t))
17561 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17562
17563 RETURN (r);
17564 }
17565
17566 case POINTER_PLUS_EXPR:
17567 {
17568 tree op0 = RECUR (TREE_OPERAND (t, 0));
17569 tree op1 = RECUR (TREE_OPERAND (t, 1));
17570 RETURN (fold_build_pointer_plus (op0, op1));
17571 }
17572
17573 case SCOPE_REF:
17574 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17575 /*address_p=*/false));
17576 case ARRAY_REF:
17577 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17578 args, complain, in_decl);
17579 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17580 RECUR (TREE_OPERAND (t, 1)),
17581 complain|decltype_flag));
17582
17583 case SIZEOF_EXPR:
17584 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17585 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17586 RETURN (tsubst_copy (t, args, complain, in_decl));
17587 /* Fall through */
17588
17589 case ALIGNOF_EXPR:
17590 {
17591 tree r;
17592
17593 op1 = TREE_OPERAND (t, 0);
17594 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17595 op1 = TREE_TYPE (op1);
17596 if (!args)
17597 {
17598 /* When there are no ARGS, we are trying to evaluate a
17599 non-dependent expression from the parser. Trying to do
17600 the substitutions may not work. */
17601 if (!TYPE_P (op1))
17602 op1 = TREE_TYPE (op1);
17603 }
17604 else
17605 {
17606 ++cp_unevaluated_operand;
17607 ++c_inhibit_evaluation_warnings;
17608 if (TYPE_P (op1))
17609 op1 = tsubst (op1, args, complain, in_decl);
17610 else
17611 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17612 /*function_p=*/false,
17613 /*integral_constant_expression_p=*/
17614 false);
17615 --cp_unevaluated_operand;
17616 --c_inhibit_evaluation_warnings;
17617 }
17618 if (TYPE_P (op1))
17619 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17620 complain & tf_error);
17621 else
17622 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17623 complain & tf_error);
17624 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17625 {
17626 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17627 {
17628 if (!processing_template_decl && TYPE_P (op1))
17629 {
17630 r = build_min (SIZEOF_EXPR, size_type_node,
17631 build1 (NOP_EXPR, op1, error_mark_node));
17632 SIZEOF_EXPR_TYPE_P (r) = 1;
17633 }
17634 else
17635 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17636 TREE_SIDE_EFFECTS (r) = 0;
17637 TREE_READONLY (r) = 1;
17638 }
17639 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17640 }
17641 RETURN (r);
17642 }
17643
17644 case AT_ENCODE_EXPR:
17645 {
17646 op1 = TREE_OPERAND (t, 0);
17647 ++cp_unevaluated_operand;
17648 ++c_inhibit_evaluation_warnings;
17649 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17650 /*function_p=*/false,
17651 /*integral_constant_expression_p=*/false);
17652 --cp_unevaluated_operand;
17653 --c_inhibit_evaluation_warnings;
17654 RETURN (objc_build_encode_expr (op1));
17655 }
17656
17657 case NOEXCEPT_EXPR:
17658 op1 = TREE_OPERAND (t, 0);
17659 ++cp_unevaluated_operand;
17660 ++c_inhibit_evaluation_warnings;
17661 ++cp_noexcept_operand;
17662 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17663 /*function_p=*/false,
17664 /*integral_constant_expression_p=*/false);
17665 --cp_unevaluated_operand;
17666 --c_inhibit_evaluation_warnings;
17667 --cp_noexcept_operand;
17668 RETURN (finish_noexcept_expr (op1, complain));
17669
17670 case MODOP_EXPR:
17671 {
17672 warning_sentinel s(warn_div_by_zero);
17673 tree lhs = RECUR (TREE_OPERAND (t, 0));
17674 tree rhs = RECUR (TREE_OPERAND (t, 2));
17675 tree r = build_x_modify_expr
17676 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17677 complain|decltype_flag);
17678 /* TREE_NO_WARNING must be set if either the expression was
17679 parenthesized or it uses an operator such as >>= rather
17680 than plain assignment. In the former case, it was already
17681 set and must be copied. In the latter case,
17682 build_x_modify_expr sets it and it must not be reset
17683 here. */
17684 if (TREE_NO_WARNING (t))
17685 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17686
17687 RETURN (r);
17688 }
17689
17690 case ARROW_EXPR:
17691 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17692 args, complain, in_decl);
17693 /* Remember that there was a reference to this entity. */
17694 if (DECL_P (op1)
17695 && !mark_used (op1, complain) && !(complain & tf_error))
17696 RETURN (error_mark_node);
17697 RETURN (build_x_arrow (input_location, op1, complain));
17698
17699 case NEW_EXPR:
17700 {
17701 tree placement = RECUR (TREE_OPERAND (t, 0));
17702 tree init = RECUR (TREE_OPERAND (t, 3));
17703 vec<tree, va_gc> *placement_vec;
17704 vec<tree, va_gc> *init_vec;
17705 tree ret;
17706
17707 if (placement == NULL_TREE)
17708 placement_vec = NULL;
17709 else
17710 {
17711 placement_vec = make_tree_vector ();
17712 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17713 vec_safe_push (placement_vec, TREE_VALUE (placement));
17714 }
17715
17716 /* If there was an initializer in the original tree, but it
17717 instantiated to an empty list, then we should pass a
17718 non-NULL empty vector to tell build_new that it was an
17719 empty initializer() rather than no initializer. This can
17720 only happen when the initializer is a pack expansion whose
17721 parameter packs are of length zero. */
17722 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17723 init_vec = NULL;
17724 else
17725 {
17726 init_vec = make_tree_vector ();
17727 if (init == void_node)
17728 gcc_assert (init_vec != NULL);
17729 else
17730 {
17731 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17732 vec_safe_push (init_vec, TREE_VALUE (init));
17733 }
17734 }
17735
17736 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17737 tree op2 = RECUR (TREE_OPERAND (t, 2));
17738 ret = build_new (&placement_vec, op1, op2, &init_vec,
17739 NEW_EXPR_USE_GLOBAL (t),
17740 complain);
17741
17742 if (placement_vec != NULL)
17743 release_tree_vector (placement_vec);
17744 if (init_vec != NULL)
17745 release_tree_vector (init_vec);
17746
17747 RETURN (ret);
17748 }
17749
17750 case DELETE_EXPR:
17751 {
17752 tree op0 = RECUR (TREE_OPERAND (t, 0));
17753 tree op1 = RECUR (TREE_OPERAND (t, 1));
17754 RETURN (delete_sanity (op0, op1,
17755 DELETE_EXPR_USE_VEC (t),
17756 DELETE_EXPR_USE_GLOBAL (t),
17757 complain));
17758 }
17759
17760 case COMPOUND_EXPR:
17761 {
17762 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17763 complain & ~tf_decltype, in_decl,
17764 /*function_p=*/false,
17765 integral_constant_expression_p);
17766 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17767 op0,
17768 RECUR (TREE_OPERAND (t, 1)),
17769 complain|decltype_flag));
17770 }
17771
17772 case CALL_EXPR:
17773 {
17774 tree function;
17775 vec<tree, va_gc> *call_args;
17776 unsigned int nargs, i;
17777 bool qualified_p;
17778 bool koenig_p;
17779 tree ret;
17780
17781 function = CALL_EXPR_FN (t);
17782 /* Internal function with no arguments. */
17783 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17784 RETURN (t);
17785
17786 /* When we parsed the expression, we determined whether or
17787 not Koenig lookup should be performed. */
17788 koenig_p = KOENIG_LOOKUP_P (t);
17789 if (function == NULL_TREE)
17790 {
17791 koenig_p = false;
17792 qualified_p = false;
17793 }
17794 else if (TREE_CODE (function) == SCOPE_REF)
17795 {
17796 qualified_p = true;
17797 function = tsubst_qualified_id (function, args, complain, in_decl,
17798 /*done=*/false,
17799 /*address_p=*/false);
17800 }
17801 else if (koenig_p && identifier_p (function))
17802 {
17803 /* Do nothing; calling tsubst_copy_and_build on an identifier
17804 would incorrectly perform unqualified lookup again.
17805
17806 Note that we can also have an IDENTIFIER_NODE if the earlier
17807 unqualified lookup found a member function; in that case
17808 koenig_p will be false and we do want to do the lookup
17809 again to find the instantiated member function.
17810
17811 FIXME but doing that causes c++/15272, so we need to stop
17812 using IDENTIFIER_NODE in that situation. */
17813 qualified_p = false;
17814 }
17815 else
17816 {
17817 if (TREE_CODE (function) == COMPONENT_REF)
17818 {
17819 tree op = TREE_OPERAND (function, 1);
17820
17821 qualified_p = (TREE_CODE (op) == SCOPE_REF
17822 || (BASELINK_P (op)
17823 && BASELINK_QUALIFIED_P (op)));
17824 }
17825 else
17826 qualified_p = false;
17827
17828 if (TREE_CODE (function) == ADDR_EXPR
17829 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17830 /* Avoid error about taking the address of a constructor. */
17831 function = TREE_OPERAND (function, 0);
17832
17833 function = tsubst_copy_and_build (function, args, complain,
17834 in_decl,
17835 !qualified_p,
17836 integral_constant_expression_p);
17837
17838 if (BASELINK_P (function))
17839 qualified_p = true;
17840 }
17841
17842 nargs = call_expr_nargs (t);
17843 call_args = make_tree_vector ();
17844 for (i = 0; i < nargs; ++i)
17845 {
17846 tree arg = CALL_EXPR_ARG (t, i);
17847
17848 if (!PACK_EXPANSION_P (arg))
17849 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17850 else
17851 {
17852 /* Expand the pack expansion and push each entry onto
17853 CALL_ARGS. */
17854 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17855 if (TREE_CODE (arg) == TREE_VEC)
17856 {
17857 unsigned int len, j;
17858
17859 len = TREE_VEC_LENGTH (arg);
17860 for (j = 0; j < len; ++j)
17861 {
17862 tree value = TREE_VEC_ELT (arg, j);
17863 if (value != NULL_TREE)
17864 value = convert_from_reference (value);
17865 vec_safe_push (call_args, value);
17866 }
17867 }
17868 else
17869 {
17870 /* A partial substitution. Add one entry. */
17871 vec_safe_push (call_args, arg);
17872 }
17873 }
17874 }
17875
17876 /* We do not perform argument-dependent lookup if normal
17877 lookup finds a non-function, in accordance with the
17878 expected resolution of DR 218. */
17879 if (koenig_p
17880 && ((is_overloaded_fn (function)
17881 /* If lookup found a member function, the Koenig lookup is
17882 not appropriate, even if an unqualified-name was used
17883 to denote the function. */
17884 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17885 || identifier_p (function))
17886 /* Only do this when substitution turns a dependent call
17887 into a non-dependent call. */
17888 && type_dependent_expression_p_push (t)
17889 && !any_type_dependent_arguments_p (call_args))
17890 function = perform_koenig_lookup (function, call_args, tf_none);
17891
17892 if (function != NULL_TREE
17893 && identifier_p (function)
17894 && !any_type_dependent_arguments_p (call_args))
17895 {
17896 if (koenig_p && (complain & tf_warning_or_error))
17897 {
17898 /* For backwards compatibility and good diagnostics, try
17899 the unqualified lookup again if we aren't in SFINAE
17900 context. */
17901 tree unq = (tsubst_copy_and_build
17902 (function, args, complain, in_decl, true,
17903 integral_constant_expression_p));
17904 if (unq == error_mark_node)
17905 {
17906 release_tree_vector (call_args);
17907 RETURN (error_mark_node);
17908 }
17909
17910 if (unq != function)
17911 {
17912 /* In a lambda fn, we have to be careful to not
17913 introduce new this captures. Legacy code can't
17914 be using lambdas anyway, so it's ok to be
17915 stricter. */
17916 bool in_lambda = (current_class_type
17917 && LAMBDA_TYPE_P (current_class_type));
17918 char const *const msg
17919 = G_("%qD was not declared in this scope, "
17920 "and no declarations were found by "
17921 "argument-dependent lookup at the point "
17922 "of instantiation");
17923
17924 bool diag = true;
17925 if (in_lambda)
17926 error_at (EXPR_LOC_OR_LOC (t, input_location),
17927 msg, function);
17928 else
17929 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17930 msg, function);
17931 if (diag)
17932 {
17933 tree fn = unq;
17934
17935 if (INDIRECT_REF_P (fn))
17936 fn = TREE_OPERAND (fn, 0);
17937 if (is_overloaded_fn (fn))
17938 fn = get_first_fn (fn);
17939
17940 if (!DECL_P (fn))
17941 /* Can't say anything more. */;
17942 else if (DECL_CLASS_SCOPE_P (fn))
17943 {
17944 location_t loc = EXPR_LOC_OR_LOC (t,
17945 input_location);
17946 inform (loc,
17947 "declarations in dependent base %qT are "
17948 "not found by unqualified lookup",
17949 DECL_CLASS_CONTEXT (fn));
17950 if (current_class_ptr)
17951 inform (loc,
17952 "use %<this->%D%> instead", function);
17953 else
17954 inform (loc,
17955 "use %<%T::%D%> instead",
17956 current_class_name, function);
17957 }
17958 else
17959 inform (DECL_SOURCE_LOCATION (fn),
17960 "%qD declared here, later in the "
17961 "translation unit", fn);
17962 if (in_lambda)
17963 {
17964 release_tree_vector (call_args);
17965 RETURN (error_mark_node);
17966 }
17967 }
17968
17969 function = unq;
17970 }
17971 }
17972 if (identifier_p (function))
17973 {
17974 if (complain & tf_error)
17975 unqualified_name_lookup_error (function);
17976 release_tree_vector (call_args);
17977 RETURN (error_mark_node);
17978 }
17979 }
17980
17981 /* Remember that there was a reference to this entity. */
17982 if (function != NULL_TREE
17983 && DECL_P (function)
17984 && !mark_used (function, complain) && !(complain & tf_error))
17985 {
17986 release_tree_vector (call_args);
17987 RETURN (error_mark_node);
17988 }
17989
17990 /* Put back tf_decltype for the actual call. */
17991 complain |= decltype_flag;
17992
17993 if (function == NULL_TREE)
17994 switch (CALL_EXPR_IFN (t))
17995 {
17996 case IFN_LAUNDER:
17997 gcc_assert (nargs == 1);
17998 if (vec_safe_length (call_args) != 1)
17999 {
18000 error_at (EXPR_LOC_OR_LOC (t, input_location),
18001 "wrong number of arguments to "
18002 "%<__builtin_launder%>");
18003 ret = error_mark_node;
18004 }
18005 else
18006 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
18007 input_location),
18008 (*call_args)[0], complain);
18009 break;
18010
18011 default:
18012 /* Unsupported internal function with arguments. */
18013 gcc_unreachable ();
18014 }
18015 else if (TREE_CODE (function) == OFFSET_REF)
18016 ret = build_offset_ref_call_from_tree (function, &call_args,
18017 complain);
18018 else if (TREE_CODE (function) == COMPONENT_REF)
18019 {
18020 tree instance = TREE_OPERAND (function, 0);
18021 tree fn = TREE_OPERAND (function, 1);
18022
18023 if (processing_template_decl
18024 && (type_dependent_expression_p (instance)
18025 || (!BASELINK_P (fn)
18026 && TREE_CODE (fn) != FIELD_DECL)
18027 || type_dependent_expression_p (fn)
18028 || any_type_dependent_arguments_p (call_args)))
18029 ret = build_min_nt_call_vec (function, call_args);
18030 else if (!BASELINK_P (fn))
18031 ret = finish_call_expr (function, &call_args,
18032 /*disallow_virtual=*/false,
18033 /*koenig_p=*/false,
18034 complain);
18035 else
18036 ret = (build_new_method_call
18037 (instance, fn,
18038 &call_args, NULL_TREE,
18039 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18040 /*fn_p=*/NULL,
18041 complain));
18042 }
18043 else
18044 ret = finish_call_expr (function, &call_args,
18045 /*disallow_virtual=*/qualified_p,
18046 koenig_p,
18047 complain);
18048
18049 release_tree_vector (call_args);
18050
18051 if (ret != error_mark_node)
18052 {
18053 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18054 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18055 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18056 bool thk = CALL_FROM_THUNK_P (t);
18057 if (op || ord || rev || thk)
18058 {
18059 function = extract_call_expr (ret);
18060 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18061 CALL_EXPR_ORDERED_ARGS (function) = ord;
18062 CALL_EXPR_REVERSE_ARGS (function) = rev;
18063 if (thk)
18064 {
18065 if (TREE_CODE (function) == CALL_EXPR)
18066 CALL_FROM_THUNK_P (function) = true;
18067 else
18068 AGGR_INIT_FROM_THUNK_P (function) = true;
18069 /* The thunk location is not interesting. */
18070 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18071 }
18072 }
18073 }
18074
18075 RETURN (ret);
18076 }
18077
18078 case COND_EXPR:
18079 {
18080 tree cond = RECUR (TREE_OPERAND (t, 0));
18081 tree folded_cond = fold_non_dependent_expr (cond);
18082 tree exp1, exp2;
18083
18084 if (TREE_CODE (folded_cond) == INTEGER_CST)
18085 {
18086 if (integer_zerop (folded_cond))
18087 {
18088 ++c_inhibit_evaluation_warnings;
18089 exp1 = RECUR (TREE_OPERAND (t, 1));
18090 --c_inhibit_evaluation_warnings;
18091 exp2 = RECUR (TREE_OPERAND (t, 2));
18092 }
18093 else
18094 {
18095 exp1 = RECUR (TREE_OPERAND (t, 1));
18096 ++c_inhibit_evaluation_warnings;
18097 exp2 = RECUR (TREE_OPERAND (t, 2));
18098 --c_inhibit_evaluation_warnings;
18099 }
18100 cond = folded_cond;
18101 }
18102 else
18103 {
18104 exp1 = RECUR (TREE_OPERAND (t, 1));
18105 exp2 = RECUR (TREE_OPERAND (t, 2));
18106 }
18107
18108 warning_sentinel s(warn_duplicated_branches);
18109 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18110 cond, exp1, exp2, complain));
18111 }
18112
18113 case PSEUDO_DTOR_EXPR:
18114 {
18115 tree op0 = RECUR (TREE_OPERAND (t, 0));
18116 tree op1 = RECUR (TREE_OPERAND (t, 1));
18117 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18118 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18119 input_location));
18120 }
18121
18122 case TREE_LIST:
18123 {
18124 tree purpose, value, chain;
18125
18126 if (t == void_list_node)
18127 RETURN (t);
18128
18129 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18130 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18131 {
18132 /* We have pack expansions, so expand those and
18133 create a new list out of it. */
18134 tree purposevec = NULL_TREE;
18135 tree valuevec = NULL_TREE;
18136 tree chain;
18137 int i, len = -1;
18138
18139 /* Expand the argument expressions. */
18140 if (TREE_PURPOSE (t))
18141 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18142 complain, in_decl);
18143 if (TREE_VALUE (t))
18144 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18145 complain, in_decl);
18146
18147 /* Build the rest of the list. */
18148 chain = TREE_CHAIN (t);
18149 if (chain && chain != void_type_node)
18150 chain = RECUR (chain);
18151
18152 /* Determine the number of arguments. */
18153 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18154 {
18155 len = TREE_VEC_LENGTH (purposevec);
18156 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18157 }
18158 else if (TREE_CODE (valuevec) == TREE_VEC)
18159 len = TREE_VEC_LENGTH (valuevec);
18160 else
18161 {
18162 /* Since we only performed a partial substitution into
18163 the argument pack, we only RETURN (a single list
18164 node. */
18165 if (purposevec == TREE_PURPOSE (t)
18166 && valuevec == TREE_VALUE (t)
18167 && chain == TREE_CHAIN (t))
18168 RETURN (t);
18169
18170 RETURN (tree_cons (purposevec, valuevec, chain));
18171 }
18172
18173 /* Convert the argument vectors into a TREE_LIST */
18174 i = len;
18175 while (i > 0)
18176 {
18177 /* Grab the Ith values. */
18178 i--;
18179 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18180 : NULL_TREE;
18181 value
18182 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18183 : NULL_TREE;
18184
18185 /* Build the list (backwards). */
18186 chain = tree_cons (purpose, value, chain);
18187 }
18188
18189 RETURN (chain);
18190 }
18191
18192 purpose = TREE_PURPOSE (t);
18193 if (purpose)
18194 purpose = RECUR (purpose);
18195 value = TREE_VALUE (t);
18196 if (value)
18197 value = RECUR (value);
18198 chain = TREE_CHAIN (t);
18199 if (chain && chain != void_type_node)
18200 chain = RECUR (chain);
18201 if (purpose == TREE_PURPOSE (t)
18202 && value == TREE_VALUE (t)
18203 && chain == TREE_CHAIN (t))
18204 RETURN (t);
18205 RETURN (tree_cons (purpose, value, chain));
18206 }
18207
18208 case COMPONENT_REF:
18209 {
18210 tree object;
18211 tree object_type;
18212 tree member;
18213 tree r;
18214
18215 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18216 args, complain, in_decl);
18217 /* Remember that there was a reference to this entity. */
18218 if (DECL_P (object)
18219 && !mark_used (object, complain) && !(complain & tf_error))
18220 RETURN (error_mark_node);
18221 object_type = TREE_TYPE (object);
18222
18223 member = TREE_OPERAND (t, 1);
18224 if (BASELINK_P (member))
18225 member = tsubst_baselink (member,
18226 non_reference (TREE_TYPE (object)),
18227 args, complain, in_decl);
18228 else
18229 member = tsubst_copy (member, args, complain, in_decl);
18230 if (member == error_mark_node)
18231 RETURN (error_mark_node);
18232
18233 if (TREE_CODE (member) == FIELD_DECL)
18234 {
18235 r = finish_non_static_data_member (member, object, NULL_TREE);
18236 if (TREE_CODE (r) == COMPONENT_REF)
18237 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18238 RETURN (r);
18239 }
18240 else if (type_dependent_expression_p (object))
18241 /* We can't do much here. */;
18242 else if (!CLASS_TYPE_P (object_type))
18243 {
18244 if (scalarish_type_p (object_type))
18245 {
18246 tree s = NULL_TREE;
18247 tree dtor = member;
18248
18249 if (TREE_CODE (dtor) == SCOPE_REF)
18250 {
18251 s = TREE_OPERAND (dtor, 0);
18252 dtor = TREE_OPERAND (dtor, 1);
18253 }
18254 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18255 {
18256 dtor = TREE_OPERAND (dtor, 0);
18257 if (TYPE_P (dtor))
18258 RETURN (finish_pseudo_destructor_expr
18259 (object, s, dtor, input_location));
18260 }
18261 }
18262 }
18263 else if (TREE_CODE (member) == SCOPE_REF
18264 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18265 {
18266 /* Lookup the template functions now that we know what the
18267 scope is. */
18268 tree scope = TREE_OPERAND (member, 0);
18269 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18270 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18271 member = lookup_qualified_name (scope, tmpl,
18272 /*is_type_p=*/false,
18273 /*complain=*/false);
18274 if (BASELINK_P (member))
18275 {
18276 BASELINK_FUNCTIONS (member)
18277 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18278 args);
18279 member = (adjust_result_of_qualified_name_lookup
18280 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18281 object_type));
18282 }
18283 else
18284 {
18285 qualified_name_lookup_error (scope, tmpl, member,
18286 input_location);
18287 RETURN (error_mark_node);
18288 }
18289 }
18290 else if (TREE_CODE (member) == SCOPE_REF
18291 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18292 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18293 {
18294 if (complain & tf_error)
18295 {
18296 if (TYPE_P (TREE_OPERAND (member, 0)))
18297 error ("%qT is not a class or namespace",
18298 TREE_OPERAND (member, 0));
18299 else
18300 error ("%qD is not a class or namespace",
18301 TREE_OPERAND (member, 0));
18302 }
18303 RETURN (error_mark_node);
18304 }
18305
18306 r = finish_class_member_access_expr (object, member,
18307 /*template_p=*/false,
18308 complain);
18309 if (TREE_CODE (r) == COMPONENT_REF)
18310 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18311 RETURN (r);
18312 }
18313
18314 case THROW_EXPR:
18315 RETURN (build_throw
18316 (RECUR (TREE_OPERAND (t, 0))));
18317
18318 case CONSTRUCTOR:
18319 {
18320 vec<constructor_elt, va_gc> *n;
18321 constructor_elt *ce;
18322 unsigned HOST_WIDE_INT idx;
18323 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18324 bool process_index_p;
18325 int newlen;
18326 bool need_copy_p = false;
18327 tree r;
18328
18329 if (type == error_mark_node)
18330 RETURN (error_mark_node);
18331
18332 /* We do not want to process the index of aggregate
18333 initializers as they are identifier nodes which will be
18334 looked up by digest_init. */
18335 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18336
18337 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18338 newlen = vec_safe_length (n);
18339 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18340 {
18341 if (ce->index && process_index_p
18342 /* An identifier index is looked up in the type
18343 being initialized, not the current scope. */
18344 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18345 ce->index = RECUR (ce->index);
18346
18347 if (PACK_EXPANSION_P (ce->value))
18348 {
18349 /* Substitute into the pack expansion. */
18350 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18351 in_decl);
18352
18353 if (ce->value == error_mark_node
18354 || PACK_EXPANSION_P (ce->value))
18355 ;
18356 else if (TREE_VEC_LENGTH (ce->value) == 1)
18357 /* Just move the argument into place. */
18358 ce->value = TREE_VEC_ELT (ce->value, 0);
18359 else
18360 {
18361 /* Update the length of the final CONSTRUCTOR
18362 arguments vector, and note that we will need to
18363 copy.*/
18364 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18365 need_copy_p = true;
18366 }
18367 }
18368 else
18369 ce->value = RECUR (ce->value);
18370 }
18371
18372 if (need_copy_p)
18373 {
18374 vec<constructor_elt, va_gc> *old_n = n;
18375
18376 vec_alloc (n, newlen);
18377 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18378 {
18379 if (TREE_CODE (ce->value) == TREE_VEC)
18380 {
18381 int i, len = TREE_VEC_LENGTH (ce->value);
18382 for (i = 0; i < len; ++i)
18383 CONSTRUCTOR_APPEND_ELT (n, 0,
18384 TREE_VEC_ELT (ce->value, i));
18385 }
18386 else
18387 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18388 }
18389 }
18390
18391 r = build_constructor (init_list_type_node, n);
18392 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18393
18394 if (TREE_HAS_CONSTRUCTOR (t))
18395 {
18396 fcl_t cl = fcl_functional;
18397 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18398 cl = fcl_c99;
18399 RETURN (finish_compound_literal (type, r, complain, cl));
18400 }
18401
18402 TREE_TYPE (r) = type;
18403 RETURN (r);
18404 }
18405
18406 case TYPEID_EXPR:
18407 {
18408 tree operand_0 = TREE_OPERAND (t, 0);
18409 if (TYPE_P (operand_0))
18410 {
18411 operand_0 = tsubst (operand_0, args, complain, in_decl);
18412 RETURN (get_typeid (operand_0, complain));
18413 }
18414 else
18415 {
18416 operand_0 = RECUR (operand_0);
18417 RETURN (build_typeid (operand_0, complain));
18418 }
18419 }
18420
18421 case VAR_DECL:
18422 if (!args)
18423 RETURN (t);
18424 /* Fall through */
18425
18426 case PARM_DECL:
18427 {
18428 tree r = tsubst_copy (t, args, complain, in_decl);
18429 /* ??? We're doing a subset of finish_id_expression here. */
18430 if (VAR_P (r)
18431 && !processing_template_decl
18432 && !cp_unevaluated_operand
18433 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18434 && CP_DECL_THREAD_LOCAL_P (r))
18435 {
18436 if (tree wrap = get_tls_wrapper_fn (r))
18437 /* Replace an evaluated use of the thread_local variable with
18438 a call to its wrapper. */
18439 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18440 }
18441 else if (outer_automatic_var_p (r))
18442 r = process_outer_var_ref (r, complain);
18443
18444 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18445 /* If the original type was a reference, we'll be wrapped in
18446 the appropriate INDIRECT_REF. */
18447 r = convert_from_reference (r);
18448 RETURN (r);
18449 }
18450
18451 case VA_ARG_EXPR:
18452 {
18453 tree op0 = RECUR (TREE_OPERAND (t, 0));
18454 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18455 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18456 }
18457
18458 case OFFSETOF_EXPR:
18459 {
18460 tree object_ptr
18461 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18462 in_decl, /*function_p=*/false,
18463 /*integral_constant_expression_p=*/false);
18464 RETURN (finish_offsetof (object_ptr,
18465 RECUR (TREE_OPERAND (t, 0)),
18466 EXPR_LOCATION (t)));
18467 }
18468
18469 case ADDRESSOF_EXPR:
18470 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18471 RECUR (TREE_OPERAND (t, 0)), complain));
18472
18473 case TRAIT_EXPR:
18474 {
18475 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18476 complain, in_decl);
18477
18478 tree type2 = TRAIT_EXPR_TYPE2 (t);
18479 if (type2 && TREE_CODE (type2) == TREE_LIST)
18480 type2 = RECUR (type2);
18481 else if (type2)
18482 type2 = tsubst (type2, args, complain, in_decl);
18483
18484 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18485 }
18486
18487 case STMT_EXPR:
18488 {
18489 tree old_stmt_expr = cur_stmt_expr;
18490 tree stmt_expr = begin_stmt_expr ();
18491
18492 cur_stmt_expr = stmt_expr;
18493 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18494 integral_constant_expression_p);
18495 stmt_expr = finish_stmt_expr (stmt_expr, false);
18496 cur_stmt_expr = old_stmt_expr;
18497
18498 /* If the resulting list of expression statement is empty,
18499 fold it further into void_node. */
18500 if (empty_expr_stmt_p (stmt_expr))
18501 stmt_expr = void_node;
18502
18503 RETURN (stmt_expr);
18504 }
18505
18506 case LAMBDA_EXPR:
18507 {
18508 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18509
18510 RETURN (build_lambda_object (r));
18511 }
18512
18513 case TARGET_EXPR:
18514 /* We can get here for a constant initializer of non-dependent type.
18515 FIXME stop folding in cp_parser_initializer_clause. */
18516 {
18517 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18518 complain);
18519 RETURN (r);
18520 }
18521
18522 case TRANSACTION_EXPR:
18523 RETURN (tsubst_expr(t, args, complain, in_decl,
18524 integral_constant_expression_p));
18525
18526 case PAREN_EXPR:
18527 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18528
18529 case VEC_PERM_EXPR:
18530 {
18531 tree op0 = RECUR (TREE_OPERAND (t, 0));
18532 tree op1 = RECUR (TREE_OPERAND (t, 1));
18533 tree op2 = RECUR (TREE_OPERAND (t, 2));
18534 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18535 complain));
18536 }
18537
18538 case REQUIRES_EXPR:
18539 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18540
18541 case NON_LVALUE_EXPR:
18542 case VIEW_CONVERT_EXPR:
18543 /* We should only see these for location wrapper nodes, or within
18544 instantiate_non_dependent_expr (when args is NULL_TREE). */
18545 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18546 if (location_wrapper_p (t))
18547 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18548 EXPR_LOCATION (t)));
18549 /* fallthrough. */
18550
18551 default:
18552 /* Handle Objective-C++ constructs, if appropriate. */
18553 {
18554 tree subst
18555 = objcp_tsubst_copy_and_build (t, args, complain,
18556 in_decl, /*function_p=*/false);
18557 if (subst)
18558 RETURN (subst);
18559 }
18560 RETURN (tsubst_copy (t, args, complain, in_decl));
18561 }
18562
18563 #undef RECUR
18564 #undef RETURN
18565 out:
18566 input_location = loc;
18567 return retval;
18568 }
18569
18570 /* Verify that the instantiated ARGS are valid. For type arguments,
18571 make sure that the type's linkage is ok. For non-type arguments,
18572 make sure they are constants if they are integral or enumerations.
18573 Emit an error under control of COMPLAIN, and return TRUE on error. */
18574
18575 static bool
18576 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18577 {
18578 if (dependent_template_arg_p (t))
18579 return false;
18580 if (ARGUMENT_PACK_P (t))
18581 {
18582 tree vec = ARGUMENT_PACK_ARGS (t);
18583 int len = TREE_VEC_LENGTH (vec);
18584 bool result = false;
18585 int i;
18586
18587 for (i = 0; i < len; ++i)
18588 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18589 result = true;
18590 return result;
18591 }
18592 else if (TYPE_P (t))
18593 {
18594 /* [basic.link]: A name with no linkage (notably, the name
18595 of a class or enumeration declared in a local scope)
18596 shall not be used to declare an entity with linkage.
18597 This implies that names with no linkage cannot be used as
18598 template arguments
18599
18600 DR 757 relaxes this restriction for C++0x. */
18601 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18602 : no_linkage_check (t, /*relaxed_p=*/false));
18603
18604 if (nt)
18605 {
18606 /* DR 488 makes use of a type with no linkage cause
18607 type deduction to fail. */
18608 if (complain & tf_error)
18609 {
18610 if (TYPE_UNNAMED_P (nt))
18611 error ("%qT is/uses unnamed type", t);
18612 else
18613 error ("template argument for %qD uses local type %qT",
18614 tmpl, t);
18615 }
18616 return true;
18617 }
18618 /* In order to avoid all sorts of complications, we do not
18619 allow variably-modified types as template arguments. */
18620 else if (variably_modified_type_p (t, NULL_TREE))
18621 {
18622 if (complain & tf_error)
18623 error ("%qT is a variably modified type", t);
18624 return true;
18625 }
18626 }
18627 /* Class template and alias template arguments should be OK. */
18628 else if (DECL_TYPE_TEMPLATE_P (t))
18629 ;
18630 /* A non-type argument of integral or enumerated type must be a
18631 constant. */
18632 else if (TREE_TYPE (t)
18633 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18634 && !REFERENCE_REF_P (t)
18635 && !TREE_CONSTANT (t))
18636 {
18637 if (complain & tf_error)
18638 error ("integral expression %qE is not constant", t);
18639 return true;
18640 }
18641 return false;
18642 }
18643
18644 static bool
18645 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18646 {
18647 int ix, len = DECL_NTPARMS (tmpl);
18648 bool result = false;
18649
18650 for (ix = 0; ix != len; ix++)
18651 {
18652 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18653 result = true;
18654 }
18655 if (result && (complain & tf_error))
18656 error (" trying to instantiate %qD", tmpl);
18657 return result;
18658 }
18659
18660 /* We're out of SFINAE context now, so generate diagnostics for the access
18661 errors we saw earlier when instantiating D from TMPL and ARGS. */
18662
18663 static void
18664 recheck_decl_substitution (tree d, tree tmpl, tree args)
18665 {
18666 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18667 tree type = TREE_TYPE (pattern);
18668 location_t loc = input_location;
18669
18670 push_access_scope (d);
18671 push_deferring_access_checks (dk_no_deferred);
18672 input_location = DECL_SOURCE_LOCATION (pattern);
18673 tsubst (type, args, tf_warning_or_error, d);
18674 input_location = loc;
18675 pop_deferring_access_checks ();
18676 pop_access_scope (d);
18677 }
18678
18679 /* Instantiate the indicated variable, function, or alias template TMPL with
18680 the template arguments in TARG_PTR. */
18681
18682 static tree
18683 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18684 {
18685 tree targ_ptr = orig_args;
18686 tree fndecl;
18687 tree gen_tmpl;
18688 tree spec;
18689 bool access_ok = true;
18690
18691 if (tmpl == error_mark_node)
18692 return error_mark_node;
18693
18694 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18695
18696 /* If this function is a clone, handle it specially. */
18697 if (DECL_CLONED_FUNCTION_P (tmpl))
18698 {
18699 tree spec;
18700 tree clone;
18701
18702 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18703 DECL_CLONED_FUNCTION. */
18704 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18705 targ_ptr, complain);
18706 if (spec == error_mark_node)
18707 return error_mark_node;
18708
18709 /* Look for the clone. */
18710 FOR_EACH_CLONE (clone, spec)
18711 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18712 return clone;
18713 /* We should always have found the clone by now. */
18714 gcc_unreachable ();
18715 return NULL_TREE;
18716 }
18717
18718 if (targ_ptr == error_mark_node)
18719 return error_mark_node;
18720
18721 /* Check to see if we already have this specialization. */
18722 gen_tmpl = most_general_template (tmpl);
18723 if (TMPL_ARGS_DEPTH (targ_ptr)
18724 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18725 /* targ_ptr only has the innermost template args, so add the outer ones
18726 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18727 the case of a non-dependent call within a template definition). */
18728 targ_ptr = (add_outermost_template_args
18729 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18730 targ_ptr));
18731
18732 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18733 but it doesn't seem to be on the hot path. */
18734 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18735
18736 gcc_assert (tmpl == gen_tmpl
18737 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18738 == spec)
18739 || fndecl == NULL_TREE);
18740
18741 if (spec != NULL_TREE)
18742 {
18743 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18744 {
18745 if (complain & tf_error)
18746 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18747 return error_mark_node;
18748 }
18749 return spec;
18750 }
18751
18752 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18753 complain))
18754 return error_mark_node;
18755
18756 /* We are building a FUNCTION_DECL, during which the access of its
18757 parameters and return types have to be checked. However this
18758 FUNCTION_DECL which is the desired context for access checking
18759 is not built yet. We solve this chicken-and-egg problem by
18760 deferring all checks until we have the FUNCTION_DECL. */
18761 push_deferring_access_checks (dk_deferred);
18762
18763 /* Instantiation of the function happens in the context of the function
18764 template, not the context of the overload resolution we're doing. */
18765 push_to_top_level ();
18766 /* If there are dependent arguments, e.g. because we're doing partial
18767 ordering, make sure processing_template_decl stays set. */
18768 if (uses_template_parms (targ_ptr))
18769 ++processing_template_decl;
18770 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18771 {
18772 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18773 complain, gen_tmpl, true);
18774 push_nested_class (ctx);
18775 }
18776
18777 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18778
18779 fndecl = NULL_TREE;
18780 if (VAR_P (pattern))
18781 {
18782 /* We need to determine if we're using a partial or explicit
18783 specialization now, because the type of the variable could be
18784 different. */
18785 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18786 tree elt = most_specialized_partial_spec (tid, complain);
18787 if (elt == error_mark_node)
18788 pattern = error_mark_node;
18789 else if (elt)
18790 {
18791 tree partial_tmpl = TREE_VALUE (elt);
18792 tree partial_args = TREE_PURPOSE (elt);
18793 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18794 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18795 }
18796 }
18797
18798 /* Substitute template parameters to obtain the specialization. */
18799 if (fndecl == NULL_TREE)
18800 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18801 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18802 pop_nested_class ();
18803 pop_from_top_level ();
18804
18805 if (fndecl == error_mark_node)
18806 {
18807 pop_deferring_access_checks ();
18808 return error_mark_node;
18809 }
18810
18811 /* The DECL_TI_TEMPLATE should always be the immediate parent
18812 template, not the most general template. */
18813 DECL_TI_TEMPLATE (fndecl) = tmpl;
18814 DECL_TI_ARGS (fndecl) = targ_ptr;
18815
18816 /* Now we know the specialization, compute access previously
18817 deferred. Do no access control for inheriting constructors,
18818 as we already checked access for the inherited constructor. */
18819 if (!(flag_new_inheriting_ctors
18820 && DECL_INHERITED_CTOR (fndecl)))
18821 {
18822 push_access_scope (fndecl);
18823 if (!perform_deferred_access_checks (complain))
18824 access_ok = false;
18825 pop_access_scope (fndecl);
18826 }
18827 pop_deferring_access_checks ();
18828
18829 /* If we've just instantiated the main entry point for a function,
18830 instantiate all the alternate entry points as well. We do this
18831 by cloning the instantiation of the main entry point, not by
18832 instantiating the template clones. */
18833 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18834 clone_function_decl (fndecl, /*update_methods=*/false);
18835
18836 if (!access_ok)
18837 {
18838 if (!(complain & tf_error))
18839 {
18840 /* Remember to reinstantiate when we're out of SFINAE so the user
18841 can see the errors. */
18842 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18843 }
18844 return error_mark_node;
18845 }
18846 return fndecl;
18847 }
18848
18849 /* Wrapper for instantiate_template_1. */
18850
18851 tree
18852 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18853 {
18854 tree ret;
18855 timevar_push (TV_TEMPLATE_INST);
18856 ret = instantiate_template_1 (tmpl, orig_args, complain);
18857 timevar_pop (TV_TEMPLATE_INST);
18858 return ret;
18859 }
18860
18861 /* Instantiate the alias template TMPL with ARGS. Also push a template
18862 instantiation level, which instantiate_template doesn't do because
18863 functions and variables have sufficient context established by the
18864 callers. */
18865
18866 static tree
18867 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18868 {
18869 struct pending_template *old_last_pend = last_pending_template;
18870 struct tinst_level *old_error_tinst = last_error_tinst_level;
18871 if (tmpl == error_mark_node || args == error_mark_node)
18872 return error_mark_node;
18873 tree tinst = build_tree_list (tmpl, args);
18874 if (!push_tinst_level (tinst))
18875 {
18876 ggc_free (tinst);
18877 return error_mark_node;
18878 }
18879
18880 args =
18881 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18882 args, tmpl, complain,
18883 /*require_all_args=*/true,
18884 /*use_default_args=*/true);
18885
18886 tree r = instantiate_template (tmpl, args, complain);
18887 pop_tinst_level ();
18888 /* We can't free this if a pending_template entry or last_error_tinst_level
18889 is pointing at it. */
18890 if (last_pending_template == old_last_pend
18891 && last_error_tinst_level == old_error_tinst)
18892 ggc_free (tinst);
18893
18894 return r;
18895 }
18896
18897 /* PARM is a template parameter pack for FN. Returns true iff
18898 PARM is used in a deducible way in the argument list of FN. */
18899
18900 static bool
18901 pack_deducible_p (tree parm, tree fn)
18902 {
18903 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18904 for (; t; t = TREE_CHAIN (t))
18905 {
18906 tree type = TREE_VALUE (t);
18907 tree packs;
18908 if (!PACK_EXPANSION_P (type))
18909 continue;
18910 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18911 packs; packs = TREE_CHAIN (packs))
18912 if (template_args_equal (TREE_VALUE (packs), parm))
18913 {
18914 /* The template parameter pack is used in a function parameter
18915 pack. If this is the end of the parameter list, the
18916 template parameter pack is deducible. */
18917 if (TREE_CHAIN (t) == void_list_node)
18918 return true;
18919 else
18920 /* Otherwise, not. Well, it could be deduced from
18921 a non-pack parameter, but doing so would end up with
18922 a deduction mismatch, so don't bother. */
18923 return false;
18924 }
18925 }
18926 /* The template parameter pack isn't used in any function parameter
18927 packs, but it might be used deeper, e.g. tuple<Args...>. */
18928 return true;
18929 }
18930
18931 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18932 NARGS elements of the arguments that are being used when calling
18933 it. TARGS is a vector into which the deduced template arguments
18934 are placed.
18935
18936 Returns either a FUNCTION_DECL for the matching specialization of FN or
18937 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18938 true, diagnostics will be printed to explain why it failed.
18939
18940 If FN is a conversion operator, or we are trying to produce a specific
18941 specialization, RETURN_TYPE is the return type desired.
18942
18943 The EXPLICIT_TARGS are explicit template arguments provided via a
18944 template-id.
18945
18946 The parameter STRICT is one of:
18947
18948 DEDUCE_CALL:
18949 We are deducing arguments for a function call, as in
18950 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18951 deducing arguments for a call to the result of a conversion
18952 function template, as in [over.call.object].
18953
18954 DEDUCE_CONV:
18955 We are deducing arguments for a conversion function, as in
18956 [temp.deduct.conv].
18957
18958 DEDUCE_EXACT:
18959 We are deducing arguments when doing an explicit instantiation
18960 as in [temp.explicit], when determining an explicit specialization
18961 as in [temp.expl.spec], or when taking the address of a function
18962 template, as in [temp.deduct.funcaddr]. */
18963
18964 tree
18965 fn_type_unification (tree fn,
18966 tree explicit_targs,
18967 tree targs,
18968 const tree *args,
18969 unsigned int nargs,
18970 tree return_type,
18971 unification_kind_t strict,
18972 int flags,
18973 bool explain_p,
18974 bool decltype_p)
18975 {
18976 tree parms;
18977 tree fntype;
18978 tree decl = NULL_TREE;
18979 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18980 bool ok;
18981 static int deduction_depth;
18982 struct pending_template *old_last_pend = last_pending_template;
18983 struct tinst_level *old_error_tinst = last_error_tinst_level;
18984
18985 tree orig_fn = fn;
18986 if (flag_new_inheriting_ctors)
18987 fn = strip_inheriting_ctors (fn);
18988
18989 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18990 tree tinst;
18991 tree r = error_mark_node;
18992
18993 tree full_targs = targs;
18994 if (TMPL_ARGS_DEPTH (targs)
18995 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18996 full_targs = (add_outermost_template_args
18997 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18998 targs));
18999
19000 if (decltype_p)
19001 complain |= tf_decltype;
19002
19003 /* In C++0x, it's possible to have a function template whose type depends
19004 on itself recursively. This is most obvious with decltype, but can also
19005 occur with enumeration scope (c++/48969). So we need to catch infinite
19006 recursion and reject the substitution at deduction time; this function
19007 will return error_mark_node for any repeated substitution.
19008
19009 This also catches excessive recursion such as when f<N> depends on
19010 f<N-1> across all integers, and returns error_mark_node for all the
19011 substitutions back up to the initial one.
19012
19013 This is, of course, not reentrant. */
19014 if (excessive_deduction_depth)
19015 return error_mark_node;
19016 tinst = build_tree_list (fn, NULL_TREE);
19017 ++deduction_depth;
19018
19019 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19020
19021 fntype = TREE_TYPE (fn);
19022 if (explicit_targs)
19023 {
19024 /* [temp.deduct]
19025
19026 The specified template arguments must match the template
19027 parameters in kind (i.e., type, nontype, template), and there
19028 must not be more arguments than there are parameters;
19029 otherwise type deduction fails.
19030
19031 Nontype arguments must match the types of the corresponding
19032 nontype template parameters, or must be convertible to the
19033 types of the corresponding nontype parameters as specified in
19034 _temp.arg.nontype_, otherwise type deduction fails.
19035
19036 All references in the function type of the function template
19037 to the corresponding template parameters are replaced by the
19038 specified template argument values. If a substitution in a
19039 template parameter or in the function type of the function
19040 template results in an invalid type, type deduction fails. */
19041 int i, len = TREE_VEC_LENGTH (tparms);
19042 location_t loc = input_location;
19043 bool incomplete = false;
19044
19045 if (explicit_targs == error_mark_node)
19046 goto fail;
19047
19048 if (TMPL_ARGS_DEPTH (explicit_targs)
19049 < TMPL_ARGS_DEPTH (full_targs))
19050 explicit_targs = add_outermost_template_args (full_targs,
19051 explicit_targs);
19052
19053 /* Adjust any explicit template arguments before entering the
19054 substitution context. */
19055 explicit_targs
19056 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19057 complain,
19058 /*require_all_args=*/false,
19059 /*use_default_args=*/false));
19060 if (explicit_targs == error_mark_node)
19061 goto fail;
19062
19063 /* Substitute the explicit args into the function type. This is
19064 necessary so that, for instance, explicitly declared function
19065 arguments can match null pointed constants. If we were given
19066 an incomplete set of explicit args, we must not do semantic
19067 processing during substitution as we could create partial
19068 instantiations. */
19069 for (i = 0; i < len; i++)
19070 {
19071 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19072 bool parameter_pack = false;
19073 tree targ = TREE_VEC_ELT (explicit_targs, i);
19074
19075 /* Dig out the actual parm. */
19076 if (TREE_CODE (parm) == TYPE_DECL
19077 || TREE_CODE (parm) == TEMPLATE_DECL)
19078 {
19079 parm = TREE_TYPE (parm);
19080 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19081 }
19082 else if (TREE_CODE (parm) == PARM_DECL)
19083 {
19084 parm = DECL_INITIAL (parm);
19085 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19086 }
19087
19088 if (!parameter_pack && targ == NULL_TREE)
19089 /* No explicit argument for this template parameter. */
19090 incomplete = true;
19091
19092 if (parameter_pack && pack_deducible_p (parm, fn))
19093 {
19094 /* Mark the argument pack as "incomplete". We could
19095 still deduce more arguments during unification.
19096 We remove this mark in type_unification_real. */
19097 if (targ)
19098 {
19099 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19100 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19101 = ARGUMENT_PACK_ARGS (targ);
19102 }
19103
19104 /* We have some incomplete argument packs. */
19105 incomplete = true;
19106 }
19107 }
19108
19109 TREE_VALUE (tinst) = explicit_targs;
19110 if (!push_tinst_level (tinst))
19111 {
19112 excessive_deduction_depth = true;
19113 goto fail;
19114 }
19115 processing_template_decl += incomplete;
19116 input_location = DECL_SOURCE_LOCATION (fn);
19117 /* Ignore any access checks; we'll see them again in
19118 instantiate_template and they might have the wrong
19119 access path at this point. */
19120 push_deferring_access_checks (dk_deferred);
19121 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19122 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19123 pop_deferring_access_checks ();
19124 input_location = loc;
19125 processing_template_decl -= incomplete;
19126 pop_tinst_level ();
19127
19128 if (fntype == error_mark_node)
19129 goto fail;
19130
19131 /* Place the explicitly specified arguments in TARGS. */
19132 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19133 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19134 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19135 }
19136
19137 /* Never do unification on the 'this' parameter. */
19138 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19139
19140 if (return_type && strict == DEDUCE_CALL)
19141 {
19142 /* We're deducing for a call to the result of a template conversion
19143 function. The parms we really want are in return_type. */
19144 if (POINTER_TYPE_P (return_type))
19145 return_type = TREE_TYPE (return_type);
19146 parms = TYPE_ARG_TYPES (return_type);
19147 }
19148 else if (return_type)
19149 {
19150 tree *new_args;
19151
19152 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19153 new_args = XALLOCAVEC (tree, nargs + 1);
19154 new_args[0] = return_type;
19155 memcpy (new_args + 1, args, nargs * sizeof (tree));
19156 args = new_args;
19157 ++nargs;
19158 }
19159
19160 /* We allow incomplete unification without an error message here
19161 because the standard doesn't seem to explicitly prohibit it. Our
19162 callers must be ready to deal with unification failures in any
19163 event. */
19164
19165 TREE_VALUE (tinst) = targs;
19166 /* If we aren't explaining yet, push tinst context so we can see where
19167 any errors (e.g. from class instantiations triggered by instantiation
19168 of default template arguments) come from. If we are explaining, this
19169 context is redundant. */
19170 if (!explain_p && !push_tinst_level (tinst))
19171 {
19172 excessive_deduction_depth = true;
19173 goto fail;
19174 }
19175
19176 /* type_unification_real will pass back any access checks from default
19177 template argument substitution. */
19178 vec<deferred_access_check, va_gc> *checks;
19179 checks = NULL;
19180
19181 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19182 full_targs, parms, args, nargs, /*subr=*/0,
19183 strict, flags, &checks, explain_p);
19184 if (!explain_p)
19185 pop_tinst_level ();
19186 if (!ok)
19187 goto fail;
19188
19189 /* Now that we have bindings for all of the template arguments,
19190 ensure that the arguments deduced for the template template
19191 parameters have compatible template parameter lists. We cannot
19192 check this property before we have deduced all template
19193 arguments, because the template parameter types of a template
19194 template parameter might depend on prior template parameters
19195 deduced after the template template parameter. The following
19196 ill-formed example illustrates this issue:
19197
19198 template<typename T, template<T> class C> void f(C<5>, T);
19199
19200 template<int N> struct X {};
19201
19202 void g() {
19203 f(X<5>(), 5l); // error: template argument deduction fails
19204 }
19205
19206 The template parameter list of 'C' depends on the template type
19207 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19208 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19209 time that we deduce 'C'. */
19210 if (!template_template_parm_bindings_ok_p
19211 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19212 {
19213 unify_inconsistent_template_template_parameters (explain_p);
19214 goto fail;
19215 }
19216
19217 /* All is well so far. Now, check:
19218
19219 [temp.deduct]
19220
19221 When all template arguments have been deduced, all uses of
19222 template parameters in nondeduced contexts are replaced with
19223 the corresponding deduced argument values. If the
19224 substitution results in an invalid type, as described above,
19225 type deduction fails. */
19226 TREE_VALUE (tinst) = targs;
19227 if (!push_tinst_level (tinst))
19228 {
19229 excessive_deduction_depth = true;
19230 goto fail;
19231 }
19232
19233 /* Also collect access checks from the instantiation. */
19234 reopen_deferring_access_checks (checks);
19235
19236 decl = instantiate_template (fn, targs, complain);
19237
19238 checks = get_deferred_access_checks ();
19239 pop_deferring_access_checks ();
19240
19241 pop_tinst_level ();
19242
19243 if (decl == error_mark_node)
19244 goto fail;
19245
19246 /* Now perform any access checks encountered during substitution. */
19247 push_access_scope (decl);
19248 ok = perform_access_checks (checks, complain);
19249 pop_access_scope (decl);
19250 if (!ok)
19251 goto fail;
19252
19253 /* If we're looking for an exact match, check that what we got
19254 is indeed an exact match. It might not be if some template
19255 parameters are used in non-deduced contexts. But don't check
19256 for an exact match if we have dependent template arguments;
19257 in that case we're doing partial ordering, and we already know
19258 that we have two candidates that will provide the actual type. */
19259 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19260 {
19261 tree substed = TREE_TYPE (decl);
19262 unsigned int i;
19263
19264 tree sarg
19265 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19266 if (return_type)
19267 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19268 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19269 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19270 {
19271 unify_type_mismatch (explain_p, args[i],
19272 TREE_VALUE (sarg));
19273 goto fail;
19274 }
19275 }
19276
19277 /* After doing deduction with the inherited constructor, actually return an
19278 instantiation of the inheriting constructor. */
19279 if (orig_fn != fn)
19280 decl = instantiate_template (orig_fn, targs, complain);
19281
19282 r = decl;
19283
19284 fail:
19285 --deduction_depth;
19286 if (excessive_deduction_depth)
19287 {
19288 if (deduction_depth == 0)
19289 /* Reset once we're all the way out. */
19290 excessive_deduction_depth = false;
19291 }
19292
19293 /* We can't free this if a pending_template entry or last_error_tinst_level
19294 is pointing at it. */
19295 if (last_pending_template == old_last_pend
19296 && last_error_tinst_level == old_error_tinst)
19297 ggc_free (tinst);
19298
19299 return r;
19300 }
19301
19302 /* Adjust types before performing type deduction, as described in
19303 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19304 sections are symmetric. PARM is the type of a function parameter
19305 or the return type of the conversion function. ARG is the type of
19306 the argument passed to the call, or the type of the value
19307 initialized with the result of the conversion function.
19308 ARG_EXPR is the original argument expression, which may be null. */
19309
19310 static int
19311 maybe_adjust_types_for_deduction (unification_kind_t strict,
19312 tree* parm,
19313 tree* arg,
19314 tree arg_expr)
19315 {
19316 int result = 0;
19317
19318 switch (strict)
19319 {
19320 case DEDUCE_CALL:
19321 break;
19322
19323 case DEDUCE_CONV:
19324 /* Swap PARM and ARG throughout the remainder of this
19325 function; the handling is precisely symmetric since PARM
19326 will initialize ARG rather than vice versa. */
19327 std::swap (parm, arg);
19328 break;
19329
19330 case DEDUCE_EXACT:
19331 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19332 too, but here handle it by stripping the reference from PARM
19333 rather than by adding it to ARG. */
19334 if (TREE_CODE (*parm) == REFERENCE_TYPE
19335 && TYPE_REF_IS_RVALUE (*parm)
19336 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19337 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19338 && TREE_CODE (*arg) == REFERENCE_TYPE
19339 && !TYPE_REF_IS_RVALUE (*arg))
19340 *parm = TREE_TYPE (*parm);
19341 /* Nothing else to do in this case. */
19342 return 0;
19343
19344 default:
19345 gcc_unreachable ();
19346 }
19347
19348 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19349 {
19350 /* [temp.deduct.call]
19351
19352 If P is not a reference type:
19353
19354 --If A is an array type, the pointer type produced by the
19355 array-to-pointer standard conversion (_conv.array_) is
19356 used in place of A for type deduction; otherwise,
19357
19358 --If A is a function type, the pointer type produced by
19359 the function-to-pointer standard conversion
19360 (_conv.func_) is used in place of A for type deduction;
19361 otherwise,
19362
19363 --If A is a cv-qualified type, the top level
19364 cv-qualifiers of A's type are ignored for type
19365 deduction. */
19366 if (TREE_CODE (*arg) == ARRAY_TYPE)
19367 *arg = build_pointer_type (TREE_TYPE (*arg));
19368 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19369 *arg = build_pointer_type (*arg);
19370 else
19371 *arg = TYPE_MAIN_VARIANT (*arg);
19372 }
19373
19374 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19375 reference to a cv-unqualified template parameter that does not represent a
19376 template parameter of a class template (during class template argument
19377 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19378 an lvalue, the type "lvalue reference to A" is used in place of A for type
19379 deduction. */
19380 if (TREE_CODE (*parm) == REFERENCE_TYPE
19381 && TYPE_REF_IS_RVALUE (*parm)
19382 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19383 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19384 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19385 && (arg_expr ? lvalue_p (arg_expr)
19386 /* try_one_overload doesn't provide an arg_expr, but
19387 functions are always lvalues. */
19388 : TREE_CODE (*arg) == FUNCTION_TYPE))
19389 *arg = build_reference_type (*arg);
19390
19391 /* [temp.deduct.call]
19392
19393 If P is a cv-qualified type, the top level cv-qualifiers
19394 of P's type are ignored for type deduction. If P is a
19395 reference type, the type referred to by P is used for
19396 type deduction. */
19397 *parm = TYPE_MAIN_VARIANT (*parm);
19398 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19399 {
19400 *parm = TREE_TYPE (*parm);
19401 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19402 }
19403
19404 /* DR 322. For conversion deduction, remove a reference type on parm
19405 too (which has been swapped into ARG). */
19406 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19407 *arg = TREE_TYPE (*arg);
19408
19409 return result;
19410 }
19411
19412 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19413 template which does contain any deducible template parameters; check if
19414 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19415 unify_one_argument. */
19416
19417 static int
19418 check_non_deducible_conversion (tree parm, tree arg, int strict,
19419 int flags, bool explain_p)
19420 {
19421 tree type;
19422
19423 if (!TYPE_P (arg))
19424 type = TREE_TYPE (arg);
19425 else
19426 type = arg;
19427
19428 if (same_type_p (parm, type))
19429 return unify_success (explain_p);
19430
19431 if (strict == DEDUCE_CONV)
19432 {
19433 if (can_convert_arg (type, parm, NULL_TREE, flags,
19434 explain_p ? tf_warning_or_error : tf_none))
19435 return unify_success (explain_p);
19436 }
19437 else if (strict != DEDUCE_EXACT)
19438 {
19439 if (can_convert_arg (parm, type,
19440 TYPE_P (arg) ? NULL_TREE : arg,
19441 flags, explain_p ? tf_warning_or_error : tf_none))
19442 return unify_success (explain_p);
19443 }
19444
19445 if (strict == DEDUCE_EXACT)
19446 return unify_type_mismatch (explain_p, parm, arg);
19447 else
19448 return unify_arg_conversion (explain_p, parm, type, arg);
19449 }
19450
19451 static bool uses_deducible_template_parms (tree type);
19452
19453 /* Returns true iff the expression EXPR is one from which a template
19454 argument can be deduced. In other words, if it's an undecorated
19455 use of a template non-type parameter. */
19456
19457 static bool
19458 deducible_expression (tree expr)
19459 {
19460 /* Strip implicit conversions. */
19461 while (CONVERT_EXPR_P (expr))
19462 expr = TREE_OPERAND (expr, 0);
19463 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19464 }
19465
19466 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19467 deducible way; that is, if it has a max value of <PARM> - 1. */
19468
19469 static bool
19470 deducible_array_bound (tree domain)
19471 {
19472 if (domain == NULL_TREE)
19473 return false;
19474
19475 tree max = TYPE_MAX_VALUE (domain);
19476 if (TREE_CODE (max) != MINUS_EXPR)
19477 return false;
19478
19479 return deducible_expression (TREE_OPERAND (max, 0));
19480 }
19481
19482 /* Returns true iff the template arguments ARGS use a template parameter
19483 in a deducible way. */
19484
19485 static bool
19486 deducible_template_args (tree args)
19487 {
19488 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19489 {
19490 bool deducible;
19491 tree elt = TREE_VEC_ELT (args, i);
19492 if (ARGUMENT_PACK_P (elt))
19493 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19494 else
19495 {
19496 if (PACK_EXPANSION_P (elt))
19497 elt = PACK_EXPANSION_PATTERN (elt);
19498 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19499 deducible = true;
19500 else if (TYPE_P (elt))
19501 deducible = uses_deducible_template_parms (elt);
19502 else
19503 deducible = deducible_expression (elt);
19504 }
19505 if (deducible)
19506 return true;
19507 }
19508 return false;
19509 }
19510
19511 /* Returns true iff TYPE contains any deducible references to template
19512 parameters, as per 14.8.2.5. */
19513
19514 static bool
19515 uses_deducible_template_parms (tree type)
19516 {
19517 if (PACK_EXPANSION_P (type))
19518 type = PACK_EXPANSION_PATTERN (type);
19519
19520 /* T
19521 cv-list T
19522 TT<T>
19523 TT<i>
19524 TT<> */
19525 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19526 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19527 return true;
19528
19529 /* T*
19530 T&
19531 T&& */
19532 if (POINTER_TYPE_P (type))
19533 return uses_deducible_template_parms (TREE_TYPE (type));
19534
19535 /* T[integer-constant ]
19536 type [i] */
19537 if (TREE_CODE (type) == ARRAY_TYPE)
19538 return (uses_deducible_template_parms (TREE_TYPE (type))
19539 || deducible_array_bound (TYPE_DOMAIN (type)));
19540
19541 /* T type ::*
19542 type T::*
19543 T T::*
19544 T (type ::*)()
19545 type (T::*)()
19546 type (type ::*)(T)
19547 type (T::*)(T)
19548 T (type ::*)(T)
19549 T (T::*)()
19550 T (T::*)(T) */
19551 if (TYPE_PTRMEM_P (type))
19552 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19553 || (uses_deducible_template_parms
19554 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19555
19556 /* template-name <T> (where template-name refers to a class template)
19557 template-name <i> (where template-name refers to a class template) */
19558 if (CLASS_TYPE_P (type)
19559 && CLASSTYPE_TEMPLATE_INFO (type)
19560 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19561 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19562 (CLASSTYPE_TI_ARGS (type)));
19563
19564 /* type (T)
19565 T()
19566 T(T) */
19567 if (TREE_CODE (type) == FUNCTION_TYPE
19568 || TREE_CODE (type) == METHOD_TYPE)
19569 {
19570 if (uses_deducible_template_parms (TREE_TYPE (type)))
19571 return true;
19572 tree parm = TYPE_ARG_TYPES (type);
19573 if (TREE_CODE (type) == METHOD_TYPE)
19574 parm = TREE_CHAIN (parm);
19575 for (; parm; parm = TREE_CHAIN (parm))
19576 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19577 return true;
19578 }
19579
19580 return false;
19581 }
19582
19583 /* Subroutine of type_unification_real and unify_pack_expansion to
19584 handle unification of a single P/A pair. Parameters are as
19585 for those functions. */
19586
19587 static int
19588 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19589 int subr, unification_kind_t strict,
19590 bool explain_p)
19591 {
19592 tree arg_expr = NULL_TREE;
19593 int arg_strict;
19594
19595 if (arg == error_mark_node || parm == error_mark_node)
19596 return unify_invalid (explain_p);
19597 if (arg == unknown_type_node)
19598 /* We can't deduce anything from this, but we might get all the
19599 template args from other function args. */
19600 return unify_success (explain_p);
19601
19602 /* Implicit conversions (Clause 4) will be performed on a function
19603 argument to convert it to the type of the corresponding function
19604 parameter if the parameter type contains no template-parameters that
19605 participate in template argument deduction. */
19606 if (strict != DEDUCE_EXACT
19607 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19608 /* For function parameters with no deducible template parameters,
19609 just return. We'll check non-dependent conversions later. */
19610 return unify_success (explain_p);
19611
19612 switch (strict)
19613 {
19614 case DEDUCE_CALL:
19615 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19616 | UNIFY_ALLOW_MORE_CV_QUAL
19617 | UNIFY_ALLOW_DERIVED);
19618 break;
19619
19620 case DEDUCE_CONV:
19621 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19622 break;
19623
19624 case DEDUCE_EXACT:
19625 arg_strict = UNIFY_ALLOW_NONE;
19626 break;
19627
19628 default:
19629 gcc_unreachable ();
19630 }
19631
19632 /* We only do these transformations if this is the top-level
19633 parameter_type_list in a call or declaration matching; in other
19634 situations (nested function declarators, template argument lists) we
19635 won't be comparing a type to an expression, and we don't do any type
19636 adjustments. */
19637 if (!subr)
19638 {
19639 if (!TYPE_P (arg))
19640 {
19641 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19642 if (type_unknown_p (arg))
19643 {
19644 /* [temp.deduct.type] A template-argument can be
19645 deduced from a pointer to function or pointer
19646 to member function argument if the set of
19647 overloaded functions does not contain function
19648 templates and at most one of a set of
19649 overloaded functions provides a unique
19650 match. */
19651 resolve_overloaded_unification (tparms, targs, parm,
19652 arg, strict,
19653 arg_strict, explain_p);
19654 /* If a unique match was not found, this is a
19655 non-deduced context, so we still succeed. */
19656 return unify_success (explain_p);
19657 }
19658
19659 arg_expr = arg;
19660 arg = unlowered_expr_type (arg);
19661 if (arg == error_mark_node)
19662 return unify_invalid (explain_p);
19663 }
19664
19665 arg_strict |=
19666 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19667 }
19668 else
19669 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19670 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19671 return unify_template_argument_mismatch (explain_p, parm, arg);
19672
19673 /* For deduction from an init-list we need the actual list. */
19674 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19675 arg = arg_expr;
19676 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19677 }
19678
19679 /* for_each_template_parm callback that always returns 0. */
19680
19681 static int
19682 zero_r (tree, void *)
19683 {
19684 return 0;
19685 }
19686
19687 /* for_each_template_parm any_fn callback to handle deduction of a template
19688 type argument from the type of an array bound. */
19689
19690 static int
19691 array_deduction_r (tree t, void *data)
19692 {
19693 tree_pair_p d = (tree_pair_p)data;
19694 tree &tparms = d->purpose;
19695 tree &targs = d->value;
19696
19697 if (TREE_CODE (t) == ARRAY_TYPE)
19698 if (tree dom = TYPE_DOMAIN (t))
19699 if (tree max = TYPE_MAX_VALUE (dom))
19700 {
19701 if (TREE_CODE (max) == MINUS_EXPR)
19702 max = TREE_OPERAND (max, 0);
19703 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19704 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19705 UNIFY_ALLOW_NONE, /*explain*/false);
19706 }
19707
19708 /* Keep walking. */
19709 return 0;
19710 }
19711
19712 /* Try to deduce any not-yet-deduced template type arguments from the type of
19713 an array bound. This is handled separately from unify because 14.8.2.5 says
19714 "The type of a type parameter is only deduced from an array bound if it is
19715 not otherwise deduced." */
19716
19717 static void
19718 try_array_deduction (tree tparms, tree targs, tree parm)
19719 {
19720 tree_pair_s data = { tparms, targs };
19721 hash_set<tree> visited;
19722 for_each_template_parm (parm, zero_r, &data, &visited,
19723 /*nondeduced*/false, array_deduction_r);
19724 }
19725
19726 /* Most parms like fn_type_unification.
19727
19728 If SUBR is 1, we're being called recursively (to unify the
19729 arguments of a function or method parameter of a function
19730 template).
19731
19732 CHECKS is a pointer to a vector of access checks encountered while
19733 substituting default template arguments. */
19734
19735 static int
19736 type_unification_real (tree tparms,
19737 tree full_targs,
19738 tree xparms,
19739 const tree *xargs,
19740 unsigned int xnargs,
19741 int subr,
19742 unification_kind_t strict,
19743 int flags,
19744 vec<deferred_access_check, va_gc> **checks,
19745 bool explain_p)
19746 {
19747 tree parm, arg;
19748 int i;
19749 int ntparms = TREE_VEC_LENGTH (tparms);
19750 int saw_undeduced = 0;
19751 tree parms;
19752 const tree *args;
19753 unsigned int nargs;
19754 unsigned int ia;
19755
19756 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19757 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19758 gcc_assert (ntparms > 0);
19759
19760 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19761
19762 /* Reset the number of non-defaulted template arguments contained
19763 in TARGS. */
19764 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19765
19766 again:
19767 parms = xparms;
19768 args = xargs;
19769 nargs = xnargs;
19770
19771 ia = 0;
19772 while (parms && parms != void_list_node
19773 && ia < nargs)
19774 {
19775 parm = TREE_VALUE (parms);
19776
19777 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19778 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19779 /* For a function parameter pack that occurs at the end of the
19780 parameter-declaration-list, the type A of each remaining
19781 argument of the call is compared with the type P of the
19782 declarator-id of the function parameter pack. */
19783 break;
19784
19785 parms = TREE_CHAIN (parms);
19786
19787 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19788 /* For a function parameter pack that does not occur at the
19789 end of the parameter-declaration-list, the type of the
19790 parameter pack is a non-deduced context. */
19791 continue;
19792
19793 arg = args[ia];
19794 ++ia;
19795
19796 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19797 explain_p))
19798 return 1;
19799 }
19800
19801 if (parms
19802 && parms != void_list_node
19803 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19804 {
19805 /* Unify the remaining arguments with the pack expansion type. */
19806 tree argvec;
19807 tree parmvec = make_tree_vec (1);
19808
19809 /* Allocate a TREE_VEC and copy in all of the arguments */
19810 argvec = make_tree_vec (nargs - ia);
19811 for (i = 0; ia < nargs; ++ia, ++i)
19812 TREE_VEC_ELT (argvec, i) = args[ia];
19813
19814 /* Copy the parameter into parmvec. */
19815 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19816 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19817 /*subr=*/subr, explain_p))
19818 return 1;
19819
19820 /* Advance to the end of the list of parameters. */
19821 parms = TREE_CHAIN (parms);
19822 }
19823
19824 /* Fail if we've reached the end of the parm list, and more args
19825 are present, and the parm list isn't variadic. */
19826 if (ia < nargs && parms == void_list_node)
19827 return unify_too_many_arguments (explain_p, nargs, ia);
19828 /* Fail if parms are left and they don't have default values and
19829 they aren't all deduced as empty packs (c++/57397). This is
19830 consistent with sufficient_parms_p. */
19831 if (parms && parms != void_list_node
19832 && TREE_PURPOSE (parms) == NULL_TREE)
19833 {
19834 unsigned int count = nargs;
19835 tree p = parms;
19836 bool type_pack_p;
19837 do
19838 {
19839 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19840 if (!type_pack_p)
19841 count++;
19842 p = TREE_CHAIN (p);
19843 }
19844 while (p && p != void_list_node);
19845 if (count != nargs)
19846 return unify_too_few_arguments (explain_p, ia, count,
19847 type_pack_p);
19848 }
19849
19850 if (!subr)
19851 {
19852 tsubst_flags_t complain = (explain_p
19853 ? tf_warning_or_error
19854 : tf_none);
19855 bool tried_array_deduction = (cxx_dialect < cxx17);
19856
19857 for (i = 0; i < ntparms; i++)
19858 {
19859 tree targ = TREE_VEC_ELT (targs, i);
19860 tree tparm = TREE_VEC_ELT (tparms, i);
19861
19862 /* Clear the "incomplete" flags on all argument packs now so that
19863 substituting them into later default arguments works. */
19864 if (targ && ARGUMENT_PACK_P (targ))
19865 {
19866 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19867 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19868 }
19869
19870 if (targ || tparm == error_mark_node)
19871 continue;
19872 tparm = TREE_VALUE (tparm);
19873
19874 if (TREE_CODE (tparm) == TYPE_DECL
19875 && !tried_array_deduction)
19876 {
19877 try_array_deduction (tparms, targs, xparms);
19878 tried_array_deduction = true;
19879 if (TREE_VEC_ELT (targs, i))
19880 continue;
19881 }
19882
19883 /* If this is an undeduced nontype parameter that depends on
19884 a type parameter, try another pass; its type may have been
19885 deduced from a later argument than the one from which
19886 this parameter can be deduced. */
19887 if (TREE_CODE (tparm) == PARM_DECL
19888 && uses_template_parms (TREE_TYPE (tparm))
19889 && saw_undeduced < 2)
19890 {
19891 saw_undeduced = 1;
19892 continue;
19893 }
19894
19895 /* Core issue #226 (C++0x) [temp.deduct]:
19896
19897 If a template argument has not been deduced, its
19898 default template argument, if any, is used.
19899
19900 When we are in C++98 mode, TREE_PURPOSE will either
19901 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19902 to explicitly check cxx_dialect here. */
19903 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19904 /* OK, there is a default argument. Wait until after the
19905 conversion check to do substitution. */
19906 continue;
19907
19908 /* If the type parameter is a parameter pack, then it will
19909 be deduced to an empty parameter pack. */
19910 if (template_parameter_pack_p (tparm))
19911 {
19912 tree arg;
19913
19914 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19915 {
19916 arg = make_node (NONTYPE_ARGUMENT_PACK);
19917 TREE_CONSTANT (arg) = 1;
19918 }
19919 else
19920 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19921
19922 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19923
19924 TREE_VEC_ELT (targs, i) = arg;
19925 continue;
19926 }
19927
19928 return unify_parameter_deduction_failure (explain_p, tparm);
19929 }
19930
19931 /* DR 1391: All parameters have args, now check non-dependent parms for
19932 convertibility. */
19933 if (saw_undeduced < 2)
19934 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19935 parms && parms != void_list_node && ia < nargs; )
19936 {
19937 parm = TREE_VALUE (parms);
19938
19939 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19940 && (!TREE_CHAIN (parms)
19941 || TREE_CHAIN (parms) == void_list_node))
19942 /* For a function parameter pack that occurs at the end of the
19943 parameter-declaration-list, the type A of each remaining
19944 argument of the call is compared with the type P of the
19945 declarator-id of the function parameter pack. */
19946 break;
19947
19948 parms = TREE_CHAIN (parms);
19949
19950 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19951 /* For a function parameter pack that does not occur at the
19952 end of the parameter-declaration-list, the type of the
19953 parameter pack is a non-deduced context. */
19954 continue;
19955
19956 arg = args[ia];
19957 ++ia;
19958
19959 if (uses_template_parms (parm))
19960 continue;
19961 if (check_non_deducible_conversion (parm, arg, strict, flags,
19962 explain_p))
19963 return 1;
19964 }
19965
19966 /* Now substitute into the default template arguments. */
19967 for (i = 0; i < ntparms; i++)
19968 {
19969 tree targ = TREE_VEC_ELT (targs, i);
19970 tree tparm = TREE_VEC_ELT (tparms, i);
19971
19972 if (targ || tparm == error_mark_node)
19973 continue;
19974 tree parm = TREE_VALUE (tparm);
19975
19976 tsubst_flags_t fcomplain = complain;
19977 if (saw_undeduced == 1)
19978 {
19979 /* When saw_undeduced == 1, substitution into parm and arg might
19980 fail or not replace all template parameters, and that's
19981 fine. */
19982 fcomplain = tf_none;
19983 if (TREE_CODE (parm) == PARM_DECL
19984 && uses_template_parms (TREE_TYPE (parm)))
19985 continue;
19986 }
19987
19988 tree arg = TREE_PURPOSE (tparm);
19989 reopen_deferring_access_checks (*checks);
19990 location_t save_loc = input_location;
19991 if (DECL_P (parm))
19992 input_location = DECL_SOURCE_LOCATION (parm);
19993
19994 if (saw_undeduced == 1)
19995 ++processing_template_decl;
19996 arg = tsubst_template_arg (arg, full_targs, fcomplain, NULL_TREE);
19997 if (saw_undeduced == 1)
19998 --processing_template_decl;
19999
20000 if (arg != error_mark_node && !uses_template_parms (arg))
20001 arg = convert_template_argument (parm, arg, full_targs, complain,
20002 i, NULL_TREE);
20003 else if (saw_undeduced == 1)
20004 arg = NULL_TREE;
20005 else
20006 arg = error_mark_node;
20007 input_location = save_loc;
20008 *checks = get_deferred_access_checks ();
20009 pop_deferring_access_checks ();
20010 if (arg == error_mark_node)
20011 return 1;
20012 else if (arg)
20013 {
20014 TREE_VEC_ELT (targs, i) = arg;
20015 /* The position of the first default template argument,
20016 is also the number of non-defaulted arguments in TARGS.
20017 Record that. */
20018 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20019 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20020 }
20021 }
20022
20023 if (saw_undeduced++ == 1)
20024 goto again;
20025 }
20026
20027 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20028 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20029
20030 return unify_success (explain_p);
20031 }
20032
20033 /* Subroutine of type_unification_real. Args are like the variables
20034 at the call site. ARG is an overloaded function (or template-id);
20035 we try deducing template args from each of the overloads, and if
20036 only one succeeds, we go with that. Modifies TARGS and returns
20037 true on success. */
20038
20039 static bool
20040 resolve_overloaded_unification (tree tparms,
20041 tree targs,
20042 tree parm,
20043 tree arg,
20044 unification_kind_t strict,
20045 int sub_strict,
20046 bool explain_p)
20047 {
20048 tree tempargs = copy_node (targs);
20049 int good = 0;
20050 tree goodfn = NULL_TREE;
20051 bool addr_p;
20052
20053 if (TREE_CODE (arg) == ADDR_EXPR)
20054 {
20055 arg = TREE_OPERAND (arg, 0);
20056 addr_p = true;
20057 }
20058 else
20059 addr_p = false;
20060
20061 if (TREE_CODE (arg) == COMPONENT_REF)
20062 /* Handle `&x' where `x' is some static or non-static member
20063 function name. */
20064 arg = TREE_OPERAND (arg, 1);
20065
20066 if (TREE_CODE (arg) == OFFSET_REF)
20067 arg = TREE_OPERAND (arg, 1);
20068
20069 /* Strip baselink information. */
20070 if (BASELINK_P (arg))
20071 arg = BASELINK_FUNCTIONS (arg);
20072
20073 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20074 {
20075 /* If we got some explicit template args, we need to plug them into
20076 the affected templates before we try to unify, in case the
20077 explicit args will completely resolve the templates in question. */
20078
20079 int ok = 0;
20080 tree expl_subargs = TREE_OPERAND (arg, 1);
20081 arg = TREE_OPERAND (arg, 0);
20082
20083 for (lkp_iterator iter (arg); iter; ++iter)
20084 {
20085 tree fn = *iter;
20086 tree subargs, elem;
20087
20088 if (TREE_CODE (fn) != TEMPLATE_DECL)
20089 continue;
20090
20091 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20092 expl_subargs, NULL_TREE, tf_none,
20093 /*require_all_args=*/true,
20094 /*use_default_args=*/true);
20095 if (subargs != error_mark_node
20096 && !any_dependent_template_arguments_p (subargs))
20097 {
20098 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20099 if (try_one_overload (tparms, targs, tempargs, parm,
20100 elem, strict, sub_strict, addr_p, explain_p)
20101 && (!goodfn || !same_type_p (goodfn, elem)))
20102 {
20103 goodfn = elem;
20104 ++good;
20105 }
20106 }
20107 else if (subargs)
20108 ++ok;
20109 }
20110 /* If no templates (or more than one) are fully resolved by the
20111 explicit arguments, this template-id is a non-deduced context; it
20112 could still be OK if we deduce all template arguments for the
20113 enclosing call through other arguments. */
20114 if (good != 1)
20115 good = ok;
20116 }
20117 else if (TREE_CODE (arg) != OVERLOAD
20118 && TREE_CODE (arg) != FUNCTION_DECL)
20119 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20120 -- but the deduction does not succeed because the expression is
20121 not just the function on its own. */
20122 return false;
20123 else
20124 for (lkp_iterator iter (arg); iter; ++iter)
20125 {
20126 tree fn = *iter;
20127 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20128 strict, sub_strict, addr_p, explain_p)
20129 && (!goodfn || !decls_match (goodfn, fn)))
20130 {
20131 goodfn = fn;
20132 ++good;
20133 }
20134 }
20135
20136 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20137 to function or pointer to member function argument if the set of
20138 overloaded functions does not contain function templates and at most
20139 one of a set of overloaded functions provides a unique match.
20140
20141 So if we found multiple possibilities, we return success but don't
20142 deduce anything. */
20143
20144 if (good == 1)
20145 {
20146 int i = TREE_VEC_LENGTH (targs);
20147 for (; i--; )
20148 if (TREE_VEC_ELT (tempargs, i))
20149 {
20150 tree old = TREE_VEC_ELT (targs, i);
20151 tree new_ = TREE_VEC_ELT (tempargs, i);
20152 if (new_ && old && ARGUMENT_PACK_P (old)
20153 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20154 /* Don't forget explicit template arguments in a pack. */
20155 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20156 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20157 TREE_VEC_ELT (targs, i) = new_;
20158 }
20159 }
20160 if (good)
20161 return true;
20162
20163 return false;
20164 }
20165
20166 /* Core DR 115: In contexts where deduction is done and fails, or in
20167 contexts where deduction is not done, if a template argument list is
20168 specified and it, along with any default template arguments, identifies
20169 a single function template specialization, then the template-id is an
20170 lvalue for the function template specialization. */
20171
20172 tree
20173 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20174 {
20175 tree expr, offset, baselink;
20176 bool addr;
20177
20178 if (!type_unknown_p (orig_expr))
20179 return orig_expr;
20180
20181 expr = orig_expr;
20182 addr = false;
20183 offset = NULL_TREE;
20184 baselink = NULL_TREE;
20185
20186 if (TREE_CODE (expr) == ADDR_EXPR)
20187 {
20188 expr = TREE_OPERAND (expr, 0);
20189 addr = true;
20190 }
20191 if (TREE_CODE (expr) == OFFSET_REF)
20192 {
20193 offset = expr;
20194 expr = TREE_OPERAND (expr, 1);
20195 }
20196 if (BASELINK_P (expr))
20197 {
20198 baselink = expr;
20199 expr = BASELINK_FUNCTIONS (expr);
20200 }
20201
20202 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20203 {
20204 int good = 0;
20205 tree goodfn = NULL_TREE;
20206
20207 /* If we got some explicit template args, we need to plug them into
20208 the affected templates before we try to unify, in case the
20209 explicit args will completely resolve the templates in question. */
20210
20211 tree expl_subargs = TREE_OPERAND (expr, 1);
20212 tree arg = TREE_OPERAND (expr, 0);
20213 tree badfn = NULL_TREE;
20214 tree badargs = NULL_TREE;
20215
20216 for (lkp_iterator iter (arg); iter; ++iter)
20217 {
20218 tree fn = *iter;
20219 tree subargs, elem;
20220
20221 if (TREE_CODE (fn) != TEMPLATE_DECL)
20222 continue;
20223
20224 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20225 expl_subargs, NULL_TREE, tf_none,
20226 /*require_all_args=*/true,
20227 /*use_default_args=*/true);
20228 if (subargs != error_mark_node
20229 && !any_dependent_template_arguments_p (subargs))
20230 {
20231 elem = instantiate_template (fn, subargs, tf_none);
20232 if (elem == error_mark_node)
20233 {
20234 badfn = fn;
20235 badargs = subargs;
20236 }
20237 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20238 {
20239 goodfn = elem;
20240 ++good;
20241 }
20242 }
20243 }
20244 if (good == 1)
20245 {
20246 mark_used (goodfn);
20247 expr = goodfn;
20248 if (baselink)
20249 expr = build_baselink (BASELINK_BINFO (baselink),
20250 BASELINK_ACCESS_BINFO (baselink),
20251 expr, BASELINK_OPTYPE (baselink));
20252 if (offset)
20253 {
20254 tree base
20255 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20256 expr = build_offset_ref (base, expr, addr, complain);
20257 }
20258 if (addr)
20259 expr = cp_build_addr_expr (expr, complain);
20260 return expr;
20261 }
20262 else if (good == 0 && badargs && (complain & tf_error))
20263 /* There were no good options and at least one bad one, so let the
20264 user know what the problem is. */
20265 instantiate_template (badfn, badargs, complain);
20266 }
20267 return orig_expr;
20268 }
20269
20270 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20271 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20272 different overloads deduce different arguments for a given parm.
20273 ADDR_P is true if the expression for which deduction is being
20274 performed was of the form "& fn" rather than simply "fn".
20275
20276 Returns 1 on success. */
20277
20278 static int
20279 try_one_overload (tree tparms,
20280 tree orig_targs,
20281 tree targs,
20282 tree parm,
20283 tree arg,
20284 unification_kind_t strict,
20285 int sub_strict,
20286 bool addr_p,
20287 bool explain_p)
20288 {
20289 int nargs;
20290 tree tempargs;
20291 int i;
20292
20293 if (arg == error_mark_node)
20294 return 0;
20295
20296 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20297 to function or pointer to member function argument if the set of
20298 overloaded functions does not contain function templates and at most
20299 one of a set of overloaded functions provides a unique match.
20300
20301 So if this is a template, just return success. */
20302
20303 if (uses_template_parms (arg))
20304 return 1;
20305
20306 if (TREE_CODE (arg) == METHOD_TYPE)
20307 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20308 else if (addr_p)
20309 arg = build_pointer_type (arg);
20310
20311 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20312
20313 /* We don't copy orig_targs for this because if we have already deduced
20314 some template args from previous args, unify would complain when we
20315 try to deduce a template parameter for the same argument, even though
20316 there isn't really a conflict. */
20317 nargs = TREE_VEC_LENGTH (targs);
20318 tempargs = make_tree_vec (nargs);
20319
20320 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20321 return 0;
20322
20323 /* First make sure we didn't deduce anything that conflicts with
20324 explicitly specified args. */
20325 for (i = nargs; i--; )
20326 {
20327 tree elt = TREE_VEC_ELT (tempargs, i);
20328 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20329
20330 if (!elt)
20331 /*NOP*/;
20332 else if (uses_template_parms (elt))
20333 /* Since we're unifying against ourselves, we will fill in
20334 template args used in the function parm list with our own
20335 template parms. Discard them. */
20336 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20337 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20338 {
20339 /* Check that the argument at each index of the deduced argument pack
20340 is equivalent to the corresponding explicitly specified argument.
20341 We may have deduced more arguments than were explicitly specified,
20342 and that's OK. */
20343
20344 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20345 that's wrong if we deduce the same argument pack from multiple
20346 function arguments: it's only incomplete the first time. */
20347
20348 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20349 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20350
20351 if (TREE_VEC_LENGTH (deduced_pack)
20352 < TREE_VEC_LENGTH (explicit_pack))
20353 return 0;
20354
20355 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20356 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20357 TREE_VEC_ELT (deduced_pack, j)))
20358 return 0;
20359 }
20360 else if (oldelt && !template_args_equal (oldelt, elt))
20361 return 0;
20362 }
20363
20364 for (i = nargs; i--; )
20365 {
20366 tree elt = TREE_VEC_ELT (tempargs, i);
20367
20368 if (elt)
20369 TREE_VEC_ELT (targs, i) = elt;
20370 }
20371
20372 return 1;
20373 }
20374
20375 /* PARM is a template class (perhaps with unbound template
20376 parameters). ARG is a fully instantiated type. If ARG can be
20377 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20378 TARGS are as for unify. */
20379
20380 static tree
20381 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20382 bool explain_p)
20383 {
20384 tree copy_of_targs;
20385
20386 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20387 return NULL_TREE;
20388 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20389 /* Matches anything. */;
20390 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20391 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20392 return NULL_TREE;
20393
20394 /* We need to make a new template argument vector for the call to
20395 unify. If we used TARGS, we'd clutter it up with the result of
20396 the attempted unification, even if this class didn't work out.
20397 We also don't want to commit ourselves to all the unifications
20398 we've already done, since unification is supposed to be done on
20399 an argument-by-argument basis. In other words, consider the
20400 following pathological case:
20401
20402 template <int I, int J, int K>
20403 struct S {};
20404
20405 template <int I, int J>
20406 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20407
20408 template <int I, int J, int K>
20409 void f(S<I, J, K>, S<I, I, I>);
20410
20411 void g() {
20412 S<0, 0, 0> s0;
20413 S<0, 1, 2> s2;
20414
20415 f(s0, s2);
20416 }
20417
20418 Now, by the time we consider the unification involving `s2', we
20419 already know that we must have `f<0, 0, 0>'. But, even though
20420 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20421 because there are two ways to unify base classes of S<0, 1, 2>
20422 with S<I, I, I>. If we kept the already deduced knowledge, we
20423 would reject the possibility I=1. */
20424 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20425
20426 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20427 {
20428 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20429 return NULL_TREE;
20430 return arg;
20431 }
20432
20433 /* If unification failed, we're done. */
20434 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20435 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20436 return NULL_TREE;
20437
20438 return arg;
20439 }
20440
20441 /* Given a template type PARM and a class type ARG, find the unique
20442 base type in ARG that is an instance of PARM. We do not examine
20443 ARG itself; only its base-classes. If there is not exactly one
20444 appropriate base class, return NULL_TREE. PARM may be the type of
20445 a partial specialization, as well as a plain template type. Used
20446 by unify. */
20447
20448 static enum template_base_result
20449 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20450 bool explain_p, tree *result)
20451 {
20452 tree rval = NULL_TREE;
20453 tree binfo;
20454
20455 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20456
20457 binfo = TYPE_BINFO (complete_type (arg));
20458 if (!binfo)
20459 {
20460 /* The type could not be completed. */
20461 *result = NULL_TREE;
20462 return tbr_incomplete_type;
20463 }
20464
20465 /* Walk in inheritance graph order. The search order is not
20466 important, and this avoids multiple walks of virtual bases. */
20467 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20468 {
20469 tree r = try_class_unification (tparms, targs, parm,
20470 BINFO_TYPE (binfo), explain_p);
20471
20472 if (r)
20473 {
20474 /* If there is more than one satisfactory baseclass, then:
20475
20476 [temp.deduct.call]
20477
20478 If they yield more than one possible deduced A, the type
20479 deduction fails.
20480
20481 applies. */
20482 if (rval && !same_type_p (r, rval))
20483 {
20484 *result = NULL_TREE;
20485 return tbr_ambiguous_baseclass;
20486 }
20487
20488 rval = r;
20489 }
20490 }
20491
20492 *result = rval;
20493 return tbr_success;
20494 }
20495
20496 /* Returns the level of DECL, which declares a template parameter. */
20497
20498 static int
20499 template_decl_level (tree decl)
20500 {
20501 switch (TREE_CODE (decl))
20502 {
20503 case TYPE_DECL:
20504 case TEMPLATE_DECL:
20505 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20506
20507 case PARM_DECL:
20508 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20509
20510 default:
20511 gcc_unreachable ();
20512 }
20513 return 0;
20514 }
20515
20516 /* Decide whether ARG can be unified with PARM, considering only the
20517 cv-qualifiers of each type, given STRICT as documented for unify.
20518 Returns nonzero iff the unification is OK on that basis. */
20519
20520 static int
20521 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20522 {
20523 int arg_quals = cp_type_quals (arg);
20524 int parm_quals = cp_type_quals (parm);
20525
20526 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20527 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20528 {
20529 /* Although a CVR qualifier is ignored when being applied to a
20530 substituted template parameter ([8.3.2]/1 for example), that
20531 does not allow us to unify "const T" with "int&" because both
20532 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20533 It is ok when we're allowing additional CV qualifiers
20534 at the outer level [14.8.2.1]/3,1st bullet. */
20535 if ((TREE_CODE (arg) == REFERENCE_TYPE
20536 || TREE_CODE (arg) == FUNCTION_TYPE
20537 || TREE_CODE (arg) == METHOD_TYPE)
20538 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20539 return 0;
20540
20541 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20542 && (parm_quals & TYPE_QUAL_RESTRICT))
20543 return 0;
20544 }
20545
20546 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20547 && (arg_quals & parm_quals) != parm_quals)
20548 return 0;
20549
20550 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20551 && (parm_quals & arg_quals) != arg_quals)
20552 return 0;
20553
20554 return 1;
20555 }
20556
20557 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20558 void
20559 template_parm_level_and_index (tree parm, int* level, int* index)
20560 {
20561 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20562 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20563 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20564 {
20565 *index = TEMPLATE_TYPE_IDX (parm);
20566 *level = TEMPLATE_TYPE_LEVEL (parm);
20567 }
20568 else
20569 {
20570 *index = TEMPLATE_PARM_IDX (parm);
20571 *level = TEMPLATE_PARM_LEVEL (parm);
20572 }
20573 }
20574
20575 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20576 do { \
20577 if (unify (TP, TA, P, A, S, EP)) \
20578 return 1; \
20579 } while (0)
20580
20581 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20582 expansion at the end of PACKED_PARMS. Returns 0 if the type
20583 deduction succeeds, 1 otherwise. STRICT is the same as in
20584 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20585 function call argument list. We'll need to adjust the arguments to make them
20586 types. SUBR tells us if this is from a recursive call to
20587 type_unification_real, or for comparing two template argument
20588 lists. */
20589
20590 static int
20591 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20592 tree packed_args, unification_kind_t strict,
20593 bool subr, bool explain_p)
20594 {
20595 tree parm
20596 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20597 tree pattern = PACK_EXPANSION_PATTERN (parm);
20598 tree pack, packs = NULL_TREE;
20599 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20600
20601 /* Add in any args remembered from an earlier partial instantiation. */
20602 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20603 int levels = TMPL_ARGS_DEPTH (targs);
20604
20605 packed_args = expand_template_argument_pack (packed_args);
20606
20607 int len = TREE_VEC_LENGTH (packed_args);
20608
20609 /* Determine the parameter packs we will be deducing from the
20610 pattern, and record their current deductions. */
20611 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20612 pack; pack = TREE_CHAIN (pack))
20613 {
20614 tree parm_pack = TREE_VALUE (pack);
20615 int idx, level;
20616
20617 /* Determine the index and level of this parameter pack. */
20618 template_parm_level_and_index (parm_pack, &level, &idx);
20619 if (level < levels)
20620 continue;
20621
20622 /* Keep track of the parameter packs and their corresponding
20623 argument packs. */
20624 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20625 TREE_TYPE (packs) = make_tree_vec (len - start);
20626 }
20627
20628 /* Loop through all of the arguments that have not yet been
20629 unified and unify each with the pattern. */
20630 for (i = start; i < len; i++)
20631 {
20632 tree parm;
20633 bool any_explicit = false;
20634 tree arg = TREE_VEC_ELT (packed_args, i);
20635
20636 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20637 or the element of its argument pack at the current index if
20638 this argument was explicitly specified. */
20639 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20640 {
20641 int idx, level;
20642 tree arg, pargs;
20643 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20644
20645 arg = NULL_TREE;
20646 if (TREE_VALUE (pack)
20647 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20648 && (i - start < TREE_VEC_LENGTH (pargs)))
20649 {
20650 any_explicit = true;
20651 arg = TREE_VEC_ELT (pargs, i - start);
20652 }
20653 TMPL_ARG (targs, level, idx) = arg;
20654 }
20655
20656 /* If we had explicit template arguments, substitute them into the
20657 pattern before deduction. */
20658 if (any_explicit)
20659 {
20660 /* Some arguments might still be unspecified or dependent. */
20661 bool dependent;
20662 ++processing_template_decl;
20663 dependent = any_dependent_template_arguments_p (targs);
20664 if (!dependent)
20665 --processing_template_decl;
20666 parm = tsubst (pattern, targs,
20667 explain_p ? tf_warning_or_error : tf_none,
20668 NULL_TREE);
20669 if (dependent)
20670 --processing_template_decl;
20671 if (parm == error_mark_node)
20672 return 1;
20673 }
20674 else
20675 parm = pattern;
20676
20677 /* Unify the pattern with the current argument. */
20678 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20679 explain_p))
20680 return 1;
20681
20682 /* For each parameter pack, collect the deduced value. */
20683 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20684 {
20685 int idx, level;
20686 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20687
20688 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20689 TMPL_ARG (targs, level, idx);
20690 }
20691 }
20692
20693 /* Verify that the results of unification with the parameter packs
20694 produce results consistent with what we've seen before, and make
20695 the deduced argument packs available. */
20696 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20697 {
20698 tree old_pack = TREE_VALUE (pack);
20699 tree new_args = TREE_TYPE (pack);
20700 int i, len = TREE_VEC_LENGTH (new_args);
20701 int idx, level;
20702 bool nondeduced_p = false;
20703
20704 /* By default keep the original deduced argument pack.
20705 If necessary, more specific code is going to update the
20706 resulting deduced argument later down in this function. */
20707 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20708 TMPL_ARG (targs, level, idx) = old_pack;
20709
20710 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20711 actually deduce anything. */
20712 for (i = 0; i < len && !nondeduced_p; ++i)
20713 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20714 nondeduced_p = true;
20715 if (nondeduced_p)
20716 continue;
20717
20718 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20719 {
20720 /* If we had fewer function args than explicit template args,
20721 just use the explicits. */
20722 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20723 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20724 if (len < explicit_len)
20725 new_args = explicit_args;
20726 }
20727
20728 if (!old_pack)
20729 {
20730 tree result;
20731 /* Build the deduced *_ARGUMENT_PACK. */
20732 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20733 {
20734 result = make_node (NONTYPE_ARGUMENT_PACK);
20735 TREE_CONSTANT (result) = 1;
20736 }
20737 else
20738 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20739
20740 SET_ARGUMENT_PACK_ARGS (result, new_args);
20741
20742 /* Note the deduced argument packs for this parameter
20743 pack. */
20744 TMPL_ARG (targs, level, idx) = result;
20745 }
20746 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20747 && (ARGUMENT_PACK_ARGS (old_pack)
20748 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20749 {
20750 /* We only had the explicitly-provided arguments before, but
20751 now we have a complete set of arguments. */
20752 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20753
20754 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20755 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20756 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20757 }
20758 else
20759 {
20760 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20761 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20762
20763 if (!comp_template_args (old_args, new_args,
20764 &bad_old_arg, &bad_new_arg))
20765 /* Inconsistent unification of this parameter pack. */
20766 return unify_parameter_pack_inconsistent (explain_p,
20767 bad_old_arg,
20768 bad_new_arg);
20769 }
20770 }
20771
20772 return unify_success (explain_p);
20773 }
20774
20775 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20776 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20777 parameters and return value are as for unify. */
20778
20779 static int
20780 unify_array_domain (tree tparms, tree targs,
20781 tree parm_dom, tree arg_dom,
20782 bool explain_p)
20783 {
20784 tree parm_max;
20785 tree arg_max;
20786 bool parm_cst;
20787 bool arg_cst;
20788
20789 /* Our representation of array types uses "N - 1" as the
20790 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20791 not an integer constant. We cannot unify arbitrarily
20792 complex expressions, so we eliminate the MINUS_EXPRs
20793 here. */
20794 parm_max = TYPE_MAX_VALUE (parm_dom);
20795 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20796 if (!parm_cst)
20797 {
20798 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20799 parm_max = TREE_OPERAND (parm_max, 0);
20800 }
20801 arg_max = TYPE_MAX_VALUE (arg_dom);
20802 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20803 if (!arg_cst)
20804 {
20805 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20806 trying to unify the type of a variable with the type
20807 of a template parameter. For example:
20808
20809 template <unsigned int N>
20810 void f (char (&) [N]);
20811 int g();
20812 void h(int i) {
20813 char a[g(i)];
20814 f(a);
20815 }
20816
20817 Here, the type of the ARG will be "int [g(i)]", and
20818 may be a SAVE_EXPR, etc. */
20819 if (TREE_CODE (arg_max) != MINUS_EXPR)
20820 return unify_vla_arg (explain_p, arg_dom);
20821 arg_max = TREE_OPERAND (arg_max, 0);
20822 }
20823
20824 /* If only one of the bounds used a MINUS_EXPR, compensate
20825 by adding one to the other bound. */
20826 if (parm_cst && !arg_cst)
20827 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20828 integer_type_node,
20829 parm_max,
20830 integer_one_node);
20831 else if (arg_cst && !parm_cst)
20832 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20833 integer_type_node,
20834 arg_max,
20835 integer_one_node);
20836
20837 return unify (tparms, targs, parm_max, arg_max,
20838 UNIFY_ALLOW_INTEGER, explain_p);
20839 }
20840
20841 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20842
20843 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20844
20845 static pa_kind_t
20846 pa_kind (tree t)
20847 {
20848 if (PACK_EXPANSION_P (t))
20849 t = PACK_EXPANSION_PATTERN (t);
20850 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20851 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20852 || DECL_TYPE_TEMPLATE_P (t))
20853 return pa_tmpl;
20854 else if (TYPE_P (t))
20855 return pa_type;
20856 else
20857 return pa_expr;
20858 }
20859
20860 /* Deduce the value of template parameters. TPARMS is the (innermost)
20861 set of template parameters to a template. TARGS is the bindings
20862 for those template parameters, as determined thus far; TARGS may
20863 include template arguments for outer levels of template parameters
20864 as well. PARM is a parameter to a template function, or a
20865 subcomponent of that parameter; ARG is the corresponding argument.
20866 This function attempts to match PARM with ARG in a manner
20867 consistent with the existing assignments in TARGS. If more values
20868 are deduced, then TARGS is updated.
20869
20870 Returns 0 if the type deduction succeeds, 1 otherwise. The
20871 parameter STRICT is a bitwise or of the following flags:
20872
20873 UNIFY_ALLOW_NONE:
20874 Require an exact match between PARM and ARG.
20875 UNIFY_ALLOW_MORE_CV_QUAL:
20876 Allow the deduced ARG to be more cv-qualified (by qualification
20877 conversion) than ARG.
20878 UNIFY_ALLOW_LESS_CV_QUAL:
20879 Allow the deduced ARG to be less cv-qualified than ARG.
20880 UNIFY_ALLOW_DERIVED:
20881 Allow the deduced ARG to be a template base class of ARG,
20882 or a pointer to a template base class of the type pointed to by
20883 ARG.
20884 UNIFY_ALLOW_INTEGER:
20885 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20886 case for more information.
20887 UNIFY_ALLOW_OUTER_LEVEL:
20888 This is the outermost level of a deduction. Used to determine validity
20889 of qualification conversions. A valid qualification conversion must
20890 have const qualified pointers leading up to the inner type which
20891 requires additional CV quals, except at the outer level, where const
20892 is not required [conv.qual]. It would be normal to set this flag in
20893 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20894 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20895 This is the outermost level of a deduction, and PARM can be more CV
20896 qualified at this point.
20897 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20898 This is the outermost level of a deduction, and PARM can be less CV
20899 qualified at this point. */
20900
20901 static int
20902 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20903 bool explain_p)
20904 {
20905 int idx;
20906 tree targ;
20907 tree tparm;
20908 int strict_in = strict;
20909 tsubst_flags_t complain = (explain_p
20910 ? tf_warning_or_error
20911 : tf_none);
20912
20913 /* I don't think this will do the right thing with respect to types.
20914 But the only case I've seen it in so far has been array bounds, where
20915 signedness is the only information lost, and I think that will be
20916 okay. */
20917 while (CONVERT_EXPR_P (parm))
20918 parm = TREE_OPERAND (parm, 0);
20919
20920 if (arg == error_mark_node)
20921 return unify_invalid (explain_p);
20922 if (arg == unknown_type_node
20923 || arg == init_list_type_node)
20924 /* We can't deduce anything from this, but we might get all the
20925 template args from other function args. */
20926 return unify_success (explain_p);
20927
20928 if (parm == any_targ_node || arg == any_targ_node)
20929 return unify_success (explain_p);
20930
20931 /* If PARM uses template parameters, then we can't bail out here,
20932 even if ARG == PARM, since we won't record unifications for the
20933 template parameters. We might need them if we're trying to
20934 figure out which of two things is more specialized. */
20935 if (arg == parm && !uses_template_parms (parm))
20936 return unify_success (explain_p);
20937
20938 /* Handle init lists early, so the rest of the function can assume
20939 we're dealing with a type. */
20940 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20941 {
20942 tree elt, elttype;
20943 unsigned i;
20944 tree orig_parm = parm;
20945
20946 /* Replace T with std::initializer_list<T> for deduction. */
20947 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20948 && flag_deduce_init_list)
20949 parm = listify (parm);
20950
20951 if (!is_std_init_list (parm)
20952 && TREE_CODE (parm) != ARRAY_TYPE)
20953 /* We can only deduce from an initializer list argument if the
20954 parameter is std::initializer_list or an array; otherwise this
20955 is a non-deduced context. */
20956 return unify_success (explain_p);
20957
20958 if (TREE_CODE (parm) == ARRAY_TYPE)
20959 elttype = TREE_TYPE (parm);
20960 else
20961 {
20962 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20963 /* Deduction is defined in terms of a single type, so just punt
20964 on the (bizarre) std::initializer_list<T...>. */
20965 if (PACK_EXPANSION_P (elttype))
20966 return unify_success (explain_p);
20967 }
20968
20969 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20970 {
20971 int elt_strict = strict;
20972
20973 if (elt == error_mark_node)
20974 return unify_invalid (explain_p);
20975
20976 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20977 {
20978 tree type = TREE_TYPE (elt);
20979 if (type == error_mark_node)
20980 return unify_invalid (explain_p);
20981 /* It should only be possible to get here for a call. */
20982 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20983 elt_strict |= maybe_adjust_types_for_deduction
20984 (DEDUCE_CALL, &elttype, &type, elt);
20985 elt = type;
20986 }
20987
20988 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20989 explain_p);
20990 }
20991
20992 if (TREE_CODE (parm) == ARRAY_TYPE
20993 && deducible_array_bound (TYPE_DOMAIN (parm)))
20994 {
20995 /* Also deduce from the length of the initializer list. */
20996 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20997 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20998 if (idx == error_mark_node)
20999 return unify_invalid (explain_p);
21000 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21001 idx, explain_p);
21002 }
21003
21004 /* If the std::initializer_list<T> deduction worked, replace the
21005 deduced A with std::initializer_list<A>. */
21006 if (orig_parm != parm)
21007 {
21008 idx = TEMPLATE_TYPE_IDX (orig_parm);
21009 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21010 targ = listify (targ);
21011 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
21012 }
21013 return unify_success (explain_p);
21014 }
21015
21016 /* If parm and arg aren't the same kind of thing (template, type, or
21017 expression), fail early. */
21018 if (pa_kind (parm) != pa_kind (arg))
21019 return unify_invalid (explain_p);
21020
21021 /* Immediately reject some pairs that won't unify because of
21022 cv-qualification mismatches. */
21023 if (TREE_CODE (arg) == TREE_CODE (parm)
21024 && TYPE_P (arg)
21025 /* It is the elements of the array which hold the cv quals of an array
21026 type, and the elements might be template type parms. We'll check
21027 when we recurse. */
21028 && TREE_CODE (arg) != ARRAY_TYPE
21029 /* We check the cv-qualifiers when unifying with template type
21030 parameters below. We want to allow ARG `const T' to unify with
21031 PARM `T' for example, when computing which of two templates
21032 is more specialized, for example. */
21033 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21034 && !check_cv_quals_for_unify (strict_in, arg, parm))
21035 return unify_cv_qual_mismatch (explain_p, parm, arg);
21036
21037 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21038 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21039 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21040 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21041 strict &= ~UNIFY_ALLOW_DERIVED;
21042 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21043 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21044
21045 switch (TREE_CODE (parm))
21046 {
21047 case TYPENAME_TYPE:
21048 case SCOPE_REF:
21049 case UNBOUND_CLASS_TEMPLATE:
21050 /* In a type which contains a nested-name-specifier, template
21051 argument values cannot be deduced for template parameters used
21052 within the nested-name-specifier. */
21053 return unify_success (explain_p);
21054
21055 case TEMPLATE_TYPE_PARM:
21056 case TEMPLATE_TEMPLATE_PARM:
21057 case BOUND_TEMPLATE_TEMPLATE_PARM:
21058 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21059 if (error_operand_p (tparm))
21060 return unify_invalid (explain_p);
21061
21062 if (TEMPLATE_TYPE_LEVEL (parm)
21063 != template_decl_level (tparm))
21064 /* The PARM is not one we're trying to unify. Just check
21065 to see if it matches ARG. */
21066 {
21067 if (TREE_CODE (arg) == TREE_CODE (parm)
21068 && (is_auto (parm) ? is_auto (arg)
21069 : same_type_p (parm, arg)))
21070 return unify_success (explain_p);
21071 else
21072 return unify_type_mismatch (explain_p, parm, arg);
21073 }
21074 idx = TEMPLATE_TYPE_IDX (parm);
21075 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21076 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21077 if (error_operand_p (tparm))
21078 return unify_invalid (explain_p);
21079
21080 /* Check for mixed types and values. */
21081 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21082 && TREE_CODE (tparm) != TYPE_DECL)
21083 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21084 && TREE_CODE (tparm) != TEMPLATE_DECL))
21085 gcc_unreachable ();
21086
21087 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21088 {
21089 if ((strict_in & UNIFY_ALLOW_DERIVED)
21090 && CLASS_TYPE_P (arg))
21091 {
21092 /* First try to match ARG directly. */
21093 tree t = try_class_unification (tparms, targs, parm, arg,
21094 explain_p);
21095 if (!t)
21096 {
21097 /* Otherwise, look for a suitable base of ARG, as below. */
21098 enum template_base_result r;
21099 r = get_template_base (tparms, targs, parm, arg,
21100 explain_p, &t);
21101 if (!t)
21102 return unify_no_common_base (explain_p, r, parm, arg);
21103 arg = t;
21104 }
21105 }
21106 /* ARG must be constructed from a template class or a template
21107 template parameter. */
21108 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21109 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21110 return unify_template_deduction_failure (explain_p, parm, arg);
21111
21112 /* Deduce arguments T, i from TT<T> or TT<i>. */
21113 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21114 return 1;
21115
21116 arg = TYPE_TI_TEMPLATE (arg);
21117
21118 /* Fall through to deduce template name. */
21119 }
21120
21121 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21122 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21123 {
21124 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21125
21126 /* Simple cases: Value already set, does match or doesn't. */
21127 if (targ != NULL_TREE && template_args_equal (targ, arg))
21128 return unify_success (explain_p);
21129 else if (targ)
21130 return unify_inconsistency (explain_p, parm, targ, arg);
21131 }
21132 else
21133 {
21134 /* If PARM is `const T' and ARG is only `int', we don't have
21135 a match unless we are allowing additional qualification.
21136 If ARG is `const int' and PARM is just `T' that's OK;
21137 that binds `const int' to `T'. */
21138 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21139 arg, parm))
21140 return unify_cv_qual_mismatch (explain_p, parm, arg);
21141
21142 /* Consider the case where ARG is `const volatile int' and
21143 PARM is `const T'. Then, T should be `volatile int'. */
21144 arg = cp_build_qualified_type_real
21145 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21146 if (arg == error_mark_node)
21147 return unify_invalid (explain_p);
21148
21149 /* Simple cases: Value already set, does match or doesn't. */
21150 if (targ != NULL_TREE && same_type_p (targ, arg))
21151 return unify_success (explain_p);
21152 else if (targ)
21153 return unify_inconsistency (explain_p, parm, targ, arg);
21154
21155 /* Make sure that ARG is not a variable-sized array. (Note
21156 that were talking about variable-sized arrays (like
21157 `int[n]'), rather than arrays of unknown size (like
21158 `int[]').) We'll get very confused by such a type since
21159 the bound of the array is not constant, and therefore
21160 not mangleable. Besides, such types are not allowed in
21161 ISO C++, so we can do as we please here. We do allow
21162 them for 'auto' deduction, since that isn't ABI-exposed. */
21163 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21164 return unify_vla_arg (explain_p, arg);
21165
21166 /* Strip typedefs as in convert_template_argument. */
21167 arg = canonicalize_type_argument (arg, tf_none);
21168 }
21169
21170 /* If ARG is a parameter pack or an expansion, we cannot unify
21171 against it unless PARM is also a parameter pack. */
21172 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21173 && !template_parameter_pack_p (parm))
21174 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21175
21176 /* If the argument deduction results is a METHOD_TYPE,
21177 then there is a problem.
21178 METHOD_TYPE doesn't map to any real C++ type the result of
21179 the deduction can not be of that type. */
21180 if (TREE_CODE (arg) == METHOD_TYPE)
21181 return unify_method_type_error (explain_p, arg);
21182
21183 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21184 return unify_success (explain_p);
21185
21186 case TEMPLATE_PARM_INDEX:
21187 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21188 if (error_operand_p (tparm))
21189 return unify_invalid (explain_p);
21190
21191 if (TEMPLATE_PARM_LEVEL (parm)
21192 != template_decl_level (tparm))
21193 {
21194 /* The PARM is not one we're trying to unify. Just check
21195 to see if it matches ARG. */
21196 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21197 && cp_tree_equal (parm, arg));
21198 if (result)
21199 unify_expression_unequal (explain_p, parm, arg);
21200 return result;
21201 }
21202
21203 idx = TEMPLATE_PARM_IDX (parm);
21204 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21205
21206 if (targ)
21207 {
21208 if ((strict & UNIFY_ALLOW_INTEGER)
21209 && TREE_TYPE (targ) && TREE_TYPE (arg)
21210 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21211 /* We're deducing from an array bound, the type doesn't matter. */
21212 arg = fold_convert (TREE_TYPE (targ), arg);
21213 int x = !cp_tree_equal (targ, arg);
21214 if (x)
21215 unify_inconsistency (explain_p, parm, targ, arg);
21216 return x;
21217 }
21218
21219 /* [temp.deduct.type] If, in the declaration of a function template
21220 with a non-type template-parameter, the non-type
21221 template-parameter is used in an expression in the function
21222 parameter-list and, if the corresponding template-argument is
21223 deduced, the template-argument type shall match the type of the
21224 template-parameter exactly, except that a template-argument
21225 deduced from an array bound may be of any integral type.
21226 The non-type parameter might use already deduced type parameters. */
21227 ++processing_template_decl;
21228 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
21229 --processing_template_decl;
21230 if (tree a = type_uses_auto (tparm))
21231 {
21232 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21233 if (tparm == error_mark_node)
21234 return 1;
21235 }
21236
21237 if (!TREE_TYPE (arg))
21238 /* Template-parameter dependent expression. Just accept it for now.
21239 It will later be processed in convert_template_argument. */
21240 ;
21241 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21242 non_reference (tparm)))
21243 /* OK */;
21244 else if ((strict & UNIFY_ALLOW_INTEGER)
21245 && CP_INTEGRAL_TYPE_P (tparm))
21246 /* Convert the ARG to the type of PARM; the deduced non-type
21247 template argument must exactly match the types of the
21248 corresponding parameter. */
21249 arg = fold (build_nop (tparm, arg));
21250 else if (uses_template_parms (tparm))
21251 {
21252 /* We haven't deduced the type of this parameter yet. */
21253 if (cxx_dialect >= cxx17
21254 /* We deduce from array bounds in try_array_deduction. */
21255 && !(strict & UNIFY_ALLOW_INTEGER))
21256 {
21257 /* Deduce it from the non-type argument. */
21258 tree atype = TREE_TYPE (arg);
21259 RECUR_AND_CHECK_FAILURE (tparms, targs,
21260 tparm, atype,
21261 UNIFY_ALLOW_NONE, explain_p);
21262 }
21263 else
21264 /* Try again later. */
21265 return unify_success (explain_p);
21266 }
21267 else
21268 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21269
21270 /* If ARG is a parameter pack or an expansion, we cannot unify
21271 against it unless PARM is also a parameter pack. */
21272 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21273 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21274 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21275
21276 {
21277 bool removed_attr = false;
21278 arg = strip_typedefs_expr (arg, &removed_attr);
21279 }
21280 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21281 return unify_success (explain_p);
21282
21283 case PTRMEM_CST:
21284 {
21285 /* A pointer-to-member constant can be unified only with
21286 another constant. */
21287 if (TREE_CODE (arg) != PTRMEM_CST)
21288 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21289
21290 /* Just unify the class member. It would be useless (and possibly
21291 wrong, depending on the strict flags) to unify also
21292 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21293 arg refer to the same variable, even if through different
21294 classes. For instance:
21295
21296 struct A { int x; };
21297 struct B : A { };
21298
21299 Unification of &A::x and &B::x must succeed. */
21300 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21301 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21302 }
21303
21304 case POINTER_TYPE:
21305 {
21306 if (!TYPE_PTR_P (arg))
21307 return unify_type_mismatch (explain_p, parm, arg);
21308
21309 /* [temp.deduct.call]
21310
21311 A can be another pointer or pointer to member type that can
21312 be converted to the deduced A via a qualification
21313 conversion (_conv.qual_).
21314
21315 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21316 This will allow for additional cv-qualification of the
21317 pointed-to types if appropriate. */
21318
21319 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21320 /* The derived-to-base conversion only persists through one
21321 level of pointers. */
21322 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21323
21324 return unify (tparms, targs, TREE_TYPE (parm),
21325 TREE_TYPE (arg), strict, explain_p);
21326 }
21327
21328 case REFERENCE_TYPE:
21329 if (TREE_CODE (arg) != REFERENCE_TYPE)
21330 return unify_type_mismatch (explain_p, parm, arg);
21331 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21332 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21333
21334 case ARRAY_TYPE:
21335 if (TREE_CODE (arg) != ARRAY_TYPE)
21336 return unify_type_mismatch (explain_p, parm, arg);
21337 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21338 != (TYPE_DOMAIN (arg) == NULL_TREE))
21339 return unify_type_mismatch (explain_p, parm, arg);
21340 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21341 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21342 if (TYPE_DOMAIN (parm) != NULL_TREE)
21343 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21344 TYPE_DOMAIN (arg), explain_p);
21345 return unify_success (explain_p);
21346
21347 case REAL_TYPE:
21348 case COMPLEX_TYPE:
21349 case VECTOR_TYPE:
21350 case INTEGER_TYPE:
21351 case BOOLEAN_TYPE:
21352 case ENUMERAL_TYPE:
21353 case VOID_TYPE:
21354 case NULLPTR_TYPE:
21355 if (TREE_CODE (arg) != TREE_CODE (parm))
21356 return unify_type_mismatch (explain_p, parm, arg);
21357
21358 /* We have already checked cv-qualification at the top of the
21359 function. */
21360 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21361 return unify_type_mismatch (explain_p, parm, arg);
21362
21363 /* As far as unification is concerned, this wins. Later checks
21364 will invalidate it if necessary. */
21365 return unify_success (explain_p);
21366
21367 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21368 /* Type INTEGER_CST can come from ordinary constant template args. */
21369 case INTEGER_CST:
21370 while (CONVERT_EXPR_P (arg))
21371 arg = TREE_OPERAND (arg, 0);
21372
21373 if (TREE_CODE (arg) != INTEGER_CST)
21374 return unify_template_argument_mismatch (explain_p, parm, arg);
21375 return (tree_int_cst_equal (parm, arg)
21376 ? unify_success (explain_p)
21377 : unify_template_argument_mismatch (explain_p, parm, arg));
21378
21379 case TREE_VEC:
21380 {
21381 int i, len, argslen;
21382 int parm_variadic_p = 0;
21383
21384 if (TREE_CODE (arg) != TREE_VEC)
21385 return unify_template_argument_mismatch (explain_p, parm, arg);
21386
21387 len = TREE_VEC_LENGTH (parm);
21388 argslen = TREE_VEC_LENGTH (arg);
21389
21390 /* Check for pack expansions in the parameters. */
21391 for (i = 0; i < len; ++i)
21392 {
21393 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21394 {
21395 if (i == len - 1)
21396 /* We can unify against something with a trailing
21397 parameter pack. */
21398 parm_variadic_p = 1;
21399 else
21400 /* [temp.deduct.type]/9: If the template argument list of
21401 P contains a pack expansion that is not the last
21402 template argument, the entire template argument list
21403 is a non-deduced context. */
21404 return unify_success (explain_p);
21405 }
21406 }
21407
21408 /* If we don't have enough arguments to satisfy the parameters
21409 (not counting the pack expression at the end), or we have
21410 too many arguments for a parameter list that doesn't end in
21411 a pack expression, we can't unify. */
21412 if (parm_variadic_p
21413 ? argslen < len - parm_variadic_p
21414 : argslen != len)
21415 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21416
21417 /* Unify all of the parameters that precede the (optional)
21418 pack expression. */
21419 for (i = 0; i < len - parm_variadic_p; ++i)
21420 {
21421 RECUR_AND_CHECK_FAILURE (tparms, targs,
21422 TREE_VEC_ELT (parm, i),
21423 TREE_VEC_ELT (arg, i),
21424 UNIFY_ALLOW_NONE, explain_p);
21425 }
21426 if (parm_variadic_p)
21427 return unify_pack_expansion (tparms, targs, parm, arg,
21428 DEDUCE_EXACT,
21429 /*subr=*/true, explain_p);
21430 return unify_success (explain_p);
21431 }
21432
21433 case RECORD_TYPE:
21434 case UNION_TYPE:
21435 if (TREE_CODE (arg) != TREE_CODE (parm))
21436 return unify_type_mismatch (explain_p, parm, arg);
21437
21438 if (TYPE_PTRMEMFUNC_P (parm))
21439 {
21440 if (!TYPE_PTRMEMFUNC_P (arg))
21441 return unify_type_mismatch (explain_p, parm, arg);
21442
21443 return unify (tparms, targs,
21444 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21445 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21446 strict, explain_p);
21447 }
21448 else if (TYPE_PTRMEMFUNC_P (arg))
21449 return unify_type_mismatch (explain_p, parm, arg);
21450
21451 if (CLASSTYPE_TEMPLATE_INFO (parm))
21452 {
21453 tree t = NULL_TREE;
21454
21455 if (strict_in & UNIFY_ALLOW_DERIVED)
21456 {
21457 /* First, we try to unify the PARM and ARG directly. */
21458 t = try_class_unification (tparms, targs,
21459 parm, arg, explain_p);
21460
21461 if (!t)
21462 {
21463 /* Fallback to the special case allowed in
21464 [temp.deduct.call]:
21465
21466 If P is a class, and P has the form
21467 template-id, then A can be a derived class of
21468 the deduced A. Likewise, if P is a pointer to
21469 a class of the form template-id, A can be a
21470 pointer to a derived class pointed to by the
21471 deduced A. */
21472 enum template_base_result r;
21473 r = get_template_base (tparms, targs, parm, arg,
21474 explain_p, &t);
21475
21476 if (!t)
21477 {
21478 /* Don't give the derived diagnostic if we're
21479 already dealing with the same template. */
21480 bool same_template
21481 = (CLASSTYPE_TEMPLATE_INFO (arg)
21482 && (CLASSTYPE_TI_TEMPLATE (parm)
21483 == CLASSTYPE_TI_TEMPLATE (arg)));
21484 return unify_no_common_base (explain_p && !same_template,
21485 r, parm, arg);
21486 }
21487 }
21488 }
21489 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21490 && (CLASSTYPE_TI_TEMPLATE (parm)
21491 == CLASSTYPE_TI_TEMPLATE (arg)))
21492 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21493 Then, we should unify `int' and `U'. */
21494 t = arg;
21495 else
21496 /* There's no chance of unification succeeding. */
21497 return unify_type_mismatch (explain_p, parm, arg);
21498
21499 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21500 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21501 }
21502 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21503 return unify_type_mismatch (explain_p, parm, arg);
21504 return unify_success (explain_p);
21505
21506 case METHOD_TYPE:
21507 case FUNCTION_TYPE:
21508 {
21509 unsigned int nargs;
21510 tree *args;
21511 tree a;
21512 unsigned int i;
21513
21514 if (TREE_CODE (arg) != TREE_CODE (parm))
21515 return unify_type_mismatch (explain_p, parm, arg);
21516
21517 /* CV qualifications for methods can never be deduced, they must
21518 match exactly. We need to check them explicitly here,
21519 because type_unification_real treats them as any other
21520 cv-qualified parameter. */
21521 if (TREE_CODE (parm) == METHOD_TYPE
21522 && (!check_cv_quals_for_unify
21523 (UNIFY_ALLOW_NONE,
21524 class_of_this_parm (arg),
21525 class_of_this_parm (parm))))
21526 return unify_cv_qual_mismatch (explain_p, parm, arg);
21527 if (TREE_CODE (arg) == FUNCTION_TYPE
21528 && type_memfn_quals (parm) != type_memfn_quals (arg))
21529 return unify_cv_qual_mismatch (explain_p, parm, arg);
21530 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21531 return unify_type_mismatch (explain_p, parm, arg);
21532
21533 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21534 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21535
21536 nargs = list_length (TYPE_ARG_TYPES (arg));
21537 args = XALLOCAVEC (tree, nargs);
21538 for (a = TYPE_ARG_TYPES (arg), i = 0;
21539 a != NULL_TREE && a != void_list_node;
21540 a = TREE_CHAIN (a), ++i)
21541 args[i] = TREE_VALUE (a);
21542 nargs = i;
21543
21544 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21545 args, nargs, 1, DEDUCE_EXACT,
21546 LOOKUP_NORMAL, NULL, explain_p))
21547 return 1;
21548
21549 if (flag_noexcept_type)
21550 {
21551 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21552 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21553 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21554 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21555 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21556 && uses_template_parms (TREE_PURPOSE (pspec)))
21557 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21558 TREE_PURPOSE (aspec),
21559 UNIFY_ALLOW_NONE, explain_p);
21560 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21561 return unify_type_mismatch (explain_p, parm, arg);
21562 }
21563
21564 return 0;
21565 }
21566
21567 case OFFSET_TYPE:
21568 /* Unify a pointer to member with a pointer to member function, which
21569 deduces the type of the member as a function type. */
21570 if (TYPE_PTRMEMFUNC_P (arg))
21571 {
21572 /* Check top-level cv qualifiers */
21573 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21574 return unify_cv_qual_mismatch (explain_p, parm, arg);
21575
21576 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21577 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21578 UNIFY_ALLOW_NONE, explain_p);
21579
21580 /* Determine the type of the function we are unifying against. */
21581 tree fntype = static_fn_type (arg);
21582
21583 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21584 }
21585
21586 if (TREE_CODE (arg) != OFFSET_TYPE)
21587 return unify_type_mismatch (explain_p, parm, arg);
21588 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21589 TYPE_OFFSET_BASETYPE (arg),
21590 UNIFY_ALLOW_NONE, explain_p);
21591 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21592 strict, explain_p);
21593
21594 case CONST_DECL:
21595 if (DECL_TEMPLATE_PARM_P (parm))
21596 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21597 if (arg != scalar_constant_value (parm))
21598 return unify_template_argument_mismatch (explain_p, parm, arg);
21599 return unify_success (explain_p);
21600
21601 case FIELD_DECL:
21602 case TEMPLATE_DECL:
21603 /* Matched cases are handled by the ARG == PARM test above. */
21604 return unify_template_argument_mismatch (explain_p, parm, arg);
21605
21606 case VAR_DECL:
21607 /* We might get a variable as a non-type template argument in parm if the
21608 corresponding parameter is type-dependent. Make any necessary
21609 adjustments based on whether arg is a reference. */
21610 if (CONSTANT_CLASS_P (arg))
21611 parm = fold_non_dependent_expr (parm);
21612 else if (REFERENCE_REF_P (arg))
21613 {
21614 tree sub = TREE_OPERAND (arg, 0);
21615 STRIP_NOPS (sub);
21616 if (TREE_CODE (sub) == ADDR_EXPR)
21617 arg = TREE_OPERAND (sub, 0);
21618 }
21619 /* Now use the normal expression code to check whether they match. */
21620 goto expr;
21621
21622 case TYPE_ARGUMENT_PACK:
21623 case NONTYPE_ARGUMENT_PACK:
21624 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21625 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21626
21627 case TYPEOF_TYPE:
21628 case DECLTYPE_TYPE:
21629 case UNDERLYING_TYPE:
21630 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21631 or UNDERLYING_TYPE nodes. */
21632 return unify_success (explain_p);
21633
21634 case ERROR_MARK:
21635 /* Unification fails if we hit an error node. */
21636 return unify_invalid (explain_p);
21637
21638 case INDIRECT_REF:
21639 if (REFERENCE_REF_P (parm))
21640 {
21641 bool pexp = PACK_EXPANSION_P (arg);
21642 if (pexp)
21643 arg = PACK_EXPANSION_PATTERN (arg);
21644 if (REFERENCE_REF_P (arg))
21645 arg = TREE_OPERAND (arg, 0);
21646 if (pexp)
21647 arg = make_pack_expansion (arg, complain);
21648 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21649 strict, explain_p);
21650 }
21651 /* FALLTHRU */
21652
21653 default:
21654 /* An unresolved overload is a nondeduced context. */
21655 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21656 return unify_success (explain_p);
21657 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21658 expr:
21659 /* We must be looking at an expression. This can happen with
21660 something like:
21661
21662 template <int I>
21663 void foo(S<I>, S<I + 2>);
21664
21665 This is a "nondeduced context":
21666
21667 [deduct.type]
21668
21669 The nondeduced contexts are:
21670
21671 --A type that is a template-id in which one or more of
21672 the template-arguments is an expression that references
21673 a template-parameter.
21674
21675 In these cases, we assume deduction succeeded, but don't
21676 actually infer any unifications. */
21677
21678 if (!uses_template_parms (parm)
21679 && !template_args_equal (parm, arg))
21680 return unify_expression_unequal (explain_p, parm, arg);
21681 else
21682 return unify_success (explain_p);
21683 }
21684 }
21685 #undef RECUR_AND_CHECK_FAILURE
21686 \f
21687 /* Note that DECL can be defined in this translation unit, if
21688 required. */
21689
21690 static void
21691 mark_definable (tree decl)
21692 {
21693 tree clone;
21694 DECL_NOT_REALLY_EXTERN (decl) = 1;
21695 FOR_EACH_CLONE (clone, decl)
21696 DECL_NOT_REALLY_EXTERN (clone) = 1;
21697 }
21698
21699 /* Called if RESULT is explicitly instantiated, or is a member of an
21700 explicitly instantiated class. */
21701
21702 void
21703 mark_decl_instantiated (tree result, int extern_p)
21704 {
21705 SET_DECL_EXPLICIT_INSTANTIATION (result);
21706
21707 /* If this entity has already been written out, it's too late to
21708 make any modifications. */
21709 if (TREE_ASM_WRITTEN (result))
21710 return;
21711
21712 /* For anonymous namespace we don't need to do anything. */
21713 if (decl_anon_ns_mem_p (result))
21714 {
21715 gcc_assert (!TREE_PUBLIC (result));
21716 return;
21717 }
21718
21719 if (TREE_CODE (result) != FUNCTION_DECL)
21720 /* The TREE_PUBLIC flag for function declarations will have been
21721 set correctly by tsubst. */
21722 TREE_PUBLIC (result) = 1;
21723
21724 /* This might have been set by an earlier implicit instantiation. */
21725 DECL_COMDAT (result) = 0;
21726
21727 if (extern_p)
21728 DECL_NOT_REALLY_EXTERN (result) = 0;
21729 else
21730 {
21731 mark_definable (result);
21732 mark_needed (result);
21733 /* Always make artificials weak. */
21734 if (DECL_ARTIFICIAL (result) && flag_weak)
21735 comdat_linkage (result);
21736 /* For WIN32 we also want to put explicit instantiations in
21737 linkonce sections. */
21738 else if (TREE_PUBLIC (result))
21739 maybe_make_one_only (result);
21740 }
21741
21742 /* If EXTERN_P, then this function will not be emitted -- unless
21743 followed by an explicit instantiation, at which point its linkage
21744 will be adjusted. If !EXTERN_P, then this function will be
21745 emitted here. In neither circumstance do we want
21746 import_export_decl to adjust the linkage. */
21747 DECL_INTERFACE_KNOWN (result) = 1;
21748 }
21749
21750 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21751 important template arguments. If any are missing, we check whether
21752 they're important by using error_mark_node for substituting into any
21753 args that were used for partial ordering (the ones between ARGS and END)
21754 and seeing if it bubbles up. */
21755
21756 static bool
21757 check_undeduced_parms (tree targs, tree args, tree end)
21758 {
21759 bool found = false;
21760 int i;
21761 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21762 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21763 {
21764 found = true;
21765 TREE_VEC_ELT (targs, i) = error_mark_node;
21766 }
21767 if (found)
21768 {
21769 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21770 if (substed == error_mark_node)
21771 return true;
21772 }
21773 return false;
21774 }
21775
21776 /* Given two function templates PAT1 and PAT2, return:
21777
21778 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21779 -1 if PAT2 is more specialized than PAT1.
21780 0 if neither is more specialized.
21781
21782 LEN indicates the number of parameters we should consider
21783 (defaulted parameters should not be considered).
21784
21785 The 1998 std underspecified function template partial ordering, and
21786 DR214 addresses the issue. We take pairs of arguments, one from
21787 each of the templates, and deduce them against each other. One of
21788 the templates will be more specialized if all the *other*
21789 template's arguments deduce against its arguments and at least one
21790 of its arguments *does* *not* deduce against the other template's
21791 corresponding argument. Deduction is done as for class templates.
21792 The arguments used in deduction have reference and top level cv
21793 qualifiers removed. Iff both arguments were originally reference
21794 types *and* deduction succeeds in both directions, an lvalue reference
21795 wins against an rvalue reference and otherwise the template
21796 with the more cv-qualified argument wins for that pairing (if
21797 neither is more cv-qualified, they both are equal). Unlike regular
21798 deduction, after all the arguments have been deduced in this way,
21799 we do *not* verify the deduced template argument values can be
21800 substituted into non-deduced contexts.
21801
21802 The logic can be a bit confusing here, because we look at deduce1 and
21803 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21804 can find template arguments for pat1 to make arg1 look like arg2, that
21805 means that arg2 is at least as specialized as arg1. */
21806
21807 int
21808 more_specialized_fn (tree pat1, tree pat2, int len)
21809 {
21810 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21811 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21812 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21813 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21814 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21815 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21816 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21817 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21818 tree origs1, origs2;
21819 bool lose1 = false;
21820 bool lose2 = false;
21821
21822 /* Remove the this parameter from non-static member functions. If
21823 one is a non-static member function and the other is not a static
21824 member function, remove the first parameter from that function
21825 also. This situation occurs for operator functions where we
21826 locate both a member function (with this pointer) and non-member
21827 operator (with explicit first operand). */
21828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21829 {
21830 len--; /* LEN is the number of significant arguments for DECL1 */
21831 args1 = TREE_CHAIN (args1);
21832 if (!DECL_STATIC_FUNCTION_P (decl2))
21833 args2 = TREE_CHAIN (args2);
21834 }
21835 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21836 {
21837 args2 = TREE_CHAIN (args2);
21838 if (!DECL_STATIC_FUNCTION_P (decl1))
21839 {
21840 len--;
21841 args1 = TREE_CHAIN (args1);
21842 }
21843 }
21844
21845 /* If only one is a conversion operator, they are unordered. */
21846 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21847 return 0;
21848
21849 /* Consider the return type for a conversion function */
21850 if (DECL_CONV_FN_P (decl1))
21851 {
21852 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21853 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21854 len++;
21855 }
21856
21857 processing_template_decl++;
21858
21859 origs1 = args1;
21860 origs2 = args2;
21861
21862 while (len--
21863 /* Stop when an ellipsis is seen. */
21864 && args1 != NULL_TREE && args2 != NULL_TREE)
21865 {
21866 tree arg1 = TREE_VALUE (args1);
21867 tree arg2 = TREE_VALUE (args2);
21868 int deduce1, deduce2;
21869 int quals1 = -1;
21870 int quals2 = -1;
21871 int ref1 = 0;
21872 int ref2 = 0;
21873
21874 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21875 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21876 {
21877 /* When both arguments are pack expansions, we need only
21878 unify the patterns themselves. */
21879 arg1 = PACK_EXPANSION_PATTERN (arg1);
21880 arg2 = PACK_EXPANSION_PATTERN (arg2);
21881
21882 /* This is the last comparison we need to do. */
21883 len = 0;
21884 }
21885
21886 /* DR 1847: If a particular P contains no template-parameters that
21887 participate in template argument deduction, that P is not used to
21888 determine the ordering. */
21889 if (!uses_deducible_template_parms (arg1)
21890 && !uses_deducible_template_parms (arg2))
21891 goto next;
21892
21893 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21894 {
21895 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21896 arg1 = TREE_TYPE (arg1);
21897 quals1 = cp_type_quals (arg1);
21898 }
21899
21900 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21901 {
21902 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21903 arg2 = TREE_TYPE (arg2);
21904 quals2 = cp_type_quals (arg2);
21905 }
21906
21907 arg1 = TYPE_MAIN_VARIANT (arg1);
21908 arg2 = TYPE_MAIN_VARIANT (arg2);
21909
21910 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21911 {
21912 int i, len2 = remaining_arguments (args2);
21913 tree parmvec = make_tree_vec (1);
21914 tree argvec = make_tree_vec (len2);
21915 tree ta = args2;
21916
21917 /* Setup the parameter vector, which contains only ARG1. */
21918 TREE_VEC_ELT (parmvec, 0) = arg1;
21919
21920 /* Setup the argument vector, which contains the remaining
21921 arguments. */
21922 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21923 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21924
21925 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21926 argvec, DEDUCE_EXACT,
21927 /*subr=*/true, /*explain_p=*/false)
21928 == 0);
21929
21930 /* We cannot deduce in the other direction, because ARG1 is
21931 a pack expansion but ARG2 is not. */
21932 deduce2 = 0;
21933 }
21934 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21935 {
21936 int i, len1 = remaining_arguments (args1);
21937 tree parmvec = make_tree_vec (1);
21938 tree argvec = make_tree_vec (len1);
21939 tree ta = args1;
21940
21941 /* Setup the parameter vector, which contains only ARG1. */
21942 TREE_VEC_ELT (parmvec, 0) = arg2;
21943
21944 /* Setup the argument vector, which contains the remaining
21945 arguments. */
21946 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21947 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21948
21949 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21950 argvec, DEDUCE_EXACT,
21951 /*subr=*/true, /*explain_p=*/false)
21952 == 0);
21953
21954 /* We cannot deduce in the other direction, because ARG2 is
21955 a pack expansion but ARG1 is not.*/
21956 deduce1 = 0;
21957 }
21958
21959 else
21960 {
21961 /* The normal case, where neither argument is a pack
21962 expansion. */
21963 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21964 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21965 == 0);
21966 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21967 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21968 == 0);
21969 }
21970
21971 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21972 arg2, then arg2 is not as specialized as arg1. */
21973 if (!deduce1)
21974 lose2 = true;
21975 if (!deduce2)
21976 lose1 = true;
21977
21978 /* "If, for a given type, deduction succeeds in both directions
21979 (i.e., the types are identical after the transformations above)
21980 and both P and A were reference types (before being replaced with
21981 the type referred to above):
21982 - if the type from the argument template was an lvalue reference and
21983 the type from the parameter template was not, the argument type is
21984 considered to be more specialized than the other; otherwise,
21985 - if the type from the argument template is more cv-qualified
21986 than the type from the parameter template (as described above),
21987 the argument type is considered to be more specialized than the other;
21988 otherwise,
21989 - neither type is more specialized than the other." */
21990
21991 if (deduce1 && deduce2)
21992 {
21993 if (ref1 && ref2 && ref1 != ref2)
21994 {
21995 if (ref1 > ref2)
21996 lose1 = true;
21997 else
21998 lose2 = true;
21999 }
22000 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
22001 {
22002 if ((quals1 & quals2) == quals2)
22003 lose2 = true;
22004 if ((quals1 & quals2) == quals1)
22005 lose1 = true;
22006 }
22007 }
22008
22009 if (lose1 && lose2)
22010 /* We've failed to deduce something in either direction.
22011 These must be unordered. */
22012 break;
22013
22014 next:
22015
22016 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22017 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22018 /* We have already processed all of the arguments in our
22019 handing of the pack expansion type. */
22020 len = 0;
22021
22022 args1 = TREE_CHAIN (args1);
22023 args2 = TREE_CHAIN (args2);
22024 }
22025
22026 /* "In most cases, all template parameters must have values in order for
22027 deduction to succeed, but for partial ordering purposes a template
22028 parameter may remain without a value provided it is not used in the
22029 types being used for partial ordering."
22030
22031 Thus, if we are missing any of the targs1 we need to substitute into
22032 origs1, then pat2 is not as specialized as pat1. This can happen when
22033 there is a nondeduced context. */
22034 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22035 lose2 = true;
22036 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22037 lose1 = true;
22038
22039 processing_template_decl--;
22040
22041 /* If both deductions succeed, the partial ordering selects the more
22042 constrained template. */
22043 if (!lose1 && !lose2)
22044 {
22045 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22046 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22047 lose1 = !subsumes_constraints (c1, c2);
22048 lose2 = !subsumes_constraints (c2, c1);
22049 }
22050
22051 /* All things being equal, if the next argument is a pack expansion
22052 for one function but not for the other, prefer the
22053 non-variadic function. FIXME this is bogus; see c++/41958. */
22054 if (lose1 == lose2
22055 && args1 && TREE_VALUE (args1)
22056 && args2 && TREE_VALUE (args2))
22057 {
22058 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22059 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22060 }
22061
22062 if (lose1 == lose2)
22063 return 0;
22064 else if (!lose1)
22065 return 1;
22066 else
22067 return -1;
22068 }
22069
22070 /* Determine which of two partial specializations of TMPL is more
22071 specialized.
22072
22073 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22074 to the first partial specialization. The TREE_PURPOSE is the
22075 innermost set of template parameters for the partial
22076 specialization. PAT2 is similar, but for the second template.
22077
22078 Return 1 if the first partial specialization is more specialized;
22079 -1 if the second is more specialized; 0 if neither is more
22080 specialized.
22081
22082 See [temp.class.order] for information about determining which of
22083 two templates is more specialized. */
22084
22085 static int
22086 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22087 {
22088 tree targs;
22089 int winner = 0;
22090 bool any_deductions = false;
22091
22092 tree tmpl1 = TREE_VALUE (pat1);
22093 tree tmpl2 = TREE_VALUE (pat2);
22094 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22095 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22096
22097 /* Just like what happens for functions, if we are ordering between
22098 different template specializations, we may encounter dependent
22099 types in the arguments, and we need our dependency check functions
22100 to behave correctly. */
22101 ++processing_template_decl;
22102 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22103 if (targs)
22104 {
22105 --winner;
22106 any_deductions = true;
22107 }
22108
22109 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22110 if (targs)
22111 {
22112 ++winner;
22113 any_deductions = true;
22114 }
22115 --processing_template_decl;
22116
22117 /* If both deductions succeed, the partial ordering selects the more
22118 constrained template. */
22119 if (!winner && any_deductions)
22120 return more_constrained (tmpl1, tmpl2);
22121
22122 /* In the case of a tie where at least one of the templates
22123 has a parameter pack at the end, the template with the most
22124 non-packed parameters wins. */
22125 if (winner == 0
22126 && any_deductions
22127 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22128 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22129 {
22130 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22131 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22132 int len1 = TREE_VEC_LENGTH (args1);
22133 int len2 = TREE_VEC_LENGTH (args2);
22134
22135 /* We don't count the pack expansion at the end. */
22136 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22137 --len1;
22138 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22139 --len2;
22140
22141 if (len1 > len2)
22142 return 1;
22143 else if (len1 < len2)
22144 return -1;
22145 }
22146
22147 return winner;
22148 }
22149
22150 /* Return the template arguments that will produce the function signature
22151 DECL from the function template FN, with the explicit template
22152 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22153 also match. Return NULL_TREE if no satisfactory arguments could be
22154 found. */
22155
22156 static tree
22157 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22158 {
22159 int ntparms = DECL_NTPARMS (fn);
22160 tree targs = make_tree_vec (ntparms);
22161 tree decl_type = TREE_TYPE (decl);
22162 tree decl_arg_types;
22163 tree *args;
22164 unsigned int nargs, ix;
22165 tree arg;
22166
22167 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22168
22169 /* Never do unification on the 'this' parameter. */
22170 decl_arg_types = skip_artificial_parms_for (decl,
22171 TYPE_ARG_TYPES (decl_type));
22172
22173 nargs = list_length (decl_arg_types);
22174 args = XALLOCAVEC (tree, nargs);
22175 for (arg = decl_arg_types, ix = 0;
22176 arg != NULL_TREE && arg != void_list_node;
22177 arg = TREE_CHAIN (arg), ++ix)
22178 args[ix] = TREE_VALUE (arg);
22179
22180 if (fn_type_unification (fn, explicit_args, targs,
22181 args, ix,
22182 (check_rettype || DECL_CONV_FN_P (fn)
22183 ? TREE_TYPE (decl_type) : NULL_TREE),
22184 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22185 /*decltype*/false)
22186 == error_mark_node)
22187 return NULL_TREE;
22188
22189 return targs;
22190 }
22191
22192 /* Return the innermost template arguments that, when applied to a partial
22193 specialization SPEC_TMPL of TMPL, yield the ARGS.
22194
22195 For example, suppose we have:
22196
22197 template <class T, class U> struct S {};
22198 template <class T> struct S<T*, int> {};
22199
22200 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22201 partial specialization and the ARGS will be {double*, int}. The resulting
22202 vector will be {double}, indicating that `T' is bound to `double'. */
22203
22204 static tree
22205 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22206 {
22207 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22208 tree spec_args
22209 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22210 int i, ntparms = TREE_VEC_LENGTH (tparms);
22211 tree deduced_args;
22212 tree innermost_deduced_args;
22213
22214 innermost_deduced_args = make_tree_vec (ntparms);
22215 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22216 {
22217 deduced_args = copy_node (args);
22218 SET_TMPL_ARGS_LEVEL (deduced_args,
22219 TMPL_ARGS_DEPTH (deduced_args),
22220 innermost_deduced_args);
22221 }
22222 else
22223 deduced_args = innermost_deduced_args;
22224
22225 bool tried_array_deduction = (cxx_dialect < cxx17);
22226 again:
22227 if (unify (tparms, deduced_args,
22228 INNERMOST_TEMPLATE_ARGS (spec_args),
22229 INNERMOST_TEMPLATE_ARGS (args),
22230 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22231 return NULL_TREE;
22232
22233 for (i = 0; i < ntparms; ++i)
22234 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22235 {
22236 if (!tried_array_deduction)
22237 {
22238 try_array_deduction (tparms, innermost_deduced_args,
22239 INNERMOST_TEMPLATE_ARGS (spec_args));
22240 tried_array_deduction = true;
22241 if (TREE_VEC_ELT (innermost_deduced_args, i))
22242 goto again;
22243 }
22244 return NULL_TREE;
22245 }
22246
22247 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22248 if (!push_tinst_level (tinst))
22249 {
22250 excessive_deduction_depth = true;
22251 return NULL_TREE;
22252 }
22253
22254 /* Verify that nondeduced template arguments agree with the type
22255 obtained from argument deduction.
22256
22257 For example:
22258
22259 struct A { typedef int X; };
22260 template <class T, class U> struct C {};
22261 template <class T> struct C<T, typename T::X> {};
22262
22263 Then with the instantiation `C<A, int>', we can deduce that
22264 `T' is `A' but unify () does not check whether `typename T::X'
22265 is `int'. */
22266 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22267
22268 if (spec_args != error_mark_node)
22269 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22270 INNERMOST_TEMPLATE_ARGS (spec_args),
22271 tmpl, tf_none, false, false);
22272
22273 pop_tinst_level ();
22274
22275 if (spec_args == error_mark_node
22276 /* We only need to check the innermost arguments; the other
22277 arguments will always agree. */
22278 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22279 INNERMOST_TEMPLATE_ARGS (args)))
22280 return NULL_TREE;
22281
22282 /* Now that we have bindings for all of the template arguments,
22283 ensure that the arguments deduced for the template template
22284 parameters have compatible template parameter lists. See the use
22285 of template_template_parm_bindings_ok_p in fn_type_unification
22286 for more information. */
22287 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22288 return NULL_TREE;
22289
22290 return deduced_args;
22291 }
22292
22293 // Compare two function templates T1 and T2 by deducing bindings
22294 // from one against the other. If both deductions succeed, compare
22295 // constraints to see which is more constrained.
22296 static int
22297 more_specialized_inst (tree t1, tree t2)
22298 {
22299 int fate = 0;
22300 int count = 0;
22301
22302 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22303 {
22304 --fate;
22305 ++count;
22306 }
22307
22308 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22309 {
22310 ++fate;
22311 ++count;
22312 }
22313
22314 // If both deductions succeed, then one may be more constrained.
22315 if (count == 2 && fate == 0)
22316 fate = more_constrained (t1, t2);
22317
22318 return fate;
22319 }
22320
22321 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22322 Return the TREE_LIST node with the most specialized template, if
22323 any. If there is no most specialized template, the error_mark_node
22324 is returned.
22325
22326 Note that this function does not look at, or modify, the
22327 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22328 returned is one of the elements of INSTANTIATIONS, callers may
22329 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22330 and retrieve it from the value returned. */
22331
22332 tree
22333 most_specialized_instantiation (tree templates)
22334 {
22335 tree fn, champ;
22336
22337 ++processing_template_decl;
22338
22339 champ = templates;
22340 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22341 {
22342 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22343 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22344 if (fate == -1)
22345 champ = fn;
22346 else if (!fate)
22347 {
22348 /* Equally specialized, move to next function. If there
22349 is no next function, nothing's most specialized. */
22350 fn = TREE_CHAIN (fn);
22351 champ = fn;
22352 if (!fn)
22353 break;
22354 }
22355 }
22356
22357 if (champ)
22358 /* Now verify that champ is better than everything earlier in the
22359 instantiation list. */
22360 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22361 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22362 {
22363 champ = NULL_TREE;
22364 break;
22365 }
22366 }
22367
22368 processing_template_decl--;
22369
22370 if (!champ)
22371 return error_mark_node;
22372
22373 return champ;
22374 }
22375
22376 /* If DECL is a specialization of some template, return the most
22377 general such template. Otherwise, returns NULL_TREE.
22378
22379 For example, given:
22380
22381 template <class T> struct S { template <class U> void f(U); };
22382
22383 if TMPL is `template <class U> void S<int>::f(U)' this will return
22384 the full template. This function will not trace past partial
22385 specializations, however. For example, given in addition:
22386
22387 template <class T> struct S<T*> { template <class U> void f(U); };
22388
22389 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22390 `template <class T> template <class U> S<T*>::f(U)'. */
22391
22392 tree
22393 most_general_template (tree decl)
22394 {
22395 if (TREE_CODE (decl) != TEMPLATE_DECL)
22396 {
22397 if (tree tinfo = get_template_info (decl))
22398 decl = TI_TEMPLATE (tinfo);
22399 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22400 template friend, or a FIELD_DECL for a capture pack. */
22401 if (TREE_CODE (decl) != TEMPLATE_DECL)
22402 return NULL_TREE;
22403 }
22404
22405 /* Look for more and more general templates. */
22406 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22407 {
22408 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22409 (See cp-tree.h for details.) */
22410 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22411 break;
22412
22413 if (CLASS_TYPE_P (TREE_TYPE (decl))
22414 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22415 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22416 break;
22417
22418 /* Stop if we run into an explicitly specialized class template. */
22419 if (!DECL_NAMESPACE_SCOPE_P (decl)
22420 && DECL_CONTEXT (decl)
22421 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22422 break;
22423
22424 decl = DECL_TI_TEMPLATE (decl);
22425 }
22426
22427 return decl;
22428 }
22429
22430 /* Return the most specialized of the template partial specializations
22431 which can produce TARGET, a specialization of some class or variable
22432 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22433 a TEMPLATE_DECL node corresponding to the partial specialization, while
22434 the TREE_PURPOSE is the set of template arguments that must be
22435 substituted into the template pattern in order to generate TARGET.
22436
22437 If the choice of partial specialization is ambiguous, a diagnostic
22438 is issued, and the error_mark_node is returned. If there are no
22439 partial specializations matching TARGET, then NULL_TREE is
22440 returned, indicating that the primary template should be used. */
22441
22442 static tree
22443 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22444 {
22445 tree list = NULL_TREE;
22446 tree t;
22447 tree champ;
22448 int fate;
22449 bool ambiguous_p;
22450 tree outer_args = NULL_TREE;
22451 tree tmpl, args;
22452
22453 if (TYPE_P (target))
22454 {
22455 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22456 tmpl = TI_TEMPLATE (tinfo);
22457 args = TI_ARGS (tinfo);
22458 }
22459 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22460 {
22461 tmpl = TREE_OPERAND (target, 0);
22462 args = TREE_OPERAND (target, 1);
22463 }
22464 else if (VAR_P (target))
22465 {
22466 tree tinfo = DECL_TEMPLATE_INFO (target);
22467 tmpl = TI_TEMPLATE (tinfo);
22468 args = TI_ARGS (tinfo);
22469 }
22470 else
22471 gcc_unreachable ();
22472
22473 tree main_tmpl = most_general_template (tmpl);
22474
22475 /* For determining which partial specialization to use, only the
22476 innermost args are interesting. */
22477 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22478 {
22479 outer_args = strip_innermost_template_args (args, 1);
22480 args = INNERMOST_TEMPLATE_ARGS (args);
22481 }
22482
22483 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22484 {
22485 tree spec_args;
22486 tree spec_tmpl = TREE_VALUE (t);
22487
22488 if (outer_args)
22489 {
22490 /* Substitute in the template args from the enclosing class. */
22491 ++processing_template_decl;
22492 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22493 --processing_template_decl;
22494 }
22495
22496 if (spec_tmpl == error_mark_node)
22497 return error_mark_node;
22498
22499 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22500 if (spec_args)
22501 {
22502 if (outer_args)
22503 spec_args = add_to_template_args (outer_args, spec_args);
22504
22505 /* Keep the candidate only if the constraints are satisfied,
22506 or if we're not compiling with concepts. */
22507 if (!flag_concepts
22508 || constraints_satisfied_p (spec_tmpl, spec_args))
22509 {
22510 list = tree_cons (spec_args, TREE_VALUE (t), list);
22511 TREE_TYPE (list) = TREE_TYPE (t);
22512 }
22513 }
22514 }
22515
22516 if (! list)
22517 return NULL_TREE;
22518
22519 ambiguous_p = false;
22520 t = list;
22521 champ = t;
22522 t = TREE_CHAIN (t);
22523 for (; t; t = TREE_CHAIN (t))
22524 {
22525 fate = more_specialized_partial_spec (tmpl, champ, t);
22526 if (fate == 1)
22527 ;
22528 else
22529 {
22530 if (fate == 0)
22531 {
22532 t = TREE_CHAIN (t);
22533 if (! t)
22534 {
22535 ambiguous_p = true;
22536 break;
22537 }
22538 }
22539 champ = t;
22540 }
22541 }
22542
22543 if (!ambiguous_p)
22544 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22545 {
22546 fate = more_specialized_partial_spec (tmpl, champ, t);
22547 if (fate != 1)
22548 {
22549 ambiguous_p = true;
22550 break;
22551 }
22552 }
22553
22554 if (ambiguous_p)
22555 {
22556 const char *str;
22557 char *spaces = NULL;
22558 if (!(complain & tf_error))
22559 return error_mark_node;
22560 if (TYPE_P (target))
22561 error ("ambiguous template instantiation for %q#T", target);
22562 else
22563 error ("ambiguous template instantiation for %q#D", target);
22564 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22565 for (t = list; t; t = TREE_CHAIN (t))
22566 {
22567 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22568 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22569 "%s %#qS", spaces ? spaces : str, subst);
22570 spaces = spaces ? spaces : get_spaces (str);
22571 }
22572 free (spaces);
22573 return error_mark_node;
22574 }
22575
22576 return champ;
22577 }
22578
22579 /* Explicitly instantiate DECL. */
22580
22581 void
22582 do_decl_instantiation (tree decl, tree storage)
22583 {
22584 tree result = NULL_TREE;
22585 int extern_p = 0;
22586
22587 if (!decl || decl == error_mark_node)
22588 /* An error occurred, for which grokdeclarator has already issued
22589 an appropriate message. */
22590 return;
22591 else if (! DECL_LANG_SPECIFIC (decl))
22592 {
22593 error ("explicit instantiation of non-template %q#D", decl);
22594 return;
22595 }
22596
22597 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22598 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22599
22600 if (VAR_P (decl) && !var_templ)
22601 {
22602 /* There is an asymmetry here in the way VAR_DECLs and
22603 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22604 the latter, the DECL we get back will be marked as a
22605 template instantiation, and the appropriate
22606 DECL_TEMPLATE_INFO will be set up. This does not happen for
22607 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22608 should handle VAR_DECLs as it currently handles
22609 FUNCTION_DECLs. */
22610 if (!DECL_CLASS_SCOPE_P (decl))
22611 {
22612 error ("%qD is not a static data member of a class template", decl);
22613 return;
22614 }
22615 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22616 if (!result || !VAR_P (result))
22617 {
22618 error ("no matching template for %qD found", decl);
22619 return;
22620 }
22621 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22622 {
22623 error ("type %qT for explicit instantiation %qD does not match "
22624 "declared type %qT", TREE_TYPE (result), decl,
22625 TREE_TYPE (decl));
22626 return;
22627 }
22628 }
22629 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22630 {
22631 error ("explicit instantiation of %q#D", decl);
22632 return;
22633 }
22634 else
22635 result = decl;
22636
22637 /* Check for various error cases. Note that if the explicit
22638 instantiation is valid the RESULT will currently be marked as an
22639 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22640 until we get here. */
22641
22642 if (DECL_TEMPLATE_SPECIALIZATION (result))
22643 {
22644 /* DR 259 [temp.spec].
22645
22646 Both an explicit instantiation and a declaration of an explicit
22647 specialization shall not appear in a program unless the explicit
22648 instantiation follows a declaration of the explicit specialization.
22649
22650 For a given set of template parameters, if an explicit
22651 instantiation of a template appears after a declaration of an
22652 explicit specialization for that template, the explicit
22653 instantiation has no effect. */
22654 return;
22655 }
22656 else if (DECL_EXPLICIT_INSTANTIATION (result))
22657 {
22658 /* [temp.spec]
22659
22660 No program shall explicitly instantiate any template more
22661 than once.
22662
22663 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22664 the first instantiation was `extern' and the second is not,
22665 and EXTERN_P for the opposite case. */
22666 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22667 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22668 /* If an "extern" explicit instantiation follows an ordinary
22669 explicit instantiation, the template is instantiated. */
22670 if (extern_p)
22671 return;
22672 }
22673 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22674 {
22675 error ("no matching template for %qD found", result);
22676 return;
22677 }
22678 else if (!DECL_TEMPLATE_INFO (result))
22679 {
22680 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22681 return;
22682 }
22683
22684 if (storage == NULL_TREE)
22685 ;
22686 else if (storage == ridpointers[(int) RID_EXTERN])
22687 {
22688 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22689 pedwarn (input_location, OPT_Wpedantic,
22690 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22691 "instantiations");
22692 extern_p = 1;
22693 }
22694 else
22695 error ("storage class %qD applied to template instantiation", storage);
22696
22697 check_explicit_instantiation_namespace (result);
22698 mark_decl_instantiated (result, extern_p);
22699 if (! extern_p)
22700 instantiate_decl (result, /*defer_ok=*/true,
22701 /*expl_inst_class_mem_p=*/false);
22702 }
22703
22704 static void
22705 mark_class_instantiated (tree t, int extern_p)
22706 {
22707 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22708 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22709 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22710 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22711 if (! extern_p)
22712 {
22713 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22714 rest_of_type_compilation (t, 1);
22715 }
22716 }
22717
22718 /* Called from do_type_instantiation through binding_table_foreach to
22719 do recursive instantiation for the type bound in ENTRY. */
22720 static void
22721 bt_instantiate_type_proc (binding_entry entry, void *data)
22722 {
22723 tree storage = *(tree *) data;
22724
22725 if (MAYBE_CLASS_TYPE_P (entry->type)
22726 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22727 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22728 }
22729
22730 /* Perform an explicit instantiation of template class T. STORAGE, if
22731 non-null, is the RID for extern, inline or static. COMPLAIN is
22732 nonzero if this is called from the parser, zero if called recursively,
22733 since the standard is unclear (as detailed below). */
22734
22735 void
22736 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22737 {
22738 int extern_p = 0;
22739 int nomem_p = 0;
22740 int static_p = 0;
22741 int previous_instantiation_extern_p = 0;
22742
22743 if (TREE_CODE (t) == TYPE_DECL)
22744 t = TREE_TYPE (t);
22745
22746 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22747 {
22748 tree tmpl =
22749 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22750 if (tmpl)
22751 error ("explicit instantiation of non-class template %qD", tmpl);
22752 else
22753 error ("explicit instantiation of non-template type %qT", t);
22754 return;
22755 }
22756
22757 complete_type (t);
22758
22759 if (!COMPLETE_TYPE_P (t))
22760 {
22761 if (complain & tf_error)
22762 error ("explicit instantiation of %q#T before definition of template",
22763 t);
22764 return;
22765 }
22766
22767 if (storage != NULL_TREE)
22768 {
22769 if (!in_system_header_at (input_location))
22770 {
22771 if (storage == ridpointers[(int) RID_EXTERN])
22772 {
22773 if (cxx_dialect == cxx98)
22774 pedwarn (input_location, OPT_Wpedantic,
22775 "ISO C++ 1998 forbids the use of %<extern%> on "
22776 "explicit instantiations");
22777 }
22778 else
22779 pedwarn (input_location, OPT_Wpedantic,
22780 "ISO C++ forbids the use of %qE"
22781 " on explicit instantiations", storage);
22782 }
22783
22784 if (storage == ridpointers[(int) RID_INLINE])
22785 nomem_p = 1;
22786 else if (storage == ridpointers[(int) RID_EXTERN])
22787 extern_p = 1;
22788 else if (storage == ridpointers[(int) RID_STATIC])
22789 static_p = 1;
22790 else
22791 {
22792 error ("storage class %qD applied to template instantiation",
22793 storage);
22794 extern_p = 0;
22795 }
22796 }
22797
22798 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22799 {
22800 /* DR 259 [temp.spec].
22801
22802 Both an explicit instantiation and a declaration of an explicit
22803 specialization shall not appear in a program unless the explicit
22804 instantiation follows a declaration of the explicit specialization.
22805
22806 For a given set of template parameters, if an explicit
22807 instantiation of a template appears after a declaration of an
22808 explicit specialization for that template, the explicit
22809 instantiation has no effect. */
22810 return;
22811 }
22812 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22813 {
22814 /* [temp.spec]
22815
22816 No program shall explicitly instantiate any template more
22817 than once.
22818
22819 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22820 instantiation was `extern'. If EXTERN_P then the second is.
22821 These cases are OK. */
22822 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22823
22824 if (!previous_instantiation_extern_p && !extern_p
22825 && (complain & tf_error))
22826 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22827
22828 /* If we've already instantiated the template, just return now. */
22829 if (!CLASSTYPE_INTERFACE_ONLY (t))
22830 return;
22831 }
22832
22833 check_explicit_instantiation_namespace (TYPE_NAME (t));
22834 mark_class_instantiated (t, extern_p);
22835
22836 if (nomem_p)
22837 return;
22838
22839 /* In contrast to implicit instantiation, where only the
22840 declarations, and not the definitions, of members are
22841 instantiated, we have here:
22842
22843 [temp.explicit]
22844
22845 The explicit instantiation of a class template specialization
22846 implies the instantiation of all of its members not
22847 previously explicitly specialized in the translation unit
22848 containing the explicit instantiation.
22849
22850 Of course, we can't instantiate member template classes, since we
22851 don't have any arguments for them. Note that the standard is
22852 unclear on whether the instantiation of the members are
22853 *explicit* instantiations or not. However, the most natural
22854 interpretation is that it should be an explicit
22855 instantiation. */
22856 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22857 if ((VAR_P (fld)
22858 || (TREE_CODE (fld) == FUNCTION_DECL
22859 && !static_p
22860 && user_provided_p (fld)))
22861 && DECL_TEMPLATE_INSTANTIATION (fld))
22862 {
22863 mark_decl_instantiated (fld, extern_p);
22864 if (! extern_p)
22865 instantiate_decl (fld, /*defer_ok=*/true,
22866 /*expl_inst_class_mem_p=*/true);
22867 }
22868
22869 if (CLASSTYPE_NESTED_UTDS (t))
22870 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22871 bt_instantiate_type_proc, &storage);
22872 }
22873
22874 /* Given a function DECL, which is a specialization of TMPL, modify
22875 DECL to be a re-instantiation of TMPL with the same template
22876 arguments. TMPL should be the template into which tsubst'ing
22877 should occur for DECL, not the most general template.
22878
22879 One reason for doing this is a scenario like this:
22880
22881 template <class T>
22882 void f(const T&, int i);
22883
22884 void g() { f(3, 7); }
22885
22886 template <class T>
22887 void f(const T& t, const int i) { }
22888
22889 Note that when the template is first instantiated, with
22890 instantiate_template, the resulting DECL will have no name for the
22891 first parameter, and the wrong type for the second. So, when we go
22892 to instantiate the DECL, we regenerate it. */
22893
22894 static void
22895 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22896 {
22897 /* The arguments used to instantiate DECL, from the most general
22898 template. */
22899 tree code_pattern;
22900
22901 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22902
22903 /* Make sure that we can see identifiers, and compute access
22904 correctly. */
22905 push_access_scope (decl);
22906
22907 if (TREE_CODE (decl) == FUNCTION_DECL)
22908 {
22909 tree decl_parm;
22910 tree pattern_parm;
22911 tree specs;
22912 int args_depth;
22913 int parms_depth;
22914
22915 args_depth = TMPL_ARGS_DEPTH (args);
22916 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22917 if (args_depth > parms_depth)
22918 args = get_innermost_template_args (args, parms_depth);
22919
22920 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22921 args, tf_error, NULL_TREE,
22922 /*defer_ok*/false);
22923 if (specs && specs != error_mark_node)
22924 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22925 specs);
22926
22927 /* Merge parameter declarations. */
22928 decl_parm = skip_artificial_parms_for (decl,
22929 DECL_ARGUMENTS (decl));
22930 pattern_parm
22931 = skip_artificial_parms_for (code_pattern,
22932 DECL_ARGUMENTS (code_pattern));
22933 while (decl_parm && !DECL_PACK_P (pattern_parm))
22934 {
22935 tree parm_type;
22936 tree attributes;
22937
22938 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22939 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22940 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22941 NULL_TREE);
22942 parm_type = type_decays_to (parm_type);
22943 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22944 TREE_TYPE (decl_parm) = parm_type;
22945 attributes = DECL_ATTRIBUTES (pattern_parm);
22946 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22947 {
22948 DECL_ATTRIBUTES (decl_parm) = attributes;
22949 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22950 }
22951 decl_parm = DECL_CHAIN (decl_parm);
22952 pattern_parm = DECL_CHAIN (pattern_parm);
22953 }
22954 /* Merge any parameters that match with the function parameter
22955 pack. */
22956 if (pattern_parm && DECL_PACK_P (pattern_parm))
22957 {
22958 int i, len;
22959 tree expanded_types;
22960 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22961 the parameters in this function parameter pack. */
22962 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22963 args, tf_error, NULL_TREE);
22964 len = TREE_VEC_LENGTH (expanded_types);
22965 for (i = 0; i < len; i++)
22966 {
22967 tree parm_type;
22968 tree attributes;
22969
22970 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22971 /* Rename the parameter to include the index. */
22972 DECL_NAME (decl_parm) =
22973 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22974 parm_type = TREE_VEC_ELT (expanded_types, i);
22975 parm_type = type_decays_to (parm_type);
22976 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22977 TREE_TYPE (decl_parm) = parm_type;
22978 attributes = DECL_ATTRIBUTES (pattern_parm);
22979 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22980 {
22981 DECL_ATTRIBUTES (decl_parm) = attributes;
22982 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22983 }
22984 decl_parm = DECL_CHAIN (decl_parm);
22985 }
22986 }
22987 /* Merge additional specifiers from the CODE_PATTERN. */
22988 if (DECL_DECLARED_INLINE_P (code_pattern)
22989 && !DECL_DECLARED_INLINE_P (decl))
22990 DECL_DECLARED_INLINE_P (decl) = 1;
22991 }
22992 else if (VAR_P (decl))
22993 {
22994 start_lambda_scope (decl);
22995 DECL_INITIAL (decl) =
22996 tsubst_expr (DECL_INITIAL (code_pattern), args,
22997 tf_error, DECL_TI_TEMPLATE (decl),
22998 /*integral_constant_expression_p=*/false);
22999 finish_lambda_scope ();
23000 if (VAR_HAD_UNKNOWN_BOUND (decl))
23001 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
23002 tf_error, DECL_TI_TEMPLATE (decl));
23003 }
23004 else
23005 gcc_unreachable ();
23006
23007 pop_access_scope (decl);
23008 }
23009
23010 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23011 substituted to get DECL. */
23012
23013 tree
23014 template_for_substitution (tree decl)
23015 {
23016 tree tmpl = DECL_TI_TEMPLATE (decl);
23017
23018 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23019 for the instantiation. This is not always the most general
23020 template. Consider, for example:
23021
23022 template <class T>
23023 struct S { template <class U> void f();
23024 template <> void f<int>(); };
23025
23026 and an instantiation of S<double>::f<int>. We want TD to be the
23027 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23028 while (/* An instantiation cannot have a definition, so we need a
23029 more general template. */
23030 DECL_TEMPLATE_INSTANTIATION (tmpl)
23031 /* We must also deal with friend templates. Given:
23032
23033 template <class T> struct S {
23034 template <class U> friend void f() {};
23035 };
23036
23037 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23038 so far as the language is concerned, but that's still
23039 where we get the pattern for the instantiation from. On
23040 other hand, if the definition comes outside the class, say:
23041
23042 template <class T> struct S {
23043 template <class U> friend void f();
23044 };
23045 template <class U> friend void f() {}
23046
23047 we don't need to look any further. That's what the check for
23048 DECL_INITIAL is for. */
23049 || (TREE_CODE (decl) == FUNCTION_DECL
23050 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23051 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23052 {
23053 /* The present template, TD, should not be a definition. If it
23054 were a definition, we should be using it! Note that we
23055 cannot restructure the loop to just keep going until we find
23056 a template with a definition, since that might go too far if
23057 a specialization was declared, but not defined. */
23058
23059 /* Fetch the more general template. */
23060 tmpl = DECL_TI_TEMPLATE (tmpl);
23061 }
23062
23063 return tmpl;
23064 }
23065
23066 /* Returns true if we need to instantiate this template instance even if we
23067 know we aren't going to emit it. */
23068
23069 bool
23070 always_instantiate_p (tree decl)
23071 {
23072 /* We always instantiate inline functions so that we can inline them. An
23073 explicit instantiation declaration prohibits implicit instantiation of
23074 non-inline functions. With high levels of optimization, we would
23075 normally inline non-inline functions -- but we're not allowed to do
23076 that for "extern template" functions. Therefore, we check
23077 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23078 return ((TREE_CODE (decl) == FUNCTION_DECL
23079 && (DECL_DECLARED_INLINE_P (decl)
23080 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23081 /* And we need to instantiate static data members so that
23082 their initializers are available in integral constant
23083 expressions. */
23084 || (VAR_P (decl)
23085 && decl_maybe_constant_var_p (decl)));
23086 }
23087
23088 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23089 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23090 error, true otherwise. */
23091
23092 bool
23093 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23094 {
23095 tree fntype, spec, noex, clone;
23096
23097 /* Don't instantiate a noexcept-specification from template context. */
23098 if (processing_template_decl)
23099 return true;
23100
23101 if (DECL_CLONED_FUNCTION_P (fn))
23102 fn = DECL_CLONED_FUNCTION (fn);
23103 fntype = TREE_TYPE (fn);
23104 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23105
23106 if (!spec || !TREE_PURPOSE (spec))
23107 return true;
23108
23109 noex = TREE_PURPOSE (spec);
23110
23111 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23112 {
23113 static hash_set<tree>* fns = new hash_set<tree>;
23114 bool added = false;
23115 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23116 spec = get_defaulted_eh_spec (fn, complain);
23117 else if (!(added = !fns->add (fn)))
23118 {
23119 /* If hash_set::add returns true, the element was already there. */
23120 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
23121 DECL_SOURCE_LOCATION (fn));
23122 error_at (loc,
23123 "exception specification of %qD depends on itself",
23124 fn);
23125 spec = noexcept_false_spec;
23126 }
23127 else if (push_tinst_level (fn))
23128 {
23129 push_access_scope (fn);
23130 push_deferring_access_checks (dk_no_deferred);
23131 input_location = DECL_SOURCE_LOCATION (fn);
23132 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23133 DEFERRED_NOEXCEPT_ARGS (noex),
23134 tf_warning_or_error, fn,
23135 /*function_p=*/false,
23136 /*integral_constant_expression_p=*/true);
23137 pop_deferring_access_checks ();
23138 pop_access_scope (fn);
23139 pop_tinst_level ();
23140 spec = build_noexcept_spec (noex, tf_warning_or_error);
23141 if (spec == error_mark_node)
23142 spec = noexcept_false_spec;
23143 }
23144 else
23145 spec = noexcept_false_spec;
23146
23147 if (added)
23148 fns->remove (fn);
23149
23150 if (spec == error_mark_node)
23151 return false;
23152
23153 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23154 }
23155
23156 FOR_EACH_CLONE (clone, fn)
23157 {
23158 if (TREE_TYPE (clone) == fntype)
23159 TREE_TYPE (clone) = TREE_TYPE (fn);
23160 else
23161 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23162 }
23163
23164 return true;
23165 }
23166
23167 /* We're starting to process the function INST, an instantiation of PATTERN;
23168 add their parameters to local_specializations. */
23169
23170 static void
23171 register_parameter_specializations (tree pattern, tree inst)
23172 {
23173 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23174 tree spec_parm = DECL_ARGUMENTS (inst);
23175 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23176 {
23177 register_local_specialization (spec_parm, tmpl_parm);
23178 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23179 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23180 }
23181 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23182 {
23183 if (!DECL_PACK_P (tmpl_parm))
23184 {
23185 register_local_specialization (spec_parm, tmpl_parm);
23186 spec_parm = DECL_CHAIN (spec_parm);
23187 }
23188 else
23189 {
23190 /* Register the (value) argument pack as a specialization of
23191 TMPL_PARM, then move on. */
23192 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23193 register_local_specialization (argpack, tmpl_parm);
23194 }
23195 }
23196 gcc_assert (!spec_parm);
23197 }
23198
23199 /* Produce the definition of D, a _DECL generated from a template. If
23200 DEFER_OK is true, then we don't have to actually do the
23201 instantiation now; we just have to do it sometime. Normally it is
23202 an error if this is an explicit instantiation but D is undefined.
23203 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23204 instantiated class template. */
23205
23206 tree
23207 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23208 {
23209 tree tmpl = DECL_TI_TEMPLATE (d);
23210 tree gen_args;
23211 tree args;
23212 tree td;
23213 tree code_pattern;
23214 tree spec;
23215 tree gen_tmpl;
23216 bool pattern_defined;
23217 location_t saved_loc = input_location;
23218 int saved_unevaluated_operand = cp_unevaluated_operand;
23219 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23220 bool external_p;
23221 bool deleted_p;
23222
23223 /* This function should only be used to instantiate templates for
23224 functions and static member variables. */
23225 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23226
23227 /* A concept is never instantiated. */
23228 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23229
23230 /* Variables are never deferred; if instantiation is required, they
23231 are instantiated right away. That allows for better code in the
23232 case that an expression refers to the value of the variable --
23233 if the variable has a constant value the referring expression can
23234 take advantage of that fact. */
23235 if (VAR_P (d))
23236 defer_ok = false;
23237
23238 /* Don't instantiate cloned functions. Instead, instantiate the
23239 functions they cloned. */
23240 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23241 d = DECL_CLONED_FUNCTION (d);
23242
23243 if (DECL_TEMPLATE_INSTANTIATED (d)
23244 || (TREE_CODE (d) == FUNCTION_DECL
23245 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23246 || DECL_TEMPLATE_SPECIALIZATION (d))
23247 /* D has already been instantiated or explicitly specialized, so
23248 there's nothing for us to do here.
23249
23250 It might seem reasonable to check whether or not D is an explicit
23251 instantiation, and, if so, stop here. But when an explicit
23252 instantiation is deferred until the end of the compilation,
23253 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23254 the instantiation. */
23255 return d;
23256
23257 /* Check to see whether we know that this template will be
23258 instantiated in some other file, as with "extern template"
23259 extension. */
23260 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23261
23262 /* In general, we do not instantiate such templates. */
23263 if (external_p && !always_instantiate_p (d))
23264 return d;
23265
23266 gen_tmpl = most_general_template (tmpl);
23267 gen_args = DECL_TI_ARGS (d);
23268
23269 if (tmpl != gen_tmpl)
23270 /* We should already have the extra args. */
23271 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23272 == TMPL_ARGS_DEPTH (gen_args));
23273 /* And what's in the hash table should match D. */
23274 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23275 || spec == NULL_TREE);
23276
23277 /* This needs to happen before any tsubsting. */
23278 if (! push_tinst_level (d))
23279 return d;
23280
23281 timevar_push (TV_TEMPLATE_INST);
23282
23283 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23284 for the instantiation. */
23285 td = template_for_substitution (d);
23286 args = gen_args;
23287
23288 if (VAR_P (d))
23289 {
23290 /* Look up an explicit specialization, if any. */
23291 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23292 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23293 if (elt && elt != error_mark_node)
23294 {
23295 td = TREE_VALUE (elt);
23296 args = TREE_PURPOSE (elt);
23297 }
23298 }
23299
23300 code_pattern = DECL_TEMPLATE_RESULT (td);
23301
23302 /* We should never be trying to instantiate a member of a class
23303 template or partial specialization. */
23304 gcc_assert (d != code_pattern);
23305
23306 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23307 || DECL_TEMPLATE_SPECIALIZATION (td))
23308 /* In the case of a friend template whose definition is provided
23309 outside the class, we may have too many arguments. Drop the
23310 ones we don't need. The same is true for specializations. */
23311 args = get_innermost_template_args
23312 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23313
23314 if (TREE_CODE (d) == FUNCTION_DECL)
23315 {
23316 deleted_p = DECL_DELETED_FN (code_pattern);
23317 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23318 && DECL_INITIAL (code_pattern) != error_mark_node)
23319 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23320 || deleted_p);
23321 }
23322 else
23323 {
23324 deleted_p = false;
23325 if (DECL_CLASS_SCOPE_P (code_pattern))
23326 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23327 || DECL_INLINE_VAR_P (code_pattern));
23328 else
23329 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23330 }
23331
23332 /* We may be in the middle of deferred access check. Disable it now. */
23333 push_deferring_access_checks (dk_no_deferred);
23334
23335 /* Unless an explicit instantiation directive has already determined
23336 the linkage of D, remember that a definition is available for
23337 this entity. */
23338 if (pattern_defined
23339 && !DECL_INTERFACE_KNOWN (d)
23340 && !DECL_NOT_REALLY_EXTERN (d))
23341 mark_definable (d);
23342
23343 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23344 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23345 input_location = DECL_SOURCE_LOCATION (d);
23346
23347 /* If D is a member of an explicitly instantiated class template,
23348 and no definition is available, treat it like an implicit
23349 instantiation. */
23350 if (!pattern_defined && expl_inst_class_mem_p
23351 && DECL_EXPLICIT_INSTANTIATION (d))
23352 {
23353 /* Leave linkage flags alone on instantiations with anonymous
23354 visibility. */
23355 if (TREE_PUBLIC (d))
23356 {
23357 DECL_NOT_REALLY_EXTERN (d) = 0;
23358 DECL_INTERFACE_KNOWN (d) = 0;
23359 }
23360 SET_DECL_IMPLICIT_INSTANTIATION (d);
23361 }
23362
23363 /* Defer all other templates, unless we have been explicitly
23364 forbidden from doing so. */
23365 if (/* If there is no definition, we cannot instantiate the
23366 template. */
23367 ! pattern_defined
23368 /* If it's OK to postpone instantiation, do so. */
23369 || defer_ok
23370 /* If this is a static data member that will be defined
23371 elsewhere, we don't want to instantiate the entire data
23372 member, but we do want to instantiate the initializer so that
23373 we can substitute that elsewhere. */
23374 || (external_p && VAR_P (d))
23375 /* Handle here a deleted function too, avoid generating
23376 its body (c++/61080). */
23377 || deleted_p)
23378 {
23379 /* The definition of the static data member is now required so
23380 we must substitute the initializer. */
23381 if (VAR_P (d)
23382 && !DECL_INITIAL (d)
23383 && DECL_INITIAL (code_pattern))
23384 {
23385 tree ns;
23386 tree init;
23387 bool const_init = false;
23388 bool enter_context = DECL_CLASS_SCOPE_P (d);
23389
23390 ns = decl_namespace_context (d);
23391 push_nested_namespace (ns);
23392 if (enter_context)
23393 push_nested_class (DECL_CONTEXT (d));
23394 init = tsubst_expr (DECL_INITIAL (code_pattern),
23395 args,
23396 tf_warning_or_error, NULL_TREE,
23397 /*integral_constant_expression_p=*/false);
23398 /* If instantiating the initializer involved instantiating this
23399 again, don't call cp_finish_decl twice. */
23400 if (!DECL_INITIAL (d))
23401 {
23402 /* Make sure the initializer is still constant, in case of
23403 circular dependency (template/instantiate6.C). */
23404 const_init
23405 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23406 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23407 /*asmspec_tree=*/NULL_TREE,
23408 LOOKUP_ONLYCONVERTING);
23409 }
23410 if (enter_context)
23411 pop_nested_class ();
23412 pop_nested_namespace (ns);
23413 }
23414
23415 /* We restore the source position here because it's used by
23416 add_pending_template. */
23417 input_location = saved_loc;
23418
23419 if (at_eof && !pattern_defined
23420 && DECL_EXPLICIT_INSTANTIATION (d)
23421 && DECL_NOT_REALLY_EXTERN (d))
23422 /* [temp.explicit]
23423
23424 The definition of a non-exported function template, a
23425 non-exported member function template, or a non-exported
23426 member function or static data member of a class template
23427 shall be present in every translation unit in which it is
23428 explicitly instantiated. */
23429 permerror (input_location, "explicit instantiation of %qD "
23430 "but no definition available", d);
23431
23432 /* If we're in unevaluated context, we just wanted to get the
23433 constant value; this isn't an odr use, so don't queue
23434 a full instantiation. */
23435 if (cp_unevaluated_operand != 0)
23436 goto out;
23437 /* ??? Historically, we have instantiated inline functions, even
23438 when marked as "extern template". */
23439 if (!(external_p && VAR_P (d)))
23440 add_pending_template (d);
23441 goto out;
23442 }
23443 /* Tell the repository that D is available in this translation unit
23444 -- and see if it is supposed to be instantiated here. */
23445 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23446 {
23447 /* In a PCH file, despite the fact that the repository hasn't
23448 requested instantiation in the PCH it is still possible that
23449 an instantiation will be required in a file that includes the
23450 PCH. */
23451 if (pch_file)
23452 add_pending_template (d);
23453 /* Instantiate inline functions so that the inliner can do its
23454 job, even though we'll not be emitting a copy of this
23455 function. */
23456 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23457 goto out;
23458 }
23459
23460 bool push_to_top, nested;
23461 tree fn_context;
23462 fn_context = decl_function_context (d);
23463 if (LAMBDA_FUNCTION_P (d))
23464 /* tsubst_lambda_expr resolved any references to enclosing functions. */
23465 fn_context = NULL_TREE;
23466 nested = current_function_decl != NULL_TREE;
23467 push_to_top = !(nested && fn_context == current_function_decl);
23468
23469 vec<tree> omp_privatization_save;
23470 if (nested)
23471 save_omp_privatization_clauses (omp_privatization_save);
23472
23473 if (push_to_top)
23474 push_to_top_level ();
23475 else
23476 {
23477 push_function_context ();
23478 cp_unevaluated_operand = 0;
23479 c_inhibit_evaluation_warnings = 0;
23480 }
23481
23482 /* Mark D as instantiated so that recursive calls to
23483 instantiate_decl do not try to instantiate it again. */
23484 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23485
23486 /* Regenerate the declaration in case the template has been modified
23487 by a subsequent redeclaration. */
23488 regenerate_decl_from_template (d, td, args);
23489
23490 /* We already set the file and line above. Reset them now in case
23491 they changed as a result of calling regenerate_decl_from_template. */
23492 input_location = DECL_SOURCE_LOCATION (d);
23493
23494 if (VAR_P (d))
23495 {
23496 tree init;
23497 bool const_init = false;
23498
23499 /* Clear out DECL_RTL; whatever was there before may not be right
23500 since we've reset the type of the declaration. */
23501 SET_DECL_RTL (d, NULL);
23502 DECL_IN_AGGR_P (d) = 0;
23503
23504 /* The initializer is placed in DECL_INITIAL by
23505 regenerate_decl_from_template so we don't need to
23506 push/pop_access_scope again here. Pull it out so that
23507 cp_finish_decl can process it. */
23508 init = DECL_INITIAL (d);
23509 DECL_INITIAL (d) = NULL_TREE;
23510 DECL_INITIALIZED_P (d) = 0;
23511
23512 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23513 initializer. That function will defer actual emission until
23514 we have a chance to determine linkage. */
23515 DECL_EXTERNAL (d) = 0;
23516
23517 /* Enter the scope of D so that access-checking works correctly. */
23518 bool enter_context = DECL_CLASS_SCOPE_P (d);
23519 if (enter_context)
23520 push_nested_class (DECL_CONTEXT (d));
23521
23522 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23523 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23524
23525 if (enter_context)
23526 pop_nested_class ();
23527
23528 if (variable_template_p (gen_tmpl))
23529 note_variable_template_instantiation (d);
23530 }
23531 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23532 synthesize_method (d);
23533 else if (TREE_CODE (d) == FUNCTION_DECL)
23534 {
23535 /* Set up the list of local specializations. */
23536 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23537 tree block = NULL_TREE;
23538
23539 /* Set up context. */
23540 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23541 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23542 block = push_stmt_list ();
23543 else
23544 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23545
23546 /* Some typedefs referenced from within the template code need to be
23547 access checked at template instantiation time, i.e now. These
23548 types were added to the template at parsing time. Let's get those
23549 and perform the access checks then. */
23550 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23551 args);
23552
23553 /* Create substitution entries for the parameters. */
23554 register_parameter_specializations (code_pattern, d);
23555
23556 /* Substitute into the body of the function. */
23557 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23558 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23559 tf_warning_or_error, tmpl);
23560 else
23561 {
23562 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23563 tf_warning_or_error, tmpl,
23564 /*integral_constant_expression_p=*/false);
23565
23566 /* Set the current input_location to the end of the function
23567 so that finish_function knows where we are. */
23568 input_location
23569 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23570
23571 /* Remember if we saw an infinite loop in the template. */
23572 current_function_infinite_loop
23573 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23574 }
23575
23576 /* Finish the function. */
23577 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23578 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23579 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23580 else
23581 {
23582 d = finish_function (/*inline_p=*/false);
23583 expand_or_defer_fn (d);
23584 }
23585
23586 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23587 cp_check_omp_declare_reduction (d);
23588 }
23589
23590 /* We're not deferring instantiation any more. */
23591 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23592
23593 if (push_to_top)
23594 pop_from_top_level ();
23595 else
23596 pop_function_context ();
23597
23598 if (nested)
23599 restore_omp_privatization_clauses (omp_privatization_save);
23600
23601 out:
23602 pop_deferring_access_checks ();
23603 timevar_pop (TV_TEMPLATE_INST);
23604 pop_tinst_level ();
23605 input_location = saved_loc;
23606 cp_unevaluated_operand = saved_unevaluated_operand;
23607 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23608
23609 return d;
23610 }
23611
23612 /* Run through the list of templates that we wish we could
23613 instantiate, and instantiate any we can. RETRIES is the
23614 number of times we retry pending template instantiation. */
23615
23616 void
23617 instantiate_pending_templates (int retries)
23618 {
23619 int reconsider;
23620 location_t saved_loc = input_location;
23621
23622 /* Instantiating templates may trigger vtable generation. This in turn
23623 may require further template instantiations. We place a limit here
23624 to avoid infinite loop. */
23625 if (pending_templates && retries >= max_tinst_depth)
23626 {
23627 tree decl = pending_templates->tinst->decl;
23628
23629 fatal_error (input_location,
23630 "template instantiation depth exceeds maximum of %d"
23631 " instantiating %q+D, possibly from virtual table generation"
23632 " (use -ftemplate-depth= to increase the maximum)",
23633 max_tinst_depth, decl);
23634 if (TREE_CODE (decl) == FUNCTION_DECL)
23635 /* Pretend that we defined it. */
23636 DECL_INITIAL (decl) = error_mark_node;
23637 return;
23638 }
23639
23640 do
23641 {
23642 struct pending_template **t = &pending_templates;
23643 struct pending_template *last = NULL;
23644 reconsider = 0;
23645 while (*t)
23646 {
23647 tree instantiation = reopen_tinst_level ((*t)->tinst);
23648 bool complete = false;
23649
23650 if (TYPE_P (instantiation))
23651 {
23652 if (!COMPLETE_TYPE_P (instantiation))
23653 {
23654 instantiate_class_template (instantiation);
23655 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23656 for (tree fld = TYPE_FIELDS (instantiation);
23657 fld; fld = TREE_CHAIN (fld))
23658 if ((VAR_P (fld)
23659 || (TREE_CODE (fld) == FUNCTION_DECL
23660 && !DECL_ARTIFICIAL (fld)))
23661 && DECL_TEMPLATE_INSTANTIATION (fld))
23662 instantiate_decl (fld,
23663 /*defer_ok=*/false,
23664 /*expl_inst_class_mem_p=*/false);
23665
23666 if (COMPLETE_TYPE_P (instantiation))
23667 reconsider = 1;
23668 }
23669
23670 complete = COMPLETE_TYPE_P (instantiation);
23671 }
23672 else
23673 {
23674 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23675 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23676 {
23677 instantiation
23678 = instantiate_decl (instantiation,
23679 /*defer_ok=*/false,
23680 /*expl_inst_class_mem_p=*/false);
23681 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23682 reconsider = 1;
23683 }
23684
23685 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23686 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23687 }
23688
23689 if (complete)
23690 /* If INSTANTIATION has been instantiated, then we don't
23691 need to consider it again in the future. */
23692 *t = (*t)->next;
23693 else
23694 {
23695 last = *t;
23696 t = &(*t)->next;
23697 }
23698 tinst_depth = 0;
23699 current_tinst_level = NULL;
23700 }
23701 last_pending_template = last;
23702 }
23703 while (reconsider);
23704
23705 input_location = saved_loc;
23706 }
23707
23708 /* Substitute ARGVEC into T, which is a list of initializers for
23709 either base class or a non-static data member. The TREE_PURPOSEs
23710 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23711 instantiate_decl. */
23712
23713 static tree
23714 tsubst_initializer_list (tree t, tree argvec)
23715 {
23716 tree inits = NULL_TREE;
23717 tree target_ctor = error_mark_node;
23718
23719 for (; t; t = TREE_CHAIN (t))
23720 {
23721 tree decl;
23722 tree init;
23723 tree expanded_bases = NULL_TREE;
23724 tree expanded_arguments = NULL_TREE;
23725 int i, len = 1;
23726
23727 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23728 {
23729 tree expr;
23730 tree arg;
23731
23732 /* Expand the base class expansion type into separate base
23733 classes. */
23734 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23735 tf_warning_or_error,
23736 NULL_TREE);
23737 if (expanded_bases == error_mark_node)
23738 continue;
23739
23740 /* We'll be building separate TREE_LISTs of arguments for
23741 each base. */
23742 len = TREE_VEC_LENGTH (expanded_bases);
23743 expanded_arguments = make_tree_vec (len);
23744 for (i = 0; i < len; i++)
23745 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23746
23747 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23748 expand each argument in the TREE_VALUE of t. */
23749 expr = make_node (EXPR_PACK_EXPANSION);
23750 PACK_EXPANSION_LOCAL_P (expr) = true;
23751 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23752 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23753
23754 if (TREE_VALUE (t) == void_type_node)
23755 /* VOID_TYPE_NODE is used to indicate
23756 value-initialization. */
23757 {
23758 for (i = 0; i < len; i++)
23759 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23760 }
23761 else
23762 {
23763 /* Substitute parameter packs into each argument in the
23764 TREE_LIST. */
23765 in_base_initializer = 1;
23766 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23767 {
23768 tree expanded_exprs;
23769
23770 /* Expand the argument. */
23771 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23772 expanded_exprs
23773 = tsubst_pack_expansion (expr, argvec,
23774 tf_warning_or_error,
23775 NULL_TREE);
23776 if (expanded_exprs == error_mark_node)
23777 continue;
23778
23779 /* Prepend each of the expanded expressions to the
23780 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23781 for (i = 0; i < len; i++)
23782 {
23783 TREE_VEC_ELT (expanded_arguments, i) =
23784 tree_cons (NULL_TREE,
23785 TREE_VEC_ELT (expanded_exprs, i),
23786 TREE_VEC_ELT (expanded_arguments, i));
23787 }
23788 }
23789 in_base_initializer = 0;
23790
23791 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23792 since we built them backwards. */
23793 for (i = 0; i < len; i++)
23794 {
23795 TREE_VEC_ELT (expanded_arguments, i) =
23796 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23797 }
23798 }
23799 }
23800
23801 for (i = 0; i < len; ++i)
23802 {
23803 if (expanded_bases)
23804 {
23805 decl = TREE_VEC_ELT (expanded_bases, i);
23806 decl = expand_member_init (decl);
23807 init = TREE_VEC_ELT (expanded_arguments, i);
23808 }
23809 else
23810 {
23811 tree tmp;
23812 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23813 tf_warning_or_error, NULL_TREE);
23814
23815 decl = expand_member_init (decl);
23816 if (decl && !DECL_P (decl))
23817 in_base_initializer = 1;
23818
23819 init = TREE_VALUE (t);
23820 tmp = init;
23821 if (init != void_type_node)
23822 init = tsubst_expr (init, argvec,
23823 tf_warning_or_error, NULL_TREE,
23824 /*integral_constant_expression_p=*/false);
23825 if (init == NULL_TREE && tmp != NULL_TREE)
23826 /* If we had an initializer but it instantiated to nothing,
23827 value-initialize the object. This will only occur when
23828 the initializer was a pack expansion where the parameter
23829 packs used in that expansion were of length zero. */
23830 init = void_type_node;
23831 in_base_initializer = 0;
23832 }
23833
23834 if (target_ctor != error_mark_node
23835 && init != error_mark_node)
23836 {
23837 error ("mem-initializer for %qD follows constructor delegation",
23838 decl);
23839 return inits;
23840 }
23841 /* Look for a target constructor. */
23842 if (init != error_mark_node
23843 && decl && CLASS_TYPE_P (decl)
23844 && same_type_p (decl, current_class_type))
23845 {
23846 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
23847 if (inits)
23848 {
23849 error ("constructor delegation follows mem-initializer for %qD",
23850 TREE_PURPOSE (inits));
23851 continue;
23852 }
23853 target_ctor = init;
23854 }
23855
23856 if (decl)
23857 {
23858 init = build_tree_list (decl, init);
23859 TREE_CHAIN (init) = inits;
23860 inits = init;
23861 }
23862 }
23863 }
23864 return inits;
23865 }
23866
23867 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23868
23869 static void
23870 set_current_access_from_decl (tree decl)
23871 {
23872 if (TREE_PRIVATE (decl))
23873 current_access_specifier = access_private_node;
23874 else if (TREE_PROTECTED (decl))
23875 current_access_specifier = access_protected_node;
23876 else
23877 current_access_specifier = access_public_node;
23878 }
23879
23880 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23881 is the instantiation (which should have been created with
23882 start_enum) and ARGS are the template arguments to use. */
23883
23884 static void
23885 tsubst_enum (tree tag, tree newtag, tree args)
23886 {
23887 tree e;
23888
23889 if (SCOPED_ENUM_P (newtag))
23890 begin_scope (sk_scoped_enum, newtag);
23891
23892 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23893 {
23894 tree value;
23895 tree decl;
23896
23897 decl = TREE_VALUE (e);
23898 /* Note that in a template enum, the TREE_VALUE is the
23899 CONST_DECL, not the corresponding INTEGER_CST. */
23900 value = tsubst_expr (DECL_INITIAL (decl),
23901 args, tf_warning_or_error, NULL_TREE,
23902 /*integral_constant_expression_p=*/true);
23903
23904 /* Give this enumeration constant the correct access. */
23905 set_current_access_from_decl (decl);
23906
23907 /* Actually build the enumerator itself. Here we're assuming that
23908 enumerators can't have dependent attributes. */
23909 build_enumerator (DECL_NAME (decl), value, newtag,
23910 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23911 }
23912
23913 if (SCOPED_ENUM_P (newtag))
23914 finish_scope ();
23915
23916 finish_enum_value_list (newtag);
23917 finish_enum (newtag);
23918
23919 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23920 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23921 }
23922
23923 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23924 its type -- but without substituting the innermost set of template
23925 arguments. So, innermost set of template parameters will appear in
23926 the type. */
23927
23928 tree
23929 get_mostly_instantiated_function_type (tree decl)
23930 {
23931 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23932 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23933 }
23934
23935 /* Return truthvalue if we're processing a template different from
23936 the last one involved in diagnostics. */
23937 bool
23938 problematic_instantiation_changed (void)
23939 {
23940 return current_tinst_level != last_error_tinst_level;
23941 }
23942
23943 /* Remember current template involved in diagnostics. */
23944 void
23945 record_last_problematic_instantiation (void)
23946 {
23947 last_error_tinst_level = current_tinst_level;
23948 }
23949
23950 struct tinst_level *
23951 current_instantiation (void)
23952 {
23953 return current_tinst_level;
23954 }
23955
23956 /* Return TRUE if current_function_decl is being instantiated, false
23957 otherwise. */
23958
23959 bool
23960 instantiating_current_function_p (void)
23961 {
23962 return (current_instantiation ()
23963 && current_instantiation ()->decl == current_function_decl);
23964 }
23965
23966 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23967 type. Return false for ok, true for disallowed. Issue error and
23968 inform messages under control of COMPLAIN. */
23969
23970 static bool
23971 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23972 {
23973 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23974 return false;
23975 else if (POINTER_TYPE_P (type))
23976 return false;
23977 else if (TYPE_PTRMEM_P (type))
23978 return false;
23979 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23980 return false;
23981 else if (TREE_CODE (type) == TYPENAME_TYPE)
23982 return false;
23983 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23984 return false;
23985 else if (TREE_CODE (type) == NULLPTR_TYPE)
23986 return false;
23987 /* A bound template template parm could later be instantiated to have a valid
23988 nontype parm type via an alias template. */
23989 else if (cxx_dialect >= cxx11
23990 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23991 return false;
23992
23993 if (complain & tf_error)
23994 {
23995 if (type == error_mark_node)
23996 inform (input_location, "invalid template non-type parameter");
23997 else
23998 error ("%q#T is not a valid type for a template non-type parameter",
23999 type);
24000 }
24001 return true;
24002 }
24003
24004 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24005 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24006
24007 static bool
24008 dependent_type_p_r (tree type)
24009 {
24010 tree scope;
24011
24012 /* [temp.dep.type]
24013
24014 A type is dependent if it is:
24015
24016 -- a template parameter. Template template parameters are types
24017 for us (since TYPE_P holds true for them) so we handle
24018 them here. */
24019 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24020 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
24021 return true;
24022 /* -- a qualified-id with a nested-name-specifier which contains a
24023 class-name that names a dependent type or whose unqualified-id
24024 names a dependent type. */
24025 if (TREE_CODE (type) == TYPENAME_TYPE)
24026 return true;
24027
24028 /* An alias template specialization can be dependent even if the
24029 resulting type is not. */
24030 if (dependent_alias_template_spec_p (type))
24031 return true;
24032
24033 /* -- a cv-qualified type where the cv-unqualified type is
24034 dependent.
24035 No code is necessary for this bullet; the code below handles
24036 cv-qualified types, and we don't want to strip aliases with
24037 TYPE_MAIN_VARIANT because of DR 1558. */
24038 /* -- a compound type constructed from any dependent type. */
24039 if (TYPE_PTRMEM_P (type))
24040 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24041 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24042 (type)));
24043 else if (TYPE_PTR_P (type)
24044 || TREE_CODE (type) == REFERENCE_TYPE)
24045 return dependent_type_p (TREE_TYPE (type));
24046 else if (TREE_CODE (type) == FUNCTION_TYPE
24047 || TREE_CODE (type) == METHOD_TYPE)
24048 {
24049 tree arg_type;
24050
24051 if (dependent_type_p (TREE_TYPE (type)))
24052 return true;
24053 for (arg_type = TYPE_ARG_TYPES (type);
24054 arg_type;
24055 arg_type = TREE_CHAIN (arg_type))
24056 if (dependent_type_p (TREE_VALUE (arg_type)))
24057 return true;
24058 if (cxx_dialect >= cxx17)
24059 /* A value-dependent noexcept-specifier makes the type dependent. */
24060 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24061 if (tree noex = TREE_PURPOSE (spec))
24062 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24063 affect overload resolution and treating it as dependent breaks
24064 things. */
24065 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24066 && value_dependent_expression_p (noex))
24067 return true;
24068 return false;
24069 }
24070 /* -- an array type constructed from any dependent type or whose
24071 size is specified by a constant expression that is
24072 value-dependent.
24073
24074 We checked for type- and value-dependence of the bounds in
24075 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24076 if (TREE_CODE (type) == ARRAY_TYPE)
24077 {
24078 if (TYPE_DOMAIN (type)
24079 && dependent_type_p (TYPE_DOMAIN (type)))
24080 return true;
24081 return dependent_type_p (TREE_TYPE (type));
24082 }
24083
24084 /* -- a template-id in which either the template name is a template
24085 parameter ... */
24086 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24087 return true;
24088 /* ... or any of the template arguments is a dependent type or
24089 an expression that is type-dependent or value-dependent. */
24090 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24091 && (any_dependent_template_arguments_p
24092 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24093 return true;
24094
24095 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24096 dependent; if the argument of the `typeof' expression is not
24097 type-dependent, then it should already been have resolved. */
24098 if (TREE_CODE (type) == TYPEOF_TYPE
24099 || TREE_CODE (type) == DECLTYPE_TYPE
24100 || TREE_CODE (type) == UNDERLYING_TYPE)
24101 return true;
24102
24103 /* A template argument pack is dependent if any of its packed
24104 arguments are. */
24105 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24106 {
24107 tree args = ARGUMENT_PACK_ARGS (type);
24108 int i, len = TREE_VEC_LENGTH (args);
24109 for (i = 0; i < len; ++i)
24110 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24111 return true;
24112 }
24113
24114 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24115 be template parameters. */
24116 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24117 return true;
24118
24119 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24120 return true;
24121
24122 /* The standard does not specifically mention types that are local
24123 to template functions or local classes, but they should be
24124 considered dependent too. For example:
24125
24126 template <int I> void f() {
24127 enum E { a = I };
24128 S<sizeof (E)> s;
24129 }
24130
24131 The size of `E' cannot be known until the value of `I' has been
24132 determined. Therefore, `E' must be considered dependent. */
24133 scope = TYPE_CONTEXT (type);
24134 if (scope && TYPE_P (scope))
24135 return dependent_type_p (scope);
24136 /* Don't use type_dependent_expression_p here, as it can lead
24137 to infinite recursion trying to determine whether a lambda
24138 nested in a lambda is dependent (c++/47687). */
24139 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24140 && DECL_LANG_SPECIFIC (scope)
24141 && DECL_TEMPLATE_INFO (scope)
24142 && (any_dependent_template_arguments_p
24143 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24144 return true;
24145
24146 /* Other types are non-dependent. */
24147 return false;
24148 }
24149
24150 /* Returns TRUE if TYPE is dependent, in the sense of
24151 [temp.dep.type]. Note that a NULL type is considered dependent. */
24152
24153 bool
24154 dependent_type_p (tree type)
24155 {
24156 /* If there are no template parameters in scope, then there can't be
24157 any dependent types. */
24158 if (!processing_template_decl)
24159 {
24160 /* If we are not processing a template, then nobody should be
24161 providing us with a dependent type. */
24162 gcc_assert (type);
24163 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24164 return false;
24165 }
24166
24167 /* If the type is NULL, we have not computed a type for the entity
24168 in question; in that case, the type is dependent. */
24169 if (!type)
24170 return true;
24171
24172 /* Erroneous types can be considered non-dependent. */
24173 if (type == error_mark_node)
24174 return false;
24175
24176 /* Getting here with global_type_node means we improperly called this
24177 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24178 gcc_checking_assert (type != global_type_node);
24179
24180 /* If we have not already computed the appropriate value for TYPE,
24181 do so now. */
24182 if (!TYPE_DEPENDENT_P_VALID (type))
24183 {
24184 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24185 TYPE_DEPENDENT_P_VALID (type) = 1;
24186 }
24187
24188 return TYPE_DEPENDENT_P (type);
24189 }
24190
24191 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24192 lookup. In other words, a dependent type that is not the current
24193 instantiation. */
24194
24195 bool
24196 dependent_scope_p (tree scope)
24197 {
24198 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24199 && !currently_open_class (scope));
24200 }
24201
24202 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24203 an unknown base of 'this' (and is therefore instantiation-dependent). */
24204
24205 static bool
24206 unknown_base_ref_p (tree t)
24207 {
24208 if (!current_class_ptr)
24209 return false;
24210
24211 tree mem = TREE_OPERAND (t, 1);
24212 if (shared_member_p (mem))
24213 return false;
24214
24215 tree cur = current_nonlambda_class_type ();
24216 if (!any_dependent_bases_p (cur))
24217 return false;
24218
24219 tree ctx = TREE_OPERAND (t, 0);
24220 if (DERIVED_FROM_P (ctx, cur))
24221 return false;
24222
24223 return true;
24224 }
24225
24226 /* T is a SCOPE_REF; return whether we need to consider it
24227 instantiation-dependent so that we can check access at instantiation
24228 time even though we know which member it resolves to. */
24229
24230 static bool
24231 instantiation_dependent_scope_ref_p (tree t)
24232 {
24233 if (DECL_P (TREE_OPERAND (t, 1))
24234 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24235 && !unknown_base_ref_p (t)
24236 && accessible_in_template_p (TREE_OPERAND (t, 0),
24237 TREE_OPERAND (t, 1)))
24238 return false;
24239 else
24240 return true;
24241 }
24242
24243 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24244 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24245 expression. */
24246
24247 /* Note that this predicate is not appropriate for general expressions;
24248 only constant expressions (that satisfy potential_constant_expression)
24249 can be tested for value dependence. */
24250
24251 bool
24252 value_dependent_expression_p (tree expression)
24253 {
24254 if (!processing_template_decl || expression == NULL_TREE)
24255 return false;
24256
24257 /* A type-dependent expression is also value-dependent. */
24258 if (type_dependent_expression_p (expression))
24259 return true;
24260
24261 switch (TREE_CODE (expression))
24262 {
24263 case BASELINK:
24264 /* A dependent member function of the current instantiation. */
24265 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24266
24267 case FUNCTION_DECL:
24268 /* A dependent member function of the current instantiation. */
24269 if (DECL_CLASS_SCOPE_P (expression)
24270 && dependent_type_p (DECL_CONTEXT (expression)))
24271 return true;
24272 break;
24273
24274 case IDENTIFIER_NODE:
24275 /* A name that has not been looked up -- must be dependent. */
24276 return true;
24277
24278 case TEMPLATE_PARM_INDEX:
24279 /* A non-type template parm. */
24280 return true;
24281
24282 case CONST_DECL:
24283 /* A non-type template parm. */
24284 if (DECL_TEMPLATE_PARM_P (expression))
24285 return true;
24286 return value_dependent_expression_p (DECL_INITIAL (expression));
24287
24288 case VAR_DECL:
24289 /* A constant with literal type and is initialized
24290 with an expression that is value-dependent. */
24291 if (DECL_DEPENDENT_INIT_P (expression)
24292 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24293 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24294 return true;
24295 if (DECL_HAS_VALUE_EXPR_P (expression))
24296 {
24297 tree value_expr = DECL_VALUE_EXPR (expression);
24298 if (value_dependent_expression_p (value_expr))
24299 return true;
24300 }
24301 return false;
24302
24303 case DYNAMIC_CAST_EXPR:
24304 case STATIC_CAST_EXPR:
24305 case CONST_CAST_EXPR:
24306 case REINTERPRET_CAST_EXPR:
24307 case CAST_EXPR:
24308 case IMPLICIT_CONV_EXPR:
24309 /* These expressions are value-dependent if the type to which
24310 the cast occurs is dependent or the expression being casted
24311 is value-dependent. */
24312 {
24313 tree type = TREE_TYPE (expression);
24314
24315 if (dependent_type_p (type))
24316 return true;
24317
24318 /* A functional cast has a list of operands. */
24319 expression = TREE_OPERAND (expression, 0);
24320 if (!expression)
24321 {
24322 /* If there are no operands, it must be an expression such
24323 as "int()". This should not happen for aggregate types
24324 because it would form non-constant expressions. */
24325 gcc_assert (cxx_dialect >= cxx11
24326 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24327
24328 return false;
24329 }
24330
24331 if (TREE_CODE (expression) == TREE_LIST)
24332 return any_value_dependent_elements_p (expression);
24333
24334 return value_dependent_expression_p (expression);
24335 }
24336
24337 case SIZEOF_EXPR:
24338 if (SIZEOF_EXPR_TYPE_P (expression))
24339 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24340 /* FALLTHRU */
24341 case ALIGNOF_EXPR:
24342 case TYPEID_EXPR:
24343 /* A `sizeof' expression is value-dependent if the operand is
24344 type-dependent or is a pack expansion. */
24345 expression = TREE_OPERAND (expression, 0);
24346 if (PACK_EXPANSION_P (expression))
24347 return true;
24348 else if (TYPE_P (expression))
24349 return dependent_type_p (expression);
24350 return instantiation_dependent_uneval_expression_p (expression);
24351
24352 case AT_ENCODE_EXPR:
24353 /* An 'encode' expression is value-dependent if the operand is
24354 type-dependent. */
24355 expression = TREE_OPERAND (expression, 0);
24356 return dependent_type_p (expression);
24357
24358 case NOEXCEPT_EXPR:
24359 expression = TREE_OPERAND (expression, 0);
24360 return instantiation_dependent_uneval_expression_p (expression);
24361
24362 case SCOPE_REF:
24363 /* All instantiation-dependent expressions should also be considered
24364 value-dependent. */
24365 return instantiation_dependent_scope_ref_p (expression);
24366
24367 case COMPONENT_REF:
24368 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24369 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24370
24371 case NONTYPE_ARGUMENT_PACK:
24372 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24373 is value-dependent. */
24374 {
24375 tree values = ARGUMENT_PACK_ARGS (expression);
24376 int i, len = TREE_VEC_LENGTH (values);
24377
24378 for (i = 0; i < len; ++i)
24379 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24380 return true;
24381
24382 return false;
24383 }
24384
24385 case TRAIT_EXPR:
24386 {
24387 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24388
24389 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24390 return true;
24391
24392 if (!type2)
24393 return false;
24394
24395 if (TREE_CODE (type2) != TREE_LIST)
24396 return dependent_type_p (type2);
24397
24398 for (; type2; type2 = TREE_CHAIN (type2))
24399 if (dependent_type_p (TREE_VALUE (type2)))
24400 return true;
24401
24402 return false;
24403 }
24404
24405 case MODOP_EXPR:
24406 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24407 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24408
24409 case ARRAY_REF:
24410 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24411 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24412
24413 case ADDR_EXPR:
24414 {
24415 tree op = TREE_OPERAND (expression, 0);
24416 return (value_dependent_expression_p (op)
24417 || has_value_dependent_address (op));
24418 }
24419
24420 case REQUIRES_EXPR:
24421 /* Treat all requires-expressions as value-dependent so
24422 we don't try to fold them. */
24423 return true;
24424
24425 case TYPE_REQ:
24426 return dependent_type_p (TREE_OPERAND (expression, 0));
24427
24428 case CALL_EXPR:
24429 {
24430 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24431 return true;
24432 tree fn = get_callee_fndecl (expression);
24433 int i, nargs;
24434 nargs = call_expr_nargs (expression);
24435 for (i = 0; i < nargs; ++i)
24436 {
24437 tree op = CALL_EXPR_ARG (expression, i);
24438 /* In a call to a constexpr member function, look through the
24439 implicit ADDR_EXPR on the object argument so that it doesn't
24440 cause the call to be considered value-dependent. We also
24441 look through it in potential_constant_expression. */
24442 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24443 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24444 && TREE_CODE (op) == ADDR_EXPR)
24445 op = TREE_OPERAND (op, 0);
24446 if (value_dependent_expression_p (op))
24447 return true;
24448 }
24449 return false;
24450 }
24451
24452 case TEMPLATE_ID_EXPR:
24453 return variable_concept_p (TREE_OPERAND (expression, 0));
24454
24455 case CONSTRUCTOR:
24456 {
24457 unsigned ix;
24458 tree val;
24459 if (dependent_type_p (TREE_TYPE (expression)))
24460 return true;
24461 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24462 if (value_dependent_expression_p (val))
24463 return true;
24464 return false;
24465 }
24466
24467 case STMT_EXPR:
24468 /* Treat a GNU statement expression as dependent to avoid crashing
24469 under instantiate_non_dependent_expr; it can't be constant. */
24470 return true;
24471
24472 default:
24473 /* A constant expression is value-dependent if any subexpression is
24474 value-dependent. */
24475 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24476 {
24477 case tcc_reference:
24478 case tcc_unary:
24479 case tcc_comparison:
24480 case tcc_binary:
24481 case tcc_expression:
24482 case tcc_vl_exp:
24483 {
24484 int i, len = cp_tree_operand_length (expression);
24485
24486 for (i = 0; i < len; i++)
24487 {
24488 tree t = TREE_OPERAND (expression, i);
24489
24490 /* In some cases, some of the operands may be missing.
24491 (For example, in the case of PREDECREMENT_EXPR, the
24492 amount to increment by may be missing.) That doesn't
24493 make the expression dependent. */
24494 if (t && value_dependent_expression_p (t))
24495 return true;
24496 }
24497 }
24498 break;
24499 default:
24500 break;
24501 }
24502 break;
24503 }
24504
24505 /* The expression is not value-dependent. */
24506 return false;
24507 }
24508
24509 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24510 [temp.dep.expr]. Note that an expression with no type is
24511 considered dependent. Other parts of the compiler arrange for an
24512 expression with type-dependent subexpressions to have no type, so
24513 this function doesn't have to be fully recursive. */
24514
24515 bool
24516 type_dependent_expression_p (tree expression)
24517 {
24518 if (!processing_template_decl)
24519 return false;
24520
24521 if (expression == NULL_TREE || expression == error_mark_node)
24522 return false;
24523
24524 STRIP_ANY_LOCATION_WRAPPER (expression);
24525
24526 /* An unresolved name is always dependent. */
24527 if (identifier_p (expression)
24528 || TREE_CODE (expression) == USING_DECL
24529 || TREE_CODE (expression) == WILDCARD_DECL)
24530 return true;
24531
24532 /* A fold expression is type-dependent. */
24533 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24534 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24535 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24536 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24537 return true;
24538
24539 /* Some expression forms are never type-dependent. */
24540 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24541 || TREE_CODE (expression) == SIZEOF_EXPR
24542 || TREE_CODE (expression) == ALIGNOF_EXPR
24543 || TREE_CODE (expression) == AT_ENCODE_EXPR
24544 || TREE_CODE (expression) == NOEXCEPT_EXPR
24545 || TREE_CODE (expression) == TRAIT_EXPR
24546 || TREE_CODE (expression) == TYPEID_EXPR
24547 || TREE_CODE (expression) == DELETE_EXPR
24548 || TREE_CODE (expression) == VEC_DELETE_EXPR
24549 || TREE_CODE (expression) == THROW_EXPR
24550 || TREE_CODE (expression) == REQUIRES_EXPR)
24551 return false;
24552
24553 /* The types of these expressions depends only on the type to which
24554 the cast occurs. */
24555 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24556 || TREE_CODE (expression) == STATIC_CAST_EXPR
24557 || TREE_CODE (expression) == CONST_CAST_EXPR
24558 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24559 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24560 || TREE_CODE (expression) == CAST_EXPR)
24561 return dependent_type_p (TREE_TYPE (expression));
24562
24563 /* The types of these expressions depends only on the type created
24564 by the expression. */
24565 if (TREE_CODE (expression) == NEW_EXPR
24566 || TREE_CODE (expression) == VEC_NEW_EXPR)
24567 {
24568 /* For NEW_EXPR tree nodes created inside a template, either
24569 the object type itself or a TREE_LIST may appear as the
24570 operand 1. */
24571 tree type = TREE_OPERAND (expression, 1);
24572 if (TREE_CODE (type) == TREE_LIST)
24573 /* This is an array type. We need to check array dimensions
24574 as well. */
24575 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24576 || value_dependent_expression_p
24577 (TREE_OPERAND (TREE_VALUE (type), 1));
24578 else
24579 return dependent_type_p (type);
24580 }
24581
24582 if (TREE_CODE (expression) == SCOPE_REF)
24583 {
24584 tree scope = TREE_OPERAND (expression, 0);
24585 tree name = TREE_OPERAND (expression, 1);
24586
24587 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24588 contains an identifier associated by name lookup with one or more
24589 declarations declared with a dependent type, or...a
24590 nested-name-specifier or qualified-id that names a member of an
24591 unknown specialization. */
24592 return (type_dependent_expression_p (name)
24593 || dependent_scope_p (scope));
24594 }
24595
24596 if (TREE_CODE (expression) == TEMPLATE_DECL
24597 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24598 return uses_outer_template_parms (expression);
24599
24600 if (TREE_CODE (expression) == STMT_EXPR)
24601 expression = stmt_expr_value_expr (expression);
24602
24603 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24604 {
24605 tree elt;
24606 unsigned i;
24607
24608 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24609 {
24610 if (type_dependent_expression_p (elt))
24611 return true;
24612 }
24613 return false;
24614 }
24615
24616 /* A static data member of the current instantiation with incomplete
24617 array type is type-dependent, as the definition and specializations
24618 can have different bounds. */
24619 if (VAR_P (expression)
24620 && DECL_CLASS_SCOPE_P (expression)
24621 && dependent_type_p (DECL_CONTEXT (expression))
24622 && VAR_HAD_UNKNOWN_BOUND (expression))
24623 return true;
24624
24625 /* An array of unknown bound depending on a variadic parameter, eg:
24626
24627 template<typename... Args>
24628 void foo (Args... args)
24629 {
24630 int arr[] = { args... };
24631 }
24632
24633 template<int... vals>
24634 void bar ()
24635 {
24636 int arr[] = { vals... };
24637 }
24638
24639 If the array has no length and has an initializer, it must be that
24640 we couldn't determine its length in cp_complete_array_type because
24641 it is dependent. */
24642 if (VAR_P (expression)
24643 && TREE_TYPE (expression) != NULL_TREE
24644 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24645 && !TYPE_DOMAIN (TREE_TYPE (expression))
24646 && DECL_INITIAL (expression))
24647 return true;
24648
24649 /* A function or variable template-id is type-dependent if it has any
24650 dependent template arguments. */
24651 if (VAR_OR_FUNCTION_DECL_P (expression)
24652 && DECL_LANG_SPECIFIC (expression)
24653 && DECL_TEMPLATE_INFO (expression))
24654 {
24655 /* Consider the innermost template arguments, since those are the ones
24656 that come from the template-id; the template arguments for the
24657 enclosing class do not make it type-dependent unless they are used in
24658 the type of the decl. */
24659 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24660 && (any_dependent_template_arguments_p
24661 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24662 return true;
24663 }
24664
24665 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24666 type-dependent. Checking this is important for functions with auto return
24667 type, which looks like a dependent type. */
24668 if (TREE_CODE (expression) == FUNCTION_DECL
24669 && !(DECL_CLASS_SCOPE_P (expression)
24670 && dependent_type_p (DECL_CONTEXT (expression)))
24671 && !(DECL_LANG_SPECIFIC (expression)
24672 && DECL_FRIEND_P (expression)
24673 && (!DECL_FRIEND_CONTEXT (expression)
24674 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24675 && !DECL_LOCAL_FUNCTION_P (expression))
24676 {
24677 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24678 || undeduced_auto_decl (expression));
24679 return false;
24680 }
24681
24682 /* Always dependent, on the number of arguments if nothing else. */
24683 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24684 return true;
24685
24686 if (TREE_TYPE (expression) == unknown_type_node)
24687 {
24688 if (TREE_CODE (expression) == ADDR_EXPR)
24689 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24690 if (TREE_CODE (expression) == COMPONENT_REF
24691 || TREE_CODE (expression) == OFFSET_REF)
24692 {
24693 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24694 return true;
24695 expression = TREE_OPERAND (expression, 1);
24696 if (identifier_p (expression))
24697 return false;
24698 }
24699 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24700 if (TREE_CODE (expression) == SCOPE_REF)
24701 return false;
24702
24703 if (BASELINK_P (expression))
24704 {
24705 if (BASELINK_OPTYPE (expression)
24706 && dependent_type_p (BASELINK_OPTYPE (expression)))
24707 return true;
24708 expression = BASELINK_FUNCTIONS (expression);
24709 }
24710
24711 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24712 {
24713 if (any_dependent_template_arguments_p
24714 (TREE_OPERAND (expression, 1)))
24715 return true;
24716 expression = TREE_OPERAND (expression, 0);
24717 if (identifier_p (expression))
24718 return true;
24719 }
24720
24721 gcc_assert (TREE_CODE (expression) == OVERLOAD
24722 || TREE_CODE (expression) == FUNCTION_DECL);
24723
24724 for (lkp_iterator iter (expression); iter; ++iter)
24725 if (type_dependent_expression_p (*iter))
24726 return true;
24727
24728 return false;
24729 }
24730
24731 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24732
24733 /* Dependent type attributes might not have made it from the decl to
24734 the type yet. */
24735 if (DECL_P (expression)
24736 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24737 return true;
24738
24739 return (dependent_type_p (TREE_TYPE (expression)));
24740 }
24741
24742 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24743 type-dependent if the expression refers to a member of the current
24744 instantiation and the type of the referenced member is dependent, or the
24745 class member access expression refers to a member of an unknown
24746 specialization.
24747
24748 This function returns true if the OBJECT in such a class member access
24749 expression is of an unknown specialization. */
24750
24751 bool
24752 type_dependent_object_expression_p (tree object)
24753 {
24754 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24755 dependent. */
24756 if (TREE_CODE (object) == IDENTIFIER_NODE)
24757 return true;
24758 tree scope = TREE_TYPE (object);
24759 return (!scope || dependent_scope_p (scope));
24760 }
24761
24762 /* walk_tree callback function for instantiation_dependent_expression_p,
24763 below. Returns non-zero if a dependent subexpression is found. */
24764
24765 static tree
24766 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24767 void * /*data*/)
24768 {
24769 if (TYPE_P (*tp))
24770 {
24771 /* We don't have to worry about decltype currently because decltype
24772 of an instantiation-dependent expr is a dependent type. This
24773 might change depending on the resolution of DR 1172. */
24774 *walk_subtrees = false;
24775 return NULL_TREE;
24776 }
24777 enum tree_code code = TREE_CODE (*tp);
24778 switch (code)
24779 {
24780 /* Don't treat an argument list as dependent just because it has no
24781 TREE_TYPE. */
24782 case TREE_LIST:
24783 case TREE_VEC:
24784 return NULL_TREE;
24785
24786 case TEMPLATE_PARM_INDEX:
24787 return *tp;
24788
24789 /* Handle expressions with type operands. */
24790 case SIZEOF_EXPR:
24791 case ALIGNOF_EXPR:
24792 case TYPEID_EXPR:
24793 case AT_ENCODE_EXPR:
24794 {
24795 tree op = TREE_OPERAND (*tp, 0);
24796 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24797 op = TREE_TYPE (op);
24798 if (TYPE_P (op))
24799 {
24800 if (dependent_type_p (op))
24801 return *tp;
24802 else
24803 {
24804 *walk_subtrees = false;
24805 return NULL_TREE;
24806 }
24807 }
24808 break;
24809 }
24810
24811 case COMPONENT_REF:
24812 if (identifier_p (TREE_OPERAND (*tp, 1)))
24813 /* In a template, finish_class_member_access_expr creates a
24814 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24815 type-dependent, so that we can check access control at
24816 instantiation time (PR 42277). See also Core issue 1273. */
24817 return *tp;
24818 break;
24819
24820 case SCOPE_REF:
24821 if (instantiation_dependent_scope_ref_p (*tp))
24822 return *tp;
24823 else
24824 break;
24825
24826 /* Treat statement-expressions as dependent. */
24827 case BIND_EXPR:
24828 return *tp;
24829
24830 /* Treat requires-expressions as dependent. */
24831 case REQUIRES_EXPR:
24832 return *tp;
24833
24834 case CALL_EXPR:
24835 /* Treat calls to function concepts as dependent. */
24836 if (function_concept_check_p (*tp))
24837 return *tp;
24838 break;
24839
24840 case TEMPLATE_ID_EXPR:
24841 /* And variable concepts. */
24842 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24843 return *tp;
24844 break;
24845
24846 default:
24847 break;
24848 }
24849
24850 if (type_dependent_expression_p (*tp))
24851 return *tp;
24852 else
24853 return NULL_TREE;
24854 }
24855
24856 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24857 sense defined by the ABI:
24858
24859 "An expression is instantiation-dependent if it is type-dependent
24860 or value-dependent, or it has a subexpression that is type-dependent
24861 or value-dependent."
24862
24863 Except don't actually check value-dependence for unevaluated expressions,
24864 because in sizeof(i) we don't care about the value of i. Checking
24865 type-dependence will in turn check value-dependence of array bounds/template
24866 arguments as needed. */
24867
24868 bool
24869 instantiation_dependent_uneval_expression_p (tree expression)
24870 {
24871 tree result;
24872
24873 if (!processing_template_decl)
24874 return false;
24875
24876 if (expression == error_mark_node)
24877 return false;
24878
24879 result = cp_walk_tree_without_duplicates (&expression,
24880 instantiation_dependent_r, NULL);
24881 return result != NULL_TREE;
24882 }
24883
24884 /* As above, but also check value-dependence of the expression as a whole. */
24885
24886 bool
24887 instantiation_dependent_expression_p (tree expression)
24888 {
24889 return (instantiation_dependent_uneval_expression_p (expression)
24890 || value_dependent_expression_p (expression));
24891 }
24892
24893 /* Like type_dependent_expression_p, but it also works while not processing
24894 a template definition, i.e. during substitution or mangling. */
24895
24896 bool
24897 type_dependent_expression_p_push (tree expr)
24898 {
24899 bool b;
24900 ++processing_template_decl;
24901 b = type_dependent_expression_p (expr);
24902 --processing_template_decl;
24903 return b;
24904 }
24905
24906 /* Returns TRUE if ARGS contains a type-dependent expression. */
24907
24908 bool
24909 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24910 {
24911 unsigned int i;
24912 tree arg;
24913
24914 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24915 {
24916 if (type_dependent_expression_p (arg))
24917 return true;
24918 }
24919 return false;
24920 }
24921
24922 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24923 expressions) contains any type-dependent expressions. */
24924
24925 bool
24926 any_type_dependent_elements_p (const_tree list)
24927 {
24928 for (; list; list = TREE_CHAIN (list))
24929 if (type_dependent_expression_p (TREE_VALUE (list)))
24930 return true;
24931
24932 return false;
24933 }
24934
24935 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24936 expressions) contains any value-dependent expressions. */
24937
24938 bool
24939 any_value_dependent_elements_p (const_tree list)
24940 {
24941 for (; list; list = TREE_CHAIN (list))
24942 if (value_dependent_expression_p (TREE_VALUE (list)))
24943 return true;
24944
24945 return false;
24946 }
24947
24948 /* Returns TRUE if the ARG (a template argument) is dependent. */
24949
24950 bool
24951 dependent_template_arg_p (tree arg)
24952 {
24953 if (!processing_template_decl)
24954 return false;
24955
24956 /* Assume a template argument that was wrongly written by the user
24957 is dependent. This is consistent with what
24958 any_dependent_template_arguments_p [that calls this function]
24959 does. */
24960 if (!arg || arg == error_mark_node)
24961 return true;
24962
24963 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24964 arg = argument_pack_select_arg (arg);
24965
24966 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24967 return true;
24968 if (TREE_CODE (arg) == TEMPLATE_DECL)
24969 {
24970 if (DECL_TEMPLATE_PARM_P (arg))
24971 return true;
24972 /* A member template of a dependent class is not necessarily
24973 type-dependent, but it is a dependent template argument because it
24974 will be a member of an unknown specialization to that template. */
24975 tree scope = CP_DECL_CONTEXT (arg);
24976 return TYPE_P (scope) && dependent_type_p (scope);
24977 }
24978 else if (ARGUMENT_PACK_P (arg))
24979 {
24980 tree args = ARGUMENT_PACK_ARGS (arg);
24981 int i, len = TREE_VEC_LENGTH (args);
24982 for (i = 0; i < len; ++i)
24983 {
24984 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24985 return true;
24986 }
24987
24988 return false;
24989 }
24990 else if (TYPE_P (arg))
24991 return dependent_type_p (arg);
24992 else
24993 return (type_dependent_expression_p (arg)
24994 || value_dependent_expression_p (arg));
24995 }
24996
24997 /* Returns true if ARGS (a collection of template arguments) contains
24998 any types that require structural equality testing. */
24999
25000 bool
25001 any_template_arguments_need_structural_equality_p (tree args)
25002 {
25003 int i;
25004 int j;
25005
25006 if (!args)
25007 return false;
25008 if (args == error_mark_node)
25009 return true;
25010
25011 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25012 {
25013 tree level = TMPL_ARGS_LEVEL (args, i + 1);
25014 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25015 {
25016 tree arg = TREE_VEC_ELT (level, j);
25017 tree packed_args = NULL_TREE;
25018 int k, len = 1;
25019
25020 if (ARGUMENT_PACK_P (arg))
25021 {
25022 /* Look inside the argument pack. */
25023 packed_args = ARGUMENT_PACK_ARGS (arg);
25024 len = TREE_VEC_LENGTH (packed_args);
25025 }
25026
25027 for (k = 0; k < len; ++k)
25028 {
25029 if (packed_args)
25030 arg = TREE_VEC_ELT (packed_args, k);
25031
25032 if (error_operand_p (arg))
25033 return true;
25034 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25035 continue;
25036 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25037 return true;
25038 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25039 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25040 return true;
25041 }
25042 }
25043 }
25044
25045 return false;
25046 }
25047
25048 /* Returns true if ARGS (a collection of template arguments) contains
25049 any dependent arguments. */
25050
25051 bool
25052 any_dependent_template_arguments_p (const_tree args)
25053 {
25054 int i;
25055 int j;
25056
25057 if (!args)
25058 return false;
25059 if (args == error_mark_node)
25060 return true;
25061
25062 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25063 {
25064 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25065 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25066 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25067 return true;
25068 }
25069
25070 return false;
25071 }
25072
25073 /* Returns true if ARGS contains any errors. */
25074
25075 bool
25076 any_erroneous_template_args_p (const_tree args)
25077 {
25078 int i;
25079 int j;
25080
25081 if (args == error_mark_node)
25082 return true;
25083
25084 if (args && TREE_CODE (args) != TREE_VEC)
25085 {
25086 if (tree ti = get_template_info (args))
25087 args = TI_ARGS (ti);
25088 else
25089 args = NULL_TREE;
25090 }
25091
25092 if (!args)
25093 return false;
25094
25095 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25096 {
25097 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25098 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25099 if (error_operand_p (TREE_VEC_ELT (level, j)))
25100 return true;
25101 }
25102
25103 return false;
25104 }
25105
25106 /* Returns TRUE if the template TMPL is type-dependent. */
25107
25108 bool
25109 dependent_template_p (tree tmpl)
25110 {
25111 if (TREE_CODE (tmpl) == OVERLOAD)
25112 {
25113 for (lkp_iterator iter (tmpl); iter; ++iter)
25114 if (dependent_template_p (*iter))
25115 return true;
25116 return false;
25117 }
25118
25119 /* Template template parameters are dependent. */
25120 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25121 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25122 return true;
25123 /* So are names that have not been looked up. */
25124 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25125 return true;
25126 return false;
25127 }
25128
25129 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25130
25131 bool
25132 dependent_template_id_p (tree tmpl, tree args)
25133 {
25134 return (dependent_template_p (tmpl)
25135 || any_dependent_template_arguments_p (args));
25136 }
25137
25138 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25139 are dependent. */
25140
25141 bool
25142 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25143 {
25144 int i;
25145
25146 if (!processing_template_decl)
25147 return false;
25148
25149 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25150 {
25151 tree decl = TREE_VEC_ELT (declv, i);
25152 tree init = TREE_VEC_ELT (initv, i);
25153 tree cond = TREE_VEC_ELT (condv, i);
25154 tree incr = TREE_VEC_ELT (incrv, i);
25155
25156 if (type_dependent_expression_p (decl)
25157 || TREE_CODE (decl) == SCOPE_REF)
25158 return true;
25159
25160 if (init && type_dependent_expression_p (init))
25161 return true;
25162
25163 if (type_dependent_expression_p (cond))
25164 return true;
25165
25166 if (COMPARISON_CLASS_P (cond)
25167 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25168 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25169 return true;
25170
25171 if (TREE_CODE (incr) == MODOP_EXPR)
25172 {
25173 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25174 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25175 return true;
25176 }
25177 else if (type_dependent_expression_p (incr))
25178 return true;
25179 else if (TREE_CODE (incr) == MODIFY_EXPR)
25180 {
25181 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25182 return true;
25183 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25184 {
25185 tree t = TREE_OPERAND (incr, 1);
25186 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25187 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25188 return true;
25189 }
25190 }
25191 }
25192
25193 return false;
25194 }
25195
25196 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25197 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25198 no such TYPE can be found. Note that this function peers inside
25199 uninstantiated templates and therefore should be used only in
25200 extremely limited situations. ONLY_CURRENT_P restricts this
25201 peering to the currently open classes hierarchy (which is required
25202 when comparing types). */
25203
25204 tree
25205 resolve_typename_type (tree type, bool only_current_p)
25206 {
25207 tree scope;
25208 tree name;
25209 tree decl;
25210 int quals;
25211 tree pushed_scope;
25212 tree result;
25213
25214 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25215
25216 scope = TYPE_CONTEXT (type);
25217 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25218 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25219 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25220 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25221 identifier of the TYPENAME_TYPE anymore.
25222 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25223 TYPENAME_TYPE instead, we avoid messing up with a possible
25224 typedef variant case. */
25225 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25226
25227 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25228 it first before we can figure out what NAME refers to. */
25229 if (TREE_CODE (scope) == TYPENAME_TYPE)
25230 {
25231 if (TYPENAME_IS_RESOLVING_P (scope))
25232 /* Given a class template A with a dependent base with nested type C,
25233 typedef typename A::C::C C will land us here, as trying to resolve
25234 the initial A::C leads to the local C typedef, which leads back to
25235 A::C::C. So we break the recursion now. */
25236 return type;
25237 else
25238 scope = resolve_typename_type (scope, only_current_p);
25239 }
25240 /* If we don't know what SCOPE refers to, then we cannot resolve the
25241 TYPENAME_TYPE. */
25242 if (!CLASS_TYPE_P (scope))
25243 return type;
25244 /* If this is a typedef, we don't want to look inside (c++/11987). */
25245 if (typedef_variant_p (type))
25246 return type;
25247 /* If SCOPE isn't the template itself, it will not have a valid
25248 TYPE_FIELDS list. */
25249 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25250 /* scope is either the template itself or a compatible instantiation
25251 like X<T>, so look up the name in the original template. */
25252 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25253 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25254 gcc_checking_assert (uses_template_parms (scope));
25255 /* If scope has no fields, it can't be a current instantiation. Check this
25256 before currently_open_class to avoid infinite recursion (71515). */
25257 if (!TYPE_FIELDS (scope))
25258 return type;
25259 /* If the SCOPE is not the current instantiation, there's no reason
25260 to look inside it. */
25261 if (only_current_p && !currently_open_class (scope))
25262 return type;
25263 /* Enter the SCOPE so that name lookup will be resolved as if we
25264 were in the class definition. In particular, SCOPE will no
25265 longer be considered a dependent type. */
25266 pushed_scope = push_scope (scope);
25267 /* Look up the declaration. */
25268 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25269 tf_warning_or_error);
25270
25271 result = NULL_TREE;
25272
25273 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25274 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25275 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25276 if (!decl)
25277 /*nop*/;
25278 else if (identifier_p (fullname)
25279 && TREE_CODE (decl) == TYPE_DECL)
25280 {
25281 result = TREE_TYPE (decl);
25282 if (result == error_mark_node)
25283 result = NULL_TREE;
25284 }
25285 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25286 && DECL_CLASS_TEMPLATE_P (decl))
25287 {
25288 /* Obtain the template and the arguments. */
25289 tree tmpl = TREE_OPERAND (fullname, 0);
25290 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25291 {
25292 /* We get here with a plain identifier because a previous tentative
25293 parse of the nested-name-specifier as part of a ptr-operator saw
25294 ::template X<A>. The use of ::template is necessary in a
25295 ptr-operator, but wrong in a declarator-id.
25296
25297 [temp.names]: In a qualified-id of a declarator-id, the keyword
25298 template shall not appear at the top level. */
25299 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25300 "keyword %<template%> not allowed in declarator-id");
25301 tmpl = decl;
25302 }
25303 tree args = TREE_OPERAND (fullname, 1);
25304 /* Instantiate the template. */
25305 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25306 /*entering_scope=*/true,
25307 tf_error | tf_user);
25308 if (result == error_mark_node)
25309 result = NULL_TREE;
25310 }
25311
25312 /* Leave the SCOPE. */
25313 if (pushed_scope)
25314 pop_scope (pushed_scope);
25315
25316 /* If we failed to resolve it, return the original typename. */
25317 if (!result)
25318 return type;
25319
25320 /* If lookup found a typename type, resolve that too. */
25321 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25322 {
25323 /* Ill-formed programs can cause infinite recursion here, so we
25324 must catch that. */
25325 TYPENAME_IS_RESOLVING_P (result) = 1;
25326 result = resolve_typename_type (result, only_current_p);
25327 TYPENAME_IS_RESOLVING_P (result) = 0;
25328 }
25329
25330 /* Qualify the resulting type. */
25331 quals = cp_type_quals (type);
25332 if (quals)
25333 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25334
25335 return result;
25336 }
25337
25338 /* EXPR is an expression which is not type-dependent. Return a proxy
25339 for EXPR that can be used to compute the types of larger
25340 expressions containing EXPR. */
25341
25342 tree
25343 build_non_dependent_expr (tree expr)
25344 {
25345 tree orig_expr = expr;
25346 tree inner_expr;
25347
25348 /* When checking, try to get a constant value for all non-dependent
25349 expressions in order to expose bugs in *_dependent_expression_p
25350 and constexpr. This can affect code generation, see PR70704, so
25351 only do this for -fchecking=2. */
25352 if (flag_checking > 1
25353 && cxx_dialect >= cxx11
25354 /* Don't do this during nsdmi parsing as it can lead to
25355 unexpected recursive instantiations. */
25356 && !parsing_nsdmi ()
25357 /* Don't do this during concept expansion either and for
25358 the same reason. */
25359 && !expanding_concept ())
25360 fold_non_dependent_expr (expr);
25361
25362 STRIP_ANY_LOCATION_WRAPPER (expr);
25363
25364 /* Preserve OVERLOADs; the functions must be available to resolve
25365 types. */
25366 inner_expr = expr;
25367 if (TREE_CODE (inner_expr) == STMT_EXPR)
25368 inner_expr = stmt_expr_value_expr (inner_expr);
25369 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25370 inner_expr = TREE_OPERAND (inner_expr, 0);
25371 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25372 inner_expr = TREE_OPERAND (inner_expr, 1);
25373 if (is_overloaded_fn (inner_expr)
25374 || TREE_CODE (inner_expr) == OFFSET_REF)
25375 return orig_expr;
25376 /* There is no need to return a proxy for a variable. */
25377 if (VAR_P (expr))
25378 return orig_expr;
25379 /* Preserve string constants; conversions from string constants to
25380 "char *" are allowed, even though normally a "const char *"
25381 cannot be used to initialize a "char *". */
25382 if (TREE_CODE (expr) == STRING_CST)
25383 return orig_expr;
25384 /* Preserve void and arithmetic constants, as an optimization -- there is no
25385 reason to create a new node. */
25386 if (TREE_CODE (expr) == VOID_CST
25387 || TREE_CODE (expr) == INTEGER_CST
25388 || TREE_CODE (expr) == REAL_CST)
25389 return orig_expr;
25390 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25391 There is at least one place where we want to know that a
25392 particular expression is a throw-expression: when checking a ?:
25393 expression, there are special rules if the second or third
25394 argument is a throw-expression. */
25395 if (TREE_CODE (expr) == THROW_EXPR)
25396 return orig_expr;
25397
25398 /* Don't wrap an initializer list, we need to be able to look inside. */
25399 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25400 return orig_expr;
25401
25402 /* Don't wrap a dummy object, we need to be able to test for it. */
25403 if (is_dummy_object (expr))
25404 return orig_expr;
25405
25406 if (TREE_CODE (expr) == COND_EXPR)
25407 return build3 (COND_EXPR,
25408 TREE_TYPE (expr),
25409 TREE_OPERAND (expr, 0),
25410 (TREE_OPERAND (expr, 1)
25411 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25412 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25413 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25414 if (TREE_CODE (expr) == COMPOUND_EXPR
25415 && !COMPOUND_EXPR_OVERLOADED (expr))
25416 return build2 (COMPOUND_EXPR,
25417 TREE_TYPE (expr),
25418 TREE_OPERAND (expr, 0),
25419 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25420
25421 /* If the type is unknown, it can't really be non-dependent */
25422 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25423
25424 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25425 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25426 }
25427
25428 /* ARGS is a vector of expressions as arguments to a function call.
25429 Replace the arguments with equivalent non-dependent expressions.
25430 This modifies ARGS in place. */
25431
25432 void
25433 make_args_non_dependent (vec<tree, va_gc> *args)
25434 {
25435 unsigned int ix;
25436 tree arg;
25437
25438 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25439 {
25440 tree newarg = build_non_dependent_expr (arg);
25441 if (newarg != arg)
25442 (*args)[ix] = newarg;
25443 }
25444 }
25445
25446 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25447 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25448 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25449
25450 static tree
25451 make_auto_1 (tree name, bool set_canonical)
25452 {
25453 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25454 TYPE_NAME (au) = build_decl (input_location,
25455 TYPE_DECL, name, au);
25456 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25457 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25458 (0, processing_template_decl + 1, processing_template_decl + 1,
25459 TYPE_NAME (au), NULL_TREE);
25460 if (set_canonical)
25461 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25462 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25463 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25464
25465 return au;
25466 }
25467
25468 tree
25469 make_decltype_auto (void)
25470 {
25471 return make_auto_1 (decltype_auto_identifier, true);
25472 }
25473
25474 tree
25475 make_auto (void)
25476 {
25477 return make_auto_1 (auto_identifier, true);
25478 }
25479
25480 /* Return a C++17 deduction placeholder for class template TMPL. */
25481
25482 tree
25483 make_template_placeholder (tree tmpl)
25484 {
25485 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25486 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25487 return t;
25488 }
25489
25490 /* True iff T is a C++17 class template deduction placeholder. */
25491
25492 bool
25493 template_placeholder_p (tree t)
25494 {
25495 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25496 }
25497
25498 /* Make a "constrained auto" type-specifier. This is an
25499 auto type with constraints that must be associated after
25500 deduction. The constraint is formed from the given
25501 CONC and its optional sequence of arguments, which are
25502 non-null if written as partial-concept-id. */
25503
25504 tree
25505 make_constrained_auto (tree con, tree args)
25506 {
25507 tree type = make_auto_1 (auto_identifier, false);
25508
25509 /* Build the constraint. */
25510 tree tmpl = DECL_TI_TEMPLATE (con);
25511 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25512 expr = build_concept_check (expr, type, args);
25513
25514 tree constr = normalize_expression (expr);
25515 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25516
25517 /* Our canonical type depends on the constraint. */
25518 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25519
25520 /* Attach the constraint to the type declaration. */
25521 tree decl = TYPE_NAME (type);
25522 return decl;
25523 }
25524
25525 /* Given type ARG, return std::initializer_list<ARG>. */
25526
25527 static tree
25528 listify (tree arg)
25529 {
25530 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25531
25532 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25533 {
25534 gcc_rich_location richloc (input_location);
25535 maybe_add_include_fixit (&richloc, "<initializer_list>");
25536 error_at (&richloc,
25537 "deducing from brace-enclosed initializer list"
25538 " requires %<#include <initializer_list>%>");
25539
25540 return error_mark_node;
25541 }
25542 tree argvec = make_tree_vec (1);
25543 TREE_VEC_ELT (argvec, 0) = arg;
25544
25545 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25546 NULL_TREE, 0, tf_warning_or_error);
25547 }
25548
25549 /* Replace auto in TYPE with std::initializer_list<auto>. */
25550
25551 static tree
25552 listify_autos (tree type, tree auto_node)
25553 {
25554 tree init_auto = listify (auto_node);
25555 tree argvec = make_tree_vec (1);
25556 TREE_VEC_ELT (argvec, 0) = init_auto;
25557 if (processing_template_decl)
25558 argvec = add_to_template_args (current_template_args (), argvec);
25559 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25560 }
25561
25562 /* Hash traits for hashing possibly constrained 'auto'
25563 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25564
25565 struct auto_hash : default_hash_traits<tree>
25566 {
25567 static inline hashval_t hash (tree);
25568 static inline bool equal (tree, tree);
25569 };
25570
25571 /* Hash the 'auto' T. */
25572
25573 inline hashval_t
25574 auto_hash::hash (tree t)
25575 {
25576 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25577 /* Matching constrained-type-specifiers denote the same template
25578 parameter, so hash the constraint. */
25579 return hash_placeholder_constraint (c);
25580 else
25581 /* But unconstrained autos are all separate, so just hash the pointer. */
25582 return iterative_hash_object (t, 0);
25583 }
25584
25585 /* Compare two 'auto's. */
25586
25587 inline bool
25588 auto_hash::equal (tree t1, tree t2)
25589 {
25590 if (t1 == t2)
25591 return true;
25592
25593 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25594 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25595
25596 /* Two unconstrained autos are distinct. */
25597 if (!c1 || !c2)
25598 return false;
25599
25600 return equivalent_placeholder_constraints (c1, c2);
25601 }
25602
25603 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25604 constrained) auto, add it to the vector. */
25605
25606 static int
25607 extract_autos_r (tree t, void *data)
25608 {
25609 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25610 if (is_auto (t))
25611 {
25612 /* All the autos were built with index 0; fix that up now. */
25613 tree *p = hash.find_slot (t, INSERT);
25614 unsigned idx;
25615 if (*p)
25616 /* If this is a repeated constrained-type-specifier, use the index we
25617 chose before. */
25618 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25619 else
25620 {
25621 /* Otherwise this is new, so use the current count. */
25622 *p = t;
25623 idx = hash.elements () - 1;
25624 }
25625 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25626 }
25627
25628 /* Always keep walking. */
25629 return 0;
25630 }
25631
25632 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25633 says they can appear anywhere in the type. */
25634
25635 static tree
25636 extract_autos (tree type)
25637 {
25638 hash_set<tree> visited;
25639 hash_table<auto_hash> hash (2);
25640
25641 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25642
25643 tree tree_vec = make_tree_vec (hash.elements());
25644 for (hash_table<auto_hash>::iterator iter = hash.begin();
25645 iter != hash.end(); ++iter)
25646 {
25647 tree elt = *iter;
25648 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25649 TREE_VEC_ELT (tree_vec, i)
25650 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25651 }
25652
25653 return tree_vec;
25654 }
25655
25656 /* The stem for deduction guide names. */
25657 const char *const dguide_base = "__dguide_";
25658
25659 /* Return the name for a deduction guide for class template TMPL. */
25660
25661 tree
25662 dguide_name (tree tmpl)
25663 {
25664 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25665 tree tname = TYPE_IDENTIFIER (type);
25666 char *buf = (char *) alloca (1 + strlen (dguide_base)
25667 + IDENTIFIER_LENGTH (tname));
25668 memcpy (buf, dguide_base, strlen (dguide_base));
25669 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25670 IDENTIFIER_LENGTH (tname) + 1);
25671 tree dname = get_identifier (buf);
25672 TREE_TYPE (dname) = type;
25673 return dname;
25674 }
25675
25676 /* True if NAME is the name of a deduction guide. */
25677
25678 bool
25679 dguide_name_p (tree name)
25680 {
25681 return (TREE_CODE (name) == IDENTIFIER_NODE
25682 && TREE_TYPE (name)
25683 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25684 strlen (dguide_base)));
25685 }
25686
25687 /* True if FN is a deduction guide. */
25688
25689 bool
25690 deduction_guide_p (const_tree fn)
25691 {
25692 if (DECL_P (fn))
25693 if (tree name = DECL_NAME (fn))
25694 return dguide_name_p (name);
25695 return false;
25696 }
25697
25698 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25699
25700 bool
25701 copy_guide_p (const_tree fn)
25702 {
25703 gcc_assert (deduction_guide_p (fn));
25704 if (!DECL_ARTIFICIAL (fn))
25705 return false;
25706 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25707 return (TREE_CHAIN (parms) == void_list_node
25708 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25709 }
25710
25711 /* True if FN is a guide generated from a constructor template. */
25712
25713 bool
25714 template_guide_p (const_tree fn)
25715 {
25716 gcc_assert (deduction_guide_p (fn));
25717 if (!DECL_ARTIFICIAL (fn))
25718 return false;
25719 tree tmpl = DECL_TI_TEMPLATE (fn);
25720 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25721 return PRIMARY_TEMPLATE_P (org);
25722 return false;
25723 }
25724
25725 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25726 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25727 template parameter types. Note that the handling of template template
25728 parameters relies on current_template_parms being set appropriately for the
25729 new template. */
25730
25731 static tree
25732 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25733 tree tsubst_args, tsubst_flags_t complain)
25734 {
25735 tree oldidx = get_template_parm_index (olddecl);
25736
25737 tree newtype;
25738 if (TREE_CODE (olddecl) == TYPE_DECL
25739 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25740 {
25741 tree oldtype = TREE_TYPE (olddecl);
25742 newtype = cxx_make_type (TREE_CODE (oldtype));
25743 TYPE_MAIN_VARIANT (newtype) = newtype;
25744 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25745 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25746 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25747 }
25748 else
25749 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25750 complain, NULL_TREE);
25751
25752 tree newdecl
25753 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25754 DECL_NAME (olddecl), newtype);
25755 SET_DECL_TEMPLATE_PARM_P (newdecl);
25756
25757 tree newidx;
25758 if (TREE_CODE (olddecl) == TYPE_DECL
25759 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25760 {
25761 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25762 = build_template_parm_index (index, level, level,
25763 newdecl, newtype);
25764 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25765 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25766 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25767 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25768
25769 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25770 {
25771 DECL_TEMPLATE_RESULT (newdecl)
25772 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25773 DECL_NAME (olddecl), newtype);
25774 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25775 // First create a copy (ttargs) of tsubst_args with an
25776 // additional level for the template template parameter's own
25777 // template parameters (ttparms).
25778 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25779 (DECL_TEMPLATE_PARMS (olddecl)));
25780 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25781 tree ttargs = make_tree_vec (depth + 1);
25782 for (int i = 0; i < depth; ++i)
25783 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25784 TREE_VEC_ELT (ttargs, depth)
25785 = template_parms_level_to_args (ttparms);
25786 // Substitute ttargs into ttparms to fix references to
25787 // other template parameters.
25788 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25789 complain|tf_partial);
25790 // Now substitute again with args based on tparms, to reduce
25791 // the level of the ttparms.
25792 ttargs = current_template_args ();
25793 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25794 complain);
25795 // Finally, tack the adjusted parms onto tparms.
25796 ttparms = tree_cons (size_int (depth), ttparms,
25797 current_template_parms);
25798 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25799 }
25800 }
25801 else
25802 {
25803 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25804 tree newconst
25805 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25806 TREE_CODE (oldconst),
25807 DECL_NAME (oldconst), newtype);
25808 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25809 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25810 SET_DECL_TEMPLATE_PARM_P (newconst);
25811 newidx = build_template_parm_index (index, level, level,
25812 newconst, newtype);
25813 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25814 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25815 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25816 }
25817
25818 return newdecl;
25819 }
25820
25821 /* Returns a C++17 class deduction guide template based on the constructor
25822 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25823 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25824
25825 static tree
25826 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25827 {
25828 tree type, tparms, targs, fparms, fargs, ci;
25829 bool memtmpl = false;
25830 bool explicit_p;
25831 location_t loc;
25832 tree fn_tmpl = NULL_TREE;
25833
25834 if (TYPE_P (ctor))
25835 {
25836 type = ctor;
25837 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25838 if (copy_p)
25839 {
25840 type = TREE_TYPE (type);
25841 fparms = tree_cons (NULL_TREE, type, void_list_node);
25842 }
25843 else
25844 fparms = void_list_node;
25845
25846 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25847 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25848 targs = CLASSTYPE_TI_ARGS (type);
25849 ci = NULL_TREE;
25850 fargs = NULL_TREE;
25851 loc = DECL_SOURCE_LOCATION (ctmpl);
25852 explicit_p = false;
25853 }
25854 else
25855 {
25856 ++processing_template_decl;
25857
25858 fn_tmpl
25859 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25860 : DECL_TI_TEMPLATE (ctor));
25861 if (outer_args)
25862 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25863 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25864
25865 type = DECL_CONTEXT (ctor);
25866
25867 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25868 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25869 fully specialized args for the enclosing class. Strip those off, as
25870 the deduction guide won't have those template parameters. */
25871 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25872 TMPL_PARMS_DEPTH (tparms));
25873 /* Discard the 'this' parameter. */
25874 fparms = FUNCTION_ARG_CHAIN (ctor);
25875 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25876 ci = get_constraints (ctor);
25877 loc = DECL_SOURCE_LOCATION (ctor);
25878 explicit_p = DECL_NONCONVERTING_P (ctor);
25879
25880 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25881 {
25882 memtmpl = true;
25883
25884 /* For a member template constructor, we need to flatten the two
25885 template parameter lists into one, and then adjust the function
25886 signature accordingly. This gets...complicated. */
25887 tree save_parms = current_template_parms;
25888
25889 /* For a member template we should have two levels of parms/args, one
25890 for the class and one for the constructor. We stripped
25891 specialized args for further enclosing classes above. */
25892 const int depth = 2;
25893 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25894
25895 /* Template args for translating references to the two-level template
25896 parameters into references to the one-level template parameters we
25897 are creating. */
25898 tree tsubst_args = copy_node (targs);
25899 TMPL_ARGS_LEVEL (tsubst_args, depth)
25900 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25901
25902 /* Template parms for the constructor template. */
25903 tree ftparms = TREE_VALUE (tparms);
25904 unsigned flen = TREE_VEC_LENGTH (ftparms);
25905 /* Template parms for the class template. */
25906 tparms = TREE_CHAIN (tparms);
25907 tree ctparms = TREE_VALUE (tparms);
25908 unsigned clen = TREE_VEC_LENGTH (ctparms);
25909 /* Template parms for the deduction guide start as a copy of the
25910 template parms for the class. We set current_template_parms for
25911 lookup_template_class_1. */
25912 current_template_parms = tparms = copy_node (tparms);
25913 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25914 for (unsigned i = 0; i < clen; ++i)
25915 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25916
25917 /* Now we need to rewrite the constructor parms to append them to the
25918 class parms. */
25919 for (unsigned i = 0; i < flen; ++i)
25920 {
25921 unsigned index = i + clen;
25922 unsigned level = 1;
25923 tree oldelt = TREE_VEC_ELT (ftparms, i);
25924 tree olddecl = TREE_VALUE (oldelt);
25925 tree newdecl = rewrite_template_parm (olddecl, index, level,
25926 tsubst_args, complain);
25927 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25928 tsubst_args, complain, ctor);
25929 tree list = build_tree_list (newdef, newdecl);
25930 TEMPLATE_PARM_CONSTRAINTS (list)
25931 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25932 tsubst_args, complain, ctor);
25933 TREE_VEC_ELT (new_vec, index) = list;
25934 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25935 }
25936
25937 /* Now we have a final set of template parms to substitute into the
25938 function signature. */
25939 targs = template_parms_to_args (tparms);
25940 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25941 complain, ctor);
25942 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25943 if (ci)
25944 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25945
25946 current_template_parms = save_parms;
25947 }
25948 --processing_template_decl;
25949 }
25950
25951 if (!memtmpl)
25952 {
25953 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25954 tparms = copy_node (tparms);
25955 INNERMOST_TEMPLATE_PARMS (tparms)
25956 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25957 }
25958
25959 tree fntype = build_function_type (type, fparms);
25960 tree ded_fn = build_lang_decl_loc (loc,
25961 FUNCTION_DECL,
25962 dguide_name (type), fntype);
25963 DECL_ARGUMENTS (ded_fn) = fargs;
25964 DECL_ARTIFICIAL (ded_fn) = true;
25965 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25966 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25967 DECL_ARTIFICIAL (ded_tmpl) = true;
25968 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25969 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25970 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25971 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25972 if (DECL_P (ctor))
25973 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25974 if (ci)
25975 set_constraints (ded_tmpl, ci);
25976
25977 return ded_tmpl;
25978 }
25979
25980 /* Deduce template arguments for the class template placeholder PTYPE for
25981 template TMPL based on the initializer INIT, and return the resulting
25982 type. */
25983
25984 static tree
25985 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25986 tsubst_flags_t complain)
25987 {
25988 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25989 {
25990 /* We should have handled this in the caller. */
25991 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25992 return ptype;
25993 if (complain & tf_error)
25994 error ("non-class template %qT used without template arguments", tmpl);
25995 return error_mark_node;
25996 }
25997
25998 tree type = TREE_TYPE (tmpl);
25999
26000 bool try_list_ctor = false;
26001
26002 vec<tree,va_gc> *args;
26003 if (init == NULL_TREE
26004 || TREE_CODE (init) == TREE_LIST)
26005 args = make_tree_vector_from_list (init);
26006 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
26007 {
26008 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
26009 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
26010 {
26011 /* As an exception, the first phase in 16.3.1.7 (considering the
26012 initializer list as a single argument) is omitted if the
26013 initializer list consists of a single expression of type cv U,
26014 where U is a specialization of C or a class derived from a
26015 specialization of C. */
26016 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
26017 tree etype = TREE_TYPE (elt);
26018
26019 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
26020 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26021 int err = unify (tparms, targs, type, etype,
26022 UNIFY_ALLOW_DERIVED, /*explain*/false);
26023 if (err == 0)
26024 try_list_ctor = false;
26025 ggc_free (targs);
26026 }
26027 if (try_list_ctor || is_std_init_list (type))
26028 args = make_tree_vector_single (init);
26029 else
26030 args = make_tree_vector_from_ctor (init);
26031 }
26032 else
26033 args = make_tree_vector_single (init);
26034
26035 tree dname = dguide_name (tmpl);
26036 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
26037 /*type*/false, /*complain*/false,
26038 /*hidden*/false);
26039 bool elided = false;
26040 if (cands == error_mark_node)
26041 cands = NULL_TREE;
26042
26043 /* Prune explicit deduction guides in copy-initialization context. */
26044 if (flags & LOOKUP_ONLYCONVERTING)
26045 {
26046 for (lkp_iterator iter (cands); !elided && iter; ++iter)
26047 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26048 elided = true;
26049
26050 if (elided)
26051 {
26052 /* Found a nonconverting guide, prune the candidates. */
26053 tree pruned = NULL_TREE;
26054 for (lkp_iterator iter (cands); iter; ++iter)
26055 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26056 pruned = lookup_add (*iter, pruned);
26057
26058 cands = pruned;
26059 }
26060 }
26061
26062 tree outer_args = NULL_TREE;
26063 if (DECL_CLASS_SCOPE_P (tmpl)
26064 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26065 {
26066 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26067 type = TREE_TYPE (most_general_template (tmpl));
26068 }
26069
26070 bool saw_ctor = false;
26071 // FIXME cache artificial deduction guides
26072 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26073 {
26074 tree guide = build_deduction_guide (*iter, outer_args, complain);
26075 if ((flags & LOOKUP_ONLYCONVERTING)
26076 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26077 elided = true;
26078 else
26079 cands = lookup_add (guide, cands);
26080
26081 saw_ctor = true;
26082 }
26083
26084 tree call = error_mark_node;
26085
26086 /* If this is list-initialization and the class has a list constructor, first
26087 try deducing from the list as a single argument, as [over.match.list]. */
26088 tree list_cands = NULL_TREE;
26089 if (try_list_ctor && cands)
26090 for (lkp_iterator iter (cands); iter; ++iter)
26091 {
26092 tree dg = *iter;
26093 if (is_list_ctor (dg))
26094 list_cands = lookup_add (dg, list_cands);
26095 }
26096 if (list_cands)
26097 {
26098 ++cp_unevaluated_operand;
26099 call = build_new_function_call (list_cands, &args, tf_decltype);
26100 --cp_unevaluated_operand;
26101
26102 if (call == error_mark_node)
26103 {
26104 /* That didn't work, now try treating the list as a sequence of
26105 arguments. */
26106 release_tree_vector (args);
26107 args = make_tree_vector_from_ctor (init);
26108 }
26109 }
26110
26111 /* Maybe generate an implicit deduction guide. */
26112 if (call == error_mark_node && args->length () < 2)
26113 {
26114 tree gtype = NULL_TREE;
26115
26116 if (args->length () == 1)
26117 /* Generate a copy guide. */
26118 gtype = build_reference_type (type);
26119 else if (!saw_ctor)
26120 /* Generate a default guide. */
26121 gtype = type;
26122
26123 if (gtype)
26124 {
26125 tree guide = build_deduction_guide (gtype, outer_args, complain);
26126 cands = lookup_add (guide, cands);
26127 }
26128 }
26129
26130 if (elided && !cands)
26131 {
26132 error ("cannot deduce template arguments for copy-initialization"
26133 " of %qT, as it has no non-explicit deduction guides or "
26134 "user-declared constructors", type);
26135 return error_mark_node;
26136 }
26137 else if (!cands && call == error_mark_node)
26138 {
26139 error ("cannot deduce template arguments of %qT, as it has no viable "
26140 "deduction guides", type);
26141 return error_mark_node;
26142 }
26143
26144 if (call == error_mark_node)
26145 {
26146 ++cp_unevaluated_operand;
26147 call = build_new_function_call (cands, &args, tf_decltype);
26148 --cp_unevaluated_operand;
26149 }
26150
26151 if (call == error_mark_node && (complain & tf_warning_or_error))
26152 {
26153 error ("class template argument deduction failed:");
26154
26155 ++cp_unevaluated_operand;
26156 call = build_new_function_call (cands, &args, complain | tf_decltype);
26157 --cp_unevaluated_operand;
26158
26159 if (elided)
26160 inform (input_location, "explicit deduction guides not considered "
26161 "for copy-initialization");
26162 }
26163
26164 release_tree_vector (args);
26165
26166 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26167 }
26168
26169 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26170 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26171 The CONTEXT determines the context in which auto deduction is performed
26172 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26173 OUTER_TARGS are used during template argument deduction
26174 (context == adc_unify) to properly substitute the result, and is ignored
26175 in other contexts.
26176
26177 For partial-concept-ids, extra args may be appended to the list of deduced
26178 template arguments prior to determining constraint satisfaction. */
26179
26180 tree
26181 do_auto_deduction (tree type, tree init, tree auto_node,
26182 tsubst_flags_t complain, auto_deduction_context context,
26183 tree outer_targs, int flags)
26184 {
26185 tree targs;
26186
26187 if (init == error_mark_node)
26188 return error_mark_node;
26189
26190 if (init && type_dependent_expression_p (init)
26191 && context != adc_unify)
26192 /* Defining a subset of type-dependent expressions that we can deduce
26193 from ahead of time isn't worth the trouble. */
26194 return type;
26195
26196 /* Similarly, we can't deduce from another undeduced decl. */
26197 if (init && undeduced_auto_decl (init))
26198 return type;
26199
26200 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26201 /* C++17 class template argument deduction. */
26202 return do_class_deduction (type, tmpl, init, flags, complain);
26203
26204 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26205 /* Nothing we can do with this, even in deduction context. */
26206 return type;
26207
26208 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26209 with either a new invented type template parameter U or, if the
26210 initializer is a braced-init-list (8.5.4), with
26211 std::initializer_list<U>. */
26212 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26213 {
26214 if (!DIRECT_LIST_INIT_P (init))
26215 type = listify_autos (type, auto_node);
26216 else if (CONSTRUCTOR_NELTS (init) == 1)
26217 init = CONSTRUCTOR_ELT (init, 0)->value;
26218 else
26219 {
26220 if (complain & tf_warning_or_error)
26221 {
26222 if (permerror (input_location, "direct-list-initialization of "
26223 "%<auto%> requires exactly one element"))
26224 inform (input_location,
26225 "for deduction to %<std::initializer_list%>, use copy-"
26226 "list-initialization (i.e. add %<=%> before the %<{%>)");
26227 }
26228 type = listify_autos (type, auto_node);
26229 }
26230 }
26231
26232 if (type == error_mark_node)
26233 return error_mark_node;
26234
26235 init = resolve_nondeduced_context (init, complain);
26236
26237 if (context == adc_decomp_type
26238 && auto_node == type
26239 && init != error_mark_node
26240 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26241 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26242 and initializer has array type, deduce cv-qualified array type. */
26243 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26244 complain);
26245 else if (AUTO_IS_DECLTYPE (auto_node))
26246 {
26247 bool id = (DECL_P (init)
26248 || ((TREE_CODE (init) == COMPONENT_REF
26249 || TREE_CODE (init) == SCOPE_REF)
26250 && !REF_PARENTHESIZED_P (init)));
26251 targs = make_tree_vec (1);
26252 TREE_VEC_ELT (targs, 0)
26253 = finish_decltype_type (init, id, tf_warning_or_error);
26254 if (type != auto_node)
26255 {
26256 if (complain & tf_error)
26257 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26258 return error_mark_node;
26259 }
26260 }
26261 else
26262 {
26263 tree parms = build_tree_list (NULL_TREE, type);
26264 tree tparms;
26265
26266 if (flag_concepts)
26267 tparms = extract_autos (type);
26268 else
26269 {
26270 tparms = make_tree_vec (1);
26271 TREE_VEC_ELT (tparms, 0)
26272 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26273 }
26274
26275 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26276 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26277 DEDUCE_CALL, LOOKUP_NORMAL,
26278 NULL, /*explain_p=*/false);
26279 if (val > 0)
26280 {
26281 if (processing_template_decl)
26282 /* Try again at instantiation time. */
26283 return type;
26284 if (type && type != error_mark_node
26285 && (complain & tf_error))
26286 /* If type is error_mark_node a diagnostic must have been
26287 emitted by now. Also, having a mention to '<type error>'
26288 in the diagnostic is not really useful to the user. */
26289 {
26290 if (cfun && auto_node == current_function_auto_return_pattern
26291 && LAMBDA_FUNCTION_P (current_function_decl))
26292 error ("unable to deduce lambda return type from %qE", init);
26293 else
26294 error ("unable to deduce %qT from %qE", type, init);
26295 type_unification_real (tparms, targs, parms, &init, 1, 0,
26296 DEDUCE_CALL, LOOKUP_NORMAL,
26297 NULL, /*explain_p=*/true);
26298 }
26299 return error_mark_node;
26300 }
26301 }
26302
26303 /* Check any placeholder constraints against the deduced type. */
26304 if (flag_concepts && !processing_template_decl)
26305 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26306 {
26307 /* Use the deduced type to check the associated constraints. If we
26308 have a partial-concept-id, rebuild the argument list so that
26309 we check using the extra arguments. */
26310 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26311 tree cargs = CHECK_CONSTR_ARGS (constr);
26312 if (TREE_VEC_LENGTH (cargs) > 1)
26313 {
26314 cargs = copy_node (cargs);
26315 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26316 }
26317 else
26318 cargs = targs;
26319 if (!constraints_satisfied_p (constr, cargs))
26320 {
26321 if (complain & tf_warning_or_error)
26322 {
26323 switch (context)
26324 {
26325 case adc_unspecified:
26326 case adc_unify:
26327 error("placeholder constraints not satisfied");
26328 break;
26329 case adc_variable_type:
26330 case adc_decomp_type:
26331 error ("deduced initializer does not satisfy "
26332 "placeholder constraints");
26333 break;
26334 case adc_return_type:
26335 error ("deduced return type does not satisfy "
26336 "placeholder constraints");
26337 break;
26338 case adc_requirement:
26339 error ("deduced expression type does not satisfy "
26340 "placeholder constraints");
26341 break;
26342 }
26343 diagnose_constraints (input_location, constr, targs);
26344 }
26345 return error_mark_node;
26346 }
26347 }
26348
26349 if (processing_template_decl && context != adc_unify)
26350 outer_targs = current_template_args ();
26351 targs = add_to_template_args (outer_targs, targs);
26352 return tsubst (type, targs, complain, NULL_TREE);
26353 }
26354
26355 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26356 result. */
26357
26358 tree
26359 splice_late_return_type (tree type, tree late_return_type)
26360 {
26361 if (is_auto (type))
26362 {
26363 if (late_return_type)
26364 return late_return_type;
26365
26366 tree idx = get_template_parm_index (type);
26367 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26368 /* In an abbreviated function template we didn't know we were dealing
26369 with a function template when we saw the auto return type, so update
26370 it to have the correct level. */
26371 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26372 }
26373 return type;
26374 }
26375
26376 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26377 'decltype(auto)' or a deduced class template. */
26378
26379 bool
26380 is_auto (const_tree type)
26381 {
26382 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26383 && (TYPE_IDENTIFIER (type) == auto_identifier
26384 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26385 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26386 return true;
26387 else
26388 return false;
26389 }
26390
26391 /* for_each_template_parm callback for type_uses_auto. */
26392
26393 int
26394 is_auto_r (tree tp, void */*data*/)
26395 {
26396 return is_auto (tp);
26397 }
26398
26399 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26400 a use of `auto'. Returns NULL_TREE otherwise. */
26401
26402 tree
26403 type_uses_auto (tree type)
26404 {
26405 if (type == NULL_TREE)
26406 return NULL_TREE;
26407 else if (flag_concepts)
26408 {
26409 /* The Concepts TS allows multiple autos in one type-specifier; just
26410 return the first one we find, do_auto_deduction will collect all of
26411 them. */
26412 if (uses_template_parms (type))
26413 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26414 /*visited*/NULL, /*nondeduced*/true);
26415 else
26416 return NULL_TREE;
26417 }
26418 else
26419 return find_type_usage (type, is_auto);
26420 }
26421
26422 /* For a given template T, return the vector of typedefs referenced
26423 in T for which access check is needed at T instantiation time.
26424 T is either a FUNCTION_DECL or a RECORD_TYPE.
26425 Those typedefs were added to T by the function
26426 append_type_to_template_for_access_check. */
26427
26428 vec<qualified_typedef_usage_t, va_gc> *
26429 get_types_needing_access_check (tree t)
26430 {
26431 tree ti;
26432 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26433
26434 if (!t || t == error_mark_node)
26435 return NULL;
26436
26437 if (!(ti = get_template_info (t)))
26438 return NULL;
26439
26440 if (CLASS_TYPE_P (t)
26441 || TREE_CODE (t) == FUNCTION_DECL)
26442 {
26443 if (!TI_TEMPLATE (ti))
26444 return NULL;
26445
26446 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26447 }
26448
26449 return result;
26450 }
26451
26452 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26453 tied to T. That list of typedefs will be access checked at
26454 T instantiation time.
26455 T is either a FUNCTION_DECL or a RECORD_TYPE.
26456 TYPE_DECL is a TYPE_DECL node representing a typedef.
26457 SCOPE is the scope through which TYPE_DECL is accessed.
26458 LOCATION is the location of the usage point of TYPE_DECL.
26459
26460 This function is a subroutine of
26461 append_type_to_template_for_access_check. */
26462
26463 static void
26464 append_type_to_template_for_access_check_1 (tree t,
26465 tree type_decl,
26466 tree scope,
26467 location_t location)
26468 {
26469 qualified_typedef_usage_t typedef_usage;
26470 tree ti;
26471
26472 if (!t || t == error_mark_node)
26473 return;
26474
26475 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26476 || CLASS_TYPE_P (t))
26477 && type_decl
26478 && TREE_CODE (type_decl) == TYPE_DECL
26479 && scope);
26480
26481 if (!(ti = get_template_info (t)))
26482 return;
26483
26484 gcc_assert (TI_TEMPLATE (ti));
26485
26486 typedef_usage.typedef_decl = type_decl;
26487 typedef_usage.context = scope;
26488 typedef_usage.locus = location;
26489
26490 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26491 }
26492
26493 /* Append TYPE_DECL to the template TEMPL.
26494 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26495 At TEMPL instanciation time, TYPE_DECL will be checked to see
26496 if it can be accessed through SCOPE.
26497 LOCATION is the location of the usage point of TYPE_DECL.
26498
26499 e.g. consider the following code snippet:
26500
26501 class C
26502 {
26503 typedef int myint;
26504 };
26505
26506 template<class U> struct S
26507 {
26508 C::myint mi; // <-- usage point of the typedef C::myint
26509 };
26510
26511 S<char> s;
26512
26513 At S<char> instantiation time, we need to check the access of C::myint
26514 In other words, we need to check the access of the myint typedef through
26515 the C scope. For that purpose, this function will add the myint typedef
26516 and the scope C through which its being accessed to a list of typedefs
26517 tied to the template S. That list will be walked at template instantiation
26518 time and access check performed on each typedefs it contains.
26519 Note that this particular code snippet should yield an error because
26520 myint is private to C. */
26521
26522 void
26523 append_type_to_template_for_access_check (tree templ,
26524 tree type_decl,
26525 tree scope,
26526 location_t location)
26527 {
26528 qualified_typedef_usage_t *iter;
26529 unsigned i;
26530
26531 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26532
26533 /* Make sure we don't append the type to the template twice. */
26534 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26535 if (iter->typedef_decl == type_decl && scope == iter->context)
26536 return;
26537
26538 append_type_to_template_for_access_check_1 (templ, type_decl,
26539 scope, location);
26540 }
26541
26542 /* Convert the generic type parameters in PARM that match the types given in the
26543 range [START_IDX, END_IDX) from the current_template_parms into generic type
26544 packs. */
26545
26546 tree
26547 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26548 {
26549 tree current = current_template_parms;
26550 int depth = TMPL_PARMS_DEPTH (current);
26551 current = INNERMOST_TEMPLATE_PARMS (current);
26552 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26553
26554 for (int i = 0; i < start_idx; ++i)
26555 TREE_VEC_ELT (replacement, i)
26556 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26557
26558 for (int i = start_idx; i < end_idx; ++i)
26559 {
26560 /* Create a distinct parameter pack type from the current parm and add it
26561 to the replacement args to tsubst below into the generic function
26562 parameter. */
26563
26564 tree o = TREE_TYPE (TREE_VALUE
26565 (TREE_VEC_ELT (current, i)));
26566 tree t = copy_type (o);
26567 TEMPLATE_TYPE_PARM_INDEX (t)
26568 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26569 o, 0, 0, tf_none);
26570 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26571 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26572 TYPE_MAIN_VARIANT (t) = t;
26573 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26574 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26575 TREE_VEC_ELT (replacement, i) = t;
26576 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26577 }
26578
26579 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26580 TREE_VEC_ELT (replacement, i)
26581 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26582
26583 /* If there are more levels then build up the replacement with the outer
26584 template parms. */
26585 if (depth > 1)
26586 replacement = add_to_template_args (template_parms_to_args
26587 (TREE_CHAIN (current_template_parms)),
26588 replacement);
26589
26590 return tsubst (parm, replacement, tf_none, NULL_TREE);
26591 }
26592
26593 /* Entries in the decl_constraint hash table. */
26594 struct GTY((for_user)) constr_entry
26595 {
26596 tree decl;
26597 tree ci;
26598 };
26599
26600 /* Hashing function and equality for constraint entries. */
26601 struct constr_hasher : ggc_ptr_hash<constr_entry>
26602 {
26603 static hashval_t hash (constr_entry *e)
26604 {
26605 return (hashval_t)DECL_UID (e->decl);
26606 }
26607
26608 static bool equal (constr_entry *e1, constr_entry *e2)
26609 {
26610 return e1->decl == e2->decl;
26611 }
26612 };
26613
26614 /* A mapping from declarations to constraint information. Note that
26615 both templates and their underlying declarations are mapped to the
26616 same constraint information.
26617
26618 FIXME: This is defined in pt.c because garbage collection
26619 code is not being generated for constraint.cc. */
26620
26621 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26622
26623 /* Returns the template constraints of declaration T. If T is not
26624 constrained, return NULL_TREE. Note that T must be non-null. */
26625
26626 tree
26627 get_constraints (tree t)
26628 {
26629 if (!flag_concepts)
26630 return NULL_TREE;
26631
26632 gcc_assert (DECL_P (t));
26633 if (TREE_CODE (t) == TEMPLATE_DECL)
26634 t = DECL_TEMPLATE_RESULT (t);
26635 constr_entry elt = { t, NULL_TREE };
26636 constr_entry* found = decl_constraints->find (&elt);
26637 if (found)
26638 return found->ci;
26639 else
26640 return NULL_TREE;
26641 }
26642
26643 /* Associate the given constraint information CI with the declaration
26644 T. If T is a template, then the constraints are associated with
26645 its underlying declaration. Don't build associations if CI is
26646 NULL_TREE. */
26647
26648 void
26649 set_constraints (tree t, tree ci)
26650 {
26651 if (!ci)
26652 return;
26653 gcc_assert (t && flag_concepts);
26654 if (TREE_CODE (t) == TEMPLATE_DECL)
26655 t = DECL_TEMPLATE_RESULT (t);
26656 gcc_assert (!get_constraints (t));
26657 constr_entry elt = {t, ci};
26658 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26659 constr_entry* entry = ggc_alloc<constr_entry> ();
26660 *entry = elt;
26661 *slot = entry;
26662 }
26663
26664 /* Remove the associated constraints of the declaration T. */
26665
26666 void
26667 remove_constraints (tree t)
26668 {
26669 gcc_assert (DECL_P (t));
26670 if (TREE_CODE (t) == TEMPLATE_DECL)
26671 t = DECL_TEMPLATE_RESULT (t);
26672
26673 constr_entry elt = {t, NULL_TREE};
26674 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26675 if (slot)
26676 decl_constraints->clear_slot (slot);
26677 }
26678
26679 /* Memoized satisfaction results for declarations. This
26680 maps the pair (constraint_info, arguments) to the result computed
26681 by constraints_satisfied_p. */
26682
26683 struct GTY((for_user)) constraint_sat_entry
26684 {
26685 tree ci;
26686 tree args;
26687 tree result;
26688 };
26689
26690 /* Hashing function and equality for constraint entries. */
26691
26692 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26693 {
26694 static hashval_t hash (constraint_sat_entry *e)
26695 {
26696 hashval_t val = iterative_hash_object(e->ci, 0);
26697 return iterative_hash_template_arg (e->args, val);
26698 }
26699
26700 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26701 {
26702 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26703 }
26704 };
26705
26706 /* Memoized satisfaction results for concept checks. */
26707
26708 struct GTY((for_user)) concept_spec_entry
26709 {
26710 tree tmpl;
26711 tree args;
26712 tree result;
26713 };
26714
26715 /* Hashing function and equality for constraint entries. */
26716
26717 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26718 {
26719 static hashval_t hash (concept_spec_entry *e)
26720 {
26721 return hash_tmpl_and_args (e->tmpl, e->args);
26722 }
26723
26724 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26725 {
26726 ++comparing_specializations;
26727 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26728 --comparing_specializations;
26729 return eq;
26730 }
26731 };
26732
26733 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26734 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26735
26736 /* Search for a memoized satisfaction result. Returns one of the
26737 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26738
26739 tree
26740 lookup_constraint_satisfaction (tree ci, tree args)
26741 {
26742 constraint_sat_entry elt = { ci, args, NULL_TREE };
26743 constraint_sat_entry* found = constraint_memos->find (&elt);
26744 if (found)
26745 return found->result;
26746 else
26747 return NULL_TREE;
26748 }
26749
26750 /* Memoize the result of a satisfication test. Returns the saved result. */
26751
26752 tree
26753 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26754 {
26755 constraint_sat_entry elt = {ci, args, result};
26756 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26757 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26758 *entry = elt;
26759 *slot = entry;
26760 return result;
26761 }
26762
26763 /* Search for a memoized satisfaction result for a concept. */
26764
26765 tree
26766 lookup_concept_satisfaction (tree tmpl, tree args)
26767 {
26768 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26769 concept_spec_entry* found = concept_memos->find (&elt);
26770 if (found)
26771 return found->result;
26772 else
26773 return NULL_TREE;
26774 }
26775
26776 /* Memoize the result of a concept check. Returns the saved result. */
26777
26778 tree
26779 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26780 {
26781 concept_spec_entry elt = {tmpl, args, result};
26782 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26783 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26784 *entry = elt;
26785 *slot = entry;
26786 return result;
26787 }
26788
26789 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26790
26791 /* Returns a prior concept specialization. This returns the substituted
26792 and normalized constraints defined by the concept. */
26793
26794 tree
26795 get_concept_expansion (tree tmpl, tree args)
26796 {
26797 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26798 concept_spec_entry* found = concept_expansions->find (&elt);
26799 if (found)
26800 return found->result;
26801 else
26802 return NULL_TREE;
26803 }
26804
26805 /* Save a concept expansion for later. */
26806
26807 tree
26808 save_concept_expansion (tree tmpl, tree args, tree def)
26809 {
26810 concept_spec_entry elt = {tmpl, args, def};
26811 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26812 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26813 *entry = elt;
26814 *slot = entry;
26815 return def;
26816 }
26817
26818 static hashval_t
26819 hash_subsumption_args (tree t1, tree t2)
26820 {
26821 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26822 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26823 int val = 0;
26824 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26825 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26826 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26827 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26828 return val;
26829 }
26830
26831 /* Compare the constraints of two subsumption entries. The LEFT1 and
26832 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26833 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26834
26835 static bool
26836 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26837 {
26838 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26839 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26840 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26841 CHECK_CONSTR_ARGS (right1)))
26842 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26843 CHECK_CONSTR_ARGS (right2));
26844 return false;
26845 }
26846
26847 /* Key/value pair for learning and memoizing subsumption results. This
26848 associates a pair of check constraints (including arguments) with
26849 a boolean value indicating the result. */
26850
26851 struct GTY((for_user)) subsumption_entry
26852 {
26853 tree t1;
26854 tree t2;
26855 bool result;
26856 };
26857
26858 /* Hashing function and equality for constraint entries. */
26859
26860 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26861 {
26862 static hashval_t hash (subsumption_entry *e)
26863 {
26864 return hash_subsumption_args (e->t1, e->t2);
26865 }
26866
26867 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26868 {
26869 ++comparing_specializations;
26870 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26871 --comparing_specializations;
26872 return eq;
26873 }
26874 };
26875
26876 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26877
26878 /* Search for a previously cached subsumption result. */
26879
26880 bool*
26881 lookup_subsumption_result (tree t1, tree t2)
26882 {
26883 subsumption_entry elt = { t1, t2, false };
26884 subsumption_entry* found = subsumption_table->find (&elt);
26885 if (found)
26886 return &found->result;
26887 else
26888 return 0;
26889 }
26890
26891 /* Save a subsumption result. */
26892
26893 bool
26894 save_subsumption_result (tree t1, tree t2, bool result)
26895 {
26896 subsumption_entry elt = {t1, t2, result};
26897 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26898 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26899 *entry = elt;
26900 *slot = entry;
26901 return result;
26902 }
26903
26904 /* Set up the hash table for constraint association. */
26905
26906 void
26907 init_constraint_processing (void)
26908 {
26909 if (!flag_concepts)
26910 return;
26911
26912 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26913 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26914 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26915 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26916 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26917 }
26918
26919 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26920 0..N-1. */
26921
26922 void
26923 declare_integer_pack (void)
26924 {
26925 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26926 build_function_type_list (integer_type_node,
26927 integer_type_node,
26928 NULL_TREE),
26929 NULL_TREE, ECF_CONST);
26930 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26931 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26932 }
26933
26934 /* Set up the hash tables for template instantiations. */
26935
26936 void
26937 init_template_processing (void)
26938 {
26939 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26940 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26941
26942 if (cxx_dialect >= cxx11)
26943 declare_integer_pack ();
26944 }
26945
26946 /* Print stats about the template hash tables for -fstats. */
26947
26948 void
26949 print_template_statistics (void)
26950 {
26951 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26952 "%f collisions\n", (long) decl_specializations->size (),
26953 (long) decl_specializations->elements (),
26954 decl_specializations->collisions ());
26955 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26956 "%f collisions\n", (long) type_specializations->size (),
26957 (long) type_specializations->elements (),
26958 type_specializations->collisions ());
26959 }
26960
26961 #if CHECKING_P
26962
26963 namespace selftest {
26964
26965 /* Verify that build_non_dependent_expr () works, for various expressions,
26966 and that location wrappers don't affect the results. */
26967
26968 static void
26969 test_build_non_dependent_expr ()
26970 {
26971 location_t loc = BUILTINS_LOCATION;
26972
26973 /* Verify constants, without and with location wrappers. */
26974 tree int_cst = build_int_cst (integer_type_node, 42);
26975 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26976
26977 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26978 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26979 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26980
26981 tree string_lit = build_string (4, "foo");
26982 TREE_TYPE (string_lit) = char_array_type_node;
26983 string_lit = fix_string_type (string_lit);
26984 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26985
26986 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26987 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26988 ASSERT_EQ (wrapped_string_lit,
26989 build_non_dependent_expr (wrapped_string_lit));
26990 }
26991
26992 /* Verify that type_dependent_expression_p () works correctly, even
26993 in the presence of location wrapper nodes. */
26994
26995 static void
26996 test_type_dependent_expression_p ()
26997 {
26998 location_t loc = BUILTINS_LOCATION;
26999
27000 tree name = get_identifier ("foo");
27001
27002 /* If no templates are involved, nothing is type-dependent. */
27003 gcc_assert (!processing_template_decl);
27004 ASSERT_FALSE (type_dependent_expression_p (name));
27005
27006 ++processing_template_decl;
27007
27008 /* Within a template, an unresolved name is always type-dependent. */
27009 ASSERT_TRUE (type_dependent_expression_p (name));
27010
27011 /* Ensure it copes with NULL_TREE and errors. */
27012 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
27013 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
27014
27015 /* A USING_DECL in a template should be type-dependent, even if wrapped
27016 with a location wrapper (PR c++/83799). */
27017 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
27018 TREE_TYPE (using_decl) = integer_type_node;
27019 ASSERT_TRUE (type_dependent_expression_p (using_decl));
27020 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
27021 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
27022 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
27023
27024 --processing_template_decl;
27025 }
27026
27027 /* Run all of the selftests within this file. */
27028
27029 void
27030 cp_pt_c_tests ()
27031 {
27032 test_build_non_dependent_expr ();
27033 test_type_dependent_expression_p ();
27034 }
27035
27036 } // namespace selftest
27037
27038 #endif /* #if CHECKING_P */
27039
27040 #include "gt-cp-pt.h"