]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
PR c++/85006 - -fconcepts ICE with A<auto...> return type
[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 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4048 return false;
4049
4050 if (TREE_CODE (t) == TYPE_DECL)
4051 t = TREE_TYPE (t);
4052
4053 ppd.parameter_packs = &parameter_packs;
4054 ppd.visited = new hash_set<tree>;
4055 ppd.type_pack_expansion_p = false;
4056 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4057 delete ppd.visited;
4058
4059 if (parameter_packs)
4060 {
4061 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
4062 error_at (loc, "parameter packs not expanded with %<...%>:");
4063 while (parameter_packs)
4064 {
4065 tree pack = TREE_VALUE (parameter_packs);
4066 tree name = NULL_TREE;
4067
4068 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4069 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4070 name = TYPE_NAME (pack);
4071 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4072 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4073 else if (TREE_CODE (pack) == CALL_EXPR)
4074 name = DECL_NAME (CALL_EXPR_FN (pack));
4075 else
4076 name = DECL_NAME (pack);
4077
4078 if (name)
4079 inform (loc, " %qD", name);
4080 else
4081 inform (loc, " <anonymous>");
4082
4083 parameter_packs = TREE_CHAIN (parameter_packs);
4084 }
4085
4086 return true;
4087 }
4088
4089 return false;
4090 }
4091
4092 /* Expand any parameter packs that occur in the template arguments in
4093 ARGS. */
4094 tree
4095 expand_template_argument_pack (tree args)
4096 {
4097 if (args == error_mark_node)
4098 return error_mark_node;
4099
4100 tree result_args = NULL_TREE;
4101 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4102 int num_result_args = -1;
4103 int non_default_args_count = -1;
4104
4105 /* First, determine if we need to expand anything, and the number of
4106 slots we'll need. */
4107 for (in_arg = 0; in_arg < nargs; ++in_arg)
4108 {
4109 tree arg = TREE_VEC_ELT (args, in_arg);
4110 if (arg == NULL_TREE)
4111 return args;
4112 if (ARGUMENT_PACK_P (arg))
4113 {
4114 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4115 if (num_result_args < 0)
4116 num_result_args = in_arg + num_packed;
4117 else
4118 num_result_args += num_packed;
4119 }
4120 else
4121 {
4122 if (num_result_args >= 0)
4123 num_result_args++;
4124 }
4125 }
4126
4127 /* If no expansion is necessary, we're done. */
4128 if (num_result_args < 0)
4129 return args;
4130
4131 /* Expand arguments. */
4132 result_args = make_tree_vec (num_result_args);
4133 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4134 non_default_args_count =
4135 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4136 for (in_arg = 0; in_arg < nargs; ++in_arg)
4137 {
4138 tree arg = TREE_VEC_ELT (args, in_arg);
4139 if (ARGUMENT_PACK_P (arg))
4140 {
4141 tree packed = ARGUMENT_PACK_ARGS (arg);
4142 int i, num_packed = TREE_VEC_LENGTH (packed);
4143 for (i = 0; i < num_packed; ++i, ++out_arg)
4144 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4145 if (non_default_args_count > 0)
4146 non_default_args_count += num_packed - 1;
4147 }
4148 else
4149 {
4150 TREE_VEC_ELT (result_args, out_arg) = arg;
4151 ++out_arg;
4152 }
4153 }
4154 if (non_default_args_count >= 0)
4155 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4156 return result_args;
4157 }
4158
4159 /* Checks if DECL shadows a template parameter.
4160
4161 [temp.local]: A template-parameter shall not be redeclared within its
4162 scope (including nested scopes).
4163
4164 Emits an error and returns TRUE if the DECL shadows a parameter,
4165 returns FALSE otherwise. */
4166
4167 bool
4168 check_template_shadow (tree decl)
4169 {
4170 tree olddecl;
4171
4172 /* If we're not in a template, we can't possibly shadow a template
4173 parameter. */
4174 if (!current_template_parms)
4175 return true;
4176
4177 /* Figure out what we're shadowing. */
4178 decl = OVL_FIRST (decl);
4179 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4180
4181 /* If there's no previous binding for this name, we're not shadowing
4182 anything, let alone a template parameter. */
4183 if (!olddecl)
4184 return true;
4185
4186 /* If we're not shadowing a template parameter, we're done. Note
4187 that OLDDECL might be an OVERLOAD (or perhaps even an
4188 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4189 node. */
4190 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4191 return true;
4192
4193 /* We check for decl != olddecl to avoid bogus errors for using a
4194 name inside a class. We check TPFI to avoid duplicate errors for
4195 inline member templates. */
4196 if (decl == olddecl
4197 || (DECL_TEMPLATE_PARM_P (decl)
4198 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4199 return true;
4200
4201 /* Don't complain about the injected class name, as we've already
4202 complained about the class itself. */
4203 if (DECL_SELF_REFERENCE_P (decl))
4204 return false;
4205
4206 if (DECL_TEMPLATE_PARM_P (decl))
4207 error ("declaration of template parameter %q+D shadows "
4208 "template parameter", decl);
4209 else
4210 error ("declaration of %q+#D shadows template parameter", decl);
4211 inform (DECL_SOURCE_LOCATION (olddecl),
4212 "template parameter %qD declared here", olddecl);
4213 return false;
4214 }
4215
4216 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4217 ORIG_LEVEL, DECL, and TYPE. */
4218
4219 static tree
4220 build_template_parm_index (int index,
4221 int level,
4222 int orig_level,
4223 tree decl,
4224 tree type)
4225 {
4226 tree t = make_node (TEMPLATE_PARM_INDEX);
4227 TEMPLATE_PARM_IDX (t) = index;
4228 TEMPLATE_PARM_LEVEL (t) = level;
4229 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4230 TEMPLATE_PARM_DECL (t) = decl;
4231 TREE_TYPE (t) = type;
4232 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4233 TREE_READONLY (t) = TREE_READONLY (decl);
4234
4235 return t;
4236 }
4237
4238 /* Find the canonical type parameter for the given template type
4239 parameter. Returns the canonical type parameter, which may be TYPE
4240 if no such parameter existed. */
4241
4242 static tree
4243 canonical_type_parameter (tree type)
4244 {
4245 tree list;
4246 int idx = TEMPLATE_TYPE_IDX (type);
4247 if (!canonical_template_parms)
4248 vec_alloc (canonical_template_parms, idx + 1);
4249
4250 if (canonical_template_parms->length () <= (unsigned) idx)
4251 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4252
4253 list = (*canonical_template_parms)[idx];
4254 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4255 list = TREE_CHAIN (list);
4256
4257 if (list)
4258 return TREE_VALUE (list);
4259 else
4260 {
4261 (*canonical_template_parms)[idx]
4262 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4263 return type;
4264 }
4265 }
4266
4267 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4268 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4269 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4270 new one is created. */
4271
4272 static tree
4273 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4274 tsubst_flags_t complain)
4275 {
4276 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4277 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4278 != TEMPLATE_PARM_LEVEL (index) - levels)
4279 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4280 {
4281 tree orig_decl = TEMPLATE_PARM_DECL (index);
4282 tree decl, t;
4283
4284 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4285 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4286 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4287 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4288 DECL_ARTIFICIAL (decl) = 1;
4289 SET_DECL_TEMPLATE_PARM_P (decl);
4290
4291 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4292 TEMPLATE_PARM_LEVEL (index) - levels,
4293 TEMPLATE_PARM_ORIG_LEVEL (index),
4294 decl, type);
4295 TEMPLATE_PARM_DESCENDANTS (index) = t;
4296 TEMPLATE_PARM_PARAMETER_PACK (t)
4297 = TEMPLATE_PARM_PARAMETER_PACK (index);
4298
4299 /* Template template parameters need this. */
4300 if (TREE_CODE (decl) == TEMPLATE_DECL)
4301 {
4302 DECL_TEMPLATE_RESULT (decl)
4303 = build_decl (DECL_SOURCE_LOCATION (decl),
4304 TYPE_DECL, DECL_NAME (decl), type);
4305 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4306 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4307 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4308 }
4309 }
4310
4311 return TEMPLATE_PARM_DESCENDANTS (index);
4312 }
4313
4314 /* Process information from new template parameter PARM and append it
4315 to the LIST being built. This new parameter is a non-type
4316 parameter iff IS_NON_TYPE is true. This new parameter is a
4317 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4318 is in PARM_LOC. */
4319
4320 tree
4321 process_template_parm (tree list, location_t parm_loc, tree parm,
4322 bool is_non_type, bool is_parameter_pack)
4323 {
4324 tree decl = 0;
4325 int idx = 0;
4326
4327 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4328 tree defval = TREE_PURPOSE (parm);
4329 tree constr = TREE_TYPE (parm);
4330
4331 if (list)
4332 {
4333 tree p = tree_last (list);
4334
4335 if (p && TREE_VALUE (p) != error_mark_node)
4336 {
4337 p = TREE_VALUE (p);
4338 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4339 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4340 else
4341 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4342 }
4343
4344 ++idx;
4345 }
4346
4347 if (is_non_type)
4348 {
4349 parm = TREE_VALUE (parm);
4350
4351 SET_DECL_TEMPLATE_PARM_P (parm);
4352
4353 if (TREE_TYPE (parm) != error_mark_node)
4354 {
4355 /* [temp.param]
4356
4357 The top-level cv-qualifiers on the template-parameter are
4358 ignored when determining its type. */
4359 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4360 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4361 TREE_TYPE (parm) = error_mark_node;
4362 else if (uses_parameter_packs (TREE_TYPE (parm))
4363 && !is_parameter_pack
4364 /* If we're in a nested template parameter list, the template
4365 template parameter could be a parameter pack. */
4366 && processing_template_parmlist == 1)
4367 {
4368 /* This template parameter is not a parameter pack, but it
4369 should be. Complain about "bare" parameter packs. */
4370 check_for_bare_parameter_packs (TREE_TYPE (parm));
4371
4372 /* Recover by calling this a parameter pack. */
4373 is_parameter_pack = true;
4374 }
4375 }
4376
4377 /* A template parameter is not modifiable. */
4378 TREE_CONSTANT (parm) = 1;
4379 TREE_READONLY (parm) = 1;
4380 decl = build_decl (parm_loc,
4381 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4382 TREE_CONSTANT (decl) = 1;
4383 TREE_READONLY (decl) = 1;
4384 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4385 = build_template_parm_index (idx, processing_template_decl,
4386 processing_template_decl,
4387 decl, TREE_TYPE (parm));
4388
4389 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4390 = is_parameter_pack;
4391 }
4392 else
4393 {
4394 tree t;
4395 parm = TREE_VALUE (TREE_VALUE (parm));
4396
4397 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4398 {
4399 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4400 /* This is for distinguishing between real templates and template
4401 template parameters */
4402 TREE_TYPE (parm) = t;
4403 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4404 decl = parm;
4405 }
4406 else
4407 {
4408 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4409 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4410 decl = build_decl (parm_loc,
4411 TYPE_DECL, parm, t);
4412 }
4413
4414 TYPE_NAME (t) = decl;
4415 TYPE_STUB_DECL (t) = decl;
4416 parm = decl;
4417 TEMPLATE_TYPE_PARM_INDEX (t)
4418 = build_template_parm_index (idx, processing_template_decl,
4419 processing_template_decl,
4420 decl, TREE_TYPE (parm));
4421 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4422 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4423 }
4424 DECL_ARTIFICIAL (decl) = 1;
4425 SET_DECL_TEMPLATE_PARM_P (decl);
4426
4427 /* Build requirements for the type/template parameter.
4428 This must be done after SET_DECL_TEMPLATE_PARM_P or
4429 process_template_parm could fail. */
4430 tree reqs = finish_shorthand_constraint (parm, constr);
4431
4432 pushdecl (decl);
4433
4434 if (defval && TREE_CODE (defval) == OVERLOAD)
4435 lookup_keep (defval, true);
4436
4437 /* Build the parameter node linking the parameter declaration,
4438 its default argument (if any), and its constraints (if any). */
4439 parm = build_tree_list (defval, parm);
4440 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4441
4442 return chainon (list, parm);
4443 }
4444
4445 /* The end of a template parameter list has been reached. Process the
4446 tree list into a parameter vector, converting each parameter into a more
4447 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4448 as PARM_DECLs. */
4449
4450 tree
4451 end_template_parm_list (tree parms)
4452 {
4453 int nparms;
4454 tree parm, next;
4455 tree saved_parmlist = make_tree_vec (list_length (parms));
4456
4457 /* Pop the dummy parameter level and add the real one. */
4458 current_template_parms = TREE_CHAIN (current_template_parms);
4459
4460 current_template_parms
4461 = tree_cons (size_int (processing_template_decl),
4462 saved_parmlist, current_template_parms);
4463
4464 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4465 {
4466 next = TREE_CHAIN (parm);
4467 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4468 TREE_CHAIN (parm) = NULL_TREE;
4469 }
4470
4471 --processing_template_parmlist;
4472
4473 return saved_parmlist;
4474 }
4475
4476 // Explicitly indicate the end of the template parameter list. We assume
4477 // that the current template parameters have been constructed and/or
4478 // managed explicitly, as when creating new template template parameters
4479 // from a shorthand constraint.
4480 void
4481 end_template_parm_list ()
4482 {
4483 --processing_template_parmlist;
4484 }
4485
4486 /* end_template_decl is called after a template declaration is seen. */
4487
4488 void
4489 end_template_decl (void)
4490 {
4491 reset_specialization ();
4492
4493 if (! processing_template_decl)
4494 return;
4495
4496 /* This matches the pushlevel in begin_template_parm_list. */
4497 finish_scope ();
4498
4499 --processing_template_decl;
4500 current_template_parms = TREE_CHAIN (current_template_parms);
4501 }
4502
4503 /* Takes a TREE_LIST representing a template parameter and convert it
4504 into an argument suitable to be passed to the type substitution
4505 functions. Note that If the TREE_LIST contains an error_mark
4506 node, the returned argument is error_mark_node. */
4507
4508 tree
4509 template_parm_to_arg (tree t)
4510 {
4511
4512 if (t == NULL_TREE
4513 || TREE_CODE (t) != TREE_LIST)
4514 return t;
4515
4516 if (error_operand_p (TREE_VALUE (t)))
4517 return error_mark_node;
4518
4519 t = TREE_VALUE (t);
4520
4521 if (TREE_CODE (t) == TYPE_DECL
4522 || TREE_CODE (t) == TEMPLATE_DECL)
4523 {
4524 t = TREE_TYPE (t);
4525
4526 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4527 {
4528 /* Turn this argument into a TYPE_ARGUMENT_PACK
4529 with a single element, which expands T. */
4530 tree vec = make_tree_vec (1);
4531 if (CHECKING_P)
4532 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4533
4534 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4535
4536 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4537 SET_ARGUMENT_PACK_ARGS (t, vec);
4538 }
4539 }
4540 else
4541 {
4542 t = DECL_INITIAL (t);
4543
4544 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4545 {
4546 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4547 with a single element, which expands T. */
4548 tree vec = make_tree_vec (1);
4549 if (CHECKING_P)
4550 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4551
4552 t = convert_from_reference (t);
4553 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4554
4555 t = make_node (NONTYPE_ARGUMENT_PACK);
4556 SET_ARGUMENT_PACK_ARGS (t, vec);
4557 }
4558 else
4559 t = convert_from_reference (t);
4560 }
4561 return t;
4562 }
4563
4564 /* Given a single level of template parameters (a TREE_VEC), return it
4565 as a set of template arguments. */
4566
4567 static tree
4568 template_parms_level_to_args (tree parms)
4569 {
4570 tree a = copy_node (parms);
4571 TREE_TYPE (a) = NULL_TREE;
4572 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4573 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4574
4575 if (CHECKING_P)
4576 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4577
4578 return a;
4579 }
4580
4581 /* Given a set of template parameters, return them as a set of template
4582 arguments. The template parameters are represented as a TREE_VEC, in
4583 the form documented in cp-tree.h for template arguments. */
4584
4585 static tree
4586 template_parms_to_args (tree parms)
4587 {
4588 tree header;
4589 tree args = NULL_TREE;
4590 int length = TMPL_PARMS_DEPTH (parms);
4591 int l = length;
4592
4593 /* If there is only one level of template parameters, we do not
4594 create a TREE_VEC of TREE_VECs. Instead, we return a single
4595 TREE_VEC containing the arguments. */
4596 if (length > 1)
4597 args = make_tree_vec (length);
4598
4599 for (header = parms; header; header = TREE_CHAIN (header))
4600 {
4601 tree a = template_parms_level_to_args (TREE_VALUE (header));
4602
4603 if (length > 1)
4604 TREE_VEC_ELT (args, --l) = a;
4605 else
4606 args = a;
4607 }
4608
4609 return args;
4610 }
4611
4612 /* Within the declaration of a template, return the currently active
4613 template parameters as an argument TREE_VEC. */
4614
4615 static tree
4616 current_template_args (void)
4617 {
4618 return template_parms_to_args (current_template_parms);
4619 }
4620
4621 /* Update the declared TYPE by doing any lookups which were thought to be
4622 dependent, but are not now that we know the SCOPE of the declarator. */
4623
4624 tree
4625 maybe_update_decl_type (tree orig_type, tree scope)
4626 {
4627 tree type = orig_type;
4628
4629 if (type == NULL_TREE)
4630 return type;
4631
4632 if (TREE_CODE (orig_type) == TYPE_DECL)
4633 type = TREE_TYPE (type);
4634
4635 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4636 && dependent_type_p (type)
4637 /* Don't bother building up the args in this case. */
4638 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4639 {
4640 /* tsubst in the args corresponding to the template parameters,
4641 including auto if present. Most things will be unchanged, but
4642 make_typename_type and tsubst_qualified_id will resolve
4643 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4644 tree args = current_template_args ();
4645 tree auto_node = type_uses_auto (type);
4646 tree pushed;
4647 if (auto_node)
4648 {
4649 tree auto_vec = make_tree_vec (1);
4650 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4651 args = add_to_template_args (args, auto_vec);
4652 }
4653 pushed = push_scope (scope);
4654 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4655 if (pushed)
4656 pop_scope (scope);
4657 }
4658
4659 if (type == error_mark_node)
4660 return orig_type;
4661
4662 if (TREE_CODE (orig_type) == TYPE_DECL)
4663 {
4664 if (same_type_p (type, TREE_TYPE (orig_type)))
4665 type = orig_type;
4666 else
4667 type = TYPE_NAME (type);
4668 }
4669 return type;
4670 }
4671
4672 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4673 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4674 the new template is a member template. */
4675
4676 tree
4677 build_template_decl (tree decl, tree parms, bool member_template_p)
4678 {
4679 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4680 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4681 DECL_TEMPLATE_PARMS (tmpl) = parms;
4682 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4683 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4684 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4685
4686 return tmpl;
4687 }
4688
4689 struct template_parm_data
4690 {
4691 /* The level of the template parameters we are currently
4692 processing. */
4693 int level;
4694
4695 /* The index of the specialization argument we are currently
4696 processing. */
4697 int current_arg;
4698
4699 /* An array whose size is the number of template parameters. The
4700 elements are nonzero if the parameter has been used in any one
4701 of the arguments processed so far. */
4702 int* parms;
4703
4704 /* An array whose size is the number of template arguments. The
4705 elements are nonzero if the argument makes use of template
4706 parameters of this level. */
4707 int* arg_uses_template_parms;
4708 };
4709
4710 /* Subroutine of push_template_decl used to see if each template
4711 parameter in a partial specialization is used in the explicit
4712 argument list. If T is of the LEVEL given in DATA (which is
4713 treated as a template_parm_data*), then DATA->PARMS is marked
4714 appropriately. */
4715
4716 static int
4717 mark_template_parm (tree t, void* data)
4718 {
4719 int level;
4720 int idx;
4721 struct template_parm_data* tpd = (struct template_parm_data*) data;
4722
4723 template_parm_level_and_index (t, &level, &idx);
4724
4725 if (level == tpd->level)
4726 {
4727 tpd->parms[idx] = 1;
4728 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4729 }
4730
4731 /* In C++17 the type of a non-type argument is a deduced context. */
4732 if (cxx_dialect >= cxx17
4733 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4734 for_each_template_parm (TREE_TYPE (t),
4735 &mark_template_parm,
4736 data,
4737 NULL,
4738 /*include_nondeduced_p=*/false);
4739
4740 /* Return zero so that for_each_template_parm will continue the
4741 traversal of the tree; we want to mark *every* template parm. */
4742 return 0;
4743 }
4744
4745 /* Process the partial specialization DECL. */
4746
4747 static tree
4748 process_partial_specialization (tree decl)
4749 {
4750 tree type = TREE_TYPE (decl);
4751 tree tinfo = get_template_info (decl);
4752 tree maintmpl = TI_TEMPLATE (tinfo);
4753 tree specargs = TI_ARGS (tinfo);
4754 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4755 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4756 tree inner_parms;
4757 tree inst;
4758 int nargs = TREE_VEC_LENGTH (inner_args);
4759 int ntparms;
4760 int i;
4761 bool did_error_intro = false;
4762 struct template_parm_data tpd;
4763 struct template_parm_data tpd2;
4764
4765 gcc_assert (current_template_parms);
4766
4767 /* A concept cannot be specialized. */
4768 if (flag_concepts && variable_concept_p (maintmpl))
4769 {
4770 error ("specialization of variable concept %q#D", maintmpl);
4771 return error_mark_node;
4772 }
4773
4774 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4775 ntparms = TREE_VEC_LENGTH (inner_parms);
4776
4777 /* We check that each of the template parameters given in the
4778 partial specialization is used in the argument list to the
4779 specialization. For example:
4780
4781 template <class T> struct S;
4782 template <class T> struct S<T*>;
4783
4784 The second declaration is OK because `T*' uses the template
4785 parameter T, whereas
4786
4787 template <class T> struct S<int>;
4788
4789 is no good. Even trickier is:
4790
4791 template <class T>
4792 struct S1
4793 {
4794 template <class U>
4795 struct S2;
4796 template <class U>
4797 struct S2<T>;
4798 };
4799
4800 The S2<T> declaration is actually invalid; it is a
4801 full-specialization. Of course,
4802
4803 template <class U>
4804 struct S2<T (*)(U)>;
4805
4806 or some such would have been OK. */
4807 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4808 tpd.parms = XALLOCAVEC (int, ntparms);
4809 memset (tpd.parms, 0, sizeof (int) * ntparms);
4810
4811 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4812 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4813 for (i = 0; i < nargs; ++i)
4814 {
4815 tpd.current_arg = i;
4816 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4817 &mark_template_parm,
4818 &tpd,
4819 NULL,
4820 /*include_nondeduced_p=*/false);
4821 }
4822 for (i = 0; i < ntparms; ++i)
4823 if (tpd.parms[i] == 0)
4824 {
4825 /* One of the template parms was not used in a deduced context in the
4826 specialization. */
4827 if (!did_error_intro)
4828 {
4829 error ("template parameters not deducible in "
4830 "partial specialization:");
4831 did_error_intro = true;
4832 }
4833
4834 inform (input_location, " %qD",
4835 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4836 }
4837
4838 if (did_error_intro)
4839 return error_mark_node;
4840
4841 /* [temp.class.spec]
4842
4843 The argument list of the specialization shall not be identical to
4844 the implicit argument list of the primary template. */
4845 tree main_args
4846 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4847 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4848 && (!flag_concepts
4849 || !strictly_subsumes (current_template_constraints (),
4850 get_constraints (maintmpl))))
4851 {
4852 if (!flag_concepts)
4853 error ("partial specialization %q+D does not specialize "
4854 "any template arguments; to define the primary template, "
4855 "remove the template argument list", decl);
4856 else
4857 error ("partial specialization %q+D does not specialize any "
4858 "template arguments and is not more constrained than "
4859 "the primary template; to define the primary template, "
4860 "remove the template argument list", decl);
4861 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4862 }
4863
4864 /* A partial specialization that replaces multiple parameters of the
4865 primary template with a pack expansion is less specialized for those
4866 parameters. */
4867 if (nargs < DECL_NTPARMS (maintmpl))
4868 {
4869 error ("partial specialization is not more specialized than the "
4870 "primary template because it replaces multiple parameters "
4871 "with a pack expansion");
4872 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4873 /* Avoid crash in process_partial_specialization. */
4874 return decl;
4875 }
4876
4877 /* If we aren't in a dependent class, we can actually try deduction. */
4878 else if (tpd.level == 1
4879 /* FIXME we should be able to handle a partial specialization of a
4880 partial instantiation, but currently we can't (c++/41727). */
4881 && TMPL_ARGS_DEPTH (specargs) == 1
4882 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4883 {
4884 if (permerror (input_location, "partial specialization %qD is not "
4885 "more specialized than", decl))
4886 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4887 maintmpl);
4888 }
4889
4890 /* [temp.class.spec]
4891
4892 A partially specialized non-type argument expression shall not
4893 involve template parameters of the partial specialization except
4894 when the argument expression is a simple identifier.
4895
4896 The type of a template parameter corresponding to a specialized
4897 non-type argument shall not be dependent on a parameter of the
4898 specialization.
4899
4900 Also, we verify that pack expansions only occur at the
4901 end of the argument list. */
4902 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4903 tpd2.parms = 0;
4904 for (i = 0; i < nargs; ++i)
4905 {
4906 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4907 tree arg = TREE_VEC_ELT (inner_args, i);
4908 tree packed_args = NULL_TREE;
4909 int j, len = 1;
4910
4911 if (ARGUMENT_PACK_P (arg))
4912 {
4913 /* Extract the arguments from the argument pack. We'll be
4914 iterating over these in the following loop. */
4915 packed_args = ARGUMENT_PACK_ARGS (arg);
4916 len = TREE_VEC_LENGTH (packed_args);
4917 }
4918
4919 for (j = 0; j < len; j++)
4920 {
4921 if (packed_args)
4922 /* Get the Jth argument in the parameter pack. */
4923 arg = TREE_VEC_ELT (packed_args, j);
4924
4925 if (PACK_EXPANSION_P (arg))
4926 {
4927 /* Pack expansions must come at the end of the
4928 argument list. */
4929 if ((packed_args && j < len - 1)
4930 || (!packed_args && i < nargs - 1))
4931 {
4932 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4933 error ("parameter pack argument %qE must be at the "
4934 "end of the template argument list", arg);
4935 else
4936 error ("parameter pack argument %qT must be at the "
4937 "end of the template argument list", arg);
4938 }
4939 }
4940
4941 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4942 /* We only care about the pattern. */
4943 arg = PACK_EXPANSION_PATTERN (arg);
4944
4945 if (/* These first two lines are the `non-type' bit. */
4946 !TYPE_P (arg)
4947 && TREE_CODE (arg) != TEMPLATE_DECL
4948 /* This next two lines are the `argument expression is not just a
4949 simple identifier' condition and also the `specialized
4950 non-type argument' bit. */
4951 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4952 && !(REFERENCE_REF_P (arg)
4953 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4954 {
4955 if ((!packed_args && tpd.arg_uses_template_parms[i])
4956 || (packed_args && uses_template_parms (arg)))
4957 error ("template argument %qE involves template parameter(s)",
4958 arg);
4959 else
4960 {
4961 /* Look at the corresponding template parameter,
4962 marking which template parameters its type depends
4963 upon. */
4964 tree type = TREE_TYPE (parm);
4965
4966 if (!tpd2.parms)
4967 {
4968 /* We haven't yet initialized TPD2. Do so now. */
4969 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4970 /* The number of parameters here is the number in the
4971 main template, which, as checked in the assertion
4972 above, is NARGS. */
4973 tpd2.parms = XALLOCAVEC (int, nargs);
4974 tpd2.level =
4975 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4976 }
4977
4978 /* Mark the template parameters. But this time, we're
4979 looking for the template parameters of the main
4980 template, not in the specialization. */
4981 tpd2.current_arg = i;
4982 tpd2.arg_uses_template_parms[i] = 0;
4983 memset (tpd2.parms, 0, sizeof (int) * nargs);
4984 for_each_template_parm (type,
4985 &mark_template_parm,
4986 &tpd2,
4987 NULL,
4988 /*include_nondeduced_p=*/false);
4989
4990 if (tpd2.arg_uses_template_parms [i])
4991 {
4992 /* The type depended on some template parameters.
4993 If they are fully specialized in the
4994 specialization, that's OK. */
4995 int j;
4996 int count = 0;
4997 for (j = 0; j < nargs; ++j)
4998 if (tpd2.parms[j] != 0
4999 && tpd.arg_uses_template_parms [j])
5000 ++count;
5001 if (count != 0)
5002 error_n (input_location, count,
5003 "type %qT of template argument %qE depends "
5004 "on a template parameter",
5005 "type %qT of template argument %qE depends "
5006 "on template parameters",
5007 type,
5008 arg);
5009 }
5010 }
5011 }
5012 }
5013 }
5014
5015 /* We should only get here once. */
5016 if (TREE_CODE (decl) == TYPE_DECL)
5017 gcc_assert (!COMPLETE_TYPE_P (type));
5018
5019 // Build the template decl.
5020 tree tmpl = build_template_decl (decl, current_template_parms,
5021 DECL_MEMBER_TEMPLATE_P (maintmpl));
5022 TREE_TYPE (tmpl) = type;
5023 DECL_TEMPLATE_RESULT (tmpl) = decl;
5024 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5025 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5026 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5027
5028 /* Give template template parms a DECL_CONTEXT of the template
5029 for which they are a parameter. */
5030 for (i = 0; i < ntparms; ++i)
5031 {
5032 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5033 if (TREE_CODE (parm) == TEMPLATE_DECL)
5034 DECL_CONTEXT (parm) = tmpl;
5035 }
5036
5037 if (VAR_P (decl))
5038 /* We didn't register this in check_explicit_specialization so we could
5039 wait until the constraints were set. */
5040 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5041 else
5042 associate_classtype_constraints (type);
5043
5044 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5045 = tree_cons (specargs, tmpl,
5046 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5047 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5048
5049 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5050 inst = TREE_CHAIN (inst))
5051 {
5052 tree instance = TREE_VALUE (inst);
5053 if (TYPE_P (instance)
5054 ? (COMPLETE_TYPE_P (instance)
5055 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5056 : DECL_TEMPLATE_INSTANTIATION (instance))
5057 {
5058 tree spec = most_specialized_partial_spec (instance, tf_none);
5059 tree inst_decl = (DECL_P (instance)
5060 ? instance : TYPE_NAME (instance));
5061 if (!spec)
5062 /* OK */;
5063 else if (spec == error_mark_node)
5064 permerror (input_location,
5065 "declaration of %qD ambiguates earlier template "
5066 "instantiation for %qD", decl, inst_decl);
5067 else if (TREE_VALUE (spec) == tmpl)
5068 permerror (input_location,
5069 "partial specialization of %qD after instantiation "
5070 "of %qD", decl, inst_decl);
5071 }
5072 }
5073
5074 return decl;
5075 }
5076
5077 /* PARM is a template parameter of some form; return the corresponding
5078 TEMPLATE_PARM_INDEX. */
5079
5080 static tree
5081 get_template_parm_index (tree parm)
5082 {
5083 if (TREE_CODE (parm) == PARM_DECL
5084 || TREE_CODE (parm) == CONST_DECL)
5085 parm = DECL_INITIAL (parm);
5086 else if (TREE_CODE (parm) == TYPE_DECL
5087 || TREE_CODE (parm) == TEMPLATE_DECL)
5088 parm = TREE_TYPE (parm);
5089 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5090 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5091 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5092 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5093 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5094 return parm;
5095 }
5096
5097 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5098 parameter packs used by the template parameter PARM. */
5099
5100 static void
5101 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5102 {
5103 /* A type parm can't refer to another parm. */
5104 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5105 return;
5106 else if (TREE_CODE (parm) == PARM_DECL)
5107 {
5108 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5109 ppd, ppd->visited);
5110 return;
5111 }
5112
5113 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5114
5115 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5116 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5117 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5118 }
5119
5120 /* PARM is a template parameter pack. Return any parameter packs used in
5121 its type or the type of any of its template parameters. If there are
5122 any such packs, it will be instantiated into a fixed template parameter
5123 list by partial instantiation rather than be fully deduced. */
5124
5125 tree
5126 fixed_parameter_pack_p (tree parm)
5127 {
5128 /* This can only be true in a member template. */
5129 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5130 return NULL_TREE;
5131 /* This can only be true for a parameter pack. */
5132 if (!template_parameter_pack_p (parm))
5133 return NULL_TREE;
5134 /* A type parm can't refer to another parm. */
5135 if (TREE_CODE (parm) == TYPE_DECL)
5136 return NULL_TREE;
5137
5138 tree parameter_packs = NULL_TREE;
5139 struct find_parameter_pack_data ppd;
5140 ppd.parameter_packs = &parameter_packs;
5141 ppd.visited = new hash_set<tree>;
5142 ppd.type_pack_expansion_p = false;
5143
5144 fixed_parameter_pack_p_1 (parm, &ppd);
5145
5146 delete ppd.visited;
5147 return parameter_packs;
5148 }
5149
5150 /* Check that a template declaration's use of default arguments and
5151 parameter packs is not invalid. Here, PARMS are the template
5152 parameters. IS_PRIMARY is true if DECL is the thing declared by
5153 a primary template. IS_PARTIAL is true if DECL is a partial
5154 specialization.
5155
5156 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5157 function template declaration or a friend class template
5158 declaration. In the function case, 1 indicates a declaration, 2
5159 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5160 emitted for extraneous default arguments.
5161
5162 Returns TRUE if there were no errors found, FALSE otherwise. */
5163
5164 bool
5165 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5166 bool is_partial, int is_friend_decl)
5167 {
5168 const char *msg;
5169 int last_level_to_check;
5170 tree parm_level;
5171 bool no_errors = true;
5172
5173 /* [temp.param]
5174
5175 A default template-argument shall not be specified in a
5176 function template declaration or a function template definition, nor
5177 in the template-parameter-list of the definition of a member of a
5178 class template. */
5179
5180 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5181 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5182 /* You can't have a function template declaration in a local
5183 scope, nor you can you define a member of a class template in a
5184 local scope. */
5185 return true;
5186
5187 if ((TREE_CODE (decl) == TYPE_DECL
5188 && TREE_TYPE (decl)
5189 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5190 || (TREE_CODE (decl) == FUNCTION_DECL
5191 && LAMBDA_FUNCTION_P (decl)))
5192 /* A lambda doesn't have an explicit declaration; don't complain
5193 about the parms of the enclosing class. */
5194 return true;
5195
5196 if (current_class_type
5197 && !TYPE_BEING_DEFINED (current_class_type)
5198 && DECL_LANG_SPECIFIC (decl)
5199 && DECL_DECLARES_FUNCTION_P (decl)
5200 /* If this is either a friend defined in the scope of the class
5201 or a member function. */
5202 && (DECL_FUNCTION_MEMBER_P (decl)
5203 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5204 : DECL_FRIEND_CONTEXT (decl)
5205 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5206 : false)
5207 /* And, if it was a member function, it really was defined in
5208 the scope of the class. */
5209 && (!DECL_FUNCTION_MEMBER_P (decl)
5210 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5211 /* We already checked these parameters when the template was
5212 declared, so there's no need to do it again now. This function
5213 was defined in class scope, but we're processing its body now
5214 that the class is complete. */
5215 return true;
5216
5217 /* Core issue 226 (C++0x only): the following only applies to class
5218 templates. */
5219 if (is_primary
5220 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5221 {
5222 /* [temp.param]
5223
5224 If a template-parameter has a default template-argument, all
5225 subsequent template-parameters shall have a default
5226 template-argument supplied. */
5227 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5228 {
5229 tree inner_parms = TREE_VALUE (parm_level);
5230 int ntparms = TREE_VEC_LENGTH (inner_parms);
5231 int seen_def_arg_p = 0;
5232 int i;
5233
5234 for (i = 0; i < ntparms; ++i)
5235 {
5236 tree parm = TREE_VEC_ELT (inner_parms, i);
5237
5238 if (parm == error_mark_node)
5239 continue;
5240
5241 if (TREE_PURPOSE (parm))
5242 seen_def_arg_p = 1;
5243 else if (seen_def_arg_p
5244 && !template_parameter_pack_p (TREE_VALUE (parm)))
5245 {
5246 error ("no default argument for %qD", TREE_VALUE (parm));
5247 /* For better subsequent error-recovery, we indicate that
5248 there should have been a default argument. */
5249 TREE_PURPOSE (parm) = error_mark_node;
5250 no_errors = false;
5251 }
5252 else if (!is_partial
5253 && !is_friend_decl
5254 /* Don't complain about an enclosing partial
5255 specialization. */
5256 && parm_level == parms
5257 && TREE_CODE (decl) == TYPE_DECL
5258 && i < ntparms - 1
5259 && template_parameter_pack_p (TREE_VALUE (parm))
5260 /* A fixed parameter pack will be partially
5261 instantiated into a fixed length list. */
5262 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5263 {
5264 /* A primary class template can only have one
5265 parameter pack, at the end of the template
5266 parameter list. */
5267
5268 error ("parameter pack %q+D must be at the end of the"
5269 " template parameter list", TREE_VALUE (parm));
5270
5271 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5272 = error_mark_node;
5273 no_errors = false;
5274 }
5275 }
5276 }
5277 }
5278
5279 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5280 || is_partial
5281 || !is_primary
5282 || is_friend_decl)
5283 /* For an ordinary class template, default template arguments are
5284 allowed at the innermost level, e.g.:
5285 template <class T = int>
5286 struct S {};
5287 but, in a partial specialization, they're not allowed even
5288 there, as we have in [temp.class.spec]:
5289
5290 The template parameter list of a specialization shall not
5291 contain default template argument values.
5292
5293 So, for a partial specialization, or for a function template
5294 (in C++98/C++03), we look at all of them. */
5295 ;
5296 else
5297 /* But, for a primary class template that is not a partial
5298 specialization we look at all template parameters except the
5299 innermost ones. */
5300 parms = TREE_CHAIN (parms);
5301
5302 /* Figure out what error message to issue. */
5303 if (is_friend_decl == 2)
5304 msg = G_("default template arguments may not be used in function template "
5305 "friend re-declaration");
5306 else if (is_friend_decl)
5307 msg = G_("default template arguments may not be used in template "
5308 "friend declarations");
5309 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5310 msg = G_("default template arguments may not be used in function templates "
5311 "without -std=c++11 or -std=gnu++11");
5312 else if (is_partial)
5313 msg = G_("default template arguments may not be used in "
5314 "partial specializations");
5315 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5316 msg = G_("default argument for template parameter for class enclosing %qD");
5317 else
5318 /* Per [temp.param]/9, "A default template-argument shall not be
5319 specified in the template-parameter-lists of the definition of
5320 a member of a class template that appears outside of the member's
5321 class.", thus if we aren't handling a member of a class template
5322 there is no need to examine the parameters. */
5323 return true;
5324
5325 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5326 /* If we're inside a class definition, there's no need to
5327 examine the parameters to the class itself. On the one
5328 hand, they will be checked when the class is defined, and,
5329 on the other, default arguments are valid in things like:
5330 template <class T = double>
5331 struct S { template <class U> void f(U); };
5332 Here the default argument for `S' has no bearing on the
5333 declaration of `f'. */
5334 last_level_to_check = template_class_depth (current_class_type) + 1;
5335 else
5336 /* Check everything. */
5337 last_level_to_check = 0;
5338
5339 for (parm_level = parms;
5340 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5341 parm_level = TREE_CHAIN (parm_level))
5342 {
5343 tree inner_parms = TREE_VALUE (parm_level);
5344 int i;
5345 int ntparms;
5346
5347 ntparms = TREE_VEC_LENGTH (inner_parms);
5348 for (i = 0; i < ntparms; ++i)
5349 {
5350 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5351 continue;
5352
5353 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5354 {
5355 if (msg)
5356 {
5357 no_errors = false;
5358 if (is_friend_decl == 2)
5359 return no_errors;
5360
5361 error (msg, decl);
5362 msg = 0;
5363 }
5364
5365 /* Clear out the default argument so that we are not
5366 confused later. */
5367 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5368 }
5369 }
5370
5371 /* At this point, if we're still interested in issuing messages,
5372 they must apply to classes surrounding the object declared. */
5373 if (msg)
5374 msg = G_("default argument for template parameter for class "
5375 "enclosing %qD");
5376 }
5377
5378 return no_errors;
5379 }
5380
5381 /* Worker for push_template_decl_real, called via
5382 for_each_template_parm. DATA is really an int, indicating the
5383 level of the parameters we are interested in. If T is a template
5384 parameter of that level, return nonzero. */
5385
5386 static int
5387 template_parm_this_level_p (tree t, void* data)
5388 {
5389 int this_level = *(int *)data;
5390 int level;
5391
5392 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5393 level = TEMPLATE_PARM_LEVEL (t);
5394 else
5395 level = TEMPLATE_TYPE_LEVEL (t);
5396 return level == this_level;
5397 }
5398
5399 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5400 DATA is really an int, indicating the innermost outer level of parameters.
5401 If T is a template parameter of that level or further out, return
5402 nonzero. */
5403
5404 static int
5405 template_parm_outer_level (tree t, void *data)
5406 {
5407 int this_level = *(int *)data;
5408 int level;
5409
5410 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5411 level = TEMPLATE_PARM_LEVEL (t);
5412 else
5413 level = TEMPLATE_TYPE_LEVEL (t);
5414 return level <= this_level;
5415 }
5416
5417 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5418 parameters given by current_template_args, or reuses a
5419 previously existing one, if appropriate. Returns the DECL, or an
5420 equivalent one, if it is replaced via a call to duplicate_decls.
5421
5422 If IS_FRIEND is true, DECL is a friend declaration. */
5423
5424 tree
5425 push_template_decl_real (tree decl, bool is_friend)
5426 {
5427 tree tmpl;
5428 tree args;
5429 tree info;
5430 tree ctx;
5431 bool is_primary;
5432 bool is_partial;
5433 int new_template_p = 0;
5434 /* True if the template is a member template, in the sense of
5435 [temp.mem]. */
5436 bool member_template_p = false;
5437
5438 if (decl == error_mark_node || !current_template_parms)
5439 return error_mark_node;
5440
5441 /* See if this is a partial specialization. */
5442 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5443 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5444 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5445 || (VAR_P (decl)
5446 && DECL_LANG_SPECIFIC (decl)
5447 && DECL_TEMPLATE_SPECIALIZATION (decl)
5448 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5449
5450 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5451 is_friend = true;
5452
5453 if (is_friend)
5454 /* For a friend, we want the context of the friend, not
5455 the type of which it is a friend. */
5456 ctx = CP_DECL_CONTEXT (decl);
5457 else if (CP_DECL_CONTEXT (decl)
5458 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5459 /* In the case of a virtual function, we want the class in which
5460 it is defined. */
5461 ctx = CP_DECL_CONTEXT (decl);
5462 else
5463 /* Otherwise, if we're currently defining some class, the DECL
5464 is assumed to be a member of the class. */
5465 ctx = current_scope ();
5466
5467 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5468 ctx = NULL_TREE;
5469
5470 if (!DECL_CONTEXT (decl))
5471 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5472
5473 /* See if this is a primary template. */
5474 if (is_friend && ctx
5475 && uses_template_parms_level (ctx, processing_template_decl))
5476 /* A friend template that specifies a class context, i.e.
5477 template <typename T> friend void A<T>::f();
5478 is not primary. */
5479 is_primary = false;
5480 else if (TREE_CODE (decl) == TYPE_DECL
5481 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5482 is_primary = false;
5483 else
5484 is_primary = template_parm_scope_p ();
5485
5486 if (is_primary)
5487 {
5488 warning (OPT_Wtemplates, "template %qD declared", decl);
5489
5490 if (DECL_CLASS_SCOPE_P (decl))
5491 member_template_p = true;
5492 if (TREE_CODE (decl) == TYPE_DECL
5493 && anon_aggrname_p (DECL_NAME (decl)))
5494 {
5495 error ("template class without a name");
5496 return error_mark_node;
5497 }
5498 else if (TREE_CODE (decl) == FUNCTION_DECL)
5499 {
5500 if (member_template_p)
5501 {
5502 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5503 error ("member template %qD may not have virt-specifiers", decl);
5504 }
5505 if (DECL_DESTRUCTOR_P (decl))
5506 {
5507 /* [temp.mem]
5508
5509 A destructor shall not be a member template. */
5510 error ("destructor %qD declared as member template", decl);
5511 return error_mark_node;
5512 }
5513 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5514 && (!prototype_p (TREE_TYPE (decl))
5515 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5516 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5517 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5518 == void_list_node)))
5519 {
5520 /* [basic.stc.dynamic.allocation]
5521
5522 An allocation function can be a function
5523 template. ... Template allocation functions shall
5524 have two or more parameters. */
5525 error ("invalid template declaration of %qD", decl);
5526 return error_mark_node;
5527 }
5528 }
5529 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5530 && CLASS_TYPE_P (TREE_TYPE (decl)))
5531 {
5532 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5533 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5534 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5535 {
5536 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5537 if (TREE_CODE (t) == TYPE_DECL)
5538 t = TREE_TYPE (t);
5539 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5540 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5541 }
5542 }
5543 else if (TREE_CODE (decl) == TYPE_DECL
5544 && TYPE_DECL_ALIAS_P (decl))
5545 /* alias-declaration */
5546 gcc_assert (!DECL_ARTIFICIAL (decl));
5547 else if (VAR_P (decl))
5548 /* C++14 variable template. */;
5549 else
5550 {
5551 error ("template declaration of %q#D", decl);
5552 return error_mark_node;
5553 }
5554 }
5555
5556 /* Check to see that the rules regarding the use of default
5557 arguments are not being violated. We check args for a friend
5558 functions when we know whether it's a definition, introducing
5559 declaration or re-declaration. */
5560 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5561 check_default_tmpl_args (decl, current_template_parms,
5562 is_primary, is_partial, is_friend);
5563
5564 /* Ensure that there are no parameter packs in the type of this
5565 declaration that have not been expanded. */
5566 if (TREE_CODE (decl) == FUNCTION_DECL)
5567 {
5568 /* Check each of the arguments individually to see if there are
5569 any bare parameter packs. */
5570 tree type = TREE_TYPE (decl);
5571 tree arg = DECL_ARGUMENTS (decl);
5572 tree argtype = TYPE_ARG_TYPES (type);
5573
5574 while (arg && argtype)
5575 {
5576 if (!DECL_PACK_P (arg)
5577 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5578 {
5579 /* This is a PARM_DECL that contains unexpanded parameter
5580 packs. We have already complained about this in the
5581 check_for_bare_parameter_packs call, so just replace
5582 these types with ERROR_MARK_NODE. */
5583 TREE_TYPE (arg) = error_mark_node;
5584 TREE_VALUE (argtype) = error_mark_node;
5585 }
5586
5587 arg = DECL_CHAIN (arg);
5588 argtype = TREE_CHAIN (argtype);
5589 }
5590
5591 /* Check for bare parameter packs in the return type and the
5592 exception specifiers. */
5593 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5594 /* Errors were already issued, set return type to int
5595 as the frontend doesn't expect error_mark_node as
5596 the return type. */
5597 TREE_TYPE (type) = integer_type_node;
5598 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5599 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5600 }
5601 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5602 && TYPE_DECL_ALIAS_P (decl))
5603 ? DECL_ORIGINAL_TYPE (decl)
5604 : TREE_TYPE (decl)))
5605 {
5606 TREE_TYPE (decl) = error_mark_node;
5607 return error_mark_node;
5608 }
5609
5610 if (is_partial)
5611 return process_partial_specialization (decl);
5612
5613 args = current_template_args ();
5614
5615 if (!ctx
5616 || TREE_CODE (ctx) == FUNCTION_DECL
5617 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5618 || (TREE_CODE (decl) == TYPE_DECL
5619 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5620 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5621 {
5622 if (DECL_LANG_SPECIFIC (decl)
5623 && DECL_TEMPLATE_INFO (decl)
5624 && DECL_TI_TEMPLATE (decl))
5625 tmpl = DECL_TI_TEMPLATE (decl);
5626 /* If DECL is a TYPE_DECL for a class-template, then there won't
5627 be DECL_LANG_SPECIFIC. The information equivalent to
5628 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5629 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5630 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5631 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5632 {
5633 /* Since a template declaration already existed for this
5634 class-type, we must be redeclaring it here. Make sure
5635 that the redeclaration is valid. */
5636 redeclare_class_template (TREE_TYPE (decl),
5637 current_template_parms,
5638 current_template_constraints ());
5639 /* We don't need to create a new TEMPLATE_DECL; just use the
5640 one we already had. */
5641 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5642 }
5643 else
5644 {
5645 tmpl = build_template_decl (decl, current_template_parms,
5646 member_template_p);
5647 new_template_p = 1;
5648
5649 if (DECL_LANG_SPECIFIC (decl)
5650 && DECL_TEMPLATE_SPECIALIZATION (decl))
5651 {
5652 /* A specialization of a member template of a template
5653 class. */
5654 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5655 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5656 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5657 }
5658 }
5659 }
5660 else
5661 {
5662 tree a, t, current, parms;
5663 int i;
5664 tree tinfo = get_template_info (decl);
5665
5666 if (!tinfo)
5667 {
5668 error ("template definition of non-template %q#D", decl);
5669 return error_mark_node;
5670 }
5671
5672 tmpl = TI_TEMPLATE (tinfo);
5673
5674 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5675 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5676 && DECL_TEMPLATE_SPECIALIZATION (decl)
5677 && DECL_MEMBER_TEMPLATE_P (tmpl))
5678 {
5679 tree new_tmpl;
5680
5681 /* The declaration is a specialization of a member
5682 template, declared outside the class. Therefore, the
5683 innermost template arguments will be NULL, so we
5684 replace them with the arguments determined by the
5685 earlier call to check_explicit_specialization. */
5686 args = DECL_TI_ARGS (decl);
5687
5688 new_tmpl
5689 = build_template_decl (decl, current_template_parms,
5690 member_template_p);
5691 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5692 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5693 DECL_TI_TEMPLATE (decl) = new_tmpl;
5694 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5695 DECL_TEMPLATE_INFO (new_tmpl)
5696 = build_template_info (tmpl, args);
5697
5698 register_specialization (new_tmpl,
5699 most_general_template (tmpl),
5700 args,
5701 is_friend, 0);
5702 return decl;
5703 }
5704
5705 /* Make sure the template headers we got make sense. */
5706
5707 parms = DECL_TEMPLATE_PARMS (tmpl);
5708 i = TMPL_PARMS_DEPTH (parms);
5709 if (TMPL_ARGS_DEPTH (args) != i)
5710 {
5711 error ("expected %d levels of template parms for %q#D, got %d",
5712 i, decl, TMPL_ARGS_DEPTH (args));
5713 DECL_INTERFACE_KNOWN (decl) = 1;
5714 return error_mark_node;
5715 }
5716 else
5717 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5718 {
5719 a = TMPL_ARGS_LEVEL (args, i);
5720 t = INNERMOST_TEMPLATE_PARMS (parms);
5721
5722 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5723 {
5724 if (current == decl)
5725 error ("got %d template parameters for %q#D",
5726 TREE_VEC_LENGTH (a), decl);
5727 else
5728 error ("got %d template parameters for %q#T",
5729 TREE_VEC_LENGTH (a), current);
5730 error (" but %d required", TREE_VEC_LENGTH (t));
5731 /* Avoid crash in import_export_decl. */
5732 DECL_INTERFACE_KNOWN (decl) = 1;
5733 return error_mark_node;
5734 }
5735
5736 if (current == decl)
5737 current = ctx;
5738 else if (current == NULL_TREE)
5739 /* Can happen in erroneous input. */
5740 break;
5741 else
5742 current = get_containing_scope (current);
5743 }
5744
5745 /* Check that the parms are used in the appropriate qualifying scopes
5746 in the declarator. */
5747 if (!comp_template_args
5748 (TI_ARGS (tinfo),
5749 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5750 {
5751 error ("template arguments to %qD do not match original "
5752 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5753 if (!uses_template_parms (TI_ARGS (tinfo)))
5754 inform (input_location, "use %<template<>%> for"
5755 " an explicit specialization");
5756 /* Avoid crash in import_export_decl. */
5757 DECL_INTERFACE_KNOWN (decl) = 1;
5758 return error_mark_node;
5759 }
5760 }
5761
5762 DECL_TEMPLATE_RESULT (tmpl) = decl;
5763 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5764
5765 /* Push template declarations for global functions and types. Note
5766 that we do not try to push a global template friend declared in a
5767 template class; such a thing may well depend on the template
5768 parameters of the class. */
5769 if (new_template_p && !ctx
5770 && !(is_friend && template_class_depth (current_class_type) > 0))
5771 {
5772 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5773 if (tmpl == error_mark_node)
5774 return error_mark_node;
5775
5776 /* Hide template friend classes that haven't been declared yet. */
5777 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5778 {
5779 DECL_ANTICIPATED (tmpl) = 1;
5780 DECL_FRIEND_P (tmpl) = 1;
5781 }
5782 }
5783
5784 if (is_primary)
5785 {
5786 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5787
5788 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5789
5790 /* Give template template parms a DECL_CONTEXT of the template
5791 for which they are a parameter. */
5792 parms = INNERMOST_TEMPLATE_PARMS (parms);
5793 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5794 {
5795 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5796 if (TREE_CODE (parm) == TEMPLATE_DECL)
5797 DECL_CONTEXT (parm) = tmpl;
5798 }
5799
5800 if (TREE_CODE (decl) == TYPE_DECL
5801 && TYPE_DECL_ALIAS_P (decl)
5802 && complex_alias_template_p (tmpl))
5803 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5804 }
5805
5806 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5807 back to its most general template. If TMPL is a specialization,
5808 ARGS may only have the innermost set of arguments. Add the missing
5809 argument levels if necessary. */
5810 if (DECL_TEMPLATE_INFO (tmpl))
5811 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5812
5813 info = build_template_info (tmpl, args);
5814
5815 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5816 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5817 else
5818 {
5819 if (is_primary)
5820 retrofit_lang_decl (decl);
5821 if (DECL_LANG_SPECIFIC (decl))
5822 DECL_TEMPLATE_INFO (decl) = info;
5823 }
5824
5825 if (flag_implicit_templates
5826 && !is_friend
5827 && TREE_PUBLIC (decl)
5828 && VAR_OR_FUNCTION_DECL_P (decl))
5829 /* Set DECL_COMDAT on template instantiations; if we force
5830 them to be emitted by explicit instantiation or -frepo,
5831 mark_needed will tell cgraph to do the right thing. */
5832 DECL_COMDAT (decl) = true;
5833
5834 return DECL_TEMPLATE_RESULT (tmpl);
5835 }
5836
5837 tree
5838 push_template_decl (tree decl)
5839 {
5840 return push_template_decl_real (decl, false);
5841 }
5842
5843 /* FN is an inheriting constructor that inherits from the constructor
5844 template INHERITED; turn FN into a constructor template with a matching
5845 template header. */
5846
5847 tree
5848 add_inherited_template_parms (tree fn, tree inherited)
5849 {
5850 tree inner_parms
5851 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5852 inner_parms = copy_node (inner_parms);
5853 tree parms
5854 = tree_cons (size_int (processing_template_decl + 1),
5855 inner_parms, current_template_parms);
5856 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5857 tree args = template_parms_to_args (parms);
5858 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5859 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5860 DECL_TEMPLATE_RESULT (tmpl) = fn;
5861 DECL_ARTIFICIAL (tmpl) = true;
5862 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5863 return tmpl;
5864 }
5865
5866 /* Called when a class template TYPE is redeclared with the indicated
5867 template PARMS, e.g.:
5868
5869 template <class T> struct S;
5870 template <class T> struct S {}; */
5871
5872 bool
5873 redeclare_class_template (tree type, tree parms, tree cons)
5874 {
5875 tree tmpl;
5876 tree tmpl_parms;
5877 int i;
5878
5879 if (!TYPE_TEMPLATE_INFO (type))
5880 {
5881 error ("%qT is not a template type", type);
5882 return false;
5883 }
5884
5885 tmpl = TYPE_TI_TEMPLATE (type);
5886 if (!PRIMARY_TEMPLATE_P (tmpl))
5887 /* The type is nested in some template class. Nothing to worry
5888 about here; there are no new template parameters for the nested
5889 type. */
5890 return true;
5891
5892 if (!parms)
5893 {
5894 error ("template specifiers not specified in declaration of %qD",
5895 tmpl);
5896 return false;
5897 }
5898
5899 parms = INNERMOST_TEMPLATE_PARMS (parms);
5900 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5901
5902 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5903 {
5904 error_n (input_location, TREE_VEC_LENGTH (parms),
5905 "redeclared with %d template parameter",
5906 "redeclared with %d template parameters",
5907 TREE_VEC_LENGTH (parms));
5908 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5909 "previous declaration %qD used %d template parameter",
5910 "previous declaration %qD used %d template parameters",
5911 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5912 return false;
5913 }
5914
5915 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5916 {
5917 tree tmpl_parm;
5918 tree parm;
5919 tree tmpl_default;
5920 tree parm_default;
5921
5922 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5923 || TREE_VEC_ELT (parms, i) == error_mark_node)
5924 continue;
5925
5926 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5927 if (error_operand_p (tmpl_parm))
5928 return false;
5929
5930 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5931 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5932 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5933
5934 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5935 TEMPLATE_DECL. */
5936 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5937 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5938 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5939 || (TREE_CODE (tmpl_parm) != PARM_DECL
5940 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5941 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5942 || (TREE_CODE (tmpl_parm) == PARM_DECL
5943 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5944 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5945 {
5946 error ("template parameter %q+#D", tmpl_parm);
5947 error ("redeclared here as %q#D", parm);
5948 return false;
5949 }
5950
5951 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5952 {
5953 /* We have in [temp.param]:
5954
5955 A template-parameter may not be given default arguments
5956 by two different declarations in the same scope. */
5957 error_at (input_location, "redefinition of default argument for %q#D", parm);
5958 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5959 "original definition appeared here");
5960 return false;
5961 }
5962
5963 if (parm_default != NULL_TREE)
5964 /* Update the previous template parameters (which are the ones
5965 that will really count) with the new default value. */
5966 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5967 else if (tmpl_default != NULL_TREE)
5968 /* Update the new parameters, too; they'll be used as the
5969 parameters for any members. */
5970 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5971
5972 /* Give each template template parm in this redeclaration a
5973 DECL_CONTEXT of the template for which they are a parameter. */
5974 if (TREE_CODE (parm) == TEMPLATE_DECL)
5975 {
5976 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5977 DECL_CONTEXT (parm) = tmpl;
5978 }
5979
5980 if (TREE_CODE (parm) == TYPE_DECL)
5981 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5982 }
5983
5984 // Cannot redeclare a class template with a different set of constraints.
5985 if (!equivalent_constraints (get_constraints (tmpl), cons))
5986 {
5987 error_at (input_location, "redeclaration %q#D with different "
5988 "constraints", tmpl);
5989 inform (DECL_SOURCE_LOCATION (tmpl),
5990 "original declaration appeared here");
5991 }
5992
5993 return true;
5994 }
5995
5996 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5997 to be used when the caller has already checked
5998 (processing_template_decl
5999 && !instantiation_dependent_expression_p (expr)
6000 && potential_constant_expression (expr))
6001 and cleared processing_template_decl. */
6002
6003 tree
6004 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6005 {
6006 return tsubst_copy_and_build (expr,
6007 /*args=*/NULL_TREE,
6008 complain,
6009 /*in_decl=*/NULL_TREE,
6010 /*function_p=*/false,
6011 /*integral_constant_expression_p=*/true);
6012 }
6013
6014 /* Simplify EXPR if it is a non-dependent expression. Returns the
6015 (possibly simplified) expression. */
6016
6017 tree
6018 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6019 {
6020 if (expr == NULL_TREE)
6021 return NULL_TREE;
6022
6023 /* If we're in a template, but EXPR isn't value dependent, simplify
6024 it. We're supposed to treat:
6025
6026 template <typename T> void f(T[1 + 1]);
6027 template <typename T> void f(T[2]);
6028
6029 as two declarations of the same function, for example. */
6030 if (processing_template_decl
6031 && is_nondependent_constant_expression (expr))
6032 {
6033 processing_template_decl_sentinel s;
6034 expr = instantiate_non_dependent_expr_internal (expr, complain);
6035 }
6036 return expr;
6037 }
6038
6039 tree
6040 instantiate_non_dependent_expr (tree expr)
6041 {
6042 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6043 }
6044
6045 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6046 an uninstantiated expression. */
6047
6048 tree
6049 instantiate_non_dependent_or_null (tree expr)
6050 {
6051 if (expr == NULL_TREE)
6052 return NULL_TREE;
6053 if (processing_template_decl)
6054 {
6055 if (!is_nondependent_constant_expression (expr))
6056 expr = NULL_TREE;
6057 else
6058 {
6059 processing_template_decl_sentinel s;
6060 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6061 }
6062 }
6063 return expr;
6064 }
6065
6066 /* True iff T is a specialization of a variable template. */
6067
6068 bool
6069 variable_template_specialization_p (tree t)
6070 {
6071 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6072 return false;
6073 tree tmpl = DECL_TI_TEMPLATE (t);
6074 return variable_template_p (tmpl);
6075 }
6076
6077 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6078 template declaration, or a TYPE_DECL for an alias declaration. */
6079
6080 bool
6081 alias_type_or_template_p (tree t)
6082 {
6083 if (t == NULL_TREE)
6084 return false;
6085 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6086 || (TYPE_P (t)
6087 && TYPE_NAME (t)
6088 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6089 || DECL_ALIAS_TEMPLATE_P (t));
6090 }
6091
6092 /* Return TRUE iff T is a specialization of an alias template. */
6093
6094 bool
6095 alias_template_specialization_p (const_tree t)
6096 {
6097 /* It's an alias template specialization if it's an alias and its
6098 TYPE_NAME is a specialization of a primary template. */
6099 if (TYPE_ALIAS_P (t))
6100 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6101 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6102
6103 return false;
6104 }
6105
6106 /* An alias template is complex from a SFINAE perspective if a template-id
6107 using that alias can be ill-formed when the expansion is not, as with
6108 the void_t template. We determine this by checking whether the
6109 expansion for the alias template uses all its template parameters. */
6110
6111 struct uses_all_template_parms_data
6112 {
6113 int level;
6114 bool *seen;
6115 };
6116
6117 static int
6118 uses_all_template_parms_r (tree t, void *data_)
6119 {
6120 struct uses_all_template_parms_data &data
6121 = *(struct uses_all_template_parms_data*)data_;
6122 tree idx = get_template_parm_index (t);
6123
6124 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6125 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6126 return 0;
6127 }
6128
6129 static bool
6130 complex_alias_template_p (const_tree tmpl)
6131 {
6132 struct uses_all_template_parms_data data;
6133 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6134 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6135 data.level = TMPL_PARMS_DEPTH (parms);
6136 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6137 data.seen = XALLOCAVEC (bool, len);
6138 for (int i = 0; i < len; ++i)
6139 data.seen[i] = false;
6140
6141 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6142 for (int i = 0; i < len; ++i)
6143 if (!data.seen[i])
6144 return true;
6145 return false;
6146 }
6147
6148 /* Return TRUE iff T is a specialization of a complex alias template with
6149 dependent template-arguments. */
6150
6151 bool
6152 dependent_alias_template_spec_p (const_tree t)
6153 {
6154 if (!alias_template_specialization_p (t))
6155 return false;
6156
6157 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6158 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6159 return false;
6160
6161 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6162 if (!any_dependent_template_arguments_p (args))
6163 return false;
6164
6165 return true;
6166 }
6167
6168 /* Return the number of innermost template parameters in TMPL. */
6169
6170 static int
6171 num_innermost_template_parms (tree tmpl)
6172 {
6173 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6174 return TREE_VEC_LENGTH (parms);
6175 }
6176
6177 /* Return either TMPL or another template that it is equivalent to under DR
6178 1286: An alias that just changes the name of a template is equivalent to
6179 the other template. */
6180
6181 static tree
6182 get_underlying_template (tree tmpl)
6183 {
6184 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6185 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6186 {
6187 /* Determine if the alias is equivalent to an underlying template. */
6188 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6189 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6190 if (!tinfo)
6191 break;
6192
6193 tree underlying = TI_TEMPLATE (tinfo);
6194 if (!PRIMARY_TEMPLATE_P (underlying)
6195 || (num_innermost_template_parms (tmpl)
6196 != num_innermost_template_parms (underlying)))
6197 break;
6198
6199 tree alias_args = INNERMOST_TEMPLATE_ARGS
6200 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6201 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6202 break;
6203
6204 /* Alias is equivalent. Strip it and repeat. */
6205 tmpl = underlying;
6206 }
6207
6208 return tmpl;
6209 }
6210
6211 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6212 must be a reference-to-function or a pointer-to-function type, as specified
6213 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6214 and check that the resulting function has external linkage. */
6215
6216 static tree
6217 convert_nontype_argument_function (tree type, tree expr,
6218 tsubst_flags_t complain)
6219 {
6220 tree fns = expr;
6221 tree fn, fn_no_ptr;
6222 linkage_kind linkage;
6223
6224 fn = instantiate_type (type, fns, tf_none);
6225 if (fn == error_mark_node)
6226 return error_mark_node;
6227
6228 if (value_dependent_expression_p (fn))
6229 goto accept;
6230
6231 fn_no_ptr = strip_fnptr_conv (fn);
6232 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6233 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6234 if (BASELINK_P (fn_no_ptr))
6235 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6236
6237 /* [temp.arg.nontype]/1
6238
6239 A template-argument for a non-type, non-template template-parameter
6240 shall be one of:
6241 [...]
6242 -- the address of an object or function with external [C++11: or
6243 internal] linkage. */
6244
6245 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6246 {
6247 if (complain & tf_error)
6248 {
6249 error ("%qE is not a valid template argument for type %qT",
6250 expr, type);
6251 if (TYPE_PTR_P (type))
6252 inform (input_location, "it must be the address of a function "
6253 "with external linkage");
6254 else
6255 inform (input_location, "it must be the name of a function with "
6256 "external linkage");
6257 }
6258 return NULL_TREE;
6259 }
6260
6261 linkage = decl_linkage (fn_no_ptr);
6262 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6263 {
6264 if (complain & tf_error)
6265 {
6266 if (cxx_dialect >= cxx11)
6267 error ("%qE is not a valid template argument for type %qT "
6268 "because %qD has no linkage",
6269 expr, type, fn_no_ptr);
6270 else
6271 error ("%qE is not a valid template argument for type %qT "
6272 "because %qD does not have external linkage",
6273 expr, type, fn_no_ptr);
6274 }
6275 return NULL_TREE;
6276 }
6277
6278 accept:
6279 if (TREE_CODE (type) == REFERENCE_TYPE)
6280 {
6281 if (REFERENCE_REF_P (fn))
6282 fn = TREE_OPERAND (fn, 0);
6283 else
6284 fn = build_address (fn);
6285 }
6286 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6287 fn = build_nop (type, fn);
6288
6289 return fn;
6290 }
6291
6292 /* Subroutine of convert_nontype_argument.
6293 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6294 Emit an error otherwise. */
6295
6296 static bool
6297 check_valid_ptrmem_cst_expr (tree type, tree expr,
6298 tsubst_flags_t complain)
6299 {
6300 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6301 tree orig_expr = expr;
6302 STRIP_NOPS (expr);
6303 if (null_ptr_cst_p (expr))
6304 return true;
6305 if (TREE_CODE (expr) == PTRMEM_CST
6306 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6307 PTRMEM_CST_CLASS (expr)))
6308 return true;
6309 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6310 return true;
6311 if (processing_template_decl
6312 && TREE_CODE (expr) == ADDR_EXPR
6313 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6314 return true;
6315 if (complain & tf_error)
6316 {
6317 error_at (loc, "%qE is not a valid template argument for type %qT",
6318 orig_expr, type);
6319 if (TREE_CODE (expr) != PTRMEM_CST)
6320 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6321 else
6322 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6323 }
6324 return false;
6325 }
6326
6327 /* Returns TRUE iff the address of OP is value-dependent.
6328
6329 14.6.2.4 [temp.dep.temp]:
6330 A non-integral non-type template-argument is dependent if its type is
6331 dependent or it has either of the following forms
6332 qualified-id
6333 & qualified-id
6334 and contains a nested-name-specifier which specifies a class-name that
6335 names a dependent type.
6336
6337 We generalize this to just say that the address of a member of a
6338 dependent class is value-dependent; the above doesn't cover the
6339 address of a static data member named with an unqualified-id. */
6340
6341 static bool
6342 has_value_dependent_address (tree op)
6343 {
6344 /* We could use get_inner_reference here, but there's no need;
6345 this is only relevant for template non-type arguments, which
6346 can only be expressed as &id-expression. */
6347 if (DECL_P (op))
6348 {
6349 tree ctx = CP_DECL_CONTEXT (op);
6350 if (TYPE_P (ctx) && dependent_type_p (ctx))
6351 return true;
6352 }
6353
6354 return false;
6355 }
6356
6357 /* The next set of functions are used for providing helpful explanatory
6358 diagnostics for failed overload resolution. Their messages should be
6359 indented by two spaces for consistency with the messages in
6360 call.c */
6361
6362 static int
6363 unify_success (bool /*explain_p*/)
6364 {
6365 return 0;
6366 }
6367
6368 /* Other failure functions should call this one, to provide a single function
6369 for setting a breakpoint on. */
6370
6371 static int
6372 unify_invalid (bool /*explain_p*/)
6373 {
6374 return 1;
6375 }
6376
6377 static int
6378 unify_parameter_deduction_failure (bool explain_p, tree parm)
6379 {
6380 if (explain_p)
6381 inform (input_location,
6382 " couldn't deduce template parameter %qD", parm);
6383 return unify_invalid (explain_p);
6384 }
6385
6386 static int
6387 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6388 {
6389 if (explain_p)
6390 inform (input_location,
6391 " types %qT and %qT have incompatible cv-qualifiers",
6392 parm, arg);
6393 return unify_invalid (explain_p);
6394 }
6395
6396 static int
6397 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6398 {
6399 if (explain_p)
6400 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6401 return unify_invalid (explain_p);
6402 }
6403
6404 static int
6405 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6406 {
6407 if (explain_p)
6408 inform (input_location,
6409 " template parameter %qD is not a parameter pack, but "
6410 "argument %qD is",
6411 parm, arg);
6412 return unify_invalid (explain_p);
6413 }
6414
6415 static int
6416 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6417 {
6418 if (explain_p)
6419 inform (input_location,
6420 " template argument %qE does not match "
6421 "pointer-to-member constant %qE",
6422 arg, parm);
6423 return unify_invalid (explain_p);
6424 }
6425
6426 static int
6427 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6428 {
6429 if (explain_p)
6430 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6431 return unify_invalid (explain_p);
6432 }
6433
6434 static int
6435 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6436 {
6437 if (explain_p)
6438 inform (input_location,
6439 " inconsistent parameter pack deduction with %qT and %qT",
6440 old_arg, new_arg);
6441 return unify_invalid (explain_p);
6442 }
6443
6444 static int
6445 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6446 {
6447 if (explain_p)
6448 {
6449 if (TYPE_P (parm))
6450 inform (input_location,
6451 " deduced conflicting types for parameter %qT (%qT and %qT)",
6452 parm, first, second);
6453 else
6454 inform (input_location,
6455 " deduced conflicting values for non-type parameter "
6456 "%qE (%qE and %qE)", parm, first, second);
6457 }
6458 return unify_invalid (explain_p);
6459 }
6460
6461 static int
6462 unify_vla_arg (bool explain_p, tree arg)
6463 {
6464 if (explain_p)
6465 inform (input_location,
6466 " variable-sized array type %qT is not "
6467 "a valid template argument",
6468 arg);
6469 return unify_invalid (explain_p);
6470 }
6471
6472 static int
6473 unify_method_type_error (bool explain_p, tree arg)
6474 {
6475 if (explain_p)
6476 inform (input_location,
6477 " member function type %qT is not a valid template argument",
6478 arg);
6479 return unify_invalid (explain_p);
6480 }
6481
6482 static int
6483 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6484 {
6485 if (explain_p)
6486 {
6487 if (least_p)
6488 inform_n (input_location, wanted,
6489 " candidate expects at least %d argument, %d provided",
6490 " candidate expects at least %d arguments, %d provided",
6491 wanted, have);
6492 else
6493 inform_n (input_location, wanted,
6494 " candidate expects %d argument, %d provided",
6495 " candidate expects %d arguments, %d provided",
6496 wanted, have);
6497 }
6498 return unify_invalid (explain_p);
6499 }
6500
6501 static int
6502 unify_too_many_arguments (bool explain_p, int have, int wanted)
6503 {
6504 return unify_arity (explain_p, have, wanted);
6505 }
6506
6507 static int
6508 unify_too_few_arguments (bool explain_p, int have, int wanted,
6509 bool least_p = false)
6510 {
6511 return unify_arity (explain_p, have, wanted, least_p);
6512 }
6513
6514 static int
6515 unify_arg_conversion (bool explain_p, tree to_type,
6516 tree from_type, tree arg)
6517 {
6518 if (explain_p)
6519 inform (EXPR_LOC_OR_LOC (arg, input_location),
6520 " cannot convert %qE (type %qT) to type %qT",
6521 arg, from_type, to_type);
6522 return unify_invalid (explain_p);
6523 }
6524
6525 static int
6526 unify_no_common_base (bool explain_p, enum template_base_result r,
6527 tree parm, tree arg)
6528 {
6529 if (explain_p)
6530 switch (r)
6531 {
6532 case tbr_ambiguous_baseclass:
6533 inform (input_location, " %qT is an ambiguous base class of %qT",
6534 parm, arg);
6535 break;
6536 default:
6537 inform (input_location, " %qT is not derived from %qT", arg, parm);
6538 break;
6539 }
6540 return unify_invalid (explain_p);
6541 }
6542
6543 static int
6544 unify_inconsistent_template_template_parameters (bool explain_p)
6545 {
6546 if (explain_p)
6547 inform (input_location,
6548 " template parameters of a template template argument are "
6549 "inconsistent with other deduced template arguments");
6550 return unify_invalid (explain_p);
6551 }
6552
6553 static int
6554 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6555 {
6556 if (explain_p)
6557 inform (input_location,
6558 " can't deduce a template for %qT from non-template type %qT",
6559 parm, arg);
6560 return unify_invalid (explain_p);
6561 }
6562
6563 static int
6564 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6565 {
6566 if (explain_p)
6567 inform (input_location,
6568 " template argument %qE does not match %qE", arg, parm);
6569 return unify_invalid (explain_p);
6570 }
6571
6572 /* Attempt to convert the non-type template parameter EXPR to the
6573 indicated TYPE. If the conversion is successful, return the
6574 converted value. If the conversion is unsuccessful, return
6575 NULL_TREE if we issued an error message, or error_mark_node if we
6576 did not. We issue error messages for out-and-out bad template
6577 parameters, but not simply because the conversion failed, since we
6578 might be just trying to do argument deduction. Both TYPE and EXPR
6579 must be non-dependent.
6580
6581 The conversion follows the special rules described in
6582 [temp.arg.nontype], and it is much more strict than an implicit
6583 conversion.
6584
6585 This function is called twice for each template argument (see
6586 lookup_template_class for a more accurate description of this
6587 problem). This means that we need to handle expressions which
6588 are not valid in a C++ source, but can be created from the
6589 first call (for instance, casts to perform conversions). These
6590 hacks can go away after we fix the double coercion problem. */
6591
6592 static tree
6593 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6594 {
6595 tree expr_type;
6596 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6597 tree orig_expr = expr;
6598
6599 /* Detect immediately string literals as invalid non-type argument.
6600 This special-case is not needed for correctness (we would easily
6601 catch this later), but only to provide better diagnostic for this
6602 common user mistake. As suggested by DR 100, we do not mention
6603 linkage issues in the diagnostic as this is not the point. */
6604 /* FIXME we're making this OK. */
6605 if (TREE_CODE (expr) == STRING_CST)
6606 {
6607 if (complain & tf_error)
6608 error ("%qE is not a valid template argument for type %qT "
6609 "because string literals can never be used in this context",
6610 expr, type);
6611 return NULL_TREE;
6612 }
6613
6614 /* Add the ADDR_EXPR now for the benefit of
6615 value_dependent_expression_p. */
6616 if (TYPE_PTROBV_P (type)
6617 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6618 {
6619 expr = decay_conversion (expr, complain);
6620 if (expr == error_mark_node)
6621 return error_mark_node;
6622 }
6623
6624 /* If we are in a template, EXPR may be non-dependent, but still
6625 have a syntactic, rather than semantic, form. For example, EXPR
6626 might be a SCOPE_REF, rather than the VAR_DECL to which the
6627 SCOPE_REF refers. Preserving the qualifying scope is necessary
6628 so that access checking can be performed when the template is
6629 instantiated -- but here we need the resolved form so that we can
6630 convert the argument. */
6631 bool non_dep = false;
6632 if (TYPE_REF_OBJ_P (type)
6633 && has_value_dependent_address (expr))
6634 /* If we want the address and it's value-dependent, don't fold. */;
6635 else if (processing_template_decl
6636 && is_nondependent_constant_expression (expr))
6637 non_dep = true;
6638 if (error_operand_p (expr))
6639 return error_mark_node;
6640 expr_type = TREE_TYPE (expr);
6641
6642 /* If the argument is non-dependent, perform any conversions in
6643 non-dependent context as well. */
6644 processing_template_decl_sentinel s (non_dep);
6645 if (non_dep)
6646 expr = instantiate_non_dependent_expr_internal (expr, complain);
6647
6648 if (value_dependent_expression_p (expr))
6649 expr = canonicalize_expr_argument (expr, complain);
6650
6651 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6652 to a non-type argument of "nullptr". */
6653 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6654 expr = fold_simple (convert (type, expr));
6655
6656 /* In C++11, integral or enumeration non-type template arguments can be
6657 arbitrary constant expressions. Pointer and pointer to
6658 member arguments can be general constant expressions that evaluate
6659 to a null value, but otherwise still need to be of a specific form. */
6660 if (cxx_dialect >= cxx11)
6661 {
6662 if (TREE_CODE (expr) == PTRMEM_CST)
6663 /* A PTRMEM_CST is already constant, and a valid template
6664 argument for a parameter of pointer to member type, we just want
6665 to leave it in that form rather than lower it to a
6666 CONSTRUCTOR. */;
6667 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6668 || cxx_dialect >= cxx17)
6669 {
6670 /* C++17: A template-argument for a non-type template-parameter shall
6671 be a converted constant expression (8.20) of the type of the
6672 template-parameter. */
6673 expr = build_converted_constant_expr (type, expr, complain);
6674 if (expr == error_mark_node)
6675 return error_mark_node;
6676 expr = maybe_constant_value (expr);
6677 expr = convert_from_reference (expr);
6678 }
6679 else if (TYPE_PTR_OR_PTRMEM_P (type))
6680 {
6681 tree folded = maybe_constant_value (expr);
6682 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6683 : null_member_pointer_value_p (folded))
6684 expr = folded;
6685 }
6686 }
6687
6688 if (TREE_CODE (type) == REFERENCE_TYPE)
6689 expr = mark_lvalue_use (expr);
6690 else
6691 expr = mark_rvalue_use (expr);
6692
6693 /* HACK: Due to double coercion, we can get a
6694 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6695 which is the tree that we built on the first call (see
6696 below when coercing to reference to object or to reference to
6697 function). We just strip everything and get to the arg.
6698 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6699 for examples. */
6700 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6701 {
6702 tree probe_type, probe = expr;
6703 if (REFERENCE_REF_P (probe))
6704 probe = TREE_OPERAND (probe, 0);
6705 probe_type = TREE_TYPE (probe);
6706 if (TREE_CODE (probe) == NOP_EXPR)
6707 {
6708 /* ??? Maybe we could use convert_from_reference here, but we
6709 would need to relax its constraints because the NOP_EXPR
6710 could actually change the type to something more cv-qualified,
6711 and this is not folded by convert_from_reference. */
6712 tree addr = TREE_OPERAND (probe, 0);
6713 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6714 && TREE_CODE (addr) == ADDR_EXPR
6715 && TYPE_PTR_P (TREE_TYPE (addr))
6716 && (same_type_ignoring_top_level_qualifiers_p
6717 (TREE_TYPE (probe_type),
6718 TREE_TYPE (TREE_TYPE (addr)))))
6719 {
6720 expr = TREE_OPERAND (addr, 0);
6721 expr_type = TREE_TYPE (probe_type);
6722 }
6723 }
6724 }
6725
6726 /* [temp.arg.nontype]/5, bullet 1
6727
6728 For a non-type template-parameter of integral or enumeration type,
6729 integral promotions (_conv.prom_) and integral conversions
6730 (_conv.integral_) are applied. */
6731 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6732 {
6733 if (cxx_dialect < cxx11)
6734 {
6735 tree t = build_converted_constant_expr (type, expr, complain);
6736 t = maybe_constant_value (t);
6737 if (t != error_mark_node)
6738 expr = t;
6739 }
6740
6741 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6742 return error_mark_node;
6743
6744 /* Notice that there are constant expressions like '4 % 0' which
6745 do not fold into integer constants. */
6746 if (TREE_CODE (expr) != INTEGER_CST
6747 && !value_dependent_expression_p (expr))
6748 {
6749 if (complain & tf_error)
6750 {
6751 int errs = errorcount, warns = warningcount + werrorcount;
6752 if (!require_potential_constant_expression (expr))
6753 expr = error_mark_node;
6754 else
6755 expr = cxx_constant_value (expr);
6756 if (errorcount > errs || warningcount + werrorcount > warns)
6757 inform (loc, "in template argument for type %qT", type);
6758 if (expr == error_mark_node)
6759 return NULL_TREE;
6760 /* else cxx_constant_value complained but gave us
6761 a real constant, so go ahead. */
6762 if (TREE_CODE (expr) != INTEGER_CST)
6763 {
6764 /* Some assemble time constant expressions like
6765 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6766 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6767 as we can emit them into .rodata initializers of
6768 variables, yet they can't fold into an INTEGER_CST at
6769 compile time. Refuse them here. */
6770 gcc_checking_assert (reduced_constant_expression_p (expr));
6771 error_at (loc, "template argument %qE for type %qT not "
6772 "a constant integer", expr, type);
6773 return NULL_TREE;
6774 }
6775 }
6776 else
6777 return NULL_TREE;
6778 }
6779
6780 /* Avoid typedef problems. */
6781 if (TREE_TYPE (expr) != type)
6782 expr = fold_convert (type, expr);
6783 }
6784 /* [temp.arg.nontype]/5, bullet 2
6785
6786 For a non-type template-parameter of type pointer to object,
6787 qualification conversions (_conv.qual_) and the array-to-pointer
6788 conversion (_conv.array_) are applied. */
6789 else if (TYPE_PTROBV_P (type))
6790 {
6791 tree decayed = expr;
6792
6793 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6794 decay_conversion or an explicit cast. If it's a problematic cast,
6795 we'll complain about it below. */
6796 if (TREE_CODE (expr) == NOP_EXPR)
6797 {
6798 tree probe = expr;
6799 STRIP_NOPS (probe);
6800 if (TREE_CODE (probe) == ADDR_EXPR
6801 && TYPE_PTR_P (TREE_TYPE (probe)))
6802 {
6803 expr = probe;
6804 expr_type = TREE_TYPE (expr);
6805 }
6806 }
6807
6808 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6809
6810 A template-argument for a non-type, non-template template-parameter
6811 shall be one of: [...]
6812
6813 -- the name of a non-type template-parameter;
6814 -- the address of an object or function with external linkage, [...]
6815 expressed as "& id-expression" where the & is optional if the name
6816 refers to a function or array, or if the corresponding
6817 template-parameter is a reference.
6818
6819 Here, we do not care about functions, as they are invalid anyway
6820 for a parameter of type pointer-to-object. */
6821
6822 if (value_dependent_expression_p (expr))
6823 /* Non-type template parameters are OK. */
6824 ;
6825 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6826 /* Null pointer values are OK in C++11. */;
6827 else if (TREE_CODE (expr) != ADDR_EXPR)
6828 {
6829 if (VAR_P (expr))
6830 {
6831 if (complain & tf_error)
6832 error ("%qD is not a valid template argument "
6833 "because %qD is a variable, not the address of "
6834 "a variable", orig_expr, expr);
6835 return NULL_TREE;
6836 }
6837 if (POINTER_TYPE_P (expr_type))
6838 {
6839 if (complain & tf_error)
6840 error ("%qE is not a valid template argument for %qT "
6841 "because it is not the address of a variable",
6842 orig_expr, type);
6843 return NULL_TREE;
6844 }
6845 /* Other values, like integer constants, might be valid
6846 non-type arguments of some other type. */
6847 return error_mark_node;
6848 }
6849 else
6850 {
6851 tree decl = TREE_OPERAND (expr, 0);
6852
6853 if (!VAR_P (decl))
6854 {
6855 if (complain & tf_error)
6856 error ("%qE is not a valid template argument of type %qT "
6857 "because %qE is not a variable", orig_expr, type, decl);
6858 return NULL_TREE;
6859 }
6860 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6861 {
6862 if (complain & tf_error)
6863 error ("%qE is not a valid template argument of type %qT "
6864 "because %qD does not have external linkage",
6865 orig_expr, type, decl);
6866 return NULL_TREE;
6867 }
6868 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6869 && decl_linkage (decl) == lk_none)
6870 {
6871 if (complain & tf_error)
6872 error ("%qE is not a valid template argument of type %qT "
6873 "because %qD has no linkage", orig_expr, type, decl);
6874 return NULL_TREE;
6875 }
6876 /* C++17: For a non-type template-parameter of reference or pointer
6877 type, the value of the constant expression shall not refer to (or
6878 for a pointer type, shall not be the address of):
6879 * a subobject (4.5),
6880 * a temporary object (15.2),
6881 * a string literal (5.13.5),
6882 * the result of a typeid expression (8.2.8), or
6883 * a predefined __func__ variable (11.4.1). */
6884 else if (DECL_ARTIFICIAL (decl))
6885 {
6886 if (complain & tf_error)
6887 error ("the address of %qD is not a valid template argument",
6888 decl);
6889 return NULL_TREE;
6890 }
6891 else if (!same_type_ignoring_top_level_qualifiers_p
6892 (strip_array_types (TREE_TYPE (type)),
6893 strip_array_types (TREE_TYPE (decl))))
6894 {
6895 if (complain & tf_error)
6896 error ("the address of the %qT subobject of %qD is not a "
6897 "valid template argument", TREE_TYPE (type), decl);
6898 return NULL_TREE;
6899 }
6900 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6901 {
6902 if (complain & tf_error)
6903 error ("the address of %qD is not a valid template argument "
6904 "because it does not have static storage duration",
6905 decl);
6906 return NULL_TREE;
6907 }
6908 }
6909
6910 expr = decayed;
6911
6912 expr = perform_qualification_conversions (type, expr);
6913 if (expr == error_mark_node)
6914 return error_mark_node;
6915 }
6916 /* [temp.arg.nontype]/5, bullet 3
6917
6918 For a non-type template-parameter of type reference to object, no
6919 conversions apply. The type referred to by the reference may be more
6920 cv-qualified than the (otherwise identical) type of the
6921 template-argument. The template-parameter is bound directly to the
6922 template-argument, which must be an lvalue. */
6923 else if (TYPE_REF_OBJ_P (type))
6924 {
6925 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6926 expr_type))
6927 return error_mark_node;
6928
6929 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6930 {
6931 if (complain & tf_error)
6932 error ("%qE is not a valid template argument for type %qT "
6933 "because of conflicts in cv-qualification", expr, type);
6934 return NULL_TREE;
6935 }
6936
6937 if (!lvalue_p (expr))
6938 {
6939 if (complain & tf_error)
6940 error ("%qE is not a valid template argument for type %qT "
6941 "because it is not an lvalue", expr, type);
6942 return NULL_TREE;
6943 }
6944
6945 /* [temp.arg.nontype]/1
6946
6947 A template-argument for a non-type, non-template template-parameter
6948 shall be one of: [...]
6949
6950 -- the address of an object or function with external linkage. */
6951 if (INDIRECT_REF_P (expr)
6952 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6953 {
6954 expr = TREE_OPERAND (expr, 0);
6955 if (DECL_P (expr))
6956 {
6957 if (complain & tf_error)
6958 error ("%q#D is not a valid template argument for type %qT "
6959 "because a reference variable does not have a constant "
6960 "address", expr, type);
6961 return NULL_TREE;
6962 }
6963 }
6964
6965 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6966 && value_dependent_expression_p (expr))
6967 /* OK, dependent reference. We don't want to ask whether a DECL is
6968 itself value-dependent, since what we want here is its address. */;
6969 else
6970 {
6971 if (!DECL_P (expr))
6972 {
6973 if (complain & tf_error)
6974 error ("%qE is not a valid template argument for type %qT "
6975 "because it is not an object with linkage",
6976 expr, type);
6977 return NULL_TREE;
6978 }
6979
6980 /* DR 1155 allows internal linkage in C++11 and up. */
6981 linkage_kind linkage = decl_linkage (expr);
6982 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6983 {
6984 if (complain & tf_error)
6985 error ("%qE is not a valid template argument for type %qT "
6986 "because object %qD does not have linkage",
6987 expr, type, expr);
6988 return NULL_TREE;
6989 }
6990
6991 expr = build_address (expr);
6992 }
6993
6994 if (!same_type_p (type, TREE_TYPE (expr)))
6995 expr = build_nop (type, expr);
6996 }
6997 /* [temp.arg.nontype]/5, bullet 4
6998
6999 For a non-type template-parameter of type pointer to function, only
7000 the function-to-pointer conversion (_conv.func_) is applied. If the
7001 template-argument represents a set of overloaded functions (or a
7002 pointer to such), the matching function is selected from the set
7003 (_over.over_). */
7004 else if (TYPE_PTRFN_P (type))
7005 {
7006 /* If the argument is a template-id, we might not have enough
7007 context information to decay the pointer. */
7008 if (!type_unknown_p (expr_type))
7009 {
7010 expr = decay_conversion (expr, complain);
7011 if (expr == error_mark_node)
7012 return error_mark_node;
7013 }
7014
7015 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7016 /* Null pointer values are OK in C++11. */
7017 return perform_qualification_conversions (type, expr);
7018
7019 expr = convert_nontype_argument_function (type, expr, complain);
7020 if (!expr || expr == error_mark_node)
7021 return expr;
7022 }
7023 /* [temp.arg.nontype]/5, bullet 5
7024
7025 For a non-type template-parameter of type reference to function, no
7026 conversions apply. If the template-argument represents a set of
7027 overloaded functions, the matching function is selected from the set
7028 (_over.over_). */
7029 else if (TYPE_REFFN_P (type))
7030 {
7031 if (TREE_CODE (expr) == ADDR_EXPR)
7032 {
7033 if (complain & tf_error)
7034 {
7035 error ("%qE is not a valid template argument for type %qT "
7036 "because it is a pointer", expr, type);
7037 inform (input_location, "try using %qE instead",
7038 TREE_OPERAND (expr, 0));
7039 }
7040 return NULL_TREE;
7041 }
7042
7043 expr = convert_nontype_argument_function (type, expr, complain);
7044 if (!expr || expr == error_mark_node)
7045 return expr;
7046 }
7047 /* [temp.arg.nontype]/5, bullet 6
7048
7049 For a non-type template-parameter of type pointer to member function,
7050 no conversions apply. If the template-argument represents a set of
7051 overloaded member functions, the matching member function is selected
7052 from the set (_over.over_). */
7053 else if (TYPE_PTRMEMFUNC_P (type))
7054 {
7055 expr = instantiate_type (type, expr, tf_none);
7056 if (expr == error_mark_node)
7057 return error_mark_node;
7058
7059 /* [temp.arg.nontype] bullet 1 says the pointer to member
7060 expression must be a pointer-to-member constant. */
7061 if (!value_dependent_expression_p (expr)
7062 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7063 return NULL_TREE;
7064
7065 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7066 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7067 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7068 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7069 }
7070 /* [temp.arg.nontype]/5, bullet 7
7071
7072 For a non-type template-parameter of type pointer to data member,
7073 qualification conversions (_conv.qual_) are applied. */
7074 else if (TYPE_PTRDATAMEM_P (type))
7075 {
7076 /* [temp.arg.nontype] bullet 1 says the pointer to member
7077 expression must be a pointer-to-member constant. */
7078 if (!value_dependent_expression_p (expr)
7079 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7080 return NULL_TREE;
7081
7082 expr = perform_qualification_conversions (type, expr);
7083 if (expr == error_mark_node)
7084 return expr;
7085 }
7086 else if (NULLPTR_TYPE_P (type))
7087 {
7088 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7089 {
7090 if (complain & tf_error)
7091 error ("%qE is not a valid template argument for type %qT "
7092 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7093 return NULL_TREE;
7094 }
7095 return expr;
7096 }
7097 /* A template non-type parameter must be one of the above. */
7098 else
7099 gcc_unreachable ();
7100
7101 /* Sanity check: did we actually convert the argument to the
7102 right type? */
7103 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7104 (type, TREE_TYPE (expr)));
7105 return convert_from_reference (expr);
7106 }
7107
7108 /* Subroutine of coerce_template_template_parms, which returns 1 if
7109 PARM_PARM and ARG_PARM match using the rule for the template
7110 parameters of template template parameters. Both PARM and ARG are
7111 template parameters; the rest of the arguments are the same as for
7112 coerce_template_template_parms.
7113 */
7114 static int
7115 coerce_template_template_parm (tree parm,
7116 tree arg,
7117 tsubst_flags_t complain,
7118 tree in_decl,
7119 tree outer_args)
7120 {
7121 if (arg == NULL_TREE || error_operand_p (arg)
7122 || parm == NULL_TREE || error_operand_p (parm))
7123 return 0;
7124
7125 if (TREE_CODE (arg) != TREE_CODE (parm))
7126 return 0;
7127
7128 switch (TREE_CODE (parm))
7129 {
7130 case TEMPLATE_DECL:
7131 /* We encounter instantiations of templates like
7132 template <template <template <class> class> class TT>
7133 class C; */
7134 {
7135 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7136 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7137
7138 if (!coerce_template_template_parms
7139 (parmparm, argparm, complain, in_decl, outer_args))
7140 return 0;
7141 }
7142 /* Fall through. */
7143
7144 case TYPE_DECL:
7145 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7146 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7147 /* Argument is a parameter pack but parameter is not. */
7148 return 0;
7149 break;
7150
7151 case PARM_DECL:
7152 /* The tsubst call is used to handle cases such as
7153
7154 template <int> class C {};
7155 template <class T, template <T> class TT> class D {};
7156 D<int, C> d;
7157
7158 i.e. the parameter list of TT depends on earlier parameters. */
7159 if (!uses_template_parms (TREE_TYPE (arg)))
7160 {
7161 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7162 if (!uses_template_parms (t)
7163 && !same_type_p (t, TREE_TYPE (arg)))
7164 return 0;
7165 }
7166
7167 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7168 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7169 /* Argument is a parameter pack but parameter is not. */
7170 return 0;
7171
7172 break;
7173
7174 default:
7175 gcc_unreachable ();
7176 }
7177
7178 return 1;
7179 }
7180
7181 /* Coerce template argument list ARGLIST for use with template
7182 template-parameter TEMPL. */
7183
7184 static tree
7185 coerce_template_args_for_ttp (tree templ, tree arglist,
7186 tsubst_flags_t complain)
7187 {
7188 /* Consider an example where a template template parameter declared as
7189
7190 template <class T, class U = std::allocator<T> > class TT
7191
7192 The template parameter level of T and U are one level larger than
7193 of TT. To proper process the default argument of U, say when an
7194 instantiation `TT<int>' is seen, we need to build the full
7195 arguments containing {int} as the innermost level. Outer levels,
7196 available when not appearing as default template argument, can be
7197 obtained from the arguments of the enclosing template.
7198
7199 Suppose that TT is later substituted with std::vector. The above
7200 instantiation is `TT<int, std::allocator<T> >' with TT at
7201 level 1, and T at level 2, while the template arguments at level 1
7202 becomes {std::vector} and the inner level 2 is {int}. */
7203
7204 tree outer = DECL_CONTEXT (templ);
7205 if (outer)
7206 {
7207 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7208 /* We want arguments for the partial specialization, not arguments for
7209 the primary template. */
7210 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7211 else
7212 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7213 }
7214 else if (current_template_parms)
7215 {
7216 /* This is an argument of the current template, so we haven't set
7217 DECL_CONTEXT yet. */
7218 tree relevant_template_parms;
7219
7220 /* Parameter levels that are greater than the level of the given
7221 template template parm are irrelevant. */
7222 relevant_template_parms = current_template_parms;
7223 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7224 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7225 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7226
7227 outer = template_parms_to_args (relevant_template_parms);
7228 }
7229
7230 if (outer)
7231 arglist = add_to_template_args (outer, arglist);
7232
7233 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7234 return coerce_template_parms (parmlist, arglist, templ,
7235 complain,
7236 /*require_all_args=*/true,
7237 /*use_default_args=*/true);
7238 }
7239
7240 /* A cache of template template parameters with match-all default
7241 arguments. */
7242 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7243 static void
7244 store_defaulted_ttp (tree v, tree t)
7245 {
7246 if (!defaulted_ttp_cache)
7247 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7248 defaulted_ttp_cache->put (v, t);
7249 }
7250 static tree
7251 lookup_defaulted_ttp (tree v)
7252 {
7253 if (defaulted_ttp_cache)
7254 if (tree *p = defaulted_ttp_cache->get (v))
7255 return *p;
7256 return NULL_TREE;
7257 }
7258
7259 /* T is a bound template template-parameter. Copy its arguments into default
7260 arguments of the template template-parameter's template parameters. */
7261
7262 static tree
7263 add_defaults_to_ttp (tree otmpl)
7264 {
7265 if (tree c = lookup_defaulted_ttp (otmpl))
7266 return c;
7267
7268 tree ntmpl = copy_node (otmpl);
7269
7270 tree ntype = copy_node (TREE_TYPE (otmpl));
7271 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7272 TYPE_MAIN_VARIANT (ntype) = ntype;
7273 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7274 TYPE_NAME (ntype) = ntmpl;
7275 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7276
7277 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7278 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7279 TEMPLATE_PARM_DECL (idx) = ntmpl;
7280 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7281
7282 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7283 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7284 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7285 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7286 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7287 {
7288 tree o = TREE_VEC_ELT (vec, i);
7289 if (!template_parameter_pack_p (TREE_VALUE (o)))
7290 {
7291 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7292 TREE_PURPOSE (n) = any_targ_node;
7293 }
7294 }
7295
7296 store_defaulted_ttp (otmpl, ntmpl);
7297 return ntmpl;
7298 }
7299
7300 /* ARG is a bound potential template template-argument, and PARGS is a list
7301 of arguments for the corresponding template template-parameter. Adjust
7302 PARGS as appropriate for application to ARG's template, and if ARG is a
7303 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7304 arguments to the template template parameter. */
7305
7306 static tree
7307 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7308 {
7309 ++processing_template_decl;
7310 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7311 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7312 {
7313 /* When comparing two template template-parameters in partial ordering,
7314 rewrite the one currently being used as an argument to have default
7315 arguments for all parameters. */
7316 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7317 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7318 if (pargs != error_mark_node)
7319 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7320 TYPE_TI_ARGS (arg));
7321 }
7322 else
7323 {
7324 tree aparms
7325 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7326 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7327 /*require_all*/true,
7328 /*use_default*/true);
7329 }
7330 --processing_template_decl;
7331 return pargs;
7332 }
7333
7334 /* Subroutine of unify for the case when PARM is a
7335 BOUND_TEMPLATE_TEMPLATE_PARM. */
7336
7337 static int
7338 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7339 bool explain_p)
7340 {
7341 tree parmvec = TYPE_TI_ARGS (parm);
7342 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7343
7344 /* The template template parm might be variadic and the argument
7345 not, so flatten both argument lists. */
7346 parmvec = expand_template_argument_pack (parmvec);
7347 argvec = expand_template_argument_pack (argvec);
7348
7349 if (flag_new_ttp)
7350 {
7351 /* In keeping with P0522R0, adjust P's template arguments
7352 to apply to A's template; then flatten it again. */
7353 tree nparmvec = parmvec;
7354 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7355 nparmvec = expand_template_argument_pack (nparmvec);
7356
7357 if (unify (tparms, targs, nparmvec, argvec,
7358 UNIFY_ALLOW_NONE, explain_p))
7359 return 1;
7360
7361 /* If the P0522 adjustment eliminated a pack expansion, deduce
7362 empty packs. */
7363 if (flag_new_ttp
7364 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7365 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7366 DEDUCE_EXACT, /*sub*/true, explain_p))
7367 return 1;
7368 }
7369 else
7370 {
7371 /* Deduce arguments T, i from TT<T> or TT<i>.
7372 We check each element of PARMVEC and ARGVEC individually
7373 rather than the whole TREE_VEC since they can have
7374 different number of elements, which is allowed under N2555. */
7375
7376 int len = TREE_VEC_LENGTH (parmvec);
7377
7378 /* Check if the parameters end in a pack, making them
7379 variadic. */
7380 int parm_variadic_p = 0;
7381 if (len > 0
7382 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7383 parm_variadic_p = 1;
7384
7385 for (int i = 0; i < len - parm_variadic_p; ++i)
7386 /* If the template argument list of P contains a pack
7387 expansion that is not the last template argument, the
7388 entire template argument list is a non-deduced
7389 context. */
7390 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7391 return unify_success (explain_p);
7392
7393 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7394 return unify_too_few_arguments (explain_p,
7395 TREE_VEC_LENGTH (argvec), len);
7396
7397 for (int i = 0; i < len - parm_variadic_p; ++i)
7398 if (unify (tparms, targs,
7399 TREE_VEC_ELT (parmvec, i),
7400 TREE_VEC_ELT (argvec, i),
7401 UNIFY_ALLOW_NONE, explain_p))
7402 return 1;
7403
7404 if (parm_variadic_p
7405 && unify_pack_expansion (tparms, targs,
7406 parmvec, argvec,
7407 DEDUCE_EXACT,
7408 /*subr=*/true, explain_p))
7409 return 1;
7410 }
7411
7412 return 0;
7413 }
7414
7415 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7416 template template parameters. Both PARM_PARMS and ARG_PARMS are
7417 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7418 or PARM_DECL.
7419
7420 Consider the example:
7421 template <class T> class A;
7422 template<template <class U> class TT> class B;
7423
7424 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7425 the parameters to A, and OUTER_ARGS contains A. */
7426
7427 static int
7428 coerce_template_template_parms (tree parm_parms,
7429 tree arg_parms,
7430 tsubst_flags_t complain,
7431 tree in_decl,
7432 tree outer_args)
7433 {
7434 int nparms, nargs, i;
7435 tree parm, arg;
7436 int variadic_p = 0;
7437
7438 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7439 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7440
7441 nparms = TREE_VEC_LENGTH (parm_parms);
7442 nargs = TREE_VEC_LENGTH (arg_parms);
7443
7444 if (flag_new_ttp)
7445 {
7446 /* P0522R0: A template template-parameter P is at least as specialized as
7447 a template template-argument A if, given the following rewrite to two
7448 function templates, the function template corresponding to P is at
7449 least as specialized as the function template corresponding to A
7450 according to the partial ordering rules for function templates
7451 ([temp.func.order]). Given an invented class template X with the
7452 template parameter list of A (including default arguments):
7453
7454 * Each of the two function templates has the same template parameters,
7455 respectively, as P or A.
7456
7457 * Each function template has a single function parameter whose type is
7458 a specialization of X with template arguments corresponding to the
7459 template parameters from the respective function template where, for
7460 each template parameter PP in the template parameter list of the
7461 function template, a corresponding template argument AA is formed. If
7462 PP declares a parameter pack, then AA is the pack expansion
7463 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7464
7465 If the rewrite produces an invalid type, then P is not at least as
7466 specialized as A. */
7467
7468 /* So coerce P's args to apply to A's parms, and then deduce between A's
7469 args and the converted args. If that succeeds, A is at least as
7470 specialized as P, so they match.*/
7471 tree pargs = template_parms_level_to_args (parm_parms);
7472 ++processing_template_decl;
7473 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7474 /*require_all*/true, /*use_default*/true);
7475 --processing_template_decl;
7476 if (pargs != error_mark_node)
7477 {
7478 tree targs = make_tree_vec (nargs);
7479 tree aargs = template_parms_level_to_args (arg_parms);
7480 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7481 /*explain*/false))
7482 return 1;
7483 }
7484 }
7485
7486 /* Determine whether we have a parameter pack at the end of the
7487 template template parameter's template parameter list. */
7488 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7489 {
7490 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7491
7492 if (error_operand_p (parm))
7493 return 0;
7494
7495 switch (TREE_CODE (parm))
7496 {
7497 case TEMPLATE_DECL:
7498 case TYPE_DECL:
7499 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7500 variadic_p = 1;
7501 break;
7502
7503 case PARM_DECL:
7504 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7505 variadic_p = 1;
7506 break;
7507
7508 default:
7509 gcc_unreachable ();
7510 }
7511 }
7512
7513 if (nargs != nparms
7514 && !(variadic_p && nargs >= nparms - 1))
7515 return 0;
7516
7517 /* Check all of the template parameters except the parameter pack at
7518 the end (if any). */
7519 for (i = 0; i < nparms - variadic_p; ++i)
7520 {
7521 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7522 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7523 continue;
7524
7525 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7526 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7527
7528 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7529 outer_args))
7530 return 0;
7531
7532 }
7533
7534 if (variadic_p)
7535 {
7536 /* Check each of the template parameters in the template
7537 argument against the template parameter pack at the end of
7538 the template template parameter. */
7539 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7540 return 0;
7541
7542 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7543
7544 for (; i < nargs; ++i)
7545 {
7546 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7547 continue;
7548
7549 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7550
7551 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7552 outer_args))
7553 return 0;
7554 }
7555 }
7556
7557 return 1;
7558 }
7559
7560 /* Verifies that the deduced template arguments (in TARGS) for the
7561 template template parameters (in TPARMS) represent valid bindings,
7562 by comparing the template parameter list of each template argument
7563 to the template parameter list of its corresponding template
7564 template parameter, in accordance with DR150. This
7565 routine can only be called after all template arguments have been
7566 deduced. It will return TRUE if all of the template template
7567 parameter bindings are okay, FALSE otherwise. */
7568 bool
7569 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7570 {
7571 int i, ntparms = TREE_VEC_LENGTH (tparms);
7572 bool ret = true;
7573
7574 /* We're dealing with template parms in this process. */
7575 ++processing_template_decl;
7576
7577 targs = INNERMOST_TEMPLATE_ARGS (targs);
7578
7579 for (i = 0; i < ntparms; ++i)
7580 {
7581 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7582 tree targ = TREE_VEC_ELT (targs, i);
7583
7584 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7585 {
7586 tree packed_args = NULL_TREE;
7587 int idx, len = 1;
7588
7589 if (ARGUMENT_PACK_P (targ))
7590 {
7591 /* Look inside the argument pack. */
7592 packed_args = ARGUMENT_PACK_ARGS (targ);
7593 len = TREE_VEC_LENGTH (packed_args);
7594 }
7595
7596 for (idx = 0; idx < len; ++idx)
7597 {
7598 tree targ_parms = NULL_TREE;
7599
7600 if (packed_args)
7601 /* Extract the next argument from the argument
7602 pack. */
7603 targ = TREE_VEC_ELT (packed_args, idx);
7604
7605 if (PACK_EXPANSION_P (targ))
7606 /* Look at the pattern of the pack expansion. */
7607 targ = PACK_EXPANSION_PATTERN (targ);
7608
7609 /* Extract the template parameters from the template
7610 argument. */
7611 if (TREE_CODE (targ) == TEMPLATE_DECL)
7612 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7613 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7614 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7615
7616 /* Verify that we can coerce the template template
7617 parameters from the template argument to the template
7618 parameter. This requires an exact match. */
7619 if (targ_parms
7620 && !coerce_template_template_parms
7621 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7622 targ_parms,
7623 tf_none,
7624 tparm,
7625 targs))
7626 {
7627 ret = false;
7628 goto out;
7629 }
7630 }
7631 }
7632 }
7633
7634 out:
7635
7636 --processing_template_decl;
7637 return ret;
7638 }
7639
7640 /* Since type attributes aren't mangled, we need to strip them from
7641 template type arguments. */
7642
7643 static tree
7644 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7645 {
7646 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7647 return arg;
7648 bool removed_attributes = false;
7649 tree canon = strip_typedefs (arg, &removed_attributes);
7650 if (removed_attributes
7651 && (complain & tf_warning))
7652 warning (OPT_Wignored_attributes,
7653 "ignoring attributes on template argument %qT", arg);
7654 return canon;
7655 }
7656
7657 /* And from inside dependent non-type arguments like sizeof(Type). */
7658
7659 static tree
7660 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7661 {
7662 if (!arg || arg == error_mark_node)
7663 return arg;
7664 bool removed_attributes = false;
7665 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7666 if (removed_attributes
7667 && (complain & tf_warning))
7668 warning (OPT_Wignored_attributes,
7669 "ignoring attributes in template argument %qE", arg);
7670 return canon;
7671 }
7672
7673 // A template declaration can be substituted for a constrained
7674 // template template parameter only when the argument is more
7675 // constrained than the parameter.
7676 static bool
7677 is_compatible_template_arg (tree parm, tree arg)
7678 {
7679 tree parm_cons = get_constraints (parm);
7680
7681 /* For now, allow constrained template template arguments
7682 and unconstrained template template parameters. */
7683 if (parm_cons == NULL_TREE)
7684 return true;
7685
7686 tree arg_cons = get_constraints (arg);
7687
7688 // If the template parameter is constrained, we need to rewrite its
7689 // constraints in terms of the ARG's template parameters. This ensures
7690 // that all of the template parameter types will have the same depth.
7691 //
7692 // Note that this is only valid when coerce_template_template_parm is
7693 // true for the innermost template parameters of PARM and ARG. In other
7694 // words, because coercion is successful, this conversion will be valid.
7695 if (parm_cons)
7696 {
7697 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7698 parm_cons = tsubst_constraint_info (parm_cons,
7699 INNERMOST_TEMPLATE_ARGS (args),
7700 tf_none, NULL_TREE);
7701 if (parm_cons == error_mark_node)
7702 return false;
7703 }
7704
7705 return subsumes (parm_cons, arg_cons);
7706 }
7707
7708 // Convert a placeholder argument into a binding to the original
7709 // parameter. The original parameter is saved as the TREE_TYPE of
7710 // ARG.
7711 static inline tree
7712 convert_wildcard_argument (tree parm, tree arg)
7713 {
7714 TREE_TYPE (arg) = parm;
7715 return arg;
7716 }
7717
7718 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7719 because one of them is dependent. But we need to represent the
7720 conversion for the benefit of cp_tree_equal. */
7721
7722 static tree
7723 maybe_convert_nontype_argument (tree type, tree arg)
7724 {
7725 /* Auto parms get no conversion. */
7726 if (type_uses_auto (type))
7727 return arg;
7728 /* We don't need or want to add this conversion now if we're going to use the
7729 argument for deduction. */
7730 if (value_dependent_expression_p (arg))
7731 return arg;
7732
7733 type = cv_unqualified (type);
7734 tree argtype = TREE_TYPE (arg);
7735 if (same_type_p (type, argtype))
7736 return arg;
7737
7738 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7739 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7740 return arg;
7741 }
7742
7743 /* Convert the indicated template ARG as necessary to match the
7744 indicated template PARM. Returns the converted ARG, or
7745 error_mark_node if the conversion was unsuccessful. Error and
7746 warning messages are issued under control of COMPLAIN. This
7747 conversion is for the Ith parameter in the parameter list. ARGS is
7748 the full set of template arguments deduced so far. */
7749
7750 static tree
7751 convert_template_argument (tree parm,
7752 tree arg,
7753 tree args,
7754 tsubst_flags_t complain,
7755 int i,
7756 tree in_decl)
7757 {
7758 tree orig_arg;
7759 tree val;
7760 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7761
7762 if (parm == error_mark_node)
7763 return error_mark_node;
7764
7765 /* Trivially convert placeholders. */
7766 if (TREE_CODE (arg) == WILDCARD_DECL)
7767 return convert_wildcard_argument (parm, arg);
7768
7769 if (arg == any_targ_node)
7770 return arg;
7771
7772 if (TREE_CODE (arg) == TREE_LIST
7773 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7774 {
7775 /* The template argument was the name of some
7776 member function. That's usually
7777 invalid, but static members are OK. In any
7778 case, grab the underlying fields/functions
7779 and issue an error later if required. */
7780 orig_arg = TREE_VALUE (arg);
7781 TREE_TYPE (arg) = unknown_type_node;
7782 }
7783
7784 orig_arg = arg;
7785
7786 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7787 requires_type = (TREE_CODE (parm) == TYPE_DECL
7788 || requires_tmpl_type);
7789
7790 /* When determining whether an argument pack expansion is a template,
7791 look at the pattern. */
7792 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7793 arg = PACK_EXPANSION_PATTERN (arg);
7794
7795 /* Deal with an injected-class-name used as a template template arg. */
7796 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7797 {
7798 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7799 if (TREE_CODE (t) == TEMPLATE_DECL)
7800 {
7801 if (cxx_dialect >= cxx11)
7802 /* OK under DR 1004. */;
7803 else if (complain & tf_warning_or_error)
7804 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7805 " used as template template argument", TYPE_NAME (arg));
7806 else if (flag_pedantic_errors)
7807 t = arg;
7808
7809 arg = t;
7810 }
7811 }
7812
7813 is_tmpl_type =
7814 ((TREE_CODE (arg) == TEMPLATE_DECL
7815 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7816 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7817 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7818 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7819
7820 if (is_tmpl_type
7821 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7822 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7823 arg = TYPE_STUB_DECL (arg);
7824
7825 is_type = TYPE_P (arg) || is_tmpl_type;
7826
7827 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7828 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7829 {
7830 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7831 {
7832 if (complain & tf_error)
7833 error ("invalid use of destructor %qE as a type", orig_arg);
7834 return error_mark_node;
7835 }
7836
7837 permerror (input_location,
7838 "to refer to a type member of a template parameter, "
7839 "use %<typename %E%>", orig_arg);
7840
7841 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7842 TREE_OPERAND (arg, 1),
7843 typename_type,
7844 complain);
7845 arg = orig_arg;
7846 is_type = 1;
7847 }
7848 if (is_type != requires_type)
7849 {
7850 if (in_decl)
7851 {
7852 if (complain & tf_error)
7853 {
7854 error ("type/value mismatch at argument %d in template "
7855 "parameter list for %qD",
7856 i + 1, in_decl);
7857 if (is_type)
7858 inform (input_location,
7859 " expected a constant of type %qT, got %qT",
7860 TREE_TYPE (parm),
7861 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7862 else if (requires_tmpl_type)
7863 inform (input_location,
7864 " expected a class template, got %qE", orig_arg);
7865 else
7866 inform (input_location,
7867 " expected a type, got %qE", orig_arg);
7868 }
7869 }
7870 return error_mark_node;
7871 }
7872 if (is_tmpl_type ^ requires_tmpl_type)
7873 {
7874 if (in_decl && (complain & tf_error))
7875 {
7876 error ("type/value mismatch at argument %d in template "
7877 "parameter list for %qD",
7878 i + 1, in_decl);
7879 if (is_tmpl_type)
7880 inform (input_location,
7881 " expected a type, got %qT", DECL_NAME (arg));
7882 else
7883 inform (input_location,
7884 " expected a class template, got %qT", orig_arg);
7885 }
7886 return error_mark_node;
7887 }
7888
7889 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7890 /* We already did the appropriate conversion when packing args. */
7891 val = orig_arg;
7892 else if (is_type)
7893 {
7894 if (requires_tmpl_type)
7895 {
7896 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7897 /* The number of argument required is not known yet.
7898 Just accept it for now. */
7899 val = orig_arg;
7900 else
7901 {
7902 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7903 tree argparm;
7904
7905 /* Strip alias templates that are equivalent to another
7906 template. */
7907 arg = get_underlying_template (arg);
7908 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7909
7910 if (coerce_template_template_parms (parmparm, argparm,
7911 complain, in_decl,
7912 args))
7913 {
7914 val = arg;
7915
7916 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7917 TEMPLATE_DECL. */
7918 if (val != error_mark_node)
7919 {
7920 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7921 val = TREE_TYPE (val);
7922 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7923 val = make_pack_expansion (val, complain);
7924 }
7925 }
7926 else
7927 {
7928 if (in_decl && (complain & tf_error))
7929 {
7930 error ("type/value mismatch at argument %d in "
7931 "template parameter list for %qD",
7932 i + 1, in_decl);
7933 inform (input_location,
7934 " expected a template of type %qD, got %qT",
7935 parm, orig_arg);
7936 }
7937
7938 val = error_mark_node;
7939 }
7940
7941 // Check that the constraints are compatible before allowing the
7942 // substitution.
7943 if (val != error_mark_node)
7944 if (!is_compatible_template_arg (parm, arg))
7945 {
7946 if (in_decl && (complain & tf_error))
7947 {
7948 error ("constraint mismatch at argument %d in "
7949 "template parameter list for %qD",
7950 i + 1, in_decl);
7951 inform (input_location, " expected %qD but got %qD",
7952 parm, arg);
7953 }
7954 val = error_mark_node;
7955 }
7956 }
7957 }
7958 else
7959 val = orig_arg;
7960 /* We only form one instance of each template specialization.
7961 Therefore, if we use a non-canonical variant (i.e., a
7962 typedef), any future messages referring to the type will use
7963 the typedef, which is confusing if those future uses do not
7964 themselves also use the typedef. */
7965 if (TYPE_P (val))
7966 val = canonicalize_type_argument (val, complain);
7967 }
7968 else
7969 {
7970 tree t = TREE_TYPE (parm);
7971
7972 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
7973 > TMPL_ARGS_DEPTH (args))
7974 /* We don't have enough levels of args to do any substitution. This
7975 can happen in the context of -fnew-ttp-matching. */;
7976 else if (tree a = type_uses_auto (t))
7977 {
7978 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7979 if (t == error_mark_node)
7980 return error_mark_node;
7981 }
7982 else
7983 t = tsubst (t, args, complain, in_decl);
7984
7985 if (invalid_nontype_parm_type_p (t, complain))
7986 return error_mark_node;
7987
7988 if (!type_dependent_expression_p (orig_arg)
7989 && !uses_template_parms (t))
7990 /* We used to call digest_init here. However, digest_init
7991 will report errors, which we don't want when complain
7992 is zero. More importantly, digest_init will try too
7993 hard to convert things: for example, `0' should not be
7994 converted to pointer type at this point according to
7995 the standard. Accepting this is not merely an
7996 extension, since deciding whether or not these
7997 conversions can occur is part of determining which
7998 function template to call, or whether a given explicit
7999 argument specification is valid. */
8000 val = convert_nontype_argument (t, orig_arg, complain);
8001 else
8002 {
8003 val = canonicalize_expr_argument (orig_arg, complain);
8004 val = maybe_convert_nontype_argument (t, val);
8005 }
8006
8007
8008 if (val == NULL_TREE)
8009 val = error_mark_node;
8010 else if (val == error_mark_node && (complain & tf_error))
8011 error ("could not convert template argument %qE from %qT to %qT",
8012 orig_arg, TREE_TYPE (orig_arg), t);
8013
8014 if (INDIRECT_REF_P (val))
8015 {
8016 /* Reject template arguments that are references to built-in
8017 functions with no library fallbacks. */
8018 const_tree inner = TREE_OPERAND (val, 0);
8019 const_tree innertype = TREE_TYPE (inner);
8020 if (innertype
8021 && TREE_CODE (innertype) == REFERENCE_TYPE
8022 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8023 && TREE_OPERAND_LENGTH (inner) > 0
8024 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8025 return error_mark_node;
8026 }
8027
8028 if (TREE_CODE (val) == SCOPE_REF)
8029 {
8030 /* Strip typedefs from the SCOPE_REF. */
8031 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8032 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8033 complain);
8034 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8035 QUALIFIED_NAME_IS_TEMPLATE (val));
8036 }
8037 }
8038
8039 return val;
8040 }
8041
8042 /* Coerces the remaining template arguments in INNER_ARGS (from
8043 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8044 Returns the coerced argument pack. PARM_IDX is the position of this
8045 parameter in the template parameter list. ARGS is the original
8046 template argument list. */
8047 static tree
8048 coerce_template_parameter_pack (tree parms,
8049 int parm_idx,
8050 tree args,
8051 tree inner_args,
8052 int arg_idx,
8053 tree new_args,
8054 int* lost,
8055 tree in_decl,
8056 tsubst_flags_t complain)
8057 {
8058 tree parm = TREE_VEC_ELT (parms, parm_idx);
8059 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8060 tree packed_args;
8061 tree argument_pack;
8062 tree packed_parms = NULL_TREE;
8063
8064 if (arg_idx > nargs)
8065 arg_idx = nargs;
8066
8067 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8068 {
8069 /* When the template parameter is a non-type template parameter pack
8070 or template template parameter pack whose type or template
8071 parameters use parameter packs, we know exactly how many arguments
8072 we are looking for. Build a vector of the instantiated decls for
8073 these template parameters in PACKED_PARMS. */
8074 /* We can't use make_pack_expansion here because it would interpret a
8075 _DECL as a use rather than a declaration. */
8076 tree decl = TREE_VALUE (parm);
8077 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8078 SET_PACK_EXPANSION_PATTERN (exp, decl);
8079 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8080 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8081
8082 TREE_VEC_LENGTH (args)--;
8083 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8084 TREE_VEC_LENGTH (args)++;
8085
8086 if (packed_parms == error_mark_node)
8087 return error_mark_node;
8088
8089 /* If we're doing a partial instantiation of a member template,
8090 verify that all of the types used for the non-type
8091 template parameter pack are, in fact, valid for non-type
8092 template parameters. */
8093 if (arg_idx < nargs
8094 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8095 {
8096 int j, len = TREE_VEC_LENGTH (packed_parms);
8097 for (j = 0; j < len; ++j)
8098 {
8099 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8100 if (invalid_nontype_parm_type_p (t, complain))
8101 return error_mark_node;
8102 }
8103 /* We don't know how many args we have yet, just
8104 use the unconverted ones for now. */
8105 return NULL_TREE;
8106 }
8107
8108 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8109 }
8110 /* Check if we have a placeholder pack, which indicates we're
8111 in the context of a introduction list. In that case we want
8112 to match this pack to the single placeholder. */
8113 else if (arg_idx < nargs
8114 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8115 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8116 {
8117 nargs = arg_idx + 1;
8118 packed_args = make_tree_vec (1);
8119 }
8120 else
8121 packed_args = make_tree_vec (nargs - arg_idx);
8122
8123 /* Convert the remaining arguments, which will be a part of the
8124 parameter pack "parm". */
8125 int first_pack_arg = arg_idx;
8126 for (; arg_idx < nargs; ++arg_idx)
8127 {
8128 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8129 tree actual_parm = TREE_VALUE (parm);
8130 int pack_idx = arg_idx - first_pack_arg;
8131
8132 if (packed_parms)
8133 {
8134 /* Once we've packed as many args as we have types, stop. */
8135 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8136 break;
8137 else if (PACK_EXPANSION_P (arg))
8138 /* We don't know how many args we have yet, just
8139 use the unconverted ones for now. */
8140 return NULL_TREE;
8141 else
8142 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8143 }
8144
8145 if (arg == error_mark_node)
8146 {
8147 if (complain & tf_error)
8148 error ("template argument %d is invalid", arg_idx + 1);
8149 }
8150 else
8151 arg = convert_template_argument (actual_parm,
8152 arg, new_args, complain, parm_idx,
8153 in_decl);
8154 if (arg == error_mark_node)
8155 (*lost)++;
8156 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8157 }
8158
8159 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8160 && TREE_VEC_LENGTH (packed_args) > 0)
8161 {
8162 if (complain & tf_error)
8163 error ("wrong number of template arguments (%d, should be %d)",
8164 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8165 return error_mark_node;
8166 }
8167
8168 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8169 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8170 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8171 else
8172 {
8173 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8174 TREE_CONSTANT (argument_pack) = 1;
8175 }
8176
8177 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8178 if (CHECKING_P)
8179 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8180 TREE_VEC_LENGTH (packed_args));
8181 return argument_pack;
8182 }
8183
8184 /* Returns the number of pack expansions in the template argument vector
8185 ARGS. */
8186
8187 static int
8188 pack_expansion_args_count (tree args)
8189 {
8190 int i;
8191 int count = 0;
8192 if (args)
8193 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8194 {
8195 tree elt = TREE_VEC_ELT (args, i);
8196 if (elt && PACK_EXPANSION_P (elt))
8197 ++count;
8198 }
8199 return count;
8200 }
8201
8202 /* Convert all template arguments to their appropriate types, and
8203 return a vector containing the innermost resulting template
8204 arguments. If any error occurs, return error_mark_node. Error and
8205 warning messages are issued under control of COMPLAIN.
8206
8207 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8208 for arguments not specified in ARGS. Otherwise, if
8209 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8210 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8211 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8212 ARGS. */
8213
8214 static tree
8215 coerce_template_parms (tree parms,
8216 tree args,
8217 tree in_decl,
8218 tsubst_flags_t complain,
8219 bool require_all_args,
8220 bool use_default_args)
8221 {
8222 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8223 tree orig_inner_args;
8224 tree inner_args;
8225 tree new_args;
8226 tree new_inner_args;
8227 int saved_unevaluated_operand;
8228 int saved_inhibit_evaluation_warnings;
8229
8230 /* When used as a boolean value, indicates whether this is a
8231 variadic template parameter list. Since it's an int, we can also
8232 subtract it from nparms to get the number of non-variadic
8233 parameters. */
8234 int variadic_p = 0;
8235 int variadic_args_p = 0;
8236 int post_variadic_parms = 0;
8237
8238 /* Adjustment to nparms for fixed parameter packs. */
8239 int fixed_pack_adjust = 0;
8240 int fixed_packs = 0;
8241 int missing = 0;
8242
8243 /* Likewise for parameters with default arguments. */
8244 int default_p = 0;
8245
8246 if (args == error_mark_node)
8247 return error_mark_node;
8248
8249 nparms = TREE_VEC_LENGTH (parms);
8250
8251 /* Determine if there are any parameter packs or default arguments. */
8252 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8253 {
8254 tree parm = TREE_VEC_ELT (parms, parm_idx);
8255 if (variadic_p)
8256 ++post_variadic_parms;
8257 if (template_parameter_pack_p (TREE_VALUE (parm)))
8258 ++variadic_p;
8259 if (TREE_PURPOSE (parm))
8260 ++default_p;
8261 }
8262
8263 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8264 /* If there are no parameters that follow a parameter pack, we need to
8265 expand any argument packs so that we can deduce a parameter pack from
8266 some non-packed args followed by an argument pack, as in variadic85.C.
8267 If there are such parameters, we need to leave argument packs intact
8268 so the arguments are assigned properly. This can happen when dealing
8269 with a nested class inside a partial specialization of a class
8270 template, as in variadic92.C, or when deducing a template parameter pack
8271 from a sub-declarator, as in variadic114.C. */
8272 if (!post_variadic_parms)
8273 inner_args = expand_template_argument_pack (inner_args);
8274
8275 /* Count any pack expansion args. */
8276 variadic_args_p = pack_expansion_args_count (inner_args);
8277
8278 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8279 if ((nargs - variadic_args_p > nparms && !variadic_p)
8280 || (nargs < nparms - variadic_p
8281 && require_all_args
8282 && !variadic_args_p
8283 && (!use_default_args
8284 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8285 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8286 {
8287 bad_nargs:
8288 if (complain & tf_error)
8289 {
8290 if (variadic_p || default_p)
8291 {
8292 nparms -= variadic_p + default_p;
8293 error ("wrong number of template arguments "
8294 "(%d, should be at least %d)", nargs, nparms);
8295 }
8296 else
8297 error ("wrong number of template arguments "
8298 "(%d, should be %d)", nargs, nparms);
8299
8300 if (in_decl)
8301 inform (DECL_SOURCE_LOCATION (in_decl),
8302 "provided for %qD", in_decl);
8303 }
8304
8305 return error_mark_node;
8306 }
8307 /* We can't pass a pack expansion to a non-pack parameter of an alias
8308 template (DR 1430). */
8309 else if (in_decl
8310 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8311 || concept_template_p (in_decl))
8312 && variadic_args_p
8313 && nargs - variadic_args_p < nparms - variadic_p)
8314 {
8315 if (complain & tf_error)
8316 {
8317 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8318 {
8319 tree arg = TREE_VEC_ELT (inner_args, i);
8320 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8321
8322 if (PACK_EXPANSION_P (arg)
8323 && !template_parameter_pack_p (parm))
8324 {
8325 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8326 error_at (location_of (arg),
8327 "pack expansion argument for non-pack parameter "
8328 "%qD of alias template %qD", parm, in_decl);
8329 else
8330 error_at (location_of (arg),
8331 "pack expansion argument for non-pack parameter "
8332 "%qD of concept %qD", parm, in_decl);
8333 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8334 goto found;
8335 }
8336 }
8337 gcc_unreachable ();
8338 found:;
8339 }
8340 return error_mark_node;
8341 }
8342
8343 /* We need to evaluate the template arguments, even though this
8344 template-id may be nested within a "sizeof". */
8345 saved_unevaluated_operand = cp_unevaluated_operand;
8346 cp_unevaluated_operand = 0;
8347 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8348 c_inhibit_evaluation_warnings = 0;
8349 new_inner_args = make_tree_vec (nparms);
8350 new_args = add_outermost_template_args (args, new_inner_args);
8351 int pack_adjust = 0;
8352 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8353 {
8354 tree arg;
8355 tree parm;
8356
8357 /* Get the Ith template parameter. */
8358 parm = TREE_VEC_ELT (parms, parm_idx);
8359
8360 if (parm == error_mark_node)
8361 {
8362 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8363 continue;
8364 }
8365
8366 /* Calculate the next argument. */
8367 if (arg_idx < nargs)
8368 arg = TREE_VEC_ELT (inner_args, arg_idx);
8369 else
8370 arg = NULL_TREE;
8371
8372 if (template_parameter_pack_p (TREE_VALUE (parm))
8373 && !(arg && ARGUMENT_PACK_P (arg)))
8374 {
8375 /* Some arguments will be placed in the
8376 template parameter pack PARM. */
8377 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8378 inner_args, arg_idx,
8379 new_args, &lost,
8380 in_decl, complain);
8381
8382 if (arg == NULL_TREE)
8383 {
8384 /* We don't know how many args we have yet, just use the
8385 unconverted (and still packed) ones for now. */
8386 new_inner_args = orig_inner_args;
8387 arg_idx = nargs;
8388 break;
8389 }
8390
8391 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8392
8393 /* Store this argument. */
8394 if (arg == error_mark_node)
8395 {
8396 lost++;
8397 /* We are done with all of the arguments. */
8398 arg_idx = nargs;
8399 break;
8400 }
8401 else
8402 {
8403 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8404 arg_idx += pack_adjust;
8405 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8406 {
8407 ++fixed_packs;
8408 fixed_pack_adjust += pack_adjust;
8409 }
8410 }
8411
8412 continue;
8413 }
8414 else if (arg)
8415 {
8416 if (PACK_EXPANSION_P (arg))
8417 {
8418 /* "If every valid specialization of a variadic template
8419 requires an empty template parameter pack, the template is
8420 ill-formed, no diagnostic required." So check that the
8421 pattern works with this parameter. */
8422 tree pattern = PACK_EXPANSION_PATTERN (arg);
8423 tree conv = convert_template_argument (TREE_VALUE (parm),
8424 pattern, new_args,
8425 complain, parm_idx,
8426 in_decl);
8427 if (conv == error_mark_node)
8428 {
8429 if (complain & tf_error)
8430 inform (input_location, "so any instantiation with a "
8431 "non-empty parameter pack would be ill-formed");
8432 ++lost;
8433 }
8434 else if (TYPE_P (conv) && !TYPE_P (pattern))
8435 /* Recover from missing typename. */
8436 TREE_VEC_ELT (inner_args, arg_idx)
8437 = make_pack_expansion (conv, complain);
8438
8439 /* We don't know how many args we have yet, just
8440 use the unconverted ones for now. */
8441 new_inner_args = inner_args;
8442 arg_idx = nargs;
8443 break;
8444 }
8445 }
8446 else if (require_all_args)
8447 {
8448 /* There must be a default arg in this case. */
8449 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8450 complain, in_decl);
8451 /* The position of the first default template argument,
8452 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8453 Record that. */
8454 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8455 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8456 arg_idx - pack_adjust);
8457 }
8458 else
8459 break;
8460
8461 if (arg == error_mark_node)
8462 {
8463 if (complain & tf_error)
8464 error ("template argument %d is invalid", arg_idx + 1);
8465 }
8466 else if (!arg)
8467 {
8468 /* This can occur if there was an error in the template
8469 parameter list itself (which we would already have
8470 reported) that we are trying to recover from, e.g., a class
8471 template with a parameter list such as
8472 template<typename..., typename> (cpp0x/variadic150.C). */
8473 ++lost;
8474
8475 /* This can also happen with a fixed parameter pack (71834). */
8476 if (arg_idx >= nargs)
8477 ++missing;
8478 }
8479 else
8480 arg = convert_template_argument (TREE_VALUE (parm),
8481 arg, new_args, complain,
8482 parm_idx, in_decl);
8483
8484 if (arg == error_mark_node)
8485 lost++;
8486 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8487 }
8488 cp_unevaluated_operand = saved_unevaluated_operand;
8489 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8490
8491 if (missing || arg_idx < nargs - variadic_args_p)
8492 {
8493 /* If we had fixed parameter packs, we didn't know how many arguments we
8494 actually needed earlier; now we do. */
8495 nparms += fixed_pack_adjust;
8496 variadic_p -= fixed_packs;
8497 goto bad_nargs;
8498 }
8499
8500 if (arg_idx < nargs)
8501 {
8502 /* We had some pack expansion arguments that will only work if the packs
8503 are empty, but wait until instantiation time to complain.
8504 See variadic-ttp3.C. */
8505 int len = nparms + (nargs - arg_idx);
8506 tree args = make_tree_vec (len);
8507 int i = 0;
8508 for (; i < nparms; ++i)
8509 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8510 for (; i < len; ++i, ++arg_idx)
8511 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8512 arg_idx - pack_adjust);
8513 new_inner_args = args;
8514 }
8515
8516 if (lost)
8517 {
8518 gcc_assert (!(complain & tf_error) || seen_error ());
8519 return error_mark_node;
8520 }
8521
8522 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8523 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8524 TREE_VEC_LENGTH (new_inner_args));
8525
8526 return new_inner_args;
8527 }
8528
8529 /* Convert all template arguments to their appropriate types, and
8530 return a vector containing the innermost resulting template
8531 arguments. If any error occurs, return error_mark_node. Error and
8532 warning messages are not issued.
8533
8534 Note that no function argument deduction is performed, and default
8535 arguments are used to fill in unspecified arguments. */
8536 tree
8537 coerce_template_parms (tree parms, tree args, tree in_decl)
8538 {
8539 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8540 }
8541
8542 /* Convert all template arguments to their appropriate type, and
8543 instantiate default arguments as needed. This returns a vector
8544 containing the innermost resulting template arguments, or
8545 error_mark_node if unsuccessful. */
8546 tree
8547 coerce_template_parms (tree parms, tree args, tree in_decl,
8548 tsubst_flags_t complain)
8549 {
8550 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8551 }
8552
8553 /* Like coerce_template_parms. If PARMS represents all template
8554 parameters levels, this function returns a vector of vectors
8555 representing all the resulting argument levels. Note that in this
8556 case, only the innermost arguments are coerced because the
8557 outermost ones are supposed to have been coerced already.
8558
8559 Otherwise, if PARMS represents only (the innermost) vector of
8560 parameters, this function returns a vector containing just the
8561 innermost resulting arguments. */
8562
8563 static tree
8564 coerce_innermost_template_parms (tree parms,
8565 tree args,
8566 tree in_decl,
8567 tsubst_flags_t complain,
8568 bool require_all_args,
8569 bool use_default_args)
8570 {
8571 int parms_depth = TMPL_PARMS_DEPTH (parms);
8572 int args_depth = TMPL_ARGS_DEPTH (args);
8573 tree coerced_args;
8574
8575 if (parms_depth > 1)
8576 {
8577 coerced_args = make_tree_vec (parms_depth);
8578 tree level;
8579 int cur_depth;
8580
8581 for (level = parms, cur_depth = parms_depth;
8582 parms_depth > 0 && level != NULL_TREE;
8583 level = TREE_CHAIN (level), --cur_depth)
8584 {
8585 tree l;
8586 if (cur_depth == args_depth)
8587 l = coerce_template_parms (TREE_VALUE (level),
8588 args, in_decl, complain,
8589 require_all_args,
8590 use_default_args);
8591 else
8592 l = TMPL_ARGS_LEVEL (args, cur_depth);
8593
8594 if (l == error_mark_node)
8595 return error_mark_node;
8596
8597 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8598 }
8599 }
8600 else
8601 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8602 args, in_decl, complain,
8603 require_all_args,
8604 use_default_args);
8605 return coerced_args;
8606 }
8607
8608 /* Returns 1 if template args OT and NT are equivalent. */
8609
8610 int
8611 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8612 {
8613 if (nt == ot)
8614 return 1;
8615 if (nt == NULL_TREE || ot == NULL_TREE)
8616 return false;
8617 if (nt == any_targ_node || ot == any_targ_node)
8618 return true;
8619
8620 if (TREE_CODE (nt) == TREE_VEC)
8621 /* For member templates */
8622 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8623 else if (PACK_EXPANSION_P (ot))
8624 return (PACK_EXPANSION_P (nt)
8625 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8626 PACK_EXPANSION_PATTERN (nt))
8627 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8628 PACK_EXPANSION_EXTRA_ARGS (nt)));
8629 else if (ARGUMENT_PACK_P (ot))
8630 {
8631 int i, len;
8632 tree opack, npack;
8633
8634 if (!ARGUMENT_PACK_P (nt))
8635 return 0;
8636
8637 opack = ARGUMENT_PACK_ARGS (ot);
8638 npack = ARGUMENT_PACK_ARGS (nt);
8639 len = TREE_VEC_LENGTH (opack);
8640 if (TREE_VEC_LENGTH (npack) != len)
8641 return 0;
8642 for (i = 0; i < len; ++i)
8643 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8644 TREE_VEC_ELT (npack, i)))
8645 return 0;
8646 return 1;
8647 }
8648 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8649 gcc_unreachable ();
8650 else if (TYPE_P (nt))
8651 {
8652 if (!TYPE_P (ot))
8653 return false;
8654 /* Don't treat an alias template specialization with dependent
8655 arguments as equivalent to its underlying type when used as a
8656 template argument; we need them to be distinct so that we
8657 substitute into the specialization arguments at instantiation
8658 time. And aliases can't be equivalent without being ==, so
8659 we don't need to look any deeper.
8660
8661 During partial ordering, however, we need to treat them normally so
8662 that we can order uses of the same alias with different
8663 cv-qualification (79960). */
8664 if (!partial_order
8665 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8666 return false;
8667 else
8668 return same_type_p (ot, nt);
8669 }
8670 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8671 return 0;
8672 else
8673 {
8674 /* Try to treat a template non-type argument that has been converted
8675 to the parameter type as equivalent to one that hasn't yet. */
8676 for (enum tree_code code1 = TREE_CODE (ot);
8677 CONVERT_EXPR_CODE_P (code1)
8678 || code1 == NON_LVALUE_EXPR;
8679 code1 = TREE_CODE (ot))
8680 ot = TREE_OPERAND (ot, 0);
8681 for (enum tree_code code2 = TREE_CODE (nt);
8682 CONVERT_EXPR_CODE_P (code2)
8683 || code2 == NON_LVALUE_EXPR;
8684 code2 = TREE_CODE (nt))
8685 nt = TREE_OPERAND (nt, 0);
8686
8687 return cp_tree_equal (ot, nt);
8688 }
8689 }
8690
8691 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8692 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8693 NEWARG_PTR with the offending arguments if they are non-NULL. */
8694
8695 int
8696 comp_template_args (tree oldargs, tree newargs,
8697 tree *oldarg_ptr, tree *newarg_ptr,
8698 bool partial_order)
8699 {
8700 int i;
8701
8702 if (oldargs == newargs)
8703 return 1;
8704
8705 if (!oldargs || !newargs)
8706 return 0;
8707
8708 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8709 return 0;
8710
8711 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8712 {
8713 tree nt = TREE_VEC_ELT (newargs, i);
8714 tree ot = TREE_VEC_ELT (oldargs, i);
8715
8716 if (! template_args_equal (ot, nt, partial_order))
8717 {
8718 if (oldarg_ptr != NULL)
8719 *oldarg_ptr = ot;
8720 if (newarg_ptr != NULL)
8721 *newarg_ptr = nt;
8722 return 0;
8723 }
8724 }
8725 return 1;
8726 }
8727
8728 inline bool
8729 comp_template_args_porder (tree oargs, tree nargs)
8730 {
8731 return comp_template_args (oargs, nargs, NULL, NULL, true);
8732 }
8733
8734 static void
8735 add_pending_template (tree d)
8736 {
8737 tree ti = (TYPE_P (d)
8738 ? CLASSTYPE_TEMPLATE_INFO (d)
8739 : DECL_TEMPLATE_INFO (d));
8740 struct pending_template *pt;
8741 int level;
8742
8743 if (TI_PENDING_TEMPLATE_FLAG (ti))
8744 return;
8745
8746 /* We are called both from instantiate_decl, where we've already had a
8747 tinst_level pushed, and instantiate_template, where we haven't.
8748 Compensate. */
8749 level = !current_tinst_level || current_tinst_level->decl != d;
8750
8751 if (level)
8752 push_tinst_level (d);
8753
8754 pt = ggc_alloc<pending_template> ();
8755 pt->next = NULL;
8756 pt->tinst = current_tinst_level;
8757 if (last_pending_template)
8758 last_pending_template->next = pt;
8759 else
8760 pending_templates = pt;
8761
8762 last_pending_template = pt;
8763
8764 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8765
8766 if (level)
8767 pop_tinst_level ();
8768 }
8769
8770
8771 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8772 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8773 documentation for TEMPLATE_ID_EXPR. */
8774
8775 tree
8776 lookup_template_function (tree fns, tree arglist)
8777 {
8778 tree type;
8779
8780 if (fns == error_mark_node || arglist == error_mark_node)
8781 return error_mark_node;
8782
8783 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8784
8785 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8786 {
8787 error ("%q#D is not a function template", fns);
8788 return error_mark_node;
8789 }
8790
8791 if (BASELINK_P (fns))
8792 {
8793 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8794 unknown_type_node,
8795 BASELINK_FUNCTIONS (fns),
8796 arglist);
8797 return fns;
8798 }
8799
8800 type = TREE_TYPE (fns);
8801 if (TREE_CODE (fns) == OVERLOAD || !type)
8802 type = unknown_type_node;
8803
8804 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8805 }
8806
8807 /* Within the scope of a template class S<T>, the name S gets bound
8808 (in build_self_reference) to a TYPE_DECL for the class, not a
8809 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8810 or one of its enclosing classes, and that type is a template,
8811 return the associated TEMPLATE_DECL. Otherwise, the original
8812 DECL is returned.
8813
8814 Also handle the case when DECL is a TREE_LIST of ambiguous
8815 injected-class-names from different bases. */
8816
8817 tree
8818 maybe_get_template_decl_from_type_decl (tree decl)
8819 {
8820 if (decl == NULL_TREE)
8821 return decl;
8822
8823 /* DR 176: A lookup that finds an injected-class-name (10.2
8824 [class.member.lookup]) can result in an ambiguity in certain cases
8825 (for example, if it is found in more than one base class). If all of
8826 the injected-class-names that are found refer to specializations of
8827 the same class template, and if the name is followed by a
8828 template-argument-list, the reference refers to the class template
8829 itself and not a specialization thereof, and is not ambiguous. */
8830 if (TREE_CODE (decl) == TREE_LIST)
8831 {
8832 tree t, tmpl = NULL_TREE;
8833 for (t = decl; t; t = TREE_CHAIN (t))
8834 {
8835 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8836 if (!tmpl)
8837 tmpl = elt;
8838 else if (tmpl != elt)
8839 break;
8840 }
8841 if (tmpl && t == NULL_TREE)
8842 return tmpl;
8843 else
8844 return decl;
8845 }
8846
8847 return (decl != NULL_TREE
8848 && DECL_SELF_REFERENCE_P (decl)
8849 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8850 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8851 }
8852
8853 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8854 parameters, find the desired type.
8855
8856 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8857
8858 IN_DECL, if non-NULL, is the template declaration we are trying to
8859 instantiate.
8860
8861 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8862 the class we are looking up.
8863
8864 Issue error and warning messages under control of COMPLAIN.
8865
8866 If the template class is really a local class in a template
8867 function, then the FUNCTION_CONTEXT is the function in which it is
8868 being instantiated.
8869
8870 ??? Note that this function is currently called *twice* for each
8871 template-id: the first time from the parser, while creating the
8872 incomplete type (finish_template_type), and the second type during the
8873 real instantiation (instantiate_template_class). This is surely something
8874 that we want to avoid. It also causes some problems with argument
8875 coercion (see convert_nontype_argument for more information on this). */
8876
8877 static tree
8878 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8879 int entering_scope, tsubst_flags_t complain)
8880 {
8881 tree templ = NULL_TREE, parmlist;
8882 tree t;
8883 spec_entry **slot;
8884 spec_entry *entry;
8885 spec_entry elt;
8886 hashval_t hash;
8887
8888 if (identifier_p (d1))
8889 {
8890 tree value = innermost_non_namespace_value (d1);
8891 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8892 templ = value;
8893 else
8894 {
8895 if (context)
8896 push_decl_namespace (context);
8897 templ = lookup_name (d1);
8898 templ = maybe_get_template_decl_from_type_decl (templ);
8899 if (context)
8900 pop_decl_namespace ();
8901 }
8902 if (templ)
8903 context = DECL_CONTEXT (templ);
8904 }
8905 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8906 {
8907 tree type = TREE_TYPE (d1);
8908
8909 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8910 an implicit typename for the second A. Deal with it. */
8911 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8912 type = TREE_TYPE (type);
8913
8914 if (CLASSTYPE_TEMPLATE_INFO (type))
8915 {
8916 templ = CLASSTYPE_TI_TEMPLATE (type);
8917 d1 = DECL_NAME (templ);
8918 }
8919 }
8920 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8921 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8922 {
8923 templ = TYPE_TI_TEMPLATE (d1);
8924 d1 = DECL_NAME (templ);
8925 }
8926 else if (DECL_TYPE_TEMPLATE_P (d1))
8927 {
8928 templ = d1;
8929 d1 = DECL_NAME (templ);
8930 context = DECL_CONTEXT (templ);
8931 }
8932 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8933 {
8934 templ = d1;
8935 d1 = DECL_NAME (templ);
8936 }
8937
8938 /* Issue an error message if we didn't find a template. */
8939 if (! templ)
8940 {
8941 if (complain & tf_error)
8942 error ("%qT is not a template", d1);
8943 return error_mark_node;
8944 }
8945
8946 if (TREE_CODE (templ) != TEMPLATE_DECL
8947 /* Make sure it's a user visible template, if it was named by
8948 the user. */
8949 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8950 && !PRIMARY_TEMPLATE_P (templ)))
8951 {
8952 if (complain & tf_error)
8953 {
8954 error ("non-template type %qT used as a template", d1);
8955 if (in_decl)
8956 error ("for template declaration %q+D", in_decl);
8957 }
8958 return error_mark_node;
8959 }
8960
8961 complain &= ~tf_user;
8962
8963 /* An alias that just changes the name of a template is equivalent to the
8964 other template, so if any of the arguments are pack expansions, strip
8965 the alias to avoid problems with a pack expansion passed to a non-pack
8966 alias template parameter (DR 1430). */
8967 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8968 templ = get_underlying_template (templ);
8969
8970 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8971 {
8972 tree parm;
8973 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8974 if (arglist2 == error_mark_node
8975 || (!uses_template_parms (arglist2)
8976 && check_instantiated_args (templ, arglist2, complain)))
8977 return error_mark_node;
8978
8979 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8980 return parm;
8981 }
8982 else
8983 {
8984 tree template_type = TREE_TYPE (templ);
8985 tree gen_tmpl;
8986 tree type_decl;
8987 tree found = NULL_TREE;
8988 int arg_depth;
8989 int parm_depth;
8990 int is_dependent_type;
8991 int use_partial_inst_tmpl = false;
8992
8993 if (template_type == error_mark_node)
8994 /* An error occurred while building the template TEMPL, and a
8995 diagnostic has most certainly been emitted for that
8996 already. Let's propagate that error. */
8997 return error_mark_node;
8998
8999 gen_tmpl = most_general_template (templ);
9000 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9001 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9002 arg_depth = TMPL_ARGS_DEPTH (arglist);
9003
9004 if (arg_depth == 1 && parm_depth > 1)
9005 {
9006 /* We've been given an incomplete set of template arguments.
9007 For example, given:
9008
9009 template <class T> struct S1 {
9010 template <class U> struct S2 {};
9011 template <class U> struct S2<U*> {};
9012 };
9013
9014 we will be called with an ARGLIST of `U*', but the
9015 TEMPLATE will be `template <class T> template
9016 <class U> struct S1<T>::S2'. We must fill in the missing
9017 arguments. */
9018 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9019 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9020 arg_depth = TMPL_ARGS_DEPTH (arglist);
9021 }
9022
9023 /* Now we should have enough arguments. */
9024 gcc_assert (parm_depth == arg_depth);
9025
9026 /* From here on, we're only interested in the most general
9027 template. */
9028
9029 /* Calculate the BOUND_ARGS. These will be the args that are
9030 actually tsubst'd into the definition to create the
9031 instantiation. */
9032 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9033 complain,
9034 /*require_all_args=*/true,
9035 /*use_default_args=*/true);
9036
9037 if (arglist == error_mark_node)
9038 /* We were unable to bind the arguments. */
9039 return error_mark_node;
9040
9041 /* In the scope of a template class, explicit references to the
9042 template class refer to the type of the template, not any
9043 instantiation of it. For example, in:
9044
9045 template <class T> class C { void f(C<T>); }
9046
9047 the `C<T>' is just the same as `C'. Outside of the
9048 class, however, such a reference is an instantiation. */
9049 if (entering_scope
9050 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9051 || currently_open_class (template_type))
9052 {
9053 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9054
9055 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9056 return template_type;
9057 }
9058
9059 /* If we already have this specialization, return it. */
9060 elt.tmpl = gen_tmpl;
9061 elt.args = arglist;
9062 elt.spec = NULL_TREE;
9063 hash = spec_hasher::hash (&elt);
9064 entry = type_specializations->find_with_hash (&elt, hash);
9065
9066 if (entry)
9067 return entry->spec;
9068
9069 /* If the the template's constraints are not satisfied,
9070 then we cannot form a valid type.
9071
9072 Note that the check is deferred until after the hash
9073 lookup. This prevents redundant checks on previously
9074 instantiated specializations. */
9075 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9076 {
9077 if (complain & tf_error)
9078 {
9079 error ("template constraint failure");
9080 diagnose_constraints (input_location, gen_tmpl, arglist);
9081 }
9082 return error_mark_node;
9083 }
9084
9085 is_dependent_type = uses_template_parms (arglist);
9086
9087 /* If the deduced arguments are invalid, then the binding
9088 failed. */
9089 if (!is_dependent_type
9090 && check_instantiated_args (gen_tmpl,
9091 INNERMOST_TEMPLATE_ARGS (arglist),
9092 complain))
9093 return error_mark_node;
9094
9095 if (!is_dependent_type
9096 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9097 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9098 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9099 {
9100 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9101 DECL_NAME (gen_tmpl),
9102 /*tag_scope=*/ts_global);
9103 return found;
9104 }
9105
9106 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
9107 complain, in_decl);
9108 if (context == error_mark_node)
9109 return error_mark_node;
9110
9111 if (!context)
9112 context = global_namespace;
9113
9114 /* Create the type. */
9115 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9116 {
9117 /* The user referred to a specialization of an alias
9118 template represented by GEN_TMPL.
9119
9120 [temp.alias]/2 says:
9121
9122 When a template-id refers to the specialization of an
9123 alias template, it is equivalent to the associated
9124 type obtained by substitution of its
9125 template-arguments for the template-parameters in the
9126 type-id of the alias template. */
9127
9128 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9129 /* Note that the call above (by indirectly calling
9130 register_specialization in tsubst_decl) registers the
9131 TYPE_DECL representing the specialization of the alias
9132 template. So next time someone substitutes ARGLIST for
9133 the template parms into the alias template (GEN_TMPL),
9134 she'll get that TYPE_DECL back. */
9135
9136 if (t == error_mark_node)
9137 return t;
9138 }
9139 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9140 {
9141 if (!is_dependent_type)
9142 {
9143 set_current_access_from_decl (TYPE_NAME (template_type));
9144 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9145 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9146 arglist, complain, in_decl),
9147 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9148 arglist, complain, in_decl),
9149 SCOPED_ENUM_P (template_type), NULL);
9150
9151 if (t == error_mark_node)
9152 return t;
9153 }
9154 else
9155 {
9156 /* We don't want to call start_enum for this type, since
9157 the values for the enumeration constants may involve
9158 template parameters. And, no one should be interested
9159 in the enumeration constants for such a type. */
9160 t = cxx_make_type (ENUMERAL_TYPE);
9161 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9162 }
9163 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9164 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9165 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9166 }
9167 else if (CLASS_TYPE_P (template_type))
9168 {
9169 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9170 instantiated here. */
9171 gcc_assert (!LAMBDA_TYPE_P (template_type));
9172
9173 t = make_class_type (TREE_CODE (template_type));
9174 CLASSTYPE_DECLARED_CLASS (t)
9175 = CLASSTYPE_DECLARED_CLASS (template_type);
9176 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9177
9178 /* A local class. Make sure the decl gets registered properly. */
9179 if (context == current_function_decl)
9180 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9181
9182 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9183 /* This instantiation is another name for the primary
9184 template type. Set the TYPE_CANONICAL field
9185 appropriately. */
9186 TYPE_CANONICAL (t) = template_type;
9187 else if (any_template_arguments_need_structural_equality_p (arglist))
9188 /* Some of the template arguments require structural
9189 equality testing, so this template class requires
9190 structural equality testing. */
9191 SET_TYPE_STRUCTURAL_EQUALITY (t);
9192 }
9193 else
9194 gcc_unreachable ();
9195
9196 /* If we called start_enum or pushtag above, this information
9197 will already be set up. */
9198 if (!TYPE_NAME (t))
9199 {
9200 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9201
9202 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9203 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9204 DECL_SOURCE_LOCATION (type_decl)
9205 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9206 }
9207 else
9208 type_decl = TYPE_NAME (t);
9209
9210 if (CLASS_TYPE_P (template_type))
9211 {
9212 TREE_PRIVATE (type_decl)
9213 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9214 TREE_PROTECTED (type_decl)
9215 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9216 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9217 {
9218 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9219 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9220 }
9221 }
9222
9223 if (OVERLOAD_TYPE_P (t)
9224 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9225 {
9226 static const char *tags[] = {"abi_tag", "may_alias"};
9227
9228 for (unsigned ix = 0; ix != 2; ix++)
9229 {
9230 tree attributes
9231 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9232
9233 if (attributes)
9234 TYPE_ATTRIBUTES (t)
9235 = tree_cons (TREE_PURPOSE (attributes),
9236 TREE_VALUE (attributes),
9237 TYPE_ATTRIBUTES (t));
9238 }
9239 }
9240
9241 /* Let's consider the explicit specialization of a member
9242 of a class template specialization that is implicitly instantiated,
9243 e.g.:
9244 template<class T>
9245 struct S
9246 {
9247 template<class U> struct M {}; //#0
9248 };
9249
9250 template<>
9251 template<>
9252 struct S<int>::M<char> //#1
9253 {
9254 int i;
9255 };
9256 [temp.expl.spec]/4 says this is valid.
9257
9258 In this case, when we write:
9259 S<int>::M<char> m;
9260
9261 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9262 the one of #0.
9263
9264 When we encounter #1, we want to store the partial instantiation
9265 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9266
9267 For all cases other than this "explicit specialization of member of a
9268 class template", we just want to store the most general template into
9269 the CLASSTYPE_TI_TEMPLATE of M.
9270
9271 This case of "explicit specialization of member of a class template"
9272 only happens when:
9273 1/ the enclosing class is an instantiation of, and therefore not
9274 the same as, the context of the most general template, and
9275 2/ we aren't looking at the partial instantiation itself, i.e.
9276 the innermost arguments are not the same as the innermost parms of
9277 the most general template.
9278
9279 So it's only when 1/ and 2/ happens that we want to use the partial
9280 instantiation of the member template in lieu of its most general
9281 template. */
9282
9283 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9284 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9285 /* the enclosing class must be an instantiation... */
9286 && CLASS_TYPE_P (context)
9287 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9288 {
9289 TREE_VEC_LENGTH (arglist)--;
9290 ++processing_template_decl;
9291 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9292 tree partial_inst_args =
9293 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9294 arglist, complain, NULL_TREE);
9295 --processing_template_decl;
9296 TREE_VEC_LENGTH (arglist)++;
9297 if (partial_inst_args == error_mark_node)
9298 return error_mark_node;
9299 use_partial_inst_tmpl =
9300 /*...and we must not be looking at the partial instantiation
9301 itself. */
9302 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9303 partial_inst_args);
9304 }
9305
9306 if (!use_partial_inst_tmpl)
9307 /* This case is easy; there are no member templates involved. */
9308 found = gen_tmpl;
9309 else
9310 {
9311 /* This is a full instantiation of a member template. Find
9312 the partial instantiation of which this is an instance. */
9313
9314 /* Temporarily reduce by one the number of levels in the ARGLIST
9315 so as to avoid comparing the last set of arguments. */
9316 TREE_VEC_LENGTH (arglist)--;
9317 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9318 TREE_VEC_LENGTH (arglist)++;
9319 /* FOUND is either a proper class type, or an alias
9320 template specialization. In the later case, it's a
9321 TYPE_DECL, resulting from the substituting of arguments
9322 for parameters in the TYPE_DECL of the alias template
9323 done earlier. So be careful while getting the template
9324 of FOUND. */
9325 found = (TREE_CODE (found) == TEMPLATE_DECL
9326 ? found
9327 : (TREE_CODE (found) == TYPE_DECL
9328 ? DECL_TI_TEMPLATE (found)
9329 : CLASSTYPE_TI_TEMPLATE (found)));
9330 }
9331
9332 // Build template info for the new specialization.
9333 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9334
9335 elt.spec = t;
9336 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9337 entry = ggc_alloc<spec_entry> ();
9338 *entry = elt;
9339 *slot = entry;
9340
9341 /* Note this use of the partial instantiation so we can check it
9342 later in maybe_process_partial_specialization. */
9343 DECL_TEMPLATE_INSTANTIATIONS (found)
9344 = tree_cons (arglist, t,
9345 DECL_TEMPLATE_INSTANTIATIONS (found));
9346
9347 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9348 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9349 /* Now that the type has been registered on the instantiations
9350 list, we set up the enumerators. Because the enumeration
9351 constants may involve the enumeration type itself, we make
9352 sure to register the type first, and then create the
9353 constants. That way, doing tsubst_expr for the enumeration
9354 constants won't result in recursive calls here; we'll find
9355 the instantiation and exit above. */
9356 tsubst_enum (template_type, t, arglist);
9357
9358 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9359 /* If the type makes use of template parameters, the
9360 code that generates debugging information will crash. */
9361 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9362
9363 /* Possibly limit visibility based on template args. */
9364 TREE_PUBLIC (type_decl) = 1;
9365 determine_visibility (type_decl);
9366
9367 inherit_targ_abi_tags (t);
9368
9369 return t;
9370 }
9371 }
9372
9373 /* Wrapper for lookup_template_class_1. */
9374
9375 tree
9376 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9377 int entering_scope, tsubst_flags_t complain)
9378 {
9379 tree ret;
9380 timevar_push (TV_TEMPLATE_INST);
9381 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9382 entering_scope, complain);
9383 timevar_pop (TV_TEMPLATE_INST);
9384 return ret;
9385 }
9386
9387 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9388
9389 tree
9390 lookup_template_variable (tree templ, tree arglist)
9391 {
9392 /* The type of the expression is NULL_TREE since the template-id could refer
9393 to an explicit or partial specialization. */
9394 tree type = NULL_TREE;
9395 if (flag_concepts && variable_concept_p (templ))
9396 /* Except that concepts are always bool. */
9397 type = boolean_type_node;
9398 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9399 }
9400
9401 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9402
9403 tree
9404 finish_template_variable (tree var, tsubst_flags_t complain)
9405 {
9406 tree templ = TREE_OPERAND (var, 0);
9407 tree arglist = TREE_OPERAND (var, 1);
9408
9409 /* We never want to return a VAR_DECL for a variable concept, since they
9410 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9411 bool concept_p = flag_concepts && variable_concept_p (templ);
9412 if (concept_p && processing_template_decl)
9413 return var;
9414
9415 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9416 arglist = add_outermost_template_args (tmpl_args, arglist);
9417
9418 templ = most_general_template (templ);
9419 tree parms = DECL_TEMPLATE_PARMS (templ);
9420 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9421 /*req_all*/true,
9422 /*use_default*/true);
9423
9424 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9425 {
9426 if (complain & tf_error)
9427 {
9428 error ("use of invalid variable template %qE", var);
9429 diagnose_constraints (location_of (var), templ, arglist);
9430 }
9431 return error_mark_node;
9432 }
9433
9434 /* If a template-id refers to a specialization of a variable
9435 concept, then the expression is true if and only if the
9436 concept's constraints are satisfied by the given template
9437 arguments.
9438
9439 NOTE: This is an extension of Concepts Lite TS that
9440 allows constraints to be used in expressions. */
9441 if (concept_p)
9442 {
9443 tree decl = DECL_TEMPLATE_RESULT (templ);
9444 return evaluate_variable_concept (decl, arglist);
9445 }
9446
9447 return instantiate_template (templ, arglist, complain);
9448 }
9449
9450 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9451 TARGS template args, and instantiate it if it's not dependent. */
9452
9453 tree
9454 lookup_and_finish_template_variable (tree templ, tree targs,
9455 tsubst_flags_t complain)
9456 {
9457 templ = lookup_template_variable (templ, targs);
9458 if (!any_dependent_template_arguments_p (targs))
9459 {
9460 templ = finish_template_variable (templ, complain);
9461 mark_used (templ);
9462 }
9463
9464 return convert_from_reference (templ);
9465 }
9466
9467 \f
9468 struct pair_fn_data
9469 {
9470 tree_fn_t fn;
9471 tree_fn_t any_fn;
9472 void *data;
9473 /* True when we should also visit template parameters that occur in
9474 non-deduced contexts. */
9475 bool include_nondeduced_p;
9476 hash_set<tree> *visited;
9477 };
9478
9479 /* Called from for_each_template_parm via walk_tree. */
9480
9481 static tree
9482 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9483 {
9484 tree t = *tp;
9485 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9486 tree_fn_t fn = pfd->fn;
9487 void *data = pfd->data;
9488 tree result = NULL_TREE;
9489
9490 #define WALK_SUBTREE(NODE) \
9491 do \
9492 { \
9493 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9494 pfd->include_nondeduced_p, \
9495 pfd->any_fn); \
9496 if (result) goto out; \
9497 } \
9498 while (0)
9499
9500 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9501 return t;
9502
9503 if (TYPE_P (t)
9504 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9505 WALK_SUBTREE (TYPE_CONTEXT (t));
9506
9507 switch (TREE_CODE (t))
9508 {
9509 case RECORD_TYPE:
9510 if (TYPE_PTRMEMFUNC_P (t))
9511 break;
9512 /* Fall through. */
9513
9514 case UNION_TYPE:
9515 case ENUMERAL_TYPE:
9516 if (!TYPE_TEMPLATE_INFO (t))
9517 *walk_subtrees = 0;
9518 else
9519 WALK_SUBTREE (TYPE_TI_ARGS (t));
9520 break;
9521
9522 case INTEGER_TYPE:
9523 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9524 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9525 break;
9526
9527 case METHOD_TYPE:
9528 /* Since we're not going to walk subtrees, we have to do this
9529 explicitly here. */
9530 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9531 /* Fall through. */
9532
9533 case FUNCTION_TYPE:
9534 /* Check the return type. */
9535 WALK_SUBTREE (TREE_TYPE (t));
9536
9537 /* Check the parameter types. Since default arguments are not
9538 instantiated until they are needed, the TYPE_ARG_TYPES may
9539 contain expressions that involve template parameters. But,
9540 no-one should be looking at them yet. And, once they're
9541 instantiated, they don't contain template parameters, so
9542 there's no point in looking at them then, either. */
9543 {
9544 tree parm;
9545
9546 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9547 WALK_SUBTREE (TREE_VALUE (parm));
9548
9549 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9550 want walk_tree walking into them itself. */
9551 *walk_subtrees = 0;
9552 }
9553
9554 if (flag_noexcept_type)
9555 {
9556 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9557 if (spec)
9558 WALK_SUBTREE (TREE_PURPOSE (spec));
9559 }
9560 break;
9561
9562 case TYPEOF_TYPE:
9563 case UNDERLYING_TYPE:
9564 if (pfd->include_nondeduced_p
9565 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9566 pfd->visited,
9567 pfd->include_nondeduced_p,
9568 pfd->any_fn))
9569 return error_mark_node;
9570 break;
9571
9572 case FUNCTION_DECL:
9573 case VAR_DECL:
9574 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9575 WALK_SUBTREE (DECL_TI_ARGS (t));
9576 /* Fall through. */
9577
9578 case PARM_DECL:
9579 case CONST_DECL:
9580 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9581 WALK_SUBTREE (DECL_INITIAL (t));
9582 if (DECL_CONTEXT (t)
9583 && pfd->include_nondeduced_p)
9584 WALK_SUBTREE (DECL_CONTEXT (t));
9585 break;
9586
9587 case BOUND_TEMPLATE_TEMPLATE_PARM:
9588 /* Record template parameters such as `T' inside `TT<T>'. */
9589 WALK_SUBTREE (TYPE_TI_ARGS (t));
9590 /* Fall through. */
9591
9592 case TEMPLATE_TEMPLATE_PARM:
9593 case TEMPLATE_TYPE_PARM:
9594 case TEMPLATE_PARM_INDEX:
9595 if (fn && (*fn)(t, data))
9596 return t;
9597 else if (!fn)
9598 return t;
9599 break;
9600
9601 case TEMPLATE_DECL:
9602 /* A template template parameter is encountered. */
9603 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9604 WALK_SUBTREE (TREE_TYPE (t));
9605
9606 /* Already substituted template template parameter */
9607 *walk_subtrees = 0;
9608 break;
9609
9610 case TYPENAME_TYPE:
9611 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9612 partial instantiation. */
9613 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9614 break;
9615
9616 case CONSTRUCTOR:
9617 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9618 && pfd->include_nondeduced_p)
9619 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9620 break;
9621
9622 case INDIRECT_REF:
9623 case COMPONENT_REF:
9624 /* If there's no type, then this thing must be some expression
9625 involving template parameters. */
9626 if (!fn && !TREE_TYPE (t))
9627 return error_mark_node;
9628 break;
9629
9630 case MODOP_EXPR:
9631 case CAST_EXPR:
9632 case IMPLICIT_CONV_EXPR:
9633 case REINTERPRET_CAST_EXPR:
9634 case CONST_CAST_EXPR:
9635 case STATIC_CAST_EXPR:
9636 case DYNAMIC_CAST_EXPR:
9637 case ARROW_EXPR:
9638 case DOTSTAR_EXPR:
9639 case TYPEID_EXPR:
9640 case PSEUDO_DTOR_EXPR:
9641 if (!fn)
9642 return error_mark_node;
9643 break;
9644
9645 default:
9646 break;
9647 }
9648
9649 #undef WALK_SUBTREE
9650
9651 /* We didn't find any template parameters we liked. */
9652 out:
9653 return result;
9654 }
9655
9656 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9657 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9658 call FN with the parameter and the DATA.
9659 If FN returns nonzero, the iteration is terminated, and
9660 for_each_template_parm returns 1. Otherwise, the iteration
9661 continues. If FN never returns a nonzero value, the value
9662 returned by for_each_template_parm is 0. If FN is NULL, it is
9663 considered to be the function which always returns 1.
9664
9665 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9666 parameters that occur in non-deduced contexts. When false, only
9667 visits those template parameters that can be deduced. */
9668
9669 static tree
9670 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9671 hash_set<tree> *visited,
9672 bool include_nondeduced_p,
9673 tree_fn_t any_fn)
9674 {
9675 struct pair_fn_data pfd;
9676 tree result;
9677
9678 /* Set up. */
9679 pfd.fn = fn;
9680 pfd.any_fn = any_fn;
9681 pfd.data = data;
9682 pfd.include_nondeduced_p = include_nondeduced_p;
9683
9684 /* Walk the tree. (Conceptually, we would like to walk without
9685 duplicates, but for_each_template_parm_r recursively calls
9686 for_each_template_parm, so we would need to reorganize a fair
9687 bit to use walk_tree_without_duplicates, so we keep our own
9688 visited list.) */
9689 if (visited)
9690 pfd.visited = visited;
9691 else
9692 pfd.visited = new hash_set<tree>;
9693 result = cp_walk_tree (&t,
9694 for_each_template_parm_r,
9695 &pfd,
9696 pfd.visited);
9697
9698 /* Clean up. */
9699 if (!visited)
9700 {
9701 delete pfd.visited;
9702 pfd.visited = 0;
9703 }
9704
9705 return result;
9706 }
9707
9708 /* Returns true if T depends on any template parameter. */
9709
9710 int
9711 uses_template_parms (tree t)
9712 {
9713 if (t == NULL_TREE)
9714 return false;
9715
9716 bool dependent_p;
9717 int saved_processing_template_decl;
9718
9719 saved_processing_template_decl = processing_template_decl;
9720 if (!saved_processing_template_decl)
9721 processing_template_decl = 1;
9722 if (TYPE_P (t))
9723 dependent_p = dependent_type_p (t);
9724 else if (TREE_CODE (t) == TREE_VEC)
9725 dependent_p = any_dependent_template_arguments_p (t);
9726 else if (TREE_CODE (t) == TREE_LIST)
9727 dependent_p = (uses_template_parms (TREE_VALUE (t))
9728 || uses_template_parms (TREE_CHAIN (t)));
9729 else if (TREE_CODE (t) == TYPE_DECL)
9730 dependent_p = dependent_type_p (TREE_TYPE (t));
9731 else if (DECL_P (t)
9732 || EXPR_P (t)
9733 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9734 || TREE_CODE (t) == OVERLOAD
9735 || BASELINK_P (t)
9736 || identifier_p (t)
9737 || TREE_CODE (t) == TRAIT_EXPR
9738 || TREE_CODE (t) == CONSTRUCTOR
9739 || CONSTANT_CLASS_P (t))
9740 dependent_p = (type_dependent_expression_p (t)
9741 || value_dependent_expression_p (t));
9742 else
9743 {
9744 gcc_assert (t == error_mark_node);
9745 dependent_p = false;
9746 }
9747
9748 processing_template_decl = saved_processing_template_decl;
9749
9750 return dependent_p;
9751 }
9752
9753 /* Returns true iff current_function_decl is an incompletely instantiated
9754 template. Useful instead of processing_template_decl because the latter
9755 is set to 0 during instantiate_non_dependent_expr. */
9756
9757 bool
9758 in_template_function (void)
9759 {
9760 tree fn = current_function_decl;
9761 bool ret;
9762 ++processing_template_decl;
9763 ret = (fn && DECL_LANG_SPECIFIC (fn)
9764 && DECL_TEMPLATE_INFO (fn)
9765 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9766 --processing_template_decl;
9767 return ret;
9768 }
9769
9770 /* Returns true if T depends on any template parameter with level LEVEL. */
9771
9772 bool
9773 uses_template_parms_level (tree t, int level)
9774 {
9775 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9776 /*include_nondeduced_p=*/true);
9777 }
9778
9779 /* Returns true if the signature of DECL depends on any template parameter from
9780 its enclosing class. */
9781
9782 bool
9783 uses_outer_template_parms (tree decl)
9784 {
9785 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9786 if (depth == 0)
9787 return false;
9788 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9789 &depth, NULL, /*include_nondeduced_p=*/true))
9790 return true;
9791 if (PRIMARY_TEMPLATE_P (decl)
9792 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9793 (DECL_TEMPLATE_PARMS (decl)),
9794 template_parm_outer_level,
9795 &depth, NULL, /*include_nondeduced_p=*/true))
9796 return true;
9797 tree ci = get_constraints (decl);
9798 if (ci)
9799 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9800 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9801 &depth, NULL, /*nondeduced*/true))
9802 return true;
9803 return false;
9804 }
9805
9806 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9807 ill-formed translation unit, i.e. a variable or function that isn't
9808 usable in a constant expression. */
9809
9810 static inline bool
9811 neglectable_inst_p (tree d)
9812 {
9813 return (DECL_P (d)
9814 && !undeduced_auto_decl (d)
9815 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9816 : decl_maybe_constant_var_p (d)));
9817 }
9818
9819 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9820 neglectable and instantiated from within an erroneous instantiation. */
9821
9822 static bool
9823 limit_bad_template_recursion (tree decl)
9824 {
9825 struct tinst_level *lev = current_tinst_level;
9826 int errs = errorcount + sorrycount;
9827 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9828 return false;
9829
9830 for (; lev; lev = lev->next)
9831 if (neglectable_inst_p (lev->decl))
9832 break;
9833
9834 return (lev && errs > lev->errors);
9835 }
9836
9837 static int tinst_depth;
9838 extern int max_tinst_depth;
9839 int depth_reached;
9840
9841 static GTY(()) struct tinst_level *last_error_tinst_level;
9842
9843 /* We're starting to instantiate D; record the template instantiation context
9844 for diagnostics and to restore it later. */
9845
9846 bool
9847 push_tinst_level (tree d)
9848 {
9849 return push_tinst_level_loc (d, input_location);
9850 }
9851
9852 /* We're starting to instantiate D; record the template instantiation context
9853 at LOC for diagnostics and to restore it later. */
9854
9855 bool
9856 push_tinst_level_loc (tree d, location_t loc)
9857 {
9858 struct tinst_level *new_level;
9859
9860 if (tinst_depth >= max_tinst_depth)
9861 {
9862 /* Tell error.c not to try to instantiate any templates. */
9863 at_eof = 2;
9864 fatal_error (input_location,
9865 "template instantiation depth exceeds maximum of %d"
9866 " (use -ftemplate-depth= to increase the maximum)",
9867 max_tinst_depth);
9868 return false;
9869 }
9870
9871 /* If the current instantiation caused problems, don't let it instantiate
9872 anything else. Do allow deduction substitution and decls usable in
9873 constant expressions. */
9874 if (limit_bad_template_recursion (d))
9875 return false;
9876
9877 /* When not -quiet, dump template instantiations other than functions, since
9878 announce_function will take care of those. */
9879 if (!quiet_flag
9880 && TREE_CODE (d) != TREE_LIST
9881 && TREE_CODE (d) != FUNCTION_DECL)
9882 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9883
9884 new_level = ggc_alloc<tinst_level> ();
9885 new_level->decl = d;
9886 new_level->locus = loc;
9887 new_level->errors = errorcount+sorrycount;
9888 new_level->in_system_header_p = in_system_header_at (input_location);
9889 new_level->next = current_tinst_level;
9890 current_tinst_level = new_level;
9891
9892 ++tinst_depth;
9893 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9894 depth_reached = tinst_depth;
9895
9896 return true;
9897 }
9898
9899 /* We're done instantiating this template; return to the instantiation
9900 context. */
9901
9902 void
9903 pop_tinst_level (void)
9904 {
9905 /* Restore the filename and line number stashed away when we started
9906 this instantiation. */
9907 input_location = current_tinst_level->locus;
9908 current_tinst_level = current_tinst_level->next;
9909 --tinst_depth;
9910 }
9911
9912 /* We're instantiating a deferred template; restore the template
9913 instantiation context in which the instantiation was requested, which
9914 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9915
9916 static tree
9917 reopen_tinst_level (struct tinst_level *level)
9918 {
9919 struct tinst_level *t;
9920
9921 tinst_depth = 0;
9922 for (t = level; t; t = t->next)
9923 ++tinst_depth;
9924
9925 current_tinst_level = level;
9926 pop_tinst_level ();
9927 if (current_tinst_level)
9928 current_tinst_level->errors = errorcount+sorrycount;
9929 return level->decl;
9930 }
9931
9932 /* Returns the TINST_LEVEL which gives the original instantiation
9933 context. */
9934
9935 struct tinst_level *
9936 outermost_tinst_level (void)
9937 {
9938 struct tinst_level *level = current_tinst_level;
9939 if (level)
9940 while (level->next)
9941 level = level->next;
9942 return level;
9943 }
9944
9945 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9946 vector of template arguments, as for tsubst.
9947
9948 Returns an appropriate tsubst'd friend declaration. */
9949
9950 static tree
9951 tsubst_friend_function (tree decl, tree args)
9952 {
9953 tree new_friend;
9954
9955 if (TREE_CODE (decl) == FUNCTION_DECL
9956 && DECL_TEMPLATE_INSTANTIATION (decl)
9957 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9958 /* This was a friend declared with an explicit template
9959 argument list, e.g.:
9960
9961 friend void f<>(T);
9962
9963 to indicate that f was a template instantiation, not a new
9964 function declaration. Now, we have to figure out what
9965 instantiation of what template. */
9966 {
9967 tree template_id, arglist, fns;
9968 tree new_args;
9969 tree tmpl;
9970 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9971
9972 /* Friend functions are looked up in the containing namespace scope.
9973 We must enter that scope, to avoid finding member functions of the
9974 current class with same name. */
9975 push_nested_namespace (ns);
9976 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9977 tf_warning_or_error, NULL_TREE,
9978 /*integral_constant_expression_p=*/false);
9979 pop_nested_namespace (ns);
9980 arglist = tsubst (DECL_TI_ARGS (decl), args,
9981 tf_warning_or_error, NULL_TREE);
9982 template_id = lookup_template_function (fns, arglist);
9983
9984 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9985 tmpl = determine_specialization (template_id, new_friend,
9986 &new_args,
9987 /*need_member_template=*/0,
9988 TREE_VEC_LENGTH (args),
9989 tsk_none);
9990 return instantiate_template (tmpl, new_args, tf_error);
9991 }
9992
9993 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9994
9995 /* The NEW_FRIEND will look like an instantiation, to the
9996 compiler, but is not an instantiation from the point of view of
9997 the language. For example, we might have had:
9998
9999 template <class T> struct S {
10000 template <class U> friend void f(T, U);
10001 };
10002
10003 Then, in S<int>, template <class U> void f(int, U) is not an
10004 instantiation of anything. */
10005 if (new_friend == error_mark_node)
10006 return error_mark_node;
10007
10008 DECL_USE_TEMPLATE (new_friend) = 0;
10009 if (TREE_CODE (decl) == TEMPLATE_DECL)
10010 {
10011 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10012 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10013 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10014 }
10015
10016 /* The mangled name for the NEW_FRIEND is incorrect. The function
10017 is not a template instantiation and should not be mangled like
10018 one. Therefore, we forget the mangling here; we'll recompute it
10019 later if we need it. */
10020 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10021 {
10022 SET_DECL_RTL (new_friend, NULL);
10023 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10024 }
10025
10026 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10027 {
10028 tree old_decl;
10029 tree new_friend_template_info;
10030 tree new_friend_result_template_info;
10031 tree ns;
10032 int new_friend_is_defn;
10033
10034 /* We must save some information from NEW_FRIEND before calling
10035 duplicate decls since that function will free NEW_FRIEND if
10036 possible. */
10037 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10038 new_friend_is_defn =
10039 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10040 (template_for_substitution (new_friend)))
10041 != NULL_TREE);
10042 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10043 {
10044 /* This declaration is a `primary' template. */
10045 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10046
10047 new_friend_result_template_info
10048 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10049 }
10050 else
10051 new_friend_result_template_info = NULL_TREE;
10052
10053 /* Inside pushdecl_namespace_level, we will push into the
10054 current namespace. However, the friend function should go
10055 into the namespace of the template. */
10056 ns = decl_namespace_context (new_friend);
10057 push_nested_namespace (ns);
10058 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10059 pop_nested_namespace (ns);
10060
10061 if (old_decl == error_mark_node)
10062 return error_mark_node;
10063
10064 if (old_decl != new_friend)
10065 {
10066 /* This new friend declaration matched an existing
10067 declaration. For example, given:
10068
10069 template <class T> void f(T);
10070 template <class U> class C {
10071 template <class T> friend void f(T) {}
10072 };
10073
10074 the friend declaration actually provides the definition
10075 of `f', once C has been instantiated for some type. So,
10076 old_decl will be the out-of-class template declaration,
10077 while new_friend is the in-class definition.
10078
10079 But, if `f' was called before this point, the
10080 instantiation of `f' will have DECL_TI_ARGS corresponding
10081 to `T' but not to `U', references to which might appear
10082 in the definition of `f'. Previously, the most general
10083 template for an instantiation of `f' was the out-of-class
10084 version; now it is the in-class version. Therefore, we
10085 run through all specialization of `f', adding to their
10086 DECL_TI_ARGS appropriately. In particular, they need a
10087 new set of outer arguments, corresponding to the
10088 arguments for this class instantiation.
10089
10090 The same situation can arise with something like this:
10091
10092 friend void f(int);
10093 template <class T> class C {
10094 friend void f(T) {}
10095 };
10096
10097 when `C<int>' is instantiated. Now, `f(int)' is defined
10098 in the class. */
10099
10100 if (!new_friend_is_defn)
10101 /* On the other hand, if the in-class declaration does
10102 *not* provide a definition, then we don't want to alter
10103 existing definitions. We can just leave everything
10104 alone. */
10105 ;
10106 else
10107 {
10108 tree new_template = TI_TEMPLATE (new_friend_template_info);
10109 tree new_args = TI_ARGS (new_friend_template_info);
10110
10111 /* Overwrite whatever template info was there before, if
10112 any, with the new template information pertaining to
10113 the declaration. */
10114 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10115
10116 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10117 {
10118 /* We should have called reregister_specialization in
10119 duplicate_decls. */
10120 gcc_assert (retrieve_specialization (new_template,
10121 new_args, 0)
10122 == old_decl);
10123
10124 /* Instantiate it if the global has already been used. */
10125 if (DECL_ODR_USED (old_decl))
10126 instantiate_decl (old_decl, /*defer_ok=*/true,
10127 /*expl_inst_class_mem_p=*/false);
10128 }
10129 else
10130 {
10131 tree t;
10132
10133 /* Indicate that the old function template is a partial
10134 instantiation. */
10135 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10136 = new_friend_result_template_info;
10137
10138 gcc_assert (new_template
10139 == most_general_template (new_template));
10140 gcc_assert (new_template != old_decl);
10141
10142 /* Reassign any specializations already in the hash table
10143 to the new more general template, and add the
10144 additional template args. */
10145 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10146 t != NULL_TREE;
10147 t = TREE_CHAIN (t))
10148 {
10149 tree spec = TREE_VALUE (t);
10150 spec_entry elt;
10151
10152 elt.tmpl = old_decl;
10153 elt.args = DECL_TI_ARGS (spec);
10154 elt.spec = NULL_TREE;
10155
10156 decl_specializations->remove_elt (&elt);
10157
10158 DECL_TI_ARGS (spec)
10159 = add_outermost_template_args (new_args,
10160 DECL_TI_ARGS (spec));
10161
10162 register_specialization
10163 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10164
10165 }
10166 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10167 }
10168 }
10169
10170 /* The information from NEW_FRIEND has been merged into OLD_DECL
10171 by duplicate_decls. */
10172 new_friend = old_decl;
10173 }
10174 }
10175 else
10176 {
10177 tree context = DECL_CONTEXT (new_friend);
10178 bool dependent_p;
10179
10180 /* In the code
10181 template <class T> class C {
10182 template <class U> friend void C1<U>::f (); // case 1
10183 friend void C2<T>::f (); // case 2
10184 };
10185 we only need to make sure CONTEXT is a complete type for
10186 case 2. To distinguish between the two cases, we note that
10187 CONTEXT of case 1 remains dependent type after tsubst while
10188 this isn't true for case 2. */
10189 ++processing_template_decl;
10190 dependent_p = dependent_type_p (context);
10191 --processing_template_decl;
10192
10193 if (!dependent_p
10194 && !complete_type_or_else (context, NULL_TREE))
10195 return error_mark_node;
10196
10197 if (COMPLETE_TYPE_P (context))
10198 {
10199 tree fn = new_friend;
10200 /* do_friend adds the TEMPLATE_DECL for any member friend
10201 template even if it isn't a member template, i.e.
10202 template <class T> friend A<T>::f();
10203 Look through it in that case. */
10204 if (TREE_CODE (fn) == TEMPLATE_DECL
10205 && !PRIMARY_TEMPLATE_P (fn))
10206 fn = DECL_TEMPLATE_RESULT (fn);
10207 /* Check to see that the declaration is really present, and,
10208 possibly obtain an improved declaration. */
10209 fn = check_classfn (context, fn, NULL_TREE);
10210
10211 if (fn)
10212 new_friend = fn;
10213 }
10214 }
10215
10216 return new_friend;
10217 }
10218
10219 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10220 template arguments, as for tsubst.
10221
10222 Returns an appropriate tsubst'd friend type or error_mark_node on
10223 failure. */
10224
10225 static tree
10226 tsubst_friend_class (tree friend_tmpl, tree args)
10227 {
10228 tree tmpl;
10229
10230 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10231 {
10232 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10233 return TREE_TYPE (tmpl);
10234 }
10235
10236 tree context = CP_DECL_CONTEXT (friend_tmpl);
10237 if (TREE_CODE (context) == NAMESPACE_DECL)
10238 push_nested_namespace (context);
10239 else
10240 push_nested_class (context);
10241
10242 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10243 /*non_class=*/false, /*block_p=*/false,
10244 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10245
10246 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10247 {
10248 /* The friend template has already been declared. Just
10249 check to see that the declarations match, and install any new
10250 default parameters. We must tsubst the default parameters,
10251 of course. We only need the innermost template parameters
10252 because that is all that redeclare_class_template will look
10253 at. */
10254 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10255 > TMPL_ARGS_DEPTH (args))
10256 {
10257 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10258 args, tf_warning_or_error);
10259 location_t saved_input_location = input_location;
10260 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10261 tree cons = get_constraints (tmpl);
10262 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10263 input_location = saved_input_location;
10264 }
10265 }
10266 else
10267 {
10268 /* The friend template has not already been declared. In this
10269 case, the instantiation of the template class will cause the
10270 injection of this template into the namespace scope. */
10271 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10272
10273 if (tmpl != error_mark_node)
10274 {
10275 /* The new TMPL is not an instantiation of anything, so we
10276 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10277 for the new type because that is supposed to be the
10278 corresponding template decl, i.e., TMPL. */
10279 DECL_USE_TEMPLATE (tmpl) = 0;
10280 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10281 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10282 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10283 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10284
10285 /* It is hidden. */
10286 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10287 DECL_ANTICIPATED (tmpl)
10288 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10289
10290 /* Inject this template into the enclosing namspace scope. */
10291 tmpl = pushdecl_namespace_level (tmpl, true);
10292 }
10293 }
10294
10295 if (TREE_CODE (context) == NAMESPACE_DECL)
10296 pop_nested_namespace (context);
10297 else
10298 pop_nested_class ();
10299
10300 return TREE_TYPE (tmpl);
10301 }
10302
10303 /* Returns zero if TYPE cannot be completed later due to circularity.
10304 Otherwise returns one. */
10305
10306 static int
10307 can_complete_type_without_circularity (tree type)
10308 {
10309 if (type == NULL_TREE || type == error_mark_node)
10310 return 0;
10311 else if (COMPLETE_TYPE_P (type))
10312 return 1;
10313 else if (TREE_CODE (type) == ARRAY_TYPE)
10314 return can_complete_type_without_circularity (TREE_TYPE (type));
10315 else if (CLASS_TYPE_P (type)
10316 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10317 return 0;
10318 else
10319 return 1;
10320 }
10321
10322 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10323 tsubst_flags_t, tree);
10324
10325 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10326 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10327
10328 static tree
10329 tsubst_attribute (tree t, tree *decl_p, tree args,
10330 tsubst_flags_t complain, tree in_decl)
10331 {
10332 gcc_assert (ATTR_IS_DEPENDENT (t));
10333
10334 tree val = TREE_VALUE (t);
10335 if (val == NULL_TREE)
10336 /* Nothing to do. */;
10337 else if ((flag_openmp || flag_openmp_simd)
10338 && is_attribute_p ("omp declare simd",
10339 get_attribute_name (t)))
10340 {
10341 tree clauses = TREE_VALUE (val);
10342 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10343 complain, in_decl);
10344 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10345 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10346 tree parms = DECL_ARGUMENTS (*decl_p);
10347 clauses
10348 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10349 if (clauses)
10350 val = build_tree_list (NULL_TREE, clauses);
10351 else
10352 val = NULL_TREE;
10353 }
10354 /* If the first attribute argument is an identifier, don't
10355 pass it through tsubst. Attributes like mode, format,
10356 cleanup and several target specific attributes expect it
10357 unmodified. */
10358 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10359 {
10360 tree chain
10361 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10362 /*integral_constant_expression_p=*/false);
10363 if (chain != TREE_CHAIN (val))
10364 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10365 }
10366 else if (PACK_EXPANSION_P (val))
10367 {
10368 /* An attribute pack expansion. */
10369 tree purp = TREE_PURPOSE (t);
10370 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10371 if (pack == error_mark_node)
10372 return error_mark_node;
10373 int len = TREE_VEC_LENGTH (pack);
10374 tree list = NULL_TREE;
10375 tree *q = &list;
10376 for (int i = 0; i < len; ++i)
10377 {
10378 tree elt = TREE_VEC_ELT (pack, i);
10379 *q = build_tree_list (purp, elt);
10380 q = &TREE_CHAIN (*q);
10381 }
10382 return list;
10383 }
10384 else
10385 val = tsubst_expr (val, args, complain, in_decl,
10386 /*integral_constant_expression_p=*/false);
10387
10388 if (val != TREE_VALUE (t))
10389 return build_tree_list (TREE_PURPOSE (t), val);
10390 return t;
10391 }
10392
10393 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10394 unchanged or a new TREE_LIST chain. */
10395
10396 static tree
10397 tsubst_attributes (tree attributes, tree args,
10398 tsubst_flags_t complain, tree in_decl)
10399 {
10400 tree last_dep = NULL_TREE;
10401
10402 for (tree t = attributes; t; t = TREE_CHAIN (t))
10403 if (ATTR_IS_DEPENDENT (t))
10404 {
10405 last_dep = t;
10406 attributes = copy_list (attributes);
10407 break;
10408 }
10409
10410 if (last_dep)
10411 for (tree *p = &attributes; *p; )
10412 {
10413 tree t = *p;
10414 if (ATTR_IS_DEPENDENT (t))
10415 {
10416 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10417 if (subst != t)
10418 {
10419 *p = subst;
10420 while (*p)
10421 p = &TREE_CHAIN (*p);
10422 *p = TREE_CHAIN (t);
10423 continue;
10424 }
10425 }
10426 p = &TREE_CHAIN (*p);
10427 }
10428
10429 return attributes;
10430 }
10431
10432 /* Apply any attributes which had to be deferred until instantiation
10433 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10434 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10435
10436 static void
10437 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10438 tree args, tsubst_flags_t complain, tree in_decl)
10439 {
10440 tree last_dep = NULL_TREE;
10441 tree t;
10442 tree *p;
10443
10444 if (attributes == NULL_TREE)
10445 return;
10446
10447 if (DECL_P (*decl_p))
10448 {
10449 if (TREE_TYPE (*decl_p) == error_mark_node)
10450 return;
10451 p = &DECL_ATTRIBUTES (*decl_p);
10452 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10453 to our attributes parameter. */
10454 gcc_assert (*p == attributes);
10455 }
10456 else
10457 {
10458 p = &TYPE_ATTRIBUTES (*decl_p);
10459 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10460 lookup_template_class_1, and should be preserved. */
10461 gcc_assert (*p != attributes);
10462 while (*p)
10463 p = &TREE_CHAIN (*p);
10464 }
10465
10466 for (t = attributes; t; t = TREE_CHAIN (t))
10467 if (ATTR_IS_DEPENDENT (t))
10468 {
10469 last_dep = t;
10470 attributes = copy_list (attributes);
10471 break;
10472 }
10473
10474 *p = attributes;
10475 if (last_dep)
10476 {
10477 tree late_attrs = NULL_TREE;
10478 tree *q = &late_attrs;
10479
10480 for (; *p; )
10481 {
10482 t = *p;
10483 if (ATTR_IS_DEPENDENT (t))
10484 {
10485 *p = TREE_CHAIN (t);
10486 TREE_CHAIN (t) = NULL_TREE;
10487 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10488 while (*q)
10489 q = &TREE_CHAIN (*q);
10490 }
10491 else
10492 p = &TREE_CHAIN (t);
10493 }
10494
10495 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10496 }
10497 }
10498
10499 /* Perform (or defer) access check for typedefs that were referenced
10500 from within the template TMPL code.
10501 This is a subroutine of instantiate_decl and instantiate_class_template.
10502 TMPL is the template to consider and TARGS is the list of arguments of
10503 that template. */
10504
10505 static void
10506 perform_typedefs_access_check (tree tmpl, tree targs)
10507 {
10508 location_t saved_location;
10509 unsigned i;
10510 qualified_typedef_usage_t *iter;
10511
10512 if (!tmpl
10513 || (!CLASS_TYPE_P (tmpl)
10514 && TREE_CODE (tmpl) != FUNCTION_DECL))
10515 return;
10516
10517 saved_location = input_location;
10518 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10519 {
10520 tree type_decl = iter->typedef_decl;
10521 tree type_scope = iter->context;
10522
10523 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10524 continue;
10525
10526 if (uses_template_parms (type_decl))
10527 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10528 if (uses_template_parms (type_scope))
10529 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10530
10531 /* Make access check error messages point to the location
10532 of the use of the typedef. */
10533 input_location = iter->locus;
10534 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10535 type_decl, type_decl,
10536 tf_warning_or_error);
10537 }
10538 input_location = saved_location;
10539 }
10540
10541 static tree
10542 instantiate_class_template_1 (tree type)
10543 {
10544 tree templ, args, pattern, t, member;
10545 tree typedecl;
10546 tree pbinfo;
10547 tree base_list;
10548 unsigned int saved_maximum_field_alignment;
10549 tree fn_context;
10550
10551 if (type == error_mark_node)
10552 return error_mark_node;
10553
10554 if (COMPLETE_OR_OPEN_TYPE_P (type)
10555 || uses_template_parms (type))
10556 return type;
10557
10558 /* Figure out which template is being instantiated. */
10559 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10560 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10561
10562 /* Mark the type as in the process of being defined. */
10563 TYPE_BEING_DEFINED (type) = 1;
10564
10565 /* Determine what specialization of the original template to
10566 instantiate. */
10567 t = most_specialized_partial_spec (type, tf_warning_or_error);
10568 if (t == error_mark_node)
10569 return error_mark_node;
10570 else if (t)
10571 {
10572 /* This TYPE is actually an instantiation of a partial
10573 specialization. We replace the innermost set of ARGS with
10574 the arguments appropriate for substitution. For example,
10575 given:
10576
10577 template <class T> struct S {};
10578 template <class T> struct S<T*> {};
10579
10580 and supposing that we are instantiating S<int*>, ARGS will
10581 presently be {int*} -- but we need {int}. */
10582 pattern = TREE_TYPE (t);
10583 args = TREE_PURPOSE (t);
10584 }
10585 else
10586 {
10587 pattern = TREE_TYPE (templ);
10588 args = CLASSTYPE_TI_ARGS (type);
10589 }
10590
10591 /* If the template we're instantiating is incomplete, then clearly
10592 there's nothing we can do. */
10593 if (!COMPLETE_TYPE_P (pattern))
10594 {
10595 /* We can try again later. */
10596 TYPE_BEING_DEFINED (type) = 0;
10597 return type;
10598 }
10599
10600 /* If we've recursively instantiated too many templates, stop. */
10601 if (! push_tinst_level (type))
10602 return type;
10603
10604 /* We may be in the middle of deferred access check. Disable
10605 it now. */
10606 push_deferring_access_checks (dk_no_deferred);
10607
10608 int saved_unevaluated_operand = cp_unevaluated_operand;
10609 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10610
10611 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10612 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10613 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10614 fn_context = error_mark_node;
10615 if (!fn_context)
10616 push_to_top_level ();
10617 else
10618 {
10619 cp_unevaluated_operand = 0;
10620 c_inhibit_evaluation_warnings = 0;
10621 }
10622 /* Use #pragma pack from the template context. */
10623 saved_maximum_field_alignment = maximum_field_alignment;
10624 maximum_field_alignment = TYPE_PRECISION (pattern);
10625
10626 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10627
10628 /* Set the input location to the most specialized template definition.
10629 This is needed if tsubsting causes an error. */
10630 typedecl = TYPE_MAIN_DECL (pattern);
10631 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10632 DECL_SOURCE_LOCATION (typedecl);
10633
10634 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10635 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10636 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10637 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10638 if (ANON_AGGR_TYPE_P (pattern))
10639 SET_ANON_AGGR_TYPE_P (type);
10640 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10641 {
10642 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10643 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10644 /* Adjust visibility for template arguments. */
10645 determine_visibility (TYPE_MAIN_DECL (type));
10646 }
10647 if (CLASS_TYPE_P (type))
10648 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10649
10650 pbinfo = TYPE_BINFO (pattern);
10651
10652 /* We should never instantiate a nested class before its enclosing
10653 class; we need to look up the nested class by name before we can
10654 instantiate it, and that lookup should instantiate the enclosing
10655 class. */
10656 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10657 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10658
10659 base_list = NULL_TREE;
10660 if (BINFO_N_BASE_BINFOS (pbinfo))
10661 {
10662 tree pbase_binfo;
10663 tree pushed_scope;
10664 int i;
10665
10666 /* We must enter the scope containing the type, as that is where
10667 the accessibility of types named in dependent bases are
10668 looked up from. */
10669 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10670
10671 /* Substitute into each of the bases to determine the actual
10672 basetypes. */
10673 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10674 {
10675 tree base;
10676 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10677 tree expanded_bases = NULL_TREE;
10678 int idx, len = 1;
10679
10680 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10681 {
10682 expanded_bases =
10683 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10684 args, tf_error, NULL_TREE);
10685 if (expanded_bases == error_mark_node)
10686 continue;
10687
10688 len = TREE_VEC_LENGTH (expanded_bases);
10689 }
10690
10691 for (idx = 0; idx < len; idx++)
10692 {
10693 if (expanded_bases)
10694 /* Extract the already-expanded base class. */
10695 base = TREE_VEC_ELT (expanded_bases, idx);
10696 else
10697 /* Substitute to figure out the base class. */
10698 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10699 NULL_TREE);
10700
10701 if (base == error_mark_node)
10702 continue;
10703
10704 base_list = tree_cons (access, base, base_list);
10705 if (BINFO_VIRTUAL_P (pbase_binfo))
10706 TREE_TYPE (base_list) = integer_type_node;
10707 }
10708 }
10709
10710 /* The list is now in reverse order; correct that. */
10711 base_list = nreverse (base_list);
10712
10713 if (pushed_scope)
10714 pop_scope (pushed_scope);
10715 }
10716 /* Now call xref_basetypes to set up all the base-class
10717 information. */
10718 xref_basetypes (type, base_list);
10719
10720 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10721 (int) ATTR_FLAG_TYPE_IN_PLACE,
10722 args, tf_error, NULL_TREE);
10723 fixup_attribute_variants (type);
10724
10725 /* Now that our base classes are set up, enter the scope of the
10726 class, so that name lookups into base classes, etc. will work
10727 correctly. This is precisely analogous to what we do in
10728 begin_class_definition when defining an ordinary non-template
10729 class, except we also need to push the enclosing classes. */
10730 push_nested_class (type);
10731
10732 /* Now members are processed in the order of declaration. */
10733 for (member = CLASSTYPE_DECL_LIST (pattern);
10734 member; member = TREE_CHAIN (member))
10735 {
10736 tree t = TREE_VALUE (member);
10737
10738 if (TREE_PURPOSE (member))
10739 {
10740 if (TYPE_P (t))
10741 {
10742 if (LAMBDA_TYPE_P (t))
10743 /* A closure type for a lambda in an NSDMI or default argument.
10744 Ignore it; it will be regenerated when needed. */
10745 continue;
10746
10747 /* Build new CLASSTYPE_NESTED_UTDS. */
10748
10749 tree newtag;
10750 bool class_template_p;
10751
10752 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10753 && TYPE_LANG_SPECIFIC (t)
10754 && CLASSTYPE_IS_TEMPLATE (t));
10755 /* If the member is a class template, then -- even after
10756 substitution -- there may be dependent types in the
10757 template argument list for the class. We increment
10758 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10759 that function will assume that no types are dependent
10760 when outside of a template. */
10761 if (class_template_p)
10762 ++processing_template_decl;
10763 newtag = tsubst (t, args, tf_error, NULL_TREE);
10764 if (class_template_p)
10765 --processing_template_decl;
10766 if (newtag == error_mark_node)
10767 continue;
10768
10769 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10770 {
10771 tree name = TYPE_IDENTIFIER (t);
10772
10773 if (class_template_p)
10774 /* Unfortunately, lookup_template_class sets
10775 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10776 instantiation (i.e., for the type of a member
10777 template class nested within a template class.)
10778 This behavior is required for
10779 maybe_process_partial_specialization to work
10780 correctly, but is not accurate in this case;
10781 the TAG is not an instantiation of anything.
10782 (The corresponding TEMPLATE_DECL is an
10783 instantiation, but the TYPE is not.) */
10784 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10785
10786 /* Now, we call pushtag to put this NEWTAG into the scope of
10787 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10788 pushtag calling push_template_decl. We don't have to do
10789 this for enums because it will already have been done in
10790 tsubst_enum. */
10791 if (name)
10792 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10793 pushtag (name, newtag, /*tag_scope=*/ts_current);
10794 }
10795 }
10796 else if (DECL_DECLARES_FUNCTION_P (t))
10797 {
10798 tree r;
10799
10800 if (TREE_CODE (t) == TEMPLATE_DECL)
10801 ++processing_template_decl;
10802 r = tsubst (t, args, tf_error, NULL_TREE);
10803 if (TREE_CODE (t) == TEMPLATE_DECL)
10804 --processing_template_decl;
10805 set_current_access_from_decl (r);
10806 finish_member_declaration (r);
10807 /* Instantiate members marked with attribute used. */
10808 if (r != error_mark_node && DECL_PRESERVE_P (r))
10809 mark_used (r);
10810 if (TREE_CODE (r) == FUNCTION_DECL
10811 && DECL_OMP_DECLARE_REDUCTION_P (r))
10812 cp_check_omp_declare_reduction (r);
10813 }
10814 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10815 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10816 /* A closure type for a lambda in an NSDMI or default argument.
10817 Ignore it; it will be regenerated when needed. */;
10818 else
10819 {
10820 /* Build new TYPE_FIELDS. */
10821 if (TREE_CODE (t) == STATIC_ASSERT)
10822 {
10823 tree condition;
10824
10825 ++c_inhibit_evaluation_warnings;
10826 condition =
10827 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10828 tf_warning_or_error, NULL_TREE,
10829 /*integral_constant_expression_p=*/true);
10830 --c_inhibit_evaluation_warnings;
10831
10832 finish_static_assert (condition,
10833 STATIC_ASSERT_MESSAGE (t),
10834 STATIC_ASSERT_SOURCE_LOCATION (t),
10835 /*member_p=*/true);
10836 }
10837 else if (TREE_CODE (t) != CONST_DECL)
10838 {
10839 tree r;
10840 tree vec = NULL_TREE;
10841 int len = 1;
10842
10843 /* The file and line for this declaration, to
10844 assist in error message reporting. Since we
10845 called push_tinst_level above, we don't need to
10846 restore these. */
10847 input_location = DECL_SOURCE_LOCATION (t);
10848
10849 if (TREE_CODE (t) == TEMPLATE_DECL)
10850 ++processing_template_decl;
10851 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10852 if (TREE_CODE (t) == TEMPLATE_DECL)
10853 --processing_template_decl;
10854
10855 if (TREE_CODE (r) == TREE_VEC)
10856 {
10857 /* A capture pack became multiple fields. */
10858 vec = r;
10859 len = TREE_VEC_LENGTH (vec);
10860 }
10861
10862 for (int i = 0; i < len; ++i)
10863 {
10864 if (vec)
10865 r = TREE_VEC_ELT (vec, i);
10866 if (VAR_P (r))
10867 {
10868 /* In [temp.inst]:
10869
10870 [t]he initialization (and any associated
10871 side-effects) of a static data member does
10872 not occur unless the static data member is
10873 itself used in a way that requires the
10874 definition of the static data member to
10875 exist.
10876
10877 Therefore, we do not substitute into the
10878 initialized for the static data member here. */
10879 finish_static_data_member_decl
10880 (r,
10881 /*init=*/NULL_TREE,
10882 /*init_const_expr_p=*/false,
10883 /*asmspec_tree=*/NULL_TREE,
10884 /*flags=*/0);
10885 /* Instantiate members marked with attribute used. */
10886 if (r != error_mark_node && DECL_PRESERVE_P (r))
10887 mark_used (r);
10888 }
10889 else if (TREE_CODE (r) == FIELD_DECL)
10890 {
10891 /* Determine whether R has a valid type and can be
10892 completed later. If R is invalid, then its type
10893 is replaced by error_mark_node. */
10894 tree rtype = TREE_TYPE (r);
10895 if (can_complete_type_without_circularity (rtype))
10896 complete_type (rtype);
10897
10898 if (!complete_or_array_type_p (rtype))
10899 {
10900 /* If R's type couldn't be completed and
10901 it isn't a flexible array member (whose
10902 type is incomplete by definition) give
10903 an error. */
10904 cxx_incomplete_type_error (r, rtype);
10905 TREE_TYPE (r) = error_mark_node;
10906 }
10907 }
10908
10909 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10910 such a thing will already have been added to the field
10911 list by tsubst_enum in finish_member_declaration in the
10912 CLASSTYPE_NESTED_UTDS case above. */
10913 if (!(TREE_CODE (r) == TYPE_DECL
10914 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10915 && DECL_ARTIFICIAL (r)))
10916 {
10917 set_current_access_from_decl (r);
10918 finish_member_declaration (r);
10919 }
10920 }
10921 }
10922 }
10923 }
10924 else
10925 {
10926 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10927 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10928 {
10929 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10930
10931 tree friend_type = t;
10932 bool adjust_processing_template_decl = false;
10933
10934 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10935 {
10936 /* template <class T> friend class C; */
10937 friend_type = tsubst_friend_class (friend_type, args);
10938 adjust_processing_template_decl = true;
10939 }
10940 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10941 {
10942 /* template <class T> friend class C::D; */
10943 friend_type = tsubst (friend_type, args,
10944 tf_warning_or_error, NULL_TREE);
10945 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10946 friend_type = TREE_TYPE (friend_type);
10947 adjust_processing_template_decl = true;
10948 }
10949 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10950 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10951 {
10952 /* This could be either
10953
10954 friend class T::C;
10955
10956 when dependent_type_p is false or
10957
10958 template <class U> friend class T::C;
10959
10960 otherwise. */
10961 /* Bump processing_template_decl in case this is something like
10962 template <class T> friend struct A<T>::B. */
10963 ++processing_template_decl;
10964 friend_type = tsubst (friend_type, args,
10965 tf_warning_or_error, NULL_TREE);
10966 if (dependent_type_p (friend_type))
10967 adjust_processing_template_decl = true;
10968 --processing_template_decl;
10969 }
10970 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10971 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10972 && TYPE_HIDDEN_P (friend_type))
10973 {
10974 /* friend class C;
10975
10976 where C hasn't been declared yet. Let's lookup name
10977 from namespace scope directly, bypassing any name that
10978 come from dependent base class. */
10979 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10980
10981 /* The call to xref_tag_from_type does injection for friend
10982 classes. */
10983 push_nested_namespace (ns);
10984 friend_type =
10985 xref_tag_from_type (friend_type, NULL_TREE,
10986 /*tag_scope=*/ts_current);
10987 pop_nested_namespace (ns);
10988 }
10989 else if (uses_template_parms (friend_type))
10990 /* friend class C<T>; */
10991 friend_type = tsubst (friend_type, args,
10992 tf_warning_or_error, NULL_TREE);
10993 /* Otherwise it's
10994
10995 friend class C;
10996
10997 where C is already declared or
10998
10999 friend class C<int>;
11000
11001 We don't have to do anything in these cases. */
11002
11003 if (adjust_processing_template_decl)
11004 /* Trick make_friend_class into realizing that the friend
11005 we're adding is a template, not an ordinary class. It's
11006 important that we use make_friend_class since it will
11007 perform some error-checking and output cross-reference
11008 information. */
11009 ++processing_template_decl;
11010
11011 if (friend_type != error_mark_node)
11012 make_friend_class (type, friend_type, /*complain=*/false);
11013
11014 if (adjust_processing_template_decl)
11015 --processing_template_decl;
11016 }
11017 else
11018 {
11019 /* Build new DECL_FRIENDLIST. */
11020 tree r;
11021
11022 /* The file and line for this declaration, to
11023 assist in error message reporting. Since we
11024 called push_tinst_level above, we don't need to
11025 restore these. */
11026 input_location = DECL_SOURCE_LOCATION (t);
11027
11028 if (TREE_CODE (t) == TEMPLATE_DECL)
11029 {
11030 ++processing_template_decl;
11031 push_deferring_access_checks (dk_no_check);
11032 }
11033
11034 r = tsubst_friend_function (t, args);
11035 add_friend (type, r, /*complain=*/false);
11036 if (TREE_CODE (t) == TEMPLATE_DECL)
11037 {
11038 pop_deferring_access_checks ();
11039 --processing_template_decl;
11040 }
11041 }
11042 }
11043 }
11044
11045 if (fn_context)
11046 {
11047 /* Restore these before substituting into the lambda capture
11048 initializers. */
11049 cp_unevaluated_operand = saved_unevaluated_operand;
11050 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11051 }
11052
11053 /* Set the file and line number information to whatever is given for
11054 the class itself. This puts error messages involving generated
11055 implicit functions at a predictable point, and the same point
11056 that would be used for non-template classes. */
11057 input_location = DECL_SOURCE_LOCATION (typedecl);
11058
11059 unreverse_member_declarations (type);
11060 finish_struct_1 (type);
11061 TYPE_BEING_DEFINED (type) = 0;
11062
11063 /* We don't instantiate default arguments for member functions. 14.7.1:
11064
11065 The implicit instantiation of a class template specialization causes
11066 the implicit instantiation of the declarations, but not of the
11067 definitions or default arguments, of the class member functions,
11068 member classes, static data members and member templates.... */
11069
11070 /* Some typedefs referenced from within the template code need to be access
11071 checked at template instantiation time, i.e now. These types were
11072 added to the template at parsing time. Let's get those and perform
11073 the access checks then. */
11074 perform_typedefs_access_check (pattern, args);
11075 perform_deferred_access_checks (tf_warning_or_error);
11076 pop_nested_class ();
11077 maximum_field_alignment = saved_maximum_field_alignment;
11078 if (!fn_context)
11079 pop_from_top_level ();
11080 pop_deferring_access_checks ();
11081 pop_tinst_level ();
11082
11083 /* The vtable for a template class can be emitted in any translation
11084 unit in which the class is instantiated. When there is no key
11085 method, however, finish_struct_1 will already have added TYPE to
11086 the keyed_classes. */
11087 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11088 vec_safe_push (keyed_classes, type);
11089
11090 return type;
11091 }
11092
11093 /* Wrapper for instantiate_class_template_1. */
11094
11095 tree
11096 instantiate_class_template (tree type)
11097 {
11098 tree ret;
11099 timevar_push (TV_TEMPLATE_INST);
11100 ret = instantiate_class_template_1 (type);
11101 timevar_pop (TV_TEMPLATE_INST);
11102 return ret;
11103 }
11104
11105 static tree
11106 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11107 {
11108 tree r;
11109
11110 if (!t)
11111 r = t;
11112 else if (TYPE_P (t))
11113 r = tsubst (t, args, complain, in_decl);
11114 else
11115 {
11116 if (!(complain & tf_warning))
11117 ++c_inhibit_evaluation_warnings;
11118 r = tsubst_expr (t, args, complain, in_decl,
11119 /*integral_constant_expression_p=*/true);
11120 if (!(complain & tf_warning))
11121 --c_inhibit_evaluation_warnings;
11122 }
11123 return r;
11124 }
11125
11126 /* Given a function parameter pack TMPL_PARM and some function parameters
11127 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11128 and set *SPEC_P to point at the next point in the list. */
11129
11130 tree
11131 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11132 {
11133 /* Collect all of the extra "packed" parameters into an
11134 argument pack. */
11135 tree parmvec;
11136 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11137 tree spec_parm = *spec_p;
11138 int i, len;
11139
11140 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11141 if (tmpl_parm
11142 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11143 break;
11144
11145 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11146 parmvec = make_tree_vec (len);
11147 spec_parm = *spec_p;
11148 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11149 {
11150 tree elt = spec_parm;
11151 if (DECL_PACK_P (elt))
11152 elt = make_pack_expansion (elt);
11153 TREE_VEC_ELT (parmvec, i) = elt;
11154 }
11155
11156 /* Build the argument packs. */
11157 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11158 *spec_p = spec_parm;
11159
11160 return argpack;
11161 }
11162
11163 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11164 NONTYPE_ARGUMENT_PACK. */
11165
11166 static tree
11167 make_fnparm_pack (tree spec_parm)
11168 {
11169 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11170 }
11171
11172 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11173 pack expansion with no extra args, 2 if it has extra args, or 0
11174 if it is not a pack expansion. */
11175
11176 static int
11177 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11178 {
11179 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11180 if (i >= TREE_VEC_LENGTH (vec))
11181 return 0;
11182 tree elt = TREE_VEC_ELT (vec, i);
11183 if (DECL_P (elt))
11184 /* A decl pack is itself an expansion. */
11185 elt = TREE_TYPE (elt);
11186 if (!PACK_EXPANSION_P (elt))
11187 return 0;
11188 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11189 return 2;
11190 return 1;
11191 }
11192
11193
11194 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11195
11196 static tree
11197 make_argument_pack_select (tree arg_pack, unsigned index)
11198 {
11199 tree aps = make_node (ARGUMENT_PACK_SELECT);
11200
11201 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11202 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11203
11204 return aps;
11205 }
11206
11207 /* This is a subroutine of tsubst_pack_expansion.
11208
11209 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11210 mechanism to store the (non complete list of) arguments of the
11211 substitution and return a non substituted pack expansion, in order
11212 to wait for when we have enough arguments to really perform the
11213 substitution. */
11214
11215 static bool
11216 use_pack_expansion_extra_args_p (tree parm_packs,
11217 int arg_pack_len,
11218 bool has_empty_arg)
11219 {
11220 /* If one pack has an expansion and another pack has a normal
11221 argument or if one pack has an empty argument and an another
11222 one hasn't then tsubst_pack_expansion cannot perform the
11223 substitution and need to fall back on the
11224 PACK_EXPANSION_EXTRA mechanism. */
11225 if (parm_packs == NULL_TREE)
11226 return false;
11227 else if (has_empty_arg)
11228 return true;
11229
11230 bool has_expansion_arg = false;
11231 for (int i = 0 ; i < arg_pack_len; ++i)
11232 {
11233 bool has_non_expansion_arg = false;
11234 for (tree parm_pack = parm_packs;
11235 parm_pack;
11236 parm_pack = TREE_CHAIN (parm_pack))
11237 {
11238 tree arg = TREE_VALUE (parm_pack);
11239
11240 int exp = argument_pack_element_is_expansion_p (arg, i);
11241 if (exp == 2)
11242 /* We can't substitute a pack expansion with extra args into
11243 our pattern. */
11244 return true;
11245 else if (exp)
11246 has_expansion_arg = true;
11247 else
11248 has_non_expansion_arg = true;
11249 }
11250
11251 if (has_expansion_arg && has_non_expansion_arg)
11252 return true;
11253 }
11254 return false;
11255 }
11256
11257 /* [temp.variadic]/6 says that:
11258
11259 The instantiation of a pack expansion [...]
11260 produces a list E1,E2, ..., En, where N is the number of elements
11261 in the pack expansion parameters.
11262
11263 This subroutine of tsubst_pack_expansion produces one of these Ei.
11264
11265 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11266 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11267 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11268 INDEX is the index 'i' of the element Ei to produce. ARGS,
11269 COMPLAIN, and IN_DECL are the same parameters as for the
11270 tsubst_pack_expansion function.
11271
11272 The function returns the resulting Ei upon successful completion,
11273 or error_mark_node.
11274
11275 Note that this function possibly modifies the ARGS parameter, so
11276 it's the responsibility of the caller to restore it. */
11277
11278 static tree
11279 gen_elem_of_pack_expansion_instantiation (tree pattern,
11280 tree parm_packs,
11281 unsigned index,
11282 tree args /* This parm gets
11283 modified. */,
11284 tsubst_flags_t complain,
11285 tree in_decl)
11286 {
11287 tree t;
11288 bool ith_elem_is_expansion = false;
11289
11290 /* For each parameter pack, change the substitution of the parameter
11291 pack to the ith argument in its argument pack, then expand the
11292 pattern. */
11293 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11294 {
11295 tree parm = TREE_PURPOSE (pack);
11296 tree arg_pack = TREE_VALUE (pack);
11297 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11298
11299 ith_elem_is_expansion |=
11300 argument_pack_element_is_expansion_p (arg_pack, index);
11301
11302 /* Select the Ith argument from the pack. */
11303 if (TREE_CODE (parm) == PARM_DECL
11304 || VAR_P (parm)
11305 || TREE_CODE (parm) == FIELD_DECL)
11306 {
11307 if (index == 0)
11308 {
11309 aps = make_argument_pack_select (arg_pack, index);
11310 if (!mark_used (parm, complain) && !(complain & tf_error))
11311 return error_mark_node;
11312 register_local_specialization (aps, parm);
11313 }
11314 else
11315 aps = retrieve_local_specialization (parm);
11316 }
11317 else
11318 {
11319 int idx, level;
11320 template_parm_level_and_index (parm, &level, &idx);
11321
11322 if (index == 0)
11323 {
11324 aps = make_argument_pack_select (arg_pack, index);
11325 /* Update the corresponding argument. */
11326 TMPL_ARG (args, level, idx) = aps;
11327 }
11328 else
11329 /* Re-use the ARGUMENT_PACK_SELECT. */
11330 aps = TMPL_ARG (args, level, idx);
11331 }
11332 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11333 }
11334
11335 /* Substitute into the PATTERN with the (possibly altered)
11336 arguments. */
11337 if (pattern == in_decl)
11338 /* Expanding a fixed parameter pack from
11339 coerce_template_parameter_pack. */
11340 t = tsubst_decl (pattern, args, complain);
11341 else if (pattern == error_mark_node)
11342 t = error_mark_node;
11343 else if (constraint_p (pattern))
11344 {
11345 if (processing_template_decl)
11346 t = tsubst_constraint (pattern, args, complain, in_decl);
11347 else
11348 t = (constraints_satisfied_p (pattern, args)
11349 ? boolean_true_node : boolean_false_node);
11350 }
11351 else if (!TYPE_P (pattern))
11352 t = tsubst_expr (pattern, args, complain, in_decl,
11353 /*integral_constant_expression_p=*/false);
11354 else
11355 t = tsubst (pattern, args, complain, in_decl);
11356
11357 /* If the Ith argument pack element is a pack expansion, then
11358 the Ith element resulting from the substituting is going to
11359 be a pack expansion as well. */
11360 if (ith_elem_is_expansion)
11361 t = make_pack_expansion (t, complain);
11362
11363 return t;
11364 }
11365
11366 /* When the unexpanded parameter pack in a fold expression expands to an empty
11367 sequence, the value of the expression is as follows; the program is
11368 ill-formed if the operator is not listed in this table.
11369
11370 && true
11371 || false
11372 , void() */
11373
11374 tree
11375 expand_empty_fold (tree t, tsubst_flags_t complain)
11376 {
11377 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11378 if (!FOLD_EXPR_MODIFY_P (t))
11379 switch (code)
11380 {
11381 case TRUTH_ANDIF_EXPR:
11382 return boolean_true_node;
11383 case TRUTH_ORIF_EXPR:
11384 return boolean_false_node;
11385 case COMPOUND_EXPR:
11386 return void_node;
11387 default:
11388 break;
11389 }
11390
11391 if (complain & tf_error)
11392 error_at (location_of (t),
11393 "fold of empty expansion over %O", code);
11394 return error_mark_node;
11395 }
11396
11397 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11398 form an expression that combines the two terms using the
11399 operator of T. */
11400
11401 static tree
11402 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11403 {
11404 tree op = FOLD_EXPR_OP (t);
11405 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11406
11407 // Handle compound assignment operators.
11408 if (FOLD_EXPR_MODIFY_P (t))
11409 return build_x_modify_expr (input_location, left, code, right, complain);
11410
11411 switch (code)
11412 {
11413 case COMPOUND_EXPR:
11414 return build_x_compound_expr (input_location, left, right, complain);
11415 case DOTSTAR_EXPR:
11416 return build_m_component_ref (left, right, complain);
11417 default:
11418 return build_x_binary_op (input_location, code,
11419 left, TREE_CODE (left),
11420 right, TREE_CODE (right),
11421 /*overload=*/NULL,
11422 complain);
11423 }
11424 }
11425
11426 /* Substitute ARGS into the pack of a fold expression T. */
11427
11428 static inline tree
11429 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11430 {
11431 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11432 }
11433
11434 /* Substitute ARGS into the pack of a fold expression T. */
11435
11436 static inline tree
11437 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11438 {
11439 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11440 }
11441
11442 /* Expand a PACK of arguments into a grouped as left fold.
11443 Given a pack containing elements A0, A1, ..., An and an
11444 operator @, this builds the expression:
11445
11446 ((A0 @ A1) @ A2) ... @ An
11447
11448 Note that PACK must not be empty.
11449
11450 The operator is defined by the original fold expression T. */
11451
11452 static tree
11453 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11454 {
11455 tree left = TREE_VEC_ELT (pack, 0);
11456 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11457 {
11458 tree right = TREE_VEC_ELT (pack, i);
11459 left = fold_expression (t, left, right, complain);
11460 }
11461 return left;
11462 }
11463
11464 /* Substitute into a unary left fold expression. */
11465
11466 static tree
11467 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11468 tree in_decl)
11469 {
11470 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11471 if (pack == error_mark_node)
11472 return error_mark_node;
11473 if (PACK_EXPANSION_P (pack))
11474 {
11475 tree r = copy_node (t);
11476 FOLD_EXPR_PACK (r) = pack;
11477 return r;
11478 }
11479 if (TREE_VEC_LENGTH (pack) == 0)
11480 return expand_empty_fold (t, complain);
11481 else
11482 return expand_left_fold (t, pack, complain);
11483 }
11484
11485 /* Substitute into a binary left fold expression.
11486
11487 Do ths by building a single (non-empty) vector of argumnts and
11488 building the expression from those elements. */
11489
11490 static tree
11491 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11492 tree in_decl)
11493 {
11494 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11495 if (pack == error_mark_node)
11496 return error_mark_node;
11497 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11498 if (init == error_mark_node)
11499 return error_mark_node;
11500
11501 if (PACK_EXPANSION_P (pack))
11502 {
11503 tree r = copy_node (t);
11504 FOLD_EXPR_PACK (r) = pack;
11505 FOLD_EXPR_INIT (r) = init;
11506 return r;
11507 }
11508
11509 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11510 TREE_VEC_ELT (vec, 0) = init;
11511 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11512 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11513
11514 return expand_left_fold (t, vec, complain);
11515 }
11516
11517 /* Expand a PACK of arguments into a grouped as right fold.
11518 Given a pack containing elementns A0, A1, ..., and an
11519 operator @, this builds the expression:
11520
11521 A0@ ... (An-2 @ (An-1 @ An))
11522
11523 Note that PACK must not be empty.
11524
11525 The operator is defined by the original fold expression T. */
11526
11527 tree
11528 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11529 {
11530 // Build the expression.
11531 int n = TREE_VEC_LENGTH (pack);
11532 tree right = TREE_VEC_ELT (pack, n - 1);
11533 for (--n; n != 0; --n)
11534 {
11535 tree left = TREE_VEC_ELT (pack, n - 1);
11536 right = fold_expression (t, left, right, complain);
11537 }
11538 return right;
11539 }
11540
11541 /* Substitute into a unary right fold expression. */
11542
11543 static tree
11544 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11545 tree in_decl)
11546 {
11547 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11548 if (pack == error_mark_node)
11549 return error_mark_node;
11550 if (PACK_EXPANSION_P (pack))
11551 {
11552 tree r = copy_node (t);
11553 FOLD_EXPR_PACK (r) = pack;
11554 return r;
11555 }
11556 if (TREE_VEC_LENGTH (pack) == 0)
11557 return expand_empty_fold (t, complain);
11558 else
11559 return expand_right_fold (t, pack, complain);
11560 }
11561
11562 /* Substitute into a binary right fold expression.
11563
11564 Do ths by building a single (non-empty) vector of arguments and
11565 building the expression from those elements. */
11566
11567 static tree
11568 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11569 tree in_decl)
11570 {
11571 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11572 if (pack == error_mark_node)
11573 return error_mark_node;
11574 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11575 if (init == error_mark_node)
11576 return error_mark_node;
11577
11578 if (PACK_EXPANSION_P (pack))
11579 {
11580 tree r = copy_node (t);
11581 FOLD_EXPR_PACK (r) = pack;
11582 FOLD_EXPR_INIT (r) = init;
11583 return r;
11584 }
11585
11586 int n = TREE_VEC_LENGTH (pack);
11587 tree vec = make_tree_vec (n + 1);
11588 for (int i = 0; i < n; ++i)
11589 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11590 TREE_VEC_ELT (vec, n) = init;
11591
11592 return expand_right_fold (t, vec, complain);
11593 }
11594
11595 /* Walk through the pattern of a pack expansion, adding everything in
11596 local_specializations to a list. */
11597
11598 struct el_data
11599 {
11600 tree extra;
11601 tsubst_flags_t complain;
11602 };
11603 static tree
11604 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11605 {
11606 el_data &data = *reinterpret_cast<el_data*>(data_);
11607 tree *extra = &data.extra;
11608 tsubst_flags_t complain = data.complain;
11609 if (tree spec = retrieve_local_specialization (*tp))
11610 {
11611 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11612 {
11613 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11614 tree args = ARGUMENT_PACK_ARGS (spec);
11615 if (TREE_VEC_LENGTH (args) == 1)
11616 {
11617 tree elt = TREE_VEC_ELT (args, 0);
11618 if (PACK_EXPANSION_P (elt))
11619 elt = PACK_EXPANSION_PATTERN (elt);
11620 if (DECL_PACK_P (elt))
11621 spec = elt;
11622 }
11623 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11624 {
11625 /* Handle lambda capture here, since we aren't doing any
11626 substitution now, and so tsubst_copy won't call
11627 process_outer_var_ref. */
11628 tree args = ARGUMENT_PACK_ARGS (spec);
11629 int len = TREE_VEC_LENGTH (args);
11630 for (int i = 0; i < len; ++i)
11631 {
11632 tree arg = TREE_VEC_ELT (args, i);
11633 tree carg = arg;
11634 if (outer_automatic_var_p (arg))
11635 carg = process_outer_var_ref (arg, complain);
11636 if (carg != arg)
11637 {
11638 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11639 proxies. */
11640 if (i == 0)
11641 {
11642 spec = copy_node (spec);
11643 args = copy_node (args);
11644 SET_ARGUMENT_PACK_ARGS (spec, args);
11645 register_local_specialization (spec, *tp);
11646 }
11647 TREE_VEC_ELT (args, i) = carg;
11648 }
11649 }
11650 }
11651 }
11652 if (outer_automatic_var_p (spec))
11653 spec = process_outer_var_ref (spec, complain);
11654 *extra = tree_cons (*tp, spec, *extra);
11655 }
11656 return NULL_TREE;
11657 }
11658 static tree
11659 extract_local_specs (tree pattern, tsubst_flags_t complain)
11660 {
11661 el_data data = { NULL_TREE, complain };
11662 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11663 return data.extra;
11664 }
11665
11666 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
11667 for use in PACK_EXPANSION_EXTRA_ARGS. */
11668
11669 tree
11670 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
11671 {
11672 tree extra = args;
11673 if (local_specializations)
11674 if (tree locals = extract_local_specs (pattern, complain))
11675 extra = tree_cons (NULL_TREE, extra, locals);
11676 return extra;
11677 }
11678
11679 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
11680 normal template args to ARGS. */
11681
11682 tree
11683 add_extra_args (tree extra, tree args)
11684 {
11685 if (extra && TREE_CODE (extra) == TREE_LIST)
11686 {
11687 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11688 {
11689 /* The partial instantiation involved local declarations collected in
11690 extract_local_specs; map from the general template to our local
11691 context. */
11692 tree gen = TREE_PURPOSE (elt);
11693 tree inst = TREE_VALUE (elt);
11694 if (DECL_P (inst))
11695 if (tree local = retrieve_local_specialization (inst))
11696 inst = local;
11697 /* else inst is already a full instantiation of the pack. */
11698 register_local_specialization (inst, gen);
11699 }
11700 gcc_assert (!TREE_PURPOSE (extra));
11701 extra = TREE_VALUE (extra);
11702 }
11703 return add_to_template_args (extra, args);
11704 }
11705
11706 /* Substitute ARGS into T, which is an pack expansion
11707 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11708 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11709 (if only a partial substitution could be performed) or
11710 ERROR_MARK_NODE if there was an error. */
11711 tree
11712 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11713 tree in_decl)
11714 {
11715 tree pattern;
11716 tree pack, packs = NULL_TREE;
11717 bool unsubstituted_packs = false;
11718 bool unsubstituted_fn_pack = false;
11719 int i, len = -1;
11720 tree result;
11721 hash_map<tree, tree> *saved_local_specializations = NULL;
11722 bool need_local_specializations = false;
11723 int levels;
11724
11725 gcc_assert (PACK_EXPANSION_P (t));
11726 pattern = PACK_EXPANSION_PATTERN (t);
11727
11728 /* Add in any args remembered from an earlier partial instantiation. */
11729 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11730
11731 levels = TMPL_ARGS_DEPTH (args);
11732
11733 /* Determine the argument packs that will instantiate the parameter
11734 packs used in the expansion expression. While we're at it,
11735 compute the number of arguments to be expanded and make sure it
11736 is consistent. */
11737 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11738 pack = TREE_CHAIN (pack))
11739 {
11740 tree parm_pack = TREE_VALUE (pack);
11741 tree arg_pack = NULL_TREE;
11742 tree orig_arg = NULL_TREE;
11743 int level = 0;
11744
11745 if (TREE_CODE (parm_pack) == BASES)
11746 {
11747 gcc_assert (parm_pack == pattern);
11748 if (BASES_DIRECT (parm_pack))
11749 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11750 args, complain, in_decl, false));
11751 else
11752 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11753 args, complain, in_decl, false));
11754 }
11755 else if (builtin_pack_call_p (parm_pack))
11756 {
11757 /* ??? Support use in other patterns. */
11758 gcc_assert (parm_pack == pattern);
11759 return expand_builtin_pack_call (parm_pack, args,
11760 complain, in_decl);
11761 }
11762 else if (TREE_CODE (parm_pack) == PARM_DECL)
11763 {
11764 /* We know we have correct local_specializations if this
11765 expansion is at function scope, or if we're dealing with a
11766 local parameter in a requires expression; for the latter,
11767 tsubst_requires_expr set it up appropriately. */
11768 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11769 arg_pack = retrieve_local_specialization (parm_pack);
11770 else
11771 /* We can't rely on local_specializations for a parameter
11772 name used later in a function declaration (such as in a
11773 late-specified return type). Even if it exists, it might
11774 have the wrong value for a recursive call. */
11775 need_local_specializations = true;
11776
11777 if (!arg_pack)
11778 {
11779 /* This parameter pack was used in an unevaluated context. Just
11780 make a dummy decl, since it's only used for its type. */
11781 ++cp_unevaluated_operand;
11782 arg_pack = tsubst_decl (parm_pack, args, complain);
11783 --cp_unevaluated_operand;
11784 if (arg_pack && DECL_PACK_P (arg_pack))
11785 /* Partial instantiation of the parm_pack, we can't build
11786 up an argument pack yet. */
11787 arg_pack = NULL_TREE;
11788 else
11789 arg_pack = make_fnparm_pack (arg_pack);
11790 }
11791 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11792 /* This argument pack isn't fully instantiated yet. We set this
11793 flag rather than clear arg_pack because we do want to do the
11794 optimization below, and we don't want to substitute directly
11795 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11796 where it isn't expected). */
11797 unsubstituted_fn_pack = true;
11798 }
11799 else if (is_normal_capture_proxy (parm_pack))
11800 {
11801 arg_pack = retrieve_local_specialization (parm_pack);
11802 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11803 unsubstituted_fn_pack = true;
11804 }
11805 else
11806 {
11807 int idx;
11808 template_parm_level_and_index (parm_pack, &level, &idx);
11809
11810 if (level <= levels)
11811 arg_pack = TMPL_ARG (args, level, idx);
11812 }
11813
11814 orig_arg = arg_pack;
11815 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11816 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11817
11818 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11819 /* This can only happen if we forget to expand an argument
11820 pack somewhere else. Just return an error, silently. */
11821 {
11822 result = make_tree_vec (1);
11823 TREE_VEC_ELT (result, 0) = error_mark_node;
11824 return result;
11825 }
11826
11827 if (arg_pack)
11828 {
11829 int my_len =
11830 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11831
11832 /* Don't bother trying to do a partial substitution with
11833 incomplete packs; we'll try again after deduction. */
11834 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11835 return t;
11836
11837 if (len < 0)
11838 len = my_len;
11839 else if (len != my_len
11840 && !unsubstituted_fn_pack)
11841 {
11842 if (!(complain & tf_error))
11843 /* Fail quietly. */;
11844 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11845 error ("mismatched argument pack lengths while expanding %qT",
11846 pattern);
11847 else
11848 error ("mismatched argument pack lengths while expanding %qE",
11849 pattern);
11850 return error_mark_node;
11851 }
11852
11853 /* Keep track of the parameter packs and their corresponding
11854 argument packs. */
11855 packs = tree_cons (parm_pack, arg_pack, packs);
11856 TREE_TYPE (packs) = orig_arg;
11857 }
11858 else
11859 {
11860 /* We can't substitute for this parameter pack. We use a flag as
11861 well as the missing_level counter because function parameter
11862 packs don't have a level. */
11863 gcc_assert (processing_template_decl || is_auto (parm_pack));
11864 unsubstituted_packs = true;
11865 }
11866 }
11867
11868 /* If the expansion is just T..., return the matching argument pack, unless
11869 we need to call convert_from_reference on all the elements. This is an
11870 important optimization; see c++/68422. */
11871 if (!unsubstituted_packs
11872 && TREE_PURPOSE (packs) == pattern)
11873 {
11874 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11875
11876 /* If the argument pack is a single pack expansion, pull it out. */
11877 if (TREE_VEC_LENGTH (args) == 1
11878 && pack_expansion_args_count (args))
11879 return TREE_VEC_ELT (args, 0);
11880
11881 /* Types need no adjustment, nor does sizeof..., and if we still have
11882 some pack expansion args we won't do anything yet. */
11883 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11884 || PACK_EXPANSION_SIZEOF_P (t)
11885 || pack_expansion_args_count (args))
11886 return args;
11887 /* Also optimize expression pack expansions if we can tell that the
11888 elements won't have reference type. */
11889 tree type = TREE_TYPE (pattern);
11890 if (type && TREE_CODE (type) != REFERENCE_TYPE
11891 && !PACK_EXPANSION_P (type)
11892 && !WILDCARD_TYPE_P (type))
11893 return args;
11894 /* Otherwise use the normal path so we get convert_from_reference. */
11895 }
11896
11897 /* We cannot expand this expansion expression, because we don't have
11898 all of the argument packs we need. */
11899 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11900 {
11901 /* We got some full packs, but we can't substitute them in until we
11902 have values for all the packs. So remember these until then. */
11903
11904 t = make_pack_expansion (pattern, complain);
11905 PACK_EXPANSION_EXTRA_ARGS (t)
11906 = build_extra_args (pattern, args, complain);
11907 return t;
11908 }
11909 else if (unsubstituted_packs)
11910 {
11911 /* There were no real arguments, we're just replacing a parameter
11912 pack with another version of itself. Substitute into the
11913 pattern and return a PACK_EXPANSION_*. The caller will need to
11914 deal with that. */
11915 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11916 t = tsubst_expr (pattern, args, complain, in_decl,
11917 /*integral_constant_expression_p=*/false);
11918 else
11919 t = tsubst (pattern, args, complain, in_decl);
11920 t = make_pack_expansion (t, complain);
11921 return t;
11922 }
11923
11924 gcc_assert (len >= 0);
11925
11926 if (need_local_specializations)
11927 {
11928 /* We're in a late-specified return type, so create our own local
11929 specializations map; the current map is either NULL or (in the
11930 case of recursive unification) might have bindings that we don't
11931 want to use or alter. */
11932 saved_local_specializations = local_specializations;
11933 local_specializations = new hash_map<tree, tree>;
11934 }
11935
11936 /* For each argument in each argument pack, substitute into the
11937 pattern. */
11938 result = make_tree_vec (len);
11939 tree elem_args = copy_template_args (args);
11940 for (i = 0; i < len; ++i)
11941 {
11942 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11943 i,
11944 elem_args, complain,
11945 in_decl);
11946 TREE_VEC_ELT (result, i) = t;
11947 if (t == error_mark_node)
11948 {
11949 result = error_mark_node;
11950 break;
11951 }
11952 }
11953
11954 /* Update ARGS to restore the substitution from parameter packs to
11955 their argument packs. */
11956 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11957 {
11958 tree parm = TREE_PURPOSE (pack);
11959
11960 if (TREE_CODE (parm) == PARM_DECL
11961 || VAR_P (parm)
11962 || TREE_CODE (parm) == FIELD_DECL)
11963 register_local_specialization (TREE_TYPE (pack), parm);
11964 else
11965 {
11966 int idx, level;
11967
11968 if (TREE_VALUE (pack) == NULL_TREE)
11969 continue;
11970
11971 template_parm_level_and_index (parm, &level, &idx);
11972
11973 /* Update the corresponding argument. */
11974 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11975 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11976 TREE_TYPE (pack);
11977 else
11978 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11979 }
11980 }
11981
11982 if (need_local_specializations)
11983 {
11984 delete local_specializations;
11985 local_specializations = saved_local_specializations;
11986 }
11987
11988 /* If the dependent pack arguments were such that we end up with only a
11989 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11990 if (len == 1 && TREE_CODE (result) == TREE_VEC
11991 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11992 return TREE_VEC_ELT (result, 0);
11993
11994 return result;
11995 }
11996
11997 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11998 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11999 parameter packs; all parms generated from a function parameter pack will
12000 have the same DECL_PARM_INDEX. */
12001
12002 tree
12003 get_pattern_parm (tree parm, tree tmpl)
12004 {
12005 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12006 tree patparm;
12007
12008 if (DECL_ARTIFICIAL (parm))
12009 {
12010 for (patparm = DECL_ARGUMENTS (pattern);
12011 patparm; patparm = DECL_CHAIN (patparm))
12012 if (DECL_ARTIFICIAL (patparm)
12013 && DECL_NAME (parm) == DECL_NAME (patparm))
12014 break;
12015 }
12016 else
12017 {
12018 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12019 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12020 gcc_assert (DECL_PARM_INDEX (patparm)
12021 == DECL_PARM_INDEX (parm));
12022 }
12023
12024 return patparm;
12025 }
12026
12027 /* Make an argument pack out of the TREE_VEC VEC. */
12028
12029 static tree
12030 make_argument_pack (tree vec)
12031 {
12032 tree pack;
12033 tree elt = TREE_VEC_ELT (vec, 0);
12034 if (TYPE_P (elt))
12035 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12036 else
12037 {
12038 pack = make_node (NONTYPE_ARGUMENT_PACK);
12039 TREE_CONSTANT (pack) = 1;
12040 }
12041 SET_ARGUMENT_PACK_ARGS (pack, vec);
12042 return pack;
12043 }
12044
12045 /* Return an exact copy of template args T that can be modified
12046 independently. */
12047
12048 static tree
12049 copy_template_args (tree t)
12050 {
12051 if (t == error_mark_node)
12052 return t;
12053
12054 int len = TREE_VEC_LENGTH (t);
12055 tree new_vec = make_tree_vec (len);
12056
12057 for (int i = 0; i < len; ++i)
12058 {
12059 tree elt = TREE_VEC_ELT (t, i);
12060 if (elt && TREE_CODE (elt) == TREE_VEC)
12061 elt = copy_template_args (elt);
12062 TREE_VEC_ELT (new_vec, i) = elt;
12063 }
12064
12065 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12066 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12067
12068 return new_vec;
12069 }
12070
12071 /* Substitute ARGS into the vector or list of template arguments T. */
12072
12073 static tree
12074 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12075 {
12076 tree orig_t = t;
12077 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12078 tree *elts;
12079
12080 if (t == error_mark_node)
12081 return error_mark_node;
12082
12083 len = TREE_VEC_LENGTH (t);
12084 elts = XALLOCAVEC (tree, len);
12085
12086 for (i = 0; i < len; i++)
12087 {
12088 tree orig_arg = TREE_VEC_ELT (t, i);
12089 tree new_arg;
12090
12091 if (TREE_CODE (orig_arg) == TREE_VEC)
12092 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12093 else if (PACK_EXPANSION_P (orig_arg))
12094 {
12095 /* Substitute into an expansion expression. */
12096 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12097
12098 if (TREE_CODE (new_arg) == TREE_VEC)
12099 /* Add to the expanded length adjustment the number of
12100 expanded arguments. We subtract one from this
12101 measurement, because the argument pack expression
12102 itself is already counted as 1 in
12103 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12104 the argument pack is empty. */
12105 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12106 }
12107 else if (ARGUMENT_PACK_P (orig_arg))
12108 {
12109 /* Substitute into each of the arguments. */
12110 new_arg = TYPE_P (orig_arg)
12111 ? cxx_make_type (TREE_CODE (orig_arg))
12112 : make_node (TREE_CODE (orig_arg));
12113
12114 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12115 args, complain, in_decl);
12116 if (pack_args == error_mark_node)
12117 new_arg = error_mark_node;
12118 else
12119 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12120
12121 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12122 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12123 }
12124 else
12125 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12126
12127 if (new_arg == error_mark_node)
12128 return error_mark_node;
12129
12130 elts[i] = new_arg;
12131 if (new_arg != orig_arg)
12132 need_new = 1;
12133 }
12134
12135 if (!need_new)
12136 return t;
12137
12138 /* Make space for the expanded arguments coming from template
12139 argument packs. */
12140 t = make_tree_vec (len + expanded_len_adjust);
12141 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12142 arguments for a member template.
12143 In that case each TREE_VEC in ORIG_T represents a level of template
12144 arguments, and ORIG_T won't carry any non defaulted argument count.
12145 It will rather be the nested TREE_VECs that will carry one.
12146 In other words, ORIG_T carries a non defaulted argument count only
12147 if it doesn't contain any nested TREE_VEC. */
12148 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12149 {
12150 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12151 count += expanded_len_adjust;
12152 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12153 }
12154 for (i = 0, out = 0; i < len; i++)
12155 {
12156 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12157 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12158 && TREE_CODE (elts[i]) == TREE_VEC)
12159 {
12160 int idx;
12161
12162 /* Now expand the template argument pack "in place". */
12163 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12164 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12165 }
12166 else
12167 {
12168 TREE_VEC_ELT (t, out) = elts[i];
12169 out++;
12170 }
12171 }
12172
12173 return t;
12174 }
12175
12176 /* Substitute ARGS into one level PARMS of template parameters. */
12177
12178 static tree
12179 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12180 {
12181 if (parms == error_mark_node)
12182 return error_mark_node;
12183
12184 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12185
12186 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12187 {
12188 tree tuple = TREE_VEC_ELT (parms, i);
12189
12190 if (tuple == error_mark_node)
12191 continue;
12192
12193 TREE_VEC_ELT (new_vec, i) =
12194 tsubst_template_parm (tuple, args, complain);
12195 }
12196
12197 return new_vec;
12198 }
12199
12200 /* Return the result of substituting ARGS into the template parameters
12201 given by PARMS. If there are m levels of ARGS and m + n levels of
12202 PARMS, then the result will contain n levels of PARMS. For
12203 example, if PARMS is `template <class T> template <class U>
12204 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12205 result will be `template <int*, double, class V>'. */
12206
12207 static tree
12208 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12209 {
12210 tree r = NULL_TREE;
12211 tree* new_parms;
12212
12213 /* When substituting into a template, we must set
12214 PROCESSING_TEMPLATE_DECL as the template parameters may be
12215 dependent if they are based on one-another, and the dependency
12216 predicates are short-circuit outside of templates. */
12217 ++processing_template_decl;
12218
12219 for (new_parms = &r;
12220 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12221 new_parms = &(TREE_CHAIN (*new_parms)),
12222 parms = TREE_CHAIN (parms))
12223 {
12224 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12225 args, complain);
12226 *new_parms =
12227 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12228 - TMPL_ARGS_DEPTH (args)),
12229 new_vec, NULL_TREE);
12230 }
12231
12232 --processing_template_decl;
12233
12234 return r;
12235 }
12236
12237 /* Return the result of substituting ARGS into one template parameter
12238 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12239 parameter and which TREE_PURPOSE is the default argument of the
12240 template parameter. */
12241
12242 static tree
12243 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12244 {
12245 tree default_value, parm_decl;
12246
12247 if (args == NULL_TREE
12248 || t == NULL_TREE
12249 || t == error_mark_node)
12250 return t;
12251
12252 gcc_assert (TREE_CODE (t) == TREE_LIST);
12253
12254 default_value = TREE_PURPOSE (t);
12255 parm_decl = TREE_VALUE (t);
12256
12257 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12258 if (TREE_CODE (parm_decl) == PARM_DECL
12259 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12260 parm_decl = error_mark_node;
12261 default_value = tsubst_template_arg (default_value, args,
12262 complain, NULL_TREE);
12263
12264 return build_tree_list (default_value, parm_decl);
12265 }
12266
12267 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12268 type T. If T is not an aggregate or enumeration type, it is
12269 handled as if by tsubst. IN_DECL is as for tsubst. If
12270 ENTERING_SCOPE is nonzero, T is the context for a template which
12271 we are presently tsubst'ing. Return the substituted value. */
12272
12273 static tree
12274 tsubst_aggr_type (tree t,
12275 tree args,
12276 tsubst_flags_t complain,
12277 tree in_decl,
12278 int entering_scope)
12279 {
12280 if (t == NULL_TREE)
12281 return NULL_TREE;
12282
12283 switch (TREE_CODE (t))
12284 {
12285 case RECORD_TYPE:
12286 if (TYPE_PTRMEMFUNC_P (t))
12287 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12288
12289 /* Fall through. */
12290 case ENUMERAL_TYPE:
12291 case UNION_TYPE:
12292 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12293 {
12294 tree argvec;
12295 tree context;
12296 tree r;
12297 int saved_unevaluated_operand;
12298 int saved_inhibit_evaluation_warnings;
12299
12300 /* In "sizeof(X<I>)" we need to evaluate "I". */
12301 saved_unevaluated_operand = cp_unevaluated_operand;
12302 cp_unevaluated_operand = 0;
12303 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12304 c_inhibit_evaluation_warnings = 0;
12305
12306 /* First, determine the context for the type we are looking
12307 up. */
12308 context = TYPE_CONTEXT (t);
12309 if (context && TYPE_P (context))
12310 {
12311 context = tsubst_aggr_type (context, args, complain,
12312 in_decl, /*entering_scope=*/1);
12313 /* If context is a nested class inside a class template,
12314 it may still need to be instantiated (c++/33959). */
12315 context = complete_type (context);
12316 }
12317
12318 /* Then, figure out what arguments are appropriate for the
12319 type we are trying to find. For example, given:
12320
12321 template <class T> struct S;
12322 template <class T, class U> void f(T, U) { S<U> su; }
12323
12324 and supposing that we are instantiating f<int, double>,
12325 then our ARGS will be {int, double}, but, when looking up
12326 S we only want {double}. */
12327 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12328 complain, in_decl);
12329 if (argvec == error_mark_node)
12330 r = error_mark_node;
12331 else
12332 {
12333 r = lookup_template_class (t, argvec, in_decl, context,
12334 entering_scope, complain);
12335 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12336 }
12337
12338 cp_unevaluated_operand = saved_unevaluated_operand;
12339 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12340
12341 return r;
12342 }
12343 else
12344 /* This is not a template type, so there's nothing to do. */
12345 return t;
12346
12347 default:
12348 return tsubst (t, args, complain, in_decl);
12349 }
12350 }
12351
12352 static GTY((cache)) tree_cache_map *defarg_inst;
12353
12354 /* Substitute into the default argument ARG (a default argument for
12355 FN), which has the indicated TYPE. */
12356
12357 tree
12358 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12359 tsubst_flags_t complain)
12360 {
12361 tree saved_class_ptr = NULL_TREE;
12362 tree saved_class_ref = NULL_TREE;
12363 int errs = errorcount + sorrycount;
12364
12365 /* This can happen in invalid code. */
12366 if (TREE_CODE (arg) == DEFAULT_ARG)
12367 return arg;
12368
12369 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12370 parm = chain_index (parmnum, parm);
12371 tree parmtype = TREE_TYPE (parm);
12372 if (DECL_BY_REFERENCE (parm))
12373 parmtype = TREE_TYPE (parmtype);
12374 if (parmtype == error_mark_node)
12375 return error_mark_node;
12376
12377 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12378
12379 tree *slot;
12380 if (defarg_inst && (slot = defarg_inst->get (parm)))
12381 return *slot;
12382
12383 /* This default argument came from a template. Instantiate the
12384 default argument here, not in tsubst. In the case of
12385 something like:
12386
12387 template <class T>
12388 struct S {
12389 static T t();
12390 void f(T = t());
12391 };
12392
12393 we must be careful to do name lookup in the scope of S<T>,
12394 rather than in the current class. */
12395 push_access_scope (fn);
12396 /* The "this" pointer is not valid in a default argument. */
12397 if (cfun)
12398 {
12399 saved_class_ptr = current_class_ptr;
12400 cp_function_chain->x_current_class_ptr = NULL_TREE;
12401 saved_class_ref = current_class_ref;
12402 cp_function_chain->x_current_class_ref = NULL_TREE;
12403 }
12404
12405 start_lambda_scope (parm);
12406
12407 push_deferring_access_checks(dk_no_deferred);
12408 /* The default argument expression may cause implicitly defined
12409 member functions to be synthesized, which will result in garbage
12410 collection. We must treat this situation as if we were within
12411 the body of function so as to avoid collecting live data on the
12412 stack. */
12413 ++function_depth;
12414 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12415 complain, NULL_TREE,
12416 /*integral_constant_expression_p=*/false);
12417 --function_depth;
12418 pop_deferring_access_checks();
12419
12420 finish_lambda_scope ();
12421
12422 /* Restore the "this" pointer. */
12423 if (cfun)
12424 {
12425 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12426 cp_function_chain->x_current_class_ref = saved_class_ref;
12427 }
12428
12429 if (errorcount+sorrycount > errs
12430 && (complain & tf_warning_or_error))
12431 inform (input_location,
12432 " when instantiating default argument for call to %qD", fn);
12433
12434 /* Make sure the default argument is reasonable. */
12435 arg = check_default_argument (type, arg, complain);
12436
12437 pop_access_scope (fn);
12438
12439 if (arg != error_mark_node && !cp_unevaluated_operand)
12440 {
12441 if (!defarg_inst)
12442 defarg_inst = tree_cache_map::create_ggc (37);
12443 defarg_inst->put (parm, arg);
12444 }
12445
12446 return arg;
12447 }
12448
12449 /* Substitute into all the default arguments for FN. */
12450
12451 static void
12452 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12453 {
12454 tree arg;
12455 tree tmpl_args;
12456
12457 tmpl_args = DECL_TI_ARGS (fn);
12458
12459 /* If this function is not yet instantiated, we certainly don't need
12460 its default arguments. */
12461 if (uses_template_parms (tmpl_args))
12462 return;
12463 /* Don't do this again for clones. */
12464 if (DECL_CLONED_FUNCTION_P (fn))
12465 return;
12466
12467 int i = 0;
12468 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12469 arg;
12470 arg = TREE_CHAIN (arg), ++i)
12471 if (TREE_PURPOSE (arg))
12472 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12473 TREE_VALUE (arg),
12474 TREE_PURPOSE (arg),
12475 complain);
12476 }
12477
12478 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12479
12480 static tree
12481 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12482 tree lambda_fntype)
12483 {
12484 tree gen_tmpl, argvec;
12485 hashval_t hash = 0;
12486 tree in_decl = t;
12487
12488 /* Nobody should be tsubst'ing into non-template functions. */
12489 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12490
12491 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12492 {
12493 /* If T is not dependent, just return it. */
12494 if (!uses_template_parms (DECL_TI_ARGS (t)))
12495 return t;
12496
12497 /* Calculate the most general template of which R is a
12498 specialization. */
12499 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12500
12501 /* We're substituting a lambda function under tsubst_lambda_expr but not
12502 directly from it; find the matching function we're already inside.
12503 But don't do this if T is a generic lambda with a single level of
12504 template parms, as in that case we're doing a normal instantiation. */
12505 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12506 && (!generic_lambda_fn_p (t)
12507 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12508 return enclosing_instantiation_of (t);
12509
12510 /* Calculate the complete set of arguments used to
12511 specialize R. */
12512 argvec = tsubst_template_args (DECL_TI_ARGS
12513 (DECL_TEMPLATE_RESULT
12514 (DECL_TI_TEMPLATE (t))),
12515 args, complain, in_decl);
12516 if (argvec == error_mark_node)
12517 return error_mark_node;
12518
12519 /* Check to see if we already have this specialization. */
12520 if (!lambda_fntype)
12521 {
12522 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12523 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12524 return spec;
12525 }
12526
12527 /* We can see more levels of arguments than parameters if
12528 there was a specialization of a member template, like
12529 this:
12530
12531 template <class T> struct S { template <class U> void f(); }
12532 template <> template <class U> void S<int>::f(U);
12533
12534 Here, we'll be substituting into the specialization,
12535 because that's where we can find the code we actually
12536 want to generate, but we'll have enough arguments for
12537 the most general template.
12538
12539 We also deal with the peculiar case:
12540
12541 template <class T> struct S {
12542 template <class U> friend void f();
12543 };
12544 template <class U> void f() {}
12545 template S<int>;
12546 template void f<double>();
12547
12548 Here, the ARGS for the instantiation of will be {int,
12549 double}. But, we only need as many ARGS as there are
12550 levels of template parameters in CODE_PATTERN. We are
12551 careful not to get fooled into reducing the ARGS in
12552 situations like:
12553
12554 template <class T> struct S { template <class U> void f(U); }
12555 template <class T> template <> void S<T>::f(int) {}
12556
12557 which we can spot because the pattern will be a
12558 specialization in this case. */
12559 int args_depth = TMPL_ARGS_DEPTH (args);
12560 int parms_depth =
12561 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12562
12563 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12564 args = get_innermost_template_args (args, parms_depth);
12565 }
12566 else
12567 {
12568 /* This special case arises when we have something like this:
12569
12570 template <class T> struct S {
12571 friend void f<int>(int, double);
12572 };
12573
12574 Here, the DECL_TI_TEMPLATE for the friend declaration
12575 will be an IDENTIFIER_NODE. We are being called from
12576 tsubst_friend_function, and we want only to create a
12577 new decl (R) with appropriate types so that we can call
12578 determine_specialization. */
12579 gen_tmpl = NULL_TREE;
12580 argvec = NULL_TREE;
12581 }
12582
12583 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12584 : NULL_TREE);
12585 tree ctx = closure ? closure : DECL_CONTEXT (t);
12586 bool member = ctx && TYPE_P (ctx);
12587
12588 if (member && !closure)
12589 ctx = tsubst_aggr_type (ctx, args,
12590 complain, t, /*entering_scope=*/1);
12591
12592 tree type = (lambda_fntype ? lambda_fntype
12593 : tsubst (TREE_TYPE (t), args,
12594 complain | tf_fndecl_type, in_decl));
12595 if (type == error_mark_node)
12596 return error_mark_node;
12597
12598 /* If we hit excessive deduction depth, the type is bogus even if
12599 it isn't error_mark_node, so don't build a decl. */
12600 if (excessive_deduction_depth)
12601 return error_mark_node;
12602
12603 /* We do NOT check for matching decls pushed separately at this
12604 point, as they may not represent instantiations of this
12605 template, and in any case are considered separate under the
12606 discrete model. */
12607 tree r = copy_decl (t);
12608 DECL_USE_TEMPLATE (r) = 0;
12609 TREE_TYPE (r) = type;
12610 /* Clear out the mangled name and RTL for the instantiation. */
12611 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12612 SET_DECL_RTL (r, NULL);
12613 /* Leave DECL_INITIAL set on deleted instantiations. */
12614 if (!DECL_DELETED_FN (r))
12615 DECL_INITIAL (r) = NULL_TREE;
12616 DECL_CONTEXT (r) = ctx;
12617
12618 /* OpenMP UDRs have the only argument a reference to the declared
12619 type. We want to diagnose if the declared type is a reference,
12620 which is invalid, but as references to references are usually
12621 quietly merged, diagnose it here. */
12622 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12623 {
12624 tree argtype
12625 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12626 argtype = tsubst (argtype, args, complain, in_decl);
12627 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12628 error_at (DECL_SOURCE_LOCATION (t),
12629 "reference type %qT in "
12630 "%<#pragma omp declare reduction%>", argtype);
12631 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12632 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12633 argtype);
12634 }
12635
12636 if (member && DECL_CONV_FN_P (r))
12637 /* Type-conversion operator. Reconstruct the name, in
12638 case it's the name of one of the template's parameters. */
12639 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12640
12641 tree parms = DECL_ARGUMENTS (t);
12642 if (closure)
12643 parms = DECL_CHAIN (parms);
12644 parms = tsubst (parms, args, complain, t);
12645 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12646 DECL_CONTEXT (parm) = r;
12647 if (closure)
12648 {
12649 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12650 DECL_CHAIN (tparm) = parms;
12651 parms = tparm;
12652 }
12653 DECL_ARGUMENTS (r) = parms;
12654 DECL_RESULT (r) = NULL_TREE;
12655
12656 TREE_STATIC (r) = 0;
12657 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12658 DECL_EXTERNAL (r) = 1;
12659 /* If this is an instantiation of a function with internal
12660 linkage, we already know what object file linkage will be
12661 assigned to the instantiation. */
12662 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12663 DECL_DEFER_OUTPUT (r) = 0;
12664 DECL_CHAIN (r) = NULL_TREE;
12665 DECL_PENDING_INLINE_INFO (r) = 0;
12666 DECL_PENDING_INLINE_P (r) = 0;
12667 DECL_SAVED_TREE (r) = NULL_TREE;
12668 DECL_STRUCT_FUNCTION (r) = NULL;
12669 TREE_USED (r) = 0;
12670 /* We'll re-clone as appropriate in instantiate_template. */
12671 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12672
12673 /* If we aren't complaining now, return on error before we register
12674 the specialization so that we'll complain eventually. */
12675 if ((complain & tf_error) == 0
12676 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12677 && !grok_op_properties (r, /*complain=*/false))
12678 return error_mark_node;
12679
12680 /* When instantiating a constrained member, substitute
12681 into the constraints to create a new constraint. */
12682 if (tree ci = get_constraints (t))
12683 if (member)
12684 {
12685 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12686 set_constraints (r, ci);
12687 }
12688
12689 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12690 this in the special friend case mentioned above where
12691 GEN_TMPL is NULL. */
12692 if (gen_tmpl && !closure)
12693 {
12694 DECL_TEMPLATE_INFO (r)
12695 = build_template_info (gen_tmpl, argvec);
12696 SET_DECL_IMPLICIT_INSTANTIATION (r);
12697
12698 tree new_r
12699 = register_specialization (r, gen_tmpl, argvec, false, hash);
12700 if (new_r != r)
12701 /* We instantiated this while substituting into
12702 the type earlier (template/friend54.C). */
12703 return new_r;
12704
12705 /* We're not supposed to instantiate default arguments
12706 until they are called, for a template. But, for a
12707 declaration like:
12708
12709 template <class T> void f ()
12710 { extern void g(int i = T()); }
12711
12712 we should do the substitution when the template is
12713 instantiated. We handle the member function case in
12714 instantiate_class_template since the default arguments
12715 might refer to other members of the class. */
12716 if (!member
12717 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12718 && !uses_template_parms (argvec))
12719 tsubst_default_arguments (r, complain);
12720 }
12721 else
12722 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12723
12724 /* Copy the list of befriending classes. */
12725 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12726 *friends;
12727 friends = &TREE_CHAIN (*friends))
12728 {
12729 *friends = copy_node (*friends);
12730 TREE_VALUE (*friends)
12731 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12732 }
12733
12734 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12735 {
12736 maybe_retrofit_in_chrg (r);
12737 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12738 return error_mark_node;
12739 /* If this is an instantiation of a member template, clone it.
12740 If it isn't, that'll be handled by
12741 clone_constructors_and_destructors. */
12742 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12743 clone_function_decl (r, /*update_methods=*/false);
12744 }
12745 else if ((complain & tf_error) != 0
12746 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12747 && !grok_op_properties (r, /*complain=*/true))
12748 return error_mark_node;
12749
12750 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12751 SET_DECL_FRIEND_CONTEXT (r,
12752 tsubst (DECL_FRIEND_CONTEXT (t),
12753 args, complain, in_decl));
12754
12755 /* Possibly limit visibility based on template args. */
12756 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12757 if (DECL_VISIBILITY_SPECIFIED (t))
12758 {
12759 DECL_VISIBILITY_SPECIFIED (r) = 0;
12760 DECL_ATTRIBUTES (r)
12761 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12762 }
12763 determine_visibility (r);
12764 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12765 && !processing_template_decl)
12766 defaulted_late_check (r);
12767
12768 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12769 args, complain, in_decl);
12770 return r;
12771 }
12772
12773 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12774
12775 static tree
12776 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12777 tree lambda_fntype)
12778 {
12779 /* We can get here when processing a member function template,
12780 member class template, or template template parameter. */
12781 tree decl = DECL_TEMPLATE_RESULT (t);
12782 tree in_decl = t;
12783 tree spec;
12784 tree tmpl_args;
12785 tree full_args;
12786 tree r;
12787 hashval_t hash = 0;
12788
12789 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12790 {
12791 /* Template template parameter is treated here. */
12792 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12793 if (new_type == error_mark_node)
12794 r = error_mark_node;
12795 /* If we get a real template back, return it. This can happen in
12796 the context of most_specialized_partial_spec. */
12797 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12798 r = new_type;
12799 else
12800 /* The new TEMPLATE_DECL was built in
12801 reduce_template_parm_level. */
12802 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12803 return r;
12804 }
12805
12806 if (!lambda_fntype)
12807 {
12808 /* We might already have an instance of this template.
12809 The ARGS are for the surrounding class type, so the
12810 full args contain the tsubst'd args for the context,
12811 plus the innermost args from the template decl. */
12812 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12813 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12814 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12815 /* Because this is a template, the arguments will still be
12816 dependent, even after substitution. If
12817 PROCESSING_TEMPLATE_DECL is not set, the dependency
12818 predicates will short-circuit. */
12819 ++processing_template_decl;
12820 full_args = tsubst_template_args (tmpl_args, args,
12821 complain, in_decl);
12822 --processing_template_decl;
12823 if (full_args == error_mark_node)
12824 return error_mark_node;
12825
12826 /* If this is a default template template argument,
12827 tsubst might not have changed anything. */
12828 if (full_args == tmpl_args)
12829 return t;
12830
12831 hash = hash_tmpl_and_args (t, full_args);
12832 spec = retrieve_specialization (t, full_args, hash);
12833 if (spec != NULL_TREE)
12834 return spec;
12835 }
12836
12837 /* Make a new template decl. It will be similar to the
12838 original, but will record the current template arguments.
12839 We also create a new function declaration, which is just
12840 like the old one, but points to this new template, rather
12841 than the old one. */
12842 r = copy_decl (t);
12843 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12844 DECL_CHAIN (r) = NULL_TREE;
12845
12846 // Build new template info linking to the original template decl.
12847 if (!lambda_fntype)
12848 {
12849 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12850 SET_DECL_IMPLICIT_INSTANTIATION (r);
12851 }
12852 else
12853 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12854
12855 /* The template parameters for this new template are all the
12856 template parameters for the old template, except the
12857 outermost level of parameters. */
12858 DECL_TEMPLATE_PARMS (r)
12859 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12860 complain);
12861
12862 if (TREE_CODE (decl) == TYPE_DECL
12863 && !TYPE_DECL_ALIAS_P (decl))
12864 {
12865 tree new_type;
12866 ++processing_template_decl;
12867 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12868 --processing_template_decl;
12869 if (new_type == error_mark_node)
12870 return error_mark_node;
12871
12872 TREE_TYPE (r) = new_type;
12873 /* For a partial specialization, we need to keep pointing to
12874 the primary template. */
12875 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12876 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12877 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12878 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12879 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12880 }
12881 else
12882 {
12883 tree new_decl;
12884 ++processing_template_decl;
12885 if (TREE_CODE (decl) == FUNCTION_DECL)
12886 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12887 else
12888 new_decl = tsubst (decl, args, complain, in_decl);
12889 --processing_template_decl;
12890 if (new_decl == error_mark_node)
12891 return error_mark_node;
12892
12893 DECL_TEMPLATE_RESULT (r) = new_decl;
12894 TREE_TYPE (r) = TREE_TYPE (new_decl);
12895 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12896 if (lambda_fntype)
12897 {
12898 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12899 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12900 }
12901 else
12902 {
12903 DECL_TI_TEMPLATE (new_decl) = r;
12904 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12905 }
12906 }
12907
12908 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12909 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12910
12911 if (PRIMARY_TEMPLATE_P (t))
12912 DECL_PRIMARY_TEMPLATE (r) = r;
12913
12914 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12915 && !lambda_fntype)
12916 /* Record this non-type partial instantiation. */
12917 register_specialization (r, t,
12918 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12919 false, hash);
12920
12921 return r;
12922 }
12923
12924 /* True if FN is the op() for a lambda in an uninstantiated template. */
12925
12926 bool
12927 lambda_fn_in_template_p (tree fn)
12928 {
12929 if (!fn || !LAMBDA_FUNCTION_P (fn))
12930 return false;
12931 tree closure = DECL_CONTEXT (fn);
12932 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12933 }
12934
12935 /* We're instantiating a variable from template function TCTX. Return the
12936 corresponding current enclosing scope. This gets complicated because lambda
12937 functions in templates are regenerated rather than instantiated, but generic
12938 lambda functions are subsequently instantiated. */
12939
12940 static tree
12941 enclosing_instantiation_of (tree otctx)
12942 {
12943 tree tctx = otctx;
12944 tree fn = current_function_decl;
12945 int lambda_count = 0;
12946
12947 for (; tctx && lambda_fn_in_template_p (tctx);
12948 tctx = decl_function_context (tctx))
12949 ++lambda_count;
12950 for (; fn; fn = decl_function_context (fn))
12951 {
12952 tree ofn = fn;
12953 int flambda_count = 0;
12954 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12955 fn = decl_function_context (fn))
12956 ++flambda_count;
12957 if ((fn && DECL_TEMPLATE_INFO (fn))
12958 ? most_general_template (fn) != most_general_template (tctx)
12959 : fn != tctx)
12960 continue;
12961 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12962 || DECL_CONV_FN_P (ofn));
12963 return ofn;
12964 }
12965 gcc_unreachable ();
12966 }
12967
12968 /* Substitute the ARGS into the T, which is a _DECL. Return the
12969 result of the substitution. Issue error and warning messages under
12970 control of COMPLAIN. */
12971
12972 static tree
12973 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12974 {
12975 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12976 location_t saved_loc;
12977 tree r = NULL_TREE;
12978 tree in_decl = t;
12979 hashval_t hash = 0;
12980
12981 /* Set the filename and linenumber to improve error-reporting. */
12982 saved_loc = input_location;
12983 input_location = DECL_SOURCE_LOCATION (t);
12984
12985 switch (TREE_CODE (t))
12986 {
12987 case TEMPLATE_DECL:
12988 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12989 break;
12990
12991 case FUNCTION_DECL:
12992 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12993 break;
12994
12995 case PARM_DECL:
12996 {
12997 tree type = NULL_TREE;
12998 int i, len = 1;
12999 tree expanded_types = NULL_TREE;
13000 tree prev_r = NULL_TREE;
13001 tree first_r = NULL_TREE;
13002
13003 if (DECL_PACK_P (t))
13004 {
13005 /* If there is a local specialization that isn't a
13006 parameter pack, it means that we're doing a "simple"
13007 substitution from inside tsubst_pack_expansion. Just
13008 return the local specialization (which will be a single
13009 parm). */
13010 tree spec = retrieve_local_specialization (t);
13011 if (spec
13012 && TREE_CODE (spec) == PARM_DECL
13013 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13014 RETURN (spec);
13015
13016 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13017 the parameters in this function parameter pack. */
13018 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13019 complain, in_decl);
13020 if (TREE_CODE (expanded_types) == TREE_VEC)
13021 {
13022 len = TREE_VEC_LENGTH (expanded_types);
13023
13024 /* Zero-length parameter packs are boring. Just substitute
13025 into the chain. */
13026 if (len == 0)
13027 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13028 TREE_CHAIN (t)));
13029 }
13030 else
13031 {
13032 /* All we did was update the type. Make a note of that. */
13033 type = expanded_types;
13034 expanded_types = NULL_TREE;
13035 }
13036 }
13037
13038 /* Loop through all of the parameters we'll build. When T is
13039 a function parameter pack, LEN is the number of expanded
13040 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13041 r = NULL_TREE;
13042 for (i = 0; i < len; ++i)
13043 {
13044 prev_r = r;
13045 r = copy_node (t);
13046 if (DECL_TEMPLATE_PARM_P (t))
13047 SET_DECL_TEMPLATE_PARM_P (r);
13048
13049 if (expanded_types)
13050 /* We're on the Ith parameter of the function parameter
13051 pack. */
13052 {
13053 /* Get the Ith type. */
13054 type = TREE_VEC_ELT (expanded_types, i);
13055
13056 /* Rename the parameter to include the index. */
13057 DECL_NAME (r)
13058 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13059 }
13060 else if (!type)
13061 /* We're dealing with a normal parameter. */
13062 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13063
13064 type = type_decays_to (type);
13065 TREE_TYPE (r) = type;
13066 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13067
13068 if (DECL_INITIAL (r))
13069 {
13070 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13071 DECL_INITIAL (r) = TREE_TYPE (r);
13072 else
13073 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13074 complain, in_decl);
13075 }
13076
13077 DECL_CONTEXT (r) = NULL_TREE;
13078
13079 if (!DECL_TEMPLATE_PARM_P (r))
13080 DECL_ARG_TYPE (r) = type_passed_as (type);
13081
13082 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13083 args, complain, in_decl);
13084
13085 /* Keep track of the first new parameter we
13086 generate. That's what will be returned to the
13087 caller. */
13088 if (!first_r)
13089 first_r = r;
13090
13091 /* Build a proper chain of parameters when substituting
13092 into a function parameter pack. */
13093 if (prev_r)
13094 DECL_CHAIN (prev_r) = r;
13095 }
13096
13097 /* If cp_unevaluated_operand is set, we're just looking for a
13098 single dummy parameter, so don't keep going. */
13099 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13100 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13101 complain, DECL_CHAIN (t));
13102
13103 /* FIRST_R contains the start of the chain we've built. */
13104 r = first_r;
13105 }
13106 break;
13107
13108 case FIELD_DECL:
13109 {
13110 tree type = NULL_TREE;
13111 tree vec = NULL_TREE;
13112 tree expanded_types = NULL_TREE;
13113 int len = 1;
13114
13115 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13116 {
13117 /* This field is a lambda capture pack. Return a TREE_VEC of
13118 the expanded fields to instantiate_class_template_1. */
13119 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13120 complain, in_decl);
13121 if (TREE_CODE (expanded_types) == TREE_VEC)
13122 {
13123 len = TREE_VEC_LENGTH (expanded_types);
13124 vec = make_tree_vec (len);
13125 }
13126 else
13127 {
13128 /* All we did was update the type. Make a note of that. */
13129 type = expanded_types;
13130 expanded_types = NULL_TREE;
13131 }
13132 }
13133
13134 for (int i = 0; i < len; ++i)
13135 {
13136 r = copy_decl (t);
13137 if (expanded_types)
13138 {
13139 type = TREE_VEC_ELT (expanded_types, i);
13140 DECL_NAME (r)
13141 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13142 }
13143 else if (!type)
13144 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13145
13146 if (type == error_mark_node)
13147 RETURN (error_mark_node);
13148 TREE_TYPE (r) = type;
13149 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13150
13151 if (DECL_C_BIT_FIELD (r))
13152 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13153 number of bits. */
13154 DECL_BIT_FIELD_REPRESENTATIVE (r)
13155 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13156 complain, in_decl,
13157 /*integral_constant_expression_p=*/true);
13158 if (DECL_INITIAL (t))
13159 {
13160 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13161 NSDMI in perform_member_init. Still set DECL_INITIAL
13162 so that we know there is one. */
13163 DECL_INITIAL (r) = void_node;
13164 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13165 retrofit_lang_decl (r);
13166 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13167 }
13168 /* We don't have to set DECL_CONTEXT here; it is set by
13169 finish_member_declaration. */
13170 DECL_CHAIN (r) = NULL_TREE;
13171
13172 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13173 args, complain, in_decl);
13174
13175 if (vec)
13176 TREE_VEC_ELT (vec, i) = r;
13177 }
13178
13179 if (vec)
13180 r = vec;
13181 }
13182 break;
13183
13184 case USING_DECL:
13185 /* We reach here only for member using decls. We also need to check
13186 uses_template_parms because DECL_DEPENDENT_P is not set for a
13187 using-declaration that designates a member of the current
13188 instantiation (c++/53549). */
13189 if (DECL_DEPENDENT_P (t)
13190 || uses_template_parms (USING_DECL_SCOPE (t)))
13191 {
13192 tree scope = USING_DECL_SCOPE (t);
13193 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13194 if (PACK_EXPANSION_P (scope))
13195 {
13196 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13197 int len = TREE_VEC_LENGTH (vec);
13198 r = make_tree_vec (len);
13199 for (int i = 0; i < len; ++i)
13200 {
13201 tree escope = TREE_VEC_ELT (vec, i);
13202 tree elt = do_class_using_decl (escope, name);
13203 if (!elt)
13204 {
13205 r = error_mark_node;
13206 break;
13207 }
13208 else
13209 {
13210 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13211 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13212 }
13213 TREE_VEC_ELT (r, i) = elt;
13214 }
13215 }
13216 else
13217 {
13218 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13219 complain, in_decl);
13220 r = do_class_using_decl (inst_scope, name);
13221 if (!r)
13222 r = error_mark_node;
13223 else
13224 {
13225 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13226 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13227 }
13228 }
13229 }
13230 else
13231 {
13232 r = copy_node (t);
13233 DECL_CHAIN (r) = NULL_TREE;
13234 }
13235 break;
13236
13237 case TYPE_DECL:
13238 case VAR_DECL:
13239 {
13240 tree argvec = NULL_TREE;
13241 tree gen_tmpl = NULL_TREE;
13242 tree spec;
13243 tree tmpl = NULL_TREE;
13244 tree ctx;
13245 tree type = NULL_TREE;
13246 bool local_p;
13247
13248 if (TREE_TYPE (t) == error_mark_node)
13249 RETURN (error_mark_node);
13250
13251 if (TREE_CODE (t) == TYPE_DECL
13252 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13253 {
13254 /* If this is the canonical decl, we don't have to
13255 mess with instantiations, and often we can't (for
13256 typename, template type parms and such). Note that
13257 TYPE_NAME is not correct for the above test if
13258 we've copied the type for a typedef. */
13259 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13260 if (type == error_mark_node)
13261 RETURN (error_mark_node);
13262 r = TYPE_NAME (type);
13263 break;
13264 }
13265
13266 /* Check to see if we already have the specialization we
13267 need. */
13268 spec = NULL_TREE;
13269 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13270 {
13271 /* T is a static data member or namespace-scope entity.
13272 We have to substitute into namespace-scope variables
13273 (not just variable templates) because of cases like:
13274
13275 template <class T> void f() { extern T t; }
13276
13277 where the entity referenced is not known until
13278 instantiation time. */
13279 local_p = false;
13280 ctx = DECL_CONTEXT (t);
13281 if (DECL_CLASS_SCOPE_P (t))
13282 {
13283 ctx = tsubst_aggr_type (ctx, args,
13284 complain,
13285 in_decl, /*entering_scope=*/1);
13286 /* If CTX is unchanged, then T is in fact the
13287 specialization we want. That situation occurs when
13288 referencing a static data member within in its own
13289 class. We can use pointer equality, rather than
13290 same_type_p, because DECL_CONTEXT is always
13291 canonical... */
13292 if (ctx == DECL_CONTEXT (t)
13293 /* ... unless T is a member template; in which
13294 case our caller can be willing to create a
13295 specialization of that template represented
13296 by T. */
13297 && !(DECL_TI_TEMPLATE (t)
13298 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13299 spec = t;
13300 }
13301
13302 if (!spec)
13303 {
13304 tmpl = DECL_TI_TEMPLATE (t);
13305 gen_tmpl = most_general_template (tmpl);
13306 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13307 if (argvec != error_mark_node)
13308 argvec = (coerce_innermost_template_parms
13309 (DECL_TEMPLATE_PARMS (gen_tmpl),
13310 argvec, t, complain,
13311 /*all*/true, /*defarg*/true));
13312 if (argvec == error_mark_node)
13313 RETURN (error_mark_node);
13314 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13315 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13316 }
13317 }
13318 else
13319 {
13320 /* A local variable. */
13321 local_p = true;
13322 /* Subsequent calls to pushdecl will fill this in. */
13323 ctx = NULL_TREE;
13324 /* Unless this is a reference to a static variable from an
13325 enclosing function, in which case we need to fill it in now. */
13326 if (TREE_STATIC (t))
13327 {
13328 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13329 if (fn != current_function_decl)
13330 ctx = fn;
13331 }
13332 spec = retrieve_local_specialization (t);
13333 }
13334 /* If we already have the specialization we need, there is
13335 nothing more to do. */
13336 if (spec)
13337 {
13338 r = spec;
13339 break;
13340 }
13341
13342 /* Create a new node for the specialization we need. */
13343 r = copy_decl (t);
13344 if (type == NULL_TREE)
13345 {
13346 if (is_typedef_decl (t))
13347 type = DECL_ORIGINAL_TYPE (t);
13348 else
13349 type = TREE_TYPE (t);
13350 if (VAR_P (t)
13351 && VAR_HAD_UNKNOWN_BOUND (t)
13352 && type != error_mark_node)
13353 type = strip_array_domain (type);
13354 tree sub_args = args;
13355 if (tree auto_node = type_uses_auto (type))
13356 {
13357 /* Mask off any template args past the variable's context so we
13358 don't replace the auto with an unrelated argument. */
13359 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13360 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13361 if (extra > 0)
13362 /* This should never happen with the new lambda instantiation
13363 model, but keep the handling just in case. */
13364 gcc_assert (!CHECKING_P),
13365 sub_args = strip_innermost_template_args (args, extra);
13366 }
13367 type = tsubst (type, sub_args, complain, in_decl);
13368 }
13369 if (VAR_P (r))
13370 {
13371 /* Even if the original location is out of scope, the
13372 newly substituted one is not. */
13373 DECL_DEAD_FOR_LOCAL (r) = 0;
13374 DECL_INITIALIZED_P (r) = 0;
13375 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13376 if (type == error_mark_node)
13377 RETURN (error_mark_node);
13378 if (TREE_CODE (type) == FUNCTION_TYPE)
13379 {
13380 /* It may seem that this case cannot occur, since:
13381
13382 typedef void f();
13383 void g() { f x; }
13384
13385 declares a function, not a variable. However:
13386
13387 typedef void f();
13388 template <typename T> void g() { T t; }
13389 template void g<f>();
13390
13391 is an attempt to declare a variable with function
13392 type. */
13393 error ("variable %qD has function type",
13394 /* R is not yet sufficiently initialized, so we
13395 just use its name. */
13396 DECL_NAME (r));
13397 RETURN (error_mark_node);
13398 }
13399 type = complete_type (type);
13400 /* Wait until cp_finish_decl to set this again, to handle
13401 circular dependency (template/instantiate6.C). */
13402 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13403 type = check_var_type (DECL_NAME (r), type);
13404
13405 if (DECL_HAS_VALUE_EXPR_P (t))
13406 {
13407 tree ve = DECL_VALUE_EXPR (t);
13408 ve = tsubst_expr (ve, args, complain, in_decl,
13409 /*constant_expression_p=*/false);
13410 if (REFERENCE_REF_P (ve))
13411 {
13412 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13413 ve = TREE_OPERAND (ve, 0);
13414 }
13415 SET_DECL_VALUE_EXPR (r, ve);
13416 }
13417 if (CP_DECL_THREAD_LOCAL_P (r)
13418 && !processing_template_decl)
13419 set_decl_tls_model (r, decl_default_tls_model (r));
13420 }
13421 else if (DECL_SELF_REFERENCE_P (t))
13422 SET_DECL_SELF_REFERENCE_P (r);
13423 TREE_TYPE (r) = type;
13424 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13425 DECL_CONTEXT (r) = ctx;
13426 /* Clear out the mangled name and RTL for the instantiation. */
13427 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13428 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13429 SET_DECL_RTL (r, NULL);
13430 /* The initializer must not be expanded until it is required;
13431 see [temp.inst]. */
13432 DECL_INITIAL (r) = NULL_TREE;
13433 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13434 if (VAR_P (r))
13435 {
13436 if (DECL_LANG_SPECIFIC (r))
13437 SET_DECL_DEPENDENT_INIT_P (r, false);
13438
13439 SET_DECL_MODE (r, VOIDmode);
13440
13441 /* Possibly limit visibility based on template args. */
13442 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13443 if (DECL_VISIBILITY_SPECIFIED (t))
13444 {
13445 DECL_VISIBILITY_SPECIFIED (r) = 0;
13446 DECL_ATTRIBUTES (r)
13447 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13448 }
13449 determine_visibility (r);
13450 }
13451
13452 if (!local_p)
13453 {
13454 /* A static data member declaration is always marked
13455 external when it is declared in-class, even if an
13456 initializer is present. We mimic the non-template
13457 processing here. */
13458 DECL_EXTERNAL (r) = 1;
13459 if (DECL_NAMESPACE_SCOPE_P (t))
13460 DECL_NOT_REALLY_EXTERN (r) = 1;
13461
13462 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13463 SET_DECL_IMPLICIT_INSTANTIATION (r);
13464 register_specialization (r, gen_tmpl, argvec, false, hash);
13465 }
13466 else
13467 {
13468 if (DECL_LANG_SPECIFIC (r))
13469 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13470 if (!cp_unevaluated_operand)
13471 register_local_specialization (r, t);
13472 }
13473
13474 DECL_CHAIN (r) = NULL_TREE;
13475
13476 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13477 /*flags=*/0,
13478 args, complain, in_decl);
13479
13480 /* Preserve a typedef that names a type. */
13481 if (is_typedef_decl (r) && type != error_mark_node)
13482 {
13483 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13484 set_underlying_type (r);
13485 if (TYPE_DECL_ALIAS_P (r))
13486 /* An alias template specialization can be dependent
13487 even if its underlying type is not. */
13488 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13489 }
13490
13491 layout_decl (r, 0);
13492 }
13493 break;
13494
13495 default:
13496 gcc_unreachable ();
13497 }
13498 #undef RETURN
13499
13500 out:
13501 /* Restore the file and line information. */
13502 input_location = saved_loc;
13503
13504 return r;
13505 }
13506
13507 /* Substitute into the ARG_TYPES of a function type.
13508 If END is a TREE_CHAIN, leave it and any following types
13509 un-substituted. */
13510
13511 static tree
13512 tsubst_arg_types (tree arg_types,
13513 tree args,
13514 tree end,
13515 tsubst_flags_t complain,
13516 tree in_decl)
13517 {
13518 tree remaining_arg_types;
13519 tree type = NULL_TREE;
13520 int i = 1;
13521 tree expanded_args = NULL_TREE;
13522 tree default_arg;
13523
13524 if (!arg_types || arg_types == void_list_node || arg_types == end)
13525 return arg_types;
13526
13527 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13528 args, end, complain, in_decl);
13529 if (remaining_arg_types == error_mark_node)
13530 return error_mark_node;
13531
13532 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13533 {
13534 /* For a pack expansion, perform substitution on the
13535 entire expression. Later on, we'll handle the arguments
13536 one-by-one. */
13537 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13538 args, complain, in_decl);
13539
13540 if (TREE_CODE (expanded_args) == TREE_VEC)
13541 /* So that we'll spin through the parameters, one by one. */
13542 i = TREE_VEC_LENGTH (expanded_args);
13543 else
13544 {
13545 /* We only partially substituted into the parameter
13546 pack. Our type is TYPE_PACK_EXPANSION. */
13547 type = expanded_args;
13548 expanded_args = NULL_TREE;
13549 }
13550 }
13551
13552 while (i > 0) {
13553 --i;
13554
13555 if (expanded_args)
13556 type = TREE_VEC_ELT (expanded_args, i);
13557 else if (!type)
13558 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13559
13560 if (type == error_mark_node)
13561 return error_mark_node;
13562 if (VOID_TYPE_P (type))
13563 {
13564 if (complain & tf_error)
13565 {
13566 error ("invalid parameter type %qT", type);
13567 if (in_decl)
13568 error ("in declaration %q+D", in_decl);
13569 }
13570 return error_mark_node;
13571 }
13572 /* DR 657. */
13573 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13574 return error_mark_node;
13575
13576 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13577 top-level qualifiers as required. */
13578 type = cv_unqualified (type_decays_to (type));
13579
13580 /* We do not substitute into default arguments here. The standard
13581 mandates that they be instantiated only when needed, which is
13582 done in build_over_call. */
13583 default_arg = TREE_PURPOSE (arg_types);
13584
13585 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13586 since the new op() won't have any associated template arguments for us
13587 to refer to later. */
13588 if (lambda_fn_in_template_p (in_decl))
13589 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13590 false/*fn*/, false/*constexpr*/);
13591
13592 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13593 {
13594 /* We've instantiated a template before its default arguments
13595 have been parsed. This can happen for a nested template
13596 class, and is not an error unless we require the default
13597 argument in a call of this function. */
13598 remaining_arg_types =
13599 tree_cons (default_arg, type, remaining_arg_types);
13600 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13601 }
13602 else
13603 remaining_arg_types =
13604 hash_tree_cons (default_arg, type, remaining_arg_types);
13605 }
13606
13607 return remaining_arg_types;
13608 }
13609
13610 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13611 *not* handle the exception-specification for FNTYPE, because the
13612 initial substitution of explicitly provided template parameters
13613 during argument deduction forbids substitution into the
13614 exception-specification:
13615
13616 [temp.deduct]
13617
13618 All references in the function type of the function template to the
13619 corresponding template parameters are replaced by the specified tem-
13620 plate argument values. If a substitution in a template parameter or
13621 in the function type of the function template results in an invalid
13622 type, type deduction fails. [Note: The equivalent substitution in
13623 exception specifications is done only when the function is instanti-
13624 ated, at which point a program is ill-formed if the substitution
13625 results in an invalid type.] */
13626
13627 static tree
13628 tsubst_function_type (tree t,
13629 tree args,
13630 tsubst_flags_t complain,
13631 tree in_decl)
13632 {
13633 tree return_type;
13634 tree arg_types = NULL_TREE;
13635 tree fntype;
13636
13637 /* The TYPE_CONTEXT is not used for function/method types. */
13638 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13639
13640 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13641 failure. */
13642 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13643
13644 if (late_return_type_p)
13645 {
13646 /* Substitute the argument types. */
13647 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13648 complain, in_decl);
13649 if (arg_types == error_mark_node)
13650 return error_mark_node;
13651
13652 tree save_ccp = current_class_ptr;
13653 tree save_ccr = current_class_ref;
13654 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13655 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13656 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13657 if (do_inject)
13658 {
13659 /* DR 1207: 'this' is in scope in the trailing return type. */
13660 inject_this_parameter (this_type, cp_type_quals (this_type));
13661 }
13662
13663 /* Substitute the return type. */
13664 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13665
13666 if (do_inject)
13667 {
13668 current_class_ptr = save_ccp;
13669 current_class_ref = save_ccr;
13670 }
13671 }
13672 else
13673 /* Substitute the return type. */
13674 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13675
13676 if (return_type == error_mark_node)
13677 return error_mark_node;
13678 /* DR 486 clarifies that creation of a function type with an
13679 invalid return type is a deduction failure. */
13680 if (TREE_CODE (return_type) == ARRAY_TYPE
13681 || TREE_CODE (return_type) == FUNCTION_TYPE)
13682 {
13683 if (complain & tf_error)
13684 {
13685 if (TREE_CODE (return_type) == ARRAY_TYPE)
13686 error ("function returning an array");
13687 else
13688 error ("function returning a function");
13689 }
13690 return error_mark_node;
13691 }
13692 /* And DR 657. */
13693 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13694 return error_mark_node;
13695
13696 if (!late_return_type_p)
13697 {
13698 /* Substitute the argument types. */
13699 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13700 complain, in_decl);
13701 if (arg_types == error_mark_node)
13702 return error_mark_node;
13703 }
13704
13705 /* Construct a new type node and return it. */
13706 if (TREE_CODE (t) == FUNCTION_TYPE)
13707 {
13708 fntype = build_function_type (return_type, arg_types);
13709 fntype = apply_memfn_quals (fntype,
13710 type_memfn_quals (t),
13711 type_memfn_rqual (t));
13712 }
13713 else
13714 {
13715 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13716 /* Don't pick up extra function qualifiers from the basetype. */
13717 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13718 if (! MAYBE_CLASS_TYPE_P (r))
13719 {
13720 /* [temp.deduct]
13721
13722 Type deduction may fail for any of the following
13723 reasons:
13724
13725 -- Attempting to create "pointer to member of T" when T
13726 is not a class type. */
13727 if (complain & tf_error)
13728 error ("creating pointer to member function of non-class type %qT",
13729 r);
13730 return error_mark_node;
13731 }
13732
13733 fntype = build_method_type_directly (r, return_type,
13734 TREE_CHAIN (arg_types));
13735 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13736 }
13737 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13738
13739 if (late_return_type_p)
13740 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13741
13742 return fntype;
13743 }
13744
13745 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13746 ARGS into that specification, and return the substituted
13747 specification. If there is no specification, return NULL_TREE. */
13748
13749 static tree
13750 tsubst_exception_specification (tree fntype,
13751 tree args,
13752 tsubst_flags_t complain,
13753 tree in_decl,
13754 bool defer_ok)
13755 {
13756 tree specs;
13757 tree new_specs;
13758
13759 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13760 new_specs = NULL_TREE;
13761 if (specs && TREE_PURPOSE (specs))
13762 {
13763 /* A noexcept-specifier. */
13764 tree expr = TREE_PURPOSE (specs);
13765 if (TREE_CODE (expr) == INTEGER_CST)
13766 new_specs = expr;
13767 else if (defer_ok)
13768 {
13769 /* Defer instantiation of noexcept-specifiers to avoid
13770 excessive instantiations (c++/49107). */
13771 new_specs = make_node (DEFERRED_NOEXCEPT);
13772 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13773 {
13774 /* We already partially instantiated this member template,
13775 so combine the new args with the old. */
13776 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13777 = DEFERRED_NOEXCEPT_PATTERN (expr);
13778 DEFERRED_NOEXCEPT_ARGS (new_specs)
13779 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13780 }
13781 else
13782 {
13783 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13784 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13785 }
13786 }
13787 else
13788 new_specs = tsubst_copy_and_build
13789 (expr, args, complain, in_decl, /*function_p=*/false,
13790 /*integral_constant_expression_p=*/true);
13791 new_specs = build_noexcept_spec (new_specs, complain);
13792 }
13793 else if (specs)
13794 {
13795 if (! TREE_VALUE (specs))
13796 new_specs = specs;
13797 else
13798 while (specs)
13799 {
13800 tree spec;
13801 int i, len = 1;
13802 tree expanded_specs = NULL_TREE;
13803
13804 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13805 {
13806 /* Expand the pack expansion type. */
13807 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13808 args, complain,
13809 in_decl);
13810
13811 if (expanded_specs == error_mark_node)
13812 return error_mark_node;
13813 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13814 len = TREE_VEC_LENGTH (expanded_specs);
13815 else
13816 {
13817 /* We're substituting into a member template, so
13818 we got a TYPE_PACK_EXPANSION back. Add that
13819 expansion and move on. */
13820 gcc_assert (TREE_CODE (expanded_specs)
13821 == TYPE_PACK_EXPANSION);
13822 new_specs = add_exception_specifier (new_specs,
13823 expanded_specs,
13824 complain);
13825 specs = TREE_CHAIN (specs);
13826 continue;
13827 }
13828 }
13829
13830 for (i = 0; i < len; ++i)
13831 {
13832 if (expanded_specs)
13833 spec = TREE_VEC_ELT (expanded_specs, i);
13834 else
13835 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13836 if (spec == error_mark_node)
13837 return spec;
13838 new_specs = add_exception_specifier (new_specs, spec,
13839 complain);
13840 }
13841
13842 specs = TREE_CHAIN (specs);
13843 }
13844 }
13845 return new_specs;
13846 }
13847
13848 /* Take the tree structure T and replace template parameters used
13849 therein with the argument vector ARGS. IN_DECL is an associated
13850 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13851 Issue error and warning messages under control of COMPLAIN. Note
13852 that we must be relatively non-tolerant of extensions here, in
13853 order to preserve conformance; if we allow substitutions that
13854 should not be allowed, we may allow argument deductions that should
13855 not succeed, and therefore report ambiguous overload situations
13856 where there are none. In theory, we could allow the substitution,
13857 but indicate that it should have failed, and allow our caller to
13858 make sure that the right thing happens, but we don't try to do this
13859 yet.
13860
13861 This function is used for dealing with types, decls and the like;
13862 for expressions, use tsubst_expr or tsubst_copy. */
13863
13864 tree
13865 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13866 {
13867 enum tree_code code;
13868 tree type, r = NULL_TREE;
13869
13870 if (t == NULL_TREE || t == error_mark_node
13871 || t == integer_type_node
13872 || t == void_type_node
13873 || t == char_type_node
13874 || t == unknown_type_node
13875 || TREE_CODE (t) == NAMESPACE_DECL
13876 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13877 return t;
13878
13879 if (DECL_P (t))
13880 return tsubst_decl (t, args, complain);
13881
13882 if (args == NULL_TREE)
13883 return t;
13884
13885 code = TREE_CODE (t);
13886
13887 if (code == IDENTIFIER_NODE)
13888 type = IDENTIFIER_TYPE_VALUE (t);
13889 else
13890 type = TREE_TYPE (t);
13891
13892 gcc_assert (type != unknown_type_node);
13893
13894 /* Reuse typedefs. We need to do this to handle dependent attributes,
13895 such as attribute aligned. */
13896 if (TYPE_P (t)
13897 && typedef_variant_p (t))
13898 {
13899 tree decl = TYPE_NAME (t);
13900
13901 if (alias_template_specialization_p (t))
13902 {
13903 /* DECL represents an alias template and we want to
13904 instantiate it. */
13905 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13906 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13907 r = instantiate_alias_template (tmpl, gen_args, complain);
13908 }
13909 else if (DECL_CLASS_SCOPE_P (decl)
13910 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13911 && uses_template_parms (DECL_CONTEXT (decl)))
13912 {
13913 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13914 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13915 r = retrieve_specialization (tmpl, gen_args, 0);
13916 }
13917 else if (DECL_FUNCTION_SCOPE_P (decl)
13918 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13919 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13920 r = retrieve_local_specialization (decl);
13921 else
13922 /* The typedef is from a non-template context. */
13923 return t;
13924
13925 if (r)
13926 {
13927 r = TREE_TYPE (r);
13928 r = cp_build_qualified_type_real
13929 (r, cp_type_quals (t) | cp_type_quals (r),
13930 complain | tf_ignore_bad_quals);
13931 return r;
13932 }
13933 else
13934 {
13935 /* We don't have an instantiation yet, so drop the typedef. */
13936 int quals = cp_type_quals (t);
13937 t = DECL_ORIGINAL_TYPE (decl);
13938 t = cp_build_qualified_type_real (t, quals,
13939 complain | tf_ignore_bad_quals);
13940 }
13941 }
13942
13943 bool fndecl_type = (complain & tf_fndecl_type);
13944 complain &= ~tf_fndecl_type;
13945
13946 if (type
13947 && code != TYPENAME_TYPE
13948 && code != TEMPLATE_TYPE_PARM
13949 && code != TEMPLATE_PARM_INDEX
13950 && code != IDENTIFIER_NODE
13951 && code != FUNCTION_TYPE
13952 && code != METHOD_TYPE)
13953 type = tsubst (type, args, complain, in_decl);
13954 if (type == error_mark_node)
13955 return error_mark_node;
13956
13957 switch (code)
13958 {
13959 case RECORD_TYPE:
13960 case UNION_TYPE:
13961 case ENUMERAL_TYPE:
13962 return tsubst_aggr_type (t, args, complain, in_decl,
13963 /*entering_scope=*/0);
13964
13965 case ERROR_MARK:
13966 case IDENTIFIER_NODE:
13967 case VOID_TYPE:
13968 case REAL_TYPE:
13969 case COMPLEX_TYPE:
13970 case VECTOR_TYPE:
13971 case BOOLEAN_TYPE:
13972 case NULLPTR_TYPE:
13973 case LANG_TYPE:
13974 return t;
13975
13976 case INTEGER_TYPE:
13977 if (t == integer_type_node)
13978 return t;
13979
13980 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13981 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13982 return t;
13983
13984 {
13985 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13986
13987 max = tsubst_expr (omax, args, complain, in_decl,
13988 /*integral_constant_expression_p=*/false);
13989
13990 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13991 needed. */
13992 if (TREE_CODE (max) == NOP_EXPR
13993 && TREE_SIDE_EFFECTS (omax)
13994 && !TREE_TYPE (max))
13995 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13996
13997 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13998 with TREE_SIDE_EFFECTS that indicates this is not an integral
13999 constant expression. */
14000 if (processing_template_decl
14001 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14002 {
14003 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14004 TREE_SIDE_EFFECTS (max) = 1;
14005 }
14006
14007 return compute_array_index_type (NULL_TREE, max, complain);
14008 }
14009
14010 case TEMPLATE_TYPE_PARM:
14011 case TEMPLATE_TEMPLATE_PARM:
14012 case BOUND_TEMPLATE_TEMPLATE_PARM:
14013 case TEMPLATE_PARM_INDEX:
14014 {
14015 int idx;
14016 int level;
14017 int levels;
14018 tree arg = NULL_TREE;
14019
14020 /* Early in template argument deduction substitution, we don't
14021 want to reduce the level of 'auto', or it will be confused
14022 with a normal template parm in subsequent deduction. */
14023 if (is_auto (t) && (complain & tf_partial))
14024 return t;
14025
14026 r = NULL_TREE;
14027
14028 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14029 template_parm_level_and_index (t, &level, &idx);
14030
14031 levels = TMPL_ARGS_DEPTH (args);
14032 if (level <= levels
14033 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14034 {
14035 arg = TMPL_ARG (args, level, idx);
14036
14037 /* See through ARGUMENT_PACK_SELECT arguments. */
14038 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14039 arg = argument_pack_select_arg (arg);
14040 }
14041
14042 if (arg == error_mark_node)
14043 return error_mark_node;
14044 else if (arg != NULL_TREE)
14045 {
14046 if (ARGUMENT_PACK_P (arg))
14047 /* If ARG is an argument pack, we don't actually want to
14048 perform a substitution here, because substitutions
14049 for argument packs are only done
14050 element-by-element. We can get to this point when
14051 substituting the type of a non-type template
14052 parameter pack, when that type actually contains
14053 template parameter packs from an outer template, e.g.,
14054
14055 template<typename... Types> struct A {
14056 template<Types... Values> struct B { };
14057 }; */
14058 return t;
14059
14060 if (code == TEMPLATE_TYPE_PARM)
14061 {
14062 int quals;
14063 gcc_assert (TYPE_P (arg));
14064
14065 quals = cp_type_quals (arg) | cp_type_quals (t);
14066
14067 return cp_build_qualified_type_real
14068 (arg, quals, complain | tf_ignore_bad_quals);
14069 }
14070 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14071 {
14072 /* We are processing a type constructed from a
14073 template template parameter. */
14074 tree argvec = tsubst (TYPE_TI_ARGS (t),
14075 args, complain, in_decl);
14076 if (argvec == error_mark_node)
14077 return error_mark_node;
14078
14079 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14080 || TREE_CODE (arg) == TEMPLATE_DECL
14081 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14082
14083 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14084 /* Consider this code:
14085
14086 template <template <class> class Template>
14087 struct Internal {
14088 template <class Arg> using Bind = Template<Arg>;
14089 };
14090
14091 template <template <class> class Template, class Arg>
14092 using Instantiate = Template<Arg>; //#0
14093
14094 template <template <class> class Template,
14095 class Argument>
14096 using Bind =
14097 Instantiate<Internal<Template>::template Bind,
14098 Argument>; //#1
14099
14100 When #1 is parsed, the
14101 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14102 parameter `Template' in #0 matches the
14103 UNBOUND_CLASS_TEMPLATE representing the argument
14104 `Internal<Template>::template Bind'; We then want
14105 to assemble the type `Bind<Argument>' that can't
14106 be fully created right now, because
14107 `Internal<Template>' not being complete, the Bind
14108 template cannot be looked up in that context. So
14109 we need to "store" `Bind<Argument>' for later
14110 when the context of Bind becomes complete. Let's
14111 store that in a TYPENAME_TYPE. */
14112 return make_typename_type (TYPE_CONTEXT (arg),
14113 build_nt (TEMPLATE_ID_EXPR,
14114 TYPE_IDENTIFIER (arg),
14115 argvec),
14116 typename_type,
14117 complain);
14118
14119 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14120 are resolving nested-types in the signature of a
14121 member function templates. Otherwise ARG is a
14122 TEMPLATE_DECL and is the real template to be
14123 instantiated. */
14124 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14125 arg = TYPE_NAME (arg);
14126
14127 r = lookup_template_class (arg,
14128 argvec, in_decl,
14129 DECL_CONTEXT (arg),
14130 /*entering_scope=*/0,
14131 complain);
14132 return cp_build_qualified_type_real
14133 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14134 }
14135 else if (code == TEMPLATE_TEMPLATE_PARM)
14136 return arg;
14137 else
14138 /* TEMPLATE_PARM_INDEX. */
14139 return convert_from_reference (unshare_expr (arg));
14140 }
14141
14142 if (level == 1)
14143 /* This can happen during the attempted tsubst'ing in
14144 unify. This means that we don't yet have any information
14145 about the template parameter in question. */
14146 return t;
14147
14148 /* If we get here, we must have been looking at a parm for a
14149 more deeply nested template. Make a new version of this
14150 template parameter, but with a lower level. */
14151 switch (code)
14152 {
14153 case TEMPLATE_TYPE_PARM:
14154 case TEMPLATE_TEMPLATE_PARM:
14155 case BOUND_TEMPLATE_TEMPLATE_PARM:
14156 if (cp_type_quals (t))
14157 {
14158 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14159 r = cp_build_qualified_type_real
14160 (r, cp_type_quals (t),
14161 complain | (code == TEMPLATE_TYPE_PARM
14162 ? tf_ignore_bad_quals : 0));
14163 }
14164 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14165 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14166 && (r = (TEMPLATE_PARM_DESCENDANTS
14167 (TEMPLATE_TYPE_PARM_INDEX (t))))
14168 && (r = TREE_TYPE (r))
14169 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14170 /* Break infinite recursion when substituting the constraints
14171 of a constrained placeholder. */;
14172 else
14173 {
14174 r = copy_type (t);
14175 TEMPLATE_TYPE_PARM_INDEX (r)
14176 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14177 r, levels, args, complain);
14178 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14179 TYPE_MAIN_VARIANT (r) = r;
14180 TYPE_POINTER_TO (r) = NULL_TREE;
14181 TYPE_REFERENCE_TO (r) = NULL_TREE;
14182
14183 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14184 {
14185 /* Propagate constraints on placeholders. */
14186 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14187 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14188 = tsubst_constraint (constr, args, complain, in_decl);
14189 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14190 {
14191 pl = tsubst_copy (pl, args, complain, in_decl);
14192 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14193 }
14194 }
14195
14196 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14197 /* We have reduced the level of the template
14198 template parameter, but not the levels of its
14199 template parameters, so canonical_type_parameter
14200 will not be able to find the canonical template
14201 template parameter for this level. Thus, we
14202 require structural equality checking to compare
14203 TEMPLATE_TEMPLATE_PARMs. */
14204 SET_TYPE_STRUCTURAL_EQUALITY (r);
14205 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14206 SET_TYPE_STRUCTURAL_EQUALITY (r);
14207 else
14208 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14209
14210 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14211 {
14212 tree tinfo = TYPE_TEMPLATE_INFO (t);
14213 /* We might need to substitute into the types of non-type
14214 template parameters. */
14215 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14216 complain, in_decl);
14217 if (tmpl == error_mark_node)
14218 return error_mark_node;
14219 tree argvec = tsubst (TI_ARGS (tinfo), args,
14220 complain, in_decl);
14221 if (argvec == error_mark_node)
14222 return error_mark_node;
14223
14224 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14225 = build_template_info (tmpl, argvec);
14226 }
14227 }
14228 break;
14229
14230 case TEMPLATE_PARM_INDEX:
14231 /* OK, now substitute the type of the non-type parameter. We
14232 couldn't do it earlier because it might be an auto parameter,
14233 and we wouldn't need to if we had an argument. */
14234 type = tsubst (type, args, complain, in_decl);
14235 if (type == error_mark_node)
14236 return error_mark_node;
14237 r = reduce_template_parm_level (t, type, levels, args, complain);
14238 break;
14239
14240 default:
14241 gcc_unreachable ();
14242 }
14243
14244 return r;
14245 }
14246
14247 case TREE_LIST:
14248 {
14249 tree purpose, value, chain;
14250
14251 if (t == void_list_node)
14252 return t;
14253
14254 purpose = TREE_PURPOSE (t);
14255 if (purpose)
14256 {
14257 purpose = tsubst (purpose, args, complain, in_decl);
14258 if (purpose == error_mark_node)
14259 return error_mark_node;
14260 }
14261 value = TREE_VALUE (t);
14262 if (value)
14263 {
14264 value = tsubst (value, args, complain, in_decl);
14265 if (value == error_mark_node)
14266 return error_mark_node;
14267 }
14268 chain = TREE_CHAIN (t);
14269 if (chain && chain != void_type_node)
14270 {
14271 chain = tsubst (chain, args, complain, in_decl);
14272 if (chain == error_mark_node)
14273 return error_mark_node;
14274 }
14275 if (purpose == TREE_PURPOSE (t)
14276 && value == TREE_VALUE (t)
14277 && chain == TREE_CHAIN (t))
14278 return t;
14279 return hash_tree_cons (purpose, value, chain);
14280 }
14281
14282 case TREE_BINFO:
14283 /* We should never be tsubsting a binfo. */
14284 gcc_unreachable ();
14285
14286 case TREE_VEC:
14287 /* A vector of template arguments. */
14288 gcc_assert (!type);
14289 return tsubst_template_args (t, args, complain, in_decl);
14290
14291 case POINTER_TYPE:
14292 case REFERENCE_TYPE:
14293 {
14294 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14295 return t;
14296
14297 /* [temp.deduct]
14298
14299 Type deduction may fail for any of the following
14300 reasons:
14301
14302 -- Attempting to create a pointer to reference type.
14303 -- Attempting to create a reference to a reference type or
14304 a reference to void.
14305
14306 Core issue 106 says that creating a reference to a reference
14307 during instantiation is no longer a cause for failure. We
14308 only enforce this check in strict C++98 mode. */
14309 if ((TREE_CODE (type) == REFERENCE_TYPE
14310 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14311 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14312 {
14313 static location_t last_loc;
14314
14315 /* We keep track of the last time we issued this error
14316 message to avoid spewing a ton of messages during a
14317 single bad template instantiation. */
14318 if (complain & tf_error
14319 && last_loc != input_location)
14320 {
14321 if (VOID_TYPE_P (type))
14322 error ("forming reference to void");
14323 else if (code == POINTER_TYPE)
14324 error ("forming pointer to reference type %qT", type);
14325 else
14326 error ("forming reference to reference type %qT", type);
14327 last_loc = input_location;
14328 }
14329
14330 return error_mark_node;
14331 }
14332 else if (TREE_CODE (type) == FUNCTION_TYPE
14333 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14334 || type_memfn_rqual (type) != REF_QUAL_NONE))
14335 {
14336 if (complain & tf_error)
14337 {
14338 if (code == POINTER_TYPE)
14339 error ("forming pointer to qualified function type %qT",
14340 type);
14341 else
14342 error ("forming reference to qualified function type %qT",
14343 type);
14344 }
14345 return error_mark_node;
14346 }
14347 else if (code == POINTER_TYPE)
14348 {
14349 r = build_pointer_type (type);
14350 if (TREE_CODE (type) == METHOD_TYPE)
14351 r = build_ptrmemfunc_type (r);
14352 }
14353 else if (TREE_CODE (type) == REFERENCE_TYPE)
14354 /* In C++0x, during template argument substitution, when there is an
14355 attempt to create a reference to a reference type, reference
14356 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14357
14358 "If a template-argument for a template-parameter T names a type
14359 that is a reference to a type A, an attempt to create the type
14360 'lvalue reference to cv T' creates the type 'lvalue reference to
14361 A,' while an attempt to create the type type rvalue reference to
14362 cv T' creates the type T"
14363 */
14364 r = cp_build_reference_type
14365 (TREE_TYPE (type),
14366 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14367 else
14368 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14369 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14370
14371 if (r != error_mark_node)
14372 /* Will this ever be needed for TYPE_..._TO values? */
14373 layout_type (r);
14374
14375 return r;
14376 }
14377 case OFFSET_TYPE:
14378 {
14379 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14380 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14381 {
14382 /* [temp.deduct]
14383
14384 Type deduction may fail for any of the following
14385 reasons:
14386
14387 -- Attempting to create "pointer to member of T" when T
14388 is not a class type. */
14389 if (complain & tf_error)
14390 error ("creating pointer to member of non-class type %qT", r);
14391 return error_mark_node;
14392 }
14393 if (TREE_CODE (type) == REFERENCE_TYPE)
14394 {
14395 if (complain & tf_error)
14396 error ("creating pointer to member reference type %qT", type);
14397 return error_mark_node;
14398 }
14399 if (VOID_TYPE_P (type))
14400 {
14401 if (complain & tf_error)
14402 error ("creating pointer to member of type void");
14403 return error_mark_node;
14404 }
14405 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14406 if (TREE_CODE (type) == FUNCTION_TYPE)
14407 {
14408 /* The type of the implicit object parameter gets its
14409 cv-qualifiers from the FUNCTION_TYPE. */
14410 tree memptr;
14411 tree method_type
14412 = build_memfn_type (type, r, type_memfn_quals (type),
14413 type_memfn_rqual (type));
14414 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14415 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14416 complain);
14417 }
14418 else
14419 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14420 cp_type_quals (t),
14421 complain);
14422 }
14423 case FUNCTION_TYPE:
14424 case METHOD_TYPE:
14425 {
14426 tree fntype;
14427 tree specs;
14428 fntype = tsubst_function_type (t, args, complain, in_decl);
14429 if (fntype == error_mark_node)
14430 return error_mark_node;
14431
14432 /* Substitute the exception specification. */
14433 specs = tsubst_exception_specification (t, args, complain, in_decl,
14434 /*defer_ok*/fndecl_type);
14435 if (specs == error_mark_node)
14436 return error_mark_node;
14437 if (specs)
14438 fntype = build_exception_variant (fntype, specs);
14439 return fntype;
14440 }
14441 case ARRAY_TYPE:
14442 {
14443 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14444 if (domain == error_mark_node)
14445 return error_mark_node;
14446
14447 /* As an optimization, we avoid regenerating the array type if
14448 it will obviously be the same as T. */
14449 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14450 return t;
14451
14452 /* These checks should match the ones in create_array_type_for_decl.
14453
14454 [temp.deduct]
14455
14456 The deduction may fail for any of the following reasons:
14457
14458 -- Attempting to create an array with an element type that
14459 is void, a function type, or a reference type, or [DR337]
14460 an abstract class type. */
14461 if (VOID_TYPE_P (type)
14462 || TREE_CODE (type) == FUNCTION_TYPE
14463 || (TREE_CODE (type) == ARRAY_TYPE
14464 && TYPE_DOMAIN (type) == NULL_TREE)
14465 || TREE_CODE (type) == REFERENCE_TYPE)
14466 {
14467 if (complain & tf_error)
14468 error ("creating array of %qT", type);
14469 return error_mark_node;
14470 }
14471
14472 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14473 return error_mark_node;
14474
14475 r = build_cplus_array_type (type, domain);
14476
14477 if (TYPE_USER_ALIGN (t))
14478 {
14479 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14480 TYPE_USER_ALIGN (r) = 1;
14481 }
14482
14483 return r;
14484 }
14485
14486 case TYPENAME_TYPE:
14487 {
14488 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14489 in_decl, /*entering_scope=*/1);
14490 if (ctx == error_mark_node)
14491 return error_mark_node;
14492
14493 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14494 complain, in_decl);
14495 if (f == error_mark_node)
14496 return error_mark_node;
14497
14498 if (!MAYBE_CLASS_TYPE_P (ctx))
14499 {
14500 if (complain & tf_error)
14501 error ("%qT is not a class, struct, or union type", ctx);
14502 return error_mark_node;
14503 }
14504 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14505 {
14506 /* Normally, make_typename_type does not require that the CTX
14507 have complete type in order to allow things like:
14508
14509 template <class T> struct S { typename S<T>::X Y; };
14510
14511 But, such constructs have already been resolved by this
14512 point, so here CTX really should have complete type, unless
14513 it's a partial instantiation. */
14514 ctx = complete_type (ctx);
14515 if (!COMPLETE_TYPE_P (ctx))
14516 {
14517 if (complain & tf_error)
14518 cxx_incomplete_type_error (NULL_TREE, ctx);
14519 return error_mark_node;
14520 }
14521 }
14522
14523 f = make_typename_type (ctx, f, typename_type,
14524 complain | tf_keep_type_decl);
14525 if (f == error_mark_node)
14526 return f;
14527 if (TREE_CODE (f) == TYPE_DECL)
14528 {
14529 complain |= tf_ignore_bad_quals;
14530 f = TREE_TYPE (f);
14531 }
14532
14533 if (TREE_CODE (f) != TYPENAME_TYPE)
14534 {
14535 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14536 {
14537 if (complain & tf_error)
14538 error ("%qT resolves to %qT, which is not an enumeration type",
14539 t, f);
14540 else
14541 return error_mark_node;
14542 }
14543 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14544 {
14545 if (complain & tf_error)
14546 error ("%qT resolves to %qT, which is is not a class type",
14547 t, f);
14548 else
14549 return error_mark_node;
14550 }
14551 }
14552
14553 return cp_build_qualified_type_real
14554 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14555 }
14556
14557 case UNBOUND_CLASS_TEMPLATE:
14558 {
14559 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14560 in_decl, /*entering_scope=*/1);
14561 tree name = TYPE_IDENTIFIER (t);
14562 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14563
14564 if (ctx == error_mark_node || name == error_mark_node)
14565 return error_mark_node;
14566
14567 if (parm_list)
14568 parm_list = tsubst_template_parms (parm_list, args, complain);
14569 return make_unbound_class_template (ctx, name, parm_list, complain);
14570 }
14571
14572 case TYPEOF_TYPE:
14573 {
14574 tree type;
14575
14576 ++cp_unevaluated_operand;
14577 ++c_inhibit_evaluation_warnings;
14578
14579 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14580 complain, in_decl,
14581 /*integral_constant_expression_p=*/false);
14582
14583 --cp_unevaluated_operand;
14584 --c_inhibit_evaluation_warnings;
14585
14586 type = finish_typeof (type);
14587 return cp_build_qualified_type_real (type,
14588 cp_type_quals (t)
14589 | cp_type_quals (type),
14590 complain);
14591 }
14592
14593 case DECLTYPE_TYPE:
14594 {
14595 tree type;
14596
14597 ++cp_unevaluated_operand;
14598 ++c_inhibit_evaluation_warnings;
14599
14600 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14601 complain|tf_decltype, in_decl,
14602 /*function_p*/false,
14603 /*integral_constant_expression*/false);
14604
14605 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14606 {
14607 if (type == NULL_TREE)
14608 {
14609 if (complain & tf_error)
14610 error ("empty initializer in lambda init-capture");
14611 type = error_mark_node;
14612 }
14613 else if (TREE_CODE (type) == TREE_LIST)
14614 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14615 }
14616
14617 --cp_unevaluated_operand;
14618 --c_inhibit_evaluation_warnings;
14619
14620 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14621 type = lambda_capture_field_type (type,
14622 DECLTYPE_FOR_INIT_CAPTURE (t),
14623 DECLTYPE_FOR_REF_CAPTURE (t));
14624 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14625 type = lambda_proxy_type (type);
14626 else
14627 {
14628 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14629 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14630 && EXPR_P (type))
14631 /* In a template ~id could be either a complement expression
14632 or an unqualified-id naming a destructor; if instantiating
14633 it produces an expression, it's not an id-expression or
14634 member access. */
14635 id = false;
14636 type = finish_decltype_type (type, id, complain);
14637 }
14638 return cp_build_qualified_type_real (type,
14639 cp_type_quals (t)
14640 | cp_type_quals (type),
14641 complain | tf_ignore_bad_quals);
14642 }
14643
14644 case UNDERLYING_TYPE:
14645 {
14646 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14647 complain, in_decl);
14648 return finish_underlying_type (type);
14649 }
14650
14651 case TYPE_ARGUMENT_PACK:
14652 case NONTYPE_ARGUMENT_PACK:
14653 {
14654 tree r;
14655
14656 if (code == NONTYPE_ARGUMENT_PACK)
14657 r = make_node (code);
14658 else
14659 r = cxx_make_type (code);
14660
14661 tree pack_args = ARGUMENT_PACK_ARGS (t);
14662 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14663 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14664
14665 return r;
14666 }
14667
14668 case VOID_CST:
14669 case INTEGER_CST:
14670 case REAL_CST:
14671 case STRING_CST:
14672 case PLUS_EXPR:
14673 case MINUS_EXPR:
14674 case NEGATE_EXPR:
14675 case NOP_EXPR:
14676 case INDIRECT_REF:
14677 case ADDR_EXPR:
14678 case CALL_EXPR:
14679 case ARRAY_REF:
14680 case SCOPE_REF:
14681 /* We should use one of the expression tsubsts for these codes. */
14682 gcc_unreachable ();
14683
14684 default:
14685 sorry ("use of %qs in template", get_tree_code_name (code));
14686 return error_mark_node;
14687 }
14688 }
14689
14690 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14691 expression on the left-hand side of the "." or "->" operator. We
14692 only do the lookup if we had a dependent BASELINK. Otherwise we
14693 adjust it onto the instantiated heirarchy. */
14694
14695 static tree
14696 tsubst_baselink (tree baselink, tree object_type,
14697 tree args, tsubst_flags_t complain, tree in_decl)
14698 {
14699 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14700 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14701 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14702
14703 tree optype = BASELINK_OPTYPE (baselink);
14704 optype = tsubst (optype, args, complain, in_decl);
14705
14706 tree template_args = NULL_TREE;
14707 bool template_id_p = false;
14708 tree fns = BASELINK_FUNCTIONS (baselink);
14709 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14710 {
14711 template_id_p = true;
14712 template_args = TREE_OPERAND (fns, 1);
14713 fns = TREE_OPERAND (fns, 0);
14714 if (template_args)
14715 template_args = tsubst_template_args (template_args, args,
14716 complain, in_decl);
14717 }
14718
14719 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14720 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14721 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14722
14723 if (dependent_p)
14724 {
14725 tree name = OVL_NAME (fns);
14726 if (IDENTIFIER_CONV_OP_P (name))
14727 name = make_conv_op_name (optype);
14728
14729 if (name == complete_dtor_identifier)
14730 /* Treat as-if non-dependent below. */
14731 dependent_p = false;
14732
14733 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14734 if (!baselink)
14735 {
14736 if ((complain & tf_error)
14737 && constructor_name_p (name, qualifying_scope))
14738 error ("cannot call constructor %<%T::%D%> directly",
14739 qualifying_scope, name);
14740 return error_mark_node;
14741 }
14742
14743 if (BASELINK_P (baselink))
14744 fns = BASELINK_FUNCTIONS (baselink);
14745 }
14746 else
14747 /* We're going to overwrite pieces below, make a duplicate. */
14748 baselink = copy_node (baselink);
14749
14750 /* If lookup found a single function, mark it as used at this point.
14751 (If lookup found multiple functions the one selected later by
14752 overload resolution will be marked as used at that point.) */
14753 if (!template_id_p && !really_overloaded_fn (fns))
14754 {
14755 tree fn = OVL_FIRST (fns);
14756 bool ok = mark_used (fn, complain);
14757 if (!ok && !(complain & tf_error))
14758 return error_mark_node;
14759 if (ok && BASELINK_P (baselink))
14760 /* We might have instantiated an auto function. */
14761 TREE_TYPE (baselink) = TREE_TYPE (fn);
14762 }
14763
14764 if (BASELINK_P (baselink))
14765 {
14766 /* Add back the template arguments, if present. */
14767 if (template_id_p)
14768 BASELINK_FUNCTIONS (baselink)
14769 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14770
14771 /* Update the conversion operator type. */
14772 BASELINK_OPTYPE (baselink) = optype;
14773 }
14774
14775 if (!object_type)
14776 object_type = current_class_type;
14777
14778 if (qualified_p || !dependent_p)
14779 {
14780 baselink = adjust_result_of_qualified_name_lookup (baselink,
14781 qualifying_scope,
14782 object_type);
14783 if (!qualified_p)
14784 /* We need to call adjust_result_of_qualified_name_lookup in case the
14785 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14786 so that we still get virtual function binding. */
14787 BASELINK_QUALIFIED_P (baselink) = false;
14788 }
14789
14790 return baselink;
14791 }
14792
14793 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14794 true if the qualified-id will be a postfix-expression in-and-of
14795 itself; false if more of the postfix-expression follows the
14796 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14797 of "&". */
14798
14799 static tree
14800 tsubst_qualified_id (tree qualified_id, tree args,
14801 tsubst_flags_t complain, tree in_decl,
14802 bool done, bool address_p)
14803 {
14804 tree expr;
14805 tree scope;
14806 tree name;
14807 bool is_template;
14808 tree template_args;
14809 location_t loc = UNKNOWN_LOCATION;
14810
14811 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14812
14813 /* Figure out what name to look up. */
14814 name = TREE_OPERAND (qualified_id, 1);
14815 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14816 {
14817 is_template = true;
14818 loc = EXPR_LOCATION (name);
14819 template_args = TREE_OPERAND (name, 1);
14820 if (template_args)
14821 template_args = tsubst_template_args (template_args, args,
14822 complain, in_decl);
14823 if (template_args == error_mark_node)
14824 return error_mark_node;
14825 name = TREE_OPERAND (name, 0);
14826 }
14827 else
14828 {
14829 is_template = false;
14830 template_args = NULL_TREE;
14831 }
14832
14833 /* Substitute into the qualifying scope. When there are no ARGS, we
14834 are just trying to simplify a non-dependent expression. In that
14835 case the qualifying scope may be dependent, and, in any case,
14836 substituting will not help. */
14837 scope = TREE_OPERAND (qualified_id, 0);
14838 if (args)
14839 {
14840 scope = tsubst (scope, args, complain, in_decl);
14841 expr = tsubst_copy (name, args, complain, in_decl);
14842 }
14843 else
14844 expr = name;
14845
14846 if (dependent_scope_p (scope))
14847 {
14848 if (is_template)
14849 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14850 tree r = build_qualified_name (NULL_TREE, scope, expr,
14851 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14852 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14853 return r;
14854 }
14855
14856 if (!BASELINK_P (name) && !DECL_P (expr))
14857 {
14858 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14859 {
14860 /* A BIT_NOT_EXPR is used to represent a destructor. */
14861 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14862 {
14863 error ("qualifying type %qT does not match destructor name ~%qT",
14864 scope, TREE_OPERAND (expr, 0));
14865 expr = error_mark_node;
14866 }
14867 else
14868 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14869 /*is_type_p=*/0, false);
14870 }
14871 else
14872 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14873 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14874 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14875 {
14876 if (complain & tf_error)
14877 {
14878 error ("dependent-name %qE is parsed as a non-type, but "
14879 "instantiation yields a type", qualified_id);
14880 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14881 }
14882 return error_mark_node;
14883 }
14884 }
14885
14886 if (DECL_P (expr))
14887 {
14888 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14889 scope);
14890 /* Remember that there was a reference to this entity. */
14891 if (!mark_used (expr, complain) && !(complain & tf_error))
14892 return error_mark_node;
14893 }
14894
14895 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14896 {
14897 if (complain & tf_error)
14898 qualified_name_lookup_error (scope,
14899 TREE_OPERAND (qualified_id, 1),
14900 expr, input_location);
14901 return error_mark_node;
14902 }
14903
14904 if (is_template)
14905 {
14906 if (variable_template_p (expr))
14907 expr = lookup_and_finish_template_variable (expr, template_args,
14908 complain);
14909 else
14910 expr = lookup_template_function (expr, template_args);
14911 }
14912
14913 if (expr == error_mark_node && complain & tf_error)
14914 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14915 expr, input_location);
14916 else if (TYPE_P (scope))
14917 {
14918 expr = (adjust_result_of_qualified_name_lookup
14919 (expr, scope, current_nonlambda_class_type ()));
14920 expr = (finish_qualified_id_expr
14921 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14922 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14923 /*template_arg_p=*/false, complain));
14924 }
14925
14926 /* Expressions do not generally have reference type. */
14927 if (TREE_CODE (expr) != SCOPE_REF
14928 /* However, if we're about to form a pointer-to-member, we just
14929 want the referenced member referenced. */
14930 && TREE_CODE (expr) != OFFSET_REF)
14931 expr = convert_from_reference (expr);
14932
14933 if (REF_PARENTHESIZED_P (qualified_id))
14934 expr = force_paren_expr (expr);
14935
14936 return expr;
14937 }
14938
14939 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14940 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14941 for tsubst. */
14942
14943 static tree
14944 tsubst_init (tree init, tree decl, tree args,
14945 tsubst_flags_t complain, tree in_decl)
14946 {
14947 if (!init)
14948 return NULL_TREE;
14949
14950 init = tsubst_expr (init, args, complain, in_decl, false);
14951
14952 if (!init && TREE_TYPE (decl) != error_mark_node)
14953 {
14954 /* If we had an initializer but it
14955 instantiated to nothing,
14956 value-initialize the object. This will
14957 only occur when the initializer was a
14958 pack expansion where the parameter packs
14959 used in that expansion were of length
14960 zero. */
14961 init = build_value_init (TREE_TYPE (decl),
14962 complain);
14963 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14964 init = get_target_expr_sfinae (init, complain);
14965 if (TREE_CODE (init) == TARGET_EXPR)
14966 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14967 }
14968
14969 return init;
14970 }
14971
14972 /* Like tsubst, but deals with expressions. This function just replaces
14973 template parms; to finish processing the resultant expression, use
14974 tsubst_copy_and_build or tsubst_expr. */
14975
14976 static tree
14977 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14978 {
14979 enum tree_code code;
14980 tree r;
14981
14982 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14983 return t;
14984
14985 code = TREE_CODE (t);
14986
14987 switch (code)
14988 {
14989 case PARM_DECL:
14990 r = retrieve_local_specialization (t);
14991
14992 if (r == NULL_TREE)
14993 {
14994 /* We get here for a use of 'this' in an NSDMI. */
14995 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14996 return current_class_ptr;
14997
14998 /* This can happen for a parameter name used later in a function
14999 declaration (such as in a late-specified return type). Just
15000 make a dummy decl, since it's only used for its type. */
15001 gcc_assert (cp_unevaluated_operand != 0);
15002 r = tsubst_decl (t, args, complain);
15003 /* Give it the template pattern as its context; its true context
15004 hasn't been instantiated yet and this is good enough for
15005 mangling. */
15006 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15007 }
15008
15009 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15010 r = argument_pack_select_arg (r);
15011 if (!mark_used (r, complain) && !(complain & tf_error))
15012 return error_mark_node;
15013 return r;
15014
15015 case CONST_DECL:
15016 {
15017 tree enum_type;
15018 tree v;
15019
15020 if (DECL_TEMPLATE_PARM_P (t))
15021 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15022 /* There is no need to substitute into namespace-scope
15023 enumerators. */
15024 if (DECL_NAMESPACE_SCOPE_P (t))
15025 return t;
15026 /* If ARGS is NULL, then T is known to be non-dependent. */
15027 if (args == NULL_TREE)
15028 return scalar_constant_value (t);
15029
15030 /* Unfortunately, we cannot just call lookup_name here.
15031 Consider:
15032
15033 template <int I> int f() {
15034 enum E { a = I };
15035 struct S { void g() { E e = a; } };
15036 };
15037
15038 When we instantiate f<7>::S::g(), say, lookup_name is not
15039 clever enough to find f<7>::a. */
15040 enum_type
15041 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15042 /*entering_scope=*/0);
15043
15044 for (v = TYPE_VALUES (enum_type);
15045 v != NULL_TREE;
15046 v = TREE_CHAIN (v))
15047 if (TREE_PURPOSE (v) == DECL_NAME (t))
15048 return TREE_VALUE (v);
15049
15050 /* We didn't find the name. That should never happen; if
15051 name-lookup found it during preliminary parsing, we
15052 should find it again here during instantiation. */
15053 gcc_unreachable ();
15054 }
15055 return t;
15056
15057 case FIELD_DECL:
15058 if (DECL_CONTEXT (t))
15059 {
15060 tree ctx;
15061
15062 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15063 /*entering_scope=*/1);
15064 if (ctx != DECL_CONTEXT (t))
15065 {
15066 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15067 if (!r)
15068 {
15069 if (complain & tf_error)
15070 error ("using invalid field %qD", t);
15071 return error_mark_node;
15072 }
15073 return r;
15074 }
15075 }
15076
15077 return t;
15078
15079 case VAR_DECL:
15080 case FUNCTION_DECL:
15081 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15082 r = tsubst (t, args, complain, in_decl);
15083 else if (local_variable_p (t)
15084 && uses_template_parms (DECL_CONTEXT (t)))
15085 {
15086 r = retrieve_local_specialization (t);
15087 if (r == NULL_TREE)
15088 {
15089 /* First try name lookup to find the instantiation. */
15090 r = lookup_name (DECL_NAME (t));
15091 if (r && !is_capture_proxy (r))
15092 {
15093 /* Make sure that the one we found is the one we want. */
15094 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15095 if (ctx != DECL_CONTEXT (r))
15096 r = NULL_TREE;
15097 }
15098
15099 if (r)
15100 /* OK */;
15101 else
15102 {
15103 /* This can happen for a variable used in a
15104 late-specified return type of a local lambda, or for a
15105 local static or constant. Building a new VAR_DECL
15106 should be OK in all those cases. */
15107 r = tsubst_decl (t, args, complain);
15108 if (local_specializations)
15109 /* Avoid infinite recursion (79640). */
15110 register_local_specialization (r, t);
15111 if (decl_maybe_constant_var_p (r))
15112 {
15113 /* We can't call cp_finish_decl, so handle the
15114 initializer by hand. */
15115 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15116 complain, in_decl);
15117 if (!processing_template_decl)
15118 init = maybe_constant_init (init);
15119 if (processing_template_decl
15120 ? potential_constant_expression (init)
15121 : reduced_constant_expression_p (init))
15122 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15123 = TREE_CONSTANT (r) = true;
15124 DECL_INITIAL (r) = init;
15125 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15126 TREE_TYPE (r)
15127 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15128 complain, adc_variable_type);
15129 }
15130 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15131 || decl_constant_var_p (r)
15132 || errorcount || sorrycount);
15133 if (!processing_template_decl
15134 && !TREE_STATIC (r))
15135 r = process_outer_var_ref (r, complain);
15136 }
15137 /* Remember this for subsequent uses. */
15138 if (local_specializations)
15139 register_local_specialization (r, t);
15140 }
15141 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15142 r = argument_pack_select_arg (r);
15143 }
15144 else
15145 r = t;
15146 if (!mark_used (r, complain))
15147 return error_mark_node;
15148 return r;
15149
15150 case NAMESPACE_DECL:
15151 return t;
15152
15153 case OVERLOAD:
15154 /* An OVERLOAD will always be a non-dependent overload set; an
15155 overload set from function scope will just be represented with an
15156 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15157 gcc_assert (!uses_template_parms (t));
15158 /* We must have marked any lookups as persistent. */
15159 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15160 return t;
15161
15162 case BASELINK:
15163 return tsubst_baselink (t, current_nonlambda_class_type (),
15164 args, complain, in_decl);
15165
15166 case TEMPLATE_DECL:
15167 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15168 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15169 args, complain, in_decl);
15170 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15171 return tsubst (t, args, complain, in_decl);
15172 else if (DECL_CLASS_SCOPE_P (t)
15173 && uses_template_parms (DECL_CONTEXT (t)))
15174 {
15175 /* Template template argument like the following example need
15176 special treatment:
15177
15178 template <template <class> class TT> struct C {};
15179 template <class T> struct D {
15180 template <class U> struct E {};
15181 C<E> c; // #1
15182 };
15183 D<int> d; // #2
15184
15185 We are processing the template argument `E' in #1 for
15186 the template instantiation #2. Originally, `E' is a
15187 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15188 have to substitute this with one having context `D<int>'. */
15189
15190 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15191 if (dependent_scope_p (context))
15192 {
15193 /* When rewriting a constructor into a deduction guide, a
15194 non-dependent name can become dependent, so memtmpl<args>
15195 becomes context::template memtmpl<args>. */
15196 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15197 return build_qualified_name (type, context, DECL_NAME (t),
15198 /*template*/true);
15199 }
15200 return lookup_field (context, DECL_NAME(t), 0, false);
15201 }
15202 else
15203 /* Ordinary template template argument. */
15204 return t;
15205
15206 case NON_LVALUE_EXPR:
15207 case VIEW_CONVERT_EXPR:
15208 {
15209 /* Handle location wrappers by substituting the wrapped node
15210 first, *then* reusing the resulting type. Doing the type
15211 first ensures that we handle template parameters and
15212 parameter pack expansions. */
15213 gcc_assert (location_wrapper_p (t));
15214 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15215 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15216 }
15217
15218 case CAST_EXPR:
15219 case REINTERPRET_CAST_EXPR:
15220 case CONST_CAST_EXPR:
15221 case STATIC_CAST_EXPR:
15222 case DYNAMIC_CAST_EXPR:
15223 case IMPLICIT_CONV_EXPR:
15224 case CONVERT_EXPR:
15225 case NOP_EXPR:
15226 {
15227 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15228 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15229 return build1 (code, type, op0);
15230 }
15231
15232 case SIZEOF_EXPR:
15233 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15234 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15235 {
15236 tree expanded, op = TREE_OPERAND (t, 0);
15237 int len = 0;
15238
15239 if (SIZEOF_EXPR_TYPE_P (t))
15240 op = TREE_TYPE (op);
15241
15242 ++cp_unevaluated_operand;
15243 ++c_inhibit_evaluation_warnings;
15244 /* We only want to compute the number of arguments. */
15245 if (PACK_EXPANSION_P (op))
15246 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15247 else
15248 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15249 args, complain, in_decl);
15250 --cp_unevaluated_operand;
15251 --c_inhibit_evaluation_warnings;
15252
15253 if (TREE_CODE (expanded) == TREE_VEC)
15254 {
15255 len = TREE_VEC_LENGTH (expanded);
15256 /* Set TREE_USED for the benefit of -Wunused. */
15257 for (int i = 0; i < len; i++)
15258 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15259 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15260 }
15261
15262 if (expanded == error_mark_node)
15263 return error_mark_node;
15264 else if (PACK_EXPANSION_P (expanded)
15265 || (TREE_CODE (expanded) == TREE_VEC
15266 && pack_expansion_args_count (expanded)))
15267
15268 {
15269 if (PACK_EXPANSION_P (expanded))
15270 /* OK. */;
15271 else if (TREE_VEC_LENGTH (expanded) == 1)
15272 expanded = TREE_VEC_ELT (expanded, 0);
15273 else
15274 expanded = make_argument_pack (expanded);
15275
15276 if (TYPE_P (expanded))
15277 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15278 complain & tf_error);
15279 else
15280 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15281 complain & tf_error);
15282 }
15283 else
15284 return build_int_cst (size_type_node, len);
15285 }
15286 if (SIZEOF_EXPR_TYPE_P (t))
15287 {
15288 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15289 args, complain, in_decl);
15290 r = build1 (NOP_EXPR, r, error_mark_node);
15291 r = build1 (SIZEOF_EXPR,
15292 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15293 SIZEOF_EXPR_TYPE_P (r) = 1;
15294 return r;
15295 }
15296 /* Fall through */
15297
15298 case INDIRECT_REF:
15299 case NEGATE_EXPR:
15300 case TRUTH_NOT_EXPR:
15301 case BIT_NOT_EXPR:
15302 case ADDR_EXPR:
15303 case UNARY_PLUS_EXPR: /* Unary + */
15304 case ALIGNOF_EXPR:
15305 case AT_ENCODE_EXPR:
15306 case ARROW_EXPR:
15307 case THROW_EXPR:
15308 case TYPEID_EXPR:
15309 case REALPART_EXPR:
15310 case IMAGPART_EXPR:
15311 case PAREN_EXPR:
15312 {
15313 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15314 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15315 return build1 (code, type, op0);
15316 }
15317
15318 case COMPONENT_REF:
15319 {
15320 tree object;
15321 tree name;
15322
15323 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15324 name = TREE_OPERAND (t, 1);
15325 if (TREE_CODE (name) == BIT_NOT_EXPR)
15326 {
15327 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15328 complain, in_decl);
15329 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15330 }
15331 else if (TREE_CODE (name) == SCOPE_REF
15332 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15333 {
15334 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15335 complain, in_decl);
15336 name = TREE_OPERAND (name, 1);
15337 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15338 complain, in_decl);
15339 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15340 name = build_qualified_name (/*type=*/NULL_TREE,
15341 base, name,
15342 /*template_p=*/false);
15343 }
15344 else if (BASELINK_P (name))
15345 name = tsubst_baselink (name,
15346 non_reference (TREE_TYPE (object)),
15347 args, complain,
15348 in_decl);
15349 else
15350 name = tsubst_copy (name, args, complain, in_decl);
15351 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15352 }
15353
15354 case PLUS_EXPR:
15355 case MINUS_EXPR:
15356 case MULT_EXPR:
15357 case TRUNC_DIV_EXPR:
15358 case CEIL_DIV_EXPR:
15359 case FLOOR_DIV_EXPR:
15360 case ROUND_DIV_EXPR:
15361 case EXACT_DIV_EXPR:
15362 case BIT_AND_EXPR:
15363 case BIT_IOR_EXPR:
15364 case BIT_XOR_EXPR:
15365 case TRUNC_MOD_EXPR:
15366 case FLOOR_MOD_EXPR:
15367 case TRUTH_ANDIF_EXPR:
15368 case TRUTH_ORIF_EXPR:
15369 case TRUTH_AND_EXPR:
15370 case TRUTH_OR_EXPR:
15371 case RSHIFT_EXPR:
15372 case LSHIFT_EXPR:
15373 case RROTATE_EXPR:
15374 case LROTATE_EXPR:
15375 case EQ_EXPR:
15376 case NE_EXPR:
15377 case MAX_EXPR:
15378 case MIN_EXPR:
15379 case LE_EXPR:
15380 case GE_EXPR:
15381 case LT_EXPR:
15382 case GT_EXPR:
15383 case COMPOUND_EXPR:
15384 case DOTSTAR_EXPR:
15385 case MEMBER_REF:
15386 case PREDECREMENT_EXPR:
15387 case PREINCREMENT_EXPR:
15388 case POSTDECREMENT_EXPR:
15389 case POSTINCREMENT_EXPR:
15390 {
15391 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15392 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15393 return build_nt (code, op0, op1);
15394 }
15395
15396 case SCOPE_REF:
15397 {
15398 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15399 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15400 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15401 QUALIFIED_NAME_IS_TEMPLATE (t));
15402 }
15403
15404 case ARRAY_REF:
15405 {
15406 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15407 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15408 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15409 }
15410
15411 case CALL_EXPR:
15412 {
15413 int n = VL_EXP_OPERAND_LENGTH (t);
15414 tree result = build_vl_exp (CALL_EXPR, n);
15415 int i;
15416 for (i = 0; i < n; i++)
15417 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15418 complain, in_decl);
15419 return result;
15420 }
15421
15422 case COND_EXPR:
15423 case MODOP_EXPR:
15424 case PSEUDO_DTOR_EXPR:
15425 case VEC_PERM_EXPR:
15426 {
15427 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15428 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15429 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15430 r = build_nt (code, op0, op1, op2);
15431 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15432 return r;
15433 }
15434
15435 case NEW_EXPR:
15436 {
15437 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15438 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15439 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15440 r = build_nt (code, op0, op1, op2);
15441 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15442 return r;
15443 }
15444
15445 case DELETE_EXPR:
15446 {
15447 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15448 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15449 r = build_nt (code, op0, op1);
15450 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15451 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15452 return r;
15453 }
15454
15455 case TEMPLATE_ID_EXPR:
15456 {
15457 /* Substituted template arguments */
15458 tree fn = TREE_OPERAND (t, 0);
15459 tree targs = TREE_OPERAND (t, 1);
15460
15461 fn = tsubst_copy (fn, args, complain, in_decl);
15462 if (targs)
15463 targs = tsubst_template_args (targs, args, complain, in_decl);
15464
15465 return lookup_template_function (fn, targs);
15466 }
15467
15468 case TREE_LIST:
15469 {
15470 tree purpose, value, chain;
15471
15472 if (t == void_list_node)
15473 return t;
15474
15475 purpose = TREE_PURPOSE (t);
15476 if (purpose)
15477 purpose = tsubst_copy (purpose, args, complain, in_decl);
15478 value = TREE_VALUE (t);
15479 if (value)
15480 value = tsubst_copy (value, args, complain, in_decl);
15481 chain = TREE_CHAIN (t);
15482 if (chain && chain != void_type_node)
15483 chain = tsubst_copy (chain, args, complain, in_decl);
15484 if (purpose == TREE_PURPOSE (t)
15485 && value == TREE_VALUE (t)
15486 && chain == TREE_CHAIN (t))
15487 return t;
15488 return tree_cons (purpose, value, chain);
15489 }
15490
15491 case RECORD_TYPE:
15492 case UNION_TYPE:
15493 case ENUMERAL_TYPE:
15494 case INTEGER_TYPE:
15495 case TEMPLATE_TYPE_PARM:
15496 case TEMPLATE_TEMPLATE_PARM:
15497 case BOUND_TEMPLATE_TEMPLATE_PARM:
15498 case TEMPLATE_PARM_INDEX:
15499 case POINTER_TYPE:
15500 case REFERENCE_TYPE:
15501 case OFFSET_TYPE:
15502 case FUNCTION_TYPE:
15503 case METHOD_TYPE:
15504 case ARRAY_TYPE:
15505 case TYPENAME_TYPE:
15506 case UNBOUND_CLASS_TEMPLATE:
15507 case TYPEOF_TYPE:
15508 case DECLTYPE_TYPE:
15509 case TYPE_DECL:
15510 return tsubst (t, args, complain, in_decl);
15511
15512 case USING_DECL:
15513 t = DECL_NAME (t);
15514 /* Fall through. */
15515 case IDENTIFIER_NODE:
15516 if (IDENTIFIER_CONV_OP_P (t))
15517 {
15518 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15519 return make_conv_op_name (new_type);
15520 }
15521 else
15522 return t;
15523
15524 case CONSTRUCTOR:
15525 /* This is handled by tsubst_copy_and_build. */
15526 gcc_unreachable ();
15527
15528 case VA_ARG_EXPR:
15529 {
15530 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15531 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15532 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15533 }
15534
15535 case CLEANUP_POINT_EXPR:
15536 /* We shouldn't have built any of these during initial template
15537 generation. Instead, they should be built during instantiation
15538 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15539 gcc_unreachable ();
15540
15541 case OFFSET_REF:
15542 {
15543 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15544 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15545 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15546 r = build2 (code, type, op0, op1);
15547 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15548 if (!mark_used (TREE_OPERAND (r, 1), complain)
15549 && !(complain & tf_error))
15550 return error_mark_node;
15551 return r;
15552 }
15553
15554 case EXPR_PACK_EXPANSION:
15555 error ("invalid use of pack expansion expression");
15556 return error_mark_node;
15557
15558 case NONTYPE_ARGUMENT_PACK:
15559 error ("use %<...%> to expand argument pack");
15560 return error_mark_node;
15561
15562 case VOID_CST:
15563 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15564 return t;
15565
15566 case INTEGER_CST:
15567 case REAL_CST:
15568 case STRING_CST:
15569 case COMPLEX_CST:
15570 {
15571 /* Instantiate any typedefs in the type. */
15572 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15573 r = fold_convert (type, t);
15574 gcc_assert (TREE_CODE (r) == code);
15575 return r;
15576 }
15577
15578 case PTRMEM_CST:
15579 /* These can sometimes show up in a partial instantiation, but never
15580 involve template parms. */
15581 gcc_assert (!uses_template_parms (t));
15582 return t;
15583
15584 case UNARY_LEFT_FOLD_EXPR:
15585 return tsubst_unary_left_fold (t, args, complain, in_decl);
15586 case UNARY_RIGHT_FOLD_EXPR:
15587 return tsubst_unary_right_fold (t, args, complain, in_decl);
15588 case BINARY_LEFT_FOLD_EXPR:
15589 return tsubst_binary_left_fold (t, args, complain, in_decl);
15590 case BINARY_RIGHT_FOLD_EXPR:
15591 return tsubst_binary_right_fold (t, args, complain, in_decl);
15592 case PREDICT_EXPR:
15593 return t;
15594
15595 case DEBUG_BEGIN_STMT:
15596 /* ??? There's no point in copying it for now, but maybe some
15597 day it will contain more information, such as a pointer back
15598 to the containing function, inlined copy or so. */
15599 return t;
15600
15601 default:
15602 /* We shouldn't get here, but keep going if !flag_checking. */
15603 if (flag_checking)
15604 gcc_unreachable ();
15605 return t;
15606 }
15607 }
15608
15609 /* Helper function for tsubst_omp_clauses, used for instantiation of
15610 OMP_CLAUSE_DECL of clauses. */
15611
15612 static tree
15613 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15614 tree in_decl)
15615 {
15616 if (decl == NULL_TREE)
15617 return NULL_TREE;
15618
15619 /* Handle an OpenMP array section represented as a TREE_LIST (or
15620 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15621 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15622 TREE_LIST. We can handle it exactly the same as an array section
15623 (purpose, value, and a chain), even though the nomenclature
15624 (low_bound, length, etc) is different. */
15625 if (TREE_CODE (decl) == TREE_LIST)
15626 {
15627 tree low_bound
15628 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15629 /*integral_constant_expression_p=*/false);
15630 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15631 /*integral_constant_expression_p=*/false);
15632 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15633 in_decl);
15634 if (TREE_PURPOSE (decl) == low_bound
15635 && TREE_VALUE (decl) == length
15636 && TREE_CHAIN (decl) == chain)
15637 return decl;
15638 tree ret = tree_cons (low_bound, length, chain);
15639 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15640 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15641 return ret;
15642 }
15643 tree ret = tsubst_expr (decl, args, complain, in_decl,
15644 /*integral_constant_expression_p=*/false);
15645 /* Undo convert_from_reference tsubst_expr could have called. */
15646 if (decl
15647 && REFERENCE_REF_P (ret)
15648 && !REFERENCE_REF_P (decl))
15649 ret = TREE_OPERAND (ret, 0);
15650 return ret;
15651 }
15652
15653 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15654
15655 static tree
15656 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15657 tree args, tsubst_flags_t complain, tree in_decl)
15658 {
15659 tree new_clauses = NULL_TREE, nc, oc;
15660 tree linear_no_step = NULL_TREE;
15661
15662 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15663 {
15664 nc = copy_node (oc);
15665 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15666 new_clauses = nc;
15667
15668 switch (OMP_CLAUSE_CODE (nc))
15669 {
15670 case OMP_CLAUSE_LASTPRIVATE:
15671 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15672 {
15673 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15674 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15675 in_decl, /*integral_constant_expression_p=*/false);
15676 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15677 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15678 }
15679 /* FALLTHRU */
15680 case OMP_CLAUSE_PRIVATE:
15681 case OMP_CLAUSE_SHARED:
15682 case OMP_CLAUSE_FIRSTPRIVATE:
15683 case OMP_CLAUSE_COPYIN:
15684 case OMP_CLAUSE_COPYPRIVATE:
15685 case OMP_CLAUSE_UNIFORM:
15686 case OMP_CLAUSE_DEPEND:
15687 case OMP_CLAUSE_FROM:
15688 case OMP_CLAUSE_TO:
15689 case OMP_CLAUSE_MAP:
15690 case OMP_CLAUSE_USE_DEVICE_PTR:
15691 case OMP_CLAUSE_IS_DEVICE_PTR:
15692 OMP_CLAUSE_DECL (nc)
15693 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15694 in_decl);
15695 break;
15696 case OMP_CLAUSE_TILE:
15697 case OMP_CLAUSE_IF:
15698 case OMP_CLAUSE_NUM_THREADS:
15699 case OMP_CLAUSE_SCHEDULE:
15700 case OMP_CLAUSE_COLLAPSE:
15701 case OMP_CLAUSE_FINAL:
15702 case OMP_CLAUSE_DEVICE:
15703 case OMP_CLAUSE_DIST_SCHEDULE:
15704 case OMP_CLAUSE_NUM_TEAMS:
15705 case OMP_CLAUSE_THREAD_LIMIT:
15706 case OMP_CLAUSE_SAFELEN:
15707 case OMP_CLAUSE_SIMDLEN:
15708 case OMP_CLAUSE_NUM_TASKS:
15709 case OMP_CLAUSE_GRAINSIZE:
15710 case OMP_CLAUSE_PRIORITY:
15711 case OMP_CLAUSE_ORDERED:
15712 case OMP_CLAUSE_HINT:
15713 case OMP_CLAUSE_NUM_GANGS:
15714 case OMP_CLAUSE_NUM_WORKERS:
15715 case OMP_CLAUSE_VECTOR_LENGTH:
15716 case OMP_CLAUSE_WORKER:
15717 case OMP_CLAUSE_VECTOR:
15718 case OMP_CLAUSE_ASYNC:
15719 case OMP_CLAUSE_WAIT:
15720 OMP_CLAUSE_OPERAND (nc, 0)
15721 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15722 in_decl, /*integral_constant_expression_p=*/false);
15723 break;
15724 case OMP_CLAUSE_REDUCTION:
15725 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15726 {
15727 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15728 if (TREE_CODE (placeholder) == SCOPE_REF)
15729 {
15730 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15731 complain, in_decl);
15732 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15733 = build_qualified_name (NULL_TREE, scope,
15734 TREE_OPERAND (placeholder, 1),
15735 false);
15736 }
15737 else
15738 gcc_assert (identifier_p (placeholder));
15739 }
15740 OMP_CLAUSE_DECL (nc)
15741 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15742 in_decl);
15743 break;
15744 case OMP_CLAUSE_GANG:
15745 case OMP_CLAUSE_ALIGNED:
15746 OMP_CLAUSE_DECL (nc)
15747 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15748 in_decl);
15749 OMP_CLAUSE_OPERAND (nc, 1)
15750 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15751 in_decl, /*integral_constant_expression_p=*/false);
15752 break;
15753 case OMP_CLAUSE_LINEAR:
15754 OMP_CLAUSE_DECL (nc)
15755 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15756 in_decl);
15757 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15758 {
15759 gcc_assert (!linear_no_step);
15760 linear_no_step = nc;
15761 }
15762 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15763 OMP_CLAUSE_LINEAR_STEP (nc)
15764 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15765 complain, in_decl);
15766 else
15767 OMP_CLAUSE_LINEAR_STEP (nc)
15768 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15769 in_decl,
15770 /*integral_constant_expression_p=*/false);
15771 break;
15772 case OMP_CLAUSE_NOWAIT:
15773 case OMP_CLAUSE_DEFAULT:
15774 case OMP_CLAUSE_UNTIED:
15775 case OMP_CLAUSE_MERGEABLE:
15776 case OMP_CLAUSE_INBRANCH:
15777 case OMP_CLAUSE_NOTINBRANCH:
15778 case OMP_CLAUSE_PROC_BIND:
15779 case OMP_CLAUSE_FOR:
15780 case OMP_CLAUSE_PARALLEL:
15781 case OMP_CLAUSE_SECTIONS:
15782 case OMP_CLAUSE_TASKGROUP:
15783 case OMP_CLAUSE_NOGROUP:
15784 case OMP_CLAUSE_THREADS:
15785 case OMP_CLAUSE_SIMD:
15786 case OMP_CLAUSE_DEFAULTMAP:
15787 case OMP_CLAUSE_INDEPENDENT:
15788 case OMP_CLAUSE_AUTO:
15789 case OMP_CLAUSE_SEQ:
15790 break;
15791 default:
15792 gcc_unreachable ();
15793 }
15794 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15795 switch (OMP_CLAUSE_CODE (nc))
15796 {
15797 case OMP_CLAUSE_SHARED:
15798 case OMP_CLAUSE_PRIVATE:
15799 case OMP_CLAUSE_FIRSTPRIVATE:
15800 case OMP_CLAUSE_LASTPRIVATE:
15801 case OMP_CLAUSE_COPYPRIVATE:
15802 case OMP_CLAUSE_LINEAR:
15803 case OMP_CLAUSE_REDUCTION:
15804 case OMP_CLAUSE_USE_DEVICE_PTR:
15805 case OMP_CLAUSE_IS_DEVICE_PTR:
15806 /* tsubst_expr on SCOPE_REF results in returning
15807 finish_non_static_data_member result. Undo that here. */
15808 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15809 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15810 == IDENTIFIER_NODE))
15811 {
15812 tree t = OMP_CLAUSE_DECL (nc);
15813 tree v = t;
15814 while (v)
15815 switch (TREE_CODE (v))
15816 {
15817 case COMPONENT_REF:
15818 case MEM_REF:
15819 case INDIRECT_REF:
15820 CASE_CONVERT:
15821 case POINTER_PLUS_EXPR:
15822 v = TREE_OPERAND (v, 0);
15823 continue;
15824 case PARM_DECL:
15825 if (DECL_CONTEXT (v) == current_function_decl
15826 && DECL_ARTIFICIAL (v)
15827 && DECL_NAME (v) == this_identifier)
15828 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15829 /* FALLTHRU */
15830 default:
15831 v = NULL_TREE;
15832 break;
15833 }
15834 }
15835 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15836 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15837 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15838 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15839 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15840 {
15841 tree decl = OMP_CLAUSE_DECL (nc);
15842 if (VAR_P (decl))
15843 {
15844 retrofit_lang_decl (decl);
15845 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15846 }
15847 }
15848 break;
15849 default:
15850 break;
15851 }
15852 }
15853
15854 new_clauses = nreverse (new_clauses);
15855 if (ort != C_ORT_OMP_DECLARE_SIMD)
15856 {
15857 new_clauses = finish_omp_clauses (new_clauses, ort);
15858 if (linear_no_step)
15859 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15860 if (nc == linear_no_step)
15861 {
15862 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15863 break;
15864 }
15865 }
15866 return new_clauses;
15867 }
15868
15869 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15870
15871 static tree
15872 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15873 tree in_decl)
15874 {
15875 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15876
15877 tree purpose, value, chain;
15878
15879 if (t == NULL)
15880 return t;
15881
15882 if (TREE_CODE (t) != TREE_LIST)
15883 return tsubst_copy_and_build (t, args, complain, in_decl,
15884 /*function_p=*/false,
15885 /*integral_constant_expression_p=*/false);
15886
15887 if (t == void_list_node)
15888 return t;
15889
15890 purpose = TREE_PURPOSE (t);
15891 if (purpose)
15892 purpose = RECUR (purpose);
15893 value = TREE_VALUE (t);
15894 if (value)
15895 {
15896 if (TREE_CODE (value) != LABEL_DECL)
15897 value = RECUR (value);
15898 else
15899 {
15900 value = lookup_label (DECL_NAME (value));
15901 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15902 TREE_USED (value) = 1;
15903 }
15904 }
15905 chain = TREE_CHAIN (t);
15906 if (chain && chain != void_type_node)
15907 chain = RECUR (chain);
15908 return tree_cons (purpose, value, chain);
15909 #undef RECUR
15910 }
15911
15912 /* Used to temporarily communicate the list of #pragma omp parallel
15913 clauses to #pragma omp for instantiation if they are combined
15914 together. */
15915
15916 static tree *omp_parallel_combined_clauses;
15917
15918 /* Substitute one OMP_FOR iterator. */
15919
15920 static void
15921 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15922 tree initv, tree condv, tree incrv, tree *clauses,
15923 tree args, tsubst_flags_t complain, tree in_decl,
15924 bool integral_constant_expression_p)
15925 {
15926 #define RECUR(NODE) \
15927 tsubst_expr ((NODE), args, complain, in_decl, \
15928 integral_constant_expression_p)
15929 tree decl, init, cond, incr;
15930
15931 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15932 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15933
15934 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15935 {
15936 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15937 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15938 }
15939
15940 decl = TREE_OPERAND (init, 0);
15941 init = TREE_OPERAND (init, 1);
15942 tree decl_expr = NULL_TREE;
15943 if (init && TREE_CODE (init) == DECL_EXPR)
15944 {
15945 /* We need to jump through some hoops to handle declarations in the
15946 init-statement, since we might need to handle auto deduction,
15947 but we need to keep control of initialization. */
15948 decl_expr = init;
15949 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15950 decl = tsubst_decl (decl, args, complain);
15951 }
15952 else
15953 {
15954 if (TREE_CODE (decl) == SCOPE_REF)
15955 {
15956 decl = RECUR (decl);
15957 if (TREE_CODE (decl) == COMPONENT_REF)
15958 {
15959 tree v = decl;
15960 while (v)
15961 switch (TREE_CODE (v))
15962 {
15963 case COMPONENT_REF:
15964 case MEM_REF:
15965 case INDIRECT_REF:
15966 CASE_CONVERT:
15967 case POINTER_PLUS_EXPR:
15968 v = TREE_OPERAND (v, 0);
15969 continue;
15970 case PARM_DECL:
15971 if (DECL_CONTEXT (v) == current_function_decl
15972 && DECL_ARTIFICIAL (v)
15973 && DECL_NAME (v) == this_identifier)
15974 {
15975 decl = TREE_OPERAND (decl, 1);
15976 decl = omp_privatize_field (decl, false);
15977 }
15978 /* FALLTHRU */
15979 default:
15980 v = NULL_TREE;
15981 break;
15982 }
15983 }
15984 }
15985 else
15986 decl = RECUR (decl);
15987 }
15988 init = RECUR (init);
15989
15990 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15991 if (auto_node && init)
15992 TREE_TYPE (decl)
15993 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
15994
15995 gcc_assert (!type_dependent_expression_p (decl));
15996
15997 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15998 {
15999 if (decl_expr)
16000 {
16001 /* Declare the variable, but don't let that initialize it. */
16002 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16003 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16004 RECUR (decl_expr);
16005 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16006 }
16007
16008 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16009 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16010 if (TREE_CODE (incr) == MODIFY_EXPR)
16011 {
16012 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16013 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16014 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16015 NOP_EXPR, rhs, complain);
16016 }
16017 else
16018 incr = RECUR (incr);
16019 TREE_VEC_ELT (declv, i) = decl;
16020 TREE_VEC_ELT (initv, i) = init;
16021 TREE_VEC_ELT (condv, i) = cond;
16022 TREE_VEC_ELT (incrv, i) = incr;
16023 return;
16024 }
16025
16026 if (decl_expr)
16027 {
16028 /* Declare and initialize the variable. */
16029 RECUR (decl_expr);
16030 init = NULL_TREE;
16031 }
16032 else if (init)
16033 {
16034 tree *pc;
16035 int j;
16036 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16037 {
16038 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16039 {
16040 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16041 && OMP_CLAUSE_DECL (*pc) == decl)
16042 break;
16043 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16044 && OMP_CLAUSE_DECL (*pc) == decl)
16045 {
16046 if (j)
16047 break;
16048 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16049 tree c = *pc;
16050 *pc = OMP_CLAUSE_CHAIN (c);
16051 OMP_CLAUSE_CHAIN (c) = *clauses;
16052 *clauses = c;
16053 }
16054 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16055 && OMP_CLAUSE_DECL (*pc) == decl)
16056 {
16057 error ("iteration variable %qD should not be firstprivate",
16058 decl);
16059 *pc = OMP_CLAUSE_CHAIN (*pc);
16060 }
16061 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16062 && OMP_CLAUSE_DECL (*pc) == decl)
16063 {
16064 error ("iteration variable %qD should not be reduction",
16065 decl);
16066 *pc = OMP_CLAUSE_CHAIN (*pc);
16067 }
16068 else
16069 pc = &OMP_CLAUSE_CHAIN (*pc);
16070 }
16071 if (*pc)
16072 break;
16073 }
16074 if (*pc == NULL_TREE)
16075 {
16076 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16077 OMP_CLAUSE_DECL (c) = decl;
16078 c = finish_omp_clauses (c, C_ORT_OMP);
16079 if (c)
16080 {
16081 OMP_CLAUSE_CHAIN (c) = *clauses;
16082 *clauses = c;
16083 }
16084 }
16085 }
16086 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16087 if (COMPARISON_CLASS_P (cond))
16088 {
16089 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16090 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16091 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16092 }
16093 else
16094 cond = RECUR (cond);
16095 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16096 switch (TREE_CODE (incr))
16097 {
16098 case PREINCREMENT_EXPR:
16099 case PREDECREMENT_EXPR:
16100 case POSTINCREMENT_EXPR:
16101 case POSTDECREMENT_EXPR:
16102 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16103 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16104 break;
16105 case MODIFY_EXPR:
16106 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16107 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16108 {
16109 tree rhs = TREE_OPERAND (incr, 1);
16110 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16111 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16112 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16113 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16114 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16115 rhs0, rhs1));
16116 }
16117 else
16118 incr = RECUR (incr);
16119 break;
16120 case MODOP_EXPR:
16121 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16122 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16123 {
16124 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16125 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16126 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16127 TREE_TYPE (decl), lhs,
16128 RECUR (TREE_OPERAND (incr, 2))));
16129 }
16130 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16131 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16132 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16133 {
16134 tree rhs = TREE_OPERAND (incr, 2);
16135 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16136 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16137 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16138 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16139 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16140 rhs0, rhs1));
16141 }
16142 else
16143 incr = RECUR (incr);
16144 break;
16145 default:
16146 incr = RECUR (incr);
16147 break;
16148 }
16149
16150 TREE_VEC_ELT (declv, i) = decl;
16151 TREE_VEC_ELT (initv, i) = init;
16152 TREE_VEC_ELT (condv, i) = cond;
16153 TREE_VEC_ELT (incrv, i) = incr;
16154 #undef RECUR
16155 }
16156
16157 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16158 of OMP_TARGET's body. */
16159
16160 static tree
16161 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16162 {
16163 *walk_subtrees = 0;
16164 switch (TREE_CODE (*tp))
16165 {
16166 case OMP_TEAMS:
16167 return *tp;
16168 case BIND_EXPR:
16169 case STATEMENT_LIST:
16170 *walk_subtrees = 1;
16171 break;
16172 default:
16173 break;
16174 }
16175 return NULL_TREE;
16176 }
16177
16178 /* Helper function for tsubst_expr. For decomposition declaration
16179 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16180 also the corresponding decls representing the identifiers
16181 of the decomposition declaration. Return DECL if successful
16182 or error_mark_node otherwise, set *FIRST to the first decl
16183 in the list chained through DECL_CHAIN and *CNT to the number
16184 of such decls. */
16185
16186 static tree
16187 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16188 tsubst_flags_t complain, tree in_decl, tree *first,
16189 unsigned int *cnt)
16190 {
16191 tree decl2, decl3, prev = decl;
16192 *cnt = 0;
16193 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16194 for (decl2 = DECL_CHAIN (pattern_decl);
16195 decl2
16196 && VAR_P (decl2)
16197 && DECL_DECOMPOSITION_P (decl2)
16198 && DECL_NAME (decl2);
16199 decl2 = DECL_CHAIN (decl2))
16200 {
16201 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16202 {
16203 gcc_assert (errorcount);
16204 return error_mark_node;
16205 }
16206 (*cnt)++;
16207 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16208 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16209 tree v = DECL_VALUE_EXPR (decl2);
16210 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16211 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16212 decl3 = tsubst (decl2, args, complain, in_decl);
16213 SET_DECL_VALUE_EXPR (decl2, v);
16214 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16215 if (VAR_P (decl3))
16216 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16217 maybe_push_decl (decl3);
16218 if (error_operand_p (decl3))
16219 decl = error_mark_node;
16220 else if (decl != error_mark_node
16221 && DECL_CHAIN (decl3) != prev)
16222 {
16223 gcc_assert (errorcount);
16224 decl = error_mark_node;
16225 }
16226 else
16227 prev = decl3;
16228 }
16229 *first = prev;
16230 return decl;
16231 }
16232
16233 /* Like tsubst_copy for expressions, etc. but also does semantic
16234 processing. */
16235
16236 tree
16237 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16238 bool integral_constant_expression_p)
16239 {
16240 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16241 #define RECUR(NODE) \
16242 tsubst_expr ((NODE), args, complain, in_decl, \
16243 integral_constant_expression_p)
16244
16245 tree stmt, tmp;
16246 tree r;
16247 location_t loc;
16248
16249 if (t == NULL_TREE || t == error_mark_node)
16250 return t;
16251
16252 loc = input_location;
16253 if (EXPR_HAS_LOCATION (t))
16254 input_location = EXPR_LOCATION (t);
16255 if (STATEMENT_CODE_P (TREE_CODE (t)))
16256 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16257
16258 switch (TREE_CODE (t))
16259 {
16260 case STATEMENT_LIST:
16261 {
16262 tree_stmt_iterator i;
16263 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16264 RECUR (tsi_stmt (i));
16265 break;
16266 }
16267
16268 case CTOR_INITIALIZER:
16269 finish_mem_initializers (tsubst_initializer_list
16270 (TREE_OPERAND (t, 0), args));
16271 break;
16272
16273 case RETURN_EXPR:
16274 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16275 break;
16276
16277 case EXPR_STMT:
16278 tmp = RECUR (EXPR_STMT_EXPR (t));
16279 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16280 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16281 else
16282 finish_expr_stmt (tmp);
16283 break;
16284
16285 case USING_STMT:
16286 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16287 /*attribs=*/NULL_TREE);
16288 break;
16289
16290 case DECL_EXPR:
16291 {
16292 tree decl, pattern_decl;
16293 tree init;
16294
16295 pattern_decl = decl = DECL_EXPR_DECL (t);
16296 if (TREE_CODE (decl) == LABEL_DECL)
16297 finish_label_decl (DECL_NAME (decl));
16298 else if (TREE_CODE (decl) == USING_DECL)
16299 {
16300 tree scope = USING_DECL_SCOPE (decl);
16301 tree name = DECL_NAME (decl);
16302
16303 scope = tsubst (scope, args, complain, in_decl);
16304 decl = lookup_qualified_name (scope, name,
16305 /*is_type_p=*/false,
16306 /*complain=*/false);
16307 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16308 qualified_name_lookup_error (scope, name, decl, input_location);
16309 else
16310 finish_local_using_decl (decl, scope, name);
16311 }
16312 else if (is_capture_proxy (decl)
16313 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16314 {
16315 /* We're in tsubst_lambda_expr, we've already inserted a new
16316 capture proxy, so look it up and register it. */
16317 tree inst;
16318 if (DECL_PACK_P (decl))
16319 {
16320 inst = (retrieve_local_specialization
16321 (DECL_CAPTURED_VARIABLE (decl)));
16322 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16323 }
16324 else
16325 {
16326 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16327 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16328 gcc_assert (inst != decl && is_capture_proxy (inst));
16329 }
16330 register_local_specialization (inst, decl);
16331 break;
16332 }
16333 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16334 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16335 /* Don't copy the old closure; we'll create a new one in
16336 tsubst_lambda_expr. */
16337 break;
16338 else
16339 {
16340 init = DECL_INITIAL (decl);
16341 decl = tsubst (decl, args, complain, in_decl);
16342 if (decl != error_mark_node)
16343 {
16344 /* By marking the declaration as instantiated, we avoid
16345 trying to instantiate it. Since instantiate_decl can't
16346 handle local variables, and since we've already done
16347 all that needs to be done, that's the right thing to
16348 do. */
16349 if (VAR_P (decl))
16350 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16351 if (VAR_P (decl)
16352 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16353 /* Anonymous aggregates are a special case. */
16354 finish_anon_union (decl);
16355 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16356 {
16357 DECL_CONTEXT (decl) = current_function_decl;
16358 if (DECL_NAME (decl) == this_identifier)
16359 {
16360 tree lam = DECL_CONTEXT (current_function_decl);
16361 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16362 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16363 }
16364 insert_capture_proxy (decl);
16365 }
16366 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16367 /* We already did a pushtag. */;
16368 else if (TREE_CODE (decl) == FUNCTION_DECL
16369 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16370 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16371 {
16372 DECL_CONTEXT (decl) = NULL_TREE;
16373 pushdecl (decl);
16374 DECL_CONTEXT (decl) = current_function_decl;
16375 cp_check_omp_declare_reduction (decl);
16376 }
16377 else
16378 {
16379 int const_init = false;
16380 maybe_push_decl (decl);
16381 if (VAR_P (decl)
16382 && DECL_PRETTY_FUNCTION_P (decl))
16383 {
16384 /* For __PRETTY_FUNCTION__ we have to adjust the
16385 initializer. */
16386 const char *const name
16387 = cxx_printable_name (current_function_decl, 2);
16388 init = cp_fname_init (name, &TREE_TYPE (decl));
16389 }
16390 else
16391 init = tsubst_init (init, decl, args, complain, in_decl);
16392
16393 if (VAR_P (decl))
16394 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16395 (pattern_decl));
16396 if (VAR_P (decl)
16397 && DECL_DECOMPOSITION_P (decl)
16398 && TREE_TYPE (pattern_decl) != error_mark_node)
16399 {
16400 unsigned int cnt;
16401 tree first;
16402 tree ndecl
16403 = tsubst_decomp_names (decl, pattern_decl, args,
16404 complain, in_decl, &first, &cnt);
16405 if (ndecl != error_mark_node)
16406 cp_maybe_mangle_decomp (ndecl, first, cnt);
16407 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16408 if (ndecl != error_mark_node)
16409 cp_finish_decomp (ndecl, first, cnt);
16410 }
16411 else
16412 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16413 }
16414 }
16415 }
16416
16417 break;
16418 }
16419
16420 case FOR_STMT:
16421 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16422 RECUR (FOR_INIT_STMT (t));
16423 finish_init_stmt (stmt);
16424 tmp = RECUR (FOR_COND (t));
16425 finish_for_cond (tmp, stmt, false, 0);
16426 tmp = RECUR (FOR_EXPR (t));
16427 finish_for_expr (tmp, stmt);
16428 {
16429 bool prev = note_iteration_stmt_body_start ();
16430 RECUR (FOR_BODY (t));
16431 note_iteration_stmt_body_end (prev);
16432 }
16433 finish_for_stmt (stmt);
16434 break;
16435
16436 case RANGE_FOR_STMT:
16437 {
16438 /* Construct another range_for, if this is not a final
16439 substitution (for inside inside a generic lambda of a
16440 template). Otherwise convert to a regular for. */
16441 tree decl, expr;
16442 stmt = (processing_template_decl
16443 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16444 : begin_for_stmt (NULL_TREE, NULL_TREE));
16445 decl = RANGE_FOR_DECL (t);
16446 decl = tsubst (decl, args, complain, in_decl);
16447 maybe_push_decl (decl);
16448 expr = RECUR (RANGE_FOR_EXPR (t));
16449
16450 tree decomp_first = NULL_TREE;
16451 unsigned decomp_cnt = 0;
16452 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16453 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16454 complain, in_decl,
16455 &decomp_first, &decomp_cnt);
16456
16457 if (processing_template_decl)
16458 {
16459 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16460 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16461 finish_range_for_decl (stmt, decl, expr);
16462 }
16463 else
16464 {
16465 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16466 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16467 stmt = cp_convert_range_for (stmt, decl, expr,
16468 decomp_first, decomp_cnt,
16469 RANGE_FOR_IVDEP (t), unroll);
16470 }
16471
16472 bool prev = note_iteration_stmt_body_start ();
16473 RECUR (RANGE_FOR_BODY (t));
16474 note_iteration_stmt_body_end (prev);
16475 finish_for_stmt (stmt);
16476 }
16477 break;
16478
16479 case WHILE_STMT:
16480 stmt = begin_while_stmt ();
16481 tmp = RECUR (WHILE_COND (t));
16482 finish_while_stmt_cond (tmp, stmt, false, 0);
16483 {
16484 bool prev = note_iteration_stmt_body_start ();
16485 RECUR (WHILE_BODY (t));
16486 note_iteration_stmt_body_end (prev);
16487 }
16488 finish_while_stmt (stmt);
16489 break;
16490
16491 case DO_STMT:
16492 stmt = begin_do_stmt ();
16493 {
16494 bool prev = note_iteration_stmt_body_start ();
16495 RECUR (DO_BODY (t));
16496 note_iteration_stmt_body_end (prev);
16497 }
16498 finish_do_body (stmt);
16499 tmp = RECUR (DO_COND (t));
16500 finish_do_stmt (tmp, stmt, false, 0);
16501 break;
16502
16503 case IF_STMT:
16504 stmt = begin_if_stmt ();
16505 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16506 if (IF_STMT_CONSTEXPR_P (t))
16507 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
16508 tmp = RECUR (IF_COND (t));
16509 tmp = finish_if_stmt_cond (tmp, stmt);
16510 if (IF_STMT_CONSTEXPR_P (t)
16511 && instantiation_dependent_expression_p (tmp))
16512 {
16513 /* We're partially instantiating a generic lambda, but the condition
16514 of the constexpr if is still dependent. Don't substitute into the
16515 branches now, just remember the template arguments. */
16516 do_poplevel (IF_SCOPE (stmt));
16517 IF_COND (stmt) = IF_COND (t);
16518 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
16519 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
16520 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
16521 add_stmt (stmt);
16522 break;
16523 }
16524 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16525 /* Don't instantiate the THEN_CLAUSE. */;
16526 else
16527 {
16528 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16529 if (inhibit)
16530 ++c_inhibit_evaluation_warnings;
16531 RECUR (THEN_CLAUSE (t));
16532 if (inhibit)
16533 --c_inhibit_evaluation_warnings;
16534 }
16535 finish_then_clause (stmt);
16536
16537 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16538 /* Don't instantiate the ELSE_CLAUSE. */;
16539 else if (ELSE_CLAUSE (t))
16540 {
16541 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16542 begin_else_clause (stmt);
16543 if (inhibit)
16544 ++c_inhibit_evaluation_warnings;
16545 RECUR (ELSE_CLAUSE (t));
16546 if (inhibit)
16547 --c_inhibit_evaluation_warnings;
16548 finish_else_clause (stmt);
16549 }
16550
16551 finish_if_stmt (stmt);
16552 break;
16553
16554 case BIND_EXPR:
16555 if (BIND_EXPR_BODY_BLOCK (t))
16556 stmt = begin_function_body ();
16557 else
16558 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16559 ? BCS_TRY_BLOCK : 0);
16560
16561 RECUR (BIND_EXPR_BODY (t));
16562
16563 if (BIND_EXPR_BODY_BLOCK (t))
16564 finish_function_body (stmt);
16565 else
16566 finish_compound_stmt (stmt);
16567 break;
16568
16569 case BREAK_STMT:
16570 finish_break_stmt ();
16571 break;
16572
16573 case CONTINUE_STMT:
16574 finish_continue_stmt ();
16575 break;
16576
16577 case SWITCH_STMT:
16578 stmt = begin_switch_stmt ();
16579 tmp = RECUR (SWITCH_STMT_COND (t));
16580 finish_switch_cond (tmp, stmt);
16581 RECUR (SWITCH_STMT_BODY (t));
16582 finish_switch_stmt (stmt);
16583 break;
16584
16585 case CASE_LABEL_EXPR:
16586 {
16587 tree low = RECUR (CASE_LOW (t));
16588 tree high = RECUR (CASE_HIGH (t));
16589 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16590 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16591 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16592 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16593 }
16594 break;
16595
16596 case LABEL_EXPR:
16597 {
16598 tree decl = LABEL_EXPR_LABEL (t);
16599 tree label;
16600
16601 label = finish_label_stmt (DECL_NAME (decl));
16602 if (TREE_CODE (label) == LABEL_DECL)
16603 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16604 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16605 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16606 }
16607 break;
16608
16609 case GOTO_EXPR:
16610 tmp = GOTO_DESTINATION (t);
16611 if (TREE_CODE (tmp) != LABEL_DECL)
16612 /* Computed goto's must be tsubst'd into. On the other hand,
16613 non-computed gotos must not be; the identifier in question
16614 will have no binding. */
16615 tmp = RECUR (tmp);
16616 else
16617 tmp = DECL_NAME (tmp);
16618 finish_goto_stmt (tmp);
16619 break;
16620
16621 case ASM_EXPR:
16622 {
16623 tree string = RECUR (ASM_STRING (t));
16624 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16625 complain, in_decl);
16626 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16627 complain, in_decl);
16628 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16629 complain, in_decl);
16630 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16631 complain, in_decl);
16632 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16633 clobbers, labels);
16634 tree asm_expr = tmp;
16635 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16636 asm_expr = TREE_OPERAND (asm_expr, 0);
16637 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16638 }
16639 break;
16640
16641 case TRY_BLOCK:
16642 if (CLEANUP_P (t))
16643 {
16644 stmt = begin_try_block ();
16645 RECUR (TRY_STMTS (t));
16646 finish_cleanup_try_block (stmt);
16647 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16648 }
16649 else
16650 {
16651 tree compound_stmt = NULL_TREE;
16652
16653 if (FN_TRY_BLOCK_P (t))
16654 stmt = begin_function_try_block (&compound_stmt);
16655 else
16656 stmt = begin_try_block ();
16657
16658 RECUR (TRY_STMTS (t));
16659
16660 if (FN_TRY_BLOCK_P (t))
16661 finish_function_try_block (stmt);
16662 else
16663 finish_try_block (stmt);
16664
16665 RECUR (TRY_HANDLERS (t));
16666 if (FN_TRY_BLOCK_P (t))
16667 finish_function_handler_sequence (stmt, compound_stmt);
16668 else
16669 finish_handler_sequence (stmt);
16670 }
16671 break;
16672
16673 case HANDLER:
16674 {
16675 tree decl = HANDLER_PARMS (t);
16676
16677 if (decl)
16678 {
16679 decl = tsubst (decl, args, complain, in_decl);
16680 /* Prevent instantiate_decl from trying to instantiate
16681 this variable. We've already done all that needs to be
16682 done. */
16683 if (decl != error_mark_node)
16684 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16685 }
16686 stmt = begin_handler ();
16687 finish_handler_parms (decl, stmt);
16688 RECUR (HANDLER_BODY (t));
16689 finish_handler (stmt);
16690 }
16691 break;
16692
16693 case TAG_DEFN:
16694 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16695 if (CLASS_TYPE_P (tmp))
16696 {
16697 /* Local classes are not independent templates; they are
16698 instantiated along with their containing function. And this
16699 way we don't have to deal with pushing out of one local class
16700 to instantiate a member of another local class. */
16701 /* Closures are handled by the LAMBDA_EXPR. */
16702 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16703 complete_type (tmp);
16704 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16705 if ((VAR_P (fld)
16706 || (TREE_CODE (fld) == FUNCTION_DECL
16707 && !DECL_ARTIFICIAL (fld)))
16708 && DECL_TEMPLATE_INSTANTIATION (fld))
16709 instantiate_decl (fld, /*defer_ok=*/false,
16710 /*expl_inst_class=*/false);
16711 }
16712 break;
16713
16714 case STATIC_ASSERT:
16715 {
16716 tree condition;
16717
16718 ++c_inhibit_evaluation_warnings;
16719 condition =
16720 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16721 args,
16722 complain, in_decl,
16723 /*integral_constant_expression_p=*/true);
16724 --c_inhibit_evaluation_warnings;
16725
16726 finish_static_assert (condition,
16727 STATIC_ASSERT_MESSAGE (t),
16728 STATIC_ASSERT_SOURCE_LOCATION (t),
16729 /*member_p=*/false);
16730 }
16731 break;
16732
16733 case OACC_KERNELS:
16734 case OACC_PARALLEL:
16735 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16736 in_decl);
16737 stmt = begin_omp_parallel ();
16738 RECUR (OMP_BODY (t));
16739 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16740 break;
16741
16742 case OMP_PARALLEL:
16743 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16744 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16745 complain, in_decl);
16746 if (OMP_PARALLEL_COMBINED (t))
16747 omp_parallel_combined_clauses = &tmp;
16748 stmt = begin_omp_parallel ();
16749 RECUR (OMP_PARALLEL_BODY (t));
16750 gcc_assert (omp_parallel_combined_clauses == NULL);
16751 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16752 = OMP_PARALLEL_COMBINED (t);
16753 pop_omp_privatization_clauses (r);
16754 break;
16755
16756 case OMP_TASK:
16757 r = push_omp_privatization_clauses (false);
16758 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16759 complain, in_decl);
16760 stmt = begin_omp_task ();
16761 RECUR (OMP_TASK_BODY (t));
16762 finish_omp_task (tmp, stmt);
16763 pop_omp_privatization_clauses (r);
16764 break;
16765
16766 case OMP_FOR:
16767 case OMP_SIMD:
16768 case OMP_DISTRIBUTE:
16769 case OMP_TASKLOOP:
16770 case OACC_LOOP:
16771 {
16772 tree clauses, body, pre_body;
16773 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16774 tree orig_declv = NULL_TREE;
16775 tree incrv = NULL_TREE;
16776 enum c_omp_region_type ort = C_ORT_OMP;
16777 int i;
16778
16779 if (TREE_CODE (t) == OACC_LOOP)
16780 ort = C_ORT_ACC;
16781
16782 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16783 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16784 in_decl);
16785 if (OMP_FOR_INIT (t) != NULL_TREE)
16786 {
16787 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16788 if (OMP_FOR_ORIG_DECLS (t))
16789 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16790 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16791 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16792 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16793 }
16794
16795 stmt = begin_omp_structured_block ();
16796
16797 pre_body = push_stmt_list ();
16798 RECUR (OMP_FOR_PRE_BODY (t));
16799 pre_body = pop_stmt_list (pre_body);
16800
16801 if (OMP_FOR_INIT (t) != NULL_TREE)
16802 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16803 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16804 incrv, &clauses, args, complain, in_decl,
16805 integral_constant_expression_p);
16806 omp_parallel_combined_clauses = NULL;
16807
16808 body = push_stmt_list ();
16809 RECUR (OMP_FOR_BODY (t));
16810 body = pop_stmt_list (body);
16811
16812 if (OMP_FOR_INIT (t) != NULL_TREE)
16813 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16814 orig_declv, initv, condv, incrv, body, pre_body,
16815 NULL, clauses);
16816 else
16817 {
16818 t = make_node (TREE_CODE (t));
16819 TREE_TYPE (t) = void_type_node;
16820 OMP_FOR_BODY (t) = body;
16821 OMP_FOR_PRE_BODY (t) = pre_body;
16822 OMP_FOR_CLAUSES (t) = clauses;
16823 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16824 add_stmt (t);
16825 }
16826
16827 add_stmt (finish_omp_structured_block (stmt));
16828 pop_omp_privatization_clauses (r);
16829 }
16830 break;
16831
16832 case OMP_SECTIONS:
16833 omp_parallel_combined_clauses = NULL;
16834 /* FALLTHRU */
16835 case OMP_SINGLE:
16836 case OMP_TEAMS:
16837 case OMP_CRITICAL:
16838 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16839 && OMP_TEAMS_COMBINED (t));
16840 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16841 in_decl);
16842 stmt = push_stmt_list ();
16843 RECUR (OMP_BODY (t));
16844 stmt = pop_stmt_list (stmt);
16845
16846 t = copy_node (t);
16847 OMP_BODY (t) = stmt;
16848 OMP_CLAUSES (t) = tmp;
16849 add_stmt (t);
16850 pop_omp_privatization_clauses (r);
16851 break;
16852
16853 case OACC_DATA:
16854 case OMP_TARGET_DATA:
16855 case OMP_TARGET:
16856 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16857 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16858 in_decl);
16859 keep_next_level (true);
16860 stmt = begin_omp_structured_block ();
16861
16862 RECUR (OMP_BODY (t));
16863 stmt = finish_omp_structured_block (stmt);
16864
16865 t = copy_node (t);
16866 OMP_BODY (t) = stmt;
16867 OMP_CLAUSES (t) = tmp;
16868 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16869 {
16870 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16871 if (teams)
16872 {
16873 /* For combined target teams, ensure the num_teams and
16874 thread_limit clause expressions are evaluated on the host,
16875 before entering the target construct. */
16876 tree c;
16877 for (c = OMP_TEAMS_CLAUSES (teams);
16878 c; c = OMP_CLAUSE_CHAIN (c))
16879 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16880 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16881 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16882 {
16883 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16884 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16885 if (expr == error_mark_node)
16886 continue;
16887 tmp = TARGET_EXPR_SLOT (expr);
16888 add_stmt (expr);
16889 OMP_CLAUSE_OPERAND (c, 0) = expr;
16890 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16891 OMP_CLAUSE_FIRSTPRIVATE);
16892 OMP_CLAUSE_DECL (tc) = tmp;
16893 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16894 OMP_TARGET_CLAUSES (t) = tc;
16895 }
16896 }
16897 }
16898 add_stmt (t);
16899 break;
16900
16901 case OACC_DECLARE:
16902 t = copy_node (t);
16903 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16904 complain, in_decl);
16905 OACC_DECLARE_CLAUSES (t) = tmp;
16906 add_stmt (t);
16907 break;
16908
16909 case OMP_TARGET_UPDATE:
16910 case OMP_TARGET_ENTER_DATA:
16911 case OMP_TARGET_EXIT_DATA:
16912 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16913 complain, in_decl);
16914 t = copy_node (t);
16915 OMP_STANDALONE_CLAUSES (t) = tmp;
16916 add_stmt (t);
16917 break;
16918
16919 case OACC_ENTER_DATA:
16920 case OACC_EXIT_DATA:
16921 case OACC_UPDATE:
16922 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16923 complain, in_decl);
16924 t = copy_node (t);
16925 OMP_STANDALONE_CLAUSES (t) = tmp;
16926 add_stmt (t);
16927 break;
16928
16929 case OMP_ORDERED:
16930 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16931 complain, in_decl);
16932 stmt = push_stmt_list ();
16933 RECUR (OMP_BODY (t));
16934 stmt = pop_stmt_list (stmt);
16935
16936 t = copy_node (t);
16937 OMP_BODY (t) = stmt;
16938 OMP_ORDERED_CLAUSES (t) = tmp;
16939 add_stmt (t);
16940 break;
16941
16942 case OMP_SECTION:
16943 case OMP_MASTER:
16944 case OMP_TASKGROUP:
16945 stmt = push_stmt_list ();
16946 RECUR (OMP_BODY (t));
16947 stmt = pop_stmt_list (stmt);
16948
16949 t = copy_node (t);
16950 OMP_BODY (t) = stmt;
16951 add_stmt (t);
16952 break;
16953
16954 case OMP_ATOMIC:
16955 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16956 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16957 {
16958 tree op1 = TREE_OPERAND (t, 1);
16959 tree rhs1 = NULL_TREE;
16960 tree lhs, rhs;
16961 if (TREE_CODE (op1) == COMPOUND_EXPR)
16962 {
16963 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16964 op1 = TREE_OPERAND (op1, 1);
16965 }
16966 lhs = RECUR (TREE_OPERAND (op1, 0));
16967 rhs = RECUR (TREE_OPERAND (op1, 1));
16968 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16969 NULL_TREE, NULL_TREE, rhs1,
16970 OMP_ATOMIC_SEQ_CST (t));
16971 }
16972 else
16973 {
16974 tree op1 = TREE_OPERAND (t, 1);
16975 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16976 tree rhs1 = NULL_TREE;
16977 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16978 enum tree_code opcode = NOP_EXPR;
16979 if (code == OMP_ATOMIC_READ)
16980 {
16981 v = RECUR (TREE_OPERAND (op1, 0));
16982 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16983 }
16984 else if (code == OMP_ATOMIC_CAPTURE_OLD
16985 || code == OMP_ATOMIC_CAPTURE_NEW)
16986 {
16987 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16988 v = RECUR (TREE_OPERAND (op1, 0));
16989 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16990 if (TREE_CODE (op11) == COMPOUND_EXPR)
16991 {
16992 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16993 op11 = TREE_OPERAND (op11, 1);
16994 }
16995 lhs = RECUR (TREE_OPERAND (op11, 0));
16996 rhs = RECUR (TREE_OPERAND (op11, 1));
16997 opcode = TREE_CODE (op11);
16998 if (opcode == MODIFY_EXPR)
16999 opcode = NOP_EXPR;
17000 }
17001 else
17002 {
17003 code = OMP_ATOMIC;
17004 lhs = RECUR (TREE_OPERAND (op1, 0));
17005 rhs = RECUR (TREE_OPERAND (op1, 1));
17006 }
17007 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
17008 OMP_ATOMIC_SEQ_CST (t));
17009 }
17010 break;
17011
17012 case TRANSACTION_EXPR:
17013 {
17014 int flags = 0;
17015 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17016 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17017
17018 if (TRANSACTION_EXPR_IS_STMT (t))
17019 {
17020 tree body = TRANSACTION_EXPR_BODY (t);
17021 tree noex = NULL_TREE;
17022 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17023 {
17024 noex = MUST_NOT_THROW_COND (body);
17025 if (noex == NULL_TREE)
17026 noex = boolean_true_node;
17027 body = TREE_OPERAND (body, 0);
17028 }
17029 stmt = begin_transaction_stmt (input_location, NULL, flags);
17030 RECUR (body);
17031 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17032 }
17033 else
17034 {
17035 stmt = build_transaction_expr (EXPR_LOCATION (t),
17036 RECUR (TRANSACTION_EXPR_BODY (t)),
17037 flags, NULL_TREE);
17038 RETURN (stmt);
17039 }
17040 }
17041 break;
17042
17043 case MUST_NOT_THROW_EXPR:
17044 {
17045 tree op0 = RECUR (TREE_OPERAND (t, 0));
17046 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17047 RETURN (build_must_not_throw_expr (op0, cond));
17048 }
17049
17050 case EXPR_PACK_EXPANSION:
17051 error ("invalid use of pack expansion expression");
17052 RETURN (error_mark_node);
17053
17054 case NONTYPE_ARGUMENT_PACK:
17055 error ("use %<...%> to expand argument pack");
17056 RETURN (error_mark_node);
17057
17058 case COMPOUND_EXPR:
17059 tmp = RECUR (TREE_OPERAND (t, 0));
17060 if (tmp == NULL_TREE)
17061 /* If the first operand was a statement, we're done with it. */
17062 RETURN (RECUR (TREE_OPERAND (t, 1)));
17063 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17064 RECUR (TREE_OPERAND (t, 1)),
17065 complain));
17066
17067 case ANNOTATE_EXPR:
17068 tmp = RECUR (TREE_OPERAND (t, 0));
17069 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17070 TREE_TYPE (tmp), tmp,
17071 RECUR (TREE_OPERAND (t, 1)),
17072 RECUR (TREE_OPERAND (t, 2))));
17073
17074 default:
17075 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17076
17077 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17078 /*function_p=*/false,
17079 integral_constant_expression_p));
17080 }
17081
17082 RETURN (NULL_TREE);
17083 out:
17084 input_location = loc;
17085 return r;
17086 #undef RECUR
17087 #undef RETURN
17088 }
17089
17090 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17091 function. For description of the body see comment above
17092 cp_parser_omp_declare_reduction_exprs. */
17093
17094 static void
17095 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17096 {
17097 if (t == NULL_TREE || t == error_mark_node)
17098 return;
17099
17100 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17101
17102 tree_stmt_iterator tsi;
17103 int i;
17104 tree stmts[7];
17105 memset (stmts, 0, sizeof stmts);
17106 for (i = 0, tsi = tsi_start (t);
17107 i < 7 && !tsi_end_p (tsi);
17108 i++, tsi_next (&tsi))
17109 stmts[i] = tsi_stmt (tsi);
17110 gcc_assert (tsi_end_p (tsi));
17111
17112 if (i >= 3)
17113 {
17114 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17115 && TREE_CODE (stmts[1]) == DECL_EXPR);
17116 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17117 args, complain, in_decl);
17118 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17119 args, complain, in_decl);
17120 DECL_CONTEXT (omp_out) = current_function_decl;
17121 DECL_CONTEXT (omp_in) = current_function_decl;
17122 keep_next_level (true);
17123 tree block = begin_omp_structured_block ();
17124 tsubst_expr (stmts[2], args, complain, in_decl, false);
17125 block = finish_omp_structured_block (block);
17126 block = maybe_cleanup_point_expr_void (block);
17127 add_decl_expr (omp_out);
17128 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17129 TREE_NO_WARNING (omp_out) = 1;
17130 add_decl_expr (omp_in);
17131 finish_expr_stmt (block);
17132 }
17133 if (i >= 6)
17134 {
17135 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17136 && TREE_CODE (stmts[4]) == DECL_EXPR);
17137 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17138 args, complain, in_decl);
17139 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17140 args, complain, in_decl);
17141 DECL_CONTEXT (omp_priv) = current_function_decl;
17142 DECL_CONTEXT (omp_orig) = current_function_decl;
17143 keep_next_level (true);
17144 tree block = begin_omp_structured_block ();
17145 tsubst_expr (stmts[5], args, complain, in_decl, false);
17146 block = finish_omp_structured_block (block);
17147 block = maybe_cleanup_point_expr_void (block);
17148 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17149 add_decl_expr (omp_priv);
17150 add_decl_expr (omp_orig);
17151 finish_expr_stmt (block);
17152 if (i == 7)
17153 add_decl_expr (omp_orig);
17154 }
17155 }
17156
17157 /* T is a postfix-expression that is not being used in a function
17158 call. Return the substituted version of T. */
17159
17160 static tree
17161 tsubst_non_call_postfix_expression (tree t, tree args,
17162 tsubst_flags_t complain,
17163 tree in_decl)
17164 {
17165 if (TREE_CODE (t) == SCOPE_REF)
17166 t = tsubst_qualified_id (t, args, complain, in_decl,
17167 /*done=*/false, /*address_p=*/false);
17168 else
17169 t = tsubst_copy_and_build (t, args, complain, in_decl,
17170 /*function_p=*/false,
17171 /*integral_constant_expression_p=*/false);
17172
17173 return t;
17174 }
17175
17176 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17177 instantiation context. Instantiating a pack expansion containing a lambda
17178 might result in multiple lambdas all based on the same lambda in the
17179 template. */
17180
17181 tree
17182 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17183 {
17184 tree oldfn = lambda_function (t);
17185 in_decl = oldfn;
17186
17187 tree r = build_lambda_expr ();
17188
17189 LAMBDA_EXPR_LOCATION (r)
17190 = LAMBDA_EXPR_LOCATION (t);
17191 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17192 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17193 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17194
17195 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17196 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
17197 else
17198 record_lambda_scope (r);
17199
17200 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17201 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17202
17203 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17204 cap = TREE_CHAIN (cap))
17205 {
17206 tree field = TREE_PURPOSE (cap);
17207 if (PACK_EXPANSION_P (field))
17208 field = PACK_EXPANSION_PATTERN (field);
17209 field = tsubst_decl (field, args, complain);
17210
17211 if (field == error_mark_node)
17212 return error_mark_node;
17213
17214 tree init = TREE_VALUE (cap);
17215 if (PACK_EXPANSION_P (init))
17216 init = tsubst_pack_expansion (init, args, complain, in_decl);
17217 else
17218 init = tsubst_copy_and_build (init, args, complain, in_decl,
17219 /*fn*/false, /*constexpr*/false);
17220
17221 if (TREE_CODE (field) == TREE_VEC)
17222 {
17223 int len = TREE_VEC_LENGTH (field);
17224 gcc_assert (TREE_CODE (init) == TREE_VEC
17225 && TREE_VEC_LENGTH (init) == len);
17226 for (int i = 0; i < len; ++i)
17227 LAMBDA_EXPR_CAPTURE_LIST (r)
17228 = tree_cons (TREE_VEC_ELT (field, i),
17229 TREE_VEC_ELT (init, i),
17230 LAMBDA_EXPR_CAPTURE_LIST (r));
17231 }
17232 else
17233 {
17234 LAMBDA_EXPR_CAPTURE_LIST (r)
17235 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17236
17237 if (id_equal (DECL_NAME (field), "__this"))
17238 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17239 }
17240 }
17241
17242 tree type = begin_lambda_type (r);
17243
17244 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17245 determine_visibility (TYPE_NAME (type));
17246
17247 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17248
17249 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17250 ? DECL_TI_TEMPLATE (oldfn)
17251 : NULL_TREE);
17252
17253 tree fntype = static_fn_type (oldfn);
17254 if (oldtmpl)
17255 ++processing_template_decl;
17256 fntype = tsubst (fntype, args, complain, in_decl);
17257 if (oldtmpl)
17258 --processing_template_decl;
17259
17260 if (fntype == error_mark_node)
17261 r = error_mark_node;
17262 else
17263 {
17264 /* Fix the type of 'this'. */
17265 fntype = build_memfn_type (fntype, type,
17266 type_memfn_quals (fntype),
17267 type_memfn_rqual (fntype));
17268 tree fn, tmpl;
17269 if (oldtmpl)
17270 {
17271 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17272 fn = DECL_TEMPLATE_RESULT (tmpl);
17273 finish_member_declaration (tmpl);
17274 }
17275 else
17276 {
17277 tmpl = NULL_TREE;
17278 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17279 finish_member_declaration (fn);
17280 }
17281
17282 /* Let finish_function set this. */
17283 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17284
17285 bool nested = cfun;
17286 if (nested)
17287 push_function_context ();
17288 else
17289 /* Still increment function_depth so that we don't GC in the
17290 middle of an expression. */
17291 ++function_depth;
17292
17293 local_specialization_stack s (lss_copy);
17294
17295 tree body = start_lambda_function (fn, r);
17296
17297 register_parameter_specializations (oldfn, fn);
17298
17299 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17300 /*constexpr*/false);
17301
17302 finish_lambda_function (body);
17303
17304 if (nested)
17305 pop_function_context ();
17306 else
17307 --function_depth;
17308
17309 /* The capture list was built up in reverse order; fix that now. */
17310 LAMBDA_EXPR_CAPTURE_LIST (r)
17311 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17312
17313 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17314
17315 maybe_add_lambda_conv_op (type);
17316 }
17317
17318 finish_struct (type, /*attr*/NULL_TREE);
17319
17320 insert_pending_capture_proxies ();
17321
17322 return r;
17323 }
17324
17325 /* Like tsubst but deals with expressions and performs semantic
17326 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17327
17328 tree
17329 tsubst_copy_and_build (tree t,
17330 tree args,
17331 tsubst_flags_t complain,
17332 tree in_decl,
17333 bool function_p,
17334 bool integral_constant_expression_p)
17335 {
17336 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17337 #define RECUR(NODE) \
17338 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17339 /*function_p=*/false, \
17340 integral_constant_expression_p)
17341
17342 tree retval, op1;
17343 location_t loc;
17344
17345 if (t == NULL_TREE || t == error_mark_node)
17346 return t;
17347
17348 loc = input_location;
17349 if (EXPR_HAS_LOCATION (t))
17350 input_location = EXPR_LOCATION (t);
17351
17352 /* N3276 decltype magic only applies to calls at the top level or on the
17353 right side of a comma. */
17354 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17355 complain &= ~tf_decltype;
17356
17357 switch (TREE_CODE (t))
17358 {
17359 case USING_DECL:
17360 t = DECL_NAME (t);
17361 /* Fall through. */
17362 case IDENTIFIER_NODE:
17363 {
17364 tree decl;
17365 cp_id_kind idk;
17366 bool non_integral_constant_expression_p;
17367 const char *error_msg;
17368
17369 if (IDENTIFIER_CONV_OP_P (t))
17370 {
17371 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17372 t = make_conv_op_name (new_type);
17373 }
17374
17375 /* Look up the name. */
17376 decl = lookup_name (t);
17377
17378 /* By convention, expressions use ERROR_MARK_NODE to indicate
17379 failure, not NULL_TREE. */
17380 if (decl == NULL_TREE)
17381 decl = error_mark_node;
17382
17383 decl = finish_id_expression (t, decl, NULL_TREE,
17384 &idk,
17385 integral_constant_expression_p,
17386 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17387 &non_integral_constant_expression_p,
17388 /*template_p=*/false,
17389 /*done=*/true,
17390 /*address_p=*/false,
17391 /*template_arg_p=*/false,
17392 &error_msg,
17393 input_location);
17394 if (error_msg)
17395 error (error_msg);
17396 if (!function_p && identifier_p (decl))
17397 {
17398 if (complain & tf_error)
17399 unqualified_name_lookup_error (decl);
17400 decl = error_mark_node;
17401 }
17402 RETURN (decl);
17403 }
17404
17405 case TEMPLATE_ID_EXPR:
17406 {
17407 tree object;
17408 tree templ = RECUR (TREE_OPERAND (t, 0));
17409 tree targs = TREE_OPERAND (t, 1);
17410
17411 if (targs)
17412 targs = tsubst_template_args (targs, args, complain, in_decl);
17413 if (targs == error_mark_node)
17414 RETURN (error_mark_node);
17415
17416 if (TREE_CODE (templ) == SCOPE_REF)
17417 {
17418 tree name = TREE_OPERAND (templ, 1);
17419 tree tid = lookup_template_function (name, targs);
17420 TREE_OPERAND (templ, 1) = tid;
17421 RETURN (templ);
17422 }
17423
17424 if (variable_template_p (templ))
17425 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17426
17427 if (TREE_CODE (templ) == COMPONENT_REF)
17428 {
17429 object = TREE_OPERAND (templ, 0);
17430 templ = TREE_OPERAND (templ, 1);
17431 }
17432 else
17433 object = NULL_TREE;
17434 templ = lookup_template_function (templ, targs);
17435
17436 if (object)
17437 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17438 object, templ, NULL_TREE));
17439 else
17440 RETURN (baselink_for_fns (templ));
17441 }
17442
17443 case INDIRECT_REF:
17444 {
17445 tree r = RECUR (TREE_OPERAND (t, 0));
17446
17447 if (REFERENCE_REF_P (t))
17448 {
17449 /* A type conversion to reference type will be enclosed in
17450 such an indirect ref, but the substitution of the cast
17451 will have also added such an indirect ref. */
17452 r = convert_from_reference (r);
17453 }
17454 else
17455 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17456 complain|decltype_flag);
17457
17458 if (REF_PARENTHESIZED_P (t))
17459 r = force_paren_expr (r);
17460
17461 RETURN (r);
17462 }
17463
17464 case NOP_EXPR:
17465 {
17466 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17467 tree op0 = RECUR (TREE_OPERAND (t, 0));
17468 RETURN (build_nop (type, op0));
17469 }
17470
17471 case IMPLICIT_CONV_EXPR:
17472 {
17473 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17474 tree expr = RECUR (TREE_OPERAND (t, 0));
17475 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17476 {
17477 retval = copy_node (t);
17478 TREE_TYPE (retval) = type;
17479 TREE_OPERAND (retval, 0) = expr;
17480 RETURN (retval);
17481 }
17482 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17483 /* We'll pass this to convert_nontype_argument again, we don't need
17484 to actually perform any conversion here. */
17485 RETURN (expr);
17486 int flags = LOOKUP_IMPLICIT;
17487 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17488 flags = LOOKUP_NORMAL;
17489 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17490 flags));
17491 }
17492
17493 case CONVERT_EXPR:
17494 {
17495 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17496 tree op0 = RECUR (TREE_OPERAND (t, 0));
17497 if (op0 == error_mark_node)
17498 RETURN (error_mark_node);
17499 RETURN (build1 (CONVERT_EXPR, type, op0));
17500 }
17501
17502 case CAST_EXPR:
17503 case REINTERPRET_CAST_EXPR:
17504 case CONST_CAST_EXPR:
17505 case DYNAMIC_CAST_EXPR:
17506 case STATIC_CAST_EXPR:
17507 {
17508 tree type;
17509 tree op, r = NULL_TREE;
17510
17511 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17512 if (integral_constant_expression_p
17513 && !cast_valid_in_integral_constant_expression_p (type))
17514 {
17515 if (complain & tf_error)
17516 error ("a cast to a type other than an integral or "
17517 "enumeration type cannot appear in a constant-expression");
17518 RETURN (error_mark_node);
17519 }
17520
17521 op = RECUR (TREE_OPERAND (t, 0));
17522
17523 warning_sentinel s(warn_useless_cast);
17524 warning_sentinel s2(warn_ignored_qualifiers);
17525 switch (TREE_CODE (t))
17526 {
17527 case CAST_EXPR:
17528 r = build_functional_cast (type, op, complain);
17529 break;
17530 case REINTERPRET_CAST_EXPR:
17531 r = build_reinterpret_cast (type, op, complain);
17532 break;
17533 case CONST_CAST_EXPR:
17534 r = build_const_cast (type, op, complain);
17535 break;
17536 case DYNAMIC_CAST_EXPR:
17537 r = build_dynamic_cast (type, op, complain);
17538 break;
17539 case STATIC_CAST_EXPR:
17540 r = build_static_cast (type, op, complain);
17541 break;
17542 default:
17543 gcc_unreachable ();
17544 }
17545
17546 RETURN (r);
17547 }
17548
17549 case POSTDECREMENT_EXPR:
17550 case POSTINCREMENT_EXPR:
17551 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17552 args, complain, in_decl);
17553 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17554 complain|decltype_flag));
17555
17556 case PREDECREMENT_EXPR:
17557 case PREINCREMENT_EXPR:
17558 case NEGATE_EXPR:
17559 case BIT_NOT_EXPR:
17560 case ABS_EXPR:
17561 case TRUTH_NOT_EXPR:
17562 case UNARY_PLUS_EXPR: /* Unary + */
17563 case REALPART_EXPR:
17564 case IMAGPART_EXPR:
17565 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17566 RECUR (TREE_OPERAND (t, 0)),
17567 complain|decltype_flag));
17568
17569 case FIX_TRUNC_EXPR:
17570 gcc_unreachable ();
17571
17572 case ADDR_EXPR:
17573 op1 = TREE_OPERAND (t, 0);
17574 if (TREE_CODE (op1) == LABEL_DECL)
17575 RETURN (finish_label_address_expr (DECL_NAME (op1),
17576 EXPR_LOCATION (op1)));
17577 if (TREE_CODE (op1) == SCOPE_REF)
17578 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17579 /*done=*/true, /*address_p=*/true);
17580 else
17581 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17582 in_decl);
17583 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17584 complain|decltype_flag));
17585
17586 case PLUS_EXPR:
17587 case MINUS_EXPR:
17588 case MULT_EXPR:
17589 case TRUNC_DIV_EXPR:
17590 case CEIL_DIV_EXPR:
17591 case FLOOR_DIV_EXPR:
17592 case ROUND_DIV_EXPR:
17593 case EXACT_DIV_EXPR:
17594 case BIT_AND_EXPR:
17595 case BIT_IOR_EXPR:
17596 case BIT_XOR_EXPR:
17597 case TRUNC_MOD_EXPR:
17598 case FLOOR_MOD_EXPR:
17599 case TRUTH_ANDIF_EXPR:
17600 case TRUTH_ORIF_EXPR:
17601 case TRUTH_AND_EXPR:
17602 case TRUTH_OR_EXPR:
17603 case RSHIFT_EXPR:
17604 case LSHIFT_EXPR:
17605 case RROTATE_EXPR:
17606 case LROTATE_EXPR:
17607 case EQ_EXPR:
17608 case NE_EXPR:
17609 case MAX_EXPR:
17610 case MIN_EXPR:
17611 case LE_EXPR:
17612 case GE_EXPR:
17613 case LT_EXPR:
17614 case GT_EXPR:
17615 case MEMBER_REF:
17616 case DOTSTAR_EXPR:
17617 {
17618 warning_sentinel s1(warn_type_limits);
17619 warning_sentinel s2(warn_div_by_zero);
17620 warning_sentinel s3(warn_logical_op);
17621 warning_sentinel s4(warn_tautological_compare);
17622 tree op0 = RECUR (TREE_OPERAND (t, 0));
17623 tree op1 = RECUR (TREE_OPERAND (t, 1));
17624 tree r = build_x_binary_op
17625 (input_location, TREE_CODE (t),
17626 op0,
17627 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17628 ? ERROR_MARK
17629 : TREE_CODE (TREE_OPERAND (t, 0))),
17630 op1,
17631 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17632 ? ERROR_MARK
17633 : TREE_CODE (TREE_OPERAND (t, 1))),
17634 /*overload=*/NULL,
17635 complain|decltype_flag);
17636 if (EXPR_P (r) && TREE_NO_WARNING (t))
17637 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17638
17639 RETURN (r);
17640 }
17641
17642 case POINTER_PLUS_EXPR:
17643 {
17644 tree op0 = RECUR (TREE_OPERAND (t, 0));
17645 tree op1 = RECUR (TREE_OPERAND (t, 1));
17646 RETURN (fold_build_pointer_plus (op0, op1));
17647 }
17648
17649 case SCOPE_REF:
17650 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17651 /*address_p=*/false));
17652 case ARRAY_REF:
17653 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17654 args, complain, in_decl);
17655 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17656 RECUR (TREE_OPERAND (t, 1)),
17657 complain|decltype_flag));
17658
17659 case SIZEOF_EXPR:
17660 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17661 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17662 RETURN (tsubst_copy (t, args, complain, in_decl));
17663 /* Fall through */
17664
17665 case ALIGNOF_EXPR:
17666 {
17667 tree r;
17668
17669 op1 = TREE_OPERAND (t, 0);
17670 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17671 op1 = TREE_TYPE (op1);
17672 if (!args)
17673 {
17674 /* When there are no ARGS, we are trying to evaluate a
17675 non-dependent expression from the parser. Trying to do
17676 the substitutions may not work. */
17677 if (!TYPE_P (op1))
17678 op1 = TREE_TYPE (op1);
17679 }
17680 else
17681 {
17682 ++cp_unevaluated_operand;
17683 ++c_inhibit_evaluation_warnings;
17684 if (TYPE_P (op1))
17685 op1 = tsubst (op1, args, complain, in_decl);
17686 else
17687 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17688 /*function_p=*/false,
17689 /*integral_constant_expression_p=*/
17690 false);
17691 --cp_unevaluated_operand;
17692 --c_inhibit_evaluation_warnings;
17693 }
17694 if (TYPE_P (op1))
17695 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17696 complain & tf_error);
17697 else
17698 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17699 complain & tf_error);
17700 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17701 {
17702 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17703 {
17704 if (!processing_template_decl && TYPE_P (op1))
17705 {
17706 r = build_min (SIZEOF_EXPR, size_type_node,
17707 build1 (NOP_EXPR, op1, error_mark_node));
17708 SIZEOF_EXPR_TYPE_P (r) = 1;
17709 }
17710 else
17711 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17712 TREE_SIDE_EFFECTS (r) = 0;
17713 TREE_READONLY (r) = 1;
17714 }
17715 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17716 }
17717 RETURN (r);
17718 }
17719
17720 case AT_ENCODE_EXPR:
17721 {
17722 op1 = TREE_OPERAND (t, 0);
17723 ++cp_unevaluated_operand;
17724 ++c_inhibit_evaluation_warnings;
17725 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17726 /*function_p=*/false,
17727 /*integral_constant_expression_p=*/false);
17728 --cp_unevaluated_operand;
17729 --c_inhibit_evaluation_warnings;
17730 RETURN (objc_build_encode_expr (op1));
17731 }
17732
17733 case NOEXCEPT_EXPR:
17734 op1 = TREE_OPERAND (t, 0);
17735 ++cp_unevaluated_operand;
17736 ++c_inhibit_evaluation_warnings;
17737 ++cp_noexcept_operand;
17738 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17739 /*function_p=*/false,
17740 /*integral_constant_expression_p=*/false);
17741 --cp_unevaluated_operand;
17742 --c_inhibit_evaluation_warnings;
17743 --cp_noexcept_operand;
17744 RETURN (finish_noexcept_expr (op1, complain));
17745
17746 case MODOP_EXPR:
17747 {
17748 warning_sentinel s(warn_div_by_zero);
17749 tree lhs = RECUR (TREE_OPERAND (t, 0));
17750 tree rhs = RECUR (TREE_OPERAND (t, 2));
17751 tree r = build_x_modify_expr
17752 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17753 complain|decltype_flag);
17754 /* TREE_NO_WARNING must be set if either the expression was
17755 parenthesized or it uses an operator such as >>= rather
17756 than plain assignment. In the former case, it was already
17757 set and must be copied. In the latter case,
17758 build_x_modify_expr sets it and it must not be reset
17759 here. */
17760 if (TREE_NO_WARNING (t))
17761 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17762
17763 RETURN (r);
17764 }
17765
17766 case ARROW_EXPR:
17767 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17768 args, complain, in_decl);
17769 /* Remember that there was a reference to this entity. */
17770 if (DECL_P (op1)
17771 && !mark_used (op1, complain) && !(complain & tf_error))
17772 RETURN (error_mark_node);
17773 RETURN (build_x_arrow (input_location, op1, complain));
17774
17775 case NEW_EXPR:
17776 {
17777 tree placement = RECUR (TREE_OPERAND (t, 0));
17778 tree init = RECUR (TREE_OPERAND (t, 3));
17779 vec<tree, va_gc> *placement_vec;
17780 vec<tree, va_gc> *init_vec;
17781 tree ret;
17782
17783 if (placement == NULL_TREE)
17784 placement_vec = NULL;
17785 else
17786 {
17787 placement_vec = make_tree_vector ();
17788 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17789 vec_safe_push (placement_vec, TREE_VALUE (placement));
17790 }
17791
17792 /* If there was an initializer in the original tree, but it
17793 instantiated to an empty list, then we should pass a
17794 non-NULL empty vector to tell build_new that it was an
17795 empty initializer() rather than no initializer. This can
17796 only happen when the initializer is a pack expansion whose
17797 parameter packs are of length zero. */
17798 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17799 init_vec = NULL;
17800 else
17801 {
17802 init_vec = make_tree_vector ();
17803 if (init == void_node)
17804 gcc_assert (init_vec != NULL);
17805 else
17806 {
17807 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17808 vec_safe_push (init_vec, TREE_VALUE (init));
17809 }
17810 }
17811
17812 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17813 tree op2 = RECUR (TREE_OPERAND (t, 2));
17814 ret = build_new (&placement_vec, op1, op2, &init_vec,
17815 NEW_EXPR_USE_GLOBAL (t),
17816 complain);
17817
17818 if (placement_vec != NULL)
17819 release_tree_vector (placement_vec);
17820 if (init_vec != NULL)
17821 release_tree_vector (init_vec);
17822
17823 RETURN (ret);
17824 }
17825
17826 case DELETE_EXPR:
17827 {
17828 tree op0 = RECUR (TREE_OPERAND (t, 0));
17829 tree op1 = RECUR (TREE_OPERAND (t, 1));
17830 RETURN (delete_sanity (op0, op1,
17831 DELETE_EXPR_USE_VEC (t),
17832 DELETE_EXPR_USE_GLOBAL (t),
17833 complain));
17834 }
17835
17836 case COMPOUND_EXPR:
17837 {
17838 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17839 complain & ~tf_decltype, in_decl,
17840 /*function_p=*/false,
17841 integral_constant_expression_p);
17842 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17843 op0,
17844 RECUR (TREE_OPERAND (t, 1)),
17845 complain|decltype_flag));
17846 }
17847
17848 case CALL_EXPR:
17849 {
17850 tree function;
17851 vec<tree, va_gc> *call_args;
17852 unsigned int nargs, i;
17853 bool qualified_p;
17854 bool koenig_p;
17855 tree ret;
17856
17857 function = CALL_EXPR_FN (t);
17858 /* Internal function with no arguments. */
17859 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17860 RETURN (t);
17861
17862 /* When we parsed the expression, we determined whether or
17863 not Koenig lookup should be performed. */
17864 koenig_p = KOENIG_LOOKUP_P (t);
17865 if (function == NULL_TREE)
17866 {
17867 koenig_p = false;
17868 qualified_p = false;
17869 }
17870 else if (TREE_CODE (function) == SCOPE_REF)
17871 {
17872 qualified_p = true;
17873 function = tsubst_qualified_id (function, args, complain, in_decl,
17874 /*done=*/false,
17875 /*address_p=*/false);
17876 }
17877 else if (koenig_p && identifier_p (function))
17878 {
17879 /* Do nothing; calling tsubst_copy_and_build on an identifier
17880 would incorrectly perform unqualified lookup again.
17881
17882 Note that we can also have an IDENTIFIER_NODE if the earlier
17883 unqualified lookup found a member function; in that case
17884 koenig_p will be false and we do want to do the lookup
17885 again to find the instantiated member function.
17886
17887 FIXME but doing that causes c++/15272, so we need to stop
17888 using IDENTIFIER_NODE in that situation. */
17889 qualified_p = false;
17890 }
17891 else
17892 {
17893 if (TREE_CODE (function) == COMPONENT_REF)
17894 {
17895 tree op = TREE_OPERAND (function, 1);
17896
17897 qualified_p = (TREE_CODE (op) == SCOPE_REF
17898 || (BASELINK_P (op)
17899 && BASELINK_QUALIFIED_P (op)));
17900 }
17901 else
17902 qualified_p = false;
17903
17904 if (TREE_CODE (function) == ADDR_EXPR
17905 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17906 /* Avoid error about taking the address of a constructor. */
17907 function = TREE_OPERAND (function, 0);
17908
17909 function = tsubst_copy_and_build (function, args, complain,
17910 in_decl,
17911 !qualified_p,
17912 integral_constant_expression_p);
17913
17914 if (BASELINK_P (function))
17915 qualified_p = true;
17916 }
17917
17918 nargs = call_expr_nargs (t);
17919 call_args = make_tree_vector ();
17920 for (i = 0; i < nargs; ++i)
17921 {
17922 tree arg = CALL_EXPR_ARG (t, i);
17923
17924 if (!PACK_EXPANSION_P (arg))
17925 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17926 else
17927 {
17928 /* Expand the pack expansion and push each entry onto
17929 CALL_ARGS. */
17930 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17931 if (TREE_CODE (arg) == TREE_VEC)
17932 {
17933 unsigned int len, j;
17934
17935 len = TREE_VEC_LENGTH (arg);
17936 for (j = 0; j < len; ++j)
17937 {
17938 tree value = TREE_VEC_ELT (arg, j);
17939 if (value != NULL_TREE)
17940 value = convert_from_reference (value);
17941 vec_safe_push (call_args, value);
17942 }
17943 }
17944 else
17945 {
17946 /* A partial substitution. Add one entry. */
17947 vec_safe_push (call_args, arg);
17948 }
17949 }
17950 }
17951
17952 /* We do not perform argument-dependent lookup if normal
17953 lookup finds a non-function, in accordance with the
17954 expected resolution of DR 218. */
17955 if (koenig_p
17956 && ((is_overloaded_fn (function)
17957 /* If lookup found a member function, the Koenig lookup is
17958 not appropriate, even if an unqualified-name was used
17959 to denote the function. */
17960 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17961 || identifier_p (function))
17962 /* Only do this when substitution turns a dependent call
17963 into a non-dependent call. */
17964 && type_dependent_expression_p_push (t)
17965 && !any_type_dependent_arguments_p (call_args))
17966 function = perform_koenig_lookup (function, call_args, tf_none);
17967
17968 if (function != NULL_TREE
17969 && identifier_p (function)
17970 && !any_type_dependent_arguments_p (call_args))
17971 {
17972 if (koenig_p && (complain & tf_warning_or_error))
17973 {
17974 /* For backwards compatibility and good diagnostics, try
17975 the unqualified lookup again if we aren't in SFINAE
17976 context. */
17977 tree unq = (tsubst_copy_and_build
17978 (function, args, complain, in_decl, true,
17979 integral_constant_expression_p));
17980 if (unq == error_mark_node)
17981 {
17982 release_tree_vector (call_args);
17983 RETURN (error_mark_node);
17984 }
17985
17986 if (unq != function)
17987 {
17988 /* In a lambda fn, we have to be careful to not
17989 introduce new this captures. Legacy code can't
17990 be using lambdas anyway, so it's ok to be
17991 stricter. */
17992 bool in_lambda = (current_class_type
17993 && LAMBDA_TYPE_P (current_class_type));
17994 char const *const msg
17995 = G_("%qD was not declared in this scope, "
17996 "and no declarations were found by "
17997 "argument-dependent lookup at the point "
17998 "of instantiation");
17999
18000 bool diag = true;
18001 if (in_lambda)
18002 error_at (EXPR_LOC_OR_LOC (t, input_location),
18003 msg, function);
18004 else
18005 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
18006 msg, function);
18007 if (diag)
18008 {
18009 tree fn = unq;
18010
18011 if (INDIRECT_REF_P (fn))
18012 fn = TREE_OPERAND (fn, 0);
18013 if (is_overloaded_fn (fn))
18014 fn = get_first_fn (fn);
18015
18016 if (!DECL_P (fn))
18017 /* Can't say anything more. */;
18018 else if (DECL_CLASS_SCOPE_P (fn))
18019 {
18020 location_t loc = EXPR_LOC_OR_LOC (t,
18021 input_location);
18022 inform (loc,
18023 "declarations in dependent base %qT are "
18024 "not found by unqualified lookup",
18025 DECL_CLASS_CONTEXT (fn));
18026 if (current_class_ptr)
18027 inform (loc,
18028 "use %<this->%D%> instead", function);
18029 else
18030 inform (loc,
18031 "use %<%T::%D%> instead",
18032 current_class_name, function);
18033 }
18034 else
18035 inform (DECL_SOURCE_LOCATION (fn),
18036 "%qD declared here, later in the "
18037 "translation unit", fn);
18038 if (in_lambda)
18039 {
18040 release_tree_vector (call_args);
18041 RETURN (error_mark_node);
18042 }
18043 }
18044
18045 function = unq;
18046 }
18047 }
18048 if (identifier_p (function))
18049 {
18050 if (complain & tf_error)
18051 unqualified_name_lookup_error (function);
18052 release_tree_vector (call_args);
18053 RETURN (error_mark_node);
18054 }
18055 }
18056
18057 /* Remember that there was a reference to this entity. */
18058 if (function != NULL_TREE
18059 && DECL_P (function)
18060 && !mark_used (function, complain) && !(complain & tf_error))
18061 {
18062 release_tree_vector (call_args);
18063 RETURN (error_mark_node);
18064 }
18065
18066 /* Put back tf_decltype for the actual call. */
18067 complain |= decltype_flag;
18068
18069 if (function == NULL_TREE)
18070 switch (CALL_EXPR_IFN (t))
18071 {
18072 case IFN_LAUNDER:
18073 gcc_assert (nargs == 1);
18074 if (vec_safe_length (call_args) != 1)
18075 {
18076 error_at (EXPR_LOC_OR_LOC (t, input_location),
18077 "wrong number of arguments to "
18078 "%<__builtin_launder%>");
18079 ret = error_mark_node;
18080 }
18081 else
18082 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
18083 input_location),
18084 (*call_args)[0], complain);
18085 break;
18086
18087 default:
18088 /* Unsupported internal function with arguments. */
18089 gcc_unreachable ();
18090 }
18091 else if (TREE_CODE (function) == OFFSET_REF)
18092 ret = build_offset_ref_call_from_tree (function, &call_args,
18093 complain);
18094 else if (TREE_CODE (function) == COMPONENT_REF)
18095 {
18096 tree instance = TREE_OPERAND (function, 0);
18097 tree fn = TREE_OPERAND (function, 1);
18098
18099 if (processing_template_decl
18100 && (type_dependent_expression_p (instance)
18101 || (!BASELINK_P (fn)
18102 && TREE_CODE (fn) != FIELD_DECL)
18103 || type_dependent_expression_p (fn)
18104 || any_type_dependent_arguments_p (call_args)))
18105 ret = build_min_nt_call_vec (function, call_args);
18106 else if (!BASELINK_P (fn))
18107 ret = finish_call_expr (function, &call_args,
18108 /*disallow_virtual=*/false,
18109 /*koenig_p=*/false,
18110 complain);
18111 else
18112 ret = (build_new_method_call
18113 (instance, fn,
18114 &call_args, NULL_TREE,
18115 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18116 /*fn_p=*/NULL,
18117 complain));
18118 }
18119 else
18120 ret = finish_call_expr (function, &call_args,
18121 /*disallow_virtual=*/qualified_p,
18122 koenig_p,
18123 complain);
18124
18125 release_tree_vector (call_args);
18126
18127 if (ret != error_mark_node)
18128 {
18129 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18130 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18131 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18132 bool thk = CALL_FROM_THUNK_P (t);
18133 if (op || ord || rev || thk)
18134 {
18135 function = extract_call_expr (ret);
18136 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18137 CALL_EXPR_ORDERED_ARGS (function) = ord;
18138 CALL_EXPR_REVERSE_ARGS (function) = rev;
18139 if (thk)
18140 {
18141 if (TREE_CODE (function) == CALL_EXPR)
18142 CALL_FROM_THUNK_P (function) = true;
18143 else
18144 AGGR_INIT_FROM_THUNK_P (function) = true;
18145 /* The thunk location is not interesting. */
18146 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18147 }
18148 }
18149 }
18150
18151 RETURN (ret);
18152 }
18153
18154 case COND_EXPR:
18155 {
18156 tree cond = RECUR (TREE_OPERAND (t, 0));
18157 tree folded_cond = fold_non_dependent_expr (cond);
18158 tree exp1, exp2;
18159
18160 if (TREE_CODE (folded_cond) == INTEGER_CST)
18161 {
18162 if (integer_zerop (folded_cond))
18163 {
18164 ++c_inhibit_evaluation_warnings;
18165 exp1 = RECUR (TREE_OPERAND (t, 1));
18166 --c_inhibit_evaluation_warnings;
18167 exp2 = RECUR (TREE_OPERAND (t, 2));
18168 }
18169 else
18170 {
18171 exp1 = RECUR (TREE_OPERAND (t, 1));
18172 ++c_inhibit_evaluation_warnings;
18173 exp2 = RECUR (TREE_OPERAND (t, 2));
18174 --c_inhibit_evaluation_warnings;
18175 }
18176 cond = folded_cond;
18177 }
18178 else
18179 {
18180 exp1 = RECUR (TREE_OPERAND (t, 1));
18181 exp2 = RECUR (TREE_OPERAND (t, 2));
18182 }
18183
18184 warning_sentinel s(warn_duplicated_branches);
18185 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18186 cond, exp1, exp2, complain));
18187 }
18188
18189 case PSEUDO_DTOR_EXPR:
18190 {
18191 tree op0 = RECUR (TREE_OPERAND (t, 0));
18192 tree op1 = RECUR (TREE_OPERAND (t, 1));
18193 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18194 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18195 input_location));
18196 }
18197
18198 case TREE_LIST:
18199 {
18200 tree purpose, value, chain;
18201
18202 if (t == void_list_node)
18203 RETURN (t);
18204
18205 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18206 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18207 {
18208 /* We have pack expansions, so expand those and
18209 create a new list out of it. */
18210 tree purposevec = NULL_TREE;
18211 tree valuevec = NULL_TREE;
18212 tree chain;
18213 int i, len = -1;
18214
18215 /* Expand the argument expressions. */
18216 if (TREE_PURPOSE (t))
18217 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18218 complain, in_decl);
18219 if (TREE_VALUE (t))
18220 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18221 complain, in_decl);
18222
18223 /* Build the rest of the list. */
18224 chain = TREE_CHAIN (t);
18225 if (chain && chain != void_type_node)
18226 chain = RECUR (chain);
18227
18228 /* Determine the number of arguments. */
18229 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18230 {
18231 len = TREE_VEC_LENGTH (purposevec);
18232 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18233 }
18234 else if (TREE_CODE (valuevec) == TREE_VEC)
18235 len = TREE_VEC_LENGTH (valuevec);
18236 else
18237 {
18238 /* Since we only performed a partial substitution into
18239 the argument pack, we only RETURN (a single list
18240 node. */
18241 if (purposevec == TREE_PURPOSE (t)
18242 && valuevec == TREE_VALUE (t)
18243 && chain == TREE_CHAIN (t))
18244 RETURN (t);
18245
18246 RETURN (tree_cons (purposevec, valuevec, chain));
18247 }
18248
18249 /* Convert the argument vectors into a TREE_LIST */
18250 i = len;
18251 while (i > 0)
18252 {
18253 /* Grab the Ith values. */
18254 i--;
18255 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18256 : NULL_TREE;
18257 value
18258 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18259 : NULL_TREE;
18260
18261 /* Build the list (backwards). */
18262 chain = tree_cons (purpose, value, chain);
18263 }
18264
18265 RETURN (chain);
18266 }
18267
18268 purpose = TREE_PURPOSE (t);
18269 if (purpose)
18270 purpose = RECUR (purpose);
18271 value = TREE_VALUE (t);
18272 if (value)
18273 value = RECUR (value);
18274 chain = TREE_CHAIN (t);
18275 if (chain && chain != void_type_node)
18276 chain = RECUR (chain);
18277 if (purpose == TREE_PURPOSE (t)
18278 && value == TREE_VALUE (t)
18279 && chain == TREE_CHAIN (t))
18280 RETURN (t);
18281 RETURN (tree_cons (purpose, value, chain));
18282 }
18283
18284 case COMPONENT_REF:
18285 {
18286 tree object;
18287 tree object_type;
18288 tree member;
18289 tree r;
18290
18291 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18292 args, complain, in_decl);
18293 /* Remember that there was a reference to this entity. */
18294 if (DECL_P (object)
18295 && !mark_used (object, complain) && !(complain & tf_error))
18296 RETURN (error_mark_node);
18297 object_type = TREE_TYPE (object);
18298
18299 member = TREE_OPERAND (t, 1);
18300 if (BASELINK_P (member))
18301 member = tsubst_baselink (member,
18302 non_reference (TREE_TYPE (object)),
18303 args, complain, in_decl);
18304 else
18305 member = tsubst_copy (member, args, complain, in_decl);
18306 if (member == error_mark_node)
18307 RETURN (error_mark_node);
18308
18309 if (TREE_CODE (member) == FIELD_DECL)
18310 {
18311 r = finish_non_static_data_member (member, object, NULL_TREE);
18312 if (TREE_CODE (r) == COMPONENT_REF)
18313 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18314 RETURN (r);
18315 }
18316 else if (type_dependent_expression_p (object))
18317 /* We can't do much here. */;
18318 else if (!CLASS_TYPE_P (object_type))
18319 {
18320 if (scalarish_type_p (object_type))
18321 {
18322 tree s = NULL_TREE;
18323 tree dtor = member;
18324
18325 if (TREE_CODE (dtor) == SCOPE_REF)
18326 {
18327 s = TREE_OPERAND (dtor, 0);
18328 dtor = TREE_OPERAND (dtor, 1);
18329 }
18330 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18331 {
18332 dtor = TREE_OPERAND (dtor, 0);
18333 if (TYPE_P (dtor))
18334 RETURN (finish_pseudo_destructor_expr
18335 (object, s, dtor, input_location));
18336 }
18337 }
18338 }
18339 else if (TREE_CODE (member) == SCOPE_REF
18340 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18341 {
18342 /* Lookup the template functions now that we know what the
18343 scope is. */
18344 tree scope = TREE_OPERAND (member, 0);
18345 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18346 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18347 member = lookup_qualified_name (scope, tmpl,
18348 /*is_type_p=*/false,
18349 /*complain=*/false);
18350 if (BASELINK_P (member))
18351 {
18352 BASELINK_FUNCTIONS (member)
18353 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18354 args);
18355 member = (adjust_result_of_qualified_name_lookup
18356 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18357 object_type));
18358 }
18359 else
18360 {
18361 qualified_name_lookup_error (scope, tmpl, member,
18362 input_location);
18363 RETURN (error_mark_node);
18364 }
18365 }
18366 else if (TREE_CODE (member) == SCOPE_REF
18367 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18368 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18369 {
18370 if (complain & tf_error)
18371 {
18372 if (TYPE_P (TREE_OPERAND (member, 0)))
18373 error ("%qT is not a class or namespace",
18374 TREE_OPERAND (member, 0));
18375 else
18376 error ("%qD is not a class or namespace",
18377 TREE_OPERAND (member, 0));
18378 }
18379 RETURN (error_mark_node);
18380 }
18381
18382 r = finish_class_member_access_expr (object, member,
18383 /*template_p=*/false,
18384 complain);
18385 if (TREE_CODE (r) == COMPONENT_REF)
18386 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18387 RETURN (r);
18388 }
18389
18390 case THROW_EXPR:
18391 RETURN (build_throw
18392 (RECUR (TREE_OPERAND (t, 0))));
18393
18394 case CONSTRUCTOR:
18395 {
18396 vec<constructor_elt, va_gc> *n;
18397 constructor_elt *ce;
18398 unsigned HOST_WIDE_INT idx;
18399 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18400 bool process_index_p;
18401 int newlen;
18402 bool need_copy_p = false;
18403 tree r;
18404
18405 if (type == error_mark_node)
18406 RETURN (error_mark_node);
18407
18408 /* We do not want to process the index of aggregate
18409 initializers as they are identifier nodes which will be
18410 looked up by digest_init. */
18411 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18412
18413 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18414 newlen = vec_safe_length (n);
18415 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18416 {
18417 if (ce->index && process_index_p
18418 /* An identifier index is looked up in the type
18419 being initialized, not the current scope. */
18420 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18421 ce->index = RECUR (ce->index);
18422
18423 if (PACK_EXPANSION_P (ce->value))
18424 {
18425 /* Substitute into the pack expansion. */
18426 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18427 in_decl);
18428
18429 if (ce->value == error_mark_node
18430 || PACK_EXPANSION_P (ce->value))
18431 ;
18432 else if (TREE_VEC_LENGTH (ce->value) == 1)
18433 /* Just move the argument into place. */
18434 ce->value = TREE_VEC_ELT (ce->value, 0);
18435 else
18436 {
18437 /* Update the length of the final CONSTRUCTOR
18438 arguments vector, and note that we will need to
18439 copy.*/
18440 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18441 need_copy_p = true;
18442 }
18443 }
18444 else
18445 ce->value = RECUR (ce->value);
18446 }
18447
18448 if (need_copy_p)
18449 {
18450 vec<constructor_elt, va_gc> *old_n = n;
18451
18452 vec_alloc (n, newlen);
18453 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18454 {
18455 if (TREE_CODE (ce->value) == TREE_VEC)
18456 {
18457 int i, len = TREE_VEC_LENGTH (ce->value);
18458 for (i = 0; i < len; ++i)
18459 CONSTRUCTOR_APPEND_ELT (n, 0,
18460 TREE_VEC_ELT (ce->value, i));
18461 }
18462 else
18463 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18464 }
18465 }
18466
18467 r = build_constructor (init_list_type_node, n);
18468 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18469
18470 if (TREE_HAS_CONSTRUCTOR (t))
18471 {
18472 fcl_t cl = fcl_functional;
18473 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18474 cl = fcl_c99;
18475 RETURN (finish_compound_literal (type, r, complain, cl));
18476 }
18477
18478 TREE_TYPE (r) = type;
18479 RETURN (r);
18480 }
18481
18482 case TYPEID_EXPR:
18483 {
18484 tree operand_0 = TREE_OPERAND (t, 0);
18485 if (TYPE_P (operand_0))
18486 {
18487 operand_0 = tsubst (operand_0, args, complain, in_decl);
18488 RETURN (get_typeid (operand_0, complain));
18489 }
18490 else
18491 {
18492 operand_0 = RECUR (operand_0);
18493 RETURN (build_typeid (operand_0, complain));
18494 }
18495 }
18496
18497 case VAR_DECL:
18498 if (!args)
18499 RETURN (t);
18500 /* Fall through */
18501
18502 case PARM_DECL:
18503 {
18504 tree r = tsubst_copy (t, args, complain, in_decl);
18505 /* ??? We're doing a subset of finish_id_expression here. */
18506 if (VAR_P (r)
18507 && !processing_template_decl
18508 && !cp_unevaluated_operand
18509 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18510 && CP_DECL_THREAD_LOCAL_P (r))
18511 {
18512 if (tree wrap = get_tls_wrapper_fn (r))
18513 /* Replace an evaluated use of the thread_local variable with
18514 a call to its wrapper. */
18515 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18516 }
18517 else if (outer_automatic_var_p (r))
18518 r = process_outer_var_ref (r, complain);
18519
18520 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18521 /* If the original type was a reference, we'll be wrapped in
18522 the appropriate INDIRECT_REF. */
18523 r = convert_from_reference (r);
18524 RETURN (r);
18525 }
18526
18527 case VA_ARG_EXPR:
18528 {
18529 tree op0 = RECUR (TREE_OPERAND (t, 0));
18530 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18531 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18532 }
18533
18534 case OFFSETOF_EXPR:
18535 {
18536 tree object_ptr
18537 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18538 in_decl, /*function_p=*/false,
18539 /*integral_constant_expression_p=*/false);
18540 RETURN (finish_offsetof (object_ptr,
18541 RECUR (TREE_OPERAND (t, 0)),
18542 EXPR_LOCATION (t)));
18543 }
18544
18545 case ADDRESSOF_EXPR:
18546 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18547 RECUR (TREE_OPERAND (t, 0)), complain));
18548
18549 case TRAIT_EXPR:
18550 {
18551 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18552 complain, in_decl);
18553
18554 tree type2 = TRAIT_EXPR_TYPE2 (t);
18555 if (type2 && TREE_CODE (type2) == TREE_LIST)
18556 type2 = RECUR (type2);
18557 else if (type2)
18558 type2 = tsubst (type2, args, complain, in_decl);
18559
18560 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18561 }
18562
18563 case STMT_EXPR:
18564 {
18565 tree old_stmt_expr = cur_stmt_expr;
18566 tree stmt_expr = begin_stmt_expr ();
18567
18568 cur_stmt_expr = stmt_expr;
18569 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18570 integral_constant_expression_p);
18571 stmt_expr = finish_stmt_expr (stmt_expr, false);
18572 cur_stmt_expr = old_stmt_expr;
18573
18574 /* If the resulting list of expression statement is empty,
18575 fold it further into void_node. */
18576 if (empty_expr_stmt_p (stmt_expr))
18577 stmt_expr = void_node;
18578
18579 RETURN (stmt_expr);
18580 }
18581
18582 case LAMBDA_EXPR:
18583 {
18584 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18585
18586 RETURN (build_lambda_object (r));
18587 }
18588
18589 case TARGET_EXPR:
18590 /* We can get here for a constant initializer of non-dependent type.
18591 FIXME stop folding in cp_parser_initializer_clause. */
18592 {
18593 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18594 complain);
18595 RETURN (r);
18596 }
18597
18598 case TRANSACTION_EXPR:
18599 RETURN (tsubst_expr(t, args, complain, in_decl,
18600 integral_constant_expression_p));
18601
18602 case PAREN_EXPR:
18603 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18604
18605 case VEC_PERM_EXPR:
18606 {
18607 tree op0 = RECUR (TREE_OPERAND (t, 0));
18608 tree op1 = RECUR (TREE_OPERAND (t, 1));
18609 tree op2 = RECUR (TREE_OPERAND (t, 2));
18610 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18611 complain));
18612 }
18613
18614 case REQUIRES_EXPR:
18615 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18616
18617 case NON_LVALUE_EXPR:
18618 case VIEW_CONVERT_EXPR:
18619 /* We should only see these for location wrapper nodes, or within
18620 instantiate_non_dependent_expr (when args is NULL_TREE). */
18621 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18622 if (location_wrapper_p (t))
18623 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18624 EXPR_LOCATION (t)));
18625 /* fallthrough. */
18626
18627 default:
18628 /* Handle Objective-C++ constructs, if appropriate. */
18629 {
18630 tree subst
18631 = objcp_tsubst_copy_and_build (t, args, complain,
18632 in_decl, /*function_p=*/false);
18633 if (subst)
18634 RETURN (subst);
18635 }
18636 RETURN (tsubst_copy (t, args, complain, in_decl));
18637 }
18638
18639 #undef RECUR
18640 #undef RETURN
18641 out:
18642 input_location = loc;
18643 return retval;
18644 }
18645
18646 /* Verify that the instantiated ARGS are valid. For type arguments,
18647 make sure that the type's linkage is ok. For non-type arguments,
18648 make sure they are constants if they are integral or enumerations.
18649 Emit an error under control of COMPLAIN, and return TRUE on error. */
18650
18651 static bool
18652 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18653 {
18654 if (dependent_template_arg_p (t))
18655 return false;
18656 if (ARGUMENT_PACK_P (t))
18657 {
18658 tree vec = ARGUMENT_PACK_ARGS (t);
18659 int len = TREE_VEC_LENGTH (vec);
18660 bool result = false;
18661 int i;
18662
18663 for (i = 0; i < len; ++i)
18664 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18665 result = true;
18666 return result;
18667 }
18668 else if (TYPE_P (t))
18669 {
18670 /* [basic.link]: A name with no linkage (notably, the name
18671 of a class or enumeration declared in a local scope)
18672 shall not be used to declare an entity with linkage.
18673 This implies that names with no linkage cannot be used as
18674 template arguments
18675
18676 DR 757 relaxes this restriction for C++0x. */
18677 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18678 : no_linkage_check (t, /*relaxed_p=*/false));
18679
18680 if (nt)
18681 {
18682 /* DR 488 makes use of a type with no linkage cause
18683 type deduction to fail. */
18684 if (complain & tf_error)
18685 {
18686 if (TYPE_UNNAMED_P (nt))
18687 error ("%qT is/uses unnamed type", t);
18688 else
18689 error ("template argument for %qD uses local type %qT",
18690 tmpl, t);
18691 }
18692 return true;
18693 }
18694 /* In order to avoid all sorts of complications, we do not
18695 allow variably-modified types as template arguments. */
18696 else if (variably_modified_type_p (t, NULL_TREE))
18697 {
18698 if (complain & tf_error)
18699 error ("%qT is a variably modified type", t);
18700 return true;
18701 }
18702 }
18703 /* Class template and alias template arguments should be OK. */
18704 else if (DECL_TYPE_TEMPLATE_P (t))
18705 ;
18706 /* A non-type argument of integral or enumerated type must be a
18707 constant. */
18708 else if (TREE_TYPE (t)
18709 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18710 && !REFERENCE_REF_P (t)
18711 && !TREE_CONSTANT (t))
18712 {
18713 if (complain & tf_error)
18714 error ("integral expression %qE is not constant", t);
18715 return true;
18716 }
18717 return false;
18718 }
18719
18720 static bool
18721 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18722 {
18723 int ix, len = DECL_NTPARMS (tmpl);
18724 bool result = false;
18725
18726 for (ix = 0; ix != len; ix++)
18727 {
18728 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18729 result = true;
18730 }
18731 if (result && (complain & tf_error))
18732 error (" trying to instantiate %qD", tmpl);
18733 return result;
18734 }
18735
18736 /* We're out of SFINAE context now, so generate diagnostics for the access
18737 errors we saw earlier when instantiating D from TMPL and ARGS. */
18738
18739 static void
18740 recheck_decl_substitution (tree d, tree tmpl, tree args)
18741 {
18742 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18743 tree type = TREE_TYPE (pattern);
18744 location_t loc = input_location;
18745
18746 push_access_scope (d);
18747 push_deferring_access_checks (dk_no_deferred);
18748 input_location = DECL_SOURCE_LOCATION (pattern);
18749 tsubst (type, args, tf_warning_or_error, d);
18750 input_location = loc;
18751 pop_deferring_access_checks ();
18752 pop_access_scope (d);
18753 }
18754
18755 /* Instantiate the indicated variable, function, or alias template TMPL with
18756 the template arguments in TARG_PTR. */
18757
18758 static tree
18759 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18760 {
18761 tree targ_ptr = orig_args;
18762 tree fndecl;
18763 tree gen_tmpl;
18764 tree spec;
18765 bool access_ok = true;
18766
18767 if (tmpl == error_mark_node)
18768 return error_mark_node;
18769
18770 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18771
18772 /* If this function is a clone, handle it specially. */
18773 if (DECL_CLONED_FUNCTION_P (tmpl))
18774 {
18775 tree spec;
18776 tree clone;
18777
18778 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18779 DECL_CLONED_FUNCTION. */
18780 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18781 targ_ptr, complain);
18782 if (spec == error_mark_node)
18783 return error_mark_node;
18784
18785 /* Look for the clone. */
18786 FOR_EACH_CLONE (clone, spec)
18787 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18788 return clone;
18789 /* We should always have found the clone by now. */
18790 gcc_unreachable ();
18791 return NULL_TREE;
18792 }
18793
18794 if (targ_ptr == error_mark_node)
18795 return error_mark_node;
18796
18797 /* Check to see if we already have this specialization. */
18798 gen_tmpl = most_general_template (tmpl);
18799 if (TMPL_ARGS_DEPTH (targ_ptr)
18800 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18801 /* targ_ptr only has the innermost template args, so add the outer ones
18802 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18803 the case of a non-dependent call within a template definition). */
18804 targ_ptr = (add_outermost_template_args
18805 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18806 targ_ptr));
18807
18808 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18809 but it doesn't seem to be on the hot path. */
18810 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18811
18812 gcc_assert (tmpl == gen_tmpl
18813 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18814 == spec)
18815 || fndecl == NULL_TREE);
18816
18817 if (spec != NULL_TREE)
18818 {
18819 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18820 {
18821 if (complain & tf_error)
18822 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18823 return error_mark_node;
18824 }
18825 return spec;
18826 }
18827
18828 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18829 complain))
18830 return error_mark_node;
18831
18832 /* We are building a FUNCTION_DECL, during which the access of its
18833 parameters and return types have to be checked. However this
18834 FUNCTION_DECL which is the desired context for access checking
18835 is not built yet. We solve this chicken-and-egg problem by
18836 deferring all checks until we have the FUNCTION_DECL. */
18837 push_deferring_access_checks (dk_deferred);
18838
18839 /* Instantiation of the function happens in the context of the function
18840 template, not the context of the overload resolution we're doing. */
18841 push_to_top_level ();
18842 /* If there are dependent arguments, e.g. because we're doing partial
18843 ordering, make sure processing_template_decl stays set. */
18844 if (uses_template_parms (targ_ptr))
18845 ++processing_template_decl;
18846 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18847 {
18848 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18849 complain, gen_tmpl, true);
18850 push_nested_class (ctx);
18851 }
18852
18853 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18854
18855 fndecl = NULL_TREE;
18856 if (VAR_P (pattern))
18857 {
18858 /* We need to determine if we're using a partial or explicit
18859 specialization now, because the type of the variable could be
18860 different. */
18861 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18862 tree elt = most_specialized_partial_spec (tid, complain);
18863 if (elt == error_mark_node)
18864 pattern = error_mark_node;
18865 else if (elt)
18866 {
18867 tree partial_tmpl = TREE_VALUE (elt);
18868 tree partial_args = TREE_PURPOSE (elt);
18869 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18870 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18871 }
18872 }
18873
18874 /* Substitute template parameters to obtain the specialization. */
18875 if (fndecl == NULL_TREE)
18876 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18877 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18878 pop_nested_class ();
18879 pop_from_top_level ();
18880
18881 if (fndecl == error_mark_node)
18882 {
18883 pop_deferring_access_checks ();
18884 return error_mark_node;
18885 }
18886
18887 /* The DECL_TI_TEMPLATE should always be the immediate parent
18888 template, not the most general template. */
18889 DECL_TI_TEMPLATE (fndecl) = tmpl;
18890 DECL_TI_ARGS (fndecl) = targ_ptr;
18891
18892 /* Now we know the specialization, compute access previously
18893 deferred. Do no access control for inheriting constructors,
18894 as we already checked access for the inherited constructor. */
18895 if (!(flag_new_inheriting_ctors
18896 && DECL_INHERITED_CTOR (fndecl)))
18897 {
18898 push_access_scope (fndecl);
18899 if (!perform_deferred_access_checks (complain))
18900 access_ok = false;
18901 pop_access_scope (fndecl);
18902 }
18903 pop_deferring_access_checks ();
18904
18905 /* If we've just instantiated the main entry point for a function,
18906 instantiate all the alternate entry points as well. We do this
18907 by cloning the instantiation of the main entry point, not by
18908 instantiating the template clones. */
18909 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18910 clone_function_decl (fndecl, /*update_methods=*/false);
18911
18912 if (!access_ok)
18913 {
18914 if (!(complain & tf_error))
18915 {
18916 /* Remember to reinstantiate when we're out of SFINAE so the user
18917 can see the errors. */
18918 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18919 }
18920 return error_mark_node;
18921 }
18922 return fndecl;
18923 }
18924
18925 /* Wrapper for instantiate_template_1. */
18926
18927 tree
18928 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18929 {
18930 tree ret;
18931 timevar_push (TV_TEMPLATE_INST);
18932 ret = instantiate_template_1 (tmpl, orig_args, complain);
18933 timevar_pop (TV_TEMPLATE_INST);
18934 return ret;
18935 }
18936
18937 /* Instantiate the alias template TMPL with ARGS. Also push a template
18938 instantiation level, which instantiate_template doesn't do because
18939 functions and variables have sufficient context established by the
18940 callers. */
18941
18942 static tree
18943 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18944 {
18945 struct pending_template *old_last_pend = last_pending_template;
18946 struct tinst_level *old_error_tinst = last_error_tinst_level;
18947 if (tmpl == error_mark_node || args == error_mark_node)
18948 return error_mark_node;
18949 tree tinst = build_tree_list (tmpl, args);
18950 if (!push_tinst_level (tinst))
18951 {
18952 ggc_free (tinst);
18953 return error_mark_node;
18954 }
18955
18956 args =
18957 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18958 args, tmpl, complain,
18959 /*require_all_args=*/true,
18960 /*use_default_args=*/true);
18961
18962 tree r = instantiate_template (tmpl, args, complain);
18963 pop_tinst_level ();
18964 /* We can't free this if a pending_template entry or last_error_tinst_level
18965 is pointing at it. */
18966 if (last_pending_template == old_last_pend
18967 && last_error_tinst_level == old_error_tinst)
18968 ggc_free (tinst);
18969
18970 return r;
18971 }
18972
18973 /* PARM is a template parameter pack for FN. Returns true iff
18974 PARM is used in a deducible way in the argument list of FN. */
18975
18976 static bool
18977 pack_deducible_p (tree parm, tree fn)
18978 {
18979 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18980 for (; t; t = TREE_CHAIN (t))
18981 {
18982 tree type = TREE_VALUE (t);
18983 tree packs;
18984 if (!PACK_EXPANSION_P (type))
18985 continue;
18986 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18987 packs; packs = TREE_CHAIN (packs))
18988 if (template_args_equal (TREE_VALUE (packs), parm))
18989 {
18990 /* The template parameter pack is used in a function parameter
18991 pack. If this is the end of the parameter list, the
18992 template parameter pack is deducible. */
18993 if (TREE_CHAIN (t) == void_list_node)
18994 return true;
18995 else
18996 /* Otherwise, not. Well, it could be deduced from
18997 a non-pack parameter, but doing so would end up with
18998 a deduction mismatch, so don't bother. */
18999 return false;
19000 }
19001 }
19002 /* The template parameter pack isn't used in any function parameter
19003 packs, but it might be used deeper, e.g. tuple<Args...>. */
19004 return true;
19005 }
19006
19007 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19008 NARGS elements of the arguments that are being used when calling
19009 it. TARGS is a vector into which the deduced template arguments
19010 are placed.
19011
19012 Returns either a FUNCTION_DECL for the matching specialization of FN or
19013 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19014 true, diagnostics will be printed to explain why it failed.
19015
19016 If FN is a conversion operator, or we are trying to produce a specific
19017 specialization, RETURN_TYPE is the return type desired.
19018
19019 The EXPLICIT_TARGS are explicit template arguments provided via a
19020 template-id.
19021
19022 The parameter STRICT is one of:
19023
19024 DEDUCE_CALL:
19025 We are deducing arguments for a function call, as in
19026 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19027 deducing arguments for a call to the result of a conversion
19028 function template, as in [over.call.object].
19029
19030 DEDUCE_CONV:
19031 We are deducing arguments for a conversion function, as in
19032 [temp.deduct.conv].
19033
19034 DEDUCE_EXACT:
19035 We are deducing arguments when doing an explicit instantiation
19036 as in [temp.explicit], when determining an explicit specialization
19037 as in [temp.expl.spec], or when taking the address of a function
19038 template, as in [temp.deduct.funcaddr]. */
19039
19040 tree
19041 fn_type_unification (tree fn,
19042 tree explicit_targs,
19043 tree targs,
19044 const tree *args,
19045 unsigned int nargs,
19046 tree return_type,
19047 unification_kind_t strict,
19048 int flags,
19049 bool explain_p,
19050 bool decltype_p)
19051 {
19052 tree parms;
19053 tree fntype;
19054 tree decl = NULL_TREE;
19055 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19056 bool ok;
19057 static int deduction_depth;
19058 struct pending_template *old_last_pend = last_pending_template;
19059 struct tinst_level *old_error_tinst = last_error_tinst_level;
19060
19061 tree orig_fn = fn;
19062 if (flag_new_inheriting_ctors)
19063 fn = strip_inheriting_ctors (fn);
19064
19065 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
19066 tree tinst;
19067 tree r = error_mark_node;
19068
19069 tree full_targs = targs;
19070 if (TMPL_ARGS_DEPTH (targs)
19071 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
19072 full_targs = (add_outermost_template_args
19073 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
19074 targs));
19075
19076 if (decltype_p)
19077 complain |= tf_decltype;
19078
19079 /* In C++0x, it's possible to have a function template whose type depends
19080 on itself recursively. This is most obvious with decltype, but can also
19081 occur with enumeration scope (c++/48969). So we need to catch infinite
19082 recursion and reject the substitution at deduction time; this function
19083 will return error_mark_node for any repeated substitution.
19084
19085 This also catches excessive recursion such as when f<N> depends on
19086 f<N-1> across all integers, and returns error_mark_node for all the
19087 substitutions back up to the initial one.
19088
19089 This is, of course, not reentrant. */
19090 if (excessive_deduction_depth)
19091 return error_mark_node;
19092 tinst = build_tree_list (fn, NULL_TREE);
19093 ++deduction_depth;
19094
19095 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19096
19097 fntype = TREE_TYPE (fn);
19098 if (explicit_targs)
19099 {
19100 /* [temp.deduct]
19101
19102 The specified template arguments must match the template
19103 parameters in kind (i.e., type, nontype, template), and there
19104 must not be more arguments than there are parameters;
19105 otherwise type deduction fails.
19106
19107 Nontype arguments must match the types of the corresponding
19108 nontype template parameters, or must be convertible to the
19109 types of the corresponding nontype parameters as specified in
19110 _temp.arg.nontype_, otherwise type deduction fails.
19111
19112 All references in the function type of the function template
19113 to the corresponding template parameters are replaced by the
19114 specified template argument values. If a substitution in a
19115 template parameter or in the function type of the function
19116 template results in an invalid type, type deduction fails. */
19117 int i, len = TREE_VEC_LENGTH (tparms);
19118 location_t loc = input_location;
19119 bool incomplete = false;
19120
19121 if (explicit_targs == error_mark_node)
19122 goto fail;
19123
19124 if (TMPL_ARGS_DEPTH (explicit_targs)
19125 < TMPL_ARGS_DEPTH (full_targs))
19126 explicit_targs = add_outermost_template_args (full_targs,
19127 explicit_targs);
19128
19129 /* Adjust any explicit template arguments before entering the
19130 substitution context. */
19131 explicit_targs
19132 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19133 complain,
19134 /*require_all_args=*/false,
19135 /*use_default_args=*/false));
19136 if (explicit_targs == error_mark_node)
19137 goto fail;
19138
19139 /* Substitute the explicit args into the function type. This is
19140 necessary so that, for instance, explicitly declared function
19141 arguments can match null pointed constants. If we were given
19142 an incomplete set of explicit args, we must not do semantic
19143 processing during substitution as we could create partial
19144 instantiations. */
19145 for (i = 0; i < len; i++)
19146 {
19147 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19148 bool parameter_pack = false;
19149 tree targ = TREE_VEC_ELT (explicit_targs, i);
19150
19151 /* Dig out the actual parm. */
19152 if (TREE_CODE (parm) == TYPE_DECL
19153 || TREE_CODE (parm) == TEMPLATE_DECL)
19154 {
19155 parm = TREE_TYPE (parm);
19156 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19157 }
19158 else if (TREE_CODE (parm) == PARM_DECL)
19159 {
19160 parm = DECL_INITIAL (parm);
19161 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19162 }
19163
19164 if (!parameter_pack && targ == NULL_TREE)
19165 /* No explicit argument for this template parameter. */
19166 incomplete = true;
19167
19168 if (parameter_pack && pack_deducible_p (parm, fn))
19169 {
19170 /* Mark the argument pack as "incomplete". We could
19171 still deduce more arguments during unification.
19172 We remove this mark in type_unification_real. */
19173 if (targ)
19174 {
19175 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19176 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19177 = ARGUMENT_PACK_ARGS (targ);
19178 }
19179
19180 /* We have some incomplete argument packs. */
19181 incomplete = true;
19182 }
19183 }
19184
19185 TREE_VALUE (tinst) = explicit_targs;
19186 if (!push_tinst_level (tinst))
19187 {
19188 excessive_deduction_depth = true;
19189 goto fail;
19190 }
19191 processing_template_decl += incomplete;
19192 input_location = DECL_SOURCE_LOCATION (fn);
19193 /* Ignore any access checks; we'll see them again in
19194 instantiate_template and they might have the wrong
19195 access path at this point. */
19196 push_deferring_access_checks (dk_deferred);
19197 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19198 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19199 pop_deferring_access_checks ();
19200 input_location = loc;
19201 processing_template_decl -= incomplete;
19202 pop_tinst_level ();
19203
19204 if (fntype == error_mark_node)
19205 goto fail;
19206
19207 /* Place the explicitly specified arguments in TARGS. */
19208 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19209 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19210 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19211 }
19212
19213 /* Never do unification on the 'this' parameter. */
19214 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19215
19216 if (return_type && strict == DEDUCE_CALL)
19217 {
19218 /* We're deducing for a call to the result of a template conversion
19219 function. The parms we really want are in return_type. */
19220 if (POINTER_TYPE_P (return_type))
19221 return_type = TREE_TYPE (return_type);
19222 parms = TYPE_ARG_TYPES (return_type);
19223 }
19224 else if (return_type)
19225 {
19226 tree *new_args;
19227
19228 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19229 new_args = XALLOCAVEC (tree, nargs + 1);
19230 new_args[0] = return_type;
19231 memcpy (new_args + 1, args, nargs * sizeof (tree));
19232 args = new_args;
19233 ++nargs;
19234 }
19235
19236 /* We allow incomplete unification without an error message here
19237 because the standard doesn't seem to explicitly prohibit it. Our
19238 callers must be ready to deal with unification failures in any
19239 event. */
19240
19241 TREE_VALUE (tinst) = targs;
19242 /* If we aren't explaining yet, push tinst context so we can see where
19243 any errors (e.g. from class instantiations triggered by instantiation
19244 of default template arguments) come from. If we are explaining, this
19245 context is redundant. */
19246 if (!explain_p && !push_tinst_level (tinst))
19247 {
19248 excessive_deduction_depth = true;
19249 goto fail;
19250 }
19251
19252 /* type_unification_real will pass back any access checks from default
19253 template argument substitution. */
19254 vec<deferred_access_check, va_gc> *checks;
19255 checks = NULL;
19256
19257 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19258 full_targs, parms, args, nargs, /*subr=*/0,
19259 strict, flags, &checks, explain_p);
19260 if (!explain_p)
19261 pop_tinst_level ();
19262 if (!ok)
19263 goto fail;
19264
19265 /* Now that we have bindings for all of the template arguments,
19266 ensure that the arguments deduced for the template template
19267 parameters have compatible template parameter lists. We cannot
19268 check this property before we have deduced all template
19269 arguments, because the template parameter types of a template
19270 template parameter might depend on prior template parameters
19271 deduced after the template template parameter. The following
19272 ill-formed example illustrates this issue:
19273
19274 template<typename T, template<T> class C> void f(C<5>, T);
19275
19276 template<int N> struct X {};
19277
19278 void g() {
19279 f(X<5>(), 5l); // error: template argument deduction fails
19280 }
19281
19282 The template parameter list of 'C' depends on the template type
19283 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19284 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19285 time that we deduce 'C'. */
19286 if (!template_template_parm_bindings_ok_p
19287 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19288 {
19289 unify_inconsistent_template_template_parameters (explain_p);
19290 goto fail;
19291 }
19292
19293 /* All is well so far. Now, check:
19294
19295 [temp.deduct]
19296
19297 When all template arguments have been deduced, all uses of
19298 template parameters in nondeduced contexts are replaced with
19299 the corresponding deduced argument values. If the
19300 substitution results in an invalid type, as described above,
19301 type deduction fails. */
19302 TREE_VALUE (tinst) = targs;
19303 if (!push_tinst_level (tinst))
19304 {
19305 excessive_deduction_depth = true;
19306 goto fail;
19307 }
19308
19309 /* Also collect access checks from the instantiation. */
19310 reopen_deferring_access_checks (checks);
19311
19312 decl = instantiate_template (fn, targs, complain);
19313
19314 checks = get_deferred_access_checks ();
19315 pop_deferring_access_checks ();
19316
19317 pop_tinst_level ();
19318
19319 if (decl == error_mark_node)
19320 goto fail;
19321
19322 /* Now perform any access checks encountered during substitution. */
19323 push_access_scope (decl);
19324 ok = perform_access_checks (checks, complain);
19325 pop_access_scope (decl);
19326 if (!ok)
19327 goto fail;
19328
19329 /* If we're looking for an exact match, check that what we got
19330 is indeed an exact match. It might not be if some template
19331 parameters are used in non-deduced contexts. But don't check
19332 for an exact match if we have dependent template arguments;
19333 in that case we're doing partial ordering, and we already know
19334 that we have two candidates that will provide the actual type. */
19335 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19336 {
19337 tree substed = TREE_TYPE (decl);
19338 unsigned int i;
19339
19340 tree sarg
19341 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19342 if (return_type)
19343 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19344 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19345 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19346 {
19347 unify_type_mismatch (explain_p, args[i],
19348 TREE_VALUE (sarg));
19349 goto fail;
19350 }
19351 }
19352
19353 /* After doing deduction with the inherited constructor, actually return an
19354 instantiation of the inheriting constructor. */
19355 if (orig_fn != fn)
19356 decl = instantiate_template (orig_fn, targs, complain);
19357
19358 r = decl;
19359
19360 fail:
19361 --deduction_depth;
19362 if (excessive_deduction_depth)
19363 {
19364 if (deduction_depth == 0)
19365 /* Reset once we're all the way out. */
19366 excessive_deduction_depth = false;
19367 }
19368
19369 /* We can't free this if a pending_template entry or last_error_tinst_level
19370 is pointing at it. */
19371 if (last_pending_template == old_last_pend
19372 && last_error_tinst_level == old_error_tinst)
19373 ggc_free (tinst);
19374
19375 return r;
19376 }
19377
19378 /* Adjust types before performing type deduction, as described in
19379 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19380 sections are symmetric. PARM is the type of a function parameter
19381 or the return type of the conversion function. ARG is the type of
19382 the argument passed to the call, or the type of the value
19383 initialized with the result of the conversion function.
19384 ARG_EXPR is the original argument expression, which may be null. */
19385
19386 static int
19387 maybe_adjust_types_for_deduction (unification_kind_t strict,
19388 tree* parm,
19389 tree* arg,
19390 tree arg_expr)
19391 {
19392 int result = 0;
19393
19394 switch (strict)
19395 {
19396 case DEDUCE_CALL:
19397 break;
19398
19399 case DEDUCE_CONV:
19400 /* Swap PARM and ARG throughout the remainder of this
19401 function; the handling is precisely symmetric since PARM
19402 will initialize ARG rather than vice versa. */
19403 std::swap (parm, arg);
19404 break;
19405
19406 case DEDUCE_EXACT:
19407 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19408 too, but here handle it by stripping the reference from PARM
19409 rather than by adding it to ARG. */
19410 if (TREE_CODE (*parm) == REFERENCE_TYPE
19411 && TYPE_REF_IS_RVALUE (*parm)
19412 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19413 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19414 && TREE_CODE (*arg) == REFERENCE_TYPE
19415 && !TYPE_REF_IS_RVALUE (*arg))
19416 *parm = TREE_TYPE (*parm);
19417 /* Nothing else to do in this case. */
19418 return 0;
19419
19420 default:
19421 gcc_unreachable ();
19422 }
19423
19424 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19425 {
19426 /* [temp.deduct.call]
19427
19428 If P is not a reference type:
19429
19430 --If A is an array type, the pointer type produced by the
19431 array-to-pointer standard conversion (_conv.array_) is
19432 used in place of A for type deduction; otherwise,
19433
19434 --If A is a function type, the pointer type produced by
19435 the function-to-pointer standard conversion
19436 (_conv.func_) is used in place of A for type deduction;
19437 otherwise,
19438
19439 --If A is a cv-qualified type, the top level
19440 cv-qualifiers of A's type are ignored for type
19441 deduction. */
19442 if (TREE_CODE (*arg) == ARRAY_TYPE)
19443 *arg = build_pointer_type (TREE_TYPE (*arg));
19444 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19445 *arg = build_pointer_type (*arg);
19446 else
19447 *arg = TYPE_MAIN_VARIANT (*arg);
19448 }
19449
19450 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19451 reference to a cv-unqualified template parameter that does not represent a
19452 template parameter of a class template (during class template argument
19453 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19454 an lvalue, the type "lvalue reference to A" is used in place of A for type
19455 deduction. */
19456 if (TREE_CODE (*parm) == REFERENCE_TYPE
19457 && TYPE_REF_IS_RVALUE (*parm)
19458 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19459 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19460 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19461 && (arg_expr ? lvalue_p (arg_expr)
19462 /* try_one_overload doesn't provide an arg_expr, but
19463 functions are always lvalues. */
19464 : TREE_CODE (*arg) == FUNCTION_TYPE))
19465 *arg = build_reference_type (*arg);
19466
19467 /* [temp.deduct.call]
19468
19469 If P is a cv-qualified type, the top level cv-qualifiers
19470 of P's type are ignored for type deduction. If P is a
19471 reference type, the type referred to by P is used for
19472 type deduction. */
19473 *parm = TYPE_MAIN_VARIANT (*parm);
19474 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19475 {
19476 *parm = TREE_TYPE (*parm);
19477 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19478 }
19479
19480 /* DR 322. For conversion deduction, remove a reference type on parm
19481 too (which has been swapped into ARG). */
19482 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19483 *arg = TREE_TYPE (*arg);
19484
19485 return result;
19486 }
19487
19488 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19489 template which does contain any deducible template parameters; check if
19490 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19491 unify_one_argument. */
19492
19493 static int
19494 check_non_deducible_conversion (tree parm, tree arg, int strict,
19495 int flags, bool explain_p)
19496 {
19497 tree type;
19498
19499 if (!TYPE_P (arg))
19500 type = TREE_TYPE (arg);
19501 else
19502 type = arg;
19503
19504 if (same_type_p (parm, type))
19505 return unify_success (explain_p);
19506
19507 if (strict == DEDUCE_CONV)
19508 {
19509 if (can_convert_arg (type, parm, NULL_TREE, flags,
19510 explain_p ? tf_warning_or_error : tf_none))
19511 return unify_success (explain_p);
19512 }
19513 else if (strict != DEDUCE_EXACT)
19514 {
19515 if (can_convert_arg (parm, type,
19516 TYPE_P (arg) ? NULL_TREE : arg,
19517 flags, explain_p ? tf_warning_or_error : tf_none))
19518 return unify_success (explain_p);
19519 }
19520
19521 if (strict == DEDUCE_EXACT)
19522 return unify_type_mismatch (explain_p, parm, arg);
19523 else
19524 return unify_arg_conversion (explain_p, parm, type, arg);
19525 }
19526
19527 static bool uses_deducible_template_parms (tree type);
19528
19529 /* Returns true iff the expression EXPR is one from which a template
19530 argument can be deduced. In other words, if it's an undecorated
19531 use of a template non-type parameter. */
19532
19533 static bool
19534 deducible_expression (tree expr)
19535 {
19536 /* Strip implicit conversions. */
19537 while (CONVERT_EXPR_P (expr))
19538 expr = TREE_OPERAND (expr, 0);
19539 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19540 }
19541
19542 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19543 deducible way; that is, if it has a max value of <PARM> - 1. */
19544
19545 static bool
19546 deducible_array_bound (tree domain)
19547 {
19548 if (domain == NULL_TREE)
19549 return false;
19550
19551 tree max = TYPE_MAX_VALUE (domain);
19552 if (TREE_CODE (max) != MINUS_EXPR)
19553 return false;
19554
19555 return deducible_expression (TREE_OPERAND (max, 0));
19556 }
19557
19558 /* Returns true iff the template arguments ARGS use a template parameter
19559 in a deducible way. */
19560
19561 static bool
19562 deducible_template_args (tree args)
19563 {
19564 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19565 {
19566 bool deducible;
19567 tree elt = TREE_VEC_ELT (args, i);
19568 if (ARGUMENT_PACK_P (elt))
19569 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19570 else
19571 {
19572 if (PACK_EXPANSION_P (elt))
19573 elt = PACK_EXPANSION_PATTERN (elt);
19574 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19575 deducible = true;
19576 else if (TYPE_P (elt))
19577 deducible = uses_deducible_template_parms (elt);
19578 else
19579 deducible = deducible_expression (elt);
19580 }
19581 if (deducible)
19582 return true;
19583 }
19584 return false;
19585 }
19586
19587 /* Returns true iff TYPE contains any deducible references to template
19588 parameters, as per 14.8.2.5. */
19589
19590 static bool
19591 uses_deducible_template_parms (tree type)
19592 {
19593 if (PACK_EXPANSION_P (type))
19594 type = PACK_EXPANSION_PATTERN (type);
19595
19596 /* T
19597 cv-list T
19598 TT<T>
19599 TT<i>
19600 TT<> */
19601 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19602 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19603 return true;
19604
19605 /* T*
19606 T&
19607 T&& */
19608 if (POINTER_TYPE_P (type))
19609 return uses_deducible_template_parms (TREE_TYPE (type));
19610
19611 /* T[integer-constant ]
19612 type [i] */
19613 if (TREE_CODE (type) == ARRAY_TYPE)
19614 return (uses_deducible_template_parms (TREE_TYPE (type))
19615 || deducible_array_bound (TYPE_DOMAIN (type)));
19616
19617 /* T type ::*
19618 type T::*
19619 T T::*
19620 T (type ::*)()
19621 type (T::*)()
19622 type (type ::*)(T)
19623 type (T::*)(T)
19624 T (type ::*)(T)
19625 T (T::*)()
19626 T (T::*)(T) */
19627 if (TYPE_PTRMEM_P (type))
19628 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19629 || (uses_deducible_template_parms
19630 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19631
19632 /* template-name <T> (where template-name refers to a class template)
19633 template-name <i> (where template-name refers to a class template) */
19634 if (CLASS_TYPE_P (type)
19635 && CLASSTYPE_TEMPLATE_INFO (type)
19636 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19637 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19638 (CLASSTYPE_TI_ARGS (type)));
19639
19640 /* type (T)
19641 T()
19642 T(T) */
19643 if (TREE_CODE (type) == FUNCTION_TYPE
19644 || TREE_CODE (type) == METHOD_TYPE)
19645 {
19646 if (uses_deducible_template_parms (TREE_TYPE (type)))
19647 return true;
19648 tree parm = TYPE_ARG_TYPES (type);
19649 if (TREE_CODE (type) == METHOD_TYPE)
19650 parm = TREE_CHAIN (parm);
19651 for (; parm; parm = TREE_CHAIN (parm))
19652 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19653 return true;
19654 }
19655
19656 return false;
19657 }
19658
19659 /* Subroutine of type_unification_real and unify_pack_expansion to
19660 handle unification of a single P/A pair. Parameters are as
19661 for those functions. */
19662
19663 static int
19664 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19665 int subr, unification_kind_t strict,
19666 bool explain_p)
19667 {
19668 tree arg_expr = NULL_TREE;
19669 int arg_strict;
19670
19671 if (arg == error_mark_node || parm == error_mark_node)
19672 return unify_invalid (explain_p);
19673 if (arg == unknown_type_node)
19674 /* We can't deduce anything from this, but we might get all the
19675 template args from other function args. */
19676 return unify_success (explain_p);
19677
19678 /* Implicit conversions (Clause 4) will be performed on a function
19679 argument to convert it to the type of the corresponding function
19680 parameter if the parameter type contains no template-parameters that
19681 participate in template argument deduction. */
19682 if (strict != DEDUCE_EXACT
19683 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19684 /* For function parameters with no deducible template parameters,
19685 just return. We'll check non-dependent conversions later. */
19686 return unify_success (explain_p);
19687
19688 switch (strict)
19689 {
19690 case DEDUCE_CALL:
19691 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19692 | UNIFY_ALLOW_MORE_CV_QUAL
19693 | UNIFY_ALLOW_DERIVED);
19694 break;
19695
19696 case DEDUCE_CONV:
19697 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19698 break;
19699
19700 case DEDUCE_EXACT:
19701 arg_strict = UNIFY_ALLOW_NONE;
19702 break;
19703
19704 default:
19705 gcc_unreachable ();
19706 }
19707
19708 /* We only do these transformations if this is the top-level
19709 parameter_type_list in a call or declaration matching; in other
19710 situations (nested function declarators, template argument lists) we
19711 won't be comparing a type to an expression, and we don't do any type
19712 adjustments. */
19713 if (!subr)
19714 {
19715 if (!TYPE_P (arg))
19716 {
19717 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19718 if (type_unknown_p (arg))
19719 {
19720 /* [temp.deduct.type] A template-argument can be
19721 deduced from a pointer to function or pointer
19722 to member function argument if the set of
19723 overloaded functions does not contain function
19724 templates and at most one of a set of
19725 overloaded functions provides a unique
19726 match. */
19727 resolve_overloaded_unification (tparms, targs, parm,
19728 arg, strict,
19729 arg_strict, explain_p);
19730 /* If a unique match was not found, this is a
19731 non-deduced context, so we still succeed. */
19732 return unify_success (explain_p);
19733 }
19734
19735 arg_expr = arg;
19736 arg = unlowered_expr_type (arg);
19737 if (arg == error_mark_node)
19738 return unify_invalid (explain_p);
19739 }
19740
19741 arg_strict |=
19742 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19743 }
19744 else
19745 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19746 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19747 return unify_template_argument_mismatch (explain_p, parm, arg);
19748
19749 /* For deduction from an init-list we need the actual list. */
19750 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19751 arg = arg_expr;
19752 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19753 }
19754
19755 /* for_each_template_parm callback that always returns 0. */
19756
19757 static int
19758 zero_r (tree, void *)
19759 {
19760 return 0;
19761 }
19762
19763 /* for_each_template_parm any_fn callback to handle deduction of a template
19764 type argument from the type of an array bound. */
19765
19766 static int
19767 array_deduction_r (tree t, void *data)
19768 {
19769 tree_pair_p d = (tree_pair_p)data;
19770 tree &tparms = d->purpose;
19771 tree &targs = d->value;
19772
19773 if (TREE_CODE (t) == ARRAY_TYPE)
19774 if (tree dom = TYPE_DOMAIN (t))
19775 if (tree max = TYPE_MAX_VALUE (dom))
19776 {
19777 if (TREE_CODE (max) == MINUS_EXPR)
19778 max = TREE_OPERAND (max, 0);
19779 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19780 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19781 UNIFY_ALLOW_NONE, /*explain*/false);
19782 }
19783
19784 /* Keep walking. */
19785 return 0;
19786 }
19787
19788 /* Try to deduce any not-yet-deduced template type arguments from the type of
19789 an array bound. This is handled separately from unify because 14.8.2.5 says
19790 "The type of a type parameter is only deduced from an array bound if it is
19791 not otherwise deduced." */
19792
19793 static void
19794 try_array_deduction (tree tparms, tree targs, tree parm)
19795 {
19796 tree_pair_s data = { tparms, targs };
19797 hash_set<tree> visited;
19798 for_each_template_parm (parm, zero_r, &data, &visited,
19799 /*nondeduced*/false, array_deduction_r);
19800 }
19801
19802 /* Most parms like fn_type_unification.
19803
19804 If SUBR is 1, we're being called recursively (to unify the
19805 arguments of a function or method parameter of a function
19806 template).
19807
19808 CHECKS is a pointer to a vector of access checks encountered while
19809 substituting default template arguments. */
19810
19811 static int
19812 type_unification_real (tree tparms,
19813 tree full_targs,
19814 tree xparms,
19815 const tree *xargs,
19816 unsigned int xnargs,
19817 int subr,
19818 unification_kind_t strict,
19819 int flags,
19820 vec<deferred_access_check, va_gc> **checks,
19821 bool explain_p)
19822 {
19823 tree parm, arg;
19824 int i;
19825 int ntparms = TREE_VEC_LENGTH (tparms);
19826 int saw_undeduced = 0;
19827 tree parms;
19828 const tree *args;
19829 unsigned int nargs;
19830 unsigned int ia;
19831
19832 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19833 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19834 gcc_assert (ntparms > 0);
19835
19836 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19837
19838 /* Reset the number of non-defaulted template arguments contained
19839 in TARGS. */
19840 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19841
19842 again:
19843 parms = xparms;
19844 args = xargs;
19845 nargs = xnargs;
19846
19847 ia = 0;
19848 while (parms && parms != void_list_node
19849 && ia < nargs)
19850 {
19851 parm = TREE_VALUE (parms);
19852
19853 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19854 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19855 /* For a function parameter pack that occurs at the end of the
19856 parameter-declaration-list, the type A of each remaining
19857 argument of the call is compared with the type P of the
19858 declarator-id of the function parameter pack. */
19859 break;
19860
19861 parms = TREE_CHAIN (parms);
19862
19863 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19864 /* For a function parameter pack that does not occur at the
19865 end of the parameter-declaration-list, the type of the
19866 parameter pack is a non-deduced context. */
19867 continue;
19868
19869 arg = args[ia];
19870 ++ia;
19871
19872 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19873 explain_p))
19874 return 1;
19875 }
19876
19877 if (parms
19878 && parms != void_list_node
19879 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19880 {
19881 /* Unify the remaining arguments with the pack expansion type. */
19882 tree argvec;
19883 tree parmvec = make_tree_vec (1);
19884
19885 /* Allocate a TREE_VEC and copy in all of the arguments */
19886 argvec = make_tree_vec (nargs - ia);
19887 for (i = 0; ia < nargs; ++ia, ++i)
19888 TREE_VEC_ELT (argvec, i) = args[ia];
19889
19890 /* Copy the parameter into parmvec. */
19891 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19892 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19893 /*subr=*/subr, explain_p))
19894 return 1;
19895
19896 /* Advance to the end of the list of parameters. */
19897 parms = TREE_CHAIN (parms);
19898 }
19899
19900 /* Fail if we've reached the end of the parm list, and more args
19901 are present, and the parm list isn't variadic. */
19902 if (ia < nargs && parms == void_list_node)
19903 return unify_too_many_arguments (explain_p, nargs, ia);
19904 /* Fail if parms are left and they don't have default values and
19905 they aren't all deduced as empty packs (c++/57397). This is
19906 consistent with sufficient_parms_p. */
19907 if (parms && parms != void_list_node
19908 && TREE_PURPOSE (parms) == NULL_TREE)
19909 {
19910 unsigned int count = nargs;
19911 tree p = parms;
19912 bool type_pack_p;
19913 do
19914 {
19915 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19916 if (!type_pack_p)
19917 count++;
19918 p = TREE_CHAIN (p);
19919 }
19920 while (p && p != void_list_node);
19921 if (count != nargs)
19922 return unify_too_few_arguments (explain_p, ia, count,
19923 type_pack_p);
19924 }
19925
19926 if (!subr)
19927 {
19928 tsubst_flags_t complain = (explain_p
19929 ? tf_warning_or_error
19930 : tf_none);
19931 bool tried_array_deduction = (cxx_dialect < cxx17);
19932
19933 for (i = 0; i < ntparms; i++)
19934 {
19935 tree targ = TREE_VEC_ELT (targs, i);
19936 tree tparm = TREE_VEC_ELT (tparms, i);
19937
19938 /* Clear the "incomplete" flags on all argument packs now so that
19939 substituting them into later default arguments works. */
19940 if (targ && ARGUMENT_PACK_P (targ))
19941 {
19942 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19943 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19944 }
19945
19946 if (targ || tparm == error_mark_node)
19947 continue;
19948 tparm = TREE_VALUE (tparm);
19949
19950 if (TREE_CODE (tparm) == TYPE_DECL
19951 && !tried_array_deduction)
19952 {
19953 try_array_deduction (tparms, targs, xparms);
19954 tried_array_deduction = true;
19955 if (TREE_VEC_ELT (targs, i))
19956 continue;
19957 }
19958
19959 /* If this is an undeduced nontype parameter that depends on
19960 a type parameter, try another pass; its type may have been
19961 deduced from a later argument than the one from which
19962 this parameter can be deduced. */
19963 if (TREE_CODE (tparm) == PARM_DECL
19964 && uses_template_parms (TREE_TYPE (tparm))
19965 && saw_undeduced < 2)
19966 {
19967 saw_undeduced = 1;
19968 continue;
19969 }
19970
19971 /* Core issue #226 (C++0x) [temp.deduct]:
19972
19973 If a template argument has not been deduced, its
19974 default template argument, if any, is used.
19975
19976 When we are in C++98 mode, TREE_PURPOSE will either
19977 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19978 to explicitly check cxx_dialect here. */
19979 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19980 /* OK, there is a default argument. Wait until after the
19981 conversion check to do substitution. */
19982 continue;
19983
19984 /* If the type parameter is a parameter pack, then it will
19985 be deduced to an empty parameter pack. */
19986 if (template_parameter_pack_p (tparm))
19987 {
19988 tree arg;
19989
19990 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19991 {
19992 arg = make_node (NONTYPE_ARGUMENT_PACK);
19993 TREE_CONSTANT (arg) = 1;
19994 }
19995 else
19996 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19997
19998 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19999
20000 TREE_VEC_ELT (targs, i) = arg;
20001 continue;
20002 }
20003
20004 return unify_parameter_deduction_failure (explain_p, tparm);
20005 }
20006
20007 /* DR 1391: All parameters have args, now check non-dependent parms for
20008 convertibility. */
20009 if (saw_undeduced < 2)
20010 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
20011 parms && parms != void_list_node && ia < nargs; )
20012 {
20013 parm = TREE_VALUE (parms);
20014
20015 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20016 && (!TREE_CHAIN (parms)
20017 || TREE_CHAIN (parms) == void_list_node))
20018 /* For a function parameter pack that occurs at the end of the
20019 parameter-declaration-list, the type A of each remaining
20020 argument of the call is compared with the type P of the
20021 declarator-id of the function parameter pack. */
20022 break;
20023
20024 parms = TREE_CHAIN (parms);
20025
20026 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20027 /* For a function parameter pack that does not occur at the
20028 end of the parameter-declaration-list, the type of the
20029 parameter pack is a non-deduced context. */
20030 continue;
20031
20032 arg = args[ia];
20033 ++ia;
20034
20035 if (uses_template_parms (parm))
20036 continue;
20037 if (check_non_deducible_conversion (parm, arg, strict, flags,
20038 explain_p))
20039 return 1;
20040 }
20041
20042 /* Now substitute into the default template arguments. */
20043 for (i = 0; i < ntparms; i++)
20044 {
20045 tree targ = TREE_VEC_ELT (targs, i);
20046 tree tparm = TREE_VEC_ELT (tparms, i);
20047
20048 if (targ || tparm == error_mark_node)
20049 continue;
20050 tree parm = TREE_VALUE (tparm);
20051 tree arg = TREE_PURPOSE (tparm);
20052 reopen_deferring_access_checks (*checks);
20053 location_t save_loc = input_location;
20054 if (DECL_P (parm))
20055 input_location = DECL_SOURCE_LOCATION (parm);
20056 if (saw_undeduced == 1)
20057 ++processing_template_decl;
20058
20059 if (saw_undeduced == 1
20060 && TREE_CODE (parm) == PARM_DECL
20061 && uses_template_parms (TREE_TYPE (parm)))
20062 {
20063 /* The type of this non-type parameter depends on undeduced
20064 parameters. Don't try to use its default argument yet,
20065 but do check whether the arguments we already have cause
20066 substitution failure, so that that happens before we try
20067 later default arguments (78489). */
20068 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
20069 NULL_TREE);
20070 if (type == error_mark_node)
20071 arg = error_mark_node;
20072 else
20073 arg = NULL_TREE;
20074 }
20075 else
20076 {
20077 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
20078
20079 if (!uses_template_parms (arg))
20080 arg = convert_template_argument (parm, arg, full_targs,
20081 complain, i, NULL_TREE);
20082 else if (saw_undeduced == 1)
20083 arg = NULL_TREE;
20084 else
20085 arg = error_mark_node;
20086 }
20087
20088 if (saw_undeduced == 1)
20089 --processing_template_decl;
20090 input_location = save_loc;
20091 *checks = get_deferred_access_checks ();
20092 pop_deferring_access_checks ();
20093
20094 if (arg == error_mark_node)
20095 return 1;
20096 else if (arg)
20097 {
20098 TREE_VEC_ELT (targs, i) = arg;
20099 /* The position of the first default template argument,
20100 is also the number of non-defaulted arguments in TARGS.
20101 Record that. */
20102 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20103 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20104 }
20105 }
20106
20107 if (saw_undeduced++ == 1)
20108 goto again;
20109 }
20110
20111 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20112 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20113
20114 return unify_success (explain_p);
20115 }
20116
20117 /* Subroutine of type_unification_real. Args are like the variables
20118 at the call site. ARG is an overloaded function (or template-id);
20119 we try deducing template args from each of the overloads, and if
20120 only one succeeds, we go with that. Modifies TARGS and returns
20121 true on success. */
20122
20123 static bool
20124 resolve_overloaded_unification (tree tparms,
20125 tree targs,
20126 tree parm,
20127 tree arg,
20128 unification_kind_t strict,
20129 int sub_strict,
20130 bool explain_p)
20131 {
20132 tree tempargs = copy_node (targs);
20133 int good = 0;
20134 tree goodfn = NULL_TREE;
20135 bool addr_p;
20136
20137 if (TREE_CODE (arg) == ADDR_EXPR)
20138 {
20139 arg = TREE_OPERAND (arg, 0);
20140 addr_p = true;
20141 }
20142 else
20143 addr_p = false;
20144
20145 if (TREE_CODE (arg) == COMPONENT_REF)
20146 /* Handle `&x' where `x' is some static or non-static member
20147 function name. */
20148 arg = TREE_OPERAND (arg, 1);
20149
20150 if (TREE_CODE (arg) == OFFSET_REF)
20151 arg = TREE_OPERAND (arg, 1);
20152
20153 /* Strip baselink information. */
20154 if (BASELINK_P (arg))
20155 arg = BASELINK_FUNCTIONS (arg);
20156
20157 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20158 {
20159 /* If we got some explicit template args, we need to plug them into
20160 the affected templates before we try to unify, in case the
20161 explicit args will completely resolve the templates in question. */
20162
20163 int ok = 0;
20164 tree expl_subargs = TREE_OPERAND (arg, 1);
20165 arg = TREE_OPERAND (arg, 0);
20166
20167 for (lkp_iterator iter (arg); iter; ++iter)
20168 {
20169 tree fn = *iter;
20170 tree subargs, elem;
20171
20172 if (TREE_CODE (fn) != TEMPLATE_DECL)
20173 continue;
20174
20175 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20176 expl_subargs, NULL_TREE, tf_none,
20177 /*require_all_args=*/true,
20178 /*use_default_args=*/true);
20179 if (subargs != error_mark_node
20180 && !any_dependent_template_arguments_p (subargs))
20181 {
20182 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20183 if (try_one_overload (tparms, targs, tempargs, parm,
20184 elem, strict, sub_strict, addr_p, explain_p)
20185 && (!goodfn || !same_type_p (goodfn, elem)))
20186 {
20187 goodfn = elem;
20188 ++good;
20189 }
20190 }
20191 else if (subargs)
20192 ++ok;
20193 }
20194 /* If no templates (or more than one) are fully resolved by the
20195 explicit arguments, this template-id is a non-deduced context; it
20196 could still be OK if we deduce all template arguments for the
20197 enclosing call through other arguments. */
20198 if (good != 1)
20199 good = ok;
20200 }
20201 else if (TREE_CODE (arg) != OVERLOAD
20202 && TREE_CODE (arg) != FUNCTION_DECL)
20203 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20204 -- but the deduction does not succeed because the expression is
20205 not just the function on its own. */
20206 return false;
20207 else
20208 for (lkp_iterator iter (arg); iter; ++iter)
20209 {
20210 tree fn = *iter;
20211 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20212 strict, sub_strict, addr_p, explain_p)
20213 && (!goodfn || !decls_match (goodfn, fn)))
20214 {
20215 goodfn = fn;
20216 ++good;
20217 }
20218 }
20219
20220 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20221 to function or pointer to member function argument if the set of
20222 overloaded functions does not contain function templates and at most
20223 one of a set of overloaded functions provides a unique match.
20224
20225 So if we found multiple possibilities, we return success but don't
20226 deduce anything. */
20227
20228 if (good == 1)
20229 {
20230 int i = TREE_VEC_LENGTH (targs);
20231 for (; i--; )
20232 if (TREE_VEC_ELT (tempargs, i))
20233 {
20234 tree old = TREE_VEC_ELT (targs, i);
20235 tree new_ = TREE_VEC_ELT (tempargs, i);
20236 if (new_ && old && ARGUMENT_PACK_P (old)
20237 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20238 /* Don't forget explicit template arguments in a pack. */
20239 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20240 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20241 TREE_VEC_ELT (targs, i) = new_;
20242 }
20243 }
20244 if (good)
20245 return true;
20246
20247 return false;
20248 }
20249
20250 /* Core DR 115: In contexts where deduction is done and fails, or in
20251 contexts where deduction is not done, if a template argument list is
20252 specified and it, along with any default template arguments, identifies
20253 a single function template specialization, then the template-id is an
20254 lvalue for the function template specialization. */
20255
20256 tree
20257 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20258 {
20259 tree expr, offset, baselink;
20260 bool addr;
20261
20262 if (!type_unknown_p (orig_expr))
20263 return orig_expr;
20264
20265 expr = orig_expr;
20266 addr = false;
20267 offset = NULL_TREE;
20268 baselink = NULL_TREE;
20269
20270 if (TREE_CODE (expr) == ADDR_EXPR)
20271 {
20272 expr = TREE_OPERAND (expr, 0);
20273 addr = true;
20274 }
20275 if (TREE_CODE (expr) == OFFSET_REF)
20276 {
20277 offset = expr;
20278 expr = TREE_OPERAND (expr, 1);
20279 }
20280 if (BASELINK_P (expr))
20281 {
20282 baselink = expr;
20283 expr = BASELINK_FUNCTIONS (expr);
20284 }
20285
20286 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20287 {
20288 int good = 0;
20289 tree goodfn = NULL_TREE;
20290
20291 /* If we got some explicit template args, we need to plug them into
20292 the affected templates before we try to unify, in case the
20293 explicit args will completely resolve the templates in question. */
20294
20295 tree expl_subargs = TREE_OPERAND (expr, 1);
20296 tree arg = TREE_OPERAND (expr, 0);
20297 tree badfn = NULL_TREE;
20298 tree badargs = NULL_TREE;
20299
20300 for (lkp_iterator iter (arg); iter; ++iter)
20301 {
20302 tree fn = *iter;
20303 tree subargs, elem;
20304
20305 if (TREE_CODE (fn) != TEMPLATE_DECL)
20306 continue;
20307
20308 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20309 expl_subargs, NULL_TREE, tf_none,
20310 /*require_all_args=*/true,
20311 /*use_default_args=*/true);
20312 if (subargs != error_mark_node
20313 && !any_dependent_template_arguments_p (subargs))
20314 {
20315 elem = instantiate_template (fn, subargs, tf_none);
20316 if (elem == error_mark_node)
20317 {
20318 badfn = fn;
20319 badargs = subargs;
20320 }
20321 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20322 {
20323 goodfn = elem;
20324 ++good;
20325 }
20326 }
20327 }
20328 if (good == 1)
20329 {
20330 mark_used (goodfn);
20331 expr = goodfn;
20332 if (baselink)
20333 expr = build_baselink (BASELINK_BINFO (baselink),
20334 BASELINK_ACCESS_BINFO (baselink),
20335 expr, BASELINK_OPTYPE (baselink));
20336 if (offset)
20337 {
20338 tree base
20339 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20340 expr = build_offset_ref (base, expr, addr, complain);
20341 }
20342 if (addr)
20343 expr = cp_build_addr_expr (expr, complain);
20344 return expr;
20345 }
20346 else if (good == 0 && badargs && (complain & tf_error))
20347 /* There were no good options and at least one bad one, so let the
20348 user know what the problem is. */
20349 instantiate_template (badfn, badargs, complain);
20350 }
20351 return orig_expr;
20352 }
20353
20354 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20355 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20356 different overloads deduce different arguments for a given parm.
20357 ADDR_P is true if the expression for which deduction is being
20358 performed was of the form "& fn" rather than simply "fn".
20359
20360 Returns 1 on success. */
20361
20362 static int
20363 try_one_overload (tree tparms,
20364 tree orig_targs,
20365 tree targs,
20366 tree parm,
20367 tree arg,
20368 unification_kind_t strict,
20369 int sub_strict,
20370 bool addr_p,
20371 bool explain_p)
20372 {
20373 int nargs;
20374 tree tempargs;
20375 int i;
20376
20377 if (arg == error_mark_node)
20378 return 0;
20379
20380 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20381 to function or pointer to member function argument if the set of
20382 overloaded functions does not contain function templates and at most
20383 one of a set of overloaded functions provides a unique match.
20384
20385 So if this is a template, just return success. */
20386
20387 if (uses_template_parms (arg))
20388 return 1;
20389
20390 if (TREE_CODE (arg) == METHOD_TYPE)
20391 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20392 else if (addr_p)
20393 arg = build_pointer_type (arg);
20394
20395 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20396
20397 /* We don't copy orig_targs for this because if we have already deduced
20398 some template args from previous args, unify would complain when we
20399 try to deduce a template parameter for the same argument, even though
20400 there isn't really a conflict. */
20401 nargs = TREE_VEC_LENGTH (targs);
20402 tempargs = make_tree_vec (nargs);
20403
20404 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20405 return 0;
20406
20407 /* First make sure we didn't deduce anything that conflicts with
20408 explicitly specified args. */
20409 for (i = nargs; i--; )
20410 {
20411 tree elt = TREE_VEC_ELT (tempargs, i);
20412 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20413
20414 if (!elt)
20415 /*NOP*/;
20416 else if (uses_template_parms (elt))
20417 /* Since we're unifying against ourselves, we will fill in
20418 template args used in the function parm list with our own
20419 template parms. Discard them. */
20420 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20421 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20422 {
20423 /* Check that the argument at each index of the deduced argument pack
20424 is equivalent to the corresponding explicitly specified argument.
20425 We may have deduced more arguments than were explicitly specified,
20426 and that's OK. */
20427
20428 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20429 that's wrong if we deduce the same argument pack from multiple
20430 function arguments: it's only incomplete the first time. */
20431
20432 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20433 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20434
20435 if (TREE_VEC_LENGTH (deduced_pack)
20436 < TREE_VEC_LENGTH (explicit_pack))
20437 return 0;
20438
20439 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20440 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20441 TREE_VEC_ELT (deduced_pack, j)))
20442 return 0;
20443 }
20444 else if (oldelt && !template_args_equal (oldelt, elt))
20445 return 0;
20446 }
20447
20448 for (i = nargs; i--; )
20449 {
20450 tree elt = TREE_VEC_ELT (tempargs, i);
20451
20452 if (elt)
20453 TREE_VEC_ELT (targs, i) = elt;
20454 }
20455
20456 return 1;
20457 }
20458
20459 /* PARM is a template class (perhaps with unbound template
20460 parameters). ARG is a fully instantiated type. If ARG can be
20461 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20462 TARGS are as for unify. */
20463
20464 static tree
20465 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20466 bool explain_p)
20467 {
20468 tree copy_of_targs;
20469
20470 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20471 return NULL_TREE;
20472 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20473 /* Matches anything. */;
20474 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20475 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20476 return NULL_TREE;
20477
20478 /* We need to make a new template argument vector for the call to
20479 unify. If we used TARGS, we'd clutter it up with the result of
20480 the attempted unification, even if this class didn't work out.
20481 We also don't want to commit ourselves to all the unifications
20482 we've already done, since unification is supposed to be done on
20483 an argument-by-argument basis. In other words, consider the
20484 following pathological case:
20485
20486 template <int I, int J, int K>
20487 struct S {};
20488
20489 template <int I, int J>
20490 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20491
20492 template <int I, int J, int K>
20493 void f(S<I, J, K>, S<I, I, I>);
20494
20495 void g() {
20496 S<0, 0, 0> s0;
20497 S<0, 1, 2> s2;
20498
20499 f(s0, s2);
20500 }
20501
20502 Now, by the time we consider the unification involving `s2', we
20503 already know that we must have `f<0, 0, 0>'. But, even though
20504 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20505 because there are two ways to unify base classes of S<0, 1, 2>
20506 with S<I, I, I>. If we kept the already deduced knowledge, we
20507 would reject the possibility I=1. */
20508 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20509
20510 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20511 {
20512 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20513 return NULL_TREE;
20514 return arg;
20515 }
20516
20517 /* If unification failed, we're done. */
20518 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20519 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20520 return NULL_TREE;
20521
20522 return arg;
20523 }
20524
20525 /* Given a template type PARM and a class type ARG, find the unique
20526 base type in ARG that is an instance of PARM. We do not examine
20527 ARG itself; only its base-classes. If there is not exactly one
20528 appropriate base class, return NULL_TREE. PARM may be the type of
20529 a partial specialization, as well as a plain template type. Used
20530 by unify. */
20531
20532 static enum template_base_result
20533 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20534 bool explain_p, tree *result)
20535 {
20536 tree rval = NULL_TREE;
20537 tree binfo;
20538
20539 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20540
20541 binfo = TYPE_BINFO (complete_type (arg));
20542 if (!binfo)
20543 {
20544 /* The type could not be completed. */
20545 *result = NULL_TREE;
20546 return tbr_incomplete_type;
20547 }
20548
20549 /* Walk in inheritance graph order. The search order is not
20550 important, and this avoids multiple walks of virtual bases. */
20551 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20552 {
20553 tree r = try_class_unification (tparms, targs, parm,
20554 BINFO_TYPE (binfo), explain_p);
20555
20556 if (r)
20557 {
20558 /* If there is more than one satisfactory baseclass, then:
20559
20560 [temp.deduct.call]
20561
20562 If they yield more than one possible deduced A, the type
20563 deduction fails.
20564
20565 applies. */
20566 if (rval && !same_type_p (r, rval))
20567 {
20568 *result = NULL_TREE;
20569 return tbr_ambiguous_baseclass;
20570 }
20571
20572 rval = r;
20573 }
20574 }
20575
20576 *result = rval;
20577 return tbr_success;
20578 }
20579
20580 /* Returns the level of DECL, which declares a template parameter. */
20581
20582 static int
20583 template_decl_level (tree decl)
20584 {
20585 switch (TREE_CODE (decl))
20586 {
20587 case TYPE_DECL:
20588 case TEMPLATE_DECL:
20589 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20590
20591 case PARM_DECL:
20592 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20593
20594 default:
20595 gcc_unreachable ();
20596 }
20597 return 0;
20598 }
20599
20600 /* Decide whether ARG can be unified with PARM, considering only the
20601 cv-qualifiers of each type, given STRICT as documented for unify.
20602 Returns nonzero iff the unification is OK on that basis. */
20603
20604 static int
20605 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20606 {
20607 int arg_quals = cp_type_quals (arg);
20608 int parm_quals = cp_type_quals (parm);
20609
20610 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20611 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20612 {
20613 /* Although a CVR qualifier is ignored when being applied to a
20614 substituted template parameter ([8.3.2]/1 for example), that
20615 does not allow us to unify "const T" with "int&" because both
20616 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20617 It is ok when we're allowing additional CV qualifiers
20618 at the outer level [14.8.2.1]/3,1st bullet. */
20619 if ((TREE_CODE (arg) == REFERENCE_TYPE
20620 || TREE_CODE (arg) == FUNCTION_TYPE
20621 || TREE_CODE (arg) == METHOD_TYPE)
20622 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20623 return 0;
20624
20625 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20626 && (parm_quals & TYPE_QUAL_RESTRICT))
20627 return 0;
20628 }
20629
20630 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20631 && (arg_quals & parm_quals) != parm_quals)
20632 return 0;
20633
20634 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20635 && (parm_quals & arg_quals) != arg_quals)
20636 return 0;
20637
20638 return 1;
20639 }
20640
20641 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20642 void
20643 template_parm_level_and_index (tree parm, int* level, int* index)
20644 {
20645 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20646 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20647 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20648 {
20649 *index = TEMPLATE_TYPE_IDX (parm);
20650 *level = TEMPLATE_TYPE_LEVEL (parm);
20651 }
20652 else
20653 {
20654 *index = TEMPLATE_PARM_IDX (parm);
20655 *level = TEMPLATE_PARM_LEVEL (parm);
20656 }
20657 }
20658
20659 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20660 do { \
20661 if (unify (TP, TA, P, A, S, EP)) \
20662 return 1; \
20663 } while (0)
20664
20665 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20666 expansion at the end of PACKED_PARMS. Returns 0 if the type
20667 deduction succeeds, 1 otherwise. STRICT is the same as in
20668 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20669 function call argument list. We'll need to adjust the arguments to make them
20670 types. SUBR tells us if this is from a recursive call to
20671 type_unification_real, or for comparing two template argument
20672 lists. */
20673
20674 static int
20675 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20676 tree packed_args, unification_kind_t strict,
20677 bool subr, bool explain_p)
20678 {
20679 tree parm
20680 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20681 tree pattern = PACK_EXPANSION_PATTERN (parm);
20682 tree pack, packs = NULL_TREE;
20683 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20684
20685 /* Add in any args remembered from an earlier partial instantiation. */
20686 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20687 int levels = TMPL_ARGS_DEPTH (targs);
20688
20689 packed_args = expand_template_argument_pack (packed_args);
20690
20691 int len = TREE_VEC_LENGTH (packed_args);
20692
20693 /* Determine the parameter packs we will be deducing from the
20694 pattern, and record their current deductions. */
20695 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20696 pack; pack = TREE_CHAIN (pack))
20697 {
20698 tree parm_pack = TREE_VALUE (pack);
20699 int idx, level;
20700
20701 /* Only template parameter packs can be deduced, not e.g. function
20702 parameter packs or __bases or __integer_pack. */
20703 if (!TEMPLATE_PARM_P (parm_pack))
20704 continue;
20705
20706 /* Determine the index and level of this parameter pack. */
20707 template_parm_level_and_index (parm_pack, &level, &idx);
20708 if (level < levels)
20709 continue;
20710
20711 /* Keep track of the parameter packs and their corresponding
20712 argument packs. */
20713 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20714 TREE_TYPE (packs) = make_tree_vec (len - start);
20715 }
20716
20717 /* Loop through all of the arguments that have not yet been
20718 unified and unify each with the pattern. */
20719 for (i = start; i < len; i++)
20720 {
20721 tree parm;
20722 bool any_explicit = false;
20723 tree arg = TREE_VEC_ELT (packed_args, i);
20724
20725 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20726 or the element of its argument pack at the current index if
20727 this argument was explicitly specified. */
20728 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20729 {
20730 int idx, level;
20731 tree arg, pargs;
20732 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20733
20734 arg = NULL_TREE;
20735 if (TREE_VALUE (pack)
20736 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20737 && (i - start < TREE_VEC_LENGTH (pargs)))
20738 {
20739 any_explicit = true;
20740 arg = TREE_VEC_ELT (pargs, i - start);
20741 }
20742 TMPL_ARG (targs, level, idx) = arg;
20743 }
20744
20745 /* If we had explicit template arguments, substitute them into the
20746 pattern before deduction. */
20747 if (any_explicit)
20748 {
20749 /* Some arguments might still be unspecified or dependent. */
20750 bool dependent;
20751 ++processing_template_decl;
20752 dependent = any_dependent_template_arguments_p (targs);
20753 if (!dependent)
20754 --processing_template_decl;
20755 parm = tsubst (pattern, targs,
20756 explain_p ? tf_warning_or_error : tf_none,
20757 NULL_TREE);
20758 if (dependent)
20759 --processing_template_decl;
20760 if (parm == error_mark_node)
20761 return 1;
20762 }
20763 else
20764 parm = pattern;
20765
20766 /* Unify the pattern with the current argument. */
20767 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20768 explain_p))
20769 return 1;
20770
20771 /* For each parameter pack, collect the deduced value. */
20772 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20773 {
20774 int idx, level;
20775 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20776
20777 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20778 TMPL_ARG (targs, level, idx);
20779 }
20780 }
20781
20782 /* Verify that the results of unification with the parameter packs
20783 produce results consistent with what we've seen before, and make
20784 the deduced argument packs available. */
20785 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20786 {
20787 tree old_pack = TREE_VALUE (pack);
20788 tree new_args = TREE_TYPE (pack);
20789 int i, len = TREE_VEC_LENGTH (new_args);
20790 int idx, level;
20791 bool nondeduced_p = false;
20792
20793 /* By default keep the original deduced argument pack.
20794 If necessary, more specific code is going to update the
20795 resulting deduced argument later down in this function. */
20796 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20797 TMPL_ARG (targs, level, idx) = old_pack;
20798
20799 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20800 actually deduce anything. */
20801 for (i = 0; i < len && !nondeduced_p; ++i)
20802 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20803 nondeduced_p = true;
20804 if (nondeduced_p)
20805 continue;
20806
20807 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20808 {
20809 /* If we had fewer function args than explicit template args,
20810 just use the explicits. */
20811 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20812 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20813 if (len < explicit_len)
20814 new_args = explicit_args;
20815 }
20816
20817 if (!old_pack)
20818 {
20819 tree result;
20820 /* Build the deduced *_ARGUMENT_PACK. */
20821 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20822 {
20823 result = make_node (NONTYPE_ARGUMENT_PACK);
20824 TREE_CONSTANT (result) = 1;
20825 }
20826 else
20827 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20828
20829 SET_ARGUMENT_PACK_ARGS (result, new_args);
20830
20831 /* Note the deduced argument packs for this parameter
20832 pack. */
20833 TMPL_ARG (targs, level, idx) = result;
20834 }
20835 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20836 && (ARGUMENT_PACK_ARGS (old_pack)
20837 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20838 {
20839 /* We only had the explicitly-provided arguments before, but
20840 now we have a complete set of arguments. */
20841 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20842
20843 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20844 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20845 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20846 }
20847 else
20848 {
20849 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20850 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20851
20852 if (!comp_template_args (old_args, new_args,
20853 &bad_old_arg, &bad_new_arg))
20854 /* Inconsistent unification of this parameter pack. */
20855 return unify_parameter_pack_inconsistent (explain_p,
20856 bad_old_arg,
20857 bad_new_arg);
20858 }
20859 }
20860
20861 return unify_success (explain_p);
20862 }
20863
20864 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20865 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20866 parameters and return value are as for unify. */
20867
20868 static int
20869 unify_array_domain (tree tparms, tree targs,
20870 tree parm_dom, tree arg_dom,
20871 bool explain_p)
20872 {
20873 tree parm_max;
20874 tree arg_max;
20875 bool parm_cst;
20876 bool arg_cst;
20877
20878 /* Our representation of array types uses "N - 1" as the
20879 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20880 not an integer constant. We cannot unify arbitrarily
20881 complex expressions, so we eliminate the MINUS_EXPRs
20882 here. */
20883 parm_max = TYPE_MAX_VALUE (parm_dom);
20884 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20885 if (!parm_cst)
20886 {
20887 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20888 parm_max = TREE_OPERAND (parm_max, 0);
20889 }
20890 arg_max = TYPE_MAX_VALUE (arg_dom);
20891 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20892 if (!arg_cst)
20893 {
20894 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20895 trying to unify the type of a variable with the type
20896 of a template parameter. For example:
20897
20898 template <unsigned int N>
20899 void f (char (&) [N]);
20900 int g();
20901 void h(int i) {
20902 char a[g(i)];
20903 f(a);
20904 }
20905
20906 Here, the type of the ARG will be "int [g(i)]", and
20907 may be a SAVE_EXPR, etc. */
20908 if (TREE_CODE (arg_max) != MINUS_EXPR)
20909 return unify_vla_arg (explain_p, arg_dom);
20910 arg_max = TREE_OPERAND (arg_max, 0);
20911 }
20912
20913 /* If only one of the bounds used a MINUS_EXPR, compensate
20914 by adding one to the other bound. */
20915 if (parm_cst && !arg_cst)
20916 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20917 integer_type_node,
20918 parm_max,
20919 integer_one_node);
20920 else if (arg_cst && !parm_cst)
20921 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20922 integer_type_node,
20923 arg_max,
20924 integer_one_node);
20925
20926 return unify (tparms, targs, parm_max, arg_max,
20927 UNIFY_ALLOW_INTEGER, explain_p);
20928 }
20929
20930 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20931
20932 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20933
20934 static pa_kind_t
20935 pa_kind (tree t)
20936 {
20937 if (PACK_EXPANSION_P (t))
20938 t = PACK_EXPANSION_PATTERN (t);
20939 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20940 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20941 || DECL_TYPE_TEMPLATE_P (t))
20942 return pa_tmpl;
20943 else if (TYPE_P (t))
20944 return pa_type;
20945 else
20946 return pa_expr;
20947 }
20948
20949 /* Deduce the value of template parameters. TPARMS is the (innermost)
20950 set of template parameters to a template. TARGS is the bindings
20951 for those template parameters, as determined thus far; TARGS may
20952 include template arguments for outer levels of template parameters
20953 as well. PARM is a parameter to a template function, or a
20954 subcomponent of that parameter; ARG is the corresponding argument.
20955 This function attempts to match PARM with ARG in a manner
20956 consistent with the existing assignments in TARGS. If more values
20957 are deduced, then TARGS is updated.
20958
20959 Returns 0 if the type deduction succeeds, 1 otherwise. The
20960 parameter STRICT is a bitwise or of the following flags:
20961
20962 UNIFY_ALLOW_NONE:
20963 Require an exact match between PARM and ARG.
20964 UNIFY_ALLOW_MORE_CV_QUAL:
20965 Allow the deduced ARG to be more cv-qualified (by qualification
20966 conversion) than ARG.
20967 UNIFY_ALLOW_LESS_CV_QUAL:
20968 Allow the deduced ARG to be less cv-qualified than ARG.
20969 UNIFY_ALLOW_DERIVED:
20970 Allow the deduced ARG to be a template base class of ARG,
20971 or a pointer to a template base class of the type pointed to by
20972 ARG.
20973 UNIFY_ALLOW_INTEGER:
20974 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20975 case for more information.
20976 UNIFY_ALLOW_OUTER_LEVEL:
20977 This is the outermost level of a deduction. Used to determine validity
20978 of qualification conversions. A valid qualification conversion must
20979 have const qualified pointers leading up to the inner type which
20980 requires additional CV quals, except at the outer level, where const
20981 is not required [conv.qual]. It would be normal to set this flag in
20982 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20983 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20984 This is the outermost level of a deduction, and PARM can be more CV
20985 qualified at this point.
20986 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20987 This is the outermost level of a deduction, and PARM can be less CV
20988 qualified at this point. */
20989
20990 static int
20991 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20992 bool explain_p)
20993 {
20994 int idx;
20995 tree targ;
20996 tree tparm;
20997 int strict_in = strict;
20998 tsubst_flags_t complain = (explain_p
20999 ? tf_warning_or_error
21000 : tf_none);
21001
21002 /* I don't think this will do the right thing with respect to types.
21003 But the only case I've seen it in so far has been array bounds, where
21004 signedness is the only information lost, and I think that will be
21005 okay. */
21006 while (CONVERT_EXPR_P (parm))
21007 parm = TREE_OPERAND (parm, 0);
21008
21009 if (arg == error_mark_node)
21010 return unify_invalid (explain_p);
21011 if (arg == unknown_type_node
21012 || arg == init_list_type_node)
21013 /* We can't deduce anything from this, but we might get all the
21014 template args from other function args. */
21015 return unify_success (explain_p);
21016
21017 if (parm == any_targ_node || arg == any_targ_node)
21018 return unify_success (explain_p);
21019
21020 /* If PARM uses template parameters, then we can't bail out here,
21021 even if ARG == PARM, since we won't record unifications for the
21022 template parameters. We might need them if we're trying to
21023 figure out which of two things is more specialized. */
21024 if (arg == parm && !uses_template_parms (parm))
21025 return unify_success (explain_p);
21026
21027 /* Handle init lists early, so the rest of the function can assume
21028 we're dealing with a type. */
21029 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
21030 {
21031 tree elt, elttype;
21032 unsigned i;
21033 tree orig_parm = parm;
21034
21035 /* Replace T with std::initializer_list<T> for deduction. */
21036 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21037 && flag_deduce_init_list)
21038 parm = listify (parm);
21039
21040 if (!is_std_init_list (parm)
21041 && TREE_CODE (parm) != ARRAY_TYPE)
21042 /* We can only deduce from an initializer list argument if the
21043 parameter is std::initializer_list or an array; otherwise this
21044 is a non-deduced context. */
21045 return unify_success (explain_p);
21046
21047 if (TREE_CODE (parm) == ARRAY_TYPE)
21048 elttype = TREE_TYPE (parm);
21049 else
21050 {
21051 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
21052 /* Deduction is defined in terms of a single type, so just punt
21053 on the (bizarre) std::initializer_list<T...>. */
21054 if (PACK_EXPANSION_P (elttype))
21055 return unify_success (explain_p);
21056 }
21057
21058 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
21059 {
21060 int elt_strict = strict;
21061
21062 if (elt == error_mark_node)
21063 return unify_invalid (explain_p);
21064
21065 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
21066 {
21067 tree type = TREE_TYPE (elt);
21068 if (type == error_mark_node)
21069 return unify_invalid (explain_p);
21070 /* It should only be possible to get here for a call. */
21071 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
21072 elt_strict |= maybe_adjust_types_for_deduction
21073 (DEDUCE_CALL, &elttype, &type, elt);
21074 elt = type;
21075 }
21076
21077 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
21078 explain_p);
21079 }
21080
21081 if (TREE_CODE (parm) == ARRAY_TYPE
21082 && deducible_array_bound (TYPE_DOMAIN (parm)))
21083 {
21084 /* Also deduce from the length of the initializer list. */
21085 tree max = size_int (CONSTRUCTOR_NELTS (arg));
21086 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
21087 if (idx == error_mark_node)
21088 return unify_invalid (explain_p);
21089 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21090 idx, explain_p);
21091 }
21092
21093 /* If the std::initializer_list<T> deduction worked, replace the
21094 deduced A with std::initializer_list<A>. */
21095 if (orig_parm != parm)
21096 {
21097 idx = TEMPLATE_TYPE_IDX (orig_parm);
21098 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21099 targ = listify (targ);
21100 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
21101 }
21102 return unify_success (explain_p);
21103 }
21104
21105 /* If parm and arg aren't the same kind of thing (template, type, or
21106 expression), fail early. */
21107 if (pa_kind (parm) != pa_kind (arg))
21108 return unify_invalid (explain_p);
21109
21110 /* Immediately reject some pairs that won't unify because of
21111 cv-qualification mismatches. */
21112 if (TREE_CODE (arg) == TREE_CODE (parm)
21113 && TYPE_P (arg)
21114 /* It is the elements of the array which hold the cv quals of an array
21115 type, and the elements might be template type parms. We'll check
21116 when we recurse. */
21117 && TREE_CODE (arg) != ARRAY_TYPE
21118 /* We check the cv-qualifiers when unifying with template type
21119 parameters below. We want to allow ARG `const T' to unify with
21120 PARM `T' for example, when computing which of two templates
21121 is more specialized, for example. */
21122 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21123 && !check_cv_quals_for_unify (strict_in, arg, parm))
21124 return unify_cv_qual_mismatch (explain_p, parm, arg);
21125
21126 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21127 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21128 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21129 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21130 strict &= ~UNIFY_ALLOW_DERIVED;
21131 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21132 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21133
21134 switch (TREE_CODE (parm))
21135 {
21136 case TYPENAME_TYPE:
21137 case SCOPE_REF:
21138 case UNBOUND_CLASS_TEMPLATE:
21139 /* In a type which contains a nested-name-specifier, template
21140 argument values cannot be deduced for template parameters used
21141 within the nested-name-specifier. */
21142 return unify_success (explain_p);
21143
21144 case TEMPLATE_TYPE_PARM:
21145 case TEMPLATE_TEMPLATE_PARM:
21146 case BOUND_TEMPLATE_TEMPLATE_PARM:
21147 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21148 if (error_operand_p (tparm))
21149 return unify_invalid (explain_p);
21150
21151 if (TEMPLATE_TYPE_LEVEL (parm)
21152 != template_decl_level (tparm))
21153 /* The PARM is not one we're trying to unify. Just check
21154 to see if it matches ARG. */
21155 {
21156 if (TREE_CODE (arg) == TREE_CODE (parm)
21157 && (is_auto (parm) ? is_auto (arg)
21158 : same_type_p (parm, arg)))
21159 return unify_success (explain_p);
21160 else
21161 return unify_type_mismatch (explain_p, parm, arg);
21162 }
21163 idx = TEMPLATE_TYPE_IDX (parm);
21164 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21165 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21166 if (error_operand_p (tparm))
21167 return unify_invalid (explain_p);
21168
21169 /* Check for mixed types and values. */
21170 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21171 && TREE_CODE (tparm) != TYPE_DECL)
21172 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21173 && TREE_CODE (tparm) != TEMPLATE_DECL))
21174 gcc_unreachable ();
21175
21176 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21177 {
21178 if ((strict_in & UNIFY_ALLOW_DERIVED)
21179 && CLASS_TYPE_P (arg))
21180 {
21181 /* First try to match ARG directly. */
21182 tree t = try_class_unification (tparms, targs, parm, arg,
21183 explain_p);
21184 if (!t)
21185 {
21186 /* Otherwise, look for a suitable base of ARG, as below. */
21187 enum template_base_result r;
21188 r = get_template_base (tparms, targs, parm, arg,
21189 explain_p, &t);
21190 if (!t)
21191 return unify_no_common_base (explain_p, r, parm, arg);
21192 arg = t;
21193 }
21194 }
21195 /* ARG must be constructed from a template class or a template
21196 template parameter. */
21197 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21198 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21199 return unify_template_deduction_failure (explain_p, parm, arg);
21200
21201 /* Deduce arguments T, i from TT<T> or TT<i>. */
21202 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21203 return 1;
21204
21205 arg = TYPE_TI_TEMPLATE (arg);
21206
21207 /* Fall through to deduce template name. */
21208 }
21209
21210 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21211 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21212 {
21213 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21214
21215 /* Simple cases: Value already set, does match or doesn't. */
21216 if (targ != NULL_TREE && template_args_equal (targ, arg))
21217 return unify_success (explain_p);
21218 else if (targ)
21219 return unify_inconsistency (explain_p, parm, targ, arg);
21220 }
21221 else
21222 {
21223 /* If PARM is `const T' and ARG is only `int', we don't have
21224 a match unless we are allowing additional qualification.
21225 If ARG is `const int' and PARM is just `T' that's OK;
21226 that binds `const int' to `T'. */
21227 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21228 arg, parm))
21229 return unify_cv_qual_mismatch (explain_p, parm, arg);
21230
21231 /* Consider the case where ARG is `const volatile int' and
21232 PARM is `const T'. Then, T should be `volatile int'. */
21233 arg = cp_build_qualified_type_real
21234 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21235 if (arg == error_mark_node)
21236 return unify_invalid (explain_p);
21237
21238 /* Simple cases: Value already set, does match or doesn't. */
21239 if (targ != NULL_TREE && same_type_p (targ, arg))
21240 return unify_success (explain_p);
21241 else if (targ)
21242 return unify_inconsistency (explain_p, parm, targ, arg);
21243
21244 /* Make sure that ARG is not a variable-sized array. (Note
21245 that were talking about variable-sized arrays (like
21246 `int[n]'), rather than arrays of unknown size (like
21247 `int[]').) We'll get very confused by such a type since
21248 the bound of the array is not constant, and therefore
21249 not mangleable. Besides, such types are not allowed in
21250 ISO C++, so we can do as we please here. We do allow
21251 them for 'auto' deduction, since that isn't ABI-exposed. */
21252 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21253 return unify_vla_arg (explain_p, arg);
21254
21255 /* Strip typedefs as in convert_template_argument. */
21256 arg = canonicalize_type_argument (arg, tf_none);
21257 }
21258
21259 /* If ARG is a parameter pack or an expansion, we cannot unify
21260 against it unless PARM is also a parameter pack. */
21261 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21262 && !template_parameter_pack_p (parm))
21263 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21264
21265 /* If the argument deduction results is a METHOD_TYPE,
21266 then there is a problem.
21267 METHOD_TYPE doesn't map to any real C++ type the result of
21268 the deduction can not be of that type. */
21269 if (TREE_CODE (arg) == METHOD_TYPE)
21270 return unify_method_type_error (explain_p, arg);
21271
21272 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21273 return unify_success (explain_p);
21274
21275 case TEMPLATE_PARM_INDEX:
21276 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21277 if (error_operand_p (tparm))
21278 return unify_invalid (explain_p);
21279
21280 if (TEMPLATE_PARM_LEVEL (parm)
21281 != template_decl_level (tparm))
21282 {
21283 /* The PARM is not one we're trying to unify. Just check
21284 to see if it matches ARG. */
21285 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21286 && cp_tree_equal (parm, arg));
21287 if (result)
21288 unify_expression_unequal (explain_p, parm, arg);
21289 return result;
21290 }
21291
21292 idx = TEMPLATE_PARM_IDX (parm);
21293 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21294
21295 if (targ)
21296 {
21297 if ((strict & UNIFY_ALLOW_INTEGER)
21298 && TREE_TYPE (targ) && TREE_TYPE (arg)
21299 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21300 /* We're deducing from an array bound, the type doesn't matter. */
21301 arg = fold_convert (TREE_TYPE (targ), arg);
21302 int x = !cp_tree_equal (targ, arg);
21303 if (x)
21304 unify_inconsistency (explain_p, parm, targ, arg);
21305 return x;
21306 }
21307
21308 /* [temp.deduct.type] If, in the declaration of a function template
21309 with a non-type template-parameter, the non-type
21310 template-parameter is used in an expression in the function
21311 parameter-list and, if the corresponding template-argument is
21312 deduced, the template-argument type shall match the type of the
21313 template-parameter exactly, except that a template-argument
21314 deduced from an array bound may be of any integral type.
21315 The non-type parameter might use already deduced type parameters. */
21316 tparm = TREE_TYPE (parm);
21317 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
21318 /* We don't have enough levels of args to do any substitution. This
21319 can happen in the context of -fnew-ttp-matching. */;
21320 else
21321 {
21322 ++processing_template_decl;
21323 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
21324 --processing_template_decl;
21325
21326 if (tree a = type_uses_auto (tparm))
21327 {
21328 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21329 if (tparm == error_mark_node)
21330 return 1;
21331 }
21332 }
21333
21334 if (!TREE_TYPE (arg))
21335 /* Template-parameter dependent expression. Just accept it for now.
21336 It will later be processed in convert_template_argument. */
21337 ;
21338 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21339 non_reference (tparm)))
21340 /* OK */;
21341 else if ((strict & UNIFY_ALLOW_INTEGER)
21342 && CP_INTEGRAL_TYPE_P (tparm))
21343 /* Convert the ARG to the type of PARM; the deduced non-type
21344 template argument must exactly match the types of the
21345 corresponding parameter. */
21346 arg = fold (build_nop (tparm, arg));
21347 else if (uses_template_parms (tparm))
21348 {
21349 /* We haven't deduced the type of this parameter yet. */
21350 if (cxx_dialect >= cxx17
21351 /* We deduce from array bounds in try_array_deduction. */
21352 && !(strict & UNIFY_ALLOW_INTEGER))
21353 {
21354 /* Deduce it from the non-type argument. */
21355 tree atype = TREE_TYPE (arg);
21356 RECUR_AND_CHECK_FAILURE (tparms, targs,
21357 tparm, atype,
21358 UNIFY_ALLOW_NONE, explain_p);
21359 }
21360 else
21361 /* Try again later. */
21362 return unify_success (explain_p);
21363 }
21364 else
21365 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21366
21367 /* If ARG is a parameter pack or an expansion, we cannot unify
21368 against it unless PARM is also a parameter pack. */
21369 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21370 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21371 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21372
21373 {
21374 bool removed_attr = false;
21375 arg = strip_typedefs_expr (arg, &removed_attr);
21376 }
21377 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21378 return unify_success (explain_p);
21379
21380 case PTRMEM_CST:
21381 {
21382 /* A pointer-to-member constant can be unified only with
21383 another constant. */
21384 if (TREE_CODE (arg) != PTRMEM_CST)
21385 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21386
21387 /* Just unify the class member. It would be useless (and possibly
21388 wrong, depending on the strict flags) to unify also
21389 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21390 arg refer to the same variable, even if through different
21391 classes. For instance:
21392
21393 struct A { int x; };
21394 struct B : A { };
21395
21396 Unification of &A::x and &B::x must succeed. */
21397 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21398 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21399 }
21400
21401 case POINTER_TYPE:
21402 {
21403 if (!TYPE_PTR_P (arg))
21404 return unify_type_mismatch (explain_p, parm, arg);
21405
21406 /* [temp.deduct.call]
21407
21408 A can be another pointer or pointer to member type that can
21409 be converted to the deduced A via a qualification
21410 conversion (_conv.qual_).
21411
21412 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21413 This will allow for additional cv-qualification of the
21414 pointed-to types if appropriate. */
21415
21416 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21417 /* The derived-to-base conversion only persists through one
21418 level of pointers. */
21419 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21420
21421 return unify (tparms, targs, TREE_TYPE (parm),
21422 TREE_TYPE (arg), strict, explain_p);
21423 }
21424
21425 case REFERENCE_TYPE:
21426 if (TREE_CODE (arg) != REFERENCE_TYPE)
21427 return unify_type_mismatch (explain_p, parm, arg);
21428 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21429 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21430
21431 case ARRAY_TYPE:
21432 if (TREE_CODE (arg) != ARRAY_TYPE)
21433 return unify_type_mismatch (explain_p, parm, arg);
21434 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21435 != (TYPE_DOMAIN (arg) == NULL_TREE))
21436 return unify_type_mismatch (explain_p, parm, arg);
21437 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21438 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21439 if (TYPE_DOMAIN (parm) != NULL_TREE)
21440 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21441 TYPE_DOMAIN (arg), explain_p);
21442 return unify_success (explain_p);
21443
21444 case REAL_TYPE:
21445 case COMPLEX_TYPE:
21446 case VECTOR_TYPE:
21447 case INTEGER_TYPE:
21448 case BOOLEAN_TYPE:
21449 case ENUMERAL_TYPE:
21450 case VOID_TYPE:
21451 case NULLPTR_TYPE:
21452 if (TREE_CODE (arg) != TREE_CODE (parm))
21453 return unify_type_mismatch (explain_p, parm, arg);
21454
21455 /* We have already checked cv-qualification at the top of the
21456 function. */
21457 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21458 return unify_type_mismatch (explain_p, parm, arg);
21459
21460 /* As far as unification is concerned, this wins. Later checks
21461 will invalidate it if necessary. */
21462 return unify_success (explain_p);
21463
21464 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21465 /* Type INTEGER_CST can come from ordinary constant template args. */
21466 case INTEGER_CST:
21467 while (CONVERT_EXPR_P (arg))
21468 arg = TREE_OPERAND (arg, 0);
21469
21470 if (TREE_CODE (arg) != INTEGER_CST)
21471 return unify_template_argument_mismatch (explain_p, parm, arg);
21472 return (tree_int_cst_equal (parm, arg)
21473 ? unify_success (explain_p)
21474 : unify_template_argument_mismatch (explain_p, parm, arg));
21475
21476 case TREE_VEC:
21477 {
21478 int i, len, argslen;
21479 int parm_variadic_p = 0;
21480
21481 if (TREE_CODE (arg) != TREE_VEC)
21482 return unify_template_argument_mismatch (explain_p, parm, arg);
21483
21484 len = TREE_VEC_LENGTH (parm);
21485 argslen = TREE_VEC_LENGTH (arg);
21486
21487 /* Check for pack expansions in the parameters. */
21488 for (i = 0; i < len; ++i)
21489 {
21490 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21491 {
21492 if (i == len - 1)
21493 /* We can unify against something with a trailing
21494 parameter pack. */
21495 parm_variadic_p = 1;
21496 else
21497 /* [temp.deduct.type]/9: If the template argument list of
21498 P contains a pack expansion that is not the last
21499 template argument, the entire template argument list
21500 is a non-deduced context. */
21501 return unify_success (explain_p);
21502 }
21503 }
21504
21505 /* If we don't have enough arguments to satisfy the parameters
21506 (not counting the pack expression at the end), or we have
21507 too many arguments for a parameter list that doesn't end in
21508 a pack expression, we can't unify. */
21509 if (parm_variadic_p
21510 ? argslen < len - parm_variadic_p
21511 : argslen != len)
21512 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21513
21514 /* Unify all of the parameters that precede the (optional)
21515 pack expression. */
21516 for (i = 0; i < len - parm_variadic_p; ++i)
21517 {
21518 RECUR_AND_CHECK_FAILURE (tparms, targs,
21519 TREE_VEC_ELT (parm, i),
21520 TREE_VEC_ELT (arg, i),
21521 UNIFY_ALLOW_NONE, explain_p);
21522 }
21523 if (parm_variadic_p)
21524 return unify_pack_expansion (tparms, targs, parm, arg,
21525 DEDUCE_EXACT,
21526 /*subr=*/true, explain_p);
21527 return unify_success (explain_p);
21528 }
21529
21530 case RECORD_TYPE:
21531 case UNION_TYPE:
21532 if (TREE_CODE (arg) != TREE_CODE (parm))
21533 return unify_type_mismatch (explain_p, parm, arg);
21534
21535 if (TYPE_PTRMEMFUNC_P (parm))
21536 {
21537 if (!TYPE_PTRMEMFUNC_P (arg))
21538 return unify_type_mismatch (explain_p, parm, arg);
21539
21540 return unify (tparms, targs,
21541 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21542 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21543 strict, explain_p);
21544 }
21545 else if (TYPE_PTRMEMFUNC_P (arg))
21546 return unify_type_mismatch (explain_p, parm, arg);
21547
21548 if (CLASSTYPE_TEMPLATE_INFO (parm))
21549 {
21550 tree t = NULL_TREE;
21551
21552 if (strict_in & UNIFY_ALLOW_DERIVED)
21553 {
21554 /* First, we try to unify the PARM and ARG directly. */
21555 t = try_class_unification (tparms, targs,
21556 parm, arg, explain_p);
21557
21558 if (!t)
21559 {
21560 /* Fallback to the special case allowed in
21561 [temp.deduct.call]:
21562
21563 If P is a class, and P has the form
21564 template-id, then A can be a derived class of
21565 the deduced A. Likewise, if P is a pointer to
21566 a class of the form template-id, A can be a
21567 pointer to a derived class pointed to by the
21568 deduced A. */
21569 enum template_base_result r;
21570 r = get_template_base (tparms, targs, parm, arg,
21571 explain_p, &t);
21572
21573 if (!t)
21574 {
21575 /* Don't give the derived diagnostic if we're
21576 already dealing with the same template. */
21577 bool same_template
21578 = (CLASSTYPE_TEMPLATE_INFO (arg)
21579 && (CLASSTYPE_TI_TEMPLATE (parm)
21580 == CLASSTYPE_TI_TEMPLATE (arg)));
21581 return unify_no_common_base (explain_p && !same_template,
21582 r, parm, arg);
21583 }
21584 }
21585 }
21586 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21587 && (CLASSTYPE_TI_TEMPLATE (parm)
21588 == CLASSTYPE_TI_TEMPLATE (arg)))
21589 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21590 Then, we should unify `int' and `U'. */
21591 t = arg;
21592 else
21593 /* There's no chance of unification succeeding. */
21594 return unify_type_mismatch (explain_p, parm, arg);
21595
21596 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21597 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21598 }
21599 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21600 return unify_type_mismatch (explain_p, parm, arg);
21601 return unify_success (explain_p);
21602
21603 case METHOD_TYPE:
21604 case FUNCTION_TYPE:
21605 {
21606 unsigned int nargs;
21607 tree *args;
21608 tree a;
21609 unsigned int i;
21610
21611 if (TREE_CODE (arg) != TREE_CODE (parm))
21612 return unify_type_mismatch (explain_p, parm, arg);
21613
21614 /* CV qualifications for methods can never be deduced, they must
21615 match exactly. We need to check them explicitly here,
21616 because type_unification_real treats them as any other
21617 cv-qualified parameter. */
21618 if (TREE_CODE (parm) == METHOD_TYPE
21619 && (!check_cv_quals_for_unify
21620 (UNIFY_ALLOW_NONE,
21621 class_of_this_parm (arg),
21622 class_of_this_parm (parm))))
21623 return unify_cv_qual_mismatch (explain_p, parm, arg);
21624 if (TREE_CODE (arg) == FUNCTION_TYPE
21625 && type_memfn_quals (parm) != type_memfn_quals (arg))
21626 return unify_cv_qual_mismatch (explain_p, parm, arg);
21627 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21628 return unify_type_mismatch (explain_p, parm, arg);
21629
21630 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21631 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21632
21633 nargs = list_length (TYPE_ARG_TYPES (arg));
21634 args = XALLOCAVEC (tree, nargs);
21635 for (a = TYPE_ARG_TYPES (arg), i = 0;
21636 a != NULL_TREE && a != void_list_node;
21637 a = TREE_CHAIN (a), ++i)
21638 args[i] = TREE_VALUE (a);
21639 nargs = i;
21640
21641 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21642 args, nargs, 1, DEDUCE_EXACT,
21643 LOOKUP_NORMAL, NULL, explain_p))
21644 return 1;
21645
21646 if (flag_noexcept_type)
21647 {
21648 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21649 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21650 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21651 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21652 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21653 && uses_template_parms (TREE_PURPOSE (pspec)))
21654 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21655 TREE_PURPOSE (aspec),
21656 UNIFY_ALLOW_NONE, explain_p);
21657 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21658 return unify_type_mismatch (explain_p, parm, arg);
21659 }
21660
21661 return 0;
21662 }
21663
21664 case OFFSET_TYPE:
21665 /* Unify a pointer to member with a pointer to member function, which
21666 deduces the type of the member as a function type. */
21667 if (TYPE_PTRMEMFUNC_P (arg))
21668 {
21669 /* Check top-level cv qualifiers */
21670 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21671 return unify_cv_qual_mismatch (explain_p, parm, arg);
21672
21673 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21674 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21675 UNIFY_ALLOW_NONE, explain_p);
21676
21677 /* Determine the type of the function we are unifying against. */
21678 tree fntype = static_fn_type (arg);
21679
21680 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21681 }
21682
21683 if (TREE_CODE (arg) != OFFSET_TYPE)
21684 return unify_type_mismatch (explain_p, parm, arg);
21685 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21686 TYPE_OFFSET_BASETYPE (arg),
21687 UNIFY_ALLOW_NONE, explain_p);
21688 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21689 strict, explain_p);
21690
21691 case CONST_DECL:
21692 if (DECL_TEMPLATE_PARM_P (parm))
21693 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21694 if (arg != scalar_constant_value (parm))
21695 return unify_template_argument_mismatch (explain_p, parm, arg);
21696 return unify_success (explain_p);
21697
21698 case FIELD_DECL:
21699 case TEMPLATE_DECL:
21700 /* Matched cases are handled by the ARG == PARM test above. */
21701 return unify_template_argument_mismatch (explain_p, parm, arg);
21702
21703 case VAR_DECL:
21704 /* We might get a variable as a non-type template argument in parm if the
21705 corresponding parameter is type-dependent. Make any necessary
21706 adjustments based on whether arg is a reference. */
21707 if (CONSTANT_CLASS_P (arg))
21708 parm = fold_non_dependent_expr (parm);
21709 else if (REFERENCE_REF_P (arg))
21710 {
21711 tree sub = TREE_OPERAND (arg, 0);
21712 STRIP_NOPS (sub);
21713 if (TREE_CODE (sub) == ADDR_EXPR)
21714 arg = TREE_OPERAND (sub, 0);
21715 }
21716 /* Now use the normal expression code to check whether they match. */
21717 goto expr;
21718
21719 case TYPE_ARGUMENT_PACK:
21720 case NONTYPE_ARGUMENT_PACK:
21721 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21722 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21723
21724 case TYPEOF_TYPE:
21725 case DECLTYPE_TYPE:
21726 case UNDERLYING_TYPE:
21727 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21728 or UNDERLYING_TYPE nodes. */
21729 return unify_success (explain_p);
21730
21731 case ERROR_MARK:
21732 /* Unification fails if we hit an error node. */
21733 return unify_invalid (explain_p);
21734
21735 case INDIRECT_REF:
21736 if (REFERENCE_REF_P (parm))
21737 {
21738 bool pexp = PACK_EXPANSION_P (arg);
21739 if (pexp)
21740 arg = PACK_EXPANSION_PATTERN (arg);
21741 if (REFERENCE_REF_P (arg))
21742 arg = TREE_OPERAND (arg, 0);
21743 if (pexp)
21744 arg = make_pack_expansion (arg, complain);
21745 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21746 strict, explain_p);
21747 }
21748 /* FALLTHRU */
21749
21750 default:
21751 /* An unresolved overload is a nondeduced context. */
21752 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21753 return unify_success (explain_p);
21754 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21755 expr:
21756 /* We must be looking at an expression. This can happen with
21757 something like:
21758
21759 template <int I>
21760 void foo(S<I>, S<I + 2>);
21761
21762 This is a "nondeduced context":
21763
21764 [deduct.type]
21765
21766 The nondeduced contexts are:
21767
21768 --A type that is a template-id in which one or more of
21769 the template-arguments is an expression that references
21770 a template-parameter.
21771
21772 In these cases, we assume deduction succeeded, but don't
21773 actually infer any unifications. */
21774
21775 if (!uses_template_parms (parm)
21776 && !template_args_equal (parm, arg))
21777 return unify_expression_unequal (explain_p, parm, arg);
21778 else
21779 return unify_success (explain_p);
21780 }
21781 }
21782 #undef RECUR_AND_CHECK_FAILURE
21783 \f
21784 /* Note that DECL can be defined in this translation unit, if
21785 required. */
21786
21787 static void
21788 mark_definable (tree decl)
21789 {
21790 tree clone;
21791 DECL_NOT_REALLY_EXTERN (decl) = 1;
21792 FOR_EACH_CLONE (clone, decl)
21793 DECL_NOT_REALLY_EXTERN (clone) = 1;
21794 }
21795
21796 /* Called if RESULT is explicitly instantiated, or is a member of an
21797 explicitly instantiated class. */
21798
21799 void
21800 mark_decl_instantiated (tree result, int extern_p)
21801 {
21802 SET_DECL_EXPLICIT_INSTANTIATION (result);
21803
21804 /* If this entity has already been written out, it's too late to
21805 make any modifications. */
21806 if (TREE_ASM_WRITTEN (result))
21807 return;
21808
21809 /* For anonymous namespace we don't need to do anything. */
21810 if (decl_anon_ns_mem_p (result))
21811 {
21812 gcc_assert (!TREE_PUBLIC (result));
21813 return;
21814 }
21815
21816 if (TREE_CODE (result) != FUNCTION_DECL)
21817 /* The TREE_PUBLIC flag for function declarations will have been
21818 set correctly by tsubst. */
21819 TREE_PUBLIC (result) = 1;
21820
21821 /* This might have been set by an earlier implicit instantiation. */
21822 DECL_COMDAT (result) = 0;
21823
21824 if (extern_p)
21825 DECL_NOT_REALLY_EXTERN (result) = 0;
21826 else
21827 {
21828 mark_definable (result);
21829 mark_needed (result);
21830 /* Always make artificials weak. */
21831 if (DECL_ARTIFICIAL (result) && flag_weak)
21832 comdat_linkage (result);
21833 /* For WIN32 we also want to put explicit instantiations in
21834 linkonce sections. */
21835 else if (TREE_PUBLIC (result))
21836 maybe_make_one_only (result);
21837 }
21838
21839 /* If EXTERN_P, then this function will not be emitted -- unless
21840 followed by an explicit instantiation, at which point its linkage
21841 will be adjusted. If !EXTERN_P, then this function will be
21842 emitted here. In neither circumstance do we want
21843 import_export_decl to adjust the linkage. */
21844 DECL_INTERFACE_KNOWN (result) = 1;
21845 }
21846
21847 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21848 important template arguments. If any are missing, we check whether
21849 they're important by using error_mark_node for substituting into any
21850 args that were used for partial ordering (the ones between ARGS and END)
21851 and seeing if it bubbles up. */
21852
21853 static bool
21854 check_undeduced_parms (tree targs, tree args, tree end)
21855 {
21856 bool found = false;
21857 int i;
21858 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21859 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21860 {
21861 found = true;
21862 TREE_VEC_ELT (targs, i) = error_mark_node;
21863 }
21864 if (found)
21865 {
21866 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21867 if (substed == error_mark_node)
21868 return true;
21869 }
21870 return false;
21871 }
21872
21873 /* Given two function templates PAT1 and PAT2, return:
21874
21875 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21876 -1 if PAT2 is more specialized than PAT1.
21877 0 if neither is more specialized.
21878
21879 LEN indicates the number of parameters we should consider
21880 (defaulted parameters should not be considered).
21881
21882 The 1998 std underspecified function template partial ordering, and
21883 DR214 addresses the issue. We take pairs of arguments, one from
21884 each of the templates, and deduce them against each other. One of
21885 the templates will be more specialized if all the *other*
21886 template's arguments deduce against its arguments and at least one
21887 of its arguments *does* *not* deduce against the other template's
21888 corresponding argument. Deduction is done as for class templates.
21889 The arguments used in deduction have reference and top level cv
21890 qualifiers removed. Iff both arguments were originally reference
21891 types *and* deduction succeeds in both directions, an lvalue reference
21892 wins against an rvalue reference and otherwise the template
21893 with the more cv-qualified argument wins for that pairing (if
21894 neither is more cv-qualified, they both are equal). Unlike regular
21895 deduction, after all the arguments have been deduced in this way,
21896 we do *not* verify the deduced template argument values can be
21897 substituted into non-deduced contexts.
21898
21899 The logic can be a bit confusing here, because we look at deduce1 and
21900 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21901 can find template arguments for pat1 to make arg1 look like arg2, that
21902 means that arg2 is at least as specialized as arg1. */
21903
21904 int
21905 more_specialized_fn (tree pat1, tree pat2, int len)
21906 {
21907 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21908 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21909 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21910 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21911 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21912 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21913 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21914 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21915 tree origs1, origs2;
21916 bool lose1 = false;
21917 bool lose2 = false;
21918
21919 /* Remove the this parameter from non-static member functions. If
21920 one is a non-static member function and the other is not a static
21921 member function, remove the first parameter from that function
21922 also. This situation occurs for operator functions where we
21923 locate both a member function (with this pointer) and non-member
21924 operator (with explicit first operand). */
21925 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21926 {
21927 len--; /* LEN is the number of significant arguments for DECL1 */
21928 args1 = TREE_CHAIN (args1);
21929 if (!DECL_STATIC_FUNCTION_P (decl2))
21930 args2 = TREE_CHAIN (args2);
21931 }
21932 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21933 {
21934 args2 = TREE_CHAIN (args2);
21935 if (!DECL_STATIC_FUNCTION_P (decl1))
21936 {
21937 len--;
21938 args1 = TREE_CHAIN (args1);
21939 }
21940 }
21941
21942 /* If only one is a conversion operator, they are unordered. */
21943 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21944 return 0;
21945
21946 /* Consider the return type for a conversion function */
21947 if (DECL_CONV_FN_P (decl1))
21948 {
21949 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21950 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21951 len++;
21952 }
21953
21954 processing_template_decl++;
21955
21956 origs1 = args1;
21957 origs2 = args2;
21958
21959 while (len--
21960 /* Stop when an ellipsis is seen. */
21961 && args1 != NULL_TREE && args2 != NULL_TREE)
21962 {
21963 tree arg1 = TREE_VALUE (args1);
21964 tree arg2 = TREE_VALUE (args2);
21965 int deduce1, deduce2;
21966 int quals1 = -1;
21967 int quals2 = -1;
21968 int ref1 = 0;
21969 int ref2 = 0;
21970
21971 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21972 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21973 {
21974 /* When both arguments are pack expansions, we need only
21975 unify the patterns themselves. */
21976 arg1 = PACK_EXPANSION_PATTERN (arg1);
21977 arg2 = PACK_EXPANSION_PATTERN (arg2);
21978
21979 /* This is the last comparison we need to do. */
21980 len = 0;
21981 }
21982
21983 /* DR 1847: If a particular P contains no template-parameters that
21984 participate in template argument deduction, that P is not used to
21985 determine the ordering. */
21986 if (!uses_deducible_template_parms (arg1)
21987 && !uses_deducible_template_parms (arg2))
21988 goto next;
21989
21990 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21991 {
21992 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21993 arg1 = TREE_TYPE (arg1);
21994 quals1 = cp_type_quals (arg1);
21995 }
21996
21997 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21998 {
21999 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
22000 arg2 = TREE_TYPE (arg2);
22001 quals2 = cp_type_quals (arg2);
22002 }
22003
22004 arg1 = TYPE_MAIN_VARIANT (arg1);
22005 arg2 = TYPE_MAIN_VARIANT (arg2);
22006
22007 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
22008 {
22009 int i, len2 = remaining_arguments (args2);
22010 tree parmvec = make_tree_vec (1);
22011 tree argvec = make_tree_vec (len2);
22012 tree ta = args2;
22013
22014 /* Setup the parameter vector, which contains only ARG1. */
22015 TREE_VEC_ELT (parmvec, 0) = arg1;
22016
22017 /* Setup the argument vector, which contains the remaining
22018 arguments. */
22019 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
22020 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22021
22022 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
22023 argvec, DEDUCE_EXACT,
22024 /*subr=*/true, /*explain_p=*/false)
22025 == 0);
22026
22027 /* We cannot deduce in the other direction, because ARG1 is
22028 a pack expansion but ARG2 is not. */
22029 deduce2 = 0;
22030 }
22031 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22032 {
22033 int i, len1 = remaining_arguments (args1);
22034 tree parmvec = make_tree_vec (1);
22035 tree argvec = make_tree_vec (len1);
22036 tree ta = args1;
22037
22038 /* Setup the parameter vector, which contains only ARG1. */
22039 TREE_VEC_ELT (parmvec, 0) = arg2;
22040
22041 /* Setup the argument vector, which contains the remaining
22042 arguments. */
22043 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
22044 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22045
22046 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
22047 argvec, DEDUCE_EXACT,
22048 /*subr=*/true, /*explain_p=*/false)
22049 == 0);
22050
22051 /* We cannot deduce in the other direction, because ARG2 is
22052 a pack expansion but ARG1 is not.*/
22053 deduce1 = 0;
22054 }
22055
22056 else
22057 {
22058 /* The normal case, where neither argument is a pack
22059 expansion. */
22060 deduce1 = (unify (tparms1, targs1, arg1, arg2,
22061 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22062 == 0);
22063 deduce2 = (unify (tparms2, targs2, arg2, arg1,
22064 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22065 == 0);
22066 }
22067
22068 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22069 arg2, then arg2 is not as specialized as arg1. */
22070 if (!deduce1)
22071 lose2 = true;
22072 if (!deduce2)
22073 lose1 = true;
22074
22075 /* "If, for a given type, deduction succeeds in both directions
22076 (i.e., the types are identical after the transformations above)
22077 and both P and A were reference types (before being replaced with
22078 the type referred to above):
22079 - if the type from the argument template was an lvalue reference and
22080 the type from the parameter template was not, the argument type is
22081 considered to be more specialized than the other; otherwise,
22082 - if the type from the argument template is more cv-qualified
22083 than the type from the parameter template (as described above),
22084 the argument type is considered to be more specialized than the other;
22085 otherwise,
22086 - neither type is more specialized than the other." */
22087
22088 if (deduce1 && deduce2)
22089 {
22090 if (ref1 && ref2 && ref1 != ref2)
22091 {
22092 if (ref1 > ref2)
22093 lose1 = true;
22094 else
22095 lose2 = true;
22096 }
22097 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
22098 {
22099 if ((quals1 & quals2) == quals2)
22100 lose2 = true;
22101 if ((quals1 & quals2) == quals1)
22102 lose1 = true;
22103 }
22104 }
22105
22106 if (lose1 && lose2)
22107 /* We've failed to deduce something in either direction.
22108 These must be unordered. */
22109 break;
22110
22111 next:
22112
22113 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22114 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22115 /* We have already processed all of the arguments in our
22116 handing of the pack expansion type. */
22117 len = 0;
22118
22119 args1 = TREE_CHAIN (args1);
22120 args2 = TREE_CHAIN (args2);
22121 }
22122
22123 /* "In most cases, all template parameters must have values in order for
22124 deduction to succeed, but for partial ordering purposes a template
22125 parameter may remain without a value provided it is not used in the
22126 types being used for partial ordering."
22127
22128 Thus, if we are missing any of the targs1 we need to substitute into
22129 origs1, then pat2 is not as specialized as pat1. This can happen when
22130 there is a nondeduced context. */
22131 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22132 lose2 = true;
22133 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22134 lose1 = true;
22135
22136 processing_template_decl--;
22137
22138 /* If both deductions succeed, the partial ordering selects the more
22139 constrained template. */
22140 if (!lose1 && !lose2)
22141 {
22142 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22143 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22144 lose1 = !subsumes_constraints (c1, c2);
22145 lose2 = !subsumes_constraints (c2, c1);
22146 }
22147
22148 /* All things being equal, if the next argument is a pack expansion
22149 for one function but not for the other, prefer the
22150 non-variadic function. FIXME this is bogus; see c++/41958. */
22151 if (lose1 == lose2
22152 && args1 && TREE_VALUE (args1)
22153 && args2 && TREE_VALUE (args2))
22154 {
22155 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22156 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22157 }
22158
22159 if (lose1 == lose2)
22160 return 0;
22161 else if (!lose1)
22162 return 1;
22163 else
22164 return -1;
22165 }
22166
22167 /* Determine which of two partial specializations of TMPL is more
22168 specialized.
22169
22170 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22171 to the first partial specialization. The TREE_PURPOSE is the
22172 innermost set of template parameters for the partial
22173 specialization. PAT2 is similar, but for the second template.
22174
22175 Return 1 if the first partial specialization is more specialized;
22176 -1 if the second is more specialized; 0 if neither is more
22177 specialized.
22178
22179 See [temp.class.order] for information about determining which of
22180 two templates is more specialized. */
22181
22182 static int
22183 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22184 {
22185 tree targs;
22186 int winner = 0;
22187 bool any_deductions = false;
22188
22189 tree tmpl1 = TREE_VALUE (pat1);
22190 tree tmpl2 = TREE_VALUE (pat2);
22191 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22192 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22193
22194 /* Just like what happens for functions, if we are ordering between
22195 different template specializations, we may encounter dependent
22196 types in the arguments, and we need our dependency check functions
22197 to behave correctly. */
22198 ++processing_template_decl;
22199 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22200 if (targs)
22201 {
22202 --winner;
22203 any_deductions = true;
22204 }
22205
22206 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22207 if (targs)
22208 {
22209 ++winner;
22210 any_deductions = true;
22211 }
22212 --processing_template_decl;
22213
22214 /* If both deductions succeed, the partial ordering selects the more
22215 constrained template. */
22216 if (!winner && any_deductions)
22217 return more_constrained (tmpl1, tmpl2);
22218
22219 /* In the case of a tie where at least one of the templates
22220 has a parameter pack at the end, the template with the most
22221 non-packed parameters wins. */
22222 if (winner == 0
22223 && any_deductions
22224 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22225 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22226 {
22227 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22228 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22229 int len1 = TREE_VEC_LENGTH (args1);
22230 int len2 = TREE_VEC_LENGTH (args2);
22231
22232 /* We don't count the pack expansion at the end. */
22233 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22234 --len1;
22235 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22236 --len2;
22237
22238 if (len1 > len2)
22239 return 1;
22240 else if (len1 < len2)
22241 return -1;
22242 }
22243
22244 return winner;
22245 }
22246
22247 /* Return the template arguments that will produce the function signature
22248 DECL from the function template FN, with the explicit template
22249 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22250 also match. Return NULL_TREE if no satisfactory arguments could be
22251 found. */
22252
22253 static tree
22254 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22255 {
22256 int ntparms = DECL_NTPARMS (fn);
22257 tree targs = make_tree_vec (ntparms);
22258 tree decl_type = TREE_TYPE (decl);
22259 tree decl_arg_types;
22260 tree *args;
22261 unsigned int nargs, ix;
22262 tree arg;
22263
22264 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22265
22266 /* Never do unification on the 'this' parameter. */
22267 decl_arg_types = skip_artificial_parms_for (decl,
22268 TYPE_ARG_TYPES (decl_type));
22269
22270 nargs = list_length (decl_arg_types);
22271 args = XALLOCAVEC (tree, nargs);
22272 for (arg = decl_arg_types, ix = 0;
22273 arg != NULL_TREE && arg != void_list_node;
22274 arg = TREE_CHAIN (arg), ++ix)
22275 args[ix] = TREE_VALUE (arg);
22276
22277 if (fn_type_unification (fn, explicit_args, targs,
22278 args, ix,
22279 (check_rettype || DECL_CONV_FN_P (fn)
22280 ? TREE_TYPE (decl_type) : NULL_TREE),
22281 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22282 /*decltype*/false)
22283 == error_mark_node)
22284 return NULL_TREE;
22285
22286 return targs;
22287 }
22288
22289 /* Return the innermost template arguments that, when applied to a partial
22290 specialization SPEC_TMPL of TMPL, yield the ARGS.
22291
22292 For example, suppose we have:
22293
22294 template <class T, class U> struct S {};
22295 template <class T> struct S<T*, int> {};
22296
22297 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22298 partial specialization and the ARGS will be {double*, int}. The resulting
22299 vector will be {double}, indicating that `T' is bound to `double'. */
22300
22301 static tree
22302 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22303 {
22304 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22305 tree spec_args
22306 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22307 int i, ntparms = TREE_VEC_LENGTH (tparms);
22308 tree deduced_args;
22309 tree innermost_deduced_args;
22310
22311 innermost_deduced_args = make_tree_vec (ntparms);
22312 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22313 {
22314 deduced_args = copy_node (args);
22315 SET_TMPL_ARGS_LEVEL (deduced_args,
22316 TMPL_ARGS_DEPTH (deduced_args),
22317 innermost_deduced_args);
22318 }
22319 else
22320 deduced_args = innermost_deduced_args;
22321
22322 bool tried_array_deduction = (cxx_dialect < cxx17);
22323 again:
22324 if (unify (tparms, deduced_args,
22325 INNERMOST_TEMPLATE_ARGS (spec_args),
22326 INNERMOST_TEMPLATE_ARGS (args),
22327 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22328 return NULL_TREE;
22329
22330 for (i = 0; i < ntparms; ++i)
22331 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22332 {
22333 if (!tried_array_deduction)
22334 {
22335 try_array_deduction (tparms, innermost_deduced_args,
22336 INNERMOST_TEMPLATE_ARGS (spec_args));
22337 tried_array_deduction = true;
22338 if (TREE_VEC_ELT (innermost_deduced_args, i))
22339 goto again;
22340 }
22341 return NULL_TREE;
22342 }
22343
22344 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22345 if (!push_tinst_level (tinst))
22346 {
22347 excessive_deduction_depth = true;
22348 return NULL_TREE;
22349 }
22350
22351 /* Verify that nondeduced template arguments agree with the type
22352 obtained from argument deduction.
22353
22354 For example:
22355
22356 struct A { typedef int X; };
22357 template <class T, class U> struct C {};
22358 template <class T> struct C<T, typename T::X> {};
22359
22360 Then with the instantiation `C<A, int>', we can deduce that
22361 `T' is `A' but unify () does not check whether `typename T::X'
22362 is `int'. */
22363 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22364
22365 if (spec_args != error_mark_node)
22366 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22367 INNERMOST_TEMPLATE_ARGS (spec_args),
22368 tmpl, tf_none, false, false);
22369
22370 pop_tinst_level ();
22371
22372 if (spec_args == error_mark_node
22373 /* We only need to check the innermost arguments; the other
22374 arguments will always agree. */
22375 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22376 INNERMOST_TEMPLATE_ARGS (args)))
22377 return NULL_TREE;
22378
22379 /* Now that we have bindings for all of the template arguments,
22380 ensure that the arguments deduced for the template template
22381 parameters have compatible template parameter lists. See the use
22382 of template_template_parm_bindings_ok_p in fn_type_unification
22383 for more information. */
22384 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22385 return NULL_TREE;
22386
22387 return deduced_args;
22388 }
22389
22390 // Compare two function templates T1 and T2 by deducing bindings
22391 // from one against the other. If both deductions succeed, compare
22392 // constraints to see which is more constrained.
22393 static int
22394 more_specialized_inst (tree t1, tree t2)
22395 {
22396 int fate = 0;
22397 int count = 0;
22398
22399 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22400 {
22401 --fate;
22402 ++count;
22403 }
22404
22405 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22406 {
22407 ++fate;
22408 ++count;
22409 }
22410
22411 // If both deductions succeed, then one may be more constrained.
22412 if (count == 2 && fate == 0)
22413 fate = more_constrained (t1, t2);
22414
22415 return fate;
22416 }
22417
22418 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22419 Return the TREE_LIST node with the most specialized template, if
22420 any. If there is no most specialized template, the error_mark_node
22421 is returned.
22422
22423 Note that this function does not look at, or modify, the
22424 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22425 returned is one of the elements of INSTANTIATIONS, callers may
22426 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22427 and retrieve it from the value returned. */
22428
22429 tree
22430 most_specialized_instantiation (tree templates)
22431 {
22432 tree fn, champ;
22433
22434 ++processing_template_decl;
22435
22436 champ = templates;
22437 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22438 {
22439 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22440 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22441 if (fate == -1)
22442 champ = fn;
22443 else if (!fate)
22444 {
22445 /* Equally specialized, move to next function. If there
22446 is no next function, nothing's most specialized. */
22447 fn = TREE_CHAIN (fn);
22448 champ = fn;
22449 if (!fn)
22450 break;
22451 }
22452 }
22453
22454 if (champ)
22455 /* Now verify that champ is better than everything earlier in the
22456 instantiation list. */
22457 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22458 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22459 {
22460 champ = NULL_TREE;
22461 break;
22462 }
22463 }
22464
22465 processing_template_decl--;
22466
22467 if (!champ)
22468 return error_mark_node;
22469
22470 return champ;
22471 }
22472
22473 /* If DECL is a specialization of some template, return the most
22474 general such template. Otherwise, returns NULL_TREE.
22475
22476 For example, given:
22477
22478 template <class T> struct S { template <class U> void f(U); };
22479
22480 if TMPL is `template <class U> void S<int>::f(U)' this will return
22481 the full template. This function will not trace past partial
22482 specializations, however. For example, given in addition:
22483
22484 template <class T> struct S<T*> { template <class U> void f(U); };
22485
22486 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22487 `template <class T> template <class U> S<T*>::f(U)'. */
22488
22489 tree
22490 most_general_template (tree decl)
22491 {
22492 if (TREE_CODE (decl) != TEMPLATE_DECL)
22493 {
22494 if (tree tinfo = get_template_info (decl))
22495 decl = TI_TEMPLATE (tinfo);
22496 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22497 template friend, or a FIELD_DECL for a capture pack. */
22498 if (TREE_CODE (decl) != TEMPLATE_DECL)
22499 return NULL_TREE;
22500 }
22501
22502 /* Look for more and more general templates. */
22503 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22504 {
22505 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22506 (See cp-tree.h for details.) */
22507 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22508 break;
22509
22510 if (CLASS_TYPE_P (TREE_TYPE (decl))
22511 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22512 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22513 break;
22514
22515 /* Stop if we run into an explicitly specialized class template. */
22516 if (!DECL_NAMESPACE_SCOPE_P (decl)
22517 && DECL_CONTEXT (decl)
22518 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22519 break;
22520
22521 decl = DECL_TI_TEMPLATE (decl);
22522 }
22523
22524 return decl;
22525 }
22526
22527 /* Return the most specialized of the template partial specializations
22528 which can produce TARGET, a specialization of some class or variable
22529 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22530 a TEMPLATE_DECL node corresponding to the partial specialization, while
22531 the TREE_PURPOSE is the set of template arguments that must be
22532 substituted into the template pattern in order to generate TARGET.
22533
22534 If the choice of partial specialization is ambiguous, a diagnostic
22535 is issued, and the error_mark_node is returned. If there are no
22536 partial specializations matching TARGET, then NULL_TREE is
22537 returned, indicating that the primary template should be used. */
22538
22539 static tree
22540 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22541 {
22542 tree list = NULL_TREE;
22543 tree t;
22544 tree champ;
22545 int fate;
22546 bool ambiguous_p;
22547 tree outer_args = NULL_TREE;
22548 tree tmpl, args;
22549
22550 if (TYPE_P (target))
22551 {
22552 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22553 tmpl = TI_TEMPLATE (tinfo);
22554 args = TI_ARGS (tinfo);
22555 }
22556 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22557 {
22558 tmpl = TREE_OPERAND (target, 0);
22559 args = TREE_OPERAND (target, 1);
22560 }
22561 else if (VAR_P (target))
22562 {
22563 tree tinfo = DECL_TEMPLATE_INFO (target);
22564 tmpl = TI_TEMPLATE (tinfo);
22565 args = TI_ARGS (tinfo);
22566 }
22567 else
22568 gcc_unreachable ();
22569
22570 tree main_tmpl = most_general_template (tmpl);
22571
22572 /* For determining which partial specialization to use, only the
22573 innermost args are interesting. */
22574 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22575 {
22576 outer_args = strip_innermost_template_args (args, 1);
22577 args = INNERMOST_TEMPLATE_ARGS (args);
22578 }
22579
22580 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22581 {
22582 tree spec_args;
22583 tree spec_tmpl = TREE_VALUE (t);
22584
22585 if (outer_args)
22586 {
22587 /* Substitute in the template args from the enclosing class. */
22588 ++processing_template_decl;
22589 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22590 --processing_template_decl;
22591 }
22592
22593 if (spec_tmpl == error_mark_node)
22594 return error_mark_node;
22595
22596 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22597 if (spec_args)
22598 {
22599 if (outer_args)
22600 spec_args = add_to_template_args (outer_args, spec_args);
22601
22602 /* Keep the candidate only if the constraints are satisfied,
22603 or if we're not compiling with concepts. */
22604 if (!flag_concepts
22605 || constraints_satisfied_p (spec_tmpl, spec_args))
22606 {
22607 list = tree_cons (spec_args, TREE_VALUE (t), list);
22608 TREE_TYPE (list) = TREE_TYPE (t);
22609 }
22610 }
22611 }
22612
22613 if (! list)
22614 return NULL_TREE;
22615
22616 ambiguous_p = false;
22617 t = list;
22618 champ = t;
22619 t = TREE_CHAIN (t);
22620 for (; t; t = TREE_CHAIN (t))
22621 {
22622 fate = more_specialized_partial_spec (tmpl, champ, t);
22623 if (fate == 1)
22624 ;
22625 else
22626 {
22627 if (fate == 0)
22628 {
22629 t = TREE_CHAIN (t);
22630 if (! t)
22631 {
22632 ambiguous_p = true;
22633 break;
22634 }
22635 }
22636 champ = t;
22637 }
22638 }
22639
22640 if (!ambiguous_p)
22641 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22642 {
22643 fate = more_specialized_partial_spec (tmpl, champ, t);
22644 if (fate != 1)
22645 {
22646 ambiguous_p = true;
22647 break;
22648 }
22649 }
22650
22651 if (ambiguous_p)
22652 {
22653 const char *str;
22654 char *spaces = NULL;
22655 if (!(complain & tf_error))
22656 return error_mark_node;
22657 if (TYPE_P (target))
22658 error ("ambiguous template instantiation for %q#T", target);
22659 else
22660 error ("ambiguous template instantiation for %q#D", target);
22661 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22662 for (t = list; t; t = TREE_CHAIN (t))
22663 {
22664 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22665 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22666 "%s %#qS", spaces ? spaces : str, subst);
22667 spaces = spaces ? spaces : get_spaces (str);
22668 }
22669 free (spaces);
22670 return error_mark_node;
22671 }
22672
22673 return champ;
22674 }
22675
22676 /* Explicitly instantiate DECL. */
22677
22678 void
22679 do_decl_instantiation (tree decl, tree storage)
22680 {
22681 tree result = NULL_TREE;
22682 int extern_p = 0;
22683
22684 if (!decl || decl == error_mark_node)
22685 /* An error occurred, for which grokdeclarator has already issued
22686 an appropriate message. */
22687 return;
22688 else if (! DECL_LANG_SPECIFIC (decl))
22689 {
22690 error ("explicit instantiation of non-template %q#D", decl);
22691 return;
22692 }
22693
22694 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22695 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22696
22697 if (VAR_P (decl) && !var_templ)
22698 {
22699 /* There is an asymmetry here in the way VAR_DECLs and
22700 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22701 the latter, the DECL we get back will be marked as a
22702 template instantiation, and the appropriate
22703 DECL_TEMPLATE_INFO will be set up. This does not happen for
22704 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22705 should handle VAR_DECLs as it currently handles
22706 FUNCTION_DECLs. */
22707 if (!DECL_CLASS_SCOPE_P (decl))
22708 {
22709 error ("%qD is not a static data member of a class template", decl);
22710 return;
22711 }
22712 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22713 if (!result || !VAR_P (result))
22714 {
22715 error ("no matching template for %qD found", decl);
22716 return;
22717 }
22718 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22719 {
22720 error ("type %qT for explicit instantiation %qD does not match "
22721 "declared type %qT", TREE_TYPE (result), decl,
22722 TREE_TYPE (decl));
22723 return;
22724 }
22725 }
22726 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22727 {
22728 error ("explicit instantiation of %q#D", decl);
22729 return;
22730 }
22731 else
22732 result = decl;
22733
22734 /* Check for various error cases. Note that if the explicit
22735 instantiation is valid the RESULT will currently be marked as an
22736 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22737 until we get here. */
22738
22739 if (DECL_TEMPLATE_SPECIALIZATION (result))
22740 {
22741 /* DR 259 [temp.spec].
22742
22743 Both an explicit instantiation and a declaration of an explicit
22744 specialization shall not appear in a program unless the explicit
22745 instantiation follows a declaration of the explicit specialization.
22746
22747 For a given set of template parameters, if an explicit
22748 instantiation of a template appears after a declaration of an
22749 explicit specialization for that template, the explicit
22750 instantiation has no effect. */
22751 return;
22752 }
22753 else if (DECL_EXPLICIT_INSTANTIATION (result))
22754 {
22755 /* [temp.spec]
22756
22757 No program shall explicitly instantiate any template more
22758 than once.
22759
22760 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22761 the first instantiation was `extern' and the second is not,
22762 and EXTERN_P for the opposite case. */
22763 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22764 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22765 /* If an "extern" explicit instantiation follows an ordinary
22766 explicit instantiation, the template is instantiated. */
22767 if (extern_p)
22768 return;
22769 }
22770 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22771 {
22772 error ("no matching template for %qD found", result);
22773 return;
22774 }
22775 else if (!DECL_TEMPLATE_INFO (result))
22776 {
22777 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22778 return;
22779 }
22780
22781 if (storage == NULL_TREE)
22782 ;
22783 else if (storage == ridpointers[(int) RID_EXTERN])
22784 {
22785 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22786 pedwarn (input_location, OPT_Wpedantic,
22787 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22788 "instantiations");
22789 extern_p = 1;
22790 }
22791 else
22792 error ("storage class %qD applied to template instantiation", storage);
22793
22794 check_explicit_instantiation_namespace (result);
22795 mark_decl_instantiated (result, extern_p);
22796 if (! extern_p)
22797 instantiate_decl (result, /*defer_ok=*/true,
22798 /*expl_inst_class_mem_p=*/false);
22799 }
22800
22801 static void
22802 mark_class_instantiated (tree t, int extern_p)
22803 {
22804 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22805 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22806 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22807 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22808 if (! extern_p)
22809 {
22810 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22811 rest_of_type_compilation (t, 1);
22812 }
22813 }
22814
22815 /* Called from do_type_instantiation through binding_table_foreach to
22816 do recursive instantiation for the type bound in ENTRY. */
22817 static void
22818 bt_instantiate_type_proc (binding_entry entry, void *data)
22819 {
22820 tree storage = *(tree *) data;
22821
22822 if (MAYBE_CLASS_TYPE_P (entry->type)
22823 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22824 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22825 }
22826
22827 /* Perform an explicit instantiation of template class T. STORAGE, if
22828 non-null, is the RID for extern, inline or static. COMPLAIN is
22829 nonzero if this is called from the parser, zero if called recursively,
22830 since the standard is unclear (as detailed below). */
22831
22832 void
22833 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22834 {
22835 int extern_p = 0;
22836 int nomem_p = 0;
22837 int static_p = 0;
22838 int previous_instantiation_extern_p = 0;
22839
22840 if (TREE_CODE (t) == TYPE_DECL)
22841 t = TREE_TYPE (t);
22842
22843 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22844 {
22845 tree tmpl =
22846 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22847 if (tmpl)
22848 error ("explicit instantiation of non-class template %qD", tmpl);
22849 else
22850 error ("explicit instantiation of non-template type %qT", t);
22851 return;
22852 }
22853
22854 complete_type (t);
22855
22856 if (!COMPLETE_TYPE_P (t))
22857 {
22858 if (complain & tf_error)
22859 error ("explicit instantiation of %q#T before definition of template",
22860 t);
22861 return;
22862 }
22863
22864 if (storage != NULL_TREE)
22865 {
22866 if (!in_system_header_at (input_location))
22867 {
22868 if (storage == ridpointers[(int) RID_EXTERN])
22869 {
22870 if (cxx_dialect == cxx98)
22871 pedwarn (input_location, OPT_Wpedantic,
22872 "ISO C++ 1998 forbids the use of %<extern%> on "
22873 "explicit instantiations");
22874 }
22875 else
22876 pedwarn (input_location, OPT_Wpedantic,
22877 "ISO C++ forbids the use of %qE"
22878 " on explicit instantiations", storage);
22879 }
22880
22881 if (storage == ridpointers[(int) RID_INLINE])
22882 nomem_p = 1;
22883 else if (storage == ridpointers[(int) RID_EXTERN])
22884 extern_p = 1;
22885 else if (storage == ridpointers[(int) RID_STATIC])
22886 static_p = 1;
22887 else
22888 {
22889 error ("storage class %qD applied to template instantiation",
22890 storage);
22891 extern_p = 0;
22892 }
22893 }
22894
22895 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22896 {
22897 /* DR 259 [temp.spec].
22898
22899 Both an explicit instantiation and a declaration of an explicit
22900 specialization shall not appear in a program unless the explicit
22901 instantiation follows a declaration of the explicit specialization.
22902
22903 For a given set of template parameters, if an explicit
22904 instantiation of a template appears after a declaration of an
22905 explicit specialization for that template, the explicit
22906 instantiation has no effect. */
22907 return;
22908 }
22909 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22910 {
22911 /* [temp.spec]
22912
22913 No program shall explicitly instantiate any template more
22914 than once.
22915
22916 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22917 instantiation was `extern'. If EXTERN_P then the second is.
22918 These cases are OK. */
22919 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22920
22921 if (!previous_instantiation_extern_p && !extern_p
22922 && (complain & tf_error))
22923 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22924
22925 /* If we've already instantiated the template, just return now. */
22926 if (!CLASSTYPE_INTERFACE_ONLY (t))
22927 return;
22928 }
22929
22930 check_explicit_instantiation_namespace (TYPE_NAME (t));
22931 mark_class_instantiated (t, extern_p);
22932
22933 if (nomem_p)
22934 return;
22935
22936 /* In contrast to implicit instantiation, where only the
22937 declarations, and not the definitions, of members are
22938 instantiated, we have here:
22939
22940 [temp.explicit]
22941
22942 The explicit instantiation of a class template specialization
22943 implies the instantiation of all of its members not
22944 previously explicitly specialized in the translation unit
22945 containing the explicit instantiation.
22946
22947 Of course, we can't instantiate member template classes, since we
22948 don't have any arguments for them. Note that the standard is
22949 unclear on whether the instantiation of the members are
22950 *explicit* instantiations or not. However, the most natural
22951 interpretation is that it should be an explicit
22952 instantiation. */
22953 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22954 if ((VAR_P (fld)
22955 || (TREE_CODE (fld) == FUNCTION_DECL
22956 && !static_p
22957 && user_provided_p (fld)))
22958 && DECL_TEMPLATE_INSTANTIATION (fld))
22959 {
22960 mark_decl_instantiated (fld, extern_p);
22961 if (! extern_p)
22962 instantiate_decl (fld, /*defer_ok=*/true,
22963 /*expl_inst_class_mem_p=*/true);
22964 }
22965
22966 if (CLASSTYPE_NESTED_UTDS (t))
22967 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22968 bt_instantiate_type_proc, &storage);
22969 }
22970
22971 /* Given a function DECL, which is a specialization of TMPL, modify
22972 DECL to be a re-instantiation of TMPL with the same template
22973 arguments. TMPL should be the template into which tsubst'ing
22974 should occur for DECL, not the most general template.
22975
22976 One reason for doing this is a scenario like this:
22977
22978 template <class T>
22979 void f(const T&, int i);
22980
22981 void g() { f(3, 7); }
22982
22983 template <class T>
22984 void f(const T& t, const int i) { }
22985
22986 Note that when the template is first instantiated, with
22987 instantiate_template, the resulting DECL will have no name for the
22988 first parameter, and the wrong type for the second. So, when we go
22989 to instantiate the DECL, we regenerate it. */
22990
22991 static void
22992 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22993 {
22994 /* The arguments used to instantiate DECL, from the most general
22995 template. */
22996 tree code_pattern;
22997
22998 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22999
23000 /* Make sure that we can see identifiers, and compute access
23001 correctly. */
23002 push_access_scope (decl);
23003
23004 if (TREE_CODE (decl) == FUNCTION_DECL)
23005 {
23006 tree decl_parm;
23007 tree pattern_parm;
23008 tree specs;
23009 int args_depth;
23010 int parms_depth;
23011
23012 args_depth = TMPL_ARGS_DEPTH (args);
23013 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
23014 if (args_depth > parms_depth)
23015 args = get_innermost_template_args (args, parms_depth);
23016
23017 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
23018 args, tf_error, NULL_TREE,
23019 /*defer_ok*/false);
23020 if (specs && specs != error_mark_node)
23021 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
23022 specs);
23023
23024 /* Merge parameter declarations. */
23025 decl_parm = skip_artificial_parms_for (decl,
23026 DECL_ARGUMENTS (decl));
23027 pattern_parm
23028 = skip_artificial_parms_for (code_pattern,
23029 DECL_ARGUMENTS (code_pattern));
23030 while (decl_parm && !DECL_PACK_P (pattern_parm))
23031 {
23032 tree parm_type;
23033 tree attributes;
23034
23035 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23036 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
23037 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
23038 NULL_TREE);
23039 parm_type = type_decays_to (parm_type);
23040 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23041 TREE_TYPE (decl_parm) = parm_type;
23042 attributes = DECL_ATTRIBUTES (pattern_parm);
23043 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23044 {
23045 DECL_ATTRIBUTES (decl_parm) = attributes;
23046 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23047 }
23048 decl_parm = DECL_CHAIN (decl_parm);
23049 pattern_parm = DECL_CHAIN (pattern_parm);
23050 }
23051 /* Merge any parameters that match with the function parameter
23052 pack. */
23053 if (pattern_parm && DECL_PACK_P (pattern_parm))
23054 {
23055 int i, len;
23056 tree expanded_types;
23057 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23058 the parameters in this function parameter pack. */
23059 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
23060 args, tf_error, NULL_TREE);
23061 len = TREE_VEC_LENGTH (expanded_types);
23062 for (i = 0; i < len; i++)
23063 {
23064 tree parm_type;
23065 tree attributes;
23066
23067 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23068 /* Rename the parameter to include the index. */
23069 DECL_NAME (decl_parm) =
23070 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
23071 parm_type = TREE_VEC_ELT (expanded_types, i);
23072 parm_type = type_decays_to (parm_type);
23073 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23074 TREE_TYPE (decl_parm) = parm_type;
23075 attributes = DECL_ATTRIBUTES (pattern_parm);
23076 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23077 {
23078 DECL_ATTRIBUTES (decl_parm) = attributes;
23079 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23080 }
23081 decl_parm = DECL_CHAIN (decl_parm);
23082 }
23083 }
23084 /* Merge additional specifiers from the CODE_PATTERN. */
23085 if (DECL_DECLARED_INLINE_P (code_pattern)
23086 && !DECL_DECLARED_INLINE_P (decl))
23087 DECL_DECLARED_INLINE_P (decl) = 1;
23088 }
23089 else if (VAR_P (decl))
23090 {
23091 start_lambda_scope (decl);
23092 DECL_INITIAL (decl) =
23093 tsubst_expr (DECL_INITIAL (code_pattern), args,
23094 tf_error, DECL_TI_TEMPLATE (decl),
23095 /*integral_constant_expression_p=*/false);
23096 finish_lambda_scope ();
23097 if (VAR_HAD_UNKNOWN_BOUND (decl))
23098 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
23099 tf_error, DECL_TI_TEMPLATE (decl));
23100 }
23101 else
23102 gcc_unreachable ();
23103
23104 pop_access_scope (decl);
23105 }
23106
23107 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23108 substituted to get DECL. */
23109
23110 tree
23111 template_for_substitution (tree decl)
23112 {
23113 tree tmpl = DECL_TI_TEMPLATE (decl);
23114
23115 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23116 for the instantiation. This is not always the most general
23117 template. Consider, for example:
23118
23119 template <class T>
23120 struct S { template <class U> void f();
23121 template <> void f<int>(); };
23122
23123 and an instantiation of S<double>::f<int>. We want TD to be the
23124 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23125 while (/* An instantiation cannot have a definition, so we need a
23126 more general template. */
23127 DECL_TEMPLATE_INSTANTIATION (tmpl)
23128 /* We must also deal with friend templates. Given:
23129
23130 template <class T> struct S {
23131 template <class U> friend void f() {};
23132 };
23133
23134 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23135 so far as the language is concerned, but that's still
23136 where we get the pattern for the instantiation from. On
23137 other hand, if the definition comes outside the class, say:
23138
23139 template <class T> struct S {
23140 template <class U> friend void f();
23141 };
23142 template <class U> friend void f() {}
23143
23144 we don't need to look any further. That's what the check for
23145 DECL_INITIAL is for. */
23146 || (TREE_CODE (decl) == FUNCTION_DECL
23147 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23148 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23149 {
23150 /* The present template, TD, should not be a definition. If it
23151 were a definition, we should be using it! Note that we
23152 cannot restructure the loop to just keep going until we find
23153 a template with a definition, since that might go too far if
23154 a specialization was declared, but not defined. */
23155
23156 /* Fetch the more general template. */
23157 tmpl = DECL_TI_TEMPLATE (tmpl);
23158 }
23159
23160 return tmpl;
23161 }
23162
23163 /* Returns true if we need to instantiate this template instance even if we
23164 know we aren't going to emit it. */
23165
23166 bool
23167 always_instantiate_p (tree decl)
23168 {
23169 /* We always instantiate inline functions so that we can inline them. An
23170 explicit instantiation declaration prohibits implicit instantiation of
23171 non-inline functions. With high levels of optimization, we would
23172 normally inline non-inline functions -- but we're not allowed to do
23173 that for "extern template" functions. Therefore, we check
23174 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23175 return ((TREE_CODE (decl) == FUNCTION_DECL
23176 && (DECL_DECLARED_INLINE_P (decl)
23177 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23178 /* And we need to instantiate static data members so that
23179 their initializers are available in integral constant
23180 expressions. */
23181 || (VAR_P (decl)
23182 && decl_maybe_constant_var_p (decl)));
23183 }
23184
23185 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23186 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23187 error, true otherwise. */
23188
23189 bool
23190 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23191 {
23192 tree fntype, spec, noex, clone;
23193
23194 /* Don't instantiate a noexcept-specification from template context. */
23195 if (processing_template_decl)
23196 return true;
23197
23198 if (DECL_CLONED_FUNCTION_P (fn))
23199 fn = DECL_CLONED_FUNCTION (fn);
23200 fntype = TREE_TYPE (fn);
23201 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23202
23203 if (!spec || !TREE_PURPOSE (spec))
23204 return true;
23205
23206 noex = TREE_PURPOSE (spec);
23207
23208 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23209 {
23210 static hash_set<tree>* fns = new hash_set<tree>;
23211 bool added = false;
23212 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23213 spec = get_defaulted_eh_spec (fn, complain);
23214 else if (!(added = !fns->add (fn)))
23215 {
23216 /* If hash_set::add returns true, the element was already there. */
23217 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
23218 DECL_SOURCE_LOCATION (fn));
23219 error_at (loc,
23220 "exception specification of %qD depends on itself",
23221 fn);
23222 spec = noexcept_false_spec;
23223 }
23224 else if (push_tinst_level (fn))
23225 {
23226 push_access_scope (fn);
23227 push_deferring_access_checks (dk_no_deferred);
23228 input_location = DECL_SOURCE_LOCATION (fn);
23229 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23230 DEFERRED_NOEXCEPT_ARGS (noex),
23231 tf_warning_or_error, fn,
23232 /*function_p=*/false,
23233 /*integral_constant_expression_p=*/true);
23234 pop_deferring_access_checks ();
23235 pop_access_scope (fn);
23236 pop_tinst_level ();
23237 spec = build_noexcept_spec (noex, tf_warning_or_error);
23238 if (spec == error_mark_node)
23239 spec = noexcept_false_spec;
23240 }
23241 else
23242 spec = noexcept_false_spec;
23243
23244 if (added)
23245 fns->remove (fn);
23246
23247 if (spec == error_mark_node)
23248 return false;
23249
23250 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23251 }
23252
23253 FOR_EACH_CLONE (clone, fn)
23254 {
23255 if (TREE_TYPE (clone) == fntype)
23256 TREE_TYPE (clone) = TREE_TYPE (fn);
23257 else
23258 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23259 }
23260
23261 return true;
23262 }
23263
23264 /* We're starting to process the function INST, an instantiation of PATTERN;
23265 add their parameters to local_specializations. */
23266
23267 static void
23268 register_parameter_specializations (tree pattern, tree inst)
23269 {
23270 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23271 tree spec_parm = DECL_ARGUMENTS (inst);
23272 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23273 {
23274 register_local_specialization (spec_parm, tmpl_parm);
23275 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23276 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23277 }
23278 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23279 {
23280 if (!DECL_PACK_P (tmpl_parm))
23281 {
23282 register_local_specialization (spec_parm, tmpl_parm);
23283 spec_parm = DECL_CHAIN (spec_parm);
23284 }
23285 else
23286 {
23287 /* Register the (value) argument pack as a specialization of
23288 TMPL_PARM, then move on. */
23289 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23290 register_local_specialization (argpack, tmpl_parm);
23291 }
23292 }
23293 gcc_assert (!spec_parm);
23294 }
23295
23296 /* Produce the definition of D, a _DECL generated from a template. If
23297 DEFER_OK is true, then we don't have to actually do the
23298 instantiation now; we just have to do it sometime. Normally it is
23299 an error if this is an explicit instantiation but D is undefined.
23300 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23301 instantiated class template. */
23302
23303 tree
23304 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23305 {
23306 tree tmpl = DECL_TI_TEMPLATE (d);
23307 tree gen_args;
23308 tree args;
23309 tree td;
23310 tree code_pattern;
23311 tree spec;
23312 tree gen_tmpl;
23313 bool pattern_defined;
23314 location_t saved_loc = input_location;
23315 int saved_unevaluated_operand = cp_unevaluated_operand;
23316 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23317 bool external_p;
23318 bool deleted_p;
23319
23320 /* This function should only be used to instantiate templates for
23321 functions and static member variables. */
23322 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23323
23324 /* A concept is never instantiated. */
23325 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23326
23327 /* Variables are never deferred; if instantiation is required, they
23328 are instantiated right away. That allows for better code in the
23329 case that an expression refers to the value of the variable --
23330 if the variable has a constant value the referring expression can
23331 take advantage of that fact. */
23332 if (VAR_P (d))
23333 defer_ok = false;
23334
23335 /* Don't instantiate cloned functions. Instead, instantiate the
23336 functions they cloned. */
23337 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23338 d = DECL_CLONED_FUNCTION (d);
23339
23340 if (DECL_TEMPLATE_INSTANTIATED (d)
23341 || (TREE_CODE (d) == FUNCTION_DECL
23342 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23343 || DECL_TEMPLATE_SPECIALIZATION (d))
23344 /* D has already been instantiated or explicitly specialized, so
23345 there's nothing for us to do here.
23346
23347 It might seem reasonable to check whether or not D is an explicit
23348 instantiation, and, if so, stop here. But when an explicit
23349 instantiation is deferred until the end of the compilation,
23350 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23351 the instantiation. */
23352 return d;
23353
23354 /* Check to see whether we know that this template will be
23355 instantiated in some other file, as with "extern template"
23356 extension. */
23357 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23358
23359 /* In general, we do not instantiate such templates. */
23360 if (external_p && !always_instantiate_p (d))
23361 return d;
23362
23363 gen_tmpl = most_general_template (tmpl);
23364 gen_args = DECL_TI_ARGS (d);
23365
23366 if (tmpl != gen_tmpl)
23367 /* We should already have the extra args. */
23368 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23369 == TMPL_ARGS_DEPTH (gen_args));
23370 /* And what's in the hash table should match D. */
23371 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23372 || spec == NULL_TREE);
23373
23374 /* This needs to happen before any tsubsting. */
23375 if (! push_tinst_level (d))
23376 return d;
23377
23378 timevar_push (TV_TEMPLATE_INST);
23379
23380 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23381 for the instantiation. */
23382 td = template_for_substitution (d);
23383 args = gen_args;
23384
23385 if (VAR_P (d))
23386 {
23387 /* Look up an explicit specialization, if any. */
23388 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23389 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23390 if (elt && elt != error_mark_node)
23391 {
23392 td = TREE_VALUE (elt);
23393 args = TREE_PURPOSE (elt);
23394 }
23395 }
23396
23397 code_pattern = DECL_TEMPLATE_RESULT (td);
23398
23399 /* We should never be trying to instantiate a member of a class
23400 template or partial specialization. */
23401 gcc_assert (d != code_pattern);
23402
23403 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23404 || DECL_TEMPLATE_SPECIALIZATION (td))
23405 /* In the case of a friend template whose definition is provided
23406 outside the class, we may have too many arguments. Drop the
23407 ones we don't need. The same is true for specializations. */
23408 args = get_innermost_template_args
23409 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23410
23411 if (TREE_CODE (d) == FUNCTION_DECL)
23412 {
23413 deleted_p = DECL_DELETED_FN (code_pattern);
23414 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23415 && DECL_INITIAL (code_pattern) != error_mark_node)
23416 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23417 || deleted_p);
23418 }
23419 else
23420 {
23421 deleted_p = false;
23422 if (DECL_CLASS_SCOPE_P (code_pattern))
23423 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23424 || DECL_INLINE_VAR_P (code_pattern));
23425 else
23426 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23427 }
23428
23429 /* We may be in the middle of deferred access check. Disable it now. */
23430 push_deferring_access_checks (dk_no_deferred);
23431
23432 /* Unless an explicit instantiation directive has already determined
23433 the linkage of D, remember that a definition is available for
23434 this entity. */
23435 if (pattern_defined
23436 && !DECL_INTERFACE_KNOWN (d)
23437 && !DECL_NOT_REALLY_EXTERN (d))
23438 mark_definable (d);
23439
23440 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23441 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23442 input_location = DECL_SOURCE_LOCATION (d);
23443
23444 /* If D is a member of an explicitly instantiated class template,
23445 and no definition is available, treat it like an implicit
23446 instantiation. */
23447 if (!pattern_defined && expl_inst_class_mem_p
23448 && DECL_EXPLICIT_INSTANTIATION (d))
23449 {
23450 /* Leave linkage flags alone on instantiations with anonymous
23451 visibility. */
23452 if (TREE_PUBLIC (d))
23453 {
23454 DECL_NOT_REALLY_EXTERN (d) = 0;
23455 DECL_INTERFACE_KNOWN (d) = 0;
23456 }
23457 SET_DECL_IMPLICIT_INSTANTIATION (d);
23458 }
23459
23460 /* Defer all other templates, unless we have been explicitly
23461 forbidden from doing so. */
23462 if (/* If there is no definition, we cannot instantiate the
23463 template. */
23464 ! pattern_defined
23465 /* If it's OK to postpone instantiation, do so. */
23466 || defer_ok
23467 /* If this is a static data member that will be defined
23468 elsewhere, we don't want to instantiate the entire data
23469 member, but we do want to instantiate the initializer so that
23470 we can substitute that elsewhere. */
23471 || (external_p && VAR_P (d))
23472 /* Handle here a deleted function too, avoid generating
23473 its body (c++/61080). */
23474 || deleted_p)
23475 {
23476 /* The definition of the static data member is now required so
23477 we must substitute the initializer. */
23478 if (VAR_P (d)
23479 && !DECL_INITIAL (d)
23480 && DECL_INITIAL (code_pattern))
23481 {
23482 tree ns;
23483 tree init;
23484 bool const_init = false;
23485 bool enter_context = DECL_CLASS_SCOPE_P (d);
23486
23487 ns = decl_namespace_context (d);
23488 push_nested_namespace (ns);
23489 if (enter_context)
23490 push_nested_class (DECL_CONTEXT (d));
23491 init = tsubst_expr (DECL_INITIAL (code_pattern),
23492 args,
23493 tf_warning_or_error, NULL_TREE,
23494 /*integral_constant_expression_p=*/false);
23495 /* If instantiating the initializer involved instantiating this
23496 again, don't call cp_finish_decl twice. */
23497 if (!DECL_INITIAL (d))
23498 {
23499 /* Make sure the initializer is still constant, in case of
23500 circular dependency (template/instantiate6.C). */
23501 const_init
23502 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23503 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23504 /*asmspec_tree=*/NULL_TREE,
23505 LOOKUP_ONLYCONVERTING);
23506 }
23507 if (enter_context)
23508 pop_nested_class ();
23509 pop_nested_namespace (ns);
23510 }
23511
23512 /* We restore the source position here because it's used by
23513 add_pending_template. */
23514 input_location = saved_loc;
23515
23516 if (at_eof && !pattern_defined
23517 && DECL_EXPLICIT_INSTANTIATION (d)
23518 && DECL_NOT_REALLY_EXTERN (d))
23519 /* [temp.explicit]
23520
23521 The definition of a non-exported function template, a
23522 non-exported member function template, or a non-exported
23523 member function or static data member of a class template
23524 shall be present in every translation unit in which it is
23525 explicitly instantiated. */
23526 permerror (input_location, "explicit instantiation of %qD "
23527 "but no definition available", d);
23528
23529 /* If we're in unevaluated context, we just wanted to get the
23530 constant value; this isn't an odr use, so don't queue
23531 a full instantiation. */
23532 if (cp_unevaluated_operand != 0)
23533 goto out;
23534 /* ??? Historically, we have instantiated inline functions, even
23535 when marked as "extern template". */
23536 if (!(external_p && VAR_P (d)))
23537 add_pending_template (d);
23538 goto out;
23539 }
23540 /* Tell the repository that D is available in this translation unit
23541 -- and see if it is supposed to be instantiated here. */
23542 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23543 {
23544 /* In a PCH file, despite the fact that the repository hasn't
23545 requested instantiation in the PCH it is still possible that
23546 an instantiation will be required in a file that includes the
23547 PCH. */
23548 if (pch_file)
23549 add_pending_template (d);
23550 /* Instantiate inline functions so that the inliner can do its
23551 job, even though we'll not be emitting a copy of this
23552 function. */
23553 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23554 goto out;
23555 }
23556
23557 bool push_to_top, nested;
23558 tree fn_context;
23559 fn_context = decl_function_context (d);
23560 if (LAMBDA_FUNCTION_P (d))
23561 /* tsubst_lambda_expr resolved any references to enclosing functions. */
23562 fn_context = NULL_TREE;
23563 nested = current_function_decl != NULL_TREE;
23564 push_to_top = !(nested && fn_context == current_function_decl);
23565
23566 vec<tree> omp_privatization_save;
23567 if (nested)
23568 save_omp_privatization_clauses (omp_privatization_save);
23569
23570 if (push_to_top)
23571 push_to_top_level ();
23572 else
23573 {
23574 push_function_context ();
23575 cp_unevaluated_operand = 0;
23576 c_inhibit_evaluation_warnings = 0;
23577 }
23578
23579 /* Mark D as instantiated so that recursive calls to
23580 instantiate_decl do not try to instantiate it again. */
23581 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23582
23583 /* Regenerate the declaration in case the template has been modified
23584 by a subsequent redeclaration. */
23585 regenerate_decl_from_template (d, td, args);
23586
23587 /* We already set the file and line above. Reset them now in case
23588 they changed as a result of calling regenerate_decl_from_template. */
23589 input_location = DECL_SOURCE_LOCATION (d);
23590
23591 if (VAR_P (d))
23592 {
23593 tree init;
23594 bool const_init = false;
23595
23596 /* Clear out DECL_RTL; whatever was there before may not be right
23597 since we've reset the type of the declaration. */
23598 SET_DECL_RTL (d, NULL);
23599 DECL_IN_AGGR_P (d) = 0;
23600
23601 /* The initializer is placed in DECL_INITIAL by
23602 regenerate_decl_from_template so we don't need to
23603 push/pop_access_scope again here. Pull it out so that
23604 cp_finish_decl can process it. */
23605 init = DECL_INITIAL (d);
23606 DECL_INITIAL (d) = NULL_TREE;
23607 DECL_INITIALIZED_P (d) = 0;
23608
23609 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23610 initializer. That function will defer actual emission until
23611 we have a chance to determine linkage. */
23612 DECL_EXTERNAL (d) = 0;
23613
23614 /* Enter the scope of D so that access-checking works correctly. */
23615 bool enter_context = DECL_CLASS_SCOPE_P (d);
23616 if (enter_context)
23617 push_nested_class (DECL_CONTEXT (d));
23618
23619 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23620 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23621
23622 if (enter_context)
23623 pop_nested_class ();
23624
23625 if (variable_template_p (gen_tmpl))
23626 note_variable_template_instantiation (d);
23627 }
23628 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23629 synthesize_method (d);
23630 else if (TREE_CODE (d) == FUNCTION_DECL)
23631 {
23632 /* Set up the list of local specializations. */
23633 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23634 tree block = NULL_TREE;
23635
23636 /* Set up context. */
23637 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23638 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23639 block = push_stmt_list ();
23640 else
23641 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23642
23643 /* Some typedefs referenced from within the template code need to be
23644 access checked at template instantiation time, i.e now. These
23645 types were added to the template at parsing time. Let's get those
23646 and perform the access checks then. */
23647 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23648 args);
23649
23650 /* Create substitution entries for the parameters. */
23651 register_parameter_specializations (code_pattern, d);
23652
23653 /* Substitute into the body of the function. */
23654 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23655 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23656 tf_warning_or_error, tmpl);
23657 else
23658 {
23659 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23660 tf_warning_or_error, tmpl,
23661 /*integral_constant_expression_p=*/false);
23662
23663 /* Set the current input_location to the end of the function
23664 so that finish_function knows where we are. */
23665 input_location
23666 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23667
23668 /* Remember if we saw an infinite loop in the template. */
23669 current_function_infinite_loop
23670 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23671 }
23672
23673 /* Finish the function. */
23674 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23675 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23676 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23677 else
23678 {
23679 d = finish_function (/*inline_p=*/false);
23680 expand_or_defer_fn (d);
23681 }
23682
23683 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23684 cp_check_omp_declare_reduction (d);
23685 }
23686
23687 /* We're not deferring instantiation any more. */
23688 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23689
23690 if (push_to_top)
23691 pop_from_top_level ();
23692 else
23693 pop_function_context ();
23694
23695 if (nested)
23696 restore_omp_privatization_clauses (omp_privatization_save);
23697
23698 out:
23699 pop_deferring_access_checks ();
23700 timevar_pop (TV_TEMPLATE_INST);
23701 pop_tinst_level ();
23702 input_location = saved_loc;
23703 cp_unevaluated_operand = saved_unevaluated_operand;
23704 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23705
23706 return d;
23707 }
23708
23709 /* Run through the list of templates that we wish we could
23710 instantiate, and instantiate any we can. RETRIES is the
23711 number of times we retry pending template instantiation. */
23712
23713 void
23714 instantiate_pending_templates (int retries)
23715 {
23716 int reconsider;
23717 location_t saved_loc = input_location;
23718
23719 /* Instantiating templates may trigger vtable generation. This in turn
23720 may require further template instantiations. We place a limit here
23721 to avoid infinite loop. */
23722 if (pending_templates && retries >= max_tinst_depth)
23723 {
23724 tree decl = pending_templates->tinst->decl;
23725
23726 fatal_error (input_location,
23727 "template instantiation depth exceeds maximum of %d"
23728 " instantiating %q+D, possibly from virtual table generation"
23729 " (use -ftemplate-depth= to increase the maximum)",
23730 max_tinst_depth, decl);
23731 if (TREE_CODE (decl) == FUNCTION_DECL)
23732 /* Pretend that we defined it. */
23733 DECL_INITIAL (decl) = error_mark_node;
23734 return;
23735 }
23736
23737 do
23738 {
23739 struct pending_template **t = &pending_templates;
23740 struct pending_template *last = NULL;
23741 reconsider = 0;
23742 while (*t)
23743 {
23744 tree instantiation = reopen_tinst_level ((*t)->tinst);
23745 bool complete = false;
23746
23747 if (TYPE_P (instantiation))
23748 {
23749 if (!COMPLETE_TYPE_P (instantiation))
23750 {
23751 instantiate_class_template (instantiation);
23752 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23753 for (tree fld = TYPE_FIELDS (instantiation);
23754 fld; fld = TREE_CHAIN (fld))
23755 if ((VAR_P (fld)
23756 || (TREE_CODE (fld) == FUNCTION_DECL
23757 && !DECL_ARTIFICIAL (fld)))
23758 && DECL_TEMPLATE_INSTANTIATION (fld))
23759 instantiate_decl (fld,
23760 /*defer_ok=*/false,
23761 /*expl_inst_class_mem_p=*/false);
23762
23763 if (COMPLETE_TYPE_P (instantiation))
23764 reconsider = 1;
23765 }
23766
23767 complete = COMPLETE_TYPE_P (instantiation);
23768 }
23769 else
23770 {
23771 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23772 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23773 {
23774 instantiation
23775 = instantiate_decl (instantiation,
23776 /*defer_ok=*/false,
23777 /*expl_inst_class_mem_p=*/false);
23778 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23779 reconsider = 1;
23780 }
23781
23782 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23783 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23784 }
23785
23786 if (complete)
23787 /* If INSTANTIATION has been instantiated, then we don't
23788 need to consider it again in the future. */
23789 *t = (*t)->next;
23790 else
23791 {
23792 last = *t;
23793 t = &(*t)->next;
23794 }
23795 tinst_depth = 0;
23796 current_tinst_level = NULL;
23797 }
23798 last_pending_template = last;
23799 }
23800 while (reconsider);
23801
23802 input_location = saved_loc;
23803 }
23804
23805 /* Substitute ARGVEC into T, which is a list of initializers for
23806 either base class or a non-static data member. The TREE_PURPOSEs
23807 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23808 instantiate_decl. */
23809
23810 static tree
23811 tsubst_initializer_list (tree t, tree argvec)
23812 {
23813 tree inits = NULL_TREE;
23814 tree target_ctor = error_mark_node;
23815
23816 for (; t; t = TREE_CHAIN (t))
23817 {
23818 tree decl;
23819 tree init;
23820 tree expanded_bases = NULL_TREE;
23821 tree expanded_arguments = NULL_TREE;
23822 int i, len = 1;
23823
23824 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23825 {
23826 tree expr;
23827 tree arg;
23828
23829 /* Expand the base class expansion type into separate base
23830 classes. */
23831 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23832 tf_warning_or_error,
23833 NULL_TREE);
23834 if (expanded_bases == error_mark_node)
23835 continue;
23836
23837 /* We'll be building separate TREE_LISTs of arguments for
23838 each base. */
23839 len = TREE_VEC_LENGTH (expanded_bases);
23840 expanded_arguments = make_tree_vec (len);
23841 for (i = 0; i < len; i++)
23842 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23843
23844 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23845 expand each argument in the TREE_VALUE of t. */
23846 expr = make_node (EXPR_PACK_EXPANSION);
23847 PACK_EXPANSION_LOCAL_P (expr) = true;
23848 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23849 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23850
23851 if (TREE_VALUE (t) == void_type_node)
23852 /* VOID_TYPE_NODE is used to indicate
23853 value-initialization. */
23854 {
23855 for (i = 0; i < len; i++)
23856 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23857 }
23858 else
23859 {
23860 /* Substitute parameter packs into each argument in the
23861 TREE_LIST. */
23862 in_base_initializer = 1;
23863 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23864 {
23865 tree expanded_exprs;
23866
23867 /* Expand the argument. */
23868 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23869 expanded_exprs
23870 = tsubst_pack_expansion (expr, argvec,
23871 tf_warning_or_error,
23872 NULL_TREE);
23873 if (expanded_exprs == error_mark_node)
23874 continue;
23875
23876 /* Prepend each of the expanded expressions to the
23877 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23878 for (i = 0; i < len; i++)
23879 {
23880 TREE_VEC_ELT (expanded_arguments, i) =
23881 tree_cons (NULL_TREE,
23882 TREE_VEC_ELT (expanded_exprs, i),
23883 TREE_VEC_ELT (expanded_arguments, i));
23884 }
23885 }
23886 in_base_initializer = 0;
23887
23888 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23889 since we built them backwards. */
23890 for (i = 0; i < len; i++)
23891 {
23892 TREE_VEC_ELT (expanded_arguments, i) =
23893 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23894 }
23895 }
23896 }
23897
23898 for (i = 0; i < len; ++i)
23899 {
23900 if (expanded_bases)
23901 {
23902 decl = TREE_VEC_ELT (expanded_bases, i);
23903 decl = expand_member_init (decl);
23904 init = TREE_VEC_ELT (expanded_arguments, i);
23905 }
23906 else
23907 {
23908 tree tmp;
23909 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23910 tf_warning_or_error, NULL_TREE);
23911
23912 decl = expand_member_init (decl);
23913 if (decl && !DECL_P (decl))
23914 in_base_initializer = 1;
23915
23916 init = TREE_VALUE (t);
23917 tmp = init;
23918 if (init != void_type_node)
23919 init = tsubst_expr (init, argvec,
23920 tf_warning_or_error, NULL_TREE,
23921 /*integral_constant_expression_p=*/false);
23922 if (init == NULL_TREE && tmp != NULL_TREE)
23923 /* If we had an initializer but it instantiated to nothing,
23924 value-initialize the object. This will only occur when
23925 the initializer was a pack expansion where the parameter
23926 packs used in that expansion were of length zero. */
23927 init = void_type_node;
23928 in_base_initializer = 0;
23929 }
23930
23931 if (target_ctor != error_mark_node
23932 && init != error_mark_node)
23933 {
23934 error ("mem-initializer for %qD follows constructor delegation",
23935 decl);
23936 return inits;
23937 }
23938 /* Look for a target constructor. */
23939 if (init != error_mark_node
23940 && decl && CLASS_TYPE_P (decl)
23941 && same_type_p (decl, current_class_type))
23942 {
23943 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
23944 if (inits)
23945 {
23946 error ("constructor delegation follows mem-initializer for %qD",
23947 TREE_PURPOSE (inits));
23948 continue;
23949 }
23950 target_ctor = init;
23951 }
23952
23953 if (decl)
23954 {
23955 init = build_tree_list (decl, init);
23956 TREE_CHAIN (init) = inits;
23957 inits = init;
23958 }
23959 }
23960 }
23961 return inits;
23962 }
23963
23964 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23965
23966 static void
23967 set_current_access_from_decl (tree decl)
23968 {
23969 if (TREE_PRIVATE (decl))
23970 current_access_specifier = access_private_node;
23971 else if (TREE_PROTECTED (decl))
23972 current_access_specifier = access_protected_node;
23973 else
23974 current_access_specifier = access_public_node;
23975 }
23976
23977 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23978 is the instantiation (which should have been created with
23979 start_enum) and ARGS are the template arguments to use. */
23980
23981 static void
23982 tsubst_enum (tree tag, tree newtag, tree args)
23983 {
23984 tree e;
23985
23986 if (SCOPED_ENUM_P (newtag))
23987 begin_scope (sk_scoped_enum, newtag);
23988
23989 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23990 {
23991 tree value;
23992 tree decl;
23993
23994 decl = TREE_VALUE (e);
23995 /* Note that in a template enum, the TREE_VALUE is the
23996 CONST_DECL, not the corresponding INTEGER_CST. */
23997 value = tsubst_expr (DECL_INITIAL (decl),
23998 args, tf_warning_or_error, NULL_TREE,
23999 /*integral_constant_expression_p=*/true);
24000
24001 /* Give this enumeration constant the correct access. */
24002 set_current_access_from_decl (decl);
24003
24004 /* Actually build the enumerator itself. Here we're assuming that
24005 enumerators can't have dependent attributes. */
24006 build_enumerator (DECL_NAME (decl), value, newtag,
24007 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
24008 }
24009
24010 if (SCOPED_ENUM_P (newtag))
24011 finish_scope ();
24012
24013 finish_enum_value_list (newtag);
24014 finish_enum (newtag);
24015
24016 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
24017 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
24018 }
24019
24020 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24021 its type -- but without substituting the innermost set of template
24022 arguments. So, innermost set of template parameters will appear in
24023 the type. */
24024
24025 tree
24026 get_mostly_instantiated_function_type (tree decl)
24027 {
24028 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24029 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
24030 }
24031
24032 /* Return truthvalue if we're processing a template different from
24033 the last one involved in diagnostics. */
24034 bool
24035 problematic_instantiation_changed (void)
24036 {
24037 return current_tinst_level != last_error_tinst_level;
24038 }
24039
24040 /* Remember current template involved in diagnostics. */
24041 void
24042 record_last_problematic_instantiation (void)
24043 {
24044 last_error_tinst_level = current_tinst_level;
24045 }
24046
24047 struct tinst_level *
24048 current_instantiation (void)
24049 {
24050 return current_tinst_level;
24051 }
24052
24053 /* Return TRUE if current_function_decl is being instantiated, false
24054 otherwise. */
24055
24056 bool
24057 instantiating_current_function_p (void)
24058 {
24059 return (current_instantiation ()
24060 && current_instantiation ()->decl == current_function_decl);
24061 }
24062
24063 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24064 type. Return false for ok, true for disallowed. Issue error and
24065 inform messages under control of COMPLAIN. */
24066
24067 static bool
24068 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
24069 {
24070 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
24071 return false;
24072 else if (TYPE_PTR_P (type))
24073 return false;
24074 else if (TREE_CODE (type) == REFERENCE_TYPE
24075 && !TYPE_REF_IS_RVALUE (type))
24076 return false;
24077 else if (TYPE_PTRMEM_P (type))
24078 return false;
24079 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
24080 return false;
24081 else if (TREE_CODE (type) == TYPENAME_TYPE)
24082 return false;
24083 else if (TREE_CODE (type) == DECLTYPE_TYPE)
24084 return false;
24085 else if (TREE_CODE (type) == NULLPTR_TYPE)
24086 return false;
24087 /* A bound template template parm could later be instantiated to have a valid
24088 nontype parm type via an alias template. */
24089 else if (cxx_dialect >= cxx11
24090 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24091 return false;
24092
24093 if (complain & tf_error)
24094 {
24095 if (type == error_mark_node)
24096 inform (input_location, "invalid template non-type parameter");
24097 else
24098 error ("%q#T is not a valid type for a template non-type parameter",
24099 type);
24100 }
24101 return true;
24102 }
24103
24104 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24105 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24106
24107 static bool
24108 dependent_type_p_r (tree type)
24109 {
24110 tree scope;
24111
24112 /* [temp.dep.type]
24113
24114 A type is dependent if it is:
24115
24116 -- a template parameter. Template template parameters are types
24117 for us (since TYPE_P holds true for them) so we handle
24118 them here. */
24119 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24120 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
24121 return true;
24122 /* -- a qualified-id with a nested-name-specifier which contains a
24123 class-name that names a dependent type or whose unqualified-id
24124 names a dependent type. */
24125 if (TREE_CODE (type) == TYPENAME_TYPE)
24126 return true;
24127
24128 /* An alias template specialization can be dependent even if the
24129 resulting type is not. */
24130 if (dependent_alias_template_spec_p (type))
24131 return true;
24132
24133 /* -- a cv-qualified type where the cv-unqualified type is
24134 dependent.
24135 No code is necessary for this bullet; the code below handles
24136 cv-qualified types, and we don't want to strip aliases with
24137 TYPE_MAIN_VARIANT because of DR 1558. */
24138 /* -- a compound type constructed from any dependent type. */
24139 if (TYPE_PTRMEM_P (type))
24140 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24141 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24142 (type)));
24143 else if (TYPE_PTR_P (type)
24144 || TREE_CODE (type) == REFERENCE_TYPE)
24145 return dependent_type_p (TREE_TYPE (type));
24146 else if (TREE_CODE (type) == FUNCTION_TYPE
24147 || TREE_CODE (type) == METHOD_TYPE)
24148 {
24149 tree arg_type;
24150
24151 if (dependent_type_p (TREE_TYPE (type)))
24152 return true;
24153 for (arg_type = TYPE_ARG_TYPES (type);
24154 arg_type;
24155 arg_type = TREE_CHAIN (arg_type))
24156 if (dependent_type_p (TREE_VALUE (arg_type)))
24157 return true;
24158 if (cxx_dialect >= cxx17)
24159 /* A value-dependent noexcept-specifier makes the type dependent. */
24160 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24161 if (tree noex = TREE_PURPOSE (spec))
24162 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24163 affect overload resolution and treating it as dependent breaks
24164 things. */
24165 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24166 && value_dependent_expression_p (noex))
24167 return true;
24168 return false;
24169 }
24170 /* -- an array type constructed from any dependent type or whose
24171 size is specified by a constant expression that is
24172 value-dependent.
24173
24174 We checked for type- and value-dependence of the bounds in
24175 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24176 if (TREE_CODE (type) == ARRAY_TYPE)
24177 {
24178 if (TYPE_DOMAIN (type)
24179 && dependent_type_p (TYPE_DOMAIN (type)))
24180 return true;
24181 return dependent_type_p (TREE_TYPE (type));
24182 }
24183
24184 /* -- a template-id in which either the template name is a template
24185 parameter ... */
24186 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24187 return true;
24188 /* ... or any of the template arguments is a dependent type or
24189 an expression that is type-dependent or value-dependent. */
24190 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24191 && (any_dependent_template_arguments_p
24192 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24193 return true;
24194
24195 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24196 dependent; if the argument of the `typeof' expression is not
24197 type-dependent, then it should already been have resolved. */
24198 if (TREE_CODE (type) == TYPEOF_TYPE
24199 || TREE_CODE (type) == DECLTYPE_TYPE
24200 || TREE_CODE (type) == UNDERLYING_TYPE)
24201 return true;
24202
24203 /* A template argument pack is dependent if any of its packed
24204 arguments are. */
24205 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24206 {
24207 tree args = ARGUMENT_PACK_ARGS (type);
24208 int i, len = TREE_VEC_LENGTH (args);
24209 for (i = 0; i < len; ++i)
24210 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24211 return true;
24212 }
24213
24214 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24215 be template parameters. */
24216 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24217 return true;
24218
24219 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24220 return true;
24221
24222 /* The standard does not specifically mention types that are local
24223 to template functions or local classes, but they should be
24224 considered dependent too. For example:
24225
24226 template <int I> void f() {
24227 enum E { a = I };
24228 S<sizeof (E)> s;
24229 }
24230
24231 The size of `E' cannot be known until the value of `I' has been
24232 determined. Therefore, `E' must be considered dependent. */
24233 scope = TYPE_CONTEXT (type);
24234 if (scope && TYPE_P (scope))
24235 return dependent_type_p (scope);
24236 /* Don't use type_dependent_expression_p here, as it can lead
24237 to infinite recursion trying to determine whether a lambda
24238 nested in a lambda is dependent (c++/47687). */
24239 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24240 && DECL_LANG_SPECIFIC (scope)
24241 && DECL_TEMPLATE_INFO (scope)
24242 && (any_dependent_template_arguments_p
24243 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24244 return true;
24245
24246 /* Other types are non-dependent. */
24247 return false;
24248 }
24249
24250 /* Returns TRUE if TYPE is dependent, in the sense of
24251 [temp.dep.type]. Note that a NULL type is considered dependent. */
24252
24253 bool
24254 dependent_type_p (tree type)
24255 {
24256 /* If there are no template parameters in scope, then there can't be
24257 any dependent types. */
24258 if (!processing_template_decl)
24259 {
24260 /* If we are not processing a template, then nobody should be
24261 providing us with a dependent type. */
24262 gcc_assert (type);
24263 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24264 return false;
24265 }
24266
24267 /* If the type is NULL, we have not computed a type for the entity
24268 in question; in that case, the type is dependent. */
24269 if (!type)
24270 return true;
24271
24272 /* Erroneous types can be considered non-dependent. */
24273 if (type == error_mark_node)
24274 return false;
24275
24276 /* Getting here with global_type_node means we improperly called this
24277 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24278 gcc_checking_assert (type != global_type_node);
24279
24280 /* If we have not already computed the appropriate value for TYPE,
24281 do so now. */
24282 if (!TYPE_DEPENDENT_P_VALID (type))
24283 {
24284 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24285 TYPE_DEPENDENT_P_VALID (type) = 1;
24286 }
24287
24288 return TYPE_DEPENDENT_P (type);
24289 }
24290
24291 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24292 lookup. In other words, a dependent type that is not the current
24293 instantiation. */
24294
24295 bool
24296 dependent_scope_p (tree scope)
24297 {
24298 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24299 && !currently_open_class (scope));
24300 }
24301
24302 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24303 an unknown base of 'this' (and is therefore instantiation-dependent). */
24304
24305 static bool
24306 unknown_base_ref_p (tree t)
24307 {
24308 if (!current_class_ptr)
24309 return false;
24310
24311 tree mem = TREE_OPERAND (t, 1);
24312 if (shared_member_p (mem))
24313 return false;
24314
24315 tree cur = current_nonlambda_class_type ();
24316 if (!any_dependent_bases_p (cur))
24317 return false;
24318
24319 tree ctx = TREE_OPERAND (t, 0);
24320 if (DERIVED_FROM_P (ctx, cur))
24321 return false;
24322
24323 return true;
24324 }
24325
24326 /* T is a SCOPE_REF; return whether we need to consider it
24327 instantiation-dependent so that we can check access at instantiation
24328 time even though we know which member it resolves to. */
24329
24330 static bool
24331 instantiation_dependent_scope_ref_p (tree t)
24332 {
24333 if (DECL_P (TREE_OPERAND (t, 1))
24334 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24335 && !unknown_base_ref_p (t)
24336 && accessible_in_template_p (TREE_OPERAND (t, 0),
24337 TREE_OPERAND (t, 1)))
24338 return false;
24339 else
24340 return true;
24341 }
24342
24343 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24344 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24345 expression. */
24346
24347 /* Note that this predicate is not appropriate for general expressions;
24348 only constant expressions (that satisfy potential_constant_expression)
24349 can be tested for value dependence. */
24350
24351 bool
24352 value_dependent_expression_p (tree expression)
24353 {
24354 if (!processing_template_decl || expression == NULL_TREE)
24355 return false;
24356
24357 /* A type-dependent expression is also value-dependent. */
24358 if (type_dependent_expression_p (expression))
24359 return true;
24360
24361 switch (TREE_CODE (expression))
24362 {
24363 case BASELINK:
24364 /* A dependent member function of the current instantiation. */
24365 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24366
24367 case FUNCTION_DECL:
24368 /* A dependent member function of the current instantiation. */
24369 if (DECL_CLASS_SCOPE_P (expression)
24370 && dependent_type_p (DECL_CONTEXT (expression)))
24371 return true;
24372 break;
24373
24374 case IDENTIFIER_NODE:
24375 /* A name that has not been looked up -- must be dependent. */
24376 return true;
24377
24378 case TEMPLATE_PARM_INDEX:
24379 /* A non-type template parm. */
24380 return true;
24381
24382 case CONST_DECL:
24383 /* A non-type template parm. */
24384 if (DECL_TEMPLATE_PARM_P (expression))
24385 return true;
24386 return value_dependent_expression_p (DECL_INITIAL (expression));
24387
24388 case VAR_DECL:
24389 /* A constant with literal type and is initialized
24390 with an expression that is value-dependent. */
24391 if (DECL_DEPENDENT_INIT_P (expression)
24392 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24393 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24394 return true;
24395 if (DECL_HAS_VALUE_EXPR_P (expression))
24396 {
24397 tree value_expr = DECL_VALUE_EXPR (expression);
24398 if (value_dependent_expression_p (value_expr))
24399 return true;
24400 }
24401 return false;
24402
24403 case DYNAMIC_CAST_EXPR:
24404 case STATIC_CAST_EXPR:
24405 case CONST_CAST_EXPR:
24406 case REINTERPRET_CAST_EXPR:
24407 case CAST_EXPR:
24408 case IMPLICIT_CONV_EXPR:
24409 /* These expressions are value-dependent if the type to which
24410 the cast occurs is dependent or the expression being casted
24411 is value-dependent. */
24412 {
24413 tree type = TREE_TYPE (expression);
24414
24415 if (dependent_type_p (type))
24416 return true;
24417
24418 /* A functional cast has a list of operands. */
24419 expression = TREE_OPERAND (expression, 0);
24420 if (!expression)
24421 {
24422 /* If there are no operands, it must be an expression such
24423 as "int()". This should not happen for aggregate types
24424 because it would form non-constant expressions. */
24425 gcc_assert (cxx_dialect >= cxx11
24426 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24427
24428 return false;
24429 }
24430
24431 if (TREE_CODE (expression) == TREE_LIST)
24432 return any_value_dependent_elements_p (expression);
24433
24434 return value_dependent_expression_p (expression);
24435 }
24436
24437 case SIZEOF_EXPR:
24438 if (SIZEOF_EXPR_TYPE_P (expression))
24439 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24440 /* FALLTHRU */
24441 case ALIGNOF_EXPR:
24442 case TYPEID_EXPR:
24443 /* A `sizeof' expression is value-dependent if the operand is
24444 type-dependent or is a pack expansion. */
24445 expression = TREE_OPERAND (expression, 0);
24446 if (PACK_EXPANSION_P (expression))
24447 return true;
24448 else if (TYPE_P (expression))
24449 return dependent_type_p (expression);
24450 return instantiation_dependent_uneval_expression_p (expression);
24451
24452 case AT_ENCODE_EXPR:
24453 /* An 'encode' expression is value-dependent if the operand is
24454 type-dependent. */
24455 expression = TREE_OPERAND (expression, 0);
24456 return dependent_type_p (expression);
24457
24458 case NOEXCEPT_EXPR:
24459 expression = TREE_OPERAND (expression, 0);
24460 return instantiation_dependent_uneval_expression_p (expression);
24461
24462 case SCOPE_REF:
24463 /* All instantiation-dependent expressions should also be considered
24464 value-dependent. */
24465 return instantiation_dependent_scope_ref_p (expression);
24466
24467 case COMPONENT_REF:
24468 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24469 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24470
24471 case NONTYPE_ARGUMENT_PACK:
24472 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24473 is value-dependent. */
24474 {
24475 tree values = ARGUMENT_PACK_ARGS (expression);
24476 int i, len = TREE_VEC_LENGTH (values);
24477
24478 for (i = 0; i < len; ++i)
24479 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24480 return true;
24481
24482 return false;
24483 }
24484
24485 case TRAIT_EXPR:
24486 {
24487 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24488
24489 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24490 return true;
24491
24492 if (!type2)
24493 return false;
24494
24495 if (TREE_CODE (type2) != TREE_LIST)
24496 return dependent_type_p (type2);
24497
24498 for (; type2; type2 = TREE_CHAIN (type2))
24499 if (dependent_type_p (TREE_VALUE (type2)))
24500 return true;
24501
24502 return false;
24503 }
24504
24505 case MODOP_EXPR:
24506 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24507 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24508
24509 case ARRAY_REF:
24510 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24511 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24512
24513 case ADDR_EXPR:
24514 {
24515 tree op = TREE_OPERAND (expression, 0);
24516 return (value_dependent_expression_p (op)
24517 || has_value_dependent_address (op));
24518 }
24519
24520 case REQUIRES_EXPR:
24521 /* Treat all requires-expressions as value-dependent so
24522 we don't try to fold them. */
24523 return true;
24524
24525 case TYPE_REQ:
24526 return dependent_type_p (TREE_OPERAND (expression, 0));
24527
24528 case CALL_EXPR:
24529 {
24530 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24531 return true;
24532 tree fn = get_callee_fndecl (expression);
24533 int i, nargs;
24534 nargs = call_expr_nargs (expression);
24535 for (i = 0; i < nargs; ++i)
24536 {
24537 tree op = CALL_EXPR_ARG (expression, i);
24538 /* In a call to a constexpr member function, look through the
24539 implicit ADDR_EXPR on the object argument so that it doesn't
24540 cause the call to be considered value-dependent. We also
24541 look through it in potential_constant_expression. */
24542 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24543 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24544 && TREE_CODE (op) == ADDR_EXPR)
24545 op = TREE_OPERAND (op, 0);
24546 if (value_dependent_expression_p (op))
24547 return true;
24548 }
24549 return false;
24550 }
24551
24552 case TEMPLATE_ID_EXPR:
24553 return variable_concept_p (TREE_OPERAND (expression, 0));
24554
24555 case CONSTRUCTOR:
24556 {
24557 unsigned ix;
24558 tree val;
24559 if (dependent_type_p (TREE_TYPE (expression)))
24560 return true;
24561 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24562 if (value_dependent_expression_p (val))
24563 return true;
24564 return false;
24565 }
24566
24567 case STMT_EXPR:
24568 /* Treat a GNU statement expression as dependent to avoid crashing
24569 under instantiate_non_dependent_expr; it can't be constant. */
24570 return true;
24571
24572 default:
24573 /* A constant expression is value-dependent if any subexpression is
24574 value-dependent. */
24575 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24576 {
24577 case tcc_reference:
24578 case tcc_unary:
24579 case tcc_comparison:
24580 case tcc_binary:
24581 case tcc_expression:
24582 case tcc_vl_exp:
24583 {
24584 int i, len = cp_tree_operand_length (expression);
24585
24586 for (i = 0; i < len; i++)
24587 {
24588 tree t = TREE_OPERAND (expression, i);
24589
24590 /* In some cases, some of the operands may be missing.
24591 (For example, in the case of PREDECREMENT_EXPR, the
24592 amount to increment by may be missing.) That doesn't
24593 make the expression dependent. */
24594 if (t && value_dependent_expression_p (t))
24595 return true;
24596 }
24597 }
24598 break;
24599 default:
24600 break;
24601 }
24602 break;
24603 }
24604
24605 /* The expression is not value-dependent. */
24606 return false;
24607 }
24608
24609 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24610 [temp.dep.expr]. Note that an expression with no type is
24611 considered dependent. Other parts of the compiler arrange for an
24612 expression with type-dependent subexpressions to have no type, so
24613 this function doesn't have to be fully recursive. */
24614
24615 bool
24616 type_dependent_expression_p (tree expression)
24617 {
24618 if (!processing_template_decl)
24619 return false;
24620
24621 if (expression == NULL_TREE || expression == error_mark_node)
24622 return false;
24623
24624 STRIP_ANY_LOCATION_WRAPPER (expression);
24625
24626 /* An unresolved name is always dependent. */
24627 if (identifier_p (expression)
24628 || TREE_CODE (expression) == USING_DECL
24629 || TREE_CODE (expression) == WILDCARD_DECL)
24630 return true;
24631
24632 /* A fold expression is type-dependent. */
24633 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24634 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24635 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24636 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24637 return true;
24638
24639 /* Some expression forms are never type-dependent. */
24640 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24641 || TREE_CODE (expression) == SIZEOF_EXPR
24642 || TREE_CODE (expression) == ALIGNOF_EXPR
24643 || TREE_CODE (expression) == AT_ENCODE_EXPR
24644 || TREE_CODE (expression) == NOEXCEPT_EXPR
24645 || TREE_CODE (expression) == TRAIT_EXPR
24646 || TREE_CODE (expression) == TYPEID_EXPR
24647 || TREE_CODE (expression) == DELETE_EXPR
24648 || TREE_CODE (expression) == VEC_DELETE_EXPR
24649 || TREE_CODE (expression) == THROW_EXPR
24650 || TREE_CODE (expression) == REQUIRES_EXPR)
24651 return false;
24652
24653 /* The types of these expressions depends only on the type to which
24654 the cast occurs. */
24655 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24656 || TREE_CODE (expression) == STATIC_CAST_EXPR
24657 || TREE_CODE (expression) == CONST_CAST_EXPR
24658 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24659 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24660 || TREE_CODE (expression) == CAST_EXPR)
24661 return dependent_type_p (TREE_TYPE (expression));
24662
24663 /* The types of these expressions depends only on the type created
24664 by the expression. */
24665 if (TREE_CODE (expression) == NEW_EXPR
24666 || TREE_CODE (expression) == VEC_NEW_EXPR)
24667 {
24668 /* For NEW_EXPR tree nodes created inside a template, either
24669 the object type itself or a TREE_LIST may appear as the
24670 operand 1. */
24671 tree type = TREE_OPERAND (expression, 1);
24672 if (TREE_CODE (type) == TREE_LIST)
24673 /* This is an array type. We need to check array dimensions
24674 as well. */
24675 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24676 || value_dependent_expression_p
24677 (TREE_OPERAND (TREE_VALUE (type), 1));
24678 else
24679 return dependent_type_p (type);
24680 }
24681
24682 if (TREE_CODE (expression) == SCOPE_REF)
24683 {
24684 tree scope = TREE_OPERAND (expression, 0);
24685 tree name = TREE_OPERAND (expression, 1);
24686
24687 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24688 contains an identifier associated by name lookup with one or more
24689 declarations declared with a dependent type, or...a
24690 nested-name-specifier or qualified-id that names a member of an
24691 unknown specialization. */
24692 return (type_dependent_expression_p (name)
24693 || dependent_scope_p (scope));
24694 }
24695
24696 if (TREE_CODE (expression) == TEMPLATE_DECL
24697 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24698 return uses_outer_template_parms (expression);
24699
24700 if (TREE_CODE (expression) == STMT_EXPR)
24701 expression = stmt_expr_value_expr (expression);
24702
24703 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24704 {
24705 tree elt;
24706 unsigned i;
24707
24708 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24709 {
24710 if (type_dependent_expression_p (elt))
24711 return true;
24712 }
24713 return false;
24714 }
24715
24716 /* A static data member of the current instantiation with incomplete
24717 array type is type-dependent, as the definition and specializations
24718 can have different bounds. */
24719 if (VAR_P (expression)
24720 && DECL_CLASS_SCOPE_P (expression)
24721 && dependent_type_p (DECL_CONTEXT (expression))
24722 && VAR_HAD_UNKNOWN_BOUND (expression))
24723 return true;
24724
24725 /* An array of unknown bound depending on a variadic parameter, eg:
24726
24727 template<typename... Args>
24728 void foo (Args... args)
24729 {
24730 int arr[] = { args... };
24731 }
24732
24733 template<int... vals>
24734 void bar ()
24735 {
24736 int arr[] = { vals... };
24737 }
24738
24739 If the array has no length and has an initializer, it must be that
24740 we couldn't determine its length in cp_complete_array_type because
24741 it is dependent. */
24742 if (VAR_P (expression)
24743 && TREE_TYPE (expression) != NULL_TREE
24744 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24745 && !TYPE_DOMAIN (TREE_TYPE (expression))
24746 && DECL_INITIAL (expression))
24747 return true;
24748
24749 /* A function or variable template-id is type-dependent if it has any
24750 dependent template arguments. */
24751 if (VAR_OR_FUNCTION_DECL_P (expression)
24752 && DECL_LANG_SPECIFIC (expression)
24753 && DECL_TEMPLATE_INFO (expression))
24754 {
24755 /* Consider the innermost template arguments, since those are the ones
24756 that come from the template-id; the template arguments for the
24757 enclosing class do not make it type-dependent unless they are used in
24758 the type of the decl. */
24759 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24760 && (any_dependent_template_arguments_p
24761 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24762 return true;
24763 }
24764
24765 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24766 type-dependent. Checking this is important for functions with auto return
24767 type, which looks like a dependent type. */
24768 if (TREE_CODE (expression) == FUNCTION_DECL
24769 && !(DECL_CLASS_SCOPE_P (expression)
24770 && dependent_type_p (DECL_CONTEXT (expression)))
24771 && !(DECL_LANG_SPECIFIC (expression)
24772 && DECL_FRIEND_P (expression)
24773 && (!DECL_FRIEND_CONTEXT (expression)
24774 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24775 && !DECL_LOCAL_FUNCTION_P (expression))
24776 {
24777 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24778 || undeduced_auto_decl (expression));
24779 return false;
24780 }
24781
24782 /* Always dependent, on the number of arguments if nothing else. */
24783 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24784 return true;
24785
24786 if (TREE_TYPE (expression) == unknown_type_node)
24787 {
24788 if (TREE_CODE (expression) == ADDR_EXPR)
24789 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24790 if (TREE_CODE (expression) == COMPONENT_REF
24791 || TREE_CODE (expression) == OFFSET_REF)
24792 {
24793 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24794 return true;
24795 expression = TREE_OPERAND (expression, 1);
24796 if (identifier_p (expression))
24797 return false;
24798 }
24799 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24800 if (TREE_CODE (expression) == SCOPE_REF)
24801 return false;
24802
24803 if (BASELINK_P (expression))
24804 {
24805 if (BASELINK_OPTYPE (expression)
24806 && dependent_type_p (BASELINK_OPTYPE (expression)))
24807 return true;
24808 expression = BASELINK_FUNCTIONS (expression);
24809 }
24810
24811 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24812 {
24813 if (any_dependent_template_arguments_p
24814 (TREE_OPERAND (expression, 1)))
24815 return true;
24816 expression = TREE_OPERAND (expression, 0);
24817 if (identifier_p (expression))
24818 return true;
24819 }
24820
24821 gcc_assert (TREE_CODE (expression) == OVERLOAD
24822 || TREE_CODE (expression) == FUNCTION_DECL);
24823
24824 for (lkp_iterator iter (expression); iter; ++iter)
24825 if (type_dependent_expression_p (*iter))
24826 return true;
24827
24828 return false;
24829 }
24830
24831 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24832
24833 /* Dependent type attributes might not have made it from the decl to
24834 the type yet. */
24835 if (DECL_P (expression)
24836 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24837 return true;
24838
24839 return (dependent_type_p (TREE_TYPE (expression)));
24840 }
24841
24842 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24843 type-dependent if the expression refers to a member of the current
24844 instantiation and the type of the referenced member is dependent, or the
24845 class member access expression refers to a member of an unknown
24846 specialization.
24847
24848 This function returns true if the OBJECT in such a class member access
24849 expression is of an unknown specialization. */
24850
24851 bool
24852 type_dependent_object_expression_p (tree object)
24853 {
24854 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24855 dependent. */
24856 if (TREE_CODE (object) == IDENTIFIER_NODE)
24857 return true;
24858 tree scope = TREE_TYPE (object);
24859 return (!scope || dependent_scope_p (scope));
24860 }
24861
24862 /* walk_tree callback function for instantiation_dependent_expression_p,
24863 below. Returns non-zero if a dependent subexpression is found. */
24864
24865 static tree
24866 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24867 void * /*data*/)
24868 {
24869 if (TYPE_P (*tp))
24870 {
24871 /* We don't have to worry about decltype currently because decltype
24872 of an instantiation-dependent expr is a dependent type. This
24873 might change depending on the resolution of DR 1172. */
24874 *walk_subtrees = false;
24875 return NULL_TREE;
24876 }
24877 enum tree_code code = TREE_CODE (*tp);
24878 switch (code)
24879 {
24880 /* Don't treat an argument list as dependent just because it has no
24881 TREE_TYPE. */
24882 case TREE_LIST:
24883 case TREE_VEC:
24884 return NULL_TREE;
24885
24886 case TEMPLATE_PARM_INDEX:
24887 return *tp;
24888
24889 /* Handle expressions with type operands. */
24890 case SIZEOF_EXPR:
24891 case ALIGNOF_EXPR:
24892 case TYPEID_EXPR:
24893 case AT_ENCODE_EXPR:
24894 {
24895 tree op = TREE_OPERAND (*tp, 0);
24896 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24897 op = TREE_TYPE (op);
24898 if (TYPE_P (op))
24899 {
24900 if (dependent_type_p (op))
24901 return *tp;
24902 else
24903 {
24904 *walk_subtrees = false;
24905 return NULL_TREE;
24906 }
24907 }
24908 break;
24909 }
24910
24911 case COMPONENT_REF:
24912 if (identifier_p (TREE_OPERAND (*tp, 1)))
24913 /* In a template, finish_class_member_access_expr creates a
24914 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24915 type-dependent, so that we can check access control at
24916 instantiation time (PR 42277). See also Core issue 1273. */
24917 return *tp;
24918 break;
24919
24920 case SCOPE_REF:
24921 if (instantiation_dependent_scope_ref_p (*tp))
24922 return *tp;
24923 else
24924 break;
24925
24926 /* Treat statement-expressions as dependent. */
24927 case BIND_EXPR:
24928 return *tp;
24929
24930 /* Treat requires-expressions as dependent. */
24931 case REQUIRES_EXPR:
24932 return *tp;
24933
24934 case CALL_EXPR:
24935 /* Treat calls to function concepts as dependent. */
24936 if (function_concept_check_p (*tp))
24937 return *tp;
24938 break;
24939
24940 case TEMPLATE_ID_EXPR:
24941 /* And variable concepts. */
24942 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24943 return *tp;
24944 break;
24945
24946 default:
24947 break;
24948 }
24949
24950 if (type_dependent_expression_p (*tp))
24951 return *tp;
24952 else
24953 return NULL_TREE;
24954 }
24955
24956 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24957 sense defined by the ABI:
24958
24959 "An expression is instantiation-dependent if it is type-dependent
24960 or value-dependent, or it has a subexpression that is type-dependent
24961 or value-dependent."
24962
24963 Except don't actually check value-dependence for unevaluated expressions,
24964 because in sizeof(i) we don't care about the value of i. Checking
24965 type-dependence will in turn check value-dependence of array bounds/template
24966 arguments as needed. */
24967
24968 bool
24969 instantiation_dependent_uneval_expression_p (tree expression)
24970 {
24971 tree result;
24972
24973 if (!processing_template_decl)
24974 return false;
24975
24976 if (expression == error_mark_node)
24977 return false;
24978
24979 result = cp_walk_tree_without_duplicates (&expression,
24980 instantiation_dependent_r, NULL);
24981 return result != NULL_TREE;
24982 }
24983
24984 /* As above, but also check value-dependence of the expression as a whole. */
24985
24986 bool
24987 instantiation_dependent_expression_p (tree expression)
24988 {
24989 return (instantiation_dependent_uneval_expression_p (expression)
24990 || value_dependent_expression_p (expression));
24991 }
24992
24993 /* Like type_dependent_expression_p, but it also works while not processing
24994 a template definition, i.e. during substitution or mangling. */
24995
24996 bool
24997 type_dependent_expression_p_push (tree expr)
24998 {
24999 bool b;
25000 ++processing_template_decl;
25001 b = type_dependent_expression_p (expr);
25002 --processing_template_decl;
25003 return b;
25004 }
25005
25006 /* Returns TRUE if ARGS contains a type-dependent expression. */
25007
25008 bool
25009 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
25010 {
25011 unsigned int i;
25012 tree arg;
25013
25014 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
25015 {
25016 if (type_dependent_expression_p (arg))
25017 return true;
25018 }
25019 return false;
25020 }
25021
25022 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25023 expressions) contains any type-dependent expressions. */
25024
25025 bool
25026 any_type_dependent_elements_p (const_tree list)
25027 {
25028 for (; list; list = TREE_CHAIN (list))
25029 if (type_dependent_expression_p (TREE_VALUE (list)))
25030 return true;
25031
25032 return false;
25033 }
25034
25035 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25036 expressions) contains any value-dependent expressions. */
25037
25038 bool
25039 any_value_dependent_elements_p (const_tree list)
25040 {
25041 for (; list; list = TREE_CHAIN (list))
25042 if (value_dependent_expression_p (TREE_VALUE (list)))
25043 return true;
25044
25045 return false;
25046 }
25047
25048 /* Returns TRUE if the ARG (a template argument) is dependent. */
25049
25050 bool
25051 dependent_template_arg_p (tree arg)
25052 {
25053 if (!processing_template_decl)
25054 return false;
25055
25056 /* Assume a template argument that was wrongly written by the user
25057 is dependent. This is consistent with what
25058 any_dependent_template_arguments_p [that calls this function]
25059 does. */
25060 if (!arg || arg == error_mark_node)
25061 return true;
25062
25063 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
25064 arg = argument_pack_select_arg (arg);
25065
25066 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
25067 return true;
25068 if (TREE_CODE (arg) == TEMPLATE_DECL)
25069 {
25070 if (DECL_TEMPLATE_PARM_P (arg))
25071 return true;
25072 /* A member template of a dependent class is not necessarily
25073 type-dependent, but it is a dependent template argument because it
25074 will be a member of an unknown specialization to that template. */
25075 tree scope = CP_DECL_CONTEXT (arg);
25076 return TYPE_P (scope) && dependent_type_p (scope);
25077 }
25078 else if (ARGUMENT_PACK_P (arg))
25079 {
25080 tree args = ARGUMENT_PACK_ARGS (arg);
25081 int i, len = TREE_VEC_LENGTH (args);
25082 for (i = 0; i < len; ++i)
25083 {
25084 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25085 return true;
25086 }
25087
25088 return false;
25089 }
25090 else if (TYPE_P (arg))
25091 return dependent_type_p (arg);
25092 else
25093 return (type_dependent_expression_p (arg)
25094 || value_dependent_expression_p (arg));
25095 }
25096
25097 /* Returns true if ARGS (a collection of template arguments) contains
25098 any types that require structural equality testing. */
25099
25100 bool
25101 any_template_arguments_need_structural_equality_p (tree args)
25102 {
25103 int i;
25104 int j;
25105
25106 if (!args)
25107 return false;
25108 if (args == error_mark_node)
25109 return true;
25110
25111 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25112 {
25113 tree level = TMPL_ARGS_LEVEL (args, i + 1);
25114 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25115 {
25116 tree arg = TREE_VEC_ELT (level, j);
25117 tree packed_args = NULL_TREE;
25118 int k, len = 1;
25119
25120 if (ARGUMENT_PACK_P (arg))
25121 {
25122 /* Look inside the argument pack. */
25123 packed_args = ARGUMENT_PACK_ARGS (arg);
25124 len = TREE_VEC_LENGTH (packed_args);
25125 }
25126
25127 for (k = 0; k < len; ++k)
25128 {
25129 if (packed_args)
25130 arg = TREE_VEC_ELT (packed_args, k);
25131
25132 if (error_operand_p (arg))
25133 return true;
25134 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25135 continue;
25136 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25137 return true;
25138 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25139 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25140 return true;
25141 }
25142 }
25143 }
25144
25145 return false;
25146 }
25147
25148 /* Returns true if ARGS (a collection of template arguments) contains
25149 any dependent arguments. */
25150
25151 bool
25152 any_dependent_template_arguments_p (const_tree args)
25153 {
25154 int i;
25155 int j;
25156
25157 if (!args)
25158 return false;
25159 if (args == error_mark_node)
25160 return true;
25161
25162 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25163 {
25164 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25165 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25166 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25167 return true;
25168 }
25169
25170 return false;
25171 }
25172
25173 /* Returns true if ARGS contains any errors. */
25174
25175 bool
25176 any_erroneous_template_args_p (const_tree args)
25177 {
25178 int i;
25179 int j;
25180
25181 if (args == error_mark_node)
25182 return true;
25183
25184 if (args && TREE_CODE (args) != TREE_VEC)
25185 {
25186 if (tree ti = get_template_info (args))
25187 args = TI_ARGS (ti);
25188 else
25189 args = NULL_TREE;
25190 }
25191
25192 if (!args)
25193 return false;
25194
25195 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25196 {
25197 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25198 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25199 if (error_operand_p (TREE_VEC_ELT (level, j)))
25200 return true;
25201 }
25202
25203 return false;
25204 }
25205
25206 /* Returns TRUE if the template TMPL is type-dependent. */
25207
25208 bool
25209 dependent_template_p (tree tmpl)
25210 {
25211 if (TREE_CODE (tmpl) == OVERLOAD)
25212 {
25213 for (lkp_iterator iter (tmpl); iter; ++iter)
25214 if (dependent_template_p (*iter))
25215 return true;
25216 return false;
25217 }
25218
25219 /* Template template parameters are dependent. */
25220 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25221 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25222 return true;
25223 /* So are names that have not been looked up. */
25224 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25225 return true;
25226 return false;
25227 }
25228
25229 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25230
25231 bool
25232 dependent_template_id_p (tree tmpl, tree args)
25233 {
25234 return (dependent_template_p (tmpl)
25235 || any_dependent_template_arguments_p (args));
25236 }
25237
25238 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25239 are dependent. */
25240
25241 bool
25242 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25243 {
25244 int i;
25245
25246 if (!processing_template_decl)
25247 return false;
25248
25249 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25250 {
25251 tree decl = TREE_VEC_ELT (declv, i);
25252 tree init = TREE_VEC_ELT (initv, i);
25253 tree cond = TREE_VEC_ELT (condv, i);
25254 tree incr = TREE_VEC_ELT (incrv, i);
25255
25256 if (type_dependent_expression_p (decl)
25257 || TREE_CODE (decl) == SCOPE_REF)
25258 return true;
25259
25260 if (init && type_dependent_expression_p (init))
25261 return true;
25262
25263 if (type_dependent_expression_p (cond))
25264 return true;
25265
25266 if (COMPARISON_CLASS_P (cond)
25267 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25268 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25269 return true;
25270
25271 if (TREE_CODE (incr) == MODOP_EXPR)
25272 {
25273 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25274 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25275 return true;
25276 }
25277 else if (type_dependent_expression_p (incr))
25278 return true;
25279 else if (TREE_CODE (incr) == MODIFY_EXPR)
25280 {
25281 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25282 return true;
25283 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25284 {
25285 tree t = TREE_OPERAND (incr, 1);
25286 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25287 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25288 return true;
25289 }
25290 }
25291 }
25292
25293 return false;
25294 }
25295
25296 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25297 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25298 no such TYPE can be found. Note that this function peers inside
25299 uninstantiated templates and therefore should be used only in
25300 extremely limited situations. ONLY_CURRENT_P restricts this
25301 peering to the currently open classes hierarchy (which is required
25302 when comparing types). */
25303
25304 tree
25305 resolve_typename_type (tree type, bool only_current_p)
25306 {
25307 tree scope;
25308 tree name;
25309 tree decl;
25310 int quals;
25311 tree pushed_scope;
25312 tree result;
25313
25314 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25315
25316 scope = TYPE_CONTEXT (type);
25317 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25318 gcc_checking_assert (uses_template_parms (scope));
25319
25320 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25321 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25322 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25323 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25324 identifier of the TYPENAME_TYPE anymore.
25325 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25326 TYPENAME_TYPE instead, we avoid messing up with a possible
25327 typedef variant case. */
25328 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25329
25330 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25331 it first before we can figure out what NAME refers to. */
25332 if (TREE_CODE (scope) == TYPENAME_TYPE)
25333 {
25334 if (TYPENAME_IS_RESOLVING_P (scope))
25335 /* Given a class template A with a dependent base with nested type C,
25336 typedef typename A::C::C C will land us here, as trying to resolve
25337 the initial A::C leads to the local C typedef, which leads back to
25338 A::C::C. So we break the recursion now. */
25339 return type;
25340 else
25341 scope = resolve_typename_type (scope, only_current_p);
25342 }
25343 /* If we don't know what SCOPE refers to, then we cannot resolve the
25344 TYPENAME_TYPE. */
25345 if (!CLASS_TYPE_P (scope))
25346 return type;
25347 /* If this is a typedef, we don't want to look inside (c++/11987). */
25348 if (typedef_variant_p (type))
25349 return type;
25350 /* If SCOPE isn't the template itself, it will not have a valid
25351 TYPE_FIELDS list. */
25352 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25353 /* scope is either the template itself or a compatible instantiation
25354 like X<T>, so look up the name in the original template. */
25355 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25356 /* If scope has no fields, it can't be a current instantiation. Check this
25357 before currently_open_class to avoid infinite recursion (71515). */
25358 if (!TYPE_FIELDS (scope))
25359 return type;
25360 /* If the SCOPE is not the current instantiation, there's no reason
25361 to look inside it. */
25362 if (only_current_p && !currently_open_class (scope))
25363 return type;
25364 /* Enter the SCOPE so that name lookup will be resolved as if we
25365 were in the class definition. In particular, SCOPE will no
25366 longer be considered a dependent type. */
25367 pushed_scope = push_scope (scope);
25368 /* Look up the declaration. */
25369 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25370 tf_warning_or_error);
25371
25372 result = NULL_TREE;
25373
25374 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25375 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25376 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25377 if (!decl)
25378 /*nop*/;
25379 else if (identifier_p (fullname)
25380 && TREE_CODE (decl) == TYPE_DECL)
25381 {
25382 result = TREE_TYPE (decl);
25383 if (result == error_mark_node)
25384 result = NULL_TREE;
25385 }
25386 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25387 && DECL_CLASS_TEMPLATE_P (decl))
25388 {
25389 /* Obtain the template and the arguments. */
25390 tree tmpl = TREE_OPERAND (fullname, 0);
25391 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25392 {
25393 /* We get here with a plain identifier because a previous tentative
25394 parse of the nested-name-specifier as part of a ptr-operator saw
25395 ::template X<A>. The use of ::template is necessary in a
25396 ptr-operator, but wrong in a declarator-id.
25397
25398 [temp.names]: In a qualified-id of a declarator-id, the keyword
25399 template shall not appear at the top level. */
25400 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25401 "keyword %<template%> not allowed in declarator-id");
25402 tmpl = decl;
25403 }
25404 tree args = TREE_OPERAND (fullname, 1);
25405 /* Instantiate the template. */
25406 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25407 /*entering_scope=*/true,
25408 tf_error | tf_user);
25409 if (result == error_mark_node)
25410 result = NULL_TREE;
25411 }
25412
25413 /* Leave the SCOPE. */
25414 if (pushed_scope)
25415 pop_scope (pushed_scope);
25416
25417 /* If we failed to resolve it, return the original typename. */
25418 if (!result)
25419 return type;
25420
25421 /* If lookup found a typename type, resolve that too. */
25422 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25423 {
25424 /* Ill-formed programs can cause infinite recursion here, so we
25425 must catch that. */
25426 TYPENAME_IS_RESOLVING_P (result) = 1;
25427 result = resolve_typename_type (result, only_current_p);
25428 TYPENAME_IS_RESOLVING_P (result) = 0;
25429 }
25430
25431 /* Qualify the resulting type. */
25432 quals = cp_type_quals (type);
25433 if (quals)
25434 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25435
25436 return result;
25437 }
25438
25439 /* EXPR is an expression which is not type-dependent. Return a proxy
25440 for EXPR that can be used to compute the types of larger
25441 expressions containing EXPR. */
25442
25443 tree
25444 build_non_dependent_expr (tree expr)
25445 {
25446 tree orig_expr = expr;
25447 tree inner_expr;
25448
25449 /* When checking, try to get a constant value for all non-dependent
25450 expressions in order to expose bugs in *_dependent_expression_p
25451 and constexpr. This can affect code generation, see PR70704, so
25452 only do this for -fchecking=2. */
25453 if (flag_checking > 1
25454 && cxx_dialect >= cxx11
25455 /* Don't do this during nsdmi parsing as it can lead to
25456 unexpected recursive instantiations. */
25457 && !parsing_nsdmi ()
25458 /* Don't do this during concept expansion either and for
25459 the same reason. */
25460 && !expanding_concept ())
25461 fold_non_dependent_expr (expr);
25462
25463 STRIP_ANY_LOCATION_WRAPPER (expr);
25464
25465 /* Preserve OVERLOADs; the functions must be available to resolve
25466 types. */
25467 inner_expr = expr;
25468 if (TREE_CODE (inner_expr) == STMT_EXPR)
25469 inner_expr = stmt_expr_value_expr (inner_expr);
25470 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25471 inner_expr = TREE_OPERAND (inner_expr, 0);
25472 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25473 inner_expr = TREE_OPERAND (inner_expr, 1);
25474 if (is_overloaded_fn (inner_expr)
25475 || TREE_CODE (inner_expr) == OFFSET_REF)
25476 return orig_expr;
25477 /* There is no need to return a proxy for a variable. */
25478 if (VAR_P (expr))
25479 return orig_expr;
25480 /* Preserve string constants; conversions from string constants to
25481 "char *" are allowed, even though normally a "const char *"
25482 cannot be used to initialize a "char *". */
25483 if (TREE_CODE (expr) == STRING_CST)
25484 return orig_expr;
25485 /* Preserve void and arithmetic constants, as an optimization -- there is no
25486 reason to create a new node. */
25487 if (TREE_CODE (expr) == VOID_CST
25488 || TREE_CODE (expr) == INTEGER_CST
25489 || TREE_CODE (expr) == REAL_CST)
25490 return orig_expr;
25491 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25492 There is at least one place where we want to know that a
25493 particular expression is a throw-expression: when checking a ?:
25494 expression, there are special rules if the second or third
25495 argument is a throw-expression. */
25496 if (TREE_CODE (expr) == THROW_EXPR)
25497 return orig_expr;
25498
25499 /* Don't wrap an initializer list, we need to be able to look inside. */
25500 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25501 return orig_expr;
25502
25503 /* Don't wrap a dummy object, we need to be able to test for it. */
25504 if (is_dummy_object (expr))
25505 return orig_expr;
25506
25507 if (TREE_CODE (expr) == COND_EXPR)
25508 return build3 (COND_EXPR,
25509 TREE_TYPE (expr),
25510 TREE_OPERAND (expr, 0),
25511 (TREE_OPERAND (expr, 1)
25512 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25513 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25514 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25515 if (TREE_CODE (expr) == COMPOUND_EXPR
25516 && !COMPOUND_EXPR_OVERLOADED (expr))
25517 return build2 (COMPOUND_EXPR,
25518 TREE_TYPE (expr),
25519 TREE_OPERAND (expr, 0),
25520 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25521
25522 /* If the type is unknown, it can't really be non-dependent */
25523 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25524
25525 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25526 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
25527 TREE_TYPE (expr), expr);
25528 }
25529
25530 /* ARGS is a vector of expressions as arguments to a function call.
25531 Replace the arguments with equivalent non-dependent expressions.
25532 This modifies ARGS in place. */
25533
25534 void
25535 make_args_non_dependent (vec<tree, va_gc> *args)
25536 {
25537 unsigned int ix;
25538 tree arg;
25539
25540 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25541 {
25542 tree newarg = build_non_dependent_expr (arg);
25543 if (newarg != arg)
25544 (*args)[ix] = newarg;
25545 }
25546 }
25547
25548 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25549 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25550 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25551
25552 static tree
25553 make_auto_1 (tree name, bool set_canonical)
25554 {
25555 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25556 TYPE_NAME (au) = build_decl (input_location,
25557 TYPE_DECL, name, au);
25558 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25559 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25560 (0, processing_template_decl + 1, processing_template_decl + 1,
25561 TYPE_NAME (au), NULL_TREE);
25562 if (set_canonical)
25563 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25564 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25565 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25566
25567 return au;
25568 }
25569
25570 tree
25571 make_decltype_auto (void)
25572 {
25573 return make_auto_1 (decltype_auto_identifier, true);
25574 }
25575
25576 tree
25577 make_auto (void)
25578 {
25579 return make_auto_1 (auto_identifier, true);
25580 }
25581
25582 /* Return a C++17 deduction placeholder for class template TMPL. */
25583
25584 tree
25585 make_template_placeholder (tree tmpl)
25586 {
25587 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25588 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25589 return t;
25590 }
25591
25592 /* True iff T is a C++17 class template deduction placeholder. */
25593
25594 bool
25595 template_placeholder_p (tree t)
25596 {
25597 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25598 }
25599
25600 /* Make a "constrained auto" type-specifier. This is an
25601 auto type with constraints that must be associated after
25602 deduction. The constraint is formed from the given
25603 CONC and its optional sequence of arguments, which are
25604 non-null if written as partial-concept-id. */
25605
25606 tree
25607 make_constrained_auto (tree con, tree args)
25608 {
25609 tree type = make_auto_1 (auto_identifier, false);
25610
25611 /* Build the constraint. */
25612 tree tmpl = DECL_TI_TEMPLATE (con);
25613 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25614 expr = build_concept_check (expr, type, args);
25615
25616 tree constr = normalize_expression (expr);
25617 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25618
25619 /* Our canonical type depends on the constraint. */
25620 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25621
25622 /* Attach the constraint to the type declaration. */
25623 tree decl = TYPE_NAME (type);
25624 return decl;
25625 }
25626
25627 /* Given type ARG, return std::initializer_list<ARG>. */
25628
25629 static tree
25630 listify (tree arg)
25631 {
25632 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25633
25634 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25635 {
25636 gcc_rich_location richloc (input_location);
25637 maybe_add_include_fixit (&richloc, "<initializer_list>");
25638 error_at (&richloc,
25639 "deducing from brace-enclosed initializer list"
25640 " requires %<#include <initializer_list>%>");
25641
25642 return error_mark_node;
25643 }
25644 tree argvec = make_tree_vec (1);
25645 TREE_VEC_ELT (argvec, 0) = arg;
25646
25647 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25648 NULL_TREE, 0, tf_warning_or_error);
25649 }
25650
25651 /* Replace auto in TYPE with std::initializer_list<auto>. */
25652
25653 static tree
25654 listify_autos (tree type, tree auto_node)
25655 {
25656 tree init_auto = listify (auto_node);
25657 tree argvec = make_tree_vec (1);
25658 TREE_VEC_ELT (argvec, 0) = init_auto;
25659 if (processing_template_decl)
25660 argvec = add_to_template_args (current_template_args (), argvec);
25661 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25662 }
25663
25664 /* Hash traits for hashing possibly constrained 'auto'
25665 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25666
25667 struct auto_hash : default_hash_traits<tree>
25668 {
25669 static inline hashval_t hash (tree);
25670 static inline bool equal (tree, tree);
25671 };
25672
25673 /* Hash the 'auto' T. */
25674
25675 inline hashval_t
25676 auto_hash::hash (tree t)
25677 {
25678 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25679 /* Matching constrained-type-specifiers denote the same template
25680 parameter, so hash the constraint. */
25681 return hash_placeholder_constraint (c);
25682 else
25683 /* But unconstrained autos are all separate, so just hash the pointer. */
25684 return iterative_hash_object (t, 0);
25685 }
25686
25687 /* Compare two 'auto's. */
25688
25689 inline bool
25690 auto_hash::equal (tree t1, tree t2)
25691 {
25692 if (t1 == t2)
25693 return true;
25694
25695 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25696 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25697
25698 /* Two unconstrained autos are distinct. */
25699 if (!c1 || !c2)
25700 return false;
25701
25702 return equivalent_placeholder_constraints (c1, c2);
25703 }
25704
25705 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25706 constrained) auto, add it to the vector. */
25707
25708 static int
25709 extract_autos_r (tree t, void *data)
25710 {
25711 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25712 if (is_auto (t))
25713 {
25714 /* All the autos were built with index 0; fix that up now. */
25715 tree *p = hash.find_slot (t, INSERT);
25716 unsigned idx;
25717 if (*p)
25718 /* If this is a repeated constrained-type-specifier, use the index we
25719 chose before. */
25720 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25721 else
25722 {
25723 /* Otherwise this is new, so use the current count. */
25724 *p = t;
25725 idx = hash.elements () - 1;
25726 }
25727 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25728 }
25729
25730 /* Always keep walking. */
25731 return 0;
25732 }
25733
25734 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25735 says they can appear anywhere in the type. */
25736
25737 static tree
25738 extract_autos (tree type)
25739 {
25740 hash_set<tree> visited;
25741 hash_table<auto_hash> hash (2);
25742
25743 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25744
25745 tree tree_vec = make_tree_vec (hash.elements());
25746 for (hash_table<auto_hash>::iterator iter = hash.begin();
25747 iter != hash.end(); ++iter)
25748 {
25749 tree elt = *iter;
25750 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25751 TREE_VEC_ELT (tree_vec, i)
25752 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25753 }
25754
25755 return tree_vec;
25756 }
25757
25758 /* The stem for deduction guide names. */
25759 const char *const dguide_base = "__dguide_";
25760
25761 /* Return the name for a deduction guide for class template TMPL. */
25762
25763 tree
25764 dguide_name (tree tmpl)
25765 {
25766 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25767 tree tname = TYPE_IDENTIFIER (type);
25768 char *buf = (char *) alloca (1 + strlen (dguide_base)
25769 + IDENTIFIER_LENGTH (tname));
25770 memcpy (buf, dguide_base, strlen (dguide_base));
25771 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25772 IDENTIFIER_LENGTH (tname) + 1);
25773 tree dname = get_identifier (buf);
25774 TREE_TYPE (dname) = type;
25775 return dname;
25776 }
25777
25778 /* True if NAME is the name of a deduction guide. */
25779
25780 bool
25781 dguide_name_p (tree name)
25782 {
25783 return (TREE_CODE (name) == IDENTIFIER_NODE
25784 && TREE_TYPE (name)
25785 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25786 strlen (dguide_base)));
25787 }
25788
25789 /* True if FN is a deduction guide. */
25790
25791 bool
25792 deduction_guide_p (const_tree fn)
25793 {
25794 if (DECL_P (fn))
25795 if (tree name = DECL_NAME (fn))
25796 return dguide_name_p (name);
25797 return false;
25798 }
25799
25800 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25801
25802 bool
25803 copy_guide_p (const_tree fn)
25804 {
25805 gcc_assert (deduction_guide_p (fn));
25806 if (!DECL_ARTIFICIAL (fn))
25807 return false;
25808 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25809 return (TREE_CHAIN (parms) == void_list_node
25810 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25811 }
25812
25813 /* True if FN is a guide generated from a constructor template. */
25814
25815 bool
25816 template_guide_p (const_tree fn)
25817 {
25818 gcc_assert (deduction_guide_p (fn));
25819 if (!DECL_ARTIFICIAL (fn))
25820 return false;
25821 tree tmpl = DECL_TI_TEMPLATE (fn);
25822 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25823 return PRIMARY_TEMPLATE_P (org);
25824 return false;
25825 }
25826
25827 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25828 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25829 template parameter types. Note that the handling of template template
25830 parameters relies on current_template_parms being set appropriately for the
25831 new template. */
25832
25833 static tree
25834 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25835 tree tsubst_args, tsubst_flags_t complain)
25836 {
25837 if (olddecl == error_mark_node)
25838 return error_mark_node;
25839
25840 tree oldidx = get_template_parm_index (olddecl);
25841
25842 tree newtype;
25843 if (TREE_CODE (olddecl) == TYPE_DECL
25844 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25845 {
25846 tree oldtype = TREE_TYPE (olddecl);
25847 newtype = cxx_make_type (TREE_CODE (oldtype));
25848 TYPE_MAIN_VARIANT (newtype) = newtype;
25849 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25850 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25851 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25852 }
25853 else
25854 {
25855 newtype = TREE_TYPE (olddecl);
25856 if (type_uses_auto (newtype))
25857 {
25858 // Substitute once to fix references to other template parameters.
25859 newtype = tsubst (newtype, tsubst_args,
25860 complain|tf_partial, NULL_TREE);
25861 // Now substitute again to reduce the level of the auto.
25862 newtype = tsubst (newtype, current_template_args (),
25863 complain, NULL_TREE);
25864 }
25865 else
25866 newtype = tsubst (newtype, tsubst_args,
25867 complain, NULL_TREE);
25868 }
25869
25870 tree newdecl
25871 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25872 DECL_NAME (olddecl), newtype);
25873 SET_DECL_TEMPLATE_PARM_P (newdecl);
25874
25875 tree newidx;
25876 if (TREE_CODE (olddecl) == TYPE_DECL
25877 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25878 {
25879 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25880 = build_template_parm_index (index, level, level,
25881 newdecl, newtype);
25882 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25883 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25884 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25885 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25886
25887 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25888 {
25889 DECL_TEMPLATE_RESULT (newdecl)
25890 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25891 DECL_NAME (olddecl), newtype);
25892 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25893 // First create a copy (ttargs) of tsubst_args with an
25894 // additional level for the template template parameter's own
25895 // template parameters (ttparms).
25896 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25897 (DECL_TEMPLATE_PARMS (olddecl)));
25898 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25899 tree ttargs = make_tree_vec (depth + 1);
25900 for (int i = 0; i < depth; ++i)
25901 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25902 TREE_VEC_ELT (ttargs, depth)
25903 = template_parms_level_to_args (ttparms);
25904 // Substitute ttargs into ttparms to fix references to
25905 // other template parameters.
25906 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25907 complain|tf_partial);
25908 // Now substitute again with args based on tparms, to reduce
25909 // the level of the ttparms.
25910 ttargs = current_template_args ();
25911 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25912 complain);
25913 // Finally, tack the adjusted parms onto tparms.
25914 ttparms = tree_cons (size_int (depth), ttparms,
25915 current_template_parms);
25916 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25917 }
25918 }
25919 else
25920 {
25921 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25922 tree newconst
25923 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25924 TREE_CODE (oldconst),
25925 DECL_NAME (oldconst), newtype);
25926 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25927 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25928 SET_DECL_TEMPLATE_PARM_P (newconst);
25929 newidx = build_template_parm_index (index, level, level,
25930 newconst, newtype);
25931 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25932 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25933 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25934 }
25935
25936 return newdecl;
25937 }
25938
25939 /* Returns a C++17 class deduction guide template based on the constructor
25940 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25941 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25942
25943 static tree
25944 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25945 {
25946 tree type, tparms, targs, fparms, fargs, ci;
25947 bool memtmpl = false;
25948 bool explicit_p;
25949 location_t loc;
25950 tree fn_tmpl = NULL_TREE;
25951
25952 if (TYPE_P (ctor))
25953 {
25954 type = ctor;
25955 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25956 if (copy_p)
25957 {
25958 type = TREE_TYPE (type);
25959 fparms = tree_cons (NULL_TREE, type, void_list_node);
25960 }
25961 else
25962 fparms = void_list_node;
25963
25964 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25965 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25966 targs = CLASSTYPE_TI_ARGS (type);
25967 ci = NULL_TREE;
25968 fargs = NULL_TREE;
25969 loc = DECL_SOURCE_LOCATION (ctmpl);
25970 explicit_p = false;
25971 }
25972 else
25973 {
25974 ++processing_template_decl;
25975 bool ok = true;
25976
25977 fn_tmpl
25978 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25979 : DECL_TI_TEMPLATE (ctor));
25980 if (outer_args)
25981 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25982 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25983
25984 type = DECL_CONTEXT (ctor);
25985
25986 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25987 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25988 fully specialized args for the enclosing class. Strip those off, as
25989 the deduction guide won't have those template parameters. */
25990 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25991 TMPL_PARMS_DEPTH (tparms));
25992 /* Discard the 'this' parameter. */
25993 fparms = FUNCTION_ARG_CHAIN (ctor);
25994 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25995 ci = get_constraints (ctor);
25996 loc = DECL_SOURCE_LOCATION (ctor);
25997 explicit_p = DECL_NONCONVERTING_P (ctor);
25998
25999 if (PRIMARY_TEMPLATE_P (fn_tmpl))
26000 {
26001 memtmpl = true;
26002
26003 /* For a member template constructor, we need to flatten the two
26004 template parameter lists into one, and then adjust the function
26005 signature accordingly. This gets...complicated. */
26006 tree save_parms = current_template_parms;
26007
26008 /* For a member template we should have two levels of parms/args, one
26009 for the class and one for the constructor. We stripped
26010 specialized args for further enclosing classes above. */
26011 const int depth = 2;
26012 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
26013
26014 /* Template args for translating references to the two-level template
26015 parameters into references to the one-level template parameters we
26016 are creating. */
26017 tree tsubst_args = copy_node (targs);
26018 TMPL_ARGS_LEVEL (tsubst_args, depth)
26019 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
26020
26021 /* Template parms for the constructor template. */
26022 tree ftparms = TREE_VALUE (tparms);
26023 unsigned flen = TREE_VEC_LENGTH (ftparms);
26024 /* Template parms for the class template. */
26025 tparms = TREE_CHAIN (tparms);
26026 tree ctparms = TREE_VALUE (tparms);
26027 unsigned clen = TREE_VEC_LENGTH (ctparms);
26028 /* Template parms for the deduction guide start as a copy of the
26029 template parms for the class. We set current_template_parms for
26030 lookup_template_class_1. */
26031 current_template_parms = tparms = copy_node (tparms);
26032 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
26033 for (unsigned i = 0; i < clen; ++i)
26034 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
26035
26036 /* Now we need to rewrite the constructor parms to append them to the
26037 class parms. */
26038 for (unsigned i = 0; i < flen; ++i)
26039 {
26040 unsigned index = i + clen;
26041 unsigned level = 1;
26042 tree oldelt = TREE_VEC_ELT (ftparms, i);
26043 tree olddecl = TREE_VALUE (oldelt);
26044 tree newdecl = rewrite_template_parm (olddecl, index, level,
26045 tsubst_args, complain);
26046 if (newdecl == error_mark_node)
26047 ok = false;
26048 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
26049 tsubst_args, complain, ctor);
26050 tree list = build_tree_list (newdef, newdecl);
26051 TEMPLATE_PARM_CONSTRAINTS (list)
26052 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
26053 tsubst_args, complain, ctor);
26054 TREE_VEC_ELT (new_vec, index) = list;
26055 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
26056 }
26057
26058 /* Now we have a final set of template parms to substitute into the
26059 function signature. */
26060 targs = template_parms_to_args (tparms);
26061 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
26062 complain, ctor);
26063 fargs = tsubst (fargs, tsubst_args, complain, ctor);
26064 if (ci)
26065 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
26066
26067 current_template_parms = save_parms;
26068 }
26069
26070 --processing_template_decl;
26071 if (!ok)
26072 return error_mark_node;
26073 }
26074
26075 if (!memtmpl)
26076 {
26077 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26078 tparms = copy_node (tparms);
26079 INNERMOST_TEMPLATE_PARMS (tparms)
26080 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
26081 }
26082
26083 tree fntype = build_function_type (type, fparms);
26084 tree ded_fn = build_lang_decl_loc (loc,
26085 FUNCTION_DECL,
26086 dguide_name (type), fntype);
26087 DECL_ARGUMENTS (ded_fn) = fargs;
26088 DECL_ARTIFICIAL (ded_fn) = true;
26089 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
26090 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
26091 DECL_ARTIFICIAL (ded_tmpl) = true;
26092 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
26093 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
26094 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
26095 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
26096 if (DECL_P (ctor))
26097 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
26098 if (ci)
26099 set_constraints (ded_tmpl, ci);
26100
26101 return ded_tmpl;
26102 }
26103
26104 /* Deduce template arguments for the class template placeholder PTYPE for
26105 template TMPL based on the initializer INIT, and return the resulting
26106 type. */
26107
26108 static tree
26109 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
26110 tsubst_flags_t complain)
26111 {
26112 if (!DECL_CLASS_TEMPLATE_P (tmpl))
26113 {
26114 /* We should have handled this in the caller. */
26115 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26116 return ptype;
26117 if (complain & tf_error)
26118 error ("non-class template %qT used without template arguments", tmpl);
26119 return error_mark_node;
26120 }
26121
26122 tree type = TREE_TYPE (tmpl);
26123
26124 bool try_list_ctor = false;
26125
26126 vec<tree,va_gc> *args;
26127 if (init == NULL_TREE
26128 || TREE_CODE (init) == TREE_LIST)
26129 args = make_tree_vector_from_list (init);
26130 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
26131 {
26132 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
26133 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
26134 {
26135 /* As an exception, the first phase in 16.3.1.7 (considering the
26136 initializer list as a single argument) is omitted if the
26137 initializer list consists of a single expression of type cv U,
26138 where U is a specialization of C or a class derived from a
26139 specialization of C. */
26140 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
26141 tree etype = TREE_TYPE (elt);
26142
26143 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
26144 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26145 int err = unify (tparms, targs, type, etype,
26146 UNIFY_ALLOW_DERIVED, /*explain*/false);
26147 if (err == 0)
26148 try_list_ctor = false;
26149 ggc_free (targs);
26150 }
26151 if (try_list_ctor || is_std_init_list (type))
26152 args = make_tree_vector_single (init);
26153 else
26154 args = make_tree_vector_from_ctor (init);
26155 }
26156 else
26157 args = make_tree_vector_single (init);
26158
26159 tree dname = dguide_name (tmpl);
26160 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
26161 /*type*/false, /*complain*/false,
26162 /*hidden*/false);
26163 bool elided = false;
26164 if (cands == error_mark_node)
26165 cands = NULL_TREE;
26166
26167 /* Prune explicit deduction guides in copy-initialization context. */
26168 if (flags & LOOKUP_ONLYCONVERTING)
26169 {
26170 for (lkp_iterator iter (cands); !elided && iter; ++iter)
26171 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26172 elided = true;
26173
26174 if (elided)
26175 {
26176 /* Found a nonconverting guide, prune the candidates. */
26177 tree pruned = NULL_TREE;
26178 for (lkp_iterator iter (cands); iter; ++iter)
26179 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26180 pruned = lookup_add (*iter, pruned);
26181
26182 cands = pruned;
26183 }
26184 }
26185
26186 tree outer_args = NULL_TREE;
26187 if (DECL_CLASS_SCOPE_P (tmpl)
26188 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26189 {
26190 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26191 type = TREE_TYPE (most_general_template (tmpl));
26192 }
26193
26194 bool saw_ctor = false;
26195 // FIXME cache artificial deduction guides
26196 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26197 {
26198 tree guide = build_deduction_guide (*iter, outer_args, complain);
26199 if (guide == error_mark_node)
26200 return error_mark_node;
26201 if ((flags & LOOKUP_ONLYCONVERTING)
26202 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26203 elided = true;
26204 else
26205 cands = lookup_add (guide, cands);
26206
26207 saw_ctor = true;
26208 }
26209
26210 tree call = error_mark_node;
26211
26212 /* If this is list-initialization and the class has a list constructor, first
26213 try deducing from the list as a single argument, as [over.match.list]. */
26214 tree list_cands = NULL_TREE;
26215 if (try_list_ctor && cands)
26216 for (lkp_iterator iter (cands); iter; ++iter)
26217 {
26218 tree dg = *iter;
26219 if (is_list_ctor (dg))
26220 list_cands = lookup_add (dg, list_cands);
26221 }
26222 if (list_cands)
26223 {
26224 ++cp_unevaluated_operand;
26225 call = build_new_function_call (list_cands, &args, tf_decltype);
26226 --cp_unevaluated_operand;
26227
26228 if (call == error_mark_node)
26229 {
26230 /* That didn't work, now try treating the list as a sequence of
26231 arguments. */
26232 release_tree_vector (args);
26233 args = make_tree_vector_from_ctor (init);
26234 }
26235 }
26236
26237 /* Maybe generate an implicit deduction guide. */
26238 if (call == error_mark_node && args->length () < 2)
26239 {
26240 tree gtype = NULL_TREE;
26241
26242 if (args->length () == 1)
26243 /* Generate a copy guide. */
26244 gtype = build_reference_type (type);
26245 else if (!saw_ctor)
26246 /* Generate a default guide. */
26247 gtype = type;
26248
26249 if (gtype)
26250 {
26251 tree guide = build_deduction_guide (gtype, outer_args, complain);
26252 if (guide == error_mark_node)
26253 return error_mark_node;
26254 cands = lookup_add (guide, cands);
26255 }
26256 }
26257
26258 if (elided && !cands)
26259 {
26260 error ("cannot deduce template arguments for copy-initialization"
26261 " of %qT, as it has no non-explicit deduction guides or "
26262 "user-declared constructors", type);
26263 return error_mark_node;
26264 }
26265 else if (!cands && call == error_mark_node)
26266 {
26267 error ("cannot deduce template arguments of %qT, as it has no viable "
26268 "deduction guides", type);
26269 return error_mark_node;
26270 }
26271
26272 if (call == error_mark_node)
26273 {
26274 ++cp_unevaluated_operand;
26275 call = build_new_function_call (cands, &args, tf_decltype);
26276 --cp_unevaluated_operand;
26277 }
26278
26279 if (call == error_mark_node && (complain & tf_warning_or_error))
26280 {
26281 error ("class template argument deduction failed:");
26282
26283 ++cp_unevaluated_operand;
26284 call = build_new_function_call (cands, &args, complain | tf_decltype);
26285 --cp_unevaluated_operand;
26286
26287 if (elided)
26288 inform (input_location, "explicit deduction guides not considered "
26289 "for copy-initialization");
26290 }
26291
26292 release_tree_vector (args);
26293
26294 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26295 }
26296
26297 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26298 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26299 The CONTEXT determines the context in which auto deduction is performed
26300 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26301 OUTER_TARGS are used during template argument deduction
26302 (context == adc_unify) to properly substitute the result, and is ignored
26303 in other contexts.
26304
26305 For partial-concept-ids, extra args may be appended to the list of deduced
26306 template arguments prior to determining constraint satisfaction. */
26307
26308 tree
26309 do_auto_deduction (tree type, tree init, tree auto_node,
26310 tsubst_flags_t complain, auto_deduction_context context,
26311 tree outer_targs, int flags)
26312 {
26313 tree targs;
26314
26315 if (init == error_mark_node)
26316 return error_mark_node;
26317
26318 if (init && type_dependent_expression_p (init)
26319 && context != adc_unify)
26320 /* Defining a subset of type-dependent expressions that we can deduce
26321 from ahead of time isn't worth the trouble. */
26322 return type;
26323
26324 /* Similarly, we can't deduce from another undeduced decl. */
26325 if (init && undeduced_auto_decl (init))
26326 return type;
26327
26328 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26329 /* C++17 class template argument deduction. */
26330 return do_class_deduction (type, tmpl, init, flags, complain);
26331
26332 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26333 /* Nothing we can do with this, even in deduction context. */
26334 return type;
26335
26336 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26337 with either a new invented type template parameter U or, if the
26338 initializer is a braced-init-list (8.5.4), with
26339 std::initializer_list<U>. */
26340 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26341 {
26342 if (!DIRECT_LIST_INIT_P (init))
26343 type = listify_autos (type, auto_node);
26344 else if (CONSTRUCTOR_NELTS (init) == 1)
26345 init = CONSTRUCTOR_ELT (init, 0)->value;
26346 else
26347 {
26348 if (complain & tf_warning_or_error)
26349 {
26350 if (permerror (input_location, "direct-list-initialization of "
26351 "%<auto%> requires exactly one element"))
26352 inform (input_location,
26353 "for deduction to %<std::initializer_list%>, use copy-"
26354 "list-initialization (i.e. add %<=%> before the %<{%>)");
26355 }
26356 type = listify_autos (type, auto_node);
26357 }
26358 }
26359
26360 if (type == error_mark_node)
26361 return error_mark_node;
26362
26363 init = resolve_nondeduced_context (init, complain);
26364
26365 if (context == adc_decomp_type
26366 && auto_node == type
26367 && init != error_mark_node
26368 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26369 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26370 and initializer has array type, deduce cv-qualified array type. */
26371 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26372 complain);
26373 else if (AUTO_IS_DECLTYPE (auto_node))
26374 {
26375 bool id = (DECL_P (init)
26376 || ((TREE_CODE (init) == COMPONENT_REF
26377 || TREE_CODE (init) == SCOPE_REF)
26378 && !REF_PARENTHESIZED_P (init)));
26379 targs = make_tree_vec (1);
26380 TREE_VEC_ELT (targs, 0)
26381 = finish_decltype_type (init, id, tf_warning_or_error);
26382 if (type != auto_node)
26383 {
26384 if (complain & tf_error)
26385 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26386 return error_mark_node;
26387 }
26388 }
26389 else
26390 {
26391 tree parms = build_tree_list (NULL_TREE, type);
26392 tree tparms;
26393
26394 if (flag_concepts)
26395 tparms = extract_autos (type);
26396 else
26397 {
26398 tparms = make_tree_vec (1);
26399 TREE_VEC_ELT (tparms, 0)
26400 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26401 }
26402
26403 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26404 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26405 DEDUCE_CALL, LOOKUP_NORMAL,
26406 NULL, /*explain_p=*/false);
26407 if (val > 0)
26408 {
26409 if (processing_template_decl)
26410 /* Try again at instantiation time. */
26411 return type;
26412 if (type && type != error_mark_node
26413 && (complain & tf_error))
26414 /* If type is error_mark_node a diagnostic must have been
26415 emitted by now. Also, having a mention to '<type error>'
26416 in the diagnostic is not really useful to the user. */
26417 {
26418 if (cfun && auto_node == current_function_auto_return_pattern
26419 && LAMBDA_FUNCTION_P (current_function_decl))
26420 error ("unable to deduce lambda return type from %qE", init);
26421 else
26422 error ("unable to deduce %qT from %qE", type, init);
26423 type_unification_real (tparms, targs, parms, &init, 1, 0,
26424 DEDUCE_CALL, LOOKUP_NORMAL,
26425 NULL, /*explain_p=*/true);
26426 }
26427 return error_mark_node;
26428 }
26429 }
26430
26431 /* Check any placeholder constraints against the deduced type. */
26432 if (flag_concepts && !processing_template_decl)
26433 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26434 {
26435 /* Use the deduced type to check the associated constraints. If we
26436 have a partial-concept-id, rebuild the argument list so that
26437 we check using the extra arguments. */
26438 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26439 tree cargs = CHECK_CONSTR_ARGS (constr);
26440 if (TREE_VEC_LENGTH (cargs) > 1)
26441 {
26442 cargs = copy_node (cargs);
26443 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26444 }
26445 else
26446 cargs = targs;
26447 if (!constraints_satisfied_p (constr, cargs))
26448 {
26449 if (complain & tf_warning_or_error)
26450 {
26451 switch (context)
26452 {
26453 case adc_unspecified:
26454 case adc_unify:
26455 error("placeholder constraints not satisfied");
26456 break;
26457 case adc_variable_type:
26458 case adc_decomp_type:
26459 error ("deduced initializer does not satisfy "
26460 "placeholder constraints");
26461 break;
26462 case adc_return_type:
26463 error ("deduced return type does not satisfy "
26464 "placeholder constraints");
26465 break;
26466 case adc_requirement:
26467 error ("deduced expression type does not satisfy "
26468 "placeholder constraints");
26469 break;
26470 }
26471 diagnose_constraints (input_location, constr, targs);
26472 }
26473 return error_mark_node;
26474 }
26475 }
26476
26477 if (processing_template_decl && context != adc_unify)
26478 outer_targs = current_template_args ();
26479 targs = add_to_template_args (outer_targs, targs);
26480 return tsubst (type, targs, complain, NULL_TREE);
26481 }
26482
26483 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26484 result. */
26485
26486 tree
26487 splice_late_return_type (tree type, tree late_return_type)
26488 {
26489 if (is_auto (type))
26490 {
26491 if (late_return_type)
26492 return late_return_type;
26493
26494 tree idx = get_template_parm_index (type);
26495 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26496 /* In an abbreviated function template we didn't know we were dealing
26497 with a function template when we saw the auto return type, so update
26498 it to have the correct level. */
26499 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26500 }
26501 return type;
26502 }
26503
26504 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26505 'decltype(auto)' or a deduced class template. */
26506
26507 bool
26508 is_auto (const_tree type)
26509 {
26510 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26511 && (TYPE_IDENTIFIER (type) == auto_identifier
26512 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26513 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26514 return true;
26515 else
26516 return false;
26517 }
26518
26519 /* for_each_template_parm callback for type_uses_auto. */
26520
26521 int
26522 is_auto_r (tree tp, void */*data*/)
26523 {
26524 return is_auto (tp);
26525 }
26526
26527 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26528 a use of `auto'. Returns NULL_TREE otherwise. */
26529
26530 tree
26531 type_uses_auto (tree type)
26532 {
26533 if (type == NULL_TREE)
26534 return NULL_TREE;
26535 else if (flag_concepts)
26536 {
26537 /* The Concepts TS allows multiple autos in one type-specifier; just
26538 return the first one we find, do_auto_deduction will collect all of
26539 them. */
26540 if (uses_template_parms (type))
26541 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26542 /*visited*/NULL, /*nondeduced*/true);
26543 else
26544 return NULL_TREE;
26545 }
26546 else
26547 return find_type_usage (type, is_auto);
26548 }
26549
26550 /* For a given template T, return the vector of typedefs referenced
26551 in T for which access check is needed at T instantiation time.
26552 T is either a FUNCTION_DECL or a RECORD_TYPE.
26553 Those typedefs were added to T by the function
26554 append_type_to_template_for_access_check. */
26555
26556 vec<qualified_typedef_usage_t, va_gc> *
26557 get_types_needing_access_check (tree t)
26558 {
26559 tree ti;
26560 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26561
26562 if (!t || t == error_mark_node)
26563 return NULL;
26564
26565 if (!(ti = get_template_info (t)))
26566 return NULL;
26567
26568 if (CLASS_TYPE_P (t)
26569 || TREE_CODE (t) == FUNCTION_DECL)
26570 {
26571 if (!TI_TEMPLATE (ti))
26572 return NULL;
26573
26574 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26575 }
26576
26577 return result;
26578 }
26579
26580 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26581 tied to T. That list of typedefs will be access checked at
26582 T instantiation time.
26583 T is either a FUNCTION_DECL or a RECORD_TYPE.
26584 TYPE_DECL is a TYPE_DECL node representing a typedef.
26585 SCOPE is the scope through which TYPE_DECL is accessed.
26586 LOCATION is the location of the usage point of TYPE_DECL.
26587
26588 This function is a subroutine of
26589 append_type_to_template_for_access_check. */
26590
26591 static void
26592 append_type_to_template_for_access_check_1 (tree t,
26593 tree type_decl,
26594 tree scope,
26595 location_t location)
26596 {
26597 qualified_typedef_usage_t typedef_usage;
26598 tree ti;
26599
26600 if (!t || t == error_mark_node)
26601 return;
26602
26603 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26604 || CLASS_TYPE_P (t))
26605 && type_decl
26606 && TREE_CODE (type_decl) == TYPE_DECL
26607 && scope);
26608
26609 if (!(ti = get_template_info (t)))
26610 return;
26611
26612 gcc_assert (TI_TEMPLATE (ti));
26613
26614 typedef_usage.typedef_decl = type_decl;
26615 typedef_usage.context = scope;
26616 typedef_usage.locus = location;
26617
26618 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26619 }
26620
26621 /* Append TYPE_DECL to the template TEMPL.
26622 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26623 At TEMPL instanciation time, TYPE_DECL will be checked to see
26624 if it can be accessed through SCOPE.
26625 LOCATION is the location of the usage point of TYPE_DECL.
26626
26627 e.g. consider the following code snippet:
26628
26629 class C
26630 {
26631 typedef int myint;
26632 };
26633
26634 template<class U> struct S
26635 {
26636 C::myint mi; // <-- usage point of the typedef C::myint
26637 };
26638
26639 S<char> s;
26640
26641 At S<char> instantiation time, we need to check the access of C::myint
26642 In other words, we need to check the access of the myint typedef through
26643 the C scope. For that purpose, this function will add the myint typedef
26644 and the scope C through which its being accessed to a list of typedefs
26645 tied to the template S. That list will be walked at template instantiation
26646 time and access check performed on each typedefs it contains.
26647 Note that this particular code snippet should yield an error because
26648 myint is private to C. */
26649
26650 void
26651 append_type_to_template_for_access_check (tree templ,
26652 tree type_decl,
26653 tree scope,
26654 location_t location)
26655 {
26656 qualified_typedef_usage_t *iter;
26657 unsigned i;
26658
26659 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26660
26661 /* Make sure we don't append the type to the template twice. */
26662 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26663 if (iter->typedef_decl == type_decl && scope == iter->context)
26664 return;
26665
26666 append_type_to_template_for_access_check_1 (templ, type_decl,
26667 scope, location);
26668 }
26669
26670 /* Convert the generic type parameters in PARM that match the types given in the
26671 range [START_IDX, END_IDX) from the current_template_parms into generic type
26672 packs. */
26673
26674 tree
26675 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26676 {
26677 tree current = current_template_parms;
26678 int depth = TMPL_PARMS_DEPTH (current);
26679 current = INNERMOST_TEMPLATE_PARMS (current);
26680 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26681
26682 for (int i = 0; i < start_idx; ++i)
26683 TREE_VEC_ELT (replacement, i)
26684 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26685
26686 for (int i = start_idx; i < end_idx; ++i)
26687 {
26688 /* Create a distinct parameter pack type from the current parm and add it
26689 to the replacement args to tsubst below into the generic function
26690 parameter. */
26691
26692 tree o = TREE_TYPE (TREE_VALUE
26693 (TREE_VEC_ELT (current, i)));
26694 tree t = copy_type (o);
26695 TEMPLATE_TYPE_PARM_INDEX (t)
26696 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26697 o, 0, 0, tf_none);
26698 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26699 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26700 TYPE_MAIN_VARIANT (t) = t;
26701 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26702 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26703 TREE_VEC_ELT (replacement, i) = t;
26704 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26705 }
26706
26707 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26708 TREE_VEC_ELT (replacement, i)
26709 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26710
26711 /* If there are more levels then build up the replacement with the outer
26712 template parms. */
26713 if (depth > 1)
26714 replacement = add_to_template_args (template_parms_to_args
26715 (TREE_CHAIN (current_template_parms)),
26716 replacement);
26717
26718 return tsubst (parm, replacement, tf_none, NULL_TREE);
26719 }
26720
26721 /* Entries in the decl_constraint hash table. */
26722 struct GTY((for_user)) constr_entry
26723 {
26724 tree decl;
26725 tree ci;
26726 };
26727
26728 /* Hashing function and equality for constraint entries. */
26729 struct constr_hasher : ggc_ptr_hash<constr_entry>
26730 {
26731 static hashval_t hash (constr_entry *e)
26732 {
26733 return (hashval_t)DECL_UID (e->decl);
26734 }
26735
26736 static bool equal (constr_entry *e1, constr_entry *e2)
26737 {
26738 return e1->decl == e2->decl;
26739 }
26740 };
26741
26742 /* A mapping from declarations to constraint information. Note that
26743 both templates and their underlying declarations are mapped to the
26744 same constraint information.
26745
26746 FIXME: This is defined in pt.c because garbage collection
26747 code is not being generated for constraint.cc. */
26748
26749 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26750
26751 /* Returns the template constraints of declaration T. If T is not
26752 constrained, return NULL_TREE. Note that T must be non-null. */
26753
26754 tree
26755 get_constraints (tree t)
26756 {
26757 if (!flag_concepts)
26758 return NULL_TREE;
26759
26760 gcc_assert (DECL_P (t));
26761 if (TREE_CODE (t) == TEMPLATE_DECL)
26762 t = DECL_TEMPLATE_RESULT (t);
26763 constr_entry elt = { t, NULL_TREE };
26764 constr_entry* found = decl_constraints->find (&elt);
26765 if (found)
26766 return found->ci;
26767 else
26768 return NULL_TREE;
26769 }
26770
26771 /* Associate the given constraint information CI with the declaration
26772 T. If T is a template, then the constraints are associated with
26773 its underlying declaration. Don't build associations if CI is
26774 NULL_TREE. */
26775
26776 void
26777 set_constraints (tree t, tree ci)
26778 {
26779 if (!ci)
26780 return;
26781 gcc_assert (t && flag_concepts);
26782 if (TREE_CODE (t) == TEMPLATE_DECL)
26783 t = DECL_TEMPLATE_RESULT (t);
26784 gcc_assert (!get_constraints (t));
26785 constr_entry elt = {t, ci};
26786 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26787 constr_entry* entry = ggc_alloc<constr_entry> ();
26788 *entry = elt;
26789 *slot = entry;
26790 }
26791
26792 /* Remove the associated constraints of the declaration T. */
26793
26794 void
26795 remove_constraints (tree t)
26796 {
26797 gcc_assert (DECL_P (t));
26798 if (TREE_CODE (t) == TEMPLATE_DECL)
26799 t = DECL_TEMPLATE_RESULT (t);
26800
26801 constr_entry elt = {t, NULL_TREE};
26802 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26803 if (slot)
26804 decl_constraints->clear_slot (slot);
26805 }
26806
26807 /* Memoized satisfaction results for declarations. This
26808 maps the pair (constraint_info, arguments) to the result computed
26809 by constraints_satisfied_p. */
26810
26811 struct GTY((for_user)) constraint_sat_entry
26812 {
26813 tree ci;
26814 tree args;
26815 tree result;
26816 };
26817
26818 /* Hashing function and equality for constraint entries. */
26819
26820 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26821 {
26822 static hashval_t hash (constraint_sat_entry *e)
26823 {
26824 hashval_t val = iterative_hash_object(e->ci, 0);
26825 return iterative_hash_template_arg (e->args, val);
26826 }
26827
26828 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26829 {
26830 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26831 }
26832 };
26833
26834 /* Memoized satisfaction results for concept checks. */
26835
26836 struct GTY((for_user)) concept_spec_entry
26837 {
26838 tree tmpl;
26839 tree args;
26840 tree result;
26841 };
26842
26843 /* Hashing function and equality for constraint entries. */
26844
26845 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26846 {
26847 static hashval_t hash (concept_spec_entry *e)
26848 {
26849 return hash_tmpl_and_args (e->tmpl, e->args);
26850 }
26851
26852 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26853 {
26854 ++comparing_specializations;
26855 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26856 --comparing_specializations;
26857 return eq;
26858 }
26859 };
26860
26861 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26862 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26863
26864 /* Search for a memoized satisfaction result. Returns one of the
26865 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26866
26867 tree
26868 lookup_constraint_satisfaction (tree ci, tree args)
26869 {
26870 constraint_sat_entry elt = { ci, args, NULL_TREE };
26871 constraint_sat_entry* found = constraint_memos->find (&elt);
26872 if (found)
26873 return found->result;
26874 else
26875 return NULL_TREE;
26876 }
26877
26878 /* Memoize the result of a satisfication test. Returns the saved result. */
26879
26880 tree
26881 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26882 {
26883 constraint_sat_entry elt = {ci, args, result};
26884 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26885 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26886 *entry = elt;
26887 *slot = entry;
26888 return result;
26889 }
26890
26891 /* Search for a memoized satisfaction result for a concept. */
26892
26893 tree
26894 lookup_concept_satisfaction (tree tmpl, tree args)
26895 {
26896 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26897 concept_spec_entry* found = concept_memos->find (&elt);
26898 if (found)
26899 return found->result;
26900 else
26901 return NULL_TREE;
26902 }
26903
26904 /* Memoize the result of a concept check. Returns the saved result. */
26905
26906 tree
26907 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26908 {
26909 concept_spec_entry elt = {tmpl, args, result};
26910 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26911 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26912 *entry = elt;
26913 *slot = entry;
26914 return result;
26915 }
26916
26917 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26918
26919 /* Returns a prior concept specialization. This returns the substituted
26920 and normalized constraints defined by the concept. */
26921
26922 tree
26923 get_concept_expansion (tree tmpl, tree args)
26924 {
26925 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26926 concept_spec_entry* found = concept_expansions->find (&elt);
26927 if (found)
26928 return found->result;
26929 else
26930 return NULL_TREE;
26931 }
26932
26933 /* Save a concept expansion for later. */
26934
26935 tree
26936 save_concept_expansion (tree tmpl, tree args, tree def)
26937 {
26938 concept_spec_entry elt = {tmpl, args, def};
26939 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26940 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26941 *entry = elt;
26942 *slot = entry;
26943 return def;
26944 }
26945
26946 static hashval_t
26947 hash_subsumption_args (tree t1, tree t2)
26948 {
26949 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26950 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26951 int val = 0;
26952 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26953 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26954 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26955 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26956 return val;
26957 }
26958
26959 /* Compare the constraints of two subsumption entries. The LEFT1 and
26960 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26961 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26962
26963 static bool
26964 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26965 {
26966 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26967 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26968 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26969 CHECK_CONSTR_ARGS (right1)))
26970 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26971 CHECK_CONSTR_ARGS (right2));
26972 return false;
26973 }
26974
26975 /* Key/value pair for learning and memoizing subsumption results. This
26976 associates a pair of check constraints (including arguments) with
26977 a boolean value indicating the result. */
26978
26979 struct GTY((for_user)) subsumption_entry
26980 {
26981 tree t1;
26982 tree t2;
26983 bool result;
26984 };
26985
26986 /* Hashing function and equality for constraint entries. */
26987
26988 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26989 {
26990 static hashval_t hash (subsumption_entry *e)
26991 {
26992 return hash_subsumption_args (e->t1, e->t2);
26993 }
26994
26995 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26996 {
26997 ++comparing_specializations;
26998 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26999 --comparing_specializations;
27000 return eq;
27001 }
27002 };
27003
27004 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
27005
27006 /* Search for a previously cached subsumption result. */
27007
27008 bool*
27009 lookup_subsumption_result (tree t1, tree t2)
27010 {
27011 subsumption_entry elt = { t1, t2, false };
27012 subsumption_entry* found = subsumption_table->find (&elt);
27013 if (found)
27014 return &found->result;
27015 else
27016 return 0;
27017 }
27018
27019 /* Save a subsumption result. */
27020
27021 bool
27022 save_subsumption_result (tree t1, tree t2, bool result)
27023 {
27024 subsumption_entry elt = {t1, t2, result};
27025 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
27026 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
27027 *entry = elt;
27028 *slot = entry;
27029 return result;
27030 }
27031
27032 /* Set up the hash table for constraint association. */
27033
27034 void
27035 init_constraint_processing (void)
27036 {
27037 if (!flag_concepts)
27038 return;
27039
27040 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
27041 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
27042 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
27043 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
27044 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
27045 }
27046
27047 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27048 0..N-1. */
27049
27050 void
27051 declare_integer_pack (void)
27052 {
27053 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
27054 build_function_type_list (integer_type_node,
27055 integer_type_node,
27056 NULL_TREE),
27057 NULL_TREE, ECF_CONST);
27058 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
27059 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
27060 }
27061
27062 /* Set up the hash tables for template instantiations. */
27063
27064 void
27065 init_template_processing (void)
27066 {
27067 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
27068 type_specializations = hash_table<spec_hasher>::create_ggc (37);
27069
27070 if (cxx_dialect >= cxx11)
27071 declare_integer_pack ();
27072 }
27073
27074 /* Print stats about the template hash tables for -fstats. */
27075
27076 void
27077 print_template_statistics (void)
27078 {
27079 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
27080 "%f collisions\n", (long) decl_specializations->size (),
27081 (long) decl_specializations->elements (),
27082 decl_specializations->collisions ());
27083 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
27084 "%f collisions\n", (long) type_specializations->size (),
27085 (long) type_specializations->elements (),
27086 type_specializations->collisions ());
27087 }
27088
27089 #if CHECKING_P
27090
27091 namespace selftest {
27092
27093 /* Verify that build_non_dependent_expr () works, for various expressions,
27094 and that location wrappers don't affect the results. */
27095
27096 static void
27097 test_build_non_dependent_expr ()
27098 {
27099 location_t loc = BUILTINS_LOCATION;
27100
27101 /* Verify constants, without and with location wrappers. */
27102 tree int_cst = build_int_cst (integer_type_node, 42);
27103 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
27104
27105 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
27106 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
27107 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
27108
27109 tree string_lit = build_string (4, "foo");
27110 TREE_TYPE (string_lit) = char_array_type_node;
27111 string_lit = fix_string_type (string_lit);
27112 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
27113
27114 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
27115 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
27116 ASSERT_EQ (wrapped_string_lit,
27117 build_non_dependent_expr (wrapped_string_lit));
27118 }
27119
27120 /* Verify that type_dependent_expression_p () works correctly, even
27121 in the presence of location wrapper nodes. */
27122
27123 static void
27124 test_type_dependent_expression_p ()
27125 {
27126 location_t loc = BUILTINS_LOCATION;
27127
27128 tree name = get_identifier ("foo");
27129
27130 /* If no templates are involved, nothing is type-dependent. */
27131 gcc_assert (!processing_template_decl);
27132 ASSERT_FALSE (type_dependent_expression_p (name));
27133
27134 ++processing_template_decl;
27135
27136 /* Within a template, an unresolved name is always type-dependent. */
27137 ASSERT_TRUE (type_dependent_expression_p (name));
27138
27139 /* Ensure it copes with NULL_TREE and errors. */
27140 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
27141 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
27142
27143 /* A USING_DECL in a template should be type-dependent, even if wrapped
27144 with a location wrapper (PR c++/83799). */
27145 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
27146 TREE_TYPE (using_decl) = integer_type_node;
27147 ASSERT_TRUE (type_dependent_expression_p (using_decl));
27148 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
27149 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
27150 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
27151
27152 --processing_template_decl;
27153 }
27154
27155 /* Run all of the selftests within this file. */
27156
27157 void
27158 cp_pt_c_tests ()
27159 {
27160 test_build_non_dependent_expr ();
27161 test_type_dependent_expression_p ();
27162 }
27163
27164 } // namespace selftest
27165
27166 #endif /* #if CHECKING_P */
27167
27168 #include "gt-cp-pt.h"