]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
re PR c++/79654 (ICE on invalid c++ code in register_dtor_fn in cp/decl.c:7877)
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 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
44 /* The type of functions taking a tree, and some additional data, and
45 returning an int. */
46 typedef int (*tree_fn_t) (tree, void*);
47
48 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51 struct GTY ((chain_next ("%h.next"))) pending_template {
52 struct pending_template *next;
53 struct tinst_level *tinst;
54 };
55
56 static GTY(()) struct pending_template *pending_templates;
57 static GTY(()) struct pending_template *last_pending_template;
58
59 int processing_template_parmlist;
60 static int template_header_count;
61
62 static GTY(()) tree saved_trees;
63 static vec<int> inline_parm_levels;
64
65 static GTY(()) struct tinst_level *current_tinst_level;
66
67 static GTY(()) tree saved_access_scope;
68
69 /* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72 static tree cur_stmt_expr;
73
74 // -------------------------------------------------------------------------- //
75 // Local Specialization Stack
76 //
77 // Implementation of the RAII helper for creating new local
78 // specializations.
79 local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations)
81 {
82 local_specializations = new hash_map<tree, tree>;
83 }
84
85 local_specialization_stack::~local_specialization_stack ()
86 {
87 delete local_specializations;
88 local_specializations = saved;
89 }
90
91 /* True if we've recursed into fn_type_unification too many times. */
92 static bool excessive_deduction_depth;
93
94 struct GTY((for_user)) spec_entry
95 {
96 tree tmpl;
97 tree args;
98 tree spec;
99 };
100
101 struct spec_hasher : ggc_ptr_hash<spec_entry>
102 {
103 static hashval_t hash (spec_entry *);
104 static bool equal (spec_entry *, spec_entry *);
105 };
106
107 static GTY (()) hash_table<spec_hasher> *decl_specializations;
108
109 static GTY (()) hash_table<spec_hasher> *type_specializations;
110
111 /* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
116
117 #define UNIFY_ALLOW_NONE 0
118 #define UNIFY_ALLOW_MORE_CV_QUAL 1
119 #define UNIFY_ALLOW_LESS_CV_QUAL 2
120 #define UNIFY_ALLOW_DERIVED 4
121 #define UNIFY_ALLOW_INTEGER 8
122 #define UNIFY_ALLOW_OUTER_LEVEL 16
123 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
125
126 enum template_base_result {
127 tbr_incomplete_type,
128 tbr_ambiguous_baseclass,
129 tbr_success
130 };
131
132 static void push_access_scope (tree);
133 static void pop_access_scope (tree);
134 static bool resolve_overloaded_unification (tree, tree, tree, tree,
135 unification_kind_t, int,
136 bool);
137 static int try_one_overload (tree, tree, tree, tree, tree,
138 unification_kind_t, int, bool, bool);
139 static int unify (tree, tree, tree, tree, int, bool);
140 static void add_pending_template (tree);
141 static tree reopen_tinst_level (struct tinst_level *);
142 static tree tsubst_initializer_list (tree, tree);
143 static tree get_partial_spec_bindings (tree, tree, tree);
144 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
145 bool, bool);
146 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
147 bool, bool);
148 static void tsubst_enum (tree, tree, tree);
149 static tree add_to_template_args (tree, tree);
150 static tree add_outermost_template_args (tree, tree);
151 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
152 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
153 tree);
154 static int type_unification_real (tree, tree, tree, const tree *,
155 unsigned int, int, unification_kind_t, int,
156 vec<deferred_access_check, va_gc> **,
157 bool);
158 static void note_template_header (int);
159 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
160 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
161 static tree convert_template_argument (tree, tree, tree,
162 tsubst_flags_t, int, tree);
163 static tree for_each_template_parm (tree, tree_fn_t, void*,
164 hash_set<tree> *, bool, tree_fn_t = NULL);
165 static tree expand_template_argument_pack (tree);
166 static tree build_template_parm_index (int, int, int, tree, tree);
167 static bool inline_needs_template_parms (tree, bool);
168 static void push_inline_template_parms_recursive (tree, int);
169 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
170 static int mark_template_parm (tree, void *);
171 static int template_parm_this_level_p (tree, void *);
172 static tree tsubst_friend_function (tree, tree);
173 static tree tsubst_friend_class (tree, tree);
174 static int can_complete_type_without_circularity (tree);
175 static tree get_bindings (tree, tree, tree, bool);
176 static int template_decl_level (tree);
177 static int check_cv_quals_for_unify (int, tree, tree);
178 static void template_parm_level_and_index (tree, int*, int*);
179 static int unify_pack_expansion (tree, tree, tree,
180 tree, unification_kind_t, bool, bool);
181 static tree copy_template_args (tree);
182 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
184 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
185 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
186 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
187 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
189 static bool check_specialization_scope (void);
190 static tree process_partial_specialization (tree);
191 static void set_current_access_from_decl (tree);
192 static enum template_base_result get_template_base (tree, tree, tree, tree,
193 bool , tree *);
194 static tree try_class_unification (tree, tree, tree, tree, bool);
195 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
196 tree, tree);
197 static bool template_template_parm_bindings_ok_p (tree, tree);
198 static void tsubst_default_arguments (tree, tsubst_flags_t);
199 static tree for_each_template_parm_r (tree *, int *, void *);
200 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
201 static void copy_default_args_to_explicit_spec (tree);
202 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
203 static bool dependent_template_arg_p (tree);
204 static bool any_template_arguments_need_structural_equality_p (tree);
205 static bool dependent_type_p_r (tree);
206 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
207 static tree tsubst_decl (tree, tree, tsubst_flags_t);
208 static void perform_typedefs_access_check (tree tmpl, tree targs);
209 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
210 location_t);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215 static bool complex_alias_template_p (const_tree tmpl);
216 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
217 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
218
219 /* Make the current scope suitable for access checking when we are
220 processing T. T can be FUNCTION_DECL for instantiated function
221 template, VAR_DECL for static member variable, or TYPE_DECL for
222 alias template (needed by instantiate_decl). */
223
224 static void
225 push_access_scope (tree t)
226 {
227 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
228 || TREE_CODE (t) == TYPE_DECL);
229
230 if (DECL_FRIEND_CONTEXT (t))
231 push_nested_class (DECL_FRIEND_CONTEXT (t));
232 else if (DECL_CLASS_SCOPE_P (t))
233 push_nested_class (DECL_CONTEXT (t));
234 else
235 push_to_top_level ();
236
237 if (TREE_CODE (t) == FUNCTION_DECL)
238 {
239 saved_access_scope = tree_cons
240 (NULL_TREE, current_function_decl, saved_access_scope);
241 current_function_decl = t;
242 }
243 }
244
245 /* Restore the scope set up by push_access_scope. T is the node we
246 are processing. */
247
248 static void
249 pop_access_scope (tree t)
250 {
251 if (TREE_CODE (t) == FUNCTION_DECL)
252 {
253 current_function_decl = TREE_VALUE (saved_access_scope);
254 saved_access_scope = TREE_CHAIN (saved_access_scope);
255 }
256
257 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
258 pop_nested_class ();
259 else
260 pop_from_top_level ();
261 }
262
263 /* Do any processing required when DECL (a member template
264 declaration) is finished. Returns the TEMPLATE_DECL corresponding
265 to DECL, unless it is a specialization, in which case the DECL
266 itself is returned. */
267
268 tree
269 finish_member_template_decl (tree decl)
270 {
271 if (decl == error_mark_node)
272 return error_mark_node;
273
274 gcc_assert (DECL_P (decl));
275
276 if (TREE_CODE (decl) == TYPE_DECL)
277 {
278 tree type;
279
280 type = TREE_TYPE (decl);
281 if (type == error_mark_node)
282 return error_mark_node;
283 if (MAYBE_CLASS_TYPE_P (type)
284 && CLASSTYPE_TEMPLATE_INFO (type)
285 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
286 {
287 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
288 check_member_template (tmpl);
289 return tmpl;
290 }
291 return NULL_TREE;
292 }
293 else if (TREE_CODE (decl) == FIELD_DECL)
294 error ("data member %qD cannot be a member template", decl);
295 else if (DECL_TEMPLATE_INFO (decl))
296 {
297 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
298 {
299 check_member_template (DECL_TI_TEMPLATE (decl));
300 return DECL_TI_TEMPLATE (decl);
301 }
302 else
303 return decl;
304 }
305 else
306 error ("invalid member template declaration %qD", decl);
307
308 return error_mark_node;
309 }
310
311 /* Create a template info node. */
312
313 tree
314 build_template_info (tree template_decl, tree template_args)
315 {
316 tree result = make_node (TEMPLATE_INFO);
317 TI_TEMPLATE (result) = template_decl;
318 TI_ARGS (result) = template_args;
319 return result;
320 }
321
322 /* Return the template info node corresponding to T, whatever T is. */
323
324 tree
325 get_template_info (const_tree t)
326 {
327 tree tinfo = NULL_TREE;
328
329 if (!t || t == error_mark_node)
330 return NULL;
331
332 if (TREE_CODE (t) == NAMESPACE_DECL
333 || TREE_CODE (t) == PARM_DECL)
334 return NULL;
335
336 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
337 tinfo = DECL_TEMPLATE_INFO (t);
338
339 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
340 t = TREE_TYPE (t);
341
342 if (OVERLOAD_TYPE_P (t))
343 tinfo = TYPE_TEMPLATE_INFO (t);
344 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
345 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
346
347 return tinfo;
348 }
349
350 /* Returns the template nesting level of the indicated class TYPE.
351
352 For example, in:
353 template <class T>
354 struct A
355 {
356 template <class U>
357 struct B {};
358 };
359
360 A<T>::B<U> has depth two, while A<T> has depth one.
361 Both A<T>::B<int> and A<int>::B<U> have depth one, if
362 they are instantiations, not specializations.
363
364 This function is guaranteed to return 0 if passed NULL_TREE so
365 that, for example, `template_class_depth (current_class_type)' is
366 always safe. */
367
368 int
369 template_class_depth (tree type)
370 {
371 int depth;
372
373 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
374 {
375 tree tinfo = get_template_info (type);
376
377 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
378 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
379 ++depth;
380
381 if (DECL_P (type))
382 type = CP_DECL_CONTEXT (type);
383 else if (LAMBDA_TYPE_P (type))
384 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
385 else
386 type = CP_TYPE_CONTEXT (type);
387 }
388
389 return depth;
390 }
391
392 /* Subroutine of maybe_begin_member_template_processing.
393 Returns true if processing DECL needs us to push template parms. */
394
395 static bool
396 inline_needs_template_parms (tree decl, bool nsdmi)
397 {
398 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
399 return false;
400
401 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
402 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
403 }
404
405 /* Subroutine of maybe_begin_member_template_processing.
406 Push the template parms in PARMS, starting from LEVELS steps into the
407 chain, and ending at the beginning, since template parms are listed
408 innermost first. */
409
410 static void
411 push_inline_template_parms_recursive (tree parmlist, int levels)
412 {
413 tree parms = TREE_VALUE (parmlist);
414 int i;
415
416 if (levels > 1)
417 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
418
419 ++processing_template_decl;
420 current_template_parms
421 = tree_cons (size_int (processing_template_decl),
422 parms, current_template_parms);
423 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
424
425 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
426 NULL);
427 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
428 {
429 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
430
431 if (error_operand_p (parm))
432 continue;
433
434 gcc_assert (DECL_P (parm));
435
436 switch (TREE_CODE (parm))
437 {
438 case TYPE_DECL:
439 case TEMPLATE_DECL:
440 pushdecl (parm);
441 break;
442
443 case PARM_DECL:
444 /* Push the CONST_DECL. */
445 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
446 break;
447
448 default:
449 gcc_unreachable ();
450 }
451 }
452 }
453
454 /* Restore the template parameter context for a member template, a
455 friend template defined in a class definition, or a non-template
456 member of template class. */
457
458 void
459 maybe_begin_member_template_processing (tree decl)
460 {
461 tree parms;
462 int levels = 0;
463 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
464
465 if (nsdmi)
466 {
467 tree ctx = DECL_CONTEXT (decl);
468 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
469 /* Disregard full specializations (c++/60999). */
470 && uses_template_parms (ctx)
471 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
472 }
473
474 if (inline_needs_template_parms (decl, nsdmi))
475 {
476 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
477 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
478
479 if (DECL_TEMPLATE_SPECIALIZATION (decl))
480 {
481 --levels;
482 parms = TREE_CHAIN (parms);
483 }
484
485 push_inline_template_parms_recursive (parms, levels);
486 }
487
488 /* Remember how many levels of template parameters we pushed so that
489 we can pop them later. */
490 inline_parm_levels.safe_push (levels);
491 }
492
493 /* Undo the effects of maybe_begin_member_template_processing. */
494
495 void
496 maybe_end_member_template_processing (void)
497 {
498 int i;
499 int last;
500
501 if (inline_parm_levels.length () == 0)
502 return;
503
504 last = inline_parm_levels.pop ();
505 for (i = 0; i < last; ++i)
506 {
507 --processing_template_decl;
508 current_template_parms = TREE_CHAIN (current_template_parms);
509 poplevel (0, 0, 0);
510 }
511 }
512
513 /* Return a new template argument vector which contains all of ARGS,
514 but has as its innermost set of arguments the EXTRA_ARGS. */
515
516 static tree
517 add_to_template_args (tree args, tree extra_args)
518 {
519 tree new_args;
520 int extra_depth;
521 int i;
522 int j;
523
524 if (args == NULL_TREE || extra_args == error_mark_node)
525 return extra_args;
526
527 extra_depth = TMPL_ARGS_DEPTH (extra_args);
528 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
529
530 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
531 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
532
533 for (j = 1; j <= extra_depth; ++j, ++i)
534 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
535
536 return new_args;
537 }
538
539 /* Like add_to_template_args, but only the outermost ARGS are added to
540 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
541 (EXTRA_ARGS) levels are added. This function is used to combine
542 the template arguments from a partial instantiation with the
543 template arguments used to attain the full instantiation from the
544 partial instantiation. */
545
546 static tree
547 add_outermost_template_args (tree args, tree extra_args)
548 {
549 tree new_args;
550
551 /* If there are more levels of EXTRA_ARGS than there are ARGS,
552 something very fishy is going on. */
553 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
554
555 /* If *all* the new arguments will be the EXTRA_ARGS, just return
556 them. */
557 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
558 return extra_args;
559
560 /* For the moment, we make ARGS look like it contains fewer levels. */
561 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
562
563 new_args = add_to_template_args (args, extra_args);
564
565 /* Now, we restore ARGS to its full dimensions. */
566 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
567
568 return new_args;
569 }
570
571 /* Return the N levels of innermost template arguments from the ARGS. */
572
573 tree
574 get_innermost_template_args (tree args, int n)
575 {
576 tree new_args;
577 int extra_levels;
578 int i;
579
580 gcc_assert (n >= 0);
581
582 /* If N is 1, just return the innermost set of template arguments. */
583 if (n == 1)
584 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
585
586 /* If we're not removing anything, just return the arguments we were
587 given. */
588 extra_levels = TMPL_ARGS_DEPTH (args) - n;
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
591 return args;
592
593 /* Make a new set of arguments, not containing the outer arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i + extra_levels));
598
599 return new_args;
600 }
601
602 /* The inverse of get_innermost_template_args: Return all but the innermost
603 EXTRA_LEVELS levels of template arguments from the ARGS. */
604
605 static tree
606 strip_innermost_template_args (tree args, int extra_levels)
607 {
608 tree new_args;
609 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
610 int i;
611
612 gcc_assert (n >= 0);
613
614 /* If N is 1, just return the outermost set of template arguments. */
615 if (n == 1)
616 return TMPL_ARGS_LEVEL (args, 1);
617
618 /* If we're not removing anything, just return the arguments we were
619 given. */
620 gcc_assert (extra_levels >= 0);
621 if (extra_levels == 0)
622 return args;
623
624 /* Make a new set of arguments, not containing the inner arguments. */
625 new_args = make_tree_vec (n);
626 for (i = 1; i <= n; ++i)
627 SET_TMPL_ARGS_LEVEL (new_args, i,
628 TMPL_ARGS_LEVEL (args, i));
629
630 return new_args;
631 }
632
633 /* We've got a template header coming up; push to a new level for storing
634 the parms. */
635
636 void
637 begin_template_parm_list (void)
638 {
639 /* We use a non-tag-transparent scope here, which causes pushtag to
640 put tags in this scope, rather than in the enclosing class or
641 namespace scope. This is the right thing, since we want
642 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
643 global template class, push_template_decl handles putting the
644 TEMPLATE_DECL into top-level scope. For a nested template class,
645 e.g.:
646
647 template <class T> struct S1 {
648 template <class T> struct S2 {};
649 };
650
651 pushtag contains special code to call pushdecl_with_scope on the
652 TEMPLATE_DECL for S2. */
653 begin_scope (sk_template_parms, NULL);
654 ++processing_template_decl;
655 ++processing_template_parmlist;
656 note_template_header (0);
657
658 /* Add a dummy parameter level while we process the parameter list. */
659 current_template_parms
660 = tree_cons (size_int (processing_template_decl),
661 make_tree_vec (0),
662 current_template_parms);
663 }
664
665 /* This routine is called when a specialization is declared. If it is
666 invalid to declare a specialization here, an error is reported and
667 false is returned, otherwise this routine will return true. */
668
669 static bool
670 check_specialization_scope (void)
671 {
672 tree scope = current_scope ();
673
674 /* [temp.expl.spec]
675
676 An explicit specialization shall be declared in the namespace of
677 which the template is a member, or, for member templates, in the
678 namespace of which the enclosing class or enclosing class
679 template is a member. An explicit specialization of a member
680 function, member class or static data member of a class template
681 shall be declared in the namespace of which the class template
682 is a member. */
683 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
684 {
685 error ("explicit specialization in non-namespace scope %qD", scope);
686 return false;
687 }
688
689 /* [temp.expl.spec]
690
691 In an explicit specialization declaration for a member of a class
692 template or a member template that appears in namespace scope,
693 the member template and some of its enclosing class templates may
694 remain unspecialized, except that the declaration shall not
695 explicitly specialize a class member template if its enclosing
696 class templates are not explicitly specialized as well. */
697 if (current_template_parms)
698 {
699 error ("enclosing class templates are not explicitly specialized");
700 return false;
701 }
702
703 return true;
704 }
705
706 /* We've just seen template <>. */
707
708 bool
709 begin_specialization (void)
710 {
711 begin_scope (sk_template_spec, NULL);
712 note_template_header (1);
713 return check_specialization_scope ();
714 }
715
716 /* Called at then end of processing a declaration preceded by
717 template<>. */
718
719 void
720 end_specialization (void)
721 {
722 finish_scope ();
723 reset_specialization ();
724 }
725
726 /* Any template <>'s that we have seen thus far are not referring to a
727 function specialization. */
728
729 void
730 reset_specialization (void)
731 {
732 processing_specialization = 0;
733 template_header_count = 0;
734 }
735
736 /* We've just seen a template header. If SPECIALIZATION is nonzero,
737 it was of the form template <>. */
738
739 static void
740 note_template_header (int specialization)
741 {
742 processing_specialization = specialization;
743 template_header_count++;
744 }
745
746 /* We're beginning an explicit instantiation. */
747
748 void
749 begin_explicit_instantiation (void)
750 {
751 gcc_assert (!processing_explicit_instantiation);
752 processing_explicit_instantiation = true;
753 }
754
755
756 void
757 end_explicit_instantiation (void)
758 {
759 gcc_assert (processing_explicit_instantiation);
760 processing_explicit_instantiation = false;
761 }
762
763 /* An explicit specialization or partial specialization of TMPL is being
764 declared. Check that the namespace in which the specialization is
765 occurring is permissible. Returns false iff it is invalid to
766 specialize TMPL in the current namespace. */
767
768 static bool
769 check_specialization_namespace (tree tmpl)
770 {
771 tree tpl_ns = decl_namespace_context (tmpl);
772
773 /* [tmpl.expl.spec]
774
775 An explicit specialization shall be declared in a namespace enclosing the
776 specialized template. An explicit specialization whose declarator-id is
777 not qualified shall be declared in the nearest enclosing namespace of the
778 template, or, if the namespace is inline (7.3.1), any namespace from its
779 enclosing namespace set. */
780 if (current_scope() != DECL_CONTEXT (tmpl)
781 && !at_namespace_scope_p ())
782 {
783 error ("specialization of %qD must appear at namespace scope", tmpl);
784 return false;
785 }
786
787 if (cxx_dialect < cxx11
788 ? is_associated_namespace (current_namespace, tpl_ns)
789 : is_ancestor (current_namespace, tpl_ns))
790 /* Same or enclosing namespace. */
791 return true;
792 else
793 {
794 permerror (input_location,
795 "specialization of %qD in different namespace", tmpl);
796 inform (DECL_SOURCE_LOCATION (tmpl),
797 " from definition of %q#D", tmpl);
798 return false;
799 }
800 }
801
802 /* SPEC is an explicit instantiation. Check that it is valid to
803 perform this explicit instantiation in the current namespace. */
804
805 static void
806 check_explicit_instantiation_namespace (tree spec)
807 {
808 tree ns;
809
810 /* DR 275: An explicit instantiation shall appear in an enclosing
811 namespace of its template. */
812 ns = decl_namespace_context (spec);
813 if (!is_ancestor (current_namespace, ns))
814 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
815 "(which does not enclose namespace %qD)",
816 spec, current_namespace, ns);
817 }
818
819 // Returns the type of a template specialization only if that
820 // specialization needs to be defined. Otherwise (e.g., if the type has
821 // already been defined), the function returns NULL_TREE.
822 static tree
823 maybe_new_partial_specialization (tree type)
824 {
825 // An implicit instantiation of an incomplete type implies
826 // the definition of a new class template.
827 //
828 // template<typename T>
829 // struct S;
830 //
831 // template<typename T>
832 // struct S<T*>;
833 //
834 // Here, S<T*> is an implicit instantiation of S whose type
835 // is incomplete.
836 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
837 return type;
838
839 // It can also be the case that TYPE is a completed specialization.
840 // Continuing the previous example, suppose we also declare:
841 //
842 // template<typename T>
843 // requires Integral<T>
844 // struct S<T*>;
845 //
846 // Here, S<T*> refers to the specialization S<T*> defined
847 // above. However, we need to differentiate definitions because
848 // we intend to define a new partial specialization. In this case,
849 // we rely on the fact that the constraints are different for
850 // this declaration than that above.
851 //
852 // Note that we also get here for injected class names and
853 // late-parsed template definitions. We must ensure that we
854 // do not create new type declarations for those cases.
855 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
856 {
857 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
858 tree args = CLASSTYPE_TI_ARGS (type);
859
860 // If there are no template parameters, this cannot be a new
861 // partial template specializtion?
862 if (!current_template_parms)
863 return NULL_TREE;
864
865 // The injected-class-name is not a new partial specialization.
866 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
867 return NULL_TREE;
868
869 // If the constraints are not the same as those of the primary
870 // then, we can probably create a new specialization.
871 tree type_constr = current_template_constraints ();
872
873 if (type == TREE_TYPE (tmpl))
874 {
875 tree main_constr = get_constraints (tmpl);
876 if (equivalent_constraints (type_constr, main_constr))
877 return NULL_TREE;
878 }
879
880 // Also, if there's a pre-existing specialization with matching
881 // constraints, then this also isn't new.
882 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
883 while (specs)
884 {
885 tree spec_tmpl = TREE_VALUE (specs);
886 tree spec_args = TREE_PURPOSE (specs);
887 tree spec_constr = get_constraints (spec_tmpl);
888 if (comp_template_args (args, spec_args)
889 && equivalent_constraints (type_constr, spec_constr))
890 return NULL_TREE;
891 specs = TREE_CHAIN (specs);
892 }
893
894 // Create a new type node (and corresponding type decl)
895 // for the newly declared specialization.
896 tree t = make_class_type (TREE_CODE (type));
897 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
898 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
899
900 /* We only need a separate type node for storing the definition of this
901 partial specialization; uses of S<T*> are unconstrained, so all are
902 equivalent. So keep TYPE_CANONICAL the same. */
903 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
904
905 // Build the corresponding type decl.
906 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
907 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
908 DECL_SOURCE_LOCATION (d) = input_location;
909
910 return t;
911 }
912
913 return NULL_TREE;
914 }
915
916 /* The TYPE is being declared. If it is a template type, that means it
917 is a partial specialization. Do appropriate error-checking. */
918
919 tree
920 maybe_process_partial_specialization (tree type)
921 {
922 tree context;
923
924 if (type == error_mark_node)
925 return error_mark_node;
926
927 /* A lambda that appears in specialization context is not itself a
928 specialization. */
929 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
930 return type;
931
932 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
933 {
934 error ("name of class shadows template template parameter %qD",
935 TYPE_NAME (type));
936 return error_mark_node;
937 }
938
939 context = TYPE_CONTEXT (type);
940
941 if (TYPE_ALIAS_P (type))
942 {
943 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
944
945 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
946 error ("specialization of alias template %qD",
947 TI_TEMPLATE (tinfo));
948 else
949 error ("explicit specialization of non-template %qT", type);
950 return error_mark_node;
951 }
952 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
953 {
954 /* This is for ordinary explicit specialization and partial
955 specialization of a template class such as:
956
957 template <> class C<int>;
958
959 or:
960
961 template <class T> class C<T*>;
962
963 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
964
965 if (tree t = maybe_new_partial_specialization (type))
966 {
967 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
968 && !at_namespace_scope_p ())
969 return error_mark_node;
970 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
971 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
972 if (processing_template_decl)
973 {
974 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
975 if (decl == error_mark_node)
976 return error_mark_node;
977 return TREE_TYPE (decl);
978 }
979 }
980 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
981 error ("specialization of %qT after instantiation", type);
982 else if (errorcount && !processing_specialization
983 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
984 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
985 /* Trying to define a specialization either without a template<> header
986 or in an inappropriate place. We've already given an error, so just
987 bail now so we don't actually define the specialization. */
988 return error_mark_node;
989 }
990 else if (CLASS_TYPE_P (type)
991 && !CLASSTYPE_USE_TEMPLATE (type)
992 && CLASSTYPE_TEMPLATE_INFO (type)
993 && context && CLASS_TYPE_P (context)
994 && CLASSTYPE_TEMPLATE_INFO (context))
995 {
996 /* This is for an explicit specialization of member class
997 template according to [temp.expl.spec/18]:
998
999 template <> template <class U> class C<int>::D;
1000
1001 The context `C<int>' must be an implicit instantiation.
1002 Otherwise this is just a member class template declared
1003 earlier like:
1004
1005 template <> class C<int> { template <class U> class D; };
1006 template <> template <class U> class C<int>::D;
1007
1008 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1009 while in the second case, `C<int>::D' is a primary template
1010 and `C<T>::D' may not exist. */
1011
1012 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1013 && !COMPLETE_TYPE_P (type))
1014 {
1015 tree t;
1016 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1017
1018 if (current_namespace
1019 != decl_namespace_context (tmpl))
1020 {
1021 permerror (input_location,
1022 "specializing %q#T in different namespace", type);
1023 permerror (DECL_SOURCE_LOCATION (tmpl),
1024 " from definition of %q#D", tmpl);
1025 }
1026
1027 /* Check for invalid specialization after instantiation:
1028
1029 template <> template <> class C<int>::D<int>;
1030 template <> template <class U> class C<int>::D; */
1031
1032 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1033 t; t = TREE_CHAIN (t))
1034 {
1035 tree inst = TREE_VALUE (t);
1036 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1037 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1038 {
1039 /* We already have a full specialization of this partial
1040 instantiation, or a full specialization has been
1041 looked up but not instantiated. Reassign it to the
1042 new member specialization template. */
1043 spec_entry elt;
1044 spec_entry *entry;
1045
1046 elt.tmpl = most_general_template (tmpl);
1047 elt.args = CLASSTYPE_TI_ARGS (inst);
1048 elt.spec = inst;
1049
1050 type_specializations->remove_elt (&elt);
1051
1052 elt.tmpl = tmpl;
1053 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1054
1055 spec_entry **slot
1056 = type_specializations->find_slot (&elt, INSERT);
1057 entry = ggc_alloc<spec_entry> ();
1058 *entry = elt;
1059 *slot = entry;
1060 }
1061 else
1062 /* But if we've had an implicit instantiation, that's a
1063 problem ([temp.expl.spec]/6). */
1064 error ("specialization %qT after instantiation %qT",
1065 type, inst);
1066 }
1067
1068 /* Mark TYPE as a specialization. And as a result, we only
1069 have one level of template argument for the innermost
1070 class template. */
1071 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1072 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1073 CLASSTYPE_TI_ARGS (type)
1074 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1075 }
1076 }
1077 else if (processing_specialization)
1078 {
1079 /* Someday C++0x may allow for enum template specialization. */
1080 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1081 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1082 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1083 "of %qD not allowed by ISO C++", type);
1084 else
1085 {
1086 error ("explicit specialization of non-template %qT", type);
1087 return error_mark_node;
1088 }
1089 }
1090
1091 return type;
1092 }
1093
1094 /* Returns nonzero if we can optimize the retrieval of specializations
1095 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1096 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1097
1098 static inline bool
1099 optimize_specialization_lookup_p (tree tmpl)
1100 {
1101 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1102 && DECL_CLASS_SCOPE_P (tmpl)
1103 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1104 parameter. */
1105 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1106 /* The optimized lookup depends on the fact that the
1107 template arguments for the member function template apply
1108 purely to the containing class, which is not true if the
1109 containing class is an explicit or partial
1110 specialization. */
1111 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1112 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1113 && !DECL_CONV_FN_P (tmpl)
1114 /* It is possible to have a template that is not a member
1115 template and is not a member of a template class:
1116
1117 template <typename T>
1118 struct S { friend A::f(); };
1119
1120 Here, the friend function is a template, but the context does
1121 not have template information. The optimized lookup relies
1122 on having ARGS be the template arguments for both the class
1123 and the function template. */
1124 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1125 }
1126
1127 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1128 gone through coerce_template_parms by now. */
1129
1130 static void
1131 verify_unstripped_args (tree args)
1132 {
1133 ++processing_template_decl;
1134 if (!any_dependent_template_arguments_p (args))
1135 {
1136 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1137 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1138 {
1139 tree arg = TREE_VEC_ELT (inner, i);
1140 if (TREE_CODE (arg) == TEMPLATE_DECL)
1141 /* OK */;
1142 else if (TYPE_P (arg))
1143 gcc_assert (strip_typedefs (arg, NULL) == arg);
1144 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1145 /* Allow typedefs on the type of a non-type argument, since a
1146 parameter can have them. */;
1147 else
1148 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1149 }
1150 }
1151 --processing_template_decl;
1152 }
1153
1154 /* Retrieve the specialization (in the sense of [temp.spec] - a
1155 specialization is either an instantiation or an explicit
1156 specialization) of TMPL for the given template ARGS. If there is
1157 no such specialization, return NULL_TREE. The ARGS are a vector of
1158 arguments, or a vector of vectors of arguments, in the case of
1159 templates with more than one level of parameters.
1160
1161 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1162 then we search for a partial specialization matching ARGS. This
1163 parameter is ignored if TMPL is not a class template.
1164
1165 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1166 result is a NONTYPE_ARGUMENT_PACK. */
1167
1168 static tree
1169 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1170 {
1171 if (tmpl == NULL_TREE)
1172 return NULL_TREE;
1173
1174 if (args == error_mark_node)
1175 return NULL_TREE;
1176
1177 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1178 || TREE_CODE (tmpl) == FIELD_DECL);
1179
1180 /* There should be as many levels of arguments as there are
1181 levels of parameters. */
1182 gcc_assert (TMPL_ARGS_DEPTH (args)
1183 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1184 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1185 : template_class_depth (DECL_CONTEXT (tmpl))));
1186
1187 if (flag_checking)
1188 verify_unstripped_args (args);
1189
1190 if (optimize_specialization_lookup_p (tmpl))
1191 {
1192 tree class_template;
1193 tree class_specialization;
1194 vec<tree, va_gc> *methods;
1195 tree fns;
1196 int idx;
1197
1198 /* The template arguments actually apply to the containing
1199 class. Find the class specialization with those
1200 arguments. */
1201 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1202 class_specialization
1203 = retrieve_specialization (class_template, args, 0);
1204 if (!class_specialization)
1205 return NULL_TREE;
1206 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1207 for the specialization. */
1208 idx = class_method_index_for_fn (class_specialization, tmpl);
1209 if (idx == -1)
1210 return NULL_TREE;
1211 /* Iterate through the methods with the indicated name, looking
1212 for the one that has an instance of TMPL. */
1213 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1214 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1215 {
1216 tree fn = OVL_CURRENT (fns);
1217 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn) == class_specialization)
1221 return fn;
1222 }
1223 return NULL_TREE;
1224 }
1225 else
1226 {
1227 spec_entry *found;
1228 spec_entry elt;
1229 hash_table<spec_hasher> *specializations;
1230
1231 elt.tmpl = tmpl;
1232 elt.args = args;
1233 elt.spec = NULL_TREE;
1234
1235 if (DECL_CLASS_TEMPLATE_P (tmpl))
1236 specializations = type_specializations;
1237 else
1238 specializations = decl_specializations;
1239
1240 if (hash == 0)
1241 hash = spec_hasher::hash (&elt);
1242 found = specializations->find_with_hash (&elt, hash);
1243 if (found)
1244 return found->spec;
1245 }
1246
1247 return NULL_TREE;
1248 }
1249
1250 /* Like retrieve_specialization, but for local declarations. */
1251
1252 tree
1253 retrieve_local_specialization (tree tmpl)
1254 {
1255 if (local_specializations == NULL)
1256 return NULL_TREE;
1257
1258 tree *slot = local_specializations->get (tmpl);
1259 return slot ? *slot : NULL_TREE;
1260 }
1261
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1263
1264 int
1265 is_specialization_of (tree decl, tree tmpl)
1266 {
1267 tree t;
1268
1269 if (TREE_CODE (decl) == FUNCTION_DECL)
1270 {
1271 for (t = decl;
1272 t != NULL_TREE;
1273 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1274 if (t == tmpl)
1275 return 1;
1276 }
1277 else
1278 {
1279 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1280
1281 for (t = TREE_TYPE (decl);
1282 t != NULL_TREE;
1283 t = CLASSTYPE_USE_TEMPLATE (t)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1285 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1286 return 1;
1287 }
1288
1289 return 0;
1290 }
1291
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1294
1295 bool
1296 is_specialization_of_friend (tree decl, tree friend_decl)
1297 {
1298 bool need_template = true;
1299 int template_depth;
1300
1301 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1302 || TREE_CODE (decl) == TYPE_DECL);
1303
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1306 if this. */
1307 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl)
1309 && !DECL_USE_TEMPLATE (friend_decl))
1310 {
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1313 need_template = false;
1314 }
1315 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl))
1317 need_template = false;
1318
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1321 return false;
1322
1323 if (is_specialization_of (decl, friend_decl))
1324 return true;
1325
1326 /* [temp.friend/6]
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1331
1332 For example, given a template friend declaration
1333
1334 template <class T> friend void A<T>::f();
1335
1336 the member function below is considered a friend
1337
1338 template <> struct A<int> {
1339 void f();
1340 };
1341
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1345
1346 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1347 if (template_depth
1348 && DECL_CLASS_SCOPE_P (decl)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1351 {
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1354
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1357
1358 and DECL's are
1359
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1362
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1367
1368 tree context = DECL_CONTEXT (decl);
1369 tree args = NULL_TREE;
1370 int current_depth = 0;
1371
1372 while (current_depth < template_depth)
1373 {
1374 if (CLASSTYPE_TEMPLATE_INFO (context))
1375 {
1376 if (current_depth == 0)
1377 args = TYPE_TI_ARGS (context);
1378 else
1379 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1380 current_depth++;
1381 }
1382 context = TYPE_CONTEXT (context);
1383 }
1384
1385 if (TREE_CODE (decl) == FUNCTION_DECL)
1386 {
1387 bool is_template;
1388 tree friend_type;
1389 tree decl_type;
1390 tree friend_args_type;
1391 tree decl_args_type;
1392
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 non-templates. */
1395 is_template = DECL_TEMPLATE_INFO (decl)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1397 if (need_template ^ is_template)
1398 return false;
1399 else if (is_template)
1400 {
1401 /* If both are templates, check template parameter list. */
1402 tree friend_parms
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1404 args, tf_none);
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1407 friend_parms))
1408 return false;
1409
1410 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1411 }
1412 else
1413 decl_type = TREE_TYPE (decl);
1414
1415 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1416 tf_none, NULL_TREE);
1417 if (friend_type == error_mark_node)
1418 return false;
1419
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1422 return false;
1423
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type = TYPE_ARG_TYPES (friend_type);
1427 decl_args_type = TYPE_ARG_TYPES (decl_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1429 friend_args_type = TREE_CHAIN (friend_args_type);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1431 decl_args_type = TREE_CHAIN (decl_args_type);
1432
1433 return compparms (decl_args_type, friend_args_type);
1434 }
1435 else
1436 {
1437 /* DECL is a TYPE_DECL */
1438 bool is_template;
1439 tree decl_type = TREE_TYPE (decl);
1440
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 non-templates. */
1443 is_template
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1446
1447 if (need_template ^ is_template)
1448 return false;
1449 else if (is_template)
1450 {
1451 tree friend_parms;
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1455 != DECL_NAME (friend_decl))
1456 return false;
1457
1458 /* Now check template parameter list. */
1459 friend_parms
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1461 args, tf_none);
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1464 friend_parms);
1465 }
1466 else
1467 return (DECL_NAME (decl)
1468 == DECL_NAME (friend_decl));
1469 }
1470 }
1471 return false;
1472 }
1473
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1478
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1481
1482 static tree
1483 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1484 hashval_t hash)
1485 {
1486 tree fn;
1487 spec_entry **slot = NULL;
1488 spec_entry elt;
1489
1490 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1491 || (TREE_CODE (tmpl) == FIELD_DECL
1492 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1493
1494 if (TREE_CODE (spec) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1502
1503 template <class T> void f(T t1, T t = T())
1504
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1507 return spec;
1508
1509 if (optimize_specialization_lookup_p (tmpl))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn = retrieve_specialization (tmpl, args, 0);
1513 else
1514 {
1515 elt.tmpl = tmpl;
1516 elt.args = args;
1517 elt.spec = spec;
1518
1519 if (hash == 0)
1520 hash = spec_hasher::hash (&elt);
1521
1522 slot =
1523 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1524 if (*slot)
1525 fn = ((spec_entry *) *slot)->spec;
1526 else
1527 fn = NULL_TREE;
1528 }
1529
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1535 if (fn == spec)
1536 return spec;
1537 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1538 {
1539 if (DECL_TEMPLATE_INSTANTIATION (fn))
1540 {
1541 if (DECL_ODR_USED (fn)
1542 || DECL_EXPLICIT_INSTANTIATION (fn))
1543 {
1544 error ("specialization of %qD after instantiation",
1545 fn);
1546 return error_mark_node;
1547 }
1548 else
1549 {
1550 tree clone;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1557 it later.
1558
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1561 specialization:
1562
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1566
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1569 declaration.
1570
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn) = NULL_TREE;
1575 duplicate_decls (spec, fn, is_friend);
1576 /* The call to duplicate_decls will have applied
1577 [temp.expl.spec]:
1578
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1582 is.
1583
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone, fn)
1587 {
1588 DECL_DECLARED_INLINE_P (clone)
1589 = DECL_DECLARED_INLINE_P (fn);
1590 DECL_SOURCE_LOCATION (clone)
1591 = DECL_SOURCE_LOCATION (fn);
1592 DECL_DELETED_FN (clone)
1593 = DECL_DELETED_FN (fn);
1594 }
1595 check_specialization_namespace (tmpl);
1596
1597 return fn;
1598 }
1599 }
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1601 {
1602 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1603 /* Dup decl failed, but this is a new definition. Set the
1604 line number so any errors match this new
1605 definition. */
1606 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1607
1608 return fn;
1609 }
1610 }
1611 else if (fn)
1612 return duplicate_decls (spec, fn, is_friend);
1613
1614 /* A specialization must be declared in the same namespace as the
1615 template it is specializing. */
1616 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1617 && !check_specialization_namespace (tmpl))
1618 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1619
1620 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1621 {
1622 spec_entry *entry = ggc_alloc<spec_entry> ();
1623 gcc_assert (tmpl && args && spec);
1624 *entry = elt;
1625 *slot = entry;
1626 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1627 && PRIMARY_TEMPLATE_P (tmpl)
1628 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1629 || variable_template_p (tmpl))
1630 /* If TMPL is a forward declaration of a template function, keep a list
1631 of all specializations in case we need to reassign them to a friend
1632 template later in tsubst_friend_function.
1633
1634 Also keep a list of all variable template instantiations so that
1635 process_partial_specialization can check whether a later partial
1636 specialization would have used it. */
1637 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1638 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1639 }
1640
1641 return spec;
1642 }
1643
1644 /* Returns true iff two spec_entry nodes are equivalent. */
1645
1646 int comparing_specializations;
1647
1648 bool
1649 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1650 {
1651 int equal;
1652
1653 ++comparing_specializations;
1654 equal = (e1->tmpl == e2->tmpl
1655 && comp_template_args (e1->args, e2->args));
1656 if (equal && flag_concepts
1657 /* tmpl could be a FIELD_DECL for a capture pack. */
1658 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1659 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1660 && uses_template_parms (e1->args))
1661 {
1662 /* Partial specializations of a variable template can be distinguished by
1663 constraints. */
1664 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1665 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1666 equal = equivalent_constraints (c1, c2);
1667 }
1668 --comparing_specializations;
1669
1670 return equal;
1671 }
1672
1673 /* Returns a hash for a template TMPL and template arguments ARGS. */
1674
1675 static hashval_t
1676 hash_tmpl_and_args (tree tmpl, tree args)
1677 {
1678 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1679 return iterative_hash_template_arg (args, val);
1680 }
1681
1682 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1683 ignoring SPEC. */
1684
1685 hashval_t
1686 spec_hasher::hash (spec_entry *e)
1687 {
1688 return hash_tmpl_and_args (e->tmpl, e->args);
1689 }
1690
1691 /* Recursively calculate a hash value for a template argument ARG, for use
1692 in the hash tables of template specializations. */
1693
1694 hashval_t
1695 iterative_hash_template_arg (tree arg, hashval_t val)
1696 {
1697 unsigned HOST_WIDE_INT i;
1698 enum tree_code code;
1699 char tclass;
1700
1701 if (arg == NULL_TREE)
1702 return iterative_hash_object (arg, val);
1703
1704 if (!TYPE_P (arg))
1705 STRIP_NOPS (arg);
1706
1707 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1708 gcc_unreachable ();
1709
1710 code = TREE_CODE (arg);
1711 tclass = TREE_CODE_CLASS (code);
1712
1713 val = iterative_hash_object (code, val);
1714
1715 switch (code)
1716 {
1717 case ERROR_MARK:
1718 return val;
1719
1720 case IDENTIFIER_NODE:
1721 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1722
1723 case TREE_VEC:
1724 {
1725 int i, len = TREE_VEC_LENGTH (arg);
1726 for (i = 0; i < len; ++i)
1727 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1728 return val;
1729 }
1730
1731 case TYPE_PACK_EXPANSION:
1732 case EXPR_PACK_EXPANSION:
1733 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1734 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1735
1736 case TYPE_ARGUMENT_PACK:
1737 case NONTYPE_ARGUMENT_PACK:
1738 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1739
1740 case TREE_LIST:
1741 for (; arg; arg = TREE_CHAIN (arg))
1742 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1743 return val;
1744
1745 case OVERLOAD:
1746 for (; arg; arg = OVL_NEXT (arg))
1747 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1748 return val;
1749
1750 case CONSTRUCTOR:
1751 {
1752 tree field, value;
1753 iterative_hash_template_arg (TREE_TYPE (arg), val);
1754 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1755 {
1756 val = iterative_hash_template_arg (field, val);
1757 val = iterative_hash_template_arg (value, val);
1758 }
1759 return val;
1760 }
1761
1762 case PARM_DECL:
1763 if (!DECL_ARTIFICIAL (arg))
1764 {
1765 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1766 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1767 }
1768 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1769
1770 case TARGET_EXPR:
1771 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1772
1773 case PTRMEM_CST:
1774 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1775 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1776
1777 case TEMPLATE_PARM_INDEX:
1778 val = iterative_hash_template_arg
1779 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1780 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1781 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1782
1783 case TRAIT_EXPR:
1784 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1785 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1786 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1787
1788 case BASELINK:
1789 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1790 val);
1791 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1792 val);
1793
1794 case MODOP_EXPR:
1795 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1796 code = TREE_CODE (TREE_OPERAND (arg, 1));
1797 val = iterative_hash_object (code, val);
1798 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1799
1800 case LAMBDA_EXPR:
1801 /* A lambda can't appear in a template arg, but don't crash on
1802 erroneous input. */
1803 gcc_assert (seen_error ());
1804 return val;
1805
1806 case CAST_EXPR:
1807 case IMPLICIT_CONV_EXPR:
1808 case STATIC_CAST_EXPR:
1809 case REINTERPRET_CAST_EXPR:
1810 case CONST_CAST_EXPR:
1811 case DYNAMIC_CAST_EXPR:
1812 case NEW_EXPR:
1813 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1814 /* Now hash operands as usual. */
1815 break;
1816
1817 default:
1818 break;
1819 }
1820
1821 switch (tclass)
1822 {
1823 case tcc_type:
1824 if (alias_template_specialization_p (arg))
1825 {
1826 // We want an alias specialization that survived strip_typedefs
1827 // to hash differently from its TYPE_CANONICAL, to avoid hash
1828 // collisions that compare as different in template_args_equal.
1829 // These could be dependent specializations that strip_typedefs
1830 // left alone, or untouched specializations because
1831 // coerce_template_parms returns the unconverted template
1832 // arguments if it sees incomplete argument packs.
1833 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1834 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1835 }
1836 if (TYPE_CANONICAL (arg))
1837 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1838 val);
1839 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1840 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1841 /* Otherwise just compare the types during lookup. */
1842 return val;
1843
1844 case tcc_declaration:
1845 case tcc_constant:
1846 return iterative_hash_expr (arg, val);
1847
1848 default:
1849 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1850 {
1851 unsigned n = cp_tree_operand_length (arg);
1852 for (i = 0; i < n; ++i)
1853 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1854 return val;
1855 }
1856 }
1857 gcc_unreachable ();
1858 return 0;
1859 }
1860
1861 /* Unregister the specialization SPEC as a specialization of TMPL.
1862 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1863 if the SPEC was listed as a specialization of TMPL.
1864
1865 Note that SPEC has been ggc_freed, so we can't look inside it. */
1866
1867 bool
1868 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1869 {
1870 spec_entry *entry;
1871 spec_entry elt;
1872
1873 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1874 elt.args = TI_ARGS (tinfo);
1875 elt.spec = NULL_TREE;
1876
1877 entry = decl_specializations->find (&elt);
1878 if (entry != NULL)
1879 {
1880 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1881 gcc_assert (new_spec != NULL_TREE);
1882 entry->spec = new_spec;
1883 return 1;
1884 }
1885
1886 return 0;
1887 }
1888
1889 /* Like register_specialization, but for local declarations. We are
1890 registering SPEC, an instantiation of TMPL. */
1891
1892 void
1893 register_local_specialization (tree spec, tree tmpl)
1894 {
1895 local_specializations->put (tmpl, spec);
1896 }
1897
1898 /* TYPE is a class type. Returns true if TYPE is an explicitly
1899 specialized class. */
1900
1901 bool
1902 explicit_class_specialization_p (tree type)
1903 {
1904 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1905 return false;
1906 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1907 }
1908
1909 /* Print the list of functions at FNS, going through all the overloads
1910 for each element of the list. Alternatively, FNS can not be a
1911 TREE_LIST, in which case it will be printed together with all the
1912 overloads.
1913
1914 MORE and *STR should respectively be FALSE and NULL when the function
1915 is called from the outside. They are used internally on recursive
1916 calls. print_candidates manages the two parameters and leaves NULL
1917 in *STR when it ends. */
1918
1919 static void
1920 print_candidates_1 (tree fns, bool more, const char **str)
1921 {
1922 tree fn, fn2;
1923 char *spaces = NULL;
1924
1925 for (fn = fns; fn; fn = OVL_NEXT (fn))
1926 if (TREE_CODE (fn) == TREE_LIST)
1927 {
1928 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1929 print_candidates_1 (TREE_VALUE (fn2),
1930 TREE_CHAIN (fn2) || more, str);
1931 }
1932 else
1933 {
1934 tree cand = OVL_CURRENT (fn);
1935 if (!*str)
1936 {
1937 /* Pick the prefix string. */
1938 if (!more && !OVL_NEXT (fns))
1939 {
1940 inform (DECL_SOURCE_LOCATION (cand),
1941 "candidate is: %#D", cand);
1942 continue;
1943 }
1944
1945 *str = _("candidates are:");
1946 spaces = get_spaces (*str);
1947 }
1948 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1949 *str = spaces ? spaces : *str;
1950 }
1951
1952 if (!more)
1953 {
1954 free (spaces);
1955 *str = NULL;
1956 }
1957 }
1958
1959 /* Print the list of candidate FNS in an error message. FNS can also
1960 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1961
1962 void
1963 print_candidates (tree fns)
1964 {
1965 const char *str = NULL;
1966 print_candidates_1 (fns, false, &str);
1967 gcc_assert (str == NULL);
1968 }
1969
1970 /* Get a (possibly) constrained template declaration for the
1971 purpose of ordering candidates. */
1972 static tree
1973 get_template_for_ordering (tree list)
1974 {
1975 gcc_assert (TREE_CODE (list) == TREE_LIST);
1976 tree f = TREE_VALUE (list);
1977 if (tree ti = DECL_TEMPLATE_INFO (f))
1978 return TI_TEMPLATE (ti);
1979 return f;
1980 }
1981
1982 /* Among candidates having the same signature, return the
1983 most constrained or NULL_TREE if there is no best candidate.
1984 If the signatures of candidates vary (e.g., template
1985 specialization vs. member function), then there can be no
1986 most constrained.
1987
1988 Note that we don't compare constraints on the functions
1989 themselves, but rather those of their templates. */
1990 static tree
1991 most_constrained_function (tree candidates)
1992 {
1993 // Try to find the best candidate in a first pass.
1994 tree champ = candidates;
1995 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1996 {
1997 int winner = more_constrained (get_template_for_ordering (champ),
1998 get_template_for_ordering (c));
1999 if (winner == -1)
2000 champ = c; // The candidate is more constrained
2001 else if (winner == 0)
2002 return NULL_TREE; // Neither is more constrained
2003 }
2004
2005 // Verify that the champ is better than previous candidates.
2006 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2007 if (!more_constrained (get_template_for_ordering (champ),
2008 get_template_for_ordering (c)))
2009 return NULL_TREE;
2010 }
2011
2012 return champ;
2013 }
2014
2015
2016 /* Returns the template (one of the functions given by TEMPLATE_ID)
2017 which can be specialized to match the indicated DECL with the
2018 explicit template args given in TEMPLATE_ID. The DECL may be
2019 NULL_TREE if none is available. In that case, the functions in
2020 TEMPLATE_ID are non-members.
2021
2022 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2023 specialization of a member template.
2024
2025 The TEMPLATE_COUNT is the number of references to qualifying
2026 template classes that appeared in the name of the function. See
2027 check_explicit_specialization for a more accurate description.
2028
2029 TSK indicates what kind of template declaration (if any) is being
2030 declared. TSK_TEMPLATE indicates that the declaration given by
2031 DECL, though a FUNCTION_DECL, has template parameters, and is
2032 therefore a template function.
2033
2034 The template args (those explicitly specified and those deduced)
2035 are output in a newly created vector *TARGS_OUT.
2036
2037 If it is impossible to determine the result, an error message is
2038 issued. The error_mark_node is returned to indicate failure. */
2039
2040 static tree
2041 determine_specialization (tree template_id,
2042 tree decl,
2043 tree* targs_out,
2044 int need_member_template,
2045 int template_count,
2046 tmpl_spec_kind tsk)
2047 {
2048 tree fns;
2049 tree targs;
2050 tree explicit_targs;
2051 tree candidates = NULL_TREE;
2052
2053 /* A TREE_LIST of templates of which DECL may be a specialization.
2054 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2055 corresponding TREE_PURPOSE is the set of template arguments that,
2056 when used to instantiate the template, would produce a function
2057 with the signature of DECL. */
2058 tree templates = NULL_TREE;
2059 int header_count;
2060 cp_binding_level *b;
2061
2062 *targs_out = NULL_TREE;
2063
2064 if (template_id == error_mark_node || decl == error_mark_node)
2065 return error_mark_node;
2066
2067 /* We shouldn't be specializing a member template of an
2068 unspecialized class template; we already gave an error in
2069 check_specialization_scope, now avoid crashing. */
2070 if (template_count && DECL_CLASS_SCOPE_P (decl)
2071 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2072 {
2073 gcc_assert (errorcount);
2074 return error_mark_node;
2075 }
2076
2077 fns = TREE_OPERAND (template_id, 0);
2078 explicit_targs = TREE_OPERAND (template_id, 1);
2079
2080 if (fns == error_mark_node)
2081 return error_mark_node;
2082
2083 /* Check for baselinks. */
2084 if (BASELINK_P (fns))
2085 fns = BASELINK_FUNCTIONS (fns);
2086
2087 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2088 {
2089 error ("%qD is not a function template", fns);
2090 return error_mark_node;
2091 }
2092 else if (VAR_P (decl) && !variable_template_p (fns))
2093 {
2094 error ("%qD is not a variable template", fns);
2095 return error_mark_node;
2096 }
2097
2098 /* Count the number of template headers specified for this
2099 specialization. */
2100 header_count = 0;
2101 for (b = current_binding_level;
2102 b->kind == sk_template_parms;
2103 b = b->level_chain)
2104 ++header_count;
2105
2106 tree orig_fns = fns;
2107
2108 if (variable_template_p (fns))
2109 {
2110 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2111 targs = coerce_template_parms (parms, explicit_targs, fns,
2112 tf_warning_or_error,
2113 /*req_all*/true, /*use_defarg*/true);
2114 if (targs != error_mark_node)
2115 templates = tree_cons (targs, fns, templates);
2116 }
2117 else for (; fns; fns = OVL_NEXT (fns))
2118 {
2119 tree fn = OVL_CURRENT (fns);
2120
2121 if (TREE_CODE (fn) == TEMPLATE_DECL)
2122 {
2123 tree decl_arg_types;
2124 tree fn_arg_types;
2125 tree insttype;
2126
2127 /* In case of explicit specialization, we need to check if
2128 the number of template headers appearing in the specialization
2129 is correct. This is usually done in check_explicit_specialization,
2130 but the check done there cannot be exhaustive when specializing
2131 member functions. Consider the following code:
2132
2133 template <> void A<int>::f(int);
2134 template <> template <> void A<int>::f(int);
2135
2136 Assuming that A<int> is not itself an explicit specialization
2137 already, the first line specializes "f" which is a non-template
2138 member function, whilst the second line specializes "f" which
2139 is a template member function. So both lines are syntactically
2140 correct, and check_explicit_specialization does not reject
2141 them.
2142
2143 Here, we can do better, as we are matching the specialization
2144 against the declarations. We count the number of template
2145 headers, and we check if they match TEMPLATE_COUNT + 1
2146 (TEMPLATE_COUNT is the number of qualifying template classes,
2147 plus there must be another header for the member template
2148 itself).
2149
2150 Notice that if header_count is zero, this is not a
2151 specialization but rather a template instantiation, so there
2152 is no check we can perform here. */
2153 if (header_count && header_count != template_count + 1)
2154 continue;
2155
2156 /* Check that the number of template arguments at the
2157 innermost level for DECL is the same as for FN. */
2158 if (current_binding_level->kind == sk_template_parms
2159 && !current_binding_level->explicit_spec_p
2160 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2161 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2162 (current_template_parms))))
2163 continue;
2164
2165 /* DECL might be a specialization of FN. */
2166 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2167 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2168
2169 /* For a non-static member function, we need to make sure
2170 that the const qualification is the same. Since
2171 get_bindings does not try to merge the "this" parameter,
2172 we must do the comparison explicitly. */
2173 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2174 && !same_type_p (TREE_VALUE (fn_arg_types),
2175 TREE_VALUE (decl_arg_types)))
2176 continue;
2177
2178 /* Skip the "this" parameter and, for constructors of
2179 classes with virtual bases, the VTT parameter. A
2180 full specialization of a constructor will have a VTT
2181 parameter, but a template never will. */
2182 decl_arg_types
2183 = skip_artificial_parms_for (decl, decl_arg_types);
2184 fn_arg_types
2185 = skip_artificial_parms_for (fn, fn_arg_types);
2186
2187 /* Function templates cannot be specializations; there are
2188 no partial specializations of functions. Therefore, if
2189 the type of DECL does not match FN, there is no
2190 match.
2191
2192 Note that it should never be the case that we have both
2193 candidates added here, and for regular member functions
2194 below. */
2195 if (tsk == tsk_template)
2196 {
2197 if (compparms (fn_arg_types, decl_arg_types))
2198 candidates = tree_cons (NULL_TREE, fn, candidates);
2199 continue;
2200 }
2201
2202 /* See whether this function might be a specialization of this
2203 template. Suppress access control because we might be trying
2204 to make this specialization a friend, and we have already done
2205 access control for the declaration of the specialization. */
2206 push_deferring_access_checks (dk_no_check);
2207 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2208 pop_deferring_access_checks ();
2209
2210 if (!targs)
2211 /* We cannot deduce template arguments that when used to
2212 specialize TMPL will produce DECL. */
2213 continue;
2214
2215 /* Remove, from the set of candidates, all those functions
2216 whose constraints are not satisfied. */
2217 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2218 continue;
2219
2220 // Then, try to form the new function type.
2221 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2222 if (insttype == error_mark_node)
2223 continue;
2224 fn_arg_types
2225 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2226 if (!compparms (fn_arg_types, decl_arg_types))
2227 continue;
2228
2229 /* Save this template, and the arguments deduced. */
2230 templates = tree_cons (targs, fn, templates);
2231 }
2232 else if (need_member_template)
2233 /* FN is an ordinary member function, and we need a
2234 specialization of a member template. */
2235 ;
2236 else if (TREE_CODE (fn) != FUNCTION_DECL)
2237 /* We can get IDENTIFIER_NODEs here in certain erroneous
2238 cases. */
2239 ;
2240 else if (!DECL_FUNCTION_MEMBER_P (fn))
2241 /* This is just an ordinary non-member function. Nothing can
2242 be a specialization of that. */
2243 ;
2244 else if (DECL_ARTIFICIAL (fn))
2245 /* Cannot specialize functions that are created implicitly. */
2246 ;
2247 else
2248 {
2249 tree decl_arg_types;
2250
2251 /* This is an ordinary member function. However, since
2252 we're here, we can assume its enclosing class is a
2253 template class. For example,
2254
2255 template <typename T> struct S { void f(); };
2256 template <> void S<int>::f() {}
2257
2258 Here, S<int>::f is a non-template, but S<int> is a
2259 template class. If FN has the same type as DECL, we
2260 might be in business. */
2261
2262 if (!DECL_TEMPLATE_INFO (fn))
2263 /* Its enclosing class is an explicit specialization
2264 of a template class. This is not a candidate. */
2265 continue;
2266
2267 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2268 TREE_TYPE (TREE_TYPE (fn))))
2269 /* The return types differ. */
2270 continue;
2271
2272 /* Adjust the type of DECL in case FN is a static member. */
2273 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2274 if (DECL_STATIC_FUNCTION_P (fn)
2275 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2276 decl_arg_types = TREE_CHAIN (decl_arg_types);
2277
2278 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2279 decl_arg_types))
2280 continue;
2281
2282 // If the deduced arguments do not satisfy the constraints,
2283 // this is not a candidate.
2284 if (flag_concepts && !constraints_satisfied_p (fn))
2285 continue;
2286
2287 // Add the candidate.
2288 candidates = tree_cons (NULL_TREE, fn, candidates);
2289 }
2290 }
2291
2292 if (templates && TREE_CHAIN (templates))
2293 {
2294 /* We have:
2295
2296 [temp.expl.spec]
2297
2298 It is possible for a specialization with a given function
2299 signature to be instantiated from more than one function
2300 template. In such cases, explicit specification of the
2301 template arguments must be used to uniquely identify the
2302 function template specialization being specialized.
2303
2304 Note that here, there's no suggestion that we're supposed to
2305 determine which of the candidate templates is most
2306 specialized. However, we, also have:
2307
2308 [temp.func.order]
2309
2310 Partial ordering of overloaded function template
2311 declarations is used in the following contexts to select
2312 the function template to which a function template
2313 specialization refers:
2314
2315 -- when an explicit specialization refers to a function
2316 template.
2317
2318 So, we do use the partial ordering rules, at least for now.
2319 This extension can only serve to make invalid programs valid,
2320 so it's safe. And, there is strong anecdotal evidence that
2321 the committee intended the partial ordering rules to apply;
2322 the EDG front end has that behavior, and John Spicer claims
2323 that the committee simply forgot to delete the wording in
2324 [temp.expl.spec]. */
2325 tree tmpl = most_specialized_instantiation (templates);
2326 if (tmpl != error_mark_node)
2327 {
2328 templates = tmpl;
2329 TREE_CHAIN (templates) = NULL_TREE;
2330 }
2331 }
2332
2333 // Concepts allows multiple declarations of member functions
2334 // with the same signature. Like above, we need to rely on
2335 // on the partial ordering of those candidates to determine which
2336 // is the best.
2337 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2338 {
2339 if (tree cand = most_constrained_function (candidates))
2340 {
2341 candidates = cand;
2342 TREE_CHAIN (cand) = NULL_TREE;
2343 }
2344 }
2345
2346 if (templates == NULL_TREE && candidates == NULL_TREE)
2347 {
2348 error ("template-id %qD for %q+D does not match any template "
2349 "declaration", template_id, decl);
2350 if (header_count && header_count != template_count + 1)
2351 inform (input_location, "saw %d %<template<>%>, need %d for "
2352 "specializing a member function template",
2353 header_count, template_count + 1);
2354 else
2355 print_candidates (orig_fns);
2356 return error_mark_node;
2357 }
2358 else if ((templates && TREE_CHAIN (templates))
2359 || (candidates && TREE_CHAIN (candidates))
2360 || (templates && candidates))
2361 {
2362 error ("ambiguous template specialization %qD for %q+D",
2363 template_id, decl);
2364 candidates = chainon (candidates, templates);
2365 print_candidates (candidates);
2366 return error_mark_node;
2367 }
2368
2369 /* We have one, and exactly one, match. */
2370 if (candidates)
2371 {
2372 tree fn = TREE_VALUE (candidates);
2373 *targs_out = copy_node (DECL_TI_ARGS (fn));
2374
2375 // Propagate the candidate's constraints to the declaration.
2376 set_constraints (decl, get_constraints (fn));
2377
2378 /* DECL is a re-declaration or partial instantiation of a template
2379 function. */
2380 if (TREE_CODE (fn) == TEMPLATE_DECL)
2381 return fn;
2382 /* It was a specialization of an ordinary member function in a
2383 template class. */
2384 return DECL_TI_TEMPLATE (fn);
2385 }
2386
2387 /* It was a specialization of a template. */
2388 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2389 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2390 {
2391 *targs_out = copy_node (targs);
2392 SET_TMPL_ARGS_LEVEL (*targs_out,
2393 TMPL_ARGS_DEPTH (*targs_out),
2394 TREE_PURPOSE (templates));
2395 }
2396 else
2397 *targs_out = TREE_PURPOSE (templates);
2398 return TREE_VALUE (templates);
2399 }
2400
2401 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2402 but with the default argument values filled in from those in the
2403 TMPL_TYPES. */
2404
2405 static tree
2406 copy_default_args_to_explicit_spec_1 (tree spec_types,
2407 tree tmpl_types)
2408 {
2409 tree new_spec_types;
2410
2411 if (!spec_types)
2412 return NULL_TREE;
2413
2414 if (spec_types == void_list_node)
2415 return void_list_node;
2416
2417 /* Substitute into the rest of the list. */
2418 new_spec_types =
2419 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2420 TREE_CHAIN (tmpl_types));
2421
2422 /* Add the default argument for this parameter. */
2423 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2424 TREE_VALUE (spec_types),
2425 new_spec_types);
2426 }
2427
2428 /* DECL is an explicit specialization. Replicate default arguments
2429 from the template it specializes. (That way, code like:
2430
2431 template <class T> void f(T = 3);
2432 template <> void f(double);
2433 void g () { f (); }
2434
2435 works, as required.) An alternative approach would be to look up
2436 the correct default arguments at the call-site, but this approach
2437 is consistent with how implicit instantiations are handled. */
2438
2439 static void
2440 copy_default_args_to_explicit_spec (tree decl)
2441 {
2442 tree tmpl;
2443 tree spec_types;
2444 tree tmpl_types;
2445 tree new_spec_types;
2446 tree old_type;
2447 tree new_type;
2448 tree t;
2449 tree object_type = NULL_TREE;
2450 tree in_charge = NULL_TREE;
2451 tree vtt = NULL_TREE;
2452
2453 /* See if there's anything we need to do. */
2454 tmpl = DECL_TI_TEMPLATE (decl);
2455 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2456 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2457 if (TREE_PURPOSE (t))
2458 break;
2459 if (!t)
2460 return;
2461
2462 old_type = TREE_TYPE (decl);
2463 spec_types = TYPE_ARG_TYPES (old_type);
2464
2465 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2466 {
2467 /* Remove the this pointer, but remember the object's type for
2468 CV quals. */
2469 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2470 spec_types = TREE_CHAIN (spec_types);
2471 tmpl_types = TREE_CHAIN (tmpl_types);
2472
2473 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2474 {
2475 /* DECL may contain more parameters than TMPL due to the extra
2476 in-charge parameter in constructors and destructors. */
2477 in_charge = spec_types;
2478 spec_types = TREE_CHAIN (spec_types);
2479 }
2480 if (DECL_HAS_VTT_PARM_P (decl))
2481 {
2482 vtt = spec_types;
2483 spec_types = TREE_CHAIN (spec_types);
2484 }
2485 }
2486
2487 /* Compute the merged default arguments. */
2488 new_spec_types =
2489 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2490
2491 /* Compute the new FUNCTION_TYPE. */
2492 if (object_type)
2493 {
2494 if (vtt)
2495 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2496 TREE_VALUE (vtt),
2497 new_spec_types);
2498
2499 if (in_charge)
2500 /* Put the in-charge parameter back. */
2501 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2502 TREE_VALUE (in_charge),
2503 new_spec_types);
2504
2505 new_type = build_method_type_directly (object_type,
2506 TREE_TYPE (old_type),
2507 new_spec_types);
2508 }
2509 else
2510 new_type = build_function_type (TREE_TYPE (old_type),
2511 new_spec_types);
2512 new_type = cp_build_type_attribute_variant (new_type,
2513 TYPE_ATTRIBUTES (old_type));
2514 new_type = build_exception_variant (new_type,
2515 TYPE_RAISES_EXCEPTIONS (old_type));
2516
2517 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2518 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2519
2520 TREE_TYPE (decl) = new_type;
2521 }
2522
2523 /* Return the number of template headers we expect to see for a definition
2524 or specialization of CTYPE or one of its non-template members. */
2525
2526 int
2527 num_template_headers_for_class (tree ctype)
2528 {
2529 int num_templates = 0;
2530
2531 while (ctype && CLASS_TYPE_P (ctype))
2532 {
2533 /* You're supposed to have one `template <...>' for every
2534 template class, but you don't need one for a full
2535 specialization. For example:
2536
2537 template <class T> struct S{};
2538 template <> struct S<int> { void f(); };
2539 void S<int>::f () {}
2540
2541 is correct; there shouldn't be a `template <>' for the
2542 definition of `S<int>::f'. */
2543 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2544 /* If CTYPE does not have template information of any
2545 kind, then it is not a template, nor is it nested
2546 within a template. */
2547 break;
2548 if (explicit_class_specialization_p (ctype))
2549 break;
2550 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2551 ++num_templates;
2552
2553 ctype = TYPE_CONTEXT (ctype);
2554 }
2555
2556 return num_templates;
2557 }
2558
2559 /* Do a simple sanity check on the template headers that precede the
2560 variable declaration DECL. */
2561
2562 void
2563 check_template_variable (tree decl)
2564 {
2565 tree ctx = CP_DECL_CONTEXT (decl);
2566 int wanted = num_template_headers_for_class (ctx);
2567 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2568 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2569 {
2570 if (cxx_dialect < cxx14)
2571 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2572 "variable templates only available with "
2573 "-std=c++14 or -std=gnu++14");
2574
2575 // Namespace-scope variable templates should have a template header.
2576 ++wanted;
2577 }
2578 if (template_header_count > wanted)
2579 {
2580 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2581 "too many template headers for %D (should be %d)",
2582 decl, wanted);
2583 if (warned && CLASS_TYPE_P (ctx)
2584 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2585 inform (DECL_SOURCE_LOCATION (decl),
2586 "members of an explicitly specialized class are defined "
2587 "without a template header");
2588 }
2589 }
2590
2591 /* An explicit specialization whose declarator-id or class-head-name is not
2592 qualified shall be declared in the nearest enclosing namespace of the
2593 template, or, if the namespace is inline (7.3.1), any namespace from its
2594 enclosing namespace set.
2595
2596 If the name declared in the explicit instantiation is an unqualified name,
2597 the explicit instantiation shall appear in the namespace where its template
2598 is declared or, if that namespace is inline (7.3.1), any namespace from its
2599 enclosing namespace set. */
2600
2601 void
2602 check_unqualified_spec_or_inst (tree t, location_t loc)
2603 {
2604 tree tmpl = most_general_template (t);
2605 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2606 && !is_associated_namespace (current_namespace,
2607 CP_DECL_CONTEXT (tmpl)))
2608 {
2609 if (processing_specialization)
2610 permerror (loc, "explicit specialization of %qD outside its "
2611 "namespace must use a nested-name-specifier", tmpl);
2612 else if (processing_explicit_instantiation
2613 && cxx_dialect >= cxx11)
2614 /* This was allowed in C++98, so only pedwarn. */
2615 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2616 "outside its namespace must use a nested-name-"
2617 "specifier", tmpl);
2618 }
2619 }
2620
2621 /* Check to see if the function just declared, as indicated in
2622 DECLARATOR, and in DECL, is a specialization of a function
2623 template. We may also discover that the declaration is an explicit
2624 instantiation at this point.
2625
2626 Returns DECL, or an equivalent declaration that should be used
2627 instead if all goes well. Issues an error message if something is
2628 amiss. Returns error_mark_node if the error is not easily
2629 recoverable.
2630
2631 FLAGS is a bitmask consisting of the following flags:
2632
2633 2: The function has a definition.
2634 4: The function is a friend.
2635
2636 The TEMPLATE_COUNT is the number of references to qualifying
2637 template classes that appeared in the name of the function. For
2638 example, in
2639
2640 template <class T> struct S { void f(); };
2641 void S<int>::f();
2642
2643 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2644 classes are not counted in the TEMPLATE_COUNT, so that in
2645
2646 template <class T> struct S {};
2647 template <> struct S<int> { void f(); }
2648 template <> void S<int>::f();
2649
2650 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2651 invalid; there should be no template <>.)
2652
2653 If the function is a specialization, it is marked as such via
2654 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2655 is set up correctly, and it is added to the list of specializations
2656 for that template. */
2657
2658 tree
2659 check_explicit_specialization (tree declarator,
2660 tree decl,
2661 int template_count,
2662 int flags)
2663 {
2664 int have_def = flags & 2;
2665 int is_friend = flags & 4;
2666 bool is_concept = flags & 8;
2667 int specialization = 0;
2668 int explicit_instantiation = 0;
2669 int member_specialization = 0;
2670 tree ctype = DECL_CLASS_CONTEXT (decl);
2671 tree dname = DECL_NAME (decl);
2672 tmpl_spec_kind tsk;
2673
2674 if (is_friend)
2675 {
2676 if (!processing_specialization)
2677 tsk = tsk_none;
2678 else
2679 tsk = tsk_excessive_parms;
2680 }
2681 else
2682 tsk = current_tmpl_spec_kind (template_count);
2683
2684 switch (tsk)
2685 {
2686 case tsk_none:
2687 if (processing_specialization && !VAR_P (decl))
2688 {
2689 specialization = 1;
2690 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2691 }
2692 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2693 {
2694 if (is_friend)
2695 /* This could be something like:
2696
2697 template <class T> void f(T);
2698 class S { friend void f<>(int); } */
2699 specialization = 1;
2700 else
2701 {
2702 /* This case handles bogus declarations like template <>
2703 template <class T> void f<int>(); */
2704
2705 error ("template-id %qD in declaration of primary template",
2706 declarator);
2707 return decl;
2708 }
2709 }
2710 break;
2711
2712 case tsk_invalid_member_spec:
2713 /* The error has already been reported in
2714 check_specialization_scope. */
2715 return error_mark_node;
2716
2717 case tsk_invalid_expl_inst:
2718 error ("template parameter list used in explicit instantiation");
2719
2720 /* Fall through. */
2721
2722 case tsk_expl_inst:
2723 if (have_def)
2724 error ("definition provided for explicit instantiation");
2725
2726 explicit_instantiation = 1;
2727 break;
2728
2729 case tsk_excessive_parms:
2730 case tsk_insufficient_parms:
2731 if (tsk == tsk_excessive_parms)
2732 error ("too many template parameter lists in declaration of %qD",
2733 decl);
2734 else if (template_header_count)
2735 error("too few template parameter lists in declaration of %qD", decl);
2736 else
2737 error("explicit specialization of %qD must be introduced by "
2738 "%<template <>%>", decl);
2739
2740 /* Fall through. */
2741 case tsk_expl_spec:
2742 if (is_concept)
2743 error ("explicit specialization declared %<concept%>");
2744
2745 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2746 /* In cases like template<> constexpr bool v = true;
2747 We'll give an error in check_template_variable. */
2748 break;
2749
2750 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2751 if (ctype)
2752 member_specialization = 1;
2753 else
2754 specialization = 1;
2755 break;
2756
2757 case tsk_template:
2758 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2759 {
2760 /* This case handles bogus declarations like template <>
2761 template <class T> void f<int>(); */
2762
2763 if (!uses_template_parms (declarator))
2764 error ("template-id %qD in declaration of primary template",
2765 declarator);
2766 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2767 {
2768 /* Partial specialization of variable template. */
2769 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2770 specialization = 1;
2771 goto ok;
2772 }
2773 else if (cxx_dialect < cxx14)
2774 error ("non-type partial specialization %qD "
2775 "is not allowed", declarator);
2776 else
2777 error ("non-class, non-variable partial specialization %qD "
2778 "is not allowed", declarator);
2779 return decl;
2780 ok:;
2781 }
2782
2783 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2784 /* This is a specialization of a member template, without
2785 specialization the containing class. Something like:
2786
2787 template <class T> struct S {
2788 template <class U> void f (U);
2789 };
2790 template <> template <class U> void S<int>::f(U) {}
2791
2792 That's a specialization -- but of the entire template. */
2793 specialization = 1;
2794 break;
2795
2796 default:
2797 gcc_unreachable ();
2798 }
2799
2800 if ((specialization || member_specialization)
2801 /* This doesn't apply to variable templates. */
2802 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2803 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2804 {
2805 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2806 for (; t; t = TREE_CHAIN (t))
2807 if (TREE_PURPOSE (t))
2808 {
2809 permerror (input_location,
2810 "default argument specified in explicit specialization");
2811 break;
2812 }
2813 }
2814
2815 if (specialization || member_specialization || explicit_instantiation)
2816 {
2817 tree tmpl = NULL_TREE;
2818 tree targs = NULL_TREE;
2819 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2820
2821 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2822 if (!was_template_id)
2823 {
2824 tree fns;
2825
2826 gcc_assert (identifier_p (declarator));
2827 if (ctype)
2828 fns = dname;
2829 else
2830 {
2831 /* If there is no class context, the explicit instantiation
2832 must be at namespace scope. */
2833 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2834
2835 /* Find the namespace binding, using the declaration
2836 context. */
2837 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2838 false, true);
2839 if (fns == error_mark_node)
2840 /* If lookup fails, look for a friend declaration so we can
2841 give a better diagnostic. */
2842 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2843 /*type*/false, /*complain*/true,
2844 /*hidden*/true);
2845
2846 if (fns == error_mark_node || !is_overloaded_fn (fns))
2847 {
2848 error ("%qD is not a template function", dname);
2849 fns = error_mark_node;
2850 }
2851 }
2852
2853 declarator = lookup_template_function (fns, NULL_TREE);
2854 }
2855
2856 if (declarator == error_mark_node)
2857 return error_mark_node;
2858
2859 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2860 {
2861 if (!explicit_instantiation)
2862 /* A specialization in class scope. This is invalid,
2863 but the error will already have been flagged by
2864 check_specialization_scope. */
2865 return error_mark_node;
2866 else
2867 {
2868 /* It's not valid to write an explicit instantiation in
2869 class scope, e.g.:
2870
2871 class C { template void f(); }
2872
2873 This case is caught by the parser. However, on
2874 something like:
2875
2876 template class C { void f(); };
2877
2878 (which is invalid) we can get here. The error will be
2879 issued later. */
2880 ;
2881 }
2882
2883 return decl;
2884 }
2885 else if (ctype != NULL_TREE
2886 && (identifier_p (TREE_OPERAND (declarator, 0))))
2887 {
2888 // We'll match variable templates in start_decl.
2889 if (VAR_P (decl))
2890 return decl;
2891
2892 /* Find the list of functions in ctype that have the same
2893 name as the declared function. */
2894 tree name = TREE_OPERAND (declarator, 0);
2895 tree fns = NULL_TREE;
2896 int idx;
2897
2898 if (constructor_name_p (name, ctype))
2899 {
2900 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2901
2902 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2903 : !CLASSTYPE_DESTRUCTORS (ctype))
2904 {
2905 /* From [temp.expl.spec]:
2906
2907 If such an explicit specialization for the member
2908 of a class template names an implicitly-declared
2909 special member function (clause _special_), the
2910 program is ill-formed.
2911
2912 Similar language is found in [temp.explicit]. */
2913 error ("specialization of implicitly-declared special member function");
2914 return error_mark_node;
2915 }
2916
2917 name = is_constructor ? ctor_identifier : dtor_identifier;
2918 }
2919
2920 if (!DECL_CONV_FN_P (decl))
2921 {
2922 idx = lookup_fnfields_1 (ctype, name);
2923 if (idx >= 0)
2924 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2925 }
2926 else
2927 {
2928 vec<tree, va_gc> *methods;
2929 tree ovl;
2930
2931 /* For a type-conversion operator, we cannot do a
2932 name-based lookup. We might be looking for `operator
2933 int' which will be a specialization of `operator T'.
2934 So, we find *all* the conversion operators, and then
2935 select from them. */
2936 fns = NULL_TREE;
2937
2938 methods = CLASSTYPE_METHOD_VEC (ctype);
2939 if (methods)
2940 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2941 methods->iterate (idx, &ovl);
2942 ++idx)
2943 {
2944 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2945 /* There are no more conversion functions. */
2946 break;
2947
2948 /* Glue all these conversion functions together
2949 with those we already have. */
2950 for (; ovl; ovl = OVL_NEXT (ovl))
2951 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2952 }
2953 }
2954
2955 if (fns == NULL_TREE)
2956 {
2957 error ("no member function %qD declared in %qT", name, ctype);
2958 return error_mark_node;
2959 }
2960 else
2961 TREE_OPERAND (declarator, 0) = fns;
2962 }
2963
2964 /* Figure out what exactly is being specialized at this point.
2965 Note that for an explicit instantiation, even one for a
2966 member function, we cannot tell apriori whether the
2967 instantiation is for a member template, or just a member
2968 function of a template class. Even if a member template is
2969 being instantiated, the member template arguments may be
2970 elided if they can be deduced from the rest of the
2971 declaration. */
2972 tmpl = determine_specialization (declarator, decl,
2973 &targs,
2974 member_specialization,
2975 template_count,
2976 tsk);
2977
2978 if (!tmpl || tmpl == error_mark_node)
2979 /* We couldn't figure out what this declaration was
2980 specializing. */
2981 return error_mark_node;
2982 else
2983 {
2984 if (TREE_CODE (decl) == FUNCTION_DECL
2985 && DECL_HIDDEN_FRIEND_P (tmpl))
2986 {
2987 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2988 "friend declaration %qD is not visible to "
2989 "explicit specialization", tmpl))
2990 inform (DECL_SOURCE_LOCATION (tmpl),
2991 "friend declaration here");
2992 }
2993 else if (!ctype && !is_friend
2994 && CP_DECL_CONTEXT (decl) == current_namespace)
2995 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2996
2997 tree gen_tmpl = most_general_template (tmpl);
2998
2999 if (explicit_instantiation)
3000 {
3001 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3002 is done by do_decl_instantiation later. */
3003
3004 int arg_depth = TMPL_ARGS_DEPTH (targs);
3005 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3006
3007 if (arg_depth > parm_depth)
3008 {
3009 /* If TMPL is not the most general template (for
3010 example, if TMPL is a friend template that is
3011 injected into namespace scope), then there will
3012 be too many levels of TARGS. Remove some of them
3013 here. */
3014 int i;
3015 tree new_targs;
3016
3017 new_targs = make_tree_vec (parm_depth);
3018 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3019 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3020 = TREE_VEC_ELT (targs, i);
3021 targs = new_targs;
3022 }
3023
3024 return instantiate_template (tmpl, targs, tf_error);
3025 }
3026
3027 /* If we thought that the DECL was a member function, but it
3028 turns out to be specializing a static member function,
3029 make DECL a static member function as well. */
3030 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3031 && DECL_STATIC_FUNCTION_P (tmpl)
3032 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3033 revert_static_member_fn (decl);
3034
3035 /* If this is a specialization of a member template of a
3036 template class, we want to return the TEMPLATE_DECL, not
3037 the specialization of it. */
3038 if (tsk == tsk_template && !was_template_id)
3039 {
3040 tree result = DECL_TEMPLATE_RESULT (tmpl);
3041 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3042 DECL_INITIAL (result) = NULL_TREE;
3043 if (have_def)
3044 {
3045 tree parm;
3046 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3047 DECL_SOURCE_LOCATION (result)
3048 = DECL_SOURCE_LOCATION (decl);
3049 /* We want to use the argument list specified in the
3050 definition, not in the original declaration. */
3051 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3052 for (parm = DECL_ARGUMENTS (result); parm;
3053 parm = DECL_CHAIN (parm))
3054 DECL_CONTEXT (parm) = result;
3055 }
3056 return register_specialization (tmpl, gen_tmpl, targs,
3057 is_friend, 0);
3058 }
3059
3060 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3061 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3062
3063 if (was_template_id)
3064 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3065
3066 /* Inherit default function arguments from the template
3067 DECL is specializing. */
3068 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3069 copy_default_args_to_explicit_spec (decl);
3070
3071 /* This specialization has the same protection as the
3072 template it specializes. */
3073 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3074 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3075
3076 /* 7.1.1-1 [dcl.stc]
3077
3078 A storage-class-specifier shall not be specified in an
3079 explicit specialization...
3080
3081 The parser rejects these, so unless action is taken here,
3082 explicit function specializations will always appear with
3083 global linkage.
3084
3085 The action recommended by the C++ CWG in response to C++
3086 defect report 605 is to make the storage class and linkage
3087 of the explicit specialization match the templated function:
3088
3089 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3090 */
3091 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3092 {
3093 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3094 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3095
3096 /* A concept cannot be specialized. */
3097 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3098 {
3099 error ("explicit specialization of function concept %qD",
3100 gen_tmpl);
3101 return error_mark_node;
3102 }
3103
3104 /* This specialization has the same linkage and visibility as
3105 the function template it specializes. */
3106 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3107 if (! TREE_PUBLIC (decl))
3108 {
3109 DECL_INTERFACE_KNOWN (decl) = 1;
3110 DECL_NOT_REALLY_EXTERN (decl) = 1;
3111 }
3112 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3113 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3114 {
3115 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3116 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3117 }
3118 }
3119
3120 /* If DECL is a friend declaration, declared using an
3121 unqualified name, the namespace associated with DECL may
3122 have been set incorrectly. For example, in:
3123
3124 template <typename T> void f(T);
3125 namespace N {
3126 struct S { friend void f<int>(int); }
3127 }
3128
3129 we will have set the DECL_CONTEXT for the friend
3130 declaration to N, rather than to the global namespace. */
3131 if (DECL_NAMESPACE_SCOPE_P (decl))
3132 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3133
3134 if (is_friend && !have_def)
3135 /* This is not really a declaration of a specialization.
3136 It's just the name of an instantiation. But, it's not
3137 a request for an instantiation, either. */
3138 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3139 else if (TREE_CODE (decl) == FUNCTION_DECL)
3140 /* A specialization is not necessarily COMDAT. */
3141 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3142 && DECL_DECLARED_INLINE_P (decl));
3143 else if (VAR_P (decl))
3144 DECL_COMDAT (decl) = false;
3145
3146 /* If this is a full specialization, register it so that we can find
3147 it again. Partial specializations will be registered in
3148 process_partial_specialization. */
3149 if (!processing_template_decl)
3150 decl = register_specialization (decl, gen_tmpl, targs,
3151 is_friend, 0);
3152
3153 /* A 'structor should already have clones. */
3154 gcc_assert (decl == error_mark_node
3155 || variable_template_p (tmpl)
3156 || !(DECL_CONSTRUCTOR_P (decl)
3157 || DECL_DESTRUCTOR_P (decl))
3158 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3159 }
3160 }
3161
3162 return decl;
3163 }
3164
3165 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3166 parameters. These are represented in the same format used for
3167 DECL_TEMPLATE_PARMS. */
3168
3169 int
3170 comp_template_parms (const_tree parms1, const_tree parms2)
3171 {
3172 const_tree p1;
3173 const_tree p2;
3174
3175 if (parms1 == parms2)
3176 return 1;
3177
3178 for (p1 = parms1, p2 = parms2;
3179 p1 != NULL_TREE && p2 != NULL_TREE;
3180 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3181 {
3182 tree t1 = TREE_VALUE (p1);
3183 tree t2 = TREE_VALUE (p2);
3184 int i;
3185
3186 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3187 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3188
3189 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3190 return 0;
3191
3192 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3193 {
3194 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3195 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3196
3197 /* If either of the template parameters are invalid, assume
3198 they match for the sake of error recovery. */
3199 if (error_operand_p (parm1) || error_operand_p (parm2))
3200 return 1;
3201
3202 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3203 return 0;
3204
3205 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3206 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3207 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3208 continue;
3209 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3210 return 0;
3211 }
3212 }
3213
3214 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3215 /* One set of parameters has more parameters lists than the
3216 other. */
3217 return 0;
3218
3219 return 1;
3220 }
3221
3222 /* Determine whether PARM is a parameter pack. */
3223
3224 bool
3225 template_parameter_pack_p (const_tree parm)
3226 {
3227 /* Determine if we have a non-type template parameter pack. */
3228 if (TREE_CODE (parm) == PARM_DECL)
3229 return (DECL_TEMPLATE_PARM_P (parm)
3230 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3231 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3232 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3233
3234 /* If this is a list of template parameters, we could get a
3235 TYPE_DECL or a TEMPLATE_DECL. */
3236 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3237 parm = TREE_TYPE (parm);
3238
3239 /* Otherwise it must be a type template parameter. */
3240 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3241 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3242 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3243 }
3244
3245 /* Determine if T is a function parameter pack. */
3246
3247 bool
3248 function_parameter_pack_p (const_tree t)
3249 {
3250 if (t && TREE_CODE (t) == PARM_DECL)
3251 return DECL_PACK_P (t);
3252 return false;
3253 }
3254
3255 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3256 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3257
3258 tree
3259 get_function_template_decl (const_tree primary_func_tmpl_inst)
3260 {
3261 if (! primary_func_tmpl_inst
3262 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3263 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3264 return NULL;
3265
3266 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3267 }
3268
3269 /* Return true iff the function parameter PARAM_DECL was expanded
3270 from the function parameter pack PACK. */
3271
3272 bool
3273 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3274 {
3275 if (DECL_ARTIFICIAL (param_decl)
3276 || !function_parameter_pack_p (pack))
3277 return false;
3278
3279 /* The parameter pack and its pack arguments have the same
3280 DECL_PARM_INDEX. */
3281 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3282 }
3283
3284 /* Determine whether ARGS describes a variadic template args list,
3285 i.e., one that is terminated by a template argument pack. */
3286
3287 static bool
3288 template_args_variadic_p (tree args)
3289 {
3290 int nargs;
3291 tree last_parm;
3292
3293 if (args == NULL_TREE)
3294 return false;
3295
3296 args = INNERMOST_TEMPLATE_ARGS (args);
3297 nargs = TREE_VEC_LENGTH (args);
3298
3299 if (nargs == 0)
3300 return false;
3301
3302 last_parm = TREE_VEC_ELT (args, nargs - 1);
3303
3304 return ARGUMENT_PACK_P (last_parm);
3305 }
3306
3307 /* Generate a new name for the parameter pack name NAME (an
3308 IDENTIFIER_NODE) that incorporates its */
3309
3310 static tree
3311 make_ith_pack_parameter_name (tree name, int i)
3312 {
3313 /* Munge the name to include the parameter index. */
3314 #define NUMBUF_LEN 128
3315 char numbuf[NUMBUF_LEN];
3316 char* newname;
3317 int newname_len;
3318
3319 if (name == NULL_TREE)
3320 return name;
3321 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3322 newname_len = IDENTIFIER_LENGTH (name)
3323 + strlen (numbuf) + 2;
3324 newname = (char*)alloca (newname_len);
3325 snprintf (newname, newname_len,
3326 "%s#%i", IDENTIFIER_POINTER (name), i);
3327 return get_identifier (newname);
3328 }
3329
3330 /* Return true if T is a primary function, class or alias template
3331 instantiation. */
3332
3333 bool
3334 primary_template_instantiation_p (const_tree t)
3335 {
3336 if (!t)
3337 return false;
3338
3339 if (TREE_CODE (t) == FUNCTION_DECL)
3340 return DECL_LANG_SPECIFIC (t)
3341 && DECL_TEMPLATE_INSTANTIATION (t)
3342 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3343 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3344 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3345 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3346 else if (alias_template_specialization_p (t))
3347 return true;
3348 return false;
3349 }
3350
3351 /* Return true if PARM is a template template parameter. */
3352
3353 bool
3354 template_template_parameter_p (const_tree parm)
3355 {
3356 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3357 }
3358
3359 /* Return true iff PARM is a DECL representing a type template
3360 parameter. */
3361
3362 bool
3363 template_type_parameter_p (const_tree parm)
3364 {
3365 return (parm
3366 && (TREE_CODE (parm) == TYPE_DECL
3367 || TREE_CODE (parm) == TEMPLATE_DECL)
3368 && DECL_TEMPLATE_PARM_P (parm));
3369 }
3370
3371 /* Return the template parameters of T if T is a
3372 primary template instantiation, NULL otherwise. */
3373
3374 tree
3375 get_primary_template_innermost_parameters (const_tree t)
3376 {
3377 tree parms = NULL, template_info = NULL;
3378
3379 if ((template_info = get_template_info (t))
3380 && primary_template_instantiation_p (t))
3381 parms = INNERMOST_TEMPLATE_PARMS
3382 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3383
3384 return parms;
3385 }
3386
3387 /* Return the template parameters of the LEVELth level from the full list
3388 of template parameters PARMS. */
3389
3390 tree
3391 get_template_parms_at_level (tree parms, int level)
3392 {
3393 tree p;
3394 if (!parms
3395 || TREE_CODE (parms) != TREE_LIST
3396 || level > TMPL_PARMS_DEPTH (parms))
3397 return NULL_TREE;
3398
3399 for (p = parms; p; p = TREE_CHAIN (p))
3400 if (TMPL_PARMS_DEPTH (p) == level)
3401 return p;
3402
3403 return NULL_TREE;
3404 }
3405
3406 /* Returns the template arguments of T if T is a template instantiation,
3407 NULL otherwise. */
3408
3409 tree
3410 get_template_innermost_arguments (const_tree t)
3411 {
3412 tree args = NULL, template_info = NULL;
3413
3414 if ((template_info = get_template_info (t))
3415 && TI_ARGS (template_info))
3416 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3417
3418 return args;
3419 }
3420
3421 /* Return the argument pack elements of T if T is a template argument pack,
3422 NULL otherwise. */
3423
3424 tree
3425 get_template_argument_pack_elems (const_tree t)
3426 {
3427 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3428 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3429 return NULL;
3430
3431 return ARGUMENT_PACK_ARGS (t);
3432 }
3433
3434 /* Structure used to track the progress of find_parameter_packs_r. */
3435 struct find_parameter_pack_data
3436 {
3437 /* TREE_LIST that will contain all of the parameter packs found by
3438 the traversal. */
3439 tree* parameter_packs;
3440
3441 /* Set of AST nodes that have been visited by the traversal. */
3442 hash_set<tree> *visited;
3443
3444 /* True iff we're making a type pack expansion. */
3445 bool type_pack_expansion_p;
3446 };
3447
3448 /* Identifies all of the argument packs that occur in a template
3449 argument and appends them to the TREE_LIST inside DATA, which is a
3450 find_parameter_pack_data structure. This is a subroutine of
3451 make_pack_expansion and uses_parameter_packs. */
3452 static tree
3453 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3454 {
3455 tree t = *tp;
3456 struct find_parameter_pack_data* ppd =
3457 (struct find_parameter_pack_data*)data;
3458 bool parameter_pack_p = false;
3459
3460 /* Handle type aliases/typedefs. */
3461 if (TYPE_ALIAS_P (t))
3462 {
3463 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3464 cp_walk_tree (&TI_ARGS (tinfo),
3465 &find_parameter_packs_r,
3466 ppd, ppd->visited);
3467 *walk_subtrees = 0;
3468 return NULL_TREE;
3469 }
3470
3471 /* Identify whether this is a parameter pack or not. */
3472 switch (TREE_CODE (t))
3473 {
3474 case TEMPLATE_PARM_INDEX:
3475 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3476 parameter_pack_p = true;
3477 break;
3478
3479 case TEMPLATE_TYPE_PARM:
3480 t = TYPE_MAIN_VARIANT (t);
3481 /* FALLTHRU */
3482 case TEMPLATE_TEMPLATE_PARM:
3483 /* If the placeholder appears in the decl-specifier-seq of a function
3484 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3485 is a pack expansion, the invented template parameter is a template
3486 parameter pack. */
3487 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3488 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3489 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3490 parameter_pack_p = true;
3491 break;
3492
3493 case FIELD_DECL:
3494 case PARM_DECL:
3495 if (DECL_PACK_P (t))
3496 {
3497 /* We don't want to walk into the type of a PARM_DECL,
3498 because we don't want to see the type parameter pack. */
3499 *walk_subtrees = 0;
3500 parameter_pack_p = true;
3501 }
3502 break;
3503
3504 /* Look through a lambda capture proxy to the field pack. */
3505 case VAR_DECL:
3506 if (DECL_HAS_VALUE_EXPR_P (t))
3507 {
3508 tree v = DECL_VALUE_EXPR (t);
3509 cp_walk_tree (&v,
3510 &find_parameter_packs_r,
3511 ppd, ppd->visited);
3512 *walk_subtrees = 0;
3513 }
3514 else if (variable_template_specialization_p (t))
3515 {
3516 cp_walk_tree (&DECL_TI_ARGS (t),
3517 find_parameter_packs_r,
3518 ppd, ppd->visited);
3519 *walk_subtrees = 0;
3520 }
3521 break;
3522
3523 case BASES:
3524 parameter_pack_p = true;
3525 break;
3526 default:
3527 /* Not a parameter pack. */
3528 break;
3529 }
3530
3531 if (parameter_pack_p)
3532 {
3533 /* Add this parameter pack to the list. */
3534 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3535 }
3536
3537 if (TYPE_P (t))
3538 cp_walk_tree (&TYPE_CONTEXT (t),
3539 &find_parameter_packs_r, ppd, ppd->visited);
3540
3541 /* This switch statement will return immediately if we don't find a
3542 parameter pack. */
3543 switch (TREE_CODE (t))
3544 {
3545 case TEMPLATE_PARM_INDEX:
3546 return NULL_TREE;
3547
3548 case BOUND_TEMPLATE_TEMPLATE_PARM:
3549 /* Check the template itself. */
3550 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3551 &find_parameter_packs_r, ppd, ppd->visited);
3552 /* Check the template arguments. */
3553 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3554 ppd->visited);
3555 *walk_subtrees = 0;
3556 return NULL_TREE;
3557
3558 case TEMPLATE_TYPE_PARM:
3559 case TEMPLATE_TEMPLATE_PARM:
3560 return NULL_TREE;
3561
3562 case PARM_DECL:
3563 return NULL_TREE;
3564
3565 case RECORD_TYPE:
3566 if (TYPE_PTRMEMFUNC_P (t))
3567 return NULL_TREE;
3568 /* Fall through. */
3569
3570 case UNION_TYPE:
3571 case ENUMERAL_TYPE:
3572 if (TYPE_TEMPLATE_INFO (t))
3573 cp_walk_tree (&TYPE_TI_ARGS (t),
3574 &find_parameter_packs_r, ppd, ppd->visited);
3575
3576 *walk_subtrees = 0;
3577 return NULL_TREE;
3578
3579 case TEMPLATE_DECL:
3580 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3581 return NULL_TREE;
3582 gcc_fallthrough();
3583
3584 case CONSTRUCTOR:
3585 cp_walk_tree (&TREE_TYPE (t),
3586 &find_parameter_packs_r, ppd, ppd->visited);
3587 return NULL_TREE;
3588
3589 case TYPENAME_TYPE:
3590 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3591 ppd, ppd->visited);
3592 *walk_subtrees = 0;
3593 return NULL_TREE;
3594
3595 case TYPE_PACK_EXPANSION:
3596 case EXPR_PACK_EXPANSION:
3597 *walk_subtrees = 0;
3598 return NULL_TREE;
3599
3600 case INTEGER_TYPE:
3601 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3602 ppd, ppd->visited);
3603 *walk_subtrees = 0;
3604 return NULL_TREE;
3605
3606 case IDENTIFIER_NODE:
3607 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3608 ppd->visited);
3609 *walk_subtrees = 0;
3610 return NULL_TREE;
3611
3612 case DECLTYPE_TYPE:
3613 {
3614 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3615 type_pack_expansion_p to false so that any placeholders
3616 within the expression don't get marked as parameter packs. */
3617 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3618 ppd->type_pack_expansion_p = false;
3619 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3620 ppd, ppd->visited);
3621 ppd->type_pack_expansion_p = type_pack_expansion_p;
3622 *walk_subtrees = 0;
3623 return NULL_TREE;
3624 }
3625
3626 default:
3627 return NULL_TREE;
3628 }
3629
3630 return NULL_TREE;
3631 }
3632
3633 /* Determines if the expression or type T uses any parameter packs. */
3634 bool
3635 uses_parameter_packs (tree t)
3636 {
3637 tree parameter_packs = NULL_TREE;
3638 struct find_parameter_pack_data ppd;
3639 ppd.parameter_packs = &parameter_packs;
3640 ppd.visited = new hash_set<tree>;
3641 ppd.type_pack_expansion_p = false;
3642 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3643 delete ppd.visited;
3644 return parameter_packs != NULL_TREE;
3645 }
3646
3647 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3648 representation a base-class initializer into a parameter pack
3649 expansion. If all goes well, the resulting node will be an
3650 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3651 respectively. */
3652 tree
3653 make_pack_expansion (tree arg)
3654 {
3655 tree result;
3656 tree parameter_packs = NULL_TREE;
3657 bool for_types = false;
3658 struct find_parameter_pack_data ppd;
3659
3660 if (!arg || arg == error_mark_node)
3661 return arg;
3662
3663 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3664 {
3665 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3666 class initializer. In this case, the TREE_PURPOSE will be a
3667 _TYPE node (representing the base class expansion we're
3668 initializing) and the TREE_VALUE will be a TREE_LIST
3669 containing the initialization arguments.
3670
3671 The resulting expansion looks somewhat different from most
3672 expansions. Rather than returning just one _EXPANSION, we
3673 return a TREE_LIST whose TREE_PURPOSE is a
3674 TYPE_PACK_EXPANSION containing the bases that will be
3675 initialized. The TREE_VALUE will be identical to the
3676 original TREE_VALUE, which is a list of arguments that will
3677 be passed to each base. We do not introduce any new pack
3678 expansion nodes into the TREE_VALUE (although it is possible
3679 that some already exist), because the TREE_PURPOSE and
3680 TREE_VALUE all need to be expanded together with the same
3681 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3682 resulting TREE_PURPOSE will mention the parameter packs in
3683 both the bases and the arguments to the bases. */
3684 tree purpose;
3685 tree value;
3686 tree parameter_packs = NULL_TREE;
3687
3688 /* Determine which parameter packs will be used by the base
3689 class expansion. */
3690 ppd.visited = new hash_set<tree>;
3691 ppd.parameter_packs = &parameter_packs;
3692 ppd.type_pack_expansion_p = true;
3693 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3694 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3695 &ppd, ppd.visited);
3696
3697 if (parameter_packs == NULL_TREE)
3698 {
3699 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3700 delete ppd.visited;
3701 return error_mark_node;
3702 }
3703
3704 if (TREE_VALUE (arg) != void_type_node)
3705 {
3706 /* Collect the sets of parameter packs used in each of the
3707 initialization arguments. */
3708 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3709 {
3710 /* Determine which parameter packs will be expanded in this
3711 argument. */
3712 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3713 &ppd, ppd.visited);
3714 }
3715 }
3716
3717 delete ppd.visited;
3718
3719 /* Create the pack expansion type for the base type. */
3720 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3721 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3722 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3723
3724 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3725 they will rarely be compared to anything. */
3726 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3727
3728 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3729 }
3730
3731 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3732 for_types = true;
3733
3734 /* Build the PACK_EXPANSION_* node. */
3735 result = for_types
3736 ? cxx_make_type (TYPE_PACK_EXPANSION)
3737 : make_node (EXPR_PACK_EXPANSION);
3738 SET_PACK_EXPANSION_PATTERN (result, arg);
3739 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3740 {
3741 /* Propagate type and const-expression information. */
3742 TREE_TYPE (result) = TREE_TYPE (arg);
3743 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3744 /* Mark this read now, since the expansion might be length 0. */
3745 mark_exp_read (arg);
3746 }
3747 else
3748 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3749 they will rarely be compared to anything. */
3750 SET_TYPE_STRUCTURAL_EQUALITY (result);
3751
3752 /* Determine which parameter packs will be expanded. */
3753 ppd.parameter_packs = &parameter_packs;
3754 ppd.visited = new hash_set<tree>;
3755 ppd.type_pack_expansion_p = TYPE_P (arg);
3756 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3757 delete ppd.visited;
3758
3759 /* Make sure we found some parameter packs. */
3760 if (parameter_packs == NULL_TREE)
3761 {
3762 if (TYPE_P (arg))
3763 error ("expansion pattern %<%T%> contains no argument packs", arg);
3764 else
3765 error ("expansion pattern %<%E%> contains no argument packs", arg);
3766 return error_mark_node;
3767 }
3768 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3769
3770 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3771
3772 return result;
3773 }
3774
3775 /* Checks T for any "bare" parameter packs, which have not yet been
3776 expanded, and issues an error if any are found. This operation can
3777 only be done on full expressions or types (e.g., an expression
3778 statement, "if" condition, etc.), because we could have expressions like:
3779
3780 foo(f(g(h(args)))...)
3781
3782 where "args" is a parameter pack. check_for_bare_parameter_packs
3783 should not be called for the subexpressions args, h(args),
3784 g(h(args)), or f(g(h(args))), because we would produce erroneous
3785 error messages.
3786
3787 Returns TRUE and emits an error if there were bare parameter packs,
3788 returns FALSE otherwise. */
3789 bool
3790 check_for_bare_parameter_packs (tree t)
3791 {
3792 tree parameter_packs = NULL_TREE;
3793 struct find_parameter_pack_data ppd;
3794
3795 if (!processing_template_decl || !t || t == error_mark_node)
3796 return false;
3797
3798 if (TREE_CODE (t) == TYPE_DECL)
3799 t = TREE_TYPE (t);
3800
3801 ppd.parameter_packs = &parameter_packs;
3802 ppd.visited = new hash_set<tree>;
3803 ppd.type_pack_expansion_p = false;
3804 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3805 delete ppd.visited;
3806
3807 if (parameter_packs)
3808 {
3809 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3810 error_at (loc, "parameter packs not expanded with %<...%>:");
3811 while (parameter_packs)
3812 {
3813 tree pack = TREE_VALUE (parameter_packs);
3814 tree name = NULL_TREE;
3815
3816 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3817 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3818 name = TYPE_NAME (pack);
3819 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3820 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3821 else
3822 name = DECL_NAME (pack);
3823
3824 if (name)
3825 inform (loc, " %qD", name);
3826 else
3827 inform (loc, " <anonymous>");
3828
3829 parameter_packs = TREE_CHAIN (parameter_packs);
3830 }
3831
3832 return true;
3833 }
3834
3835 return false;
3836 }
3837
3838 /* Expand any parameter packs that occur in the template arguments in
3839 ARGS. */
3840 tree
3841 expand_template_argument_pack (tree args)
3842 {
3843 if (args == error_mark_node)
3844 return error_mark_node;
3845
3846 tree result_args = NULL_TREE;
3847 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3848 int num_result_args = -1;
3849 int non_default_args_count = -1;
3850
3851 /* First, determine if we need to expand anything, and the number of
3852 slots we'll need. */
3853 for (in_arg = 0; in_arg < nargs; ++in_arg)
3854 {
3855 tree arg = TREE_VEC_ELT (args, in_arg);
3856 if (arg == NULL_TREE)
3857 return args;
3858 if (ARGUMENT_PACK_P (arg))
3859 {
3860 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3861 if (num_result_args < 0)
3862 num_result_args = in_arg + num_packed;
3863 else
3864 num_result_args += num_packed;
3865 }
3866 else
3867 {
3868 if (num_result_args >= 0)
3869 num_result_args++;
3870 }
3871 }
3872
3873 /* If no expansion is necessary, we're done. */
3874 if (num_result_args < 0)
3875 return args;
3876
3877 /* Expand arguments. */
3878 result_args = make_tree_vec (num_result_args);
3879 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3880 non_default_args_count =
3881 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3882 for (in_arg = 0; in_arg < nargs; ++in_arg)
3883 {
3884 tree arg = TREE_VEC_ELT (args, in_arg);
3885 if (ARGUMENT_PACK_P (arg))
3886 {
3887 tree packed = ARGUMENT_PACK_ARGS (arg);
3888 int i, num_packed = TREE_VEC_LENGTH (packed);
3889 for (i = 0; i < num_packed; ++i, ++out_arg)
3890 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3891 if (non_default_args_count > 0)
3892 non_default_args_count += num_packed - 1;
3893 }
3894 else
3895 {
3896 TREE_VEC_ELT (result_args, out_arg) = arg;
3897 ++out_arg;
3898 }
3899 }
3900 if (non_default_args_count >= 0)
3901 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3902 return result_args;
3903 }
3904
3905 /* Checks if DECL shadows a template parameter.
3906
3907 [temp.local]: A template-parameter shall not be redeclared within its
3908 scope (including nested scopes).
3909
3910 Emits an error and returns TRUE if the DECL shadows a parameter,
3911 returns FALSE otherwise. */
3912
3913 bool
3914 check_template_shadow (tree decl)
3915 {
3916 tree olddecl;
3917
3918 /* If we're not in a template, we can't possibly shadow a template
3919 parameter. */
3920 if (!current_template_parms)
3921 return true;
3922
3923 /* Figure out what we're shadowing. */
3924 if (TREE_CODE (decl) == OVERLOAD)
3925 decl = OVL_CURRENT (decl);
3926 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3927
3928 /* If there's no previous binding for this name, we're not shadowing
3929 anything, let alone a template parameter. */
3930 if (!olddecl)
3931 return true;
3932
3933 /* If we're not shadowing a template parameter, we're done. Note
3934 that OLDDECL might be an OVERLOAD (or perhaps even an
3935 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3936 node. */
3937 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3938 return true;
3939
3940 /* We check for decl != olddecl to avoid bogus errors for using a
3941 name inside a class. We check TPFI to avoid duplicate errors for
3942 inline member templates. */
3943 if (decl == olddecl
3944 || (DECL_TEMPLATE_PARM_P (decl)
3945 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3946 return true;
3947
3948 /* Don't complain about the injected class name, as we've already
3949 complained about the class itself. */
3950 if (DECL_SELF_REFERENCE_P (decl))
3951 return false;
3952
3953 if (DECL_TEMPLATE_PARM_P (decl))
3954 error ("declaration of template parameter %q+D shadows "
3955 "template parameter", decl);
3956 else
3957 error ("declaration of %q+#D shadows template parameter", decl);
3958 inform (DECL_SOURCE_LOCATION (olddecl),
3959 "template parameter %qD declared here", olddecl);
3960 return false;
3961 }
3962
3963 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3964 ORIG_LEVEL, DECL, and TYPE. */
3965
3966 static tree
3967 build_template_parm_index (int index,
3968 int level,
3969 int orig_level,
3970 tree decl,
3971 tree type)
3972 {
3973 tree t = make_node (TEMPLATE_PARM_INDEX);
3974 TEMPLATE_PARM_IDX (t) = index;
3975 TEMPLATE_PARM_LEVEL (t) = level;
3976 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3977 TEMPLATE_PARM_DECL (t) = decl;
3978 TREE_TYPE (t) = type;
3979 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3980 TREE_READONLY (t) = TREE_READONLY (decl);
3981
3982 return t;
3983 }
3984
3985 /* Find the canonical type parameter for the given template type
3986 parameter. Returns the canonical type parameter, which may be TYPE
3987 if no such parameter existed. */
3988
3989 static tree
3990 canonical_type_parameter (tree type)
3991 {
3992 tree list;
3993 int idx = TEMPLATE_TYPE_IDX (type);
3994 if (!canonical_template_parms)
3995 vec_alloc (canonical_template_parms, idx+1);
3996
3997 while (canonical_template_parms->length () <= (unsigned)idx)
3998 vec_safe_push (canonical_template_parms, NULL_TREE);
3999
4000 list = (*canonical_template_parms)[idx];
4001 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4002 list = TREE_CHAIN (list);
4003
4004 if (list)
4005 return TREE_VALUE (list);
4006 else
4007 {
4008 (*canonical_template_parms)[idx]
4009 = tree_cons (NULL_TREE, type,
4010 (*canonical_template_parms)[idx]);
4011 return type;
4012 }
4013 }
4014
4015 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4016 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4017 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4018 new one is created. */
4019
4020 static tree
4021 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4022 tsubst_flags_t complain)
4023 {
4024 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4025 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4026 != TEMPLATE_PARM_LEVEL (index) - levels)
4027 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4028 {
4029 tree orig_decl = TEMPLATE_PARM_DECL (index);
4030 tree decl, t;
4031
4032 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4033 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4034 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4035 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4036 DECL_ARTIFICIAL (decl) = 1;
4037 SET_DECL_TEMPLATE_PARM_P (decl);
4038
4039 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4040 TEMPLATE_PARM_LEVEL (index) - levels,
4041 TEMPLATE_PARM_ORIG_LEVEL (index),
4042 decl, type);
4043 TEMPLATE_PARM_DESCENDANTS (index) = t;
4044 TEMPLATE_PARM_PARAMETER_PACK (t)
4045 = TEMPLATE_PARM_PARAMETER_PACK (index);
4046
4047 /* Template template parameters need this. */
4048 if (TREE_CODE (decl) == TEMPLATE_DECL)
4049 {
4050 DECL_TEMPLATE_RESULT (decl)
4051 = build_decl (DECL_SOURCE_LOCATION (decl),
4052 TYPE_DECL, DECL_NAME (decl), type);
4053 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4054 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4055 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4056 }
4057 }
4058
4059 return TEMPLATE_PARM_DESCENDANTS (index);
4060 }
4061
4062 /* Process information from new template parameter PARM and append it
4063 to the LIST being built. This new parameter is a non-type
4064 parameter iff IS_NON_TYPE is true. This new parameter is a
4065 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4066 is in PARM_LOC. */
4067
4068 tree
4069 process_template_parm (tree list, location_t parm_loc, tree parm,
4070 bool is_non_type, bool is_parameter_pack)
4071 {
4072 tree decl = 0;
4073 int idx = 0;
4074
4075 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4076 tree defval = TREE_PURPOSE (parm);
4077 tree constr = TREE_TYPE (parm);
4078
4079 if (list)
4080 {
4081 tree p = tree_last (list);
4082
4083 if (p && TREE_VALUE (p) != error_mark_node)
4084 {
4085 p = TREE_VALUE (p);
4086 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4087 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4088 else
4089 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4090 }
4091
4092 ++idx;
4093 }
4094
4095 if (is_non_type)
4096 {
4097 parm = TREE_VALUE (parm);
4098
4099 SET_DECL_TEMPLATE_PARM_P (parm);
4100
4101 if (TREE_TYPE (parm) != error_mark_node)
4102 {
4103 /* [temp.param]
4104
4105 The top-level cv-qualifiers on the template-parameter are
4106 ignored when determining its type. */
4107 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4108 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4109 TREE_TYPE (parm) = error_mark_node;
4110 else if (uses_parameter_packs (TREE_TYPE (parm))
4111 && !is_parameter_pack
4112 /* If we're in a nested template parameter list, the template
4113 template parameter could be a parameter pack. */
4114 && processing_template_parmlist == 1)
4115 {
4116 /* This template parameter is not a parameter pack, but it
4117 should be. Complain about "bare" parameter packs. */
4118 check_for_bare_parameter_packs (TREE_TYPE (parm));
4119
4120 /* Recover by calling this a parameter pack. */
4121 is_parameter_pack = true;
4122 }
4123 }
4124
4125 /* A template parameter is not modifiable. */
4126 TREE_CONSTANT (parm) = 1;
4127 TREE_READONLY (parm) = 1;
4128 decl = build_decl (parm_loc,
4129 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4130 TREE_CONSTANT (decl) = 1;
4131 TREE_READONLY (decl) = 1;
4132 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4133 = build_template_parm_index (idx, processing_template_decl,
4134 processing_template_decl,
4135 decl, TREE_TYPE (parm));
4136
4137 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4138 = is_parameter_pack;
4139 }
4140 else
4141 {
4142 tree t;
4143 parm = TREE_VALUE (TREE_VALUE (parm));
4144
4145 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4146 {
4147 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4148 /* This is for distinguishing between real templates and template
4149 template parameters */
4150 TREE_TYPE (parm) = t;
4151 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4152 decl = parm;
4153 }
4154 else
4155 {
4156 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4157 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4158 decl = build_decl (parm_loc,
4159 TYPE_DECL, parm, t);
4160 }
4161
4162 TYPE_NAME (t) = decl;
4163 TYPE_STUB_DECL (t) = decl;
4164 parm = decl;
4165 TEMPLATE_TYPE_PARM_INDEX (t)
4166 = build_template_parm_index (idx, processing_template_decl,
4167 processing_template_decl,
4168 decl, TREE_TYPE (parm));
4169 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4170 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4171 }
4172 DECL_ARTIFICIAL (decl) = 1;
4173 SET_DECL_TEMPLATE_PARM_P (decl);
4174
4175 /* Build requirements for the type/template parameter.
4176 This must be done after SET_DECL_TEMPLATE_PARM_P or
4177 process_template_parm could fail. */
4178 tree reqs = finish_shorthand_constraint (parm, constr);
4179
4180 pushdecl (decl);
4181
4182 /* Build the parameter node linking the parameter declaration,
4183 its default argument (if any), and its constraints (if any). */
4184 parm = build_tree_list (defval, parm);
4185 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4186
4187 return chainon (list, parm);
4188 }
4189
4190 /* The end of a template parameter list has been reached. Process the
4191 tree list into a parameter vector, converting each parameter into a more
4192 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4193 as PARM_DECLs. */
4194
4195 tree
4196 end_template_parm_list (tree parms)
4197 {
4198 int nparms;
4199 tree parm, next;
4200 tree saved_parmlist = make_tree_vec (list_length (parms));
4201
4202 /* Pop the dummy parameter level and add the real one. */
4203 current_template_parms = TREE_CHAIN (current_template_parms);
4204
4205 current_template_parms
4206 = tree_cons (size_int (processing_template_decl),
4207 saved_parmlist, current_template_parms);
4208
4209 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4210 {
4211 next = TREE_CHAIN (parm);
4212 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4213 TREE_CHAIN (parm) = NULL_TREE;
4214 }
4215
4216 --processing_template_parmlist;
4217
4218 return saved_parmlist;
4219 }
4220
4221 // Explicitly indicate the end of the template parameter list. We assume
4222 // that the current template parameters have been constructed and/or
4223 // managed explicitly, as when creating new template template parameters
4224 // from a shorthand constraint.
4225 void
4226 end_template_parm_list ()
4227 {
4228 --processing_template_parmlist;
4229 }
4230
4231 /* end_template_decl is called after a template declaration is seen. */
4232
4233 void
4234 end_template_decl (void)
4235 {
4236 reset_specialization ();
4237
4238 if (! processing_template_decl)
4239 return;
4240
4241 /* This matches the pushlevel in begin_template_parm_list. */
4242 finish_scope ();
4243
4244 --processing_template_decl;
4245 current_template_parms = TREE_CHAIN (current_template_parms);
4246 }
4247
4248 /* Takes a TREE_LIST representing a template parameter and convert it
4249 into an argument suitable to be passed to the type substitution
4250 functions. Note that If the TREE_LIST contains an error_mark
4251 node, the returned argument is error_mark_node. */
4252
4253 tree
4254 template_parm_to_arg (tree t)
4255 {
4256
4257 if (t == NULL_TREE
4258 || TREE_CODE (t) != TREE_LIST)
4259 return t;
4260
4261 if (error_operand_p (TREE_VALUE (t)))
4262 return error_mark_node;
4263
4264 t = TREE_VALUE (t);
4265
4266 if (TREE_CODE (t) == TYPE_DECL
4267 || TREE_CODE (t) == TEMPLATE_DECL)
4268 {
4269 t = TREE_TYPE (t);
4270
4271 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4272 {
4273 /* Turn this argument into a TYPE_ARGUMENT_PACK
4274 with a single element, which expands T. */
4275 tree vec = make_tree_vec (1);
4276 if (CHECKING_P)
4277 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4278
4279 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4280
4281 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4282 SET_ARGUMENT_PACK_ARGS (t, vec);
4283 }
4284 }
4285 else
4286 {
4287 t = DECL_INITIAL (t);
4288
4289 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4290 {
4291 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4292 with a single element, which expands T. */
4293 tree vec = make_tree_vec (1);
4294 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4295 if (CHECKING_P)
4296 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4297
4298 t = convert_from_reference (t);
4299 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4300
4301 t = make_node (NONTYPE_ARGUMENT_PACK);
4302 SET_ARGUMENT_PACK_ARGS (t, vec);
4303 TREE_TYPE (t) = type;
4304 }
4305 else
4306 t = convert_from_reference (t);
4307 }
4308 return t;
4309 }
4310
4311 /* Given a single level of template parameters (a TREE_VEC), return it
4312 as a set of template arguments. */
4313
4314 static tree
4315 template_parms_level_to_args (tree parms)
4316 {
4317 tree a = copy_node (parms);
4318 TREE_TYPE (a) = NULL_TREE;
4319 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4320 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4321
4322 if (CHECKING_P)
4323 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4324
4325 return a;
4326 }
4327
4328 /* Given a set of template parameters, return them as a set of template
4329 arguments. The template parameters are represented as a TREE_VEC, in
4330 the form documented in cp-tree.h for template arguments. */
4331
4332 static tree
4333 template_parms_to_args (tree parms)
4334 {
4335 tree header;
4336 tree args = NULL_TREE;
4337 int length = TMPL_PARMS_DEPTH (parms);
4338 int l = length;
4339
4340 /* If there is only one level of template parameters, we do not
4341 create a TREE_VEC of TREE_VECs. Instead, we return a single
4342 TREE_VEC containing the arguments. */
4343 if (length > 1)
4344 args = make_tree_vec (length);
4345
4346 for (header = parms; header; header = TREE_CHAIN (header))
4347 {
4348 tree a = template_parms_level_to_args (TREE_VALUE (header));
4349
4350 if (length > 1)
4351 TREE_VEC_ELT (args, --l) = a;
4352 else
4353 args = a;
4354 }
4355
4356 return args;
4357 }
4358
4359 /* Within the declaration of a template, return the currently active
4360 template parameters as an argument TREE_VEC. */
4361
4362 static tree
4363 current_template_args (void)
4364 {
4365 return template_parms_to_args (current_template_parms);
4366 }
4367
4368 /* Update the declared TYPE by doing any lookups which were thought to be
4369 dependent, but are not now that we know the SCOPE of the declarator. */
4370
4371 tree
4372 maybe_update_decl_type (tree orig_type, tree scope)
4373 {
4374 tree type = orig_type;
4375
4376 if (type == NULL_TREE)
4377 return type;
4378
4379 if (TREE_CODE (orig_type) == TYPE_DECL)
4380 type = TREE_TYPE (type);
4381
4382 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4383 && dependent_type_p (type)
4384 /* Don't bother building up the args in this case. */
4385 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4386 {
4387 /* tsubst in the args corresponding to the template parameters,
4388 including auto if present. Most things will be unchanged, but
4389 make_typename_type and tsubst_qualified_id will resolve
4390 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4391 tree args = current_template_args ();
4392 tree auto_node = type_uses_auto (type);
4393 tree pushed;
4394 if (auto_node)
4395 {
4396 tree auto_vec = make_tree_vec (1);
4397 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4398 args = add_to_template_args (args, auto_vec);
4399 }
4400 pushed = push_scope (scope);
4401 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4402 if (pushed)
4403 pop_scope (scope);
4404 }
4405
4406 if (type == error_mark_node)
4407 return orig_type;
4408
4409 if (TREE_CODE (orig_type) == TYPE_DECL)
4410 {
4411 if (same_type_p (type, TREE_TYPE (orig_type)))
4412 type = orig_type;
4413 else
4414 type = TYPE_NAME (type);
4415 }
4416 return type;
4417 }
4418
4419 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4420 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4421 the new template is a member template. */
4422
4423 tree
4424 build_template_decl (tree decl, tree parms, bool member_template_p)
4425 {
4426 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4427 DECL_TEMPLATE_PARMS (tmpl) = parms;
4428 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4429 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4430 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4431
4432 return tmpl;
4433 }
4434
4435 struct template_parm_data
4436 {
4437 /* The level of the template parameters we are currently
4438 processing. */
4439 int level;
4440
4441 /* The index of the specialization argument we are currently
4442 processing. */
4443 int current_arg;
4444
4445 /* An array whose size is the number of template parameters. The
4446 elements are nonzero if the parameter has been used in any one
4447 of the arguments processed so far. */
4448 int* parms;
4449
4450 /* An array whose size is the number of template arguments. The
4451 elements are nonzero if the argument makes use of template
4452 parameters of this level. */
4453 int* arg_uses_template_parms;
4454 };
4455
4456 /* Subroutine of push_template_decl used to see if each template
4457 parameter in a partial specialization is used in the explicit
4458 argument list. If T is of the LEVEL given in DATA (which is
4459 treated as a template_parm_data*), then DATA->PARMS is marked
4460 appropriately. */
4461
4462 static int
4463 mark_template_parm (tree t, void* data)
4464 {
4465 int level;
4466 int idx;
4467 struct template_parm_data* tpd = (struct template_parm_data*) data;
4468
4469 template_parm_level_and_index (t, &level, &idx);
4470
4471 if (level == tpd->level)
4472 {
4473 tpd->parms[idx] = 1;
4474 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4475 }
4476
4477 /* In C++17 the type of a non-type argument is a deduced context. */
4478 if (cxx_dialect >= cxx1z
4479 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4480 for_each_template_parm (TREE_TYPE (t),
4481 &mark_template_parm,
4482 data,
4483 NULL,
4484 /*include_nondeduced_p=*/false);
4485
4486 /* Return zero so that for_each_template_parm will continue the
4487 traversal of the tree; we want to mark *every* template parm. */
4488 return 0;
4489 }
4490
4491 /* Process the partial specialization DECL. */
4492
4493 static tree
4494 process_partial_specialization (tree decl)
4495 {
4496 tree type = TREE_TYPE (decl);
4497 tree tinfo = get_template_info (decl);
4498 tree maintmpl = TI_TEMPLATE (tinfo);
4499 tree specargs = TI_ARGS (tinfo);
4500 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4501 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4502 tree inner_parms;
4503 tree inst;
4504 int nargs = TREE_VEC_LENGTH (inner_args);
4505 int ntparms;
4506 int i;
4507 bool did_error_intro = false;
4508 struct template_parm_data tpd;
4509 struct template_parm_data tpd2;
4510
4511 gcc_assert (current_template_parms);
4512
4513 /* A concept cannot be specialized. */
4514 if (flag_concepts && variable_concept_p (maintmpl))
4515 {
4516 error ("specialization of variable concept %q#D", maintmpl);
4517 return error_mark_node;
4518 }
4519
4520 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4521 ntparms = TREE_VEC_LENGTH (inner_parms);
4522
4523 /* We check that each of the template parameters given in the
4524 partial specialization is used in the argument list to the
4525 specialization. For example:
4526
4527 template <class T> struct S;
4528 template <class T> struct S<T*>;
4529
4530 The second declaration is OK because `T*' uses the template
4531 parameter T, whereas
4532
4533 template <class T> struct S<int>;
4534
4535 is no good. Even trickier is:
4536
4537 template <class T>
4538 struct S1
4539 {
4540 template <class U>
4541 struct S2;
4542 template <class U>
4543 struct S2<T>;
4544 };
4545
4546 The S2<T> declaration is actually invalid; it is a
4547 full-specialization. Of course,
4548
4549 template <class U>
4550 struct S2<T (*)(U)>;
4551
4552 or some such would have been OK. */
4553 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4554 tpd.parms = XALLOCAVEC (int, ntparms);
4555 memset (tpd.parms, 0, sizeof (int) * ntparms);
4556
4557 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4558 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4559 for (i = 0; i < nargs; ++i)
4560 {
4561 tpd.current_arg = i;
4562 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4563 &mark_template_parm,
4564 &tpd,
4565 NULL,
4566 /*include_nondeduced_p=*/false);
4567 }
4568 for (i = 0; i < ntparms; ++i)
4569 if (tpd.parms[i] == 0)
4570 {
4571 /* One of the template parms was not used in a deduced context in the
4572 specialization. */
4573 if (!did_error_intro)
4574 {
4575 error ("template parameters not deducible in "
4576 "partial specialization:");
4577 did_error_intro = true;
4578 }
4579
4580 inform (input_location, " %qD",
4581 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4582 }
4583
4584 if (did_error_intro)
4585 return error_mark_node;
4586
4587 /* [temp.class.spec]
4588
4589 The argument list of the specialization shall not be identical to
4590 the implicit argument list of the primary template. */
4591 tree main_args
4592 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4593 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4594 && (!flag_concepts
4595 || !strictly_subsumes (current_template_constraints (),
4596 get_constraints (maintmpl))))
4597 {
4598 if (!flag_concepts)
4599 error ("partial specialization %q+D does not specialize "
4600 "any template arguments", decl);
4601 else
4602 error ("partial specialization %q+D does not specialize any "
4603 "template arguments and is not more constrained than", decl);
4604 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4605 }
4606
4607 /* A partial specialization that replaces multiple parameters of the
4608 primary template with a pack expansion is less specialized for those
4609 parameters. */
4610 if (nargs < DECL_NTPARMS (maintmpl))
4611 {
4612 error ("partial specialization is not more specialized than the "
4613 "primary template because it replaces multiple parameters "
4614 "with a pack expansion");
4615 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4616 /* Avoid crash in process_partial_specialization. */
4617 return decl;
4618 }
4619
4620 /* If we aren't in a dependent class, we can actually try deduction. */
4621 else if (tpd.level == 1
4622 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4623 {
4624 if (permerror (input_location, "partial specialization %qD is not "
4625 "more specialized than", decl))
4626 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4627 maintmpl);
4628 }
4629
4630 /* [temp.class.spec]
4631
4632 A partially specialized non-type argument expression shall not
4633 involve template parameters of the partial specialization except
4634 when the argument expression is a simple identifier.
4635
4636 The type of a template parameter corresponding to a specialized
4637 non-type argument shall not be dependent on a parameter of the
4638 specialization.
4639
4640 Also, we verify that pack expansions only occur at the
4641 end of the argument list. */
4642 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4643 tpd2.parms = 0;
4644 for (i = 0; i < nargs; ++i)
4645 {
4646 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4647 tree arg = TREE_VEC_ELT (inner_args, i);
4648 tree packed_args = NULL_TREE;
4649 int j, len = 1;
4650
4651 if (ARGUMENT_PACK_P (arg))
4652 {
4653 /* Extract the arguments from the argument pack. We'll be
4654 iterating over these in the following loop. */
4655 packed_args = ARGUMENT_PACK_ARGS (arg);
4656 len = TREE_VEC_LENGTH (packed_args);
4657 }
4658
4659 for (j = 0; j < len; j++)
4660 {
4661 if (packed_args)
4662 /* Get the Jth argument in the parameter pack. */
4663 arg = TREE_VEC_ELT (packed_args, j);
4664
4665 if (PACK_EXPANSION_P (arg))
4666 {
4667 /* Pack expansions must come at the end of the
4668 argument list. */
4669 if ((packed_args && j < len - 1)
4670 || (!packed_args && i < nargs - 1))
4671 {
4672 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4673 error ("parameter pack argument %qE must be at the "
4674 "end of the template argument list", arg);
4675 else
4676 error ("parameter pack argument %qT must be at the "
4677 "end of the template argument list", arg);
4678 }
4679 }
4680
4681 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4682 /* We only care about the pattern. */
4683 arg = PACK_EXPANSION_PATTERN (arg);
4684
4685 if (/* These first two lines are the `non-type' bit. */
4686 !TYPE_P (arg)
4687 && TREE_CODE (arg) != TEMPLATE_DECL
4688 /* This next two lines are the `argument expression is not just a
4689 simple identifier' condition and also the `specialized
4690 non-type argument' bit. */
4691 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4692 && !(REFERENCE_REF_P (arg)
4693 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4694 {
4695 if ((!packed_args && tpd.arg_uses_template_parms[i])
4696 || (packed_args && uses_template_parms (arg)))
4697 error ("template argument %qE involves template parameter(s)",
4698 arg);
4699 else
4700 {
4701 /* Look at the corresponding template parameter,
4702 marking which template parameters its type depends
4703 upon. */
4704 tree type = TREE_TYPE (parm);
4705
4706 if (!tpd2.parms)
4707 {
4708 /* We haven't yet initialized TPD2. Do so now. */
4709 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4710 /* The number of parameters here is the number in the
4711 main template, which, as checked in the assertion
4712 above, is NARGS. */
4713 tpd2.parms = XALLOCAVEC (int, nargs);
4714 tpd2.level =
4715 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4716 }
4717
4718 /* Mark the template parameters. But this time, we're
4719 looking for the template parameters of the main
4720 template, not in the specialization. */
4721 tpd2.current_arg = i;
4722 tpd2.arg_uses_template_parms[i] = 0;
4723 memset (tpd2.parms, 0, sizeof (int) * nargs);
4724 for_each_template_parm (type,
4725 &mark_template_parm,
4726 &tpd2,
4727 NULL,
4728 /*include_nondeduced_p=*/false);
4729
4730 if (tpd2.arg_uses_template_parms [i])
4731 {
4732 /* The type depended on some template parameters.
4733 If they are fully specialized in the
4734 specialization, that's OK. */
4735 int j;
4736 int count = 0;
4737 for (j = 0; j < nargs; ++j)
4738 if (tpd2.parms[j] != 0
4739 && tpd.arg_uses_template_parms [j])
4740 ++count;
4741 if (count != 0)
4742 error_n (input_location, count,
4743 "type %qT of template argument %qE depends "
4744 "on a template parameter",
4745 "type %qT of template argument %qE depends "
4746 "on template parameters",
4747 type,
4748 arg);
4749 }
4750 }
4751 }
4752 }
4753 }
4754
4755 /* We should only get here once. */
4756 if (TREE_CODE (decl) == TYPE_DECL)
4757 gcc_assert (!COMPLETE_TYPE_P (type));
4758
4759 // Build the template decl.
4760 tree tmpl = build_template_decl (decl, current_template_parms,
4761 DECL_MEMBER_TEMPLATE_P (maintmpl));
4762 TREE_TYPE (tmpl) = type;
4763 DECL_TEMPLATE_RESULT (tmpl) = decl;
4764 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4765 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4766 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4767
4768 /* Give template template parms a DECL_CONTEXT of the template
4769 for which they are a parameter. */
4770 for (i = 0; i < ntparms; ++i)
4771 {
4772 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4773 if (TREE_CODE (parm) == TEMPLATE_DECL)
4774 DECL_CONTEXT (parm) = tmpl;
4775 }
4776
4777 if (VAR_P (decl))
4778 /* We didn't register this in check_explicit_specialization so we could
4779 wait until the constraints were set. */
4780 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4781 else
4782 associate_classtype_constraints (type);
4783
4784 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4785 = tree_cons (specargs, tmpl,
4786 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4787 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4788
4789 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4790 inst = TREE_CHAIN (inst))
4791 {
4792 tree instance = TREE_VALUE (inst);
4793 if (TYPE_P (instance)
4794 ? (COMPLETE_TYPE_P (instance)
4795 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4796 : DECL_TEMPLATE_INSTANTIATION (instance))
4797 {
4798 tree spec = most_specialized_partial_spec (instance, tf_none);
4799 tree inst_decl = (DECL_P (instance)
4800 ? instance : TYPE_NAME (instance));
4801 if (!spec)
4802 /* OK */;
4803 else if (spec == error_mark_node)
4804 permerror (input_location,
4805 "declaration of %qD ambiguates earlier template "
4806 "instantiation for %qD", decl, inst_decl);
4807 else if (TREE_VALUE (spec) == tmpl)
4808 permerror (input_location,
4809 "partial specialization of %qD after instantiation "
4810 "of %qD", decl, inst_decl);
4811 }
4812 }
4813
4814 return decl;
4815 }
4816
4817 /* PARM is a template parameter of some form; return the corresponding
4818 TEMPLATE_PARM_INDEX. */
4819
4820 static tree
4821 get_template_parm_index (tree parm)
4822 {
4823 if (TREE_CODE (parm) == PARM_DECL
4824 || TREE_CODE (parm) == CONST_DECL)
4825 parm = DECL_INITIAL (parm);
4826 else if (TREE_CODE (parm) == TYPE_DECL
4827 || TREE_CODE (parm) == TEMPLATE_DECL)
4828 parm = TREE_TYPE (parm);
4829 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4830 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4831 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4832 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4833 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4834 return parm;
4835 }
4836
4837 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4838 parameter packs used by the template parameter PARM. */
4839
4840 static void
4841 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4842 {
4843 /* A type parm can't refer to another parm. */
4844 if (TREE_CODE (parm) == TYPE_DECL)
4845 return;
4846 else if (TREE_CODE (parm) == PARM_DECL)
4847 {
4848 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4849 ppd, ppd->visited);
4850 return;
4851 }
4852
4853 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4854
4855 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4856 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4857 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4858 }
4859
4860 /* PARM is a template parameter pack. Return any parameter packs used in
4861 its type or the type of any of its template parameters. If there are
4862 any such packs, it will be instantiated into a fixed template parameter
4863 list by partial instantiation rather than be fully deduced. */
4864
4865 tree
4866 fixed_parameter_pack_p (tree parm)
4867 {
4868 /* This can only be true in a member template. */
4869 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4870 return NULL_TREE;
4871 /* This can only be true for a parameter pack. */
4872 if (!template_parameter_pack_p (parm))
4873 return NULL_TREE;
4874 /* A type parm can't refer to another parm. */
4875 if (TREE_CODE (parm) == TYPE_DECL)
4876 return NULL_TREE;
4877
4878 tree parameter_packs = NULL_TREE;
4879 struct find_parameter_pack_data ppd;
4880 ppd.parameter_packs = &parameter_packs;
4881 ppd.visited = new hash_set<tree>;
4882 ppd.type_pack_expansion_p = false;
4883
4884 fixed_parameter_pack_p_1 (parm, &ppd);
4885
4886 delete ppd.visited;
4887 return parameter_packs;
4888 }
4889
4890 /* Check that a template declaration's use of default arguments and
4891 parameter packs is not invalid. Here, PARMS are the template
4892 parameters. IS_PRIMARY is true if DECL is the thing declared by
4893 a primary template. IS_PARTIAL is true if DECL is a partial
4894 specialization.
4895
4896 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4897 declaration (but not a definition); 1 indicates a declaration, 2
4898 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4899 emitted for extraneous default arguments.
4900
4901 Returns TRUE if there were no errors found, FALSE otherwise. */
4902
4903 bool
4904 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4905 bool is_partial, int is_friend_decl)
4906 {
4907 const char *msg;
4908 int last_level_to_check;
4909 tree parm_level;
4910 bool no_errors = true;
4911
4912 /* [temp.param]
4913
4914 A default template-argument shall not be specified in a
4915 function template declaration or a function template definition, nor
4916 in the template-parameter-list of the definition of a member of a
4917 class template. */
4918
4919 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4920 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4921 /* You can't have a function template declaration in a local
4922 scope, nor you can you define a member of a class template in a
4923 local scope. */
4924 return true;
4925
4926 if ((TREE_CODE (decl) == TYPE_DECL
4927 && TREE_TYPE (decl)
4928 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4929 || (TREE_CODE (decl) == FUNCTION_DECL
4930 && LAMBDA_FUNCTION_P (decl)))
4931 /* A lambda doesn't have an explicit declaration; don't complain
4932 about the parms of the enclosing class. */
4933 return true;
4934
4935 if (current_class_type
4936 && !TYPE_BEING_DEFINED (current_class_type)
4937 && DECL_LANG_SPECIFIC (decl)
4938 && DECL_DECLARES_FUNCTION_P (decl)
4939 /* If this is either a friend defined in the scope of the class
4940 or a member function. */
4941 && (DECL_FUNCTION_MEMBER_P (decl)
4942 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4943 : DECL_FRIEND_CONTEXT (decl)
4944 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4945 : false)
4946 /* And, if it was a member function, it really was defined in
4947 the scope of the class. */
4948 && (!DECL_FUNCTION_MEMBER_P (decl)
4949 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4950 /* We already checked these parameters when the template was
4951 declared, so there's no need to do it again now. This function
4952 was defined in class scope, but we're processing its body now
4953 that the class is complete. */
4954 return true;
4955
4956 /* Core issue 226 (C++0x only): the following only applies to class
4957 templates. */
4958 if (is_primary
4959 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4960 {
4961 /* [temp.param]
4962
4963 If a template-parameter has a default template-argument, all
4964 subsequent template-parameters shall have a default
4965 template-argument supplied. */
4966 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4967 {
4968 tree inner_parms = TREE_VALUE (parm_level);
4969 int ntparms = TREE_VEC_LENGTH (inner_parms);
4970 int seen_def_arg_p = 0;
4971 int i;
4972
4973 for (i = 0; i < ntparms; ++i)
4974 {
4975 tree parm = TREE_VEC_ELT (inner_parms, i);
4976
4977 if (parm == error_mark_node)
4978 continue;
4979
4980 if (TREE_PURPOSE (parm))
4981 seen_def_arg_p = 1;
4982 else if (seen_def_arg_p
4983 && !template_parameter_pack_p (TREE_VALUE (parm)))
4984 {
4985 error ("no default argument for %qD", TREE_VALUE (parm));
4986 /* For better subsequent error-recovery, we indicate that
4987 there should have been a default argument. */
4988 TREE_PURPOSE (parm) = error_mark_node;
4989 no_errors = false;
4990 }
4991 else if (!is_partial
4992 && !is_friend_decl
4993 /* Don't complain about an enclosing partial
4994 specialization. */
4995 && parm_level == parms
4996 && TREE_CODE (decl) == TYPE_DECL
4997 && i < ntparms - 1
4998 && template_parameter_pack_p (TREE_VALUE (parm))
4999 /* A fixed parameter pack will be partially
5000 instantiated into a fixed length list. */
5001 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5002 {
5003 /* A primary class template can only have one
5004 parameter pack, at the end of the template
5005 parameter list. */
5006
5007 error ("parameter pack %q+D must be at the end of the"
5008 " template parameter list", TREE_VALUE (parm));
5009
5010 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5011 = error_mark_node;
5012 no_errors = false;
5013 }
5014 }
5015 }
5016 }
5017
5018 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5019 || is_partial
5020 || !is_primary
5021 || is_friend_decl)
5022 /* For an ordinary class template, default template arguments are
5023 allowed at the innermost level, e.g.:
5024 template <class T = int>
5025 struct S {};
5026 but, in a partial specialization, they're not allowed even
5027 there, as we have in [temp.class.spec]:
5028
5029 The template parameter list of a specialization shall not
5030 contain default template argument values.
5031
5032 So, for a partial specialization, or for a function template
5033 (in C++98/C++03), we look at all of them. */
5034 ;
5035 else
5036 /* But, for a primary class template that is not a partial
5037 specialization we look at all template parameters except the
5038 innermost ones. */
5039 parms = TREE_CHAIN (parms);
5040
5041 /* Figure out what error message to issue. */
5042 if (is_friend_decl == 2)
5043 msg = G_("default template arguments may not be used in function template "
5044 "friend re-declaration");
5045 else if (is_friend_decl)
5046 msg = G_("default template arguments may not be used in function template "
5047 "friend declarations");
5048 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5049 msg = G_("default template arguments may not be used in function templates "
5050 "without -std=c++11 or -std=gnu++11");
5051 else if (is_partial)
5052 msg = G_("default template arguments may not be used in "
5053 "partial specializations");
5054 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5055 msg = G_("default argument for template parameter for class enclosing %qD");
5056 else
5057 /* Per [temp.param]/9, "A default template-argument shall not be
5058 specified in the template-parameter-lists of the definition of
5059 a member of a class template that appears outside of the member's
5060 class.", thus if we aren't handling a member of a class template
5061 there is no need to examine the parameters. */
5062 return true;
5063
5064 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5065 /* If we're inside a class definition, there's no need to
5066 examine the parameters to the class itself. On the one
5067 hand, they will be checked when the class is defined, and,
5068 on the other, default arguments are valid in things like:
5069 template <class T = double>
5070 struct S { template <class U> void f(U); };
5071 Here the default argument for `S' has no bearing on the
5072 declaration of `f'. */
5073 last_level_to_check = template_class_depth (current_class_type) + 1;
5074 else
5075 /* Check everything. */
5076 last_level_to_check = 0;
5077
5078 for (parm_level = parms;
5079 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5080 parm_level = TREE_CHAIN (parm_level))
5081 {
5082 tree inner_parms = TREE_VALUE (parm_level);
5083 int i;
5084 int ntparms;
5085
5086 ntparms = TREE_VEC_LENGTH (inner_parms);
5087 for (i = 0; i < ntparms; ++i)
5088 {
5089 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5090 continue;
5091
5092 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5093 {
5094 if (msg)
5095 {
5096 no_errors = false;
5097 if (is_friend_decl == 2)
5098 return no_errors;
5099
5100 error (msg, decl);
5101 msg = 0;
5102 }
5103
5104 /* Clear out the default argument so that we are not
5105 confused later. */
5106 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5107 }
5108 }
5109
5110 /* At this point, if we're still interested in issuing messages,
5111 they must apply to classes surrounding the object declared. */
5112 if (msg)
5113 msg = G_("default argument for template parameter for class "
5114 "enclosing %qD");
5115 }
5116
5117 return no_errors;
5118 }
5119
5120 /* Worker for push_template_decl_real, called via
5121 for_each_template_parm. DATA is really an int, indicating the
5122 level of the parameters we are interested in. If T is a template
5123 parameter of that level, return nonzero. */
5124
5125 static int
5126 template_parm_this_level_p (tree t, void* data)
5127 {
5128 int this_level = *(int *)data;
5129 int level;
5130
5131 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5132 level = TEMPLATE_PARM_LEVEL (t);
5133 else
5134 level = TEMPLATE_TYPE_LEVEL (t);
5135 return level == this_level;
5136 }
5137
5138 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5139 DATA is really an int, indicating the innermost outer level of parameters.
5140 If T is a template parameter of that level or further out, return
5141 nonzero. */
5142
5143 static int
5144 template_parm_outer_level (tree t, void *data)
5145 {
5146 int this_level = *(int *)data;
5147 int level;
5148
5149 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5150 level = TEMPLATE_PARM_LEVEL (t);
5151 else
5152 level = TEMPLATE_TYPE_LEVEL (t);
5153 return level <= this_level;
5154 }
5155
5156 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5157 parameters given by current_template_args, or reuses a
5158 previously existing one, if appropriate. Returns the DECL, or an
5159 equivalent one, if it is replaced via a call to duplicate_decls.
5160
5161 If IS_FRIEND is true, DECL is a friend declaration. */
5162
5163 tree
5164 push_template_decl_real (tree decl, bool is_friend)
5165 {
5166 tree tmpl;
5167 tree args;
5168 tree info;
5169 tree ctx;
5170 bool is_primary;
5171 bool is_partial;
5172 int new_template_p = 0;
5173 /* True if the template is a member template, in the sense of
5174 [temp.mem]. */
5175 bool member_template_p = false;
5176
5177 if (decl == error_mark_node || !current_template_parms)
5178 return error_mark_node;
5179
5180 /* See if this is a partial specialization. */
5181 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5182 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5183 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5184 || (VAR_P (decl)
5185 && DECL_LANG_SPECIFIC (decl)
5186 && DECL_TEMPLATE_SPECIALIZATION (decl)
5187 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5188
5189 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5190 is_friend = true;
5191
5192 if (is_friend)
5193 /* For a friend, we want the context of the friend function, not
5194 the type of which it is a friend. */
5195 ctx = CP_DECL_CONTEXT (decl);
5196 else if (CP_DECL_CONTEXT (decl)
5197 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5198 /* In the case of a virtual function, we want the class in which
5199 it is defined. */
5200 ctx = CP_DECL_CONTEXT (decl);
5201 else
5202 /* Otherwise, if we're currently defining some class, the DECL
5203 is assumed to be a member of the class. */
5204 ctx = current_scope ();
5205
5206 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5207 ctx = NULL_TREE;
5208
5209 if (!DECL_CONTEXT (decl))
5210 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5211
5212 /* See if this is a primary template. */
5213 if (is_friend && ctx
5214 && uses_template_parms_level (ctx, processing_template_decl))
5215 /* A friend template that specifies a class context, i.e.
5216 template <typename T> friend void A<T>::f();
5217 is not primary. */
5218 is_primary = false;
5219 else if (TREE_CODE (decl) == TYPE_DECL
5220 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5221 is_primary = false;
5222 else
5223 is_primary = template_parm_scope_p ();
5224
5225 if (is_primary)
5226 {
5227 warning (OPT_Wtemplates, "template %qD declared", decl);
5228
5229 if (DECL_CLASS_SCOPE_P (decl))
5230 member_template_p = true;
5231 if (TREE_CODE (decl) == TYPE_DECL
5232 && anon_aggrname_p (DECL_NAME (decl)))
5233 {
5234 error ("template class without a name");
5235 return error_mark_node;
5236 }
5237 else if (TREE_CODE (decl) == FUNCTION_DECL)
5238 {
5239 if (member_template_p)
5240 {
5241 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5242 error ("member template %qD may not have virt-specifiers", decl);
5243 }
5244 if (DECL_DESTRUCTOR_P (decl))
5245 {
5246 /* [temp.mem]
5247
5248 A destructor shall not be a member template. */
5249 error ("destructor %qD declared as member template", decl);
5250 return error_mark_node;
5251 }
5252 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5253 && (!prototype_p (TREE_TYPE (decl))
5254 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5255 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5256 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5257 == void_list_node)))
5258 {
5259 /* [basic.stc.dynamic.allocation]
5260
5261 An allocation function can be a function
5262 template. ... Template allocation functions shall
5263 have two or more parameters. */
5264 error ("invalid template declaration of %qD", decl);
5265 return error_mark_node;
5266 }
5267 }
5268 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5269 && CLASS_TYPE_P (TREE_TYPE (decl)))
5270 {
5271 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5272 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5273 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5274 {
5275 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5276 if (TREE_CODE (t) == TYPE_DECL)
5277 t = TREE_TYPE (t);
5278 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5279 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5280 }
5281 }
5282 else if (TREE_CODE (decl) == TYPE_DECL
5283 && TYPE_DECL_ALIAS_P (decl))
5284 /* alias-declaration */
5285 gcc_assert (!DECL_ARTIFICIAL (decl));
5286 else if (VAR_P (decl))
5287 /* C++14 variable template. */;
5288 else
5289 {
5290 error ("template declaration of %q#D", decl);
5291 return error_mark_node;
5292 }
5293 }
5294
5295 /* Check to see that the rules regarding the use of default
5296 arguments are not being violated. */
5297 check_default_tmpl_args (decl, current_template_parms,
5298 is_primary, is_partial, /*is_friend_decl=*/0);
5299
5300 /* Ensure that there are no parameter packs in the type of this
5301 declaration that have not been expanded. */
5302 if (TREE_CODE (decl) == FUNCTION_DECL)
5303 {
5304 /* Check each of the arguments individually to see if there are
5305 any bare parameter packs. */
5306 tree type = TREE_TYPE (decl);
5307 tree arg = DECL_ARGUMENTS (decl);
5308 tree argtype = TYPE_ARG_TYPES (type);
5309
5310 while (arg && argtype)
5311 {
5312 if (!DECL_PACK_P (arg)
5313 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5314 {
5315 /* This is a PARM_DECL that contains unexpanded parameter
5316 packs. We have already complained about this in the
5317 check_for_bare_parameter_packs call, so just replace
5318 these types with ERROR_MARK_NODE. */
5319 TREE_TYPE (arg) = error_mark_node;
5320 TREE_VALUE (argtype) = error_mark_node;
5321 }
5322
5323 arg = DECL_CHAIN (arg);
5324 argtype = TREE_CHAIN (argtype);
5325 }
5326
5327 /* Check for bare parameter packs in the return type and the
5328 exception specifiers. */
5329 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5330 /* Errors were already issued, set return type to int
5331 as the frontend doesn't expect error_mark_node as
5332 the return type. */
5333 TREE_TYPE (type) = integer_type_node;
5334 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5335 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5336 }
5337 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5338 && TYPE_DECL_ALIAS_P (decl))
5339 ? DECL_ORIGINAL_TYPE (decl)
5340 : TREE_TYPE (decl)))
5341 {
5342 TREE_TYPE (decl) = error_mark_node;
5343 return error_mark_node;
5344 }
5345
5346 if (is_partial)
5347 return process_partial_specialization (decl);
5348
5349 args = current_template_args ();
5350
5351 if (!ctx
5352 || TREE_CODE (ctx) == FUNCTION_DECL
5353 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5354 || (TREE_CODE (decl) == TYPE_DECL
5355 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5356 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5357 {
5358 if (DECL_LANG_SPECIFIC (decl)
5359 && DECL_TEMPLATE_INFO (decl)
5360 && DECL_TI_TEMPLATE (decl))
5361 tmpl = DECL_TI_TEMPLATE (decl);
5362 /* If DECL is a TYPE_DECL for a class-template, then there won't
5363 be DECL_LANG_SPECIFIC. The information equivalent to
5364 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5365 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5366 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5367 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5368 {
5369 /* Since a template declaration already existed for this
5370 class-type, we must be redeclaring it here. Make sure
5371 that the redeclaration is valid. */
5372 redeclare_class_template (TREE_TYPE (decl),
5373 current_template_parms,
5374 current_template_constraints ());
5375 /* We don't need to create a new TEMPLATE_DECL; just use the
5376 one we already had. */
5377 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5378 }
5379 else
5380 {
5381 tmpl = build_template_decl (decl, current_template_parms,
5382 member_template_p);
5383 new_template_p = 1;
5384
5385 if (DECL_LANG_SPECIFIC (decl)
5386 && DECL_TEMPLATE_SPECIALIZATION (decl))
5387 {
5388 /* A specialization of a member template of a template
5389 class. */
5390 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5391 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5392 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5393 }
5394 }
5395 }
5396 else
5397 {
5398 tree a, t, current, parms;
5399 int i;
5400 tree tinfo = get_template_info (decl);
5401
5402 if (!tinfo)
5403 {
5404 error ("template definition of non-template %q#D", decl);
5405 return error_mark_node;
5406 }
5407
5408 tmpl = TI_TEMPLATE (tinfo);
5409
5410 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5411 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5412 && DECL_TEMPLATE_SPECIALIZATION (decl)
5413 && DECL_MEMBER_TEMPLATE_P (tmpl))
5414 {
5415 tree new_tmpl;
5416
5417 /* The declaration is a specialization of a member
5418 template, declared outside the class. Therefore, the
5419 innermost template arguments will be NULL, so we
5420 replace them with the arguments determined by the
5421 earlier call to check_explicit_specialization. */
5422 args = DECL_TI_ARGS (decl);
5423
5424 new_tmpl
5425 = build_template_decl (decl, current_template_parms,
5426 member_template_p);
5427 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5428 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5429 DECL_TI_TEMPLATE (decl) = new_tmpl;
5430 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5431 DECL_TEMPLATE_INFO (new_tmpl)
5432 = build_template_info (tmpl, args);
5433
5434 register_specialization (new_tmpl,
5435 most_general_template (tmpl),
5436 args,
5437 is_friend, 0);
5438 return decl;
5439 }
5440
5441 /* Make sure the template headers we got make sense. */
5442
5443 parms = DECL_TEMPLATE_PARMS (tmpl);
5444 i = TMPL_PARMS_DEPTH (parms);
5445 if (TMPL_ARGS_DEPTH (args) != i)
5446 {
5447 error ("expected %d levels of template parms for %q#D, got %d",
5448 i, decl, TMPL_ARGS_DEPTH (args));
5449 DECL_INTERFACE_KNOWN (decl) = 1;
5450 return error_mark_node;
5451 }
5452 else
5453 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5454 {
5455 a = TMPL_ARGS_LEVEL (args, i);
5456 t = INNERMOST_TEMPLATE_PARMS (parms);
5457
5458 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5459 {
5460 if (current == decl)
5461 error ("got %d template parameters for %q#D",
5462 TREE_VEC_LENGTH (a), decl);
5463 else
5464 error ("got %d template parameters for %q#T",
5465 TREE_VEC_LENGTH (a), current);
5466 error (" but %d required", TREE_VEC_LENGTH (t));
5467 /* Avoid crash in import_export_decl. */
5468 DECL_INTERFACE_KNOWN (decl) = 1;
5469 return error_mark_node;
5470 }
5471
5472 if (current == decl)
5473 current = ctx;
5474 else if (current == NULL_TREE)
5475 /* Can happen in erroneous input. */
5476 break;
5477 else
5478 current = get_containing_scope (current);
5479 }
5480
5481 /* Check that the parms are used in the appropriate qualifying scopes
5482 in the declarator. */
5483 if (!comp_template_args
5484 (TI_ARGS (tinfo),
5485 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5486 {
5487 error ("\
5488 template arguments to %qD do not match original template %qD",
5489 decl, DECL_TEMPLATE_RESULT (tmpl));
5490 if (!uses_template_parms (TI_ARGS (tinfo)))
5491 inform (input_location, "use template<> for an explicit specialization");
5492 /* Avoid crash in import_export_decl. */
5493 DECL_INTERFACE_KNOWN (decl) = 1;
5494 return error_mark_node;
5495 }
5496 }
5497
5498 DECL_TEMPLATE_RESULT (tmpl) = decl;
5499 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5500
5501 /* Push template declarations for global functions and types. Note
5502 that we do not try to push a global template friend declared in a
5503 template class; such a thing may well depend on the template
5504 parameters of the class. */
5505 if (new_template_p && !ctx
5506 && !(is_friend && template_class_depth (current_class_type) > 0))
5507 {
5508 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5509 if (tmpl == error_mark_node)
5510 return error_mark_node;
5511
5512 /* Hide template friend classes that haven't been declared yet. */
5513 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5514 {
5515 DECL_ANTICIPATED (tmpl) = 1;
5516 DECL_FRIEND_P (tmpl) = 1;
5517 }
5518 }
5519
5520 if (is_primary)
5521 {
5522 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5523 int i;
5524
5525 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5526 if (DECL_CONV_FN_P (tmpl))
5527 {
5528 int depth = TMPL_PARMS_DEPTH (parms);
5529
5530 /* It is a conversion operator. See if the type converted to
5531 depends on innermost template operands. */
5532
5533 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5534 depth))
5535 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5536 }
5537
5538 /* Give template template parms a DECL_CONTEXT of the template
5539 for which they are a parameter. */
5540 parms = INNERMOST_TEMPLATE_PARMS (parms);
5541 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5542 {
5543 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5544 if (TREE_CODE (parm) == TEMPLATE_DECL)
5545 DECL_CONTEXT (parm) = tmpl;
5546 }
5547
5548 if (TREE_CODE (decl) == TYPE_DECL
5549 && TYPE_DECL_ALIAS_P (decl)
5550 && complex_alias_template_p (tmpl))
5551 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5552 }
5553
5554 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5555 back to its most general template. If TMPL is a specialization,
5556 ARGS may only have the innermost set of arguments. Add the missing
5557 argument levels if necessary. */
5558 if (DECL_TEMPLATE_INFO (tmpl))
5559 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5560
5561 info = build_template_info (tmpl, args);
5562
5563 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5564 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5565 else
5566 {
5567 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5568 retrofit_lang_decl (decl);
5569 if (DECL_LANG_SPECIFIC (decl))
5570 DECL_TEMPLATE_INFO (decl) = info;
5571 }
5572
5573 if (flag_implicit_templates
5574 && !is_friend
5575 && TREE_PUBLIC (decl)
5576 && VAR_OR_FUNCTION_DECL_P (decl))
5577 /* Set DECL_COMDAT on template instantiations; if we force
5578 them to be emitted by explicit instantiation or -frepo,
5579 mark_needed will tell cgraph to do the right thing. */
5580 DECL_COMDAT (decl) = true;
5581
5582 return DECL_TEMPLATE_RESULT (tmpl);
5583 }
5584
5585 tree
5586 push_template_decl (tree decl)
5587 {
5588 return push_template_decl_real (decl, false);
5589 }
5590
5591 /* FN is an inheriting constructor that inherits from the constructor
5592 template INHERITED; turn FN into a constructor template with a matching
5593 template header. */
5594
5595 tree
5596 add_inherited_template_parms (tree fn, tree inherited)
5597 {
5598 tree inner_parms
5599 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5600 inner_parms = copy_node (inner_parms);
5601 tree parms
5602 = tree_cons (size_int (processing_template_decl + 1),
5603 inner_parms, current_template_parms);
5604 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5605 tree args = template_parms_to_args (parms);
5606 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5607 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5608 DECL_TEMPLATE_RESULT (tmpl) = fn;
5609 DECL_ARTIFICIAL (tmpl) = true;
5610 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5611 return tmpl;
5612 }
5613
5614 /* Called when a class template TYPE is redeclared with the indicated
5615 template PARMS, e.g.:
5616
5617 template <class T> struct S;
5618 template <class T> struct S {}; */
5619
5620 bool
5621 redeclare_class_template (tree type, tree parms, tree cons)
5622 {
5623 tree tmpl;
5624 tree tmpl_parms;
5625 int i;
5626
5627 if (!TYPE_TEMPLATE_INFO (type))
5628 {
5629 error ("%qT is not a template type", type);
5630 return false;
5631 }
5632
5633 tmpl = TYPE_TI_TEMPLATE (type);
5634 if (!PRIMARY_TEMPLATE_P (tmpl))
5635 /* The type is nested in some template class. Nothing to worry
5636 about here; there are no new template parameters for the nested
5637 type. */
5638 return true;
5639
5640 if (!parms)
5641 {
5642 error ("template specifiers not specified in declaration of %qD",
5643 tmpl);
5644 return false;
5645 }
5646
5647 parms = INNERMOST_TEMPLATE_PARMS (parms);
5648 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5649
5650 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5651 {
5652 error_n (input_location, TREE_VEC_LENGTH (parms),
5653 "redeclared with %d template parameter",
5654 "redeclared with %d template parameters",
5655 TREE_VEC_LENGTH (parms));
5656 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5657 "previous declaration %qD used %d template parameter",
5658 "previous declaration %qD used %d template parameters",
5659 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5660 return false;
5661 }
5662
5663 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5664 {
5665 tree tmpl_parm;
5666 tree parm;
5667 tree tmpl_default;
5668 tree parm_default;
5669
5670 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5671 || TREE_VEC_ELT (parms, i) == error_mark_node)
5672 continue;
5673
5674 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5675 if (error_operand_p (tmpl_parm))
5676 return false;
5677
5678 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5679 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5680 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5681
5682 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5683 TEMPLATE_DECL. */
5684 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5685 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5686 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5687 || (TREE_CODE (tmpl_parm) != PARM_DECL
5688 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5689 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5690 || (TREE_CODE (tmpl_parm) == PARM_DECL
5691 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5692 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5693 {
5694 error ("template parameter %q+#D", tmpl_parm);
5695 error ("redeclared here as %q#D", parm);
5696 return false;
5697 }
5698
5699 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5700 {
5701 /* We have in [temp.param]:
5702
5703 A template-parameter may not be given default arguments
5704 by two different declarations in the same scope. */
5705 error_at (input_location, "redefinition of default argument for %q#D", parm);
5706 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5707 "original definition appeared here");
5708 return false;
5709 }
5710
5711 if (parm_default != NULL_TREE)
5712 /* Update the previous template parameters (which are the ones
5713 that will really count) with the new default value. */
5714 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5715 else if (tmpl_default != NULL_TREE)
5716 /* Update the new parameters, too; they'll be used as the
5717 parameters for any members. */
5718 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5719
5720 /* Give each template template parm in this redeclaration a
5721 DECL_CONTEXT of the template for which they are a parameter. */
5722 if (TREE_CODE (parm) == TEMPLATE_DECL)
5723 {
5724 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5725 DECL_CONTEXT (parm) = tmpl;
5726 }
5727 }
5728
5729 // Cannot redeclare a class template with a different set of constraints.
5730 if (!equivalent_constraints (get_constraints (tmpl), cons))
5731 {
5732 error_at (input_location, "redeclaration %q#D with different "
5733 "constraints", tmpl);
5734 inform (DECL_SOURCE_LOCATION (tmpl),
5735 "original declaration appeared here");
5736 }
5737
5738 return true;
5739 }
5740
5741 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5742 to be used when the caller has already checked
5743 (processing_template_decl
5744 && !instantiation_dependent_expression_p (expr)
5745 && potential_constant_expression (expr))
5746 and cleared processing_template_decl. */
5747
5748 tree
5749 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5750 {
5751 return tsubst_copy_and_build (expr,
5752 /*args=*/NULL_TREE,
5753 complain,
5754 /*in_decl=*/NULL_TREE,
5755 /*function_p=*/false,
5756 /*integral_constant_expression_p=*/true);
5757 }
5758
5759 /* Simplify EXPR if it is a non-dependent expression. Returns the
5760 (possibly simplified) expression. */
5761
5762 tree
5763 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5764 {
5765 if (expr == NULL_TREE)
5766 return NULL_TREE;
5767
5768 /* If we're in a template, but EXPR isn't value dependent, simplify
5769 it. We're supposed to treat:
5770
5771 template <typename T> void f(T[1 + 1]);
5772 template <typename T> void f(T[2]);
5773
5774 as two declarations of the same function, for example. */
5775 if (processing_template_decl
5776 && potential_nondependent_constant_expression (expr))
5777 {
5778 processing_template_decl_sentinel s;
5779 expr = instantiate_non_dependent_expr_internal (expr, complain);
5780 }
5781 return expr;
5782 }
5783
5784 tree
5785 instantiate_non_dependent_expr (tree expr)
5786 {
5787 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5788 }
5789
5790 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5791 an uninstantiated expression. */
5792
5793 tree
5794 instantiate_non_dependent_or_null (tree expr)
5795 {
5796 if (expr == NULL_TREE)
5797 return NULL_TREE;
5798 if (processing_template_decl)
5799 {
5800 if (!potential_nondependent_constant_expression (expr))
5801 expr = NULL_TREE;
5802 else
5803 {
5804 processing_template_decl_sentinel s;
5805 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5806 }
5807 }
5808 return expr;
5809 }
5810
5811 /* True iff T is a specialization of a variable template. */
5812
5813 bool
5814 variable_template_specialization_p (tree t)
5815 {
5816 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5817 return false;
5818 tree tmpl = DECL_TI_TEMPLATE (t);
5819 return variable_template_p (tmpl);
5820 }
5821
5822 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5823 template declaration, or a TYPE_DECL for an alias declaration. */
5824
5825 bool
5826 alias_type_or_template_p (tree t)
5827 {
5828 if (t == NULL_TREE)
5829 return false;
5830 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5831 || (TYPE_P (t)
5832 && TYPE_NAME (t)
5833 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5834 || DECL_ALIAS_TEMPLATE_P (t));
5835 }
5836
5837 /* Return TRUE iff T is a specialization of an alias template. */
5838
5839 bool
5840 alias_template_specialization_p (const_tree t)
5841 {
5842 /* It's an alias template specialization if it's an alias and its
5843 TYPE_NAME is a specialization of a primary template. */
5844 if (TYPE_ALIAS_P (t))
5845 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5846 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5847
5848 return false;
5849 }
5850
5851 /* An alias template is complex from a SFINAE perspective if a template-id
5852 using that alias can be ill-formed when the expansion is not, as with
5853 the void_t template. We determine this by checking whether the
5854 expansion for the alias template uses all its template parameters. */
5855
5856 struct uses_all_template_parms_data
5857 {
5858 int level;
5859 bool *seen;
5860 };
5861
5862 static int
5863 uses_all_template_parms_r (tree t, void *data_)
5864 {
5865 struct uses_all_template_parms_data &data
5866 = *(struct uses_all_template_parms_data*)data_;
5867 tree idx = get_template_parm_index (t);
5868
5869 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5870 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5871 return 0;
5872 }
5873
5874 static bool
5875 complex_alias_template_p (const_tree tmpl)
5876 {
5877 struct uses_all_template_parms_data data;
5878 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5879 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5880 data.level = TMPL_PARMS_DEPTH (parms);
5881 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5882 data.seen = XALLOCAVEC (bool, len);
5883 for (int i = 0; i < len; ++i)
5884 data.seen[i] = false;
5885
5886 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5887 for (int i = 0; i < len; ++i)
5888 if (!data.seen[i])
5889 return true;
5890 return false;
5891 }
5892
5893 /* Return TRUE iff T is a specialization of a complex alias template with
5894 dependent template-arguments. */
5895
5896 bool
5897 dependent_alias_template_spec_p (const_tree t)
5898 {
5899 if (!alias_template_specialization_p (t))
5900 return false;
5901
5902 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5903 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5904 return false;
5905
5906 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5907 if (!any_dependent_template_arguments_p (args))
5908 return false;
5909
5910 return true;
5911 }
5912
5913 /* Return the number of innermost template parameters in TMPL. */
5914
5915 static int
5916 num_innermost_template_parms (tree tmpl)
5917 {
5918 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5919 return TREE_VEC_LENGTH (parms);
5920 }
5921
5922 /* Return either TMPL or another template that it is equivalent to under DR
5923 1286: An alias that just changes the name of a template is equivalent to
5924 the other template. */
5925
5926 static tree
5927 get_underlying_template (tree tmpl)
5928 {
5929 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5930 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5931 {
5932 /* Determine if the alias is equivalent to an underlying template. */
5933 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5934 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
5935 if (!tinfo)
5936 break;
5937
5938 tree underlying = TI_TEMPLATE (tinfo);
5939 if (!PRIMARY_TEMPLATE_P (underlying)
5940 || (num_innermost_template_parms (tmpl)
5941 != num_innermost_template_parms (underlying)))
5942 break;
5943
5944 tree alias_args = INNERMOST_TEMPLATE_ARGS
5945 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5946 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
5947 break;
5948
5949 /* Alias is equivalent. Strip it and repeat. */
5950 tmpl = underlying;
5951 }
5952
5953 return tmpl;
5954 }
5955
5956 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5957 must be a reference-to-function or a pointer-to-function type, as specified
5958 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5959 and check that the resulting function has external linkage. */
5960
5961 static tree
5962 convert_nontype_argument_function (tree type, tree expr,
5963 tsubst_flags_t complain)
5964 {
5965 tree fns = expr;
5966 tree fn, fn_no_ptr;
5967 linkage_kind linkage;
5968
5969 fn = instantiate_type (type, fns, tf_none);
5970 if (fn == error_mark_node)
5971 return error_mark_node;
5972
5973 if (value_dependent_expression_p (fn))
5974 return fn;
5975
5976 fn_no_ptr = strip_fnptr_conv (fn);
5977 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5978 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5979 if (BASELINK_P (fn_no_ptr))
5980 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5981
5982 /* [temp.arg.nontype]/1
5983
5984 A template-argument for a non-type, non-template template-parameter
5985 shall be one of:
5986 [...]
5987 -- the address of an object or function with external [C++11: or
5988 internal] linkage. */
5989
5990 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5991 {
5992 if (complain & tf_error)
5993 {
5994 error ("%qE is not a valid template argument for type %qT",
5995 expr, type);
5996 if (TYPE_PTR_P (type))
5997 inform (input_location, "it must be the address of a function "
5998 "with external linkage");
5999 else
6000 inform (input_location, "it must be the name of a function with "
6001 "external linkage");
6002 }
6003 return NULL_TREE;
6004 }
6005
6006 linkage = decl_linkage (fn_no_ptr);
6007 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6008 {
6009 if (complain & tf_error)
6010 {
6011 if (cxx_dialect >= cxx11)
6012 error ("%qE is not a valid template argument for type %qT "
6013 "because %qD has no linkage",
6014 expr, type, fn_no_ptr);
6015 else
6016 error ("%qE is not a valid template argument for type %qT "
6017 "because %qD does not have external linkage",
6018 expr, type, fn_no_ptr);
6019 }
6020 return NULL_TREE;
6021 }
6022
6023 if (TREE_CODE (type) == REFERENCE_TYPE)
6024 fn = build_address (fn);
6025 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6026 fn = build_nop (type, fn);
6027
6028 return fn;
6029 }
6030
6031 /* Subroutine of convert_nontype_argument.
6032 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6033 Emit an error otherwise. */
6034
6035 static bool
6036 check_valid_ptrmem_cst_expr (tree type, tree expr,
6037 tsubst_flags_t complain)
6038 {
6039 STRIP_NOPS (expr);
6040 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
6041 return true;
6042 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6043 return true;
6044 if (processing_template_decl
6045 && TREE_CODE (expr) == ADDR_EXPR
6046 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6047 return true;
6048 if (complain & tf_error)
6049 {
6050 error ("%qE is not a valid template argument for type %qT",
6051 expr, type);
6052 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6053 }
6054 return false;
6055 }
6056
6057 /* Returns TRUE iff the address of OP is value-dependent.
6058
6059 14.6.2.4 [temp.dep.temp]:
6060 A non-integral non-type template-argument is dependent if its type is
6061 dependent or it has either of the following forms
6062 qualified-id
6063 & qualified-id
6064 and contains a nested-name-specifier which specifies a class-name that
6065 names a dependent type.
6066
6067 We generalize this to just say that the address of a member of a
6068 dependent class is value-dependent; the above doesn't cover the
6069 address of a static data member named with an unqualified-id. */
6070
6071 static bool
6072 has_value_dependent_address (tree op)
6073 {
6074 /* We could use get_inner_reference here, but there's no need;
6075 this is only relevant for template non-type arguments, which
6076 can only be expressed as &id-expression. */
6077 if (DECL_P (op))
6078 {
6079 tree ctx = CP_DECL_CONTEXT (op);
6080 if (TYPE_P (ctx) && dependent_type_p (ctx))
6081 return true;
6082 }
6083
6084 return false;
6085 }
6086
6087 /* The next set of functions are used for providing helpful explanatory
6088 diagnostics for failed overload resolution. Their messages should be
6089 indented by two spaces for consistency with the messages in
6090 call.c */
6091
6092 static int
6093 unify_success (bool /*explain_p*/)
6094 {
6095 return 0;
6096 }
6097
6098 static int
6099 unify_parameter_deduction_failure (bool explain_p, tree parm)
6100 {
6101 if (explain_p)
6102 inform (input_location,
6103 " couldn't deduce template parameter %qD", parm);
6104 return 1;
6105 }
6106
6107 static int
6108 unify_invalid (bool /*explain_p*/)
6109 {
6110 return 1;
6111 }
6112
6113 static int
6114 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6115 {
6116 if (explain_p)
6117 inform (input_location,
6118 " types %qT and %qT have incompatible cv-qualifiers",
6119 parm, arg);
6120 return 1;
6121 }
6122
6123 static int
6124 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6125 {
6126 if (explain_p)
6127 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6128 return 1;
6129 }
6130
6131 static int
6132 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6133 {
6134 if (explain_p)
6135 inform (input_location,
6136 " template parameter %qD is not a parameter pack, but "
6137 "argument %qD is",
6138 parm, arg);
6139 return 1;
6140 }
6141
6142 static int
6143 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6144 {
6145 if (explain_p)
6146 inform (input_location,
6147 " template argument %qE does not match "
6148 "pointer-to-member constant %qE",
6149 arg, parm);
6150 return 1;
6151 }
6152
6153 static int
6154 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6155 {
6156 if (explain_p)
6157 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6158 return 1;
6159 }
6160
6161 static int
6162 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6163 {
6164 if (explain_p)
6165 inform (input_location,
6166 " inconsistent parameter pack deduction with %qT and %qT",
6167 old_arg, new_arg);
6168 return 1;
6169 }
6170
6171 static int
6172 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6173 {
6174 if (explain_p)
6175 {
6176 if (TYPE_P (parm))
6177 inform (input_location,
6178 " deduced conflicting types for parameter %qT (%qT and %qT)",
6179 parm, first, second);
6180 else
6181 inform (input_location,
6182 " deduced conflicting values for non-type parameter "
6183 "%qE (%qE and %qE)", parm, first, second);
6184 }
6185 return 1;
6186 }
6187
6188 static int
6189 unify_vla_arg (bool explain_p, tree arg)
6190 {
6191 if (explain_p)
6192 inform (input_location,
6193 " variable-sized array type %qT is not "
6194 "a valid template argument",
6195 arg);
6196 return 1;
6197 }
6198
6199 static int
6200 unify_method_type_error (bool explain_p, tree arg)
6201 {
6202 if (explain_p)
6203 inform (input_location,
6204 " member function type %qT is not a valid template argument",
6205 arg);
6206 return 1;
6207 }
6208
6209 static int
6210 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6211 {
6212 if (explain_p)
6213 {
6214 if (least_p)
6215 inform_n (input_location, wanted,
6216 " candidate expects at least %d argument, %d provided",
6217 " candidate expects at least %d arguments, %d provided",
6218 wanted, have);
6219 else
6220 inform_n (input_location, wanted,
6221 " candidate expects %d argument, %d provided",
6222 " candidate expects %d arguments, %d provided",
6223 wanted, have);
6224 }
6225 return 1;
6226 }
6227
6228 static int
6229 unify_too_many_arguments (bool explain_p, int have, int wanted)
6230 {
6231 return unify_arity (explain_p, have, wanted);
6232 }
6233
6234 static int
6235 unify_too_few_arguments (bool explain_p, int have, int wanted,
6236 bool least_p = false)
6237 {
6238 return unify_arity (explain_p, have, wanted, least_p);
6239 }
6240
6241 static int
6242 unify_arg_conversion (bool explain_p, tree to_type,
6243 tree from_type, tree arg)
6244 {
6245 if (explain_p)
6246 inform (EXPR_LOC_OR_LOC (arg, input_location),
6247 " cannot convert %qE (type %qT) to type %qT",
6248 arg, from_type, to_type);
6249 return 1;
6250 }
6251
6252 static int
6253 unify_no_common_base (bool explain_p, enum template_base_result r,
6254 tree parm, tree arg)
6255 {
6256 if (explain_p)
6257 switch (r)
6258 {
6259 case tbr_ambiguous_baseclass:
6260 inform (input_location, " %qT is an ambiguous base class of %qT",
6261 parm, arg);
6262 break;
6263 default:
6264 inform (input_location, " %qT is not derived from %qT", arg, parm);
6265 break;
6266 }
6267 return 1;
6268 }
6269
6270 static int
6271 unify_inconsistent_template_template_parameters (bool explain_p)
6272 {
6273 if (explain_p)
6274 inform (input_location,
6275 " template parameters of a template template argument are "
6276 "inconsistent with other deduced template arguments");
6277 return 1;
6278 }
6279
6280 static int
6281 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6282 {
6283 if (explain_p)
6284 inform (input_location,
6285 " can't deduce a template for %qT from non-template type %qT",
6286 parm, arg);
6287 return 1;
6288 }
6289
6290 static int
6291 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6292 {
6293 if (explain_p)
6294 inform (input_location,
6295 " template argument %qE does not match %qE", arg, parm);
6296 return 1;
6297 }
6298
6299 static int
6300 unify_overload_resolution_failure (bool explain_p, tree arg)
6301 {
6302 if (explain_p)
6303 inform (input_location,
6304 " could not resolve address from overloaded function %qE",
6305 arg);
6306 return 1;
6307 }
6308
6309 /* Attempt to convert the non-type template parameter EXPR to the
6310 indicated TYPE. If the conversion is successful, return the
6311 converted value. If the conversion is unsuccessful, return
6312 NULL_TREE if we issued an error message, or error_mark_node if we
6313 did not. We issue error messages for out-and-out bad template
6314 parameters, but not simply because the conversion failed, since we
6315 might be just trying to do argument deduction. Both TYPE and EXPR
6316 must be non-dependent.
6317
6318 The conversion follows the special rules described in
6319 [temp.arg.nontype], and it is much more strict than an implicit
6320 conversion.
6321
6322 This function is called twice for each template argument (see
6323 lookup_template_class for a more accurate description of this
6324 problem). This means that we need to handle expressions which
6325 are not valid in a C++ source, but can be created from the
6326 first call (for instance, casts to perform conversions). These
6327 hacks can go away after we fix the double coercion problem. */
6328
6329 static tree
6330 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6331 {
6332 tree expr_type;
6333
6334 /* Detect immediately string literals as invalid non-type argument.
6335 This special-case is not needed for correctness (we would easily
6336 catch this later), but only to provide better diagnostic for this
6337 common user mistake. As suggested by DR 100, we do not mention
6338 linkage issues in the diagnostic as this is not the point. */
6339 /* FIXME we're making this OK. */
6340 if (TREE_CODE (expr) == STRING_CST)
6341 {
6342 if (complain & tf_error)
6343 error ("%qE is not a valid template argument for type %qT "
6344 "because string literals can never be used in this context",
6345 expr, type);
6346 return NULL_TREE;
6347 }
6348
6349 /* Add the ADDR_EXPR now for the benefit of
6350 value_dependent_expression_p. */
6351 if (TYPE_PTROBV_P (type)
6352 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6353 {
6354 expr = decay_conversion (expr, complain);
6355 if (expr == error_mark_node)
6356 return error_mark_node;
6357 }
6358
6359 /* If we are in a template, EXPR may be non-dependent, but still
6360 have a syntactic, rather than semantic, form. For example, EXPR
6361 might be a SCOPE_REF, rather than the VAR_DECL to which the
6362 SCOPE_REF refers. Preserving the qualifying scope is necessary
6363 so that access checking can be performed when the template is
6364 instantiated -- but here we need the resolved form so that we can
6365 convert the argument. */
6366 bool non_dep = false;
6367 if (TYPE_REF_OBJ_P (type)
6368 && has_value_dependent_address (expr))
6369 /* If we want the address and it's value-dependent, don't fold. */;
6370 else if (processing_template_decl
6371 && potential_nondependent_constant_expression (expr))
6372 non_dep = true;
6373 if (error_operand_p (expr))
6374 return error_mark_node;
6375 expr_type = TREE_TYPE (expr);
6376 if (TREE_CODE (type) == REFERENCE_TYPE)
6377 expr = mark_lvalue_use (expr);
6378 else
6379 expr = mark_rvalue_use (expr);
6380
6381 /* If the argument is non-dependent, perform any conversions in
6382 non-dependent context as well. */
6383 processing_template_decl_sentinel s (non_dep);
6384 if (non_dep)
6385 expr = instantiate_non_dependent_expr_internal (expr, complain);
6386
6387 if (value_dependent_expression_p (expr))
6388 expr = canonicalize_expr_argument (expr, complain);
6389
6390 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6391 to a non-type argument of "nullptr". */
6392 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6393 expr = fold_simple (convert (type, expr));
6394
6395 /* In C++11, integral or enumeration non-type template arguments can be
6396 arbitrary constant expressions. Pointer and pointer to
6397 member arguments can be general constant expressions that evaluate
6398 to a null value, but otherwise still need to be of a specific form. */
6399 if (cxx_dialect >= cxx11)
6400 {
6401 if (TREE_CODE (expr) == PTRMEM_CST)
6402 /* A PTRMEM_CST is already constant, and a valid template
6403 argument for a parameter of pointer to member type, we just want
6404 to leave it in that form rather than lower it to a
6405 CONSTRUCTOR. */;
6406 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6407 /* Constant value checking is done later with type conversion. */;
6408 else if (cxx_dialect >= cxx1z)
6409 {
6410 if (TREE_CODE (type) != REFERENCE_TYPE)
6411 expr = maybe_constant_value (expr);
6412 else if (REFERENCE_REF_P (expr))
6413 {
6414 expr = TREE_OPERAND (expr, 0);
6415 expr = maybe_constant_value (expr);
6416 expr = convert_from_reference (expr);
6417 }
6418 }
6419 else if (TYPE_PTR_OR_PTRMEM_P (type))
6420 {
6421 tree folded = maybe_constant_value (expr);
6422 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6423 : null_member_pointer_value_p (folded))
6424 expr = folded;
6425 }
6426 }
6427
6428 /* HACK: Due to double coercion, we can get a
6429 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6430 which is the tree that we built on the first call (see
6431 below when coercing to reference to object or to reference to
6432 function). We just strip everything and get to the arg.
6433 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6434 for examples. */
6435 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6436 {
6437 tree probe_type, probe = expr;
6438 if (REFERENCE_REF_P (probe))
6439 probe = TREE_OPERAND (probe, 0);
6440 probe_type = TREE_TYPE (probe);
6441 if (TREE_CODE (probe) == NOP_EXPR)
6442 {
6443 /* ??? Maybe we could use convert_from_reference here, but we
6444 would need to relax its constraints because the NOP_EXPR
6445 could actually change the type to something more cv-qualified,
6446 and this is not folded by convert_from_reference. */
6447 tree addr = TREE_OPERAND (probe, 0);
6448 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6449 && TREE_CODE (addr) == ADDR_EXPR
6450 && TYPE_PTR_P (TREE_TYPE (addr))
6451 && (same_type_ignoring_top_level_qualifiers_p
6452 (TREE_TYPE (probe_type),
6453 TREE_TYPE (TREE_TYPE (addr)))))
6454 {
6455 expr = TREE_OPERAND (addr, 0);
6456 expr_type = TREE_TYPE (probe_type);
6457 }
6458 }
6459 }
6460
6461 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6462 parameter is a pointer to object, through decay and
6463 qualification conversion. Let's strip everything. */
6464 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6465 {
6466 tree probe = expr;
6467 STRIP_NOPS (probe);
6468 if (TREE_CODE (probe) == ADDR_EXPR
6469 && TYPE_PTR_P (TREE_TYPE (probe)))
6470 {
6471 /* Skip the ADDR_EXPR only if it is part of the decay for
6472 an array. Otherwise, it is part of the original argument
6473 in the source code. */
6474 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6475 probe = TREE_OPERAND (probe, 0);
6476 expr = probe;
6477 expr_type = TREE_TYPE (expr);
6478 }
6479 }
6480
6481 /* [temp.arg.nontype]/5, bullet 1
6482
6483 For a non-type template-parameter of integral or enumeration type,
6484 integral promotions (_conv.prom_) and integral conversions
6485 (_conv.integral_) are applied. */
6486 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6487 {
6488 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6489 t = maybe_constant_value (t);
6490 if (t != error_mark_node)
6491 expr = t;
6492
6493 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6494 return error_mark_node;
6495
6496 /* Notice that there are constant expressions like '4 % 0' which
6497 do not fold into integer constants. */
6498 if (TREE_CODE (expr) != INTEGER_CST
6499 && !value_dependent_expression_p (expr))
6500 {
6501 if (complain & tf_error)
6502 {
6503 int errs = errorcount, warns = warningcount + werrorcount;
6504 if (processing_template_decl
6505 && !require_potential_constant_expression (expr))
6506 return NULL_TREE;
6507 expr = cxx_constant_value (expr);
6508 if (errorcount > errs || warningcount + werrorcount > warns)
6509 inform (EXPR_LOC_OR_LOC (expr, input_location),
6510 "in template argument for type %qT ", type);
6511 if (expr == error_mark_node)
6512 return NULL_TREE;
6513 /* else cxx_constant_value complained but gave us
6514 a real constant, so go ahead. */
6515 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6516 }
6517 else
6518 return NULL_TREE;
6519 }
6520
6521 /* Avoid typedef problems. */
6522 if (TREE_TYPE (expr) != type)
6523 expr = fold_convert (type, expr);
6524 }
6525 /* [temp.arg.nontype]/5, bullet 2
6526
6527 For a non-type template-parameter of type pointer to object,
6528 qualification conversions (_conv.qual_) and the array-to-pointer
6529 conversion (_conv.array_) are applied. */
6530 else if (TYPE_PTROBV_P (type))
6531 {
6532 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6533
6534 A template-argument for a non-type, non-template template-parameter
6535 shall be one of: [...]
6536
6537 -- the name of a non-type template-parameter;
6538 -- the address of an object or function with external linkage, [...]
6539 expressed as "& id-expression" where the & is optional if the name
6540 refers to a function or array, or if the corresponding
6541 template-parameter is a reference.
6542
6543 Here, we do not care about functions, as they are invalid anyway
6544 for a parameter of type pointer-to-object. */
6545
6546 if (value_dependent_expression_p (expr))
6547 /* Non-type template parameters are OK. */
6548 ;
6549 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6550 /* Null pointer values are OK in C++11. */;
6551 else if (TREE_CODE (expr) != ADDR_EXPR
6552 && TREE_CODE (expr_type) != ARRAY_TYPE)
6553 {
6554 if (VAR_P (expr))
6555 {
6556 if (complain & tf_error)
6557 error ("%qD is not a valid template argument "
6558 "because %qD is a variable, not the address of "
6559 "a variable", expr, expr);
6560 return NULL_TREE;
6561 }
6562 if (POINTER_TYPE_P (expr_type))
6563 {
6564 if (complain & tf_error)
6565 error ("%qE is not a valid template argument for %qT "
6566 "because it is not the address of a variable",
6567 expr, type);
6568 return NULL_TREE;
6569 }
6570 /* Other values, like integer constants, might be valid
6571 non-type arguments of some other type. */
6572 return error_mark_node;
6573 }
6574 else
6575 {
6576 tree decl;
6577
6578 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6579 ? TREE_OPERAND (expr, 0) : expr);
6580 if (!VAR_P (decl))
6581 {
6582 if (complain & tf_error)
6583 error ("%qE is not a valid template argument of type %qT "
6584 "because %qE is not a variable", expr, type, decl);
6585 return NULL_TREE;
6586 }
6587 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6588 {
6589 if (complain & tf_error)
6590 error ("%qE is not a valid template argument of type %qT "
6591 "because %qD does not have external linkage",
6592 expr, type, decl);
6593 return NULL_TREE;
6594 }
6595 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6596 {
6597 if (complain & tf_error)
6598 error ("%qE is not a valid template argument of type %qT "
6599 "because %qD has no linkage", expr, type, decl);
6600 return NULL_TREE;
6601 }
6602 }
6603
6604 expr = decay_conversion (expr, complain);
6605 if (expr == error_mark_node)
6606 return error_mark_node;
6607
6608 expr = perform_qualification_conversions (type, expr);
6609 if (expr == error_mark_node)
6610 return error_mark_node;
6611 }
6612 /* [temp.arg.nontype]/5, bullet 3
6613
6614 For a non-type template-parameter of type reference to object, no
6615 conversions apply. The type referred to by the reference may be more
6616 cv-qualified than the (otherwise identical) type of the
6617 template-argument. The template-parameter is bound directly to the
6618 template-argument, which must be an lvalue. */
6619 else if (TYPE_REF_OBJ_P (type))
6620 {
6621 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6622 expr_type))
6623 return error_mark_node;
6624
6625 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6626 {
6627 if (complain & tf_error)
6628 error ("%qE is not a valid template argument for type %qT "
6629 "because of conflicts in cv-qualification", expr, type);
6630 return NULL_TREE;
6631 }
6632
6633 if (!lvalue_p (expr))
6634 {
6635 if (complain & tf_error)
6636 error ("%qE is not a valid template argument for type %qT "
6637 "because it is not an lvalue", expr, type);
6638 return NULL_TREE;
6639 }
6640
6641 /* [temp.arg.nontype]/1
6642
6643 A template-argument for a non-type, non-template template-parameter
6644 shall be one of: [...]
6645
6646 -- the address of an object or function with external linkage. */
6647 if (INDIRECT_REF_P (expr)
6648 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6649 {
6650 expr = TREE_OPERAND (expr, 0);
6651 if (DECL_P (expr))
6652 {
6653 if (complain & tf_error)
6654 error ("%q#D is not a valid template argument for type %qT "
6655 "because a reference variable does not have a constant "
6656 "address", expr, type);
6657 return NULL_TREE;
6658 }
6659 }
6660
6661 if (!value_dependent_expression_p (expr))
6662 {
6663 if (!DECL_P (expr))
6664 {
6665 if (complain & tf_error)
6666 error ("%qE is not a valid template argument for type %qT "
6667 "because it is not an object with linkage",
6668 expr, type);
6669 return NULL_TREE;
6670 }
6671
6672 /* DR 1155 allows internal linkage in C++11 and up. */
6673 linkage_kind linkage = decl_linkage (expr);
6674 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6675 {
6676 if (complain & tf_error)
6677 error ("%qE is not a valid template argument for type %qT "
6678 "because object %qD does not have linkage",
6679 expr, type, expr);
6680 return NULL_TREE;
6681 }
6682
6683 expr = build_nop (type, build_address (expr));
6684 }
6685 }
6686 /* [temp.arg.nontype]/5, bullet 4
6687
6688 For a non-type template-parameter of type pointer to function, only
6689 the function-to-pointer conversion (_conv.func_) is applied. If the
6690 template-argument represents a set of overloaded functions (or a
6691 pointer to such), the matching function is selected from the set
6692 (_over.over_). */
6693 else if (TYPE_PTRFN_P (type))
6694 {
6695 /* If the argument is a template-id, we might not have enough
6696 context information to decay the pointer. */
6697 if (!type_unknown_p (expr_type))
6698 {
6699 expr = decay_conversion (expr, complain);
6700 if (expr == error_mark_node)
6701 return error_mark_node;
6702 }
6703
6704 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6705 /* Null pointer values are OK in C++11. */
6706 return perform_qualification_conversions (type, expr);
6707
6708 expr = convert_nontype_argument_function (type, expr, complain);
6709 if (!expr || expr == error_mark_node)
6710 return expr;
6711 }
6712 /* [temp.arg.nontype]/5, bullet 5
6713
6714 For a non-type template-parameter of type reference to function, no
6715 conversions apply. If the template-argument represents a set of
6716 overloaded functions, the matching function is selected from the set
6717 (_over.over_). */
6718 else if (TYPE_REFFN_P (type))
6719 {
6720 if (TREE_CODE (expr) == ADDR_EXPR)
6721 {
6722 if (complain & tf_error)
6723 {
6724 error ("%qE is not a valid template argument for type %qT "
6725 "because it is a pointer", expr, type);
6726 inform (input_location, "try using %qE instead",
6727 TREE_OPERAND (expr, 0));
6728 }
6729 return NULL_TREE;
6730 }
6731
6732 expr = convert_nontype_argument_function (type, expr, complain);
6733 if (!expr || expr == error_mark_node)
6734 return expr;
6735 }
6736 /* [temp.arg.nontype]/5, bullet 6
6737
6738 For a non-type template-parameter of type pointer to member function,
6739 no conversions apply. If the template-argument represents a set of
6740 overloaded member functions, the matching member function is selected
6741 from the set (_over.over_). */
6742 else if (TYPE_PTRMEMFUNC_P (type))
6743 {
6744 expr = instantiate_type (type, expr, tf_none);
6745 if (expr == error_mark_node)
6746 return error_mark_node;
6747
6748 /* [temp.arg.nontype] bullet 1 says the pointer to member
6749 expression must be a pointer-to-member constant. */
6750 if (!value_dependent_expression_p (expr)
6751 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6752 return error_mark_node;
6753
6754 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6755 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6756 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6757 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6758
6759 /* There is no way to disable standard conversions in
6760 resolve_address_of_overloaded_function (called by
6761 instantiate_type). It is possible that the call succeeded by
6762 converting &B::I to &D::I (where B is a base of D), so we need
6763 to reject this conversion here.
6764
6765 Actually, even if there was a way to disable standard conversions,
6766 it would still be better to reject them here so that we can
6767 provide a superior diagnostic. */
6768 if (!same_type_p (TREE_TYPE (expr), type))
6769 {
6770 if (complain & tf_error)
6771 {
6772 error ("%qE is not a valid template argument for type %qT "
6773 "because it is of type %qT", expr, type,
6774 TREE_TYPE (expr));
6775 /* If we are just one standard conversion off, explain. */
6776 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6777 inform (input_location,
6778 "standard conversions are not allowed in this context");
6779 }
6780 return NULL_TREE;
6781 }
6782 }
6783 /* [temp.arg.nontype]/5, bullet 7
6784
6785 For a non-type template-parameter of type pointer to data member,
6786 qualification conversions (_conv.qual_) are applied. */
6787 else if (TYPE_PTRDATAMEM_P (type))
6788 {
6789 /* [temp.arg.nontype] bullet 1 says the pointer to member
6790 expression must be a pointer-to-member constant. */
6791 if (!value_dependent_expression_p (expr)
6792 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6793 return error_mark_node;
6794
6795 expr = perform_qualification_conversions (type, expr);
6796 if (expr == error_mark_node)
6797 return expr;
6798 }
6799 else if (NULLPTR_TYPE_P (type))
6800 {
6801 if (expr != nullptr_node)
6802 {
6803 if (complain & tf_error)
6804 error ("%qE is not a valid template argument for type %qT "
6805 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6806 return NULL_TREE;
6807 }
6808 return expr;
6809 }
6810 /* A template non-type parameter must be one of the above. */
6811 else
6812 gcc_unreachable ();
6813
6814 /* Sanity check: did we actually convert the argument to the
6815 right type? */
6816 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6817 (type, TREE_TYPE (expr)));
6818 return convert_from_reference (expr);
6819 }
6820
6821 /* Subroutine of coerce_template_template_parms, which returns 1 if
6822 PARM_PARM and ARG_PARM match using the rule for the template
6823 parameters of template template parameters. Both PARM and ARG are
6824 template parameters; the rest of the arguments are the same as for
6825 coerce_template_template_parms.
6826 */
6827 static int
6828 coerce_template_template_parm (tree parm,
6829 tree arg,
6830 tsubst_flags_t complain,
6831 tree in_decl,
6832 tree outer_args)
6833 {
6834 if (arg == NULL_TREE || error_operand_p (arg)
6835 || parm == NULL_TREE || error_operand_p (parm))
6836 return 0;
6837
6838 if (TREE_CODE (arg) != TREE_CODE (parm))
6839 return 0;
6840
6841 switch (TREE_CODE (parm))
6842 {
6843 case TEMPLATE_DECL:
6844 /* We encounter instantiations of templates like
6845 template <template <template <class> class> class TT>
6846 class C; */
6847 {
6848 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6849 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6850
6851 if (!coerce_template_template_parms
6852 (parmparm, argparm, complain, in_decl, outer_args))
6853 return 0;
6854 }
6855 /* Fall through. */
6856
6857 case TYPE_DECL:
6858 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6859 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6860 /* Argument is a parameter pack but parameter is not. */
6861 return 0;
6862 break;
6863
6864 case PARM_DECL:
6865 /* The tsubst call is used to handle cases such as
6866
6867 template <int> class C {};
6868 template <class T, template <T> class TT> class D {};
6869 D<int, C> d;
6870
6871 i.e. the parameter list of TT depends on earlier parameters. */
6872 if (!uses_template_parms (TREE_TYPE (arg)))
6873 {
6874 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6875 if (!uses_template_parms (t)
6876 && !same_type_p (t, TREE_TYPE (arg)))
6877 return 0;
6878 }
6879
6880 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6881 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6882 /* Argument is a parameter pack but parameter is not. */
6883 return 0;
6884
6885 break;
6886
6887 default:
6888 gcc_unreachable ();
6889 }
6890
6891 return 1;
6892 }
6893
6894 /* Coerce template argument list ARGLIST for use with template
6895 template-parameter TEMPL. */
6896
6897 static tree
6898 coerce_template_args_for_ttp (tree templ, tree arglist,
6899 tsubst_flags_t complain)
6900 {
6901 /* Consider an example where a template template parameter declared as
6902
6903 template <class T, class U = std::allocator<T> > class TT
6904
6905 The template parameter level of T and U are one level larger than
6906 of TT. To proper process the default argument of U, say when an
6907 instantiation `TT<int>' is seen, we need to build the full
6908 arguments containing {int} as the innermost level. Outer levels,
6909 available when not appearing as default template argument, can be
6910 obtained from the arguments of the enclosing template.
6911
6912 Suppose that TT is later substituted with std::vector. The above
6913 instantiation is `TT<int, std::allocator<T> >' with TT at
6914 level 1, and T at level 2, while the template arguments at level 1
6915 becomes {std::vector} and the inner level 2 is {int}. */
6916
6917 tree outer = DECL_CONTEXT (templ);
6918 if (outer)
6919 {
6920 if (DECL_TEMPLATE_SPECIALIZATION (outer))
6921 /* We want arguments for the partial specialization, not arguments for
6922 the primary template. */
6923 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
6924 else
6925 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6926 }
6927 else if (current_template_parms)
6928 {
6929 /* This is an argument of the current template, so we haven't set
6930 DECL_CONTEXT yet. */
6931 tree relevant_template_parms;
6932
6933 /* Parameter levels that are greater than the level of the given
6934 template template parm are irrelevant. */
6935 relevant_template_parms = current_template_parms;
6936 while (TMPL_PARMS_DEPTH (relevant_template_parms)
6937 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
6938 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
6939
6940 outer = template_parms_to_args (relevant_template_parms);
6941 }
6942
6943 if (outer)
6944 arglist = add_to_template_args (outer, arglist);
6945
6946 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6947 return coerce_template_parms (parmlist, arglist, templ,
6948 complain,
6949 /*require_all_args=*/true,
6950 /*use_default_args=*/true);
6951 }
6952
6953 /* A cache of template template parameters with match-all default
6954 arguments. */
6955 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
6956 static void
6957 store_defaulted_ttp (tree v, tree t)
6958 {
6959 if (!defaulted_ttp_cache)
6960 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
6961 defaulted_ttp_cache->put (v, t);
6962 }
6963 static tree
6964 lookup_defaulted_ttp (tree v)
6965 {
6966 if (defaulted_ttp_cache)
6967 if (tree *p = defaulted_ttp_cache->get (v))
6968 return *p;
6969 return NULL_TREE;
6970 }
6971
6972 /* T is a bound template template-parameter. Copy its arguments into default
6973 arguments of the template template-parameter's template parameters. */
6974
6975 static tree
6976 add_defaults_to_ttp (tree otmpl)
6977 {
6978 if (tree c = lookup_defaulted_ttp (otmpl))
6979 return c;
6980
6981 tree ntmpl = copy_node (otmpl);
6982
6983 tree ntype = copy_node (TREE_TYPE (otmpl));
6984 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
6985 TYPE_MAIN_VARIANT (ntype) = ntype;
6986 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
6987 TYPE_NAME (ntype) = ntmpl;
6988 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
6989
6990 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
6991 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
6992 TEMPLATE_PARM_DECL (idx) = ntmpl;
6993 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
6994
6995 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
6996 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
6997 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
6998 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
6999 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7000 {
7001 tree o = TREE_VEC_ELT (vec, i);
7002 if (!template_parameter_pack_p (TREE_VALUE (o)))
7003 {
7004 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7005 TREE_PURPOSE (n) = any_targ_node;
7006 }
7007 }
7008
7009 store_defaulted_ttp (otmpl, ntmpl);
7010 return ntmpl;
7011 }
7012
7013 /* ARG is a bound potential template template-argument, and PARGS is a list
7014 of arguments for the corresponding template template-parameter. Adjust
7015 PARGS as appropriate for application to ARG's template, and if ARG is a
7016 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7017 arguments to the template template parameter. */
7018
7019 static tree
7020 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7021 {
7022 ++processing_template_decl;
7023 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7024 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7025 {
7026 /* When comparing two template template-parameters in partial ordering,
7027 rewrite the one currently being used as an argument to have default
7028 arguments for all parameters. */
7029 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7030 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7031 if (pargs != error_mark_node)
7032 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7033 TYPE_TI_ARGS (arg));
7034 }
7035 else
7036 {
7037 tree aparms
7038 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7039 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7040 /*require_all*/true,
7041 /*use_default*/true);
7042 }
7043 --processing_template_decl;
7044 return pargs;
7045 }
7046
7047 /* Subroutine of unify for the case when PARM is a
7048 BOUND_TEMPLATE_TEMPLATE_PARM. */
7049
7050 static int
7051 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7052 bool explain_p)
7053 {
7054 tree parmvec = TYPE_TI_ARGS (parm);
7055 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7056
7057 /* The template template parm might be variadic and the argument
7058 not, so flatten both argument lists. */
7059 parmvec = expand_template_argument_pack (parmvec);
7060 argvec = expand_template_argument_pack (argvec);
7061
7062 tree nparmvec = parmvec;
7063 if (flag_new_ttp)
7064 {
7065 /* In keeping with P0522R0, adjust P's template arguments
7066 to apply to A's template; then flatten it again. */
7067 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7068 nparmvec = expand_template_argument_pack (nparmvec);
7069 }
7070
7071 if (unify (tparms, targs, nparmvec, argvec,
7072 UNIFY_ALLOW_NONE, explain_p))
7073 return 1;
7074
7075 /* If the P0522 adjustment eliminated a pack expansion, deduce
7076 empty packs. */
7077 if (flag_new_ttp
7078 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7079 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7080 DEDUCE_EXACT, /*sub*/true, explain_p))
7081 return 1;
7082
7083 return 0;
7084 }
7085
7086 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7087 template template parameters. Both PARM_PARMS and ARG_PARMS are
7088 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7089 or PARM_DECL.
7090
7091 Consider the example:
7092 template <class T> class A;
7093 template<template <class U> class TT> class B;
7094
7095 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7096 the parameters to A, and OUTER_ARGS contains A. */
7097
7098 static int
7099 coerce_template_template_parms (tree parm_parms,
7100 tree arg_parms,
7101 tsubst_flags_t complain,
7102 tree in_decl,
7103 tree outer_args)
7104 {
7105 int nparms, nargs, i;
7106 tree parm, arg;
7107 int variadic_p = 0;
7108
7109 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7110 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7111
7112 nparms = TREE_VEC_LENGTH (parm_parms);
7113 nargs = TREE_VEC_LENGTH (arg_parms);
7114
7115 if (flag_new_ttp)
7116 {
7117 /* P0522R0: A template template-parameter P is at least as specialized as
7118 a template template-argument A if, given the following rewrite to two
7119 function templates, the function template corresponding to P is at
7120 least as specialized as the function template corresponding to A
7121 according to the partial ordering rules for function templates
7122 ([temp.func.order]). Given an invented class template X with the
7123 template parameter list of A (including default arguments):
7124
7125 * Each of the two function templates has the same template parameters,
7126 respectively, as P or A.
7127
7128 * Each function template has a single function parameter whose type is
7129 a specialization of X with template arguments corresponding to the
7130 template parameters from the respective function template where, for
7131 each template parameter PP in the template parameter list of the
7132 function template, a corresponding template argument AA is formed. If
7133 PP declares a parameter pack, then AA is the pack expansion
7134 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7135
7136 If the rewrite produces an invalid type, then P is not at least as
7137 specialized as A. */
7138
7139 /* So coerce P's args to apply to A's parms, and then deduce between A's
7140 args and the converted args. If that succeeds, A is at least as
7141 specialized as P, so they match.*/
7142 tree pargs = template_parms_level_to_args (parm_parms);
7143 ++processing_template_decl;
7144 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7145 /*require_all*/true, /*use_default*/true);
7146 --processing_template_decl;
7147 if (pargs != error_mark_node)
7148 {
7149 tree targs = make_tree_vec (nargs);
7150 tree aargs = template_parms_level_to_args (arg_parms);
7151 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7152 /*explain*/false))
7153 return 1;
7154 }
7155 }
7156
7157 /* Determine whether we have a parameter pack at the end of the
7158 template template parameter's template parameter list. */
7159 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7160 {
7161 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7162
7163 if (error_operand_p (parm))
7164 return 0;
7165
7166 switch (TREE_CODE (parm))
7167 {
7168 case TEMPLATE_DECL:
7169 case TYPE_DECL:
7170 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7171 variadic_p = 1;
7172 break;
7173
7174 case PARM_DECL:
7175 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7176 variadic_p = 1;
7177 break;
7178
7179 default:
7180 gcc_unreachable ();
7181 }
7182 }
7183
7184 if (nargs != nparms
7185 && !(variadic_p && nargs >= nparms - 1))
7186 return 0;
7187
7188 /* Check all of the template parameters except the parameter pack at
7189 the end (if any). */
7190 for (i = 0; i < nparms - variadic_p; ++i)
7191 {
7192 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7193 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7194 continue;
7195
7196 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7197 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7198
7199 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7200 outer_args))
7201 return 0;
7202
7203 }
7204
7205 if (variadic_p)
7206 {
7207 /* Check each of the template parameters in the template
7208 argument against the template parameter pack at the end of
7209 the template template parameter. */
7210 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7211 return 0;
7212
7213 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7214
7215 for (; i < nargs; ++i)
7216 {
7217 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7218 continue;
7219
7220 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7221
7222 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7223 outer_args))
7224 return 0;
7225 }
7226 }
7227
7228 return 1;
7229 }
7230
7231 /* Verifies that the deduced template arguments (in TARGS) for the
7232 template template parameters (in TPARMS) represent valid bindings,
7233 by comparing the template parameter list of each template argument
7234 to the template parameter list of its corresponding template
7235 template parameter, in accordance with DR150. This
7236 routine can only be called after all template arguments have been
7237 deduced. It will return TRUE if all of the template template
7238 parameter bindings are okay, FALSE otherwise. */
7239 bool
7240 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7241 {
7242 int i, ntparms = TREE_VEC_LENGTH (tparms);
7243 bool ret = true;
7244
7245 /* We're dealing with template parms in this process. */
7246 ++processing_template_decl;
7247
7248 targs = INNERMOST_TEMPLATE_ARGS (targs);
7249
7250 for (i = 0; i < ntparms; ++i)
7251 {
7252 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7253 tree targ = TREE_VEC_ELT (targs, i);
7254
7255 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7256 {
7257 tree packed_args = NULL_TREE;
7258 int idx, len = 1;
7259
7260 if (ARGUMENT_PACK_P (targ))
7261 {
7262 /* Look inside the argument pack. */
7263 packed_args = ARGUMENT_PACK_ARGS (targ);
7264 len = TREE_VEC_LENGTH (packed_args);
7265 }
7266
7267 for (idx = 0; idx < len; ++idx)
7268 {
7269 tree targ_parms = NULL_TREE;
7270
7271 if (packed_args)
7272 /* Extract the next argument from the argument
7273 pack. */
7274 targ = TREE_VEC_ELT (packed_args, idx);
7275
7276 if (PACK_EXPANSION_P (targ))
7277 /* Look at the pattern of the pack expansion. */
7278 targ = PACK_EXPANSION_PATTERN (targ);
7279
7280 /* Extract the template parameters from the template
7281 argument. */
7282 if (TREE_CODE (targ) == TEMPLATE_DECL)
7283 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7284 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7285 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7286
7287 /* Verify that we can coerce the template template
7288 parameters from the template argument to the template
7289 parameter. This requires an exact match. */
7290 if (targ_parms
7291 && !coerce_template_template_parms
7292 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7293 targ_parms,
7294 tf_none,
7295 tparm,
7296 targs))
7297 {
7298 ret = false;
7299 goto out;
7300 }
7301 }
7302 }
7303 }
7304
7305 out:
7306
7307 --processing_template_decl;
7308 return ret;
7309 }
7310
7311 /* Since type attributes aren't mangled, we need to strip them from
7312 template type arguments. */
7313
7314 static tree
7315 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7316 {
7317 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7318 return arg;
7319 bool removed_attributes = false;
7320 tree canon = strip_typedefs (arg, &removed_attributes);
7321 if (removed_attributes
7322 && (complain & tf_warning))
7323 warning (OPT_Wignored_attributes,
7324 "ignoring attributes on template argument %qT", arg);
7325 return canon;
7326 }
7327
7328 /* And from inside dependent non-type arguments like sizeof(Type). */
7329
7330 static tree
7331 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7332 {
7333 if (!arg || arg == error_mark_node)
7334 return arg;
7335 bool removed_attributes = false;
7336 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7337 if (removed_attributes
7338 && (complain & tf_warning))
7339 warning (OPT_Wignored_attributes,
7340 "ignoring attributes in template argument %qE", arg);
7341 return canon;
7342 }
7343
7344 // A template declaration can be substituted for a constrained
7345 // template template parameter only when the argument is more
7346 // constrained than the parameter.
7347 static bool
7348 is_compatible_template_arg (tree parm, tree arg)
7349 {
7350 tree parm_cons = get_constraints (parm);
7351
7352 /* For now, allow constrained template template arguments
7353 and unconstrained template template parameters. */
7354 if (parm_cons == NULL_TREE)
7355 return true;
7356
7357 tree arg_cons = get_constraints (arg);
7358
7359 // If the template parameter is constrained, we need to rewrite its
7360 // constraints in terms of the ARG's template parameters. This ensures
7361 // that all of the template parameter types will have the same depth.
7362 //
7363 // Note that this is only valid when coerce_template_template_parm is
7364 // true for the innermost template parameters of PARM and ARG. In other
7365 // words, because coercion is successful, this conversion will be valid.
7366 if (parm_cons)
7367 {
7368 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7369 parm_cons = tsubst_constraint_info (parm_cons,
7370 INNERMOST_TEMPLATE_ARGS (args),
7371 tf_none, NULL_TREE);
7372 if (parm_cons == error_mark_node)
7373 return false;
7374 }
7375
7376 return subsumes (parm_cons, arg_cons);
7377 }
7378
7379 // Convert a placeholder argument into a binding to the original
7380 // parameter. The original parameter is saved as the TREE_TYPE of
7381 // ARG.
7382 static inline tree
7383 convert_wildcard_argument (tree parm, tree arg)
7384 {
7385 TREE_TYPE (arg) = parm;
7386 return arg;
7387 }
7388
7389 /* Convert the indicated template ARG as necessary to match the
7390 indicated template PARM. Returns the converted ARG, or
7391 error_mark_node if the conversion was unsuccessful. Error and
7392 warning messages are issued under control of COMPLAIN. This
7393 conversion is for the Ith parameter in the parameter list. ARGS is
7394 the full set of template arguments deduced so far. */
7395
7396 static tree
7397 convert_template_argument (tree parm,
7398 tree arg,
7399 tree args,
7400 tsubst_flags_t complain,
7401 int i,
7402 tree in_decl)
7403 {
7404 tree orig_arg;
7405 tree val;
7406 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7407
7408 if (parm == error_mark_node)
7409 return error_mark_node;
7410
7411 /* Trivially convert placeholders. */
7412 if (TREE_CODE (arg) == WILDCARD_DECL)
7413 return convert_wildcard_argument (parm, arg);
7414
7415 if (arg == any_targ_node)
7416 return arg;
7417
7418 if (TREE_CODE (arg) == TREE_LIST
7419 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7420 {
7421 /* The template argument was the name of some
7422 member function. That's usually
7423 invalid, but static members are OK. In any
7424 case, grab the underlying fields/functions
7425 and issue an error later if required. */
7426 orig_arg = TREE_VALUE (arg);
7427 TREE_TYPE (arg) = unknown_type_node;
7428 }
7429
7430 orig_arg = arg;
7431
7432 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7433 requires_type = (TREE_CODE (parm) == TYPE_DECL
7434 || requires_tmpl_type);
7435
7436 /* When determining whether an argument pack expansion is a template,
7437 look at the pattern. */
7438 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7439 arg = PACK_EXPANSION_PATTERN (arg);
7440
7441 /* Deal with an injected-class-name used as a template template arg. */
7442 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7443 {
7444 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7445 if (TREE_CODE (t) == TEMPLATE_DECL)
7446 {
7447 if (cxx_dialect >= cxx11)
7448 /* OK under DR 1004. */;
7449 else if (complain & tf_warning_or_error)
7450 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7451 " used as template template argument", TYPE_NAME (arg));
7452 else if (flag_pedantic_errors)
7453 t = arg;
7454
7455 arg = t;
7456 }
7457 }
7458
7459 is_tmpl_type =
7460 ((TREE_CODE (arg) == TEMPLATE_DECL
7461 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7462 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7463 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7464 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7465
7466 if (is_tmpl_type
7467 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7468 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7469 arg = TYPE_STUB_DECL (arg);
7470
7471 is_type = TYPE_P (arg) || is_tmpl_type;
7472
7473 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7474 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7475 {
7476 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7477 {
7478 if (complain & tf_error)
7479 error ("invalid use of destructor %qE as a type", orig_arg);
7480 return error_mark_node;
7481 }
7482
7483 permerror (input_location,
7484 "to refer to a type member of a template parameter, "
7485 "use %<typename %E%>", orig_arg);
7486
7487 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7488 TREE_OPERAND (arg, 1),
7489 typename_type,
7490 complain);
7491 arg = orig_arg;
7492 is_type = 1;
7493 }
7494 if (is_type != requires_type)
7495 {
7496 if (in_decl)
7497 {
7498 if (complain & tf_error)
7499 {
7500 error ("type/value mismatch at argument %d in template "
7501 "parameter list for %qD",
7502 i + 1, in_decl);
7503 if (is_type)
7504 inform (input_location,
7505 " expected a constant of type %qT, got %qT",
7506 TREE_TYPE (parm),
7507 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7508 else if (requires_tmpl_type)
7509 inform (input_location,
7510 " expected a class template, got %qE", orig_arg);
7511 else
7512 inform (input_location,
7513 " expected a type, got %qE", orig_arg);
7514 }
7515 }
7516 return error_mark_node;
7517 }
7518 if (is_tmpl_type ^ requires_tmpl_type)
7519 {
7520 if (in_decl && (complain & tf_error))
7521 {
7522 error ("type/value mismatch at argument %d in template "
7523 "parameter list for %qD",
7524 i + 1, in_decl);
7525 if (is_tmpl_type)
7526 inform (input_location,
7527 " expected a type, got %qT", DECL_NAME (arg));
7528 else
7529 inform (input_location,
7530 " expected a class template, got %qT", orig_arg);
7531 }
7532 return error_mark_node;
7533 }
7534
7535 if (is_type)
7536 {
7537 if (requires_tmpl_type)
7538 {
7539 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7540 val = orig_arg;
7541 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7542 /* The number of argument required is not known yet.
7543 Just accept it for now. */
7544 val = TREE_TYPE (arg);
7545 else
7546 {
7547 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7548 tree argparm;
7549
7550 /* Strip alias templates that are equivalent to another
7551 template. */
7552 arg = get_underlying_template (arg);
7553 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7554
7555 if (coerce_template_template_parms (parmparm, argparm,
7556 complain, in_decl,
7557 args))
7558 {
7559 val = arg;
7560
7561 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7562 TEMPLATE_DECL. */
7563 if (val != error_mark_node)
7564 {
7565 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7566 val = TREE_TYPE (val);
7567 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7568 val = make_pack_expansion (val);
7569 }
7570 }
7571 else
7572 {
7573 if (in_decl && (complain & tf_error))
7574 {
7575 error ("type/value mismatch at argument %d in "
7576 "template parameter list for %qD",
7577 i + 1, in_decl);
7578 inform (input_location,
7579 " expected a template of type %qD, got %qT",
7580 parm, orig_arg);
7581 }
7582
7583 val = error_mark_node;
7584 }
7585
7586 // Check that the constraints are compatible before allowing the
7587 // substitution.
7588 if (val != error_mark_node)
7589 if (!is_compatible_template_arg (parm, arg))
7590 {
7591 if (in_decl && (complain & tf_error))
7592 {
7593 error ("constraint mismatch at argument %d in "
7594 "template parameter list for %qD",
7595 i + 1, in_decl);
7596 inform (input_location, " expected %qD but got %qD",
7597 parm, arg);
7598 }
7599 val = error_mark_node;
7600 }
7601 }
7602 }
7603 else
7604 val = orig_arg;
7605 /* We only form one instance of each template specialization.
7606 Therefore, if we use a non-canonical variant (i.e., a
7607 typedef), any future messages referring to the type will use
7608 the typedef, which is confusing if those future uses do not
7609 themselves also use the typedef. */
7610 if (TYPE_P (val))
7611 val = canonicalize_type_argument (val, complain);
7612 }
7613 else
7614 {
7615 tree t = TREE_TYPE (parm);
7616
7617 if (tree a = type_uses_auto (t))
7618 {
7619 if (ARGUMENT_PACK_P (orig_arg))
7620 /* There's nothing to check for an auto argument pack. */
7621 return orig_arg;
7622
7623 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7624 if (t == error_mark_node)
7625 return error_mark_node;
7626 }
7627 else
7628 t = tsubst (t, args, complain, in_decl);
7629
7630 if (invalid_nontype_parm_type_p (t, complain))
7631 return error_mark_node;
7632
7633 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7634 {
7635 if (same_type_p (t, TREE_TYPE (orig_arg)))
7636 val = orig_arg;
7637 else
7638 {
7639 /* Not sure if this is reachable, but it doesn't hurt
7640 to be robust. */
7641 error ("type mismatch in nontype parameter pack");
7642 val = error_mark_node;
7643 }
7644 }
7645 else if (!type_dependent_expression_p (orig_arg)
7646 && !uses_template_parms (t))
7647 /* We used to call digest_init here. However, digest_init
7648 will report errors, which we don't want when complain
7649 is zero. More importantly, digest_init will try too
7650 hard to convert things: for example, `0' should not be
7651 converted to pointer type at this point according to
7652 the standard. Accepting this is not merely an
7653 extension, since deciding whether or not these
7654 conversions can occur is part of determining which
7655 function template to call, or whether a given explicit
7656 argument specification is valid. */
7657 val = convert_nontype_argument (t, orig_arg, complain);
7658 else
7659 val = canonicalize_expr_argument (orig_arg, complain);
7660
7661 if (val == NULL_TREE)
7662 val = error_mark_node;
7663 else if (val == error_mark_node && (complain & tf_error))
7664 error ("could not convert template argument %qE from %qT to %qT",
7665 orig_arg, TREE_TYPE (orig_arg), t);
7666
7667 if (INDIRECT_REF_P (val))
7668 {
7669 /* Reject template arguments that are references to built-in
7670 functions with no library fallbacks. */
7671 const_tree inner = TREE_OPERAND (val, 0);
7672 const_tree innertype = TREE_TYPE (inner);
7673 if (innertype
7674 && TREE_CODE (innertype) == REFERENCE_TYPE
7675 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7676 && 0 < TREE_OPERAND_LENGTH (inner)
7677 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7678 return error_mark_node;
7679 }
7680
7681 if (TREE_CODE (val) == SCOPE_REF)
7682 {
7683 /* Strip typedefs from the SCOPE_REF. */
7684 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7685 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7686 complain);
7687 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7688 QUALIFIED_NAME_IS_TEMPLATE (val));
7689 }
7690 }
7691
7692 return val;
7693 }
7694
7695 /* Coerces the remaining template arguments in INNER_ARGS (from
7696 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7697 Returns the coerced argument pack. PARM_IDX is the position of this
7698 parameter in the template parameter list. ARGS is the original
7699 template argument list. */
7700 static tree
7701 coerce_template_parameter_pack (tree parms,
7702 int parm_idx,
7703 tree args,
7704 tree inner_args,
7705 int arg_idx,
7706 tree new_args,
7707 int* lost,
7708 tree in_decl,
7709 tsubst_flags_t complain)
7710 {
7711 tree parm = TREE_VEC_ELT (parms, parm_idx);
7712 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7713 tree packed_args;
7714 tree argument_pack;
7715 tree packed_parms = NULL_TREE;
7716
7717 if (arg_idx > nargs)
7718 arg_idx = nargs;
7719
7720 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7721 {
7722 /* When the template parameter is a non-type template parameter pack
7723 or template template parameter pack whose type or template
7724 parameters use parameter packs, we know exactly how many arguments
7725 we are looking for. Build a vector of the instantiated decls for
7726 these template parameters in PACKED_PARMS. */
7727 /* We can't use make_pack_expansion here because it would interpret a
7728 _DECL as a use rather than a declaration. */
7729 tree decl = TREE_VALUE (parm);
7730 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7731 SET_PACK_EXPANSION_PATTERN (exp, decl);
7732 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7733 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7734
7735 TREE_VEC_LENGTH (args)--;
7736 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7737 TREE_VEC_LENGTH (args)++;
7738
7739 if (packed_parms == error_mark_node)
7740 return error_mark_node;
7741
7742 /* If we're doing a partial instantiation of a member template,
7743 verify that all of the types used for the non-type
7744 template parameter pack are, in fact, valid for non-type
7745 template parameters. */
7746 if (arg_idx < nargs
7747 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7748 {
7749 int j, len = TREE_VEC_LENGTH (packed_parms);
7750 for (j = 0; j < len; ++j)
7751 {
7752 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7753 if (invalid_nontype_parm_type_p (t, complain))
7754 return error_mark_node;
7755 }
7756 /* We don't know how many args we have yet, just
7757 use the unconverted ones for now. */
7758 return NULL_TREE;
7759 }
7760
7761 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7762 }
7763 /* Check if we have a placeholder pack, which indicates we're
7764 in the context of a introduction list. In that case we want
7765 to match this pack to the single placeholder. */
7766 else if (arg_idx < nargs
7767 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7768 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7769 {
7770 nargs = arg_idx + 1;
7771 packed_args = make_tree_vec (1);
7772 }
7773 else
7774 packed_args = make_tree_vec (nargs - arg_idx);
7775
7776 /* Convert the remaining arguments, which will be a part of the
7777 parameter pack "parm". */
7778 int first_pack_arg = arg_idx;
7779 for (; arg_idx < nargs; ++arg_idx)
7780 {
7781 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7782 tree actual_parm = TREE_VALUE (parm);
7783 int pack_idx = arg_idx - first_pack_arg;
7784
7785 if (packed_parms)
7786 {
7787 /* Once we've packed as many args as we have types, stop. */
7788 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7789 break;
7790 else if (PACK_EXPANSION_P (arg))
7791 /* We don't know how many args we have yet, just
7792 use the unconverted ones for now. */
7793 return NULL_TREE;
7794 else
7795 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7796 }
7797
7798 if (arg == error_mark_node)
7799 {
7800 if (complain & tf_error)
7801 error ("template argument %d is invalid", arg_idx + 1);
7802 }
7803 else
7804 arg = convert_template_argument (actual_parm,
7805 arg, new_args, complain, parm_idx,
7806 in_decl);
7807 if (arg == error_mark_node)
7808 (*lost)++;
7809 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7810 }
7811
7812 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7813 && TREE_VEC_LENGTH (packed_args) > 0)
7814 {
7815 if (complain & tf_error)
7816 error ("wrong number of template arguments (%d, should be %d)",
7817 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7818 return error_mark_node;
7819 }
7820
7821 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7822 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7823 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7824 else
7825 {
7826 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7827 TREE_TYPE (argument_pack)
7828 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7829 TREE_CONSTANT (argument_pack) = 1;
7830 }
7831
7832 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7833 if (CHECKING_P)
7834 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7835 TREE_VEC_LENGTH (packed_args));
7836 return argument_pack;
7837 }
7838
7839 /* Returns the number of pack expansions in the template argument vector
7840 ARGS. */
7841
7842 static int
7843 pack_expansion_args_count (tree args)
7844 {
7845 int i;
7846 int count = 0;
7847 if (args)
7848 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7849 {
7850 tree elt = TREE_VEC_ELT (args, i);
7851 if (elt && PACK_EXPANSION_P (elt))
7852 ++count;
7853 }
7854 return count;
7855 }
7856
7857 /* Convert all template arguments to their appropriate types, and
7858 return a vector containing the innermost resulting template
7859 arguments. If any error occurs, return error_mark_node. Error and
7860 warning messages are issued under control of COMPLAIN.
7861
7862 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7863 for arguments not specified in ARGS. Otherwise, if
7864 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7865 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7866 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7867 ARGS. */
7868
7869 static tree
7870 coerce_template_parms (tree parms,
7871 tree args,
7872 tree in_decl,
7873 tsubst_flags_t complain,
7874 bool require_all_args,
7875 bool use_default_args)
7876 {
7877 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7878 tree orig_inner_args;
7879 tree inner_args;
7880 tree new_args;
7881 tree new_inner_args;
7882 int saved_unevaluated_operand;
7883 int saved_inhibit_evaluation_warnings;
7884
7885 /* When used as a boolean value, indicates whether this is a
7886 variadic template parameter list. Since it's an int, we can also
7887 subtract it from nparms to get the number of non-variadic
7888 parameters. */
7889 int variadic_p = 0;
7890 int variadic_args_p = 0;
7891 int post_variadic_parms = 0;
7892
7893 /* Likewise for parameters with default arguments. */
7894 int default_p = 0;
7895
7896 if (args == error_mark_node)
7897 return error_mark_node;
7898
7899 nparms = TREE_VEC_LENGTH (parms);
7900
7901 /* Determine if there are any parameter packs or default arguments. */
7902 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7903 {
7904 tree parm = TREE_VEC_ELT (parms, parm_idx);
7905 if (variadic_p)
7906 ++post_variadic_parms;
7907 if (template_parameter_pack_p (TREE_VALUE (parm)))
7908 ++variadic_p;
7909 if (TREE_PURPOSE (parm))
7910 ++default_p;
7911 }
7912
7913 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7914 /* If there are no parameters that follow a parameter pack, we need to
7915 expand any argument packs so that we can deduce a parameter pack from
7916 some non-packed args followed by an argument pack, as in variadic85.C.
7917 If there are such parameters, we need to leave argument packs intact
7918 so the arguments are assigned properly. This can happen when dealing
7919 with a nested class inside a partial specialization of a class
7920 template, as in variadic92.C, or when deducing a template parameter pack
7921 from a sub-declarator, as in variadic114.C. */
7922 if (!post_variadic_parms)
7923 inner_args = expand_template_argument_pack (inner_args);
7924
7925 /* Count any pack expansion args. */
7926 variadic_args_p = pack_expansion_args_count (inner_args);
7927
7928 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7929 if ((nargs - variadic_args_p > nparms && !variadic_p)
7930 || (nargs < nparms - variadic_p
7931 && require_all_args
7932 && !variadic_args_p
7933 && (!use_default_args
7934 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7935 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7936 {
7937 if (complain & tf_error)
7938 {
7939 if (variadic_p || default_p)
7940 {
7941 nparms -= variadic_p + default_p;
7942 error ("wrong number of template arguments "
7943 "(%d, should be at least %d)", nargs, nparms);
7944 }
7945 else
7946 error ("wrong number of template arguments "
7947 "(%d, should be %d)", nargs, nparms);
7948
7949 if (in_decl)
7950 inform (DECL_SOURCE_LOCATION (in_decl),
7951 "provided for %qD", in_decl);
7952 }
7953
7954 return error_mark_node;
7955 }
7956 /* We can't pass a pack expansion to a non-pack parameter of an alias
7957 template (DR 1430). */
7958 else if (in_decl
7959 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7960 || concept_template_p (in_decl))
7961 && variadic_args_p
7962 && nargs - variadic_args_p < nparms - variadic_p)
7963 {
7964 if (complain & tf_error)
7965 {
7966 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7967 {
7968 tree arg = TREE_VEC_ELT (inner_args, i);
7969 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7970
7971 if (PACK_EXPANSION_P (arg)
7972 && !template_parameter_pack_p (parm))
7973 {
7974 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7975 error_at (location_of (arg),
7976 "pack expansion argument for non-pack parameter "
7977 "%qD of alias template %qD", parm, in_decl);
7978 else
7979 error_at (location_of (arg),
7980 "pack expansion argument for non-pack parameter "
7981 "%qD of concept %qD", parm, in_decl);
7982 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7983 goto found;
7984 }
7985 }
7986 gcc_unreachable ();
7987 found:;
7988 }
7989 return error_mark_node;
7990 }
7991
7992 /* We need to evaluate the template arguments, even though this
7993 template-id may be nested within a "sizeof". */
7994 saved_unevaluated_operand = cp_unevaluated_operand;
7995 cp_unevaluated_operand = 0;
7996 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7997 c_inhibit_evaluation_warnings = 0;
7998 new_inner_args = make_tree_vec (nparms);
7999 new_args = add_outermost_template_args (args, new_inner_args);
8000 int pack_adjust = 0;
8001 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8002 {
8003 tree arg;
8004 tree parm;
8005
8006 /* Get the Ith template parameter. */
8007 parm = TREE_VEC_ELT (parms, parm_idx);
8008
8009 if (parm == error_mark_node)
8010 {
8011 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8012 continue;
8013 }
8014
8015 /* Calculate the next argument. */
8016 if (arg_idx < nargs)
8017 arg = TREE_VEC_ELT (inner_args, arg_idx);
8018 else
8019 arg = NULL_TREE;
8020
8021 if (template_parameter_pack_p (TREE_VALUE (parm))
8022 && !(arg && ARGUMENT_PACK_P (arg)))
8023 {
8024 /* Some arguments will be placed in the
8025 template parameter pack PARM. */
8026 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8027 inner_args, arg_idx,
8028 new_args, &lost,
8029 in_decl, complain);
8030
8031 if (arg == NULL_TREE)
8032 {
8033 /* We don't know how many args we have yet, just use the
8034 unconverted (and still packed) ones for now. */
8035 new_inner_args = orig_inner_args;
8036 arg_idx = nargs;
8037 break;
8038 }
8039
8040 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8041
8042 /* Store this argument. */
8043 if (arg == error_mark_node)
8044 {
8045 lost++;
8046 /* We are done with all of the arguments. */
8047 arg_idx = nargs;
8048 }
8049 else
8050 {
8051 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8052 arg_idx += pack_adjust;
8053 }
8054
8055 continue;
8056 }
8057 else if (arg)
8058 {
8059 if (PACK_EXPANSION_P (arg))
8060 {
8061 /* "If every valid specialization of a variadic template
8062 requires an empty template parameter pack, the template is
8063 ill-formed, no diagnostic required." So check that the
8064 pattern works with this parameter. */
8065 tree pattern = PACK_EXPANSION_PATTERN (arg);
8066 tree conv = convert_template_argument (TREE_VALUE (parm),
8067 pattern, new_args,
8068 complain, parm_idx,
8069 in_decl);
8070 if (conv == error_mark_node)
8071 {
8072 if (complain & tf_error)
8073 inform (input_location, "so any instantiation with a "
8074 "non-empty parameter pack would be ill-formed");
8075 ++lost;
8076 }
8077 else if (TYPE_P (conv) && !TYPE_P (pattern))
8078 /* Recover from missing typename. */
8079 TREE_VEC_ELT (inner_args, arg_idx)
8080 = make_pack_expansion (conv);
8081
8082 /* We don't know how many args we have yet, just
8083 use the unconverted ones for now. */
8084 new_inner_args = inner_args;
8085 arg_idx = nargs;
8086 break;
8087 }
8088 }
8089 else if (require_all_args)
8090 {
8091 /* There must be a default arg in this case. */
8092 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8093 complain, in_decl);
8094 /* The position of the first default template argument,
8095 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8096 Record that. */
8097 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8098 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8099 arg_idx - pack_adjust);
8100 }
8101 else
8102 break;
8103
8104 if (arg == error_mark_node)
8105 {
8106 if (complain & tf_error)
8107 error ("template argument %d is invalid", arg_idx + 1);
8108 }
8109 else if (!arg)
8110 /* This only occurs if there was an error in the template
8111 parameter list itself (which we would already have
8112 reported) that we are trying to recover from, e.g., a class
8113 template with a parameter list such as
8114 template<typename..., typename>. */
8115 ++lost;
8116 else
8117 arg = convert_template_argument (TREE_VALUE (parm),
8118 arg, new_args, complain,
8119 parm_idx, in_decl);
8120
8121 if (arg == error_mark_node)
8122 lost++;
8123 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8124 }
8125 cp_unevaluated_operand = saved_unevaluated_operand;
8126 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8127
8128 if (variadic_p && arg_idx < nargs)
8129 {
8130 if (complain & tf_error)
8131 {
8132 error ("wrong number of template arguments "
8133 "(%d, should be %d)", nargs, arg_idx);
8134 if (in_decl)
8135 error ("provided for %q+D", in_decl);
8136 }
8137 return error_mark_node;
8138 }
8139
8140 if (lost)
8141 return error_mark_node;
8142
8143 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8144 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8145 TREE_VEC_LENGTH (new_inner_args));
8146
8147 return new_inner_args;
8148 }
8149
8150 /* Convert all template arguments to their appropriate types, and
8151 return a vector containing the innermost resulting template
8152 arguments. If any error occurs, return error_mark_node. Error and
8153 warning messages are not issued.
8154
8155 Note that no function argument deduction is performed, and default
8156 arguments are used to fill in unspecified arguments. */
8157 tree
8158 coerce_template_parms (tree parms, tree args, tree in_decl)
8159 {
8160 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8161 }
8162
8163 /* Convert all template arguments to their appropriate type, and
8164 instantiate default arguments as needed. This returns a vector
8165 containing the innermost resulting template arguments, or
8166 error_mark_node if unsuccessful. */
8167 tree
8168 coerce_template_parms (tree parms, tree args, tree in_decl,
8169 tsubst_flags_t complain)
8170 {
8171 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8172 }
8173
8174 /* Like coerce_template_parms. If PARMS represents all template
8175 parameters levels, this function returns a vector of vectors
8176 representing all the resulting argument levels. Note that in this
8177 case, only the innermost arguments are coerced because the
8178 outermost ones are supposed to have been coerced already.
8179
8180 Otherwise, if PARMS represents only (the innermost) vector of
8181 parameters, this function returns a vector containing just the
8182 innermost resulting arguments. */
8183
8184 static tree
8185 coerce_innermost_template_parms (tree parms,
8186 tree args,
8187 tree in_decl,
8188 tsubst_flags_t complain,
8189 bool require_all_args,
8190 bool use_default_args)
8191 {
8192 int parms_depth = TMPL_PARMS_DEPTH (parms);
8193 int args_depth = TMPL_ARGS_DEPTH (args);
8194 tree coerced_args;
8195
8196 if (parms_depth > 1)
8197 {
8198 coerced_args = make_tree_vec (parms_depth);
8199 tree level;
8200 int cur_depth;
8201
8202 for (level = parms, cur_depth = parms_depth;
8203 parms_depth > 0 && level != NULL_TREE;
8204 level = TREE_CHAIN (level), --cur_depth)
8205 {
8206 tree l;
8207 if (cur_depth == args_depth)
8208 l = coerce_template_parms (TREE_VALUE (level),
8209 args, in_decl, complain,
8210 require_all_args,
8211 use_default_args);
8212 else
8213 l = TMPL_ARGS_LEVEL (args, cur_depth);
8214
8215 if (l == error_mark_node)
8216 return error_mark_node;
8217
8218 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8219 }
8220 }
8221 else
8222 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8223 args, in_decl, complain,
8224 require_all_args,
8225 use_default_args);
8226 return coerced_args;
8227 }
8228
8229 /* Returns 1 if template args OT and NT are equivalent. */
8230
8231 int
8232 template_args_equal (tree ot, tree nt)
8233 {
8234 if (nt == ot)
8235 return 1;
8236 if (nt == NULL_TREE || ot == NULL_TREE)
8237 return false;
8238 if (nt == any_targ_node || ot == any_targ_node)
8239 return true;
8240
8241 if (TREE_CODE (nt) == TREE_VEC)
8242 /* For member templates */
8243 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8244 else if (PACK_EXPANSION_P (ot))
8245 return (PACK_EXPANSION_P (nt)
8246 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8247 PACK_EXPANSION_PATTERN (nt))
8248 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8249 PACK_EXPANSION_EXTRA_ARGS (nt)));
8250 else if (ARGUMENT_PACK_P (ot))
8251 {
8252 int i, len;
8253 tree opack, npack;
8254
8255 if (!ARGUMENT_PACK_P (nt))
8256 return 0;
8257
8258 opack = ARGUMENT_PACK_ARGS (ot);
8259 npack = ARGUMENT_PACK_ARGS (nt);
8260 len = TREE_VEC_LENGTH (opack);
8261 if (TREE_VEC_LENGTH (npack) != len)
8262 return 0;
8263 for (i = 0; i < len; ++i)
8264 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8265 TREE_VEC_ELT (npack, i)))
8266 return 0;
8267 return 1;
8268 }
8269 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8270 gcc_unreachable ();
8271 else if (TYPE_P (nt))
8272 {
8273 if (!TYPE_P (ot))
8274 return false;
8275 /* Don't treat an alias template specialization with dependent
8276 arguments as equivalent to its underlying type when used as a
8277 template argument; we need them to be distinct so that we
8278 substitute into the specialization arguments at instantiation
8279 time. And aliases can't be equivalent without being ==, so
8280 we don't need to look any deeper. */
8281 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
8282 return false;
8283 else
8284 return same_type_p (ot, nt);
8285 }
8286 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8287 return 0;
8288 else
8289 {
8290 /* Try to treat a template non-type argument that has been converted
8291 to the parameter type as equivalent to one that hasn't yet. */
8292 for (enum tree_code code1 = TREE_CODE (ot);
8293 CONVERT_EXPR_CODE_P (code1)
8294 || code1 == NON_LVALUE_EXPR;
8295 code1 = TREE_CODE (ot))
8296 ot = TREE_OPERAND (ot, 0);
8297 for (enum tree_code code2 = TREE_CODE (nt);
8298 CONVERT_EXPR_CODE_P (code2)
8299 || code2 == NON_LVALUE_EXPR;
8300 code2 = TREE_CODE (nt))
8301 nt = TREE_OPERAND (nt, 0);
8302
8303 return cp_tree_equal (ot, nt);
8304 }
8305 }
8306
8307 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8308 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8309 NEWARG_PTR with the offending arguments if they are non-NULL. */
8310
8311 int
8312 comp_template_args (tree oldargs, tree newargs,
8313 tree *oldarg_ptr, tree *newarg_ptr)
8314 {
8315 int i;
8316
8317 if (oldargs == newargs)
8318 return 1;
8319
8320 if (!oldargs || !newargs)
8321 return 0;
8322
8323 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8324 return 0;
8325
8326 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8327 {
8328 tree nt = TREE_VEC_ELT (newargs, i);
8329 tree ot = TREE_VEC_ELT (oldargs, i);
8330
8331 if (! template_args_equal (ot, nt))
8332 {
8333 if (oldarg_ptr != NULL)
8334 *oldarg_ptr = ot;
8335 if (newarg_ptr != NULL)
8336 *newarg_ptr = nt;
8337 return 0;
8338 }
8339 }
8340 return 1;
8341 }
8342
8343 static void
8344 add_pending_template (tree d)
8345 {
8346 tree ti = (TYPE_P (d)
8347 ? CLASSTYPE_TEMPLATE_INFO (d)
8348 : DECL_TEMPLATE_INFO (d));
8349 struct pending_template *pt;
8350 int level;
8351
8352 if (TI_PENDING_TEMPLATE_FLAG (ti))
8353 return;
8354
8355 /* We are called both from instantiate_decl, where we've already had a
8356 tinst_level pushed, and instantiate_template, where we haven't.
8357 Compensate. */
8358 level = !current_tinst_level || current_tinst_level->decl != d;
8359
8360 if (level)
8361 push_tinst_level (d);
8362
8363 pt = ggc_alloc<pending_template> ();
8364 pt->next = NULL;
8365 pt->tinst = current_tinst_level;
8366 if (last_pending_template)
8367 last_pending_template->next = pt;
8368 else
8369 pending_templates = pt;
8370
8371 last_pending_template = pt;
8372
8373 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8374
8375 if (level)
8376 pop_tinst_level ();
8377 }
8378
8379
8380 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8381 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8382 documentation for TEMPLATE_ID_EXPR. */
8383
8384 tree
8385 lookup_template_function (tree fns, tree arglist)
8386 {
8387 tree type;
8388
8389 if (fns == error_mark_node || arglist == error_mark_node)
8390 return error_mark_node;
8391
8392 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8393
8394 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8395 {
8396 error ("%q#D is not a function template", fns);
8397 return error_mark_node;
8398 }
8399
8400 if (BASELINK_P (fns))
8401 {
8402 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8403 unknown_type_node,
8404 BASELINK_FUNCTIONS (fns),
8405 arglist);
8406 return fns;
8407 }
8408
8409 type = TREE_TYPE (fns);
8410 if (TREE_CODE (fns) == OVERLOAD || !type)
8411 type = unknown_type_node;
8412
8413 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8414 }
8415
8416 /* Within the scope of a template class S<T>, the name S gets bound
8417 (in build_self_reference) to a TYPE_DECL for the class, not a
8418 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8419 or one of its enclosing classes, and that type is a template,
8420 return the associated TEMPLATE_DECL. Otherwise, the original
8421 DECL is returned.
8422
8423 Also handle the case when DECL is a TREE_LIST of ambiguous
8424 injected-class-names from different bases. */
8425
8426 tree
8427 maybe_get_template_decl_from_type_decl (tree decl)
8428 {
8429 if (decl == NULL_TREE)
8430 return decl;
8431
8432 /* DR 176: A lookup that finds an injected-class-name (10.2
8433 [class.member.lookup]) can result in an ambiguity in certain cases
8434 (for example, if it is found in more than one base class). If all of
8435 the injected-class-names that are found refer to specializations of
8436 the same class template, and if the name is followed by a
8437 template-argument-list, the reference refers to the class template
8438 itself and not a specialization thereof, and is not ambiguous. */
8439 if (TREE_CODE (decl) == TREE_LIST)
8440 {
8441 tree t, tmpl = NULL_TREE;
8442 for (t = decl; t; t = TREE_CHAIN (t))
8443 {
8444 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8445 if (!tmpl)
8446 tmpl = elt;
8447 else if (tmpl != elt)
8448 break;
8449 }
8450 if (tmpl && t == NULL_TREE)
8451 return tmpl;
8452 else
8453 return decl;
8454 }
8455
8456 return (decl != NULL_TREE
8457 && DECL_SELF_REFERENCE_P (decl)
8458 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8459 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8460 }
8461
8462 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8463 parameters, find the desired type.
8464
8465 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8466
8467 IN_DECL, if non-NULL, is the template declaration we are trying to
8468 instantiate.
8469
8470 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8471 the class we are looking up.
8472
8473 Issue error and warning messages under control of COMPLAIN.
8474
8475 If the template class is really a local class in a template
8476 function, then the FUNCTION_CONTEXT is the function in which it is
8477 being instantiated.
8478
8479 ??? Note that this function is currently called *twice* for each
8480 template-id: the first time from the parser, while creating the
8481 incomplete type (finish_template_type), and the second type during the
8482 real instantiation (instantiate_template_class). This is surely something
8483 that we want to avoid. It also causes some problems with argument
8484 coercion (see convert_nontype_argument for more information on this). */
8485
8486 static tree
8487 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8488 int entering_scope, tsubst_flags_t complain)
8489 {
8490 tree templ = NULL_TREE, parmlist;
8491 tree t;
8492 spec_entry **slot;
8493 spec_entry *entry;
8494 spec_entry elt;
8495 hashval_t hash;
8496
8497 if (identifier_p (d1))
8498 {
8499 tree value = innermost_non_namespace_value (d1);
8500 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8501 templ = value;
8502 else
8503 {
8504 if (context)
8505 push_decl_namespace (context);
8506 templ = lookup_name (d1);
8507 templ = maybe_get_template_decl_from_type_decl (templ);
8508 if (context)
8509 pop_decl_namespace ();
8510 }
8511 if (templ)
8512 context = DECL_CONTEXT (templ);
8513 }
8514 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8515 {
8516 tree type = TREE_TYPE (d1);
8517
8518 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8519 an implicit typename for the second A. Deal with it. */
8520 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8521 type = TREE_TYPE (type);
8522
8523 if (CLASSTYPE_TEMPLATE_INFO (type))
8524 {
8525 templ = CLASSTYPE_TI_TEMPLATE (type);
8526 d1 = DECL_NAME (templ);
8527 }
8528 }
8529 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8530 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8531 {
8532 templ = TYPE_TI_TEMPLATE (d1);
8533 d1 = DECL_NAME (templ);
8534 }
8535 else if (DECL_TYPE_TEMPLATE_P (d1))
8536 {
8537 templ = d1;
8538 d1 = DECL_NAME (templ);
8539 context = DECL_CONTEXT (templ);
8540 }
8541 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8542 {
8543 templ = d1;
8544 d1 = DECL_NAME (templ);
8545 }
8546
8547 /* Issue an error message if we didn't find a template. */
8548 if (! templ)
8549 {
8550 if (complain & tf_error)
8551 error ("%qT is not a template", d1);
8552 return error_mark_node;
8553 }
8554
8555 if (TREE_CODE (templ) != TEMPLATE_DECL
8556 /* Make sure it's a user visible template, if it was named by
8557 the user. */
8558 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8559 && !PRIMARY_TEMPLATE_P (templ)))
8560 {
8561 if (complain & tf_error)
8562 {
8563 error ("non-template type %qT used as a template", d1);
8564 if (in_decl)
8565 error ("for template declaration %q+D", in_decl);
8566 }
8567 return error_mark_node;
8568 }
8569
8570 complain &= ~tf_user;
8571
8572 /* An alias that just changes the name of a template is equivalent to the
8573 other template, so if any of the arguments are pack expansions, strip
8574 the alias to avoid problems with a pack expansion passed to a non-pack
8575 alias template parameter (DR 1430). */
8576 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8577 templ = get_underlying_template (templ);
8578
8579 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8580 {
8581 tree parm;
8582 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8583 if (arglist2 == error_mark_node
8584 || (!uses_template_parms (arglist2)
8585 && check_instantiated_args (templ, arglist2, complain)))
8586 return error_mark_node;
8587
8588 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8589 return parm;
8590 }
8591 else
8592 {
8593 tree template_type = TREE_TYPE (templ);
8594 tree gen_tmpl;
8595 tree type_decl;
8596 tree found = NULL_TREE;
8597 int arg_depth;
8598 int parm_depth;
8599 int is_dependent_type;
8600 int use_partial_inst_tmpl = false;
8601
8602 if (template_type == error_mark_node)
8603 /* An error occurred while building the template TEMPL, and a
8604 diagnostic has most certainly been emitted for that
8605 already. Let's propagate that error. */
8606 return error_mark_node;
8607
8608 gen_tmpl = most_general_template (templ);
8609 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8610 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8611 arg_depth = TMPL_ARGS_DEPTH (arglist);
8612
8613 if (arg_depth == 1 && parm_depth > 1)
8614 {
8615 /* We've been given an incomplete set of template arguments.
8616 For example, given:
8617
8618 template <class T> struct S1 {
8619 template <class U> struct S2 {};
8620 template <class U> struct S2<U*> {};
8621 };
8622
8623 we will be called with an ARGLIST of `U*', but the
8624 TEMPLATE will be `template <class T> template
8625 <class U> struct S1<T>::S2'. We must fill in the missing
8626 arguments. */
8627 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8628 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8629 arg_depth = TMPL_ARGS_DEPTH (arglist);
8630 }
8631
8632 /* Now we should have enough arguments. */
8633 gcc_assert (parm_depth == arg_depth);
8634
8635 /* From here on, we're only interested in the most general
8636 template. */
8637
8638 /* Calculate the BOUND_ARGS. These will be the args that are
8639 actually tsubst'd into the definition to create the
8640 instantiation. */
8641 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8642 complain,
8643 /*require_all_args=*/true,
8644 /*use_default_args=*/true);
8645
8646 if (arglist == error_mark_node)
8647 /* We were unable to bind the arguments. */
8648 return error_mark_node;
8649
8650 /* In the scope of a template class, explicit references to the
8651 template class refer to the type of the template, not any
8652 instantiation of it. For example, in:
8653
8654 template <class T> class C { void f(C<T>); }
8655
8656 the `C<T>' is just the same as `C'. Outside of the
8657 class, however, such a reference is an instantiation. */
8658 if (entering_scope
8659 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8660 || currently_open_class (template_type))
8661 {
8662 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (template_type);
8663
8664 if (comp_template_args (TI_ARGS (tinfo), arglist))
8665 return template_type;
8666 }
8667
8668 /* If we already have this specialization, return it. */
8669 elt.tmpl = gen_tmpl;
8670 elt.args = arglist;
8671 elt.spec = NULL_TREE;
8672 hash = spec_hasher::hash (&elt);
8673 entry = type_specializations->find_with_hash (&elt, hash);
8674
8675 if (entry)
8676 return entry->spec;
8677
8678 /* If the the template's constraints are not satisfied,
8679 then we cannot form a valid type.
8680
8681 Note that the check is deferred until after the hash
8682 lookup. This prevents redundant checks on previously
8683 instantiated specializations. */
8684 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8685 {
8686 if (complain & tf_error)
8687 {
8688 error ("template constraint failure");
8689 diagnose_constraints (input_location, gen_tmpl, arglist);
8690 }
8691 return error_mark_node;
8692 }
8693
8694 is_dependent_type = uses_template_parms (arglist);
8695
8696 /* If the deduced arguments are invalid, then the binding
8697 failed. */
8698 if (!is_dependent_type
8699 && check_instantiated_args (gen_tmpl,
8700 INNERMOST_TEMPLATE_ARGS (arglist),
8701 complain))
8702 return error_mark_node;
8703
8704 if (!is_dependent_type
8705 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8706 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8707 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8708 {
8709 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8710 DECL_NAME (gen_tmpl),
8711 /*tag_scope=*/ts_global);
8712 return found;
8713 }
8714
8715 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8716 complain, in_decl);
8717 if (context == error_mark_node)
8718 return error_mark_node;
8719
8720 if (!context)
8721 context = global_namespace;
8722
8723 /* Create the type. */
8724 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8725 {
8726 /* The user referred to a specialization of an alias
8727 template represented by GEN_TMPL.
8728
8729 [temp.alias]/2 says:
8730
8731 When a template-id refers to the specialization of an
8732 alias template, it is equivalent to the associated
8733 type obtained by substitution of its
8734 template-arguments for the template-parameters in the
8735 type-id of the alias template. */
8736
8737 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8738 /* Note that the call above (by indirectly calling
8739 register_specialization in tsubst_decl) registers the
8740 TYPE_DECL representing the specialization of the alias
8741 template. So next time someone substitutes ARGLIST for
8742 the template parms into the alias template (GEN_TMPL),
8743 she'll get that TYPE_DECL back. */
8744
8745 if (t == error_mark_node)
8746 return t;
8747 }
8748 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8749 {
8750 if (!is_dependent_type)
8751 {
8752 set_current_access_from_decl (TYPE_NAME (template_type));
8753 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8754 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8755 arglist, complain, in_decl),
8756 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8757 arglist, complain, in_decl),
8758 SCOPED_ENUM_P (template_type), NULL);
8759
8760 if (t == error_mark_node)
8761 return t;
8762 }
8763 else
8764 {
8765 /* We don't want to call start_enum for this type, since
8766 the values for the enumeration constants may involve
8767 template parameters. And, no one should be interested
8768 in the enumeration constants for such a type. */
8769 t = cxx_make_type (ENUMERAL_TYPE);
8770 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8771 }
8772 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8773 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8774 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8775 }
8776 else if (CLASS_TYPE_P (template_type))
8777 {
8778 t = make_class_type (TREE_CODE (template_type));
8779 CLASSTYPE_DECLARED_CLASS (t)
8780 = CLASSTYPE_DECLARED_CLASS (template_type);
8781 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8782
8783 /* A local class. Make sure the decl gets registered properly. */
8784 if (context == current_function_decl)
8785 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8786
8787 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8788 /* This instantiation is another name for the primary
8789 template type. Set the TYPE_CANONICAL field
8790 appropriately. */
8791 TYPE_CANONICAL (t) = template_type;
8792 else if (any_template_arguments_need_structural_equality_p (arglist))
8793 /* Some of the template arguments require structural
8794 equality testing, so this template class requires
8795 structural equality testing. */
8796 SET_TYPE_STRUCTURAL_EQUALITY (t);
8797 }
8798 else
8799 gcc_unreachable ();
8800
8801 /* If we called start_enum or pushtag above, this information
8802 will already be set up. */
8803 if (!TYPE_NAME (t))
8804 {
8805 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8806
8807 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8808 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8809 DECL_SOURCE_LOCATION (type_decl)
8810 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8811 }
8812 else
8813 type_decl = TYPE_NAME (t);
8814
8815 if (CLASS_TYPE_P (template_type))
8816 {
8817 TREE_PRIVATE (type_decl)
8818 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8819 TREE_PROTECTED (type_decl)
8820 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8821 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8822 {
8823 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8824 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8825 }
8826 }
8827
8828 if (OVERLOAD_TYPE_P (t)
8829 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8830 {
8831 static const char *tags[] = {"abi_tag", "may_alias"};
8832
8833 for (unsigned ix = 0; ix != 2; ix++)
8834 {
8835 tree attributes
8836 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8837
8838 if (attributes)
8839 TYPE_ATTRIBUTES (t)
8840 = tree_cons (TREE_PURPOSE (attributes),
8841 TREE_VALUE (attributes),
8842 TYPE_ATTRIBUTES (t));
8843 }
8844 }
8845
8846 /* Let's consider the explicit specialization of a member
8847 of a class template specialization that is implicitly instantiated,
8848 e.g.:
8849 template<class T>
8850 struct S
8851 {
8852 template<class U> struct M {}; //#0
8853 };
8854
8855 template<>
8856 template<>
8857 struct S<int>::M<char> //#1
8858 {
8859 int i;
8860 };
8861 [temp.expl.spec]/4 says this is valid.
8862
8863 In this case, when we write:
8864 S<int>::M<char> m;
8865
8866 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8867 the one of #0.
8868
8869 When we encounter #1, we want to store the partial instantiation
8870 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8871
8872 For all cases other than this "explicit specialization of member of a
8873 class template", we just want to store the most general template into
8874 the CLASSTYPE_TI_TEMPLATE of M.
8875
8876 This case of "explicit specialization of member of a class template"
8877 only happens when:
8878 1/ the enclosing class is an instantiation of, and therefore not
8879 the same as, the context of the most general template, and
8880 2/ we aren't looking at the partial instantiation itself, i.e.
8881 the innermost arguments are not the same as the innermost parms of
8882 the most general template.
8883
8884 So it's only when 1/ and 2/ happens that we want to use the partial
8885 instantiation of the member template in lieu of its most general
8886 template. */
8887
8888 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8889 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8890 /* the enclosing class must be an instantiation... */
8891 && CLASS_TYPE_P (context)
8892 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8893 {
8894 TREE_VEC_LENGTH (arglist)--;
8895 ++processing_template_decl;
8896 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
8897 tree partial_inst_args =
8898 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
8899 arglist, complain, NULL_TREE);
8900 --processing_template_decl;
8901 TREE_VEC_LENGTH (arglist)++;
8902 if (partial_inst_args == error_mark_node)
8903 return error_mark_node;
8904 use_partial_inst_tmpl =
8905 /*...and we must not be looking at the partial instantiation
8906 itself. */
8907 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8908 partial_inst_args);
8909 }
8910
8911 if (!use_partial_inst_tmpl)
8912 /* This case is easy; there are no member templates involved. */
8913 found = gen_tmpl;
8914 else
8915 {
8916 /* This is a full instantiation of a member template. Find
8917 the partial instantiation of which this is an instance. */
8918
8919 /* Temporarily reduce by one the number of levels in the ARGLIST
8920 so as to avoid comparing the last set of arguments. */
8921 TREE_VEC_LENGTH (arglist)--;
8922 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8923 TREE_VEC_LENGTH (arglist)++;
8924 /* FOUND is either a proper class type, or an alias
8925 template specialization. In the later case, it's a
8926 TYPE_DECL, resulting from the substituting of arguments
8927 for parameters in the TYPE_DECL of the alias template
8928 done earlier. So be careful while getting the template
8929 of FOUND. */
8930 found = (TREE_CODE (found) == TEMPLATE_DECL
8931 ? found
8932 : (TREE_CODE (found) == TYPE_DECL
8933 ? DECL_TI_TEMPLATE (found)
8934 : CLASSTYPE_TI_TEMPLATE (found)));
8935 }
8936
8937 // Build template info for the new specialization.
8938 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8939
8940 elt.spec = t;
8941 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8942 entry = ggc_alloc<spec_entry> ();
8943 *entry = elt;
8944 *slot = entry;
8945
8946 /* Note this use of the partial instantiation so we can check it
8947 later in maybe_process_partial_specialization. */
8948 DECL_TEMPLATE_INSTANTIATIONS (found)
8949 = tree_cons (arglist, t,
8950 DECL_TEMPLATE_INSTANTIATIONS (found));
8951
8952 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8953 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8954 /* Now that the type has been registered on the instantiations
8955 list, we set up the enumerators. Because the enumeration
8956 constants may involve the enumeration type itself, we make
8957 sure to register the type first, and then create the
8958 constants. That way, doing tsubst_expr for the enumeration
8959 constants won't result in recursive calls here; we'll find
8960 the instantiation and exit above. */
8961 tsubst_enum (template_type, t, arglist);
8962
8963 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8964 /* If the type makes use of template parameters, the
8965 code that generates debugging information will crash. */
8966 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8967
8968 /* Possibly limit visibility based on template args. */
8969 TREE_PUBLIC (type_decl) = 1;
8970 determine_visibility (type_decl);
8971
8972 inherit_targ_abi_tags (t);
8973
8974 return t;
8975 }
8976 }
8977
8978 /* Wrapper for lookup_template_class_1. */
8979
8980 tree
8981 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8982 int entering_scope, tsubst_flags_t complain)
8983 {
8984 tree ret;
8985 timevar_push (TV_TEMPLATE_INST);
8986 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8987 entering_scope, complain);
8988 timevar_pop (TV_TEMPLATE_INST);
8989 return ret;
8990 }
8991
8992 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8993
8994 tree
8995 lookup_template_variable (tree templ, tree arglist)
8996 {
8997 /* The type of the expression is NULL_TREE since the template-id could refer
8998 to an explicit or partial specialization. */
8999 tree type = NULL_TREE;
9000 if (flag_concepts && variable_concept_p (templ))
9001 /* Except that concepts are always bool. */
9002 type = boolean_type_node;
9003 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9004 }
9005
9006 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9007
9008 tree
9009 finish_template_variable (tree var, tsubst_flags_t complain)
9010 {
9011 tree templ = TREE_OPERAND (var, 0);
9012 tree arglist = TREE_OPERAND (var, 1);
9013
9014 /* We never want to return a VAR_DECL for a variable concept, since they
9015 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9016 bool concept_p = flag_concepts && variable_concept_p (templ);
9017 if (concept_p && processing_template_decl)
9018 return var;
9019
9020 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9021 arglist = add_outermost_template_args (tmpl_args, arglist);
9022
9023 templ = most_general_template (templ);
9024 tree parms = DECL_TEMPLATE_PARMS (templ);
9025 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9026 /*req_all*/true,
9027 /*use_default*/true);
9028
9029 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9030 {
9031 if (complain & tf_error)
9032 {
9033 error ("use of invalid variable template %qE", var);
9034 diagnose_constraints (location_of (var), templ, arglist);
9035 }
9036 return error_mark_node;
9037 }
9038
9039 /* If a template-id refers to a specialization of a variable
9040 concept, then the expression is true if and only if the
9041 concept's constraints are satisfied by the given template
9042 arguments.
9043
9044 NOTE: This is an extension of Concepts Lite TS that
9045 allows constraints to be used in expressions. */
9046 if (concept_p)
9047 {
9048 tree decl = DECL_TEMPLATE_RESULT (templ);
9049 return evaluate_variable_concept (decl, arglist);
9050 }
9051
9052 return instantiate_template (templ, arglist, complain);
9053 }
9054
9055 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9056 TARGS template args, and instantiate it if it's not dependent. */
9057
9058 tree
9059 lookup_and_finish_template_variable (tree templ, tree targs,
9060 tsubst_flags_t complain)
9061 {
9062 templ = lookup_template_variable (templ, targs);
9063 if (!any_dependent_template_arguments_p (targs))
9064 {
9065 templ = finish_template_variable (templ, complain);
9066 mark_used (templ);
9067 }
9068
9069 return convert_from_reference (templ);
9070 }
9071
9072 \f
9073 struct pair_fn_data
9074 {
9075 tree_fn_t fn;
9076 tree_fn_t any_fn;
9077 void *data;
9078 /* True when we should also visit template parameters that occur in
9079 non-deduced contexts. */
9080 bool include_nondeduced_p;
9081 hash_set<tree> *visited;
9082 };
9083
9084 /* Called from for_each_template_parm via walk_tree. */
9085
9086 static tree
9087 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9088 {
9089 tree t = *tp;
9090 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9091 tree_fn_t fn = pfd->fn;
9092 void *data = pfd->data;
9093 tree result = NULL_TREE;
9094
9095 #define WALK_SUBTREE(NODE) \
9096 do \
9097 { \
9098 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9099 pfd->include_nondeduced_p, \
9100 pfd->any_fn); \
9101 if (result) goto out; \
9102 } \
9103 while (0)
9104
9105 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9106 return t;
9107
9108 if (TYPE_P (t)
9109 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9110 WALK_SUBTREE (TYPE_CONTEXT (t));
9111
9112 switch (TREE_CODE (t))
9113 {
9114 case RECORD_TYPE:
9115 if (TYPE_PTRMEMFUNC_P (t))
9116 break;
9117 /* Fall through. */
9118
9119 case UNION_TYPE:
9120 case ENUMERAL_TYPE:
9121 if (!TYPE_TEMPLATE_INFO (t))
9122 *walk_subtrees = 0;
9123 else
9124 WALK_SUBTREE (TYPE_TI_ARGS (t));
9125 break;
9126
9127 case INTEGER_TYPE:
9128 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9129 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9130 break;
9131
9132 case METHOD_TYPE:
9133 /* Since we're not going to walk subtrees, we have to do this
9134 explicitly here. */
9135 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9136 /* Fall through. */
9137
9138 case FUNCTION_TYPE:
9139 /* Check the return type. */
9140 WALK_SUBTREE (TREE_TYPE (t));
9141
9142 /* Check the parameter types. Since default arguments are not
9143 instantiated until they are needed, the TYPE_ARG_TYPES may
9144 contain expressions that involve template parameters. But,
9145 no-one should be looking at them yet. And, once they're
9146 instantiated, they don't contain template parameters, so
9147 there's no point in looking at them then, either. */
9148 {
9149 tree parm;
9150
9151 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9152 WALK_SUBTREE (TREE_VALUE (parm));
9153
9154 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9155 want walk_tree walking into them itself. */
9156 *walk_subtrees = 0;
9157 }
9158
9159 if (flag_noexcept_type)
9160 {
9161 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9162 if (spec)
9163 WALK_SUBTREE (TREE_PURPOSE (spec));
9164 }
9165 break;
9166
9167 case TYPEOF_TYPE:
9168 case UNDERLYING_TYPE:
9169 if (pfd->include_nondeduced_p
9170 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9171 pfd->visited,
9172 pfd->include_nondeduced_p,
9173 pfd->any_fn))
9174 return error_mark_node;
9175 break;
9176
9177 case FUNCTION_DECL:
9178 case VAR_DECL:
9179 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9180 WALK_SUBTREE (DECL_TI_ARGS (t));
9181 /* Fall through. */
9182
9183 case PARM_DECL:
9184 case CONST_DECL:
9185 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9186 WALK_SUBTREE (DECL_INITIAL (t));
9187 if (DECL_CONTEXT (t)
9188 && pfd->include_nondeduced_p)
9189 WALK_SUBTREE (DECL_CONTEXT (t));
9190 break;
9191
9192 case BOUND_TEMPLATE_TEMPLATE_PARM:
9193 /* Record template parameters such as `T' inside `TT<T>'. */
9194 WALK_SUBTREE (TYPE_TI_ARGS (t));
9195 /* Fall through. */
9196
9197 case TEMPLATE_TEMPLATE_PARM:
9198 case TEMPLATE_TYPE_PARM:
9199 case TEMPLATE_PARM_INDEX:
9200 if (fn && (*fn)(t, data))
9201 return t;
9202 else if (!fn)
9203 return t;
9204 break;
9205
9206 case TEMPLATE_DECL:
9207 /* A template template parameter is encountered. */
9208 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9209 WALK_SUBTREE (TREE_TYPE (t));
9210
9211 /* Already substituted template template parameter */
9212 *walk_subtrees = 0;
9213 break;
9214
9215 case TYPENAME_TYPE:
9216 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9217 partial instantiation. */
9218 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9219 break;
9220
9221 case CONSTRUCTOR:
9222 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9223 && pfd->include_nondeduced_p)
9224 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9225 break;
9226
9227 case INDIRECT_REF:
9228 case COMPONENT_REF:
9229 /* If there's no type, then this thing must be some expression
9230 involving template parameters. */
9231 if (!fn && !TREE_TYPE (t))
9232 return error_mark_node;
9233 break;
9234
9235 case MODOP_EXPR:
9236 case CAST_EXPR:
9237 case IMPLICIT_CONV_EXPR:
9238 case REINTERPRET_CAST_EXPR:
9239 case CONST_CAST_EXPR:
9240 case STATIC_CAST_EXPR:
9241 case DYNAMIC_CAST_EXPR:
9242 case ARROW_EXPR:
9243 case DOTSTAR_EXPR:
9244 case TYPEID_EXPR:
9245 case PSEUDO_DTOR_EXPR:
9246 if (!fn)
9247 return error_mark_node;
9248 break;
9249
9250 default:
9251 break;
9252 }
9253
9254 #undef WALK_SUBTREE
9255
9256 /* We didn't find any template parameters we liked. */
9257 out:
9258 return result;
9259 }
9260
9261 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9262 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9263 call FN with the parameter and the DATA.
9264 If FN returns nonzero, the iteration is terminated, and
9265 for_each_template_parm returns 1. Otherwise, the iteration
9266 continues. If FN never returns a nonzero value, the value
9267 returned by for_each_template_parm is 0. If FN is NULL, it is
9268 considered to be the function which always returns 1.
9269
9270 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9271 parameters that occur in non-deduced contexts. When false, only
9272 visits those template parameters that can be deduced. */
9273
9274 static tree
9275 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9276 hash_set<tree> *visited,
9277 bool include_nondeduced_p,
9278 tree_fn_t any_fn)
9279 {
9280 struct pair_fn_data pfd;
9281 tree result;
9282
9283 /* Set up. */
9284 pfd.fn = fn;
9285 pfd.any_fn = any_fn;
9286 pfd.data = data;
9287 pfd.include_nondeduced_p = include_nondeduced_p;
9288
9289 /* Walk the tree. (Conceptually, we would like to walk without
9290 duplicates, but for_each_template_parm_r recursively calls
9291 for_each_template_parm, so we would need to reorganize a fair
9292 bit to use walk_tree_without_duplicates, so we keep our own
9293 visited list.) */
9294 if (visited)
9295 pfd.visited = visited;
9296 else
9297 pfd.visited = new hash_set<tree>;
9298 result = cp_walk_tree (&t,
9299 for_each_template_parm_r,
9300 &pfd,
9301 pfd.visited);
9302
9303 /* Clean up. */
9304 if (!visited)
9305 {
9306 delete pfd.visited;
9307 pfd.visited = 0;
9308 }
9309
9310 return result;
9311 }
9312
9313 /* Returns true if T depends on any template parameter. */
9314
9315 int
9316 uses_template_parms (tree t)
9317 {
9318 if (t == NULL_TREE)
9319 return false;
9320
9321 bool dependent_p;
9322 int saved_processing_template_decl;
9323
9324 saved_processing_template_decl = processing_template_decl;
9325 if (!saved_processing_template_decl)
9326 processing_template_decl = 1;
9327 if (TYPE_P (t))
9328 dependent_p = dependent_type_p (t);
9329 else if (TREE_CODE (t) == TREE_VEC)
9330 dependent_p = any_dependent_template_arguments_p (t);
9331 else if (TREE_CODE (t) == TREE_LIST)
9332 dependent_p = (uses_template_parms (TREE_VALUE (t))
9333 || uses_template_parms (TREE_CHAIN (t)));
9334 else if (TREE_CODE (t) == TYPE_DECL)
9335 dependent_p = dependent_type_p (TREE_TYPE (t));
9336 else if (DECL_P (t)
9337 || EXPR_P (t)
9338 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9339 || TREE_CODE (t) == OVERLOAD
9340 || BASELINK_P (t)
9341 || identifier_p (t)
9342 || TREE_CODE (t) == TRAIT_EXPR
9343 || TREE_CODE (t) == CONSTRUCTOR
9344 || CONSTANT_CLASS_P (t))
9345 dependent_p = (type_dependent_expression_p (t)
9346 || value_dependent_expression_p (t));
9347 else
9348 {
9349 gcc_assert (t == error_mark_node);
9350 dependent_p = false;
9351 }
9352
9353 processing_template_decl = saved_processing_template_decl;
9354
9355 return dependent_p;
9356 }
9357
9358 /* Returns true iff current_function_decl is an incompletely instantiated
9359 template. Useful instead of processing_template_decl because the latter
9360 is set to 0 during instantiate_non_dependent_expr. */
9361
9362 bool
9363 in_template_function (void)
9364 {
9365 tree fn = current_function_decl;
9366 bool ret;
9367 ++processing_template_decl;
9368 ret = (fn && DECL_LANG_SPECIFIC (fn)
9369 && DECL_TEMPLATE_INFO (fn)
9370 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9371 --processing_template_decl;
9372 return ret;
9373 }
9374
9375 /* Returns true if T depends on any template parameter with level LEVEL. */
9376
9377 bool
9378 uses_template_parms_level (tree t, int level)
9379 {
9380 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9381 /*include_nondeduced_p=*/true);
9382 }
9383
9384 /* Returns true if the signature of DECL depends on any template parameter from
9385 its enclosing class. */
9386
9387 bool
9388 uses_outer_template_parms (tree decl)
9389 {
9390 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9391 if (depth == 0)
9392 return false;
9393 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9394 &depth, NULL, /*include_nondeduced_p=*/true))
9395 return true;
9396 if (PRIMARY_TEMPLATE_P (decl)
9397 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9398 (DECL_TEMPLATE_PARMS (decl)),
9399 template_parm_outer_level,
9400 &depth, NULL, /*include_nondeduced_p=*/true))
9401 return true;
9402 tree ci = get_constraints (decl);
9403 if (ci)
9404 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9405 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9406 &depth, NULL, /*nondeduced*/true))
9407 return true;
9408 return false;
9409 }
9410
9411 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9412 ill-formed translation unit, i.e. a variable or function that isn't
9413 usable in a constant expression. */
9414
9415 static inline bool
9416 neglectable_inst_p (tree d)
9417 {
9418 return (DECL_P (d)
9419 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9420 : decl_maybe_constant_var_p (d)));
9421 }
9422
9423 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9424 neglectable and instantiated from within an erroneous instantiation. */
9425
9426 static bool
9427 limit_bad_template_recursion (tree decl)
9428 {
9429 struct tinst_level *lev = current_tinst_level;
9430 int errs = errorcount + sorrycount;
9431 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9432 return false;
9433
9434 for (; lev; lev = lev->next)
9435 if (neglectable_inst_p (lev->decl))
9436 break;
9437
9438 return (lev && errs > lev->errors);
9439 }
9440
9441 static int tinst_depth;
9442 extern int max_tinst_depth;
9443 int depth_reached;
9444
9445 static GTY(()) struct tinst_level *last_error_tinst_level;
9446
9447 /* We're starting to instantiate D; record the template instantiation context
9448 for diagnostics and to restore it later. */
9449
9450 bool
9451 push_tinst_level (tree d)
9452 {
9453 return push_tinst_level_loc (d, input_location);
9454 }
9455
9456 /* We're starting to instantiate D; record the template instantiation context
9457 at LOC for diagnostics and to restore it later. */
9458
9459 bool
9460 push_tinst_level_loc (tree d, location_t loc)
9461 {
9462 struct tinst_level *new_level;
9463
9464 if (tinst_depth >= max_tinst_depth)
9465 {
9466 /* Tell error.c not to try to instantiate any templates. */
9467 at_eof = 2;
9468 fatal_error (input_location,
9469 "template instantiation depth exceeds maximum of %d"
9470 " (use -ftemplate-depth= to increase the maximum)",
9471 max_tinst_depth);
9472 return false;
9473 }
9474
9475 /* If the current instantiation caused problems, don't let it instantiate
9476 anything else. Do allow deduction substitution and decls usable in
9477 constant expressions. */
9478 if (limit_bad_template_recursion (d))
9479 return false;
9480
9481 /* When not -quiet, dump template instantiations other than functions, since
9482 announce_function will take care of those. */
9483 if (!quiet_flag
9484 && TREE_CODE (d) != TREE_LIST
9485 && TREE_CODE (d) != FUNCTION_DECL)
9486 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9487
9488 new_level = ggc_alloc<tinst_level> ();
9489 new_level->decl = d;
9490 new_level->locus = loc;
9491 new_level->errors = errorcount+sorrycount;
9492 new_level->in_system_header_p = in_system_header_at (input_location);
9493 new_level->next = current_tinst_level;
9494 current_tinst_level = new_level;
9495
9496 ++tinst_depth;
9497 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9498 depth_reached = tinst_depth;
9499
9500 return true;
9501 }
9502
9503 /* We're done instantiating this template; return to the instantiation
9504 context. */
9505
9506 void
9507 pop_tinst_level (void)
9508 {
9509 /* Restore the filename and line number stashed away when we started
9510 this instantiation. */
9511 input_location = current_tinst_level->locus;
9512 current_tinst_level = current_tinst_level->next;
9513 --tinst_depth;
9514 }
9515
9516 /* We're instantiating a deferred template; restore the template
9517 instantiation context in which the instantiation was requested, which
9518 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9519
9520 static tree
9521 reopen_tinst_level (struct tinst_level *level)
9522 {
9523 struct tinst_level *t;
9524
9525 tinst_depth = 0;
9526 for (t = level; t; t = t->next)
9527 ++tinst_depth;
9528
9529 current_tinst_level = level;
9530 pop_tinst_level ();
9531 if (current_tinst_level)
9532 current_tinst_level->errors = errorcount+sorrycount;
9533 return level->decl;
9534 }
9535
9536 /* Returns the TINST_LEVEL which gives the original instantiation
9537 context. */
9538
9539 struct tinst_level *
9540 outermost_tinst_level (void)
9541 {
9542 struct tinst_level *level = current_tinst_level;
9543 if (level)
9544 while (level->next)
9545 level = level->next;
9546 return level;
9547 }
9548
9549 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9550 vector of template arguments, as for tsubst.
9551
9552 Returns an appropriate tsubst'd friend declaration. */
9553
9554 static tree
9555 tsubst_friend_function (tree decl, tree args)
9556 {
9557 tree new_friend;
9558
9559 if (TREE_CODE (decl) == FUNCTION_DECL
9560 && DECL_TEMPLATE_INSTANTIATION (decl)
9561 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9562 /* This was a friend declared with an explicit template
9563 argument list, e.g.:
9564
9565 friend void f<>(T);
9566
9567 to indicate that f was a template instantiation, not a new
9568 function declaration. Now, we have to figure out what
9569 instantiation of what template. */
9570 {
9571 tree template_id, arglist, fns;
9572 tree new_args;
9573 tree tmpl;
9574 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9575
9576 /* Friend functions are looked up in the containing namespace scope.
9577 We must enter that scope, to avoid finding member functions of the
9578 current class with same name. */
9579 push_nested_namespace (ns);
9580 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9581 tf_warning_or_error, NULL_TREE,
9582 /*integral_constant_expression_p=*/false);
9583 pop_nested_namespace (ns);
9584 arglist = tsubst (DECL_TI_ARGS (decl), args,
9585 tf_warning_or_error, NULL_TREE);
9586 template_id = lookup_template_function (fns, arglist);
9587
9588 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9589 tmpl = determine_specialization (template_id, new_friend,
9590 &new_args,
9591 /*need_member_template=*/0,
9592 TREE_VEC_LENGTH (args),
9593 tsk_none);
9594 return instantiate_template (tmpl, new_args, tf_error);
9595 }
9596
9597 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9598
9599 /* The NEW_FRIEND will look like an instantiation, to the
9600 compiler, but is not an instantiation from the point of view of
9601 the language. For example, we might have had:
9602
9603 template <class T> struct S {
9604 template <class U> friend void f(T, U);
9605 };
9606
9607 Then, in S<int>, template <class U> void f(int, U) is not an
9608 instantiation of anything. */
9609 if (new_friend == error_mark_node)
9610 return error_mark_node;
9611
9612 DECL_USE_TEMPLATE (new_friend) = 0;
9613 if (TREE_CODE (decl) == TEMPLATE_DECL)
9614 {
9615 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9616 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9617 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9618 }
9619
9620 /* The mangled name for the NEW_FRIEND is incorrect. The function
9621 is not a template instantiation and should not be mangled like
9622 one. Therefore, we forget the mangling here; we'll recompute it
9623 later if we need it. */
9624 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9625 {
9626 SET_DECL_RTL (new_friend, NULL);
9627 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9628 }
9629
9630 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9631 {
9632 tree old_decl;
9633 tree new_friend_template_info;
9634 tree new_friend_result_template_info;
9635 tree ns;
9636 int new_friend_is_defn;
9637
9638 /* We must save some information from NEW_FRIEND before calling
9639 duplicate decls since that function will free NEW_FRIEND if
9640 possible. */
9641 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9642 new_friend_is_defn =
9643 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9644 (template_for_substitution (new_friend)))
9645 != NULL_TREE);
9646 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9647 {
9648 /* This declaration is a `primary' template. */
9649 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9650
9651 new_friend_result_template_info
9652 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9653 }
9654 else
9655 new_friend_result_template_info = NULL_TREE;
9656
9657 /* Inside pushdecl_namespace_level, we will push into the
9658 current namespace. However, the friend function should go
9659 into the namespace of the template. */
9660 ns = decl_namespace_context (new_friend);
9661 push_nested_namespace (ns);
9662 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9663 pop_nested_namespace (ns);
9664
9665 if (old_decl == error_mark_node)
9666 return error_mark_node;
9667
9668 if (old_decl != new_friend)
9669 {
9670 /* This new friend declaration matched an existing
9671 declaration. For example, given:
9672
9673 template <class T> void f(T);
9674 template <class U> class C {
9675 template <class T> friend void f(T) {}
9676 };
9677
9678 the friend declaration actually provides the definition
9679 of `f', once C has been instantiated for some type. So,
9680 old_decl will be the out-of-class template declaration,
9681 while new_friend is the in-class definition.
9682
9683 But, if `f' was called before this point, the
9684 instantiation of `f' will have DECL_TI_ARGS corresponding
9685 to `T' but not to `U', references to which might appear
9686 in the definition of `f'. Previously, the most general
9687 template for an instantiation of `f' was the out-of-class
9688 version; now it is the in-class version. Therefore, we
9689 run through all specialization of `f', adding to their
9690 DECL_TI_ARGS appropriately. In particular, they need a
9691 new set of outer arguments, corresponding to the
9692 arguments for this class instantiation.
9693
9694 The same situation can arise with something like this:
9695
9696 friend void f(int);
9697 template <class T> class C {
9698 friend void f(T) {}
9699 };
9700
9701 when `C<int>' is instantiated. Now, `f(int)' is defined
9702 in the class. */
9703
9704 if (!new_friend_is_defn)
9705 /* On the other hand, if the in-class declaration does
9706 *not* provide a definition, then we don't want to alter
9707 existing definitions. We can just leave everything
9708 alone. */
9709 ;
9710 else
9711 {
9712 tree new_template = TI_TEMPLATE (new_friend_template_info);
9713 tree new_args = TI_ARGS (new_friend_template_info);
9714
9715 /* Overwrite whatever template info was there before, if
9716 any, with the new template information pertaining to
9717 the declaration. */
9718 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9719
9720 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9721 {
9722 /* We should have called reregister_specialization in
9723 duplicate_decls. */
9724 gcc_assert (retrieve_specialization (new_template,
9725 new_args, 0)
9726 == old_decl);
9727
9728 /* Instantiate it if the global has already been used. */
9729 if (DECL_ODR_USED (old_decl))
9730 instantiate_decl (old_decl, /*defer_ok=*/true,
9731 /*expl_inst_class_mem_p=*/false);
9732 }
9733 else
9734 {
9735 tree t;
9736
9737 /* Indicate that the old function template is a partial
9738 instantiation. */
9739 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9740 = new_friend_result_template_info;
9741
9742 gcc_assert (new_template
9743 == most_general_template (new_template));
9744 gcc_assert (new_template != old_decl);
9745
9746 /* Reassign any specializations already in the hash table
9747 to the new more general template, and add the
9748 additional template args. */
9749 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9750 t != NULL_TREE;
9751 t = TREE_CHAIN (t))
9752 {
9753 tree spec = TREE_VALUE (t);
9754 spec_entry elt;
9755
9756 elt.tmpl = old_decl;
9757 elt.args = DECL_TI_ARGS (spec);
9758 elt.spec = NULL_TREE;
9759
9760 decl_specializations->remove_elt (&elt);
9761
9762 DECL_TI_ARGS (spec)
9763 = add_outermost_template_args (new_args,
9764 DECL_TI_ARGS (spec));
9765
9766 register_specialization
9767 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9768
9769 }
9770 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9771 }
9772 }
9773
9774 /* The information from NEW_FRIEND has been merged into OLD_DECL
9775 by duplicate_decls. */
9776 new_friend = old_decl;
9777 }
9778 }
9779 else
9780 {
9781 tree context = DECL_CONTEXT (new_friend);
9782 bool dependent_p;
9783
9784 /* In the code
9785 template <class T> class C {
9786 template <class U> friend void C1<U>::f (); // case 1
9787 friend void C2<T>::f (); // case 2
9788 };
9789 we only need to make sure CONTEXT is a complete type for
9790 case 2. To distinguish between the two cases, we note that
9791 CONTEXT of case 1 remains dependent type after tsubst while
9792 this isn't true for case 2. */
9793 ++processing_template_decl;
9794 dependent_p = dependent_type_p (context);
9795 --processing_template_decl;
9796
9797 if (!dependent_p
9798 && !complete_type_or_else (context, NULL_TREE))
9799 return error_mark_node;
9800
9801 if (COMPLETE_TYPE_P (context))
9802 {
9803 tree fn = new_friend;
9804 /* do_friend adds the TEMPLATE_DECL for any member friend
9805 template even if it isn't a member template, i.e.
9806 template <class T> friend A<T>::f();
9807 Look through it in that case. */
9808 if (TREE_CODE (fn) == TEMPLATE_DECL
9809 && !PRIMARY_TEMPLATE_P (fn))
9810 fn = DECL_TEMPLATE_RESULT (fn);
9811 /* Check to see that the declaration is really present, and,
9812 possibly obtain an improved declaration. */
9813 fn = check_classfn (context, fn, NULL_TREE);
9814
9815 if (fn)
9816 new_friend = fn;
9817 }
9818 }
9819
9820 return new_friend;
9821 }
9822
9823 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9824 template arguments, as for tsubst.
9825
9826 Returns an appropriate tsubst'd friend type or error_mark_node on
9827 failure. */
9828
9829 static tree
9830 tsubst_friend_class (tree friend_tmpl, tree args)
9831 {
9832 tree friend_type;
9833 tree tmpl;
9834 tree context;
9835
9836 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9837 {
9838 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9839 return TREE_TYPE (t);
9840 }
9841
9842 context = CP_DECL_CONTEXT (friend_tmpl);
9843
9844 if (context != global_namespace)
9845 {
9846 if (TREE_CODE (context) == NAMESPACE_DECL)
9847 push_nested_namespace (context);
9848 else
9849 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9850 }
9851
9852 /* Look for a class template declaration. We look for hidden names
9853 because two friend declarations of the same template are the
9854 same. For example, in:
9855
9856 struct A {
9857 template <typename> friend class F;
9858 };
9859 template <typename> struct B {
9860 template <typename> friend class F;
9861 };
9862
9863 both F templates are the same. */
9864 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9865 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9866
9867 /* But, if we don't find one, it might be because we're in a
9868 situation like this:
9869
9870 template <class T>
9871 struct S {
9872 template <class U>
9873 friend struct S;
9874 };
9875
9876 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9877 for `S<int>', not the TEMPLATE_DECL. */
9878 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9879 {
9880 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9881 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9882 }
9883
9884 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9885 {
9886 /* The friend template has already been declared. Just
9887 check to see that the declarations match, and install any new
9888 default parameters. We must tsubst the default parameters,
9889 of course. We only need the innermost template parameters
9890 because that is all that redeclare_class_template will look
9891 at. */
9892 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9893 > TMPL_ARGS_DEPTH (args))
9894 {
9895 tree parms;
9896 location_t saved_input_location;
9897 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9898 args, tf_warning_or_error);
9899
9900 saved_input_location = input_location;
9901 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9902 tree cons = get_constraints (tmpl);
9903 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9904 input_location = saved_input_location;
9905
9906 }
9907
9908 friend_type = TREE_TYPE (tmpl);
9909 }
9910 else
9911 {
9912 /* The friend template has not already been declared. In this
9913 case, the instantiation of the template class will cause the
9914 injection of this template into the global scope. */
9915 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9916 if (tmpl == error_mark_node)
9917 return error_mark_node;
9918
9919 /* The new TMPL is not an instantiation of anything, so we
9920 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9921 the new type because that is supposed to be the corresponding
9922 template decl, i.e., TMPL. */
9923 DECL_USE_TEMPLATE (tmpl) = 0;
9924 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9925 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9926 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9927 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9928
9929 /* Inject this template into the global scope. */
9930 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9931 }
9932
9933 if (context != global_namespace)
9934 {
9935 if (TREE_CODE (context) == NAMESPACE_DECL)
9936 pop_nested_namespace (context);
9937 else
9938 pop_nested_class ();
9939 }
9940
9941 return friend_type;
9942 }
9943
9944 /* Returns zero if TYPE cannot be completed later due to circularity.
9945 Otherwise returns one. */
9946
9947 static int
9948 can_complete_type_without_circularity (tree type)
9949 {
9950 if (type == NULL_TREE || type == error_mark_node)
9951 return 0;
9952 else if (COMPLETE_TYPE_P (type))
9953 return 1;
9954 else if (TREE_CODE (type) == ARRAY_TYPE)
9955 return can_complete_type_without_circularity (TREE_TYPE (type));
9956 else if (CLASS_TYPE_P (type)
9957 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9958 return 0;
9959 else
9960 return 1;
9961 }
9962
9963 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9964 tsubst_flags_t, tree);
9965
9966 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9967 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9968
9969 static tree
9970 tsubst_attribute (tree t, tree *decl_p, tree args,
9971 tsubst_flags_t complain, tree in_decl)
9972 {
9973 gcc_assert (ATTR_IS_DEPENDENT (t));
9974
9975 tree val = TREE_VALUE (t);
9976 if (val == NULL_TREE)
9977 /* Nothing to do. */;
9978 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9979 && is_attribute_p ("omp declare simd",
9980 get_attribute_name (t)))
9981 {
9982 tree clauses = TREE_VALUE (val);
9983 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
9984 complain, in_decl);
9985 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9986 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
9987 tree parms = DECL_ARGUMENTS (*decl_p);
9988 clauses
9989 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9990 if (clauses)
9991 val = build_tree_list (NULL_TREE, clauses);
9992 else
9993 val = NULL_TREE;
9994 }
9995 /* If the first attribute argument is an identifier, don't
9996 pass it through tsubst. Attributes like mode, format,
9997 cleanup and several target specific attributes expect it
9998 unmodified. */
9999 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10000 {
10001 tree chain
10002 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10003 /*integral_constant_expression_p=*/false);
10004 if (chain != TREE_CHAIN (val))
10005 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10006 }
10007 else if (PACK_EXPANSION_P (val))
10008 {
10009 /* An attribute pack expansion. */
10010 tree purp = TREE_PURPOSE (t);
10011 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10012 int len = TREE_VEC_LENGTH (pack);
10013 tree list = NULL_TREE;
10014 tree *q = &list;
10015 for (int i = 0; i < len; ++i)
10016 {
10017 tree elt = TREE_VEC_ELT (pack, i);
10018 *q = build_tree_list (purp, elt);
10019 q = &TREE_CHAIN (*q);
10020 }
10021 return list;
10022 }
10023 else
10024 val = tsubst_expr (val, args, complain, in_decl,
10025 /*integral_constant_expression_p=*/false);
10026
10027 if (val != TREE_VALUE (t))
10028 return build_tree_list (TREE_PURPOSE (t), val);
10029 return t;
10030 }
10031
10032 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10033 unchanged or a new TREE_LIST chain. */
10034
10035 static tree
10036 tsubst_attributes (tree attributes, tree args,
10037 tsubst_flags_t complain, tree in_decl)
10038 {
10039 tree last_dep = NULL_TREE;
10040
10041 for (tree t = attributes; t; t = TREE_CHAIN (t))
10042 if (ATTR_IS_DEPENDENT (t))
10043 {
10044 last_dep = t;
10045 attributes = copy_list (attributes);
10046 break;
10047 }
10048
10049 if (last_dep)
10050 for (tree *p = &attributes; *p; )
10051 {
10052 tree t = *p;
10053 if (ATTR_IS_DEPENDENT (t))
10054 {
10055 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10056 if (subst != t)
10057 {
10058 *p = subst;
10059 do
10060 p = &TREE_CHAIN (*p);
10061 while (*p);
10062 *p = TREE_CHAIN (t);
10063 continue;
10064 }
10065 }
10066 p = &TREE_CHAIN (*p);
10067 }
10068
10069 return attributes;
10070 }
10071
10072 /* Apply any attributes which had to be deferred until instantiation
10073 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10074 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10075
10076 static void
10077 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10078 tree args, tsubst_flags_t complain, tree in_decl)
10079 {
10080 tree last_dep = NULL_TREE;
10081 tree t;
10082 tree *p;
10083
10084 if (attributes == NULL_TREE)
10085 return;
10086
10087 if (DECL_P (*decl_p))
10088 {
10089 if (TREE_TYPE (*decl_p) == error_mark_node)
10090 return;
10091 p = &DECL_ATTRIBUTES (*decl_p);
10092 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10093 to our attributes parameter. */
10094 gcc_assert (*p == attributes);
10095 }
10096 else
10097 {
10098 p = &TYPE_ATTRIBUTES (*decl_p);
10099 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10100 lookup_template_class_1, and should be preserved. */
10101 gcc_assert (*p != attributes);
10102 while (*p)
10103 p = &TREE_CHAIN (*p);
10104 }
10105
10106 for (t = attributes; t; t = TREE_CHAIN (t))
10107 if (ATTR_IS_DEPENDENT (t))
10108 {
10109 last_dep = t;
10110 attributes = copy_list (attributes);
10111 break;
10112 }
10113
10114 *p = attributes;
10115 if (last_dep)
10116 {
10117 tree late_attrs = NULL_TREE;
10118 tree *q = &late_attrs;
10119
10120 for (; *p; )
10121 {
10122 t = *p;
10123 if (ATTR_IS_DEPENDENT (t))
10124 {
10125 *p = TREE_CHAIN (t);
10126 TREE_CHAIN (t) = NULL_TREE;
10127 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10128 do
10129 q = &TREE_CHAIN (*q);
10130 while (*q);
10131 }
10132 else
10133 p = &TREE_CHAIN (t);
10134 }
10135
10136 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10137 }
10138 }
10139
10140 /* Perform (or defer) access check for typedefs that were referenced
10141 from within the template TMPL code.
10142 This is a subroutine of instantiate_decl and instantiate_class_template.
10143 TMPL is the template to consider and TARGS is the list of arguments of
10144 that template. */
10145
10146 static void
10147 perform_typedefs_access_check (tree tmpl, tree targs)
10148 {
10149 location_t saved_location;
10150 unsigned i;
10151 qualified_typedef_usage_t *iter;
10152
10153 if (!tmpl
10154 || (!CLASS_TYPE_P (tmpl)
10155 && TREE_CODE (tmpl) != FUNCTION_DECL))
10156 return;
10157
10158 saved_location = input_location;
10159 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10160 {
10161 tree type_decl = iter->typedef_decl;
10162 tree type_scope = iter->context;
10163
10164 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10165 continue;
10166
10167 if (uses_template_parms (type_decl))
10168 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10169 if (uses_template_parms (type_scope))
10170 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10171
10172 /* Make access check error messages point to the location
10173 of the use of the typedef. */
10174 input_location = iter->locus;
10175 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10176 type_decl, type_decl,
10177 tf_warning_or_error);
10178 }
10179 input_location = saved_location;
10180 }
10181
10182 static tree
10183 instantiate_class_template_1 (tree type)
10184 {
10185 tree templ, args, pattern, t, member;
10186 tree typedecl;
10187 tree pbinfo;
10188 tree base_list;
10189 unsigned int saved_maximum_field_alignment;
10190 tree fn_context;
10191
10192 if (type == error_mark_node)
10193 return error_mark_node;
10194
10195 if (COMPLETE_OR_OPEN_TYPE_P (type)
10196 || uses_template_parms (type))
10197 return type;
10198
10199 /* Figure out which template is being instantiated. */
10200 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10201 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10202
10203 /* Determine what specialization of the original template to
10204 instantiate. */
10205 t = most_specialized_partial_spec (type, tf_warning_or_error);
10206 if (t == error_mark_node)
10207 {
10208 TYPE_BEING_DEFINED (type) = 1;
10209 return error_mark_node;
10210 }
10211 else if (t)
10212 {
10213 /* This TYPE is actually an instantiation of a partial
10214 specialization. We replace the innermost set of ARGS with
10215 the arguments appropriate for substitution. For example,
10216 given:
10217
10218 template <class T> struct S {};
10219 template <class T> struct S<T*> {};
10220
10221 and supposing that we are instantiating S<int*>, ARGS will
10222 presently be {int*} -- but we need {int}. */
10223 pattern = TREE_TYPE (t);
10224 args = TREE_PURPOSE (t);
10225 }
10226 else
10227 {
10228 pattern = TREE_TYPE (templ);
10229 args = CLASSTYPE_TI_ARGS (type);
10230 }
10231
10232 /* If the template we're instantiating is incomplete, then clearly
10233 there's nothing we can do. */
10234 if (!COMPLETE_TYPE_P (pattern))
10235 return type;
10236
10237 /* If we've recursively instantiated too many templates, stop. */
10238 if (! push_tinst_level (type))
10239 return type;
10240
10241 /* Now we're really doing the instantiation. Mark the type as in
10242 the process of being defined. */
10243 TYPE_BEING_DEFINED (type) = 1;
10244
10245 /* We may be in the middle of deferred access check. Disable
10246 it now. */
10247 push_deferring_access_checks (dk_no_deferred);
10248
10249 int saved_unevaluated_operand = cp_unevaluated_operand;
10250 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10251
10252 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10253 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10254 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10255 fn_context = error_mark_node;
10256 if (!fn_context)
10257 push_to_top_level ();
10258 else
10259 {
10260 cp_unevaluated_operand = 0;
10261 c_inhibit_evaluation_warnings = 0;
10262 }
10263 /* Use #pragma pack from the template context. */
10264 saved_maximum_field_alignment = maximum_field_alignment;
10265 maximum_field_alignment = TYPE_PRECISION (pattern);
10266
10267 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10268
10269 /* Set the input location to the most specialized template definition.
10270 This is needed if tsubsting causes an error. */
10271 typedecl = TYPE_MAIN_DECL (pattern);
10272 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10273 DECL_SOURCE_LOCATION (typedecl);
10274
10275 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10276 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10277 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10278 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10279 if (ANON_AGGR_TYPE_P (pattern))
10280 SET_ANON_AGGR_TYPE_P (type);
10281 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10282 {
10283 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10284 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10285 /* Adjust visibility for template arguments. */
10286 determine_visibility (TYPE_MAIN_DECL (type));
10287 }
10288 if (CLASS_TYPE_P (type))
10289 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10290
10291 pbinfo = TYPE_BINFO (pattern);
10292
10293 /* We should never instantiate a nested class before its enclosing
10294 class; we need to look up the nested class by name before we can
10295 instantiate it, and that lookup should instantiate the enclosing
10296 class. */
10297 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10298 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10299
10300 base_list = NULL_TREE;
10301 if (BINFO_N_BASE_BINFOS (pbinfo))
10302 {
10303 tree pbase_binfo;
10304 tree pushed_scope;
10305 int i;
10306
10307 /* We must enter the scope containing the type, as that is where
10308 the accessibility of types named in dependent bases are
10309 looked up from. */
10310 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10311
10312 /* Substitute into each of the bases to determine the actual
10313 basetypes. */
10314 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10315 {
10316 tree base;
10317 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10318 tree expanded_bases = NULL_TREE;
10319 int idx, len = 1;
10320
10321 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10322 {
10323 expanded_bases =
10324 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10325 args, tf_error, NULL_TREE);
10326 if (expanded_bases == error_mark_node)
10327 continue;
10328
10329 len = TREE_VEC_LENGTH (expanded_bases);
10330 }
10331
10332 for (idx = 0; idx < len; idx++)
10333 {
10334 if (expanded_bases)
10335 /* Extract the already-expanded base class. */
10336 base = TREE_VEC_ELT (expanded_bases, idx);
10337 else
10338 /* Substitute to figure out the base class. */
10339 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10340 NULL_TREE);
10341
10342 if (base == error_mark_node)
10343 continue;
10344
10345 base_list = tree_cons (access, base, base_list);
10346 if (BINFO_VIRTUAL_P (pbase_binfo))
10347 TREE_TYPE (base_list) = integer_type_node;
10348 }
10349 }
10350
10351 /* The list is now in reverse order; correct that. */
10352 base_list = nreverse (base_list);
10353
10354 if (pushed_scope)
10355 pop_scope (pushed_scope);
10356 }
10357 /* Now call xref_basetypes to set up all the base-class
10358 information. */
10359 xref_basetypes (type, base_list);
10360
10361 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10362 (int) ATTR_FLAG_TYPE_IN_PLACE,
10363 args, tf_error, NULL_TREE);
10364 fixup_attribute_variants (type);
10365
10366 /* Now that our base classes are set up, enter the scope of the
10367 class, so that name lookups into base classes, etc. will work
10368 correctly. This is precisely analogous to what we do in
10369 begin_class_definition when defining an ordinary non-template
10370 class, except we also need to push the enclosing classes. */
10371 push_nested_class (type);
10372
10373 /* Now members are processed in the order of declaration. */
10374 for (member = CLASSTYPE_DECL_LIST (pattern);
10375 member; member = TREE_CHAIN (member))
10376 {
10377 tree t = TREE_VALUE (member);
10378
10379 if (TREE_PURPOSE (member))
10380 {
10381 if (TYPE_P (t))
10382 {
10383 /* Build new CLASSTYPE_NESTED_UTDS. */
10384
10385 tree newtag;
10386 bool class_template_p;
10387
10388 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10389 && TYPE_LANG_SPECIFIC (t)
10390 && CLASSTYPE_IS_TEMPLATE (t));
10391 /* If the member is a class template, then -- even after
10392 substitution -- there may be dependent types in the
10393 template argument list for the class. We increment
10394 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10395 that function will assume that no types are dependent
10396 when outside of a template. */
10397 if (class_template_p)
10398 ++processing_template_decl;
10399 newtag = tsubst (t, args, tf_error, NULL_TREE);
10400 if (class_template_p)
10401 --processing_template_decl;
10402 if (newtag == error_mark_node)
10403 continue;
10404
10405 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10406 {
10407 tree name = TYPE_IDENTIFIER (t);
10408
10409 if (class_template_p)
10410 /* Unfortunately, lookup_template_class sets
10411 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10412 instantiation (i.e., for the type of a member
10413 template class nested within a template class.)
10414 This behavior is required for
10415 maybe_process_partial_specialization to work
10416 correctly, but is not accurate in this case;
10417 the TAG is not an instantiation of anything.
10418 (The corresponding TEMPLATE_DECL is an
10419 instantiation, but the TYPE is not.) */
10420 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10421
10422 /* Now, we call pushtag to put this NEWTAG into the scope of
10423 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10424 pushtag calling push_template_decl. We don't have to do
10425 this for enums because it will already have been done in
10426 tsubst_enum. */
10427 if (name)
10428 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10429 pushtag (name, newtag, /*tag_scope=*/ts_current);
10430 }
10431 }
10432 else if (DECL_DECLARES_FUNCTION_P (t))
10433 {
10434 /* Build new TYPE_METHODS. */
10435 tree r;
10436
10437 if (TREE_CODE (t) == TEMPLATE_DECL)
10438 ++processing_template_decl;
10439 r = tsubst (t, args, tf_error, NULL_TREE);
10440 if (TREE_CODE (t) == TEMPLATE_DECL)
10441 --processing_template_decl;
10442 set_current_access_from_decl (r);
10443 finish_member_declaration (r);
10444 /* Instantiate members marked with attribute used. */
10445 if (r != error_mark_node && DECL_PRESERVE_P (r))
10446 mark_used (r);
10447 if (TREE_CODE (r) == FUNCTION_DECL
10448 && DECL_OMP_DECLARE_REDUCTION_P (r))
10449 cp_check_omp_declare_reduction (r);
10450 }
10451 else if (DECL_CLASS_TEMPLATE_P (t)
10452 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10453 /* A closure type for a lambda in a default argument for a
10454 member template. Ignore it; it will be instantiated with
10455 the default argument. */;
10456 else
10457 {
10458 /* Build new TYPE_FIELDS. */
10459 if (TREE_CODE (t) == STATIC_ASSERT)
10460 {
10461 tree condition;
10462
10463 ++c_inhibit_evaluation_warnings;
10464 condition =
10465 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10466 tf_warning_or_error, NULL_TREE,
10467 /*integral_constant_expression_p=*/true);
10468 --c_inhibit_evaluation_warnings;
10469
10470 finish_static_assert (condition,
10471 STATIC_ASSERT_MESSAGE (t),
10472 STATIC_ASSERT_SOURCE_LOCATION (t),
10473 /*member_p=*/true);
10474 }
10475 else if (TREE_CODE (t) != CONST_DECL)
10476 {
10477 tree r;
10478 tree vec = NULL_TREE;
10479 int len = 1;
10480
10481 /* The file and line for this declaration, to
10482 assist in error message reporting. Since we
10483 called push_tinst_level above, we don't need to
10484 restore these. */
10485 input_location = DECL_SOURCE_LOCATION (t);
10486
10487 if (TREE_CODE (t) == TEMPLATE_DECL)
10488 ++processing_template_decl;
10489 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10490 if (TREE_CODE (t) == TEMPLATE_DECL)
10491 --processing_template_decl;
10492
10493 if (TREE_CODE (r) == TREE_VEC)
10494 {
10495 /* A capture pack became multiple fields. */
10496 vec = r;
10497 len = TREE_VEC_LENGTH (vec);
10498 }
10499
10500 for (int i = 0; i < len; ++i)
10501 {
10502 if (vec)
10503 r = TREE_VEC_ELT (vec, i);
10504 if (VAR_P (r))
10505 {
10506 /* In [temp.inst]:
10507
10508 [t]he initialization (and any associated
10509 side-effects) of a static data member does
10510 not occur unless the static data member is
10511 itself used in a way that requires the
10512 definition of the static data member to
10513 exist.
10514
10515 Therefore, we do not substitute into the
10516 initialized for the static data member here. */
10517 finish_static_data_member_decl
10518 (r,
10519 /*init=*/NULL_TREE,
10520 /*init_const_expr_p=*/false,
10521 /*asmspec_tree=*/NULL_TREE,
10522 /*flags=*/0);
10523 /* Instantiate members marked with attribute used. */
10524 if (r != error_mark_node && DECL_PRESERVE_P (r))
10525 mark_used (r);
10526 }
10527 else if (TREE_CODE (r) == FIELD_DECL)
10528 {
10529 /* Determine whether R has a valid type and can be
10530 completed later. If R is invalid, then its type
10531 is replaced by error_mark_node. */
10532 tree rtype = TREE_TYPE (r);
10533 if (can_complete_type_without_circularity (rtype))
10534 complete_type (rtype);
10535
10536 if (!complete_or_array_type_p (rtype))
10537 {
10538 /* If R's type couldn't be completed and
10539 it isn't a flexible array member (whose
10540 type is incomplete by definition) give
10541 an error. */
10542 cxx_incomplete_type_error (r, rtype);
10543 TREE_TYPE (r) = error_mark_node;
10544 }
10545 }
10546
10547 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10548 such a thing will already have been added to the field
10549 list by tsubst_enum in finish_member_declaration in the
10550 CLASSTYPE_NESTED_UTDS case above. */
10551 if (!(TREE_CODE (r) == TYPE_DECL
10552 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10553 && DECL_ARTIFICIAL (r)))
10554 {
10555 set_current_access_from_decl (r);
10556 finish_member_declaration (r);
10557 }
10558 }
10559 }
10560 }
10561 }
10562 else
10563 {
10564 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10565 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10566 {
10567 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10568
10569 tree friend_type = t;
10570 bool adjust_processing_template_decl = false;
10571
10572 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10573 {
10574 /* template <class T> friend class C; */
10575 friend_type = tsubst_friend_class (friend_type, args);
10576 adjust_processing_template_decl = true;
10577 }
10578 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10579 {
10580 /* template <class T> friend class C::D; */
10581 friend_type = tsubst (friend_type, args,
10582 tf_warning_or_error, NULL_TREE);
10583 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10584 friend_type = TREE_TYPE (friend_type);
10585 adjust_processing_template_decl = true;
10586 }
10587 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10588 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10589 {
10590 /* This could be either
10591
10592 friend class T::C;
10593
10594 when dependent_type_p is false or
10595
10596 template <class U> friend class T::C;
10597
10598 otherwise. */
10599 /* Bump processing_template_decl in case this is something like
10600 template <class T> friend struct A<T>::B. */
10601 ++processing_template_decl;
10602 friend_type = tsubst (friend_type, args,
10603 tf_warning_or_error, NULL_TREE);
10604 if (dependent_type_p (friend_type))
10605 adjust_processing_template_decl = true;
10606 --processing_template_decl;
10607 }
10608 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10609 && hidden_name_p (TYPE_NAME (friend_type)))
10610 {
10611 /* friend class C;
10612
10613 where C hasn't been declared yet. Let's lookup name
10614 from namespace scope directly, bypassing any name that
10615 come from dependent base class. */
10616 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10617
10618 /* The call to xref_tag_from_type does injection for friend
10619 classes. */
10620 push_nested_namespace (ns);
10621 friend_type =
10622 xref_tag_from_type (friend_type, NULL_TREE,
10623 /*tag_scope=*/ts_current);
10624 pop_nested_namespace (ns);
10625 }
10626 else if (uses_template_parms (friend_type))
10627 /* friend class C<T>; */
10628 friend_type = tsubst (friend_type, args,
10629 tf_warning_or_error, NULL_TREE);
10630 /* Otherwise it's
10631
10632 friend class C;
10633
10634 where C is already declared or
10635
10636 friend class C<int>;
10637
10638 We don't have to do anything in these cases. */
10639
10640 if (adjust_processing_template_decl)
10641 /* Trick make_friend_class into realizing that the friend
10642 we're adding is a template, not an ordinary class. It's
10643 important that we use make_friend_class since it will
10644 perform some error-checking and output cross-reference
10645 information. */
10646 ++processing_template_decl;
10647
10648 if (friend_type != error_mark_node)
10649 make_friend_class (type, friend_type, /*complain=*/false);
10650
10651 if (adjust_processing_template_decl)
10652 --processing_template_decl;
10653 }
10654 else
10655 {
10656 /* Build new DECL_FRIENDLIST. */
10657 tree r;
10658
10659 /* The file and line for this declaration, to
10660 assist in error message reporting. Since we
10661 called push_tinst_level above, we don't need to
10662 restore these. */
10663 input_location = DECL_SOURCE_LOCATION (t);
10664
10665 if (TREE_CODE (t) == TEMPLATE_DECL)
10666 {
10667 ++processing_template_decl;
10668 push_deferring_access_checks (dk_no_check);
10669 }
10670
10671 r = tsubst_friend_function (t, args);
10672 add_friend (type, r, /*complain=*/false);
10673 if (TREE_CODE (t) == TEMPLATE_DECL)
10674 {
10675 pop_deferring_access_checks ();
10676 --processing_template_decl;
10677 }
10678 }
10679 }
10680 }
10681
10682 if (fn_context)
10683 {
10684 /* Restore these before substituting into the lambda capture
10685 initializers. */
10686 cp_unevaluated_operand = saved_unevaluated_operand;
10687 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10688 }
10689
10690 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10691 {
10692 tree decl = lambda_function (type);
10693 if (decl)
10694 {
10695 if (cxx_dialect >= cxx1z)
10696 CLASSTYPE_LITERAL_P (type) = true;
10697
10698 if (!DECL_TEMPLATE_INFO (decl)
10699 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10700 {
10701 /* Set function_depth to avoid garbage collection. */
10702 ++function_depth;
10703 instantiate_decl (decl, /*defer_ok=*/false, false);
10704 --function_depth;
10705 }
10706
10707 /* We need to instantiate the capture list from the template
10708 after we've instantiated the closure members, but before we
10709 consider adding the conversion op. Also keep any captures
10710 that may have been added during instantiation of the op(). */
10711 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10712 tree tmpl_cap
10713 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10714 args, tf_warning_or_error, NULL_TREE,
10715 false, false);
10716
10717 LAMBDA_EXPR_CAPTURE_LIST (expr)
10718 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10719
10720 maybe_add_lambda_conv_op (type);
10721 }
10722 else
10723 gcc_assert (errorcount);
10724 }
10725
10726 /* Set the file and line number information to whatever is given for
10727 the class itself. This puts error messages involving generated
10728 implicit functions at a predictable point, and the same point
10729 that would be used for non-template classes. */
10730 input_location = DECL_SOURCE_LOCATION (typedecl);
10731
10732 unreverse_member_declarations (type);
10733 finish_struct_1 (type);
10734 TYPE_BEING_DEFINED (type) = 0;
10735
10736 /* We don't instantiate default arguments for member functions. 14.7.1:
10737
10738 The implicit instantiation of a class template specialization causes
10739 the implicit instantiation of the declarations, but not of the
10740 definitions or default arguments, of the class member functions,
10741 member classes, static data members and member templates.... */
10742
10743 /* Some typedefs referenced from within the template code need to be access
10744 checked at template instantiation time, i.e now. These types were
10745 added to the template at parsing time. Let's get those and perform
10746 the access checks then. */
10747 perform_typedefs_access_check (pattern, args);
10748 perform_deferred_access_checks (tf_warning_or_error);
10749 pop_nested_class ();
10750 maximum_field_alignment = saved_maximum_field_alignment;
10751 if (!fn_context)
10752 pop_from_top_level ();
10753 pop_deferring_access_checks ();
10754 pop_tinst_level ();
10755
10756 /* The vtable for a template class can be emitted in any translation
10757 unit in which the class is instantiated. When there is no key
10758 method, however, finish_struct_1 will already have added TYPE to
10759 the keyed_classes list. */
10760 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10761 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10762
10763 return type;
10764 }
10765
10766 /* Wrapper for instantiate_class_template_1. */
10767
10768 tree
10769 instantiate_class_template (tree type)
10770 {
10771 tree ret;
10772 timevar_push (TV_TEMPLATE_INST);
10773 ret = instantiate_class_template_1 (type);
10774 timevar_pop (TV_TEMPLATE_INST);
10775 return ret;
10776 }
10777
10778 static tree
10779 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10780 {
10781 tree r;
10782
10783 if (!t)
10784 r = t;
10785 else if (TYPE_P (t))
10786 r = tsubst (t, args, complain, in_decl);
10787 else
10788 {
10789 if (!(complain & tf_warning))
10790 ++c_inhibit_evaluation_warnings;
10791 r = tsubst_expr (t, args, complain, in_decl,
10792 /*integral_constant_expression_p=*/true);
10793 if (!(complain & tf_warning))
10794 --c_inhibit_evaluation_warnings;
10795 }
10796 return r;
10797 }
10798
10799 /* Given a function parameter pack TMPL_PARM and some function parameters
10800 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10801 and set *SPEC_P to point at the next point in the list. */
10802
10803 tree
10804 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10805 {
10806 /* Collect all of the extra "packed" parameters into an
10807 argument pack. */
10808 tree parmvec;
10809 tree parmtypevec;
10810 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10811 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10812 tree spec_parm = *spec_p;
10813 int i, len;
10814
10815 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10816 if (tmpl_parm
10817 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10818 break;
10819
10820 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10821 parmvec = make_tree_vec (len);
10822 parmtypevec = make_tree_vec (len);
10823 spec_parm = *spec_p;
10824 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10825 {
10826 TREE_VEC_ELT (parmvec, i) = spec_parm;
10827 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10828 }
10829
10830 /* Build the argument packs. */
10831 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10832 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10833 TREE_TYPE (argpack) = argtypepack;
10834 *spec_p = spec_parm;
10835
10836 return argpack;
10837 }
10838
10839 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10840 NONTYPE_ARGUMENT_PACK. */
10841
10842 static tree
10843 make_fnparm_pack (tree spec_parm)
10844 {
10845 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10846 }
10847
10848 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10849 pack expansion with no extra args, 2 if it has extra args, or 0
10850 if it is not a pack expansion. */
10851
10852 static int
10853 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10854 {
10855 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10856 if (i >= TREE_VEC_LENGTH (vec))
10857 return 0;
10858 tree elt = TREE_VEC_ELT (vec, i);
10859 if (DECL_P (elt))
10860 /* A decl pack is itself an expansion. */
10861 elt = TREE_TYPE (elt);
10862 if (!PACK_EXPANSION_P (elt))
10863 return 0;
10864 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10865 return 2;
10866 return 1;
10867 }
10868
10869
10870 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10871
10872 static tree
10873 make_argument_pack_select (tree arg_pack, unsigned index)
10874 {
10875 tree aps = make_node (ARGUMENT_PACK_SELECT);
10876
10877 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10878 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10879
10880 return aps;
10881 }
10882
10883 /* This is a subroutine of tsubst_pack_expansion.
10884
10885 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10886 mechanism to store the (non complete list of) arguments of the
10887 substitution and return a non substituted pack expansion, in order
10888 to wait for when we have enough arguments to really perform the
10889 substitution. */
10890
10891 static bool
10892 use_pack_expansion_extra_args_p (tree parm_packs,
10893 int arg_pack_len,
10894 bool has_empty_arg)
10895 {
10896 /* If one pack has an expansion and another pack has a normal
10897 argument or if one pack has an empty argument and an another
10898 one hasn't then tsubst_pack_expansion cannot perform the
10899 substitution and need to fall back on the
10900 PACK_EXPANSION_EXTRA mechanism. */
10901 if (parm_packs == NULL_TREE)
10902 return false;
10903 else if (has_empty_arg)
10904 return true;
10905
10906 bool has_expansion_arg = false;
10907 for (int i = 0 ; i < arg_pack_len; ++i)
10908 {
10909 bool has_non_expansion_arg = false;
10910 for (tree parm_pack = parm_packs;
10911 parm_pack;
10912 parm_pack = TREE_CHAIN (parm_pack))
10913 {
10914 tree arg = TREE_VALUE (parm_pack);
10915
10916 int exp = argument_pack_element_is_expansion_p (arg, i);
10917 if (exp == 2)
10918 /* We can't substitute a pack expansion with extra args into
10919 our pattern. */
10920 return true;
10921 else if (exp)
10922 has_expansion_arg = true;
10923 else
10924 has_non_expansion_arg = true;
10925 }
10926
10927 if (has_expansion_arg && has_non_expansion_arg)
10928 return true;
10929 }
10930 return false;
10931 }
10932
10933 /* [temp.variadic]/6 says that:
10934
10935 The instantiation of a pack expansion [...]
10936 produces a list E1,E2, ..., En, where N is the number of elements
10937 in the pack expansion parameters.
10938
10939 This subroutine of tsubst_pack_expansion produces one of these Ei.
10940
10941 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10942 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10943 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10944 INDEX is the index 'i' of the element Ei to produce. ARGS,
10945 COMPLAIN, and IN_DECL are the same parameters as for the
10946 tsubst_pack_expansion function.
10947
10948 The function returns the resulting Ei upon successful completion,
10949 or error_mark_node.
10950
10951 Note that this function possibly modifies the ARGS parameter, so
10952 it's the responsibility of the caller to restore it. */
10953
10954 static tree
10955 gen_elem_of_pack_expansion_instantiation (tree pattern,
10956 tree parm_packs,
10957 unsigned index,
10958 tree args /* This parm gets
10959 modified. */,
10960 tsubst_flags_t complain,
10961 tree in_decl)
10962 {
10963 tree t;
10964 bool ith_elem_is_expansion = false;
10965
10966 /* For each parameter pack, change the substitution of the parameter
10967 pack to the ith argument in its argument pack, then expand the
10968 pattern. */
10969 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10970 {
10971 tree parm = TREE_PURPOSE (pack);
10972 tree arg_pack = TREE_VALUE (pack);
10973 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10974
10975 ith_elem_is_expansion |=
10976 argument_pack_element_is_expansion_p (arg_pack, index);
10977
10978 /* Select the Ith argument from the pack. */
10979 if (TREE_CODE (parm) == PARM_DECL
10980 || TREE_CODE (parm) == FIELD_DECL)
10981 {
10982 if (index == 0)
10983 {
10984 aps = make_argument_pack_select (arg_pack, index);
10985 if (!mark_used (parm, complain) && !(complain & tf_error))
10986 return error_mark_node;
10987 register_local_specialization (aps, parm);
10988 }
10989 else
10990 aps = retrieve_local_specialization (parm);
10991 }
10992 else
10993 {
10994 int idx, level;
10995 template_parm_level_and_index (parm, &level, &idx);
10996
10997 if (index == 0)
10998 {
10999 aps = make_argument_pack_select (arg_pack, index);
11000 /* Update the corresponding argument. */
11001 TMPL_ARG (args, level, idx) = aps;
11002 }
11003 else
11004 /* Re-use the ARGUMENT_PACK_SELECT. */
11005 aps = TMPL_ARG (args, level, idx);
11006 }
11007 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11008 }
11009
11010 /* Substitute into the PATTERN with the (possibly altered)
11011 arguments. */
11012 if (pattern == in_decl)
11013 /* Expanding a fixed parameter pack from
11014 coerce_template_parameter_pack. */
11015 t = tsubst_decl (pattern, args, complain);
11016 else if (pattern == error_mark_node)
11017 t = error_mark_node;
11018 else if (constraint_p (pattern))
11019 {
11020 if (processing_template_decl)
11021 t = tsubst_constraint (pattern, args, complain, in_decl);
11022 else
11023 t = (constraints_satisfied_p (pattern, args)
11024 ? boolean_true_node : boolean_false_node);
11025 }
11026 else if (!TYPE_P (pattern))
11027 t = tsubst_expr (pattern, args, complain, in_decl,
11028 /*integral_constant_expression_p=*/false);
11029 else
11030 t = tsubst (pattern, args, complain, in_decl);
11031
11032 /* If the Ith argument pack element is a pack expansion, then
11033 the Ith element resulting from the substituting is going to
11034 be a pack expansion as well. */
11035 if (ith_elem_is_expansion)
11036 t = make_pack_expansion (t);
11037
11038 return t;
11039 }
11040
11041 /* When the unexpanded parameter pack in a fold expression expands to an empty
11042 sequence, the value of the expression is as follows; the program is
11043 ill-formed if the operator is not listed in this table.
11044
11045 && true
11046 || false
11047 , void() */
11048
11049 tree
11050 expand_empty_fold (tree t, tsubst_flags_t complain)
11051 {
11052 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11053 if (!FOLD_EXPR_MODIFY_P (t))
11054 switch (code)
11055 {
11056 case TRUTH_ANDIF_EXPR:
11057 return boolean_true_node;
11058 case TRUTH_ORIF_EXPR:
11059 return boolean_false_node;
11060 case COMPOUND_EXPR:
11061 return void_node;
11062 default:
11063 break;
11064 }
11065
11066 if (complain & tf_error)
11067 error_at (location_of (t),
11068 "fold of empty expansion over %O", code);
11069 return error_mark_node;
11070 }
11071
11072 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11073 form an expression that combines the two terms using the
11074 operator of T. */
11075
11076 static tree
11077 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11078 {
11079 tree op = FOLD_EXPR_OP (t);
11080 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11081
11082 // Handle compound assignment operators.
11083 if (FOLD_EXPR_MODIFY_P (t))
11084 return build_x_modify_expr (input_location, left, code, right, complain);
11085
11086 switch (code)
11087 {
11088 case COMPOUND_EXPR:
11089 return build_x_compound_expr (input_location, left, right, complain);
11090 case DOTSTAR_EXPR:
11091 return build_m_component_ref (left, right, complain);
11092 default:
11093 return build_x_binary_op (input_location, code,
11094 left, TREE_CODE (left),
11095 right, TREE_CODE (right),
11096 /*overload=*/NULL,
11097 complain);
11098 }
11099 }
11100
11101 /* Substitute ARGS into the pack of a fold expression T. */
11102
11103 static inline tree
11104 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11105 {
11106 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11107 }
11108
11109 /* Substitute ARGS into the pack of a fold expression T. */
11110
11111 static inline tree
11112 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11113 {
11114 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11115 }
11116
11117 /* Expand a PACK of arguments into a grouped as left fold.
11118 Given a pack containing elements A0, A1, ..., An and an
11119 operator @, this builds the expression:
11120
11121 ((A0 @ A1) @ A2) ... @ An
11122
11123 Note that PACK must not be empty.
11124
11125 The operator is defined by the original fold expression T. */
11126
11127 static tree
11128 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11129 {
11130 tree left = TREE_VEC_ELT (pack, 0);
11131 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11132 {
11133 tree right = TREE_VEC_ELT (pack, i);
11134 left = fold_expression (t, left, right, complain);
11135 }
11136 return left;
11137 }
11138
11139 /* Substitute into a unary left fold expression. */
11140
11141 static tree
11142 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11143 tree in_decl)
11144 {
11145 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11146 if (pack == error_mark_node)
11147 return error_mark_node;
11148 if (PACK_EXPANSION_P (pack))
11149 {
11150 tree r = copy_node (t);
11151 FOLD_EXPR_PACK (r) = pack;
11152 return r;
11153 }
11154 if (TREE_VEC_LENGTH (pack) == 0)
11155 return expand_empty_fold (t, complain);
11156 else
11157 return expand_left_fold (t, pack, complain);
11158 }
11159
11160 /* Substitute into a binary left fold expression.
11161
11162 Do ths by building a single (non-empty) vector of argumnts and
11163 building the expression from those elements. */
11164
11165 static tree
11166 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11167 tree in_decl)
11168 {
11169 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11170 if (pack == error_mark_node)
11171 return error_mark_node;
11172 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11173 if (init == error_mark_node)
11174 return error_mark_node;
11175
11176 if (PACK_EXPANSION_P (pack))
11177 {
11178 tree r = copy_node (t);
11179 FOLD_EXPR_PACK (r) = pack;
11180 FOLD_EXPR_INIT (r) = init;
11181 return r;
11182 }
11183
11184 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11185 TREE_VEC_ELT (vec, 0) = init;
11186 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11187 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11188
11189 return expand_left_fold (t, vec, complain);
11190 }
11191
11192 /* Expand a PACK of arguments into a grouped as right fold.
11193 Given a pack containing elementns A0, A1, ..., and an
11194 operator @, this builds the expression:
11195
11196 A0@ ... (An-2 @ (An-1 @ An))
11197
11198 Note that PACK must not be empty.
11199
11200 The operator is defined by the original fold expression T. */
11201
11202 tree
11203 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11204 {
11205 // Build the expression.
11206 int n = TREE_VEC_LENGTH (pack);
11207 tree right = TREE_VEC_ELT (pack, n - 1);
11208 for (--n; n != 0; --n)
11209 {
11210 tree left = TREE_VEC_ELT (pack, n - 1);
11211 right = fold_expression (t, left, right, complain);
11212 }
11213 return right;
11214 }
11215
11216 /* Substitute into a unary right fold expression. */
11217
11218 static tree
11219 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11220 tree in_decl)
11221 {
11222 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11223 if (pack == error_mark_node)
11224 return error_mark_node;
11225 if (PACK_EXPANSION_P (pack))
11226 {
11227 tree r = copy_node (t);
11228 FOLD_EXPR_PACK (r) = pack;
11229 return r;
11230 }
11231 if (TREE_VEC_LENGTH (pack) == 0)
11232 return expand_empty_fold (t, complain);
11233 else
11234 return expand_right_fold (t, pack, complain);
11235 }
11236
11237 /* Substitute into a binary right fold expression.
11238
11239 Do ths by building a single (non-empty) vector of arguments and
11240 building the expression from those elements. */
11241
11242 static tree
11243 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11244 tree in_decl)
11245 {
11246 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11247 if (pack == error_mark_node)
11248 return error_mark_node;
11249 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11250 if (init == error_mark_node)
11251 return error_mark_node;
11252
11253 if (PACK_EXPANSION_P (pack))
11254 {
11255 tree r = copy_node (t);
11256 FOLD_EXPR_PACK (r) = pack;
11257 FOLD_EXPR_INIT (r) = init;
11258 return r;
11259 }
11260
11261 int n = TREE_VEC_LENGTH (pack);
11262 tree vec = make_tree_vec (n + 1);
11263 for (int i = 0; i < n; ++i)
11264 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11265 TREE_VEC_ELT (vec, n) = init;
11266
11267 return expand_right_fold (t, vec, complain);
11268 }
11269
11270
11271 /* Substitute ARGS into T, which is an pack expansion
11272 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11273 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11274 (if only a partial substitution could be performed) or
11275 ERROR_MARK_NODE if there was an error. */
11276 tree
11277 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11278 tree in_decl)
11279 {
11280 tree pattern;
11281 tree pack, packs = NULL_TREE;
11282 bool unsubstituted_packs = false;
11283 int i, len = -1;
11284 tree result;
11285 hash_map<tree, tree> *saved_local_specializations = NULL;
11286 bool need_local_specializations = false;
11287 int levels;
11288
11289 gcc_assert (PACK_EXPANSION_P (t));
11290 pattern = PACK_EXPANSION_PATTERN (t);
11291
11292 /* Add in any args remembered from an earlier partial instantiation. */
11293 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11294
11295 levels = TMPL_ARGS_DEPTH (args);
11296
11297 /* Determine the argument packs that will instantiate the parameter
11298 packs used in the expansion expression. While we're at it,
11299 compute the number of arguments to be expanded and make sure it
11300 is consistent. */
11301 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11302 pack = TREE_CHAIN (pack))
11303 {
11304 tree parm_pack = TREE_VALUE (pack);
11305 tree arg_pack = NULL_TREE;
11306 tree orig_arg = NULL_TREE;
11307 int level = 0;
11308
11309 if (TREE_CODE (parm_pack) == BASES)
11310 {
11311 if (BASES_DIRECT (parm_pack))
11312 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11313 args, complain, in_decl, false));
11314 else
11315 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11316 args, complain, in_decl, false));
11317 }
11318 if (TREE_CODE (parm_pack) == PARM_DECL)
11319 {
11320 /* We know we have correct local_specializations if this
11321 expansion is at function scope, or if we're dealing with a
11322 local parameter in a requires expression; for the latter,
11323 tsubst_requires_expr set it up appropriately. */
11324 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11325 arg_pack = retrieve_local_specialization (parm_pack);
11326 else
11327 /* We can't rely on local_specializations for a parameter
11328 name used later in a function declaration (such as in a
11329 late-specified return type). Even if it exists, it might
11330 have the wrong value for a recursive call. */
11331 need_local_specializations = true;
11332
11333 if (!arg_pack)
11334 {
11335 /* This parameter pack was used in an unevaluated context. Just
11336 make a dummy decl, since it's only used for its type. */
11337 arg_pack = tsubst_decl (parm_pack, args, complain);
11338 if (arg_pack && DECL_PACK_P (arg_pack))
11339 /* Partial instantiation of the parm_pack, we can't build
11340 up an argument pack yet. */
11341 arg_pack = NULL_TREE;
11342 else
11343 arg_pack = make_fnparm_pack (arg_pack);
11344 }
11345 }
11346 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11347 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11348 else
11349 {
11350 int idx;
11351 template_parm_level_and_index (parm_pack, &level, &idx);
11352
11353 if (level <= levels)
11354 arg_pack = TMPL_ARG (args, level, idx);
11355 }
11356
11357 orig_arg = arg_pack;
11358 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11359 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11360
11361 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11362 /* This can only happen if we forget to expand an argument
11363 pack somewhere else. Just return an error, silently. */
11364 {
11365 result = make_tree_vec (1);
11366 TREE_VEC_ELT (result, 0) = error_mark_node;
11367 return result;
11368 }
11369
11370 if (arg_pack)
11371 {
11372 int my_len =
11373 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11374
11375 /* Don't bother trying to do a partial substitution with
11376 incomplete packs; we'll try again after deduction. */
11377 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11378 return t;
11379
11380 if (len < 0)
11381 len = my_len;
11382 else if (len != my_len)
11383 {
11384 if (!(complain & tf_error))
11385 /* Fail quietly. */;
11386 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11387 error ("mismatched argument pack lengths while expanding "
11388 "%<%T%>",
11389 pattern);
11390 else
11391 error ("mismatched argument pack lengths while expanding "
11392 "%<%E%>",
11393 pattern);
11394 return error_mark_node;
11395 }
11396
11397 /* Keep track of the parameter packs and their corresponding
11398 argument packs. */
11399 packs = tree_cons (parm_pack, arg_pack, packs);
11400 TREE_TYPE (packs) = orig_arg;
11401 }
11402 else
11403 {
11404 /* We can't substitute for this parameter pack. We use a flag as
11405 well as the missing_level counter because function parameter
11406 packs don't have a level. */
11407 gcc_assert (processing_template_decl);
11408 unsubstituted_packs = true;
11409 }
11410 }
11411
11412 /* If the expansion is just T..., return the matching argument pack, unless
11413 we need to call convert_from_reference on all the elements. This is an
11414 important optimization; see c++/68422. */
11415 if (!unsubstituted_packs
11416 && TREE_PURPOSE (packs) == pattern)
11417 {
11418 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11419 /* Types need no adjustment, nor does sizeof..., and if we still have
11420 some pack expansion args we won't do anything yet. */
11421 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11422 || PACK_EXPANSION_SIZEOF_P (t)
11423 || pack_expansion_args_count (args))
11424 return args;
11425 /* Also optimize expression pack expansions if we can tell that the
11426 elements won't have reference type. */
11427 tree type = TREE_TYPE (pattern);
11428 if (type && TREE_CODE (type) != REFERENCE_TYPE
11429 && !PACK_EXPANSION_P (type)
11430 && !WILDCARD_TYPE_P (type))
11431 return args;
11432 /* Otherwise use the normal path so we get convert_from_reference. */
11433 }
11434
11435 /* We cannot expand this expansion expression, because we don't have
11436 all of the argument packs we need. */
11437 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11438 {
11439 /* We got some full packs, but we can't substitute them in until we
11440 have values for all the packs. So remember these until then. */
11441
11442 t = make_pack_expansion (pattern);
11443 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11444 return t;
11445 }
11446 else if (unsubstituted_packs)
11447 {
11448 /* There were no real arguments, we're just replacing a parameter
11449 pack with another version of itself. Substitute into the
11450 pattern and return a PACK_EXPANSION_*. The caller will need to
11451 deal with that. */
11452 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11453 t = tsubst_expr (pattern, args, complain, in_decl,
11454 /*integral_constant_expression_p=*/false);
11455 else
11456 t = tsubst (pattern, args, complain, in_decl);
11457 t = make_pack_expansion (t);
11458 return t;
11459 }
11460
11461 gcc_assert (len >= 0);
11462
11463 if (need_local_specializations)
11464 {
11465 /* We're in a late-specified return type, so create our own local
11466 specializations map; the current map is either NULL or (in the
11467 case of recursive unification) might have bindings that we don't
11468 want to use or alter. */
11469 saved_local_specializations = local_specializations;
11470 local_specializations = new hash_map<tree, tree>;
11471 }
11472
11473 /* For each argument in each argument pack, substitute into the
11474 pattern. */
11475 result = make_tree_vec (len);
11476 tree elem_args = copy_template_args (args);
11477 for (i = 0; i < len; ++i)
11478 {
11479 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11480 i,
11481 elem_args, complain,
11482 in_decl);
11483 TREE_VEC_ELT (result, i) = t;
11484 if (t == error_mark_node)
11485 {
11486 result = error_mark_node;
11487 break;
11488 }
11489 }
11490
11491 /* Update ARGS to restore the substitution from parameter packs to
11492 their argument packs. */
11493 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11494 {
11495 tree parm = TREE_PURPOSE (pack);
11496
11497 if (TREE_CODE (parm) == PARM_DECL
11498 || TREE_CODE (parm) == FIELD_DECL)
11499 register_local_specialization (TREE_TYPE (pack), parm);
11500 else
11501 {
11502 int idx, level;
11503
11504 if (TREE_VALUE (pack) == NULL_TREE)
11505 continue;
11506
11507 template_parm_level_and_index (parm, &level, &idx);
11508
11509 /* Update the corresponding argument. */
11510 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11511 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11512 TREE_TYPE (pack);
11513 else
11514 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11515 }
11516 }
11517
11518 if (need_local_specializations)
11519 {
11520 delete local_specializations;
11521 local_specializations = saved_local_specializations;
11522 }
11523
11524 /* If the dependent pack arguments were such that we end up with only a
11525 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11526 if (len == 1 && TREE_CODE (result) == TREE_VEC
11527 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11528 return TREE_VEC_ELT (result, 0);
11529
11530 return result;
11531 }
11532
11533 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11534 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11535 parameter packs; all parms generated from a function parameter pack will
11536 have the same DECL_PARM_INDEX. */
11537
11538 tree
11539 get_pattern_parm (tree parm, tree tmpl)
11540 {
11541 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11542 tree patparm;
11543
11544 if (DECL_ARTIFICIAL (parm))
11545 {
11546 for (patparm = DECL_ARGUMENTS (pattern);
11547 patparm; patparm = DECL_CHAIN (patparm))
11548 if (DECL_ARTIFICIAL (patparm)
11549 && DECL_NAME (parm) == DECL_NAME (patparm))
11550 break;
11551 }
11552 else
11553 {
11554 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11555 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11556 gcc_assert (DECL_PARM_INDEX (patparm)
11557 == DECL_PARM_INDEX (parm));
11558 }
11559
11560 return patparm;
11561 }
11562
11563 /* Make an argument pack out of the TREE_VEC VEC. */
11564
11565 static tree
11566 make_argument_pack (tree vec)
11567 {
11568 tree pack;
11569 tree elt = TREE_VEC_ELT (vec, 0);
11570 if (TYPE_P (elt))
11571 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11572 else
11573 {
11574 pack = make_node (NONTYPE_ARGUMENT_PACK);
11575 TREE_TYPE (pack) = TREE_TYPE (elt);
11576 TREE_CONSTANT (pack) = 1;
11577 }
11578 SET_ARGUMENT_PACK_ARGS (pack, vec);
11579 return pack;
11580 }
11581
11582 /* Return an exact copy of template args T that can be modified
11583 independently. */
11584
11585 static tree
11586 copy_template_args (tree t)
11587 {
11588 if (t == error_mark_node)
11589 return t;
11590
11591 int len = TREE_VEC_LENGTH (t);
11592 tree new_vec = make_tree_vec (len);
11593
11594 for (int i = 0; i < len; ++i)
11595 {
11596 tree elt = TREE_VEC_ELT (t, i);
11597 if (elt && TREE_CODE (elt) == TREE_VEC)
11598 elt = copy_template_args (elt);
11599 TREE_VEC_ELT (new_vec, i) = elt;
11600 }
11601
11602 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11603 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11604
11605 return new_vec;
11606 }
11607
11608 /* Substitute ARGS into the vector or list of template arguments T. */
11609
11610 static tree
11611 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11612 {
11613 tree orig_t = t;
11614 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11615 tree *elts;
11616
11617 if (t == error_mark_node)
11618 return error_mark_node;
11619
11620 len = TREE_VEC_LENGTH (t);
11621 elts = XALLOCAVEC (tree, len);
11622
11623 for (i = 0; i < len; i++)
11624 {
11625 tree orig_arg = TREE_VEC_ELT (t, i);
11626 tree new_arg;
11627
11628 if (TREE_CODE (orig_arg) == TREE_VEC)
11629 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11630 else if (PACK_EXPANSION_P (orig_arg))
11631 {
11632 /* Substitute into an expansion expression. */
11633 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11634
11635 if (TREE_CODE (new_arg) == TREE_VEC)
11636 /* Add to the expanded length adjustment the number of
11637 expanded arguments. We subtract one from this
11638 measurement, because the argument pack expression
11639 itself is already counted as 1 in
11640 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11641 the argument pack is empty. */
11642 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11643 }
11644 else if (ARGUMENT_PACK_P (orig_arg))
11645 {
11646 /* Substitute into each of the arguments. */
11647 new_arg = TYPE_P (orig_arg)
11648 ? cxx_make_type (TREE_CODE (orig_arg))
11649 : make_node (TREE_CODE (orig_arg));
11650
11651 SET_ARGUMENT_PACK_ARGS (
11652 new_arg,
11653 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11654 args, complain, in_decl));
11655
11656 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11657 new_arg = error_mark_node;
11658
11659 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11660 if (type_uses_auto (TREE_TYPE (orig_arg)))
11661 TREE_TYPE (new_arg) = TREE_TYPE (orig_arg);
11662 else
11663 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11664 complain, in_decl);
11665 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11666
11667 if (TREE_TYPE (new_arg) == error_mark_node)
11668 new_arg = error_mark_node;
11669 }
11670 }
11671 else
11672 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11673
11674 if (new_arg == error_mark_node)
11675 return error_mark_node;
11676
11677 elts[i] = new_arg;
11678 if (new_arg != orig_arg)
11679 need_new = 1;
11680 }
11681
11682 if (!need_new)
11683 return t;
11684
11685 /* Make space for the expanded arguments coming from template
11686 argument packs. */
11687 t = make_tree_vec (len + expanded_len_adjust);
11688 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11689 arguments for a member template.
11690 In that case each TREE_VEC in ORIG_T represents a level of template
11691 arguments, and ORIG_T won't carry any non defaulted argument count.
11692 It will rather be the nested TREE_VECs that will carry one.
11693 In other words, ORIG_T carries a non defaulted argument count only
11694 if it doesn't contain any nested TREE_VEC. */
11695 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11696 {
11697 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11698 count += expanded_len_adjust;
11699 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11700 }
11701 for (i = 0, out = 0; i < len; i++)
11702 {
11703 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11704 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11705 && TREE_CODE (elts[i]) == TREE_VEC)
11706 {
11707 int idx;
11708
11709 /* Now expand the template argument pack "in place". */
11710 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11711 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11712 }
11713 else
11714 {
11715 TREE_VEC_ELT (t, out) = elts[i];
11716 out++;
11717 }
11718 }
11719
11720 return t;
11721 }
11722
11723 /* Substitute ARGS into one level PARMS of template parameters. */
11724
11725 static tree
11726 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11727 {
11728 if (parms == error_mark_node)
11729 return error_mark_node;
11730
11731 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11732
11733 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11734 {
11735 tree tuple = TREE_VEC_ELT (parms, i);
11736
11737 if (tuple == error_mark_node)
11738 continue;
11739
11740 TREE_VEC_ELT (new_vec, i) =
11741 tsubst_template_parm (tuple, args, complain);
11742 }
11743
11744 return new_vec;
11745 }
11746
11747 /* Return the result of substituting ARGS into the template parameters
11748 given by PARMS. If there are m levels of ARGS and m + n levels of
11749 PARMS, then the result will contain n levels of PARMS. For
11750 example, if PARMS is `template <class T> template <class U>
11751 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11752 result will be `template <int*, double, class V>'. */
11753
11754 static tree
11755 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11756 {
11757 tree r = NULL_TREE;
11758 tree* new_parms;
11759
11760 /* When substituting into a template, we must set
11761 PROCESSING_TEMPLATE_DECL as the template parameters may be
11762 dependent if they are based on one-another, and the dependency
11763 predicates are short-circuit outside of templates. */
11764 ++processing_template_decl;
11765
11766 for (new_parms = &r;
11767 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11768 new_parms = &(TREE_CHAIN (*new_parms)),
11769 parms = TREE_CHAIN (parms))
11770 {
11771 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11772 args, complain);
11773 *new_parms =
11774 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11775 - TMPL_ARGS_DEPTH (args)),
11776 new_vec, NULL_TREE);
11777 }
11778
11779 --processing_template_decl;
11780
11781 return r;
11782 }
11783
11784 /* Return the result of substituting ARGS into one template parameter
11785 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11786 parameter and which TREE_PURPOSE is the default argument of the
11787 template parameter. */
11788
11789 static tree
11790 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11791 {
11792 tree default_value, parm_decl;
11793
11794 if (args == NULL_TREE
11795 || t == NULL_TREE
11796 || t == error_mark_node)
11797 return t;
11798
11799 gcc_assert (TREE_CODE (t) == TREE_LIST);
11800
11801 default_value = TREE_PURPOSE (t);
11802 parm_decl = TREE_VALUE (t);
11803
11804 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11805 if (TREE_CODE (parm_decl) == PARM_DECL
11806 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11807 parm_decl = error_mark_node;
11808 default_value = tsubst_template_arg (default_value, args,
11809 complain, NULL_TREE);
11810
11811 return build_tree_list (default_value, parm_decl);
11812 }
11813
11814 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11815 type T. If T is not an aggregate or enumeration type, it is
11816 handled as if by tsubst. IN_DECL is as for tsubst. If
11817 ENTERING_SCOPE is nonzero, T is the context for a template which
11818 we are presently tsubst'ing. Return the substituted value. */
11819
11820 static tree
11821 tsubst_aggr_type (tree t,
11822 tree args,
11823 tsubst_flags_t complain,
11824 tree in_decl,
11825 int entering_scope)
11826 {
11827 if (t == NULL_TREE)
11828 return NULL_TREE;
11829
11830 switch (TREE_CODE (t))
11831 {
11832 case RECORD_TYPE:
11833 if (TYPE_PTRMEMFUNC_P (t))
11834 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11835
11836 /* Fall through. */
11837 case ENUMERAL_TYPE:
11838 case UNION_TYPE:
11839 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11840 {
11841 tree argvec;
11842 tree context;
11843 tree r;
11844 int saved_unevaluated_operand;
11845 int saved_inhibit_evaluation_warnings;
11846
11847 /* In "sizeof(X<I>)" we need to evaluate "I". */
11848 saved_unevaluated_operand = cp_unevaluated_operand;
11849 cp_unevaluated_operand = 0;
11850 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11851 c_inhibit_evaluation_warnings = 0;
11852
11853 /* First, determine the context for the type we are looking
11854 up. */
11855 context = TYPE_CONTEXT (t);
11856 if (context && TYPE_P (context))
11857 {
11858 context = tsubst_aggr_type (context, args, complain,
11859 in_decl, /*entering_scope=*/1);
11860 /* If context is a nested class inside a class template,
11861 it may still need to be instantiated (c++/33959). */
11862 context = complete_type (context);
11863 }
11864
11865 /* Then, figure out what arguments are appropriate for the
11866 type we are trying to find. For example, given:
11867
11868 template <class T> struct S;
11869 template <class T, class U> void f(T, U) { S<U> su; }
11870
11871 and supposing that we are instantiating f<int, double>,
11872 then our ARGS will be {int, double}, but, when looking up
11873 S we only want {double}. */
11874 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11875 complain, in_decl);
11876 if (argvec == error_mark_node)
11877 r = error_mark_node;
11878 else
11879 {
11880 r = lookup_template_class (t, argvec, in_decl, context,
11881 entering_scope, complain);
11882 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11883 }
11884
11885 cp_unevaluated_operand = saved_unevaluated_operand;
11886 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11887
11888 return r;
11889 }
11890 else
11891 /* This is not a template type, so there's nothing to do. */
11892 return t;
11893
11894 default:
11895 return tsubst (t, args, complain, in_decl);
11896 }
11897 }
11898
11899 /* Substitute into the default argument ARG (a default argument for
11900 FN), which has the indicated TYPE. */
11901
11902 tree
11903 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11904 {
11905 tree saved_class_ptr = NULL_TREE;
11906 tree saved_class_ref = NULL_TREE;
11907 int errs = errorcount + sorrycount;
11908
11909 /* This can happen in invalid code. */
11910 if (TREE_CODE (arg) == DEFAULT_ARG)
11911 return arg;
11912
11913 /* This default argument came from a template. Instantiate the
11914 default argument here, not in tsubst. In the case of
11915 something like:
11916
11917 template <class T>
11918 struct S {
11919 static T t();
11920 void f(T = t());
11921 };
11922
11923 we must be careful to do name lookup in the scope of S<T>,
11924 rather than in the current class. */
11925 push_access_scope (fn);
11926 /* The "this" pointer is not valid in a default argument. */
11927 if (cfun)
11928 {
11929 saved_class_ptr = current_class_ptr;
11930 cp_function_chain->x_current_class_ptr = NULL_TREE;
11931 saved_class_ref = current_class_ref;
11932 cp_function_chain->x_current_class_ref = NULL_TREE;
11933 }
11934
11935 push_deferring_access_checks(dk_no_deferred);
11936 /* The default argument expression may cause implicitly defined
11937 member functions to be synthesized, which will result in garbage
11938 collection. We must treat this situation as if we were within
11939 the body of function so as to avoid collecting live data on the
11940 stack. */
11941 ++function_depth;
11942 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11943 complain, NULL_TREE,
11944 /*integral_constant_expression_p=*/false);
11945 --function_depth;
11946 pop_deferring_access_checks();
11947
11948 /* Restore the "this" pointer. */
11949 if (cfun)
11950 {
11951 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11952 cp_function_chain->x_current_class_ref = saved_class_ref;
11953 }
11954
11955 if (errorcount+sorrycount > errs
11956 && (complain & tf_warning_or_error))
11957 inform (input_location,
11958 " when instantiating default argument for call to %D", fn);
11959
11960 /* Make sure the default argument is reasonable. */
11961 arg = check_default_argument (type, arg, complain);
11962
11963 pop_access_scope (fn);
11964
11965 return arg;
11966 }
11967
11968 /* Substitute into all the default arguments for FN. */
11969
11970 static void
11971 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11972 {
11973 tree arg;
11974 tree tmpl_args;
11975
11976 tmpl_args = DECL_TI_ARGS (fn);
11977
11978 /* If this function is not yet instantiated, we certainly don't need
11979 its default arguments. */
11980 if (uses_template_parms (tmpl_args))
11981 return;
11982 /* Don't do this again for clones. */
11983 if (DECL_CLONED_FUNCTION_P (fn))
11984 return;
11985
11986 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11987 arg;
11988 arg = TREE_CHAIN (arg))
11989 if (TREE_PURPOSE (arg))
11990 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11991 TREE_VALUE (arg),
11992 TREE_PURPOSE (arg),
11993 complain);
11994 }
11995
11996 /* Substitute the ARGS into the T, which is a _DECL. Return the
11997 result of the substitution. Issue error and warning messages under
11998 control of COMPLAIN. */
11999
12000 static tree
12001 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12002 {
12003 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12004 location_t saved_loc;
12005 tree r = NULL_TREE;
12006 tree in_decl = t;
12007 hashval_t hash = 0;
12008
12009 /* Set the filename and linenumber to improve error-reporting. */
12010 saved_loc = input_location;
12011 input_location = DECL_SOURCE_LOCATION (t);
12012
12013 switch (TREE_CODE (t))
12014 {
12015 case TEMPLATE_DECL:
12016 {
12017 /* We can get here when processing a member function template,
12018 member class template, or template template parameter. */
12019 tree decl = DECL_TEMPLATE_RESULT (t);
12020 tree spec;
12021 tree tmpl_args;
12022 tree full_args;
12023
12024 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12025 {
12026 /* Template template parameter is treated here. */
12027 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12028 if (new_type == error_mark_node)
12029 r = error_mark_node;
12030 /* If we get a real template back, return it. This can happen in
12031 the context of most_specialized_partial_spec. */
12032 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12033 r = new_type;
12034 else
12035 /* The new TEMPLATE_DECL was built in
12036 reduce_template_parm_level. */
12037 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12038 break;
12039 }
12040
12041 /* We might already have an instance of this template.
12042 The ARGS are for the surrounding class type, so the
12043 full args contain the tsubst'd args for the context,
12044 plus the innermost args from the template decl. */
12045 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12046 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12047 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12048 /* Because this is a template, the arguments will still be
12049 dependent, even after substitution. If
12050 PROCESSING_TEMPLATE_DECL is not set, the dependency
12051 predicates will short-circuit. */
12052 ++processing_template_decl;
12053 full_args = tsubst_template_args (tmpl_args, args,
12054 complain, in_decl);
12055 --processing_template_decl;
12056 if (full_args == error_mark_node)
12057 RETURN (error_mark_node);
12058
12059 /* If this is a default template template argument,
12060 tsubst might not have changed anything. */
12061 if (full_args == tmpl_args)
12062 RETURN (t);
12063
12064 hash = hash_tmpl_and_args (t, full_args);
12065 spec = retrieve_specialization (t, full_args, hash);
12066 if (spec != NULL_TREE)
12067 {
12068 r = spec;
12069 break;
12070 }
12071
12072 /* Make a new template decl. It will be similar to the
12073 original, but will record the current template arguments.
12074 We also create a new function declaration, which is just
12075 like the old one, but points to this new template, rather
12076 than the old one. */
12077 r = copy_decl (t);
12078 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12079 DECL_CHAIN (r) = NULL_TREE;
12080
12081 // Build new template info linking to the original template decl.
12082 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12083
12084 if (TREE_CODE (decl) == TYPE_DECL
12085 && !TYPE_DECL_ALIAS_P (decl))
12086 {
12087 tree new_type;
12088 ++processing_template_decl;
12089 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12090 --processing_template_decl;
12091 if (new_type == error_mark_node)
12092 RETURN (error_mark_node);
12093
12094 TREE_TYPE (r) = new_type;
12095 /* For a partial specialization, we need to keep pointing to
12096 the primary template. */
12097 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12098 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12099 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12100 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12101 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12102 }
12103 else
12104 {
12105 tree new_decl;
12106 ++processing_template_decl;
12107 new_decl = tsubst (decl, args, complain, in_decl);
12108 --processing_template_decl;
12109 if (new_decl == error_mark_node)
12110 RETURN (error_mark_node);
12111
12112 DECL_TEMPLATE_RESULT (r) = new_decl;
12113 DECL_TI_TEMPLATE (new_decl) = r;
12114 TREE_TYPE (r) = TREE_TYPE (new_decl);
12115 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12116 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12117 }
12118
12119 SET_DECL_IMPLICIT_INSTANTIATION (r);
12120 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12121 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12122
12123 /* The template parameters for this new template are all the
12124 template parameters for the old template, except the
12125 outermost level of parameters. */
12126 DECL_TEMPLATE_PARMS (r)
12127 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12128 complain);
12129
12130 if (PRIMARY_TEMPLATE_P (t))
12131 DECL_PRIMARY_TEMPLATE (r) = r;
12132
12133 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12134 /* Record this non-type partial instantiation. */
12135 register_specialization (r, t,
12136 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12137 false, hash);
12138 }
12139 break;
12140
12141 case FUNCTION_DECL:
12142 {
12143 tree ctx;
12144 tree argvec = NULL_TREE;
12145 tree *friends;
12146 tree gen_tmpl;
12147 tree type;
12148 int member;
12149 int args_depth;
12150 int parms_depth;
12151
12152 /* Nobody should be tsubst'ing into non-template functions. */
12153 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12154
12155 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12156 {
12157 tree spec;
12158
12159 /* If T is not dependent, just return it. */
12160 if (!uses_template_parms (DECL_TI_ARGS (t)))
12161 RETURN (t);
12162
12163 /* Calculate the most general template of which R is a
12164 specialization, and the complete set of arguments used to
12165 specialize R. */
12166 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12167 argvec = tsubst_template_args (DECL_TI_ARGS
12168 (DECL_TEMPLATE_RESULT
12169 (DECL_TI_TEMPLATE (t))),
12170 args, complain, in_decl);
12171 if (argvec == error_mark_node)
12172 RETURN (error_mark_node);
12173
12174 /* Check to see if we already have this specialization. */
12175 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12176 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12177
12178 if (spec)
12179 {
12180 r = spec;
12181 break;
12182 }
12183
12184 /* We can see more levels of arguments than parameters if
12185 there was a specialization of a member template, like
12186 this:
12187
12188 template <class T> struct S { template <class U> void f(); }
12189 template <> template <class U> void S<int>::f(U);
12190
12191 Here, we'll be substituting into the specialization,
12192 because that's where we can find the code we actually
12193 want to generate, but we'll have enough arguments for
12194 the most general template.
12195
12196 We also deal with the peculiar case:
12197
12198 template <class T> struct S {
12199 template <class U> friend void f();
12200 };
12201 template <class U> void f() {}
12202 template S<int>;
12203 template void f<double>();
12204
12205 Here, the ARGS for the instantiation of will be {int,
12206 double}. But, we only need as many ARGS as there are
12207 levels of template parameters in CODE_PATTERN. We are
12208 careful not to get fooled into reducing the ARGS in
12209 situations like:
12210
12211 template <class T> struct S { template <class U> void f(U); }
12212 template <class T> template <> void S<T>::f(int) {}
12213
12214 which we can spot because the pattern will be a
12215 specialization in this case. */
12216 args_depth = TMPL_ARGS_DEPTH (args);
12217 parms_depth =
12218 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12219 if (args_depth > parms_depth
12220 && !DECL_TEMPLATE_SPECIALIZATION (t))
12221 args = get_innermost_template_args (args, parms_depth);
12222 }
12223 else
12224 {
12225 /* This special case arises when we have something like this:
12226
12227 template <class T> struct S {
12228 friend void f<int>(int, double);
12229 };
12230
12231 Here, the DECL_TI_TEMPLATE for the friend declaration
12232 will be an IDENTIFIER_NODE. We are being called from
12233 tsubst_friend_function, and we want only to create a
12234 new decl (R) with appropriate types so that we can call
12235 determine_specialization. */
12236 gen_tmpl = NULL_TREE;
12237 }
12238
12239 if (DECL_CLASS_SCOPE_P (t))
12240 {
12241 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12242 member = 2;
12243 else
12244 member = 1;
12245 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12246 complain, t, /*entering_scope=*/1);
12247 }
12248 else
12249 {
12250 member = 0;
12251 ctx = DECL_CONTEXT (t);
12252 }
12253 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12254 if (type == error_mark_node)
12255 RETURN (error_mark_node);
12256
12257 /* If we hit excessive deduction depth, the type is bogus even if
12258 it isn't error_mark_node, so don't build a decl. */
12259 if (excessive_deduction_depth)
12260 RETURN (error_mark_node);
12261
12262 /* We do NOT check for matching decls pushed separately at this
12263 point, as they may not represent instantiations of this
12264 template, and in any case are considered separate under the
12265 discrete model. */
12266 r = copy_decl (t);
12267 DECL_USE_TEMPLATE (r) = 0;
12268 TREE_TYPE (r) = type;
12269 /* Clear out the mangled name and RTL for the instantiation. */
12270 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12271 SET_DECL_RTL (r, NULL);
12272 /* Leave DECL_INITIAL set on deleted instantiations. */
12273 if (!DECL_DELETED_FN (r))
12274 DECL_INITIAL (r) = NULL_TREE;
12275 DECL_CONTEXT (r) = ctx;
12276
12277 /* OpenMP UDRs have the only argument a reference to the declared
12278 type. We want to diagnose if the declared type is a reference,
12279 which is invalid, but as references to references are usually
12280 quietly merged, diagnose it here. */
12281 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12282 {
12283 tree argtype
12284 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12285 argtype = tsubst (argtype, args, complain, in_decl);
12286 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12287 error_at (DECL_SOURCE_LOCATION (t),
12288 "reference type %qT in "
12289 "%<#pragma omp declare reduction%>", argtype);
12290 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12291 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12292 argtype);
12293 }
12294
12295 if (member && DECL_CONV_FN_P (r))
12296 /* Type-conversion operator. Reconstruct the name, in
12297 case it's the name of one of the template's parameters. */
12298 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12299
12300 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12301 complain, t);
12302 DECL_RESULT (r) = NULL_TREE;
12303
12304 TREE_STATIC (r) = 0;
12305 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12306 DECL_EXTERNAL (r) = 1;
12307 /* If this is an instantiation of a function with internal
12308 linkage, we already know what object file linkage will be
12309 assigned to the instantiation. */
12310 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12311 DECL_DEFER_OUTPUT (r) = 0;
12312 DECL_CHAIN (r) = NULL_TREE;
12313 DECL_PENDING_INLINE_INFO (r) = 0;
12314 DECL_PENDING_INLINE_P (r) = 0;
12315 DECL_SAVED_TREE (r) = NULL_TREE;
12316 DECL_STRUCT_FUNCTION (r) = NULL;
12317 TREE_USED (r) = 0;
12318 /* We'll re-clone as appropriate in instantiate_template. */
12319 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12320
12321 /* If we aren't complaining now, return on error before we register
12322 the specialization so that we'll complain eventually. */
12323 if ((complain & tf_error) == 0
12324 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12325 && !grok_op_properties (r, /*complain=*/false))
12326 RETURN (error_mark_node);
12327
12328 /* When instantiating a constrained member, substitute
12329 into the constraints to create a new constraint. */
12330 if (tree ci = get_constraints (t))
12331 if (member)
12332 {
12333 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12334 set_constraints (r, ci);
12335 }
12336
12337 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12338 this in the special friend case mentioned above where
12339 GEN_TMPL is NULL. */
12340 if (gen_tmpl)
12341 {
12342 DECL_TEMPLATE_INFO (r)
12343 = build_template_info (gen_tmpl, argvec);
12344 SET_DECL_IMPLICIT_INSTANTIATION (r);
12345
12346 tree new_r
12347 = register_specialization (r, gen_tmpl, argvec, false, hash);
12348 if (new_r != r)
12349 /* We instantiated this while substituting into
12350 the type earlier (template/friend54.C). */
12351 RETURN (new_r);
12352
12353 /* We're not supposed to instantiate default arguments
12354 until they are called, for a template. But, for a
12355 declaration like:
12356
12357 template <class T> void f ()
12358 { extern void g(int i = T()); }
12359
12360 we should do the substitution when the template is
12361 instantiated. We handle the member function case in
12362 instantiate_class_template since the default arguments
12363 might refer to other members of the class. */
12364 if (!member
12365 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12366 && !uses_template_parms (argvec))
12367 tsubst_default_arguments (r, complain);
12368 }
12369 else
12370 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12371
12372 /* Copy the list of befriending classes. */
12373 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12374 *friends;
12375 friends = &TREE_CHAIN (*friends))
12376 {
12377 *friends = copy_node (*friends);
12378 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12379 args, complain,
12380 in_decl);
12381 }
12382
12383 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12384 {
12385 maybe_retrofit_in_chrg (r);
12386 if (DECL_CONSTRUCTOR_P (r))
12387 grok_ctor_properties (ctx, r);
12388 /* If this is an instantiation of a member template, clone it.
12389 If it isn't, that'll be handled by
12390 clone_constructors_and_destructors. */
12391 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12392 clone_function_decl (r, /*update_method_vec_p=*/0);
12393 }
12394 else if ((complain & tf_error) != 0
12395 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12396 && !grok_op_properties (r, /*complain=*/true))
12397 RETURN (error_mark_node);
12398
12399 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12400 SET_DECL_FRIEND_CONTEXT (r,
12401 tsubst (DECL_FRIEND_CONTEXT (t),
12402 args, complain, in_decl));
12403
12404 /* Possibly limit visibility based on template args. */
12405 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12406 if (DECL_VISIBILITY_SPECIFIED (t))
12407 {
12408 DECL_VISIBILITY_SPECIFIED (r) = 0;
12409 DECL_ATTRIBUTES (r)
12410 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12411 }
12412 determine_visibility (r);
12413 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12414 && !processing_template_decl)
12415 defaulted_late_check (r);
12416
12417 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12418 args, complain, in_decl);
12419 }
12420 break;
12421
12422 case PARM_DECL:
12423 {
12424 tree type = NULL_TREE;
12425 int i, len = 1;
12426 tree expanded_types = NULL_TREE;
12427 tree prev_r = NULL_TREE;
12428 tree first_r = NULL_TREE;
12429
12430 if (DECL_PACK_P (t))
12431 {
12432 /* If there is a local specialization that isn't a
12433 parameter pack, it means that we're doing a "simple"
12434 substitution from inside tsubst_pack_expansion. Just
12435 return the local specialization (which will be a single
12436 parm). */
12437 tree spec = retrieve_local_specialization (t);
12438 if (spec
12439 && TREE_CODE (spec) == PARM_DECL
12440 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12441 RETURN (spec);
12442
12443 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12444 the parameters in this function parameter pack. */
12445 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12446 complain, in_decl);
12447 if (TREE_CODE (expanded_types) == TREE_VEC)
12448 {
12449 len = TREE_VEC_LENGTH (expanded_types);
12450
12451 /* Zero-length parameter packs are boring. Just substitute
12452 into the chain. */
12453 if (len == 0)
12454 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12455 TREE_CHAIN (t)));
12456 }
12457 else
12458 {
12459 /* All we did was update the type. Make a note of that. */
12460 type = expanded_types;
12461 expanded_types = NULL_TREE;
12462 }
12463 }
12464
12465 /* Loop through all of the parameters we'll build. When T is
12466 a function parameter pack, LEN is the number of expanded
12467 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12468 r = NULL_TREE;
12469 for (i = 0; i < len; ++i)
12470 {
12471 prev_r = r;
12472 r = copy_node (t);
12473 if (DECL_TEMPLATE_PARM_P (t))
12474 SET_DECL_TEMPLATE_PARM_P (r);
12475
12476 if (expanded_types)
12477 /* We're on the Ith parameter of the function parameter
12478 pack. */
12479 {
12480 /* Get the Ith type. */
12481 type = TREE_VEC_ELT (expanded_types, i);
12482
12483 /* Rename the parameter to include the index. */
12484 DECL_NAME (r)
12485 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12486 }
12487 else if (!type)
12488 /* We're dealing with a normal parameter. */
12489 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12490
12491 type = type_decays_to (type);
12492 TREE_TYPE (r) = type;
12493 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12494
12495 if (DECL_INITIAL (r))
12496 {
12497 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12498 DECL_INITIAL (r) = TREE_TYPE (r);
12499 else
12500 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12501 complain, in_decl);
12502 }
12503
12504 DECL_CONTEXT (r) = NULL_TREE;
12505
12506 if (!DECL_TEMPLATE_PARM_P (r))
12507 DECL_ARG_TYPE (r) = type_passed_as (type);
12508
12509 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12510 args, complain, in_decl);
12511
12512 /* Keep track of the first new parameter we
12513 generate. That's what will be returned to the
12514 caller. */
12515 if (!first_r)
12516 first_r = r;
12517
12518 /* Build a proper chain of parameters when substituting
12519 into a function parameter pack. */
12520 if (prev_r)
12521 DECL_CHAIN (prev_r) = r;
12522 }
12523
12524 /* If cp_unevaluated_operand is set, we're just looking for a
12525 single dummy parameter, so don't keep going. */
12526 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12527 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12528 complain, DECL_CHAIN (t));
12529
12530 /* FIRST_R contains the start of the chain we've built. */
12531 r = first_r;
12532 }
12533 break;
12534
12535 case FIELD_DECL:
12536 {
12537 tree type = NULL_TREE;
12538 tree vec = NULL_TREE;
12539 tree expanded_types = NULL_TREE;
12540 int len = 1;
12541
12542 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12543 {
12544 /* This field is a lambda capture pack. Return a TREE_VEC of
12545 the expanded fields to instantiate_class_template_1 and
12546 store them in the specializations hash table as a
12547 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12548 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12549 complain, in_decl);
12550 if (TREE_CODE (expanded_types) == TREE_VEC)
12551 {
12552 len = TREE_VEC_LENGTH (expanded_types);
12553 vec = make_tree_vec (len);
12554 }
12555 else
12556 {
12557 /* All we did was update the type. Make a note of that. */
12558 type = expanded_types;
12559 expanded_types = NULL_TREE;
12560 }
12561 }
12562
12563 for (int i = 0; i < len; ++i)
12564 {
12565 r = copy_decl (t);
12566 if (expanded_types)
12567 {
12568 type = TREE_VEC_ELT (expanded_types, i);
12569 DECL_NAME (r)
12570 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12571 }
12572 else if (!type)
12573 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12574
12575 if (type == error_mark_node)
12576 RETURN (error_mark_node);
12577 TREE_TYPE (r) = type;
12578 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12579
12580 if (DECL_C_BIT_FIELD (r))
12581 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12582 non-bit-fields DECL_INITIAL is a non-static data member
12583 initializer, which gets deferred instantiation. */
12584 DECL_INITIAL (r)
12585 = tsubst_expr (DECL_INITIAL (t), args,
12586 complain, in_decl,
12587 /*integral_constant_expression_p=*/true);
12588 else if (DECL_INITIAL (t))
12589 {
12590 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12591 NSDMI in perform_member_init. Still set DECL_INITIAL
12592 so that we know there is one. */
12593 DECL_INITIAL (r) = void_node;
12594 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12595 retrofit_lang_decl (r);
12596 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12597 }
12598 /* We don't have to set DECL_CONTEXT here; it is set by
12599 finish_member_declaration. */
12600 DECL_CHAIN (r) = NULL_TREE;
12601
12602 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12603 args, complain, in_decl);
12604
12605 if (vec)
12606 TREE_VEC_ELT (vec, i) = r;
12607 }
12608
12609 if (vec)
12610 {
12611 r = vec;
12612 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12613 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12614 SET_ARGUMENT_PACK_ARGS (pack, vec);
12615 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12616 TREE_TYPE (pack) = tpack;
12617 register_specialization (pack, t, args, false, 0);
12618 }
12619 }
12620 break;
12621
12622 case USING_DECL:
12623 /* We reach here only for member using decls. We also need to check
12624 uses_template_parms because DECL_DEPENDENT_P is not set for a
12625 using-declaration that designates a member of the current
12626 instantiation (c++/53549). */
12627 if (DECL_DEPENDENT_P (t)
12628 || uses_template_parms (USING_DECL_SCOPE (t)))
12629 {
12630 tree scope = USING_DECL_SCOPE (t);
12631 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12632 if (PACK_EXPANSION_P (scope))
12633 {
12634 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12635 int len = TREE_VEC_LENGTH (vec);
12636 r = make_tree_vec (len);
12637 for (int i = 0; i < len; ++i)
12638 {
12639 tree escope = TREE_VEC_ELT (vec, i);
12640 tree elt = do_class_using_decl (escope, name);
12641 if (!elt)
12642 {
12643 r = error_mark_node;
12644 break;
12645 }
12646 else
12647 {
12648 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12649 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12650 }
12651 TREE_VEC_ELT (r, i) = elt;
12652 }
12653 }
12654 else
12655 {
12656 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12657 complain, in_decl);
12658 r = do_class_using_decl (inst_scope, name);
12659 if (!r)
12660 r = error_mark_node;
12661 else
12662 {
12663 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12664 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12665 }
12666 }
12667 }
12668 else
12669 {
12670 r = copy_node (t);
12671 DECL_CHAIN (r) = NULL_TREE;
12672 }
12673 break;
12674
12675 case TYPE_DECL:
12676 case VAR_DECL:
12677 {
12678 tree argvec = NULL_TREE;
12679 tree gen_tmpl = NULL_TREE;
12680 tree spec;
12681 tree tmpl = NULL_TREE;
12682 tree ctx;
12683 tree type = NULL_TREE;
12684 bool local_p;
12685
12686 if (TREE_TYPE (t) == error_mark_node)
12687 RETURN (error_mark_node);
12688
12689 if (TREE_CODE (t) == TYPE_DECL
12690 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12691 {
12692 /* If this is the canonical decl, we don't have to
12693 mess with instantiations, and often we can't (for
12694 typename, template type parms and such). Note that
12695 TYPE_NAME is not correct for the above test if
12696 we've copied the type for a typedef. */
12697 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12698 if (type == error_mark_node)
12699 RETURN (error_mark_node);
12700 r = TYPE_NAME (type);
12701 break;
12702 }
12703
12704 /* Check to see if we already have the specialization we
12705 need. */
12706 spec = NULL_TREE;
12707 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12708 {
12709 /* T is a static data member or namespace-scope entity.
12710 We have to substitute into namespace-scope variables
12711 (not just variable templates) because of cases like:
12712
12713 template <class T> void f() { extern T t; }
12714
12715 where the entity referenced is not known until
12716 instantiation time. */
12717 local_p = false;
12718 ctx = DECL_CONTEXT (t);
12719 if (DECL_CLASS_SCOPE_P (t))
12720 {
12721 ctx = tsubst_aggr_type (ctx, args,
12722 complain,
12723 in_decl, /*entering_scope=*/1);
12724 /* If CTX is unchanged, then T is in fact the
12725 specialization we want. That situation occurs when
12726 referencing a static data member within in its own
12727 class. We can use pointer equality, rather than
12728 same_type_p, because DECL_CONTEXT is always
12729 canonical... */
12730 if (ctx == DECL_CONTEXT (t)
12731 /* ... unless T is a member template; in which
12732 case our caller can be willing to create a
12733 specialization of that template represented
12734 by T. */
12735 && !(DECL_TI_TEMPLATE (t)
12736 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12737 spec = t;
12738 }
12739
12740 if (!spec)
12741 {
12742 tmpl = DECL_TI_TEMPLATE (t);
12743 gen_tmpl = most_general_template (tmpl);
12744 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12745 if (argvec != error_mark_node)
12746 argvec = (coerce_innermost_template_parms
12747 (DECL_TEMPLATE_PARMS (gen_tmpl),
12748 argvec, t, complain,
12749 /*all*/true, /*defarg*/true));
12750 if (argvec == error_mark_node)
12751 RETURN (error_mark_node);
12752 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12753 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12754 }
12755 }
12756 else
12757 {
12758 /* A local variable. */
12759 local_p = true;
12760 /* Subsequent calls to pushdecl will fill this in. */
12761 ctx = NULL_TREE;
12762 /* Unless this is a reference to a static variable from an
12763 enclosing function, in which case we need to fill it in now. */
12764 if (TREE_STATIC (t))
12765 {
12766 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12767 if (fn != current_function_decl)
12768 ctx = fn;
12769 }
12770 spec = retrieve_local_specialization (t);
12771 }
12772 /* If we already have the specialization we need, there is
12773 nothing more to do. */
12774 if (spec)
12775 {
12776 r = spec;
12777 break;
12778 }
12779
12780 /* Create a new node for the specialization we need. */
12781 r = copy_decl (t);
12782 if (type == NULL_TREE)
12783 {
12784 if (is_typedef_decl (t))
12785 type = DECL_ORIGINAL_TYPE (t);
12786 else
12787 type = TREE_TYPE (t);
12788 if (VAR_P (t)
12789 && VAR_HAD_UNKNOWN_BOUND (t)
12790 && type != error_mark_node)
12791 type = strip_array_domain (type);
12792 type = tsubst (type, args, complain, in_decl);
12793 }
12794 if (VAR_P (r))
12795 {
12796 /* Even if the original location is out of scope, the
12797 newly substituted one is not. */
12798 DECL_DEAD_FOR_LOCAL (r) = 0;
12799 DECL_INITIALIZED_P (r) = 0;
12800 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12801 if (type == error_mark_node)
12802 RETURN (error_mark_node);
12803 if (TREE_CODE (type) == FUNCTION_TYPE)
12804 {
12805 /* It may seem that this case cannot occur, since:
12806
12807 typedef void f();
12808 void g() { f x; }
12809
12810 declares a function, not a variable. However:
12811
12812 typedef void f();
12813 template <typename T> void g() { T t; }
12814 template void g<f>();
12815
12816 is an attempt to declare a variable with function
12817 type. */
12818 error ("variable %qD has function type",
12819 /* R is not yet sufficiently initialized, so we
12820 just use its name. */
12821 DECL_NAME (r));
12822 RETURN (error_mark_node);
12823 }
12824 type = complete_type (type);
12825 /* Wait until cp_finish_decl to set this again, to handle
12826 circular dependency (template/instantiate6.C). */
12827 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12828 type = check_var_type (DECL_NAME (r), type);
12829
12830 if (DECL_HAS_VALUE_EXPR_P (t))
12831 {
12832 tree ve = DECL_VALUE_EXPR (t);
12833 ve = tsubst_expr (ve, args, complain, in_decl,
12834 /*constant_expression_p=*/false);
12835 if (REFERENCE_REF_P (ve))
12836 {
12837 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12838 ve = TREE_OPERAND (ve, 0);
12839 }
12840 SET_DECL_VALUE_EXPR (r, ve);
12841 }
12842 if (CP_DECL_THREAD_LOCAL_P (r)
12843 && !processing_template_decl)
12844 set_decl_tls_model (r, decl_default_tls_model (r));
12845 }
12846 else if (DECL_SELF_REFERENCE_P (t))
12847 SET_DECL_SELF_REFERENCE_P (r);
12848 TREE_TYPE (r) = type;
12849 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12850 DECL_CONTEXT (r) = ctx;
12851 /* Clear out the mangled name and RTL for the instantiation. */
12852 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12853 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12854 SET_DECL_RTL (r, NULL);
12855 /* The initializer must not be expanded until it is required;
12856 see [temp.inst]. */
12857 DECL_INITIAL (r) = NULL_TREE;
12858 if (VAR_P (r))
12859 SET_DECL_MODE (r, VOIDmode);
12860 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12861 SET_DECL_RTL (r, NULL);
12862 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12863 if (VAR_P (r))
12864 {
12865 /* Possibly limit visibility based on template args. */
12866 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12867 if (DECL_VISIBILITY_SPECIFIED (t))
12868 {
12869 DECL_VISIBILITY_SPECIFIED (r) = 0;
12870 DECL_ATTRIBUTES (r)
12871 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12872 }
12873 determine_visibility (r);
12874 }
12875
12876 if (!local_p)
12877 {
12878 /* A static data member declaration is always marked
12879 external when it is declared in-class, even if an
12880 initializer is present. We mimic the non-template
12881 processing here. */
12882 DECL_EXTERNAL (r) = 1;
12883 if (DECL_NAMESPACE_SCOPE_P (t))
12884 DECL_NOT_REALLY_EXTERN (r) = 1;
12885
12886 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12887 SET_DECL_IMPLICIT_INSTANTIATION (r);
12888 register_specialization (r, gen_tmpl, argvec, false, hash);
12889 }
12890 else
12891 {
12892 if (DECL_LANG_SPECIFIC (r))
12893 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12894 if (!cp_unevaluated_operand)
12895 register_local_specialization (r, t);
12896 }
12897
12898 DECL_CHAIN (r) = NULL_TREE;
12899
12900 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12901 /*flags=*/0,
12902 args, complain, in_decl);
12903
12904 /* Preserve a typedef that names a type. */
12905 if (is_typedef_decl (r) && type != error_mark_node)
12906 {
12907 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12908 set_underlying_type (r);
12909 if (TYPE_DECL_ALIAS_P (r))
12910 /* An alias template specialization can be dependent
12911 even if its underlying type is not. */
12912 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12913 }
12914
12915 layout_decl (r, 0);
12916 }
12917 break;
12918
12919 default:
12920 gcc_unreachable ();
12921 }
12922 #undef RETURN
12923
12924 out:
12925 /* Restore the file and line information. */
12926 input_location = saved_loc;
12927
12928 return r;
12929 }
12930
12931 /* Substitute into the ARG_TYPES of a function type.
12932 If END is a TREE_CHAIN, leave it and any following types
12933 un-substituted. */
12934
12935 static tree
12936 tsubst_arg_types (tree arg_types,
12937 tree args,
12938 tree end,
12939 tsubst_flags_t complain,
12940 tree in_decl)
12941 {
12942 tree remaining_arg_types;
12943 tree type = NULL_TREE;
12944 int i = 1;
12945 tree expanded_args = NULL_TREE;
12946 tree default_arg;
12947
12948 if (!arg_types || arg_types == void_list_node || arg_types == end)
12949 return arg_types;
12950
12951 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12952 args, end, complain, in_decl);
12953 if (remaining_arg_types == error_mark_node)
12954 return error_mark_node;
12955
12956 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12957 {
12958 /* For a pack expansion, perform substitution on the
12959 entire expression. Later on, we'll handle the arguments
12960 one-by-one. */
12961 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12962 args, complain, in_decl);
12963
12964 if (TREE_CODE (expanded_args) == TREE_VEC)
12965 /* So that we'll spin through the parameters, one by one. */
12966 i = TREE_VEC_LENGTH (expanded_args);
12967 else
12968 {
12969 /* We only partially substituted into the parameter
12970 pack. Our type is TYPE_PACK_EXPANSION. */
12971 type = expanded_args;
12972 expanded_args = NULL_TREE;
12973 }
12974 }
12975
12976 while (i > 0) {
12977 --i;
12978
12979 if (expanded_args)
12980 type = TREE_VEC_ELT (expanded_args, i);
12981 else if (!type)
12982 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12983
12984 if (type == error_mark_node)
12985 return error_mark_node;
12986 if (VOID_TYPE_P (type))
12987 {
12988 if (complain & tf_error)
12989 {
12990 error ("invalid parameter type %qT", type);
12991 if (in_decl)
12992 error ("in declaration %q+D", in_decl);
12993 }
12994 return error_mark_node;
12995 }
12996 /* DR 657. */
12997 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12998 return error_mark_node;
12999
13000 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13001 top-level qualifiers as required. */
13002 type = cv_unqualified (type_decays_to (type));
13003
13004 /* We do not substitute into default arguments here. The standard
13005 mandates that they be instantiated only when needed, which is
13006 done in build_over_call. */
13007 default_arg = TREE_PURPOSE (arg_types);
13008
13009 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13010 {
13011 /* We've instantiated a template before its default arguments
13012 have been parsed. This can happen for a nested template
13013 class, and is not an error unless we require the default
13014 argument in a call of this function. */
13015 remaining_arg_types =
13016 tree_cons (default_arg, type, remaining_arg_types);
13017 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13018 }
13019 else
13020 remaining_arg_types =
13021 hash_tree_cons (default_arg, type, remaining_arg_types);
13022 }
13023
13024 return remaining_arg_types;
13025 }
13026
13027 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13028 *not* handle the exception-specification for FNTYPE, because the
13029 initial substitution of explicitly provided template parameters
13030 during argument deduction forbids substitution into the
13031 exception-specification:
13032
13033 [temp.deduct]
13034
13035 All references in the function type of the function template to the
13036 corresponding template parameters are replaced by the specified tem-
13037 plate argument values. If a substitution in a template parameter or
13038 in the function type of the function template results in an invalid
13039 type, type deduction fails. [Note: The equivalent substitution in
13040 exception specifications is done only when the function is instanti-
13041 ated, at which point a program is ill-formed if the substitution
13042 results in an invalid type.] */
13043
13044 static tree
13045 tsubst_function_type (tree t,
13046 tree args,
13047 tsubst_flags_t complain,
13048 tree in_decl)
13049 {
13050 tree return_type;
13051 tree arg_types = NULL_TREE;
13052 tree fntype;
13053
13054 /* The TYPE_CONTEXT is not used for function/method types. */
13055 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13056
13057 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13058 failure. */
13059 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13060
13061 if (late_return_type_p)
13062 {
13063 /* Substitute the argument types. */
13064 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13065 complain, in_decl);
13066 if (arg_types == error_mark_node)
13067 return error_mark_node;
13068
13069 tree save_ccp = current_class_ptr;
13070 tree save_ccr = current_class_ref;
13071 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13072 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13073 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13074 if (do_inject)
13075 {
13076 /* DR 1207: 'this' is in scope in the trailing return type. */
13077 inject_this_parameter (this_type, cp_type_quals (this_type));
13078 }
13079
13080 /* Substitute the return type. */
13081 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13082
13083 if (do_inject)
13084 {
13085 current_class_ptr = save_ccp;
13086 current_class_ref = save_ccr;
13087 }
13088 }
13089 else
13090 /* Substitute the return type. */
13091 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13092
13093 if (return_type == error_mark_node)
13094 return error_mark_node;
13095 /* DR 486 clarifies that creation of a function type with an
13096 invalid return type is a deduction failure. */
13097 if (TREE_CODE (return_type) == ARRAY_TYPE
13098 || TREE_CODE (return_type) == FUNCTION_TYPE)
13099 {
13100 if (complain & tf_error)
13101 {
13102 if (TREE_CODE (return_type) == ARRAY_TYPE)
13103 error ("function returning an array");
13104 else
13105 error ("function returning a function");
13106 }
13107 return error_mark_node;
13108 }
13109 /* And DR 657. */
13110 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13111 return error_mark_node;
13112
13113 if (!late_return_type_p)
13114 {
13115 /* Substitute the argument types. */
13116 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13117 complain, in_decl);
13118 if (arg_types == error_mark_node)
13119 return error_mark_node;
13120 }
13121
13122 /* Construct a new type node and return it. */
13123 if (TREE_CODE (t) == FUNCTION_TYPE)
13124 {
13125 fntype = build_function_type (return_type, arg_types);
13126 fntype = apply_memfn_quals (fntype,
13127 type_memfn_quals (t),
13128 type_memfn_rqual (t));
13129 }
13130 else
13131 {
13132 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13133 /* Don't pick up extra function qualifiers from the basetype. */
13134 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13135 if (! MAYBE_CLASS_TYPE_P (r))
13136 {
13137 /* [temp.deduct]
13138
13139 Type deduction may fail for any of the following
13140 reasons:
13141
13142 -- Attempting to create "pointer to member of T" when T
13143 is not a class type. */
13144 if (complain & tf_error)
13145 error ("creating pointer to member function of non-class type %qT",
13146 r);
13147 return error_mark_node;
13148 }
13149
13150 fntype = build_method_type_directly (r, return_type,
13151 TREE_CHAIN (arg_types));
13152 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13153 }
13154 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13155
13156 if (late_return_type_p)
13157 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13158
13159 return fntype;
13160 }
13161
13162 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13163 ARGS into that specification, and return the substituted
13164 specification. If there is no specification, return NULL_TREE. */
13165
13166 static tree
13167 tsubst_exception_specification (tree fntype,
13168 tree args,
13169 tsubst_flags_t complain,
13170 tree in_decl,
13171 bool defer_ok)
13172 {
13173 tree specs;
13174 tree new_specs;
13175
13176 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13177 new_specs = NULL_TREE;
13178 if (specs && TREE_PURPOSE (specs))
13179 {
13180 /* A noexcept-specifier. */
13181 tree expr = TREE_PURPOSE (specs);
13182 if (TREE_CODE (expr) == INTEGER_CST)
13183 new_specs = expr;
13184 else if (defer_ok)
13185 {
13186 /* Defer instantiation of noexcept-specifiers to avoid
13187 excessive instantiations (c++/49107). */
13188 new_specs = make_node (DEFERRED_NOEXCEPT);
13189 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13190 {
13191 /* We already partially instantiated this member template,
13192 so combine the new args with the old. */
13193 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13194 = DEFERRED_NOEXCEPT_PATTERN (expr);
13195 DEFERRED_NOEXCEPT_ARGS (new_specs)
13196 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13197 }
13198 else
13199 {
13200 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13201 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13202 }
13203 }
13204 else
13205 new_specs = tsubst_copy_and_build
13206 (expr, args, complain, in_decl, /*function_p=*/false,
13207 /*integral_constant_expression_p=*/true);
13208 new_specs = build_noexcept_spec (new_specs, complain);
13209 }
13210 else if (specs)
13211 {
13212 if (! TREE_VALUE (specs))
13213 new_specs = specs;
13214 else
13215 while (specs)
13216 {
13217 tree spec;
13218 int i, len = 1;
13219 tree expanded_specs = NULL_TREE;
13220
13221 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13222 {
13223 /* Expand the pack expansion type. */
13224 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13225 args, complain,
13226 in_decl);
13227
13228 if (expanded_specs == error_mark_node)
13229 return error_mark_node;
13230 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13231 len = TREE_VEC_LENGTH (expanded_specs);
13232 else
13233 {
13234 /* We're substituting into a member template, so
13235 we got a TYPE_PACK_EXPANSION back. Add that
13236 expansion and move on. */
13237 gcc_assert (TREE_CODE (expanded_specs)
13238 == TYPE_PACK_EXPANSION);
13239 new_specs = add_exception_specifier (new_specs,
13240 expanded_specs,
13241 complain);
13242 specs = TREE_CHAIN (specs);
13243 continue;
13244 }
13245 }
13246
13247 for (i = 0; i < len; ++i)
13248 {
13249 if (expanded_specs)
13250 spec = TREE_VEC_ELT (expanded_specs, i);
13251 else
13252 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13253 if (spec == error_mark_node)
13254 return spec;
13255 new_specs = add_exception_specifier (new_specs, spec,
13256 complain);
13257 }
13258
13259 specs = TREE_CHAIN (specs);
13260 }
13261 }
13262 return new_specs;
13263 }
13264
13265 /* Take the tree structure T and replace template parameters used
13266 therein with the argument vector ARGS. IN_DECL is an associated
13267 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13268 Issue error and warning messages under control of COMPLAIN. Note
13269 that we must be relatively non-tolerant of extensions here, in
13270 order to preserve conformance; if we allow substitutions that
13271 should not be allowed, we may allow argument deductions that should
13272 not succeed, and therefore report ambiguous overload situations
13273 where there are none. In theory, we could allow the substitution,
13274 but indicate that it should have failed, and allow our caller to
13275 make sure that the right thing happens, but we don't try to do this
13276 yet.
13277
13278 This function is used for dealing with types, decls and the like;
13279 for expressions, use tsubst_expr or tsubst_copy. */
13280
13281 tree
13282 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13283 {
13284 enum tree_code code;
13285 tree type, r = NULL_TREE;
13286
13287 if (t == NULL_TREE || t == error_mark_node
13288 || t == integer_type_node
13289 || t == void_type_node
13290 || t == char_type_node
13291 || t == unknown_type_node
13292 || TREE_CODE (t) == NAMESPACE_DECL
13293 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13294 return t;
13295
13296 if (DECL_P (t))
13297 return tsubst_decl (t, args, complain);
13298
13299 if (args == NULL_TREE)
13300 return t;
13301
13302 code = TREE_CODE (t);
13303
13304 if (code == IDENTIFIER_NODE)
13305 type = IDENTIFIER_TYPE_VALUE (t);
13306 else
13307 type = TREE_TYPE (t);
13308
13309 gcc_assert (type != unknown_type_node);
13310
13311 /* Reuse typedefs. We need to do this to handle dependent attributes,
13312 such as attribute aligned. */
13313 if (TYPE_P (t)
13314 && typedef_variant_p (t))
13315 {
13316 tree decl = TYPE_NAME (t);
13317
13318 if (alias_template_specialization_p (t))
13319 {
13320 /* DECL represents an alias template and we want to
13321 instantiate it. */
13322 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13323 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13324 r = instantiate_alias_template (tmpl, gen_args, complain);
13325 }
13326 else if (DECL_CLASS_SCOPE_P (decl)
13327 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13328 && uses_template_parms (DECL_CONTEXT (decl)))
13329 {
13330 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13331 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13332 r = retrieve_specialization (tmpl, gen_args, 0);
13333 }
13334 else if (DECL_FUNCTION_SCOPE_P (decl)
13335 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13336 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13337 r = retrieve_local_specialization (decl);
13338 else
13339 /* The typedef is from a non-template context. */
13340 return t;
13341
13342 if (r)
13343 {
13344 r = TREE_TYPE (r);
13345 r = cp_build_qualified_type_real
13346 (r, cp_type_quals (t) | cp_type_quals (r),
13347 complain | tf_ignore_bad_quals);
13348 return r;
13349 }
13350 else
13351 {
13352 /* We don't have an instantiation yet, so drop the typedef. */
13353 int quals = cp_type_quals (t);
13354 t = DECL_ORIGINAL_TYPE (decl);
13355 t = cp_build_qualified_type_real (t, quals,
13356 complain | tf_ignore_bad_quals);
13357 }
13358 }
13359
13360 bool fndecl_type = (complain & tf_fndecl_type);
13361 complain &= ~tf_fndecl_type;
13362
13363 if (type
13364 && code != TYPENAME_TYPE
13365 && code != TEMPLATE_TYPE_PARM
13366 && code != IDENTIFIER_NODE
13367 && code != FUNCTION_TYPE
13368 && code != METHOD_TYPE)
13369 type = tsubst (type, args, complain, in_decl);
13370 if (type == error_mark_node)
13371 return error_mark_node;
13372
13373 switch (code)
13374 {
13375 case RECORD_TYPE:
13376 case UNION_TYPE:
13377 case ENUMERAL_TYPE:
13378 return tsubst_aggr_type (t, args, complain, in_decl,
13379 /*entering_scope=*/0);
13380
13381 case ERROR_MARK:
13382 case IDENTIFIER_NODE:
13383 case VOID_TYPE:
13384 case REAL_TYPE:
13385 case COMPLEX_TYPE:
13386 case VECTOR_TYPE:
13387 case BOOLEAN_TYPE:
13388 case NULLPTR_TYPE:
13389 case LANG_TYPE:
13390 return t;
13391
13392 case INTEGER_TYPE:
13393 if (t == integer_type_node)
13394 return t;
13395
13396 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13397 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13398 return t;
13399
13400 {
13401 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13402
13403 max = tsubst_expr (omax, args, complain, in_decl,
13404 /*integral_constant_expression_p=*/false);
13405
13406 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13407 needed. */
13408 if (TREE_CODE (max) == NOP_EXPR
13409 && TREE_SIDE_EFFECTS (omax)
13410 && !TREE_TYPE (max))
13411 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13412
13413 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13414 with TREE_SIDE_EFFECTS that indicates this is not an integral
13415 constant expression. */
13416 if (processing_template_decl
13417 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13418 {
13419 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13420 TREE_SIDE_EFFECTS (max) = 1;
13421 }
13422
13423 return compute_array_index_type (NULL_TREE, max, complain);
13424 }
13425
13426 case TEMPLATE_TYPE_PARM:
13427 case TEMPLATE_TEMPLATE_PARM:
13428 case BOUND_TEMPLATE_TEMPLATE_PARM:
13429 case TEMPLATE_PARM_INDEX:
13430 {
13431 int idx;
13432 int level;
13433 int levels;
13434 tree arg = NULL_TREE;
13435
13436 /* Early in template argument deduction substitution, we don't
13437 want to reduce the level of 'auto', or it will be confused
13438 with a normal template parm in subsequent deduction. */
13439 if (is_auto (t) && (complain & tf_partial))
13440 return t;
13441
13442 r = NULL_TREE;
13443
13444 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13445 template_parm_level_and_index (t, &level, &idx);
13446
13447 levels = TMPL_ARGS_DEPTH (args);
13448 if (level <= levels
13449 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13450 {
13451 arg = TMPL_ARG (args, level, idx);
13452
13453 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13454 {
13455 /* See through ARGUMENT_PACK_SELECT arguments. */
13456 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13457 /* If the selected argument is an expansion E, that most
13458 likely means we were called from
13459 gen_elem_of_pack_expansion_instantiation during the
13460 substituting of pack an argument pack (which Ith
13461 element is a pack expansion, where I is
13462 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13463 In this case, the Ith element resulting from this
13464 substituting is going to be a pack expansion, which
13465 pattern is the pattern of E. Let's return the
13466 pattern of E, and
13467 gen_elem_of_pack_expansion_instantiation will
13468 build the resulting pack expansion from it. */
13469 if (PACK_EXPANSION_P (arg))
13470 {
13471 /* Make sure we aren't throwing away arg info. */
13472 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13473 arg = PACK_EXPANSION_PATTERN (arg);
13474 }
13475 }
13476 }
13477
13478 if (arg == error_mark_node)
13479 return error_mark_node;
13480 else if (arg != NULL_TREE)
13481 {
13482 if (ARGUMENT_PACK_P (arg))
13483 /* If ARG is an argument pack, we don't actually want to
13484 perform a substitution here, because substitutions
13485 for argument packs are only done
13486 element-by-element. We can get to this point when
13487 substituting the type of a non-type template
13488 parameter pack, when that type actually contains
13489 template parameter packs from an outer template, e.g.,
13490
13491 template<typename... Types> struct A {
13492 template<Types... Values> struct B { };
13493 }; */
13494 return t;
13495
13496 if (code == TEMPLATE_TYPE_PARM)
13497 {
13498 int quals;
13499 gcc_assert (TYPE_P (arg));
13500
13501 quals = cp_type_quals (arg) | cp_type_quals (t);
13502
13503 return cp_build_qualified_type_real
13504 (arg, quals, complain | tf_ignore_bad_quals);
13505 }
13506 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13507 {
13508 /* We are processing a type constructed from a
13509 template template parameter. */
13510 tree argvec = tsubst (TYPE_TI_ARGS (t),
13511 args, complain, in_decl);
13512 if (argvec == error_mark_node)
13513 return error_mark_node;
13514
13515 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13516 || TREE_CODE (arg) == TEMPLATE_DECL
13517 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13518
13519 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13520 /* Consider this code:
13521
13522 template <template <class> class Template>
13523 struct Internal {
13524 template <class Arg> using Bind = Template<Arg>;
13525 };
13526
13527 template <template <class> class Template, class Arg>
13528 using Instantiate = Template<Arg>; //#0
13529
13530 template <template <class> class Template,
13531 class Argument>
13532 using Bind =
13533 Instantiate<Internal<Template>::template Bind,
13534 Argument>; //#1
13535
13536 When #1 is parsed, the
13537 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13538 parameter `Template' in #0 matches the
13539 UNBOUND_CLASS_TEMPLATE representing the argument
13540 `Internal<Template>::template Bind'; We then want
13541 to assemble the type `Bind<Argument>' that can't
13542 be fully created right now, because
13543 `Internal<Template>' not being complete, the Bind
13544 template cannot be looked up in that context. So
13545 we need to "store" `Bind<Argument>' for later
13546 when the context of Bind becomes complete. Let's
13547 store that in a TYPENAME_TYPE. */
13548 return make_typename_type (TYPE_CONTEXT (arg),
13549 build_nt (TEMPLATE_ID_EXPR,
13550 TYPE_IDENTIFIER (arg),
13551 argvec),
13552 typename_type,
13553 complain);
13554
13555 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13556 are resolving nested-types in the signature of a
13557 member function templates. Otherwise ARG is a
13558 TEMPLATE_DECL and is the real template to be
13559 instantiated. */
13560 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13561 arg = TYPE_NAME (arg);
13562
13563 r = lookup_template_class (arg,
13564 argvec, in_decl,
13565 DECL_CONTEXT (arg),
13566 /*entering_scope=*/0,
13567 complain);
13568 return cp_build_qualified_type_real
13569 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13570 }
13571 else if (code == TEMPLATE_TEMPLATE_PARM)
13572 return arg;
13573 else
13574 /* TEMPLATE_PARM_INDEX. */
13575 return convert_from_reference (unshare_expr (arg));
13576 }
13577
13578 if (level == 1)
13579 /* This can happen during the attempted tsubst'ing in
13580 unify. This means that we don't yet have any information
13581 about the template parameter in question. */
13582 return t;
13583
13584 /* If we get here, we must have been looking at a parm for a
13585 more deeply nested template. Make a new version of this
13586 template parameter, but with a lower level. */
13587 switch (code)
13588 {
13589 case TEMPLATE_TYPE_PARM:
13590 case TEMPLATE_TEMPLATE_PARM:
13591 case BOUND_TEMPLATE_TEMPLATE_PARM:
13592 if (cp_type_quals (t))
13593 {
13594 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13595 r = cp_build_qualified_type_real
13596 (r, cp_type_quals (t),
13597 complain | (code == TEMPLATE_TYPE_PARM
13598 ? tf_ignore_bad_quals : 0));
13599 }
13600 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13601 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13602 && (r = (TEMPLATE_PARM_DESCENDANTS
13603 (TEMPLATE_TYPE_PARM_INDEX (t))))
13604 && (r = TREE_TYPE (r))
13605 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13606 /* Break infinite recursion when substituting the constraints
13607 of a constrained placeholder. */;
13608 else
13609 {
13610 r = copy_type (t);
13611 TEMPLATE_TYPE_PARM_INDEX (r)
13612 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13613 r, levels, args, complain);
13614 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13615 TYPE_MAIN_VARIANT (r) = r;
13616 TYPE_POINTER_TO (r) = NULL_TREE;
13617 TYPE_REFERENCE_TO (r) = NULL_TREE;
13618
13619 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13620 {
13621 /* Propagate constraints on placeholders. */
13622 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13623 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13624 = tsubst_constraint (constr, args, complain, in_decl);
13625 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13626 {
13627 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13628 pl = tsubst (pl, args, complain, in_decl);
13629 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13630 }
13631 }
13632
13633 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13634 /* We have reduced the level of the template
13635 template parameter, but not the levels of its
13636 template parameters, so canonical_type_parameter
13637 will not be able to find the canonical template
13638 template parameter for this level. Thus, we
13639 require structural equality checking to compare
13640 TEMPLATE_TEMPLATE_PARMs. */
13641 SET_TYPE_STRUCTURAL_EQUALITY (r);
13642 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13643 SET_TYPE_STRUCTURAL_EQUALITY (r);
13644 else
13645 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13646
13647 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13648 {
13649 tree tinfo = TYPE_TEMPLATE_INFO (t);
13650 /* We might need to substitute into the types of non-type
13651 template parameters. */
13652 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13653 complain, in_decl);
13654 if (tmpl == error_mark_node)
13655 return error_mark_node;
13656 tree argvec = tsubst (TI_ARGS (tinfo), args,
13657 complain, in_decl);
13658 if (argvec == error_mark_node)
13659 return error_mark_node;
13660
13661 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13662 = build_template_info (tmpl, argvec);
13663 }
13664 }
13665 break;
13666
13667 case TEMPLATE_PARM_INDEX:
13668 r = reduce_template_parm_level (t, type, levels, args, complain);
13669 break;
13670
13671 default:
13672 gcc_unreachable ();
13673 }
13674
13675 return r;
13676 }
13677
13678 case TREE_LIST:
13679 {
13680 tree purpose, value, chain;
13681
13682 if (t == void_list_node)
13683 return t;
13684
13685 purpose = TREE_PURPOSE (t);
13686 if (purpose)
13687 {
13688 purpose = tsubst (purpose, args, complain, in_decl);
13689 if (purpose == error_mark_node)
13690 return error_mark_node;
13691 }
13692 value = TREE_VALUE (t);
13693 if (value)
13694 {
13695 value = tsubst (value, args, complain, in_decl);
13696 if (value == error_mark_node)
13697 return error_mark_node;
13698 }
13699 chain = TREE_CHAIN (t);
13700 if (chain && chain != void_type_node)
13701 {
13702 chain = tsubst (chain, args, complain, in_decl);
13703 if (chain == error_mark_node)
13704 return error_mark_node;
13705 }
13706 if (purpose == TREE_PURPOSE (t)
13707 && value == TREE_VALUE (t)
13708 && chain == TREE_CHAIN (t))
13709 return t;
13710 return hash_tree_cons (purpose, value, chain);
13711 }
13712
13713 case TREE_BINFO:
13714 /* We should never be tsubsting a binfo. */
13715 gcc_unreachable ();
13716
13717 case TREE_VEC:
13718 /* A vector of template arguments. */
13719 gcc_assert (!type);
13720 return tsubst_template_args (t, args, complain, in_decl);
13721
13722 case POINTER_TYPE:
13723 case REFERENCE_TYPE:
13724 {
13725 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13726 return t;
13727
13728 /* [temp.deduct]
13729
13730 Type deduction may fail for any of the following
13731 reasons:
13732
13733 -- Attempting to create a pointer to reference type.
13734 -- Attempting to create a reference to a reference type or
13735 a reference to void.
13736
13737 Core issue 106 says that creating a reference to a reference
13738 during instantiation is no longer a cause for failure. We
13739 only enforce this check in strict C++98 mode. */
13740 if ((TREE_CODE (type) == REFERENCE_TYPE
13741 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13742 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13743 {
13744 static location_t last_loc;
13745
13746 /* We keep track of the last time we issued this error
13747 message to avoid spewing a ton of messages during a
13748 single bad template instantiation. */
13749 if (complain & tf_error
13750 && last_loc != input_location)
13751 {
13752 if (VOID_TYPE_P (type))
13753 error ("forming reference to void");
13754 else if (code == POINTER_TYPE)
13755 error ("forming pointer to reference type %qT", type);
13756 else
13757 error ("forming reference to reference type %qT", type);
13758 last_loc = input_location;
13759 }
13760
13761 return error_mark_node;
13762 }
13763 else if (TREE_CODE (type) == FUNCTION_TYPE
13764 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13765 || type_memfn_rqual (type) != REF_QUAL_NONE))
13766 {
13767 if (complain & tf_error)
13768 {
13769 if (code == POINTER_TYPE)
13770 error ("forming pointer to qualified function type %qT",
13771 type);
13772 else
13773 error ("forming reference to qualified function type %qT",
13774 type);
13775 }
13776 return error_mark_node;
13777 }
13778 else if (code == POINTER_TYPE)
13779 {
13780 r = build_pointer_type (type);
13781 if (TREE_CODE (type) == METHOD_TYPE)
13782 r = build_ptrmemfunc_type (r);
13783 }
13784 else if (TREE_CODE (type) == REFERENCE_TYPE)
13785 /* In C++0x, during template argument substitution, when there is an
13786 attempt to create a reference to a reference type, reference
13787 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13788
13789 "If a template-argument for a template-parameter T names a type
13790 that is a reference to a type A, an attempt to create the type
13791 'lvalue reference to cv T' creates the type 'lvalue reference to
13792 A,' while an attempt to create the type type rvalue reference to
13793 cv T' creates the type T"
13794 */
13795 r = cp_build_reference_type
13796 (TREE_TYPE (type),
13797 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13798 else
13799 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13800 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13801
13802 if (r != error_mark_node)
13803 /* Will this ever be needed for TYPE_..._TO values? */
13804 layout_type (r);
13805
13806 return r;
13807 }
13808 case OFFSET_TYPE:
13809 {
13810 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13811 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13812 {
13813 /* [temp.deduct]
13814
13815 Type deduction may fail for any of the following
13816 reasons:
13817
13818 -- Attempting to create "pointer to member of T" when T
13819 is not a class type. */
13820 if (complain & tf_error)
13821 error ("creating pointer to member of non-class type %qT", r);
13822 return error_mark_node;
13823 }
13824 if (TREE_CODE (type) == REFERENCE_TYPE)
13825 {
13826 if (complain & tf_error)
13827 error ("creating pointer to member reference type %qT", type);
13828 return error_mark_node;
13829 }
13830 if (VOID_TYPE_P (type))
13831 {
13832 if (complain & tf_error)
13833 error ("creating pointer to member of type void");
13834 return error_mark_node;
13835 }
13836 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13837 if (TREE_CODE (type) == FUNCTION_TYPE)
13838 {
13839 /* The type of the implicit object parameter gets its
13840 cv-qualifiers from the FUNCTION_TYPE. */
13841 tree memptr;
13842 tree method_type
13843 = build_memfn_type (type, r, type_memfn_quals (type),
13844 type_memfn_rqual (type));
13845 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13846 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13847 complain);
13848 }
13849 else
13850 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13851 cp_type_quals (t),
13852 complain);
13853 }
13854 case FUNCTION_TYPE:
13855 case METHOD_TYPE:
13856 {
13857 tree fntype;
13858 tree specs;
13859 fntype = tsubst_function_type (t, args, complain, in_decl);
13860 if (fntype == error_mark_node)
13861 return error_mark_node;
13862
13863 /* Substitute the exception specification. */
13864 specs = tsubst_exception_specification (t, args, complain, in_decl,
13865 /*defer_ok*/fndecl_type);
13866 if (specs == error_mark_node)
13867 return error_mark_node;
13868 if (specs)
13869 fntype = build_exception_variant (fntype, specs);
13870 return fntype;
13871 }
13872 case ARRAY_TYPE:
13873 {
13874 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13875 if (domain == error_mark_node)
13876 return error_mark_node;
13877
13878 /* As an optimization, we avoid regenerating the array type if
13879 it will obviously be the same as T. */
13880 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13881 return t;
13882
13883 /* These checks should match the ones in create_array_type_for_decl.
13884
13885 [temp.deduct]
13886
13887 The deduction may fail for any of the following reasons:
13888
13889 -- Attempting to create an array with an element type that
13890 is void, a function type, or a reference type, or [DR337]
13891 an abstract class type. */
13892 if (VOID_TYPE_P (type)
13893 || TREE_CODE (type) == FUNCTION_TYPE
13894 || (TREE_CODE (type) == ARRAY_TYPE
13895 && TYPE_DOMAIN (type) == NULL_TREE)
13896 || TREE_CODE (type) == REFERENCE_TYPE)
13897 {
13898 if (complain & tf_error)
13899 error ("creating array of %qT", type);
13900 return error_mark_node;
13901 }
13902
13903 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13904 return error_mark_node;
13905
13906 r = build_cplus_array_type (type, domain);
13907
13908 if (TYPE_USER_ALIGN (t))
13909 {
13910 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13911 TYPE_USER_ALIGN (r) = 1;
13912 }
13913
13914 return r;
13915 }
13916
13917 case TYPENAME_TYPE:
13918 {
13919 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13920 in_decl, /*entering_scope=*/1);
13921 if (ctx == error_mark_node)
13922 return error_mark_node;
13923
13924 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13925 complain, in_decl);
13926 if (f == error_mark_node)
13927 return error_mark_node;
13928
13929 if (!MAYBE_CLASS_TYPE_P (ctx))
13930 {
13931 if (complain & tf_error)
13932 error ("%qT is not a class, struct, or union type", ctx);
13933 return error_mark_node;
13934 }
13935 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13936 {
13937 /* Normally, make_typename_type does not require that the CTX
13938 have complete type in order to allow things like:
13939
13940 template <class T> struct S { typename S<T>::X Y; };
13941
13942 But, such constructs have already been resolved by this
13943 point, so here CTX really should have complete type, unless
13944 it's a partial instantiation. */
13945 ctx = complete_type (ctx);
13946 if (!COMPLETE_TYPE_P (ctx))
13947 {
13948 if (complain & tf_error)
13949 cxx_incomplete_type_error (NULL_TREE, ctx);
13950 return error_mark_node;
13951 }
13952 }
13953
13954 f = make_typename_type (ctx, f, typename_type,
13955 complain | tf_keep_type_decl);
13956 if (f == error_mark_node)
13957 return f;
13958 if (TREE_CODE (f) == TYPE_DECL)
13959 {
13960 complain |= tf_ignore_bad_quals;
13961 f = TREE_TYPE (f);
13962 }
13963
13964 if (TREE_CODE (f) != TYPENAME_TYPE)
13965 {
13966 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13967 {
13968 if (complain & tf_error)
13969 error ("%qT resolves to %qT, which is not an enumeration type",
13970 t, f);
13971 else
13972 return error_mark_node;
13973 }
13974 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13975 {
13976 if (complain & tf_error)
13977 error ("%qT resolves to %qT, which is is not a class type",
13978 t, f);
13979 else
13980 return error_mark_node;
13981 }
13982 }
13983
13984 return cp_build_qualified_type_real
13985 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13986 }
13987
13988 case UNBOUND_CLASS_TEMPLATE:
13989 {
13990 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13991 in_decl, /*entering_scope=*/1);
13992 tree name = TYPE_IDENTIFIER (t);
13993 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13994
13995 if (ctx == error_mark_node || name == error_mark_node)
13996 return error_mark_node;
13997
13998 if (parm_list)
13999 parm_list = tsubst_template_parms (parm_list, args, complain);
14000 return make_unbound_class_template (ctx, name, parm_list, complain);
14001 }
14002
14003 case TYPEOF_TYPE:
14004 {
14005 tree type;
14006
14007 ++cp_unevaluated_operand;
14008 ++c_inhibit_evaluation_warnings;
14009
14010 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14011 complain, in_decl,
14012 /*integral_constant_expression_p=*/false);
14013
14014 --cp_unevaluated_operand;
14015 --c_inhibit_evaluation_warnings;
14016
14017 type = finish_typeof (type);
14018 return cp_build_qualified_type_real (type,
14019 cp_type_quals (t)
14020 | cp_type_quals (type),
14021 complain);
14022 }
14023
14024 case DECLTYPE_TYPE:
14025 {
14026 tree type;
14027
14028 ++cp_unevaluated_operand;
14029 ++c_inhibit_evaluation_warnings;
14030
14031 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14032 complain|tf_decltype, in_decl,
14033 /*function_p*/false,
14034 /*integral_constant_expression*/false);
14035
14036 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14037 {
14038 if (type == NULL_TREE)
14039 {
14040 if (complain & tf_error)
14041 error ("empty initializer in lambda init-capture");
14042 type = error_mark_node;
14043 }
14044 else if (TREE_CODE (type) == TREE_LIST)
14045 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14046 }
14047
14048 --cp_unevaluated_operand;
14049 --c_inhibit_evaluation_warnings;
14050
14051 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14052 type = lambda_capture_field_type (type,
14053 DECLTYPE_FOR_INIT_CAPTURE (t),
14054 DECLTYPE_FOR_REF_CAPTURE (t));
14055 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14056 type = lambda_proxy_type (type);
14057 else
14058 {
14059 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14060 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14061 && EXPR_P (type))
14062 /* In a template ~id could be either a complement expression
14063 or an unqualified-id naming a destructor; if instantiating
14064 it produces an expression, it's not an id-expression or
14065 member access. */
14066 id = false;
14067 type = finish_decltype_type (type, id, complain);
14068 }
14069 return cp_build_qualified_type_real (type,
14070 cp_type_quals (t)
14071 | cp_type_quals (type),
14072 complain | tf_ignore_bad_quals);
14073 }
14074
14075 case UNDERLYING_TYPE:
14076 {
14077 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14078 complain, in_decl);
14079 return finish_underlying_type (type);
14080 }
14081
14082 case TYPE_ARGUMENT_PACK:
14083 case NONTYPE_ARGUMENT_PACK:
14084 {
14085 tree r;
14086
14087 if (code == NONTYPE_ARGUMENT_PACK)
14088 {
14089 r = make_node (code);
14090 /* Set the already-substituted type. */
14091 TREE_TYPE (r) = type;
14092 }
14093 else
14094 r = cxx_make_type (code);
14095
14096 tree pack_args = ARGUMENT_PACK_ARGS (t);
14097 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14098 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14099
14100 return r;
14101 }
14102
14103 case VOID_CST:
14104 case INTEGER_CST:
14105 case REAL_CST:
14106 case STRING_CST:
14107 case PLUS_EXPR:
14108 case MINUS_EXPR:
14109 case NEGATE_EXPR:
14110 case NOP_EXPR:
14111 case INDIRECT_REF:
14112 case ADDR_EXPR:
14113 case CALL_EXPR:
14114 case ARRAY_REF:
14115 case SCOPE_REF:
14116 /* We should use one of the expression tsubsts for these codes. */
14117 gcc_unreachable ();
14118
14119 default:
14120 sorry ("use of %qs in template", get_tree_code_name (code));
14121 return error_mark_node;
14122 }
14123 }
14124
14125 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14126 type of the expression on the left-hand side of the "." or "->"
14127 operator. */
14128
14129 static tree
14130 tsubst_baselink (tree baselink, tree object_type,
14131 tree args, tsubst_flags_t complain, tree in_decl)
14132 {
14133 tree name;
14134 tree qualifying_scope;
14135 tree fns;
14136 tree optype;
14137 tree template_args = 0;
14138 bool template_id_p = false;
14139 bool qualified = BASELINK_QUALIFIED_P (baselink);
14140
14141 /* A baselink indicates a function from a base class. Both the
14142 BASELINK_ACCESS_BINFO and the base class referenced may
14143 indicate bases of the template class, rather than the
14144 instantiated class. In addition, lookups that were not
14145 ambiguous before may be ambiguous now. Therefore, we perform
14146 the lookup again. */
14147 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14148 qualifying_scope = tsubst (qualifying_scope, args,
14149 complain, in_decl);
14150 fns = BASELINK_FUNCTIONS (baselink);
14151 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
14152 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14153 {
14154 template_id_p = true;
14155 template_args = TREE_OPERAND (fns, 1);
14156 fns = TREE_OPERAND (fns, 0);
14157 if (template_args)
14158 template_args = tsubst_template_args (template_args, args,
14159 complain, in_decl);
14160 }
14161 name = DECL_NAME (get_first_fn (fns));
14162 if (IDENTIFIER_TYPENAME_P (name))
14163 name = mangle_conv_op_name_for_type (optype);
14164 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14165 if (!baselink)
14166 {
14167 if (constructor_name_p (name, qualifying_scope))
14168 {
14169 if (complain & tf_error)
14170 error ("cannot call constructor %<%T::%D%> directly",
14171 qualifying_scope, name);
14172 }
14173 return error_mark_node;
14174 }
14175
14176 /* If lookup found a single function, mark it as used at this
14177 point. (If it lookup found multiple functions the one selected
14178 later by overload resolution will be marked as used at that
14179 point.) */
14180 if (BASELINK_P (baselink))
14181 fns = BASELINK_FUNCTIONS (baselink);
14182 if (!template_id_p && !really_overloaded_fn (fns)
14183 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
14184 return error_mark_node;
14185
14186 /* Add back the template arguments, if present. */
14187 if (BASELINK_P (baselink) && template_id_p)
14188 BASELINK_FUNCTIONS (baselink)
14189 = build2 (TEMPLATE_ID_EXPR,
14190 unknown_type_node,
14191 BASELINK_FUNCTIONS (baselink),
14192 template_args);
14193 /* Update the conversion operator type. */
14194 if (BASELINK_P (baselink))
14195 BASELINK_OPTYPE (baselink) = optype;
14196
14197 if (!object_type)
14198 object_type = current_class_type;
14199
14200 if (qualified || name == complete_dtor_identifier)
14201 {
14202 baselink = adjust_result_of_qualified_name_lookup (baselink,
14203 qualifying_scope,
14204 object_type);
14205 if (!qualified)
14206 /* We need to call adjust_result_of_qualified_name_lookup in case the
14207 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14208 so that we still get virtual function binding. */
14209 BASELINK_QUALIFIED_P (baselink) = false;
14210 }
14211 return baselink;
14212 }
14213
14214 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14215 true if the qualified-id will be a postfix-expression in-and-of
14216 itself; false if more of the postfix-expression follows the
14217 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14218 of "&". */
14219
14220 static tree
14221 tsubst_qualified_id (tree qualified_id, tree args,
14222 tsubst_flags_t complain, tree in_decl,
14223 bool done, bool address_p)
14224 {
14225 tree expr;
14226 tree scope;
14227 tree name;
14228 bool is_template;
14229 tree template_args;
14230 location_t loc = UNKNOWN_LOCATION;
14231
14232 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14233
14234 /* Figure out what name to look up. */
14235 name = TREE_OPERAND (qualified_id, 1);
14236 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14237 {
14238 is_template = true;
14239 loc = EXPR_LOCATION (name);
14240 template_args = TREE_OPERAND (name, 1);
14241 if (template_args)
14242 template_args = tsubst_template_args (template_args, args,
14243 complain, in_decl);
14244 if (template_args == error_mark_node)
14245 return error_mark_node;
14246 name = TREE_OPERAND (name, 0);
14247 }
14248 else
14249 {
14250 is_template = false;
14251 template_args = NULL_TREE;
14252 }
14253
14254 /* Substitute into the qualifying scope. When there are no ARGS, we
14255 are just trying to simplify a non-dependent expression. In that
14256 case the qualifying scope may be dependent, and, in any case,
14257 substituting will not help. */
14258 scope = TREE_OPERAND (qualified_id, 0);
14259 if (args)
14260 {
14261 scope = tsubst (scope, args, complain, in_decl);
14262 expr = tsubst_copy (name, args, complain, in_decl);
14263 }
14264 else
14265 expr = name;
14266
14267 if (dependent_scope_p (scope))
14268 {
14269 if (is_template)
14270 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14271 tree r = build_qualified_name (NULL_TREE, scope, expr,
14272 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14273 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14274 return r;
14275 }
14276
14277 if (!BASELINK_P (name) && !DECL_P (expr))
14278 {
14279 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14280 {
14281 /* A BIT_NOT_EXPR is used to represent a destructor. */
14282 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14283 {
14284 error ("qualifying type %qT does not match destructor name ~%qT",
14285 scope, TREE_OPERAND (expr, 0));
14286 expr = error_mark_node;
14287 }
14288 else
14289 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14290 /*is_type_p=*/0, false);
14291 }
14292 else
14293 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14294 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14295 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14296 {
14297 if (complain & tf_error)
14298 {
14299 error ("dependent-name %qE is parsed as a non-type, but "
14300 "instantiation yields a type", qualified_id);
14301 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14302 }
14303 return error_mark_node;
14304 }
14305 }
14306
14307 if (DECL_P (expr))
14308 {
14309 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14310 scope);
14311 /* Remember that there was a reference to this entity. */
14312 if (!mark_used (expr, complain) && !(complain & tf_error))
14313 return error_mark_node;
14314 }
14315
14316 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14317 {
14318 if (complain & tf_error)
14319 qualified_name_lookup_error (scope,
14320 TREE_OPERAND (qualified_id, 1),
14321 expr, input_location);
14322 return error_mark_node;
14323 }
14324
14325 if (is_template)
14326 {
14327 if (variable_template_p (expr))
14328 expr = lookup_and_finish_template_variable (expr, template_args,
14329 complain);
14330 else
14331 expr = lookup_template_function (expr, template_args);
14332 }
14333
14334 if (expr == error_mark_node && complain & tf_error)
14335 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14336 expr, input_location);
14337 else if (TYPE_P (scope))
14338 {
14339 expr = (adjust_result_of_qualified_name_lookup
14340 (expr, scope, current_nonlambda_class_type ()));
14341 expr = (finish_qualified_id_expr
14342 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14343 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14344 /*template_arg_p=*/false, complain));
14345 }
14346
14347 /* Expressions do not generally have reference type. */
14348 if (TREE_CODE (expr) != SCOPE_REF
14349 /* However, if we're about to form a pointer-to-member, we just
14350 want the referenced member referenced. */
14351 && TREE_CODE (expr) != OFFSET_REF)
14352 expr = convert_from_reference (expr);
14353
14354 if (REF_PARENTHESIZED_P (qualified_id))
14355 expr = force_paren_expr (expr);
14356
14357 return expr;
14358 }
14359
14360 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14361 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14362 for tsubst. */
14363
14364 static tree
14365 tsubst_init (tree init, tree decl, tree args,
14366 tsubst_flags_t complain, tree in_decl)
14367 {
14368 if (!init)
14369 return NULL_TREE;
14370
14371 init = tsubst_expr (init, args, complain, in_decl, false);
14372
14373 if (!init && TREE_TYPE (decl) != error_mark_node)
14374 {
14375 /* If we had an initializer but it
14376 instantiated to nothing,
14377 value-initialize the object. This will
14378 only occur when the initializer was a
14379 pack expansion where the parameter packs
14380 used in that expansion were of length
14381 zero. */
14382 init = build_value_init (TREE_TYPE (decl),
14383 complain);
14384 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14385 init = get_target_expr_sfinae (init, complain);
14386 }
14387
14388 return init;
14389 }
14390
14391 /* Like tsubst, but deals with expressions. This function just replaces
14392 template parms; to finish processing the resultant expression, use
14393 tsubst_copy_and_build or tsubst_expr. */
14394
14395 static tree
14396 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14397 {
14398 enum tree_code code;
14399 tree r;
14400
14401 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14402 return t;
14403
14404 code = TREE_CODE (t);
14405
14406 switch (code)
14407 {
14408 case PARM_DECL:
14409 r = retrieve_local_specialization (t);
14410
14411 if (r == NULL_TREE)
14412 {
14413 /* We get here for a use of 'this' in an NSDMI as part of a
14414 constructor call or as part of an aggregate initialization. */
14415 if (DECL_NAME (t) == this_identifier
14416 && ((current_function_decl
14417 && DECL_CONSTRUCTOR_P (current_function_decl))
14418 || (current_class_ref
14419 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14420 return current_class_ptr;
14421
14422 /* This can happen for a parameter name used later in a function
14423 declaration (such as in a late-specified return type). Just
14424 make a dummy decl, since it's only used for its type. */
14425 gcc_assert (cp_unevaluated_operand != 0);
14426 r = tsubst_decl (t, args, complain);
14427 /* Give it the template pattern as its context; its true context
14428 hasn't been instantiated yet and this is good enough for
14429 mangling. */
14430 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14431 }
14432
14433 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14434 r = ARGUMENT_PACK_SELECT_ARG (r);
14435 if (!mark_used (r, complain) && !(complain & tf_error))
14436 return error_mark_node;
14437 return r;
14438
14439 case CONST_DECL:
14440 {
14441 tree enum_type;
14442 tree v;
14443
14444 if (DECL_TEMPLATE_PARM_P (t))
14445 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14446 /* There is no need to substitute into namespace-scope
14447 enumerators. */
14448 if (DECL_NAMESPACE_SCOPE_P (t))
14449 return t;
14450 /* If ARGS is NULL, then T is known to be non-dependent. */
14451 if (args == NULL_TREE)
14452 return scalar_constant_value (t);
14453
14454 /* Unfortunately, we cannot just call lookup_name here.
14455 Consider:
14456
14457 template <int I> int f() {
14458 enum E { a = I };
14459 struct S { void g() { E e = a; } };
14460 };
14461
14462 When we instantiate f<7>::S::g(), say, lookup_name is not
14463 clever enough to find f<7>::a. */
14464 enum_type
14465 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14466 /*entering_scope=*/0);
14467
14468 for (v = TYPE_VALUES (enum_type);
14469 v != NULL_TREE;
14470 v = TREE_CHAIN (v))
14471 if (TREE_PURPOSE (v) == DECL_NAME (t))
14472 return TREE_VALUE (v);
14473
14474 /* We didn't find the name. That should never happen; if
14475 name-lookup found it during preliminary parsing, we
14476 should find it again here during instantiation. */
14477 gcc_unreachable ();
14478 }
14479 return t;
14480
14481 case FIELD_DECL:
14482 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14483 {
14484 /* Check for a local specialization set up by
14485 tsubst_pack_expansion. */
14486 if (tree r = retrieve_local_specialization (t))
14487 {
14488 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14489 r = ARGUMENT_PACK_SELECT_ARG (r);
14490 return r;
14491 }
14492
14493 /* When retrieving a capture pack from a generic lambda, remove the
14494 lambda call op's own template argument list from ARGS. Only the
14495 template arguments active for the closure type should be used to
14496 retrieve the pack specialization. */
14497 if (LAMBDA_FUNCTION_P (current_function_decl)
14498 && (template_class_depth (DECL_CONTEXT (t))
14499 != TMPL_ARGS_DEPTH (args)))
14500 args = strip_innermost_template_args (args, 1);
14501
14502 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14503 tsubst_decl put in the hash table. */
14504 return retrieve_specialization (t, args, 0);
14505 }
14506
14507 if (DECL_CONTEXT (t))
14508 {
14509 tree ctx;
14510
14511 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14512 /*entering_scope=*/1);
14513 if (ctx != DECL_CONTEXT (t))
14514 {
14515 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14516 if (!r)
14517 {
14518 if (complain & tf_error)
14519 error ("using invalid field %qD", t);
14520 return error_mark_node;
14521 }
14522 return r;
14523 }
14524 }
14525
14526 return t;
14527
14528 case VAR_DECL:
14529 case FUNCTION_DECL:
14530 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14531 r = tsubst (t, args, complain, in_decl);
14532 else if (local_variable_p (t)
14533 && uses_template_parms (DECL_CONTEXT (t)))
14534 {
14535 r = retrieve_local_specialization (t);
14536 if (r == NULL_TREE)
14537 {
14538 /* First try name lookup to find the instantiation. */
14539 r = lookup_name (DECL_NAME (t));
14540 if (r)
14541 {
14542 /* Make sure that the one we found is the one we want. */
14543 tree ctx = DECL_CONTEXT (t);
14544 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14545 ctx = tsubst (ctx, args, complain, in_decl);
14546 if (ctx != DECL_CONTEXT (r))
14547 r = NULL_TREE;
14548 }
14549
14550 if (r)
14551 /* OK */;
14552 else
14553 {
14554 /* This can happen for a variable used in a
14555 late-specified return type of a local lambda, or for a
14556 local static or constant. Building a new VAR_DECL
14557 should be OK in all those cases. */
14558 r = tsubst_decl (t, args, complain);
14559 if (decl_maybe_constant_var_p (r))
14560 {
14561 /* We can't call cp_finish_decl, so handle the
14562 initializer by hand. */
14563 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14564 complain, in_decl);
14565 if (!processing_template_decl)
14566 init = maybe_constant_init (init);
14567 if (processing_template_decl
14568 ? potential_constant_expression (init)
14569 : reduced_constant_expression_p (init))
14570 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14571 = TREE_CONSTANT (r) = true;
14572 DECL_INITIAL (r) = init;
14573 }
14574 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14575 || decl_constant_var_p (r)
14576 || errorcount || sorrycount);
14577 if (!processing_template_decl
14578 && !TREE_STATIC (r))
14579 r = process_outer_var_ref (r, complain);
14580 }
14581 /* Remember this for subsequent uses. */
14582 if (local_specializations)
14583 register_local_specialization (r, t);
14584 }
14585 }
14586 else
14587 r = t;
14588 if (!mark_used (r, complain))
14589 return error_mark_node;
14590 return r;
14591
14592 case NAMESPACE_DECL:
14593 return t;
14594
14595 case OVERLOAD:
14596 /* An OVERLOAD will always be a non-dependent overload set; an
14597 overload set from function scope will just be represented with an
14598 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14599 gcc_assert (!uses_template_parms (t));
14600 return t;
14601
14602 case BASELINK:
14603 return tsubst_baselink (t, current_nonlambda_class_type (),
14604 args, complain, in_decl);
14605
14606 case TEMPLATE_DECL:
14607 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14608 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14609 args, complain, in_decl);
14610 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14611 return tsubst (t, args, complain, in_decl);
14612 else if (DECL_CLASS_SCOPE_P (t)
14613 && uses_template_parms (DECL_CONTEXT (t)))
14614 {
14615 /* Template template argument like the following example need
14616 special treatment:
14617
14618 template <template <class> class TT> struct C {};
14619 template <class T> struct D {
14620 template <class U> struct E {};
14621 C<E> c; // #1
14622 };
14623 D<int> d; // #2
14624
14625 We are processing the template argument `E' in #1 for
14626 the template instantiation #2. Originally, `E' is a
14627 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14628 have to substitute this with one having context `D<int>'. */
14629
14630 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14631 return lookup_field (context, DECL_NAME(t), 0, false);
14632 }
14633 else
14634 /* Ordinary template template argument. */
14635 return t;
14636
14637 case CAST_EXPR:
14638 case REINTERPRET_CAST_EXPR:
14639 case CONST_CAST_EXPR:
14640 case STATIC_CAST_EXPR:
14641 case DYNAMIC_CAST_EXPR:
14642 case IMPLICIT_CONV_EXPR:
14643 case CONVERT_EXPR:
14644 case NOP_EXPR:
14645 {
14646 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14647 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14648 return build1 (code, type, op0);
14649 }
14650
14651 case SIZEOF_EXPR:
14652 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14653 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14654 {
14655 tree expanded, op = TREE_OPERAND (t, 0);
14656 int len = 0;
14657
14658 if (SIZEOF_EXPR_TYPE_P (t))
14659 op = TREE_TYPE (op);
14660
14661 ++cp_unevaluated_operand;
14662 ++c_inhibit_evaluation_warnings;
14663 /* We only want to compute the number of arguments. */
14664 if (PACK_EXPANSION_P (op))
14665 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14666 else
14667 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14668 args, complain, in_decl);
14669 --cp_unevaluated_operand;
14670 --c_inhibit_evaluation_warnings;
14671
14672 if (TREE_CODE (expanded) == TREE_VEC)
14673 {
14674 len = TREE_VEC_LENGTH (expanded);
14675 /* Set TREE_USED for the benefit of -Wunused. */
14676 for (int i = 0; i < len; i++)
14677 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14678 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14679 }
14680
14681 if (expanded == error_mark_node)
14682 return error_mark_node;
14683 else if (PACK_EXPANSION_P (expanded)
14684 || (TREE_CODE (expanded) == TREE_VEC
14685 && pack_expansion_args_count (expanded)))
14686
14687 {
14688 if (PACK_EXPANSION_P (expanded))
14689 /* OK. */;
14690 else if (TREE_VEC_LENGTH (expanded) == 1)
14691 expanded = TREE_VEC_ELT (expanded, 0);
14692 else
14693 expanded = make_argument_pack (expanded);
14694
14695 if (TYPE_P (expanded))
14696 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14697 complain & tf_error);
14698 else
14699 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14700 complain & tf_error);
14701 }
14702 else
14703 return build_int_cst (size_type_node, len);
14704 }
14705 if (SIZEOF_EXPR_TYPE_P (t))
14706 {
14707 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14708 args, complain, in_decl);
14709 r = build1 (NOP_EXPR, r, error_mark_node);
14710 r = build1 (SIZEOF_EXPR,
14711 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14712 SIZEOF_EXPR_TYPE_P (r) = 1;
14713 return r;
14714 }
14715 /* Fall through */
14716
14717 case INDIRECT_REF:
14718 case NEGATE_EXPR:
14719 case TRUTH_NOT_EXPR:
14720 case BIT_NOT_EXPR:
14721 case ADDR_EXPR:
14722 case UNARY_PLUS_EXPR: /* Unary + */
14723 case ALIGNOF_EXPR:
14724 case AT_ENCODE_EXPR:
14725 case ARROW_EXPR:
14726 case THROW_EXPR:
14727 case TYPEID_EXPR:
14728 case REALPART_EXPR:
14729 case IMAGPART_EXPR:
14730 case PAREN_EXPR:
14731 {
14732 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14733 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14734 return build1 (code, type, op0);
14735 }
14736
14737 case COMPONENT_REF:
14738 {
14739 tree object;
14740 tree name;
14741
14742 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14743 name = TREE_OPERAND (t, 1);
14744 if (TREE_CODE (name) == BIT_NOT_EXPR)
14745 {
14746 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14747 complain, in_decl);
14748 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14749 }
14750 else if (TREE_CODE (name) == SCOPE_REF
14751 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14752 {
14753 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14754 complain, in_decl);
14755 name = TREE_OPERAND (name, 1);
14756 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14757 complain, in_decl);
14758 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14759 name = build_qualified_name (/*type=*/NULL_TREE,
14760 base, name,
14761 /*template_p=*/false);
14762 }
14763 else if (BASELINK_P (name))
14764 name = tsubst_baselink (name,
14765 non_reference (TREE_TYPE (object)),
14766 args, complain,
14767 in_decl);
14768 else
14769 name = tsubst_copy (name, args, complain, in_decl);
14770 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14771 }
14772
14773 case PLUS_EXPR:
14774 case MINUS_EXPR:
14775 case MULT_EXPR:
14776 case TRUNC_DIV_EXPR:
14777 case CEIL_DIV_EXPR:
14778 case FLOOR_DIV_EXPR:
14779 case ROUND_DIV_EXPR:
14780 case EXACT_DIV_EXPR:
14781 case BIT_AND_EXPR:
14782 case BIT_IOR_EXPR:
14783 case BIT_XOR_EXPR:
14784 case TRUNC_MOD_EXPR:
14785 case FLOOR_MOD_EXPR:
14786 case TRUTH_ANDIF_EXPR:
14787 case TRUTH_ORIF_EXPR:
14788 case TRUTH_AND_EXPR:
14789 case TRUTH_OR_EXPR:
14790 case RSHIFT_EXPR:
14791 case LSHIFT_EXPR:
14792 case RROTATE_EXPR:
14793 case LROTATE_EXPR:
14794 case EQ_EXPR:
14795 case NE_EXPR:
14796 case MAX_EXPR:
14797 case MIN_EXPR:
14798 case LE_EXPR:
14799 case GE_EXPR:
14800 case LT_EXPR:
14801 case GT_EXPR:
14802 case COMPOUND_EXPR:
14803 case DOTSTAR_EXPR:
14804 case MEMBER_REF:
14805 case PREDECREMENT_EXPR:
14806 case PREINCREMENT_EXPR:
14807 case POSTDECREMENT_EXPR:
14808 case POSTINCREMENT_EXPR:
14809 {
14810 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14811 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14812 return build_nt (code, op0, op1);
14813 }
14814
14815 case SCOPE_REF:
14816 {
14817 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14818 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14819 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14820 QUALIFIED_NAME_IS_TEMPLATE (t));
14821 }
14822
14823 case ARRAY_REF:
14824 {
14825 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14826 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14827 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14828 }
14829
14830 case CALL_EXPR:
14831 {
14832 int n = VL_EXP_OPERAND_LENGTH (t);
14833 tree result = build_vl_exp (CALL_EXPR, n);
14834 int i;
14835 for (i = 0; i < n; i++)
14836 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14837 complain, in_decl);
14838 return result;
14839 }
14840
14841 case COND_EXPR:
14842 case MODOP_EXPR:
14843 case PSEUDO_DTOR_EXPR:
14844 case VEC_PERM_EXPR:
14845 {
14846 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14847 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14848 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14849 r = build_nt (code, op0, op1, op2);
14850 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14851 return r;
14852 }
14853
14854 case NEW_EXPR:
14855 {
14856 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14857 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14858 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14859 r = build_nt (code, op0, op1, op2);
14860 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14861 return r;
14862 }
14863
14864 case DELETE_EXPR:
14865 {
14866 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14867 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14868 r = build_nt (code, op0, op1);
14869 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14870 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14871 return r;
14872 }
14873
14874 case TEMPLATE_ID_EXPR:
14875 {
14876 /* Substituted template arguments */
14877 tree fn = TREE_OPERAND (t, 0);
14878 tree targs = TREE_OPERAND (t, 1);
14879
14880 fn = tsubst_copy (fn, args, complain, in_decl);
14881 if (targs)
14882 targs = tsubst_template_args (targs, args, complain, in_decl);
14883
14884 return lookup_template_function (fn, targs);
14885 }
14886
14887 case TREE_LIST:
14888 {
14889 tree purpose, value, chain;
14890
14891 if (t == void_list_node)
14892 return t;
14893
14894 purpose = TREE_PURPOSE (t);
14895 if (purpose)
14896 purpose = tsubst_copy (purpose, args, complain, in_decl);
14897 value = TREE_VALUE (t);
14898 if (value)
14899 value = tsubst_copy (value, args, complain, in_decl);
14900 chain = TREE_CHAIN (t);
14901 if (chain && chain != void_type_node)
14902 chain = tsubst_copy (chain, args, complain, in_decl);
14903 if (purpose == TREE_PURPOSE (t)
14904 && value == TREE_VALUE (t)
14905 && chain == TREE_CHAIN (t))
14906 return t;
14907 return tree_cons (purpose, value, chain);
14908 }
14909
14910 case RECORD_TYPE:
14911 case UNION_TYPE:
14912 case ENUMERAL_TYPE:
14913 case INTEGER_TYPE:
14914 case TEMPLATE_TYPE_PARM:
14915 case TEMPLATE_TEMPLATE_PARM:
14916 case BOUND_TEMPLATE_TEMPLATE_PARM:
14917 case TEMPLATE_PARM_INDEX:
14918 case POINTER_TYPE:
14919 case REFERENCE_TYPE:
14920 case OFFSET_TYPE:
14921 case FUNCTION_TYPE:
14922 case METHOD_TYPE:
14923 case ARRAY_TYPE:
14924 case TYPENAME_TYPE:
14925 case UNBOUND_CLASS_TEMPLATE:
14926 case TYPEOF_TYPE:
14927 case DECLTYPE_TYPE:
14928 case TYPE_DECL:
14929 return tsubst (t, args, complain, in_decl);
14930
14931 case USING_DECL:
14932 t = DECL_NAME (t);
14933 /* Fall through. */
14934 case IDENTIFIER_NODE:
14935 if (IDENTIFIER_TYPENAME_P (t))
14936 {
14937 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14938 return mangle_conv_op_name_for_type (new_type);
14939 }
14940 else
14941 return t;
14942
14943 case CONSTRUCTOR:
14944 /* This is handled by tsubst_copy_and_build. */
14945 gcc_unreachable ();
14946
14947 case VA_ARG_EXPR:
14948 {
14949 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14950 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14951 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14952 }
14953
14954 case CLEANUP_POINT_EXPR:
14955 /* We shouldn't have built any of these during initial template
14956 generation. Instead, they should be built during instantiation
14957 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14958 gcc_unreachable ();
14959
14960 case OFFSET_REF:
14961 {
14962 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14963 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14964 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14965 r = build2 (code, type, op0, op1);
14966 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14967 if (!mark_used (TREE_OPERAND (r, 1), complain)
14968 && !(complain & tf_error))
14969 return error_mark_node;
14970 return r;
14971 }
14972
14973 case EXPR_PACK_EXPANSION:
14974 error ("invalid use of pack expansion expression");
14975 return error_mark_node;
14976
14977 case NONTYPE_ARGUMENT_PACK:
14978 error ("use %<...%> to expand argument pack");
14979 return error_mark_node;
14980
14981 case VOID_CST:
14982 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14983 return t;
14984
14985 case INTEGER_CST:
14986 case REAL_CST:
14987 case STRING_CST:
14988 case COMPLEX_CST:
14989 {
14990 /* Instantiate any typedefs in the type. */
14991 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14992 r = fold_convert (type, t);
14993 gcc_assert (TREE_CODE (r) == code);
14994 return r;
14995 }
14996
14997 case PTRMEM_CST:
14998 /* These can sometimes show up in a partial instantiation, but never
14999 involve template parms. */
15000 gcc_assert (!uses_template_parms (t));
15001 return t;
15002
15003 case UNARY_LEFT_FOLD_EXPR:
15004 return tsubst_unary_left_fold (t, args, complain, in_decl);
15005 case UNARY_RIGHT_FOLD_EXPR:
15006 return tsubst_unary_right_fold (t, args, complain, in_decl);
15007 case BINARY_LEFT_FOLD_EXPR:
15008 return tsubst_binary_left_fold (t, args, complain, in_decl);
15009 case BINARY_RIGHT_FOLD_EXPR:
15010 return tsubst_binary_right_fold (t, args, complain, in_decl);
15011
15012 default:
15013 /* We shouldn't get here, but keep going if !flag_checking. */
15014 if (flag_checking)
15015 gcc_unreachable ();
15016 return t;
15017 }
15018 }
15019
15020 /* Helper function for tsubst_omp_clauses, used for instantiation of
15021 OMP_CLAUSE_DECL of clauses. */
15022
15023 static tree
15024 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15025 tree in_decl)
15026 {
15027 if (decl == NULL_TREE)
15028 return NULL_TREE;
15029
15030 /* Handle an OpenMP array section represented as a TREE_LIST (or
15031 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15032 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15033 TREE_LIST. We can handle it exactly the same as an array section
15034 (purpose, value, and a chain), even though the nomenclature
15035 (low_bound, length, etc) is different. */
15036 if (TREE_CODE (decl) == TREE_LIST)
15037 {
15038 tree low_bound
15039 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15040 /*integral_constant_expression_p=*/false);
15041 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15042 /*integral_constant_expression_p=*/false);
15043 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15044 in_decl);
15045 if (TREE_PURPOSE (decl) == low_bound
15046 && TREE_VALUE (decl) == length
15047 && TREE_CHAIN (decl) == chain)
15048 return decl;
15049 tree ret = tree_cons (low_bound, length, chain);
15050 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15051 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15052 return ret;
15053 }
15054 tree ret = tsubst_expr (decl, args, complain, in_decl,
15055 /*integral_constant_expression_p=*/false);
15056 /* Undo convert_from_reference tsubst_expr could have called. */
15057 if (decl
15058 && REFERENCE_REF_P (ret)
15059 && !REFERENCE_REF_P (decl))
15060 ret = TREE_OPERAND (ret, 0);
15061 return ret;
15062 }
15063
15064 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15065
15066 static tree
15067 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15068 tree args, tsubst_flags_t complain, tree in_decl)
15069 {
15070 tree new_clauses = NULL_TREE, nc, oc;
15071 tree linear_no_step = NULL_TREE;
15072
15073 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15074 {
15075 nc = copy_node (oc);
15076 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15077 new_clauses = nc;
15078
15079 switch (OMP_CLAUSE_CODE (nc))
15080 {
15081 case OMP_CLAUSE_LASTPRIVATE:
15082 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15083 {
15084 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15085 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15086 in_decl, /*integral_constant_expression_p=*/false);
15087 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15088 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15089 }
15090 /* FALLTHRU */
15091 case OMP_CLAUSE_PRIVATE:
15092 case OMP_CLAUSE_SHARED:
15093 case OMP_CLAUSE_FIRSTPRIVATE:
15094 case OMP_CLAUSE_COPYIN:
15095 case OMP_CLAUSE_COPYPRIVATE:
15096 case OMP_CLAUSE_UNIFORM:
15097 case OMP_CLAUSE_DEPEND:
15098 case OMP_CLAUSE_FROM:
15099 case OMP_CLAUSE_TO:
15100 case OMP_CLAUSE_MAP:
15101 case OMP_CLAUSE_USE_DEVICE_PTR:
15102 case OMP_CLAUSE_IS_DEVICE_PTR:
15103 OMP_CLAUSE_DECL (nc)
15104 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15105 in_decl);
15106 break;
15107 case OMP_CLAUSE_TILE:
15108 case OMP_CLAUSE_IF:
15109 case OMP_CLAUSE_NUM_THREADS:
15110 case OMP_CLAUSE_SCHEDULE:
15111 case OMP_CLAUSE_COLLAPSE:
15112 case OMP_CLAUSE_FINAL:
15113 case OMP_CLAUSE_DEVICE:
15114 case OMP_CLAUSE_DIST_SCHEDULE:
15115 case OMP_CLAUSE_NUM_TEAMS:
15116 case OMP_CLAUSE_THREAD_LIMIT:
15117 case OMP_CLAUSE_SAFELEN:
15118 case OMP_CLAUSE_SIMDLEN:
15119 case OMP_CLAUSE_NUM_TASKS:
15120 case OMP_CLAUSE_GRAINSIZE:
15121 case OMP_CLAUSE_PRIORITY:
15122 case OMP_CLAUSE_ORDERED:
15123 case OMP_CLAUSE_HINT:
15124 case OMP_CLAUSE_NUM_GANGS:
15125 case OMP_CLAUSE_NUM_WORKERS:
15126 case OMP_CLAUSE_VECTOR_LENGTH:
15127 case OMP_CLAUSE_WORKER:
15128 case OMP_CLAUSE_VECTOR:
15129 case OMP_CLAUSE_ASYNC:
15130 case OMP_CLAUSE_WAIT:
15131 OMP_CLAUSE_OPERAND (nc, 0)
15132 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15133 in_decl, /*integral_constant_expression_p=*/false);
15134 break;
15135 case OMP_CLAUSE_REDUCTION:
15136 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15137 {
15138 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15139 if (TREE_CODE (placeholder) == SCOPE_REF)
15140 {
15141 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15142 complain, in_decl);
15143 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15144 = build_qualified_name (NULL_TREE, scope,
15145 TREE_OPERAND (placeholder, 1),
15146 false);
15147 }
15148 else
15149 gcc_assert (identifier_p (placeholder));
15150 }
15151 OMP_CLAUSE_DECL (nc)
15152 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15153 in_decl);
15154 break;
15155 case OMP_CLAUSE_GANG:
15156 case OMP_CLAUSE_ALIGNED:
15157 OMP_CLAUSE_DECL (nc)
15158 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15159 in_decl);
15160 OMP_CLAUSE_OPERAND (nc, 1)
15161 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15162 in_decl, /*integral_constant_expression_p=*/false);
15163 break;
15164 case OMP_CLAUSE_LINEAR:
15165 OMP_CLAUSE_DECL (nc)
15166 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15167 in_decl);
15168 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15169 {
15170 gcc_assert (!linear_no_step);
15171 linear_no_step = nc;
15172 }
15173 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15174 OMP_CLAUSE_LINEAR_STEP (nc)
15175 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15176 complain, in_decl);
15177 else
15178 OMP_CLAUSE_LINEAR_STEP (nc)
15179 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15180 in_decl,
15181 /*integral_constant_expression_p=*/false);
15182 break;
15183 case OMP_CLAUSE_NOWAIT:
15184 case OMP_CLAUSE_DEFAULT:
15185 case OMP_CLAUSE_UNTIED:
15186 case OMP_CLAUSE_MERGEABLE:
15187 case OMP_CLAUSE_INBRANCH:
15188 case OMP_CLAUSE_NOTINBRANCH:
15189 case OMP_CLAUSE_PROC_BIND:
15190 case OMP_CLAUSE_FOR:
15191 case OMP_CLAUSE_PARALLEL:
15192 case OMP_CLAUSE_SECTIONS:
15193 case OMP_CLAUSE_TASKGROUP:
15194 case OMP_CLAUSE_NOGROUP:
15195 case OMP_CLAUSE_THREADS:
15196 case OMP_CLAUSE_SIMD:
15197 case OMP_CLAUSE_DEFAULTMAP:
15198 case OMP_CLAUSE_INDEPENDENT:
15199 case OMP_CLAUSE_AUTO:
15200 case OMP_CLAUSE_SEQ:
15201 break;
15202 default:
15203 gcc_unreachable ();
15204 }
15205 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15206 switch (OMP_CLAUSE_CODE (nc))
15207 {
15208 case OMP_CLAUSE_SHARED:
15209 case OMP_CLAUSE_PRIVATE:
15210 case OMP_CLAUSE_FIRSTPRIVATE:
15211 case OMP_CLAUSE_LASTPRIVATE:
15212 case OMP_CLAUSE_COPYPRIVATE:
15213 case OMP_CLAUSE_LINEAR:
15214 case OMP_CLAUSE_REDUCTION:
15215 case OMP_CLAUSE_USE_DEVICE_PTR:
15216 case OMP_CLAUSE_IS_DEVICE_PTR:
15217 /* tsubst_expr on SCOPE_REF results in returning
15218 finish_non_static_data_member result. Undo that here. */
15219 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15220 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15221 == IDENTIFIER_NODE))
15222 {
15223 tree t = OMP_CLAUSE_DECL (nc);
15224 tree v = t;
15225 while (v)
15226 switch (TREE_CODE (v))
15227 {
15228 case COMPONENT_REF:
15229 case MEM_REF:
15230 case INDIRECT_REF:
15231 CASE_CONVERT:
15232 case POINTER_PLUS_EXPR:
15233 v = TREE_OPERAND (v, 0);
15234 continue;
15235 case PARM_DECL:
15236 if (DECL_CONTEXT (v) == current_function_decl
15237 && DECL_ARTIFICIAL (v)
15238 && DECL_NAME (v) == this_identifier)
15239 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15240 /* FALLTHRU */
15241 default:
15242 v = NULL_TREE;
15243 break;
15244 }
15245 }
15246 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15247 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15248 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15249 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15250 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15251 {
15252 tree decl = OMP_CLAUSE_DECL (nc);
15253 if (VAR_P (decl))
15254 {
15255 if (!DECL_LANG_SPECIFIC (decl))
15256 retrofit_lang_decl (decl);
15257 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15258 }
15259 }
15260 break;
15261 default:
15262 break;
15263 }
15264 }
15265
15266 new_clauses = nreverse (new_clauses);
15267 if (ort != C_ORT_OMP_DECLARE_SIMD)
15268 {
15269 new_clauses = finish_omp_clauses (new_clauses, ort);
15270 if (linear_no_step)
15271 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15272 if (nc == linear_no_step)
15273 {
15274 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15275 break;
15276 }
15277 }
15278 return new_clauses;
15279 }
15280
15281 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15282
15283 static tree
15284 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15285 tree in_decl)
15286 {
15287 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15288
15289 tree purpose, value, chain;
15290
15291 if (t == NULL)
15292 return t;
15293
15294 if (TREE_CODE (t) != TREE_LIST)
15295 return tsubst_copy_and_build (t, args, complain, in_decl,
15296 /*function_p=*/false,
15297 /*integral_constant_expression_p=*/false);
15298
15299 if (t == void_list_node)
15300 return t;
15301
15302 purpose = TREE_PURPOSE (t);
15303 if (purpose)
15304 purpose = RECUR (purpose);
15305 value = TREE_VALUE (t);
15306 if (value)
15307 {
15308 if (TREE_CODE (value) != LABEL_DECL)
15309 value = RECUR (value);
15310 else
15311 {
15312 value = lookup_label (DECL_NAME (value));
15313 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15314 TREE_USED (value) = 1;
15315 }
15316 }
15317 chain = TREE_CHAIN (t);
15318 if (chain && chain != void_type_node)
15319 chain = RECUR (chain);
15320 return tree_cons (purpose, value, chain);
15321 #undef RECUR
15322 }
15323
15324 /* Used to temporarily communicate the list of #pragma omp parallel
15325 clauses to #pragma omp for instantiation if they are combined
15326 together. */
15327
15328 static tree *omp_parallel_combined_clauses;
15329
15330 /* Substitute one OMP_FOR iterator. */
15331
15332 static void
15333 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15334 tree initv, tree condv, tree incrv, tree *clauses,
15335 tree args, tsubst_flags_t complain, tree in_decl,
15336 bool integral_constant_expression_p)
15337 {
15338 #define RECUR(NODE) \
15339 tsubst_expr ((NODE), args, complain, in_decl, \
15340 integral_constant_expression_p)
15341 tree decl, init, cond, incr;
15342
15343 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15344 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15345
15346 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15347 {
15348 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15349 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15350 }
15351
15352 decl = TREE_OPERAND (init, 0);
15353 init = TREE_OPERAND (init, 1);
15354 tree decl_expr = NULL_TREE;
15355 if (init && TREE_CODE (init) == DECL_EXPR)
15356 {
15357 /* We need to jump through some hoops to handle declarations in the
15358 init-statement, since we might need to handle auto deduction,
15359 but we need to keep control of initialization. */
15360 decl_expr = init;
15361 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15362 decl = tsubst_decl (decl, args, complain);
15363 }
15364 else
15365 {
15366 if (TREE_CODE (decl) == SCOPE_REF)
15367 {
15368 decl = RECUR (decl);
15369 if (TREE_CODE (decl) == COMPONENT_REF)
15370 {
15371 tree v = decl;
15372 while (v)
15373 switch (TREE_CODE (v))
15374 {
15375 case COMPONENT_REF:
15376 case MEM_REF:
15377 case INDIRECT_REF:
15378 CASE_CONVERT:
15379 case POINTER_PLUS_EXPR:
15380 v = TREE_OPERAND (v, 0);
15381 continue;
15382 case PARM_DECL:
15383 if (DECL_CONTEXT (v) == current_function_decl
15384 && DECL_ARTIFICIAL (v)
15385 && DECL_NAME (v) == this_identifier)
15386 {
15387 decl = TREE_OPERAND (decl, 1);
15388 decl = omp_privatize_field (decl, false);
15389 }
15390 /* FALLTHRU */
15391 default:
15392 v = NULL_TREE;
15393 break;
15394 }
15395 }
15396 }
15397 else
15398 decl = RECUR (decl);
15399 }
15400 init = RECUR (init);
15401
15402 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15403 if (auto_node && init)
15404 TREE_TYPE (decl)
15405 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15406
15407 gcc_assert (!type_dependent_expression_p (decl));
15408
15409 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15410 {
15411 if (decl_expr)
15412 {
15413 /* Declare the variable, but don't let that initialize it. */
15414 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15415 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15416 RECUR (decl_expr);
15417 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15418 }
15419
15420 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15421 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15422 if (TREE_CODE (incr) == MODIFY_EXPR)
15423 {
15424 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15425 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15426 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15427 NOP_EXPR, rhs, complain);
15428 }
15429 else
15430 incr = RECUR (incr);
15431 TREE_VEC_ELT (declv, i) = decl;
15432 TREE_VEC_ELT (initv, i) = init;
15433 TREE_VEC_ELT (condv, i) = cond;
15434 TREE_VEC_ELT (incrv, i) = incr;
15435 return;
15436 }
15437
15438 if (decl_expr)
15439 {
15440 /* Declare and initialize the variable. */
15441 RECUR (decl_expr);
15442 init = NULL_TREE;
15443 }
15444 else if (init)
15445 {
15446 tree *pc;
15447 int j;
15448 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15449 {
15450 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15451 {
15452 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15453 && OMP_CLAUSE_DECL (*pc) == decl)
15454 break;
15455 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15456 && OMP_CLAUSE_DECL (*pc) == decl)
15457 {
15458 if (j)
15459 break;
15460 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15461 tree c = *pc;
15462 *pc = OMP_CLAUSE_CHAIN (c);
15463 OMP_CLAUSE_CHAIN (c) = *clauses;
15464 *clauses = c;
15465 }
15466 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15467 && OMP_CLAUSE_DECL (*pc) == decl)
15468 {
15469 error ("iteration variable %qD should not be firstprivate",
15470 decl);
15471 *pc = OMP_CLAUSE_CHAIN (*pc);
15472 }
15473 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15474 && OMP_CLAUSE_DECL (*pc) == decl)
15475 {
15476 error ("iteration variable %qD should not be reduction",
15477 decl);
15478 *pc = OMP_CLAUSE_CHAIN (*pc);
15479 }
15480 else
15481 pc = &OMP_CLAUSE_CHAIN (*pc);
15482 }
15483 if (*pc)
15484 break;
15485 }
15486 if (*pc == NULL_TREE)
15487 {
15488 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15489 OMP_CLAUSE_DECL (c) = decl;
15490 c = finish_omp_clauses (c, C_ORT_OMP);
15491 if (c)
15492 {
15493 OMP_CLAUSE_CHAIN (c) = *clauses;
15494 *clauses = c;
15495 }
15496 }
15497 }
15498 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15499 if (COMPARISON_CLASS_P (cond))
15500 {
15501 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15502 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15503 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15504 }
15505 else
15506 cond = RECUR (cond);
15507 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15508 switch (TREE_CODE (incr))
15509 {
15510 case PREINCREMENT_EXPR:
15511 case PREDECREMENT_EXPR:
15512 case POSTINCREMENT_EXPR:
15513 case POSTDECREMENT_EXPR:
15514 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15515 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15516 break;
15517 case MODIFY_EXPR:
15518 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15519 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15520 {
15521 tree rhs = TREE_OPERAND (incr, 1);
15522 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15523 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15524 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15525 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15526 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15527 rhs0, rhs1));
15528 }
15529 else
15530 incr = RECUR (incr);
15531 break;
15532 case MODOP_EXPR:
15533 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15534 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15535 {
15536 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15537 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15538 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15539 TREE_TYPE (decl), lhs,
15540 RECUR (TREE_OPERAND (incr, 2))));
15541 }
15542 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15543 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15544 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15545 {
15546 tree rhs = TREE_OPERAND (incr, 2);
15547 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15548 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15549 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15550 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15551 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15552 rhs0, rhs1));
15553 }
15554 else
15555 incr = RECUR (incr);
15556 break;
15557 default:
15558 incr = RECUR (incr);
15559 break;
15560 }
15561
15562 TREE_VEC_ELT (declv, i) = decl;
15563 TREE_VEC_ELT (initv, i) = init;
15564 TREE_VEC_ELT (condv, i) = cond;
15565 TREE_VEC_ELT (incrv, i) = incr;
15566 #undef RECUR
15567 }
15568
15569 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15570 of OMP_TARGET's body. */
15571
15572 static tree
15573 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15574 {
15575 *walk_subtrees = 0;
15576 switch (TREE_CODE (*tp))
15577 {
15578 case OMP_TEAMS:
15579 return *tp;
15580 case BIND_EXPR:
15581 case STATEMENT_LIST:
15582 *walk_subtrees = 1;
15583 break;
15584 default:
15585 break;
15586 }
15587 return NULL_TREE;
15588 }
15589
15590 /* Helper function for tsubst_expr. For decomposition declaration
15591 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15592 also the corresponding decls representing the identifiers
15593 of the decomposition declaration. Return DECL if successful
15594 or error_mark_node otherwise, set *FIRST to the first decl
15595 in the list chained through DECL_CHAIN and *CNT to the number
15596 of such decls. */
15597
15598 static tree
15599 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15600 tsubst_flags_t complain, tree in_decl, tree *first,
15601 unsigned int *cnt)
15602 {
15603 tree decl2, decl3, prev = decl;
15604 *cnt = 0;
15605 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15606 for (decl2 = DECL_CHAIN (pattern_decl);
15607 decl2
15608 && VAR_P (decl2)
15609 && DECL_DECOMPOSITION_P (decl2)
15610 && DECL_NAME (decl2);
15611 decl2 = DECL_CHAIN (decl2))
15612 {
15613 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15614 {
15615 gcc_assert (errorcount);
15616 return error_mark_node;
15617 }
15618 (*cnt)++;
15619 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15620 tree v = DECL_VALUE_EXPR (decl2);
15621 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15622 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15623 decl3 = tsubst (decl2, args, complain, in_decl);
15624 SET_DECL_VALUE_EXPR (decl2, v);
15625 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15626 if (VAR_P (decl3))
15627 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15628 maybe_push_decl (decl3);
15629 if (error_operand_p (decl3))
15630 decl = error_mark_node;
15631 else if (decl != error_mark_node
15632 && DECL_CHAIN (decl3) != prev)
15633 {
15634 gcc_assert (errorcount);
15635 decl = error_mark_node;
15636 }
15637 else
15638 prev = decl3;
15639 }
15640 *first = prev;
15641 return decl;
15642 }
15643
15644 /* Like tsubst_copy for expressions, etc. but also does semantic
15645 processing. */
15646
15647 tree
15648 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15649 bool integral_constant_expression_p)
15650 {
15651 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15652 #define RECUR(NODE) \
15653 tsubst_expr ((NODE), args, complain, in_decl, \
15654 integral_constant_expression_p)
15655
15656 tree stmt, tmp;
15657 tree r;
15658 location_t loc;
15659
15660 if (t == NULL_TREE || t == error_mark_node)
15661 return t;
15662
15663 loc = input_location;
15664 if (EXPR_HAS_LOCATION (t))
15665 input_location = EXPR_LOCATION (t);
15666 if (STATEMENT_CODE_P (TREE_CODE (t)))
15667 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15668
15669 switch (TREE_CODE (t))
15670 {
15671 case STATEMENT_LIST:
15672 {
15673 tree_stmt_iterator i;
15674 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15675 RECUR (tsi_stmt (i));
15676 break;
15677 }
15678
15679 case CTOR_INITIALIZER:
15680 finish_mem_initializers (tsubst_initializer_list
15681 (TREE_OPERAND (t, 0), args));
15682 break;
15683
15684 case RETURN_EXPR:
15685 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15686 break;
15687
15688 case EXPR_STMT:
15689 tmp = RECUR (EXPR_STMT_EXPR (t));
15690 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15691 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15692 else
15693 finish_expr_stmt (tmp);
15694 break;
15695
15696 case USING_STMT:
15697 do_using_directive (USING_STMT_NAMESPACE (t));
15698 break;
15699
15700 case DECL_EXPR:
15701 {
15702 tree decl, pattern_decl;
15703 tree init;
15704
15705 pattern_decl = decl = DECL_EXPR_DECL (t);
15706 if (TREE_CODE (decl) == LABEL_DECL)
15707 finish_label_decl (DECL_NAME (decl));
15708 else if (TREE_CODE (decl) == USING_DECL)
15709 {
15710 tree scope = USING_DECL_SCOPE (decl);
15711 tree name = DECL_NAME (decl);
15712
15713 scope = tsubst (scope, args, complain, in_decl);
15714 decl = lookup_qualified_name (scope, name,
15715 /*is_type_p=*/false,
15716 /*complain=*/false);
15717 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15718 qualified_name_lookup_error (scope, name, decl, input_location);
15719 else
15720 do_local_using_decl (decl, scope, name);
15721 }
15722 else if (DECL_PACK_P (decl))
15723 {
15724 /* Don't build up decls for a variadic capture proxy, we'll
15725 instantiate the elements directly as needed. */
15726 break;
15727 }
15728 else
15729 {
15730 init = DECL_INITIAL (decl);
15731 decl = tsubst (decl, args, complain, in_decl);
15732 if (decl != error_mark_node)
15733 {
15734 /* By marking the declaration as instantiated, we avoid
15735 trying to instantiate it. Since instantiate_decl can't
15736 handle local variables, and since we've already done
15737 all that needs to be done, that's the right thing to
15738 do. */
15739 if (VAR_P (decl))
15740 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15741 if (VAR_P (decl)
15742 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15743 /* Anonymous aggregates are a special case. */
15744 finish_anon_union (decl);
15745 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15746 {
15747 DECL_CONTEXT (decl) = current_function_decl;
15748 if (DECL_NAME (decl) == this_identifier)
15749 {
15750 tree lam = DECL_CONTEXT (current_function_decl);
15751 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15752 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15753 }
15754 insert_capture_proxy (decl);
15755 }
15756 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15757 /* We already did a pushtag. */;
15758 else if (TREE_CODE (decl) == FUNCTION_DECL
15759 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15760 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15761 {
15762 DECL_CONTEXT (decl) = NULL_TREE;
15763 pushdecl (decl);
15764 DECL_CONTEXT (decl) = current_function_decl;
15765 cp_check_omp_declare_reduction (decl);
15766 }
15767 else
15768 {
15769 int const_init = false;
15770 maybe_push_decl (decl);
15771 if (VAR_P (decl)
15772 && DECL_PRETTY_FUNCTION_P (decl))
15773 {
15774 /* For __PRETTY_FUNCTION__ we have to adjust the
15775 initializer. */
15776 const char *const name
15777 = cxx_printable_name (current_function_decl, 2);
15778 init = cp_fname_init (name, &TREE_TYPE (decl));
15779 }
15780 else
15781 init = tsubst_init (init, decl, args, complain, in_decl);
15782
15783 if (VAR_P (decl))
15784 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15785 (pattern_decl));
15786 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15787 if (VAR_P (decl)
15788 && DECL_DECOMPOSITION_P (decl)
15789 && TREE_TYPE (pattern_decl) != error_mark_node)
15790 {
15791 unsigned int cnt;
15792 tree first;
15793 decl = tsubst_decomp_names (decl, pattern_decl, args,
15794 complain, in_decl, &first,
15795 &cnt);
15796 if (decl != error_mark_node)
15797 cp_finish_decomp (decl, first, cnt);
15798 }
15799 }
15800 }
15801 }
15802
15803 break;
15804 }
15805
15806 case FOR_STMT:
15807 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15808 RECUR (FOR_INIT_STMT (t));
15809 finish_init_stmt (stmt);
15810 tmp = RECUR (FOR_COND (t));
15811 finish_for_cond (tmp, stmt, false);
15812 tmp = RECUR (FOR_EXPR (t));
15813 finish_for_expr (tmp, stmt);
15814 RECUR (FOR_BODY (t));
15815 finish_for_stmt (stmt);
15816 break;
15817
15818 case RANGE_FOR_STMT:
15819 {
15820 tree decl, expr;
15821 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15822 decl = RANGE_FOR_DECL (t);
15823 decl = tsubst (decl, args, complain, in_decl);
15824 maybe_push_decl (decl);
15825 expr = RECUR (RANGE_FOR_EXPR (t));
15826 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15827 {
15828 unsigned int cnt;
15829 tree first;
15830 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15831 complain, in_decl, &first, &cnt);
15832 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15833 RANGE_FOR_IVDEP (t));
15834 }
15835 else
15836 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15837 RANGE_FOR_IVDEP (t));
15838 RECUR (RANGE_FOR_BODY (t));
15839 finish_for_stmt (stmt);
15840 }
15841 break;
15842
15843 case WHILE_STMT:
15844 stmt = begin_while_stmt ();
15845 tmp = RECUR (WHILE_COND (t));
15846 finish_while_stmt_cond (tmp, stmt, false);
15847 RECUR (WHILE_BODY (t));
15848 finish_while_stmt (stmt);
15849 break;
15850
15851 case DO_STMT:
15852 stmt = begin_do_stmt ();
15853 RECUR (DO_BODY (t));
15854 finish_do_body (stmt);
15855 tmp = RECUR (DO_COND (t));
15856 finish_do_stmt (tmp, stmt, false);
15857 break;
15858
15859 case IF_STMT:
15860 stmt = begin_if_stmt ();
15861 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15862 tmp = RECUR (IF_COND (t));
15863 tmp = finish_if_stmt_cond (tmp, stmt);
15864 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15865 /* Don't instantiate the THEN_CLAUSE. */;
15866 else
15867 {
15868 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15869 if (inhibit)
15870 ++c_inhibit_evaluation_warnings;
15871 RECUR (THEN_CLAUSE (t));
15872 if (inhibit)
15873 --c_inhibit_evaluation_warnings;
15874 }
15875 finish_then_clause (stmt);
15876
15877 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15878 /* Don't instantiate the ELSE_CLAUSE. */;
15879 else if (ELSE_CLAUSE (t))
15880 {
15881 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15882 begin_else_clause (stmt);
15883 if (inhibit)
15884 ++c_inhibit_evaluation_warnings;
15885 RECUR (ELSE_CLAUSE (t));
15886 if (inhibit)
15887 --c_inhibit_evaluation_warnings;
15888 finish_else_clause (stmt);
15889 }
15890
15891 finish_if_stmt (stmt);
15892 break;
15893
15894 case BIND_EXPR:
15895 if (BIND_EXPR_BODY_BLOCK (t))
15896 stmt = begin_function_body ();
15897 else
15898 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15899 ? BCS_TRY_BLOCK : 0);
15900
15901 RECUR (BIND_EXPR_BODY (t));
15902
15903 if (BIND_EXPR_BODY_BLOCK (t))
15904 finish_function_body (stmt);
15905 else
15906 finish_compound_stmt (stmt);
15907 break;
15908
15909 case BREAK_STMT:
15910 finish_break_stmt ();
15911 break;
15912
15913 case CONTINUE_STMT:
15914 finish_continue_stmt ();
15915 break;
15916
15917 case SWITCH_STMT:
15918 stmt = begin_switch_stmt ();
15919 tmp = RECUR (SWITCH_STMT_COND (t));
15920 finish_switch_cond (tmp, stmt);
15921 RECUR (SWITCH_STMT_BODY (t));
15922 finish_switch_stmt (stmt);
15923 break;
15924
15925 case CASE_LABEL_EXPR:
15926 {
15927 tree low = RECUR (CASE_LOW (t));
15928 tree high = RECUR (CASE_HIGH (t));
15929 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
15930 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
15931 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
15932 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
15933 }
15934 break;
15935
15936 case LABEL_EXPR:
15937 {
15938 tree decl = LABEL_EXPR_LABEL (t);
15939 tree label;
15940
15941 label = finish_label_stmt (DECL_NAME (decl));
15942 if (TREE_CODE (label) == LABEL_DECL)
15943 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
15944 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15945 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15946 }
15947 break;
15948
15949 case GOTO_EXPR:
15950 tmp = GOTO_DESTINATION (t);
15951 if (TREE_CODE (tmp) != LABEL_DECL)
15952 /* Computed goto's must be tsubst'd into. On the other hand,
15953 non-computed gotos must not be; the identifier in question
15954 will have no binding. */
15955 tmp = RECUR (tmp);
15956 else
15957 tmp = DECL_NAME (tmp);
15958 finish_goto_stmt (tmp);
15959 break;
15960
15961 case ASM_EXPR:
15962 {
15963 tree string = RECUR (ASM_STRING (t));
15964 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15965 complain, in_decl);
15966 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15967 complain, in_decl);
15968 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15969 complain, in_decl);
15970 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15971 complain, in_decl);
15972 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15973 clobbers, labels);
15974 tree asm_expr = tmp;
15975 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15976 asm_expr = TREE_OPERAND (asm_expr, 0);
15977 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15978 }
15979 break;
15980
15981 case TRY_BLOCK:
15982 if (CLEANUP_P (t))
15983 {
15984 stmt = begin_try_block ();
15985 RECUR (TRY_STMTS (t));
15986 finish_cleanup_try_block (stmt);
15987 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15988 }
15989 else
15990 {
15991 tree compound_stmt = NULL_TREE;
15992
15993 if (FN_TRY_BLOCK_P (t))
15994 stmt = begin_function_try_block (&compound_stmt);
15995 else
15996 stmt = begin_try_block ();
15997
15998 RECUR (TRY_STMTS (t));
15999
16000 if (FN_TRY_BLOCK_P (t))
16001 finish_function_try_block (stmt);
16002 else
16003 finish_try_block (stmt);
16004
16005 RECUR (TRY_HANDLERS (t));
16006 if (FN_TRY_BLOCK_P (t))
16007 finish_function_handler_sequence (stmt, compound_stmt);
16008 else
16009 finish_handler_sequence (stmt);
16010 }
16011 break;
16012
16013 case HANDLER:
16014 {
16015 tree decl = HANDLER_PARMS (t);
16016
16017 if (decl)
16018 {
16019 decl = tsubst (decl, args, complain, in_decl);
16020 /* Prevent instantiate_decl from trying to instantiate
16021 this variable. We've already done all that needs to be
16022 done. */
16023 if (decl != error_mark_node)
16024 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16025 }
16026 stmt = begin_handler ();
16027 finish_handler_parms (decl, stmt);
16028 RECUR (HANDLER_BODY (t));
16029 finish_handler (stmt);
16030 }
16031 break;
16032
16033 case TAG_DEFN:
16034 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16035 if (CLASS_TYPE_P (tmp))
16036 {
16037 /* Local classes are not independent templates; they are
16038 instantiated along with their containing function. And this
16039 way we don't have to deal with pushing out of one local class
16040 to instantiate a member of another local class. */
16041 tree fn;
16042 /* Closures are handled by the LAMBDA_EXPR. */
16043 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16044 complete_type (tmp);
16045 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16046 if (!DECL_ARTIFICIAL (fn))
16047 instantiate_decl (fn, /*defer_ok=*/false,
16048 /*expl_inst_class=*/false);
16049 }
16050 break;
16051
16052 case STATIC_ASSERT:
16053 {
16054 tree condition;
16055
16056 ++c_inhibit_evaluation_warnings;
16057 condition =
16058 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16059 args,
16060 complain, in_decl,
16061 /*integral_constant_expression_p=*/true);
16062 --c_inhibit_evaluation_warnings;
16063
16064 finish_static_assert (condition,
16065 STATIC_ASSERT_MESSAGE (t),
16066 STATIC_ASSERT_SOURCE_LOCATION (t),
16067 /*member_p=*/false);
16068 }
16069 break;
16070
16071 case OACC_KERNELS:
16072 case OACC_PARALLEL:
16073 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16074 in_decl);
16075 stmt = begin_omp_parallel ();
16076 RECUR (OMP_BODY (t));
16077 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16078 break;
16079
16080 case OMP_PARALLEL:
16081 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16082 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16083 complain, in_decl);
16084 if (OMP_PARALLEL_COMBINED (t))
16085 omp_parallel_combined_clauses = &tmp;
16086 stmt = begin_omp_parallel ();
16087 RECUR (OMP_PARALLEL_BODY (t));
16088 gcc_assert (omp_parallel_combined_clauses == NULL);
16089 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16090 = OMP_PARALLEL_COMBINED (t);
16091 pop_omp_privatization_clauses (r);
16092 break;
16093
16094 case OMP_TASK:
16095 r = push_omp_privatization_clauses (false);
16096 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16097 complain, in_decl);
16098 stmt = begin_omp_task ();
16099 RECUR (OMP_TASK_BODY (t));
16100 finish_omp_task (tmp, stmt);
16101 pop_omp_privatization_clauses (r);
16102 break;
16103
16104 case OMP_FOR:
16105 case OMP_SIMD:
16106 case CILK_SIMD:
16107 case CILK_FOR:
16108 case OMP_DISTRIBUTE:
16109 case OMP_TASKLOOP:
16110 case OACC_LOOP:
16111 {
16112 tree clauses, body, pre_body;
16113 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16114 tree orig_declv = NULL_TREE;
16115 tree incrv = NULL_TREE;
16116 enum c_omp_region_type ort = C_ORT_OMP;
16117 int i;
16118
16119 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16120 ort = C_ORT_CILK;
16121 else if (TREE_CODE (t) == OACC_LOOP)
16122 ort = C_ORT_ACC;
16123
16124 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16125 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16126 in_decl);
16127 if (OMP_FOR_INIT (t) != NULL_TREE)
16128 {
16129 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16130 if (OMP_FOR_ORIG_DECLS (t))
16131 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16132 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16133 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16134 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16135 }
16136
16137 stmt = begin_omp_structured_block ();
16138
16139 pre_body = push_stmt_list ();
16140 RECUR (OMP_FOR_PRE_BODY (t));
16141 pre_body = pop_stmt_list (pre_body);
16142
16143 if (OMP_FOR_INIT (t) != NULL_TREE)
16144 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16145 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16146 incrv, &clauses, args, complain, in_decl,
16147 integral_constant_expression_p);
16148 omp_parallel_combined_clauses = NULL;
16149
16150 body = push_stmt_list ();
16151 RECUR (OMP_FOR_BODY (t));
16152 body = pop_stmt_list (body);
16153
16154 if (OMP_FOR_INIT (t) != NULL_TREE)
16155 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16156 orig_declv, initv, condv, incrv, body, pre_body,
16157 NULL, clauses);
16158 else
16159 {
16160 t = make_node (TREE_CODE (t));
16161 TREE_TYPE (t) = void_type_node;
16162 OMP_FOR_BODY (t) = body;
16163 OMP_FOR_PRE_BODY (t) = pre_body;
16164 OMP_FOR_CLAUSES (t) = clauses;
16165 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16166 add_stmt (t);
16167 }
16168
16169 add_stmt (finish_omp_structured_block (stmt));
16170 pop_omp_privatization_clauses (r);
16171 }
16172 break;
16173
16174 case OMP_SECTIONS:
16175 omp_parallel_combined_clauses = NULL;
16176 /* FALLTHRU */
16177 case OMP_SINGLE:
16178 case OMP_TEAMS:
16179 case OMP_CRITICAL:
16180 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16181 && OMP_TEAMS_COMBINED (t));
16182 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16183 in_decl);
16184 stmt = push_stmt_list ();
16185 RECUR (OMP_BODY (t));
16186 stmt = pop_stmt_list (stmt);
16187
16188 t = copy_node (t);
16189 OMP_BODY (t) = stmt;
16190 OMP_CLAUSES (t) = tmp;
16191 add_stmt (t);
16192 pop_omp_privatization_clauses (r);
16193 break;
16194
16195 case OACC_DATA:
16196 case OMP_TARGET_DATA:
16197 case OMP_TARGET:
16198 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16199 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16200 in_decl);
16201 keep_next_level (true);
16202 stmt = begin_omp_structured_block ();
16203
16204 RECUR (OMP_BODY (t));
16205 stmt = finish_omp_structured_block (stmt);
16206
16207 t = copy_node (t);
16208 OMP_BODY (t) = stmt;
16209 OMP_CLAUSES (t) = tmp;
16210 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16211 {
16212 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16213 if (teams)
16214 {
16215 /* For combined target teams, ensure the num_teams and
16216 thread_limit clause expressions are evaluated on the host,
16217 before entering the target construct. */
16218 tree c;
16219 for (c = OMP_TEAMS_CLAUSES (teams);
16220 c; c = OMP_CLAUSE_CHAIN (c))
16221 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16222 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16223 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16224 {
16225 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16226 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16227 if (expr == error_mark_node)
16228 continue;
16229 tmp = TARGET_EXPR_SLOT (expr);
16230 add_stmt (expr);
16231 OMP_CLAUSE_OPERAND (c, 0) = expr;
16232 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16233 OMP_CLAUSE_FIRSTPRIVATE);
16234 OMP_CLAUSE_DECL (tc) = tmp;
16235 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16236 OMP_TARGET_CLAUSES (t) = tc;
16237 }
16238 }
16239 }
16240 add_stmt (t);
16241 break;
16242
16243 case OACC_DECLARE:
16244 t = copy_node (t);
16245 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16246 complain, in_decl);
16247 OACC_DECLARE_CLAUSES (t) = tmp;
16248 add_stmt (t);
16249 break;
16250
16251 case OMP_TARGET_UPDATE:
16252 case OMP_TARGET_ENTER_DATA:
16253 case OMP_TARGET_EXIT_DATA:
16254 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16255 complain, in_decl);
16256 t = copy_node (t);
16257 OMP_STANDALONE_CLAUSES (t) = tmp;
16258 add_stmt (t);
16259 break;
16260
16261 case OACC_ENTER_DATA:
16262 case OACC_EXIT_DATA:
16263 case OACC_UPDATE:
16264 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16265 complain, in_decl);
16266 t = copy_node (t);
16267 OMP_STANDALONE_CLAUSES (t) = tmp;
16268 add_stmt (t);
16269 break;
16270
16271 case OMP_ORDERED:
16272 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16273 complain, in_decl);
16274 stmt = push_stmt_list ();
16275 RECUR (OMP_BODY (t));
16276 stmt = pop_stmt_list (stmt);
16277
16278 t = copy_node (t);
16279 OMP_BODY (t) = stmt;
16280 OMP_ORDERED_CLAUSES (t) = tmp;
16281 add_stmt (t);
16282 break;
16283
16284 case OMP_SECTION:
16285 case OMP_MASTER:
16286 case OMP_TASKGROUP:
16287 stmt = push_stmt_list ();
16288 RECUR (OMP_BODY (t));
16289 stmt = pop_stmt_list (stmt);
16290
16291 t = copy_node (t);
16292 OMP_BODY (t) = stmt;
16293 add_stmt (t);
16294 break;
16295
16296 case OMP_ATOMIC:
16297 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16298 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16299 {
16300 tree op1 = TREE_OPERAND (t, 1);
16301 tree rhs1 = NULL_TREE;
16302 tree lhs, rhs;
16303 if (TREE_CODE (op1) == COMPOUND_EXPR)
16304 {
16305 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16306 op1 = TREE_OPERAND (op1, 1);
16307 }
16308 lhs = RECUR (TREE_OPERAND (op1, 0));
16309 rhs = RECUR (TREE_OPERAND (op1, 1));
16310 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16311 NULL_TREE, NULL_TREE, rhs1,
16312 OMP_ATOMIC_SEQ_CST (t));
16313 }
16314 else
16315 {
16316 tree op1 = TREE_OPERAND (t, 1);
16317 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16318 tree rhs1 = NULL_TREE;
16319 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16320 enum tree_code opcode = NOP_EXPR;
16321 if (code == OMP_ATOMIC_READ)
16322 {
16323 v = RECUR (TREE_OPERAND (op1, 0));
16324 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16325 }
16326 else if (code == OMP_ATOMIC_CAPTURE_OLD
16327 || code == OMP_ATOMIC_CAPTURE_NEW)
16328 {
16329 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16330 v = RECUR (TREE_OPERAND (op1, 0));
16331 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16332 if (TREE_CODE (op11) == COMPOUND_EXPR)
16333 {
16334 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16335 op11 = TREE_OPERAND (op11, 1);
16336 }
16337 lhs = RECUR (TREE_OPERAND (op11, 0));
16338 rhs = RECUR (TREE_OPERAND (op11, 1));
16339 opcode = TREE_CODE (op11);
16340 if (opcode == MODIFY_EXPR)
16341 opcode = NOP_EXPR;
16342 }
16343 else
16344 {
16345 code = OMP_ATOMIC;
16346 lhs = RECUR (TREE_OPERAND (op1, 0));
16347 rhs = RECUR (TREE_OPERAND (op1, 1));
16348 }
16349 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16350 OMP_ATOMIC_SEQ_CST (t));
16351 }
16352 break;
16353
16354 case TRANSACTION_EXPR:
16355 {
16356 int flags = 0;
16357 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16358 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16359
16360 if (TRANSACTION_EXPR_IS_STMT (t))
16361 {
16362 tree body = TRANSACTION_EXPR_BODY (t);
16363 tree noex = NULL_TREE;
16364 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16365 {
16366 noex = MUST_NOT_THROW_COND (body);
16367 if (noex == NULL_TREE)
16368 noex = boolean_true_node;
16369 body = TREE_OPERAND (body, 0);
16370 }
16371 stmt = begin_transaction_stmt (input_location, NULL, flags);
16372 RECUR (body);
16373 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16374 }
16375 else
16376 {
16377 stmt = build_transaction_expr (EXPR_LOCATION (t),
16378 RECUR (TRANSACTION_EXPR_BODY (t)),
16379 flags, NULL_TREE);
16380 RETURN (stmt);
16381 }
16382 }
16383 break;
16384
16385 case MUST_NOT_THROW_EXPR:
16386 {
16387 tree op0 = RECUR (TREE_OPERAND (t, 0));
16388 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16389 RETURN (build_must_not_throw_expr (op0, cond));
16390 }
16391
16392 case EXPR_PACK_EXPANSION:
16393 error ("invalid use of pack expansion expression");
16394 RETURN (error_mark_node);
16395
16396 case NONTYPE_ARGUMENT_PACK:
16397 error ("use %<...%> to expand argument pack");
16398 RETURN (error_mark_node);
16399
16400 case CILK_SPAWN_STMT:
16401 cfun->calls_cilk_spawn = 1;
16402 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16403
16404 case CILK_SYNC_STMT:
16405 RETURN (build_cilk_sync ());
16406
16407 case COMPOUND_EXPR:
16408 tmp = RECUR (TREE_OPERAND (t, 0));
16409 if (tmp == NULL_TREE)
16410 /* If the first operand was a statement, we're done with it. */
16411 RETURN (RECUR (TREE_OPERAND (t, 1)));
16412 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16413 RECUR (TREE_OPERAND (t, 1)),
16414 complain));
16415
16416 case ANNOTATE_EXPR:
16417 tmp = RECUR (TREE_OPERAND (t, 0));
16418 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16419 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16420
16421 default:
16422 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16423
16424 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16425 /*function_p=*/false,
16426 integral_constant_expression_p));
16427 }
16428
16429 RETURN (NULL_TREE);
16430 out:
16431 input_location = loc;
16432 return r;
16433 #undef RECUR
16434 #undef RETURN
16435 }
16436
16437 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16438 function. For description of the body see comment above
16439 cp_parser_omp_declare_reduction_exprs. */
16440
16441 static void
16442 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16443 {
16444 if (t == NULL_TREE || t == error_mark_node)
16445 return;
16446
16447 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16448
16449 tree_stmt_iterator tsi;
16450 int i;
16451 tree stmts[7];
16452 memset (stmts, 0, sizeof stmts);
16453 for (i = 0, tsi = tsi_start (t);
16454 i < 7 && !tsi_end_p (tsi);
16455 i++, tsi_next (&tsi))
16456 stmts[i] = tsi_stmt (tsi);
16457 gcc_assert (tsi_end_p (tsi));
16458
16459 if (i >= 3)
16460 {
16461 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16462 && TREE_CODE (stmts[1]) == DECL_EXPR);
16463 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16464 args, complain, in_decl);
16465 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16466 args, complain, in_decl);
16467 DECL_CONTEXT (omp_out) = current_function_decl;
16468 DECL_CONTEXT (omp_in) = current_function_decl;
16469 keep_next_level (true);
16470 tree block = begin_omp_structured_block ();
16471 tsubst_expr (stmts[2], args, complain, in_decl, false);
16472 block = finish_omp_structured_block (block);
16473 block = maybe_cleanup_point_expr_void (block);
16474 add_decl_expr (omp_out);
16475 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16476 TREE_NO_WARNING (omp_out) = 1;
16477 add_decl_expr (omp_in);
16478 finish_expr_stmt (block);
16479 }
16480 if (i >= 6)
16481 {
16482 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16483 && TREE_CODE (stmts[4]) == DECL_EXPR);
16484 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16485 args, complain, in_decl);
16486 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16487 args, complain, in_decl);
16488 DECL_CONTEXT (omp_priv) = current_function_decl;
16489 DECL_CONTEXT (omp_orig) = current_function_decl;
16490 keep_next_level (true);
16491 tree block = begin_omp_structured_block ();
16492 tsubst_expr (stmts[5], args, complain, in_decl, false);
16493 block = finish_omp_structured_block (block);
16494 block = maybe_cleanup_point_expr_void (block);
16495 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16496 add_decl_expr (omp_priv);
16497 add_decl_expr (omp_orig);
16498 finish_expr_stmt (block);
16499 if (i == 7)
16500 add_decl_expr (omp_orig);
16501 }
16502 }
16503
16504 /* T is a postfix-expression that is not being used in a function
16505 call. Return the substituted version of T. */
16506
16507 static tree
16508 tsubst_non_call_postfix_expression (tree t, tree args,
16509 tsubst_flags_t complain,
16510 tree in_decl)
16511 {
16512 if (TREE_CODE (t) == SCOPE_REF)
16513 t = tsubst_qualified_id (t, args, complain, in_decl,
16514 /*done=*/false, /*address_p=*/false);
16515 else
16516 t = tsubst_copy_and_build (t, args, complain, in_decl,
16517 /*function_p=*/false,
16518 /*integral_constant_expression_p=*/false);
16519
16520 return t;
16521 }
16522
16523 /* Like tsubst but deals with expressions and performs semantic
16524 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16525
16526 tree
16527 tsubst_copy_and_build (tree t,
16528 tree args,
16529 tsubst_flags_t complain,
16530 tree in_decl,
16531 bool function_p,
16532 bool integral_constant_expression_p)
16533 {
16534 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16535 #define RECUR(NODE) \
16536 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16537 /*function_p=*/false, \
16538 integral_constant_expression_p)
16539
16540 tree retval, op1;
16541 location_t loc;
16542
16543 if (t == NULL_TREE || t == error_mark_node)
16544 return t;
16545
16546 loc = input_location;
16547 if (EXPR_HAS_LOCATION (t))
16548 input_location = EXPR_LOCATION (t);
16549
16550 /* N3276 decltype magic only applies to calls at the top level or on the
16551 right side of a comma. */
16552 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16553 complain &= ~tf_decltype;
16554
16555 switch (TREE_CODE (t))
16556 {
16557 case USING_DECL:
16558 t = DECL_NAME (t);
16559 /* Fall through. */
16560 case IDENTIFIER_NODE:
16561 {
16562 tree decl;
16563 cp_id_kind idk;
16564 bool non_integral_constant_expression_p;
16565 const char *error_msg;
16566
16567 if (IDENTIFIER_TYPENAME_P (t))
16568 {
16569 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16570 t = mangle_conv_op_name_for_type (new_type);
16571 }
16572
16573 /* Look up the name. */
16574 decl = lookup_name (t);
16575
16576 /* By convention, expressions use ERROR_MARK_NODE to indicate
16577 failure, not NULL_TREE. */
16578 if (decl == NULL_TREE)
16579 decl = error_mark_node;
16580
16581 decl = finish_id_expression (t, decl, NULL_TREE,
16582 &idk,
16583 integral_constant_expression_p,
16584 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16585 &non_integral_constant_expression_p,
16586 /*template_p=*/false,
16587 /*done=*/true,
16588 /*address_p=*/false,
16589 /*template_arg_p=*/false,
16590 &error_msg,
16591 input_location);
16592 if (error_msg)
16593 error (error_msg);
16594 if (!function_p && identifier_p (decl))
16595 {
16596 if (complain & tf_error)
16597 unqualified_name_lookup_error (decl);
16598 decl = error_mark_node;
16599 }
16600 RETURN (decl);
16601 }
16602
16603 case TEMPLATE_ID_EXPR:
16604 {
16605 tree object;
16606 tree templ = RECUR (TREE_OPERAND (t, 0));
16607 tree targs = TREE_OPERAND (t, 1);
16608
16609 if (targs)
16610 targs = tsubst_template_args (targs, args, complain, in_decl);
16611 if (targs == error_mark_node)
16612 return error_mark_node;
16613
16614 if (variable_template_p (templ))
16615 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16616
16617 if (TREE_CODE (templ) == COMPONENT_REF)
16618 {
16619 object = TREE_OPERAND (templ, 0);
16620 templ = TREE_OPERAND (templ, 1);
16621 }
16622 else
16623 object = NULL_TREE;
16624 templ = lookup_template_function (templ, targs);
16625
16626 if (object)
16627 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16628 object, templ, NULL_TREE));
16629 else
16630 RETURN (baselink_for_fns (templ));
16631 }
16632
16633 case INDIRECT_REF:
16634 {
16635 tree r = RECUR (TREE_OPERAND (t, 0));
16636
16637 if (REFERENCE_REF_P (t))
16638 {
16639 /* A type conversion to reference type will be enclosed in
16640 such an indirect ref, but the substitution of the cast
16641 will have also added such an indirect ref. */
16642 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16643 r = convert_from_reference (r);
16644 }
16645 else
16646 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16647 complain|decltype_flag);
16648
16649 if (TREE_CODE (r) == INDIRECT_REF)
16650 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16651
16652 RETURN (r);
16653 }
16654
16655 case NOP_EXPR:
16656 {
16657 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16658 tree op0 = RECUR (TREE_OPERAND (t, 0));
16659 RETURN (build_nop (type, op0));
16660 }
16661
16662 case IMPLICIT_CONV_EXPR:
16663 {
16664 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16665 tree expr = RECUR (TREE_OPERAND (t, 0));
16666 int flags = LOOKUP_IMPLICIT;
16667 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16668 flags = LOOKUP_NORMAL;
16669 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16670 flags));
16671 }
16672
16673 case CONVERT_EXPR:
16674 {
16675 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16676 tree op0 = RECUR (TREE_OPERAND (t, 0));
16677 RETURN (build1 (CONVERT_EXPR, type, op0));
16678 }
16679
16680 case CAST_EXPR:
16681 case REINTERPRET_CAST_EXPR:
16682 case CONST_CAST_EXPR:
16683 case DYNAMIC_CAST_EXPR:
16684 case STATIC_CAST_EXPR:
16685 {
16686 tree type;
16687 tree op, r = NULL_TREE;
16688
16689 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16690 if (integral_constant_expression_p
16691 && !cast_valid_in_integral_constant_expression_p (type))
16692 {
16693 if (complain & tf_error)
16694 error ("a cast to a type other than an integral or "
16695 "enumeration type cannot appear in a constant-expression");
16696 RETURN (error_mark_node);
16697 }
16698
16699 op = RECUR (TREE_OPERAND (t, 0));
16700
16701 warning_sentinel s(warn_useless_cast);
16702 switch (TREE_CODE (t))
16703 {
16704 case CAST_EXPR:
16705 r = build_functional_cast (type, op, complain);
16706 break;
16707 case REINTERPRET_CAST_EXPR:
16708 r = build_reinterpret_cast (type, op, complain);
16709 break;
16710 case CONST_CAST_EXPR:
16711 r = build_const_cast (type, op, complain);
16712 break;
16713 case DYNAMIC_CAST_EXPR:
16714 r = build_dynamic_cast (type, op, complain);
16715 break;
16716 case STATIC_CAST_EXPR:
16717 r = build_static_cast (type, op, complain);
16718 break;
16719 default:
16720 gcc_unreachable ();
16721 }
16722
16723 RETURN (r);
16724 }
16725
16726 case POSTDECREMENT_EXPR:
16727 case POSTINCREMENT_EXPR:
16728 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16729 args, complain, in_decl);
16730 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16731 complain|decltype_flag));
16732
16733 case PREDECREMENT_EXPR:
16734 case PREINCREMENT_EXPR:
16735 case NEGATE_EXPR:
16736 case BIT_NOT_EXPR:
16737 case ABS_EXPR:
16738 case TRUTH_NOT_EXPR:
16739 case UNARY_PLUS_EXPR: /* Unary + */
16740 case REALPART_EXPR:
16741 case IMAGPART_EXPR:
16742 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16743 RECUR (TREE_OPERAND (t, 0)),
16744 complain|decltype_flag));
16745
16746 case FIX_TRUNC_EXPR:
16747 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16748 false, complain));
16749
16750 case ADDR_EXPR:
16751 op1 = TREE_OPERAND (t, 0);
16752 if (TREE_CODE (op1) == LABEL_DECL)
16753 RETURN (finish_label_address_expr (DECL_NAME (op1),
16754 EXPR_LOCATION (op1)));
16755 if (TREE_CODE (op1) == SCOPE_REF)
16756 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16757 /*done=*/true, /*address_p=*/true);
16758 else
16759 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16760 in_decl);
16761 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16762 complain|decltype_flag));
16763
16764 case PLUS_EXPR:
16765 case MINUS_EXPR:
16766 case MULT_EXPR:
16767 case TRUNC_DIV_EXPR:
16768 case CEIL_DIV_EXPR:
16769 case FLOOR_DIV_EXPR:
16770 case ROUND_DIV_EXPR:
16771 case EXACT_DIV_EXPR:
16772 case BIT_AND_EXPR:
16773 case BIT_IOR_EXPR:
16774 case BIT_XOR_EXPR:
16775 case TRUNC_MOD_EXPR:
16776 case FLOOR_MOD_EXPR:
16777 case TRUTH_ANDIF_EXPR:
16778 case TRUTH_ORIF_EXPR:
16779 case TRUTH_AND_EXPR:
16780 case TRUTH_OR_EXPR:
16781 case RSHIFT_EXPR:
16782 case LSHIFT_EXPR:
16783 case RROTATE_EXPR:
16784 case LROTATE_EXPR:
16785 case EQ_EXPR:
16786 case NE_EXPR:
16787 case MAX_EXPR:
16788 case MIN_EXPR:
16789 case LE_EXPR:
16790 case GE_EXPR:
16791 case LT_EXPR:
16792 case GT_EXPR:
16793 case MEMBER_REF:
16794 case DOTSTAR_EXPR:
16795 {
16796 warning_sentinel s1(warn_type_limits);
16797 warning_sentinel s2(warn_div_by_zero);
16798 warning_sentinel s3(warn_logical_op);
16799 warning_sentinel s4(warn_tautological_compare);
16800 tree op0 = RECUR (TREE_OPERAND (t, 0));
16801 tree op1 = RECUR (TREE_OPERAND (t, 1));
16802 tree r = build_x_binary_op
16803 (input_location, TREE_CODE (t),
16804 op0,
16805 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16806 ? ERROR_MARK
16807 : TREE_CODE (TREE_OPERAND (t, 0))),
16808 op1,
16809 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16810 ? ERROR_MARK
16811 : TREE_CODE (TREE_OPERAND (t, 1))),
16812 /*overload=*/NULL,
16813 complain|decltype_flag);
16814 if (EXPR_P (r) && TREE_NO_WARNING (t))
16815 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16816
16817 RETURN (r);
16818 }
16819
16820 case POINTER_PLUS_EXPR:
16821 {
16822 tree op0 = RECUR (TREE_OPERAND (t, 0));
16823 tree op1 = RECUR (TREE_OPERAND (t, 1));
16824 return fold_build_pointer_plus (op0, op1);
16825 }
16826
16827 case SCOPE_REF:
16828 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16829 /*address_p=*/false));
16830 case ARRAY_REF:
16831 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16832 args, complain, in_decl);
16833 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16834 RECUR (TREE_OPERAND (t, 1)),
16835 complain|decltype_flag));
16836
16837 case ARRAY_NOTATION_REF:
16838 {
16839 tree start_index, length, stride;
16840 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16841 args, complain, in_decl);
16842 start_index = RECUR (ARRAY_NOTATION_START (t));
16843 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16844 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16845 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16846 length, stride, TREE_TYPE (op1)));
16847 }
16848 case SIZEOF_EXPR:
16849 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16850 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16851 RETURN (tsubst_copy (t, args, complain, in_decl));
16852 /* Fall through */
16853
16854 case ALIGNOF_EXPR:
16855 {
16856 tree r;
16857
16858 op1 = TREE_OPERAND (t, 0);
16859 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16860 op1 = TREE_TYPE (op1);
16861 if (!args)
16862 {
16863 /* When there are no ARGS, we are trying to evaluate a
16864 non-dependent expression from the parser. Trying to do
16865 the substitutions may not work. */
16866 if (!TYPE_P (op1))
16867 op1 = TREE_TYPE (op1);
16868 }
16869 else
16870 {
16871 ++cp_unevaluated_operand;
16872 ++c_inhibit_evaluation_warnings;
16873 if (TYPE_P (op1))
16874 op1 = tsubst (op1, args, complain, in_decl);
16875 else
16876 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16877 /*function_p=*/false,
16878 /*integral_constant_expression_p=*/
16879 false);
16880 --cp_unevaluated_operand;
16881 --c_inhibit_evaluation_warnings;
16882 }
16883 if (TYPE_P (op1))
16884 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16885 complain & tf_error);
16886 else
16887 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16888 complain & tf_error);
16889 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16890 {
16891 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16892 {
16893 if (!processing_template_decl && TYPE_P (op1))
16894 {
16895 r = build_min (SIZEOF_EXPR, size_type_node,
16896 build1 (NOP_EXPR, op1, error_mark_node));
16897 SIZEOF_EXPR_TYPE_P (r) = 1;
16898 }
16899 else
16900 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16901 TREE_SIDE_EFFECTS (r) = 0;
16902 TREE_READONLY (r) = 1;
16903 }
16904 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16905 }
16906 RETURN (r);
16907 }
16908
16909 case AT_ENCODE_EXPR:
16910 {
16911 op1 = TREE_OPERAND (t, 0);
16912 ++cp_unevaluated_operand;
16913 ++c_inhibit_evaluation_warnings;
16914 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16915 /*function_p=*/false,
16916 /*integral_constant_expression_p=*/false);
16917 --cp_unevaluated_operand;
16918 --c_inhibit_evaluation_warnings;
16919 RETURN (objc_build_encode_expr (op1));
16920 }
16921
16922 case NOEXCEPT_EXPR:
16923 op1 = TREE_OPERAND (t, 0);
16924 ++cp_unevaluated_operand;
16925 ++c_inhibit_evaluation_warnings;
16926 ++cp_noexcept_operand;
16927 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16928 /*function_p=*/false,
16929 /*integral_constant_expression_p=*/false);
16930 --cp_unevaluated_operand;
16931 --c_inhibit_evaluation_warnings;
16932 --cp_noexcept_operand;
16933 RETURN (finish_noexcept_expr (op1, complain));
16934
16935 case MODOP_EXPR:
16936 {
16937 warning_sentinel s(warn_div_by_zero);
16938 tree lhs = RECUR (TREE_OPERAND (t, 0));
16939 tree rhs = RECUR (TREE_OPERAND (t, 2));
16940 tree r = build_x_modify_expr
16941 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16942 complain|decltype_flag);
16943 /* TREE_NO_WARNING must be set if either the expression was
16944 parenthesized or it uses an operator such as >>= rather
16945 than plain assignment. In the former case, it was already
16946 set and must be copied. In the latter case,
16947 build_x_modify_expr sets it and it must not be reset
16948 here. */
16949 if (TREE_NO_WARNING (t))
16950 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16951
16952 RETURN (r);
16953 }
16954
16955 case ARROW_EXPR:
16956 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16957 args, complain, in_decl);
16958 /* Remember that there was a reference to this entity. */
16959 if (DECL_P (op1)
16960 && !mark_used (op1, complain) && !(complain & tf_error))
16961 RETURN (error_mark_node);
16962 RETURN (build_x_arrow (input_location, op1, complain));
16963
16964 case NEW_EXPR:
16965 {
16966 tree placement = RECUR (TREE_OPERAND (t, 0));
16967 tree init = RECUR (TREE_OPERAND (t, 3));
16968 vec<tree, va_gc> *placement_vec;
16969 vec<tree, va_gc> *init_vec;
16970 tree ret;
16971
16972 if (placement == NULL_TREE)
16973 placement_vec = NULL;
16974 else
16975 {
16976 placement_vec = make_tree_vector ();
16977 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16978 vec_safe_push (placement_vec, TREE_VALUE (placement));
16979 }
16980
16981 /* If there was an initializer in the original tree, but it
16982 instantiated to an empty list, then we should pass a
16983 non-NULL empty vector to tell build_new that it was an
16984 empty initializer() rather than no initializer. This can
16985 only happen when the initializer is a pack expansion whose
16986 parameter packs are of length zero. */
16987 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16988 init_vec = NULL;
16989 else
16990 {
16991 init_vec = make_tree_vector ();
16992 if (init == void_node)
16993 gcc_assert (init_vec != NULL);
16994 else
16995 {
16996 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16997 vec_safe_push (init_vec, TREE_VALUE (init));
16998 }
16999 }
17000
17001 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17002 tree op2 = RECUR (TREE_OPERAND (t, 2));
17003 ret = build_new (&placement_vec, op1, op2, &init_vec,
17004 NEW_EXPR_USE_GLOBAL (t),
17005 complain);
17006
17007 if (placement_vec != NULL)
17008 release_tree_vector (placement_vec);
17009 if (init_vec != NULL)
17010 release_tree_vector (init_vec);
17011
17012 RETURN (ret);
17013 }
17014
17015 case DELETE_EXPR:
17016 {
17017 tree op0 = RECUR (TREE_OPERAND (t, 0));
17018 tree op1 = RECUR (TREE_OPERAND (t, 1));
17019 RETURN (delete_sanity (op0, op1,
17020 DELETE_EXPR_USE_VEC (t),
17021 DELETE_EXPR_USE_GLOBAL (t),
17022 complain));
17023 }
17024
17025 case COMPOUND_EXPR:
17026 {
17027 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17028 complain & ~tf_decltype, in_decl,
17029 /*function_p=*/false,
17030 integral_constant_expression_p);
17031 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17032 op0,
17033 RECUR (TREE_OPERAND (t, 1)),
17034 complain|decltype_flag));
17035 }
17036
17037 case CALL_EXPR:
17038 {
17039 tree function;
17040 vec<tree, va_gc> *call_args;
17041 unsigned int nargs, i;
17042 bool qualified_p;
17043 bool koenig_p;
17044 tree ret;
17045
17046 function = CALL_EXPR_FN (t);
17047 /* Internal function with no arguments. */
17048 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17049 RETURN (t);
17050
17051 /* When we parsed the expression, we determined whether or
17052 not Koenig lookup should be performed. */
17053 koenig_p = KOENIG_LOOKUP_P (t);
17054 if (function == NULL_TREE)
17055 {
17056 koenig_p = false;
17057 qualified_p = false;
17058 }
17059 else if (TREE_CODE (function) == SCOPE_REF)
17060 {
17061 qualified_p = true;
17062 function = tsubst_qualified_id (function, args, complain, in_decl,
17063 /*done=*/false,
17064 /*address_p=*/false);
17065 }
17066 else if (koenig_p && identifier_p (function))
17067 {
17068 /* Do nothing; calling tsubst_copy_and_build on an identifier
17069 would incorrectly perform unqualified lookup again.
17070
17071 Note that we can also have an IDENTIFIER_NODE if the earlier
17072 unqualified lookup found a member function; in that case
17073 koenig_p will be false and we do want to do the lookup
17074 again to find the instantiated member function.
17075
17076 FIXME but doing that causes c++/15272, so we need to stop
17077 using IDENTIFIER_NODE in that situation. */
17078 qualified_p = false;
17079 }
17080 else
17081 {
17082 if (TREE_CODE (function) == COMPONENT_REF)
17083 {
17084 tree op = TREE_OPERAND (function, 1);
17085
17086 qualified_p = (TREE_CODE (op) == SCOPE_REF
17087 || (BASELINK_P (op)
17088 && BASELINK_QUALIFIED_P (op)));
17089 }
17090 else
17091 qualified_p = false;
17092
17093 if (TREE_CODE (function) == ADDR_EXPR
17094 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17095 /* Avoid error about taking the address of a constructor. */
17096 function = TREE_OPERAND (function, 0);
17097
17098 function = tsubst_copy_and_build (function, args, complain,
17099 in_decl,
17100 !qualified_p,
17101 integral_constant_expression_p);
17102
17103 if (BASELINK_P (function))
17104 qualified_p = true;
17105 }
17106
17107 nargs = call_expr_nargs (t);
17108 call_args = make_tree_vector ();
17109 for (i = 0; i < nargs; ++i)
17110 {
17111 tree arg = CALL_EXPR_ARG (t, i);
17112
17113 if (!PACK_EXPANSION_P (arg))
17114 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17115 else
17116 {
17117 /* Expand the pack expansion and push each entry onto
17118 CALL_ARGS. */
17119 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17120 if (TREE_CODE (arg) == TREE_VEC)
17121 {
17122 unsigned int len, j;
17123
17124 len = TREE_VEC_LENGTH (arg);
17125 for (j = 0; j < len; ++j)
17126 {
17127 tree value = TREE_VEC_ELT (arg, j);
17128 if (value != NULL_TREE)
17129 value = convert_from_reference (value);
17130 vec_safe_push (call_args, value);
17131 }
17132 }
17133 else
17134 {
17135 /* A partial substitution. Add one entry. */
17136 vec_safe_push (call_args, arg);
17137 }
17138 }
17139 }
17140
17141 /* We do not perform argument-dependent lookup if normal
17142 lookup finds a non-function, in accordance with the
17143 expected resolution of DR 218. */
17144 if (koenig_p
17145 && ((is_overloaded_fn (function)
17146 /* If lookup found a member function, the Koenig lookup is
17147 not appropriate, even if an unqualified-name was used
17148 to denote the function. */
17149 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17150 || identifier_p (function))
17151 /* Only do this when substitution turns a dependent call
17152 into a non-dependent call. */
17153 && type_dependent_expression_p_push (t)
17154 && !any_type_dependent_arguments_p (call_args))
17155 function = perform_koenig_lookup (function, call_args, tf_none);
17156
17157 if (function != NULL_TREE
17158 && identifier_p (function)
17159 && !any_type_dependent_arguments_p (call_args))
17160 {
17161 if (koenig_p && (complain & tf_warning_or_error))
17162 {
17163 /* For backwards compatibility and good diagnostics, try
17164 the unqualified lookup again if we aren't in SFINAE
17165 context. */
17166 tree unq = (tsubst_copy_and_build
17167 (function, args, complain, in_decl, true,
17168 integral_constant_expression_p));
17169 if (unq == error_mark_node)
17170 {
17171 release_tree_vector (call_args);
17172 RETURN (error_mark_node);
17173 }
17174
17175 if (unq != function)
17176 {
17177 /* In a lambda fn, we have to be careful to not
17178 introduce new this captures. Legacy code can't
17179 be using lambdas anyway, so it's ok to be
17180 stricter. */
17181 bool in_lambda = (current_class_type
17182 && LAMBDA_TYPE_P (current_class_type));
17183 char const *msg = "%qD was not declared in this scope, "
17184 "and no declarations were found by "
17185 "argument-dependent lookup at the point "
17186 "of instantiation";
17187
17188 bool diag = true;
17189 if (in_lambda)
17190 error_at (EXPR_LOC_OR_LOC (t, input_location),
17191 msg, function);
17192 else
17193 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17194 msg, function);
17195 if (diag)
17196 {
17197 tree fn = unq;
17198 if (INDIRECT_REF_P (fn))
17199 fn = TREE_OPERAND (fn, 0);
17200 if (TREE_CODE (fn) == COMPONENT_REF)
17201 fn = TREE_OPERAND (fn, 1);
17202 if (is_overloaded_fn (fn))
17203 fn = get_first_fn (fn);
17204
17205 if (!DECL_P (fn))
17206 /* Can't say anything more. */;
17207 else if (DECL_CLASS_SCOPE_P (fn))
17208 {
17209 location_t loc = EXPR_LOC_OR_LOC (t,
17210 input_location);
17211 inform (loc,
17212 "declarations in dependent base %qT are "
17213 "not found by unqualified lookup",
17214 DECL_CLASS_CONTEXT (fn));
17215 if (current_class_ptr)
17216 inform (loc,
17217 "use %<this->%D%> instead", function);
17218 else
17219 inform (loc,
17220 "use %<%T::%D%> instead",
17221 current_class_name, function);
17222 }
17223 else
17224 inform (DECL_SOURCE_LOCATION (fn),
17225 "%qD declared here, later in the "
17226 "translation unit", fn);
17227 if (in_lambda)
17228 {
17229 release_tree_vector (call_args);
17230 RETURN (error_mark_node);
17231 }
17232 }
17233
17234 function = unq;
17235 }
17236 }
17237 if (identifier_p (function))
17238 {
17239 if (complain & tf_error)
17240 unqualified_name_lookup_error (function);
17241 release_tree_vector (call_args);
17242 RETURN (error_mark_node);
17243 }
17244 }
17245
17246 /* Remember that there was a reference to this entity. */
17247 if (function != NULL_TREE
17248 && DECL_P (function)
17249 && !mark_used (function, complain) && !(complain & tf_error))
17250 {
17251 release_tree_vector (call_args);
17252 RETURN (error_mark_node);
17253 }
17254
17255 /* Put back tf_decltype for the actual call. */
17256 complain |= decltype_flag;
17257
17258 if (function == NULL_TREE)
17259 switch (CALL_EXPR_IFN (t))
17260 {
17261 case IFN_LAUNDER:
17262 gcc_assert (nargs == 1);
17263 if (vec_safe_length (call_args) != 1)
17264 {
17265 error_at (EXPR_LOC_OR_LOC (t, input_location),
17266 "wrong number of arguments to "
17267 "%<__builtin_launder%>");
17268 ret = error_mark_node;
17269 }
17270 else
17271 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17272 input_location),
17273 (*call_args)[0], complain);
17274 break;
17275
17276 default:
17277 /* Unsupported internal function with arguments. */
17278 gcc_unreachable ();
17279 }
17280 else if (TREE_CODE (function) == OFFSET_REF)
17281 ret = build_offset_ref_call_from_tree (function, &call_args,
17282 complain);
17283 else if (TREE_CODE (function) == COMPONENT_REF)
17284 {
17285 tree instance = TREE_OPERAND (function, 0);
17286 tree fn = TREE_OPERAND (function, 1);
17287
17288 if (processing_template_decl
17289 && (type_dependent_expression_p (instance)
17290 || (!BASELINK_P (fn)
17291 && TREE_CODE (fn) != FIELD_DECL)
17292 || type_dependent_expression_p (fn)
17293 || any_type_dependent_arguments_p (call_args)))
17294 ret = build_nt_call_vec (function, call_args);
17295 else if (!BASELINK_P (fn))
17296 ret = finish_call_expr (function, &call_args,
17297 /*disallow_virtual=*/false,
17298 /*koenig_p=*/false,
17299 complain);
17300 else
17301 ret = (build_new_method_call
17302 (instance, fn,
17303 &call_args, NULL_TREE,
17304 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17305 /*fn_p=*/NULL,
17306 complain));
17307 }
17308 else
17309 ret = finish_call_expr (function, &call_args,
17310 /*disallow_virtual=*/qualified_p,
17311 koenig_p,
17312 complain);
17313
17314 release_tree_vector (call_args);
17315
17316 if (ret != error_mark_node)
17317 {
17318 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17319 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17320 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17321 bool thk = CALL_FROM_THUNK_P (t);
17322 if (op || ord || rev || thk)
17323 {
17324 function = extract_call_expr (ret);
17325 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17326 CALL_EXPR_ORDERED_ARGS (function) = ord;
17327 CALL_EXPR_REVERSE_ARGS (function) = rev;
17328 if (thk)
17329 {
17330 CALL_FROM_THUNK_P (function) = true;
17331 /* The thunk location is not interesting. */
17332 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17333 }
17334 }
17335 }
17336
17337 RETURN (ret);
17338 }
17339
17340 case COND_EXPR:
17341 {
17342 tree cond = RECUR (TREE_OPERAND (t, 0));
17343 tree folded_cond = fold_non_dependent_expr (cond);
17344 tree exp1, exp2;
17345
17346 if (TREE_CODE (folded_cond) == INTEGER_CST)
17347 {
17348 if (integer_zerop (folded_cond))
17349 {
17350 ++c_inhibit_evaluation_warnings;
17351 exp1 = RECUR (TREE_OPERAND (t, 1));
17352 --c_inhibit_evaluation_warnings;
17353 exp2 = RECUR (TREE_OPERAND (t, 2));
17354 }
17355 else
17356 {
17357 exp1 = RECUR (TREE_OPERAND (t, 1));
17358 ++c_inhibit_evaluation_warnings;
17359 exp2 = RECUR (TREE_OPERAND (t, 2));
17360 --c_inhibit_evaluation_warnings;
17361 }
17362 cond = folded_cond;
17363 }
17364 else
17365 {
17366 exp1 = RECUR (TREE_OPERAND (t, 1));
17367 exp2 = RECUR (TREE_OPERAND (t, 2));
17368 }
17369
17370 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17371 cond, exp1, exp2, complain));
17372 }
17373
17374 case PSEUDO_DTOR_EXPR:
17375 {
17376 tree op0 = RECUR (TREE_OPERAND (t, 0));
17377 tree op1 = RECUR (TREE_OPERAND (t, 1));
17378 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17379 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17380 input_location));
17381 }
17382
17383 case TREE_LIST:
17384 {
17385 tree purpose, value, chain;
17386
17387 if (t == void_list_node)
17388 RETURN (t);
17389
17390 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17391 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17392 {
17393 /* We have pack expansions, so expand those and
17394 create a new list out of it. */
17395 tree purposevec = NULL_TREE;
17396 tree valuevec = NULL_TREE;
17397 tree chain;
17398 int i, len = -1;
17399
17400 /* Expand the argument expressions. */
17401 if (TREE_PURPOSE (t))
17402 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17403 complain, in_decl);
17404 if (TREE_VALUE (t))
17405 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17406 complain, in_decl);
17407
17408 /* Build the rest of the list. */
17409 chain = TREE_CHAIN (t);
17410 if (chain && chain != void_type_node)
17411 chain = RECUR (chain);
17412
17413 /* Determine the number of arguments. */
17414 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17415 {
17416 len = TREE_VEC_LENGTH (purposevec);
17417 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17418 }
17419 else if (TREE_CODE (valuevec) == TREE_VEC)
17420 len = TREE_VEC_LENGTH (valuevec);
17421 else
17422 {
17423 /* Since we only performed a partial substitution into
17424 the argument pack, we only RETURN (a single list
17425 node. */
17426 if (purposevec == TREE_PURPOSE (t)
17427 && valuevec == TREE_VALUE (t)
17428 && chain == TREE_CHAIN (t))
17429 RETURN (t);
17430
17431 RETURN (tree_cons (purposevec, valuevec, chain));
17432 }
17433
17434 /* Convert the argument vectors into a TREE_LIST */
17435 i = len;
17436 while (i > 0)
17437 {
17438 /* Grab the Ith values. */
17439 i--;
17440 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17441 : NULL_TREE;
17442 value
17443 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17444 : NULL_TREE;
17445
17446 /* Build the list (backwards). */
17447 chain = tree_cons (purpose, value, chain);
17448 }
17449
17450 RETURN (chain);
17451 }
17452
17453 purpose = TREE_PURPOSE (t);
17454 if (purpose)
17455 purpose = RECUR (purpose);
17456 value = TREE_VALUE (t);
17457 if (value)
17458 value = RECUR (value);
17459 chain = TREE_CHAIN (t);
17460 if (chain && chain != void_type_node)
17461 chain = RECUR (chain);
17462 if (purpose == TREE_PURPOSE (t)
17463 && value == TREE_VALUE (t)
17464 && chain == TREE_CHAIN (t))
17465 RETURN (t);
17466 RETURN (tree_cons (purpose, value, chain));
17467 }
17468
17469 case COMPONENT_REF:
17470 {
17471 tree object;
17472 tree object_type;
17473 tree member;
17474 tree r;
17475
17476 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17477 args, complain, in_decl);
17478 /* Remember that there was a reference to this entity. */
17479 if (DECL_P (object)
17480 && !mark_used (object, complain) && !(complain & tf_error))
17481 RETURN (error_mark_node);
17482 object_type = TREE_TYPE (object);
17483
17484 member = TREE_OPERAND (t, 1);
17485 if (BASELINK_P (member))
17486 member = tsubst_baselink (member,
17487 non_reference (TREE_TYPE (object)),
17488 args, complain, in_decl);
17489 else
17490 member = tsubst_copy (member, args, complain, in_decl);
17491 if (member == error_mark_node)
17492 RETURN (error_mark_node);
17493
17494 if (TREE_CODE (member) == FIELD_DECL)
17495 {
17496 r = finish_non_static_data_member (member, object, NULL_TREE);
17497 if (TREE_CODE (r) == COMPONENT_REF)
17498 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17499 RETURN (r);
17500 }
17501 else if (type_dependent_expression_p (object))
17502 /* We can't do much here. */;
17503 else if (!CLASS_TYPE_P (object_type))
17504 {
17505 if (scalarish_type_p (object_type))
17506 {
17507 tree s = NULL_TREE;
17508 tree dtor = member;
17509
17510 if (TREE_CODE (dtor) == SCOPE_REF)
17511 {
17512 s = TREE_OPERAND (dtor, 0);
17513 dtor = TREE_OPERAND (dtor, 1);
17514 }
17515 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17516 {
17517 dtor = TREE_OPERAND (dtor, 0);
17518 if (TYPE_P (dtor))
17519 RETURN (finish_pseudo_destructor_expr
17520 (object, s, dtor, input_location));
17521 }
17522 }
17523 }
17524 else if (TREE_CODE (member) == SCOPE_REF
17525 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17526 {
17527 /* Lookup the template functions now that we know what the
17528 scope is. */
17529 tree scope = TREE_OPERAND (member, 0);
17530 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17531 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17532 member = lookup_qualified_name (scope, tmpl,
17533 /*is_type_p=*/false,
17534 /*complain=*/false);
17535 if (BASELINK_P (member))
17536 {
17537 BASELINK_FUNCTIONS (member)
17538 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17539 args);
17540 member = (adjust_result_of_qualified_name_lookup
17541 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17542 object_type));
17543 }
17544 else
17545 {
17546 qualified_name_lookup_error (scope, tmpl, member,
17547 input_location);
17548 RETURN (error_mark_node);
17549 }
17550 }
17551 else if (TREE_CODE (member) == SCOPE_REF
17552 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17553 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17554 {
17555 if (complain & tf_error)
17556 {
17557 if (TYPE_P (TREE_OPERAND (member, 0)))
17558 error ("%qT is not a class or namespace",
17559 TREE_OPERAND (member, 0));
17560 else
17561 error ("%qD is not a class or namespace",
17562 TREE_OPERAND (member, 0));
17563 }
17564 RETURN (error_mark_node);
17565 }
17566
17567 r = finish_class_member_access_expr (object, member,
17568 /*template_p=*/false,
17569 complain);
17570 if (TREE_CODE (r) == COMPONENT_REF)
17571 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17572 RETURN (r);
17573 }
17574
17575 case THROW_EXPR:
17576 RETURN (build_throw
17577 (RECUR (TREE_OPERAND (t, 0))));
17578
17579 case CONSTRUCTOR:
17580 {
17581 vec<constructor_elt, va_gc> *n;
17582 constructor_elt *ce;
17583 unsigned HOST_WIDE_INT idx;
17584 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17585 bool process_index_p;
17586 int newlen;
17587 bool need_copy_p = false;
17588 tree r;
17589
17590 if (type == error_mark_node)
17591 RETURN (error_mark_node);
17592
17593 /* digest_init will do the wrong thing if we let it. */
17594 if (type && TYPE_PTRMEMFUNC_P (type))
17595 RETURN (t);
17596
17597 /* We do not want to process the index of aggregate
17598 initializers as they are identifier nodes which will be
17599 looked up by digest_init. */
17600 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17601
17602 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17603 newlen = vec_safe_length (n);
17604 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17605 {
17606 if (ce->index && process_index_p
17607 /* An identifier index is looked up in the type
17608 being initialized, not the current scope. */
17609 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17610 ce->index = RECUR (ce->index);
17611
17612 if (PACK_EXPANSION_P (ce->value))
17613 {
17614 /* Substitute into the pack expansion. */
17615 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17616 in_decl);
17617
17618 if (ce->value == error_mark_node
17619 || PACK_EXPANSION_P (ce->value))
17620 ;
17621 else if (TREE_VEC_LENGTH (ce->value) == 1)
17622 /* Just move the argument into place. */
17623 ce->value = TREE_VEC_ELT (ce->value, 0);
17624 else
17625 {
17626 /* Update the length of the final CONSTRUCTOR
17627 arguments vector, and note that we will need to
17628 copy.*/
17629 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17630 need_copy_p = true;
17631 }
17632 }
17633 else
17634 ce->value = RECUR (ce->value);
17635 }
17636
17637 if (need_copy_p)
17638 {
17639 vec<constructor_elt, va_gc> *old_n = n;
17640
17641 vec_alloc (n, newlen);
17642 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17643 {
17644 if (TREE_CODE (ce->value) == TREE_VEC)
17645 {
17646 int i, len = TREE_VEC_LENGTH (ce->value);
17647 for (i = 0; i < len; ++i)
17648 CONSTRUCTOR_APPEND_ELT (n, 0,
17649 TREE_VEC_ELT (ce->value, i));
17650 }
17651 else
17652 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17653 }
17654 }
17655
17656 r = build_constructor (init_list_type_node, n);
17657 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17658
17659 if (TREE_HAS_CONSTRUCTOR (t))
17660 RETURN (finish_compound_literal (type, r, complain));
17661
17662 TREE_TYPE (r) = type;
17663 RETURN (r);
17664 }
17665
17666 case TYPEID_EXPR:
17667 {
17668 tree operand_0 = TREE_OPERAND (t, 0);
17669 if (TYPE_P (operand_0))
17670 {
17671 operand_0 = tsubst (operand_0, args, complain, in_decl);
17672 RETURN (get_typeid (operand_0, complain));
17673 }
17674 else
17675 {
17676 operand_0 = RECUR (operand_0);
17677 RETURN (build_typeid (operand_0, complain));
17678 }
17679 }
17680
17681 case VAR_DECL:
17682 if (!args)
17683 RETURN (t);
17684 else if (DECL_PACK_P (t))
17685 {
17686 /* We don't build decls for an instantiation of a
17687 variadic capture proxy, we instantiate the elements
17688 when needed. */
17689 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17690 return RECUR (DECL_VALUE_EXPR (t));
17691 }
17692 /* Fall through */
17693
17694 case PARM_DECL:
17695 {
17696 tree r = tsubst_copy (t, args, complain, in_decl);
17697 /* ??? We're doing a subset of finish_id_expression here. */
17698 if (VAR_P (r)
17699 && !processing_template_decl
17700 && !cp_unevaluated_operand
17701 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17702 && CP_DECL_THREAD_LOCAL_P (r))
17703 {
17704 if (tree wrap = get_tls_wrapper_fn (r))
17705 /* Replace an evaluated use of the thread_local variable with
17706 a call to its wrapper. */
17707 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17708 }
17709 else if (outer_automatic_var_p (r))
17710 {
17711 r = process_outer_var_ref (r, complain);
17712 if (is_capture_proxy (r))
17713 register_local_specialization (r, t);
17714 }
17715
17716 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17717 /* If the original type was a reference, we'll be wrapped in
17718 the appropriate INDIRECT_REF. */
17719 r = convert_from_reference (r);
17720 RETURN (r);
17721 }
17722
17723 case VA_ARG_EXPR:
17724 {
17725 tree op0 = RECUR (TREE_OPERAND (t, 0));
17726 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17727 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17728 }
17729
17730 case OFFSETOF_EXPR:
17731 {
17732 tree object_ptr
17733 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17734 in_decl, /*function_p=*/false,
17735 /*integral_constant_expression_p=*/false);
17736 RETURN (finish_offsetof (object_ptr,
17737 RECUR (TREE_OPERAND (t, 0)),
17738 EXPR_LOCATION (t)));
17739 }
17740
17741 case ADDRESSOF_EXPR:
17742 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17743 RECUR (TREE_OPERAND (t, 0)), complain));
17744
17745 case TRAIT_EXPR:
17746 {
17747 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17748 complain, in_decl);
17749
17750 tree type2 = TRAIT_EXPR_TYPE2 (t);
17751 if (type2 && TREE_CODE (type2) == TREE_LIST)
17752 type2 = RECUR (type2);
17753 else if (type2)
17754 type2 = tsubst (type2, args, complain, in_decl);
17755
17756 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17757 }
17758
17759 case STMT_EXPR:
17760 {
17761 tree old_stmt_expr = cur_stmt_expr;
17762 tree stmt_expr = begin_stmt_expr ();
17763
17764 cur_stmt_expr = stmt_expr;
17765 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17766 integral_constant_expression_p);
17767 stmt_expr = finish_stmt_expr (stmt_expr, false);
17768 cur_stmt_expr = old_stmt_expr;
17769
17770 /* If the resulting list of expression statement is empty,
17771 fold it further into void_node. */
17772 if (empty_expr_stmt_p (stmt_expr))
17773 stmt_expr = void_node;
17774
17775 RETURN (stmt_expr);
17776 }
17777
17778 case LAMBDA_EXPR:
17779 {
17780 tree r = build_lambda_expr ();
17781
17782 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17783 LAMBDA_EXPR_CLOSURE (r) = type;
17784 CLASSTYPE_LAMBDA_EXPR (type) = r;
17785
17786 LAMBDA_EXPR_LOCATION (r)
17787 = LAMBDA_EXPR_LOCATION (t);
17788 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17789 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17790 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17791 LAMBDA_EXPR_DISCRIMINATOR (r)
17792 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17793 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17794 if (!scope)
17795 /* No substitution needed. */;
17796 else if (VAR_OR_FUNCTION_DECL_P (scope))
17797 /* For a function or variable scope, we want to use tsubst so that we
17798 don't complain about referring to an auto before deduction. */
17799 scope = tsubst (scope, args, complain, in_decl);
17800 else if (TREE_CODE (scope) == PARM_DECL)
17801 {
17802 /* Look up the parameter we want directly, as tsubst_copy
17803 doesn't do what we need. */
17804 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17805 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17806 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17807 parm = DECL_CHAIN (parm);
17808 scope = parm;
17809 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17810 if (DECL_CONTEXT (scope) == NULL_TREE)
17811 DECL_CONTEXT (scope) = fn;
17812 }
17813 else if (TREE_CODE (scope) == FIELD_DECL)
17814 /* For a field, use tsubst_copy so that we look up the existing field
17815 rather than build a new one. */
17816 scope = RECUR (scope);
17817 else
17818 gcc_unreachable ();
17819 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17820
17821 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17822 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17823
17824 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17825 determine_visibility (TYPE_NAME (type));
17826 /* Now that we know visibility, instantiate the type so we have a
17827 declaration of the op() for later calls to lambda_function. */
17828 complete_type (type);
17829
17830 if (tree fn = lambda_function (type))
17831 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17832
17833 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17834
17835 insert_pending_capture_proxies ();
17836
17837 RETURN (build_lambda_object (r));
17838 }
17839
17840 case TARGET_EXPR:
17841 /* We can get here for a constant initializer of non-dependent type.
17842 FIXME stop folding in cp_parser_initializer_clause. */
17843 {
17844 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17845 complain);
17846 RETURN (r);
17847 }
17848
17849 case TRANSACTION_EXPR:
17850 RETURN (tsubst_expr(t, args, complain, in_decl,
17851 integral_constant_expression_p));
17852
17853 case PAREN_EXPR:
17854 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17855
17856 case VEC_PERM_EXPR:
17857 {
17858 tree op0 = RECUR (TREE_OPERAND (t, 0));
17859 tree op1 = RECUR (TREE_OPERAND (t, 1));
17860 tree op2 = RECUR (TREE_OPERAND (t, 2));
17861 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17862 complain));
17863 }
17864
17865 case REQUIRES_EXPR:
17866 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17867
17868 default:
17869 /* Handle Objective-C++ constructs, if appropriate. */
17870 {
17871 tree subst
17872 = objcp_tsubst_copy_and_build (t, args, complain,
17873 in_decl, /*function_p=*/false);
17874 if (subst)
17875 RETURN (subst);
17876 }
17877 RETURN (tsubst_copy (t, args, complain, in_decl));
17878 }
17879
17880 #undef RECUR
17881 #undef RETURN
17882 out:
17883 input_location = loc;
17884 return retval;
17885 }
17886
17887 /* Verify that the instantiated ARGS are valid. For type arguments,
17888 make sure that the type's linkage is ok. For non-type arguments,
17889 make sure they are constants if they are integral or enumerations.
17890 Emit an error under control of COMPLAIN, and return TRUE on error. */
17891
17892 static bool
17893 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17894 {
17895 if (dependent_template_arg_p (t))
17896 return false;
17897 if (ARGUMENT_PACK_P (t))
17898 {
17899 tree vec = ARGUMENT_PACK_ARGS (t);
17900 int len = TREE_VEC_LENGTH (vec);
17901 bool result = false;
17902 int i;
17903
17904 for (i = 0; i < len; ++i)
17905 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17906 result = true;
17907 return result;
17908 }
17909 else if (TYPE_P (t))
17910 {
17911 /* [basic.link]: A name with no linkage (notably, the name
17912 of a class or enumeration declared in a local scope)
17913 shall not be used to declare an entity with linkage.
17914 This implies that names with no linkage cannot be used as
17915 template arguments
17916
17917 DR 757 relaxes this restriction for C++0x. */
17918 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17919 : no_linkage_check (t, /*relaxed_p=*/false));
17920
17921 if (nt)
17922 {
17923 /* DR 488 makes use of a type with no linkage cause
17924 type deduction to fail. */
17925 if (complain & tf_error)
17926 {
17927 if (TYPE_UNNAMED_P (nt))
17928 error ("%qT is/uses unnamed type", t);
17929 else
17930 error ("template argument for %qD uses local type %qT",
17931 tmpl, t);
17932 }
17933 return true;
17934 }
17935 /* In order to avoid all sorts of complications, we do not
17936 allow variably-modified types as template arguments. */
17937 else if (variably_modified_type_p (t, NULL_TREE))
17938 {
17939 if (complain & tf_error)
17940 error ("%qT is a variably modified type", t);
17941 return true;
17942 }
17943 }
17944 /* Class template and alias template arguments should be OK. */
17945 else if (DECL_TYPE_TEMPLATE_P (t))
17946 ;
17947 /* A non-type argument of integral or enumerated type must be a
17948 constant. */
17949 else if (TREE_TYPE (t)
17950 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17951 && !REFERENCE_REF_P (t)
17952 && !TREE_CONSTANT (t))
17953 {
17954 if (complain & tf_error)
17955 error ("integral expression %qE is not constant", t);
17956 return true;
17957 }
17958 return false;
17959 }
17960
17961 static bool
17962 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17963 {
17964 int ix, len = DECL_NTPARMS (tmpl);
17965 bool result = false;
17966
17967 for (ix = 0; ix != len; ix++)
17968 {
17969 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17970 result = true;
17971 }
17972 if (result && (complain & tf_error))
17973 error (" trying to instantiate %qD", tmpl);
17974 return result;
17975 }
17976
17977 /* We're out of SFINAE context now, so generate diagnostics for the access
17978 errors we saw earlier when instantiating D from TMPL and ARGS. */
17979
17980 static void
17981 recheck_decl_substitution (tree d, tree tmpl, tree args)
17982 {
17983 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17984 tree type = TREE_TYPE (pattern);
17985 location_t loc = input_location;
17986
17987 push_access_scope (d);
17988 push_deferring_access_checks (dk_no_deferred);
17989 input_location = DECL_SOURCE_LOCATION (pattern);
17990 tsubst (type, args, tf_warning_or_error, d);
17991 input_location = loc;
17992 pop_deferring_access_checks ();
17993 pop_access_scope (d);
17994 }
17995
17996 /* Instantiate the indicated variable, function, or alias template TMPL with
17997 the template arguments in TARG_PTR. */
17998
17999 static tree
18000 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18001 {
18002 tree targ_ptr = orig_args;
18003 tree fndecl;
18004 tree gen_tmpl;
18005 tree spec;
18006 bool access_ok = true;
18007
18008 if (tmpl == error_mark_node)
18009 return error_mark_node;
18010
18011 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18012
18013 /* If this function is a clone, handle it specially. */
18014 if (DECL_CLONED_FUNCTION_P (tmpl))
18015 {
18016 tree spec;
18017 tree clone;
18018
18019 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18020 DECL_CLONED_FUNCTION. */
18021 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18022 targ_ptr, complain);
18023 if (spec == error_mark_node)
18024 return error_mark_node;
18025
18026 /* Look for the clone. */
18027 FOR_EACH_CLONE (clone, spec)
18028 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18029 return clone;
18030 /* We should always have found the clone by now. */
18031 gcc_unreachable ();
18032 return NULL_TREE;
18033 }
18034
18035 if (targ_ptr == error_mark_node)
18036 return error_mark_node;
18037
18038 /* Check to see if we already have this specialization. */
18039 gen_tmpl = most_general_template (tmpl);
18040 if (TMPL_ARGS_DEPTH (targ_ptr)
18041 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18042 /* targ_ptr only has the innermost template args, so add the outer ones
18043 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18044 the case of a non-dependent call within a template definition). */
18045 targ_ptr = (add_outermost_template_args
18046 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18047 targ_ptr));
18048
18049 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18050 but it doesn't seem to be on the hot path. */
18051 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18052
18053 gcc_assert (tmpl == gen_tmpl
18054 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18055 == spec)
18056 || fndecl == NULL_TREE);
18057
18058 if (spec != NULL_TREE)
18059 {
18060 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18061 {
18062 if (complain & tf_error)
18063 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18064 return error_mark_node;
18065 }
18066 return spec;
18067 }
18068
18069 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18070 complain))
18071 return error_mark_node;
18072
18073 /* We are building a FUNCTION_DECL, during which the access of its
18074 parameters and return types have to be checked. However this
18075 FUNCTION_DECL which is the desired context for access checking
18076 is not built yet. We solve this chicken-and-egg problem by
18077 deferring all checks until we have the FUNCTION_DECL. */
18078 push_deferring_access_checks (dk_deferred);
18079
18080 /* Instantiation of the function happens in the context of the function
18081 template, not the context of the overload resolution we're doing. */
18082 push_to_top_level ();
18083 /* If there are dependent arguments, e.g. because we're doing partial
18084 ordering, make sure processing_template_decl stays set. */
18085 if (uses_template_parms (targ_ptr))
18086 ++processing_template_decl;
18087 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18088 {
18089 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18090 complain, gen_tmpl, true);
18091 push_nested_class (ctx);
18092 }
18093
18094 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18095
18096 fndecl = NULL_TREE;
18097 if (VAR_P (pattern))
18098 {
18099 /* We need to determine if we're using a partial or explicit
18100 specialization now, because the type of the variable could be
18101 different. */
18102 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18103 tree elt = most_specialized_partial_spec (tid, complain);
18104 if (elt == error_mark_node)
18105 pattern = error_mark_node;
18106 else if (elt)
18107 {
18108 tree partial_tmpl = TREE_VALUE (elt);
18109 tree partial_args = TREE_PURPOSE (elt);
18110 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18111 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18112 }
18113 }
18114
18115 /* Substitute template parameters to obtain the specialization. */
18116 if (fndecl == NULL_TREE)
18117 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18118 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18119 pop_nested_class ();
18120 pop_from_top_level ();
18121
18122 if (fndecl == error_mark_node)
18123 {
18124 pop_deferring_access_checks ();
18125 return error_mark_node;
18126 }
18127
18128 /* The DECL_TI_TEMPLATE should always be the immediate parent
18129 template, not the most general template. */
18130 DECL_TI_TEMPLATE (fndecl) = tmpl;
18131 DECL_TI_ARGS (fndecl) = targ_ptr;
18132
18133 /* Now we know the specialization, compute access previously
18134 deferred. Do no access control for inheriting constructors,
18135 as we already checked access for the inherited constructor. */
18136 if (!(flag_new_inheriting_ctors
18137 && DECL_INHERITED_CTOR (fndecl)))
18138 {
18139 push_access_scope (fndecl);
18140 if (!perform_deferred_access_checks (complain))
18141 access_ok = false;
18142 pop_access_scope (fndecl);
18143 }
18144 pop_deferring_access_checks ();
18145
18146 /* If we've just instantiated the main entry point for a function,
18147 instantiate all the alternate entry points as well. We do this
18148 by cloning the instantiation of the main entry point, not by
18149 instantiating the template clones. */
18150 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18151 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
18152
18153 if (!access_ok)
18154 {
18155 if (!(complain & tf_error))
18156 {
18157 /* Remember to reinstantiate when we're out of SFINAE so the user
18158 can see the errors. */
18159 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18160 }
18161 return error_mark_node;
18162 }
18163 return fndecl;
18164 }
18165
18166 /* Wrapper for instantiate_template_1. */
18167
18168 tree
18169 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18170 {
18171 tree ret;
18172 timevar_push (TV_TEMPLATE_INST);
18173 ret = instantiate_template_1 (tmpl, orig_args, complain);
18174 timevar_pop (TV_TEMPLATE_INST);
18175 return ret;
18176 }
18177
18178 /* Instantiate the alias template TMPL with ARGS. Also push a template
18179 instantiation level, which instantiate_template doesn't do because
18180 functions and variables have sufficient context established by the
18181 callers. */
18182
18183 static tree
18184 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18185 {
18186 struct pending_template *old_last_pend = last_pending_template;
18187 struct tinst_level *old_error_tinst = last_error_tinst_level;
18188 if (tmpl == error_mark_node || args == error_mark_node)
18189 return error_mark_node;
18190 tree tinst = build_tree_list (tmpl, args);
18191 if (!push_tinst_level (tinst))
18192 {
18193 ggc_free (tinst);
18194 return error_mark_node;
18195 }
18196
18197 args =
18198 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18199 args, tmpl, complain,
18200 /*require_all_args=*/true,
18201 /*use_default_args=*/true);
18202
18203 tree r = instantiate_template (tmpl, args, complain);
18204 pop_tinst_level ();
18205 /* We can't free this if a pending_template entry or last_error_tinst_level
18206 is pointing at it. */
18207 if (last_pending_template == old_last_pend
18208 && last_error_tinst_level == old_error_tinst)
18209 ggc_free (tinst);
18210
18211 return r;
18212 }
18213
18214 /* PARM is a template parameter pack for FN. Returns true iff
18215 PARM is used in a deducible way in the argument list of FN. */
18216
18217 static bool
18218 pack_deducible_p (tree parm, tree fn)
18219 {
18220 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18221 for (; t; t = TREE_CHAIN (t))
18222 {
18223 tree type = TREE_VALUE (t);
18224 tree packs;
18225 if (!PACK_EXPANSION_P (type))
18226 continue;
18227 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18228 packs; packs = TREE_CHAIN (packs))
18229 if (template_args_equal (TREE_VALUE (packs), parm))
18230 {
18231 /* The template parameter pack is used in a function parameter
18232 pack. If this is the end of the parameter list, the
18233 template parameter pack is deducible. */
18234 if (TREE_CHAIN (t) == void_list_node)
18235 return true;
18236 else
18237 /* Otherwise, not. Well, it could be deduced from
18238 a non-pack parameter, but doing so would end up with
18239 a deduction mismatch, so don't bother. */
18240 return false;
18241 }
18242 }
18243 /* The template parameter pack isn't used in any function parameter
18244 packs, but it might be used deeper, e.g. tuple<Args...>. */
18245 return true;
18246 }
18247
18248 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18249 NARGS elements of the arguments that are being used when calling
18250 it. TARGS is a vector into which the deduced template arguments
18251 are placed.
18252
18253 Returns either a FUNCTION_DECL for the matching specialization of FN or
18254 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18255 true, diagnostics will be printed to explain why it failed.
18256
18257 If FN is a conversion operator, or we are trying to produce a specific
18258 specialization, RETURN_TYPE is the return type desired.
18259
18260 The EXPLICIT_TARGS are explicit template arguments provided via a
18261 template-id.
18262
18263 The parameter STRICT is one of:
18264
18265 DEDUCE_CALL:
18266 We are deducing arguments for a function call, as in
18267 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18268 deducing arguments for a call to the result of a conversion
18269 function template, as in [over.call.object].
18270
18271 DEDUCE_CONV:
18272 We are deducing arguments for a conversion function, as in
18273 [temp.deduct.conv].
18274
18275 DEDUCE_EXACT:
18276 We are deducing arguments when doing an explicit instantiation
18277 as in [temp.explicit], when determining an explicit specialization
18278 as in [temp.expl.spec], or when taking the address of a function
18279 template, as in [temp.deduct.funcaddr]. */
18280
18281 tree
18282 fn_type_unification (tree fn,
18283 tree explicit_targs,
18284 tree targs,
18285 const tree *args,
18286 unsigned int nargs,
18287 tree return_type,
18288 unification_kind_t strict,
18289 int flags,
18290 bool explain_p,
18291 bool decltype_p)
18292 {
18293 tree parms;
18294 tree fntype;
18295 tree decl = NULL_TREE;
18296 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18297 bool ok;
18298 static int deduction_depth;
18299 struct pending_template *old_last_pend = last_pending_template;
18300 struct tinst_level *old_error_tinst = last_error_tinst_level;
18301
18302 tree orig_fn = fn;
18303 if (flag_new_inheriting_ctors)
18304 fn = strip_inheriting_ctors (fn);
18305
18306 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18307 tree tinst;
18308 tree r = error_mark_node;
18309
18310 tree full_targs = targs;
18311 if (TMPL_ARGS_DEPTH (targs)
18312 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18313 full_targs = (add_outermost_template_args
18314 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18315 targs));
18316
18317 if (decltype_p)
18318 complain |= tf_decltype;
18319
18320 /* In C++0x, it's possible to have a function template whose type depends
18321 on itself recursively. This is most obvious with decltype, but can also
18322 occur with enumeration scope (c++/48969). So we need to catch infinite
18323 recursion and reject the substitution at deduction time; this function
18324 will return error_mark_node for any repeated substitution.
18325
18326 This also catches excessive recursion such as when f<N> depends on
18327 f<N-1> across all integers, and returns error_mark_node for all the
18328 substitutions back up to the initial one.
18329
18330 This is, of course, not reentrant. */
18331 if (excessive_deduction_depth)
18332 return error_mark_node;
18333 tinst = build_tree_list (fn, NULL_TREE);
18334 ++deduction_depth;
18335
18336 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18337
18338 fntype = TREE_TYPE (fn);
18339 if (explicit_targs)
18340 {
18341 /* [temp.deduct]
18342
18343 The specified template arguments must match the template
18344 parameters in kind (i.e., type, nontype, template), and there
18345 must not be more arguments than there are parameters;
18346 otherwise type deduction fails.
18347
18348 Nontype arguments must match the types of the corresponding
18349 nontype template parameters, or must be convertible to the
18350 types of the corresponding nontype parameters as specified in
18351 _temp.arg.nontype_, otherwise type deduction fails.
18352
18353 All references in the function type of the function template
18354 to the corresponding template parameters are replaced by the
18355 specified template argument values. If a substitution in a
18356 template parameter or in the function type of the function
18357 template results in an invalid type, type deduction fails. */
18358 int i, len = TREE_VEC_LENGTH (tparms);
18359 location_t loc = input_location;
18360 bool incomplete = false;
18361
18362 if (explicit_targs == error_mark_node)
18363 goto fail;
18364
18365 if (TMPL_ARGS_DEPTH (explicit_targs)
18366 < TMPL_ARGS_DEPTH (full_targs))
18367 explicit_targs = add_outermost_template_args (full_targs,
18368 explicit_targs);
18369
18370 /* Adjust any explicit template arguments before entering the
18371 substitution context. */
18372 explicit_targs
18373 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18374 complain,
18375 /*require_all_args=*/false,
18376 /*use_default_args=*/false));
18377 if (explicit_targs == error_mark_node)
18378 goto fail;
18379
18380 /* Substitute the explicit args into the function type. This is
18381 necessary so that, for instance, explicitly declared function
18382 arguments can match null pointed constants. If we were given
18383 an incomplete set of explicit args, we must not do semantic
18384 processing during substitution as we could create partial
18385 instantiations. */
18386 for (i = 0; i < len; i++)
18387 {
18388 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18389 bool parameter_pack = false;
18390 tree targ = TREE_VEC_ELT (explicit_targs, i);
18391
18392 /* Dig out the actual parm. */
18393 if (TREE_CODE (parm) == TYPE_DECL
18394 || TREE_CODE (parm) == TEMPLATE_DECL)
18395 {
18396 parm = TREE_TYPE (parm);
18397 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18398 }
18399 else if (TREE_CODE (parm) == PARM_DECL)
18400 {
18401 parm = DECL_INITIAL (parm);
18402 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18403 }
18404
18405 if (!parameter_pack && targ == NULL_TREE)
18406 /* No explicit argument for this template parameter. */
18407 incomplete = true;
18408
18409 if (parameter_pack && pack_deducible_p (parm, fn))
18410 {
18411 /* Mark the argument pack as "incomplete". We could
18412 still deduce more arguments during unification.
18413 We remove this mark in type_unification_real. */
18414 if (targ)
18415 {
18416 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18417 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18418 = ARGUMENT_PACK_ARGS (targ);
18419 }
18420
18421 /* We have some incomplete argument packs. */
18422 incomplete = true;
18423 }
18424 }
18425
18426 TREE_VALUE (tinst) = explicit_targs;
18427 if (!push_tinst_level (tinst))
18428 {
18429 excessive_deduction_depth = true;
18430 goto fail;
18431 }
18432 processing_template_decl += incomplete;
18433 input_location = DECL_SOURCE_LOCATION (fn);
18434 /* Ignore any access checks; we'll see them again in
18435 instantiate_template and they might have the wrong
18436 access path at this point. */
18437 push_deferring_access_checks (dk_deferred);
18438 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18439 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18440 pop_deferring_access_checks ();
18441 input_location = loc;
18442 processing_template_decl -= incomplete;
18443 pop_tinst_level ();
18444
18445 if (fntype == error_mark_node)
18446 goto fail;
18447
18448 /* Place the explicitly specified arguments in TARGS. */
18449 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18450 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18451 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18452 }
18453
18454 /* Never do unification on the 'this' parameter. */
18455 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18456
18457 if (return_type && strict == DEDUCE_CALL)
18458 {
18459 /* We're deducing for a call to the result of a template conversion
18460 function. The parms we really want are in return_type. */
18461 if (POINTER_TYPE_P (return_type))
18462 return_type = TREE_TYPE (return_type);
18463 parms = TYPE_ARG_TYPES (return_type);
18464 }
18465 else if (return_type)
18466 {
18467 tree *new_args;
18468
18469 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18470 new_args = XALLOCAVEC (tree, nargs + 1);
18471 new_args[0] = return_type;
18472 memcpy (new_args + 1, args, nargs * sizeof (tree));
18473 args = new_args;
18474 ++nargs;
18475 }
18476
18477 /* We allow incomplete unification without an error message here
18478 because the standard doesn't seem to explicitly prohibit it. Our
18479 callers must be ready to deal with unification failures in any
18480 event. */
18481
18482 TREE_VALUE (tinst) = targs;
18483 /* If we aren't explaining yet, push tinst context so we can see where
18484 any errors (e.g. from class instantiations triggered by instantiation
18485 of default template arguments) come from. If we are explaining, this
18486 context is redundant. */
18487 if (!explain_p && !push_tinst_level (tinst))
18488 {
18489 excessive_deduction_depth = true;
18490 goto fail;
18491 }
18492
18493 /* type_unification_real will pass back any access checks from default
18494 template argument substitution. */
18495 vec<deferred_access_check, va_gc> *checks;
18496 checks = NULL;
18497
18498 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18499 full_targs, parms, args, nargs, /*subr=*/0,
18500 strict, flags, &checks, explain_p);
18501 if (!explain_p)
18502 pop_tinst_level ();
18503 if (!ok)
18504 goto fail;
18505
18506 /* Now that we have bindings for all of the template arguments,
18507 ensure that the arguments deduced for the template template
18508 parameters have compatible template parameter lists. We cannot
18509 check this property before we have deduced all template
18510 arguments, because the template parameter types of a template
18511 template parameter might depend on prior template parameters
18512 deduced after the template template parameter. The following
18513 ill-formed example illustrates this issue:
18514
18515 template<typename T, template<T> class C> void f(C<5>, T);
18516
18517 template<int N> struct X {};
18518
18519 void g() {
18520 f(X<5>(), 5l); // error: template argument deduction fails
18521 }
18522
18523 The template parameter list of 'C' depends on the template type
18524 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18525 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18526 time that we deduce 'C'. */
18527 if (!template_template_parm_bindings_ok_p
18528 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18529 {
18530 unify_inconsistent_template_template_parameters (explain_p);
18531 goto fail;
18532 }
18533
18534 /* All is well so far. Now, check:
18535
18536 [temp.deduct]
18537
18538 When all template arguments have been deduced, all uses of
18539 template parameters in nondeduced contexts are replaced with
18540 the corresponding deduced argument values. If the
18541 substitution results in an invalid type, as described above,
18542 type deduction fails. */
18543 TREE_VALUE (tinst) = targs;
18544 if (!push_tinst_level (tinst))
18545 {
18546 excessive_deduction_depth = true;
18547 goto fail;
18548 }
18549
18550 /* Also collect access checks from the instantiation. */
18551 reopen_deferring_access_checks (checks);
18552
18553 decl = instantiate_template (fn, targs, complain);
18554
18555 checks = get_deferred_access_checks ();
18556 pop_deferring_access_checks ();
18557
18558 pop_tinst_level ();
18559
18560 if (decl == error_mark_node)
18561 goto fail;
18562
18563 /* Now perform any access checks encountered during substitution. */
18564 push_access_scope (decl);
18565 ok = perform_access_checks (checks, complain);
18566 pop_access_scope (decl);
18567 if (!ok)
18568 goto fail;
18569
18570 /* If we're looking for an exact match, check that what we got
18571 is indeed an exact match. It might not be if some template
18572 parameters are used in non-deduced contexts. But don't check
18573 for an exact match if we have dependent template arguments;
18574 in that case we're doing partial ordering, and we already know
18575 that we have two candidates that will provide the actual type. */
18576 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18577 {
18578 tree substed = TREE_TYPE (decl);
18579 unsigned int i;
18580
18581 tree sarg
18582 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18583 if (return_type)
18584 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18585 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18586 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18587 {
18588 unify_type_mismatch (explain_p, args[i],
18589 TREE_VALUE (sarg));
18590 goto fail;
18591 }
18592 }
18593
18594 /* After doing deduction with the inherited constructor, actually return an
18595 instantiation of the inheriting constructor. */
18596 if (orig_fn != fn)
18597 decl = instantiate_template (orig_fn, targs, complain);
18598
18599 r = decl;
18600
18601 fail:
18602 --deduction_depth;
18603 if (excessive_deduction_depth)
18604 {
18605 if (deduction_depth == 0)
18606 /* Reset once we're all the way out. */
18607 excessive_deduction_depth = false;
18608 }
18609
18610 /* We can't free this if a pending_template entry or last_error_tinst_level
18611 is pointing at it. */
18612 if (last_pending_template == old_last_pend
18613 && last_error_tinst_level == old_error_tinst)
18614 ggc_free (tinst);
18615
18616 return r;
18617 }
18618
18619 /* Adjust types before performing type deduction, as described in
18620 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18621 sections are symmetric. PARM is the type of a function parameter
18622 or the return type of the conversion function. ARG is the type of
18623 the argument passed to the call, or the type of the value
18624 initialized with the result of the conversion function.
18625 ARG_EXPR is the original argument expression, which may be null. */
18626
18627 static int
18628 maybe_adjust_types_for_deduction (unification_kind_t strict,
18629 tree* parm,
18630 tree* arg,
18631 tree arg_expr)
18632 {
18633 int result = 0;
18634
18635 switch (strict)
18636 {
18637 case DEDUCE_CALL:
18638 break;
18639
18640 case DEDUCE_CONV:
18641 /* Swap PARM and ARG throughout the remainder of this
18642 function; the handling is precisely symmetric since PARM
18643 will initialize ARG rather than vice versa. */
18644 std::swap (parm, arg);
18645 break;
18646
18647 case DEDUCE_EXACT:
18648 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18649 too, but here handle it by stripping the reference from PARM
18650 rather than by adding it to ARG. */
18651 if (TREE_CODE (*parm) == REFERENCE_TYPE
18652 && TYPE_REF_IS_RVALUE (*parm)
18653 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18654 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18655 && TREE_CODE (*arg) == REFERENCE_TYPE
18656 && !TYPE_REF_IS_RVALUE (*arg))
18657 *parm = TREE_TYPE (*parm);
18658 /* Nothing else to do in this case. */
18659 return 0;
18660
18661 default:
18662 gcc_unreachable ();
18663 }
18664
18665 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18666 {
18667 /* [temp.deduct.call]
18668
18669 If P is not a reference type:
18670
18671 --If A is an array type, the pointer type produced by the
18672 array-to-pointer standard conversion (_conv.array_) is
18673 used in place of A for type deduction; otherwise,
18674
18675 --If A is a function type, the pointer type produced by
18676 the function-to-pointer standard conversion
18677 (_conv.func_) is used in place of A for type deduction;
18678 otherwise,
18679
18680 --If A is a cv-qualified type, the top level
18681 cv-qualifiers of A's type are ignored for type
18682 deduction. */
18683 if (TREE_CODE (*arg) == ARRAY_TYPE)
18684 *arg = build_pointer_type (TREE_TYPE (*arg));
18685 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18686 *arg = build_pointer_type (*arg);
18687 else
18688 *arg = TYPE_MAIN_VARIANT (*arg);
18689 }
18690
18691 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18692 reference to a cv-unqualified template parameter that does not represent a
18693 template parameter of a class template (during class template argument
18694 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18695 an lvalue, the type "lvalue reference to A" is used in place of A for type
18696 deduction. */
18697 if (TREE_CODE (*parm) == REFERENCE_TYPE
18698 && TYPE_REF_IS_RVALUE (*parm)
18699 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18700 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18701 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18702 && (arg_expr ? lvalue_p (arg_expr)
18703 /* try_one_overload doesn't provide an arg_expr, but
18704 functions are always lvalues. */
18705 : TREE_CODE (*arg) == FUNCTION_TYPE))
18706 *arg = build_reference_type (*arg);
18707
18708 /* [temp.deduct.call]
18709
18710 If P is a cv-qualified type, the top level cv-qualifiers
18711 of P's type are ignored for type deduction. If P is a
18712 reference type, the type referred to by P is used for
18713 type deduction. */
18714 *parm = TYPE_MAIN_VARIANT (*parm);
18715 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18716 {
18717 *parm = TREE_TYPE (*parm);
18718 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18719 }
18720
18721 /* DR 322. For conversion deduction, remove a reference type on parm
18722 too (which has been swapped into ARG). */
18723 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18724 *arg = TREE_TYPE (*arg);
18725
18726 return result;
18727 }
18728
18729 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18730 template which does contain any deducible template parameters; check if
18731 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18732 unify_one_argument. */
18733
18734 static int
18735 check_non_deducible_conversion (tree parm, tree arg, int strict,
18736 int flags, bool explain_p)
18737 {
18738 tree type;
18739
18740 if (!TYPE_P (arg))
18741 type = TREE_TYPE (arg);
18742 else
18743 type = arg;
18744
18745 if (same_type_p (parm, type))
18746 return unify_success (explain_p);
18747
18748 if (strict == DEDUCE_CONV)
18749 {
18750 if (can_convert_arg (type, parm, NULL_TREE, flags,
18751 explain_p ? tf_warning_or_error : tf_none))
18752 return unify_success (explain_p);
18753 }
18754 else if (strict != DEDUCE_EXACT)
18755 {
18756 if (can_convert_arg (parm, type,
18757 TYPE_P (arg) ? NULL_TREE : arg,
18758 flags, explain_p ? tf_warning_or_error : tf_none))
18759 return unify_success (explain_p);
18760 }
18761
18762 if (strict == DEDUCE_EXACT)
18763 return unify_type_mismatch (explain_p, parm, arg);
18764 else
18765 return unify_arg_conversion (explain_p, parm, type, arg);
18766 }
18767
18768 static bool uses_deducible_template_parms (tree type);
18769
18770 /* Returns true iff the expression EXPR is one from which a template
18771 argument can be deduced. In other words, if it's an undecorated
18772 use of a template non-type parameter. */
18773
18774 static bool
18775 deducible_expression (tree expr)
18776 {
18777 /* Strip implicit conversions. */
18778 while (CONVERT_EXPR_P (expr))
18779 expr = TREE_OPERAND (expr, 0);
18780 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18781 }
18782
18783 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18784 deducible way; that is, if it has a max value of <PARM> - 1. */
18785
18786 static bool
18787 deducible_array_bound (tree domain)
18788 {
18789 if (domain == NULL_TREE)
18790 return false;
18791
18792 tree max = TYPE_MAX_VALUE (domain);
18793 if (TREE_CODE (max) != MINUS_EXPR)
18794 return false;
18795
18796 return deducible_expression (TREE_OPERAND (max, 0));
18797 }
18798
18799 /* Returns true iff the template arguments ARGS use a template parameter
18800 in a deducible way. */
18801
18802 static bool
18803 deducible_template_args (tree args)
18804 {
18805 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18806 {
18807 bool deducible;
18808 tree elt = TREE_VEC_ELT (args, i);
18809 if (ARGUMENT_PACK_P (elt))
18810 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18811 else
18812 {
18813 if (PACK_EXPANSION_P (elt))
18814 elt = PACK_EXPANSION_PATTERN (elt);
18815 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18816 deducible = true;
18817 else if (TYPE_P (elt))
18818 deducible = uses_deducible_template_parms (elt);
18819 else
18820 deducible = deducible_expression (elt);
18821 }
18822 if (deducible)
18823 return true;
18824 }
18825 return false;
18826 }
18827
18828 /* Returns true iff TYPE contains any deducible references to template
18829 parameters, as per 14.8.2.5. */
18830
18831 static bool
18832 uses_deducible_template_parms (tree type)
18833 {
18834 if (PACK_EXPANSION_P (type))
18835 type = PACK_EXPANSION_PATTERN (type);
18836
18837 /* T
18838 cv-list T
18839 TT<T>
18840 TT<i>
18841 TT<> */
18842 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18843 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18844 return true;
18845
18846 /* T*
18847 T&
18848 T&& */
18849 if (POINTER_TYPE_P (type))
18850 return uses_deducible_template_parms (TREE_TYPE (type));
18851
18852 /* T[integer-constant ]
18853 type [i] */
18854 if (TREE_CODE (type) == ARRAY_TYPE)
18855 return (uses_deducible_template_parms (TREE_TYPE (type))
18856 || deducible_array_bound (TYPE_DOMAIN (type)));
18857
18858 /* T type ::*
18859 type T::*
18860 T T::*
18861 T (type ::*)()
18862 type (T::*)()
18863 type (type ::*)(T)
18864 type (T::*)(T)
18865 T (type ::*)(T)
18866 T (T::*)()
18867 T (T::*)(T) */
18868 if (TYPE_PTRMEM_P (type))
18869 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18870 || (uses_deducible_template_parms
18871 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18872
18873 /* template-name <T> (where template-name refers to a class template)
18874 template-name <i> (where template-name refers to a class template) */
18875 if (CLASS_TYPE_P (type)
18876 && CLASSTYPE_TEMPLATE_INFO (type)
18877 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18878 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18879 (CLASSTYPE_TI_ARGS (type)));
18880
18881 /* type (T)
18882 T()
18883 T(T) */
18884 if (TREE_CODE (type) == FUNCTION_TYPE
18885 || TREE_CODE (type) == METHOD_TYPE)
18886 {
18887 if (uses_deducible_template_parms (TREE_TYPE (type)))
18888 return true;
18889 tree parm = TYPE_ARG_TYPES (type);
18890 if (TREE_CODE (type) == METHOD_TYPE)
18891 parm = TREE_CHAIN (parm);
18892 for (; parm; parm = TREE_CHAIN (parm))
18893 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18894 return true;
18895 }
18896
18897 return false;
18898 }
18899
18900 /* Subroutine of type_unification_real and unify_pack_expansion to
18901 handle unification of a single P/A pair. Parameters are as
18902 for those functions. */
18903
18904 static int
18905 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18906 int subr, unification_kind_t strict,
18907 bool explain_p)
18908 {
18909 tree arg_expr = NULL_TREE;
18910 int arg_strict;
18911
18912 if (arg == error_mark_node || parm == error_mark_node)
18913 return unify_invalid (explain_p);
18914 if (arg == unknown_type_node)
18915 /* We can't deduce anything from this, but we might get all the
18916 template args from other function args. */
18917 return unify_success (explain_p);
18918
18919 /* Implicit conversions (Clause 4) will be performed on a function
18920 argument to convert it to the type of the corresponding function
18921 parameter if the parameter type contains no template-parameters that
18922 participate in template argument deduction. */
18923 if (strict != DEDUCE_EXACT
18924 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18925 /* For function parameters with no deducible template parameters,
18926 just return. We'll check non-dependent conversions later. */
18927 return unify_success (explain_p);
18928
18929 switch (strict)
18930 {
18931 case DEDUCE_CALL:
18932 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18933 | UNIFY_ALLOW_MORE_CV_QUAL
18934 | UNIFY_ALLOW_DERIVED);
18935 break;
18936
18937 case DEDUCE_CONV:
18938 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18939 break;
18940
18941 case DEDUCE_EXACT:
18942 arg_strict = UNIFY_ALLOW_NONE;
18943 break;
18944
18945 default:
18946 gcc_unreachable ();
18947 }
18948
18949 /* We only do these transformations if this is the top-level
18950 parameter_type_list in a call or declaration matching; in other
18951 situations (nested function declarators, template argument lists) we
18952 won't be comparing a type to an expression, and we don't do any type
18953 adjustments. */
18954 if (!subr)
18955 {
18956 if (!TYPE_P (arg))
18957 {
18958 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18959 if (type_unknown_p (arg))
18960 {
18961 /* [temp.deduct.type] A template-argument can be
18962 deduced from a pointer to function or pointer
18963 to member function argument if the set of
18964 overloaded functions does not contain function
18965 templates and at most one of a set of
18966 overloaded functions provides a unique
18967 match. */
18968
18969 if (resolve_overloaded_unification
18970 (tparms, targs, parm, arg, strict,
18971 arg_strict, explain_p))
18972 return unify_success (explain_p);
18973 return unify_overload_resolution_failure (explain_p, arg);
18974 }
18975
18976 arg_expr = arg;
18977 arg = unlowered_expr_type (arg);
18978 if (arg == error_mark_node)
18979 return unify_invalid (explain_p);
18980 }
18981
18982 arg_strict |=
18983 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18984 }
18985 else
18986 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18987 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18988 return unify_template_argument_mismatch (explain_p, parm, arg);
18989
18990 /* For deduction from an init-list we need the actual list. */
18991 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18992 arg = arg_expr;
18993 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18994 }
18995
18996 /* for_each_template_parm callback that always returns 0. */
18997
18998 static int
18999 zero_r (tree, void *)
19000 {
19001 return 0;
19002 }
19003
19004 /* for_each_template_parm any_fn callback to handle deduction of a template
19005 type argument from the type of an array bound. */
19006
19007 static int
19008 array_deduction_r (tree t, void *data)
19009 {
19010 tree_pair_p d = (tree_pair_p)data;
19011 tree &tparms = d->purpose;
19012 tree &targs = d->value;
19013
19014 if (TREE_CODE (t) == ARRAY_TYPE)
19015 if (tree dom = TYPE_DOMAIN (t))
19016 if (tree max = TYPE_MAX_VALUE (dom))
19017 {
19018 if (TREE_CODE (max) == MINUS_EXPR)
19019 max = TREE_OPERAND (max, 0);
19020 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19021 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19022 UNIFY_ALLOW_NONE, /*explain*/false);
19023 }
19024
19025 /* Keep walking. */
19026 return 0;
19027 }
19028
19029 /* Try to deduce any not-yet-deduced template type arguments from the type of
19030 an array bound. This is handled separately from unify because 14.8.2.5 says
19031 "The type of a type parameter is only deduced from an array bound if it is
19032 not otherwise deduced." */
19033
19034 static void
19035 try_array_deduction (tree tparms, tree targs, tree parm)
19036 {
19037 tree_pair_s data = { tparms, targs };
19038 hash_set<tree> visited;
19039 for_each_template_parm (parm, zero_r, &data, &visited,
19040 /*nondeduced*/false, array_deduction_r);
19041 }
19042
19043 /* Most parms like fn_type_unification.
19044
19045 If SUBR is 1, we're being called recursively (to unify the
19046 arguments of a function or method parameter of a function
19047 template).
19048
19049 CHECKS is a pointer to a vector of access checks encountered while
19050 substituting default template arguments. */
19051
19052 static int
19053 type_unification_real (tree tparms,
19054 tree full_targs,
19055 tree xparms,
19056 const tree *xargs,
19057 unsigned int xnargs,
19058 int subr,
19059 unification_kind_t strict,
19060 int flags,
19061 vec<deferred_access_check, va_gc> **checks,
19062 bool explain_p)
19063 {
19064 tree parm, arg;
19065 int i;
19066 int ntparms = TREE_VEC_LENGTH (tparms);
19067 int saw_undeduced = 0;
19068 tree parms;
19069 const tree *args;
19070 unsigned int nargs;
19071 unsigned int ia;
19072
19073 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19074 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19075 gcc_assert (ntparms > 0);
19076
19077 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19078
19079 /* Reset the number of non-defaulted template arguments contained
19080 in TARGS. */
19081 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19082
19083 again:
19084 parms = xparms;
19085 args = xargs;
19086 nargs = xnargs;
19087
19088 ia = 0;
19089 while (parms && parms != void_list_node
19090 && ia < nargs)
19091 {
19092 parm = TREE_VALUE (parms);
19093
19094 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19095 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19096 /* For a function parameter pack that occurs at the end of the
19097 parameter-declaration-list, the type A of each remaining
19098 argument of the call is compared with the type P of the
19099 declarator-id of the function parameter pack. */
19100 break;
19101
19102 parms = TREE_CHAIN (parms);
19103
19104 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19105 /* For a function parameter pack that does not occur at the
19106 end of the parameter-declaration-list, the type of the
19107 parameter pack is a non-deduced context. */
19108 continue;
19109
19110 arg = args[ia];
19111 ++ia;
19112
19113 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19114 explain_p))
19115 return 1;
19116 }
19117
19118 if (parms
19119 && parms != void_list_node
19120 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19121 {
19122 /* Unify the remaining arguments with the pack expansion type. */
19123 tree argvec;
19124 tree parmvec = make_tree_vec (1);
19125
19126 /* Allocate a TREE_VEC and copy in all of the arguments */
19127 argvec = make_tree_vec (nargs - ia);
19128 for (i = 0; ia < nargs; ++ia, ++i)
19129 TREE_VEC_ELT (argvec, i) = args[ia];
19130
19131 /* Copy the parameter into parmvec. */
19132 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19133 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19134 /*subr=*/subr, explain_p))
19135 return 1;
19136
19137 /* Advance to the end of the list of parameters. */
19138 parms = TREE_CHAIN (parms);
19139 }
19140
19141 /* Fail if we've reached the end of the parm list, and more args
19142 are present, and the parm list isn't variadic. */
19143 if (ia < nargs && parms == void_list_node)
19144 return unify_too_many_arguments (explain_p, nargs, ia);
19145 /* Fail if parms are left and they don't have default values and
19146 they aren't all deduced as empty packs (c++/57397). This is
19147 consistent with sufficient_parms_p. */
19148 if (parms && parms != void_list_node
19149 && TREE_PURPOSE (parms) == NULL_TREE)
19150 {
19151 unsigned int count = nargs;
19152 tree p = parms;
19153 bool type_pack_p;
19154 do
19155 {
19156 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19157 if (!type_pack_p)
19158 count++;
19159 p = TREE_CHAIN (p);
19160 }
19161 while (p && p != void_list_node);
19162 if (count != nargs)
19163 return unify_too_few_arguments (explain_p, ia, count,
19164 type_pack_p);
19165 }
19166
19167 if (!subr)
19168 {
19169 tsubst_flags_t complain = (explain_p
19170 ? tf_warning_or_error
19171 : tf_none);
19172 bool tried_array_deduction = (cxx_dialect < cxx1z);
19173
19174 for (i = 0; i < ntparms; i++)
19175 {
19176 tree targ = TREE_VEC_ELT (targs, i);
19177 tree tparm = TREE_VEC_ELT (tparms, i);
19178
19179 /* Clear the "incomplete" flags on all argument packs now so that
19180 substituting them into later default arguments works. */
19181 if (targ && ARGUMENT_PACK_P (targ))
19182 {
19183 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19184 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19185 }
19186
19187 if (targ || tparm == error_mark_node)
19188 continue;
19189 tparm = TREE_VALUE (tparm);
19190
19191 if (TREE_CODE (tparm) == TYPE_DECL
19192 && !tried_array_deduction)
19193 {
19194 try_array_deduction (tparms, targs, xparms);
19195 tried_array_deduction = true;
19196 if (TREE_VEC_ELT (targs, i))
19197 continue;
19198 }
19199
19200 /* If this is an undeduced nontype parameter that depends on
19201 a type parameter, try another pass; its type may have been
19202 deduced from a later argument than the one from which
19203 this parameter can be deduced. */
19204 if (TREE_CODE (tparm) == PARM_DECL
19205 && uses_template_parms (TREE_TYPE (tparm))
19206 && saw_undeduced < 2)
19207 {
19208 saw_undeduced = 1;
19209 continue;
19210 }
19211
19212 /* Core issue #226 (C++0x) [temp.deduct]:
19213
19214 If a template argument has not been deduced, its
19215 default template argument, if any, is used.
19216
19217 When we are in C++98 mode, TREE_PURPOSE will either
19218 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19219 to explicitly check cxx_dialect here. */
19220 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19221 /* OK, there is a default argument. Wait until after the
19222 conversion check to do substitution. */
19223 continue;
19224
19225 /* If the type parameter is a parameter pack, then it will
19226 be deduced to an empty parameter pack. */
19227 if (template_parameter_pack_p (tparm))
19228 {
19229 tree arg;
19230
19231 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19232 {
19233 arg = make_node (NONTYPE_ARGUMENT_PACK);
19234 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
19235 TREE_CONSTANT (arg) = 1;
19236 }
19237 else
19238 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19239
19240 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19241
19242 TREE_VEC_ELT (targs, i) = arg;
19243 continue;
19244 }
19245
19246 return unify_parameter_deduction_failure (explain_p, tparm);
19247 }
19248
19249 /* DR 1391: All parameters have args, now check non-dependent parms for
19250 convertibility. */
19251 if (saw_undeduced < 2)
19252 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19253 parms && parms != void_list_node && ia < nargs; )
19254 {
19255 parm = TREE_VALUE (parms);
19256
19257 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19258 && (!TREE_CHAIN (parms)
19259 || TREE_CHAIN (parms) == void_list_node))
19260 /* For a function parameter pack that occurs at the end of the
19261 parameter-declaration-list, the type A of each remaining
19262 argument of the call is compared with the type P of the
19263 declarator-id of the function parameter pack. */
19264 break;
19265
19266 parms = TREE_CHAIN (parms);
19267
19268 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19269 /* For a function parameter pack that does not occur at the
19270 end of the parameter-declaration-list, the type of the
19271 parameter pack is a non-deduced context. */
19272 continue;
19273
19274 arg = args[ia];
19275 ++ia;
19276
19277 if (uses_template_parms (parm))
19278 continue;
19279 if (check_non_deducible_conversion (parm, arg, strict, flags,
19280 explain_p))
19281 return 1;
19282 }
19283
19284 /* Now substitute into the default template arguments. */
19285 for (i = 0; i < ntparms; i++)
19286 {
19287 tree targ = TREE_VEC_ELT (targs, i);
19288 tree tparm = TREE_VEC_ELT (tparms, i);
19289
19290 if (targ || tparm == error_mark_node)
19291 continue;
19292 tree parm = TREE_VALUE (tparm);
19293
19294 if (TREE_CODE (parm) == PARM_DECL
19295 && uses_template_parms (TREE_TYPE (parm))
19296 && saw_undeduced < 2)
19297 continue;
19298
19299 tree arg = TREE_PURPOSE (tparm);
19300 reopen_deferring_access_checks (*checks);
19301 location_t save_loc = input_location;
19302 if (DECL_P (parm))
19303 input_location = DECL_SOURCE_LOCATION (parm);
19304 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19305 if (!uses_template_parms (arg))
19306 arg = convert_template_argument (parm, arg, full_targs, complain,
19307 i, NULL_TREE);
19308 else if (saw_undeduced < 2)
19309 arg = NULL_TREE;
19310 else
19311 arg = error_mark_node;
19312 input_location = save_loc;
19313 *checks = get_deferred_access_checks ();
19314 pop_deferring_access_checks ();
19315 if (arg == error_mark_node)
19316 return 1;
19317 else if (arg)
19318 {
19319 TREE_VEC_ELT (targs, i) = arg;
19320 /* The position of the first default template argument,
19321 is also the number of non-defaulted arguments in TARGS.
19322 Record that. */
19323 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19324 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19325 }
19326 }
19327
19328 if (saw_undeduced++ == 1)
19329 goto again;
19330 }
19331
19332 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19333 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19334
19335 return unify_success (explain_p);
19336 }
19337
19338 /* Subroutine of type_unification_real. Args are like the variables
19339 at the call site. ARG is an overloaded function (or template-id);
19340 we try deducing template args from each of the overloads, and if
19341 only one succeeds, we go with that. Modifies TARGS and returns
19342 true on success. */
19343
19344 static bool
19345 resolve_overloaded_unification (tree tparms,
19346 tree targs,
19347 tree parm,
19348 tree arg,
19349 unification_kind_t strict,
19350 int sub_strict,
19351 bool explain_p)
19352 {
19353 tree tempargs = copy_node (targs);
19354 int good = 0;
19355 tree goodfn = NULL_TREE;
19356 bool addr_p;
19357
19358 if (TREE_CODE (arg) == ADDR_EXPR)
19359 {
19360 arg = TREE_OPERAND (arg, 0);
19361 addr_p = true;
19362 }
19363 else
19364 addr_p = false;
19365
19366 if (TREE_CODE (arg) == COMPONENT_REF)
19367 /* Handle `&x' where `x' is some static or non-static member
19368 function name. */
19369 arg = TREE_OPERAND (arg, 1);
19370
19371 if (TREE_CODE (arg) == OFFSET_REF)
19372 arg = TREE_OPERAND (arg, 1);
19373
19374 /* Strip baselink information. */
19375 if (BASELINK_P (arg))
19376 arg = BASELINK_FUNCTIONS (arg);
19377
19378 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19379 {
19380 /* If we got some explicit template args, we need to plug them into
19381 the affected templates before we try to unify, in case the
19382 explicit args will completely resolve the templates in question. */
19383
19384 int ok = 0;
19385 tree expl_subargs = TREE_OPERAND (arg, 1);
19386 arg = TREE_OPERAND (arg, 0);
19387
19388 for (; arg; arg = OVL_NEXT (arg))
19389 {
19390 tree fn = OVL_CURRENT (arg);
19391 tree subargs, elem;
19392
19393 if (TREE_CODE (fn) != TEMPLATE_DECL)
19394 continue;
19395
19396 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19397 expl_subargs, NULL_TREE, tf_none,
19398 /*require_all_args=*/true,
19399 /*use_default_args=*/true);
19400 if (subargs != error_mark_node
19401 && !any_dependent_template_arguments_p (subargs))
19402 {
19403 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19404 if (try_one_overload (tparms, targs, tempargs, parm,
19405 elem, strict, sub_strict, addr_p, explain_p)
19406 && (!goodfn || !same_type_p (goodfn, elem)))
19407 {
19408 goodfn = elem;
19409 ++good;
19410 }
19411 }
19412 else if (subargs)
19413 ++ok;
19414 }
19415 /* If no templates (or more than one) are fully resolved by the
19416 explicit arguments, this template-id is a non-deduced context; it
19417 could still be OK if we deduce all template arguments for the
19418 enclosing call through other arguments. */
19419 if (good != 1)
19420 good = ok;
19421 }
19422 else if (TREE_CODE (arg) != OVERLOAD
19423 && TREE_CODE (arg) != FUNCTION_DECL)
19424 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19425 -- but the deduction does not succeed because the expression is
19426 not just the function on its own. */
19427 return false;
19428 else
19429 for (; arg; arg = OVL_NEXT (arg))
19430 if (try_one_overload (tparms, targs, tempargs, parm,
19431 TREE_TYPE (OVL_CURRENT (arg)),
19432 strict, sub_strict, addr_p, explain_p)
19433 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
19434 {
19435 goodfn = OVL_CURRENT (arg);
19436 ++good;
19437 }
19438
19439 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19440 to function or pointer to member function argument if the set of
19441 overloaded functions does not contain function templates and at most
19442 one of a set of overloaded functions provides a unique match.
19443
19444 So if we found multiple possibilities, we return success but don't
19445 deduce anything. */
19446
19447 if (good == 1)
19448 {
19449 int i = TREE_VEC_LENGTH (targs);
19450 for (; i--; )
19451 if (TREE_VEC_ELT (tempargs, i))
19452 {
19453 tree old = TREE_VEC_ELT (targs, i);
19454 tree new_ = TREE_VEC_ELT (tempargs, i);
19455 if (new_ && old && ARGUMENT_PACK_P (old)
19456 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19457 /* Don't forget explicit template arguments in a pack. */
19458 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19459 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19460 TREE_VEC_ELT (targs, i) = new_;
19461 }
19462 }
19463 if (good)
19464 return true;
19465
19466 return false;
19467 }
19468
19469 /* Core DR 115: In contexts where deduction is done and fails, or in
19470 contexts where deduction is not done, if a template argument list is
19471 specified and it, along with any default template arguments, identifies
19472 a single function template specialization, then the template-id is an
19473 lvalue for the function template specialization. */
19474
19475 tree
19476 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19477 {
19478 tree expr, offset, baselink;
19479 bool addr;
19480
19481 if (!type_unknown_p (orig_expr))
19482 return orig_expr;
19483
19484 expr = orig_expr;
19485 addr = false;
19486 offset = NULL_TREE;
19487 baselink = NULL_TREE;
19488
19489 if (TREE_CODE (expr) == ADDR_EXPR)
19490 {
19491 expr = TREE_OPERAND (expr, 0);
19492 addr = true;
19493 }
19494 if (TREE_CODE (expr) == OFFSET_REF)
19495 {
19496 offset = expr;
19497 expr = TREE_OPERAND (expr, 1);
19498 }
19499 if (BASELINK_P (expr))
19500 {
19501 baselink = expr;
19502 expr = BASELINK_FUNCTIONS (expr);
19503 }
19504
19505 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19506 {
19507 int good = 0;
19508 tree goodfn = NULL_TREE;
19509
19510 /* If we got some explicit template args, we need to plug them into
19511 the affected templates before we try to unify, in case the
19512 explicit args will completely resolve the templates in question. */
19513
19514 tree expl_subargs = TREE_OPERAND (expr, 1);
19515 tree arg = TREE_OPERAND (expr, 0);
19516 tree badfn = NULL_TREE;
19517 tree badargs = NULL_TREE;
19518
19519 for (; arg; arg = OVL_NEXT (arg))
19520 {
19521 tree fn = OVL_CURRENT (arg);
19522 tree subargs, elem;
19523
19524 if (TREE_CODE (fn) != TEMPLATE_DECL)
19525 continue;
19526
19527 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19528 expl_subargs, NULL_TREE, tf_none,
19529 /*require_all_args=*/true,
19530 /*use_default_args=*/true);
19531 if (subargs != error_mark_node
19532 && !any_dependent_template_arguments_p (subargs))
19533 {
19534 elem = instantiate_template (fn, subargs, tf_none);
19535 if (elem == error_mark_node)
19536 {
19537 badfn = fn;
19538 badargs = subargs;
19539 }
19540 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19541 {
19542 goodfn = elem;
19543 ++good;
19544 }
19545 }
19546 }
19547 if (good == 1)
19548 {
19549 mark_used (goodfn);
19550 expr = goodfn;
19551 if (baselink)
19552 expr = build_baselink (BASELINK_BINFO (baselink),
19553 BASELINK_ACCESS_BINFO (baselink),
19554 expr, BASELINK_OPTYPE (baselink));
19555 if (offset)
19556 {
19557 tree base
19558 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19559 expr = build_offset_ref (base, expr, addr, complain);
19560 }
19561 if (addr)
19562 expr = cp_build_addr_expr (expr, complain);
19563 return expr;
19564 }
19565 else if (good == 0 && badargs && (complain & tf_error))
19566 /* There were no good options and at least one bad one, so let the
19567 user know what the problem is. */
19568 instantiate_template (badfn, badargs, complain);
19569 }
19570 return orig_expr;
19571 }
19572
19573 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19574 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19575 different overloads deduce different arguments for a given parm.
19576 ADDR_P is true if the expression for which deduction is being
19577 performed was of the form "& fn" rather than simply "fn".
19578
19579 Returns 1 on success. */
19580
19581 static int
19582 try_one_overload (tree tparms,
19583 tree orig_targs,
19584 tree targs,
19585 tree parm,
19586 tree arg,
19587 unification_kind_t strict,
19588 int sub_strict,
19589 bool addr_p,
19590 bool explain_p)
19591 {
19592 int nargs;
19593 tree tempargs;
19594 int i;
19595
19596 if (arg == error_mark_node)
19597 return 0;
19598
19599 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19600 to function or pointer to member function argument if the set of
19601 overloaded functions does not contain function templates and at most
19602 one of a set of overloaded functions provides a unique match.
19603
19604 So if this is a template, just return success. */
19605
19606 if (uses_template_parms (arg))
19607 return 1;
19608
19609 if (TREE_CODE (arg) == METHOD_TYPE)
19610 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19611 else if (addr_p)
19612 arg = build_pointer_type (arg);
19613
19614 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19615
19616 /* We don't copy orig_targs for this because if we have already deduced
19617 some template args from previous args, unify would complain when we
19618 try to deduce a template parameter for the same argument, even though
19619 there isn't really a conflict. */
19620 nargs = TREE_VEC_LENGTH (targs);
19621 tempargs = make_tree_vec (nargs);
19622
19623 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19624 return 0;
19625
19626 /* First make sure we didn't deduce anything that conflicts with
19627 explicitly specified args. */
19628 for (i = nargs; i--; )
19629 {
19630 tree elt = TREE_VEC_ELT (tempargs, i);
19631 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19632
19633 if (!elt)
19634 /*NOP*/;
19635 else if (uses_template_parms (elt))
19636 /* Since we're unifying against ourselves, we will fill in
19637 template args used in the function parm list with our own
19638 template parms. Discard them. */
19639 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19640 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19641 {
19642 /* Check that the argument at each index of the deduced argument pack
19643 is equivalent to the corresponding explicitly specified argument.
19644 We may have deduced more arguments than were explicitly specified,
19645 and that's OK. */
19646 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt));
19647 gcc_assert (ARGUMENT_PACK_ARGS (oldelt)
19648 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt));
19649
19650 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19651 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19652
19653 if (TREE_VEC_LENGTH (deduced_pack)
19654 < TREE_VEC_LENGTH (explicit_pack))
19655 return 0;
19656
19657 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19658 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19659 TREE_VEC_ELT (deduced_pack, j)))
19660 return 0;
19661 }
19662 else if (oldelt && !template_args_equal (oldelt, elt))
19663 return 0;
19664 }
19665
19666 for (i = nargs; i--; )
19667 {
19668 tree elt = TREE_VEC_ELT (tempargs, i);
19669
19670 if (elt)
19671 TREE_VEC_ELT (targs, i) = elt;
19672 }
19673
19674 return 1;
19675 }
19676
19677 /* PARM is a template class (perhaps with unbound template
19678 parameters). ARG is a fully instantiated type. If ARG can be
19679 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19680 TARGS are as for unify. */
19681
19682 static tree
19683 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19684 bool explain_p)
19685 {
19686 tree copy_of_targs;
19687
19688 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19689 return NULL_TREE;
19690 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19691 /* Matches anything. */;
19692 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19693 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19694 return NULL_TREE;
19695
19696 /* We need to make a new template argument vector for the call to
19697 unify. If we used TARGS, we'd clutter it up with the result of
19698 the attempted unification, even if this class didn't work out.
19699 We also don't want to commit ourselves to all the unifications
19700 we've already done, since unification is supposed to be done on
19701 an argument-by-argument basis. In other words, consider the
19702 following pathological case:
19703
19704 template <int I, int J, int K>
19705 struct S {};
19706
19707 template <int I, int J>
19708 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19709
19710 template <int I, int J, int K>
19711 void f(S<I, J, K>, S<I, I, I>);
19712
19713 void g() {
19714 S<0, 0, 0> s0;
19715 S<0, 1, 2> s2;
19716
19717 f(s0, s2);
19718 }
19719
19720 Now, by the time we consider the unification involving `s2', we
19721 already know that we must have `f<0, 0, 0>'. But, even though
19722 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19723 because there are two ways to unify base classes of S<0, 1, 2>
19724 with S<I, I, I>. If we kept the already deduced knowledge, we
19725 would reject the possibility I=1. */
19726 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19727
19728 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19729 {
19730 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19731 return NULL_TREE;
19732 return arg;
19733 }
19734
19735 /* If unification failed, we're done. */
19736 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19737 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19738 return NULL_TREE;
19739
19740 return arg;
19741 }
19742
19743 /* Given a template type PARM and a class type ARG, find the unique
19744 base type in ARG that is an instance of PARM. We do not examine
19745 ARG itself; only its base-classes. If there is not exactly one
19746 appropriate base class, return NULL_TREE. PARM may be the type of
19747 a partial specialization, as well as a plain template type. Used
19748 by unify. */
19749
19750 static enum template_base_result
19751 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19752 bool explain_p, tree *result)
19753 {
19754 tree rval = NULL_TREE;
19755 tree binfo;
19756
19757 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19758
19759 binfo = TYPE_BINFO (complete_type (arg));
19760 if (!binfo)
19761 {
19762 /* The type could not be completed. */
19763 *result = NULL_TREE;
19764 return tbr_incomplete_type;
19765 }
19766
19767 /* Walk in inheritance graph order. The search order is not
19768 important, and this avoids multiple walks of virtual bases. */
19769 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19770 {
19771 tree r = try_class_unification (tparms, targs, parm,
19772 BINFO_TYPE (binfo), explain_p);
19773
19774 if (r)
19775 {
19776 /* If there is more than one satisfactory baseclass, then:
19777
19778 [temp.deduct.call]
19779
19780 If they yield more than one possible deduced A, the type
19781 deduction fails.
19782
19783 applies. */
19784 if (rval && !same_type_p (r, rval))
19785 {
19786 *result = NULL_TREE;
19787 return tbr_ambiguous_baseclass;
19788 }
19789
19790 rval = r;
19791 }
19792 }
19793
19794 *result = rval;
19795 return tbr_success;
19796 }
19797
19798 /* Returns the level of DECL, which declares a template parameter. */
19799
19800 static int
19801 template_decl_level (tree decl)
19802 {
19803 switch (TREE_CODE (decl))
19804 {
19805 case TYPE_DECL:
19806 case TEMPLATE_DECL:
19807 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19808
19809 case PARM_DECL:
19810 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19811
19812 default:
19813 gcc_unreachable ();
19814 }
19815 return 0;
19816 }
19817
19818 /* Decide whether ARG can be unified with PARM, considering only the
19819 cv-qualifiers of each type, given STRICT as documented for unify.
19820 Returns nonzero iff the unification is OK on that basis. */
19821
19822 static int
19823 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19824 {
19825 int arg_quals = cp_type_quals (arg);
19826 int parm_quals = cp_type_quals (parm);
19827
19828 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19829 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19830 {
19831 /* Although a CVR qualifier is ignored when being applied to a
19832 substituted template parameter ([8.3.2]/1 for example), that
19833 does not allow us to unify "const T" with "int&" because both
19834 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19835 It is ok when we're allowing additional CV qualifiers
19836 at the outer level [14.8.2.1]/3,1st bullet. */
19837 if ((TREE_CODE (arg) == REFERENCE_TYPE
19838 || TREE_CODE (arg) == FUNCTION_TYPE
19839 || TREE_CODE (arg) == METHOD_TYPE)
19840 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19841 return 0;
19842
19843 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19844 && (parm_quals & TYPE_QUAL_RESTRICT))
19845 return 0;
19846 }
19847
19848 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19849 && (arg_quals & parm_quals) != parm_quals)
19850 return 0;
19851
19852 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19853 && (parm_quals & arg_quals) != arg_quals)
19854 return 0;
19855
19856 return 1;
19857 }
19858
19859 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19860 void
19861 template_parm_level_and_index (tree parm, int* level, int* index)
19862 {
19863 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19864 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19865 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19866 {
19867 *index = TEMPLATE_TYPE_IDX (parm);
19868 *level = TEMPLATE_TYPE_LEVEL (parm);
19869 }
19870 else
19871 {
19872 *index = TEMPLATE_PARM_IDX (parm);
19873 *level = TEMPLATE_PARM_LEVEL (parm);
19874 }
19875 }
19876
19877 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19878 do { \
19879 if (unify (TP, TA, P, A, S, EP)) \
19880 return 1; \
19881 } while (0)
19882
19883 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19884 expansion at the end of PACKED_PARMS. Returns 0 if the type
19885 deduction succeeds, 1 otherwise. STRICT is the same as in
19886 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19887 function call argument list. We'll need to adjust the arguments to make them
19888 types. SUBR tells us if this is from a recursive call to
19889 type_unification_real, or for comparing two template argument
19890 lists. */
19891
19892 static int
19893 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19894 tree packed_args, unification_kind_t strict,
19895 bool subr, bool explain_p)
19896 {
19897 tree parm
19898 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19899 tree pattern = PACK_EXPANSION_PATTERN (parm);
19900 tree pack, packs = NULL_TREE;
19901 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19902
19903 packed_args = expand_template_argument_pack (packed_args);
19904
19905 int len = TREE_VEC_LENGTH (packed_args);
19906
19907 /* Determine the parameter packs we will be deducing from the
19908 pattern, and record their current deductions. */
19909 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19910 pack; pack = TREE_CHAIN (pack))
19911 {
19912 tree parm_pack = TREE_VALUE (pack);
19913 int idx, level;
19914
19915 /* Determine the index and level of this parameter pack. */
19916 template_parm_level_and_index (parm_pack, &level, &idx);
19917
19918 /* Keep track of the parameter packs and their corresponding
19919 argument packs. */
19920 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19921 TREE_TYPE (packs) = make_tree_vec (len - start);
19922 }
19923
19924 /* Loop through all of the arguments that have not yet been
19925 unified and unify each with the pattern. */
19926 for (i = start; i < len; i++)
19927 {
19928 tree parm;
19929 bool any_explicit = false;
19930 tree arg = TREE_VEC_ELT (packed_args, i);
19931
19932 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19933 or the element of its argument pack at the current index if
19934 this argument was explicitly specified. */
19935 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19936 {
19937 int idx, level;
19938 tree arg, pargs;
19939 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19940
19941 arg = NULL_TREE;
19942 if (TREE_VALUE (pack)
19943 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19944 && (i - start < TREE_VEC_LENGTH (pargs)))
19945 {
19946 any_explicit = true;
19947 arg = TREE_VEC_ELT (pargs, i - start);
19948 }
19949 TMPL_ARG (targs, level, idx) = arg;
19950 }
19951
19952 /* If we had explicit template arguments, substitute them into the
19953 pattern before deduction. */
19954 if (any_explicit)
19955 {
19956 /* Some arguments might still be unspecified or dependent. */
19957 bool dependent;
19958 ++processing_template_decl;
19959 dependent = any_dependent_template_arguments_p (targs);
19960 if (!dependent)
19961 --processing_template_decl;
19962 parm = tsubst (pattern, targs,
19963 explain_p ? tf_warning_or_error : tf_none,
19964 NULL_TREE);
19965 if (dependent)
19966 --processing_template_decl;
19967 if (parm == error_mark_node)
19968 return 1;
19969 }
19970 else
19971 parm = pattern;
19972
19973 /* Unify the pattern with the current argument. */
19974 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
19975 explain_p))
19976 return 1;
19977
19978 /* For each parameter pack, collect the deduced value. */
19979 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19980 {
19981 int idx, level;
19982 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19983
19984 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
19985 TMPL_ARG (targs, level, idx);
19986 }
19987 }
19988
19989 /* Verify that the results of unification with the parameter packs
19990 produce results consistent with what we've seen before, and make
19991 the deduced argument packs available. */
19992 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19993 {
19994 tree old_pack = TREE_VALUE (pack);
19995 tree new_args = TREE_TYPE (pack);
19996 int i, len = TREE_VEC_LENGTH (new_args);
19997 int idx, level;
19998 bool nondeduced_p = false;
19999
20000 /* By default keep the original deduced argument pack.
20001 If necessary, more specific code is going to update the
20002 resulting deduced argument later down in this function. */
20003 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20004 TMPL_ARG (targs, level, idx) = old_pack;
20005
20006 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20007 actually deduce anything. */
20008 for (i = 0; i < len && !nondeduced_p; ++i)
20009 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20010 nondeduced_p = true;
20011 if (nondeduced_p)
20012 continue;
20013
20014 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20015 {
20016 /* If we had fewer function args than explicit template args,
20017 just use the explicits. */
20018 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20019 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20020 if (len < explicit_len)
20021 new_args = explicit_args;
20022 }
20023
20024 if (!old_pack)
20025 {
20026 tree result;
20027 /* Build the deduced *_ARGUMENT_PACK. */
20028 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20029 {
20030 result = make_node (NONTYPE_ARGUMENT_PACK);
20031 TREE_TYPE (result) =
20032 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
20033 TREE_CONSTANT (result) = 1;
20034 }
20035 else
20036 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20037
20038 SET_ARGUMENT_PACK_ARGS (result, new_args);
20039
20040 /* Note the deduced argument packs for this parameter
20041 pack. */
20042 TMPL_ARG (targs, level, idx) = result;
20043 }
20044 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20045 && (ARGUMENT_PACK_ARGS (old_pack)
20046 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20047 {
20048 /* We only had the explicitly-provided arguments before, but
20049 now we have a complete set of arguments. */
20050 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20051
20052 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20053 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20054 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20055 }
20056 else
20057 {
20058 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20059 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20060
20061 if (!comp_template_args (old_args, new_args,
20062 &bad_old_arg, &bad_new_arg))
20063 /* Inconsistent unification of this parameter pack. */
20064 return unify_parameter_pack_inconsistent (explain_p,
20065 bad_old_arg,
20066 bad_new_arg);
20067 }
20068 }
20069
20070 return unify_success (explain_p);
20071 }
20072
20073 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20074 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20075 parameters and return value are as for unify. */
20076
20077 static int
20078 unify_array_domain (tree tparms, tree targs,
20079 tree parm_dom, tree arg_dom,
20080 bool explain_p)
20081 {
20082 tree parm_max;
20083 tree arg_max;
20084 bool parm_cst;
20085 bool arg_cst;
20086
20087 /* Our representation of array types uses "N - 1" as the
20088 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20089 not an integer constant. We cannot unify arbitrarily
20090 complex expressions, so we eliminate the MINUS_EXPRs
20091 here. */
20092 parm_max = TYPE_MAX_VALUE (parm_dom);
20093 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20094 if (!parm_cst)
20095 {
20096 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20097 parm_max = TREE_OPERAND (parm_max, 0);
20098 }
20099 arg_max = TYPE_MAX_VALUE (arg_dom);
20100 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20101 if (!arg_cst)
20102 {
20103 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20104 trying to unify the type of a variable with the type
20105 of a template parameter. For example:
20106
20107 template <unsigned int N>
20108 void f (char (&) [N]);
20109 int g();
20110 void h(int i) {
20111 char a[g(i)];
20112 f(a);
20113 }
20114
20115 Here, the type of the ARG will be "int [g(i)]", and
20116 may be a SAVE_EXPR, etc. */
20117 if (TREE_CODE (arg_max) != MINUS_EXPR)
20118 return unify_vla_arg (explain_p, arg_dom);
20119 arg_max = TREE_OPERAND (arg_max, 0);
20120 }
20121
20122 /* If only one of the bounds used a MINUS_EXPR, compensate
20123 by adding one to the other bound. */
20124 if (parm_cst && !arg_cst)
20125 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20126 integer_type_node,
20127 parm_max,
20128 integer_one_node);
20129 else if (arg_cst && !parm_cst)
20130 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20131 integer_type_node,
20132 arg_max,
20133 integer_one_node);
20134
20135 return unify (tparms, targs, parm_max, arg_max,
20136 UNIFY_ALLOW_INTEGER, explain_p);
20137 }
20138
20139 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20140
20141 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20142
20143 static pa_kind_t
20144 pa_kind (tree t)
20145 {
20146 if (PACK_EXPANSION_P (t))
20147 t = PACK_EXPANSION_PATTERN (t);
20148 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20149 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20150 || DECL_TYPE_TEMPLATE_P (t))
20151 return pa_tmpl;
20152 else if (TYPE_P (t))
20153 return pa_type;
20154 else
20155 return pa_expr;
20156 }
20157
20158 /* Deduce the value of template parameters. TPARMS is the (innermost)
20159 set of template parameters to a template. TARGS is the bindings
20160 for those template parameters, as determined thus far; TARGS may
20161 include template arguments for outer levels of template parameters
20162 as well. PARM is a parameter to a template function, or a
20163 subcomponent of that parameter; ARG is the corresponding argument.
20164 This function attempts to match PARM with ARG in a manner
20165 consistent with the existing assignments in TARGS. If more values
20166 are deduced, then TARGS is updated.
20167
20168 Returns 0 if the type deduction succeeds, 1 otherwise. The
20169 parameter STRICT is a bitwise or of the following flags:
20170
20171 UNIFY_ALLOW_NONE:
20172 Require an exact match between PARM and ARG.
20173 UNIFY_ALLOW_MORE_CV_QUAL:
20174 Allow the deduced ARG to be more cv-qualified (by qualification
20175 conversion) than ARG.
20176 UNIFY_ALLOW_LESS_CV_QUAL:
20177 Allow the deduced ARG to be less cv-qualified than ARG.
20178 UNIFY_ALLOW_DERIVED:
20179 Allow the deduced ARG to be a template base class of ARG,
20180 or a pointer to a template base class of the type pointed to by
20181 ARG.
20182 UNIFY_ALLOW_INTEGER:
20183 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20184 case for more information.
20185 UNIFY_ALLOW_OUTER_LEVEL:
20186 This is the outermost level of a deduction. Used to determine validity
20187 of qualification conversions. A valid qualification conversion must
20188 have const qualified pointers leading up to the inner type which
20189 requires additional CV quals, except at the outer level, where const
20190 is not required [conv.qual]. It would be normal to set this flag in
20191 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20192 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20193 This is the outermost level of a deduction, and PARM can be more CV
20194 qualified at this point.
20195 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20196 This is the outermost level of a deduction, and PARM can be less CV
20197 qualified at this point. */
20198
20199 static int
20200 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20201 bool explain_p)
20202 {
20203 int idx;
20204 tree targ;
20205 tree tparm;
20206 int strict_in = strict;
20207 tsubst_flags_t complain = (explain_p
20208 ? tf_warning_or_error
20209 : tf_none);
20210
20211 /* I don't think this will do the right thing with respect to types.
20212 But the only case I've seen it in so far has been array bounds, where
20213 signedness is the only information lost, and I think that will be
20214 okay. */
20215 while (CONVERT_EXPR_P (parm))
20216 parm = TREE_OPERAND (parm, 0);
20217
20218 if (arg == error_mark_node)
20219 return unify_invalid (explain_p);
20220 if (arg == unknown_type_node
20221 || arg == init_list_type_node)
20222 /* We can't deduce anything from this, but we might get all the
20223 template args from other function args. */
20224 return unify_success (explain_p);
20225
20226 if (parm == any_targ_node || arg == any_targ_node)
20227 return unify_success (explain_p);
20228
20229 /* If PARM uses template parameters, then we can't bail out here,
20230 even if ARG == PARM, since we won't record unifications for the
20231 template parameters. We might need them if we're trying to
20232 figure out which of two things is more specialized. */
20233 if (arg == parm && !uses_template_parms (parm))
20234 return unify_success (explain_p);
20235
20236 /* Handle init lists early, so the rest of the function can assume
20237 we're dealing with a type. */
20238 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20239 {
20240 tree elt, elttype;
20241 unsigned i;
20242 tree orig_parm = parm;
20243
20244 /* Replace T with std::initializer_list<T> for deduction. */
20245 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20246 && flag_deduce_init_list)
20247 parm = listify (parm);
20248
20249 if (!is_std_init_list (parm)
20250 && TREE_CODE (parm) != ARRAY_TYPE)
20251 /* We can only deduce from an initializer list argument if the
20252 parameter is std::initializer_list or an array; otherwise this
20253 is a non-deduced context. */
20254 return unify_success (explain_p);
20255
20256 if (TREE_CODE (parm) == ARRAY_TYPE)
20257 elttype = TREE_TYPE (parm);
20258 else
20259 {
20260 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20261 /* Deduction is defined in terms of a single type, so just punt
20262 on the (bizarre) std::initializer_list<T...>. */
20263 if (PACK_EXPANSION_P (elttype))
20264 return unify_success (explain_p);
20265 }
20266
20267 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20268 {
20269 int elt_strict = strict;
20270
20271 if (elt == error_mark_node)
20272 return unify_invalid (explain_p);
20273
20274 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20275 {
20276 tree type = TREE_TYPE (elt);
20277 if (type == error_mark_node)
20278 return unify_invalid (explain_p);
20279 /* It should only be possible to get here for a call. */
20280 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20281 elt_strict |= maybe_adjust_types_for_deduction
20282 (DEDUCE_CALL, &elttype, &type, elt);
20283 elt = type;
20284 }
20285
20286 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20287 explain_p);
20288 }
20289
20290 if (TREE_CODE (parm) == ARRAY_TYPE
20291 && deducible_array_bound (TYPE_DOMAIN (parm)))
20292 {
20293 /* Also deduce from the length of the initializer list. */
20294 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20295 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20296 if (idx == error_mark_node)
20297 return unify_invalid (explain_p);
20298 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20299 idx, explain_p);
20300 }
20301
20302 /* If the std::initializer_list<T> deduction worked, replace the
20303 deduced A with std::initializer_list<A>. */
20304 if (orig_parm != parm)
20305 {
20306 idx = TEMPLATE_TYPE_IDX (orig_parm);
20307 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20308 targ = listify (targ);
20309 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20310 }
20311 return unify_success (explain_p);
20312 }
20313
20314 /* If parm and arg aren't the same kind of thing (template, type, or
20315 expression), fail early. */
20316 if (pa_kind (parm) != pa_kind (arg))
20317 return unify_invalid (explain_p);
20318
20319 /* Immediately reject some pairs that won't unify because of
20320 cv-qualification mismatches. */
20321 if (TREE_CODE (arg) == TREE_CODE (parm)
20322 && TYPE_P (arg)
20323 /* It is the elements of the array which hold the cv quals of an array
20324 type, and the elements might be template type parms. We'll check
20325 when we recurse. */
20326 && TREE_CODE (arg) != ARRAY_TYPE
20327 /* We check the cv-qualifiers when unifying with template type
20328 parameters below. We want to allow ARG `const T' to unify with
20329 PARM `T' for example, when computing which of two templates
20330 is more specialized, for example. */
20331 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20332 && !check_cv_quals_for_unify (strict_in, arg, parm))
20333 return unify_cv_qual_mismatch (explain_p, parm, arg);
20334
20335 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20336 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20337 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20338 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20339 strict &= ~UNIFY_ALLOW_DERIVED;
20340 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20341 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20342
20343 switch (TREE_CODE (parm))
20344 {
20345 case TYPENAME_TYPE:
20346 case SCOPE_REF:
20347 case UNBOUND_CLASS_TEMPLATE:
20348 /* In a type which contains a nested-name-specifier, template
20349 argument values cannot be deduced for template parameters used
20350 within the nested-name-specifier. */
20351 return unify_success (explain_p);
20352
20353 case TEMPLATE_TYPE_PARM:
20354 case TEMPLATE_TEMPLATE_PARM:
20355 case BOUND_TEMPLATE_TEMPLATE_PARM:
20356 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20357 if (error_operand_p (tparm))
20358 return unify_invalid (explain_p);
20359
20360 if (TEMPLATE_TYPE_LEVEL (parm)
20361 != template_decl_level (tparm))
20362 /* The PARM is not one we're trying to unify. Just check
20363 to see if it matches ARG. */
20364 {
20365 if (TREE_CODE (arg) == TREE_CODE (parm)
20366 && (is_auto (parm) ? is_auto (arg)
20367 : same_type_p (parm, arg)))
20368 return unify_success (explain_p);
20369 else
20370 return unify_type_mismatch (explain_p, parm, arg);
20371 }
20372 idx = TEMPLATE_TYPE_IDX (parm);
20373 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20374 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20375 if (error_operand_p (tparm))
20376 return unify_invalid (explain_p);
20377
20378 /* Check for mixed types and values. */
20379 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20380 && TREE_CODE (tparm) != TYPE_DECL)
20381 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20382 && TREE_CODE (tparm) != TEMPLATE_DECL))
20383 gcc_unreachable ();
20384
20385 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20386 {
20387 if ((strict_in & UNIFY_ALLOW_DERIVED)
20388 && CLASS_TYPE_P (arg))
20389 {
20390 /* First try to match ARG directly. */
20391 tree t = try_class_unification (tparms, targs, parm, arg,
20392 explain_p);
20393 if (!t)
20394 {
20395 /* Otherwise, look for a suitable base of ARG, as below. */
20396 enum template_base_result r;
20397 r = get_template_base (tparms, targs, parm, arg,
20398 explain_p, &t);
20399 if (!t)
20400 return unify_no_common_base (explain_p, r, parm, arg);
20401 arg = t;
20402 }
20403 }
20404 /* ARG must be constructed from a template class or a template
20405 template parameter. */
20406 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20407 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20408 return unify_template_deduction_failure (explain_p, parm, arg);
20409
20410 /* Deduce arguments T, i from TT<T> or TT<i>. */
20411 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20412 return 1;
20413
20414 arg = TYPE_TI_TEMPLATE (arg);
20415
20416 /* Fall through to deduce template name. */
20417 }
20418
20419 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20420 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20421 {
20422 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20423
20424 /* Simple cases: Value already set, does match or doesn't. */
20425 if (targ != NULL_TREE && template_args_equal (targ, arg))
20426 return unify_success (explain_p);
20427 else if (targ)
20428 return unify_inconsistency (explain_p, parm, targ, arg);
20429 }
20430 else
20431 {
20432 /* If PARM is `const T' and ARG is only `int', we don't have
20433 a match unless we are allowing additional qualification.
20434 If ARG is `const int' and PARM is just `T' that's OK;
20435 that binds `const int' to `T'. */
20436 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20437 arg, parm))
20438 return unify_cv_qual_mismatch (explain_p, parm, arg);
20439
20440 /* Consider the case where ARG is `const volatile int' and
20441 PARM is `const T'. Then, T should be `volatile int'. */
20442 arg = cp_build_qualified_type_real
20443 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20444 if (arg == error_mark_node)
20445 return unify_invalid (explain_p);
20446
20447 /* Simple cases: Value already set, does match or doesn't. */
20448 if (targ != NULL_TREE && same_type_p (targ, arg))
20449 return unify_success (explain_p);
20450 else if (targ)
20451 return unify_inconsistency (explain_p, parm, targ, arg);
20452
20453 /* Make sure that ARG is not a variable-sized array. (Note
20454 that were talking about variable-sized arrays (like
20455 `int[n]'), rather than arrays of unknown size (like
20456 `int[]').) We'll get very confused by such a type since
20457 the bound of the array is not constant, and therefore
20458 not mangleable. Besides, such types are not allowed in
20459 ISO C++, so we can do as we please here. We do allow
20460 them for 'auto' deduction, since that isn't ABI-exposed. */
20461 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20462 return unify_vla_arg (explain_p, arg);
20463
20464 /* Strip typedefs as in convert_template_argument. */
20465 arg = canonicalize_type_argument (arg, tf_none);
20466 }
20467
20468 /* If ARG is a parameter pack or an expansion, we cannot unify
20469 against it unless PARM is also a parameter pack. */
20470 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20471 && !template_parameter_pack_p (parm))
20472 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20473
20474 /* If the argument deduction results is a METHOD_TYPE,
20475 then there is a problem.
20476 METHOD_TYPE doesn't map to any real C++ type the result of
20477 the deduction can not be of that type. */
20478 if (TREE_CODE (arg) == METHOD_TYPE)
20479 return unify_method_type_error (explain_p, arg);
20480
20481 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20482 return unify_success (explain_p);
20483
20484 case TEMPLATE_PARM_INDEX:
20485 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20486 if (error_operand_p (tparm))
20487 return unify_invalid (explain_p);
20488
20489 if (TEMPLATE_PARM_LEVEL (parm)
20490 != template_decl_level (tparm))
20491 {
20492 /* The PARM is not one we're trying to unify. Just check
20493 to see if it matches ARG. */
20494 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20495 && cp_tree_equal (parm, arg));
20496 if (result)
20497 unify_expression_unequal (explain_p, parm, arg);
20498 return result;
20499 }
20500
20501 idx = TEMPLATE_PARM_IDX (parm);
20502 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20503
20504 if (targ)
20505 {
20506 int x = !cp_tree_equal (targ, arg);
20507 if (x)
20508 unify_inconsistency (explain_p, parm, targ, arg);
20509 return x;
20510 }
20511
20512 if (cxx_dialect >= cxx1z
20513 /* We deduce from array bounds in try_array_deduction. */
20514 && !(strict & UNIFY_ALLOW_INTEGER)
20515 && uses_template_parms (TREE_TYPE (parm))
20516 && !type_uses_auto (TREE_TYPE (parm)))
20517 {
20518 tree atype = TREE_TYPE (arg);
20519 RECUR_AND_CHECK_FAILURE (tparms, targs,
20520 TREE_TYPE (parm), atype,
20521 UNIFY_ALLOW_NONE, explain_p);
20522 }
20523
20524 /* [temp.deduct.type] If, in the declaration of a function template
20525 with a non-type template-parameter, the non-type
20526 template-parameter is used in an expression in the function
20527 parameter-list and, if the corresponding template-argument is
20528 deduced, the template-argument type shall match the type of the
20529 template-parameter exactly, except that a template-argument
20530 deduced from an array bound may be of any integral type.
20531 The non-type parameter might use already deduced type parameters. */
20532 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20533 if (tree a = type_uses_auto (tparm))
20534 {
20535 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20536 if (tparm == error_mark_node)
20537 return 1;
20538 }
20539
20540 if (!TREE_TYPE (arg))
20541 /* Template-parameter dependent expression. Just accept it for now.
20542 It will later be processed in convert_template_argument. */
20543 ;
20544 else if (same_type_p (TREE_TYPE (arg), tparm))
20545 /* OK */;
20546 else if ((strict & UNIFY_ALLOW_INTEGER)
20547 && CP_INTEGRAL_TYPE_P (tparm))
20548 /* Convert the ARG to the type of PARM; the deduced non-type
20549 template argument must exactly match the types of the
20550 corresponding parameter. */
20551 arg = fold (build_nop (tparm, arg));
20552 else if (uses_template_parms (tparm))
20553 /* We haven't deduced the type of this parameter yet. Try again
20554 later. */
20555 return unify_success (explain_p);
20556 else
20557 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20558
20559 /* If ARG is a parameter pack or an expansion, we cannot unify
20560 against it unless PARM is also a parameter pack. */
20561 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20562 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20563 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20564
20565 {
20566 bool removed_attr = false;
20567 arg = strip_typedefs_expr (arg, &removed_attr);
20568 }
20569 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20570 return unify_success (explain_p);
20571
20572 case PTRMEM_CST:
20573 {
20574 /* A pointer-to-member constant can be unified only with
20575 another constant. */
20576 if (TREE_CODE (arg) != PTRMEM_CST)
20577 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20578
20579 /* Just unify the class member. It would be useless (and possibly
20580 wrong, depending on the strict flags) to unify also
20581 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20582 arg refer to the same variable, even if through different
20583 classes. For instance:
20584
20585 struct A { int x; };
20586 struct B : A { };
20587
20588 Unification of &A::x and &B::x must succeed. */
20589 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20590 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20591 }
20592
20593 case POINTER_TYPE:
20594 {
20595 if (!TYPE_PTR_P (arg))
20596 return unify_type_mismatch (explain_p, parm, arg);
20597
20598 /* [temp.deduct.call]
20599
20600 A can be another pointer or pointer to member type that can
20601 be converted to the deduced A via a qualification
20602 conversion (_conv.qual_).
20603
20604 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20605 This will allow for additional cv-qualification of the
20606 pointed-to types if appropriate. */
20607
20608 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20609 /* The derived-to-base conversion only persists through one
20610 level of pointers. */
20611 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20612
20613 return unify (tparms, targs, TREE_TYPE (parm),
20614 TREE_TYPE (arg), strict, explain_p);
20615 }
20616
20617 case REFERENCE_TYPE:
20618 if (TREE_CODE (arg) != REFERENCE_TYPE)
20619 return unify_type_mismatch (explain_p, parm, arg);
20620 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20621 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20622
20623 case ARRAY_TYPE:
20624 if (TREE_CODE (arg) != ARRAY_TYPE)
20625 return unify_type_mismatch (explain_p, parm, arg);
20626 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20627 != (TYPE_DOMAIN (arg) == NULL_TREE))
20628 return unify_type_mismatch (explain_p, parm, arg);
20629 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20630 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20631 if (TYPE_DOMAIN (parm) != NULL_TREE)
20632 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20633 TYPE_DOMAIN (arg), explain_p);
20634 return unify_success (explain_p);
20635
20636 case REAL_TYPE:
20637 case COMPLEX_TYPE:
20638 case VECTOR_TYPE:
20639 case INTEGER_TYPE:
20640 case BOOLEAN_TYPE:
20641 case ENUMERAL_TYPE:
20642 case VOID_TYPE:
20643 case NULLPTR_TYPE:
20644 if (TREE_CODE (arg) != TREE_CODE (parm))
20645 return unify_type_mismatch (explain_p, parm, arg);
20646
20647 /* We have already checked cv-qualification at the top of the
20648 function. */
20649 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20650 return unify_type_mismatch (explain_p, parm, arg);
20651
20652 /* As far as unification is concerned, this wins. Later checks
20653 will invalidate it if necessary. */
20654 return unify_success (explain_p);
20655
20656 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20657 /* Type INTEGER_CST can come from ordinary constant template args. */
20658 case INTEGER_CST:
20659 while (CONVERT_EXPR_P (arg))
20660 arg = TREE_OPERAND (arg, 0);
20661
20662 if (TREE_CODE (arg) != INTEGER_CST)
20663 return unify_template_argument_mismatch (explain_p, parm, arg);
20664 return (tree_int_cst_equal (parm, arg)
20665 ? unify_success (explain_p)
20666 : unify_template_argument_mismatch (explain_p, parm, arg));
20667
20668 case TREE_VEC:
20669 {
20670 int i, len, argslen;
20671 int parm_variadic_p = 0;
20672
20673 if (TREE_CODE (arg) != TREE_VEC)
20674 return unify_template_argument_mismatch (explain_p, parm, arg);
20675
20676 len = TREE_VEC_LENGTH (parm);
20677 argslen = TREE_VEC_LENGTH (arg);
20678
20679 /* Check for pack expansions in the parameters. */
20680 for (i = 0; i < len; ++i)
20681 {
20682 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20683 {
20684 if (i == len - 1)
20685 /* We can unify against something with a trailing
20686 parameter pack. */
20687 parm_variadic_p = 1;
20688 else
20689 /* [temp.deduct.type]/9: If the template argument list of
20690 P contains a pack expansion that is not the last
20691 template argument, the entire template argument list
20692 is a non-deduced context. */
20693 return unify_success (explain_p);
20694 }
20695 }
20696
20697 /* If we don't have enough arguments to satisfy the parameters
20698 (not counting the pack expression at the end), or we have
20699 too many arguments for a parameter list that doesn't end in
20700 a pack expression, we can't unify. */
20701 if (parm_variadic_p
20702 ? argslen < len - parm_variadic_p
20703 : argslen != len)
20704 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20705
20706 /* Unify all of the parameters that precede the (optional)
20707 pack expression. */
20708 for (i = 0; i < len - parm_variadic_p; ++i)
20709 {
20710 RECUR_AND_CHECK_FAILURE (tparms, targs,
20711 TREE_VEC_ELT (parm, i),
20712 TREE_VEC_ELT (arg, i),
20713 UNIFY_ALLOW_NONE, explain_p);
20714 }
20715 if (parm_variadic_p)
20716 return unify_pack_expansion (tparms, targs, parm, arg,
20717 DEDUCE_EXACT,
20718 /*subr=*/true, explain_p);
20719 return unify_success (explain_p);
20720 }
20721
20722 case RECORD_TYPE:
20723 case UNION_TYPE:
20724 if (TREE_CODE (arg) != TREE_CODE (parm))
20725 return unify_type_mismatch (explain_p, parm, arg);
20726
20727 if (TYPE_PTRMEMFUNC_P (parm))
20728 {
20729 if (!TYPE_PTRMEMFUNC_P (arg))
20730 return unify_type_mismatch (explain_p, parm, arg);
20731
20732 return unify (tparms, targs,
20733 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20734 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20735 strict, explain_p);
20736 }
20737 else if (TYPE_PTRMEMFUNC_P (arg))
20738 return unify_type_mismatch (explain_p, parm, arg);
20739
20740 if (CLASSTYPE_TEMPLATE_INFO (parm))
20741 {
20742 tree t = NULL_TREE;
20743
20744 if (strict_in & UNIFY_ALLOW_DERIVED)
20745 {
20746 /* First, we try to unify the PARM and ARG directly. */
20747 t = try_class_unification (tparms, targs,
20748 parm, arg, explain_p);
20749
20750 if (!t)
20751 {
20752 /* Fallback to the special case allowed in
20753 [temp.deduct.call]:
20754
20755 If P is a class, and P has the form
20756 template-id, then A can be a derived class of
20757 the deduced A. Likewise, if P is a pointer to
20758 a class of the form template-id, A can be a
20759 pointer to a derived class pointed to by the
20760 deduced A. */
20761 enum template_base_result r;
20762 r = get_template_base (tparms, targs, parm, arg,
20763 explain_p, &t);
20764
20765 if (!t)
20766 {
20767 /* Don't give the derived diagnostic if we're
20768 already dealing with the same template. */
20769 bool same_template
20770 = (CLASSTYPE_TEMPLATE_INFO (arg)
20771 && (CLASSTYPE_TI_TEMPLATE (parm)
20772 == CLASSTYPE_TI_TEMPLATE (arg)));
20773 return unify_no_common_base (explain_p && !same_template,
20774 r, parm, arg);
20775 }
20776 }
20777 }
20778 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20779 && (CLASSTYPE_TI_TEMPLATE (parm)
20780 == CLASSTYPE_TI_TEMPLATE (arg)))
20781 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20782 Then, we should unify `int' and `U'. */
20783 t = arg;
20784 else
20785 /* There's no chance of unification succeeding. */
20786 return unify_type_mismatch (explain_p, parm, arg);
20787
20788 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20789 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20790 }
20791 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20792 return unify_type_mismatch (explain_p, parm, arg);
20793 return unify_success (explain_p);
20794
20795 case METHOD_TYPE:
20796 case FUNCTION_TYPE:
20797 {
20798 unsigned int nargs;
20799 tree *args;
20800 tree a;
20801 unsigned int i;
20802
20803 if (TREE_CODE (arg) != TREE_CODE (parm))
20804 return unify_type_mismatch (explain_p, parm, arg);
20805
20806 /* CV qualifications for methods can never be deduced, they must
20807 match exactly. We need to check them explicitly here,
20808 because type_unification_real treats them as any other
20809 cv-qualified parameter. */
20810 if (TREE_CODE (parm) == METHOD_TYPE
20811 && (!check_cv_quals_for_unify
20812 (UNIFY_ALLOW_NONE,
20813 class_of_this_parm (arg),
20814 class_of_this_parm (parm))))
20815 return unify_cv_qual_mismatch (explain_p, parm, arg);
20816 if (TREE_CODE (arg) == FUNCTION_TYPE
20817 && type_memfn_quals (parm) != type_memfn_quals (arg))
20818 return unify_cv_qual_mismatch (explain_p, parm, arg);
20819 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20820 return unify_type_mismatch (explain_p, parm, arg);
20821
20822 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20823 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20824
20825 nargs = list_length (TYPE_ARG_TYPES (arg));
20826 args = XALLOCAVEC (tree, nargs);
20827 for (a = TYPE_ARG_TYPES (arg), i = 0;
20828 a != NULL_TREE && a != void_list_node;
20829 a = TREE_CHAIN (a), ++i)
20830 args[i] = TREE_VALUE (a);
20831 nargs = i;
20832
20833 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20834 args, nargs, 1, DEDUCE_EXACT,
20835 LOOKUP_NORMAL, NULL, explain_p))
20836 return 1;
20837
20838 if (flag_noexcept_type)
20839 {
20840 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20841 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20842 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20843 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20844 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20845 && uses_template_parms (TREE_PURPOSE (pspec)))
20846 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20847 TREE_PURPOSE (aspec),
20848 UNIFY_ALLOW_NONE, explain_p);
20849 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20850 return unify_type_mismatch (explain_p, parm, arg);
20851 }
20852
20853 return 0;
20854 }
20855
20856 case OFFSET_TYPE:
20857 /* Unify a pointer to member with a pointer to member function, which
20858 deduces the type of the member as a function type. */
20859 if (TYPE_PTRMEMFUNC_P (arg))
20860 {
20861 /* Check top-level cv qualifiers */
20862 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20863 return unify_cv_qual_mismatch (explain_p, parm, arg);
20864
20865 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20866 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20867 UNIFY_ALLOW_NONE, explain_p);
20868
20869 /* Determine the type of the function we are unifying against. */
20870 tree fntype = static_fn_type (arg);
20871
20872 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20873 }
20874
20875 if (TREE_CODE (arg) != OFFSET_TYPE)
20876 return unify_type_mismatch (explain_p, parm, arg);
20877 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20878 TYPE_OFFSET_BASETYPE (arg),
20879 UNIFY_ALLOW_NONE, explain_p);
20880 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20881 strict, explain_p);
20882
20883 case CONST_DECL:
20884 if (DECL_TEMPLATE_PARM_P (parm))
20885 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20886 if (arg != scalar_constant_value (parm))
20887 return unify_template_argument_mismatch (explain_p, parm, arg);
20888 return unify_success (explain_p);
20889
20890 case FIELD_DECL:
20891 case TEMPLATE_DECL:
20892 /* Matched cases are handled by the ARG == PARM test above. */
20893 return unify_template_argument_mismatch (explain_p, parm, arg);
20894
20895 case VAR_DECL:
20896 /* We might get a variable as a non-type template argument in parm if the
20897 corresponding parameter is type-dependent. Make any necessary
20898 adjustments based on whether arg is a reference. */
20899 if (CONSTANT_CLASS_P (arg))
20900 parm = fold_non_dependent_expr (parm);
20901 else if (REFERENCE_REF_P (arg))
20902 {
20903 tree sub = TREE_OPERAND (arg, 0);
20904 STRIP_NOPS (sub);
20905 if (TREE_CODE (sub) == ADDR_EXPR)
20906 arg = TREE_OPERAND (sub, 0);
20907 }
20908 /* Now use the normal expression code to check whether they match. */
20909 goto expr;
20910
20911 case TYPE_ARGUMENT_PACK:
20912 case NONTYPE_ARGUMENT_PACK:
20913 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20914 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20915
20916 case TYPEOF_TYPE:
20917 case DECLTYPE_TYPE:
20918 case UNDERLYING_TYPE:
20919 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20920 or UNDERLYING_TYPE nodes. */
20921 return unify_success (explain_p);
20922
20923 case ERROR_MARK:
20924 /* Unification fails if we hit an error node. */
20925 return unify_invalid (explain_p);
20926
20927 case INDIRECT_REF:
20928 if (REFERENCE_REF_P (parm))
20929 {
20930 bool pexp = PACK_EXPANSION_P (arg);
20931 if (pexp)
20932 arg = PACK_EXPANSION_PATTERN (arg);
20933 if (REFERENCE_REF_P (arg))
20934 arg = TREE_OPERAND (arg, 0);
20935 if (pexp)
20936 arg = make_pack_expansion (arg);
20937 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20938 strict, explain_p);
20939 }
20940 /* FALLTHRU */
20941
20942 default:
20943 /* An unresolved overload is a nondeduced context. */
20944 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20945 return unify_success (explain_p);
20946 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
20947 expr:
20948 /* We must be looking at an expression. This can happen with
20949 something like:
20950
20951 template <int I>
20952 void foo(S<I>, S<I + 2>);
20953
20954 This is a "nondeduced context":
20955
20956 [deduct.type]
20957
20958 The nondeduced contexts are:
20959
20960 --A type that is a template-id in which one or more of
20961 the template-arguments is an expression that references
20962 a template-parameter.
20963
20964 In these cases, we assume deduction succeeded, but don't
20965 actually infer any unifications. */
20966
20967 if (!uses_template_parms (parm)
20968 && !template_args_equal (parm, arg))
20969 return unify_expression_unequal (explain_p, parm, arg);
20970 else
20971 return unify_success (explain_p);
20972 }
20973 }
20974 #undef RECUR_AND_CHECK_FAILURE
20975 \f
20976 /* Note that DECL can be defined in this translation unit, if
20977 required. */
20978
20979 static void
20980 mark_definable (tree decl)
20981 {
20982 tree clone;
20983 DECL_NOT_REALLY_EXTERN (decl) = 1;
20984 FOR_EACH_CLONE (clone, decl)
20985 DECL_NOT_REALLY_EXTERN (clone) = 1;
20986 }
20987
20988 /* Called if RESULT is explicitly instantiated, or is a member of an
20989 explicitly instantiated class. */
20990
20991 void
20992 mark_decl_instantiated (tree result, int extern_p)
20993 {
20994 SET_DECL_EXPLICIT_INSTANTIATION (result);
20995
20996 /* If this entity has already been written out, it's too late to
20997 make any modifications. */
20998 if (TREE_ASM_WRITTEN (result))
20999 return;
21000
21001 /* For anonymous namespace we don't need to do anything. */
21002 if (decl_anon_ns_mem_p (result))
21003 {
21004 gcc_assert (!TREE_PUBLIC (result));
21005 return;
21006 }
21007
21008 if (TREE_CODE (result) != FUNCTION_DECL)
21009 /* The TREE_PUBLIC flag for function declarations will have been
21010 set correctly by tsubst. */
21011 TREE_PUBLIC (result) = 1;
21012
21013 /* This might have been set by an earlier implicit instantiation. */
21014 DECL_COMDAT (result) = 0;
21015
21016 if (extern_p)
21017 DECL_NOT_REALLY_EXTERN (result) = 0;
21018 else
21019 {
21020 mark_definable (result);
21021 mark_needed (result);
21022 /* Always make artificials weak. */
21023 if (DECL_ARTIFICIAL (result) && flag_weak)
21024 comdat_linkage (result);
21025 /* For WIN32 we also want to put explicit instantiations in
21026 linkonce sections. */
21027 else if (TREE_PUBLIC (result))
21028 maybe_make_one_only (result);
21029 }
21030
21031 /* If EXTERN_P, then this function will not be emitted -- unless
21032 followed by an explicit instantiation, at which point its linkage
21033 will be adjusted. If !EXTERN_P, then this function will be
21034 emitted here. In neither circumstance do we want
21035 import_export_decl to adjust the linkage. */
21036 DECL_INTERFACE_KNOWN (result) = 1;
21037 }
21038
21039 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21040 important template arguments. If any are missing, we check whether
21041 they're important by using error_mark_node for substituting into any
21042 args that were used for partial ordering (the ones between ARGS and END)
21043 and seeing if it bubbles up. */
21044
21045 static bool
21046 check_undeduced_parms (tree targs, tree args, tree end)
21047 {
21048 bool found = false;
21049 int i;
21050 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21051 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21052 {
21053 found = true;
21054 TREE_VEC_ELT (targs, i) = error_mark_node;
21055 }
21056 if (found)
21057 {
21058 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21059 if (substed == error_mark_node)
21060 return true;
21061 }
21062 return false;
21063 }
21064
21065 /* Given two function templates PAT1 and PAT2, return:
21066
21067 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21068 -1 if PAT2 is more specialized than PAT1.
21069 0 if neither is more specialized.
21070
21071 LEN indicates the number of parameters we should consider
21072 (defaulted parameters should not be considered).
21073
21074 The 1998 std underspecified function template partial ordering, and
21075 DR214 addresses the issue. We take pairs of arguments, one from
21076 each of the templates, and deduce them against each other. One of
21077 the templates will be more specialized if all the *other*
21078 template's arguments deduce against its arguments and at least one
21079 of its arguments *does* *not* deduce against the other template's
21080 corresponding argument. Deduction is done as for class templates.
21081 The arguments used in deduction have reference and top level cv
21082 qualifiers removed. Iff both arguments were originally reference
21083 types *and* deduction succeeds in both directions, an lvalue reference
21084 wins against an rvalue reference and otherwise the template
21085 with the more cv-qualified argument wins for that pairing (if
21086 neither is more cv-qualified, they both are equal). Unlike regular
21087 deduction, after all the arguments have been deduced in this way,
21088 we do *not* verify the deduced template argument values can be
21089 substituted into non-deduced contexts.
21090
21091 The logic can be a bit confusing here, because we look at deduce1 and
21092 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21093 can find template arguments for pat1 to make arg1 look like arg2, that
21094 means that arg2 is at least as specialized as arg1. */
21095
21096 int
21097 more_specialized_fn (tree pat1, tree pat2, int len)
21098 {
21099 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21100 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21101 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21102 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21103 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21104 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21105 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21106 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21107 tree origs1, origs2;
21108 bool lose1 = false;
21109 bool lose2 = false;
21110
21111 /* Remove the this parameter from non-static member functions. If
21112 one is a non-static member function and the other is not a static
21113 member function, remove the first parameter from that function
21114 also. This situation occurs for operator functions where we
21115 locate both a member function (with this pointer) and non-member
21116 operator (with explicit first operand). */
21117 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21118 {
21119 len--; /* LEN is the number of significant arguments for DECL1 */
21120 args1 = TREE_CHAIN (args1);
21121 if (!DECL_STATIC_FUNCTION_P (decl2))
21122 args2 = TREE_CHAIN (args2);
21123 }
21124 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21125 {
21126 args2 = TREE_CHAIN (args2);
21127 if (!DECL_STATIC_FUNCTION_P (decl1))
21128 {
21129 len--;
21130 args1 = TREE_CHAIN (args1);
21131 }
21132 }
21133
21134 /* If only one is a conversion operator, they are unordered. */
21135 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21136 return 0;
21137
21138 /* Consider the return type for a conversion function */
21139 if (DECL_CONV_FN_P (decl1))
21140 {
21141 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21142 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21143 len++;
21144 }
21145
21146 processing_template_decl++;
21147
21148 origs1 = args1;
21149 origs2 = args2;
21150
21151 while (len--
21152 /* Stop when an ellipsis is seen. */
21153 && args1 != NULL_TREE && args2 != NULL_TREE)
21154 {
21155 tree arg1 = TREE_VALUE (args1);
21156 tree arg2 = TREE_VALUE (args2);
21157 int deduce1, deduce2;
21158 int quals1 = -1;
21159 int quals2 = -1;
21160 int ref1 = 0;
21161 int ref2 = 0;
21162
21163 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21164 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21165 {
21166 /* When both arguments are pack expansions, we need only
21167 unify the patterns themselves. */
21168 arg1 = PACK_EXPANSION_PATTERN (arg1);
21169 arg2 = PACK_EXPANSION_PATTERN (arg2);
21170
21171 /* This is the last comparison we need to do. */
21172 len = 0;
21173 }
21174
21175 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21176 {
21177 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21178 arg1 = TREE_TYPE (arg1);
21179 quals1 = cp_type_quals (arg1);
21180 }
21181
21182 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21183 {
21184 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21185 arg2 = TREE_TYPE (arg2);
21186 quals2 = cp_type_quals (arg2);
21187 }
21188
21189 arg1 = TYPE_MAIN_VARIANT (arg1);
21190 arg2 = TYPE_MAIN_VARIANT (arg2);
21191
21192 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21193 {
21194 int i, len2 = remaining_arguments (args2);
21195 tree parmvec = make_tree_vec (1);
21196 tree argvec = make_tree_vec (len2);
21197 tree ta = args2;
21198
21199 /* Setup the parameter vector, which contains only ARG1. */
21200 TREE_VEC_ELT (parmvec, 0) = arg1;
21201
21202 /* Setup the argument vector, which contains the remaining
21203 arguments. */
21204 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21205 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21206
21207 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21208 argvec, DEDUCE_EXACT,
21209 /*subr=*/true, /*explain_p=*/false)
21210 == 0);
21211
21212 /* We cannot deduce in the other direction, because ARG1 is
21213 a pack expansion but ARG2 is not. */
21214 deduce2 = 0;
21215 }
21216 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21217 {
21218 int i, len1 = remaining_arguments (args1);
21219 tree parmvec = make_tree_vec (1);
21220 tree argvec = make_tree_vec (len1);
21221 tree ta = args1;
21222
21223 /* Setup the parameter vector, which contains only ARG1. */
21224 TREE_VEC_ELT (parmvec, 0) = arg2;
21225
21226 /* Setup the argument vector, which contains the remaining
21227 arguments. */
21228 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21229 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21230
21231 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21232 argvec, DEDUCE_EXACT,
21233 /*subr=*/true, /*explain_p=*/false)
21234 == 0);
21235
21236 /* We cannot deduce in the other direction, because ARG2 is
21237 a pack expansion but ARG1 is not.*/
21238 deduce1 = 0;
21239 }
21240
21241 else
21242 {
21243 /* The normal case, where neither argument is a pack
21244 expansion. */
21245 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21246 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21247 == 0);
21248 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21249 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21250 == 0);
21251 }
21252
21253 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21254 arg2, then arg2 is not as specialized as arg1. */
21255 if (!deduce1)
21256 lose2 = true;
21257 if (!deduce2)
21258 lose1 = true;
21259
21260 /* "If, for a given type, deduction succeeds in both directions
21261 (i.e., the types are identical after the transformations above)
21262 and both P and A were reference types (before being replaced with
21263 the type referred to above):
21264 - if the type from the argument template was an lvalue reference and
21265 the type from the parameter template was not, the argument type is
21266 considered to be more specialized than the other; otherwise,
21267 - if the type from the argument template is more cv-qualified
21268 than the type from the parameter template (as described above),
21269 the argument type is considered to be more specialized than the other;
21270 otherwise,
21271 - neither type is more specialized than the other." */
21272
21273 if (deduce1 && deduce2)
21274 {
21275 if (ref1 && ref2 && ref1 != ref2)
21276 {
21277 if (ref1 > ref2)
21278 lose1 = true;
21279 else
21280 lose2 = true;
21281 }
21282 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21283 {
21284 if ((quals1 & quals2) == quals2)
21285 lose2 = true;
21286 if ((quals1 & quals2) == quals1)
21287 lose1 = true;
21288 }
21289 }
21290
21291 if (lose1 && lose2)
21292 /* We've failed to deduce something in either direction.
21293 These must be unordered. */
21294 break;
21295
21296 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21297 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21298 /* We have already processed all of the arguments in our
21299 handing of the pack expansion type. */
21300 len = 0;
21301
21302 args1 = TREE_CHAIN (args1);
21303 args2 = TREE_CHAIN (args2);
21304 }
21305
21306 /* "In most cases, all template parameters must have values in order for
21307 deduction to succeed, but for partial ordering purposes a template
21308 parameter may remain without a value provided it is not used in the
21309 types being used for partial ordering."
21310
21311 Thus, if we are missing any of the targs1 we need to substitute into
21312 origs1, then pat2 is not as specialized as pat1. This can happen when
21313 there is a nondeduced context. */
21314 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21315 lose2 = true;
21316 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21317 lose1 = true;
21318
21319 processing_template_decl--;
21320
21321 /* If both deductions succeed, the partial ordering selects the more
21322 constrained template. */
21323 if (!lose1 && !lose2)
21324 {
21325 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21326 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21327 lose1 = !subsumes_constraints (c1, c2);
21328 lose2 = !subsumes_constraints (c2, c1);
21329 }
21330
21331 /* All things being equal, if the next argument is a pack expansion
21332 for one function but not for the other, prefer the
21333 non-variadic function. FIXME this is bogus; see c++/41958. */
21334 if (lose1 == lose2
21335 && args1 && TREE_VALUE (args1)
21336 && args2 && TREE_VALUE (args2))
21337 {
21338 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21339 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21340 }
21341
21342 if (lose1 == lose2)
21343 return 0;
21344 else if (!lose1)
21345 return 1;
21346 else
21347 return -1;
21348 }
21349
21350 /* Determine which of two partial specializations of TMPL is more
21351 specialized.
21352
21353 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21354 to the first partial specialization. The TREE_PURPOSE is the
21355 innermost set of template parameters for the partial
21356 specialization. PAT2 is similar, but for the second template.
21357
21358 Return 1 if the first partial specialization is more specialized;
21359 -1 if the second is more specialized; 0 if neither is more
21360 specialized.
21361
21362 See [temp.class.order] for information about determining which of
21363 two templates is more specialized. */
21364
21365 static int
21366 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21367 {
21368 tree targs;
21369 int winner = 0;
21370 bool any_deductions = false;
21371
21372 tree tmpl1 = TREE_VALUE (pat1);
21373 tree tmpl2 = TREE_VALUE (pat2);
21374 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21375 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21376
21377 /* Just like what happens for functions, if we are ordering between
21378 different template specializations, we may encounter dependent
21379 types in the arguments, and we need our dependency check functions
21380 to behave correctly. */
21381 ++processing_template_decl;
21382 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21383 if (targs)
21384 {
21385 --winner;
21386 any_deductions = true;
21387 }
21388
21389 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21390 if (targs)
21391 {
21392 ++winner;
21393 any_deductions = true;
21394 }
21395 --processing_template_decl;
21396
21397 /* If both deductions succeed, the partial ordering selects the more
21398 constrained template. */
21399 if (!winner && any_deductions)
21400 return more_constrained (tmpl1, tmpl2);
21401
21402 /* In the case of a tie where at least one of the templates
21403 has a parameter pack at the end, the template with the most
21404 non-packed parameters wins. */
21405 if (winner == 0
21406 && any_deductions
21407 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21408 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21409 {
21410 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21411 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21412 int len1 = TREE_VEC_LENGTH (args1);
21413 int len2 = TREE_VEC_LENGTH (args2);
21414
21415 /* We don't count the pack expansion at the end. */
21416 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21417 --len1;
21418 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21419 --len2;
21420
21421 if (len1 > len2)
21422 return 1;
21423 else if (len1 < len2)
21424 return -1;
21425 }
21426
21427 return winner;
21428 }
21429
21430 /* Return the template arguments that will produce the function signature
21431 DECL from the function template FN, with the explicit template
21432 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21433 also match. Return NULL_TREE if no satisfactory arguments could be
21434 found. */
21435
21436 static tree
21437 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21438 {
21439 int ntparms = DECL_NTPARMS (fn);
21440 tree targs = make_tree_vec (ntparms);
21441 tree decl_type = TREE_TYPE (decl);
21442 tree decl_arg_types;
21443 tree *args;
21444 unsigned int nargs, ix;
21445 tree arg;
21446
21447 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21448
21449 /* Never do unification on the 'this' parameter. */
21450 decl_arg_types = skip_artificial_parms_for (decl,
21451 TYPE_ARG_TYPES (decl_type));
21452
21453 nargs = list_length (decl_arg_types);
21454 args = XALLOCAVEC (tree, nargs);
21455 for (arg = decl_arg_types, ix = 0;
21456 arg != NULL_TREE && arg != void_list_node;
21457 arg = TREE_CHAIN (arg), ++ix)
21458 args[ix] = TREE_VALUE (arg);
21459
21460 if (fn_type_unification (fn, explicit_args, targs,
21461 args, ix,
21462 (check_rettype || DECL_CONV_FN_P (fn)
21463 ? TREE_TYPE (decl_type) : NULL_TREE),
21464 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21465 /*decltype*/false)
21466 == error_mark_node)
21467 return NULL_TREE;
21468
21469 return targs;
21470 }
21471
21472 /* Return the innermost template arguments that, when applied to a partial
21473 specialization SPEC_TMPL of TMPL, yield the ARGS.
21474
21475 For example, suppose we have:
21476
21477 template <class T, class U> struct S {};
21478 template <class T> struct S<T*, int> {};
21479
21480 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21481 partial specialization and the ARGS will be {double*, int}. The resulting
21482 vector will be {double}, indicating that `T' is bound to `double'. */
21483
21484 static tree
21485 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21486 {
21487 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21488 tree spec_args
21489 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21490 int i, ntparms = TREE_VEC_LENGTH (tparms);
21491 tree deduced_args;
21492 tree innermost_deduced_args;
21493
21494 innermost_deduced_args = make_tree_vec (ntparms);
21495 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21496 {
21497 deduced_args = copy_node (args);
21498 SET_TMPL_ARGS_LEVEL (deduced_args,
21499 TMPL_ARGS_DEPTH (deduced_args),
21500 innermost_deduced_args);
21501 }
21502 else
21503 deduced_args = innermost_deduced_args;
21504
21505 bool tried_array_deduction = (cxx_dialect < cxx1z);
21506 again:
21507 if (unify (tparms, deduced_args,
21508 INNERMOST_TEMPLATE_ARGS (spec_args),
21509 INNERMOST_TEMPLATE_ARGS (args),
21510 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21511 return NULL_TREE;
21512
21513 for (i = 0; i < ntparms; ++i)
21514 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21515 {
21516 if (!tried_array_deduction)
21517 {
21518 try_array_deduction (tparms, innermost_deduced_args,
21519 INNERMOST_TEMPLATE_ARGS (spec_args));
21520 tried_array_deduction = true;
21521 if (TREE_VEC_ELT (innermost_deduced_args, i))
21522 goto again;
21523 }
21524 return NULL_TREE;
21525 }
21526
21527 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21528 if (!push_tinst_level (tinst))
21529 {
21530 excessive_deduction_depth = true;
21531 return NULL_TREE;
21532 }
21533
21534 /* Verify that nondeduced template arguments agree with the type
21535 obtained from argument deduction.
21536
21537 For example:
21538
21539 struct A { typedef int X; };
21540 template <class T, class U> struct C {};
21541 template <class T> struct C<T, typename T::X> {};
21542
21543 Then with the instantiation `C<A, int>', we can deduce that
21544 `T' is `A' but unify () does not check whether `typename T::X'
21545 is `int'. */
21546 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21547 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21548 spec_args, tmpl,
21549 tf_none, false, false);
21550
21551 pop_tinst_level ();
21552
21553 if (spec_args == error_mark_node
21554 /* We only need to check the innermost arguments; the other
21555 arguments will always agree. */
21556 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
21557 INNERMOST_TEMPLATE_ARGS (args)))
21558 return NULL_TREE;
21559
21560 /* Now that we have bindings for all of the template arguments,
21561 ensure that the arguments deduced for the template template
21562 parameters have compatible template parameter lists. See the use
21563 of template_template_parm_bindings_ok_p in fn_type_unification
21564 for more information. */
21565 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21566 return NULL_TREE;
21567
21568 return deduced_args;
21569 }
21570
21571 // Compare two function templates T1 and T2 by deducing bindings
21572 // from one against the other. If both deductions succeed, compare
21573 // constraints to see which is more constrained.
21574 static int
21575 more_specialized_inst (tree t1, tree t2)
21576 {
21577 int fate = 0;
21578 int count = 0;
21579
21580 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21581 {
21582 --fate;
21583 ++count;
21584 }
21585
21586 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21587 {
21588 ++fate;
21589 ++count;
21590 }
21591
21592 // If both deductions succeed, then one may be more constrained.
21593 if (count == 2 && fate == 0)
21594 fate = more_constrained (t1, t2);
21595
21596 return fate;
21597 }
21598
21599 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21600 Return the TREE_LIST node with the most specialized template, if
21601 any. If there is no most specialized template, the error_mark_node
21602 is returned.
21603
21604 Note that this function does not look at, or modify, the
21605 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21606 returned is one of the elements of INSTANTIATIONS, callers may
21607 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21608 and retrieve it from the value returned. */
21609
21610 tree
21611 most_specialized_instantiation (tree templates)
21612 {
21613 tree fn, champ;
21614
21615 ++processing_template_decl;
21616
21617 champ = templates;
21618 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21619 {
21620 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21621 if (fate == -1)
21622 champ = fn;
21623 else if (!fate)
21624 {
21625 /* Equally specialized, move to next function. If there
21626 is no next function, nothing's most specialized. */
21627 fn = TREE_CHAIN (fn);
21628 champ = fn;
21629 if (!fn)
21630 break;
21631 }
21632 }
21633
21634 if (champ)
21635 /* Now verify that champ is better than everything earlier in the
21636 instantiation list. */
21637 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21638 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21639 {
21640 champ = NULL_TREE;
21641 break;
21642 }
21643 }
21644
21645 processing_template_decl--;
21646
21647 if (!champ)
21648 return error_mark_node;
21649
21650 return champ;
21651 }
21652
21653 /* If DECL is a specialization of some template, return the most
21654 general such template. Otherwise, returns NULL_TREE.
21655
21656 For example, given:
21657
21658 template <class T> struct S { template <class U> void f(U); };
21659
21660 if TMPL is `template <class U> void S<int>::f(U)' this will return
21661 the full template. This function will not trace past partial
21662 specializations, however. For example, given in addition:
21663
21664 template <class T> struct S<T*> { template <class U> void f(U); };
21665
21666 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21667 `template <class T> template <class U> S<T*>::f(U)'. */
21668
21669 tree
21670 most_general_template (tree decl)
21671 {
21672 if (TREE_CODE (decl) != TEMPLATE_DECL)
21673 {
21674 if (tree tinfo = get_template_info (decl))
21675 decl = TI_TEMPLATE (tinfo);
21676 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21677 template friend, or a FIELD_DECL for a capture pack. */
21678 if (TREE_CODE (decl) != TEMPLATE_DECL)
21679 return NULL_TREE;
21680 }
21681
21682 /* Look for more and more general templates. */
21683 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21684 {
21685 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21686 (See cp-tree.h for details.) */
21687 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21688 break;
21689
21690 if (CLASS_TYPE_P (TREE_TYPE (decl))
21691 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21692 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21693 break;
21694
21695 /* Stop if we run into an explicitly specialized class template. */
21696 if (!DECL_NAMESPACE_SCOPE_P (decl)
21697 && DECL_CONTEXT (decl)
21698 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21699 break;
21700
21701 decl = DECL_TI_TEMPLATE (decl);
21702 }
21703
21704 return decl;
21705 }
21706
21707 /* Return the most specialized of the template partial specializations
21708 which can produce TARGET, a specialization of some class or variable
21709 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21710 a TEMPLATE_DECL node corresponding to the partial specialization, while
21711 the TREE_PURPOSE is the set of template arguments that must be
21712 substituted into the template pattern in order to generate TARGET.
21713
21714 If the choice of partial specialization is ambiguous, a diagnostic
21715 is issued, and the error_mark_node is returned. If there are no
21716 partial specializations matching TARGET, then NULL_TREE is
21717 returned, indicating that the primary template should be used. */
21718
21719 static tree
21720 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21721 {
21722 tree list = NULL_TREE;
21723 tree t;
21724 tree champ;
21725 int fate;
21726 bool ambiguous_p;
21727 tree outer_args = NULL_TREE;
21728 tree tmpl, args;
21729
21730 if (TYPE_P (target))
21731 {
21732 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21733 tmpl = TI_TEMPLATE (tinfo);
21734 args = TI_ARGS (tinfo);
21735 }
21736 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21737 {
21738 tmpl = TREE_OPERAND (target, 0);
21739 args = TREE_OPERAND (target, 1);
21740 }
21741 else if (VAR_P (target))
21742 {
21743 tree tinfo = DECL_TEMPLATE_INFO (target);
21744 tmpl = TI_TEMPLATE (tinfo);
21745 args = TI_ARGS (tinfo);
21746 }
21747 else
21748 gcc_unreachable ();
21749
21750 tree main_tmpl = most_general_template (tmpl);
21751
21752 /* For determining which partial specialization to use, only the
21753 innermost args are interesting. */
21754 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21755 {
21756 outer_args = strip_innermost_template_args (args, 1);
21757 args = INNERMOST_TEMPLATE_ARGS (args);
21758 }
21759
21760 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21761 {
21762 tree spec_args;
21763 tree spec_tmpl = TREE_VALUE (t);
21764
21765 if (outer_args)
21766 {
21767 /* Substitute in the template args from the enclosing class. */
21768 ++processing_template_decl;
21769 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21770 --processing_template_decl;
21771 }
21772
21773 if (spec_tmpl == error_mark_node)
21774 return error_mark_node;
21775
21776 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21777 if (spec_args)
21778 {
21779 if (outer_args)
21780 spec_args = add_to_template_args (outer_args, spec_args);
21781
21782 /* Keep the candidate only if the constraints are satisfied,
21783 or if we're not compiling with concepts. */
21784 if (!flag_concepts
21785 || constraints_satisfied_p (spec_tmpl, spec_args))
21786 {
21787 list = tree_cons (spec_args, TREE_VALUE (t), list);
21788 TREE_TYPE (list) = TREE_TYPE (t);
21789 }
21790 }
21791 }
21792
21793 if (! list)
21794 return NULL_TREE;
21795
21796 ambiguous_p = false;
21797 t = list;
21798 champ = t;
21799 t = TREE_CHAIN (t);
21800 for (; t; t = TREE_CHAIN (t))
21801 {
21802 fate = more_specialized_partial_spec (tmpl, champ, t);
21803 if (fate == 1)
21804 ;
21805 else
21806 {
21807 if (fate == 0)
21808 {
21809 t = TREE_CHAIN (t);
21810 if (! t)
21811 {
21812 ambiguous_p = true;
21813 break;
21814 }
21815 }
21816 champ = t;
21817 }
21818 }
21819
21820 if (!ambiguous_p)
21821 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21822 {
21823 fate = more_specialized_partial_spec (tmpl, champ, t);
21824 if (fate != 1)
21825 {
21826 ambiguous_p = true;
21827 break;
21828 }
21829 }
21830
21831 if (ambiguous_p)
21832 {
21833 const char *str;
21834 char *spaces = NULL;
21835 if (!(complain & tf_error))
21836 return error_mark_node;
21837 if (TYPE_P (target))
21838 error ("ambiguous template instantiation for %q#T", target);
21839 else
21840 error ("ambiguous template instantiation for %q#D", target);
21841 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21842 for (t = list; t; t = TREE_CHAIN (t))
21843 {
21844 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21845 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21846 "%s %#S", spaces ? spaces : str, subst);
21847 spaces = spaces ? spaces : get_spaces (str);
21848 }
21849 free (spaces);
21850 return error_mark_node;
21851 }
21852
21853 return champ;
21854 }
21855
21856 /* Explicitly instantiate DECL. */
21857
21858 void
21859 do_decl_instantiation (tree decl, tree storage)
21860 {
21861 tree result = NULL_TREE;
21862 int extern_p = 0;
21863
21864 if (!decl || decl == error_mark_node)
21865 /* An error occurred, for which grokdeclarator has already issued
21866 an appropriate message. */
21867 return;
21868 else if (! DECL_LANG_SPECIFIC (decl))
21869 {
21870 error ("explicit instantiation of non-template %q#D", decl);
21871 return;
21872 }
21873
21874 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21875 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21876
21877 if (VAR_P (decl) && !var_templ)
21878 {
21879 /* There is an asymmetry here in the way VAR_DECLs and
21880 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21881 the latter, the DECL we get back will be marked as a
21882 template instantiation, and the appropriate
21883 DECL_TEMPLATE_INFO will be set up. This does not happen for
21884 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21885 should handle VAR_DECLs as it currently handles
21886 FUNCTION_DECLs. */
21887 if (!DECL_CLASS_SCOPE_P (decl))
21888 {
21889 error ("%qD is not a static data member of a class template", decl);
21890 return;
21891 }
21892 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21893 if (!result || !VAR_P (result))
21894 {
21895 error ("no matching template for %qD found", decl);
21896 return;
21897 }
21898 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21899 {
21900 error ("type %qT for explicit instantiation %qD does not match "
21901 "declared type %qT", TREE_TYPE (result), decl,
21902 TREE_TYPE (decl));
21903 return;
21904 }
21905 }
21906 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21907 {
21908 error ("explicit instantiation of %q#D", decl);
21909 return;
21910 }
21911 else
21912 result = decl;
21913
21914 /* Check for various error cases. Note that if the explicit
21915 instantiation is valid the RESULT will currently be marked as an
21916 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21917 until we get here. */
21918
21919 if (DECL_TEMPLATE_SPECIALIZATION (result))
21920 {
21921 /* DR 259 [temp.spec].
21922
21923 Both an explicit instantiation and a declaration of an explicit
21924 specialization shall not appear in a program unless the explicit
21925 instantiation follows a declaration of the explicit specialization.
21926
21927 For a given set of template parameters, if an explicit
21928 instantiation of a template appears after a declaration of an
21929 explicit specialization for that template, the explicit
21930 instantiation has no effect. */
21931 return;
21932 }
21933 else if (DECL_EXPLICIT_INSTANTIATION (result))
21934 {
21935 /* [temp.spec]
21936
21937 No program shall explicitly instantiate any template more
21938 than once.
21939
21940 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21941 the first instantiation was `extern' and the second is not,
21942 and EXTERN_P for the opposite case. */
21943 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21944 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21945 /* If an "extern" explicit instantiation follows an ordinary
21946 explicit instantiation, the template is instantiated. */
21947 if (extern_p)
21948 return;
21949 }
21950 else if (!DECL_IMPLICIT_INSTANTIATION (result))
21951 {
21952 error ("no matching template for %qD found", result);
21953 return;
21954 }
21955 else if (!DECL_TEMPLATE_INFO (result))
21956 {
21957 permerror (input_location, "explicit instantiation of non-template %q#D", result);
21958 return;
21959 }
21960
21961 if (storage == NULL_TREE)
21962 ;
21963 else if (storage == ridpointers[(int) RID_EXTERN])
21964 {
21965 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
21966 pedwarn (input_location, OPT_Wpedantic,
21967 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21968 "instantiations");
21969 extern_p = 1;
21970 }
21971 else
21972 error ("storage class %qD applied to template instantiation", storage);
21973
21974 check_explicit_instantiation_namespace (result);
21975 mark_decl_instantiated (result, extern_p);
21976 if (! extern_p)
21977 instantiate_decl (result, /*defer_ok=*/true,
21978 /*expl_inst_class_mem_p=*/false);
21979 }
21980
21981 static void
21982 mark_class_instantiated (tree t, int extern_p)
21983 {
21984 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
21985 SET_CLASSTYPE_INTERFACE_KNOWN (t);
21986 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
21987 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
21988 if (! extern_p)
21989 {
21990 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
21991 rest_of_type_compilation (t, 1);
21992 }
21993 }
21994
21995 /* Called from do_type_instantiation through binding_table_foreach to
21996 do recursive instantiation for the type bound in ENTRY. */
21997 static void
21998 bt_instantiate_type_proc (binding_entry entry, void *data)
21999 {
22000 tree storage = *(tree *) data;
22001
22002 if (MAYBE_CLASS_TYPE_P (entry->type)
22003 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22004 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22005 }
22006
22007 /* Called from do_type_instantiation to instantiate a member
22008 (a member function or a static member variable) of an
22009 explicitly instantiated class template. */
22010 static void
22011 instantiate_class_member (tree decl, int extern_p)
22012 {
22013 mark_decl_instantiated (decl, extern_p);
22014 if (! extern_p)
22015 instantiate_decl (decl, /*defer_ok=*/true,
22016 /*expl_inst_class_mem_p=*/true);
22017 }
22018
22019 /* Perform an explicit instantiation of template class T. STORAGE, if
22020 non-null, is the RID for extern, inline or static. COMPLAIN is
22021 nonzero if this is called from the parser, zero if called recursively,
22022 since the standard is unclear (as detailed below). */
22023
22024 void
22025 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22026 {
22027 int extern_p = 0;
22028 int nomem_p = 0;
22029 int static_p = 0;
22030 int previous_instantiation_extern_p = 0;
22031
22032 if (TREE_CODE (t) == TYPE_DECL)
22033 t = TREE_TYPE (t);
22034
22035 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22036 {
22037 tree tmpl =
22038 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22039 if (tmpl)
22040 error ("explicit instantiation of non-class template %qD", tmpl);
22041 else
22042 error ("explicit instantiation of non-template type %qT", t);
22043 return;
22044 }
22045
22046 complete_type (t);
22047
22048 if (!COMPLETE_TYPE_P (t))
22049 {
22050 if (complain & tf_error)
22051 error ("explicit instantiation of %q#T before definition of template",
22052 t);
22053 return;
22054 }
22055
22056 if (storage != NULL_TREE)
22057 {
22058 if (!in_system_header_at (input_location))
22059 {
22060 if (storage == ridpointers[(int) RID_EXTERN])
22061 {
22062 if (cxx_dialect == cxx98)
22063 pedwarn (input_location, OPT_Wpedantic,
22064 "ISO C++ 1998 forbids the use of %<extern%> on "
22065 "explicit instantiations");
22066 }
22067 else
22068 pedwarn (input_location, OPT_Wpedantic,
22069 "ISO C++ forbids the use of %qE"
22070 " on explicit instantiations", storage);
22071 }
22072
22073 if (storage == ridpointers[(int) RID_INLINE])
22074 nomem_p = 1;
22075 else if (storage == ridpointers[(int) RID_EXTERN])
22076 extern_p = 1;
22077 else if (storage == ridpointers[(int) RID_STATIC])
22078 static_p = 1;
22079 else
22080 {
22081 error ("storage class %qD applied to template instantiation",
22082 storage);
22083 extern_p = 0;
22084 }
22085 }
22086
22087 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22088 {
22089 /* DR 259 [temp.spec].
22090
22091 Both an explicit instantiation and a declaration of an explicit
22092 specialization shall not appear in a program unless the explicit
22093 instantiation follows a declaration of the explicit specialization.
22094
22095 For a given set of template parameters, if an explicit
22096 instantiation of a template appears after a declaration of an
22097 explicit specialization for that template, the explicit
22098 instantiation has no effect. */
22099 return;
22100 }
22101 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22102 {
22103 /* [temp.spec]
22104
22105 No program shall explicitly instantiate any template more
22106 than once.
22107
22108 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22109 instantiation was `extern'. If EXTERN_P then the second is.
22110 These cases are OK. */
22111 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22112
22113 if (!previous_instantiation_extern_p && !extern_p
22114 && (complain & tf_error))
22115 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22116
22117 /* If we've already instantiated the template, just return now. */
22118 if (!CLASSTYPE_INTERFACE_ONLY (t))
22119 return;
22120 }
22121
22122 check_explicit_instantiation_namespace (TYPE_NAME (t));
22123 mark_class_instantiated (t, extern_p);
22124
22125 if (nomem_p)
22126 return;
22127
22128 {
22129 tree tmp;
22130
22131 /* In contrast to implicit instantiation, where only the
22132 declarations, and not the definitions, of members are
22133 instantiated, we have here:
22134
22135 [temp.explicit]
22136
22137 The explicit instantiation of a class template specialization
22138 implies the instantiation of all of its members not
22139 previously explicitly specialized in the translation unit
22140 containing the explicit instantiation.
22141
22142 Of course, we can't instantiate member template classes, since
22143 we don't have any arguments for them. Note that the standard
22144 is unclear on whether the instantiation of the members are
22145 *explicit* instantiations or not. However, the most natural
22146 interpretation is that it should be an explicit instantiation. */
22147
22148 if (! static_p)
22149 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22150 if (TREE_CODE (tmp) == FUNCTION_DECL
22151 && DECL_TEMPLATE_INSTANTIATION (tmp)
22152 && user_provided_p (tmp))
22153 instantiate_class_member (tmp, extern_p);
22154
22155 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22156 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22157 instantiate_class_member (tmp, extern_p);
22158
22159 if (CLASSTYPE_NESTED_UTDS (t))
22160 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22161 bt_instantiate_type_proc, &storage);
22162 }
22163 }
22164
22165 /* Given a function DECL, which is a specialization of TMPL, modify
22166 DECL to be a re-instantiation of TMPL with the same template
22167 arguments. TMPL should be the template into which tsubst'ing
22168 should occur for DECL, not the most general template.
22169
22170 One reason for doing this is a scenario like this:
22171
22172 template <class T>
22173 void f(const T&, int i);
22174
22175 void g() { f(3, 7); }
22176
22177 template <class T>
22178 void f(const T& t, const int i) { }
22179
22180 Note that when the template is first instantiated, with
22181 instantiate_template, the resulting DECL will have no name for the
22182 first parameter, and the wrong type for the second. So, when we go
22183 to instantiate the DECL, we regenerate it. */
22184
22185 static void
22186 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22187 {
22188 /* The arguments used to instantiate DECL, from the most general
22189 template. */
22190 tree code_pattern;
22191
22192 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22193
22194 /* Make sure that we can see identifiers, and compute access
22195 correctly. */
22196 push_access_scope (decl);
22197
22198 if (TREE_CODE (decl) == FUNCTION_DECL)
22199 {
22200 tree decl_parm;
22201 tree pattern_parm;
22202 tree specs;
22203 int args_depth;
22204 int parms_depth;
22205
22206 args_depth = TMPL_ARGS_DEPTH (args);
22207 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22208 if (args_depth > parms_depth)
22209 args = get_innermost_template_args (args, parms_depth);
22210
22211 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22212 args, tf_error, NULL_TREE,
22213 /*defer_ok*/false);
22214 if (specs && specs != error_mark_node)
22215 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22216 specs);
22217
22218 /* Merge parameter declarations. */
22219 decl_parm = skip_artificial_parms_for (decl,
22220 DECL_ARGUMENTS (decl));
22221 pattern_parm
22222 = skip_artificial_parms_for (code_pattern,
22223 DECL_ARGUMENTS (code_pattern));
22224 while (decl_parm && !DECL_PACK_P (pattern_parm))
22225 {
22226 tree parm_type;
22227 tree attributes;
22228
22229 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22230 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22231 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22232 NULL_TREE);
22233 parm_type = type_decays_to (parm_type);
22234 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22235 TREE_TYPE (decl_parm) = parm_type;
22236 attributes = DECL_ATTRIBUTES (pattern_parm);
22237 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22238 {
22239 DECL_ATTRIBUTES (decl_parm) = attributes;
22240 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22241 }
22242 decl_parm = DECL_CHAIN (decl_parm);
22243 pattern_parm = DECL_CHAIN (pattern_parm);
22244 }
22245 /* Merge any parameters that match with the function parameter
22246 pack. */
22247 if (pattern_parm && DECL_PACK_P (pattern_parm))
22248 {
22249 int i, len;
22250 tree expanded_types;
22251 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22252 the parameters in this function parameter pack. */
22253 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22254 args, tf_error, NULL_TREE);
22255 len = TREE_VEC_LENGTH (expanded_types);
22256 for (i = 0; i < len; i++)
22257 {
22258 tree parm_type;
22259 tree attributes;
22260
22261 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22262 /* Rename the parameter to include the index. */
22263 DECL_NAME (decl_parm) =
22264 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22265 parm_type = TREE_VEC_ELT (expanded_types, i);
22266 parm_type = type_decays_to (parm_type);
22267 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22268 TREE_TYPE (decl_parm) = parm_type;
22269 attributes = DECL_ATTRIBUTES (pattern_parm);
22270 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22271 {
22272 DECL_ATTRIBUTES (decl_parm) = attributes;
22273 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22274 }
22275 decl_parm = DECL_CHAIN (decl_parm);
22276 }
22277 }
22278 /* Merge additional specifiers from the CODE_PATTERN. */
22279 if (DECL_DECLARED_INLINE_P (code_pattern)
22280 && !DECL_DECLARED_INLINE_P (decl))
22281 DECL_DECLARED_INLINE_P (decl) = 1;
22282 }
22283 else if (VAR_P (decl))
22284 {
22285 DECL_INITIAL (decl) =
22286 tsubst_expr (DECL_INITIAL (code_pattern), args,
22287 tf_error, DECL_TI_TEMPLATE (decl),
22288 /*integral_constant_expression_p=*/false);
22289 if (VAR_HAD_UNKNOWN_BOUND (decl))
22290 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22291 tf_error, DECL_TI_TEMPLATE (decl));
22292 }
22293 else
22294 gcc_unreachable ();
22295
22296 pop_access_scope (decl);
22297 }
22298
22299 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22300 substituted to get DECL. */
22301
22302 tree
22303 template_for_substitution (tree decl)
22304 {
22305 tree tmpl = DECL_TI_TEMPLATE (decl);
22306
22307 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22308 for the instantiation. This is not always the most general
22309 template. Consider, for example:
22310
22311 template <class T>
22312 struct S { template <class U> void f();
22313 template <> void f<int>(); };
22314
22315 and an instantiation of S<double>::f<int>. We want TD to be the
22316 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22317 while (/* An instantiation cannot have a definition, so we need a
22318 more general template. */
22319 DECL_TEMPLATE_INSTANTIATION (tmpl)
22320 /* We must also deal with friend templates. Given:
22321
22322 template <class T> struct S {
22323 template <class U> friend void f() {};
22324 };
22325
22326 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22327 so far as the language is concerned, but that's still
22328 where we get the pattern for the instantiation from. On
22329 other hand, if the definition comes outside the class, say:
22330
22331 template <class T> struct S {
22332 template <class U> friend void f();
22333 };
22334 template <class U> friend void f() {}
22335
22336 we don't need to look any further. That's what the check for
22337 DECL_INITIAL is for. */
22338 || (TREE_CODE (decl) == FUNCTION_DECL
22339 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22340 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22341 {
22342 /* The present template, TD, should not be a definition. If it
22343 were a definition, we should be using it! Note that we
22344 cannot restructure the loop to just keep going until we find
22345 a template with a definition, since that might go too far if
22346 a specialization was declared, but not defined. */
22347
22348 /* Fetch the more general template. */
22349 tmpl = DECL_TI_TEMPLATE (tmpl);
22350 }
22351
22352 return tmpl;
22353 }
22354
22355 /* Returns true if we need to instantiate this template instance even if we
22356 know we aren't going to emit it. */
22357
22358 bool
22359 always_instantiate_p (tree decl)
22360 {
22361 /* We always instantiate inline functions so that we can inline them. An
22362 explicit instantiation declaration prohibits implicit instantiation of
22363 non-inline functions. With high levels of optimization, we would
22364 normally inline non-inline functions -- but we're not allowed to do
22365 that for "extern template" functions. Therefore, we check
22366 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22367 return ((TREE_CODE (decl) == FUNCTION_DECL
22368 && (DECL_DECLARED_INLINE_P (decl)
22369 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22370 /* And we need to instantiate static data members so that
22371 their initializers are available in integral constant
22372 expressions. */
22373 || (VAR_P (decl)
22374 && decl_maybe_constant_var_p (decl)));
22375 }
22376
22377 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22378 instantiate it now, modifying TREE_TYPE (fn). */
22379
22380 void
22381 maybe_instantiate_noexcept (tree fn)
22382 {
22383 tree fntype, spec, noex, clone;
22384
22385 /* Don't instantiate a noexcept-specification from template context. */
22386 if (processing_template_decl)
22387 return;
22388
22389 if (DECL_CLONED_FUNCTION_P (fn))
22390 fn = DECL_CLONED_FUNCTION (fn);
22391 fntype = TREE_TYPE (fn);
22392 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22393
22394 if (!spec || !TREE_PURPOSE (spec))
22395 return;
22396
22397 noex = TREE_PURPOSE (spec);
22398
22399 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22400 {
22401 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22402 spec = get_defaulted_eh_spec (fn);
22403 else if (push_tinst_level (fn))
22404 {
22405 push_access_scope (fn);
22406 push_deferring_access_checks (dk_no_deferred);
22407 input_location = DECL_SOURCE_LOCATION (fn);
22408 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22409 DEFERRED_NOEXCEPT_ARGS (noex),
22410 tf_warning_or_error, fn,
22411 /*function_p=*/false,
22412 /*integral_constant_expression_p=*/true);
22413 pop_deferring_access_checks ();
22414 pop_access_scope (fn);
22415 pop_tinst_level ();
22416 spec = build_noexcept_spec (noex, tf_warning_or_error);
22417 if (spec == error_mark_node)
22418 spec = noexcept_false_spec;
22419 }
22420 else
22421 spec = noexcept_false_spec;
22422
22423 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22424 }
22425
22426 FOR_EACH_CLONE (clone, fn)
22427 {
22428 if (TREE_TYPE (clone) == fntype)
22429 TREE_TYPE (clone) = TREE_TYPE (fn);
22430 else
22431 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22432 }
22433 }
22434
22435 /* Produce the definition of D, a _DECL generated from a template. If
22436 DEFER_OK is true, then we don't have to actually do the
22437 instantiation now; we just have to do it sometime. Normally it is
22438 an error if this is an explicit instantiation but D is undefined.
22439 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22440 instantiated class template. */
22441
22442 tree
22443 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22444 {
22445 tree tmpl = DECL_TI_TEMPLATE (d);
22446 tree gen_args;
22447 tree args;
22448 tree td;
22449 tree code_pattern;
22450 tree spec;
22451 tree gen_tmpl;
22452 bool pattern_defined;
22453 location_t saved_loc = input_location;
22454 int saved_unevaluated_operand = cp_unevaluated_operand;
22455 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22456 bool external_p;
22457 bool deleted_p;
22458
22459 /* This function should only be used to instantiate templates for
22460 functions and static member variables. */
22461 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22462
22463 /* A concept is never instantiated. */
22464 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22465
22466 /* Variables are never deferred; if instantiation is required, they
22467 are instantiated right away. That allows for better code in the
22468 case that an expression refers to the value of the variable --
22469 if the variable has a constant value the referring expression can
22470 take advantage of that fact. */
22471 if (VAR_P (d))
22472 defer_ok = false;
22473
22474 /* Don't instantiate cloned functions. Instead, instantiate the
22475 functions they cloned. */
22476 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22477 d = DECL_CLONED_FUNCTION (d);
22478
22479 if (DECL_TEMPLATE_INSTANTIATED (d)
22480 || (TREE_CODE (d) == FUNCTION_DECL
22481 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22482 || DECL_TEMPLATE_SPECIALIZATION (d))
22483 /* D has already been instantiated or explicitly specialized, so
22484 there's nothing for us to do here.
22485
22486 It might seem reasonable to check whether or not D is an explicit
22487 instantiation, and, if so, stop here. But when an explicit
22488 instantiation is deferred until the end of the compilation,
22489 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22490 the instantiation. */
22491 return d;
22492
22493 /* Check to see whether we know that this template will be
22494 instantiated in some other file, as with "extern template"
22495 extension. */
22496 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22497
22498 /* In general, we do not instantiate such templates. */
22499 if (external_p && !always_instantiate_p (d))
22500 return d;
22501
22502 gen_tmpl = most_general_template (tmpl);
22503 gen_args = DECL_TI_ARGS (d);
22504
22505 if (tmpl != gen_tmpl)
22506 /* We should already have the extra args. */
22507 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22508 == TMPL_ARGS_DEPTH (gen_args));
22509 /* And what's in the hash table should match D. */
22510 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22511 || spec == NULL_TREE);
22512
22513 /* This needs to happen before any tsubsting. */
22514 if (! push_tinst_level (d))
22515 return d;
22516
22517 timevar_push (TV_TEMPLATE_INST);
22518
22519 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22520 for the instantiation. */
22521 td = template_for_substitution (d);
22522 args = gen_args;
22523
22524 if (VAR_P (d))
22525 {
22526 /* Look up an explicit specialization, if any. */
22527 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22528 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22529 if (elt && elt != error_mark_node)
22530 {
22531 td = TREE_VALUE (elt);
22532 args = TREE_PURPOSE (elt);
22533 }
22534 }
22535
22536 code_pattern = DECL_TEMPLATE_RESULT (td);
22537
22538 /* We should never be trying to instantiate a member of a class
22539 template or partial specialization. */
22540 gcc_assert (d != code_pattern);
22541
22542 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22543 || DECL_TEMPLATE_SPECIALIZATION (td))
22544 /* In the case of a friend template whose definition is provided
22545 outside the class, we may have too many arguments. Drop the
22546 ones we don't need. The same is true for specializations. */
22547 args = get_innermost_template_args
22548 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22549
22550 if (TREE_CODE (d) == FUNCTION_DECL)
22551 {
22552 deleted_p = DECL_DELETED_FN (code_pattern);
22553 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22554 && DECL_INITIAL (code_pattern) != error_mark_node)
22555 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22556 || deleted_p);
22557 }
22558 else
22559 {
22560 deleted_p = false;
22561 if (DECL_CLASS_SCOPE_P (code_pattern))
22562 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22563 || DECL_INLINE_VAR_P (code_pattern));
22564 else
22565 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22566 }
22567
22568 /* We may be in the middle of deferred access check. Disable it now. */
22569 push_deferring_access_checks (dk_no_deferred);
22570
22571 /* Unless an explicit instantiation directive has already determined
22572 the linkage of D, remember that a definition is available for
22573 this entity. */
22574 if (pattern_defined
22575 && !DECL_INTERFACE_KNOWN (d)
22576 && !DECL_NOT_REALLY_EXTERN (d))
22577 mark_definable (d);
22578
22579 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22580 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22581 input_location = DECL_SOURCE_LOCATION (d);
22582
22583 /* If D is a member of an explicitly instantiated class template,
22584 and no definition is available, treat it like an implicit
22585 instantiation. */
22586 if (!pattern_defined && expl_inst_class_mem_p
22587 && DECL_EXPLICIT_INSTANTIATION (d))
22588 {
22589 /* Leave linkage flags alone on instantiations with anonymous
22590 visibility. */
22591 if (TREE_PUBLIC (d))
22592 {
22593 DECL_NOT_REALLY_EXTERN (d) = 0;
22594 DECL_INTERFACE_KNOWN (d) = 0;
22595 }
22596 SET_DECL_IMPLICIT_INSTANTIATION (d);
22597 }
22598
22599 /* Defer all other templates, unless we have been explicitly
22600 forbidden from doing so. */
22601 if (/* If there is no definition, we cannot instantiate the
22602 template. */
22603 ! pattern_defined
22604 /* If it's OK to postpone instantiation, do so. */
22605 || defer_ok
22606 /* If this is a static data member that will be defined
22607 elsewhere, we don't want to instantiate the entire data
22608 member, but we do want to instantiate the initializer so that
22609 we can substitute that elsewhere. */
22610 || (external_p && VAR_P (d))
22611 /* Handle here a deleted function too, avoid generating
22612 its body (c++/61080). */
22613 || deleted_p)
22614 {
22615 /* The definition of the static data member is now required so
22616 we must substitute the initializer. */
22617 if (VAR_P (d)
22618 && !DECL_INITIAL (d)
22619 && DECL_INITIAL (code_pattern))
22620 {
22621 tree ns;
22622 tree init;
22623 bool const_init = false;
22624 bool enter_context = DECL_CLASS_SCOPE_P (d);
22625
22626 ns = decl_namespace_context (d);
22627 push_nested_namespace (ns);
22628 if (enter_context)
22629 push_nested_class (DECL_CONTEXT (d));
22630 init = tsubst_expr (DECL_INITIAL (code_pattern),
22631 args,
22632 tf_warning_or_error, NULL_TREE,
22633 /*integral_constant_expression_p=*/false);
22634 /* If instantiating the initializer involved instantiating this
22635 again, don't call cp_finish_decl twice. */
22636 if (!DECL_INITIAL (d))
22637 {
22638 /* Make sure the initializer is still constant, in case of
22639 circular dependency (template/instantiate6.C). */
22640 const_init
22641 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22642 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22643 /*asmspec_tree=*/NULL_TREE,
22644 LOOKUP_ONLYCONVERTING);
22645 }
22646 if (enter_context)
22647 pop_nested_class ();
22648 pop_nested_namespace (ns);
22649 }
22650
22651 /* We restore the source position here because it's used by
22652 add_pending_template. */
22653 input_location = saved_loc;
22654
22655 if (at_eof && !pattern_defined
22656 && DECL_EXPLICIT_INSTANTIATION (d)
22657 && DECL_NOT_REALLY_EXTERN (d))
22658 /* [temp.explicit]
22659
22660 The definition of a non-exported function template, a
22661 non-exported member function template, or a non-exported
22662 member function or static data member of a class template
22663 shall be present in every translation unit in which it is
22664 explicitly instantiated. */
22665 permerror (input_location, "explicit instantiation of %qD "
22666 "but no definition available", d);
22667
22668 /* If we're in unevaluated context, we just wanted to get the
22669 constant value; this isn't an odr use, so don't queue
22670 a full instantiation. */
22671 if (cp_unevaluated_operand != 0)
22672 goto out;
22673 /* ??? Historically, we have instantiated inline functions, even
22674 when marked as "extern template". */
22675 if (!(external_p && VAR_P (d)))
22676 add_pending_template (d);
22677 goto out;
22678 }
22679 /* Tell the repository that D is available in this translation unit
22680 -- and see if it is supposed to be instantiated here. */
22681 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22682 {
22683 /* In a PCH file, despite the fact that the repository hasn't
22684 requested instantiation in the PCH it is still possible that
22685 an instantiation will be required in a file that includes the
22686 PCH. */
22687 if (pch_file)
22688 add_pending_template (d);
22689 /* Instantiate inline functions so that the inliner can do its
22690 job, even though we'll not be emitting a copy of this
22691 function. */
22692 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22693 goto out;
22694 }
22695
22696 bool push_to_top, nested;
22697 tree fn_context;
22698 fn_context = decl_function_context (d);
22699 nested = current_function_decl != NULL_TREE;
22700 push_to_top = !(nested && fn_context == current_function_decl);
22701
22702 vec<tree> omp_privatization_save;
22703 if (nested)
22704 save_omp_privatization_clauses (omp_privatization_save);
22705
22706 if (push_to_top)
22707 push_to_top_level ();
22708 else
22709 {
22710 push_function_context ();
22711 cp_unevaluated_operand = 0;
22712 c_inhibit_evaluation_warnings = 0;
22713 }
22714
22715 /* Mark D as instantiated so that recursive calls to
22716 instantiate_decl do not try to instantiate it again. */
22717 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22718
22719 /* Regenerate the declaration in case the template has been modified
22720 by a subsequent redeclaration. */
22721 regenerate_decl_from_template (d, td, args);
22722
22723 /* We already set the file and line above. Reset them now in case
22724 they changed as a result of calling regenerate_decl_from_template. */
22725 input_location = DECL_SOURCE_LOCATION (d);
22726
22727 if (VAR_P (d))
22728 {
22729 tree init;
22730 bool const_init = false;
22731
22732 /* Clear out DECL_RTL; whatever was there before may not be right
22733 since we've reset the type of the declaration. */
22734 SET_DECL_RTL (d, NULL);
22735 DECL_IN_AGGR_P (d) = 0;
22736
22737 /* The initializer is placed in DECL_INITIAL by
22738 regenerate_decl_from_template so we don't need to
22739 push/pop_access_scope again here. Pull it out so that
22740 cp_finish_decl can process it. */
22741 init = DECL_INITIAL (d);
22742 DECL_INITIAL (d) = NULL_TREE;
22743 DECL_INITIALIZED_P (d) = 0;
22744
22745 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22746 initializer. That function will defer actual emission until
22747 we have a chance to determine linkage. */
22748 DECL_EXTERNAL (d) = 0;
22749
22750 /* Enter the scope of D so that access-checking works correctly. */
22751 bool enter_context = DECL_CLASS_SCOPE_P (d);
22752 if (enter_context)
22753 push_nested_class (DECL_CONTEXT (d));
22754
22755 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22756 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22757
22758 if (enter_context)
22759 pop_nested_class ();
22760
22761 if (variable_template_p (gen_tmpl))
22762 note_variable_template_instantiation (d);
22763 }
22764 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22765 synthesize_method (d);
22766 else if (TREE_CODE (d) == FUNCTION_DECL)
22767 {
22768 hash_map<tree, tree> *saved_local_specializations;
22769 tree tmpl_parm;
22770 tree spec_parm;
22771 tree block = NULL_TREE;
22772 tree lambda_ctx = NULL_TREE;
22773
22774 /* Save away the current list, in case we are instantiating one
22775 template from within the body of another. */
22776 saved_local_specializations = local_specializations;
22777
22778 /* Set up the list of local specializations. */
22779 local_specializations = new hash_map<tree, tree>;
22780
22781 /* Set up context. */
22782 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22783 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22784 block = push_stmt_list ();
22785 else
22786 {
22787 if (push_to_top && LAMBDA_FUNCTION_P (d))
22788 {
22789 /* When instantiating a lambda's templated function
22790 operator, we need to push the non-lambda class scope
22791 of the lambda itself so that the nested function
22792 stack is sufficiently correct to deal with this
22793 capture. */
22794 lambda_ctx = DECL_CONTEXT (d);
22795 do
22796 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22797 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22798 if (lambda_ctx)
22799 push_nested_class (lambda_ctx);
22800 }
22801 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22802 }
22803
22804 /* Some typedefs referenced from within the template code need to be
22805 access checked at template instantiation time, i.e now. These
22806 types were added to the template at parsing time. Let's get those
22807 and perform the access checks then. */
22808 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22809 args);
22810
22811 /* Create substitution entries for the parameters. */
22812 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22813 spec_parm = DECL_ARGUMENTS (d);
22814 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22815 {
22816 register_local_specialization (spec_parm, tmpl_parm);
22817 spec_parm = skip_artificial_parms_for (d, spec_parm);
22818 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22819 }
22820 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22821 {
22822 if (!DECL_PACK_P (tmpl_parm))
22823 {
22824 register_local_specialization (spec_parm, tmpl_parm);
22825 spec_parm = DECL_CHAIN (spec_parm);
22826 }
22827 else
22828 {
22829 /* Register the (value) argument pack as a specialization of
22830 TMPL_PARM, then move on. */
22831 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22832 register_local_specialization (argpack, tmpl_parm);
22833 }
22834 }
22835 gcc_assert (!spec_parm);
22836
22837 /* Substitute into the body of the function. */
22838 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22839 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22840 tf_warning_or_error, tmpl);
22841 else
22842 {
22843 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22844 tf_warning_or_error, tmpl,
22845 /*integral_constant_expression_p=*/false);
22846
22847 /* Set the current input_location to the end of the function
22848 so that finish_function knows where we are. */
22849 input_location
22850 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22851
22852 /* Remember if we saw an infinite loop in the template. */
22853 current_function_infinite_loop
22854 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22855 }
22856
22857 /* We don't need the local specializations any more. */
22858 delete local_specializations;
22859 local_specializations = saved_local_specializations;
22860
22861 /* Finish the function. */
22862 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22863 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22864 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22865 else
22866 {
22867 d = finish_function (0);
22868 expand_or_defer_fn (d);
22869 }
22870 if (lambda_ctx)
22871 pop_nested_class ();
22872
22873 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22874 cp_check_omp_declare_reduction (d);
22875 }
22876
22877 /* We're not deferring instantiation any more. */
22878 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22879
22880 if (push_to_top)
22881 pop_from_top_level ();
22882 else
22883 pop_function_context ();
22884
22885 if (nested)
22886 restore_omp_privatization_clauses (omp_privatization_save);
22887
22888 out:
22889 pop_deferring_access_checks ();
22890 timevar_pop (TV_TEMPLATE_INST);
22891 pop_tinst_level ();
22892 input_location = saved_loc;
22893 cp_unevaluated_operand = saved_unevaluated_operand;
22894 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22895
22896 return d;
22897 }
22898
22899 /* Run through the list of templates that we wish we could
22900 instantiate, and instantiate any we can. RETRIES is the
22901 number of times we retry pending template instantiation. */
22902
22903 void
22904 instantiate_pending_templates (int retries)
22905 {
22906 int reconsider;
22907 location_t saved_loc = input_location;
22908
22909 /* Instantiating templates may trigger vtable generation. This in turn
22910 may require further template instantiations. We place a limit here
22911 to avoid infinite loop. */
22912 if (pending_templates && retries >= max_tinst_depth)
22913 {
22914 tree decl = pending_templates->tinst->decl;
22915
22916 fatal_error (input_location,
22917 "template instantiation depth exceeds maximum of %d"
22918 " instantiating %q+D, possibly from virtual table generation"
22919 " (use -ftemplate-depth= to increase the maximum)",
22920 max_tinst_depth, decl);
22921 if (TREE_CODE (decl) == FUNCTION_DECL)
22922 /* Pretend that we defined it. */
22923 DECL_INITIAL (decl) = error_mark_node;
22924 return;
22925 }
22926
22927 do
22928 {
22929 struct pending_template **t = &pending_templates;
22930 struct pending_template *last = NULL;
22931 reconsider = 0;
22932 while (*t)
22933 {
22934 tree instantiation = reopen_tinst_level ((*t)->tinst);
22935 bool complete = false;
22936
22937 if (TYPE_P (instantiation))
22938 {
22939 tree fn;
22940
22941 if (!COMPLETE_TYPE_P (instantiation))
22942 {
22943 instantiate_class_template (instantiation);
22944 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22945 for (fn = TYPE_METHODS (instantiation);
22946 fn;
22947 fn = TREE_CHAIN (fn))
22948 if (! DECL_ARTIFICIAL (fn))
22949 instantiate_decl (fn,
22950 /*defer_ok=*/false,
22951 /*expl_inst_class_mem_p=*/false);
22952 if (COMPLETE_TYPE_P (instantiation))
22953 reconsider = 1;
22954 }
22955
22956 complete = COMPLETE_TYPE_P (instantiation);
22957 }
22958 else
22959 {
22960 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
22961 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
22962 {
22963 instantiation
22964 = instantiate_decl (instantiation,
22965 /*defer_ok=*/false,
22966 /*expl_inst_class_mem_p=*/false);
22967 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
22968 reconsider = 1;
22969 }
22970
22971 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
22972 || DECL_TEMPLATE_INSTANTIATED (instantiation));
22973 }
22974
22975 if (complete)
22976 /* If INSTANTIATION has been instantiated, then we don't
22977 need to consider it again in the future. */
22978 *t = (*t)->next;
22979 else
22980 {
22981 last = *t;
22982 t = &(*t)->next;
22983 }
22984 tinst_depth = 0;
22985 current_tinst_level = NULL;
22986 }
22987 last_pending_template = last;
22988 }
22989 while (reconsider);
22990
22991 input_location = saved_loc;
22992 }
22993
22994 /* Substitute ARGVEC into T, which is a list of initializers for
22995 either base class or a non-static data member. The TREE_PURPOSEs
22996 are DECLs, and the TREE_VALUEs are the initializer values. Used by
22997 instantiate_decl. */
22998
22999 static tree
23000 tsubst_initializer_list (tree t, tree argvec)
23001 {
23002 tree inits = NULL_TREE;
23003
23004 for (; t; t = TREE_CHAIN (t))
23005 {
23006 tree decl;
23007 tree init;
23008 tree expanded_bases = NULL_TREE;
23009 tree expanded_arguments = NULL_TREE;
23010 int i, len = 1;
23011
23012 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23013 {
23014 tree expr;
23015 tree arg;
23016
23017 /* Expand the base class expansion type into separate base
23018 classes. */
23019 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23020 tf_warning_or_error,
23021 NULL_TREE);
23022 if (expanded_bases == error_mark_node)
23023 continue;
23024
23025 /* We'll be building separate TREE_LISTs of arguments for
23026 each base. */
23027 len = TREE_VEC_LENGTH (expanded_bases);
23028 expanded_arguments = make_tree_vec (len);
23029 for (i = 0; i < len; i++)
23030 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23031
23032 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23033 expand each argument in the TREE_VALUE of t. */
23034 expr = make_node (EXPR_PACK_EXPANSION);
23035 PACK_EXPANSION_LOCAL_P (expr) = true;
23036 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23037 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23038
23039 if (TREE_VALUE (t) == void_type_node)
23040 /* VOID_TYPE_NODE is used to indicate
23041 value-initialization. */
23042 {
23043 for (i = 0; i < len; i++)
23044 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23045 }
23046 else
23047 {
23048 /* Substitute parameter packs into each argument in the
23049 TREE_LIST. */
23050 in_base_initializer = 1;
23051 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23052 {
23053 tree expanded_exprs;
23054
23055 /* Expand the argument. */
23056 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23057 expanded_exprs
23058 = tsubst_pack_expansion (expr, argvec,
23059 tf_warning_or_error,
23060 NULL_TREE);
23061 if (expanded_exprs == error_mark_node)
23062 continue;
23063
23064 /* Prepend each of the expanded expressions to the
23065 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23066 for (i = 0; i < len; i++)
23067 {
23068 TREE_VEC_ELT (expanded_arguments, i) =
23069 tree_cons (NULL_TREE,
23070 TREE_VEC_ELT (expanded_exprs, i),
23071 TREE_VEC_ELT (expanded_arguments, i));
23072 }
23073 }
23074 in_base_initializer = 0;
23075
23076 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23077 since we built them backwards. */
23078 for (i = 0; i < len; i++)
23079 {
23080 TREE_VEC_ELT (expanded_arguments, i) =
23081 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23082 }
23083 }
23084 }
23085
23086 for (i = 0; i < len; ++i)
23087 {
23088 if (expanded_bases)
23089 {
23090 decl = TREE_VEC_ELT (expanded_bases, i);
23091 decl = expand_member_init (decl);
23092 init = TREE_VEC_ELT (expanded_arguments, i);
23093 }
23094 else
23095 {
23096 tree tmp;
23097 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23098 tf_warning_or_error, NULL_TREE);
23099
23100 decl = expand_member_init (decl);
23101 if (decl && !DECL_P (decl))
23102 in_base_initializer = 1;
23103
23104 init = TREE_VALUE (t);
23105 tmp = init;
23106 if (init != void_type_node)
23107 init = tsubst_expr (init, argvec,
23108 tf_warning_or_error, NULL_TREE,
23109 /*integral_constant_expression_p=*/false);
23110 if (init == NULL_TREE && tmp != NULL_TREE)
23111 /* If we had an initializer but it instantiated to nothing,
23112 value-initialize the object. This will only occur when
23113 the initializer was a pack expansion where the parameter
23114 packs used in that expansion were of length zero. */
23115 init = void_type_node;
23116 in_base_initializer = 0;
23117 }
23118
23119 if (decl)
23120 {
23121 init = build_tree_list (decl, init);
23122 TREE_CHAIN (init) = inits;
23123 inits = init;
23124 }
23125 }
23126 }
23127 return inits;
23128 }
23129
23130 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23131
23132 static void
23133 set_current_access_from_decl (tree decl)
23134 {
23135 if (TREE_PRIVATE (decl))
23136 current_access_specifier = access_private_node;
23137 else if (TREE_PROTECTED (decl))
23138 current_access_specifier = access_protected_node;
23139 else
23140 current_access_specifier = access_public_node;
23141 }
23142
23143 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23144 is the instantiation (which should have been created with
23145 start_enum) and ARGS are the template arguments to use. */
23146
23147 static void
23148 tsubst_enum (tree tag, tree newtag, tree args)
23149 {
23150 tree e;
23151
23152 if (SCOPED_ENUM_P (newtag))
23153 begin_scope (sk_scoped_enum, newtag);
23154
23155 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23156 {
23157 tree value;
23158 tree decl;
23159
23160 decl = TREE_VALUE (e);
23161 /* Note that in a template enum, the TREE_VALUE is the
23162 CONST_DECL, not the corresponding INTEGER_CST. */
23163 value = tsubst_expr (DECL_INITIAL (decl),
23164 args, tf_warning_or_error, NULL_TREE,
23165 /*integral_constant_expression_p=*/true);
23166
23167 /* Give this enumeration constant the correct access. */
23168 set_current_access_from_decl (decl);
23169
23170 /* Actually build the enumerator itself. Here we're assuming that
23171 enumerators can't have dependent attributes. */
23172 build_enumerator (DECL_NAME (decl), value, newtag,
23173 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23174 }
23175
23176 if (SCOPED_ENUM_P (newtag))
23177 finish_scope ();
23178
23179 finish_enum_value_list (newtag);
23180 finish_enum (newtag);
23181
23182 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23183 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23184 }
23185
23186 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23187 its type -- but without substituting the innermost set of template
23188 arguments. So, innermost set of template parameters will appear in
23189 the type. */
23190
23191 tree
23192 get_mostly_instantiated_function_type (tree decl)
23193 {
23194 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23195 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23196 }
23197
23198 /* Return truthvalue if we're processing a template different from
23199 the last one involved in diagnostics. */
23200 bool
23201 problematic_instantiation_changed (void)
23202 {
23203 return current_tinst_level != last_error_tinst_level;
23204 }
23205
23206 /* Remember current template involved in diagnostics. */
23207 void
23208 record_last_problematic_instantiation (void)
23209 {
23210 last_error_tinst_level = current_tinst_level;
23211 }
23212
23213 struct tinst_level *
23214 current_instantiation (void)
23215 {
23216 return current_tinst_level;
23217 }
23218
23219 /* Return TRUE if current_function_decl is being instantiated, false
23220 otherwise. */
23221
23222 bool
23223 instantiating_current_function_p (void)
23224 {
23225 return (current_instantiation ()
23226 && current_instantiation ()->decl == current_function_decl);
23227 }
23228
23229 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23230 type. Return zero for ok, nonzero for disallowed. Issue error and
23231 warning messages under control of COMPLAIN. */
23232
23233 static int
23234 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23235 {
23236 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23237 return 0;
23238 else if (POINTER_TYPE_P (type))
23239 return 0;
23240 else if (TYPE_PTRMEM_P (type))
23241 return 0;
23242 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23243 return 0;
23244 else if (TREE_CODE (type) == TYPENAME_TYPE)
23245 return 0;
23246 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23247 return 0;
23248 else if (TREE_CODE (type) == NULLPTR_TYPE)
23249 return 0;
23250 /* A bound template template parm could later be instantiated to have a valid
23251 nontype parm type via an alias template. */
23252 else if (cxx_dialect >= cxx11
23253 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23254 return 0;
23255
23256 if (complain & tf_error)
23257 {
23258 if (type == error_mark_node)
23259 inform (input_location, "invalid template non-type parameter");
23260 else
23261 error ("%q#T is not a valid type for a template non-type parameter",
23262 type);
23263 }
23264 return 1;
23265 }
23266
23267 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23268 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23269
23270 static bool
23271 dependent_type_p_r (tree type)
23272 {
23273 tree scope;
23274
23275 /* [temp.dep.type]
23276
23277 A type is dependent if it is:
23278
23279 -- a template parameter. Template template parameters are types
23280 for us (since TYPE_P holds true for them) so we handle
23281 them here. */
23282 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23283 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23284 return true;
23285 /* -- a qualified-id with a nested-name-specifier which contains a
23286 class-name that names a dependent type or whose unqualified-id
23287 names a dependent type. */
23288 if (TREE_CODE (type) == TYPENAME_TYPE)
23289 return true;
23290
23291 /* An alias template specialization can be dependent even if the
23292 resulting type is not. */
23293 if (dependent_alias_template_spec_p (type))
23294 return true;
23295
23296 /* -- a cv-qualified type where the cv-unqualified type is
23297 dependent.
23298 No code is necessary for this bullet; the code below handles
23299 cv-qualified types, and we don't want to strip aliases with
23300 TYPE_MAIN_VARIANT because of DR 1558. */
23301 /* -- a compound type constructed from any dependent type. */
23302 if (TYPE_PTRMEM_P (type))
23303 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23304 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23305 (type)));
23306 else if (TYPE_PTR_P (type)
23307 || TREE_CODE (type) == REFERENCE_TYPE)
23308 return dependent_type_p (TREE_TYPE (type));
23309 else if (TREE_CODE (type) == FUNCTION_TYPE
23310 || TREE_CODE (type) == METHOD_TYPE)
23311 {
23312 tree arg_type;
23313
23314 if (dependent_type_p (TREE_TYPE (type)))
23315 return true;
23316 for (arg_type = TYPE_ARG_TYPES (type);
23317 arg_type;
23318 arg_type = TREE_CHAIN (arg_type))
23319 if (dependent_type_p (TREE_VALUE (arg_type)))
23320 return true;
23321 return false;
23322 }
23323 /* -- an array type constructed from any dependent type or whose
23324 size is specified by a constant expression that is
23325 value-dependent.
23326
23327 We checked for type- and value-dependence of the bounds in
23328 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23329 if (TREE_CODE (type) == ARRAY_TYPE)
23330 {
23331 if (TYPE_DOMAIN (type)
23332 && dependent_type_p (TYPE_DOMAIN (type)))
23333 return true;
23334 return dependent_type_p (TREE_TYPE (type));
23335 }
23336
23337 /* -- a template-id in which either the template name is a template
23338 parameter ... */
23339 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23340 return true;
23341 /* ... or any of the template arguments is a dependent type or
23342 an expression that is type-dependent or value-dependent. */
23343 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23344 && (any_dependent_template_arguments_p
23345 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23346 return true;
23347
23348 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23349 dependent; if the argument of the `typeof' expression is not
23350 type-dependent, then it should already been have resolved. */
23351 if (TREE_CODE (type) == TYPEOF_TYPE
23352 || TREE_CODE (type) == DECLTYPE_TYPE
23353 || TREE_CODE (type) == UNDERLYING_TYPE)
23354 return true;
23355
23356 /* A template argument pack is dependent if any of its packed
23357 arguments are. */
23358 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23359 {
23360 tree args = ARGUMENT_PACK_ARGS (type);
23361 int i, len = TREE_VEC_LENGTH (args);
23362 for (i = 0; i < len; ++i)
23363 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23364 return true;
23365 }
23366
23367 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23368 be template parameters. */
23369 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23370 return true;
23371
23372 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23373 return true;
23374
23375 /* The standard does not specifically mention types that are local
23376 to template functions or local classes, but they should be
23377 considered dependent too. For example:
23378
23379 template <int I> void f() {
23380 enum E { a = I };
23381 S<sizeof (E)> s;
23382 }
23383
23384 The size of `E' cannot be known until the value of `I' has been
23385 determined. Therefore, `E' must be considered dependent. */
23386 scope = TYPE_CONTEXT (type);
23387 if (scope && TYPE_P (scope))
23388 return dependent_type_p (scope);
23389 /* Don't use type_dependent_expression_p here, as it can lead
23390 to infinite recursion trying to determine whether a lambda
23391 nested in a lambda is dependent (c++/47687). */
23392 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23393 && DECL_LANG_SPECIFIC (scope)
23394 && DECL_TEMPLATE_INFO (scope)
23395 && (any_dependent_template_arguments_p
23396 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23397 return true;
23398
23399 /* Other types are non-dependent. */
23400 return false;
23401 }
23402
23403 /* Returns TRUE if TYPE is dependent, in the sense of
23404 [temp.dep.type]. Note that a NULL type is considered dependent. */
23405
23406 bool
23407 dependent_type_p (tree type)
23408 {
23409 /* If there are no template parameters in scope, then there can't be
23410 any dependent types. */
23411 if (!processing_template_decl)
23412 {
23413 /* If we are not processing a template, then nobody should be
23414 providing us with a dependent type. */
23415 gcc_assert (type);
23416 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23417 return false;
23418 }
23419
23420 /* If the type is NULL, we have not computed a type for the entity
23421 in question; in that case, the type is dependent. */
23422 if (!type)
23423 return true;
23424
23425 /* Erroneous types can be considered non-dependent. */
23426 if (type == error_mark_node)
23427 return false;
23428
23429 /* If we have not already computed the appropriate value for TYPE,
23430 do so now. */
23431 if (!TYPE_DEPENDENT_P_VALID (type))
23432 {
23433 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23434 TYPE_DEPENDENT_P_VALID (type) = 1;
23435 }
23436
23437 return TYPE_DEPENDENT_P (type);
23438 }
23439
23440 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23441 lookup. In other words, a dependent type that is not the current
23442 instantiation. */
23443
23444 bool
23445 dependent_scope_p (tree scope)
23446 {
23447 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23448 && !currently_open_class (scope));
23449 }
23450
23451 /* T is a SCOPE_REF; return whether we need to consider it
23452 instantiation-dependent so that we can check access at instantiation
23453 time even though we know which member it resolves to. */
23454
23455 static bool
23456 instantiation_dependent_scope_ref_p (tree t)
23457 {
23458 if (DECL_P (TREE_OPERAND (t, 1))
23459 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23460 && accessible_in_template_p (TREE_OPERAND (t, 0),
23461 TREE_OPERAND (t, 1)))
23462 return false;
23463 else
23464 return true;
23465 }
23466
23467 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23468 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23469 expression. */
23470
23471 /* Note that this predicate is not appropriate for general expressions;
23472 only constant expressions (that satisfy potential_constant_expression)
23473 can be tested for value dependence. */
23474
23475 bool
23476 value_dependent_expression_p (tree expression)
23477 {
23478 if (!processing_template_decl || expression == NULL_TREE)
23479 return false;
23480
23481 /* A name declared with a dependent type. */
23482 if (DECL_P (expression) && type_dependent_expression_p (expression))
23483 return true;
23484
23485 switch (TREE_CODE (expression))
23486 {
23487 case BASELINK:
23488 /* A dependent member function of the current instantiation. */
23489 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23490
23491 case FUNCTION_DECL:
23492 /* A dependent member function of the current instantiation. */
23493 if (DECL_CLASS_SCOPE_P (expression)
23494 && dependent_type_p (DECL_CONTEXT (expression)))
23495 return true;
23496 break;
23497
23498 case IDENTIFIER_NODE:
23499 /* A name that has not been looked up -- must be dependent. */
23500 return true;
23501
23502 case TEMPLATE_PARM_INDEX:
23503 /* A non-type template parm. */
23504 return true;
23505
23506 case CONST_DECL:
23507 /* A non-type template parm. */
23508 if (DECL_TEMPLATE_PARM_P (expression))
23509 return true;
23510 return value_dependent_expression_p (DECL_INITIAL (expression));
23511
23512 case VAR_DECL:
23513 /* A constant with literal type and is initialized
23514 with an expression that is value-dependent.
23515
23516 Note that a non-dependent parenthesized initializer will have
23517 already been replaced with its constant value, so if we see
23518 a TREE_LIST it must be dependent. */
23519 if (DECL_INITIAL (expression)
23520 && decl_constant_var_p (expression)
23521 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23522 /* cp_finish_decl doesn't fold reference initializers. */
23523 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23524 || type_dependent_expression_p (DECL_INITIAL (expression))
23525 || value_dependent_expression_p (DECL_INITIAL (expression))))
23526 return true;
23527 return false;
23528
23529 case DYNAMIC_CAST_EXPR:
23530 case STATIC_CAST_EXPR:
23531 case CONST_CAST_EXPR:
23532 case REINTERPRET_CAST_EXPR:
23533 case CAST_EXPR:
23534 /* These expressions are value-dependent if the type to which
23535 the cast occurs is dependent or the expression being casted
23536 is value-dependent. */
23537 {
23538 tree type = TREE_TYPE (expression);
23539
23540 if (dependent_type_p (type))
23541 return true;
23542
23543 /* A functional cast has a list of operands. */
23544 expression = TREE_OPERAND (expression, 0);
23545 if (!expression)
23546 {
23547 /* If there are no operands, it must be an expression such
23548 as "int()". This should not happen for aggregate types
23549 because it would form non-constant expressions. */
23550 gcc_assert (cxx_dialect >= cxx11
23551 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23552
23553 return false;
23554 }
23555
23556 if (TREE_CODE (expression) == TREE_LIST)
23557 return any_value_dependent_elements_p (expression);
23558
23559 return value_dependent_expression_p (expression);
23560 }
23561
23562 case SIZEOF_EXPR:
23563 if (SIZEOF_EXPR_TYPE_P (expression))
23564 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23565 /* FALLTHRU */
23566 case ALIGNOF_EXPR:
23567 case TYPEID_EXPR:
23568 /* A `sizeof' expression is value-dependent if the operand is
23569 type-dependent or is a pack expansion. */
23570 expression = TREE_OPERAND (expression, 0);
23571 if (PACK_EXPANSION_P (expression))
23572 return true;
23573 else if (TYPE_P (expression))
23574 return dependent_type_p (expression);
23575 return instantiation_dependent_uneval_expression_p (expression);
23576
23577 case AT_ENCODE_EXPR:
23578 /* An 'encode' expression is value-dependent if the operand is
23579 type-dependent. */
23580 expression = TREE_OPERAND (expression, 0);
23581 return dependent_type_p (expression);
23582
23583 case NOEXCEPT_EXPR:
23584 expression = TREE_OPERAND (expression, 0);
23585 return instantiation_dependent_uneval_expression_p (expression);
23586
23587 case SCOPE_REF:
23588 /* All instantiation-dependent expressions should also be considered
23589 value-dependent. */
23590 return instantiation_dependent_scope_ref_p (expression);
23591
23592 case COMPONENT_REF:
23593 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23594 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23595
23596 case NONTYPE_ARGUMENT_PACK:
23597 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23598 is value-dependent. */
23599 {
23600 tree values = ARGUMENT_PACK_ARGS (expression);
23601 int i, len = TREE_VEC_LENGTH (values);
23602
23603 for (i = 0; i < len; ++i)
23604 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23605 return true;
23606
23607 return false;
23608 }
23609
23610 case TRAIT_EXPR:
23611 {
23612 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23613 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23614 || (type2 ? dependent_type_p (type2) : false));
23615 }
23616
23617 case MODOP_EXPR:
23618 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23619 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23620
23621 case ARRAY_REF:
23622 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23623 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23624
23625 case ADDR_EXPR:
23626 {
23627 tree op = TREE_OPERAND (expression, 0);
23628 return (value_dependent_expression_p (op)
23629 || has_value_dependent_address (op));
23630 }
23631
23632 case REQUIRES_EXPR:
23633 /* Treat all requires-expressions as value-dependent so
23634 we don't try to fold them. */
23635 return true;
23636
23637 case TYPE_REQ:
23638 return dependent_type_p (TREE_OPERAND (expression, 0));
23639
23640 case CALL_EXPR:
23641 {
23642 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23643 return true;
23644 tree fn = get_callee_fndecl (expression);
23645 int i, nargs;
23646 nargs = call_expr_nargs (expression);
23647 for (i = 0; i < nargs; ++i)
23648 {
23649 tree op = CALL_EXPR_ARG (expression, i);
23650 /* In a call to a constexpr member function, look through the
23651 implicit ADDR_EXPR on the object argument so that it doesn't
23652 cause the call to be considered value-dependent. We also
23653 look through it in potential_constant_expression. */
23654 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23655 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23656 && TREE_CODE (op) == ADDR_EXPR)
23657 op = TREE_OPERAND (op, 0);
23658 if (value_dependent_expression_p (op))
23659 return true;
23660 }
23661 return false;
23662 }
23663
23664 case TEMPLATE_ID_EXPR:
23665 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23666 type-dependent. */
23667 return type_dependent_expression_p (expression)
23668 || variable_concept_p (TREE_OPERAND (expression, 0));
23669
23670 case CONSTRUCTOR:
23671 {
23672 unsigned ix;
23673 tree val;
23674 if (dependent_type_p (TREE_TYPE (expression)))
23675 return true;
23676 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23677 if (value_dependent_expression_p (val))
23678 return true;
23679 return false;
23680 }
23681
23682 case STMT_EXPR:
23683 /* Treat a GNU statement expression as dependent to avoid crashing
23684 under instantiate_non_dependent_expr; it can't be constant. */
23685 return true;
23686
23687 default:
23688 /* A constant expression is value-dependent if any subexpression is
23689 value-dependent. */
23690 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23691 {
23692 case tcc_reference:
23693 case tcc_unary:
23694 case tcc_comparison:
23695 case tcc_binary:
23696 case tcc_expression:
23697 case tcc_vl_exp:
23698 {
23699 int i, len = cp_tree_operand_length (expression);
23700
23701 for (i = 0; i < len; i++)
23702 {
23703 tree t = TREE_OPERAND (expression, i);
23704
23705 /* In some cases, some of the operands may be missing.l
23706 (For example, in the case of PREDECREMENT_EXPR, the
23707 amount to increment by may be missing.) That doesn't
23708 make the expression dependent. */
23709 if (t && value_dependent_expression_p (t))
23710 return true;
23711 }
23712 }
23713 break;
23714 default:
23715 break;
23716 }
23717 break;
23718 }
23719
23720 /* The expression is not value-dependent. */
23721 return false;
23722 }
23723
23724 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23725 [temp.dep.expr]. Note that an expression with no type is
23726 considered dependent. Other parts of the compiler arrange for an
23727 expression with type-dependent subexpressions to have no type, so
23728 this function doesn't have to be fully recursive. */
23729
23730 bool
23731 type_dependent_expression_p (tree expression)
23732 {
23733 if (!processing_template_decl)
23734 return false;
23735
23736 if (expression == NULL_TREE || expression == error_mark_node)
23737 return false;
23738
23739 /* An unresolved name is always dependent. */
23740 if (identifier_p (expression)
23741 || TREE_CODE (expression) == USING_DECL
23742 || TREE_CODE (expression) == WILDCARD_DECL)
23743 return true;
23744
23745 /* A fold expression is type-dependent. */
23746 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23747 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23748 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23749 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23750 return true;
23751
23752 /* Some expression forms are never type-dependent. */
23753 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23754 || TREE_CODE (expression) == SIZEOF_EXPR
23755 || TREE_CODE (expression) == ALIGNOF_EXPR
23756 || TREE_CODE (expression) == AT_ENCODE_EXPR
23757 || TREE_CODE (expression) == NOEXCEPT_EXPR
23758 || TREE_CODE (expression) == TRAIT_EXPR
23759 || TREE_CODE (expression) == TYPEID_EXPR
23760 || TREE_CODE (expression) == DELETE_EXPR
23761 || TREE_CODE (expression) == VEC_DELETE_EXPR
23762 || TREE_CODE (expression) == THROW_EXPR
23763 || TREE_CODE (expression) == REQUIRES_EXPR)
23764 return false;
23765
23766 /* The types of these expressions depends only on the type to which
23767 the cast occurs. */
23768 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23769 || TREE_CODE (expression) == STATIC_CAST_EXPR
23770 || TREE_CODE (expression) == CONST_CAST_EXPR
23771 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23772 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23773 || TREE_CODE (expression) == CAST_EXPR)
23774 return dependent_type_p (TREE_TYPE (expression));
23775
23776 /* The types of these expressions depends only on the type created
23777 by the expression. */
23778 if (TREE_CODE (expression) == NEW_EXPR
23779 || TREE_CODE (expression) == VEC_NEW_EXPR)
23780 {
23781 /* For NEW_EXPR tree nodes created inside a template, either
23782 the object type itself or a TREE_LIST may appear as the
23783 operand 1. */
23784 tree type = TREE_OPERAND (expression, 1);
23785 if (TREE_CODE (type) == TREE_LIST)
23786 /* This is an array type. We need to check array dimensions
23787 as well. */
23788 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23789 || value_dependent_expression_p
23790 (TREE_OPERAND (TREE_VALUE (type), 1));
23791 else
23792 return dependent_type_p (type);
23793 }
23794
23795 if (TREE_CODE (expression) == SCOPE_REF)
23796 {
23797 tree scope = TREE_OPERAND (expression, 0);
23798 tree name = TREE_OPERAND (expression, 1);
23799
23800 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23801 contains an identifier associated by name lookup with one or more
23802 declarations declared with a dependent type, or...a
23803 nested-name-specifier or qualified-id that names a member of an
23804 unknown specialization. */
23805 return (type_dependent_expression_p (name)
23806 || dependent_scope_p (scope));
23807 }
23808
23809 if (TREE_CODE (expression) == TEMPLATE_DECL
23810 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23811 return uses_outer_template_parms (expression);
23812
23813 if (TREE_CODE (expression) == STMT_EXPR)
23814 expression = stmt_expr_value_expr (expression);
23815
23816 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23817 {
23818 tree elt;
23819 unsigned i;
23820
23821 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23822 {
23823 if (type_dependent_expression_p (elt))
23824 return true;
23825 }
23826 return false;
23827 }
23828
23829 /* A static data member of the current instantiation with incomplete
23830 array type is type-dependent, as the definition and specializations
23831 can have different bounds. */
23832 if (VAR_P (expression)
23833 && DECL_CLASS_SCOPE_P (expression)
23834 && dependent_type_p (DECL_CONTEXT (expression))
23835 && VAR_HAD_UNKNOWN_BOUND (expression))
23836 return true;
23837
23838 /* An array of unknown bound depending on a variadic parameter, eg:
23839
23840 template<typename... Args>
23841 void foo (Args... args)
23842 {
23843 int arr[] = { args... };
23844 }
23845
23846 template<int... vals>
23847 void bar ()
23848 {
23849 int arr[] = { vals... };
23850 }
23851
23852 If the array has no length and has an initializer, it must be that
23853 we couldn't determine its length in cp_complete_array_type because
23854 it is dependent. */
23855 if (VAR_P (expression)
23856 && TREE_TYPE (expression) != NULL_TREE
23857 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23858 && !TYPE_DOMAIN (TREE_TYPE (expression))
23859 && DECL_INITIAL (expression))
23860 return true;
23861
23862 /* A function or variable template-id is type-dependent if it has any
23863 dependent template arguments. Note that we only consider the innermost
23864 template arguments here, since those are the ones that come from the
23865 template-id; the template arguments for the enclosing class do not make it
23866 type-dependent, they only make a member function value-dependent. */
23867 if (VAR_OR_FUNCTION_DECL_P (expression)
23868 && DECL_LANG_SPECIFIC (expression)
23869 && DECL_TEMPLATE_INFO (expression)
23870 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23871 && (any_dependent_template_arguments_p
23872 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23873 return true;
23874
23875 /* Always dependent, on the number of arguments if nothing else. */
23876 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23877 return true;
23878
23879 if (TREE_TYPE (expression) == unknown_type_node)
23880 {
23881 if (TREE_CODE (expression) == ADDR_EXPR)
23882 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23883 if (TREE_CODE (expression) == COMPONENT_REF
23884 || TREE_CODE (expression) == OFFSET_REF)
23885 {
23886 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23887 return true;
23888 expression = TREE_OPERAND (expression, 1);
23889 if (identifier_p (expression))
23890 return false;
23891 }
23892 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23893 if (TREE_CODE (expression) == SCOPE_REF)
23894 return false;
23895
23896 if (BASELINK_P (expression))
23897 {
23898 if (BASELINK_OPTYPE (expression)
23899 && dependent_type_p (BASELINK_OPTYPE (expression)))
23900 return true;
23901 expression = BASELINK_FUNCTIONS (expression);
23902 }
23903
23904 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23905 {
23906 if (any_dependent_template_arguments_p
23907 (TREE_OPERAND (expression, 1)))
23908 return true;
23909 expression = TREE_OPERAND (expression, 0);
23910 if (identifier_p (expression))
23911 return true;
23912 }
23913
23914 gcc_assert (TREE_CODE (expression) == OVERLOAD
23915 || TREE_CODE (expression) == FUNCTION_DECL);
23916
23917 while (expression)
23918 {
23919 if (type_dependent_expression_p (OVL_CURRENT (expression)))
23920 return true;
23921 expression = OVL_NEXT (expression);
23922 }
23923 return false;
23924 }
23925
23926 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23927
23928 /* Dependent type attributes might not have made it from the decl to
23929 the type yet. */
23930 if (DECL_P (expression)
23931 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
23932 return true;
23933
23934 return (dependent_type_p (TREE_TYPE (expression)));
23935 }
23936
23937 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23938 type-dependent if the expression refers to a member of the current
23939 instantiation and the type of the referenced member is dependent, or the
23940 class member access expression refers to a member of an unknown
23941 specialization.
23942
23943 This function returns true if the OBJECT in such a class member access
23944 expression is of an unknown specialization. */
23945
23946 bool
23947 type_dependent_object_expression_p (tree object)
23948 {
23949 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
23950 dependent. */
23951 if (TREE_CODE (object) == IDENTIFIER_NODE)
23952 return true;
23953 tree scope = TREE_TYPE (object);
23954 return (!scope || dependent_scope_p (scope));
23955 }
23956
23957 /* walk_tree callback function for instantiation_dependent_expression_p,
23958 below. Returns non-zero if a dependent subexpression is found. */
23959
23960 static tree
23961 instantiation_dependent_r (tree *tp, int *walk_subtrees,
23962 void * /*data*/)
23963 {
23964 if (TYPE_P (*tp))
23965 {
23966 /* We don't have to worry about decltype currently because decltype
23967 of an instantiation-dependent expr is a dependent type. This
23968 might change depending on the resolution of DR 1172. */
23969 *walk_subtrees = false;
23970 return NULL_TREE;
23971 }
23972 enum tree_code code = TREE_CODE (*tp);
23973 switch (code)
23974 {
23975 /* Don't treat an argument list as dependent just because it has no
23976 TREE_TYPE. */
23977 case TREE_LIST:
23978 case TREE_VEC:
23979 return NULL_TREE;
23980
23981 case TEMPLATE_PARM_INDEX:
23982 return *tp;
23983
23984 /* Handle expressions with type operands. */
23985 case SIZEOF_EXPR:
23986 case ALIGNOF_EXPR:
23987 case TYPEID_EXPR:
23988 case AT_ENCODE_EXPR:
23989 {
23990 tree op = TREE_OPERAND (*tp, 0);
23991 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
23992 op = TREE_TYPE (op);
23993 if (TYPE_P (op))
23994 {
23995 if (dependent_type_p (op))
23996 return *tp;
23997 else
23998 {
23999 *walk_subtrees = false;
24000 return NULL_TREE;
24001 }
24002 }
24003 break;
24004 }
24005
24006 case COMPONENT_REF:
24007 if (identifier_p (TREE_OPERAND (*tp, 1)))
24008 /* In a template, finish_class_member_access_expr creates a
24009 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24010 type-dependent, so that we can check access control at
24011 instantiation time (PR 42277). See also Core issue 1273. */
24012 return *tp;
24013 break;
24014
24015 case SCOPE_REF:
24016 if (instantiation_dependent_scope_ref_p (*tp))
24017 return *tp;
24018 else
24019 break;
24020
24021 /* Treat statement-expressions as dependent. */
24022 case BIND_EXPR:
24023 return *tp;
24024
24025 /* Treat requires-expressions as dependent. */
24026 case REQUIRES_EXPR:
24027 return *tp;
24028
24029 case CALL_EXPR:
24030 /* Treat calls to function concepts as dependent. */
24031 if (function_concept_check_p (*tp))
24032 return *tp;
24033 break;
24034
24035 case TEMPLATE_ID_EXPR:
24036 /* And variable concepts. */
24037 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24038 return *tp;
24039 break;
24040
24041 default:
24042 break;
24043 }
24044
24045 if (type_dependent_expression_p (*tp))
24046 return *tp;
24047 else
24048 return NULL_TREE;
24049 }
24050
24051 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24052 sense defined by the ABI:
24053
24054 "An expression is instantiation-dependent if it is type-dependent
24055 or value-dependent, or it has a subexpression that is type-dependent
24056 or value-dependent."
24057
24058 Except don't actually check value-dependence for unevaluated expressions,
24059 because in sizeof(i) we don't care about the value of i. Checking
24060 type-dependence will in turn check value-dependence of array bounds/template
24061 arguments as needed. */
24062
24063 bool
24064 instantiation_dependent_uneval_expression_p (tree expression)
24065 {
24066 tree result;
24067
24068 if (!processing_template_decl)
24069 return false;
24070
24071 if (expression == error_mark_node)
24072 return false;
24073
24074 result = cp_walk_tree_without_duplicates (&expression,
24075 instantiation_dependent_r, NULL);
24076 return result != NULL_TREE;
24077 }
24078
24079 /* As above, but also check value-dependence of the expression as a whole. */
24080
24081 bool
24082 instantiation_dependent_expression_p (tree expression)
24083 {
24084 return (instantiation_dependent_uneval_expression_p (expression)
24085 || value_dependent_expression_p (expression));
24086 }
24087
24088 /* Like type_dependent_expression_p, but it also works while not processing
24089 a template definition, i.e. during substitution or mangling. */
24090
24091 bool
24092 type_dependent_expression_p_push (tree expr)
24093 {
24094 bool b;
24095 ++processing_template_decl;
24096 b = type_dependent_expression_p (expr);
24097 --processing_template_decl;
24098 return b;
24099 }
24100
24101 /* Returns TRUE if ARGS contains a type-dependent expression. */
24102
24103 bool
24104 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24105 {
24106 unsigned int i;
24107 tree arg;
24108
24109 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24110 {
24111 if (type_dependent_expression_p (arg))
24112 return true;
24113 }
24114 return false;
24115 }
24116
24117 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24118 expressions) contains any type-dependent expressions. */
24119
24120 bool
24121 any_type_dependent_elements_p (const_tree list)
24122 {
24123 for (; list; list = TREE_CHAIN (list))
24124 if (type_dependent_expression_p (TREE_VALUE (list)))
24125 return true;
24126
24127 return false;
24128 }
24129
24130 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24131 expressions) contains any value-dependent expressions. */
24132
24133 bool
24134 any_value_dependent_elements_p (const_tree list)
24135 {
24136 for (; list; list = TREE_CHAIN (list))
24137 if (value_dependent_expression_p (TREE_VALUE (list)))
24138 return true;
24139
24140 return false;
24141 }
24142
24143 /* Returns TRUE if the ARG (a template argument) is dependent. */
24144
24145 bool
24146 dependent_template_arg_p (tree arg)
24147 {
24148 if (!processing_template_decl)
24149 return false;
24150
24151 /* Assume a template argument that was wrongly written by the user
24152 is dependent. This is consistent with what
24153 any_dependent_template_arguments_p [that calls this function]
24154 does. */
24155 if (!arg || arg == error_mark_node)
24156 return true;
24157
24158 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24159 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24160
24161 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24162 return true;
24163 if (TREE_CODE (arg) == TEMPLATE_DECL)
24164 {
24165 if (DECL_TEMPLATE_PARM_P (arg))
24166 return true;
24167 /* A member template of a dependent class is not necessarily
24168 type-dependent, but it is a dependent template argument because it
24169 will be a member of an unknown specialization to that template. */
24170 tree scope = CP_DECL_CONTEXT (arg);
24171 return TYPE_P (scope) && dependent_type_p (scope);
24172 }
24173 else if (ARGUMENT_PACK_P (arg))
24174 {
24175 tree args = ARGUMENT_PACK_ARGS (arg);
24176 int i, len = TREE_VEC_LENGTH (args);
24177 for (i = 0; i < len; ++i)
24178 {
24179 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24180 return true;
24181 }
24182
24183 return false;
24184 }
24185 else if (TYPE_P (arg))
24186 return dependent_type_p (arg);
24187 else
24188 return (type_dependent_expression_p (arg)
24189 || value_dependent_expression_p (arg));
24190 }
24191
24192 /* Returns true if ARGS (a collection of template arguments) contains
24193 any types that require structural equality testing. */
24194
24195 bool
24196 any_template_arguments_need_structural_equality_p (tree args)
24197 {
24198 int i;
24199 int j;
24200
24201 if (!args)
24202 return false;
24203 if (args == error_mark_node)
24204 return true;
24205
24206 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24207 {
24208 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24209 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24210 {
24211 tree arg = TREE_VEC_ELT (level, j);
24212 tree packed_args = NULL_TREE;
24213 int k, len = 1;
24214
24215 if (ARGUMENT_PACK_P (arg))
24216 {
24217 /* Look inside the argument pack. */
24218 packed_args = ARGUMENT_PACK_ARGS (arg);
24219 len = TREE_VEC_LENGTH (packed_args);
24220 }
24221
24222 for (k = 0; k < len; ++k)
24223 {
24224 if (packed_args)
24225 arg = TREE_VEC_ELT (packed_args, k);
24226
24227 if (error_operand_p (arg))
24228 return true;
24229 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24230 continue;
24231 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24232 return true;
24233 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24234 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24235 return true;
24236 }
24237 }
24238 }
24239
24240 return false;
24241 }
24242
24243 /* Returns true if ARGS (a collection of template arguments) contains
24244 any dependent arguments. */
24245
24246 bool
24247 any_dependent_template_arguments_p (const_tree args)
24248 {
24249 int i;
24250 int j;
24251
24252 if (!args)
24253 return false;
24254 if (args == error_mark_node)
24255 return true;
24256
24257 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24258 {
24259 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24260 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24261 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24262 return true;
24263 }
24264
24265 return false;
24266 }
24267
24268 /* Returns TRUE if the template TMPL is type-dependent. */
24269
24270 bool
24271 dependent_template_p (tree tmpl)
24272 {
24273 if (TREE_CODE (tmpl) == OVERLOAD)
24274 {
24275 while (tmpl)
24276 {
24277 if (dependent_template_p (OVL_CURRENT (tmpl)))
24278 return true;
24279 tmpl = OVL_NEXT (tmpl);
24280 }
24281 return false;
24282 }
24283
24284 /* Template template parameters are dependent. */
24285 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24286 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24287 return true;
24288 /* So are names that have not been looked up. */
24289 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24290 return true;
24291 return false;
24292 }
24293
24294 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24295
24296 bool
24297 dependent_template_id_p (tree tmpl, tree args)
24298 {
24299 return (dependent_template_p (tmpl)
24300 || any_dependent_template_arguments_p (args));
24301 }
24302
24303 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24304 are dependent. */
24305
24306 bool
24307 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24308 {
24309 int i;
24310
24311 if (!processing_template_decl)
24312 return false;
24313
24314 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24315 {
24316 tree decl = TREE_VEC_ELT (declv, i);
24317 tree init = TREE_VEC_ELT (initv, i);
24318 tree cond = TREE_VEC_ELT (condv, i);
24319 tree incr = TREE_VEC_ELT (incrv, i);
24320
24321 if (type_dependent_expression_p (decl)
24322 || TREE_CODE (decl) == SCOPE_REF)
24323 return true;
24324
24325 if (init && type_dependent_expression_p (init))
24326 return true;
24327
24328 if (type_dependent_expression_p (cond))
24329 return true;
24330
24331 if (COMPARISON_CLASS_P (cond)
24332 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24333 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24334 return true;
24335
24336 if (TREE_CODE (incr) == MODOP_EXPR)
24337 {
24338 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24339 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24340 return true;
24341 }
24342 else if (type_dependent_expression_p (incr))
24343 return true;
24344 else if (TREE_CODE (incr) == MODIFY_EXPR)
24345 {
24346 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24347 return true;
24348 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24349 {
24350 tree t = TREE_OPERAND (incr, 1);
24351 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24352 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24353 return true;
24354 }
24355 }
24356 }
24357
24358 return false;
24359 }
24360
24361 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24362 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24363 no such TYPE can be found. Note that this function peers inside
24364 uninstantiated templates and therefore should be used only in
24365 extremely limited situations. ONLY_CURRENT_P restricts this
24366 peering to the currently open classes hierarchy (which is required
24367 when comparing types). */
24368
24369 tree
24370 resolve_typename_type (tree type, bool only_current_p)
24371 {
24372 tree scope;
24373 tree name;
24374 tree decl;
24375 int quals;
24376 tree pushed_scope;
24377 tree result;
24378
24379 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24380
24381 scope = TYPE_CONTEXT (type);
24382 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24383 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24384 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24385 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24386 identifier of the TYPENAME_TYPE anymore.
24387 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24388 TYPENAME_TYPE instead, we avoid messing up with a possible
24389 typedef variant case. */
24390 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24391
24392 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24393 it first before we can figure out what NAME refers to. */
24394 if (TREE_CODE (scope) == TYPENAME_TYPE)
24395 {
24396 if (TYPENAME_IS_RESOLVING_P (scope))
24397 /* Given a class template A with a dependent base with nested type C,
24398 typedef typename A::C::C C will land us here, as trying to resolve
24399 the initial A::C leads to the local C typedef, which leads back to
24400 A::C::C. So we break the recursion now. */
24401 return type;
24402 else
24403 scope = resolve_typename_type (scope, only_current_p);
24404 }
24405 /* If we don't know what SCOPE refers to, then we cannot resolve the
24406 TYPENAME_TYPE. */
24407 if (!CLASS_TYPE_P (scope))
24408 return type;
24409 /* If this is a typedef, we don't want to look inside (c++/11987). */
24410 if (typedef_variant_p (type))
24411 return type;
24412 /* If SCOPE isn't the template itself, it will not have a valid
24413 TYPE_FIELDS list. */
24414 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24415 /* scope is either the template itself or a compatible instantiation
24416 like X<T>, so look up the name in the original template. */
24417 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24418 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24419 gcc_checking_assert (uses_template_parms (scope));
24420 /* If scope has no fields, it can't be a current instantiation. Check this
24421 before currently_open_class to avoid infinite recursion (71515). */
24422 if (!TYPE_FIELDS (scope))
24423 return type;
24424 /* If the SCOPE is not the current instantiation, there's no reason
24425 to look inside it. */
24426 if (only_current_p && !currently_open_class (scope))
24427 return type;
24428 /* Enter the SCOPE so that name lookup will be resolved as if we
24429 were in the class definition. In particular, SCOPE will no
24430 longer be considered a dependent type. */
24431 pushed_scope = push_scope (scope);
24432 /* Look up the declaration. */
24433 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24434 tf_warning_or_error);
24435
24436 result = NULL_TREE;
24437
24438 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24439 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24440 if (!decl)
24441 /*nop*/;
24442 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24443 && TREE_CODE (decl) == TYPE_DECL)
24444 {
24445 result = TREE_TYPE (decl);
24446 if (result == error_mark_node)
24447 result = NULL_TREE;
24448 }
24449 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24450 && DECL_CLASS_TEMPLATE_P (decl))
24451 {
24452 tree tmpl;
24453 tree args;
24454 /* Obtain the template and the arguments. */
24455 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24456 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24457 /* Instantiate the template. */
24458 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24459 /*entering_scope=*/0,
24460 tf_error | tf_user);
24461 if (result == error_mark_node)
24462 result = NULL_TREE;
24463 }
24464
24465 /* Leave the SCOPE. */
24466 if (pushed_scope)
24467 pop_scope (pushed_scope);
24468
24469 /* If we failed to resolve it, return the original typename. */
24470 if (!result)
24471 return type;
24472
24473 /* If lookup found a typename type, resolve that too. */
24474 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24475 {
24476 /* Ill-formed programs can cause infinite recursion here, so we
24477 must catch that. */
24478 TYPENAME_IS_RESOLVING_P (result) = 1;
24479 result = resolve_typename_type (result, only_current_p);
24480 TYPENAME_IS_RESOLVING_P (result) = 0;
24481 }
24482
24483 /* Qualify the resulting type. */
24484 quals = cp_type_quals (type);
24485 if (quals)
24486 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24487
24488 return result;
24489 }
24490
24491 /* EXPR is an expression which is not type-dependent. Return a proxy
24492 for EXPR that can be used to compute the types of larger
24493 expressions containing EXPR. */
24494
24495 tree
24496 build_non_dependent_expr (tree expr)
24497 {
24498 tree inner_expr;
24499
24500 /* When checking, try to get a constant value for all non-dependent
24501 expressions in order to expose bugs in *_dependent_expression_p
24502 and constexpr. This can affect code generation, see PR70704, so
24503 only do this for -fchecking=2. */
24504 if (flag_checking > 1
24505 && cxx_dialect >= cxx11
24506 /* Don't do this during nsdmi parsing as it can lead to
24507 unexpected recursive instantiations. */
24508 && !parsing_nsdmi ()
24509 /* Don't do this during concept expansion either and for
24510 the same reason. */
24511 && !expanding_concept ())
24512 fold_non_dependent_expr (expr);
24513
24514 /* Preserve OVERLOADs; the functions must be available to resolve
24515 types. */
24516 inner_expr = expr;
24517 if (TREE_CODE (inner_expr) == STMT_EXPR)
24518 inner_expr = stmt_expr_value_expr (inner_expr);
24519 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24520 inner_expr = TREE_OPERAND (inner_expr, 0);
24521 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24522 inner_expr = TREE_OPERAND (inner_expr, 1);
24523 if (is_overloaded_fn (inner_expr)
24524 || TREE_CODE (inner_expr) == OFFSET_REF)
24525 return expr;
24526 /* There is no need to return a proxy for a variable. */
24527 if (VAR_P (expr))
24528 return expr;
24529 /* Preserve string constants; conversions from string constants to
24530 "char *" are allowed, even though normally a "const char *"
24531 cannot be used to initialize a "char *". */
24532 if (TREE_CODE (expr) == STRING_CST)
24533 return expr;
24534 /* Preserve void and arithmetic constants, as an optimization -- there is no
24535 reason to create a new node. */
24536 if (TREE_CODE (expr) == VOID_CST
24537 || TREE_CODE (expr) == INTEGER_CST
24538 || TREE_CODE (expr) == REAL_CST)
24539 return expr;
24540 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24541 There is at least one place where we want to know that a
24542 particular expression is a throw-expression: when checking a ?:
24543 expression, there are special rules if the second or third
24544 argument is a throw-expression. */
24545 if (TREE_CODE (expr) == THROW_EXPR)
24546 return expr;
24547
24548 /* Don't wrap an initializer list, we need to be able to look inside. */
24549 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24550 return expr;
24551
24552 /* Don't wrap a dummy object, we need to be able to test for it. */
24553 if (is_dummy_object (expr))
24554 return expr;
24555
24556 if (TREE_CODE (expr) == COND_EXPR)
24557 return build3 (COND_EXPR,
24558 TREE_TYPE (expr),
24559 TREE_OPERAND (expr, 0),
24560 (TREE_OPERAND (expr, 1)
24561 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24562 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24563 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24564 if (TREE_CODE (expr) == COMPOUND_EXPR
24565 && !COMPOUND_EXPR_OVERLOADED (expr))
24566 return build2 (COMPOUND_EXPR,
24567 TREE_TYPE (expr),
24568 TREE_OPERAND (expr, 0),
24569 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24570
24571 /* If the type is unknown, it can't really be non-dependent */
24572 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24573
24574 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24575 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24576 }
24577
24578 /* ARGS is a vector of expressions as arguments to a function call.
24579 Replace the arguments with equivalent non-dependent expressions.
24580 This modifies ARGS in place. */
24581
24582 void
24583 make_args_non_dependent (vec<tree, va_gc> *args)
24584 {
24585 unsigned int ix;
24586 tree arg;
24587
24588 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24589 {
24590 tree newarg = build_non_dependent_expr (arg);
24591 if (newarg != arg)
24592 (*args)[ix] = newarg;
24593 }
24594 }
24595
24596 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24597 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24598 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24599
24600 static tree
24601 make_auto_1 (tree name, bool set_canonical)
24602 {
24603 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24604 TYPE_NAME (au) = build_decl (input_location,
24605 TYPE_DECL, name, au);
24606 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24607 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24608 (0, processing_template_decl + 1, processing_template_decl + 1,
24609 TYPE_NAME (au), NULL_TREE);
24610 if (set_canonical)
24611 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24612 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24613 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24614
24615 return au;
24616 }
24617
24618 tree
24619 make_decltype_auto (void)
24620 {
24621 return make_auto_1 (decltype_auto_identifier, true);
24622 }
24623
24624 tree
24625 make_auto (void)
24626 {
24627 return make_auto_1 (auto_identifier, true);
24628 }
24629
24630 /* Return a C++17 deduction placeholder for class template TMPL. */
24631
24632 tree
24633 make_template_placeholder (tree tmpl)
24634 {
24635 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24636 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24637 return t;
24638 }
24639
24640 /* Make a "constrained auto" type-specifier. This is an
24641 auto type with constraints that must be associated after
24642 deduction. The constraint is formed from the given
24643 CONC and its optional sequence of arguments, which are
24644 non-null if written as partial-concept-id. */
24645
24646 tree
24647 make_constrained_auto (tree con, tree args)
24648 {
24649 tree type = make_auto_1 (auto_identifier, false);
24650
24651 /* Build the constraint. */
24652 tree tmpl = DECL_TI_TEMPLATE (con);
24653 tree expr;
24654 if (VAR_P (con))
24655 expr = build_concept_check (tmpl, type, args);
24656 else
24657 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
24658
24659 tree constr = normalize_expression (expr);
24660 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24661
24662 /* Our canonical type depends on the constraint. */
24663 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24664
24665 /* Attach the constraint to the type declaration. */
24666 tree decl = TYPE_NAME (type);
24667 return decl;
24668 }
24669
24670 /* Given type ARG, return std::initializer_list<ARG>. */
24671
24672 static tree
24673 listify (tree arg)
24674 {
24675 tree std_init_list = namespace_binding
24676 (get_identifier ("initializer_list"), std_node);
24677 tree argvec;
24678 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24679 {
24680 error ("deducing from brace-enclosed initializer list requires "
24681 "#include <initializer_list>");
24682 return error_mark_node;
24683 }
24684 argvec = make_tree_vec (1);
24685 TREE_VEC_ELT (argvec, 0) = arg;
24686 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24687 NULL_TREE, 0, tf_warning_or_error);
24688 }
24689
24690 /* Replace auto in TYPE with std::initializer_list<auto>. */
24691
24692 static tree
24693 listify_autos (tree type, tree auto_node)
24694 {
24695 tree init_auto = listify (auto_node);
24696 tree argvec = make_tree_vec (1);
24697 TREE_VEC_ELT (argvec, 0) = init_auto;
24698 if (processing_template_decl)
24699 argvec = add_to_template_args (current_template_args (), argvec);
24700 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24701 }
24702
24703 /* Hash traits for hashing possibly constrained 'auto'
24704 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24705
24706 struct auto_hash : default_hash_traits<tree>
24707 {
24708 static inline hashval_t hash (tree);
24709 static inline bool equal (tree, tree);
24710 };
24711
24712 /* Hash the 'auto' T. */
24713
24714 inline hashval_t
24715 auto_hash::hash (tree t)
24716 {
24717 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24718 /* Matching constrained-type-specifiers denote the same template
24719 parameter, so hash the constraint. */
24720 return hash_placeholder_constraint (c);
24721 else
24722 /* But unconstrained autos are all separate, so just hash the pointer. */
24723 return iterative_hash_object (t, 0);
24724 }
24725
24726 /* Compare two 'auto's. */
24727
24728 inline bool
24729 auto_hash::equal (tree t1, tree t2)
24730 {
24731 if (t1 == t2)
24732 return true;
24733
24734 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24735 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24736
24737 /* Two unconstrained autos are distinct. */
24738 if (!c1 || !c2)
24739 return false;
24740
24741 return equivalent_placeholder_constraints (c1, c2);
24742 }
24743
24744 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24745 constrained) auto, add it to the vector. */
24746
24747 static int
24748 extract_autos_r (tree t, void *data)
24749 {
24750 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24751 if (is_auto_or_concept (t))
24752 {
24753 /* All the autos were built with index 0; fix that up now. */
24754 tree *p = hash.find_slot (t, INSERT);
24755 unsigned idx;
24756 if (*p)
24757 /* If this is a repeated constrained-type-specifier, use the index we
24758 chose before. */
24759 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24760 else
24761 {
24762 /* Otherwise this is new, so use the current count. */
24763 *p = t;
24764 idx = hash.elements () - 1;
24765 }
24766 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24767 }
24768
24769 /* Always keep walking. */
24770 return 0;
24771 }
24772
24773 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24774 says they can appear anywhere in the type. */
24775
24776 static tree
24777 extract_autos (tree type)
24778 {
24779 hash_set<tree> visited;
24780 hash_table<auto_hash> hash (2);
24781
24782 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24783
24784 tree tree_vec = make_tree_vec (hash.elements());
24785 for (hash_table<auto_hash>::iterator iter = hash.begin();
24786 iter != hash.end(); ++iter)
24787 {
24788 tree elt = *iter;
24789 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24790 TREE_VEC_ELT (tree_vec, i)
24791 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24792 }
24793
24794 return tree_vec;
24795 }
24796
24797 /* The stem for deduction guide names. */
24798 const char *const dguide_base = "__dguide_";
24799
24800 /* Return the name for a deduction guide for class template TMPL. */
24801
24802 tree
24803 dguide_name (tree tmpl)
24804 {
24805 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24806 tree tname = TYPE_IDENTIFIER (type);
24807 char *buf = (char *) alloca (1 + strlen (dguide_base)
24808 + IDENTIFIER_LENGTH (tname));
24809 memcpy (buf, dguide_base, strlen (dguide_base));
24810 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24811 IDENTIFIER_LENGTH (tname) + 1);
24812 tree dname = get_identifier (buf);
24813 TREE_TYPE (dname) = type;
24814 return dname;
24815 }
24816
24817 /* True if NAME is the name of a deduction guide. */
24818
24819 bool
24820 dguide_name_p (tree name)
24821 {
24822 return (TREE_TYPE (name)
24823 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24824 strlen (dguide_base)));
24825 }
24826
24827 /* True if FN is a deduction guide. */
24828
24829 bool
24830 deduction_guide_p (const_tree fn)
24831 {
24832 if (DECL_P (fn))
24833 if (tree name = DECL_NAME (fn))
24834 return dguide_name_p (name);
24835 return false;
24836 }
24837
24838 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24839 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24840 template parameter types. Note that the handling of template template
24841 parameters relies on current_template_parms being set appropriately for the
24842 new template. */
24843
24844 static tree
24845 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
24846 tree tsubst_args, tsubst_flags_t complain)
24847 {
24848 tree oldidx = get_template_parm_index (olddecl);
24849
24850 tree newtype;
24851 if (TREE_CODE (olddecl) == TYPE_DECL
24852 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24853 {
24854 tree oldtype = TREE_TYPE (olddecl);
24855 newtype = cxx_make_type (TREE_CODE (oldtype));
24856 TYPE_MAIN_VARIANT (newtype) = newtype;
24857 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
24858 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
24859 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
24860 }
24861 else
24862 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
24863 complain, NULL_TREE);
24864
24865 tree newdecl
24866 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
24867 DECL_NAME (olddecl), newtype);
24868 SET_DECL_TEMPLATE_PARM_P (newdecl);
24869
24870 tree newidx;
24871 if (TREE_CODE (olddecl) == TYPE_DECL
24872 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24873 {
24874 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
24875 = build_template_parm_index (index, level, level,
24876 newdecl, newtype);
24877 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
24878 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
24879
24880 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
24881 {
24882 DECL_TEMPLATE_RESULT (newdecl)
24883 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
24884 DECL_NAME (olddecl), newtype);
24885 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
24886 // First create a copy (ttargs) of tsubst_args with an
24887 // additional level for the template template parameter's own
24888 // template parameters (ttparms).
24889 tree ttparms = (INNERMOST_TEMPLATE_PARMS
24890 (DECL_TEMPLATE_PARMS (olddecl)));
24891 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
24892 tree ttargs = make_tree_vec (depth + 1);
24893 for (int i = 0; i < depth; ++i)
24894 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
24895 TREE_VEC_ELT (ttargs, depth)
24896 = template_parms_level_to_args (ttparms);
24897 // Substitute ttargs into ttparms to fix references to
24898 // other template parameters.
24899 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24900 complain);
24901 // Now substitute again with args based on tparms, to reduce
24902 // the level of the ttparms.
24903 ttargs = current_template_args ();
24904 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24905 complain);
24906 // Finally, tack the adjusted parms onto tparms.
24907 ttparms = tree_cons (size_int (depth), ttparms,
24908 current_template_parms);
24909 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
24910 }
24911 }
24912 else
24913 {
24914 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
24915 tree newconst
24916 = build_decl (DECL_SOURCE_LOCATION (oldconst),
24917 TREE_CODE (oldconst),
24918 DECL_NAME (oldconst), newtype);
24919 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
24920 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
24921 SET_DECL_TEMPLATE_PARM_P (newconst);
24922 newidx = build_template_parm_index (index, level, level,
24923 newconst, newtype);
24924 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
24925 }
24926
24927 TEMPLATE_PARM_PARAMETER_PACK (newidx)
24928 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
24929 return newdecl;
24930 }
24931
24932 /* Returns a C++17 class deduction guide template based on the constructor
24933 CTOR. */
24934
24935 static tree
24936 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
24937 {
24938 if (outer_args)
24939 ctor = tsubst (ctor, outer_args, complain, ctor);
24940 tree type = DECL_CONTEXT (ctor);
24941 tree fn_tmpl;
24942 if (TREE_CODE (ctor) == TEMPLATE_DECL)
24943 {
24944 fn_tmpl = ctor;
24945 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
24946 }
24947 else
24948 fn_tmpl = DECL_TI_TEMPLATE (ctor);
24949
24950 tree tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
24951 /* If type is a member class template, DECL_TI_ARGS (ctor) will have fully
24952 specialized args for the enclosing class. Strip those off, as the
24953 deduction guide won't have those template parameters. */
24954 tree targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
24955 TMPL_PARMS_DEPTH (tparms));
24956 /* Discard the 'this' parameter. */
24957 tree fparms = FUNCTION_ARG_CHAIN (ctor);
24958 tree fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
24959 tree ci = get_constraints (ctor);
24960
24961 if (PRIMARY_TEMPLATE_P (fn_tmpl))
24962 {
24963 /* For a member template constructor, we need to flatten the two template
24964 parameter lists into one, and then adjust the function signature
24965 accordingly. This gets...complicated. */
24966 ++processing_template_decl;
24967 tree save_parms = current_template_parms;
24968
24969 /* For a member template we should have two levels of parms/args, one for
24970 the class and one for the constructor. We stripped specialized args
24971 for further enclosing classes above. */
24972 const int depth = 2;
24973 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
24974
24975 /* Template args for translating references to the two-level template
24976 parameters into references to the one-level template parameters we are
24977 creating. */
24978 tree tsubst_args = copy_node (targs);
24979 TMPL_ARGS_LEVEL (tsubst_args, depth)
24980 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
24981
24982 /* Template parms for the constructor template. */
24983 tree ftparms = TREE_VALUE (tparms);
24984 unsigned flen = TREE_VEC_LENGTH (ftparms);
24985 /* Template parms for the class template. */
24986 tparms = TREE_CHAIN (tparms);
24987 tree ctparms = TREE_VALUE (tparms);
24988 unsigned clen = TREE_VEC_LENGTH (ctparms);
24989 /* Template parms for the deduction guide start as a copy of the template
24990 parms for the class. We set current_template_parms for
24991 lookup_template_class_1. */
24992 current_template_parms = tparms = copy_node (tparms);
24993 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
24994 for (unsigned i = 0; i < clen; ++i)
24995 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
24996
24997 /* Now we need to rewrite the constructor parms to append them to the
24998 class parms. */
24999 for (unsigned i = 0; i < flen; ++i)
25000 {
25001 unsigned index = i + clen;
25002 unsigned level = 1;
25003 tree oldelt = TREE_VEC_ELT (ftparms, i);
25004 tree olddecl = TREE_VALUE (oldelt);
25005 tree newdecl = rewrite_template_parm (olddecl, index, level,
25006 tsubst_args, complain);
25007 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25008 tsubst_args, complain, ctor);
25009 tree list = build_tree_list (newdef, newdecl);
25010 TEMPLATE_PARM_CONSTRAINTS (list)
25011 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25012 tsubst_args, complain, ctor);
25013 TREE_VEC_ELT (new_vec, index) = list;
25014 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25015 }
25016
25017 /* Now we have a final set of template parms to substitute into the
25018 function signature. */
25019 targs = template_parms_to_args (tparms);
25020 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25021 complain, ctor);
25022 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25023 if (ci)
25024 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25025
25026 current_template_parms = save_parms;
25027 --processing_template_decl;
25028 }
25029 else
25030 {
25031 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25032 tparms = copy_node (tparms);
25033 INNERMOST_TEMPLATE_PARMS (tparms)
25034 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25035 }
25036
25037 tree fntype = build_function_type (type, fparms);
25038 tree ded_fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (ctor),
25039 FUNCTION_DECL,
25040 dguide_name (type), fntype);
25041 DECL_ARGUMENTS (ded_fn) = fargs;
25042 DECL_ARTIFICIAL (ded_fn) = true;
25043 DECL_NONCONVERTING_P (ded_fn) = DECL_NONCONVERTING_P (ctor);
25044 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25045 DECL_ARTIFICIAL (ded_tmpl) = true;
25046 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25047 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25048 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25049 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25050 if (ci)
25051 set_constraints (ded_tmpl, ci);
25052
25053 return ded_tmpl;
25054 }
25055
25056 /* Deduce template arguments for the class template placeholder PTYPE for
25057 template TMPL based on the initializer INIT, and return the resulting
25058 type. */
25059
25060 static tree
25061 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25062 tsubst_flags_t complain)
25063 {
25064 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25065 {
25066 /* We should have handled this in the caller. */
25067 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25068 return ptype;
25069 if (complain & tf_error)
25070 error ("non-class template %qT used without template arguments", tmpl);
25071 return error_mark_node;
25072 }
25073
25074 tree type = TREE_TYPE (tmpl);
25075
25076 vec<tree,va_gc> *args;
25077 if (TREE_CODE (init) == TREE_LIST)
25078 args = make_tree_vector_from_list (init);
25079 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25080 args = make_tree_vector_from_ctor (init);
25081 else
25082 args = make_tree_vector_single (init);
25083
25084 if (args->length() == 1)
25085 {
25086 /* First try to deduce directly, since we don't have implicitly-declared
25087 constructors yet. */
25088 tree parms = build_tree_list (NULL_TREE, type);
25089 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25090 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25091 int err = type_unification_real (tparms, targs, parms, &(*args)[0],
25092 1, /*subr*/false, DEDUCE_CALL,
25093 LOOKUP_NORMAL, NULL, /*explain*/false);
25094 if (err == 0)
25095 return tsubst (type, targs, complain, tmpl);
25096 }
25097
25098 tree dname = dguide_name (tmpl);
25099 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25100 /*type*/false, /*complain*/false,
25101 /*hidden*/false);
25102 if (cands == error_mark_node)
25103 cands = NULL_TREE;
25104
25105 tree outer_args = NULL_TREE;
25106 if (DECL_CLASS_SCOPE_P (tmpl)
25107 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25108 {
25109 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25110 type = TREE_TYPE (most_general_template (tmpl));
25111 }
25112
25113 if (CLASSTYPE_METHOD_VEC (type))
25114 // FIXME cache artificial deduction guides
25115 for (tree fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
25116 {
25117 tree fn = OVL_CURRENT (fns);
25118 tree guide = build_deduction_guide (fn, outer_args, complain);
25119 cands = ovl_cons (guide, cands);
25120 }
25121
25122 if (cands == NULL_TREE)
25123 {
25124 error ("cannot deduce template arguments for %qT, as it has "
25125 "no deduction guides or user-declared constructors", type);
25126 return error_mark_node;
25127 }
25128
25129 /* Prune explicit deduction guides in copy-initialization context. */
25130 tree old_cands = cands;
25131 if (flags & LOOKUP_ONLYCONVERTING)
25132 {
25133 tree t = cands;
25134 for (; t; t = OVL_NEXT (t))
25135 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (OVL_CURRENT (t))))
25136 break;
25137 if (t)
25138 {
25139 tree pruned = NULL_TREE;
25140 for (t = cands; t; t = OVL_NEXT (t))
25141 {
25142 tree f = OVL_CURRENT (t);
25143 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (f)))
25144 pruned = build_overload (f, pruned);
25145 }
25146 cands = pruned;
25147 if (cands == NULL_TREE)
25148 {
25149 error ("cannot deduce template arguments for copy-initialization"
25150 " of %qT, as it has no non-explicit deduction guides or "
25151 "user-declared constructors", type);
25152 return error_mark_node;
25153 }
25154 }
25155 }
25156
25157 ++cp_unevaluated_operand;
25158 tree t = build_new_function_call (cands, &args, /*koenig*/false,
25159 tf_decltype);
25160
25161 if (t == error_mark_node && (complain & tf_warning_or_error))
25162 {
25163 error ("class template argument deduction failed:");
25164 t = build_new_function_call (cands, &args, /*koenig*/false,
25165 complain | tf_decltype);
25166 if (old_cands != cands)
25167 inform (input_location, "explicit deduction guides not considered "
25168 "for copy-initialization");
25169 }
25170
25171 --cp_unevaluated_operand;
25172 release_tree_vector (args);
25173
25174 return TREE_TYPE (t);
25175 }
25176
25177 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25178 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25179
25180 tree
25181 do_auto_deduction (tree type, tree init, tree auto_node)
25182 {
25183 return do_auto_deduction (type, init, auto_node,
25184 tf_warning_or_error,
25185 adc_unspecified);
25186 }
25187
25188 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25189 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25190 The CONTEXT determines the context in which auto deduction is performed
25191 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25192 OUTER_TARGS are used during template argument deduction
25193 (context == adc_unify) to properly substitute the result, and is ignored
25194 in other contexts.
25195
25196 For partial-concept-ids, extra args may be appended to the list of deduced
25197 template arguments prior to determining constraint satisfaction. */
25198
25199 tree
25200 do_auto_deduction (tree type, tree init, tree auto_node,
25201 tsubst_flags_t complain, auto_deduction_context context,
25202 tree outer_targs, int flags)
25203 {
25204 tree targs;
25205
25206 if (init == error_mark_node)
25207 return error_mark_node;
25208
25209 if (type_dependent_expression_p (init)
25210 && context != adc_unify)
25211 /* Defining a subset of type-dependent expressions that we can deduce
25212 from ahead of time isn't worth the trouble. */
25213 return type;
25214
25215 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25216 /* C++17 class template argument deduction. */
25217 return do_class_deduction (type, tmpl, init, flags, complain);
25218
25219 if (TREE_TYPE (init) == NULL_TREE)
25220 /* Nothing we can do with this, even in deduction context. */
25221 return type;
25222
25223 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25224 with either a new invented type template parameter U or, if the
25225 initializer is a braced-init-list (8.5.4), with
25226 std::initializer_list<U>. */
25227 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25228 {
25229 if (!DIRECT_LIST_INIT_P (init))
25230 type = listify_autos (type, auto_node);
25231 else if (CONSTRUCTOR_NELTS (init) == 1)
25232 init = CONSTRUCTOR_ELT (init, 0)->value;
25233 else
25234 {
25235 if (complain & tf_warning_or_error)
25236 {
25237 if (permerror (input_location, "direct-list-initialization of "
25238 "%<auto%> requires exactly one element"))
25239 inform (input_location,
25240 "for deduction to %<std::initializer_list%>, use copy-"
25241 "list-initialization (i.e. add %<=%> before the %<{%>)");
25242 }
25243 type = listify_autos (type, auto_node);
25244 }
25245 }
25246
25247 if (type == error_mark_node)
25248 return error_mark_node;
25249
25250 init = resolve_nondeduced_context (init, complain);
25251
25252 if (context == adc_decomp_type
25253 && auto_node == type
25254 && init != error_mark_node
25255 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25256 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25257 and initializer has array type, deduce cv-qualified array type. */
25258 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25259 complain);
25260 else if (AUTO_IS_DECLTYPE (auto_node))
25261 {
25262 bool id = (DECL_P (init)
25263 || ((TREE_CODE (init) == COMPONENT_REF
25264 || TREE_CODE (init) == SCOPE_REF)
25265 && !REF_PARENTHESIZED_P (init)));
25266 targs = make_tree_vec (1);
25267 TREE_VEC_ELT (targs, 0)
25268 = finish_decltype_type (init, id, tf_warning_or_error);
25269 if (type != auto_node)
25270 {
25271 if (complain & tf_error)
25272 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25273 return error_mark_node;
25274 }
25275 }
25276 else
25277 {
25278 tree parms = build_tree_list (NULL_TREE, type);
25279 tree tparms;
25280
25281 if (flag_concepts)
25282 tparms = extract_autos (type);
25283 else
25284 {
25285 tparms = make_tree_vec (1);
25286 TREE_VEC_ELT (tparms, 0)
25287 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25288 }
25289
25290 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25291 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25292 DEDUCE_CALL, LOOKUP_NORMAL,
25293 NULL, /*explain_p=*/false);
25294 if (val > 0)
25295 {
25296 if (processing_template_decl)
25297 /* Try again at instantiation time. */
25298 return type;
25299 if (type && type != error_mark_node
25300 && (complain & tf_error))
25301 /* If type is error_mark_node a diagnostic must have been
25302 emitted by now. Also, having a mention to '<type error>'
25303 in the diagnostic is not really useful to the user. */
25304 {
25305 if (cfun && auto_node == current_function_auto_return_pattern
25306 && LAMBDA_FUNCTION_P (current_function_decl))
25307 error ("unable to deduce lambda return type from %qE", init);
25308 else
25309 error ("unable to deduce %qT from %qE", type, init);
25310 type_unification_real (tparms, targs, parms, &init, 1, 0,
25311 DEDUCE_CALL, LOOKUP_NORMAL,
25312 NULL, /*explain_p=*/true);
25313 }
25314 return error_mark_node;
25315 }
25316 }
25317
25318 /* Check any placeholder constraints against the deduced type. */
25319 if (flag_concepts && !processing_template_decl)
25320 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25321 {
25322 /* Use the deduced type to check the associated constraints. If we
25323 have a partial-concept-id, rebuild the argument list so that
25324 we check using the extra arguments. */
25325 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25326 tree cargs = CHECK_CONSTR_ARGS (constr);
25327 if (TREE_VEC_LENGTH (cargs) > 1)
25328 {
25329 cargs = copy_node (cargs);
25330 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25331 }
25332 else
25333 cargs = targs;
25334 if (!constraints_satisfied_p (constr, cargs))
25335 {
25336 if (complain & tf_warning_or_error)
25337 {
25338 switch (context)
25339 {
25340 case adc_unspecified:
25341 case adc_unify:
25342 error("placeholder constraints not satisfied");
25343 break;
25344 case adc_variable_type:
25345 case adc_decomp_type:
25346 error ("deduced initializer does not satisfy "
25347 "placeholder constraints");
25348 break;
25349 case adc_return_type:
25350 error ("deduced return type does not satisfy "
25351 "placeholder constraints");
25352 break;
25353 case adc_requirement:
25354 error ("deduced expression type does not satisfy "
25355 "placeholder constraints");
25356 break;
25357 }
25358 diagnose_constraints (input_location, constr, targs);
25359 }
25360 return error_mark_node;
25361 }
25362 }
25363
25364 if (processing_template_decl && context != adc_unify)
25365 outer_targs = current_template_args ();
25366 targs = add_to_template_args (outer_targs, targs);
25367 return tsubst (type, targs, complain, NULL_TREE);
25368 }
25369
25370 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25371 result. */
25372
25373 tree
25374 splice_late_return_type (tree type, tree late_return_type)
25375 {
25376 if (is_auto (type))
25377 {
25378 if (late_return_type)
25379 return late_return_type;
25380
25381 tree idx = get_template_parm_index (type);
25382 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25383 /* In an abbreviated function template we didn't know we were dealing
25384 with a function template when we saw the auto return type, so update
25385 it to have the correct level. */
25386 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25387 }
25388 return type;
25389 }
25390
25391 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25392 'decltype(auto)' or a deduced class template. */
25393
25394 bool
25395 is_auto (const_tree type)
25396 {
25397 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25398 && (TYPE_IDENTIFIER (type) == auto_identifier
25399 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25400 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25401 return true;
25402 else
25403 return false;
25404 }
25405
25406 /* for_each_template_parm callback for type_uses_auto. */
25407
25408 int
25409 is_auto_r (tree tp, void */*data*/)
25410 {
25411 return is_auto_or_concept (tp);
25412 }
25413
25414 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25415 a use of `auto'. Returns NULL_TREE otherwise. */
25416
25417 tree
25418 type_uses_auto (tree type)
25419 {
25420 if (type == NULL_TREE)
25421 return NULL_TREE;
25422 else if (flag_concepts)
25423 {
25424 /* The Concepts TS allows multiple autos in one type-specifier; just
25425 return the first one we find, do_auto_deduction will collect all of
25426 them. */
25427 if (uses_template_parms (type))
25428 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25429 /*visited*/NULL, /*nondeduced*/true);
25430 else
25431 return NULL_TREE;
25432 }
25433 else
25434 return find_type_usage (type, is_auto);
25435 }
25436
25437 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
25438 'decltype(auto)' or a concept. */
25439
25440 bool
25441 is_auto_or_concept (const_tree type)
25442 {
25443 return is_auto (type); // or concept
25444 }
25445
25446 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
25447 a concept identifier) iff TYPE contains a use of a generic type. Returns
25448 NULL_TREE otherwise. */
25449
25450 tree
25451 type_uses_auto_or_concept (tree type)
25452 {
25453 return find_type_usage (type, is_auto_or_concept);
25454 }
25455
25456
25457 /* For a given template T, return the vector of typedefs referenced
25458 in T for which access check is needed at T instantiation time.
25459 T is either a FUNCTION_DECL or a RECORD_TYPE.
25460 Those typedefs were added to T by the function
25461 append_type_to_template_for_access_check. */
25462
25463 vec<qualified_typedef_usage_t, va_gc> *
25464 get_types_needing_access_check (tree t)
25465 {
25466 tree ti;
25467 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25468
25469 if (!t || t == error_mark_node)
25470 return NULL;
25471
25472 if (!(ti = get_template_info (t)))
25473 return NULL;
25474
25475 if (CLASS_TYPE_P (t)
25476 || TREE_CODE (t) == FUNCTION_DECL)
25477 {
25478 if (!TI_TEMPLATE (ti))
25479 return NULL;
25480
25481 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25482 }
25483
25484 return result;
25485 }
25486
25487 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25488 tied to T. That list of typedefs will be access checked at
25489 T instantiation time.
25490 T is either a FUNCTION_DECL or a RECORD_TYPE.
25491 TYPE_DECL is a TYPE_DECL node representing a typedef.
25492 SCOPE is the scope through which TYPE_DECL is accessed.
25493 LOCATION is the location of the usage point of TYPE_DECL.
25494
25495 This function is a subroutine of
25496 append_type_to_template_for_access_check. */
25497
25498 static void
25499 append_type_to_template_for_access_check_1 (tree t,
25500 tree type_decl,
25501 tree scope,
25502 location_t location)
25503 {
25504 qualified_typedef_usage_t typedef_usage;
25505 tree ti;
25506
25507 if (!t || t == error_mark_node)
25508 return;
25509
25510 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25511 || CLASS_TYPE_P (t))
25512 && type_decl
25513 && TREE_CODE (type_decl) == TYPE_DECL
25514 && scope);
25515
25516 if (!(ti = get_template_info (t)))
25517 return;
25518
25519 gcc_assert (TI_TEMPLATE (ti));
25520
25521 typedef_usage.typedef_decl = type_decl;
25522 typedef_usage.context = scope;
25523 typedef_usage.locus = location;
25524
25525 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25526 }
25527
25528 /* Append TYPE_DECL to the template TEMPL.
25529 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25530 At TEMPL instanciation time, TYPE_DECL will be checked to see
25531 if it can be accessed through SCOPE.
25532 LOCATION is the location of the usage point of TYPE_DECL.
25533
25534 e.g. consider the following code snippet:
25535
25536 class C
25537 {
25538 typedef int myint;
25539 };
25540
25541 template<class U> struct S
25542 {
25543 C::myint mi; // <-- usage point of the typedef C::myint
25544 };
25545
25546 S<char> s;
25547
25548 At S<char> instantiation time, we need to check the access of C::myint
25549 In other words, we need to check the access of the myint typedef through
25550 the C scope. For that purpose, this function will add the myint typedef
25551 and the scope C through which its being accessed to a list of typedefs
25552 tied to the template S. That list will be walked at template instantiation
25553 time and access check performed on each typedefs it contains.
25554 Note that this particular code snippet should yield an error because
25555 myint is private to C. */
25556
25557 void
25558 append_type_to_template_for_access_check (tree templ,
25559 tree type_decl,
25560 tree scope,
25561 location_t location)
25562 {
25563 qualified_typedef_usage_t *iter;
25564 unsigned i;
25565
25566 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25567
25568 /* Make sure we don't append the type to the template twice. */
25569 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25570 if (iter->typedef_decl == type_decl && scope == iter->context)
25571 return;
25572
25573 append_type_to_template_for_access_check_1 (templ, type_decl,
25574 scope, location);
25575 }
25576
25577 /* Convert the generic type parameters in PARM that match the types given in the
25578 range [START_IDX, END_IDX) from the current_template_parms into generic type
25579 packs. */
25580
25581 tree
25582 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25583 {
25584 tree current = current_template_parms;
25585 int depth = TMPL_PARMS_DEPTH (current);
25586 current = INNERMOST_TEMPLATE_PARMS (current);
25587 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25588
25589 for (int i = 0; i < start_idx; ++i)
25590 TREE_VEC_ELT (replacement, i)
25591 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25592
25593 for (int i = start_idx; i < end_idx; ++i)
25594 {
25595 /* Create a distinct parameter pack type from the current parm and add it
25596 to the replacement args to tsubst below into the generic function
25597 parameter. */
25598
25599 tree o = TREE_TYPE (TREE_VALUE
25600 (TREE_VEC_ELT (current, i)));
25601 tree t = copy_type (o);
25602 TEMPLATE_TYPE_PARM_INDEX (t)
25603 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25604 o, 0, 0, tf_none);
25605 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25606 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25607 TYPE_MAIN_VARIANT (t) = t;
25608 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25609 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25610 TREE_VEC_ELT (replacement, i) = t;
25611 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25612 }
25613
25614 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25615 TREE_VEC_ELT (replacement, i)
25616 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25617
25618 /* If there are more levels then build up the replacement with the outer
25619 template parms. */
25620 if (depth > 1)
25621 replacement = add_to_template_args (template_parms_to_args
25622 (TREE_CHAIN (current_template_parms)),
25623 replacement);
25624
25625 return tsubst (parm, replacement, tf_none, NULL_TREE);
25626 }
25627
25628 /* Entries in the decl_constraint hash table. */
25629 struct GTY((for_user)) constr_entry
25630 {
25631 tree decl;
25632 tree ci;
25633 };
25634
25635 /* Hashing function and equality for constraint entries. */
25636 struct constr_hasher : ggc_ptr_hash<constr_entry>
25637 {
25638 static hashval_t hash (constr_entry *e)
25639 {
25640 return (hashval_t)DECL_UID (e->decl);
25641 }
25642
25643 static bool equal (constr_entry *e1, constr_entry *e2)
25644 {
25645 return e1->decl == e2->decl;
25646 }
25647 };
25648
25649 /* A mapping from declarations to constraint information. Note that
25650 both templates and their underlying declarations are mapped to the
25651 same constraint information.
25652
25653 FIXME: This is defined in pt.c because garbage collection
25654 code is not being generated for constraint.cc. */
25655
25656 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25657
25658 /* Returns the template constraints of declaration T. If T is not
25659 constrained, return NULL_TREE. Note that T must be non-null. */
25660
25661 tree
25662 get_constraints (tree t)
25663 {
25664 if (!flag_concepts)
25665 return NULL_TREE;
25666
25667 gcc_assert (DECL_P (t));
25668 if (TREE_CODE (t) == TEMPLATE_DECL)
25669 t = DECL_TEMPLATE_RESULT (t);
25670 constr_entry elt = { t, NULL_TREE };
25671 constr_entry* found = decl_constraints->find (&elt);
25672 if (found)
25673 return found->ci;
25674 else
25675 return NULL_TREE;
25676 }
25677
25678 /* Associate the given constraint information CI with the declaration
25679 T. If T is a template, then the constraints are associated with
25680 its underlying declaration. Don't build associations if CI is
25681 NULL_TREE. */
25682
25683 void
25684 set_constraints (tree t, tree ci)
25685 {
25686 if (!ci)
25687 return;
25688 gcc_assert (t && flag_concepts);
25689 if (TREE_CODE (t) == TEMPLATE_DECL)
25690 t = DECL_TEMPLATE_RESULT (t);
25691 gcc_assert (!get_constraints (t));
25692 constr_entry elt = {t, ci};
25693 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25694 constr_entry* entry = ggc_alloc<constr_entry> ();
25695 *entry = elt;
25696 *slot = entry;
25697 }
25698
25699 /* Remove the associated constraints of the declaration T. */
25700
25701 void
25702 remove_constraints (tree t)
25703 {
25704 gcc_assert (DECL_P (t));
25705 if (TREE_CODE (t) == TEMPLATE_DECL)
25706 t = DECL_TEMPLATE_RESULT (t);
25707
25708 constr_entry elt = {t, NULL_TREE};
25709 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25710 if (slot)
25711 decl_constraints->clear_slot (slot);
25712 }
25713
25714 /* Memoized satisfaction results for declarations. This
25715 maps the pair (constraint_info, arguments) to the result computed
25716 by constraints_satisfied_p. */
25717
25718 struct GTY((for_user)) constraint_sat_entry
25719 {
25720 tree ci;
25721 tree args;
25722 tree result;
25723 };
25724
25725 /* Hashing function and equality for constraint entries. */
25726
25727 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25728 {
25729 static hashval_t hash (constraint_sat_entry *e)
25730 {
25731 hashval_t val = iterative_hash_object(e->ci, 0);
25732 return iterative_hash_template_arg (e->args, val);
25733 }
25734
25735 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25736 {
25737 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25738 }
25739 };
25740
25741 /* Memoized satisfaction results for concept checks. */
25742
25743 struct GTY((for_user)) concept_spec_entry
25744 {
25745 tree tmpl;
25746 tree args;
25747 tree result;
25748 };
25749
25750 /* Hashing function and equality for constraint entries. */
25751
25752 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25753 {
25754 static hashval_t hash (concept_spec_entry *e)
25755 {
25756 return hash_tmpl_and_args (e->tmpl, e->args);
25757 }
25758
25759 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25760 {
25761 ++comparing_specializations;
25762 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25763 --comparing_specializations;
25764 return eq;
25765 }
25766 };
25767
25768 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25769 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25770
25771 /* Search for a memoized satisfaction result. Returns one of the
25772 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25773
25774 tree
25775 lookup_constraint_satisfaction (tree ci, tree args)
25776 {
25777 constraint_sat_entry elt = { ci, args, NULL_TREE };
25778 constraint_sat_entry* found = constraint_memos->find (&elt);
25779 if (found)
25780 return found->result;
25781 else
25782 return NULL_TREE;
25783 }
25784
25785 /* Memoize the result of a satisfication test. Returns the saved result. */
25786
25787 tree
25788 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25789 {
25790 constraint_sat_entry elt = {ci, args, result};
25791 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25792 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25793 *entry = elt;
25794 *slot = entry;
25795 return result;
25796 }
25797
25798 /* Search for a memoized satisfaction result for a concept. */
25799
25800 tree
25801 lookup_concept_satisfaction (tree tmpl, tree args)
25802 {
25803 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25804 concept_spec_entry* found = concept_memos->find (&elt);
25805 if (found)
25806 return found->result;
25807 else
25808 return NULL_TREE;
25809 }
25810
25811 /* Memoize the result of a concept check. Returns the saved result. */
25812
25813 tree
25814 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
25815 {
25816 concept_spec_entry elt = {tmpl, args, result};
25817 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
25818 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25819 *entry = elt;
25820 *slot = entry;
25821 return result;
25822 }
25823
25824 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
25825
25826 /* Returns a prior concept specialization. This returns the substituted
25827 and normalized constraints defined by the concept. */
25828
25829 tree
25830 get_concept_expansion (tree tmpl, tree args)
25831 {
25832 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25833 concept_spec_entry* found = concept_expansions->find (&elt);
25834 if (found)
25835 return found->result;
25836 else
25837 return NULL_TREE;
25838 }
25839
25840 /* Save a concept expansion for later. */
25841
25842 tree
25843 save_concept_expansion (tree tmpl, tree args, tree def)
25844 {
25845 concept_spec_entry elt = {tmpl, args, def};
25846 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
25847 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25848 *entry = elt;
25849 *slot = entry;
25850 return def;
25851 }
25852
25853 static hashval_t
25854 hash_subsumption_args (tree t1, tree t2)
25855 {
25856 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
25857 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
25858 int val = 0;
25859 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
25860 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
25861 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
25862 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
25863 return val;
25864 }
25865
25866 /* Compare the constraints of two subsumption entries. The LEFT1 and
25867 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
25868 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
25869
25870 static bool
25871 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
25872 {
25873 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
25874 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
25875 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
25876 CHECK_CONSTR_ARGS (right1)))
25877 return comp_template_args (CHECK_CONSTR_ARGS (left2),
25878 CHECK_CONSTR_ARGS (right2));
25879 return false;
25880 }
25881
25882 /* Key/value pair for learning and memoizing subsumption results. This
25883 associates a pair of check constraints (including arguments) with
25884 a boolean value indicating the result. */
25885
25886 struct GTY((for_user)) subsumption_entry
25887 {
25888 tree t1;
25889 tree t2;
25890 bool result;
25891 };
25892
25893 /* Hashing function and equality for constraint entries. */
25894
25895 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
25896 {
25897 static hashval_t hash (subsumption_entry *e)
25898 {
25899 return hash_subsumption_args (e->t1, e->t2);
25900 }
25901
25902 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
25903 {
25904 ++comparing_specializations;
25905 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
25906 --comparing_specializations;
25907 return eq;
25908 }
25909 };
25910
25911 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
25912
25913 /* Search for a previously cached subsumption result. */
25914
25915 bool*
25916 lookup_subsumption_result (tree t1, tree t2)
25917 {
25918 subsumption_entry elt = { t1, t2, false };
25919 subsumption_entry* found = subsumption_table->find (&elt);
25920 if (found)
25921 return &found->result;
25922 else
25923 return 0;
25924 }
25925
25926 /* Save a subsumption result. */
25927
25928 bool
25929 save_subsumption_result (tree t1, tree t2, bool result)
25930 {
25931 subsumption_entry elt = {t1, t2, result};
25932 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
25933 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
25934 *entry = elt;
25935 *slot = entry;
25936 return result;
25937 }
25938
25939 /* Set up the hash table for constraint association. */
25940
25941 void
25942 init_constraint_processing (void)
25943 {
25944 if (!flag_concepts)
25945 return;
25946
25947 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
25948 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
25949 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
25950 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
25951 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
25952 }
25953
25954 /* Set up the hash tables for template instantiations. */
25955
25956 void
25957 init_template_processing (void)
25958 {
25959 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
25960 type_specializations = hash_table<spec_hasher>::create_ggc (37);
25961 }
25962
25963 /* Print stats about the template hash tables for -fstats. */
25964
25965 void
25966 print_template_statistics (void)
25967 {
25968 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
25969 "%f collisions\n", (long) decl_specializations->size (),
25970 (long) decl_specializations->elements (),
25971 decl_specializations->collisions ());
25972 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
25973 "%f collisions\n", (long) type_specializations->size (),
25974 (long) type_specializations->elements (),
25975 type_specializations->collisions ());
25976 }
25977
25978 #include "gt-cp-pt.h"