]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/pt.c
Fix numerous typos in comments
[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 tree dd = duplicate_decls (spec, fn, is_friend);
1603 if (dd == error_mark_node)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node;
1606
1607 if (dd == NULL_TREE && DECL_INITIAL (spec))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1610 definition. */
1611 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1612
1613 return fn;
1614 }
1615 }
1616 else if (fn)
1617 return duplicate_decls (spec, fn, is_friend);
1618
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1622 && !check_specialization_namespace (tmpl))
1623 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1624
1625 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1626 {
1627 spec_entry *entry = ggc_alloc<spec_entry> ();
1628 gcc_assert (tmpl && args && spec);
1629 *entry = elt;
1630 *slot = entry;
1631 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1632 && PRIMARY_TEMPLATE_P (tmpl)
1633 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1634 || variable_template_p (tmpl))
1635 /* If TMPL is a forward declaration of a template function, keep a list
1636 of all specializations in case we need to reassign them to a friend
1637 template later in tsubst_friend_function.
1638
1639 Also keep a list of all variable template instantiations so that
1640 process_partial_specialization can check whether a later partial
1641 specialization would have used it. */
1642 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1643 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1644 }
1645
1646 return spec;
1647 }
1648
1649 /* Returns true iff two spec_entry nodes are equivalent. */
1650
1651 int comparing_specializations;
1652
1653 bool
1654 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1655 {
1656 int equal;
1657
1658 ++comparing_specializations;
1659 equal = (e1->tmpl == e2->tmpl
1660 && comp_template_args (e1->args, e2->args));
1661 if (equal && flag_concepts
1662 /* tmpl could be a FIELD_DECL for a capture pack. */
1663 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1664 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1665 && uses_template_parms (e1->args))
1666 {
1667 /* Partial specializations of a variable template can be distinguished by
1668 constraints. */
1669 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1670 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1671 equal = equivalent_constraints (c1, c2);
1672 }
1673 --comparing_specializations;
1674
1675 return equal;
1676 }
1677
1678 /* Returns a hash for a template TMPL and template arguments ARGS. */
1679
1680 static hashval_t
1681 hash_tmpl_and_args (tree tmpl, tree args)
1682 {
1683 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1684 return iterative_hash_template_arg (args, val);
1685 }
1686
1687 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1688 ignoring SPEC. */
1689
1690 hashval_t
1691 spec_hasher::hash (spec_entry *e)
1692 {
1693 return hash_tmpl_and_args (e->tmpl, e->args);
1694 }
1695
1696 /* Recursively calculate a hash value for a template argument ARG, for use
1697 in the hash tables of template specializations. */
1698
1699 hashval_t
1700 iterative_hash_template_arg (tree arg, hashval_t val)
1701 {
1702 unsigned HOST_WIDE_INT i;
1703 enum tree_code code;
1704 char tclass;
1705
1706 if (arg == NULL_TREE)
1707 return iterative_hash_object (arg, val);
1708
1709 if (!TYPE_P (arg))
1710 STRIP_NOPS (arg);
1711
1712 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1713 gcc_unreachable ();
1714
1715 code = TREE_CODE (arg);
1716 tclass = TREE_CODE_CLASS (code);
1717
1718 val = iterative_hash_object (code, val);
1719
1720 switch (code)
1721 {
1722 case ERROR_MARK:
1723 return val;
1724
1725 case IDENTIFIER_NODE:
1726 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1727
1728 case TREE_VEC:
1729 {
1730 int i, len = TREE_VEC_LENGTH (arg);
1731 for (i = 0; i < len; ++i)
1732 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1733 return val;
1734 }
1735
1736 case TYPE_PACK_EXPANSION:
1737 case EXPR_PACK_EXPANSION:
1738 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1739 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1740
1741 case TYPE_ARGUMENT_PACK:
1742 case NONTYPE_ARGUMENT_PACK:
1743 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1744
1745 case TREE_LIST:
1746 for (; arg; arg = TREE_CHAIN (arg))
1747 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1748 return val;
1749
1750 case OVERLOAD:
1751 for (; arg; arg = OVL_NEXT (arg))
1752 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1753 return val;
1754
1755 case CONSTRUCTOR:
1756 {
1757 tree field, value;
1758 iterative_hash_template_arg (TREE_TYPE (arg), val);
1759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1760 {
1761 val = iterative_hash_template_arg (field, val);
1762 val = iterative_hash_template_arg (value, val);
1763 }
1764 return val;
1765 }
1766
1767 case PARM_DECL:
1768 if (!DECL_ARTIFICIAL (arg))
1769 {
1770 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1771 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1772 }
1773 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1774
1775 case TARGET_EXPR:
1776 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1777
1778 case PTRMEM_CST:
1779 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1780 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1781
1782 case TEMPLATE_PARM_INDEX:
1783 val = iterative_hash_template_arg
1784 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1785 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1786 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1787
1788 case TRAIT_EXPR:
1789 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1790 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1791 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1792
1793 case BASELINK:
1794 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1795 val);
1796 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1797 val);
1798
1799 case MODOP_EXPR:
1800 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1801 code = TREE_CODE (TREE_OPERAND (arg, 1));
1802 val = iterative_hash_object (code, val);
1803 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1804
1805 case LAMBDA_EXPR:
1806 /* A lambda can't appear in a template arg, but don't crash on
1807 erroneous input. */
1808 gcc_assert (seen_error ());
1809 return val;
1810
1811 case CAST_EXPR:
1812 case IMPLICIT_CONV_EXPR:
1813 case STATIC_CAST_EXPR:
1814 case REINTERPRET_CAST_EXPR:
1815 case CONST_CAST_EXPR:
1816 case DYNAMIC_CAST_EXPR:
1817 case NEW_EXPR:
1818 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1819 /* Now hash operands as usual. */
1820 break;
1821
1822 default:
1823 break;
1824 }
1825
1826 switch (tclass)
1827 {
1828 case tcc_type:
1829 if (alias_template_specialization_p (arg))
1830 {
1831 // We want an alias specialization that survived strip_typedefs
1832 // to hash differently from its TYPE_CANONICAL, to avoid hash
1833 // collisions that compare as different in template_args_equal.
1834 // These could be dependent specializations that strip_typedefs
1835 // left alone, or untouched specializations because
1836 // coerce_template_parms returns the unconverted template
1837 // arguments if it sees incomplete argument packs.
1838 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1839 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1840 }
1841 if (TYPE_CANONICAL (arg))
1842 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1843 val);
1844 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1845 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1846 /* Otherwise just compare the types during lookup. */
1847 return val;
1848
1849 case tcc_declaration:
1850 case tcc_constant:
1851 return iterative_hash_expr (arg, val);
1852
1853 default:
1854 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1855 {
1856 unsigned n = cp_tree_operand_length (arg);
1857 for (i = 0; i < n; ++i)
1858 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1859 return val;
1860 }
1861 }
1862 gcc_unreachable ();
1863 return 0;
1864 }
1865
1866 /* Unregister the specialization SPEC as a specialization of TMPL.
1867 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1868 if the SPEC was listed as a specialization of TMPL.
1869
1870 Note that SPEC has been ggc_freed, so we can't look inside it. */
1871
1872 bool
1873 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1874 {
1875 spec_entry *entry;
1876 spec_entry elt;
1877
1878 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1879 elt.args = TI_ARGS (tinfo);
1880 elt.spec = NULL_TREE;
1881
1882 entry = decl_specializations->find (&elt);
1883 if (entry != NULL)
1884 {
1885 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1886 gcc_assert (new_spec != NULL_TREE);
1887 entry->spec = new_spec;
1888 return 1;
1889 }
1890
1891 return 0;
1892 }
1893
1894 /* Like register_specialization, but for local declarations. We are
1895 registering SPEC, an instantiation of TMPL. */
1896
1897 void
1898 register_local_specialization (tree spec, tree tmpl)
1899 {
1900 local_specializations->put (tmpl, spec);
1901 }
1902
1903 /* TYPE is a class type. Returns true if TYPE is an explicitly
1904 specialized class. */
1905
1906 bool
1907 explicit_class_specialization_p (tree type)
1908 {
1909 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1910 return false;
1911 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1912 }
1913
1914 /* Print the list of functions at FNS, going through all the overloads
1915 for each element of the list. Alternatively, FNS can not be a
1916 TREE_LIST, in which case it will be printed together with all the
1917 overloads.
1918
1919 MORE and *STR should respectively be FALSE and NULL when the function
1920 is called from the outside. They are used internally on recursive
1921 calls. print_candidates manages the two parameters and leaves NULL
1922 in *STR when it ends. */
1923
1924 static void
1925 print_candidates_1 (tree fns, bool more, const char **str)
1926 {
1927 tree fn, fn2;
1928 char *spaces = NULL;
1929
1930 for (fn = fns; fn; fn = OVL_NEXT (fn))
1931 if (TREE_CODE (fn) == TREE_LIST)
1932 {
1933 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1934 print_candidates_1 (TREE_VALUE (fn2),
1935 TREE_CHAIN (fn2) || more, str);
1936 }
1937 else
1938 {
1939 tree cand = OVL_CURRENT (fn);
1940 if (!*str)
1941 {
1942 /* Pick the prefix string. */
1943 if (!more && !OVL_NEXT (fns))
1944 {
1945 inform (DECL_SOURCE_LOCATION (cand),
1946 "candidate is: %#D", cand);
1947 continue;
1948 }
1949
1950 *str = _("candidates are:");
1951 spaces = get_spaces (*str);
1952 }
1953 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1954 *str = spaces ? spaces : *str;
1955 }
1956
1957 if (!more)
1958 {
1959 free (spaces);
1960 *str = NULL;
1961 }
1962 }
1963
1964 /* Print the list of candidate FNS in an error message. FNS can also
1965 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1966
1967 void
1968 print_candidates (tree fns)
1969 {
1970 const char *str = NULL;
1971 print_candidates_1 (fns, false, &str);
1972 gcc_assert (str == NULL);
1973 }
1974
1975 /* Get a (possibly) constrained template declaration for the
1976 purpose of ordering candidates. */
1977 static tree
1978 get_template_for_ordering (tree list)
1979 {
1980 gcc_assert (TREE_CODE (list) == TREE_LIST);
1981 tree f = TREE_VALUE (list);
1982 if (tree ti = DECL_TEMPLATE_INFO (f))
1983 return TI_TEMPLATE (ti);
1984 return f;
1985 }
1986
1987 /* Among candidates having the same signature, return the
1988 most constrained or NULL_TREE if there is no best candidate.
1989 If the signatures of candidates vary (e.g., template
1990 specialization vs. member function), then there can be no
1991 most constrained.
1992
1993 Note that we don't compare constraints on the functions
1994 themselves, but rather those of their templates. */
1995 static tree
1996 most_constrained_function (tree candidates)
1997 {
1998 // Try to find the best candidate in a first pass.
1999 tree champ = candidates;
2000 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2001 {
2002 int winner = more_constrained (get_template_for_ordering (champ),
2003 get_template_for_ordering (c));
2004 if (winner == -1)
2005 champ = c; // The candidate is more constrained
2006 else if (winner == 0)
2007 return NULL_TREE; // Neither is more constrained
2008 }
2009
2010 // Verify that the champ is better than previous candidates.
2011 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2012 if (!more_constrained (get_template_for_ordering (champ),
2013 get_template_for_ordering (c)))
2014 return NULL_TREE;
2015 }
2016
2017 return champ;
2018 }
2019
2020
2021 /* Returns the template (one of the functions given by TEMPLATE_ID)
2022 which can be specialized to match the indicated DECL with the
2023 explicit template args given in TEMPLATE_ID. The DECL may be
2024 NULL_TREE if none is available. In that case, the functions in
2025 TEMPLATE_ID are non-members.
2026
2027 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2028 specialization of a member template.
2029
2030 The TEMPLATE_COUNT is the number of references to qualifying
2031 template classes that appeared in the name of the function. See
2032 check_explicit_specialization for a more accurate description.
2033
2034 TSK indicates what kind of template declaration (if any) is being
2035 declared. TSK_TEMPLATE indicates that the declaration given by
2036 DECL, though a FUNCTION_DECL, has template parameters, and is
2037 therefore a template function.
2038
2039 The template args (those explicitly specified and those deduced)
2040 are output in a newly created vector *TARGS_OUT.
2041
2042 If it is impossible to determine the result, an error message is
2043 issued. The error_mark_node is returned to indicate failure. */
2044
2045 static tree
2046 determine_specialization (tree template_id,
2047 tree decl,
2048 tree* targs_out,
2049 int need_member_template,
2050 int template_count,
2051 tmpl_spec_kind tsk)
2052 {
2053 tree fns;
2054 tree targs;
2055 tree explicit_targs;
2056 tree candidates = NULL_TREE;
2057
2058 /* A TREE_LIST of templates of which DECL may be a specialization.
2059 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2060 corresponding TREE_PURPOSE is the set of template arguments that,
2061 when used to instantiate the template, would produce a function
2062 with the signature of DECL. */
2063 tree templates = NULL_TREE;
2064 int header_count;
2065 cp_binding_level *b;
2066
2067 *targs_out = NULL_TREE;
2068
2069 if (template_id == error_mark_node || decl == error_mark_node)
2070 return error_mark_node;
2071
2072 /* We shouldn't be specializing a member template of an
2073 unspecialized class template; we already gave an error in
2074 check_specialization_scope, now avoid crashing. */
2075 if (template_count && DECL_CLASS_SCOPE_P (decl)
2076 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2077 {
2078 gcc_assert (errorcount);
2079 return error_mark_node;
2080 }
2081
2082 fns = TREE_OPERAND (template_id, 0);
2083 explicit_targs = TREE_OPERAND (template_id, 1);
2084
2085 if (fns == error_mark_node)
2086 return error_mark_node;
2087
2088 /* Check for baselinks. */
2089 if (BASELINK_P (fns))
2090 fns = BASELINK_FUNCTIONS (fns);
2091
2092 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2093 {
2094 error ("%qD is not a function template", fns);
2095 return error_mark_node;
2096 }
2097 else if (VAR_P (decl) && !variable_template_p (fns))
2098 {
2099 error ("%qD is not a variable template", fns);
2100 return error_mark_node;
2101 }
2102
2103 /* Count the number of template headers specified for this
2104 specialization. */
2105 header_count = 0;
2106 for (b = current_binding_level;
2107 b->kind == sk_template_parms;
2108 b = b->level_chain)
2109 ++header_count;
2110
2111 tree orig_fns = fns;
2112
2113 if (variable_template_p (fns))
2114 {
2115 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2116 targs = coerce_template_parms (parms, explicit_targs, fns,
2117 tf_warning_or_error,
2118 /*req_all*/true, /*use_defarg*/true);
2119 if (targs != error_mark_node)
2120 templates = tree_cons (targs, fns, templates);
2121 }
2122 else for (; fns; fns = OVL_NEXT (fns))
2123 {
2124 tree fn = OVL_CURRENT (fns);
2125
2126 if (TREE_CODE (fn) == TEMPLATE_DECL)
2127 {
2128 tree decl_arg_types;
2129 tree fn_arg_types;
2130 tree insttype;
2131
2132 /* In case of explicit specialization, we need to check if
2133 the number of template headers appearing in the specialization
2134 is correct. This is usually done in check_explicit_specialization,
2135 but the check done there cannot be exhaustive when specializing
2136 member functions. Consider the following code:
2137
2138 template <> void A<int>::f(int);
2139 template <> template <> void A<int>::f(int);
2140
2141 Assuming that A<int> is not itself an explicit specialization
2142 already, the first line specializes "f" which is a non-template
2143 member function, whilst the second line specializes "f" which
2144 is a template member function. So both lines are syntactically
2145 correct, and check_explicit_specialization does not reject
2146 them.
2147
2148 Here, we can do better, as we are matching the specialization
2149 against the declarations. We count the number of template
2150 headers, and we check if they match TEMPLATE_COUNT + 1
2151 (TEMPLATE_COUNT is the number of qualifying template classes,
2152 plus there must be another header for the member template
2153 itself).
2154
2155 Notice that if header_count is zero, this is not a
2156 specialization but rather a template instantiation, so there
2157 is no check we can perform here. */
2158 if (header_count && header_count != template_count + 1)
2159 continue;
2160
2161 /* Check that the number of template arguments at the
2162 innermost level for DECL is the same as for FN. */
2163 if (current_binding_level->kind == sk_template_parms
2164 && !current_binding_level->explicit_spec_p
2165 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2166 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2167 (current_template_parms))))
2168 continue;
2169
2170 /* DECL might be a specialization of FN. */
2171 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2172 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2173
2174 /* For a non-static member function, we need to make sure
2175 that the const qualification is the same. Since
2176 get_bindings does not try to merge the "this" parameter,
2177 we must do the comparison explicitly. */
2178 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2179 && !same_type_p (TREE_VALUE (fn_arg_types),
2180 TREE_VALUE (decl_arg_types)))
2181 continue;
2182
2183 /* Skip the "this" parameter and, for constructors of
2184 classes with virtual bases, the VTT parameter. A
2185 full specialization of a constructor will have a VTT
2186 parameter, but a template never will. */
2187 decl_arg_types
2188 = skip_artificial_parms_for (decl, decl_arg_types);
2189 fn_arg_types
2190 = skip_artificial_parms_for (fn, fn_arg_types);
2191
2192 /* Function templates cannot be specializations; there are
2193 no partial specializations of functions. Therefore, if
2194 the type of DECL does not match FN, there is no
2195 match.
2196
2197 Note that it should never be the case that we have both
2198 candidates added here, and for regular member functions
2199 below. */
2200 if (tsk == tsk_template)
2201 {
2202 if (compparms (fn_arg_types, decl_arg_types))
2203 candidates = tree_cons (NULL_TREE, fn, candidates);
2204 continue;
2205 }
2206
2207 /* See whether this function might be a specialization of this
2208 template. Suppress access control because we might be trying
2209 to make this specialization a friend, and we have already done
2210 access control for the declaration of the specialization. */
2211 push_deferring_access_checks (dk_no_check);
2212 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2213 pop_deferring_access_checks ();
2214
2215 if (!targs)
2216 /* We cannot deduce template arguments that when used to
2217 specialize TMPL will produce DECL. */
2218 continue;
2219
2220 /* Remove, from the set of candidates, all those functions
2221 whose constraints are not satisfied. */
2222 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2223 continue;
2224
2225 // Then, try to form the new function type.
2226 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2227 if (insttype == error_mark_node)
2228 continue;
2229 fn_arg_types
2230 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2231 if (!compparms (fn_arg_types, decl_arg_types))
2232 continue;
2233
2234 /* Save this template, and the arguments deduced. */
2235 templates = tree_cons (targs, fn, templates);
2236 }
2237 else if (need_member_template)
2238 /* FN is an ordinary member function, and we need a
2239 specialization of a member template. */
2240 ;
2241 else if (TREE_CODE (fn) != FUNCTION_DECL)
2242 /* We can get IDENTIFIER_NODEs here in certain erroneous
2243 cases. */
2244 ;
2245 else if (!DECL_FUNCTION_MEMBER_P (fn))
2246 /* This is just an ordinary non-member function. Nothing can
2247 be a specialization of that. */
2248 ;
2249 else if (DECL_ARTIFICIAL (fn))
2250 /* Cannot specialize functions that are created implicitly. */
2251 ;
2252 else
2253 {
2254 tree decl_arg_types;
2255
2256 /* This is an ordinary member function. However, since
2257 we're here, we can assume its enclosing class is a
2258 template class. For example,
2259
2260 template <typename T> struct S { void f(); };
2261 template <> void S<int>::f() {}
2262
2263 Here, S<int>::f is a non-template, but S<int> is a
2264 template class. If FN has the same type as DECL, we
2265 might be in business. */
2266
2267 if (!DECL_TEMPLATE_INFO (fn))
2268 /* Its enclosing class is an explicit specialization
2269 of a template class. This is not a candidate. */
2270 continue;
2271
2272 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2273 TREE_TYPE (TREE_TYPE (fn))))
2274 /* The return types differ. */
2275 continue;
2276
2277 /* Adjust the type of DECL in case FN is a static member. */
2278 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2279 if (DECL_STATIC_FUNCTION_P (fn)
2280 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2281 decl_arg_types = TREE_CHAIN (decl_arg_types);
2282
2283 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2284 decl_arg_types))
2285 continue;
2286
2287 // If the deduced arguments do not satisfy the constraints,
2288 // this is not a candidate.
2289 if (flag_concepts && !constraints_satisfied_p (fn))
2290 continue;
2291
2292 // Add the candidate.
2293 candidates = tree_cons (NULL_TREE, fn, candidates);
2294 }
2295 }
2296
2297 if (templates && TREE_CHAIN (templates))
2298 {
2299 /* We have:
2300
2301 [temp.expl.spec]
2302
2303 It is possible for a specialization with a given function
2304 signature to be instantiated from more than one function
2305 template. In such cases, explicit specification of the
2306 template arguments must be used to uniquely identify the
2307 function template specialization being specialized.
2308
2309 Note that here, there's no suggestion that we're supposed to
2310 determine which of the candidate templates is most
2311 specialized. However, we, also have:
2312
2313 [temp.func.order]
2314
2315 Partial ordering of overloaded function template
2316 declarations is used in the following contexts to select
2317 the function template to which a function template
2318 specialization refers:
2319
2320 -- when an explicit specialization refers to a function
2321 template.
2322
2323 So, we do use the partial ordering rules, at least for now.
2324 This extension can only serve to make invalid programs valid,
2325 so it's safe. And, there is strong anecdotal evidence that
2326 the committee intended the partial ordering rules to apply;
2327 the EDG front end has that behavior, and John Spicer claims
2328 that the committee simply forgot to delete the wording in
2329 [temp.expl.spec]. */
2330 tree tmpl = most_specialized_instantiation (templates);
2331 if (tmpl != error_mark_node)
2332 {
2333 templates = tmpl;
2334 TREE_CHAIN (templates) = NULL_TREE;
2335 }
2336 }
2337
2338 // Concepts allows multiple declarations of member functions
2339 // with the same signature. Like above, we need to rely on
2340 // on the partial ordering of those candidates to determine which
2341 // is the best.
2342 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2343 {
2344 if (tree cand = most_constrained_function (candidates))
2345 {
2346 candidates = cand;
2347 TREE_CHAIN (cand) = NULL_TREE;
2348 }
2349 }
2350
2351 if (templates == NULL_TREE && candidates == NULL_TREE)
2352 {
2353 error ("template-id %qD for %q+D does not match any template "
2354 "declaration", template_id, decl);
2355 if (header_count && header_count != template_count + 1)
2356 inform (input_location, "saw %d %<template<>%>, need %d for "
2357 "specializing a member function template",
2358 header_count, template_count + 1);
2359 else
2360 print_candidates (orig_fns);
2361 return error_mark_node;
2362 }
2363 else if ((templates && TREE_CHAIN (templates))
2364 || (candidates && TREE_CHAIN (candidates))
2365 || (templates && candidates))
2366 {
2367 error ("ambiguous template specialization %qD for %q+D",
2368 template_id, decl);
2369 candidates = chainon (candidates, templates);
2370 print_candidates (candidates);
2371 return error_mark_node;
2372 }
2373
2374 /* We have one, and exactly one, match. */
2375 if (candidates)
2376 {
2377 tree fn = TREE_VALUE (candidates);
2378 *targs_out = copy_node (DECL_TI_ARGS (fn));
2379
2380 // Propagate the candidate's constraints to the declaration.
2381 set_constraints (decl, get_constraints (fn));
2382
2383 /* DECL is a re-declaration or partial instantiation of a template
2384 function. */
2385 if (TREE_CODE (fn) == TEMPLATE_DECL)
2386 return fn;
2387 /* It was a specialization of an ordinary member function in a
2388 template class. */
2389 return DECL_TI_TEMPLATE (fn);
2390 }
2391
2392 /* It was a specialization of a template. */
2393 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2394 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2395 {
2396 *targs_out = copy_node (targs);
2397 SET_TMPL_ARGS_LEVEL (*targs_out,
2398 TMPL_ARGS_DEPTH (*targs_out),
2399 TREE_PURPOSE (templates));
2400 }
2401 else
2402 *targs_out = TREE_PURPOSE (templates);
2403 return TREE_VALUE (templates);
2404 }
2405
2406 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2407 but with the default argument values filled in from those in the
2408 TMPL_TYPES. */
2409
2410 static tree
2411 copy_default_args_to_explicit_spec_1 (tree spec_types,
2412 tree tmpl_types)
2413 {
2414 tree new_spec_types;
2415
2416 if (!spec_types)
2417 return NULL_TREE;
2418
2419 if (spec_types == void_list_node)
2420 return void_list_node;
2421
2422 /* Substitute into the rest of the list. */
2423 new_spec_types =
2424 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2425 TREE_CHAIN (tmpl_types));
2426
2427 /* Add the default argument for this parameter. */
2428 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2429 TREE_VALUE (spec_types),
2430 new_spec_types);
2431 }
2432
2433 /* DECL is an explicit specialization. Replicate default arguments
2434 from the template it specializes. (That way, code like:
2435
2436 template <class T> void f(T = 3);
2437 template <> void f(double);
2438 void g () { f (); }
2439
2440 works, as required.) An alternative approach would be to look up
2441 the correct default arguments at the call-site, but this approach
2442 is consistent with how implicit instantiations are handled. */
2443
2444 static void
2445 copy_default_args_to_explicit_spec (tree decl)
2446 {
2447 tree tmpl;
2448 tree spec_types;
2449 tree tmpl_types;
2450 tree new_spec_types;
2451 tree old_type;
2452 tree new_type;
2453 tree t;
2454 tree object_type = NULL_TREE;
2455 tree in_charge = NULL_TREE;
2456 tree vtt = NULL_TREE;
2457
2458 /* See if there's anything we need to do. */
2459 tmpl = DECL_TI_TEMPLATE (decl);
2460 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2461 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2462 if (TREE_PURPOSE (t))
2463 break;
2464 if (!t)
2465 return;
2466
2467 old_type = TREE_TYPE (decl);
2468 spec_types = TYPE_ARG_TYPES (old_type);
2469
2470 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2471 {
2472 /* Remove the this pointer, but remember the object's type for
2473 CV quals. */
2474 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2475 spec_types = TREE_CHAIN (spec_types);
2476 tmpl_types = TREE_CHAIN (tmpl_types);
2477
2478 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2479 {
2480 /* DECL may contain more parameters than TMPL due to the extra
2481 in-charge parameter in constructors and destructors. */
2482 in_charge = spec_types;
2483 spec_types = TREE_CHAIN (spec_types);
2484 }
2485 if (DECL_HAS_VTT_PARM_P (decl))
2486 {
2487 vtt = spec_types;
2488 spec_types = TREE_CHAIN (spec_types);
2489 }
2490 }
2491
2492 /* Compute the merged default arguments. */
2493 new_spec_types =
2494 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2495
2496 /* Compute the new FUNCTION_TYPE. */
2497 if (object_type)
2498 {
2499 if (vtt)
2500 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2501 TREE_VALUE (vtt),
2502 new_spec_types);
2503
2504 if (in_charge)
2505 /* Put the in-charge parameter back. */
2506 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2507 TREE_VALUE (in_charge),
2508 new_spec_types);
2509
2510 new_type = build_method_type_directly (object_type,
2511 TREE_TYPE (old_type),
2512 new_spec_types);
2513 }
2514 else
2515 new_type = build_function_type (TREE_TYPE (old_type),
2516 new_spec_types);
2517 new_type = cp_build_type_attribute_variant (new_type,
2518 TYPE_ATTRIBUTES (old_type));
2519 new_type = build_exception_variant (new_type,
2520 TYPE_RAISES_EXCEPTIONS (old_type));
2521
2522 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2523 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2524
2525 TREE_TYPE (decl) = new_type;
2526 }
2527
2528 /* Return the number of template headers we expect to see for a definition
2529 or specialization of CTYPE or one of its non-template members. */
2530
2531 int
2532 num_template_headers_for_class (tree ctype)
2533 {
2534 int num_templates = 0;
2535
2536 while (ctype && CLASS_TYPE_P (ctype))
2537 {
2538 /* You're supposed to have one `template <...>' for every
2539 template class, but you don't need one for a full
2540 specialization. For example:
2541
2542 template <class T> struct S{};
2543 template <> struct S<int> { void f(); };
2544 void S<int>::f () {}
2545
2546 is correct; there shouldn't be a `template <>' for the
2547 definition of `S<int>::f'. */
2548 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2549 /* If CTYPE does not have template information of any
2550 kind, then it is not a template, nor is it nested
2551 within a template. */
2552 break;
2553 if (explicit_class_specialization_p (ctype))
2554 break;
2555 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2556 ++num_templates;
2557
2558 ctype = TYPE_CONTEXT (ctype);
2559 }
2560
2561 return num_templates;
2562 }
2563
2564 /* Do a simple sanity check on the template headers that precede the
2565 variable declaration DECL. */
2566
2567 void
2568 check_template_variable (tree decl)
2569 {
2570 tree ctx = CP_DECL_CONTEXT (decl);
2571 int wanted = num_template_headers_for_class (ctx);
2572 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2573 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2574 {
2575 if (cxx_dialect < cxx14)
2576 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2577 "variable templates only available with "
2578 "-std=c++14 or -std=gnu++14");
2579
2580 // Namespace-scope variable templates should have a template header.
2581 ++wanted;
2582 }
2583 if (template_header_count > wanted)
2584 {
2585 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2586 "too many template headers for %D (should be %d)",
2587 decl, wanted);
2588 if (warned && CLASS_TYPE_P (ctx)
2589 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2590 inform (DECL_SOURCE_LOCATION (decl),
2591 "members of an explicitly specialized class are defined "
2592 "without a template header");
2593 }
2594 }
2595
2596 /* An explicit specialization whose declarator-id or class-head-name is not
2597 qualified shall be declared in the nearest enclosing namespace of the
2598 template, or, if the namespace is inline (7.3.1), any namespace from its
2599 enclosing namespace set.
2600
2601 If the name declared in the explicit instantiation is an unqualified name,
2602 the explicit instantiation shall appear in the namespace where its template
2603 is declared or, if that namespace is inline (7.3.1), any namespace from its
2604 enclosing namespace set. */
2605
2606 void
2607 check_unqualified_spec_or_inst (tree t, location_t loc)
2608 {
2609 tree tmpl = most_general_template (t);
2610 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2611 && !is_associated_namespace (current_namespace,
2612 CP_DECL_CONTEXT (tmpl)))
2613 {
2614 if (processing_specialization)
2615 permerror (loc, "explicit specialization of %qD outside its "
2616 "namespace must use a nested-name-specifier", tmpl);
2617 else if (processing_explicit_instantiation
2618 && cxx_dialect >= cxx11)
2619 /* This was allowed in C++98, so only pedwarn. */
2620 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2621 "outside its namespace must use a nested-name-"
2622 "specifier", tmpl);
2623 }
2624 }
2625
2626 /* Check to see if the function just declared, as indicated in
2627 DECLARATOR, and in DECL, is a specialization of a function
2628 template. We may also discover that the declaration is an explicit
2629 instantiation at this point.
2630
2631 Returns DECL, or an equivalent declaration that should be used
2632 instead if all goes well. Issues an error message if something is
2633 amiss. Returns error_mark_node if the error is not easily
2634 recoverable.
2635
2636 FLAGS is a bitmask consisting of the following flags:
2637
2638 2: The function has a definition.
2639 4: The function is a friend.
2640
2641 The TEMPLATE_COUNT is the number of references to qualifying
2642 template classes that appeared in the name of the function. For
2643 example, in
2644
2645 template <class T> struct S { void f(); };
2646 void S<int>::f();
2647
2648 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2649 classes are not counted in the TEMPLATE_COUNT, so that in
2650
2651 template <class T> struct S {};
2652 template <> struct S<int> { void f(); }
2653 template <> void S<int>::f();
2654
2655 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2656 invalid; there should be no template <>.)
2657
2658 If the function is a specialization, it is marked as such via
2659 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2660 is set up correctly, and it is added to the list of specializations
2661 for that template. */
2662
2663 tree
2664 check_explicit_specialization (tree declarator,
2665 tree decl,
2666 int template_count,
2667 int flags)
2668 {
2669 int have_def = flags & 2;
2670 int is_friend = flags & 4;
2671 bool is_concept = flags & 8;
2672 int specialization = 0;
2673 int explicit_instantiation = 0;
2674 int member_specialization = 0;
2675 tree ctype = DECL_CLASS_CONTEXT (decl);
2676 tree dname = DECL_NAME (decl);
2677 tmpl_spec_kind tsk;
2678
2679 if (is_friend)
2680 {
2681 if (!processing_specialization)
2682 tsk = tsk_none;
2683 else
2684 tsk = tsk_excessive_parms;
2685 }
2686 else
2687 tsk = current_tmpl_spec_kind (template_count);
2688
2689 switch (tsk)
2690 {
2691 case tsk_none:
2692 if (processing_specialization && !VAR_P (decl))
2693 {
2694 specialization = 1;
2695 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2696 }
2697 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2698 {
2699 if (is_friend)
2700 /* This could be something like:
2701
2702 template <class T> void f(T);
2703 class S { friend void f<>(int); } */
2704 specialization = 1;
2705 else
2706 {
2707 /* This case handles bogus declarations like template <>
2708 template <class T> void f<int>(); */
2709
2710 error ("template-id %qD in declaration of primary template",
2711 declarator);
2712 return decl;
2713 }
2714 }
2715 break;
2716
2717 case tsk_invalid_member_spec:
2718 /* The error has already been reported in
2719 check_specialization_scope. */
2720 return error_mark_node;
2721
2722 case tsk_invalid_expl_inst:
2723 error ("template parameter list used in explicit instantiation");
2724
2725 /* Fall through. */
2726
2727 case tsk_expl_inst:
2728 if (have_def)
2729 error ("definition provided for explicit instantiation");
2730
2731 explicit_instantiation = 1;
2732 break;
2733
2734 case tsk_excessive_parms:
2735 case tsk_insufficient_parms:
2736 if (tsk == tsk_excessive_parms)
2737 error ("too many template parameter lists in declaration of %qD",
2738 decl);
2739 else if (template_header_count)
2740 error("too few template parameter lists in declaration of %qD", decl);
2741 else
2742 error("explicit specialization of %qD must be introduced by "
2743 "%<template <>%>", decl);
2744
2745 /* Fall through. */
2746 case tsk_expl_spec:
2747 if (is_concept)
2748 error ("explicit specialization declared %<concept%>");
2749
2750 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2751 /* In cases like template<> constexpr bool v = true;
2752 We'll give an error in check_template_variable. */
2753 break;
2754
2755 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2756 if (ctype)
2757 member_specialization = 1;
2758 else
2759 specialization = 1;
2760 break;
2761
2762 case tsk_template:
2763 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2764 {
2765 /* This case handles bogus declarations like template <>
2766 template <class T> void f<int>(); */
2767
2768 if (!uses_template_parms (declarator))
2769 error ("template-id %qD in declaration of primary template",
2770 declarator);
2771 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2772 {
2773 /* Partial specialization of variable template. */
2774 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2775 specialization = 1;
2776 goto ok;
2777 }
2778 else if (cxx_dialect < cxx14)
2779 error ("non-type partial specialization %qD "
2780 "is not allowed", declarator);
2781 else
2782 error ("non-class, non-variable partial specialization %qD "
2783 "is not allowed", declarator);
2784 return decl;
2785 ok:;
2786 }
2787
2788 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2789 /* This is a specialization of a member template, without
2790 specialization the containing class. Something like:
2791
2792 template <class T> struct S {
2793 template <class U> void f (U);
2794 };
2795 template <> template <class U> void S<int>::f(U) {}
2796
2797 That's a specialization -- but of the entire template. */
2798 specialization = 1;
2799 break;
2800
2801 default:
2802 gcc_unreachable ();
2803 }
2804
2805 if ((specialization || member_specialization)
2806 /* This doesn't apply to variable templates. */
2807 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2808 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2809 {
2810 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2811 for (; t; t = TREE_CHAIN (t))
2812 if (TREE_PURPOSE (t))
2813 {
2814 permerror (input_location,
2815 "default argument specified in explicit specialization");
2816 break;
2817 }
2818 }
2819
2820 if (specialization || member_specialization || explicit_instantiation)
2821 {
2822 tree tmpl = NULL_TREE;
2823 tree targs = NULL_TREE;
2824 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2825
2826 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2827 if (!was_template_id)
2828 {
2829 tree fns;
2830
2831 gcc_assert (identifier_p (declarator));
2832 if (ctype)
2833 fns = dname;
2834 else
2835 {
2836 /* If there is no class context, the explicit instantiation
2837 must be at namespace scope. */
2838 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2839
2840 /* Find the namespace binding, using the declaration
2841 context. */
2842 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2843 false, true);
2844 if (fns == error_mark_node)
2845 /* If lookup fails, look for a friend declaration so we can
2846 give a better diagnostic. */
2847 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2848 /*type*/false, /*complain*/true,
2849 /*hidden*/true);
2850
2851 if (fns == error_mark_node || !is_overloaded_fn (fns))
2852 {
2853 error ("%qD is not a template function", dname);
2854 fns = error_mark_node;
2855 }
2856 }
2857
2858 declarator = lookup_template_function (fns, NULL_TREE);
2859 }
2860
2861 if (declarator == error_mark_node)
2862 return error_mark_node;
2863
2864 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2865 {
2866 if (!explicit_instantiation)
2867 /* A specialization in class scope. This is invalid,
2868 but the error will already have been flagged by
2869 check_specialization_scope. */
2870 return error_mark_node;
2871 else
2872 {
2873 /* It's not valid to write an explicit instantiation in
2874 class scope, e.g.:
2875
2876 class C { template void f(); }
2877
2878 This case is caught by the parser. However, on
2879 something like:
2880
2881 template class C { void f(); };
2882
2883 (which is invalid) we can get here. The error will be
2884 issued later. */
2885 ;
2886 }
2887
2888 return decl;
2889 }
2890 else if (ctype != NULL_TREE
2891 && (identifier_p (TREE_OPERAND (declarator, 0))))
2892 {
2893 // We'll match variable templates in start_decl.
2894 if (VAR_P (decl))
2895 return decl;
2896
2897 /* Find the list of functions in ctype that have the same
2898 name as the declared function. */
2899 tree name = TREE_OPERAND (declarator, 0);
2900 tree fns = NULL_TREE;
2901 int idx;
2902
2903 if (constructor_name_p (name, ctype))
2904 {
2905 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2906
2907 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2908 : !CLASSTYPE_DESTRUCTORS (ctype))
2909 {
2910 /* From [temp.expl.spec]:
2911
2912 If such an explicit specialization for the member
2913 of a class template names an implicitly-declared
2914 special member function (clause _special_), the
2915 program is ill-formed.
2916
2917 Similar language is found in [temp.explicit]. */
2918 error ("specialization of implicitly-declared special member function");
2919 return error_mark_node;
2920 }
2921
2922 name = is_constructor ? ctor_identifier : dtor_identifier;
2923 }
2924
2925 if (!DECL_CONV_FN_P (decl))
2926 {
2927 idx = lookup_fnfields_1 (ctype, name);
2928 if (idx >= 0)
2929 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2930 }
2931 else
2932 {
2933 vec<tree, va_gc> *methods;
2934 tree ovl;
2935
2936 /* For a type-conversion operator, we cannot do a
2937 name-based lookup. We might be looking for `operator
2938 int' which will be a specialization of `operator T'.
2939 So, we find *all* the conversion operators, and then
2940 select from them. */
2941 fns = NULL_TREE;
2942
2943 methods = CLASSTYPE_METHOD_VEC (ctype);
2944 if (methods)
2945 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2946 methods->iterate (idx, &ovl);
2947 ++idx)
2948 {
2949 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2950 /* There are no more conversion functions. */
2951 break;
2952
2953 /* Glue all these conversion functions together
2954 with those we already have. */
2955 for (; ovl; ovl = OVL_NEXT (ovl))
2956 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2957 }
2958 }
2959
2960 if (fns == NULL_TREE)
2961 {
2962 error ("no member function %qD declared in %qT", name, ctype);
2963 return error_mark_node;
2964 }
2965 else
2966 TREE_OPERAND (declarator, 0) = fns;
2967 }
2968
2969 /* Figure out what exactly is being specialized at this point.
2970 Note that for an explicit instantiation, even one for a
2971 member function, we cannot tell a priori whether the
2972 instantiation is for a member template, or just a member
2973 function of a template class. Even if a member template is
2974 being instantiated, the member template arguments may be
2975 elided if they can be deduced from the rest of the
2976 declaration. */
2977 tmpl = determine_specialization (declarator, decl,
2978 &targs,
2979 member_specialization,
2980 template_count,
2981 tsk);
2982
2983 if (!tmpl || tmpl == error_mark_node)
2984 /* We couldn't figure out what this declaration was
2985 specializing. */
2986 return error_mark_node;
2987 else
2988 {
2989 if (TREE_CODE (decl) == FUNCTION_DECL
2990 && DECL_HIDDEN_FRIEND_P (tmpl))
2991 {
2992 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2993 "friend declaration %qD is not visible to "
2994 "explicit specialization", tmpl))
2995 inform (DECL_SOURCE_LOCATION (tmpl),
2996 "friend declaration here");
2997 }
2998 else if (!ctype && !is_friend
2999 && CP_DECL_CONTEXT (decl) == current_namespace)
3000 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3001
3002 tree gen_tmpl = most_general_template (tmpl);
3003
3004 if (explicit_instantiation)
3005 {
3006 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3007 is done by do_decl_instantiation later. */
3008
3009 int arg_depth = TMPL_ARGS_DEPTH (targs);
3010 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3011
3012 if (arg_depth > parm_depth)
3013 {
3014 /* If TMPL is not the most general template (for
3015 example, if TMPL is a friend template that is
3016 injected into namespace scope), then there will
3017 be too many levels of TARGS. Remove some of them
3018 here. */
3019 int i;
3020 tree new_targs;
3021
3022 new_targs = make_tree_vec (parm_depth);
3023 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3024 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3025 = TREE_VEC_ELT (targs, i);
3026 targs = new_targs;
3027 }
3028
3029 return instantiate_template (tmpl, targs, tf_error);
3030 }
3031
3032 /* If we thought that the DECL was a member function, but it
3033 turns out to be specializing a static member function,
3034 make DECL a static member function as well. */
3035 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3036 && DECL_STATIC_FUNCTION_P (tmpl)
3037 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3038 revert_static_member_fn (decl);
3039
3040 /* If this is a specialization of a member template of a
3041 template class, we want to return the TEMPLATE_DECL, not
3042 the specialization of it. */
3043 if (tsk == tsk_template && !was_template_id)
3044 {
3045 tree result = DECL_TEMPLATE_RESULT (tmpl);
3046 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3047 DECL_INITIAL (result) = NULL_TREE;
3048 if (have_def)
3049 {
3050 tree parm;
3051 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3052 DECL_SOURCE_LOCATION (result)
3053 = DECL_SOURCE_LOCATION (decl);
3054 /* We want to use the argument list specified in the
3055 definition, not in the original declaration. */
3056 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3057 for (parm = DECL_ARGUMENTS (result); parm;
3058 parm = DECL_CHAIN (parm))
3059 DECL_CONTEXT (parm) = result;
3060 }
3061 return register_specialization (tmpl, gen_tmpl, targs,
3062 is_friend, 0);
3063 }
3064
3065 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3066 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3067
3068 if (was_template_id)
3069 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3070
3071 /* Inherit default function arguments from the template
3072 DECL is specializing. */
3073 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3074 copy_default_args_to_explicit_spec (decl);
3075
3076 /* This specialization has the same protection as the
3077 template it specializes. */
3078 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3079 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3080
3081 /* 7.1.1-1 [dcl.stc]
3082
3083 A storage-class-specifier shall not be specified in an
3084 explicit specialization...
3085
3086 The parser rejects these, so unless action is taken here,
3087 explicit function specializations will always appear with
3088 global linkage.
3089
3090 The action recommended by the C++ CWG in response to C++
3091 defect report 605 is to make the storage class and linkage
3092 of the explicit specialization match the templated function:
3093
3094 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3095 */
3096 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3097 {
3098 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3099 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3100
3101 /* A concept cannot be specialized. */
3102 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3103 {
3104 error ("explicit specialization of function concept %qD",
3105 gen_tmpl);
3106 return error_mark_node;
3107 }
3108
3109 /* This specialization has the same linkage and visibility as
3110 the function template it specializes. */
3111 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3112 if (! TREE_PUBLIC (decl))
3113 {
3114 DECL_INTERFACE_KNOWN (decl) = 1;
3115 DECL_NOT_REALLY_EXTERN (decl) = 1;
3116 }
3117 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3118 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3119 {
3120 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3121 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3122 }
3123 }
3124
3125 /* If DECL is a friend declaration, declared using an
3126 unqualified name, the namespace associated with DECL may
3127 have been set incorrectly. For example, in:
3128
3129 template <typename T> void f(T);
3130 namespace N {
3131 struct S { friend void f<int>(int); }
3132 }
3133
3134 we will have set the DECL_CONTEXT for the friend
3135 declaration to N, rather than to the global namespace. */
3136 if (DECL_NAMESPACE_SCOPE_P (decl))
3137 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3138
3139 if (is_friend && !have_def)
3140 /* This is not really a declaration of a specialization.
3141 It's just the name of an instantiation. But, it's not
3142 a request for an instantiation, either. */
3143 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3144 else if (TREE_CODE (decl) == FUNCTION_DECL)
3145 /* A specialization is not necessarily COMDAT. */
3146 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3147 && DECL_DECLARED_INLINE_P (decl));
3148 else if (VAR_P (decl))
3149 DECL_COMDAT (decl) = false;
3150
3151 /* If this is a full specialization, register it so that we can find
3152 it again. Partial specializations will be registered in
3153 process_partial_specialization. */
3154 if (!processing_template_decl)
3155 decl = register_specialization (decl, gen_tmpl, targs,
3156 is_friend, 0);
3157
3158 /* A 'structor should already have clones. */
3159 gcc_assert (decl == error_mark_node
3160 || variable_template_p (tmpl)
3161 || !(DECL_CONSTRUCTOR_P (decl)
3162 || DECL_DESTRUCTOR_P (decl))
3163 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3164 }
3165 }
3166
3167 return decl;
3168 }
3169
3170 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3171 parameters. These are represented in the same format used for
3172 DECL_TEMPLATE_PARMS. */
3173
3174 int
3175 comp_template_parms (const_tree parms1, const_tree parms2)
3176 {
3177 const_tree p1;
3178 const_tree p2;
3179
3180 if (parms1 == parms2)
3181 return 1;
3182
3183 for (p1 = parms1, p2 = parms2;
3184 p1 != NULL_TREE && p2 != NULL_TREE;
3185 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3186 {
3187 tree t1 = TREE_VALUE (p1);
3188 tree t2 = TREE_VALUE (p2);
3189 int i;
3190
3191 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3192 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3193
3194 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3195 return 0;
3196
3197 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3198 {
3199 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3200 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3201
3202 /* If either of the template parameters are invalid, assume
3203 they match for the sake of error recovery. */
3204 if (error_operand_p (parm1) || error_operand_p (parm2))
3205 return 1;
3206
3207 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3208 return 0;
3209
3210 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3211 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3212 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3213 continue;
3214 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3215 return 0;
3216 }
3217 }
3218
3219 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3220 /* One set of parameters has more parameters lists than the
3221 other. */
3222 return 0;
3223
3224 return 1;
3225 }
3226
3227 /* Determine whether PARM is a parameter pack. */
3228
3229 bool
3230 template_parameter_pack_p (const_tree parm)
3231 {
3232 /* Determine if we have a non-type template parameter pack. */
3233 if (TREE_CODE (parm) == PARM_DECL)
3234 return (DECL_TEMPLATE_PARM_P (parm)
3235 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3236 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3237 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3238
3239 /* If this is a list of template parameters, we could get a
3240 TYPE_DECL or a TEMPLATE_DECL. */
3241 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3242 parm = TREE_TYPE (parm);
3243
3244 /* Otherwise it must be a type template parameter. */
3245 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3246 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3247 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3248 }
3249
3250 /* Determine if T is a function parameter pack. */
3251
3252 bool
3253 function_parameter_pack_p (const_tree t)
3254 {
3255 if (t && TREE_CODE (t) == PARM_DECL)
3256 return DECL_PACK_P (t);
3257 return false;
3258 }
3259
3260 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3261 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3262
3263 tree
3264 get_function_template_decl (const_tree primary_func_tmpl_inst)
3265 {
3266 if (! primary_func_tmpl_inst
3267 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3268 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3269 return NULL;
3270
3271 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3272 }
3273
3274 /* Return true iff the function parameter PARAM_DECL was expanded
3275 from the function parameter pack PACK. */
3276
3277 bool
3278 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3279 {
3280 if (DECL_ARTIFICIAL (param_decl)
3281 || !function_parameter_pack_p (pack))
3282 return false;
3283
3284 /* The parameter pack and its pack arguments have the same
3285 DECL_PARM_INDEX. */
3286 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3287 }
3288
3289 /* Determine whether ARGS describes a variadic template args list,
3290 i.e., one that is terminated by a template argument pack. */
3291
3292 static bool
3293 template_args_variadic_p (tree args)
3294 {
3295 int nargs;
3296 tree last_parm;
3297
3298 if (args == NULL_TREE)
3299 return false;
3300
3301 args = INNERMOST_TEMPLATE_ARGS (args);
3302 nargs = TREE_VEC_LENGTH (args);
3303
3304 if (nargs == 0)
3305 return false;
3306
3307 last_parm = TREE_VEC_ELT (args, nargs - 1);
3308
3309 return ARGUMENT_PACK_P (last_parm);
3310 }
3311
3312 /* Generate a new name for the parameter pack name NAME (an
3313 IDENTIFIER_NODE) that incorporates its */
3314
3315 static tree
3316 make_ith_pack_parameter_name (tree name, int i)
3317 {
3318 /* Munge the name to include the parameter index. */
3319 #define NUMBUF_LEN 128
3320 char numbuf[NUMBUF_LEN];
3321 char* newname;
3322 int newname_len;
3323
3324 if (name == NULL_TREE)
3325 return name;
3326 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3327 newname_len = IDENTIFIER_LENGTH (name)
3328 + strlen (numbuf) + 2;
3329 newname = (char*)alloca (newname_len);
3330 snprintf (newname, newname_len,
3331 "%s#%i", IDENTIFIER_POINTER (name), i);
3332 return get_identifier (newname);
3333 }
3334
3335 /* Return true if T is a primary function, class or alias template
3336 instantiation. */
3337
3338 bool
3339 primary_template_instantiation_p (const_tree t)
3340 {
3341 if (!t)
3342 return false;
3343
3344 if (TREE_CODE (t) == FUNCTION_DECL)
3345 return DECL_LANG_SPECIFIC (t)
3346 && DECL_TEMPLATE_INSTANTIATION (t)
3347 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3348 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3349 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3350 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3351 else if (alias_template_specialization_p (t))
3352 return true;
3353 return false;
3354 }
3355
3356 /* Return true if PARM is a template template parameter. */
3357
3358 bool
3359 template_template_parameter_p (const_tree parm)
3360 {
3361 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3362 }
3363
3364 /* Return true iff PARM is a DECL representing a type template
3365 parameter. */
3366
3367 bool
3368 template_type_parameter_p (const_tree parm)
3369 {
3370 return (parm
3371 && (TREE_CODE (parm) == TYPE_DECL
3372 || TREE_CODE (parm) == TEMPLATE_DECL)
3373 && DECL_TEMPLATE_PARM_P (parm));
3374 }
3375
3376 /* Return the template parameters of T if T is a
3377 primary template instantiation, NULL otherwise. */
3378
3379 tree
3380 get_primary_template_innermost_parameters (const_tree t)
3381 {
3382 tree parms = NULL, template_info = NULL;
3383
3384 if ((template_info = get_template_info (t))
3385 && primary_template_instantiation_p (t))
3386 parms = INNERMOST_TEMPLATE_PARMS
3387 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3388
3389 return parms;
3390 }
3391
3392 /* Return the template parameters of the LEVELth level from the full list
3393 of template parameters PARMS. */
3394
3395 tree
3396 get_template_parms_at_level (tree parms, int level)
3397 {
3398 tree p;
3399 if (!parms
3400 || TREE_CODE (parms) != TREE_LIST
3401 || level > TMPL_PARMS_DEPTH (parms))
3402 return NULL_TREE;
3403
3404 for (p = parms; p; p = TREE_CHAIN (p))
3405 if (TMPL_PARMS_DEPTH (p) == level)
3406 return p;
3407
3408 return NULL_TREE;
3409 }
3410
3411 /* Returns the template arguments of T if T is a template instantiation,
3412 NULL otherwise. */
3413
3414 tree
3415 get_template_innermost_arguments (const_tree t)
3416 {
3417 tree args = NULL, template_info = NULL;
3418
3419 if ((template_info = get_template_info (t))
3420 && TI_ARGS (template_info))
3421 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3422
3423 return args;
3424 }
3425
3426 /* Return the argument pack elements of T if T is a template argument pack,
3427 NULL otherwise. */
3428
3429 tree
3430 get_template_argument_pack_elems (const_tree t)
3431 {
3432 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3433 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3434 return NULL;
3435
3436 return ARGUMENT_PACK_ARGS (t);
3437 }
3438
3439 /* Structure used to track the progress of find_parameter_packs_r. */
3440 struct find_parameter_pack_data
3441 {
3442 /* TREE_LIST that will contain all of the parameter packs found by
3443 the traversal. */
3444 tree* parameter_packs;
3445
3446 /* Set of AST nodes that have been visited by the traversal. */
3447 hash_set<tree> *visited;
3448
3449 /* True iff we're making a type pack expansion. */
3450 bool type_pack_expansion_p;
3451 };
3452
3453 /* Identifies all of the argument packs that occur in a template
3454 argument and appends them to the TREE_LIST inside DATA, which is a
3455 find_parameter_pack_data structure. This is a subroutine of
3456 make_pack_expansion and uses_parameter_packs. */
3457 static tree
3458 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3459 {
3460 tree t = *tp;
3461 struct find_parameter_pack_data* ppd =
3462 (struct find_parameter_pack_data*)data;
3463 bool parameter_pack_p = false;
3464
3465 /* Handle type aliases/typedefs. */
3466 if (TYPE_ALIAS_P (t))
3467 {
3468 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3469 cp_walk_tree (&TI_ARGS (tinfo),
3470 &find_parameter_packs_r,
3471 ppd, ppd->visited);
3472 *walk_subtrees = 0;
3473 return NULL_TREE;
3474 }
3475
3476 /* Identify whether this is a parameter pack or not. */
3477 switch (TREE_CODE (t))
3478 {
3479 case TEMPLATE_PARM_INDEX:
3480 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3481 parameter_pack_p = true;
3482 break;
3483
3484 case TEMPLATE_TYPE_PARM:
3485 t = TYPE_MAIN_VARIANT (t);
3486 /* FALLTHRU */
3487 case TEMPLATE_TEMPLATE_PARM:
3488 /* If the placeholder appears in the decl-specifier-seq of a function
3489 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3490 is a pack expansion, the invented template parameter is a template
3491 parameter pack. */
3492 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3493 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3494 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3495 parameter_pack_p = true;
3496 break;
3497
3498 case FIELD_DECL:
3499 case PARM_DECL:
3500 if (DECL_PACK_P (t))
3501 {
3502 /* We don't want to walk into the type of a PARM_DECL,
3503 because we don't want to see the type parameter pack. */
3504 *walk_subtrees = 0;
3505 parameter_pack_p = true;
3506 }
3507 break;
3508
3509 /* Look through a lambda capture proxy to the field pack. */
3510 case VAR_DECL:
3511 if (DECL_HAS_VALUE_EXPR_P (t))
3512 {
3513 tree v = DECL_VALUE_EXPR (t);
3514 cp_walk_tree (&v,
3515 &find_parameter_packs_r,
3516 ppd, ppd->visited);
3517 *walk_subtrees = 0;
3518 }
3519 else if (variable_template_specialization_p (t))
3520 {
3521 cp_walk_tree (&DECL_TI_ARGS (t),
3522 find_parameter_packs_r,
3523 ppd, ppd->visited);
3524 *walk_subtrees = 0;
3525 }
3526 break;
3527
3528 case BASES:
3529 parameter_pack_p = true;
3530 break;
3531 default:
3532 /* Not a parameter pack. */
3533 break;
3534 }
3535
3536 if (parameter_pack_p)
3537 {
3538 /* Add this parameter pack to the list. */
3539 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3540 }
3541
3542 if (TYPE_P (t))
3543 cp_walk_tree (&TYPE_CONTEXT (t),
3544 &find_parameter_packs_r, ppd, ppd->visited);
3545
3546 /* This switch statement will return immediately if we don't find a
3547 parameter pack. */
3548 switch (TREE_CODE (t))
3549 {
3550 case TEMPLATE_PARM_INDEX:
3551 return NULL_TREE;
3552
3553 case BOUND_TEMPLATE_TEMPLATE_PARM:
3554 /* Check the template itself. */
3555 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3556 &find_parameter_packs_r, ppd, ppd->visited);
3557 /* Check the template arguments. */
3558 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3559 ppd->visited);
3560 *walk_subtrees = 0;
3561 return NULL_TREE;
3562
3563 case TEMPLATE_TYPE_PARM:
3564 case TEMPLATE_TEMPLATE_PARM:
3565 return NULL_TREE;
3566
3567 case PARM_DECL:
3568 return NULL_TREE;
3569
3570 case RECORD_TYPE:
3571 if (TYPE_PTRMEMFUNC_P (t))
3572 return NULL_TREE;
3573 /* Fall through. */
3574
3575 case UNION_TYPE:
3576 case ENUMERAL_TYPE:
3577 if (TYPE_TEMPLATE_INFO (t))
3578 cp_walk_tree (&TYPE_TI_ARGS (t),
3579 &find_parameter_packs_r, ppd, ppd->visited);
3580
3581 *walk_subtrees = 0;
3582 return NULL_TREE;
3583
3584 case TEMPLATE_DECL:
3585 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3586 return NULL_TREE;
3587 gcc_fallthrough();
3588
3589 case CONSTRUCTOR:
3590 cp_walk_tree (&TREE_TYPE (t),
3591 &find_parameter_packs_r, ppd, ppd->visited);
3592 return NULL_TREE;
3593
3594 case TYPENAME_TYPE:
3595 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3596 ppd, ppd->visited);
3597 *walk_subtrees = 0;
3598 return NULL_TREE;
3599
3600 case TYPE_PACK_EXPANSION:
3601 case EXPR_PACK_EXPANSION:
3602 *walk_subtrees = 0;
3603 return NULL_TREE;
3604
3605 case INTEGER_TYPE:
3606 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3607 ppd, ppd->visited);
3608 *walk_subtrees = 0;
3609 return NULL_TREE;
3610
3611 case IDENTIFIER_NODE:
3612 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3613 ppd->visited);
3614 *walk_subtrees = 0;
3615 return NULL_TREE;
3616
3617 case DECLTYPE_TYPE:
3618 {
3619 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3620 type_pack_expansion_p to false so that any placeholders
3621 within the expression don't get marked as parameter packs. */
3622 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3623 ppd->type_pack_expansion_p = false;
3624 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3625 ppd, ppd->visited);
3626 ppd->type_pack_expansion_p = type_pack_expansion_p;
3627 *walk_subtrees = 0;
3628 return NULL_TREE;
3629 }
3630
3631 default:
3632 return NULL_TREE;
3633 }
3634
3635 return NULL_TREE;
3636 }
3637
3638 /* Determines if the expression or type T uses any parameter packs. */
3639 bool
3640 uses_parameter_packs (tree t)
3641 {
3642 tree parameter_packs = NULL_TREE;
3643 struct find_parameter_pack_data ppd;
3644 ppd.parameter_packs = &parameter_packs;
3645 ppd.visited = new hash_set<tree>;
3646 ppd.type_pack_expansion_p = false;
3647 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3648 delete ppd.visited;
3649 return parameter_packs != NULL_TREE;
3650 }
3651
3652 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3653 representation a base-class initializer into a parameter pack
3654 expansion. If all goes well, the resulting node will be an
3655 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3656 respectively. */
3657 tree
3658 make_pack_expansion (tree arg)
3659 {
3660 tree result;
3661 tree parameter_packs = NULL_TREE;
3662 bool for_types = false;
3663 struct find_parameter_pack_data ppd;
3664
3665 if (!arg || arg == error_mark_node)
3666 return arg;
3667
3668 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3669 {
3670 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3671 class initializer. In this case, the TREE_PURPOSE will be a
3672 _TYPE node (representing the base class expansion we're
3673 initializing) and the TREE_VALUE will be a TREE_LIST
3674 containing the initialization arguments.
3675
3676 The resulting expansion looks somewhat different from most
3677 expansions. Rather than returning just one _EXPANSION, we
3678 return a TREE_LIST whose TREE_PURPOSE is a
3679 TYPE_PACK_EXPANSION containing the bases that will be
3680 initialized. The TREE_VALUE will be identical to the
3681 original TREE_VALUE, which is a list of arguments that will
3682 be passed to each base. We do not introduce any new pack
3683 expansion nodes into the TREE_VALUE (although it is possible
3684 that some already exist), because the TREE_PURPOSE and
3685 TREE_VALUE all need to be expanded together with the same
3686 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3687 resulting TREE_PURPOSE will mention the parameter packs in
3688 both the bases and the arguments to the bases. */
3689 tree purpose;
3690 tree value;
3691 tree parameter_packs = NULL_TREE;
3692
3693 /* Determine which parameter packs will be used by the base
3694 class expansion. */
3695 ppd.visited = new hash_set<tree>;
3696 ppd.parameter_packs = &parameter_packs;
3697 ppd.type_pack_expansion_p = true;
3698 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3699 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3700 &ppd, ppd.visited);
3701
3702 if (parameter_packs == NULL_TREE)
3703 {
3704 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3705 delete ppd.visited;
3706 return error_mark_node;
3707 }
3708
3709 if (TREE_VALUE (arg) != void_type_node)
3710 {
3711 /* Collect the sets of parameter packs used in each of the
3712 initialization arguments. */
3713 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3714 {
3715 /* Determine which parameter packs will be expanded in this
3716 argument. */
3717 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3718 &ppd, ppd.visited);
3719 }
3720 }
3721
3722 delete ppd.visited;
3723
3724 /* Create the pack expansion type for the base type. */
3725 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3726 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3727 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3728
3729 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3730 they will rarely be compared to anything. */
3731 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3732
3733 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3734 }
3735
3736 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3737 for_types = true;
3738
3739 /* Build the PACK_EXPANSION_* node. */
3740 result = for_types
3741 ? cxx_make_type (TYPE_PACK_EXPANSION)
3742 : make_node (EXPR_PACK_EXPANSION);
3743 SET_PACK_EXPANSION_PATTERN (result, arg);
3744 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3745 {
3746 /* Propagate type and const-expression information. */
3747 TREE_TYPE (result) = TREE_TYPE (arg);
3748 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3749 /* Mark this read now, since the expansion might be length 0. */
3750 mark_exp_read (arg);
3751 }
3752 else
3753 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3754 they will rarely be compared to anything. */
3755 SET_TYPE_STRUCTURAL_EQUALITY (result);
3756
3757 /* Determine which parameter packs will be expanded. */
3758 ppd.parameter_packs = &parameter_packs;
3759 ppd.visited = new hash_set<tree>;
3760 ppd.type_pack_expansion_p = TYPE_P (arg);
3761 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3762 delete ppd.visited;
3763
3764 /* Make sure we found some parameter packs. */
3765 if (parameter_packs == NULL_TREE)
3766 {
3767 if (TYPE_P (arg))
3768 error ("expansion pattern %<%T%> contains no argument packs", arg);
3769 else
3770 error ("expansion pattern %<%E%> contains no argument packs", arg);
3771 return error_mark_node;
3772 }
3773 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3774
3775 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3776
3777 return result;
3778 }
3779
3780 /* Checks T for any "bare" parameter packs, which have not yet been
3781 expanded, and issues an error if any are found. This operation can
3782 only be done on full expressions or types (e.g., an expression
3783 statement, "if" condition, etc.), because we could have expressions like:
3784
3785 foo(f(g(h(args)))...)
3786
3787 where "args" is a parameter pack. check_for_bare_parameter_packs
3788 should not be called for the subexpressions args, h(args),
3789 g(h(args)), or f(g(h(args))), because we would produce erroneous
3790 error messages.
3791
3792 Returns TRUE and emits an error if there were bare parameter packs,
3793 returns FALSE otherwise. */
3794 bool
3795 check_for_bare_parameter_packs (tree t)
3796 {
3797 tree parameter_packs = NULL_TREE;
3798 struct find_parameter_pack_data ppd;
3799
3800 if (!processing_template_decl || !t || t == error_mark_node)
3801 return false;
3802
3803 if (TREE_CODE (t) == TYPE_DECL)
3804 t = TREE_TYPE (t);
3805
3806 ppd.parameter_packs = &parameter_packs;
3807 ppd.visited = new hash_set<tree>;
3808 ppd.type_pack_expansion_p = false;
3809 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3810 delete ppd.visited;
3811
3812 if (parameter_packs)
3813 {
3814 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3815 error_at (loc, "parameter packs not expanded with %<...%>:");
3816 while (parameter_packs)
3817 {
3818 tree pack = TREE_VALUE (parameter_packs);
3819 tree name = NULL_TREE;
3820
3821 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3822 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3823 name = TYPE_NAME (pack);
3824 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3825 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3826 else
3827 name = DECL_NAME (pack);
3828
3829 if (name)
3830 inform (loc, " %qD", name);
3831 else
3832 inform (loc, " <anonymous>");
3833
3834 parameter_packs = TREE_CHAIN (parameter_packs);
3835 }
3836
3837 return true;
3838 }
3839
3840 return false;
3841 }
3842
3843 /* Expand any parameter packs that occur in the template arguments in
3844 ARGS. */
3845 tree
3846 expand_template_argument_pack (tree args)
3847 {
3848 if (args == error_mark_node)
3849 return error_mark_node;
3850
3851 tree result_args = NULL_TREE;
3852 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3853 int num_result_args = -1;
3854 int non_default_args_count = -1;
3855
3856 /* First, determine if we need to expand anything, and the number of
3857 slots we'll need. */
3858 for (in_arg = 0; in_arg < nargs; ++in_arg)
3859 {
3860 tree arg = TREE_VEC_ELT (args, in_arg);
3861 if (arg == NULL_TREE)
3862 return args;
3863 if (ARGUMENT_PACK_P (arg))
3864 {
3865 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3866 if (num_result_args < 0)
3867 num_result_args = in_arg + num_packed;
3868 else
3869 num_result_args += num_packed;
3870 }
3871 else
3872 {
3873 if (num_result_args >= 0)
3874 num_result_args++;
3875 }
3876 }
3877
3878 /* If no expansion is necessary, we're done. */
3879 if (num_result_args < 0)
3880 return args;
3881
3882 /* Expand arguments. */
3883 result_args = make_tree_vec (num_result_args);
3884 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3885 non_default_args_count =
3886 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3887 for (in_arg = 0; in_arg < nargs; ++in_arg)
3888 {
3889 tree arg = TREE_VEC_ELT (args, in_arg);
3890 if (ARGUMENT_PACK_P (arg))
3891 {
3892 tree packed = ARGUMENT_PACK_ARGS (arg);
3893 int i, num_packed = TREE_VEC_LENGTH (packed);
3894 for (i = 0; i < num_packed; ++i, ++out_arg)
3895 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3896 if (non_default_args_count > 0)
3897 non_default_args_count += num_packed - 1;
3898 }
3899 else
3900 {
3901 TREE_VEC_ELT (result_args, out_arg) = arg;
3902 ++out_arg;
3903 }
3904 }
3905 if (non_default_args_count >= 0)
3906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3907 return result_args;
3908 }
3909
3910 /* Checks if DECL shadows a template parameter.
3911
3912 [temp.local]: A template-parameter shall not be redeclared within its
3913 scope (including nested scopes).
3914
3915 Emits an error and returns TRUE if the DECL shadows a parameter,
3916 returns FALSE otherwise. */
3917
3918 bool
3919 check_template_shadow (tree decl)
3920 {
3921 tree olddecl;
3922
3923 /* If we're not in a template, we can't possibly shadow a template
3924 parameter. */
3925 if (!current_template_parms)
3926 return true;
3927
3928 /* Figure out what we're shadowing. */
3929 if (TREE_CODE (decl) == OVERLOAD)
3930 decl = OVL_CURRENT (decl);
3931 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3932
3933 /* If there's no previous binding for this name, we're not shadowing
3934 anything, let alone a template parameter. */
3935 if (!olddecl)
3936 return true;
3937
3938 /* If we're not shadowing a template parameter, we're done. Note
3939 that OLDDECL might be an OVERLOAD (or perhaps even an
3940 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3941 node. */
3942 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3943 return true;
3944
3945 /* We check for decl != olddecl to avoid bogus errors for using a
3946 name inside a class. We check TPFI to avoid duplicate errors for
3947 inline member templates. */
3948 if (decl == olddecl
3949 || (DECL_TEMPLATE_PARM_P (decl)
3950 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3951 return true;
3952
3953 /* Don't complain about the injected class name, as we've already
3954 complained about the class itself. */
3955 if (DECL_SELF_REFERENCE_P (decl))
3956 return false;
3957
3958 if (DECL_TEMPLATE_PARM_P (decl))
3959 error ("declaration of template parameter %q+D shadows "
3960 "template parameter", decl);
3961 else
3962 error ("declaration of %q+#D shadows template parameter", decl);
3963 inform (DECL_SOURCE_LOCATION (olddecl),
3964 "template parameter %qD declared here", olddecl);
3965 return false;
3966 }
3967
3968 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3969 ORIG_LEVEL, DECL, and TYPE. */
3970
3971 static tree
3972 build_template_parm_index (int index,
3973 int level,
3974 int orig_level,
3975 tree decl,
3976 tree type)
3977 {
3978 tree t = make_node (TEMPLATE_PARM_INDEX);
3979 TEMPLATE_PARM_IDX (t) = index;
3980 TEMPLATE_PARM_LEVEL (t) = level;
3981 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3982 TEMPLATE_PARM_DECL (t) = decl;
3983 TREE_TYPE (t) = type;
3984 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3985 TREE_READONLY (t) = TREE_READONLY (decl);
3986
3987 return t;
3988 }
3989
3990 /* Find the canonical type parameter for the given template type
3991 parameter. Returns the canonical type parameter, which may be TYPE
3992 if no such parameter existed. */
3993
3994 static tree
3995 canonical_type_parameter (tree type)
3996 {
3997 tree list;
3998 int idx = TEMPLATE_TYPE_IDX (type);
3999 if (!canonical_template_parms)
4000 vec_alloc (canonical_template_parms, idx+1);
4001
4002 while (canonical_template_parms->length () <= (unsigned)idx)
4003 vec_safe_push (canonical_template_parms, NULL_TREE);
4004
4005 list = (*canonical_template_parms)[idx];
4006 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4007 list = TREE_CHAIN (list);
4008
4009 if (list)
4010 return TREE_VALUE (list);
4011 else
4012 {
4013 (*canonical_template_parms)[idx]
4014 = tree_cons (NULL_TREE, type,
4015 (*canonical_template_parms)[idx]);
4016 return type;
4017 }
4018 }
4019
4020 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4021 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4022 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4023 new one is created. */
4024
4025 static tree
4026 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4027 tsubst_flags_t complain)
4028 {
4029 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4030 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4031 != TEMPLATE_PARM_LEVEL (index) - levels)
4032 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4033 {
4034 tree orig_decl = TEMPLATE_PARM_DECL (index);
4035 tree decl, t;
4036
4037 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4038 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4039 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4040 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4041 DECL_ARTIFICIAL (decl) = 1;
4042 SET_DECL_TEMPLATE_PARM_P (decl);
4043
4044 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4045 TEMPLATE_PARM_LEVEL (index) - levels,
4046 TEMPLATE_PARM_ORIG_LEVEL (index),
4047 decl, type);
4048 TEMPLATE_PARM_DESCENDANTS (index) = t;
4049 TEMPLATE_PARM_PARAMETER_PACK (t)
4050 = TEMPLATE_PARM_PARAMETER_PACK (index);
4051
4052 /* Template template parameters need this. */
4053 if (TREE_CODE (decl) == TEMPLATE_DECL)
4054 {
4055 DECL_TEMPLATE_RESULT (decl)
4056 = build_decl (DECL_SOURCE_LOCATION (decl),
4057 TYPE_DECL, DECL_NAME (decl), type);
4058 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4059 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4060 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4061 }
4062 }
4063
4064 return TEMPLATE_PARM_DESCENDANTS (index);
4065 }
4066
4067 /* Process information from new template parameter PARM and append it
4068 to the LIST being built. This new parameter is a non-type
4069 parameter iff IS_NON_TYPE is true. This new parameter is a
4070 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4071 is in PARM_LOC. */
4072
4073 tree
4074 process_template_parm (tree list, location_t parm_loc, tree parm,
4075 bool is_non_type, bool is_parameter_pack)
4076 {
4077 tree decl = 0;
4078 int idx = 0;
4079
4080 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4081 tree defval = TREE_PURPOSE (parm);
4082 tree constr = TREE_TYPE (parm);
4083
4084 if (list)
4085 {
4086 tree p = tree_last (list);
4087
4088 if (p && TREE_VALUE (p) != error_mark_node)
4089 {
4090 p = TREE_VALUE (p);
4091 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4092 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4093 else
4094 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4095 }
4096
4097 ++idx;
4098 }
4099
4100 if (is_non_type)
4101 {
4102 parm = TREE_VALUE (parm);
4103
4104 SET_DECL_TEMPLATE_PARM_P (parm);
4105
4106 if (TREE_TYPE (parm) != error_mark_node)
4107 {
4108 /* [temp.param]
4109
4110 The top-level cv-qualifiers on the template-parameter are
4111 ignored when determining its type. */
4112 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4113 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4114 TREE_TYPE (parm) = error_mark_node;
4115 else if (uses_parameter_packs (TREE_TYPE (parm))
4116 && !is_parameter_pack
4117 /* If we're in a nested template parameter list, the template
4118 template parameter could be a parameter pack. */
4119 && processing_template_parmlist == 1)
4120 {
4121 /* This template parameter is not a parameter pack, but it
4122 should be. Complain about "bare" parameter packs. */
4123 check_for_bare_parameter_packs (TREE_TYPE (parm));
4124
4125 /* Recover by calling this a parameter pack. */
4126 is_parameter_pack = true;
4127 }
4128 }
4129
4130 /* A template parameter is not modifiable. */
4131 TREE_CONSTANT (parm) = 1;
4132 TREE_READONLY (parm) = 1;
4133 decl = build_decl (parm_loc,
4134 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4135 TREE_CONSTANT (decl) = 1;
4136 TREE_READONLY (decl) = 1;
4137 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4138 = build_template_parm_index (idx, processing_template_decl,
4139 processing_template_decl,
4140 decl, TREE_TYPE (parm));
4141
4142 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4143 = is_parameter_pack;
4144 }
4145 else
4146 {
4147 tree t;
4148 parm = TREE_VALUE (TREE_VALUE (parm));
4149
4150 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4151 {
4152 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4153 /* This is for distinguishing between real templates and template
4154 template parameters */
4155 TREE_TYPE (parm) = t;
4156 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4157 decl = parm;
4158 }
4159 else
4160 {
4161 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4162 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4163 decl = build_decl (parm_loc,
4164 TYPE_DECL, parm, t);
4165 }
4166
4167 TYPE_NAME (t) = decl;
4168 TYPE_STUB_DECL (t) = decl;
4169 parm = decl;
4170 TEMPLATE_TYPE_PARM_INDEX (t)
4171 = build_template_parm_index (idx, processing_template_decl,
4172 processing_template_decl,
4173 decl, TREE_TYPE (parm));
4174 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4175 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4176 }
4177 DECL_ARTIFICIAL (decl) = 1;
4178 SET_DECL_TEMPLATE_PARM_P (decl);
4179
4180 /* Build requirements for the type/template parameter.
4181 This must be done after SET_DECL_TEMPLATE_PARM_P or
4182 process_template_parm could fail. */
4183 tree reqs = finish_shorthand_constraint (parm, constr);
4184
4185 pushdecl (decl);
4186
4187 /* Build the parameter node linking the parameter declaration,
4188 its default argument (if any), and its constraints (if any). */
4189 parm = build_tree_list (defval, parm);
4190 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4191
4192 return chainon (list, parm);
4193 }
4194
4195 /* The end of a template parameter list has been reached. Process the
4196 tree list into a parameter vector, converting each parameter into a more
4197 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4198 as PARM_DECLs. */
4199
4200 tree
4201 end_template_parm_list (tree parms)
4202 {
4203 int nparms;
4204 tree parm, next;
4205 tree saved_parmlist = make_tree_vec (list_length (parms));
4206
4207 /* Pop the dummy parameter level and add the real one. */
4208 current_template_parms = TREE_CHAIN (current_template_parms);
4209
4210 current_template_parms
4211 = tree_cons (size_int (processing_template_decl),
4212 saved_parmlist, current_template_parms);
4213
4214 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4215 {
4216 next = TREE_CHAIN (parm);
4217 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4218 TREE_CHAIN (parm) = NULL_TREE;
4219 }
4220
4221 --processing_template_parmlist;
4222
4223 return saved_parmlist;
4224 }
4225
4226 // Explicitly indicate the end of the template parameter list. We assume
4227 // that the current template parameters have been constructed and/or
4228 // managed explicitly, as when creating new template template parameters
4229 // from a shorthand constraint.
4230 void
4231 end_template_parm_list ()
4232 {
4233 --processing_template_parmlist;
4234 }
4235
4236 /* end_template_decl is called after a template declaration is seen. */
4237
4238 void
4239 end_template_decl (void)
4240 {
4241 reset_specialization ();
4242
4243 if (! processing_template_decl)
4244 return;
4245
4246 /* This matches the pushlevel in begin_template_parm_list. */
4247 finish_scope ();
4248
4249 --processing_template_decl;
4250 current_template_parms = TREE_CHAIN (current_template_parms);
4251 }
4252
4253 /* Takes a TREE_LIST representing a template parameter and convert it
4254 into an argument suitable to be passed to the type substitution
4255 functions. Note that If the TREE_LIST contains an error_mark
4256 node, the returned argument is error_mark_node. */
4257
4258 tree
4259 template_parm_to_arg (tree t)
4260 {
4261
4262 if (t == NULL_TREE
4263 || TREE_CODE (t) != TREE_LIST)
4264 return t;
4265
4266 if (error_operand_p (TREE_VALUE (t)))
4267 return error_mark_node;
4268
4269 t = TREE_VALUE (t);
4270
4271 if (TREE_CODE (t) == TYPE_DECL
4272 || TREE_CODE (t) == TEMPLATE_DECL)
4273 {
4274 t = TREE_TYPE (t);
4275
4276 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4277 {
4278 /* Turn this argument into a TYPE_ARGUMENT_PACK
4279 with a single element, which expands T. */
4280 tree vec = make_tree_vec (1);
4281 if (CHECKING_P)
4282 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4283
4284 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4285
4286 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4287 SET_ARGUMENT_PACK_ARGS (t, vec);
4288 }
4289 }
4290 else
4291 {
4292 t = DECL_INITIAL (t);
4293
4294 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4295 {
4296 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4297 with a single element, which expands T. */
4298 tree vec = make_tree_vec (1);
4299 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4300 if (CHECKING_P)
4301 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4302
4303 t = convert_from_reference (t);
4304 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4305
4306 t = make_node (NONTYPE_ARGUMENT_PACK);
4307 SET_ARGUMENT_PACK_ARGS (t, vec);
4308 TREE_TYPE (t) = type;
4309 }
4310 else
4311 t = convert_from_reference (t);
4312 }
4313 return t;
4314 }
4315
4316 /* Given a single level of template parameters (a TREE_VEC), return it
4317 as a set of template arguments. */
4318
4319 static tree
4320 template_parms_level_to_args (tree parms)
4321 {
4322 tree a = copy_node (parms);
4323 TREE_TYPE (a) = NULL_TREE;
4324 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4325 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4326
4327 if (CHECKING_P)
4328 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4329
4330 return a;
4331 }
4332
4333 /* Given a set of template parameters, return them as a set of template
4334 arguments. The template parameters are represented as a TREE_VEC, in
4335 the form documented in cp-tree.h for template arguments. */
4336
4337 static tree
4338 template_parms_to_args (tree parms)
4339 {
4340 tree header;
4341 tree args = NULL_TREE;
4342 int length = TMPL_PARMS_DEPTH (parms);
4343 int l = length;
4344
4345 /* If there is only one level of template parameters, we do not
4346 create a TREE_VEC of TREE_VECs. Instead, we return a single
4347 TREE_VEC containing the arguments. */
4348 if (length > 1)
4349 args = make_tree_vec (length);
4350
4351 for (header = parms; header; header = TREE_CHAIN (header))
4352 {
4353 tree a = template_parms_level_to_args (TREE_VALUE (header));
4354
4355 if (length > 1)
4356 TREE_VEC_ELT (args, --l) = a;
4357 else
4358 args = a;
4359 }
4360
4361 return args;
4362 }
4363
4364 /* Within the declaration of a template, return the currently active
4365 template parameters as an argument TREE_VEC. */
4366
4367 static tree
4368 current_template_args (void)
4369 {
4370 return template_parms_to_args (current_template_parms);
4371 }
4372
4373 /* Update the declared TYPE by doing any lookups which were thought to be
4374 dependent, but are not now that we know the SCOPE of the declarator. */
4375
4376 tree
4377 maybe_update_decl_type (tree orig_type, tree scope)
4378 {
4379 tree type = orig_type;
4380
4381 if (type == NULL_TREE)
4382 return type;
4383
4384 if (TREE_CODE (orig_type) == TYPE_DECL)
4385 type = TREE_TYPE (type);
4386
4387 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4388 && dependent_type_p (type)
4389 /* Don't bother building up the args in this case. */
4390 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4391 {
4392 /* tsubst in the args corresponding to the template parameters,
4393 including auto if present. Most things will be unchanged, but
4394 make_typename_type and tsubst_qualified_id will resolve
4395 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4396 tree args = current_template_args ();
4397 tree auto_node = type_uses_auto (type);
4398 tree pushed;
4399 if (auto_node)
4400 {
4401 tree auto_vec = make_tree_vec (1);
4402 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4403 args = add_to_template_args (args, auto_vec);
4404 }
4405 pushed = push_scope (scope);
4406 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4407 if (pushed)
4408 pop_scope (scope);
4409 }
4410
4411 if (type == error_mark_node)
4412 return orig_type;
4413
4414 if (TREE_CODE (orig_type) == TYPE_DECL)
4415 {
4416 if (same_type_p (type, TREE_TYPE (orig_type)))
4417 type = orig_type;
4418 else
4419 type = TYPE_NAME (type);
4420 }
4421 return type;
4422 }
4423
4424 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4425 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4426 the new template is a member template. */
4427
4428 tree
4429 build_template_decl (tree decl, tree parms, bool member_template_p)
4430 {
4431 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4432 DECL_TEMPLATE_PARMS (tmpl) = parms;
4433 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4434 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4435 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4436
4437 return tmpl;
4438 }
4439
4440 struct template_parm_data
4441 {
4442 /* The level of the template parameters we are currently
4443 processing. */
4444 int level;
4445
4446 /* The index of the specialization argument we are currently
4447 processing. */
4448 int current_arg;
4449
4450 /* An array whose size is the number of template parameters. The
4451 elements are nonzero if the parameter has been used in any one
4452 of the arguments processed so far. */
4453 int* parms;
4454
4455 /* An array whose size is the number of template arguments. The
4456 elements are nonzero if the argument makes use of template
4457 parameters of this level. */
4458 int* arg_uses_template_parms;
4459 };
4460
4461 /* Subroutine of push_template_decl used to see if each template
4462 parameter in a partial specialization is used in the explicit
4463 argument list. If T is of the LEVEL given in DATA (which is
4464 treated as a template_parm_data*), then DATA->PARMS is marked
4465 appropriately. */
4466
4467 static int
4468 mark_template_parm (tree t, void* data)
4469 {
4470 int level;
4471 int idx;
4472 struct template_parm_data* tpd = (struct template_parm_data*) data;
4473
4474 template_parm_level_and_index (t, &level, &idx);
4475
4476 if (level == tpd->level)
4477 {
4478 tpd->parms[idx] = 1;
4479 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4480 }
4481
4482 /* In C++17 the type of a non-type argument is a deduced context. */
4483 if (cxx_dialect >= cxx1z
4484 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4485 for_each_template_parm (TREE_TYPE (t),
4486 &mark_template_parm,
4487 data,
4488 NULL,
4489 /*include_nondeduced_p=*/false);
4490
4491 /* Return zero so that for_each_template_parm will continue the
4492 traversal of the tree; we want to mark *every* template parm. */
4493 return 0;
4494 }
4495
4496 /* Process the partial specialization DECL. */
4497
4498 static tree
4499 process_partial_specialization (tree decl)
4500 {
4501 tree type = TREE_TYPE (decl);
4502 tree tinfo = get_template_info (decl);
4503 tree maintmpl = TI_TEMPLATE (tinfo);
4504 tree specargs = TI_ARGS (tinfo);
4505 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4506 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4507 tree inner_parms;
4508 tree inst;
4509 int nargs = TREE_VEC_LENGTH (inner_args);
4510 int ntparms;
4511 int i;
4512 bool did_error_intro = false;
4513 struct template_parm_data tpd;
4514 struct template_parm_data tpd2;
4515
4516 gcc_assert (current_template_parms);
4517
4518 /* A concept cannot be specialized. */
4519 if (flag_concepts && variable_concept_p (maintmpl))
4520 {
4521 error ("specialization of variable concept %q#D", maintmpl);
4522 return error_mark_node;
4523 }
4524
4525 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4526 ntparms = TREE_VEC_LENGTH (inner_parms);
4527
4528 /* We check that each of the template parameters given in the
4529 partial specialization is used in the argument list to the
4530 specialization. For example:
4531
4532 template <class T> struct S;
4533 template <class T> struct S<T*>;
4534
4535 The second declaration is OK because `T*' uses the template
4536 parameter T, whereas
4537
4538 template <class T> struct S<int>;
4539
4540 is no good. Even trickier is:
4541
4542 template <class T>
4543 struct S1
4544 {
4545 template <class U>
4546 struct S2;
4547 template <class U>
4548 struct S2<T>;
4549 };
4550
4551 The S2<T> declaration is actually invalid; it is a
4552 full-specialization. Of course,
4553
4554 template <class U>
4555 struct S2<T (*)(U)>;
4556
4557 or some such would have been OK. */
4558 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4559 tpd.parms = XALLOCAVEC (int, ntparms);
4560 memset (tpd.parms, 0, sizeof (int) * ntparms);
4561
4562 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4563 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4564 for (i = 0; i < nargs; ++i)
4565 {
4566 tpd.current_arg = i;
4567 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4568 &mark_template_parm,
4569 &tpd,
4570 NULL,
4571 /*include_nondeduced_p=*/false);
4572 }
4573 for (i = 0; i < ntparms; ++i)
4574 if (tpd.parms[i] == 0)
4575 {
4576 /* One of the template parms was not used in a deduced context in the
4577 specialization. */
4578 if (!did_error_intro)
4579 {
4580 error ("template parameters not deducible in "
4581 "partial specialization:");
4582 did_error_intro = true;
4583 }
4584
4585 inform (input_location, " %qD",
4586 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4587 }
4588
4589 if (did_error_intro)
4590 return error_mark_node;
4591
4592 /* [temp.class.spec]
4593
4594 The argument list of the specialization shall not be identical to
4595 the implicit argument list of the primary template. */
4596 tree main_args
4597 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4598 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4599 && (!flag_concepts
4600 || !strictly_subsumes (current_template_constraints (),
4601 get_constraints (maintmpl))))
4602 {
4603 if (!flag_concepts)
4604 error ("partial specialization %q+D does not specialize "
4605 "any template arguments", decl);
4606 else
4607 error ("partial specialization %q+D does not specialize any "
4608 "template arguments and is not more constrained than", decl);
4609 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4610 }
4611
4612 /* A partial specialization that replaces multiple parameters of the
4613 primary template with a pack expansion is less specialized for those
4614 parameters. */
4615 if (nargs < DECL_NTPARMS (maintmpl))
4616 {
4617 error ("partial specialization is not more specialized than the "
4618 "primary template because it replaces multiple parameters "
4619 "with a pack expansion");
4620 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4621 /* Avoid crash in process_partial_specialization. */
4622 return decl;
4623 }
4624
4625 /* If we aren't in a dependent class, we can actually try deduction. */
4626 else if (tpd.level == 1
4627 /* FIXME we should be able to handle a partial specialization of a
4628 partial instantiation, but currently we can't (c++/41727). */
4629 && TMPL_ARGS_DEPTH (specargs) == 1
4630 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4631 {
4632 if (permerror (input_location, "partial specialization %qD is not "
4633 "more specialized than", decl))
4634 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4635 maintmpl);
4636 }
4637
4638 /* [temp.class.spec]
4639
4640 A partially specialized non-type argument expression shall not
4641 involve template parameters of the partial specialization except
4642 when the argument expression is a simple identifier.
4643
4644 The type of a template parameter corresponding to a specialized
4645 non-type argument shall not be dependent on a parameter of the
4646 specialization.
4647
4648 Also, we verify that pack expansions only occur at the
4649 end of the argument list. */
4650 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4651 tpd2.parms = 0;
4652 for (i = 0; i < nargs; ++i)
4653 {
4654 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4655 tree arg = TREE_VEC_ELT (inner_args, i);
4656 tree packed_args = NULL_TREE;
4657 int j, len = 1;
4658
4659 if (ARGUMENT_PACK_P (arg))
4660 {
4661 /* Extract the arguments from the argument pack. We'll be
4662 iterating over these in the following loop. */
4663 packed_args = ARGUMENT_PACK_ARGS (arg);
4664 len = TREE_VEC_LENGTH (packed_args);
4665 }
4666
4667 for (j = 0; j < len; j++)
4668 {
4669 if (packed_args)
4670 /* Get the Jth argument in the parameter pack. */
4671 arg = TREE_VEC_ELT (packed_args, j);
4672
4673 if (PACK_EXPANSION_P (arg))
4674 {
4675 /* Pack expansions must come at the end of the
4676 argument list. */
4677 if ((packed_args && j < len - 1)
4678 || (!packed_args && i < nargs - 1))
4679 {
4680 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4681 error ("parameter pack argument %qE must be at the "
4682 "end of the template argument list", arg);
4683 else
4684 error ("parameter pack argument %qT must be at the "
4685 "end of the template argument list", arg);
4686 }
4687 }
4688
4689 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4690 /* We only care about the pattern. */
4691 arg = PACK_EXPANSION_PATTERN (arg);
4692
4693 if (/* These first two lines are the `non-type' bit. */
4694 !TYPE_P (arg)
4695 && TREE_CODE (arg) != TEMPLATE_DECL
4696 /* This next two lines are the `argument expression is not just a
4697 simple identifier' condition and also the `specialized
4698 non-type argument' bit. */
4699 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4700 && !(REFERENCE_REF_P (arg)
4701 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4702 {
4703 if ((!packed_args && tpd.arg_uses_template_parms[i])
4704 || (packed_args && uses_template_parms (arg)))
4705 error ("template argument %qE involves template parameter(s)",
4706 arg);
4707 else
4708 {
4709 /* Look at the corresponding template parameter,
4710 marking which template parameters its type depends
4711 upon. */
4712 tree type = TREE_TYPE (parm);
4713
4714 if (!tpd2.parms)
4715 {
4716 /* We haven't yet initialized TPD2. Do so now. */
4717 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4718 /* The number of parameters here is the number in the
4719 main template, which, as checked in the assertion
4720 above, is NARGS. */
4721 tpd2.parms = XALLOCAVEC (int, nargs);
4722 tpd2.level =
4723 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4724 }
4725
4726 /* Mark the template parameters. But this time, we're
4727 looking for the template parameters of the main
4728 template, not in the specialization. */
4729 tpd2.current_arg = i;
4730 tpd2.arg_uses_template_parms[i] = 0;
4731 memset (tpd2.parms, 0, sizeof (int) * nargs);
4732 for_each_template_parm (type,
4733 &mark_template_parm,
4734 &tpd2,
4735 NULL,
4736 /*include_nondeduced_p=*/false);
4737
4738 if (tpd2.arg_uses_template_parms [i])
4739 {
4740 /* The type depended on some template parameters.
4741 If they are fully specialized in the
4742 specialization, that's OK. */
4743 int j;
4744 int count = 0;
4745 for (j = 0; j < nargs; ++j)
4746 if (tpd2.parms[j] != 0
4747 && tpd.arg_uses_template_parms [j])
4748 ++count;
4749 if (count != 0)
4750 error_n (input_location, count,
4751 "type %qT of template argument %qE depends "
4752 "on a template parameter",
4753 "type %qT of template argument %qE depends "
4754 "on template parameters",
4755 type,
4756 arg);
4757 }
4758 }
4759 }
4760 }
4761 }
4762
4763 /* We should only get here once. */
4764 if (TREE_CODE (decl) == TYPE_DECL)
4765 gcc_assert (!COMPLETE_TYPE_P (type));
4766
4767 // Build the template decl.
4768 tree tmpl = build_template_decl (decl, current_template_parms,
4769 DECL_MEMBER_TEMPLATE_P (maintmpl));
4770 TREE_TYPE (tmpl) = type;
4771 DECL_TEMPLATE_RESULT (tmpl) = decl;
4772 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4773 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4774 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4775
4776 /* Give template template parms a DECL_CONTEXT of the template
4777 for which they are a parameter. */
4778 for (i = 0; i < ntparms; ++i)
4779 {
4780 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4781 if (TREE_CODE (parm) == TEMPLATE_DECL)
4782 DECL_CONTEXT (parm) = tmpl;
4783 }
4784
4785 if (VAR_P (decl))
4786 /* We didn't register this in check_explicit_specialization so we could
4787 wait until the constraints were set. */
4788 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4789 else
4790 associate_classtype_constraints (type);
4791
4792 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4793 = tree_cons (specargs, tmpl,
4794 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4795 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4796
4797 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4798 inst = TREE_CHAIN (inst))
4799 {
4800 tree instance = TREE_VALUE (inst);
4801 if (TYPE_P (instance)
4802 ? (COMPLETE_TYPE_P (instance)
4803 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4804 : DECL_TEMPLATE_INSTANTIATION (instance))
4805 {
4806 tree spec = most_specialized_partial_spec (instance, tf_none);
4807 tree inst_decl = (DECL_P (instance)
4808 ? instance : TYPE_NAME (instance));
4809 if (!spec)
4810 /* OK */;
4811 else if (spec == error_mark_node)
4812 permerror (input_location,
4813 "declaration of %qD ambiguates earlier template "
4814 "instantiation for %qD", decl, inst_decl);
4815 else if (TREE_VALUE (spec) == tmpl)
4816 permerror (input_location,
4817 "partial specialization of %qD after instantiation "
4818 "of %qD", decl, inst_decl);
4819 }
4820 }
4821
4822 return decl;
4823 }
4824
4825 /* PARM is a template parameter of some form; return the corresponding
4826 TEMPLATE_PARM_INDEX. */
4827
4828 static tree
4829 get_template_parm_index (tree parm)
4830 {
4831 if (TREE_CODE (parm) == PARM_DECL
4832 || TREE_CODE (parm) == CONST_DECL)
4833 parm = DECL_INITIAL (parm);
4834 else if (TREE_CODE (parm) == TYPE_DECL
4835 || TREE_CODE (parm) == TEMPLATE_DECL)
4836 parm = TREE_TYPE (parm);
4837 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4838 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4839 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4840 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4841 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4842 return parm;
4843 }
4844
4845 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4846 parameter packs used by the template parameter PARM. */
4847
4848 static void
4849 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4850 {
4851 /* A type parm can't refer to another parm. */
4852 if (TREE_CODE (parm) == TYPE_DECL)
4853 return;
4854 else if (TREE_CODE (parm) == PARM_DECL)
4855 {
4856 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4857 ppd, ppd->visited);
4858 return;
4859 }
4860
4861 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4862
4863 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4864 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4865 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4866 }
4867
4868 /* PARM is a template parameter pack. Return any parameter packs used in
4869 its type or the type of any of its template parameters. If there are
4870 any such packs, it will be instantiated into a fixed template parameter
4871 list by partial instantiation rather than be fully deduced. */
4872
4873 tree
4874 fixed_parameter_pack_p (tree parm)
4875 {
4876 /* This can only be true in a member template. */
4877 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4878 return NULL_TREE;
4879 /* This can only be true for a parameter pack. */
4880 if (!template_parameter_pack_p (parm))
4881 return NULL_TREE;
4882 /* A type parm can't refer to another parm. */
4883 if (TREE_CODE (parm) == TYPE_DECL)
4884 return NULL_TREE;
4885
4886 tree parameter_packs = NULL_TREE;
4887 struct find_parameter_pack_data ppd;
4888 ppd.parameter_packs = &parameter_packs;
4889 ppd.visited = new hash_set<tree>;
4890 ppd.type_pack_expansion_p = false;
4891
4892 fixed_parameter_pack_p_1 (parm, &ppd);
4893
4894 delete ppd.visited;
4895 return parameter_packs;
4896 }
4897
4898 /* Check that a template declaration's use of default arguments and
4899 parameter packs is not invalid. Here, PARMS are the template
4900 parameters. IS_PRIMARY is true if DECL is the thing declared by
4901 a primary template. IS_PARTIAL is true if DECL is a partial
4902 specialization.
4903
4904 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4905 declaration (but not a definition); 1 indicates a declaration, 2
4906 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4907 emitted for extraneous default arguments.
4908
4909 Returns TRUE if there were no errors found, FALSE otherwise. */
4910
4911 bool
4912 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4913 bool is_partial, int is_friend_decl)
4914 {
4915 const char *msg;
4916 int last_level_to_check;
4917 tree parm_level;
4918 bool no_errors = true;
4919
4920 /* [temp.param]
4921
4922 A default template-argument shall not be specified in a
4923 function template declaration or a function template definition, nor
4924 in the template-parameter-list of the definition of a member of a
4925 class template. */
4926
4927 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4928 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4929 /* You can't have a function template declaration in a local
4930 scope, nor you can you define a member of a class template in a
4931 local scope. */
4932 return true;
4933
4934 if ((TREE_CODE (decl) == TYPE_DECL
4935 && TREE_TYPE (decl)
4936 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4937 || (TREE_CODE (decl) == FUNCTION_DECL
4938 && LAMBDA_FUNCTION_P (decl)))
4939 /* A lambda doesn't have an explicit declaration; don't complain
4940 about the parms of the enclosing class. */
4941 return true;
4942
4943 if (current_class_type
4944 && !TYPE_BEING_DEFINED (current_class_type)
4945 && DECL_LANG_SPECIFIC (decl)
4946 && DECL_DECLARES_FUNCTION_P (decl)
4947 /* If this is either a friend defined in the scope of the class
4948 or a member function. */
4949 && (DECL_FUNCTION_MEMBER_P (decl)
4950 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4951 : DECL_FRIEND_CONTEXT (decl)
4952 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4953 : false)
4954 /* And, if it was a member function, it really was defined in
4955 the scope of the class. */
4956 && (!DECL_FUNCTION_MEMBER_P (decl)
4957 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4958 /* We already checked these parameters when the template was
4959 declared, so there's no need to do it again now. This function
4960 was defined in class scope, but we're processing its body now
4961 that the class is complete. */
4962 return true;
4963
4964 /* Core issue 226 (C++0x only): the following only applies to class
4965 templates. */
4966 if (is_primary
4967 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4968 {
4969 /* [temp.param]
4970
4971 If a template-parameter has a default template-argument, all
4972 subsequent template-parameters shall have a default
4973 template-argument supplied. */
4974 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4975 {
4976 tree inner_parms = TREE_VALUE (parm_level);
4977 int ntparms = TREE_VEC_LENGTH (inner_parms);
4978 int seen_def_arg_p = 0;
4979 int i;
4980
4981 for (i = 0; i < ntparms; ++i)
4982 {
4983 tree parm = TREE_VEC_ELT (inner_parms, i);
4984
4985 if (parm == error_mark_node)
4986 continue;
4987
4988 if (TREE_PURPOSE (parm))
4989 seen_def_arg_p = 1;
4990 else if (seen_def_arg_p
4991 && !template_parameter_pack_p (TREE_VALUE (parm)))
4992 {
4993 error ("no default argument for %qD", TREE_VALUE (parm));
4994 /* For better subsequent error-recovery, we indicate that
4995 there should have been a default argument. */
4996 TREE_PURPOSE (parm) = error_mark_node;
4997 no_errors = false;
4998 }
4999 else if (!is_partial
5000 && !is_friend_decl
5001 /* Don't complain about an enclosing partial
5002 specialization. */
5003 && parm_level == parms
5004 && TREE_CODE (decl) == TYPE_DECL
5005 && i < ntparms - 1
5006 && template_parameter_pack_p (TREE_VALUE (parm))
5007 /* A fixed parameter pack will be partially
5008 instantiated into a fixed length list. */
5009 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5010 {
5011 /* A primary class template can only have one
5012 parameter pack, at the end of the template
5013 parameter list. */
5014
5015 error ("parameter pack %q+D must be at the end of the"
5016 " template parameter list", TREE_VALUE (parm));
5017
5018 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5019 = error_mark_node;
5020 no_errors = false;
5021 }
5022 }
5023 }
5024 }
5025
5026 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5027 || is_partial
5028 || !is_primary
5029 || is_friend_decl)
5030 /* For an ordinary class template, default template arguments are
5031 allowed at the innermost level, e.g.:
5032 template <class T = int>
5033 struct S {};
5034 but, in a partial specialization, they're not allowed even
5035 there, as we have in [temp.class.spec]:
5036
5037 The template parameter list of a specialization shall not
5038 contain default template argument values.
5039
5040 So, for a partial specialization, or for a function template
5041 (in C++98/C++03), we look at all of them. */
5042 ;
5043 else
5044 /* But, for a primary class template that is not a partial
5045 specialization we look at all template parameters except the
5046 innermost ones. */
5047 parms = TREE_CHAIN (parms);
5048
5049 /* Figure out what error message to issue. */
5050 if (is_friend_decl == 2)
5051 msg = G_("default template arguments may not be used in function template "
5052 "friend re-declaration");
5053 else if (is_friend_decl)
5054 msg = G_("default template arguments may not be used in function template "
5055 "friend declarations");
5056 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5057 msg = G_("default template arguments may not be used in function templates "
5058 "without -std=c++11 or -std=gnu++11");
5059 else if (is_partial)
5060 msg = G_("default template arguments may not be used in "
5061 "partial specializations");
5062 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5063 msg = G_("default argument for template parameter for class enclosing %qD");
5064 else
5065 /* Per [temp.param]/9, "A default template-argument shall not be
5066 specified in the template-parameter-lists of the definition of
5067 a member of a class template that appears outside of the member's
5068 class.", thus if we aren't handling a member of a class template
5069 there is no need to examine the parameters. */
5070 return true;
5071
5072 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5073 /* If we're inside a class definition, there's no need to
5074 examine the parameters to the class itself. On the one
5075 hand, they will be checked when the class is defined, and,
5076 on the other, default arguments are valid in things like:
5077 template <class T = double>
5078 struct S { template <class U> void f(U); };
5079 Here the default argument for `S' has no bearing on the
5080 declaration of `f'. */
5081 last_level_to_check = template_class_depth (current_class_type) + 1;
5082 else
5083 /* Check everything. */
5084 last_level_to_check = 0;
5085
5086 for (parm_level = parms;
5087 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5088 parm_level = TREE_CHAIN (parm_level))
5089 {
5090 tree inner_parms = TREE_VALUE (parm_level);
5091 int i;
5092 int ntparms;
5093
5094 ntparms = TREE_VEC_LENGTH (inner_parms);
5095 for (i = 0; i < ntparms; ++i)
5096 {
5097 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5098 continue;
5099
5100 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5101 {
5102 if (msg)
5103 {
5104 no_errors = false;
5105 if (is_friend_decl == 2)
5106 return no_errors;
5107
5108 error (msg, decl);
5109 msg = 0;
5110 }
5111
5112 /* Clear out the default argument so that we are not
5113 confused later. */
5114 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5115 }
5116 }
5117
5118 /* At this point, if we're still interested in issuing messages,
5119 they must apply to classes surrounding the object declared. */
5120 if (msg)
5121 msg = G_("default argument for template parameter for class "
5122 "enclosing %qD");
5123 }
5124
5125 return no_errors;
5126 }
5127
5128 /* Worker for push_template_decl_real, called via
5129 for_each_template_parm. DATA is really an int, indicating the
5130 level of the parameters we are interested in. If T is a template
5131 parameter of that level, return nonzero. */
5132
5133 static int
5134 template_parm_this_level_p (tree t, void* data)
5135 {
5136 int this_level = *(int *)data;
5137 int level;
5138
5139 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5140 level = TEMPLATE_PARM_LEVEL (t);
5141 else
5142 level = TEMPLATE_TYPE_LEVEL (t);
5143 return level == this_level;
5144 }
5145
5146 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5147 DATA is really an int, indicating the innermost outer level of parameters.
5148 If T is a template parameter of that level or further out, return
5149 nonzero. */
5150
5151 static int
5152 template_parm_outer_level (tree t, void *data)
5153 {
5154 int this_level = *(int *)data;
5155 int level;
5156
5157 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5158 level = TEMPLATE_PARM_LEVEL (t);
5159 else
5160 level = TEMPLATE_TYPE_LEVEL (t);
5161 return level <= this_level;
5162 }
5163
5164 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5165 parameters given by current_template_args, or reuses a
5166 previously existing one, if appropriate. Returns the DECL, or an
5167 equivalent one, if it is replaced via a call to duplicate_decls.
5168
5169 If IS_FRIEND is true, DECL is a friend declaration. */
5170
5171 tree
5172 push_template_decl_real (tree decl, bool is_friend)
5173 {
5174 tree tmpl;
5175 tree args;
5176 tree info;
5177 tree ctx;
5178 bool is_primary;
5179 bool is_partial;
5180 int new_template_p = 0;
5181 /* True if the template is a member template, in the sense of
5182 [temp.mem]. */
5183 bool member_template_p = false;
5184
5185 if (decl == error_mark_node || !current_template_parms)
5186 return error_mark_node;
5187
5188 /* See if this is a partial specialization. */
5189 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5190 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5191 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5192 || (VAR_P (decl)
5193 && DECL_LANG_SPECIFIC (decl)
5194 && DECL_TEMPLATE_SPECIALIZATION (decl)
5195 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5196
5197 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5198 is_friend = true;
5199
5200 if (is_friend)
5201 /* For a friend, we want the context of the friend function, not
5202 the type of which it is a friend. */
5203 ctx = CP_DECL_CONTEXT (decl);
5204 else if (CP_DECL_CONTEXT (decl)
5205 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5206 /* In the case of a virtual function, we want the class in which
5207 it is defined. */
5208 ctx = CP_DECL_CONTEXT (decl);
5209 else
5210 /* Otherwise, if we're currently defining some class, the DECL
5211 is assumed to be a member of the class. */
5212 ctx = current_scope ();
5213
5214 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5215 ctx = NULL_TREE;
5216
5217 if (!DECL_CONTEXT (decl))
5218 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5219
5220 /* See if this is a primary template. */
5221 if (is_friend && ctx
5222 && uses_template_parms_level (ctx, processing_template_decl))
5223 /* A friend template that specifies a class context, i.e.
5224 template <typename T> friend void A<T>::f();
5225 is not primary. */
5226 is_primary = false;
5227 else if (TREE_CODE (decl) == TYPE_DECL
5228 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5229 is_primary = false;
5230 else
5231 is_primary = template_parm_scope_p ();
5232
5233 if (is_primary)
5234 {
5235 warning (OPT_Wtemplates, "template %qD declared", decl);
5236
5237 if (DECL_CLASS_SCOPE_P (decl))
5238 member_template_p = true;
5239 if (TREE_CODE (decl) == TYPE_DECL
5240 && anon_aggrname_p (DECL_NAME (decl)))
5241 {
5242 error ("template class without a name");
5243 return error_mark_node;
5244 }
5245 else if (TREE_CODE (decl) == FUNCTION_DECL)
5246 {
5247 if (member_template_p)
5248 {
5249 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5250 error ("member template %qD may not have virt-specifiers", decl);
5251 }
5252 if (DECL_DESTRUCTOR_P (decl))
5253 {
5254 /* [temp.mem]
5255
5256 A destructor shall not be a member template. */
5257 error ("destructor %qD declared as member template", decl);
5258 return error_mark_node;
5259 }
5260 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5261 && (!prototype_p (TREE_TYPE (decl))
5262 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5263 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5264 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5265 == void_list_node)))
5266 {
5267 /* [basic.stc.dynamic.allocation]
5268
5269 An allocation function can be a function
5270 template. ... Template allocation functions shall
5271 have two or more parameters. */
5272 error ("invalid template declaration of %qD", decl);
5273 return error_mark_node;
5274 }
5275 }
5276 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5277 && CLASS_TYPE_P (TREE_TYPE (decl)))
5278 {
5279 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5280 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5281 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5282 {
5283 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5284 if (TREE_CODE (t) == TYPE_DECL)
5285 t = TREE_TYPE (t);
5286 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5287 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5288 }
5289 }
5290 else if (TREE_CODE (decl) == TYPE_DECL
5291 && TYPE_DECL_ALIAS_P (decl))
5292 /* alias-declaration */
5293 gcc_assert (!DECL_ARTIFICIAL (decl));
5294 else if (VAR_P (decl))
5295 /* C++14 variable template. */;
5296 else
5297 {
5298 error ("template declaration of %q#D", decl);
5299 return error_mark_node;
5300 }
5301 }
5302
5303 /* Check to see that the rules regarding the use of default
5304 arguments are not being violated. */
5305 check_default_tmpl_args (decl, current_template_parms,
5306 is_primary, is_partial, /*is_friend_decl=*/0);
5307
5308 /* Ensure that there are no parameter packs in the type of this
5309 declaration that have not been expanded. */
5310 if (TREE_CODE (decl) == FUNCTION_DECL)
5311 {
5312 /* Check each of the arguments individually to see if there are
5313 any bare parameter packs. */
5314 tree type = TREE_TYPE (decl);
5315 tree arg = DECL_ARGUMENTS (decl);
5316 tree argtype = TYPE_ARG_TYPES (type);
5317
5318 while (arg && argtype)
5319 {
5320 if (!DECL_PACK_P (arg)
5321 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5322 {
5323 /* This is a PARM_DECL that contains unexpanded parameter
5324 packs. We have already complained about this in the
5325 check_for_bare_parameter_packs call, so just replace
5326 these types with ERROR_MARK_NODE. */
5327 TREE_TYPE (arg) = error_mark_node;
5328 TREE_VALUE (argtype) = error_mark_node;
5329 }
5330
5331 arg = DECL_CHAIN (arg);
5332 argtype = TREE_CHAIN (argtype);
5333 }
5334
5335 /* Check for bare parameter packs in the return type and the
5336 exception specifiers. */
5337 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5338 /* Errors were already issued, set return type to int
5339 as the frontend doesn't expect error_mark_node as
5340 the return type. */
5341 TREE_TYPE (type) = integer_type_node;
5342 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5343 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5344 }
5345 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5346 && TYPE_DECL_ALIAS_P (decl))
5347 ? DECL_ORIGINAL_TYPE (decl)
5348 : TREE_TYPE (decl)))
5349 {
5350 TREE_TYPE (decl) = error_mark_node;
5351 return error_mark_node;
5352 }
5353
5354 if (is_partial)
5355 return process_partial_specialization (decl);
5356
5357 args = current_template_args ();
5358
5359 if (!ctx
5360 || TREE_CODE (ctx) == FUNCTION_DECL
5361 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5362 || (TREE_CODE (decl) == TYPE_DECL
5363 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5364 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5365 {
5366 if (DECL_LANG_SPECIFIC (decl)
5367 && DECL_TEMPLATE_INFO (decl)
5368 && DECL_TI_TEMPLATE (decl))
5369 tmpl = DECL_TI_TEMPLATE (decl);
5370 /* If DECL is a TYPE_DECL for a class-template, then there won't
5371 be DECL_LANG_SPECIFIC. The information equivalent to
5372 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5373 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5374 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5375 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5376 {
5377 /* Since a template declaration already existed for this
5378 class-type, we must be redeclaring it here. Make sure
5379 that the redeclaration is valid. */
5380 redeclare_class_template (TREE_TYPE (decl),
5381 current_template_parms,
5382 current_template_constraints ());
5383 /* We don't need to create a new TEMPLATE_DECL; just use the
5384 one we already had. */
5385 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5386 }
5387 else
5388 {
5389 tmpl = build_template_decl (decl, current_template_parms,
5390 member_template_p);
5391 new_template_p = 1;
5392
5393 if (DECL_LANG_SPECIFIC (decl)
5394 && DECL_TEMPLATE_SPECIALIZATION (decl))
5395 {
5396 /* A specialization of a member template of a template
5397 class. */
5398 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5399 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5400 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5401 }
5402 }
5403 }
5404 else
5405 {
5406 tree a, t, current, parms;
5407 int i;
5408 tree tinfo = get_template_info (decl);
5409
5410 if (!tinfo)
5411 {
5412 error ("template definition of non-template %q#D", decl);
5413 return error_mark_node;
5414 }
5415
5416 tmpl = TI_TEMPLATE (tinfo);
5417
5418 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5419 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5420 && DECL_TEMPLATE_SPECIALIZATION (decl)
5421 && DECL_MEMBER_TEMPLATE_P (tmpl))
5422 {
5423 tree new_tmpl;
5424
5425 /* The declaration is a specialization of a member
5426 template, declared outside the class. Therefore, the
5427 innermost template arguments will be NULL, so we
5428 replace them with the arguments determined by the
5429 earlier call to check_explicit_specialization. */
5430 args = DECL_TI_ARGS (decl);
5431
5432 new_tmpl
5433 = build_template_decl (decl, current_template_parms,
5434 member_template_p);
5435 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5436 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5437 DECL_TI_TEMPLATE (decl) = new_tmpl;
5438 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5439 DECL_TEMPLATE_INFO (new_tmpl)
5440 = build_template_info (tmpl, args);
5441
5442 register_specialization (new_tmpl,
5443 most_general_template (tmpl),
5444 args,
5445 is_friend, 0);
5446 return decl;
5447 }
5448
5449 /* Make sure the template headers we got make sense. */
5450
5451 parms = DECL_TEMPLATE_PARMS (tmpl);
5452 i = TMPL_PARMS_DEPTH (parms);
5453 if (TMPL_ARGS_DEPTH (args) != i)
5454 {
5455 error ("expected %d levels of template parms for %q#D, got %d",
5456 i, decl, TMPL_ARGS_DEPTH (args));
5457 DECL_INTERFACE_KNOWN (decl) = 1;
5458 return error_mark_node;
5459 }
5460 else
5461 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5462 {
5463 a = TMPL_ARGS_LEVEL (args, i);
5464 t = INNERMOST_TEMPLATE_PARMS (parms);
5465
5466 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5467 {
5468 if (current == decl)
5469 error ("got %d template parameters for %q#D",
5470 TREE_VEC_LENGTH (a), decl);
5471 else
5472 error ("got %d template parameters for %q#T",
5473 TREE_VEC_LENGTH (a), current);
5474 error (" but %d required", TREE_VEC_LENGTH (t));
5475 /* Avoid crash in import_export_decl. */
5476 DECL_INTERFACE_KNOWN (decl) = 1;
5477 return error_mark_node;
5478 }
5479
5480 if (current == decl)
5481 current = ctx;
5482 else if (current == NULL_TREE)
5483 /* Can happen in erroneous input. */
5484 break;
5485 else
5486 current = get_containing_scope (current);
5487 }
5488
5489 /* Check that the parms are used in the appropriate qualifying scopes
5490 in the declarator. */
5491 if (!comp_template_args
5492 (TI_ARGS (tinfo),
5493 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5494 {
5495 error ("\
5496 template arguments to %qD do not match original template %qD",
5497 decl, DECL_TEMPLATE_RESULT (tmpl));
5498 if (!uses_template_parms (TI_ARGS (tinfo)))
5499 inform (input_location, "use template<> for an explicit specialization");
5500 /* Avoid crash in import_export_decl. */
5501 DECL_INTERFACE_KNOWN (decl) = 1;
5502 return error_mark_node;
5503 }
5504 }
5505
5506 DECL_TEMPLATE_RESULT (tmpl) = decl;
5507 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5508
5509 /* Push template declarations for global functions and types. Note
5510 that we do not try to push a global template friend declared in a
5511 template class; such a thing may well depend on the template
5512 parameters of the class. */
5513 if (new_template_p && !ctx
5514 && !(is_friend && template_class_depth (current_class_type) > 0))
5515 {
5516 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5517 if (tmpl == error_mark_node)
5518 return error_mark_node;
5519
5520 /* Hide template friend classes that haven't been declared yet. */
5521 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5522 {
5523 DECL_ANTICIPATED (tmpl) = 1;
5524 DECL_FRIEND_P (tmpl) = 1;
5525 }
5526 }
5527
5528 if (is_primary)
5529 {
5530 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5531 int i;
5532
5533 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5534 if (DECL_CONV_FN_P (tmpl))
5535 {
5536 int depth = TMPL_PARMS_DEPTH (parms);
5537
5538 /* It is a conversion operator. See if the type converted to
5539 depends on innermost template operands. */
5540
5541 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5542 depth))
5543 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5544 }
5545
5546 /* Give template template parms a DECL_CONTEXT of the template
5547 for which they are a parameter. */
5548 parms = INNERMOST_TEMPLATE_PARMS (parms);
5549 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5550 {
5551 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5552 if (TREE_CODE (parm) == TEMPLATE_DECL)
5553 DECL_CONTEXT (parm) = tmpl;
5554 }
5555
5556 if (TREE_CODE (decl) == TYPE_DECL
5557 && TYPE_DECL_ALIAS_P (decl)
5558 && complex_alias_template_p (tmpl))
5559 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5560 }
5561
5562 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5563 back to its most general template. If TMPL is a specialization,
5564 ARGS may only have the innermost set of arguments. Add the missing
5565 argument levels if necessary. */
5566 if (DECL_TEMPLATE_INFO (tmpl))
5567 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5568
5569 info = build_template_info (tmpl, args);
5570
5571 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5572 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5573 else
5574 {
5575 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5576 retrofit_lang_decl (decl);
5577 if (DECL_LANG_SPECIFIC (decl))
5578 DECL_TEMPLATE_INFO (decl) = info;
5579 }
5580
5581 if (flag_implicit_templates
5582 && !is_friend
5583 && TREE_PUBLIC (decl)
5584 && VAR_OR_FUNCTION_DECL_P (decl))
5585 /* Set DECL_COMDAT on template instantiations; if we force
5586 them to be emitted by explicit instantiation or -frepo,
5587 mark_needed will tell cgraph to do the right thing. */
5588 DECL_COMDAT (decl) = true;
5589
5590 return DECL_TEMPLATE_RESULT (tmpl);
5591 }
5592
5593 tree
5594 push_template_decl (tree decl)
5595 {
5596 return push_template_decl_real (decl, false);
5597 }
5598
5599 /* FN is an inheriting constructor that inherits from the constructor
5600 template INHERITED; turn FN into a constructor template with a matching
5601 template header. */
5602
5603 tree
5604 add_inherited_template_parms (tree fn, tree inherited)
5605 {
5606 tree inner_parms
5607 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5608 inner_parms = copy_node (inner_parms);
5609 tree parms
5610 = tree_cons (size_int (processing_template_decl + 1),
5611 inner_parms, current_template_parms);
5612 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5613 tree args = template_parms_to_args (parms);
5614 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5615 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5616 DECL_TEMPLATE_RESULT (tmpl) = fn;
5617 DECL_ARTIFICIAL (tmpl) = true;
5618 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5619 return tmpl;
5620 }
5621
5622 /* Called when a class template TYPE is redeclared with the indicated
5623 template PARMS, e.g.:
5624
5625 template <class T> struct S;
5626 template <class T> struct S {}; */
5627
5628 bool
5629 redeclare_class_template (tree type, tree parms, tree cons)
5630 {
5631 tree tmpl;
5632 tree tmpl_parms;
5633 int i;
5634
5635 if (!TYPE_TEMPLATE_INFO (type))
5636 {
5637 error ("%qT is not a template type", type);
5638 return false;
5639 }
5640
5641 tmpl = TYPE_TI_TEMPLATE (type);
5642 if (!PRIMARY_TEMPLATE_P (tmpl))
5643 /* The type is nested in some template class. Nothing to worry
5644 about here; there are no new template parameters for the nested
5645 type. */
5646 return true;
5647
5648 if (!parms)
5649 {
5650 error ("template specifiers not specified in declaration of %qD",
5651 tmpl);
5652 return false;
5653 }
5654
5655 parms = INNERMOST_TEMPLATE_PARMS (parms);
5656 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5657
5658 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5659 {
5660 error_n (input_location, TREE_VEC_LENGTH (parms),
5661 "redeclared with %d template parameter",
5662 "redeclared with %d template parameters",
5663 TREE_VEC_LENGTH (parms));
5664 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5665 "previous declaration %qD used %d template parameter",
5666 "previous declaration %qD used %d template parameters",
5667 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5668 return false;
5669 }
5670
5671 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5672 {
5673 tree tmpl_parm;
5674 tree parm;
5675 tree tmpl_default;
5676 tree parm_default;
5677
5678 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5679 || TREE_VEC_ELT (parms, i) == error_mark_node)
5680 continue;
5681
5682 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5683 if (error_operand_p (tmpl_parm))
5684 return false;
5685
5686 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5687 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5688 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5689
5690 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5691 TEMPLATE_DECL. */
5692 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5693 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5694 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5695 || (TREE_CODE (tmpl_parm) != PARM_DECL
5696 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5697 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5698 || (TREE_CODE (tmpl_parm) == PARM_DECL
5699 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5700 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5701 {
5702 error ("template parameter %q+#D", tmpl_parm);
5703 error ("redeclared here as %q#D", parm);
5704 return false;
5705 }
5706
5707 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5708 {
5709 /* We have in [temp.param]:
5710
5711 A template-parameter may not be given default arguments
5712 by two different declarations in the same scope. */
5713 error_at (input_location, "redefinition of default argument for %q#D", parm);
5714 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5715 "original definition appeared here");
5716 return false;
5717 }
5718
5719 if (parm_default != NULL_TREE)
5720 /* Update the previous template parameters (which are the ones
5721 that will really count) with the new default value. */
5722 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5723 else if (tmpl_default != NULL_TREE)
5724 /* Update the new parameters, too; they'll be used as the
5725 parameters for any members. */
5726 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5727
5728 /* Give each template template parm in this redeclaration a
5729 DECL_CONTEXT of the template for which they are a parameter. */
5730 if (TREE_CODE (parm) == TEMPLATE_DECL)
5731 {
5732 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5733 DECL_CONTEXT (parm) = tmpl;
5734 }
5735
5736 if (TREE_CODE (parm) == TYPE_DECL)
5737 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5738 }
5739
5740 // Cannot redeclare a class template with a different set of constraints.
5741 if (!equivalent_constraints (get_constraints (tmpl), cons))
5742 {
5743 error_at (input_location, "redeclaration %q#D with different "
5744 "constraints", tmpl);
5745 inform (DECL_SOURCE_LOCATION (tmpl),
5746 "original declaration appeared here");
5747 }
5748
5749 return true;
5750 }
5751
5752 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5753 to be used when the caller has already checked
5754 (processing_template_decl
5755 && !instantiation_dependent_expression_p (expr)
5756 && potential_constant_expression (expr))
5757 and cleared processing_template_decl. */
5758
5759 tree
5760 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5761 {
5762 return tsubst_copy_and_build (expr,
5763 /*args=*/NULL_TREE,
5764 complain,
5765 /*in_decl=*/NULL_TREE,
5766 /*function_p=*/false,
5767 /*integral_constant_expression_p=*/true);
5768 }
5769
5770 /* Simplify EXPR if it is a non-dependent expression. Returns the
5771 (possibly simplified) expression. */
5772
5773 tree
5774 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5775 {
5776 if (expr == NULL_TREE)
5777 return NULL_TREE;
5778
5779 /* If we're in a template, but EXPR isn't value dependent, simplify
5780 it. We're supposed to treat:
5781
5782 template <typename T> void f(T[1 + 1]);
5783 template <typename T> void f(T[2]);
5784
5785 as two declarations of the same function, for example. */
5786 if (processing_template_decl
5787 && potential_nondependent_constant_expression (expr))
5788 {
5789 processing_template_decl_sentinel s;
5790 expr = instantiate_non_dependent_expr_internal (expr, complain);
5791 }
5792 return expr;
5793 }
5794
5795 tree
5796 instantiate_non_dependent_expr (tree expr)
5797 {
5798 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5799 }
5800
5801 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5802 an uninstantiated expression. */
5803
5804 tree
5805 instantiate_non_dependent_or_null (tree expr)
5806 {
5807 if (expr == NULL_TREE)
5808 return NULL_TREE;
5809 if (processing_template_decl)
5810 {
5811 if (!potential_nondependent_constant_expression (expr))
5812 expr = NULL_TREE;
5813 else
5814 {
5815 processing_template_decl_sentinel s;
5816 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5817 }
5818 }
5819 return expr;
5820 }
5821
5822 /* True iff T is a specialization of a variable template. */
5823
5824 bool
5825 variable_template_specialization_p (tree t)
5826 {
5827 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5828 return false;
5829 tree tmpl = DECL_TI_TEMPLATE (t);
5830 return variable_template_p (tmpl);
5831 }
5832
5833 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5834 template declaration, or a TYPE_DECL for an alias declaration. */
5835
5836 bool
5837 alias_type_or_template_p (tree t)
5838 {
5839 if (t == NULL_TREE)
5840 return false;
5841 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5842 || (TYPE_P (t)
5843 && TYPE_NAME (t)
5844 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5845 || DECL_ALIAS_TEMPLATE_P (t));
5846 }
5847
5848 /* Return TRUE iff T is a specialization of an alias template. */
5849
5850 bool
5851 alias_template_specialization_p (const_tree t)
5852 {
5853 /* It's an alias template specialization if it's an alias and its
5854 TYPE_NAME is a specialization of a primary template. */
5855 if (TYPE_ALIAS_P (t))
5856 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5857 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5858
5859 return false;
5860 }
5861
5862 /* An alias template is complex from a SFINAE perspective if a template-id
5863 using that alias can be ill-formed when the expansion is not, as with
5864 the void_t template. We determine this by checking whether the
5865 expansion for the alias template uses all its template parameters. */
5866
5867 struct uses_all_template_parms_data
5868 {
5869 int level;
5870 bool *seen;
5871 };
5872
5873 static int
5874 uses_all_template_parms_r (tree t, void *data_)
5875 {
5876 struct uses_all_template_parms_data &data
5877 = *(struct uses_all_template_parms_data*)data_;
5878 tree idx = get_template_parm_index (t);
5879
5880 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5881 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5882 return 0;
5883 }
5884
5885 static bool
5886 complex_alias_template_p (const_tree tmpl)
5887 {
5888 struct uses_all_template_parms_data data;
5889 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5890 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5891 data.level = TMPL_PARMS_DEPTH (parms);
5892 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5893 data.seen = XALLOCAVEC (bool, len);
5894 for (int i = 0; i < len; ++i)
5895 data.seen[i] = false;
5896
5897 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5898 for (int i = 0; i < len; ++i)
5899 if (!data.seen[i])
5900 return true;
5901 return false;
5902 }
5903
5904 /* Return TRUE iff T is a specialization of a complex alias template with
5905 dependent template-arguments. */
5906
5907 bool
5908 dependent_alias_template_spec_p (const_tree t)
5909 {
5910 if (!alias_template_specialization_p (t))
5911 return false;
5912
5913 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5914 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5915 return false;
5916
5917 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5918 if (!any_dependent_template_arguments_p (args))
5919 return false;
5920
5921 return true;
5922 }
5923
5924 /* Return the number of innermost template parameters in TMPL. */
5925
5926 static int
5927 num_innermost_template_parms (tree tmpl)
5928 {
5929 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5930 return TREE_VEC_LENGTH (parms);
5931 }
5932
5933 /* Return either TMPL or another template that it is equivalent to under DR
5934 1286: An alias that just changes the name of a template is equivalent to
5935 the other template. */
5936
5937 static tree
5938 get_underlying_template (tree tmpl)
5939 {
5940 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5941 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5942 {
5943 /* Determine if the alias is equivalent to an underlying template. */
5944 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5945 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
5946 if (!tinfo)
5947 break;
5948
5949 tree underlying = TI_TEMPLATE (tinfo);
5950 if (!PRIMARY_TEMPLATE_P (underlying)
5951 || (num_innermost_template_parms (tmpl)
5952 != num_innermost_template_parms (underlying)))
5953 break;
5954
5955 tree alias_args = INNERMOST_TEMPLATE_ARGS
5956 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5957 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
5958 break;
5959
5960 /* Alias is equivalent. Strip it and repeat. */
5961 tmpl = underlying;
5962 }
5963
5964 return tmpl;
5965 }
5966
5967 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5968 must be a reference-to-function or a pointer-to-function type, as specified
5969 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5970 and check that the resulting function has external linkage. */
5971
5972 static tree
5973 convert_nontype_argument_function (tree type, tree expr,
5974 tsubst_flags_t complain)
5975 {
5976 tree fns = expr;
5977 tree fn, fn_no_ptr;
5978 linkage_kind linkage;
5979
5980 fn = instantiate_type (type, fns, tf_none);
5981 if (fn == error_mark_node)
5982 return error_mark_node;
5983
5984 if (value_dependent_expression_p (fn))
5985 return fn;
5986
5987 fn_no_ptr = strip_fnptr_conv (fn);
5988 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5989 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5990 if (BASELINK_P (fn_no_ptr))
5991 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5992
5993 /* [temp.arg.nontype]/1
5994
5995 A template-argument for a non-type, non-template template-parameter
5996 shall be one of:
5997 [...]
5998 -- the address of an object or function with external [C++11: or
5999 internal] linkage. */
6000
6001 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6002 {
6003 if (complain & tf_error)
6004 {
6005 error ("%qE is not a valid template argument for type %qT",
6006 expr, type);
6007 if (TYPE_PTR_P (type))
6008 inform (input_location, "it must be the address of a function "
6009 "with external linkage");
6010 else
6011 inform (input_location, "it must be the name of a function with "
6012 "external linkage");
6013 }
6014 return NULL_TREE;
6015 }
6016
6017 linkage = decl_linkage (fn_no_ptr);
6018 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6019 {
6020 if (complain & tf_error)
6021 {
6022 if (cxx_dialect >= cxx11)
6023 error ("%qE is not a valid template argument for type %qT "
6024 "because %qD has no linkage",
6025 expr, type, fn_no_ptr);
6026 else
6027 error ("%qE is not a valid template argument for type %qT "
6028 "because %qD does not have external linkage",
6029 expr, type, fn_no_ptr);
6030 }
6031 return NULL_TREE;
6032 }
6033
6034 if (TREE_CODE (type) == REFERENCE_TYPE)
6035 fn = build_address (fn);
6036 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6037 fn = build_nop (type, fn);
6038
6039 return fn;
6040 }
6041
6042 /* Subroutine of convert_nontype_argument.
6043 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6044 Emit an error otherwise. */
6045
6046 static bool
6047 check_valid_ptrmem_cst_expr (tree type, tree expr,
6048 tsubst_flags_t complain)
6049 {
6050 STRIP_NOPS (expr);
6051 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
6052 return true;
6053 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6054 return true;
6055 if (processing_template_decl
6056 && TREE_CODE (expr) == ADDR_EXPR
6057 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6058 return true;
6059 if (complain & tf_error)
6060 {
6061 error ("%qE is not a valid template argument for type %qT",
6062 expr, type);
6063 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6064 }
6065 return false;
6066 }
6067
6068 /* Returns TRUE iff the address of OP is value-dependent.
6069
6070 14.6.2.4 [temp.dep.temp]:
6071 A non-integral non-type template-argument is dependent if its type is
6072 dependent or it has either of the following forms
6073 qualified-id
6074 & qualified-id
6075 and contains a nested-name-specifier which specifies a class-name that
6076 names a dependent type.
6077
6078 We generalize this to just say that the address of a member of a
6079 dependent class is value-dependent; the above doesn't cover the
6080 address of a static data member named with an unqualified-id. */
6081
6082 static bool
6083 has_value_dependent_address (tree op)
6084 {
6085 /* We could use get_inner_reference here, but there's no need;
6086 this is only relevant for template non-type arguments, which
6087 can only be expressed as &id-expression. */
6088 if (DECL_P (op))
6089 {
6090 tree ctx = CP_DECL_CONTEXT (op);
6091 if (TYPE_P (ctx) && dependent_type_p (ctx))
6092 return true;
6093 }
6094
6095 return false;
6096 }
6097
6098 /* The next set of functions are used for providing helpful explanatory
6099 diagnostics for failed overload resolution. Their messages should be
6100 indented by two spaces for consistency with the messages in
6101 call.c */
6102
6103 static int
6104 unify_success (bool /*explain_p*/)
6105 {
6106 return 0;
6107 }
6108
6109 static int
6110 unify_parameter_deduction_failure (bool explain_p, tree parm)
6111 {
6112 if (explain_p)
6113 inform (input_location,
6114 " couldn't deduce template parameter %qD", parm);
6115 return 1;
6116 }
6117
6118 static int
6119 unify_invalid (bool /*explain_p*/)
6120 {
6121 return 1;
6122 }
6123
6124 static int
6125 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6126 {
6127 if (explain_p)
6128 inform (input_location,
6129 " types %qT and %qT have incompatible cv-qualifiers",
6130 parm, arg);
6131 return 1;
6132 }
6133
6134 static int
6135 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6136 {
6137 if (explain_p)
6138 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6139 return 1;
6140 }
6141
6142 static int
6143 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6144 {
6145 if (explain_p)
6146 inform (input_location,
6147 " template parameter %qD is not a parameter pack, but "
6148 "argument %qD is",
6149 parm, arg);
6150 return 1;
6151 }
6152
6153 static int
6154 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6155 {
6156 if (explain_p)
6157 inform (input_location,
6158 " template argument %qE does not match "
6159 "pointer-to-member constant %qE",
6160 arg, parm);
6161 return 1;
6162 }
6163
6164 static int
6165 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6166 {
6167 if (explain_p)
6168 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6169 return 1;
6170 }
6171
6172 static int
6173 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6174 {
6175 if (explain_p)
6176 inform (input_location,
6177 " inconsistent parameter pack deduction with %qT and %qT",
6178 old_arg, new_arg);
6179 return 1;
6180 }
6181
6182 static int
6183 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6184 {
6185 if (explain_p)
6186 {
6187 if (TYPE_P (parm))
6188 inform (input_location,
6189 " deduced conflicting types for parameter %qT (%qT and %qT)",
6190 parm, first, second);
6191 else
6192 inform (input_location,
6193 " deduced conflicting values for non-type parameter "
6194 "%qE (%qE and %qE)", parm, first, second);
6195 }
6196 return 1;
6197 }
6198
6199 static int
6200 unify_vla_arg (bool explain_p, tree arg)
6201 {
6202 if (explain_p)
6203 inform (input_location,
6204 " variable-sized array type %qT is not "
6205 "a valid template argument",
6206 arg);
6207 return 1;
6208 }
6209
6210 static int
6211 unify_method_type_error (bool explain_p, tree arg)
6212 {
6213 if (explain_p)
6214 inform (input_location,
6215 " member function type %qT is not a valid template argument",
6216 arg);
6217 return 1;
6218 }
6219
6220 static int
6221 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6222 {
6223 if (explain_p)
6224 {
6225 if (least_p)
6226 inform_n (input_location, wanted,
6227 " candidate expects at least %d argument, %d provided",
6228 " candidate expects at least %d arguments, %d provided",
6229 wanted, have);
6230 else
6231 inform_n (input_location, wanted,
6232 " candidate expects %d argument, %d provided",
6233 " candidate expects %d arguments, %d provided",
6234 wanted, have);
6235 }
6236 return 1;
6237 }
6238
6239 static int
6240 unify_too_many_arguments (bool explain_p, int have, int wanted)
6241 {
6242 return unify_arity (explain_p, have, wanted);
6243 }
6244
6245 static int
6246 unify_too_few_arguments (bool explain_p, int have, int wanted,
6247 bool least_p = false)
6248 {
6249 return unify_arity (explain_p, have, wanted, least_p);
6250 }
6251
6252 static int
6253 unify_arg_conversion (bool explain_p, tree to_type,
6254 tree from_type, tree arg)
6255 {
6256 if (explain_p)
6257 inform (EXPR_LOC_OR_LOC (arg, input_location),
6258 " cannot convert %qE (type %qT) to type %qT",
6259 arg, from_type, to_type);
6260 return 1;
6261 }
6262
6263 static int
6264 unify_no_common_base (bool explain_p, enum template_base_result r,
6265 tree parm, tree arg)
6266 {
6267 if (explain_p)
6268 switch (r)
6269 {
6270 case tbr_ambiguous_baseclass:
6271 inform (input_location, " %qT is an ambiguous base class of %qT",
6272 parm, arg);
6273 break;
6274 default:
6275 inform (input_location, " %qT is not derived from %qT", arg, parm);
6276 break;
6277 }
6278 return 1;
6279 }
6280
6281 static int
6282 unify_inconsistent_template_template_parameters (bool explain_p)
6283 {
6284 if (explain_p)
6285 inform (input_location,
6286 " template parameters of a template template argument are "
6287 "inconsistent with other deduced template arguments");
6288 return 1;
6289 }
6290
6291 static int
6292 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6293 {
6294 if (explain_p)
6295 inform (input_location,
6296 " can't deduce a template for %qT from non-template type %qT",
6297 parm, arg);
6298 return 1;
6299 }
6300
6301 static int
6302 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6303 {
6304 if (explain_p)
6305 inform (input_location,
6306 " template argument %qE does not match %qE", arg, parm);
6307 return 1;
6308 }
6309
6310 static int
6311 unify_overload_resolution_failure (bool explain_p, tree arg)
6312 {
6313 if (explain_p)
6314 inform (input_location,
6315 " could not resolve address from overloaded function %qE",
6316 arg);
6317 return 1;
6318 }
6319
6320 /* Attempt to convert the non-type template parameter EXPR to the
6321 indicated TYPE. If the conversion is successful, return the
6322 converted value. If the conversion is unsuccessful, return
6323 NULL_TREE if we issued an error message, or error_mark_node if we
6324 did not. We issue error messages for out-and-out bad template
6325 parameters, but not simply because the conversion failed, since we
6326 might be just trying to do argument deduction. Both TYPE and EXPR
6327 must be non-dependent.
6328
6329 The conversion follows the special rules described in
6330 [temp.arg.nontype], and it is much more strict than an implicit
6331 conversion.
6332
6333 This function is called twice for each template argument (see
6334 lookup_template_class for a more accurate description of this
6335 problem). This means that we need to handle expressions which
6336 are not valid in a C++ source, but can be created from the
6337 first call (for instance, casts to perform conversions). These
6338 hacks can go away after we fix the double coercion problem. */
6339
6340 static tree
6341 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6342 {
6343 tree expr_type;
6344
6345 /* Detect immediately string literals as invalid non-type argument.
6346 This special-case is not needed for correctness (we would easily
6347 catch this later), but only to provide better diagnostic for this
6348 common user mistake. As suggested by DR 100, we do not mention
6349 linkage issues in the diagnostic as this is not the point. */
6350 /* FIXME we're making this OK. */
6351 if (TREE_CODE (expr) == STRING_CST)
6352 {
6353 if (complain & tf_error)
6354 error ("%qE is not a valid template argument for type %qT "
6355 "because string literals can never be used in this context",
6356 expr, type);
6357 return NULL_TREE;
6358 }
6359
6360 /* Add the ADDR_EXPR now for the benefit of
6361 value_dependent_expression_p. */
6362 if (TYPE_PTROBV_P (type)
6363 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6364 {
6365 expr = decay_conversion (expr, complain);
6366 if (expr == error_mark_node)
6367 return error_mark_node;
6368 }
6369
6370 /* If we are in a template, EXPR may be non-dependent, but still
6371 have a syntactic, rather than semantic, form. For example, EXPR
6372 might be a SCOPE_REF, rather than the VAR_DECL to which the
6373 SCOPE_REF refers. Preserving the qualifying scope is necessary
6374 so that access checking can be performed when the template is
6375 instantiated -- but here we need the resolved form so that we can
6376 convert the argument. */
6377 bool non_dep = false;
6378 if (TYPE_REF_OBJ_P (type)
6379 && has_value_dependent_address (expr))
6380 /* If we want the address and it's value-dependent, don't fold. */;
6381 else if (processing_template_decl
6382 && potential_nondependent_constant_expression (expr))
6383 non_dep = true;
6384 if (error_operand_p (expr))
6385 return error_mark_node;
6386 expr_type = TREE_TYPE (expr);
6387 if (TREE_CODE (type) == REFERENCE_TYPE)
6388 expr = mark_lvalue_use (expr);
6389 else
6390 expr = mark_rvalue_use (expr);
6391
6392 /* If the argument is non-dependent, perform any conversions in
6393 non-dependent context as well. */
6394 processing_template_decl_sentinel s (non_dep);
6395 if (non_dep)
6396 expr = instantiate_non_dependent_expr_internal (expr, complain);
6397
6398 if (value_dependent_expression_p (expr))
6399 expr = canonicalize_expr_argument (expr, complain);
6400
6401 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6402 to a non-type argument of "nullptr". */
6403 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6404 expr = fold_simple (convert (type, expr));
6405
6406 /* In C++11, integral or enumeration non-type template arguments can be
6407 arbitrary constant expressions. Pointer and pointer to
6408 member arguments can be general constant expressions that evaluate
6409 to a null value, but otherwise still need to be of a specific form. */
6410 if (cxx_dialect >= cxx11)
6411 {
6412 if (TREE_CODE (expr) == PTRMEM_CST)
6413 /* A PTRMEM_CST is already constant, and a valid template
6414 argument for a parameter of pointer to member type, we just want
6415 to leave it in that form rather than lower it to a
6416 CONSTRUCTOR. */;
6417 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6418 /* Constant value checking is done later with type conversion. */;
6419 else if (cxx_dialect >= cxx1z)
6420 {
6421 if (TREE_CODE (type) != REFERENCE_TYPE)
6422 expr = maybe_constant_value (expr);
6423 else if (REFERENCE_REF_P (expr))
6424 {
6425 expr = TREE_OPERAND (expr, 0);
6426 expr = maybe_constant_value (expr);
6427 expr = convert_from_reference (expr);
6428 }
6429 }
6430 else if (TYPE_PTR_OR_PTRMEM_P (type))
6431 {
6432 tree folded = maybe_constant_value (expr);
6433 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6434 : null_member_pointer_value_p (folded))
6435 expr = folded;
6436 }
6437 }
6438
6439 /* HACK: Due to double coercion, we can get a
6440 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6441 which is the tree that we built on the first call (see
6442 below when coercing to reference to object or to reference to
6443 function). We just strip everything and get to the arg.
6444 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6445 for examples. */
6446 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6447 {
6448 tree probe_type, probe = expr;
6449 if (REFERENCE_REF_P (probe))
6450 probe = TREE_OPERAND (probe, 0);
6451 probe_type = TREE_TYPE (probe);
6452 if (TREE_CODE (probe) == NOP_EXPR)
6453 {
6454 /* ??? Maybe we could use convert_from_reference here, but we
6455 would need to relax its constraints because the NOP_EXPR
6456 could actually change the type to something more cv-qualified,
6457 and this is not folded by convert_from_reference. */
6458 tree addr = TREE_OPERAND (probe, 0);
6459 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6460 && TREE_CODE (addr) == ADDR_EXPR
6461 && TYPE_PTR_P (TREE_TYPE (addr))
6462 && (same_type_ignoring_top_level_qualifiers_p
6463 (TREE_TYPE (probe_type),
6464 TREE_TYPE (TREE_TYPE (addr)))))
6465 {
6466 expr = TREE_OPERAND (addr, 0);
6467 expr_type = TREE_TYPE (probe_type);
6468 }
6469 }
6470 }
6471
6472 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6473 parameter is a pointer to object, through decay and
6474 qualification conversion. Let's strip everything. */
6475 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6476 {
6477 tree probe = expr;
6478 STRIP_NOPS (probe);
6479 if (TREE_CODE (probe) == ADDR_EXPR
6480 && TYPE_PTR_P (TREE_TYPE (probe)))
6481 {
6482 /* Skip the ADDR_EXPR only if it is part of the decay for
6483 an array. Otherwise, it is part of the original argument
6484 in the source code. */
6485 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6486 probe = TREE_OPERAND (probe, 0);
6487 expr = probe;
6488 expr_type = TREE_TYPE (expr);
6489 }
6490 }
6491
6492 /* [temp.arg.nontype]/5, bullet 1
6493
6494 For a non-type template-parameter of integral or enumeration type,
6495 integral promotions (_conv.prom_) and integral conversions
6496 (_conv.integral_) are applied. */
6497 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6498 {
6499 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6500 t = maybe_constant_value (t);
6501 if (t != error_mark_node)
6502 expr = t;
6503
6504 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6505 return error_mark_node;
6506
6507 /* Notice that there are constant expressions like '4 % 0' which
6508 do not fold into integer constants. */
6509 if (TREE_CODE (expr) != INTEGER_CST
6510 && !value_dependent_expression_p (expr))
6511 {
6512 if (complain & tf_error)
6513 {
6514 int errs = errorcount, warns = warningcount + werrorcount;
6515 if (processing_template_decl
6516 && !require_potential_constant_expression (expr))
6517 return NULL_TREE;
6518 expr = cxx_constant_value (expr);
6519 if (errorcount > errs || warningcount + werrorcount > warns)
6520 inform (EXPR_LOC_OR_LOC (expr, input_location),
6521 "in template argument for type %qT ", type);
6522 if (expr == error_mark_node)
6523 return NULL_TREE;
6524 /* else cxx_constant_value complained but gave us
6525 a real constant, so go ahead. */
6526 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6527 }
6528 else
6529 return NULL_TREE;
6530 }
6531
6532 /* Avoid typedef problems. */
6533 if (TREE_TYPE (expr) != type)
6534 expr = fold_convert (type, expr);
6535 }
6536 /* [temp.arg.nontype]/5, bullet 2
6537
6538 For a non-type template-parameter of type pointer to object,
6539 qualification conversions (_conv.qual_) and the array-to-pointer
6540 conversion (_conv.array_) are applied. */
6541 else if (TYPE_PTROBV_P (type))
6542 {
6543 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6544
6545 A template-argument for a non-type, non-template template-parameter
6546 shall be one of: [...]
6547
6548 -- the name of a non-type template-parameter;
6549 -- the address of an object or function with external linkage, [...]
6550 expressed as "& id-expression" where the & is optional if the name
6551 refers to a function or array, or if the corresponding
6552 template-parameter is a reference.
6553
6554 Here, we do not care about functions, as they are invalid anyway
6555 for a parameter of type pointer-to-object. */
6556
6557 if (value_dependent_expression_p (expr))
6558 /* Non-type template parameters are OK. */
6559 ;
6560 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6561 /* Null pointer values are OK in C++11. */;
6562 else if (TREE_CODE (expr) != ADDR_EXPR
6563 && TREE_CODE (expr_type) != ARRAY_TYPE)
6564 {
6565 if (VAR_P (expr))
6566 {
6567 if (complain & tf_error)
6568 error ("%qD is not a valid template argument "
6569 "because %qD is a variable, not the address of "
6570 "a variable", expr, expr);
6571 return NULL_TREE;
6572 }
6573 if (POINTER_TYPE_P (expr_type))
6574 {
6575 if (complain & tf_error)
6576 error ("%qE is not a valid template argument for %qT "
6577 "because it is not the address of a variable",
6578 expr, type);
6579 return NULL_TREE;
6580 }
6581 /* Other values, like integer constants, might be valid
6582 non-type arguments of some other type. */
6583 return error_mark_node;
6584 }
6585 else
6586 {
6587 tree decl;
6588
6589 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6590 ? TREE_OPERAND (expr, 0) : expr);
6591 if (!VAR_P (decl))
6592 {
6593 if (complain & tf_error)
6594 error ("%qE is not a valid template argument of type %qT "
6595 "because %qE is not a variable", expr, type, decl);
6596 return NULL_TREE;
6597 }
6598 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6599 {
6600 if (complain & tf_error)
6601 error ("%qE is not a valid template argument of type %qT "
6602 "because %qD does not have external linkage",
6603 expr, type, decl);
6604 return NULL_TREE;
6605 }
6606 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6607 {
6608 if (complain & tf_error)
6609 error ("%qE is not a valid template argument of type %qT "
6610 "because %qD has no linkage", expr, type, decl);
6611 return NULL_TREE;
6612 }
6613 }
6614
6615 expr = decay_conversion (expr, complain);
6616 if (expr == error_mark_node)
6617 return error_mark_node;
6618
6619 expr = perform_qualification_conversions (type, expr);
6620 if (expr == error_mark_node)
6621 return error_mark_node;
6622 }
6623 /* [temp.arg.nontype]/5, bullet 3
6624
6625 For a non-type template-parameter of type reference to object, no
6626 conversions apply. The type referred to by the reference may be more
6627 cv-qualified than the (otherwise identical) type of the
6628 template-argument. The template-parameter is bound directly to the
6629 template-argument, which must be an lvalue. */
6630 else if (TYPE_REF_OBJ_P (type))
6631 {
6632 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6633 expr_type))
6634 return error_mark_node;
6635
6636 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6637 {
6638 if (complain & tf_error)
6639 error ("%qE is not a valid template argument for type %qT "
6640 "because of conflicts in cv-qualification", expr, type);
6641 return NULL_TREE;
6642 }
6643
6644 if (!lvalue_p (expr))
6645 {
6646 if (complain & tf_error)
6647 error ("%qE is not a valid template argument for type %qT "
6648 "because it is not an lvalue", expr, type);
6649 return NULL_TREE;
6650 }
6651
6652 /* [temp.arg.nontype]/1
6653
6654 A template-argument for a non-type, non-template template-parameter
6655 shall be one of: [...]
6656
6657 -- the address of an object or function with external linkage. */
6658 if (INDIRECT_REF_P (expr)
6659 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6660 {
6661 expr = TREE_OPERAND (expr, 0);
6662 if (DECL_P (expr))
6663 {
6664 if (complain & tf_error)
6665 error ("%q#D is not a valid template argument for type %qT "
6666 "because a reference variable does not have a constant "
6667 "address", expr, type);
6668 return NULL_TREE;
6669 }
6670 }
6671
6672 if (!value_dependent_expression_p (expr))
6673 {
6674 if (!DECL_P (expr))
6675 {
6676 if (complain & tf_error)
6677 error ("%qE is not a valid template argument for type %qT "
6678 "because it is not an object with linkage",
6679 expr, type);
6680 return NULL_TREE;
6681 }
6682
6683 /* DR 1155 allows internal linkage in C++11 and up. */
6684 linkage_kind linkage = decl_linkage (expr);
6685 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6686 {
6687 if (complain & tf_error)
6688 error ("%qE is not a valid template argument for type %qT "
6689 "because object %qD does not have linkage",
6690 expr, type, expr);
6691 return NULL_TREE;
6692 }
6693
6694 expr = build_nop (type, build_address (expr));
6695 }
6696 }
6697 /* [temp.arg.nontype]/5, bullet 4
6698
6699 For a non-type template-parameter of type pointer to function, only
6700 the function-to-pointer conversion (_conv.func_) is applied. If the
6701 template-argument represents a set of overloaded functions (or a
6702 pointer to such), the matching function is selected from the set
6703 (_over.over_). */
6704 else if (TYPE_PTRFN_P (type))
6705 {
6706 /* If the argument is a template-id, we might not have enough
6707 context information to decay the pointer. */
6708 if (!type_unknown_p (expr_type))
6709 {
6710 expr = decay_conversion (expr, complain);
6711 if (expr == error_mark_node)
6712 return error_mark_node;
6713 }
6714
6715 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6716 /* Null pointer values are OK in C++11. */
6717 return perform_qualification_conversions (type, expr);
6718
6719 expr = convert_nontype_argument_function (type, expr, complain);
6720 if (!expr || expr == error_mark_node)
6721 return expr;
6722 }
6723 /* [temp.arg.nontype]/5, bullet 5
6724
6725 For a non-type template-parameter of type reference to function, no
6726 conversions apply. If the template-argument represents a set of
6727 overloaded functions, the matching function is selected from the set
6728 (_over.over_). */
6729 else if (TYPE_REFFN_P (type))
6730 {
6731 if (TREE_CODE (expr) == ADDR_EXPR)
6732 {
6733 if (complain & tf_error)
6734 {
6735 error ("%qE is not a valid template argument for type %qT "
6736 "because it is a pointer", expr, type);
6737 inform (input_location, "try using %qE instead",
6738 TREE_OPERAND (expr, 0));
6739 }
6740 return NULL_TREE;
6741 }
6742
6743 expr = convert_nontype_argument_function (type, expr, complain);
6744 if (!expr || expr == error_mark_node)
6745 return expr;
6746 }
6747 /* [temp.arg.nontype]/5, bullet 6
6748
6749 For a non-type template-parameter of type pointer to member function,
6750 no conversions apply. If the template-argument represents a set of
6751 overloaded member functions, the matching member function is selected
6752 from the set (_over.over_). */
6753 else if (TYPE_PTRMEMFUNC_P (type))
6754 {
6755 expr = instantiate_type (type, expr, tf_none);
6756 if (expr == error_mark_node)
6757 return error_mark_node;
6758
6759 /* [temp.arg.nontype] bullet 1 says the pointer to member
6760 expression must be a pointer-to-member constant. */
6761 if (!value_dependent_expression_p (expr)
6762 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6763 return error_mark_node;
6764
6765 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6766 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6767 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6768 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6769
6770 /* There is no way to disable standard conversions in
6771 resolve_address_of_overloaded_function (called by
6772 instantiate_type). It is possible that the call succeeded by
6773 converting &B::I to &D::I (where B is a base of D), so we need
6774 to reject this conversion here.
6775
6776 Actually, even if there was a way to disable standard conversions,
6777 it would still be better to reject them here so that we can
6778 provide a superior diagnostic. */
6779 if (!same_type_p (TREE_TYPE (expr), type))
6780 {
6781 if (complain & tf_error)
6782 {
6783 error ("%qE is not a valid template argument for type %qT "
6784 "because it is of type %qT", expr, type,
6785 TREE_TYPE (expr));
6786 /* If we are just one standard conversion off, explain. */
6787 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6788 inform (input_location,
6789 "standard conversions are not allowed in this context");
6790 }
6791 return NULL_TREE;
6792 }
6793 }
6794 /* [temp.arg.nontype]/5, bullet 7
6795
6796 For a non-type template-parameter of type pointer to data member,
6797 qualification conversions (_conv.qual_) are applied. */
6798 else if (TYPE_PTRDATAMEM_P (type))
6799 {
6800 /* [temp.arg.nontype] bullet 1 says the pointer to member
6801 expression must be a pointer-to-member constant. */
6802 if (!value_dependent_expression_p (expr)
6803 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6804 return error_mark_node;
6805
6806 expr = perform_qualification_conversions (type, expr);
6807 if (expr == error_mark_node)
6808 return expr;
6809 }
6810 else if (NULLPTR_TYPE_P (type))
6811 {
6812 if (expr != nullptr_node)
6813 {
6814 if (complain & tf_error)
6815 error ("%qE is not a valid template argument for type %qT "
6816 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6817 return NULL_TREE;
6818 }
6819 return expr;
6820 }
6821 /* A template non-type parameter must be one of the above. */
6822 else
6823 gcc_unreachable ();
6824
6825 /* Sanity check: did we actually convert the argument to the
6826 right type? */
6827 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6828 (type, TREE_TYPE (expr)));
6829 return convert_from_reference (expr);
6830 }
6831
6832 /* Subroutine of coerce_template_template_parms, which returns 1 if
6833 PARM_PARM and ARG_PARM match using the rule for the template
6834 parameters of template template parameters. Both PARM and ARG are
6835 template parameters; the rest of the arguments are the same as for
6836 coerce_template_template_parms.
6837 */
6838 static int
6839 coerce_template_template_parm (tree parm,
6840 tree arg,
6841 tsubst_flags_t complain,
6842 tree in_decl,
6843 tree outer_args)
6844 {
6845 if (arg == NULL_TREE || error_operand_p (arg)
6846 || parm == NULL_TREE || error_operand_p (parm))
6847 return 0;
6848
6849 if (TREE_CODE (arg) != TREE_CODE (parm))
6850 return 0;
6851
6852 switch (TREE_CODE (parm))
6853 {
6854 case TEMPLATE_DECL:
6855 /* We encounter instantiations of templates like
6856 template <template <template <class> class> class TT>
6857 class C; */
6858 {
6859 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6860 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6861
6862 if (!coerce_template_template_parms
6863 (parmparm, argparm, complain, in_decl, outer_args))
6864 return 0;
6865 }
6866 /* Fall through. */
6867
6868 case TYPE_DECL:
6869 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6870 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6871 /* Argument is a parameter pack but parameter is not. */
6872 return 0;
6873 break;
6874
6875 case PARM_DECL:
6876 /* The tsubst call is used to handle cases such as
6877
6878 template <int> class C {};
6879 template <class T, template <T> class TT> class D {};
6880 D<int, C> d;
6881
6882 i.e. the parameter list of TT depends on earlier parameters. */
6883 if (!uses_template_parms (TREE_TYPE (arg)))
6884 {
6885 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6886 if (!uses_template_parms (t)
6887 && !same_type_p (t, TREE_TYPE (arg)))
6888 return 0;
6889 }
6890
6891 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6892 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6893 /* Argument is a parameter pack but parameter is not. */
6894 return 0;
6895
6896 break;
6897
6898 default:
6899 gcc_unreachable ();
6900 }
6901
6902 return 1;
6903 }
6904
6905 /* Coerce template argument list ARGLIST for use with template
6906 template-parameter TEMPL. */
6907
6908 static tree
6909 coerce_template_args_for_ttp (tree templ, tree arglist,
6910 tsubst_flags_t complain)
6911 {
6912 /* Consider an example where a template template parameter declared as
6913
6914 template <class T, class U = std::allocator<T> > class TT
6915
6916 The template parameter level of T and U are one level larger than
6917 of TT. To proper process the default argument of U, say when an
6918 instantiation `TT<int>' is seen, we need to build the full
6919 arguments containing {int} as the innermost level. Outer levels,
6920 available when not appearing as default template argument, can be
6921 obtained from the arguments of the enclosing template.
6922
6923 Suppose that TT is later substituted with std::vector. The above
6924 instantiation is `TT<int, std::allocator<T> >' with TT at
6925 level 1, and T at level 2, while the template arguments at level 1
6926 becomes {std::vector} and the inner level 2 is {int}. */
6927
6928 tree outer = DECL_CONTEXT (templ);
6929 if (outer)
6930 {
6931 if (DECL_TEMPLATE_SPECIALIZATION (outer))
6932 /* We want arguments for the partial specialization, not arguments for
6933 the primary template. */
6934 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
6935 else
6936 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6937 }
6938 else if (current_template_parms)
6939 {
6940 /* This is an argument of the current template, so we haven't set
6941 DECL_CONTEXT yet. */
6942 tree relevant_template_parms;
6943
6944 /* Parameter levels that are greater than the level of the given
6945 template template parm are irrelevant. */
6946 relevant_template_parms = current_template_parms;
6947 while (TMPL_PARMS_DEPTH (relevant_template_parms)
6948 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
6949 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
6950
6951 outer = template_parms_to_args (relevant_template_parms);
6952 }
6953
6954 if (outer)
6955 arglist = add_to_template_args (outer, arglist);
6956
6957 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6958 return coerce_template_parms (parmlist, arglist, templ,
6959 complain,
6960 /*require_all_args=*/true,
6961 /*use_default_args=*/true);
6962 }
6963
6964 /* A cache of template template parameters with match-all default
6965 arguments. */
6966 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
6967 static void
6968 store_defaulted_ttp (tree v, tree t)
6969 {
6970 if (!defaulted_ttp_cache)
6971 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
6972 defaulted_ttp_cache->put (v, t);
6973 }
6974 static tree
6975 lookup_defaulted_ttp (tree v)
6976 {
6977 if (defaulted_ttp_cache)
6978 if (tree *p = defaulted_ttp_cache->get (v))
6979 return *p;
6980 return NULL_TREE;
6981 }
6982
6983 /* T is a bound template template-parameter. Copy its arguments into default
6984 arguments of the template template-parameter's template parameters. */
6985
6986 static tree
6987 add_defaults_to_ttp (tree otmpl)
6988 {
6989 if (tree c = lookup_defaulted_ttp (otmpl))
6990 return c;
6991
6992 tree ntmpl = copy_node (otmpl);
6993
6994 tree ntype = copy_node (TREE_TYPE (otmpl));
6995 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
6996 TYPE_MAIN_VARIANT (ntype) = ntype;
6997 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
6998 TYPE_NAME (ntype) = ntmpl;
6999 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7000
7001 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7002 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7003 TEMPLATE_PARM_DECL (idx) = ntmpl;
7004 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7005
7006 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7007 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7008 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7009 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7010 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7011 {
7012 tree o = TREE_VEC_ELT (vec, i);
7013 if (!template_parameter_pack_p (TREE_VALUE (o)))
7014 {
7015 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7016 TREE_PURPOSE (n) = any_targ_node;
7017 }
7018 }
7019
7020 store_defaulted_ttp (otmpl, ntmpl);
7021 return ntmpl;
7022 }
7023
7024 /* ARG is a bound potential template template-argument, and PARGS is a list
7025 of arguments for the corresponding template template-parameter. Adjust
7026 PARGS as appropriate for application to ARG's template, and if ARG is a
7027 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7028 arguments to the template template parameter. */
7029
7030 static tree
7031 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7032 {
7033 ++processing_template_decl;
7034 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7035 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7036 {
7037 /* When comparing two template template-parameters in partial ordering,
7038 rewrite the one currently being used as an argument to have default
7039 arguments for all parameters. */
7040 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7041 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7042 if (pargs != error_mark_node)
7043 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7044 TYPE_TI_ARGS (arg));
7045 }
7046 else
7047 {
7048 tree aparms
7049 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7050 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7051 /*require_all*/true,
7052 /*use_default*/true);
7053 }
7054 --processing_template_decl;
7055 return pargs;
7056 }
7057
7058 /* Subroutine of unify for the case when PARM is a
7059 BOUND_TEMPLATE_TEMPLATE_PARM. */
7060
7061 static int
7062 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7063 bool explain_p)
7064 {
7065 tree parmvec = TYPE_TI_ARGS (parm);
7066 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7067
7068 /* The template template parm might be variadic and the argument
7069 not, so flatten both argument lists. */
7070 parmvec = expand_template_argument_pack (parmvec);
7071 argvec = expand_template_argument_pack (argvec);
7072
7073 tree nparmvec = parmvec;
7074 if (flag_new_ttp)
7075 {
7076 /* In keeping with P0522R0, adjust P's template arguments
7077 to apply to A's template; then flatten it again. */
7078 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7079 nparmvec = expand_template_argument_pack (nparmvec);
7080 }
7081
7082 if (unify (tparms, targs, nparmvec, argvec,
7083 UNIFY_ALLOW_NONE, explain_p))
7084 return 1;
7085
7086 /* If the P0522 adjustment eliminated a pack expansion, deduce
7087 empty packs. */
7088 if (flag_new_ttp
7089 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7090 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7091 DEDUCE_EXACT, /*sub*/true, explain_p))
7092 return 1;
7093
7094 return 0;
7095 }
7096
7097 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7098 template template parameters. Both PARM_PARMS and ARG_PARMS are
7099 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7100 or PARM_DECL.
7101
7102 Consider the example:
7103 template <class T> class A;
7104 template<template <class U> class TT> class B;
7105
7106 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7107 the parameters to A, and OUTER_ARGS contains A. */
7108
7109 static int
7110 coerce_template_template_parms (tree parm_parms,
7111 tree arg_parms,
7112 tsubst_flags_t complain,
7113 tree in_decl,
7114 tree outer_args)
7115 {
7116 int nparms, nargs, i;
7117 tree parm, arg;
7118 int variadic_p = 0;
7119
7120 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7121 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7122
7123 nparms = TREE_VEC_LENGTH (parm_parms);
7124 nargs = TREE_VEC_LENGTH (arg_parms);
7125
7126 if (flag_new_ttp)
7127 {
7128 /* P0522R0: A template template-parameter P is at least as specialized as
7129 a template template-argument A if, given the following rewrite to two
7130 function templates, the function template corresponding to P is at
7131 least as specialized as the function template corresponding to A
7132 according to the partial ordering rules for function templates
7133 ([temp.func.order]). Given an invented class template X with the
7134 template parameter list of A (including default arguments):
7135
7136 * Each of the two function templates has the same template parameters,
7137 respectively, as P or A.
7138
7139 * Each function template has a single function parameter whose type is
7140 a specialization of X with template arguments corresponding to the
7141 template parameters from the respective function template where, for
7142 each template parameter PP in the template parameter list of the
7143 function template, a corresponding template argument AA is formed. If
7144 PP declares a parameter pack, then AA is the pack expansion
7145 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7146
7147 If the rewrite produces an invalid type, then P is not at least as
7148 specialized as A. */
7149
7150 /* So coerce P's args to apply to A's parms, and then deduce between A's
7151 args and the converted args. If that succeeds, A is at least as
7152 specialized as P, so they match.*/
7153 tree pargs = template_parms_level_to_args (parm_parms);
7154 ++processing_template_decl;
7155 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7156 /*require_all*/true, /*use_default*/true);
7157 --processing_template_decl;
7158 if (pargs != error_mark_node)
7159 {
7160 tree targs = make_tree_vec (nargs);
7161 tree aargs = template_parms_level_to_args (arg_parms);
7162 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7163 /*explain*/false))
7164 return 1;
7165 }
7166 }
7167
7168 /* Determine whether we have a parameter pack at the end of the
7169 template template parameter's template parameter list. */
7170 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7171 {
7172 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7173
7174 if (error_operand_p (parm))
7175 return 0;
7176
7177 switch (TREE_CODE (parm))
7178 {
7179 case TEMPLATE_DECL:
7180 case TYPE_DECL:
7181 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7182 variadic_p = 1;
7183 break;
7184
7185 case PARM_DECL:
7186 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7187 variadic_p = 1;
7188 break;
7189
7190 default:
7191 gcc_unreachable ();
7192 }
7193 }
7194
7195 if (nargs != nparms
7196 && !(variadic_p && nargs >= nparms - 1))
7197 return 0;
7198
7199 /* Check all of the template parameters except the parameter pack at
7200 the end (if any). */
7201 for (i = 0; i < nparms - variadic_p; ++i)
7202 {
7203 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7204 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7205 continue;
7206
7207 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7208 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7209
7210 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7211 outer_args))
7212 return 0;
7213
7214 }
7215
7216 if (variadic_p)
7217 {
7218 /* Check each of the template parameters in the template
7219 argument against the template parameter pack at the end of
7220 the template template parameter. */
7221 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7222 return 0;
7223
7224 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7225
7226 for (; i < nargs; ++i)
7227 {
7228 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7229 continue;
7230
7231 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7232
7233 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7234 outer_args))
7235 return 0;
7236 }
7237 }
7238
7239 return 1;
7240 }
7241
7242 /* Verifies that the deduced template arguments (in TARGS) for the
7243 template template parameters (in TPARMS) represent valid bindings,
7244 by comparing the template parameter list of each template argument
7245 to the template parameter list of its corresponding template
7246 template parameter, in accordance with DR150. This
7247 routine can only be called after all template arguments have been
7248 deduced. It will return TRUE if all of the template template
7249 parameter bindings are okay, FALSE otherwise. */
7250 bool
7251 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7252 {
7253 int i, ntparms = TREE_VEC_LENGTH (tparms);
7254 bool ret = true;
7255
7256 /* We're dealing with template parms in this process. */
7257 ++processing_template_decl;
7258
7259 targs = INNERMOST_TEMPLATE_ARGS (targs);
7260
7261 for (i = 0; i < ntparms; ++i)
7262 {
7263 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7264 tree targ = TREE_VEC_ELT (targs, i);
7265
7266 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7267 {
7268 tree packed_args = NULL_TREE;
7269 int idx, len = 1;
7270
7271 if (ARGUMENT_PACK_P (targ))
7272 {
7273 /* Look inside the argument pack. */
7274 packed_args = ARGUMENT_PACK_ARGS (targ);
7275 len = TREE_VEC_LENGTH (packed_args);
7276 }
7277
7278 for (idx = 0; idx < len; ++idx)
7279 {
7280 tree targ_parms = NULL_TREE;
7281
7282 if (packed_args)
7283 /* Extract the next argument from the argument
7284 pack. */
7285 targ = TREE_VEC_ELT (packed_args, idx);
7286
7287 if (PACK_EXPANSION_P (targ))
7288 /* Look at the pattern of the pack expansion. */
7289 targ = PACK_EXPANSION_PATTERN (targ);
7290
7291 /* Extract the template parameters from the template
7292 argument. */
7293 if (TREE_CODE (targ) == TEMPLATE_DECL)
7294 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7295 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7296 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7297
7298 /* Verify that we can coerce the template template
7299 parameters from the template argument to the template
7300 parameter. This requires an exact match. */
7301 if (targ_parms
7302 && !coerce_template_template_parms
7303 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7304 targ_parms,
7305 tf_none,
7306 tparm,
7307 targs))
7308 {
7309 ret = false;
7310 goto out;
7311 }
7312 }
7313 }
7314 }
7315
7316 out:
7317
7318 --processing_template_decl;
7319 return ret;
7320 }
7321
7322 /* Since type attributes aren't mangled, we need to strip them from
7323 template type arguments. */
7324
7325 static tree
7326 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7327 {
7328 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7329 return arg;
7330 bool removed_attributes = false;
7331 tree canon = strip_typedefs (arg, &removed_attributes);
7332 if (removed_attributes
7333 && (complain & tf_warning))
7334 warning (OPT_Wignored_attributes,
7335 "ignoring attributes on template argument %qT", arg);
7336 return canon;
7337 }
7338
7339 /* And from inside dependent non-type arguments like sizeof(Type). */
7340
7341 static tree
7342 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7343 {
7344 if (!arg || arg == error_mark_node)
7345 return arg;
7346 bool removed_attributes = false;
7347 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7348 if (removed_attributes
7349 && (complain & tf_warning))
7350 warning (OPT_Wignored_attributes,
7351 "ignoring attributes in template argument %qE", arg);
7352 return canon;
7353 }
7354
7355 // A template declaration can be substituted for a constrained
7356 // template template parameter only when the argument is more
7357 // constrained than the parameter.
7358 static bool
7359 is_compatible_template_arg (tree parm, tree arg)
7360 {
7361 tree parm_cons = get_constraints (parm);
7362
7363 /* For now, allow constrained template template arguments
7364 and unconstrained template template parameters. */
7365 if (parm_cons == NULL_TREE)
7366 return true;
7367
7368 tree arg_cons = get_constraints (arg);
7369
7370 // If the template parameter is constrained, we need to rewrite its
7371 // constraints in terms of the ARG's template parameters. This ensures
7372 // that all of the template parameter types will have the same depth.
7373 //
7374 // Note that this is only valid when coerce_template_template_parm is
7375 // true for the innermost template parameters of PARM and ARG. In other
7376 // words, because coercion is successful, this conversion will be valid.
7377 if (parm_cons)
7378 {
7379 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7380 parm_cons = tsubst_constraint_info (parm_cons,
7381 INNERMOST_TEMPLATE_ARGS (args),
7382 tf_none, NULL_TREE);
7383 if (parm_cons == error_mark_node)
7384 return false;
7385 }
7386
7387 return subsumes (parm_cons, arg_cons);
7388 }
7389
7390 // Convert a placeholder argument into a binding to the original
7391 // parameter. The original parameter is saved as the TREE_TYPE of
7392 // ARG.
7393 static inline tree
7394 convert_wildcard_argument (tree parm, tree arg)
7395 {
7396 TREE_TYPE (arg) = parm;
7397 return arg;
7398 }
7399
7400 /* Convert the indicated template ARG as necessary to match the
7401 indicated template PARM. Returns the converted ARG, or
7402 error_mark_node if the conversion was unsuccessful. Error and
7403 warning messages are issued under control of COMPLAIN. This
7404 conversion is for the Ith parameter in the parameter list. ARGS is
7405 the full set of template arguments deduced so far. */
7406
7407 static tree
7408 convert_template_argument (tree parm,
7409 tree arg,
7410 tree args,
7411 tsubst_flags_t complain,
7412 int i,
7413 tree in_decl)
7414 {
7415 tree orig_arg;
7416 tree val;
7417 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7418
7419 if (parm == error_mark_node)
7420 return error_mark_node;
7421
7422 /* Trivially convert placeholders. */
7423 if (TREE_CODE (arg) == WILDCARD_DECL)
7424 return convert_wildcard_argument (parm, arg);
7425
7426 if (arg == any_targ_node)
7427 return arg;
7428
7429 if (TREE_CODE (arg) == TREE_LIST
7430 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7431 {
7432 /* The template argument was the name of some
7433 member function. That's usually
7434 invalid, but static members are OK. In any
7435 case, grab the underlying fields/functions
7436 and issue an error later if required. */
7437 orig_arg = TREE_VALUE (arg);
7438 TREE_TYPE (arg) = unknown_type_node;
7439 }
7440
7441 orig_arg = arg;
7442
7443 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7444 requires_type = (TREE_CODE (parm) == TYPE_DECL
7445 || requires_tmpl_type);
7446
7447 /* When determining whether an argument pack expansion is a template,
7448 look at the pattern. */
7449 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7450 arg = PACK_EXPANSION_PATTERN (arg);
7451
7452 /* Deal with an injected-class-name used as a template template arg. */
7453 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7454 {
7455 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7456 if (TREE_CODE (t) == TEMPLATE_DECL)
7457 {
7458 if (cxx_dialect >= cxx11)
7459 /* OK under DR 1004. */;
7460 else if (complain & tf_warning_or_error)
7461 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7462 " used as template template argument", TYPE_NAME (arg));
7463 else if (flag_pedantic_errors)
7464 t = arg;
7465
7466 arg = t;
7467 }
7468 }
7469
7470 is_tmpl_type =
7471 ((TREE_CODE (arg) == TEMPLATE_DECL
7472 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7473 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7474 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7475 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7476
7477 if (is_tmpl_type
7478 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7479 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7480 arg = TYPE_STUB_DECL (arg);
7481
7482 is_type = TYPE_P (arg) || is_tmpl_type;
7483
7484 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7485 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7486 {
7487 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7488 {
7489 if (complain & tf_error)
7490 error ("invalid use of destructor %qE as a type", orig_arg);
7491 return error_mark_node;
7492 }
7493
7494 permerror (input_location,
7495 "to refer to a type member of a template parameter, "
7496 "use %<typename %E%>", orig_arg);
7497
7498 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7499 TREE_OPERAND (arg, 1),
7500 typename_type,
7501 complain);
7502 arg = orig_arg;
7503 is_type = 1;
7504 }
7505 if (is_type != requires_type)
7506 {
7507 if (in_decl)
7508 {
7509 if (complain & tf_error)
7510 {
7511 error ("type/value mismatch at argument %d in template "
7512 "parameter list for %qD",
7513 i + 1, in_decl);
7514 if (is_type)
7515 inform (input_location,
7516 " expected a constant of type %qT, got %qT",
7517 TREE_TYPE (parm),
7518 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7519 else if (requires_tmpl_type)
7520 inform (input_location,
7521 " expected a class template, got %qE", orig_arg);
7522 else
7523 inform (input_location,
7524 " expected a type, got %qE", orig_arg);
7525 }
7526 }
7527 return error_mark_node;
7528 }
7529 if (is_tmpl_type ^ requires_tmpl_type)
7530 {
7531 if (in_decl && (complain & tf_error))
7532 {
7533 error ("type/value mismatch at argument %d in template "
7534 "parameter list for %qD",
7535 i + 1, in_decl);
7536 if (is_tmpl_type)
7537 inform (input_location,
7538 " expected a type, got %qT", DECL_NAME (arg));
7539 else
7540 inform (input_location,
7541 " expected a class template, got %qT", orig_arg);
7542 }
7543 return error_mark_node;
7544 }
7545
7546 if (is_type)
7547 {
7548 if (requires_tmpl_type)
7549 {
7550 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7551 val = orig_arg;
7552 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7553 /* The number of argument required is not known yet.
7554 Just accept it for now. */
7555 val = TREE_TYPE (arg);
7556 else
7557 {
7558 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7559 tree argparm;
7560
7561 /* Strip alias templates that are equivalent to another
7562 template. */
7563 arg = get_underlying_template (arg);
7564 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7565
7566 if (coerce_template_template_parms (parmparm, argparm,
7567 complain, in_decl,
7568 args))
7569 {
7570 val = arg;
7571
7572 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7573 TEMPLATE_DECL. */
7574 if (val != error_mark_node)
7575 {
7576 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7577 val = TREE_TYPE (val);
7578 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7579 val = make_pack_expansion (val);
7580 }
7581 }
7582 else
7583 {
7584 if (in_decl && (complain & tf_error))
7585 {
7586 error ("type/value mismatch at argument %d in "
7587 "template parameter list for %qD",
7588 i + 1, in_decl);
7589 inform (input_location,
7590 " expected a template of type %qD, got %qT",
7591 parm, orig_arg);
7592 }
7593
7594 val = error_mark_node;
7595 }
7596
7597 // Check that the constraints are compatible before allowing the
7598 // substitution.
7599 if (val != error_mark_node)
7600 if (!is_compatible_template_arg (parm, arg))
7601 {
7602 if (in_decl && (complain & tf_error))
7603 {
7604 error ("constraint mismatch at argument %d in "
7605 "template parameter list for %qD",
7606 i + 1, in_decl);
7607 inform (input_location, " expected %qD but got %qD",
7608 parm, arg);
7609 }
7610 val = error_mark_node;
7611 }
7612 }
7613 }
7614 else
7615 val = orig_arg;
7616 /* We only form one instance of each template specialization.
7617 Therefore, if we use a non-canonical variant (i.e., a
7618 typedef), any future messages referring to the type will use
7619 the typedef, which is confusing if those future uses do not
7620 themselves also use the typedef. */
7621 if (TYPE_P (val))
7622 val = canonicalize_type_argument (val, complain);
7623 }
7624 else
7625 {
7626 tree t = TREE_TYPE (parm);
7627
7628 if (tree a = type_uses_auto (t))
7629 {
7630 if (ARGUMENT_PACK_P (orig_arg))
7631 /* There's nothing to check for an auto argument pack. */
7632 return orig_arg;
7633
7634 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7635 if (t == error_mark_node)
7636 return error_mark_node;
7637 }
7638 else
7639 t = tsubst (t, args, complain, in_decl);
7640
7641 if (invalid_nontype_parm_type_p (t, complain))
7642 return error_mark_node;
7643
7644 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7645 {
7646 if (same_type_p (t, TREE_TYPE (orig_arg)))
7647 val = orig_arg;
7648 else
7649 {
7650 /* Not sure if this is reachable, but it doesn't hurt
7651 to be robust. */
7652 error ("type mismatch in nontype parameter pack");
7653 val = error_mark_node;
7654 }
7655 }
7656 else if (!type_dependent_expression_p (orig_arg)
7657 && !uses_template_parms (t))
7658 /* We used to call digest_init here. However, digest_init
7659 will report errors, which we don't want when complain
7660 is zero. More importantly, digest_init will try too
7661 hard to convert things: for example, `0' should not be
7662 converted to pointer type at this point according to
7663 the standard. Accepting this is not merely an
7664 extension, since deciding whether or not these
7665 conversions can occur is part of determining which
7666 function template to call, or whether a given explicit
7667 argument specification is valid. */
7668 val = convert_nontype_argument (t, orig_arg, complain);
7669 else
7670 val = canonicalize_expr_argument (orig_arg, complain);
7671
7672 if (val == NULL_TREE)
7673 val = error_mark_node;
7674 else if (val == error_mark_node && (complain & tf_error))
7675 error ("could not convert template argument %qE from %qT to %qT",
7676 orig_arg, TREE_TYPE (orig_arg), t);
7677
7678 if (INDIRECT_REF_P (val))
7679 {
7680 /* Reject template arguments that are references to built-in
7681 functions with no library fallbacks. */
7682 const_tree inner = TREE_OPERAND (val, 0);
7683 const_tree innertype = TREE_TYPE (inner);
7684 if (innertype
7685 && TREE_CODE (innertype) == REFERENCE_TYPE
7686 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7687 && 0 < TREE_OPERAND_LENGTH (inner)
7688 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7689 return error_mark_node;
7690 }
7691
7692 if (TREE_CODE (val) == SCOPE_REF)
7693 {
7694 /* Strip typedefs from the SCOPE_REF. */
7695 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7696 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7697 complain);
7698 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7699 QUALIFIED_NAME_IS_TEMPLATE (val));
7700 }
7701 }
7702
7703 return val;
7704 }
7705
7706 /* Coerces the remaining template arguments in INNER_ARGS (from
7707 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7708 Returns the coerced argument pack. PARM_IDX is the position of this
7709 parameter in the template parameter list. ARGS is the original
7710 template argument list. */
7711 static tree
7712 coerce_template_parameter_pack (tree parms,
7713 int parm_idx,
7714 tree args,
7715 tree inner_args,
7716 int arg_idx,
7717 tree new_args,
7718 int* lost,
7719 tree in_decl,
7720 tsubst_flags_t complain)
7721 {
7722 tree parm = TREE_VEC_ELT (parms, parm_idx);
7723 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7724 tree packed_args;
7725 tree argument_pack;
7726 tree packed_parms = NULL_TREE;
7727
7728 if (arg_idx > nargs)
7729 arg_idx = nargs;
7730
7731 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7732 {
7733 /* When the template parameter is a non-type template parameter pack
7734 or template template parameter pack whose type or template
7735 parameters use parameter packs, we know exactly how many arguments
7736 we are looking for. Build a vector of the instantiated decls for
7737 these template parameters in PACKED_PARMS. */
7738 /* We can't use make_pack_expansion here because it would interpret a
7739 _DECL as a use rather than a declaration. */
7740 tree decl = TREE_VALUE (parm);
7741 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7742 SET_PACK_EXPANSION_PATTERN (exp, decl);
7743 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7744 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7745
7746 TREE_VEC_LENGTH (args)--;
7747 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7748 TREE_VEC_LENGTH (args)++;
7749
7750 if (packed_parms == error_mark_node)
7751 return error_mark_node;
7752
7753 /* If we're doing a partial instantiation of a member template,
7754 verify that all of the types used for the non-type
7755 template parameter pack are, in fact, valid for non-type
7756 template parameters. */
7757 if (arg_idx < nargs
7758 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7759 {
7760 int j, len = TREE_VEC_LENGTH (packed_parms);
7761 for (j = 0; j < len; ++j)
7762 {
7763 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7764 if (invalid_nontype_parm_type_p (t, complain))
7765 return error_mark_node;
7766 }
7767 /* We don't know how many args we have yet, just
7768 use the unconverted ones for now. */
7769 return NULL_TREE;
7770 }
7771
7772 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7773 }
7774 /* Check if we have a placeholder pack, which indicates we're
7775 in the context of a introduction list. In that case we want
7776 to match this pack to the single placeholder. */
7777 else if (arg_idx < nargs
7778 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7779 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7780 {
7781 nargs = arg_idx + 1;
7782 packed_args = make_tree_vec (1);
7783 }
7784 else
7785 packed_args = make_tree_vec (nargs - arg_idx);
7786
7787 /* Convert the remaining arguments, which will be a part of the
7788 parameter pack "parm". */
7789 int first_pack_arg = arg_idx;
7790 for (; arg_idx < nargs; ++arg_idx)
7791 {
7792 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7793 tree actual_parm = TREE_VALUE (parm);
7794 int pack_idx = arg_idx - first_pack_arg;
7795
7796 if (packed_parms)
7797 {
7798 /* Once we've packed as many args as we have types, stop. */
7799 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7800 break;
7801 else if (PACK_EXPANSION_P (arg))
7802 /* We don't know how many args we have yet, just
7803 use the unconverted ones for now. */
7804 return NULL_TREE;
7805 else
7806 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7807 }
7808
7809 if (arg == error_mark_node)
7810 {
7811 if (complain & tf_error)
7812 error ("template argument %d is invalid", arg_idx + 1);
7813 }
7814 else
7815 arg = convert_template_argument (actual_parm,
7816 arg, new_args, complain, parm_idx,
7817 in_decl);
7818 if (arg == error_mark_node)
7819 (*lost)++;
7820 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7821 }
7822
7823 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7824 && TREE_VEC_LENGTH (packed_args) > 0)
7825 {
7826 if (complain & tf_error)
7827 error ("wrong number of template arguments (%d, should be %d)",
7828 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7829 return error_mark_node;
7830 }
7831
7832 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7833 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7834 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7835 else
7836 {
7837 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7838 TREE_TYPE (argument_pack)
7839 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7840 TREE_CONSTANT (argument_pack) = 1;
7841 }
7842
7843 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7844 if (CHECKING_P)
7845 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7846 TREE_VEC_LENGTH (packed_args));
7847 return argument_pack;
7848 }
7849
7850 /* Returns the number of pack expansions in the template argument vector
7851 ARGS. */
7852
7853 static int
7854 pack_expansion_args_count (tree args)
7855 {
7856 int i;
7857 int count = 0;
7858 if (args)
7859 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7860 {
7861 tree elt = TREE_VEC_ELT (args, i);
7862 if (elt && PACK_EXPANSION_P (elt))
7863 ++count;
7864 }
7865 return count;
7866 }
7867
7868 /* Convert all template arguments to their appropriate types, and
7869 return a vector containing the innermost resulting template
7870 arguments. If any error occurs, return error_mark_node. Error and
7871 warning messages are issued under control of COMPLAIN.
7872
7873 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7874 for arguments not specified in ARGS. Otherwise, if
7875 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7876 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7877 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7878 ARGS. */
7879
7880 static tree
7881 coerce_template_parms (tree parms,
7882 tree args,
7883 tree in_decl,
7884 tsubst_flags_t complain,
7885 bool require_all_args,
7886 bool use_default_args)
7887 {
7888 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7889 tree orig_inner_args;
7890 tree inner_args;
7891 tree new_args;
7892 tree new_inner_args;
7893 int saved_unevaluated_operand;
7894 int saved_inhibit_evaluation_warnings;
7895
7896 /* When used as a boolean value, indicates whether this is a
7897 variadic template parameter list. Since it's an int, we can also
7898 subtract it from nparms to get the number of non-variadic
7899 parameters. */
7900 int variadic_p = 0;
7901 int variadic_args_p = 0;
7902 int post_variadic_parms = 0;
7903
7904 /* Likewise for parameters with default arguments. */
7905 int default_p = 0;
7906
7907 if (args == error_mark_node)
7908 return error_mark_node;
7909
7910 nparms = TREE_VEC_LENGTH (parms);
7911
7912 /* Determine if there are any parameter packs or default arguments. */
7913 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7914 {
7915 tree parm = TREE_VEC_ELT (parms, parm_idx);
7916 if (variadic_p)
7917 ++post_variadic_parms;
7918 if (template_parameter_pack_p (TREE_VALUE (parm)))
7919 ++variadic_p;
7920 if (TREE_PURPOSE (parm))
7921 ++default_p;
7922 }
7923
7924 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7925 /* If there are no parameters that follow a parameter pack, we need to
7926 expand any argument packs so that we can deduce a parameter pack from
7927 some non-packed args followed by an argument pack, as in variadic85.C.
7928 If there are such parameters, we need to leave argument packs intact
7929 so the arguments are assigned properly. This can happen when dealing
7930 with a nested class inside a partial specialization of a class
7931 template, as in variadic92.C, or when deducing a template parameter pack
7932 from a sub-declarator, as in variadic114.C. */
7933 if (!post_variadic_parms)
7934 inner_args = expand_template_argument_pack (inner_args);
7935
7936 /* Count any pack expansion args. */
7937 variadic_args_p = pack_expansion_args_count (inner_args);
7938
7939 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7940 if ((nargs - variadic_args_p > nparms && !variadic_p)
7941 || (nargs < nparms - variadic_p
7942 && require_all_args
7943 && !variadic_args_p
7944 && (!use_default_args
7945 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7946 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7947 {
7948 if (complain & tf_error)
7949 {
7950 if (variadic_p || default_p)
7951 {
7952 nparms -= variadic_p + default_p;
7953 error ("wrong number of template arguments "
7954 "(%d, should be at least %d)", nargs, nparms);
7955 }
7956 else
7957 error ("wrong number of template arguments "
7958 "(%d, should be %d)", nargs, nparms);
7959
7960 if (in_decl)
7961 inform (DECL_SOURCE_LOCATION (in_decl),
7962 "provided for %qD", in_decl);
7963 }
7964
7965 return error_mark_node;
7966 }
7967 /* We can't pass a pack expansion to a non-pack parameter of an alias
7968 template (DR 1430). */
7969 else if (in_decl
7970 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7971 || concept_template_p (in_decl))
7972 && variadic_args_p
7973 && nargs - variadic_args_p < nparms - variadic_p)
7974 {
7975 if (complain & tf_error)
7976 {
7977 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7978 {
7979 tree arg = TREE_VEC_ELT (inner_args, i);
7980 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7981
7982 if (PACK_EXPANSION_P (arg)
7983 && !template_parameter_pack_p (parm))
7984 {
7985 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7986 error_at (location_of (arg),
7987 "pack expansion argument for non-pack parameter "
7988 "%qD of alias template %qD", parm, in_decl);
7989 else
7990 error_at (location_of (arg),
7991 "pack expansion argument for non-pack parameter "
7992 "%qD of concept %qD", parm, in_decl);
7993 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7994 goto found;
7995 }
7996 }
7997 gcc_unreachable ();
7998 found:;
7999 }
8000 return error_mark_node;
8001 }
8002
8003 /* We need to evaluate the template arguments, even though this
8004 template-id may be nested within a "sizeof". */
8005 saved_unevaluated_operand = cp_unevaluated_operand;
8006 cp_unevaluated_operand = 0;
8007 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8008 c_inhibit_evaluation_warnings = 0;
8009 new_inner_args = make_tree_vec (nparms);
8010 new_args = add_outermost_template_args (args, new_inner_args);
8011 int pack_adjust = 0;
8012 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8013 {
8014 tree arg;
8015 tree parm;
8016
8017 /* Get the Ith template parameter. */
8018 parm = TREE_VEC_ELT (parms, parm_idx);
8019
8020 if (parm == error_mark_node)
8021 {
8022 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8023 continue;
8024 }
8025
8026 /* Calculate the next argument. */
8027 if (arg_idx < nargs)
8028 arg = TREE_VEC_ELT (inner_args, arg_idx);
8029 else
8030 arg = NULL_TREE;
8031
8032 if (template_parameter_pack_p (TREE_VALUE (parm))
8033 && !(arg && ARGUMENT_PACK_P (arg)))
8034 {
8035 /* Some arguments will be placed in the
8036 template parameter pack PARM. */
8037 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8038 inner_args, arg_idx,
8039 new_args, &lost,
8040 in_decl, complain);
8041
8042 if (arg == NULL_TREE)
8043 {
8044 /* We don't know how many args we have yet, just use the
8045 unconverted (and still packed) ones for now. */
8046 new_inner_args = orig_inner_args;
8047 arg_idx = nargs;
8048 break;
8049 }
8050
8051 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8052
8053 /* Store this argument. */
8054 if (arg == error_mark_node)
8055 {
8056 lost++;
8057 /* We are done with all of the arguments. */
8058 arg_idx = nargs;
8059 }
8060 else
8061 {
8062 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8063 arg_idx += pack_adjust;
8064 }
8065
8066 continue;
8067 }
8068 else if (arg)
8069 {
8070 if (PACK_EXPANSION_P (arg))
8071 {
8072 /* "If every valid specialization of a variadic template
8073 requires an empty template parameter pack, the template is
8074 ill-formed, no diagnostic required." So check that the
8075 pattern works with this parameter. */
8076 tree pattern = PACK_EXPANSION_PATTERN (arg);
8077 tree conv = convert_template_argument (TREE_VALUE (parm),
8078 pattern, new_args,
8079 complain, parm_idx,
8080 in_decl);
8081 if (conv == error_mark_node)
8082 {
8083 if (complain & tf_error)
8084 inform (input_location, "so any instantiation with a "
8085 "non-empty parameter pack would be ill-formed");
8086 ++lost;
8087 }
8088 else if (TYPE_P (conv) && !TYPE_P (pattern))
8089 /* Recover from missing typename. */
8090 TREE_VEC_ELT (inner_args, arg_idx)
8091 = make_pack_expansion (conv);
8092
8093 /* We don't know how many args we have yet, just
8094 use the unconverted ones for now. */
8095 new_inner_args = inner_args;
8096 arg_idx = nargs;
8097 break;
8098 }
8099 }
8100 else if (require_all_args)
8101 {
8102 /* There must be a default arg in this case. */
8103 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8104 complain, in_decl);
8105 /* The position of the first default template argument,
8106 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8107 Record that. */
8108 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8109 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8110 arg_idx - pack_adjust);
8111 }
8112 else
8113 break;
8114
8115 if (arg == error_mark_node)
8116 {
8117 if (complain & tf_error)
8118 error ("template argument %d is invalid", arg_idx + 1);
8119 }
8120 else if (!arg)
8121 /* This only occurs if there was an error in the template
8122 parameter list itself (which we would already have
8123 reported) that we are trying to recover from, e.g., a class
8124 template with a parameter list such as
8125 template<typename..., typename>. */
8126 ++lost;
8127 else
8128 arg = convert_template_argument (TREE_VALUE (parm),
8129 arg, new_args, complain,
8130 parm_idx, in_decl);
8131
8132 if (arg == error_mark_node)
8133 lost++;
8134 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8135 }
8136 cp_unevaluated_operand = saved_unevaluated_operand;
8137 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8138
8139 if (variadic_p && arg_idx < nargs)
8140 {
8141 if (complain & tf_error)
8142 {
8143 error ("wrong number of template arguments "
8144 "(%d, should be %d)", nargs, arg_idx);
8145 if (in_decl)
8146 error ("provided for %q+D", in_decl);
8147 }
8148 return error_mark_node;
8149 }
8150
8151 if (lost)
8152 return error_mark_node;
8153
8154 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8155 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8156 TREE_VEC_LENGTH (new_inner_args));
8157
8158 return new_inner_args;
8159 }
8160
8161 /* Convert all template arguments to their appropriate types, and
8162 return a vector containing the innermost resulting template
8163 arguments. If any error occurs, return error_mark_node. Error and
8164 warning messages are not issued.
8165
8166 Note that no function argument deduction is performed, and default
8167 arguments are used to fill in unspecified arguments. */
8168 tree
8169 coerce_template_parms (tree parms, tree args, tree in_decl)
8170 {
8171 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8172 }
8173
8174 /* Convert all template arguments to their appropriate type, and
8175 instantiate default arguments as needed. This returns a vector
8176 containing the innermost resulting template arguments, or
8177 error_mark_node if unsuccessful. */
8178 tree
8179 coerce_template_parms (tree parms, tree args, tree in_decl,
8180 tsubst_flags_t complain)
8181 {
8182 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8183 }
8184
8185 /* Like coerce_template_parms. If PARMS represents all template
8186 parameters levels, this function returns a vector of vectors
8187 representing all the resulting argument levels. Note that in this
8188 case, only the innermost arguments are coerced because the
8189 outermost ones are supposed to have been coerced already.
8190
8191 Otherwise, if PARMS represents only (the innermost) vector of
8192 parameters, this function returns a vector containing just the
8193 innermost resulting arguments. */
8194
8195 static tree
8196 coerce_innermost_template_parms (tree parms,
8197 tree args,
8198 tree in_decl,
8199 tsubst_flags_t complain,
8200 bool require_all_args,
8201 bool use_default_args)
8202 {
8203 int parms_depth = TMPL_PARMS_DEPTH (parms);
8204 int args_depth = TMPL_ARGS_DEPTH (args);
8205 tree coerced_args;
8206
8207 if (parms_depth > 1)
8208 {
8209 coerced_args = make_tree_vec (parms_depth);
8210 tree level;
8211 int cur_depth;
8212
8213 for (level = parms, cur_depth = parms_depth;
8214 parms_depth > 0 && level != NULL_TREE;
8215 level = TREE_CHAIN (level), --cur_depth)
8216 {
8217 tree l;
8218 if (cur_depth == args_depth)
8219 l = coerce_template_parms (TREE_VALUE (level),
8220 args, in_decl, complain,
8221 require_all_args,
8222 use_default_args);
8223 else
8224 l = TMPL_ARGS_LEVEL (args, cur_depth);
8225
8226 if (l == error_mark_node)
8227 return error_mark_node;
8228
8229 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8230 }
8231 }
8232 else
8233 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8234 args, in_decl, complain,
8235 require_all_args,
8236 use_default_args);
8237 return coerced_args;
8238 }
8239
8240 /* Returns 1 if template args OT and NT are equivalent. */
8241
8242 int
8243 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8244 {
8245 if (nt == ot)
8246 return 1;
8247 if (nt == NULL_TREE || ot == NULL_TREE)
8248 return false;
8249 if (nt == any_targ_node || ot == any_targ_node)
8250 return true;
8251
8252 if (TREE_CODE (nt) == TREE_VEC)
8253 /* For member templates */
8254 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8255 else if (PACK_EXPANSION_P (ot))
8256 return (PACK_EXPANSION_P (nt)
8257 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8258 PACK_EXPANSION_PATTERN (nt))
8259 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8260 PACK_EXPANSION_EXTRA_ARGS (nt)));
8261 else if (ARGUMENT_PACK_P (ot))
8262 {
8263 int i, len;
8264 tree opack, npack;
8265
8266 if (!ARGUMENT_PACK_P (nt))
8267 return 0;
8268
8269 opack = ARGUMENT_PACK_ARGS (ot);
8270 npack = ARGUMENT_PACK_ARGS (nt);
8271 len = TREE_VEC_LENGTH (opack);
8272 if (TREE_VEC_LENGTH (npack) != len)
8273 return 0;
8274 for (i = 0; i < len; ++i)
8275 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8276 TREE_VEC_ELT (npack, i)))
8277 return 0;
8278 return 1;
8279 }
8280 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8281 gcc_unreachable ();
8282 else if (TYPE_P (nt))
8283 {
8284 if (!TYPE_P (ot))
8285 return false;
8286 /* Don't treat an alias template specialization with dependent
8287 arguments as equivalent to its underlying type when used as a
8288 template argument; we need them to be distinct so that we
8289 substitute into the specialization arguments at instantiation
8290 time. And aliases can't be equivalent without being ==, so
8291 we don't need to look any deeper.
8292
8293 During partial ordering, however, we need to treat them normally so
8294 that we can order uses of the same alias with different
8295 cv-qualification (79960). */
8296 if (!partial_order
8297 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8298 return false;
8299 else
8300 return same_type_p (ot, nt);
8301 }
8302 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8303 return 0;
8304 else
8305 {
8306 /* Try to treat a template non-type argument that has been converted
8307 to the parameter type as equivalent to one that hasn't yet. */
8308 for (enum tree_code code1 = TREE_CODE (ot);
8309 CONVERT_EXPR_CODE_P (code1)
8310 || code1 == NON_LVALUE_EXPR;
8311 code1 = TREE_CODE (ot))
8312 ot = TREE_OPERAND (ot, 0);
8313 for (enum tree_code code2 = TREE_CODE (nt);
8314 CONVERT_EXPR_CODE_P (code2)
8315 || code2 == NON_LVALUE_EXPR;
8316 code2 = TREE_CODE (nt))
8317 nt = TREE_OPERAND (nt, 0);
8318
8319 return cp_tree_equal (ot, nt);
8320 }
8321 }
8322
8323 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8324 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8325 NEWARG_PTR with the offending arguments if they are non-NULL. */
8326
8327 int
8328 comp_template_args (tree oldargs, tree newargs,
8329 tree *oldarg_ptr, tree *newarg_ptr,
8330 bool partial_order)
8331 {
8332 int i;
8333
8334 if (oldargs == newargs)
8335 return 1;
8336
8337 if (!oldargs || !newargs)
8338 return 0;
8339
8340 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8341 return 0;
8342
8343 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8344 {
8345 tree nt = TREE_VEC_ELT (newargs, i);
8346 tree ot = TREE_VEC_ELT (oldargs, i);
8347
8348 if (! template_args_equal (ot, nt, partial_order))
8349 {
8350 if (oldarg_ptr != NULL)
8351 *oldarg_ptr = ot;
8352 if (newarg_ptr != NULL)
8353 *newarg_ptr = nt;
8354 return 0;
8355 }
8356 }
8357 return 1;
8358 }
8359
8360 inline bool
8361 comp_template_args_porder (tree oargs, tree nargs)
8362 {
8363 return comp_template_args (oargs, nargs, NULL, NULL, true);
8364 }
8365
8366 static void
8367 add_pending_template (tree d)
8368 {
8369 tree ti = (TYPE_P (d)
8370 ? CLASSTYPE_TEMPLATE_INFO (d)
8371 : DECL_TEMPLATE_INFO (d));
8372 struct pending_template *pt;
8373 int level;
8374
8375 if (TI_PENDING_TEMPLATE_FLAG (ti))
8376 return;
8377
8378 /* We are called both from instantiate_decl, where we've already had a
8379 tinst_level pushed, and instantiate_template, where we haven't.
8380 Compensate. */
8381 level = !current_tinst_level || current_tinst_level->decl != d;
8382
8383 if (level)
8384 push_tinst_level (d);
8385
8386 pt = ggc_alloc<pending_template> ();
8387 pt->next = NULL;
8388 pt->tinst = current_tinst_level;
8389 if (last_pending_template)
8390 last_pending_template->next = pt;
8391 else
8392 pending_templates = pt;
8393
8394 last_pending_template = pt;
8395
8396 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8397
8398 if (level)
8399 pop_tinst_level ();
8400 }
8401
8402
8403 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8404 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8405 documentation for TEMPLATE_ID_EXPR. */
8406
8407 tree
8408 lookup_template_function (tree fns, tree arglist)
8409 {
8410 tree type;
8411
8412 if (fns == error_mark_node || arglist == error_mark_node)
8413 return error_mark_node;
8414
8415 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8416
8417 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8418 {
8419 error ("%q#D is not a function template", fns);
8420 return error_mark_node;
8421 }
8422
8423 if (BASELINK_P (fns))
8424 {
8425 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8426 unknown_type_node,
8427 BASELINK_FUNCTIONS (fns),
8428 arglist);
8429 return fns;
8430 }
8431
8432 type = TREE_TYPE (fns);
8433 if (TREE_CODE (fns) == OVERLOAD || !type)
8434 type = unknown_type_node;
8435
8436 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8437 }
8438
8439 /* Within the scope of a template class S<T>, the name S gets bound
8440 (in build_self_reference) to a TYPE_DECL for the class, not a
8441 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8442 or one of its enclosing classes, and that type is a template,
8443 return the associated TEMPLATE_DECL. Otherwise, the original
8444 DECL is returned.
8445
8446 Also handle the case when DECL is a TREE_LIST of ambiguous
8447 injected-class-names from different bases. */
8448
8449 tree
8450 maybe_get_template_decl_from_type_decl (tree decl)
8451 {
8452 if (decl == NULL_TREE)
8453 return decl;
8454
8455 /* DR 176: A lookup that finds an injected-class-name (10.2
8456 [class.member.lookup]) can result in an ambiguity in certain cases
8457 (for example, if it is found in more than one base class). If all of
8458 the injected-class-names that are found refer to specializations of
8459 the same class template, and if the name is followed by a
8460 template-argument-list, the reference refers to the class template
8461 itself and not a specialization thereof, and is not ambiguous. */
8462 if (TREE_CODE (decl) == TREE_LIST)
8463 {
8464 tree t, tmpl = NULL_TREE;
8465 for (t = decl; t; t = TREE_CHAIN (t))
8466 {
8467 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8468 if (!tmpl)
8469 tmpl = elt;
8470 else if (tmpl != elt)
8471 break;
8472 }
8473 if (tmpl && t == NULL_TREE)
8474 return tmpl;
8475 else
8476 return decl;
8477 }
8478
8479 return (decl != NULL_TREE
8480 && DECL_SELF_REFERENCE_P (decl)
8481 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8482 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8483 }
8484
8485 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8486 parameters, find the desired type.
8487
8488 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8489
8490 IN_DECL, if non-NULL, is the template declaration we are trying to
8491 instantiate.
8492
8493 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8494 the class we are looking up.
8495
8496 Issue error and warning messages under control of COMPLAIN.
8497
8498 If the template class is really a local class in a template
8499 function, then the FUNCTION_CONTEXT is the function in which it is
8500 being instantiated.
8501
8502 ??? Note that this function is currently called *twice* for each
8503 template-id: the first time from the parser, while creating the
8504 incomplete type (finish_template_type), and the second type during the
8505 real instantiation (instantiate_template_class). This is surely something
8506 that we want to avoid. It also causes some problems with argument
8507 coercion (see convert_nontype_argument for more information on this). */
8508
8509 static tree
8510 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8511 int entering_scope, tsubst_flags_t complain)
8512 {
8513 tree templ = NULL_TREE, parmlist;
8514 tree t;
8515 spec_entry **slot;
8516 spec_entry *entry;
8517 spec_entry elt;
8518 hashval_t hash;
8519
8520 if (identifier_p (d1))
8521 {
8522 tree value = innermost_non_namespace_value (d1);
8523 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8524 templ = value;
8525 else
8526 {
8527 if (context)
8528 push_decl_namespace (context);
8529 templ = lookup_name (d1);
8530 templ = maybe_get_template_decl_from_type_decl (templ);
8531 if (context)
8532 pop_decl_namespace ();
8533 }
8534 if (templ)
8535 context = DECL_CONTEXT (templ);
8536 }
8537 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8538 {
8539 tree type = TREE_TYPE (d1);
8540
8541 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8542 an implicit typename for the second A. Deal with it. */
8543 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8544 type = TREE_TYPE (type);
8545
8546 if (CLASSTYPE_TEMPLATE_INFO (type))
8547 {
8548 templ = CLASSTYPE_TI_TEMPLATE (type);
8549 d1 = DECL_NAME (templ);
8550 }
8551 }
8552 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8553 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8554 {
8555 templ = TYPE_TI_TEMPLATE (d1);
8556 d1 = DECL_NAME (templ);
8557 }
8558 else if (DECL_TYPE_TEMPLATE_P (d1))
8559 {
8560 templ = d1;
8561 d1 = DECL_NAME (templ);
8562 context = DECL_CONTEXT (templ);
8563 }
8564 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8565 {
8566 templ = d1;
8567 d1 = DECL_NAME (templ);
8568 }
8569
8570 /* Issue an error message if we didn't find a template. */
8571 if (! templ)
8572 {
8573 if (complain & tf_error)
8574 error ("%qT is not a template", d1);
8575 return error_mark_node;
8576 }
8577
8578 if (TREE_CODE (templ) != TEMPLATE_DECL
8579 /* Make sure it's a user visible template, if it was named by
8580 the user. */
8581 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8582 && !PRIMARY_TEMPLATE_P (templ)))
8583 {
8584 if (complain & tf_error)
8585 {
8586 error ("non-template type %qT used as a template", d1);
8587 if (in_decl)
8588 error ("for template declaration %q+D", in_decl);
8589 }
8590 return error_mark_node;
8591 }
8592
8593 complain &= ~tf_user;
8594
8595 /* An alias that just changes the name of a template is equivalent to the
8596 other template, so if any of the arguments are pack expansions, strip
8597 the alias to avoid problems with a pack expansion passed to a non-pack
8598 alias template parameter (DR 1430). */
8599 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8600 templ = get_underlying_template (templ);
8601
8602 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8603 {
8604 tree parm;
8605 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8606 if (arglist2 == error_mark_node
8607 || (!uses_template_parms (arglist2)
8608 && check_instantiated_args (templ, arglist2, complain)))
8609 return error_mark_node;
8610
8611 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8612 return parm;
8613 }
8614 else
8615 {
8616 tree template_type = TREE_TYPE (templ);
8617 tree gen_tmpl;
8618 tree type_decl;
8619 tree found = NULL_TREE;
8620 int arg_depth;
8621 int parm_depth;
8622 int is_dependent_type;
8623 int use_partial_inst_tmpl = false;
8624
8625 if (template_type == error_mark_node)
8626 /* An error occurred while building the template TEMPL, and a
8627 diagnostic has most certainly been emitted for that
8628 already. Let's propagate that error. */
8629 return error_mark_node;
8630
8631 gen_tmpl = most_general_template (templ);
8632 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8633 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8634 arg_depth = TMPL_ARGS_DEPTH (arglist);
8635
8636 if (arg_depth == 1 && parm_depth > 1)
8637 {
8638 /* We've been given an incomplete set of template arguments.
8639 For example, given:
8640
8641 template <class T> struct S1 {
8642 template <class U> struct S2 {};
8643 template <class U> struct S2<U*> {};
8644 };
8645
8646 we will be called with an ARGLIST of `U*', but the
8647 TEMPLATE will be `template <class T> template
8648 <class U> struct S1<T>::S2'. We must fill in the missing
8649 arguments. */
8650 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8651 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8652 arg_depth = TMPL_ARGS_DEPTH (arglist);
8653 }
8654
8655 /* Now we should have enough arguments. */
8656 gcc_assert (parm_depth == arg_depth);
8657
8658 /* From here on, we're only interested in the most general
8659 template. */
8660
8661 /* Calculate the BOUND_ARGS. These will be the args that are
8662 actually tsubst'd into the definition to create the
8663 instantiation. */
8664 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8665 complain,
8666 /*require_all_args=*/true,
8667 /*use_default_args=*/true);
8668
8669 if (arglist == error_mark_node)
8670 /* We were unable to bind the arguments. */
8671 return error_mark_node;
8672
8673 /* In the scope of a template class, explicit references to the
8674 template class refer to the type of the template, not any
8675 instantiation of it. For example, in:
8676
8677 template <class T> class C { void f(C<T>); }
8678
8679 the `C<T>' is just the same as `C'. Outside of the
8680 class, however, such a reference is an instantiation. */
8681 if (entering_scope
8682 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8683 || currently_open_class (template_type))
8684 {
8685 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8686
8687 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8688 return template_type;
8689 }
8690
8691 /* If we already have this specialization, return it. */
8692 elt.tmpl = gen_tmpl;
8693 elt.args = arglist;
8694 elt.spec = NULL_TREE;
8695 hash = spec_hasher::hash (&elt);
8696 entry = type_specializations->find_with_hash (&elt, hash);
8697
8698 if (entry)
8699 return entry->spec;
8700
8701 /* If the the template's constraints are not satisfied,
8702 then we cannot form a valid type.
8703
8704 Note that the check is deferred until after the hash
8705 lookup. This prevents redundant checks on previously
8706 instantiated specializations. */
8707 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8708 {
8709 if (complain & tf_error)
8710 {
8711 error ("template constraint failure");
8712 diagnose_constraints (input_location, gen_tmpl, arglist);
8713 }
8714 return error_mark_node;
8715 }
8716
8717 is_dependent_type = uses_template_parms (arglist);
8718
8719 /* If the deduced arguments are invalid, then the binding
8720 failed. */
8721 if (!is_dependent_type
8722 && check_instantiated_args (gen_tmpl,
8723 INNERMOST_TEMPLATE_ARGS (arglist),
8724 complain))
8725 return error_mark_node;
8726
8727 if (!is_dependent_type
8728 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8729 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8730 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8731 {
8732 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8733 DECL_NAME (gen_tmpl),
8734 /*tag_scope=*/ts_global);
8735 return found;
8736 }
8737
8738 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8739 complain, in_decl);
8740 if (context == error_mark_node)
8741 return error_mark_node;
8742
8743 if (!context)
8744 context = global_namespace;
8745
8746 /* Create the type. */
8747 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8748 {
8749 /* The user referred to a specialization of an alias
8750 template represented by GEN_TMPL.
8751
8752 [temp.alias]/2 says:
8753
8754 When a template-id refers to the specialization of an
8755 alias template, it is equivalent to the associated
8756 type obtained by substitution of its
8757 template-arguments for the template-parameters in the
8758 type-id of the alias template. */
8759
8760 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8761 /* Note that the call above (by indirectly calling
8762 register_specialization in tsubst_decl) registers the
8763 TYPE_DECL representing the specialization of the alias
8764 template. So next time someone substitutes ARGLIST for
8765 the template parms into the alias template (GEN_TMPL),
8766 she'll get that TYPE_DECL back. */
8767
8768 if (t == error_mark_node)
8769 return t;
8770 }
8771 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8772 {
8773 if (!is_dependent_type)
8774 {
8775 set_current_access_from_decl (TYPE_NAME (template_type));
8776 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8777 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8778 arglist, complain, in_decl),
8779 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8780 arglist, complain, in_decl),
8781 SCOPED_ENUM_P (template_type), NULL);
8782
8783 if (t == error_mark_node)
8784 return t;
8785 }
8786 else
8787 {
8788 /* We don't want to call start_enum for this type, since
8789 the values for the enumeration constants may involve
8790 template parameters. And, no one should be interested
8791 in the enumeration constants for such a type. */
8792 t = cxx_make_type (ENUMERAL_TYPE);
8793 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8794 }
8795 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8796 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8797 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8798 }
8799 else if (CLASS_TYPE_P (template_type))
8800 {
8801 t = make_class_type (TREE_CODE (template_type));
8802 CLASSTYPE_DECLARED_CLASS (t)
8803 = CLASSTYPE_DECLARED_CLASS (template_type);
8804 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8805
8806 /* A local class. Make sure the decl gets registered properly. */
8807 if (context == current_function_decl)
8808 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8809
8810 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8811 /* This instantiation is another name for the primary
8812 template type. Set the TYPE_CANONICAL field
8813 appropriately. */
8814 TYPE_CANONICAL (t) = template_type;
8815 else if (any_template_arguments_need_structural_equality_p (arglist))
8816 /* Some of the template arguments require structural
8817 equality testing, so this template class requires
8818 structural equality testing. */
8819 SET_TYPE_STRUCTURAL_EQUALITY (t);
8820 }
8821 else
8822 gcc_unreachable ();
8823
8824 /* If we called start_enum or pushtag above, this information
8825 will already be set up. */
8826 if (!TYPE_NAME (t))
8827 {
8828 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8829
8830 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8831 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8832 DECL_SOURCE_LOCATION (type_decl)
8833 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8834 }
8835 else
8836 type_decl = TYPE_NAME (t);
8837
8838 if (CLASS_TYPE_P (template_type))
8839 {
8840 TREE_PRIVATE (type_decl)
8841 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8842 TREE_PROTECTED (type_decl)
8843 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8844 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8845 {
8846 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8847 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8848 }
8849 }
8850
8851 if (OVERLOAD_TYPE_P (t)
8852 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8853 {
8854 static const char *tags[] = {"abi_tag", "may_alias"};
8855
8856 for (unsigned ix = 0; ix != 2; ix++)
8857 {
8858 tree attributes
8859 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8860
8861 if (attributes)
8862 TYPE_ATTRIBUTES (t)
8863 = tree_cons (TREE_PURPOSE (attributes),
8864 TREE_VALUE (attributes),
8865 TYPE_ATTRIBUTES (t));
8866 }
8867 }
8868
8869 /* Let's consider the explicit specialization of a member
8870 of a class template specialization that is implicitly instantiated,
8871 e.g.:
8872 template<class T>
8873 struct S
8874 {
8875 template<class U> struct M {}; //#0
8876 };
8877
8878 template<>
8879 template<>
8880 struct S<int>::M<char> //#1
8881 {
8882 int i;
8883 };
8884 [temp.expl.spec]/4 says this is valid.
8885
8886 In this case, when we write:
8887 S<int>::M<char> m;
8888
8889 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8890 the one of #0.
8891
8892 When we encounter #1, we want to store the partial instantiation
8893 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8894
8895 For all cases other than this "explicit specialization of member of a
8896 class template", we just want to store the most general template into
8897 the CLASSTYPE_TI_TEMPLATE of M.
8898
8899 This case of "explicit specialization of member of a class template"
8900 only happens when:
8901 1/ the enclosing class is an instantiation of, and therefore not
8902 the same as, the context of the most general template, and
8903 2/ we aren't looking at the partial instantiation itself, i.e.
8904 the innermost arguments are not the same as the innermost parms of
8905 the most general template.
8906
8907 So it's only when 1/ and 2/ happens that we want to use the partial
8908 instantiation of the member template in lieu of its most general
8909 template. */
8910
8911 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8912 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8913 /* the enclosing class must be an instantiation... */
8914 && CLASS_TYPE_P (context)
8915 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8916 {
8917 TREE_VEC_LENGTH (arglist)--;
8918 ++processing_template_decl;
8919 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
8920 tree partial_inst_args =
8921 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
8922 arglist, complain, NULL_TREE);
8923 --processing_template_decl;
8924 TREE_VEC_LENGTH (arglist)++;
8925 if (partial_inst_args == error_mark_node)
8926 return error_mark_node;
8927 use_partial_inst_tmpl =
8928 /*...and we must not be looking at the partial instantiation
8929 itself. */
8930 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8931 partial_inst_args);
8932 }
8933
8934 if (!use_partial_inst_tmpl)
8935 /* This case is easy; there are no member templates involved. */
8936 found = gen_tmpl;
8937 else
8938 {
8939 /* This is a full instantiation of a member template. Find
8940 the partial instantiation of which this is an instance. */
8941
8942 /* Temporarily reduce by one the number of levels in the ARGLIST
8943 so as to avoid comparing the last set of arguments. */
8944 TREE_VEC_LENGTH (arglist)--;
8945 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8946 TREE_VEC_LENGTH (arglist)++;
8947 /* FOUND is either a proper class type, or an alias
8948 template specialization. In the later case, it's a
8949 TYPE_DECL, resulting from the substituting of arguments
8950 for parameters in the TYPE_DECL of the alias template
8951 done earlier. So be careful while getting the template
8952 of FOUND. */
8953 found = (TREE_CODE (found) == TEMPLATE_DECL
8954 ? found
8955 : (TREE_CODE (found) == TYPE_DECL
8956 ? DECL_TI_TEMPLATE (found)
8957 : CLASSTYPE_TI_TEMPLATE (found)));
8958 }
8959
8960 // Build template info for the new specialization.
8961 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8962
8963 elt.spec = t;
8964 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8965 entry = ggc_alloc<spec_entry> ();
8966 *entry = elt;
8967 *slot = entry;
8968
8969 /* Note this use of the partial instantiation so we can check it
8970 later in maybe_process_partial_specialization. */
8971 DECL_TEMPLATE_INSTANTIATIONS (found)
8972 = tree_cons (arglist, t,
8973 DECL_TEMPLATE_INSTANTIATIONS (found));
8974
8975 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8976 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8977 /* Now that the type has been registered on the instantiations
8978 list, we set up the enumerators. Because the enumeration
8979 constants may involve the enumeration type itself, we make
8980 sure to register the type first, and then create the
8981 constants. That way, doing tsubst_expr for the enumeration
8982 constants won't result in recursive calls here; we'll find
8983 the instantiation and exit above. */
8984 tsubst_enum (template_type, t, arglist);
8985
8986 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8987 /* If the type makes use of template parameters, the
8988 code that generates debugging information will crash. */
8989 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8990
8991 /* Possibly limit visibility based on template args. */
8992 TREE_PUBLIC (type_decl) = 1;
8993 determine_visibility (type_decl);
8994
8995 inherit_targ_abi_tags (t);
8996
8997 return t;
8998 }
8999 }
9000
9001 /* Wrapper for lookup_template_class_1. */
9002
9003 tree
9004 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9005 int entering_scope, tsubst_flags_t complain)
9006 {
9007 tree ret;
9008 timevar_push (TV_TEMPLATE_INST);
9009 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9010 entering_scope, complain);
9011 timevar_pop (TV_TEMPLATE_INST);
9012 return ret;
9013 }
9014
9015 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9016
9017 tree
9018 lookup_template_variable (tree templ, tree arglist)
9019 {
9020 /* The type of the expression is NULL_TREE since the template-id could refer
9021 to an explicit or partial specialization. */
9022 tree type = NULL_TREE;
9023 if (flag_concepts && variable_concept_p (templ))
9024 /* Except that concepts are always bool. */
9025 type = boolean_type_node;
9026 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9027 }
9028
9029 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9030
9031 tree
9032 finish_template_variable (tree var, tsubst_flags_t complain)
9033 {
9034 tree templ = TREE_OPERAND (var, 0);
9035 tree arglist = TREE_OPERAND (var, 1);
9036
9037 /* We never want to return a VAR_DECL for a variable concept, since they
9038 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9039 bool concept_p = flag_concepts && variable_concept_p (templ);
9040 if (concept_p && processing_template_decl)
9041 return var;
9042
9043 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9044 arglist = add_outermost_template_args (tmpl_args, arglist);
9045
9046 templ = most_general_template (templ);
9047 tree parms = DECL_TEMPLATE_PARMS (templ);
9048 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9049 /*req_all*/true,
9050 /*use_default*/true);
9051
9052 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9053 {
9054 if (complain & tf_error)
9055 {
9056 error ("use of invalid variable template %qE", var);
9057 diagnose_constraints (location_of (var), templ, arglist);
9058 }
9059 return error_mark_node;
9060 }
9061
9062 /* If a template-id refers to a specialization of a variable
9063 concept, then the expression is true if and only if the
9064 concept's constraints are satisfied by the given template
9065 arguments.
9066
9067 NOTE: This is an extension of Concepts Lite TS that
9068 allows constraints to be used in expressions. */
9069 if (concept_p)
9070 {
9071 tree decl = DECL_TEMPLATE_RESULT (templ);
9072 return evaluate_variable_concept (decl, arglist);
9073 }
9074
9075 return instantiate_template (templ, arglist, complain);
9076 }
9077
9078 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9079 TARGS template args, and instantiate it if it's not dependent. */
9080
9081 tree
9082 lookup_and_finish_template_variable (tree templ, tree targs,
9083 tsubst_flags_t complain)
9084 {
9085 templ = lookup_template_variable (templ, targs);
9086 if (!any_dependent_template_arguments_p (targs))
9087 {
9088 templ = finish_template_variable (templ, complain);
9089 mark_used (templ);
9090 }
9091
9092 return convert_from_reference (templ);
9093 }
9094
9095 \f
9096 struct pair_fn_data
9097 {
9098 tree_fn_t fn;
9099 tree_fn_t any_fn;
9100 void *data;
9101 /* True when we should also visit template parameters that occur in
9102 non-deduced contexts. */
9103 bool include_nondeduced_p;
9104 hash_set<tree> *visited;
9105 };
9106
9107 /* Called from for_each_template_parm via walk_tree. */
9108
9109 static tree
9110 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9111 {
9112 tree t = *tp;
9113 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9114 tree_fn_t fn = pfd->fn;
9115 void *data = pfd->data;
9116 tree result = NULL_TREE;
9117
9118 #define WALK_SUBTREE(NODE) \
9119 do \
9120 { \
9121 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9122 pfd->include_nondeduced_p, \
9123 pfd->any_fn); \
9124 if (result) goto out; \
9125 } \
9126 while (0)
9127
9128 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9129 return t;
9130
9131 if (TYPE_P (t)
9132 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9133 WALK_SUBTREE (TYPE_CONTEXT (t));
9134
9135 switch (TREE_CODE (t))
9136 {
9137 case RECORD_TYPE:
9138 if (TYPE_PTRMEMFUNC_P (t))
9139 break;
9140 /* Fall through. */
9141
9142 case UNION_TYPE:
9143 case ENUMERAL_TYPE:
9144 if (!TYPE_TEMPLATE_INFO (t))
9145 *walk_subtrees = 0;
9146 else
9147 WALK_SUBTREE (TYPE_TI_ARGS (t));
9148 break;
9149
9150 case INTEGER_TYPE:
9151 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9152 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9153 break;
9154
9155 case METHOD_TYPE:
9156 /* Since we're not going to walk subtrees, we have to do this
9157 explicitly here. */
9158 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9159 /* Fall through. */
9160
9161 case FUNCTION_TYPE:
9162 /* Check the return type. */
9163 WALK_SUBTREE (TREE_TYPE (t));
9164
9165 /* Check the parameter types. Since default arguments are not
9166 instantiated until they are needed, the TYPE_ARG_TYPES may
9167 contain expressions that involve template parameters. But,
9168 no-one should be looking at them yet. And, once they're
9169 instantiated, they don't contain template parameters, so
9170 there's no point in looking at them then, either. */
9171 {
9172 tree parm;
9173
9174 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9175 WALK_SUBTREE (TREE_VALUE (parm));
9176
9177 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9178 want walk_tree walking into them itself. */
9179 *walk_subtrees = 0;
9180 }
9181
9182 if (flag_noexcept_type)
9183 {
9184 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9185 if (spec)
9186 WALK_SUBTREE (TREE_PURPOSE (spec));
9187 }
9188 break;
9189
9190 case TYPEOF_TYPE:
9191 case UNDERLYING_TYPE:
9192 if (pfd->include_nondeduced_p
9193 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9194 pfd->visited,
9195 pfd->include_nondeduced_p,
9196 pfd->any_fn))
9197 return error_mark_node;
9198 break;
9199
9200 case FUNCTION_DECL:
9201 case VAR_DECL:
9202 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9203 WALK_SUBTREE (DECL_TI_ARGS (t));
9204 /* Fall through. */
9205
9206 case PARM_DECL:
9207 case CONST_DECL:
9208 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9209 WALK_SUBTREE (DECL_INITIAL (t));
9210 if (DECL_CONTEXT (t)
9211 && pfd->include_nondeduced_p)
9212 WALK_SUBTREE (DECL_CONTEXT (t));
9213 break;
9214
9215 case BOUND_TEMPLATE_TEMPLATE_PARM:
9216 /* Record template parameters such as `T' inside `TT<T>'. */
9217 WALK_SUBTREE (TYPE_TI_ARGS (t));
9218 /* Fall through. */
9219
9220 case TEMPLATE_TEMPLATE_PARM:
9221 case TEMPLATE_TYPE_PARM:
9222 case TEMPLATE_PARM_INDEX:
9223 if (fn && (*fn)(t, data))
9224 return t;
9225 else if (!fn)
9226 return t;
9227 break;
9228
9229 case TEMPLATE_DECL:
9230 /* A template template parameter is encountered. */
9231 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9232 WALK_SUBTREE (TREE_TYPE (t));
9233
9234 /* Already substituted template template parameter */
9235 *walk_subtrees = 0;
9236 break;
9237
9238 case TYPENAME_TYPE:
9239 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9240 partial instantiation. */
9241 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9242 break;
9243
9244 case CONSTRUCTOR:
9245 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9246 && pfd->include_nondeduced_p)
9247 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9248 break;
9249
9250 case INDIRECT_REF:
9251 case COMPONENT_REF:
9252 /* If there's no type, then this thing must be some expression
9253 involving template parameters. */
9254 if (!fn && !TREE_TYPE (t))
9255 return error_mark_node;
9256 break;
9257
9258 case MODOP_EXPR:
9259 case CAST_EXPR:
9260 case IMPLICIT_CONV_EXPR:
9261 case REINTERPRET_CAST_EXPR:
9262 case CONST_CAST_EXPR:
9263 case STATIC_CAST_EXPR:
9264 case DYNAMIC_CAST_EXPR:
9265 case ARROW_EXPR:
9266 case DOTSTAR_EXPR:
9267 case TYPEID_EXPR:
9268 case PSEUDO_DTOR_EXPR:
9269 if (!fn)
9270 return error_mark_node;
9271 break;
9272
9273 default:
9274 break;
9275 }
9276
9277 #undef WALK_SUBTREE
9278
9279 /* We didn't find any template parameters we liked. */
9280 out:
9281 return result;
9282 }
9283
9284 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9285 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9286 call FN with the parameter and the DATA.
9287 If FN returns nonzero, the iteration is terminated, and
9288 for_each_template_parm returns 1. Otherwise, the iteration
9289 continues. If FN never returns a nonzero value, the value
9290 returned by for_each_template_parm is 0. If FN is NULL, it is
9291 considered to be the function which always returns 1.
9292
9293 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9294 parameters that occur in non-deduced contexts. When false, only
9295 visits those template parameters that can be deduced. */
9296
9297 static tree
9298 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9299 hash_set<tree> *visited,
9300 bool include_nondeduced_p,
9301 tree_fn_t any_fn)
9302 {
9303 struct pair_fn_data pfd;
9304 tree result;
9305
9306 /* Set up. */
9307 pfd.fn = fn;
9308 pfd.any_fn = any_fn;
9309 pfd.data = data;
9310 pfd.include_nondeduced_p = include_nondeduced_p;
9311
9312 /* Walk the tree. (Conceptually, we would like to walk without
9313 duplicates, but for_each_template_parm_r recursively calls
9314 for_each_template_parm, so we would need to reorganize a fair
9315 bit to use walk_tree_without_duplicates, so we keep our own
9316 visited list.) */
9317 if (visited)
9318 pfd.visited = visited;
9319 else
9320 pfd.visited = new hash_set<tree>;
9321 result = cp_walk_tree (&t,
9322 for_each_template_parm_r,
9323 &pfd,
9324 pfd.visited);
9325
9326 /* Clean up. */
9327 if (!visited)
9328 {
9329 delete pfd.visited;
9330 pfd.visited = 0;
9331 }
9332
9333 return result;
9334 }
9335
9336 /* Returns true if T depends on any template parameter. */
9337
9338 int
9339 uses_template_parms (tree t)
9340 {
9341 if (t == NULL_TREE)
9342 return false;
9343
9344 bool dependent_p;
9345 int saved_processing_template_decl;
9346
9347 saved_processing_template_decl = processing_template_decl;
9348 if (!saved_processing_template_decl)
9349 processing_template_decl = 1;
9350 if (TYPE_P (t))
9351 dependent_p = dependent_type_p (t);
9352 else if (TREE_CODE (t) == TREE_VEC)
9353 dependent_p = any_dependent_template_arguments_p (t);
9354 else if (TREE_CODE (t) == TREE_LIST)
9355 dependent_p = (uses_template_parms (TREE_VALUE (t))
9356 || uses_template_parms (TREE_CHAIN (t)));
9357 else if (TREE_CODE (t) == TYPE_DECL)
9358 dependent_p = dependent_type_p (TREE_TYPE (t));
9359 else if (DECL_P (t)
9360 || EXPR_P (t)
9361 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9362 || TREE_CODE (t) == OVERLOAD
9363 || BASELINK_P (t)
9364 || identifier_p (t)
9365 || TREE_CODE (t) == TRAIT_EXPR
9366 || TREE_CODE (t) == CONSTRUCTOR
9367 || CONSTANT_CLASS_P (t))
9368 dependent_p = (type_dependent_expression_p (t)
9369 || value_dependent_expression_p (t));
9370 else
9371 {
9372 gcc_assert (t == error_mark_node);
9373 dependent_p = false;
9374 }
9375
9376 processing_template_decl = saved_processing_template_decl;
9377
9378 return dependent_p;
9379 }
9380
9381 /* Returns true iff current_function_decl is an incompletely instantiated
9382 template. Useful instead of processing_template_decl because the latter
9383 is set to 0 during instantiate_non_dependent_expr. */
9384
9385 bool
9386 in_template_function (void)
9387 {
9388 tree fn = current_function_decl;
9389 bool ret;
9390 ++processing_template_decl;
9391 ret = (fn && DECL_LANG_SPECIFIC (fn)
9392 && DECL_TEMPLATE_INFO (fn)
9393 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9394 --processing_template_decl;
9395 return ret;
9396 }
9397
9398 /* Returns true if T depends on any template parameter with level LEVEL. */
9399
9400 bool
9401 uses_template_parms_level (tree t, int level)
9402 {
9403 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9404 /*include_nondeduced_p=*/true);
9405 }
9406
9407 /* Returns true if the signature of DECL depends on any template parameter from
9408 its enclosing class. */
9409
9410 bool
9411 uses_outer_template_parms (tree decl)
9412 {
9413 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9414 if (depth == 0)
9415 return false;
9416 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9417 &depth, NULL, /*include_nondeduced_p=*/true))
9418 return true;
9419 if (PRIMARY_TEMPLATE_P (decl)
9420 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9421 (DECL_TEMPLATE_PARMS (decl)),
9422 template_parm_outer_level,
9423 &depth, NULL, /*include_nondeduced_p=*/true))
9424 return true;
9425 tree ci = get_constraints (decl);
9426 if (ci)
9427 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9428 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9429 &depth, NULL, /*nondeduced*/true))
9430 return true;
9431 return false;
9432 }
9433
9434 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9435 ill-formed translation unit, i.e. a variable or function that isn't
9436 usable in a constant expression. */
9437
9438 static inline bool
9439 neglectable_inst_p (tree d)
9440 {
9441 return (DECL_P (d)
9442 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9443 : decl_maybe_constant_var_p (d)));
9444 }
9445
9446 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9447 neglectable and instantiated from within an erroneous instantiation. */
9448
9449 static bool
9450 limit_bad_template_recursion (tree decl)
9451 {
9452 struct tinst_level *lev = current_tinst_level;
9453 int errs = errorcount + sorrycount;
9454 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9455 return false;
9456
9457 for (; lev; lev = lev->next)
9458 if (neglectable_inst_p (lev->decl))
9459 break;
9460
9461 return (lev && errs > lev->errors);
9462 }
9463
9464 static int tinst_depth;
9465 extern int max_tinst_depth;
9466 int depth_reached;
9467
9468 static GTY(()) struct tinst_level *last_error_tinst_level;
9469
9470 /* We're starting to instantiate D; record the template instantiation context
9471 for diagnostics and to restore it later. */
9472
9473 bool
9474 push_tinst_level (tree d)
9475 {
9476 return push_tinst_level_loc (d, input_location);
9477 }
9478
9479 /* We're starting to instantiate D; record the template instantiation context
9480 at LOC for diagnostics and to restore it later. */
9481
9482 bool
9483 push_tinst_level_loc (tree d, location_t loc)
9484 {
9485 struct tinst_level *new_level;
9486
9487 if (tinst_depth >= max_tinst_depth)
9488 {
9489 /* Tell error.c not to try to instantiate any templates. */
9490 at_eof = 2;
9491 fatal_error (input_location,
9492 "template instantiation depth exceeds maximum of %d"
9493 " (use -ftemplate-depth= to increase the maximum)",
9494 max_tinst_depth);
9495 return false;
9496 }
9497
9498 /* If the current instantiation caused problems, don't let it instantiate
9499 anything else. Do allow deduction substitution and decls usable in
9500 constant expressions. */
9501 if (limit_bad_template_recursion (d))
9502 return false;
9503
9504 /* When not -quiet, dump template instantiations other than functions, since
9505 announce_function will take care of those. */
9506 if (!quiet_flag
9507 && TREE_CODE (d) != TREE_LIST
9508 && TREE_CODE (d) != FUNCTION_DECL)
9509 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9510
9511 new_level = ggc_alloc<tinst_level> ();
9512 new_level->decl = d;
9513 new_level->locus = loc;
9514 new_level->errors = errorcount+sorrycount;
9515 new_level->in_system_header_p = in_system_header_at (input_location);
9516 new_level->next = current_tinst_level;
9517 current_tinst_level = new_level;
9518
9519 ++tinst_depth;
9520 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9521 depth_reached = tinst_depth;
9522
9523 return true;
9524 }
9525
9526 /* We're done instantiating this template; return to the instantiation
9527 context. */
9528
9529 void
9530 pop_tinst_level (void)
9531 {
9532 /* Restore the filename and line number stashed away when we started
9533 this instantiation. */
9534 input_location = current_tinst_level->locus;
9535 current_tinst_level = current_tinst_level->next;
9536 --tinst_depth;
9537 }
9538
9539 /* We're instantiating a deferred template; restore the template
9540 instantiation context in which the instantiation was requested, which
9541 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9542
9543 static tree
9544 reopen_tinst_level (struct tinst_level *level)
9545 {
9546 struct tinst_level *t;
9547
9548 tinst_depth = 0;
9549 for (t = level; t; t = t->next)
9550 ++tinst_depth;
9551
9552 current_tinst_level = level;
9553 pop_tinst_level ();
9554 if (current_tinst_level)
9555 current_tinst_level->errors = errorcount+sorrycount;
9556 return level->decl;
9557 }
9558
9559 /* Returns the TINST_LEVEL which gives the original instantiation
9560 context. */
9561
9562 struct tinst_level *
9563 outermost_tinst_level (void)
9564 {
9565 struct tinst_level *level = current_tinst_level;
9566 if (level)
9567 while (level->next)
9568 level = level->next;
9569 return level;
9570 }
9571
9572 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9573 vector of template arguments, as for tsubst.
9574
9575 Returns an appropriate tsubst'd friend declaration. */
9576
9577 static tree
9578 tsubst_friend_function (tree decl, tree args)
9579 {
9580 tree new_friend;
9581
9582 if (TREE_CODE (decl) == FUNCTION_DECL
9583 && DECL_TEMPLATE_INSTANTIATION (decl)
9584 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9585 /* This was a friend declared with an explicit template
9586 argument list, e.g.:
9587
9588 friend void f<>(T);
9589
9590 to indicate that f was a template instantiation, not a new
9591 function declaration. Now, we have to figure out what
9592 instantiation of what template. */
9593 {
9594 tree template_id, arglist, fns;
9595 tree new_args;
9596 tree tmpl;
9597 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9598
9599 /* Friend functions are looked up in the containing namespace scope.
9600 We must enter that scope, to avoid finding member functions of the
9601 current class with same name. */
9602 push_nested_namespace (ns);
9603 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9604 tf_warning_or_error, NULL_TREE,
9605 /*integral_constant_expression_p=*/false);
9606 pop_nested_namespace (ns);
9607 arglist = tsubst (DECL_TI_ARGS (decl), args,
9608 tf_warning_or_error, NULL_TREE);
9609 template_id = lookup_template_function (fns, arglist);
9610
9611 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9612 tmpl = determine_specialization (template_id, new_friend,
9613 &new_args,
9614 /*need_member_template=*/0,
9615 TREE_VEC_LENGTH (args),
9616 tsk_none);
9617 return instantiate_template (tmpl, new_args, tf_error);
9618 }
9619
9620 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9621
9622 /* The NEW_FRIEND will look like an instantiation, to the
9623 compiler, but is not an instantiation from the point of view of
9624 the language. For example, we might have had:
9625
9626 template <class T> struct S {
9627 template <class U> friend void f(T, U);
9628 };
9629
9630 Then, in S<int>, template <class U> void f(int, U) is not an
9631 instantiation of anything. */
9632 if (new_friend == error_mark_node)
9633 return error_mark_node;
9634
9635 DECL_USE_TEMPLATE (new_friend) = 0;
9636 if (TREE_CODE (decl) == TEMPLATE_DECL)
9637 {
9638 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9639 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9640 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9641 }
9642
9643 /* The mangled name for the NEW_FRIEND is incorrect. The function
9644 is not a template instantiation and should not be mangled like
9645 one. Therefore, we forget the mangling here; we'll recompute it
9646 later if we need it. */
9647 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9648 {
9649 SET_DECL_RTL (new_friend, NULL);
9650 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9651 }
9652
9653 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9654 {
9655 tree old_decl;
9656 tree new_friend_template_info;
9657 tree new_friend_result_template_info;
9658 tree ns;
9659 int new_friend_is_defn;
9660
9661 /* We must save some information from NEW_FRIEND before calling
9662 duplicate decls since that function will free NEW_FRIEND if
9663 possible. */
9664 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9665 new_friend_is_defn =
9666 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9667 (template_for_substitution (new_friend)))
9668 != NULL_TREE);
9669 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9670 {
9671 /* This declaration is a `primary' template. */
9672 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9673
9674 new_friend_result_template_info
9675 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9676 }
9677 else
9678 new_friend_result_template_info = NULL_TREE;
9679
9680 /* Inside pushdecl_namespace_level, we will push into the
9681 current namespace. However, the friend function should go
9682 into the namespace of the template. */
9683 ns = decl_namespace_context (new_friend);
9684 push_nested_namespace (ns);
9685 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9686 pop_nested_namespace (ns);
9687
9688 if (old_decl == error_mark_node)
9689 return error_mark_node;
9690
9691 if (old_decl != new_friend)
9692 {
9693 /* This new friend declaration matched an existing
9694 declaration. For example, given:
9695
9696 template <class T> void f(T);
9697 template <class U> class C {
9698 template <class T> friend void f(T) {}
9699 };
9700
9701 the friend declaration actually provides the definition
9702 of `f', once C has been instantiated for some type. So,
9703 old_decl will be the out-of-class template declaration,
9704 while new_friend is the in-class definition.
9705
9706 But, if `f' was called before this point, the
9707 instantiation of `f' will have DECL_TI_ARGS corresponding
9708 to `T' but not to `U', references to which might appear
9709 in the definition of `f'. Previously, the most general
9710 template for an instantiation of `f' was the out-of-class
9711 version; now it is the in-class version. Therefore, we
9712 run through all specialization of `f', adding to their
9713 DECL_TI_ARGS appropriately. In particular, they need a
9714 new set of outer arguments, corresponding to the
9715 arguments for this class instantiation.
9716
9717 The same situation can arise with something like this:
9718
9719 friend void f(int);
9720 template <class T> class C {
9721 friend void f(T) {}
9722 };
9723
9724 when `C<int>' is instantiated. Now, `f(int)' is defined
9725 in the class. */
9726
9727 if (!new_friend_is_defn)
9728 /* On the other hand, if the in-class declaration does
9729 *not* provide a definition, then we don't want to alter
9730 existing definitions. We can just leave everything
9731 alone. */
9732 ;
9733 else
9734 {
9735 tree new_template = TI_TEMPLATE (new_friend_template_info);
9736 tree new_args = TI_ARGS (new_friend_template_info);
9737
9738 /* Overwrite whatever template info was there before, if
9739 any, with the new template information pertaining to
9740 the declaration. */
9741 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9742
9743 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9744 {
9745 /* We should have called reregister_specialization in
9746 duplicate_decls. */
9747 gcc_assert (retrieve_specialization (new_template,
9748 new_args, 0)
9749 == old_decl);
9750
9751 /* Instantiate it if the global has already been used. */
9752 if (DECL_ODR_USED (old_decl))
9753 instantiate_decl (old_decl, /*defer_ok=*/true,
9754 /*expl_inst_class_mem_p=*/false);
9755 }
9756 else
9757 {
9758 tree t;
9759
9760 /* Indicate that the old function template is a partial
9761 instantiation. */
9762 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9763 = new_friend_result_template_info;
9764
9765 gcc_assert (new_template
9766 == most_general_template (new_template));
9767 gcc_assert (new_template != old_decl);
9768
9769 /* Reassign any specializations already in the hash table
9770 to the new more general template, and add the
9771 additional template args. */
9772 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9773 t != NULL_TREE;
9774 t = TREE_CHAIN (t))
9775 {
9776 tree spec = TREE_VALUE (t);
9777 spec_entry elt;
9778
9779 elt.tmpl = old_decl;
9780 elt.args = DECL_TI_ARGS (spec);
9781 elt.spec = NULL_TREE;
9782
9783 decl_specializations->remove_elt (&elt);
9784
9785 DECL_TI_ARGS (spec)
9786 = add_outermost_template_args (new_args,
9787 DECL_TI_ARGS (spec));
9788
9789 register_specialization
9790 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9791
9792 }
9793 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9794 }
9795 }
9796
9797 /* The information from NEW_FRIEND has been merged into OLD_DECL
9798 by duplicate_decls. */
9799 new_friend = old_decl;
9800 }
9801 }
9802 else
9803 {
9804 tree context = DECL_CONTEXT (new_friend);
9805 bool dependent_p;
9806
9807 /* In the code
9808 template <class T> class C {
9809 template <class U> friend void C1<U>::f (); // case 1
9810 friend void C2<T>::f (); // case 2
9811 };
9812 we only need to make sure CONTEXT is a complete type for
9813 case 2. To distinguish between the two cases, we note that
9814 CONTEXT of case 1 remains dependent type after tsubst while
9815 this isn't true for case 2. */
9816 ++processing_template_decl;
9817 dependent_p = dependent_type_p (context);
9818 --processing_template_decl;
9819
9820 if (!dependent_p
9821 && !complete_type_or_else (context, NULL_TREE))
9822 return error_mark_node;
9823
9824 if (COMPLETE_TYPE_P (context))
9825 {
9826 tree fn = new_friend;
9827 /* do_friend adds the TEMPLATE_DECL for any member friend
9828 template even if it isn't a member template, i.e.
9829 template <class T> friend A<T>::f();
9830 Look through it in that case. */
9831 if (TREE_CODE (fn) == TEMPLATE_DECL
9832 && !PRIMARY_TEMPLATE_P (fn))
9833 fn = DECL_TEMPLATE_RESULT (fn);
9834 /* Check to see that the declaration is really present, and,
9835 possibly obtain an improved declaration. */
9836 fn = check_classfn (context, fn, NULL_TREE);
9837
9838 if (fn)
9839 new_friend = fn;
9840 }
9841 }
9842
9843 return new_friend;
9844 }
9845
9846 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9847 template arguments, as for tsubst.
9848
9849 Returns an appropriate tsubst'd friend type or error_mark_node on
9850 failure. */
9851
9852 static tree
9853 tsubst_friend_class (tree friend_tmpl, tree args)
9854 {
9855 tree friend_type;
9856 tree tmpl;
9857 tree context;
9858
9859 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9860 {
9861 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9862 return TREE_TYPE (t);
9863 }
9864
9865 context = CP_DECL_CONTEXT (friend_tmpl);
9866
9867 if (context != global_namespace)
9868 {
9869 if (TREE_CODE (context) == NAMESPACE_DECL)
9870 push_nested_namespace (context);
9871 else
9872 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9873 }
9874
9875 /* Look for a class template declaration. We look for hidden names
9876 because two friend declarations of the same template are the
9877 same. For example, in:
9878
9879 struct A {
9880 template <typename> friend class F;
9881 };
9882 template <typename> struct B {
9883 template <typename> friend class F;
9884 };
9885
9886 both F templates are the same. */
9887 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9888 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9889
9890 /* But, if we don't find one, it might be because we're in a
9891 situation like this:
9892
9893 template <class T>
9894 struct S {
9895 template <class U>
9896 friend struct S;
9897 };
9898
9899 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9900 for `S<int>', not the TEMPLATE_DECL. */
9901 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9902 {
9903 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9904 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9905 }
9906
9907 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9908 {
9909 /* The friend template has already been declared. Just
9910 check to see that the declarations match, and install any new
9911 default parameters. We must tsubst the default parameters,
9912 of course. We only need the innermost template parameters
9913 because that is all that redeclare_class_template will look
9914 at. */
9915 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9916 > TMPL_ARGS_DEPTH (args))
9917 {
9918 tree parms;
9919 location_t saved_input_location;
9920 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9921 args, tf_warning_or_error);
9922
9923 saved_input_location = input_location;
9924 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9925 tree cons = get_constraints (tmpl);
9926 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9927 input_location = saved_input_location;
9928
9929 }
9930
9931 friend_type = TREE_TYPE (tmpl);
9932 }
9933 else
9934 {
9935 /* The friend template has not already been declared. In this
9936 case, the instantiation of the template class will cause the
9937 injection of this template into the global scope. */
9938 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9939 if (tmpl == error_mark_node)
9940 return error_mark_node;
9941
9942 /* The new TMPL is not an instantiation of anything, so we
9943 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9944 the new type because that is supposed to be the corresponding
9945 template decl, i.e., TMPL. */
9946 DECL_USE_TEMPLATE (tmpl) = 0;
9947 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9948 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9949 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9950 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9951
9952 /* Inject this template into the global scope. */
9953 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9954 }
9955
9956 if (context != global_namespace)
9957 {
9958 if (TREE_CODE (context) == NAMESPACE_DECL)
9959 pop_nested_namespace (context);
9960 else
9961 pop_nested_class ();
9962 }
9963
9964 return friend_type;
9965 }
9966
9967 /* Returns zero if TYPE cannot be completed later due to circularity.
9968 Otherwise returns one. */
9969
9970 static int
9971 can_complete_type_without_circularity (tree type)
9972 {
9973 if (type == NULL_TREE || type == error_mark_node)
9974 return 0;
9975 else if (COMPLETE_TYPE_P (type))
9976 return 1;
9977 else if (TREE_CODE (type) == ARRAY_TYPE)
9978 return can_complete_type_without_circularity (TREE_TYPE (type));
9979 else if (CLASS_TYPE_P (type)
9980 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9981 return 0;
9982 else
9983 return 1;
9984 }
9985
9986 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9987 tsubst_flags_t, tree);
9988
9989 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9990 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9991
9992 static tree
9993 tsubst_attribute (tree t, tree *decl_p, tree args,
9994 tsubst_flags_t complain, tree in_decl)
9995 {
9996 gcc_assert (ATTR_IS_DEPENDENT (t));
9997
9998 tree val = TREE_VALUE (t);
9999 if (val == NULL_TREE)
10000 /* Nothing to do. */;
10001 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10002 && is_attribute_p ("omp declare simd",
10003 get_attribute_name (t)))
10004 {
10005 tree clauses = TREE_VALUE (val);
10006 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10007 complain, in_decl);
10008 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10009 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10010 tree parms = DECL_ARGUMENTS (*decl_p);
10011 clauses
10012 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10013 if (clauses)
10014 val = build_tree_list (NULL_TREE, clauses);
10015 else
10016 val = NULL_TREE;
10017 }
10018 /* If the first attribute argument is an identifier, don't
10019 pass it through tsubst. Attributes like mode, format,
10020 cleanup and several target specific attributes expect it
10021 unmodified. */
10022 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10023 {
10024 tree chain
10025 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10026 /*integral_constant_expression_p=*/false);
10027 if (chain != TREE_CHAIN (val))
10028 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10029 }
10030 else if (PACK_EXPANSION_P (val))
10031 {
10032 /* An attribute pack expansion. */
10033 tree purp = TREE_PURPOSE (t);
10034 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10035 if (pack == error_mark_node)
10036 return error_mark_node;
10037 int len = TREE_VEC_LENGTH (pack);
10038 tree list = NULL_TREE;
10039 tree *q = &list;
10040 for (int i = 0; i < len; ++i)
10041 {
10042 tree elt = TREE_VEC_ELT (pack, i);
10043 *q = build_tree_list (purp, elt);
10044 q = &TREE_CHAIN (*q);
10045 }
10046 return list;
10047 }
10048 else
10049 val = tsubst_expr (val, args, complain, in_decl,
10050 /*integral_constant_expression_p=*/false);
10051
10052 if (val != TREE_VALUE (t))
10053 return build_tree_list (TREE_PURPOSE (t), val);
10054 return t;
10055 }
10056
10057 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10058 unchanged or a new TREE_LIST chain. */
10059
10060 static tree
10061 tsubst_attributes (tree attributes, tree args,
10062 tsubst_flags_t complain, tree in_decl)
10063 {
10064 tree last_dep = NULL_TREE;
10065
10066 for (tree t = attributes; t; t = TREE_CHAIN (t))
10067 if (ATTR_IS_DEPENDENT (t))
10068 {
10069 last_dep = t;
10070 attributes = copy_list (attributes);
10071 break;
10072 }
10073
10074 if (last_dep)
10075 for (tree *p = &attributes; *p; )
10076 {
10077 tree t = *p;
10078 if (ATTR_IS_DEPENDENT (t))
10079 {
10080 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10081 if (subst != t)
10082 {
10083 *p = subst;
10084 do
10085 p = &TREE_CHAIN (*p);
10086 while (*p);
10087 *p = TREE_CHAIN (t);
10088 continue;
10089 }
10090 }
10091 p = &TREE_CHAIN (*p);
10092 }
10093
10094 return attributes;
10095 }
10096
10097 /* Apply any attributes which had to be deferred until instantiation
10098 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10099 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10100
10101 static void
10102 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10103 tree args, tsubst_flags_t complain, tree in_decl)
10104 {
10105 tree last_dep = NULL_TREE;
10106 tree t;
10107 tree *p;
10108
10109 if (attributes == NULL_TREE)
10110 return;
10111
10112 if (DECL_P (*decl_p))
10113 {
10114 if (TREE_TYPE (*decl_p) == error_mark_node)
10115 return;
10116 p = &DECL_ATTRIBUTES (*decl_p);
10117 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10118 to our attributes parameter. */
10119 gcc_assert (*p == attributes);
10120 }
10121 else
10122 {
10123 p = &TYPE_ATTRIBUTES (*decl_p);
10124 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10125 lookup_template_class_1, and should be preserved. */
10126 gcc_assert (*p != attributes);
10127 while (*p)
10128 p = &TREE_CHAIN (*p);
10129 }
10130
10131 for (t = attributes; t; t = TREE_CHAIN (t))
10132 if (ATTR_IS_DEPENDENT (t))
10133 {
10134 last_dep = t;
10135 attributes = copy_list (attributes);
10136 break;
10137 }
10138
10139 *p = attributes;
10140 if (last_dep)
10141 {
10142 tree late_attrs = NULL_TREE;
10143 tree *q = &late_attrs;
10144
10145 for (; *p; )
10146 {
10147 t = *p;
10148 if (ATTR_IS_DEPENDENT (t))
10149 {
10150 *p = TREE_CHAIN (t);
10151 TREE_CHAIN (t) = NULL_TREE;
10152 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10153 do
10154 q = &TREE_CHAIN (*q);
10155 while (*q);
10156 }
10157 else
10158 p = &TREE_CHAIN (t);
10159 }
10160
10161 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10162 }
10163 }
10164
10165 /* Perform (or defer) access check for typedefs that were referenced
10166 from within the template TMPL code.
10167 This is a subroutine of instantiate_decl and instantiate_class_template.
10168 TMPL is the template to consider and TARGS is the list of arguments of
10169 that template. */
10170
10171 static void
10172 perform_typedefs_access_check (tree tmpl, tree targs)
10173 {
10174 location_t saved_location;
10175 unsigned i;
10176 qualified_typedef_usage_t *iter;
10177
10178 if (!tmpl
10179 || (!CLASS_TYPE_P (tmpl)
10180 && TREE_CODE (tmpl) != FUNCTION_DECL))
10181 return;
10182
10183 saved_location = input_location;
10184 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10185 {
10186 tree type_decl = iter->typedef_decl;
10187 tree type_scope = iter->context;
10188
10189 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10190 continue;
10191
10192 if (uses_template_parms (type_decl))
10193 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10194 if (uses_template_parms (type_scope))
10195 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10196
10197 /* Make access check error messages point to the location
10198 of the use of the typedef. */
10199 input_location = iter->locus;
10200 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10201 type_decl, type_decl,
10202 tf_warning_or_error);
10203 }
10204 input_location = saved_location;
10205 }
10206
10207 static tree
10208 instantiate_class_template_1 (tree type)
10209 {
10210 tree templ, args, pattern, t, member;
10211 tree typedecl;
10212 tree pbinfo;
10213 tree base_list;
10214 unsigned int saved_maximum_field_alignment;
10215 tree fn_context;
10216
10217 if (type == error_mark_node)
10218 return error_mark_node;
10219
10220 if (COMPLETE_OR_OPEN_TYPE_P (type)
10221 || uses_template_parms (type))
10222 return type;
10223
10224 /* Figure out which template is being instantiated. */
10225 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10226 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10227
10228 /* Determine what specialization of the original template to
10229 instantiate. */
10230 t = most_specialized_partial_spec (type, tf_warning_or_error);
10231 if (t == error_mark_node)
10232 {
10233 TYPE_BEING_DEFINED (type) = 1;
10234 return error_mark_node;
10235 }
10236 else if (t)
10237 {
10238 /* This TYPE is actually an instantiation of a partial
10239 specialization. We replace the innermost set of ARGS with
10240 the arguments appropriate for substitution. For example,
10241 given:
10242
10243 template <class T> struct S {};
10244 template <class T> struct S<T*> {};
10245
10246 and supposing that we are instantiating S<int*>, ARGS will
10247 presently be {int*} -- but we need {int}. */
10248 pattern = TREE_TYPE (t);
10249 args = TREE_PURPOSE (t);
10250 }
10251 else
10252 {
10253 pattern = TREE_TYPE (templ);
10254 args = CLASSTYPE_TI_ARGS (type);
10255 }
10256
10257 /* If the template we're instantiating is incomplete, then clearly
10258 there's nothing we can do. */
10259 if (!COMPLETE_TYPE_P (pattern))
10260 return type;
10261
10262 /* If we've recursively instantiated too many templates, stop. */
10263 if (! push_tinst_level (type))
10264 return type;
10265
10266 /* Now we're really doing the instantiation. Mark the type as in
10267 the process of being defined. */
10268 TYPE_BEING_DEFINED (type) = 1;
10269
10270 /* We may be in the middle of deferred access check. Disable
10271 it now. */
10272 push_deferring_access_checks (dk_no_deferred);
10273
10274 int saved_unevaluated_operand = cp_unevaluated_operand;
10275 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10276
10277 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10278 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10279 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10280 fn_context = error_mark_node;
10281 if (!fn_context)
10282 push_to_top_level ();
10283 else
10284 {
10285 cp_unevaluated_operand = 0;
10286 c_inhibit_evaluation_warnings = 0;
10287 }
10288 /* Use #pragma pack from the template context. */
10289 saved_maximum_field_alignment = maximum_field_alignment;
10290 maximum_field_alignment = TYPE_PRECISION (pattern);
10291
10292 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10293
10294 /* Set the input location to the most specialized template definition.
10295 This is needed if tsubsting causes an error. */
10296 typedecl = TYPE_MAIN_DECL (pattern);
10297 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10298 DECL_SOURCE_LOCATION (typedecl);
10299
10300 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10301 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10302 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10303 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10304 if (ANON_AGGR_TYPE_P (pattern))
10305 SET_ANON_AGGR_TYPE_P (type);
10306 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10307 {
10308 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10309 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10310 /* Adjust visibility for template arguments. */
10311 determine_visibility (TYPE_MAIN_DECL (type));
10312 }
10313 if (CLASS_TYPE_P (type))
10314 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10315
10316 pbinfo = TYPE_BINFO (pattern);
10317
10318 /* We should never instantiate a nested class before its enclosing
10319 class; we need to look up the nested class by name before we can
10320 instantiate it, and that lookup should instantiate the enclosing
10321 class. */
10322 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10323 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10324
10325 base_list = NULL_TREE;
10326 if (BINFO_N_BASE_BINFOS (pbinfo))
10327 {
10328 tree pbase_binfo;
10329 tree pushed_scope;
10330 int i;
10331
10332 /* We must enter the scope containing the type, as that is where
10333 the accessibility of types named in dependent bases are
10334 looked up from. */
10335 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10336
10337 /* Substitute into each of the bases to determine the actual
10338 basetypes. */
10339 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10340 {
10341 tree base;
10342 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10343 tree expanded_bases = NULL_TREE;
10344 int idx, len = 1;
10345
10346 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10347 {
10348 expanded_bases =
10349 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10350 args, tf_error, NULL_TREE);
10351 if (expanded_bases == error_mark_node)
10352 continue;
10353
10354 len = TREE_VEC_LENGTH (expanded_bases);
10355 }
10356
10357 for (idx = 0; idx < len; idx++)
10358 {
10359 if (expanded_bases)
10360 /* Extract the already-expanded base class. */
10361 base = TREE_VEC_ELT (expanded_bases, idx);
10362 else
10363 /* Substitute to figure out the base class. */
10364 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10365 NULL_TREE);
10366
10367 if (base == error_mark_node)
10368 continue;
10369
10370 base_list = tree_cons (access, base, base_list);
10371 if (BINFO_VIRTUAL_P (pbase_binfo))
10372 TREE_TYPE (base_list) = integer_type_node;
10373 }
10374 }
10375
10376 /* The list is now in reverse order; correct that. */
10377 base_list = nreverse (base_list);
10378
10379 if (pushed_scope)
10380 pop_scope (pushed_scope);
10381 }
10382 /* Now call xref_basetypes to set up all the base-class
10383 information. */
10384 xref_basetypes (type, base_list);
10385
10386 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10387 (int) ATTR_FLAG_TYPE_IN_PLACE,
10388 args, tf_error, NULL_TREE);
10389 fixup_attribute_variants (type);
10390
10391 /* Now that our base classes are set up, enter the scope of the
10392 class, so that name lookups into base classes, etc. will work
10393 correctly. This is precisely analogous to what we do in
10394 begin_class_definition when defining an ordinary non-template
10395 class, except we also need to push the enclosing classes. */
10396 push_nested_class (type);
10397
10398 /* Now members are processed in the order of declaration. */
10399 for (member = CLASSTYPE_DECL_LIST (pattern);
10400 member; member = TREE_CHAIN (member))
10401 {
10402 tree t = TREE_VALUE (member);
10403
10404 if (TREE_PURPOSE (member))
10405 {
10406 if (TYPE_P (t))
10407 {
10408 /* Build new CLASSTYPE_NESTED_UTDS. */
10409
10410 tree newtag;
10411 bool class_template_p;
10412
10413 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10414 && TYPE_LANG_SPECIFIC (t)
10415 && CLASSTYPE_IS_TEMPLATE (t));
10416 /* If the member is a class template, then -- even after
10417 substitution -- there may be dependent types in the
10418 template argument list for the class. We increment
10419 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10420 that function will assume that no types are dependent
10421 when outside of a template. */
10422 if (class_template_p)
10423 ++processing_template_decl;
10424 newtag = tsubst (t, args, tf_error, NULL_TREE);
10425 if (class_template_p)
10426 --processing_template_decl;
10427 if (newtag == error_mark_node)
10428 continue;
10429
10430 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10431 {
10432 tree name = TYPE_IDENTIFIER (t);
10433
10434 if (class_template_p)
10435 /* Unfortunately, lookup_template_class sets
10436 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10437 instantiation (i.e., for the type of a member
10438 template class nested within a template class.)
10439 This behavior is required for
10440 maybe_process_partial_specialization to work
10441 correctly, but is not accurate in this case;
10442 the TAG is not an instantiation of anything.
10443 (The corresponding TEMPLATE_DECL is an
10444 instantiation, but the TYPE is not.) */
10445 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10446
10447 /* Now, we call pushtag to put this NEWTAG into the scope of
10448 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10449 pushtag calling push_template_decl. We don't have to do
10450 this for enums because it will already have been done in
10451 tsubst_enum. */
10452 if (name)
10453 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10454 pushtag (name, newtag, /*tag_scope=*/ts_current);
10455 }
10456 }
10457 else if (DECL_DECLARES_FUNCTION_P (t))
10458 {
10459 /* Build new TYPE_METHODS. */
10460 tree r;
10461
10462 if (TREE_CODE (t) == TEMPLATE_DECL)
10463 ++processing_template_decl;
10464 r = tsubst (t, args, tf_error, NULL_TREE);
10465 if (TREE_CODE (t) == TEMPLATE_DECL)
10466 --processing_template_decl;
10467 set_current_access_from_decl (r);
10468 finish_member_declaration (r);
10469 /* Instantiate members marked with attribute used. */
10470 if (r != error_mark_node && DECL_PRESERVE_P (r))
10471 mark_used (r);
10472 if (TREE_CODE (r) == FUNCTION_DECL
10473 && DECL_OMP_DECLARE_REDUCTION_P (r))
10474 cp_check_omp_declare_reduction (r);
10475 }
10476 else if (DECL_CLASS_TEMPLATE_P (t)
10477 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10478 /* A closure type for a lambda in a default argument for a
10479 member template. Ignore it; it will be instantiated with
10480 the default argument. */;
10481 else
10482 {
10483 /* Build new TYPE_FIELDS. */
10484 if (TREE_CODE (t) == STATIC_ASSERT)
10485 {
10486 tree condition;
10487
10488 ++c_inhibit_evaluation_warnings;
10489 condition =
10490 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10491 tf_warning_or_error, NULL_TREE,
10492 /*integral_constant_expression_p=*/true);
10493 --c_inhibit_evaluation_warnings;
10494
10495 finish_static_assert (condition,
10496 STATIC_ASSERT_MESSAGE (t),
10497 STATIC_ASSERT_SOURCE_LOCATION (t),
10498 /*member_p=*/true);
10499 }
10500 else if (TREE_CODE (t) != CONST_DECL)
10501 {
10502 tree r;
10503 tree vec = NULL_TREE;
10504 int len = 1;
10505
10506 /* The file and line for this declaration, to
10507 assist in error message reporting. Since we
10508 called push_tinst_level above, we don't need to
10509 restore these. */
10510 input_location = DECL_SOURCE_LOCATION (t);
10511
10512 if (TREE_CODE (t) == TEMPLATE_DECL)
10513 ++processing_template_decl;
10514 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10515 if (TREE_CODE (t) == TEMPLATE_DECL)
10516 --processing_template_decl;
10517
10518 if (TREE_CODE (r) == TREE_VEC)
10519 {
10520 /* A capture pack became multiple fields. */
10521 vec = r;
10522 len = TREE_VEC_LENGTH (vec);
10523 }
10524
10525 for (int i = 0; i < len; ++i)
10526 {
10527 if (vec)
10528 r = TREE_VEC_ELT (vec, i);
10529 if (VAR_P (r))
10530 {
10531 /* In [temp.inst]:
10532
10533 [t]he initialization (and any associated
10534 side-effects) of a static data member does
10535 not occur unless the static data member is
10536 itself used in a way that requires the
10537 definition of the static data member to
10538 exist.
10539
10540 Therefore, we do not substitute into the
10541 initialized for the static data member here. */
10542 finish_static_data_member_decl
10543 (r,
10544 /*init=*/NULL_TREE,
10545 /*init_const_expr_p=*/false,
10546 /*asmspec_tree=*/NULL_TREE,
10547 /*flags=*/0);
10548 /* Instantiate members marked with attribute used. */
10549 if (r != error_mark_node && DECL_PRESERVE_P (r))
10550 mark_used (r);
10551 }
10552 else if (TREE_CODE (r) == FIELD_DECL)
10553 {
10554 /* Determine whether R has a valid type and can be
10555 completed later. If R is invalid, then its type
10556 is replaced by error_mark_node. */
10557 tree rtype = TREE_TYPE (r);
10558 if (can_complete_type_without_circularity (rtype))
10559 complete_type (rtype);
10560
10561 if (!complete_or_array_type_p (rtype))
10562 {
10563 /* If R's type couldn't be completed and
10564 it isn't a flexible array member (whose
10565 type is incomplete by definition) give
10566 an error. */
10567 cxx_incomplete_type_error (r, rtype);
10568 TREE_TYPE (r) = error_mark_node;
10569 }
10570 }
10571
10572 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10573 such a thing will already have been added to the field
10574 list by tsubst_enum in finish_member_declaration in the
10575 CLASSTYPE_NESTED_UTDS case above. */
10576 if (!(TREE_CODE (r) == TYPE_DECL
10577 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10578 && DECL_ARTIFICIAL (r)))
10579 {
10580 set_current_access_from_decl (r);
10581 finish_member_declaration (r);
10582 }
10583 }
10584 }
10585 }
10586 }
10587 else
10588 {
10589 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10590 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10591 {
10592 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10593
10594 tree friend_type = t;
10595 bool adjust_processing_template_decl = false;
10596
10597 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10598 {
10599 /* template <class T> friend class C; */
10600 friend_type = tsubst_friend_class (friend_type, args);
10601 adjust_processing_template_decl = true;
10602 }
10603 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10604 {
10605 /* template <class T> friend class C::D; */
10606 friend_type = tsubst (friend_type, args,
10607 tf_warning_or_error, NULL_TREE);
10608 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10609 friend_type = TREE_TYPE (friend_type);
10610 adjust_processing_template_decl = true;
10611 }
10612 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10613 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10614 {
10615 /* This could be either
10616
10617 friend class T::C;
10618
10619 when dependent_type_p is false or
10620
10621 template <class U> friend class T::C;
10622
10623 otherwise. */
10624 /* Bump processing_template_decl in case this is something like
10625 template <class T> friend struct A<T>::B. */
10626 ++processing_template_decl;
10627 friend_type = tsubst (friend_type, args,
10628 tf_warning_or_error, NULL_TREE);
10629 if (dependent_type_p (friend_type))
10630 adjust_processing_template_decl = true;
10631 --processing_template_decl;
10632 }
10633 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10634 && hidden_name_p (TYPE_NAME (friend_type)))
10635 {
10636 /* friend class C;
10637
10638 where C hasn't been declared yet. Let's lookup name
10639 from namespace scope directly, bypassing any name that
10640 come from dependent base class. */
10641 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10642
10643 /* The call to xref_tag_from_type does injection for friend
10644 classes. */
10645 push_nested_namespace (ns);
10646 friend_type =
10647 xref_tag_from_type (friend_type, NULL_TREE,
10648 /*tag_scope=*/ts_current);
10649 pop_nested_namespace (ns);
10650 }
10651 else if (uses_template_parms (friend_type))
10652 /* friend class C<T>; */
10653 friend_type = tsubst (friend_type, args,
10654 tf_warning_or_error, NULL_TREE);
10655 /* Otherwise it's
10656
10657 friend class C;
10658
10659 where C is already declared or
10660
10661 friend class C<int>;
10662
10663 We don't have to do anything in these cases. */
10664
10665 if (adjust_processing_template_decl)
10666 /* Trick make_friend_class into realizing that the friend
10667 we're adding is a template, not an ordinary class. It's
10668 important that we use make_friend_class since it will
10669 perform some error-checking and output cross-reference
10670 information. */
10671 ++processing_template_decl;
10672
10673 if (friend_type != error_mark_node)
10674 make_friend_class (type, friend_type, /*complain=*/false);
10675
10676 if (adjust_processing_template_decl)
10677 --processing_template_decl;
10678 }
10679 else
10680 {
10681 /* Build new DECL_FRIENDLIST. */
10682 tree r;
10683
10684 /* The file and line for this declaration, to
10685 assist in error message reporting. Since we
10686 called push_tinst_level above, we don't need to
10687 restore these. */
10688 input_location = DECL_SOURCE_LOCATION (t);
10689
10690 if (TREE_CODE (t) == TEMPLATE_DECL)
10691 {
10692 ++processing_template_decl;
10693 push_deferring_access_checks (dk_no_check);
10694 }
10695
10696 r = tsubst_friend_function (t, args);
10697 add_friend (type, r, /*complain=*/false);
10698 if (TREE_CODE (t) == TEMPLATE_DECL)
10699 {
10700 pop_deferring_access_checks ();
10701 --processing_template_decl;
10702 }
10703 }
10704 }
10705 }
10706
10707 if (fn_context)
10708 {
10709 /* Restore these before substituting into the lambda capture
10710 initializers. */
10711 cp_unevaluated_operand = saved_unevaluated_operand;
10712 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10713 }
10714
10715 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10716 {
10717 tree decl = lambda_function (type);
10718 if (decl)
10719 {
10720 if (cxx_dialect >= cxx1z)
10721 CLASSTYPE_LITERAL_P (type) = true;
10722
10723 if (!DECL_TEMPLATE_INFO (decl)
10724 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10725 {
10726 /* Set function_depth to avoid garbage collection. */
10727 ++function_depth;
10728 instantiate_decl (decl, /*defer_ok=*/false, false);
10729 --function_depth;
10730 }
10731
10732 /* We need to instantiate the capture list from the template
10733 after we've instantiated the closure members, but before we
10734 consider adding the conversion op. Also keep any captures
10735 that may have been added during instantiation of the op(). */
10736 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10737 tree tmpl_cap
10738 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10739 args, tf_warning_or_error, NULL_TREE,
10740 false, false);
10741
10742 LAMBDA_EXPR_CAPTURE_LIST (expr)
10743 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10744
10745 maybe_add_lambda_conv_op (type);
10746 }
10747 else
10748 gcc_assert (errorcount);
10749 }
10750
10751 /* Set the file and line number information to whatever is given for
10752 the class itself. This puts error messages involving generated
10753 implicit functions at a predictable point, and the same point
10754 that would be used for non-template classes. */
10755 input_location = DECL_SOURCE_LOCATION (typedecl);
10756
10757 unreverse_member_declarations (type);
10758 finish_struct_1 (type);
10759 TYPE_BEING_DEFINED (type) = 0;
10760
10761 /* We don't instantiate default arguments for member functions. 14.7.1:
10762
10763 The implicit instantiation of a class template specialization causes
10764 the implicit instantiation of the declarations, but not of the
10765 definitions or default arguments, of the class member functions,
10766 member classes, static data members and member templates.... */
10767
10768 /* Some typedefs referenced from within the template code need to be access
10769 checked at template instantiation time, i.e now. These types were
10770 added to the template at parsing time. Let's get those and perform
10771 the access checks then. */
10772 perform_typedefs_access_check (pattern, args);
10773 perform_deferred_access_checks (tf_warning_or_error);
10774 pop_nested_class ();
10775 maximum_field_alignment = saved_maximum_field_alignment;
10776 if (!fn_context)
10777 pop_from_top_level ();
10778 pop_deferring_access_checks ();
10779 pop_tinst_level ();
10780
10781 /* The vtable for a template class can be emitted in any translation
10782 unit in which the class is instantiated. When there is no key
10783 method, however, finish_struct_1 will already have added TYPE to
10784 the keyed_classes list. */
10785 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10786 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10787
10788 return type;
10789 }
10790
10791 /* Wrapper for instantiate_class_template_1. */
10792
10793 tree
10794 instantiate_class_template (tree type)
10795 {
10796 tree ret;
10797 timevar_push (TV_TEMPLATE_INST);
10798 ret = instantiate_class_template_1 (type);
10799 timevar_pop (TV_TEMPLATE_INST);
10800 return ret;
10801 }
10802
10803 static tree
10804 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10805 {
10806 tree r;
10807
10808 if (!t)
10809 r = t;
10810 else if (TYPE_P (t))
10811 r = tsubst (t, args, complain, in_decl);
10812 else
10813 {
10814 if (!(complain & tf_warning))
10815 ++c_inhibit_evaluation_warnings;
10816 r = tsubst_expr (t, args, complain, in_decl,
10817 /*integral_constant_expression_p=*/true);
10818 if (!(complain & tf_warning))
10819 --c_inhibit_evaluation_warnings;
10820 }
10821 return r;
10822 }
10823
10824 /* Given a function parameter pack TMPL_PARM and some function parameters
10825 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10826 and set *SPEC_P to point at the next point in the list. */
10827
10828 tree
10829 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10830 {
10831 /* Collect all of the extra "packed" parameters into an
10832 argument pack. */
10833 tree parmvec;
10834 tree parmtypevec;
10835 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10836 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10837 tree spec_parm = *spec_p;
10838 int i, len;
10839
10840 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10841 if (tmpl_parm
10842 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10843 break;
10844
10845 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10846 parmvec = make_tree_vec (len);
10847 parmtypevec = make_tree_vec (len);
10848 spec_parm = *spec_p;
10849 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10850 {
10851 TREE_VEC_ELT (parmvec, i) = spec_parm;
10852 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10853 }
10854
10855 /* Build the argument packs. */
10856 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10857 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10858 TREE_TYPE (argpack) = argtypepack;
10859 *spec_p = spec_parm;
10860
10861 return argpack;
10862 }
10863
10864 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10865 NONTYPE_ARGUMENT_PACK. */
10866
10867 static tree
10868 make_fnparm_pack (tree spec_parm)
10869 {
10870 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10871 }
10872
10873 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10874 pack expansion with no extra args, 2 if it has extra args, or 0
10875 if it is not a pack expansion. */
10876
10877 static int
10878 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10879 {
10880 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10881 if (i >= TREE_VEC_LENGTH (vec))
10882 return 0;
10883 tree elt = TREE_VEC_ELT (vec, i);
10884 if (DECL_P (elt))
10885 /* A decl pack is itself an expansion. */
10886 elt = TREE_TYPE (elt);
10887 if (!PACK_EXPANSION_P (elt))
10888 return 0;
10889 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10890 return 2;
10891 return 1;
10892 }
10893
10894
10895 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10896
10897 static tree
10898 make_argument_pack_select (tree arg_pack, unsigned index)
10899 {
10900 tree aps = make_node (ARGUMENT_PACK_SELECT);
10901
10902 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10903 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10904
10905 return aps;
10906 }
10907
10908 /* This is a subroutine of tsubst_pack_expansion.
10909
10910 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10911 mechanism to store the (non complete list of) arguments of the
10912 substitution and return a non substituted pack expansion, in order
10913 to wait for when we have enough arguments to really perform the
10914 substitution. */
10915
10916 static bool
10917 use_pack_expansion_extra_args_p (tree parm_packs,
10918 int arg_pack_len,
10919 bool has_empty_arg)
10920 {
10921 /* If one pack has an expansion and another pack has a normal
10922 argument or if one pack has an empty argument and an another
10923 one hasn't then tsubst_pack_expansion cannot perform the
10924 substitution and need to fall back on the
10925 PACK_EXPANSION_EXTRA mechanism. */
10926 if (parm_packs == NULL_TREE)
10927 return false;
10928 else if (has_empty_arg)
10929 return true;
10930
10931 bool has_expansion_arg = false;
10932 for (int i = 0 ; i < arg_pack_len; ++i)
10933 {
10934 bool has_non_expansion_arg = false;
10935 for (tree parm_pack = parm_packs;
10936 parm_pack;
10937 parm_pack = TREE_CHAIN (parm_pack))
10938 {
10939 tree arg = TREE_VALUE (parm_pack);
10940
10941 int exp = argument_pack_element_is_expansion_p (arg, i);
10942 if (exp == 2)
10943 /* We can't substitute a pack expansion with extra args into
10944 our pattern. */
10945 return true;
10946 else if (exp)
10947 has_expansion_arg = true;
10948 else
10949 has_non_expansion_arg = true;
10950 }
10951
10952 if (has_expansion_arg && has_non_expansion_arg)
10953 return true;
10954 }
10955 return false;
10956 }
10957
10958 /* [temp.variadic]/6 says that:
10959
10960 The instantiation of a pack expansion [...]
10961 produces a list E1,E2, ..., En, where N is the number of elements
10962 in the pack expansion parameters.
10963
10964 This subroutine of tsubst_pack_expansion produces one of these Ei.
10965
10966 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10967 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10968 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10969 INDEX is the index 'i' of the element Ei to produce. ARGS,
10970 COMPLAIN, and IN_DECL are the same parameters as for the
10971 tsubst_pack_expansion function.
10972
10973 The function returns the resulting Ei upon successful completion,
10974 or error_mark_node.
10975
10976 Note that this function possibly modifies the ARGS parameter, so
10977 it's the responsibility of the caller to restore it. */
10978
10979 static tree
10980 gen_elem_of_pack_expansion_instantiation (tree pattern,
10981 tree parm_packs,
10982 unsigned index,
10983 tree args /* This parm gets
10984 modified. */,
10985 tsubst_flags_t complain,
10986 tree in_decl)
10987 {
10988 tree t;
10989 bool ith_elem_is_expansion = false;
10990
10991 /* For each parameter pack, change the substitution of the parameter
10992 pack to the ith argument in its argument pack, then expand the
10993 pattern. */
10994 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10995 {
10996 tree parm = TREE_PURPOSE (pack);
10997 tree arg_pack = TREE_VALUE (pack);
10998 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10999
11000 ith_elem_is_expansion |=
11001 argument_pack_element_is_expansion_p (arg_pack, index);
11002
11003 /* Select the Ith argument from the pack. */
11004 if (TREE_CODE (parm) == PARM_DECL
11005 || TREE_CODE (parm) == FIELD_DECL)
11006 {
11007 if (index == 0)
11008 {
11009 aps = make_argument_pack_select (arg_pack, index);
11010 if (!mark_used (parm, complain) && !(complain & tf_error))
11011 return error_mark_node;
11012 register_local_specialization (aps, parm);
11013 }
11014 else
11015 aps = retrieve_local_specialization (parm);
11016 }
11017 else
11018 {
11019 int idx, level;
11020 template_parm_level_and_index (parm, &level, &idx);
11021
11022 if (index == 0)
11023 {
11024 aps = make_argument_pack_select (arg_pack, index);
11025 /* Update the corresponding argument. */
11026 TMPL_ARG (args, level, idx) = aps;
11027 }
11028 else
11029 /* Re-use the ARGUMENT_PACK_SELECT. */
11030 aps = TMPL_ARG (args, level, idx);
11031 }
11032 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11033 }
11034
11035 /* Substitute into the PATTERN with the (possibly altered)
11036 arguments. */
11037 if (pattern == in_decl)
11038 /* Expanding a fixed parameter pack from
11039 coerce_template_parameter_pack. */
11040 t = tsubst_decl (pattern, args, complain);
11041 else if (pattern == error_mark_node)
11042 t = error_mark_node;
11043 else if (constraint_p (pattern))
11044 {
11045 if (processing_template_decl)
11046 t = tsubst_constraint (pattern, args, complain, in_decl);
11047 else
11048 t = (constraints_satisfied_p (pattern, args)
11049 ? boolean_true_node : boolean_false_node);
11050 }
11051 else if (!TYPE_P (pattern))
11052 t = tsubst_expr (pattern, args, complain, in_decl,
11053 /*integral_constant_expression_p=*/false);
11054 else
11055 t = tsubst (pattern, args, complain, in_decl);
11056
11057 /* If the Ith argument pack element is a pack expansion, then
11058 the Ith element resulting from the substituting is going to
11059 be a pack expansion as well. */
11060 if (ith_elem_is_expansion)
11061 t = make_pack_expansion (t);
11062
11063 return t;
11064 }
11065
11066 /* When the unexpanded parameter pack in a fold expression expands to an empty
11067 sequence, the value of the expression is as follows; the program is
11068 ill-formed if the operator is not listed in this table.
11069
11070 && true
11071 || false
11072 , void() */
11073
11074 tree
11075 expand_empty_fold (tree t, tsubst_flags_t complain)
11076 {
11077 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11078 if (!FOLD_EXPR_MODIFY_P (t))
11079 switch (code)
11080 {
11081 case TRUTH_ANDIF_EXPR:
11082 return boolean_true_node;
11083 case TRUTH_ORIF_EXPR:
11084 return boolean_false_node;
11085 case COMPOUND_EXPR:
11086 return void_node;
11087 default:
11088 break;
11089 }
11090
11091 if (complain & tf_error)
11092 error_at (location_of (t),
11093 "fold of empty expansion over %O", code);
11094 return error_mark_node;
11095 }
11096
11097 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11098 form an expression that combines the two terms using the
11099 operator of T. */
11100
11101 static tree
11102 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11103 {
11104 tree op = FOLD_EXPR_OP (t);
11105 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11106
11107 // Handle compound assignment operators.
11108 if (FOLD_EXPR_MODIFY_P (t))
11109 return build_x_modify_expr (input_location, left, code, right, complain);
11110
11111 switch (code)
11112 {
11113 case COMPOUND_EXPR:
11114 return build_x_compound_expr (input_location, left, right, complain);
11115 case DOTSTAR_EXPR:
11116 return build_m_component_ref (left, right, complain);
11117 default:
11118 return build_x_binary_op (input_location, code,
11119 left, TREE_CODE (left),
11120 right, TREE_CODE (right),
11121 /*overload=*/NULL,
11122 complain);
11123 }
11124 }
11125
11126 /* Substitute ARGS into the pack of a fold expression T. */
11127
11128 static inline tree
11129 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11130 {
11131 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11132 }
11133
11134 /* Substitute ARGS into the pack of a fold expression T. */
11135
11136 static inline tree
11137 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11138 {
11139 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11140 }
11141
11142 /* Expand a PACK of arguments into a grouped as left fold.
11143 Given a pack containing elements A0, A1, ..., An and an
11144 operator @, this builds the expression:
11145
11146 ((A0 @ A1) @ A2) ... @ An
11147
11148 Note that PACK must not be empty.
11149
11150 The operator is defined by the original fold expression T. */
11151
11152 static tree
11153 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11154 {
11155 tree left = TREE_VEC_ELT (pack, 0);
11156 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11157 {
11158 tree right = TREE_VEC_ELT (pack, i);
11159 left = fold_expression (t, left, right, complain);
11160 }
11161 return left;
11162 }
11163
11164 /* Substitute into a unary left fold expression. */
11165
11166 static tree
11167 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11168 tree in_decl)
11169 {
11170 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11171 if (pack == error_mark_node)
11172 return error_mark_node;
11173 if (PACK_EXPANSION_P (pack))
11174 {
11175 tree r = copy_node (t);
11176 FOLD_EXPR_PACK (r) = pack;
11177 return r;
11178 }
11179 if (TREE_VEC_LENGTH (pack) == 0)
11180 return expand_empty_fold (t, complain);
11181 else
11182 return expand_left_fold (t, pack, complain);
11183 }
11184
11185 /* Substitute into a binary left fold expression.
11186
11187 Do ths by building a single (non-empty) vector of argumnts and
11188 building the expression from those elements. */
11189
11190 static tree
11191 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11192 tree in_decl)
11193 {
11194 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11195 if (pack == error_mark_node)
11196 return error_mark_node;
11197 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11198 if (init == error_mark_node)
11199 return error_mark_node;
11200
11201 if (PACK_EXPANSION_P (pack))
11202 {
11203 tree r = copy_node (t);
11204 FOLD_EXPR_PACK (r) = pack;
11205 FOLD_EXPR_INIT (r) = init;
11206 return r;
11207 }
11208
11209 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11210 TREE_VEC_ELT (vec, 0) = init;
11211 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11212 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11213
11214 return expand_left_fold (t, vec, complain);
11215 }
11216
11217 /* Expand a PACK of arguments into a grouped as right fold.
11218 Given a pack containing elementns A0, A1, ..., and an
11219 operator @, this builds the expression:
11220
11221 A0@ ... (An-2 @ (An-1 @ An))
11222
11223 Note that PACK must not be empty.
11224
11225 The operator is defined by the original fold expression T. */
11226
11227 tree
11228 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11229 {
11230 // Build the expression.
11231 int n = TREE_VEC_LENGTH (pack);
11232 tree right = TREE_VEC_ELT (pack, n - 1);
11233 for (--n; n != 0; --n)
11234 {
11235 tree left = TREE_VEC_ELT (pack, n - 1);
11236 right = fold_expression (t, left, right, complain);
11237 }
11238 return right;
11239 }
11240
11241 /* Substitute into a unary right fold expression. */
11242
11243 static tree
11244 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11245 tree in_decl)
11246 {
11247 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11248 if (pack == error_mark_node)
11249 return error_mark_node;
11250 if (PACK_EXPANSION_P (pack))
11251 {
11252 tree r = copy_node (t);
11253 FOLD_EXPR_PACK (r) = pack;
11254 return r;
11255 }
11256 if (TREE_VEC_LENGTH (pack) == 0)
11257 return expand_empty_fold (t, complain);
11258 else
11259 return expand_right_fold (t, pack, complain);
11260 }
11261
11262 /* Substitute into a binary right fold expression.
11263
11264 Do ths by building a single (non-empty) vector of arguments and
11265 building the expression from those elements. */
11266
11267 static tree
11268 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11269 tree in_decl)
11270 {
11271 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11272 if (pack == error_mark_node)
11273 return error_mark_node;
11274 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11275 if (init == error_mark_node)
11276 return error_mark_node;
11277
11278 if (PACK_EXPANSION_P (pack))
11279 {
11280 tree r = copy_node (t);
11281 FOLD_EXPR_PACK (r) = pack;
11282 FOLD_EXPR_INIT (r) = init;
11283 return r;
11284 }
11285
11286 int n = TREE_VEC_LENGTH (pack);
11287 tree vec = make_tree_vec (n + 1);
11288 for (int i = 0; i < n; ++i)
11289 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11290 TREE_VEC_ELT (vec, n) = init;
11291
11292 return expand_right_fold (t, vec, complain);
11293 }
11294
11295
11296 /* Substitute ARGS into T, which is an pack expansion
11297 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11298 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11299 (if only a partial substitution could be performed) or
11300 ERROR_MARK_NODE if there was an error. */
11301 tree
11302 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11303 tree in_decl)
11304 {
11305 tree pattern;
11306 tree pack, packs = NULL_TREE;
11307 bool unsubstituted_packs = false;
11308 int i, len = -1;
11309 tree result;
11310 hash_map<tree, tree> *saved_local_specializations = NULL;
11311 bool need_local_specializations = false;
11312 int levels;
11313
11314 gcc_assert (PACK_EXPANSION_P (t));
11315 pattern = PACK_EXPANSION_PATTERN (t);
11316
11317 /* Add in any args remembered from an earlier partial instantiation. */
11318 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11319
11320 levels = TMPL_ARGS_DEPTH (args);
11321
11322 /* Determine the argument packs that will instantiate the parameter
11323 packs used in the expansion expression. While we're at it,
11324 compute the number of arguments to be expanded and make sure it
11325 is consistent. */
11326 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11327 pack = TREE_CHAIN (pack))
11328 {
11329 tree parm_pack = TREE_VALUE (pack);
11330 tree arg_pack = NULL_TREE;
11331 tree orig_arg = NULL_TREE;
11332 int level = 0;
11333
11334 if (TREE_CODE (parm_pack) == BASES)
11335 {
11336 if (BASES_DIRECT (parm_pack))
11337 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11338 args, complain, in_decl, false));
11339 else
11340 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11341 args, complain, in_decl, false));
11342 }
11343 if (TREE_CODE (parm_pack) == PARM_DECL)
11344 {
11345 /* We know we have correct local_specializations if this
11346 expansion is at function scope, or if we're dealing with a
11347 local parameter in a requires expression; for the latter,
11348 tsubst_requires_expr set it up appropriately. */
11349 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11350 arg_pack = retrieve_local_specialization (parm_pack);
11351 else
11352 /* We can't rely on local_specializations for a parameter
11353 name used later in a function declaration (such as in a
11354 late-specified return type). Even if it exists, it might
11355 have the wrong value for a recursive call. */
11356 need_local_specializations = true;
11357
11358 if (!arg_pack)
11359 {
11360 /* This parameter pack was used in an unevaluated context. Just
11361 make a dummy decl, since it's only used for its type. */
11362 arg_pack = tsubst_decl (parm_pack, args, complain);
11363 if (arg_pack && DECL_PACK_P (arg_pack))
11364 /* Partial instantiation of the parm_pack, we can't build
11365 up an argument pack yet. */
11366 arg_pack = NULL_TREE;
11367 else
11368 arg_pack = make_fnparm_pack (arg_pack);
11369 }
11370 }
11371 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11372 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11373 else
11374 {
11375 int idx;
11376 template_parm_level_and_index (parm_pack, &level, &idx);
11377
11378 if (level <= levels)
11379 arg_pack = TMPL_ARG (args, level, idx);
11380 }
11381
11382 orig_arg = arg_pack;
11383 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11384 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11385
11386 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11387 /* This can only happen if we forget to expand an argument
11388 pack somewhere else. Just return an error, silently. */
11389 {
11390 result = make_tree_vec (1);
11391 TREE_VEC_ELT (result, 0) = error_mark_node;
11392 return result;
11393 }
11394
11395 if (arg_pack)
11396 {
11397 int my_len =
11398 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11399
11400 /* Don't bother trying to do a partial substitution with
11401 incomplete packs; we'll try again after deduction. */
11402 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11403 return t;
11404
11405 if (len < 0)
11406 len = my_len;
11407 else if (len != my_len)
11408 {
11409 if (!(complain & tf_error))
11410 /* Fail quietly. */;
11411 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11412 error ("mismatched argument pack lengths while expanding "
11413 "%<%T%>",
11414 pattern);
11415 else
11416 error ("mismatched argument pack lengths while expanding "
11417 "%<%E%>",
11418 pattern);
11419 return error_mark_node;
11420 }
11421
11422 /* Keep track of the parameter packs and their corresponding
11423 argument packs. */
11424 packs = tree_cons (parm_pack, arg_pack, packs);
11425 TREE_TYPE (packs) = orig_arg;
11426 }
11427 else
11428 {
11429 /* We can't substitute for this parameter pack. We use a flag as
11430 well as the missing_level counter because function parameter
11431 packs don't have a level. */
11432 gcc_assert (processing_template_decl);
11433 unsubstituted_packs = true;
11434 }
11435 }
11436
11437 /* If the expansion is just T..., return the matching argument pack, unless
11438 we need to call convert_from_reference on all the elements. This is an
11439 important optimization; see c++/68422. */
11440 if (!unsubstituted_packs
11441 && TREE_PURPOSE (packs) == pattern)
11442 {
11443 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11444 /* Types need no adjustment, nor does sizeof..., and if we still have
11445 some pack expansion args we won't do anything yet. */
11446 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11447 || PACK_EXPANSION_SIZEOF_P (t)
11448 || pack_expansion_args_count (args))
11449 return args;
11450 /* Also optimize expression pack expansions if we can tell that the
11451 elements won't have reference type. */
11452 tree type = TREE_TYPE (pattern);
11453 if (type && TREE_CODE (type) != REFERENCE_TYPE
11454 && !PACK_EXPANSION_P (type)
11455 && !WILDCARD_TYPE_P (type))
11456 return args;
11457 /* Otherwise use the normal path so we get convert_from_reference. */
11458 }
11459
11460 /* We cannot expand this expansion expression, because we don't have
11461 all of the argument packs we need. */
11462 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11463 {
11464 /* We got some full packs, but we can't substitute them in until we
11465 have values for all the packs. So remember these until then. */
11466
11467 t = make_pack_expansion (pattern);
11468 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11469 return t;
11470 }
11471 else if (unsubstituted_packs)
11472 {
11473 /* There were no real arguments, we're just replacing a parameter
11474 pack with another version of itself. Substitute into the
11475 pattern and return a PACK_EXPANSION_*. The caller will need to
11476 deal with that. */
11477 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11478 t = tsubst_expr (pattern, args, complain, in_decl,
11479 /*integral_constant_expression_p=*/false);
11480 else
11481 t = tsubst (pattern, args, complain, in_decl);
11482 t = make_pack_expansion (t);
11483 return t;
11484 }
11485
11486 gcc_assert (len >= 0);
11487
11488 if (need_local_specializations)
11489 {
11490 /* We're in a late-specified return type, so create our own local
11491 specializations map; the current map is either NULL or (in the
11492 case of recursive unification) might have bindings that we don't
11493 want to use or alter. */
11494 saved_local_specializations = local_specializations;
11495 local_specializations = new hash_map<tree, tree>;
11496 }
11497
11498 /* For each argument in each argument pack, substitute into the
11499 pattern. */
11500 result = make_tree_vec (len);
11501 tree elem_args = copy_template_args (args);
11502 for (i = 0; i < len; ++i)
11503 {
11504 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11505 i,
11506 elem_args, complain,
11507 in_decl);
11508 TREE_VEC_ELT (result, i) = t;
11509 if (t == error_mark_node)
11510 {
11511 result = error_mark_node;
11512 break;
11513 }
11514 }
11515
11516 /* Update ARGS to restore the substitution from parameter packs to
11517 their argument packs. */
11518 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11519 {
11520 tree parm = TREE_PURPOSE (pack);
11521
11522 if (TREE_CODE (parm) == PARM_DECL
11523 || TREE_CODE (parm) == FIELD_DECL)
11524 register_local_specialization (TREE_TYPE (pack), parm);
11525 else
11526 {
11527 int idx, level;
11528
11529 if (TREE_VALUE (pack) == NULL_TREE)
11530 continue;
11531
11532 template_parm_level_and_index (parm, &level, &idx);
11533
11534 /* Update the corresponding argument. */
11535 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11536 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11537 TREE_TYPE (pack);
11538 else
11539 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11540 }
11541 }
11542
11543 if (need_local_specializations)
11544 {
11545 delete local_specializations;
11546 local_specializations = saved_local_specializations;
11547 }
11548
11549 /* If the dependent pack arguments were such that we end up with only a
11550 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11551 if (len == 1 && TREE_CODE (result) == TREE_VEC
11552 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11553 return TREE_VEC_ELT (result, 0);
11554
11555 return result;
11556 }
11557
11558 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11559 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11560 parameter packs; all parms generated from a function parameter pack will
11561 have the same DECL_PARM_INDEX. */
11562
11563 tree
11564 get_pattern_parm (tree parm, tree tmpl)
11565 {
11566 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11567 tree patparm;
11568
11569 if (DECL_ARTIFICIAL (parm))
11570 {
11571 for (patparm = DECL_ARGUMENTS (pattern);
11572 patparm; patparm = DECL_CHAIN (patparm))
11573 if (DECL_ARTIFICIAL (patparm)
11574 && DECL_NAME (parm) == DECL_NAME (patparm))
11575 break;
11576 }
11577 else
11578 {
11579 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11580 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11581 gcc_assert (DECL_PARM_INDEX (patparm)
11582 == DECL_PARM_INDEX (parm));
11583 }
11584
11585 return patparm;
11586 }
11587
11588 /* Make an argument pack out of the TREE_VEC VEC. */
11589
11590 static tree
11591 make_argument_pack (tree vec)
11592 {
11593 tree pack;
11594 tree elt = TREE_VEC_ELT (vec, 0);
11595 if (TYPE_P (elt))
11596 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11597 else
11598 {
11599 pack = make_node (NONTYPE_ARGUMENT_PACK);
11600 TREE_TYPE (pack) = TREE_TYPE (elt);
11601 TREE_CONSTANT (pack) = 1;
11602 }
11603 SET_ARGUMENT_PACK_ARGS (pack, vec);
11604 return pack;
11605 }
11606
11607 /* Return an exact copy of template args T that can be modified
11608 independently. */
11609
11610 static tree
11611 copy_template_args (tree t)
11612 {
11613 if (t == error_mark_node)
11614 return t;
11615
11616 int len = TREE_VEC_LENGTH (t);
11617 tree new_vec = make_tree_vec (len);
11618
11619 for (int i = 0; i < len; ++i)
11620 {
11621 tree elt = TREE_VEC_ELT (t, i);
11622 if (elt && TREE_CODE (elt) == TREE_VEC)
11623 elt = copy_template_args (elt);
11624 TREE_VEC_ELT (new_vec, i) = elt;
11625 }
11626
11627 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11628 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11629
11630 return new_vec;
11631 }
11632
11633 /* Substitute ARGS into the vector or list of template arguments T. */
11634
11635 static tree
11636 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11637 {
11638 tree orig_t = t;
11639 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11640 tree *elts;
11641
11642 if (t == error_mark_node)
11643 return error_mark_node;
11644
11645 len = TREE_VEC_LENGTH (t);
11646 elts = XALLOCAVEC (tree, len);
11647
11648 for (i = 0; i < len; i++)
11649 {
11650 tree orig_arg = TREE_VEC_ELT (t, i);
11651 tree new_arg;
11652
11653 if (TREE_CODE (orig_arg) == TREE_VEC)
11654 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11655 else if (PACK_EXPANSION_P (orig_arg))
11656 {
11657 /* Substitute into an expansion expression. */
11658 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11659
11660 if (TREE_CODE (new_arg) == TREE_VEC)
11661 /* Add to the expanded length adjustment the number of
11662 expanded arguments. We subtract one from this
11663 measurement, because the argument pack expression
11664 itself is already counted as 1 in
11665 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11666 the argument pack is empty. */
11667 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11668 }
11669 else if (ARGUMENT_PACK_P (orig_arg))
11670 {
11671 /* Substitute into each of the arguments. */
11672 new_arg = TYPE_P (orig_arg)
11673 ? cxx_make_type (TREE_CODE (orig_arg))
11674 : make_node (TREE_CODE (orig_arg));
11675
11676 SET_ARGUMENT_PACK_ARGS (
11677 new_arg,
11678 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11679 args, complain, in_decl));
11680
11681 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11682 new_arg = error_mark_node;
11683
11684 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11685 if (type_uses_auto (TREE_TYPE (orig_arg)))
11686 TREE_TYPE (new_arg) = TREE_TYPE (orig_arg);
11687 else
11688 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11689 complain, in_decl);
11690 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11691
11692 if (TREE_TYPE (new_arg) == error_mark_node)
11693 new_arg = error_mark_node;
11694 }
11695 }
11696 else
11697 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11698
11699 if (new_arg == error_mark_node)
11700 return error_mark_node;
11701
11702 elts[i] = new_arg;
11703 if (new_arg != orig_arg)
11704 need_new = 1;
11705 }
11706
11707 if (!need_new)
11708 return t;
11709
11710 /* Make space for the expanded arguments coming from template
11711 argument packs. */
11712 t = make_tree_vec (len + expanded_len_adjust);
11713 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11714 arguments for a member template.
11715 In that case each TREE_VEC in ORIG_T represents a level of template
11716 arguments, and ORIG_T won't carry any non defaulted argument count.
11717 It will rather be the nested TREE_VECs that will carry one.
11718 In other words, ORIG_T carries a non defaulted argument count only
11719 if it doesn't contain any nested TREE_VEC. */
11720 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11721 {
11722 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11723 count += expanded_len_adjust;
11724 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11725 }
11726 for (i = 0, out = 0; i < len; i++)
11727 {
11728 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11729 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11730 && TREE_CODE (elts[i]) == TREE_VEC)
11731 {
11732 int idx;
11733
11734 /* Now expand the template argument pack "in place". */
11735 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11736 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11737 }
11738 else
11739 {
11740 TREE_VEC_ELT (t, out) = elts[i];
11741 out++;
11742 }
11743 }
11744
11745 return t;
11746 }
11747
11748 /* Substitute ARGS into one level PARMS of template parameters. */
11749
11750 static tree
11751 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11752 {
11753 if (parms == error_mark_node)
11754 return error_mark_node;
11755
11756 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11757
11758 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11759 {
11760 tree tuple = TREE_VEC_ELT (parms, i);
11761
11762 if (tuple == error_mark_node)
11763 continue;
11764
11765 TREE_VEC_ELT (new_vec, i) =
11766 tsubst_template_parm (tuple, args, complain);
11767 }
11768
11769 return new_vec;
11770 }
11771
11772 /* Return the result of substituting ARGS into the template parameters
11773 given by PARMS. If there are m levels of ARGS and m + n levels of
11774 PARMS, then the result will contain n levels of PARMS. For
11775 example, if PARMS is `template <class T> template <class U>
11776 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11777 result will be `template <int*, double, class V>'. */
11778
11779 static tree
11780 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11781 {
11782 tree r = NULL_TREE;
11783 tree* new_parms;
11784
11785 /* When substituting into a template, we must set
11786 PROCESSING_TEMPLATE_DECL as the template parameters may be
11787 dependent if they are based on one-another, and the dependency
11788 predicates are short-circuit outside of templates. */
11789 ++processing_template_decl;
11790
11791 for (new_parms = &r;
11792 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11793 new_parms = &(TREE_CHAIN (*new_parms)),
11794 parms = TREE_CHAIN (parms))
11795 {
11796 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11797 args, complain);
11798 *new_parms =
11799 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11800 - TMPL_ARGS_DEPTH (args)),
11801 new_vec, NULL_TREE);
11802 }
11803
11804 --processing_template_decl;
11805
11806 return r;
11807 }
11808
11809 /* Return the result of substituting ARGS into one template parameter
11810 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11811 parameter and which TREE_PURPOSE is the default argument of the
11812 template parameter. */
11813
11814 static tree
11815 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11816 {
11817 tree default_value, parm_decl;
11818
11819 if (args == NULL_TREE
11820 || t == NULL_TREE
11821 || t == error_mark_node)
11822 return t;
11823
11824 gcc_assert (TREE_CODE (t) == TREE_LIST);
11825
11826 default_value = TREE_PURPOSE (t);
11827 parm_decl = TREE_VALUE (t);
11828
11829 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11830 if (TREE_CODE (parm_decl) == PARM_DECL
11831 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11832 parm_decl = error_mark_node;
11833 default_value = tsubst_template_arg (default_value, args,
11834 complain, NULL_TREE);
11835
11836 return build_tree_list (default_value, parm_decl);
11837 }
11838
11839 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11840 type T. If T is not an aggregate or enumeration type, it is
11841 handled as if by tsubst. IN_DECL is as for tsubst. If
11842 ENTERING_SCOPE is nonzero, T is the context for a template which
11843 we are presently tsubst'ing. Return the substituted value. */
11844
11845 static tree
11846 tsubst_aggr_type (tree t,
11847 tree args,
11848 tsubst_flags_t complain,
11849 tree in_decl,
11850 int entering_scope)
11851 {
11852 if (t == NULL_TREE)
11853 return NULL_TREE;
11854
11855 switch (TREE_CODE (t))
11856 {
11857 case RECORD_TYPE:
11858 if (TYPE_PTRMEMFUNC_P (t))
11859 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11860
11861 /* Fall through. */
11862 case ENUMERAL_TYPE:
11863 case UNION_TYPE:
11864 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11865 {
11866 tree argvec;
11867 tree context;
11868 tree r;
11869 int saved_unevaluated_operand;
11870 int saved_inhibit_evaluation_warnings;
11871
11872 /* In "sizeof(X<I>)" we need to evaluate "I". */
11873 saved_unevaluated_operand = cp_unevaluated_operand;
11874 cp_unevaluated_operand = 0;
11875 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11876 c_inhibit_evaluation_warnings = 0;
11877
11878 /* First, determine the context for the type we are looking
11879 up. */
11880 context = TYPE_CONTEXT (t);
11881 if (context && TYPE_P (context))
11882 {
11883 context = tsubst_aggr_type (context, args, complain,
11884 in_decl, /*entering_scope=*/1);
11885 /* If context is a nested class inside a class template,
11886 it may still need to be instantiated (c++/33959). */
11887 context = complete_type (context);
11888 }
11889
11890 /* Then, figure out what arguments are appropriate for the
11891 type we are trying to find. For example, given:
11892
11893 template <class T> struct S;
11894 template <class T, class U> void f(T, U) { S<U> su; }
11895
11896 and supposing that we are instantiating f<int, double>,
11897 then our ARGS will be {int, double}, but, when looking up
11898 S we only want {double}. */
11899 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11900 complain, in_decl);
11901 if (argvec == error_mark_node)
11902 r = error_mark_node;
11903 else
11904 {
11905 r = lookup_template_class (t, argvec, in_decl, context,
11906 entering_scope, complain);
11907 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11908 }
11909
11910 cp_unevaluated_operand = saved_unevaluated_operand;
11911 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11912
11913 return r;
11914 }
11915 else
11916 /* This is not a template type, so there's nothing to do. */
11917 return t;
11918
11919 default:
11920 return tsubst (t, args, complain, in_decl);
11921 }
11922 }
11923
11924 /* Substitute into the default argument ARG (a default argument for
11925 FN), which has the indicated TYPE. */
11926
11927 tree
11928 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11929 {
11930 tree saved_class_ptr = NULL_TREE;
11931 tree saved_class_ref = NULL_TREE;
11932 int errs = errorcount + sorrycount;
11933
11934 /* This can happen in invalid code. */
11935 if (TREE_CODE (arg) == DEFAULT_ARG)
11936 return arg;
11937
11938 /* This default argument came from a template. Instantiate the
11939 default argument here, not in tsubst. In the case of
11940 something like:
11941
11942 template <class T>
11943 struct S {
11944 static T t();
11945 void f(T = t());
11946 };
11947
11948 we must be careful to do name lookup in the scope of S<T>,
11949 rather than in the current class. */
11950 push_access_scope (fn);
11951 /* The "this" pointer is not valid in a default argument. */
11952 if (cfun)
11953 {
11954 saved_class_ptr = current_class_ptr;
11955 cp_function_chain->x_current_class_ptr = NULL_TREE;
11956 saved_class_ref = current_class_ref;
11957 cp_function_chain->x_current_class_ref = NULL_TREE;
11958 }
11959
11960 push_deferring_access_checks(dk_no_deferred);
11961 /* The default argument expression may cause implicitly defined
11962 member functions to be synthesized, which will result in garbage
11963 collection. We must treat this situation as if we were within
11964 the body of function so as to avoid collecting live data on the
11965 stack. */
11966 ++function_depth;
11967 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11968 complain, NULL_TREE,
11969 /*integral_constant_expression_p=*/false);
11970 --function_depth;
11971 pop_deferring_access_checks();
11972
11973 /* Restore the "this" pointer. */
11974 if (cfun)
11975 {
11976 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11977 cp_function_chain->x_current_class_ref = saved_class_ref;
11978 }
11979
11980 if (errorcount+sorrycount > errs
11981 && (complain & tf_warning_or_error))
11982 inform (input_location,
11983 " when instantiating default argument for call to %D", fn);
11984
11985 /* Make sure the default argument is reasonable. */
11986 arg = check_default_argument (type, arg, complain);
11987
11988 pop_access_scope (fn);
11989
11990 return arg;
11991 }
11992
11993 /* Substitute into all the default arguments for FN. */
11994
11995 static void
11996 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11997 {
11998 tree arg;
11999 tree tmpl_args;
12000
12001 tmpl_args = DECL_TI_ARGS (fn);
12002
12003 /* If this function is not yet instantiated, we certainly don't need
12004 its default arguments. */
12005 if (uses_template_parms (tmpl_args))
12006 return;
12007 /* Don't do this again for clones. */
12008 if (DECL_CLONED_FUNCTION_P (fn))
12009 return;
12010
12011 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12012 arg;
12013 arg = TREE_CHAIN (arg))
12014 if (TREE_PURPOSE (arg))
12015 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
12016 TREE_VALUE (arg),
12017 TREE_PURPOSE (arg),
12018 complain);
12019 }
12020
12021 /* Substitute the ARGS into the T, which is a _DECL. Return the
12022 result of the substitution. Issue error and warning messages under
12023 control of COMPLAIN. */
12024
12025 static tree
12026 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12027 {
12028 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12029 location_t saved_loc;
12030 tree r = NULL_TREE;
12031 tree in_decl = t;
12032 hashval_t hash = 0;
12033
12034 /* Set the filename and linenumber to improve error-reporting. */
12035 saved_loc = input_location;
12036 input_location = DECL_SOURCE_LOCATION (t);
12037
12038 switch (TREE_CODE (t))
12039 {
12040 case TEMPLATE_DECL:
12041 {
12042 /* We can get here when processing a member function template,
12043 member class template, or template template parameter. */
12044 tree decl = DECL_TEMPLATE_RESULT (t);
12045 tree spec;
12046 tree tmpl_args;
12047 tree full_args;
12048
12049 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12050 {
12051 /* Template template parameter is treated here. */
12052 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12053 if (new_type == error_mark_node)
12054 r = error_mark_node;
12055 /* If we get a real template back, return it. This can happen in
12056 the context of most_specialized_partial_spec. */
12057 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12058 r = new_type;
12059 else
12060 /* The new TEMPLATE_DECL was built in
12061 reduce_template_parm_level. */
12062 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12063 break;
12064 }
12065
12066 /* We might already have an instance of this template.
12067 The ARGS are for the surrounding class type, so the
12068 full args contain the tsubst'd args for the context,
12069 plus the innermost args from the template decl. */
12070 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12071 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12072 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12073 /* Because this is a template, the arguments will still be
12074 dependent, even after substitution. If
12075 PROCESSING_TEMPLATE_DECL is not set, the dependency
12076 predicates will short-circuit. */
12077 ++processing_template_decl;
12078 full_args = tsubst_template_args (tmpl_args, args,
12079 complain, in_decl);
12080 --processing_template_decl;
12081 if (full_args == error_mark_node)
12082 RETURN (error_mark_node);
12083
12084 /* If this is a default template template argument,
12085 tsubst might not have changed anything. */
12086 if (full_args == tmpl_args)
12087 RETURN (t);
12088
12089 hash = hash_tmpl_and_args (t, full_args);
12090 spec = retrieve_specialization (t, full_args, hash);
12091 if (spec != NULL_TREE)
12092 {
12093 r = spec;
12094 break;
12095 }
12096
12097 /* Make a new template decl. It will be similar to the
12098 original, but will record the current template arguments.
12099 We also create a new function declaration, which is just
12100 like the old one, but points to this new template, rather
12101 than the old one. */
12102 r = copy_decl (t);
12103 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12104 DECL_CHAIN (r) = NULL_TREE;
12105
12106 // Build new template info linking to the original template decl.
12107 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12108
12109 if (TREE_CODE (decl) == TYPE_DECL
12110 && !TYPE_DECL_ALIAS_P (decl))
12111 {
12112 tree new_type;
12113 ++processing_template_decl;
12114 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12115 --processing_template_decl;
12116 if (new_type == error_mark_node)
12117 RETURN (error_mark_node);
12118
12119 TREE_TYPE (r) = new_type;
12120 /* For a partial specialization, we need to keep pointing to
12121 the primary template. */
12122 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12123 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12124 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12125 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12126 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12127 }
12128 else
12129 {
12130 tree new_decl;
12131 ++processing_template_decl;
12132 new_decl = tsubst (decl, args, complain, in_decl);
12133 --processing_template_decl;
12134 if (new_decl == error_mark_node)
12135 RETURN (error_mark_node);
12136
12137 DECL_TEMPLATE_RESULT (r) = new_decl;
12138 DECL_TI_TEMPLATE (new_decl) = r;
12139 TREE_TYPE (r) = TREE_TYPE (new_decl);
12140 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12141 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12142 }
12143
12144 SET_DECL_IMPLICIT_INSTANTIATION (r);
12145 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12146 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12147
12148 /* The template parameters for this new template are all the
12149 template parameters for the old template, except the
12150 outermost level of parameters. */
12151 DECL_TEMPLATE_PARMS (r)
12152 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12153 complain);
12154
12155 if (PRIMARY_TEMPLATE_P (t))
12156 DECL_PRIMARY_TEMPLATE (r) = r;
12157
12158 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12159 /* Record this non-type partial instantiation. */
12160 register_specialization (r, t,
12161 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12162 false, hash);
12163 }
12164 break;
12165
12166 case FUNCTION_DECL:
12167 {
12168 tree ctx;
12169 tree argvec = NULL_TREE;
12170 tree *friends;
12171 tree gen_tmpl;
12172 tree type;
12173 int member;
12174 int args_depth;
12175 int parms_depth;
12176
12177 /* Nobody should be tsubst'ing into non-template functions. */
12178 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12179
12180 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12181 {
12182 tree spec;
12183
12184 /* If T is not dependent, just return it. */
12185 if (!uses_template_parms (DECL_TI_ARGS (t)))
12186 RETURN (t);
12187
12188 /* Calculate the most general template of which R is a
12189 specialization, and the complete set of arguments used to
12190 specialize R. */
12191 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12192 argvec = tsubst_template_args (DECL_TI_ARGS
12193 (DECL_TEMPLATE_RESULT
12194 (DECL_TI_TEMPLATE (t))),
12195 args, complain, in_decl);
12196 if (argvec == error_mark_node)
12197 RETURN (error_mark_node);
12198
12199 /* Check to see if we already have this specialization. */
12200 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12201 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12202
12203 if (spec)
12204 {
12205 r = spec;
12206 break;
12207 }
12208
12209 /* We can see more levels of arguments than parameters if
12210 there was a specialization of a member template, like
12211 this:
12212
12213 template <class T> struct S { template <class U> void f(); }
12214 template <> template <class U> void S<int>::f(U);
12215
12216 Here, we'll be substituting into the specialization,
12217 because that's where we can find the code we actually
12218 want to generate, but we'll have enough arguments for
12219 the most general template.
12220
12221 We also deal with the peculiar case:
12222
12223 template <class T> struct S {
12224 template <class U> friend void f();
12225 };
12226 template <class U> void f() {}
12227 template S<int>;
12228 template void f<double>();
12229
12230 Here, the ARGS for the instantiation of will be {int,
12231 double}. But, we only need as many ARGS as there are
12232 levels of template parameters in CODE_PATTERN. We are
12233 careful not to get fooled into reducing the ARGS in
12234 situations like:
12235
12236 template <class T> struct S { template <class U> void f(U); }
12237 template <class T> template <> void S<T>::f(int) {}
12238
12239 which we can spot because the pattern will be a
12240 specialization in this case. */
12241 args_depth = TMPL_ARGS_DEPTH (args);
12242 parms_depth =
12243 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12244 if (args_depth > parms_depth
12245 && !DECL_TEMPLATE_SPECIALIZATION (t))
12246 args = get_innermost_template_args (args, parms_depth);
12247 }
12248 else
12249 {
12250 /* This special case arises when we have something like this:
12251
12252 template <class T> struct S {
12253 friend void f<int>(int, double);
12254 };
12255
12256 Here, the DECL_TI_TEMPLATE for the friend declaration
12257 will be an IDENTIFIER_NODE. We are being called from
12258 tsubst_friend_function, and we want only to create a
12259 new decl (R) with appropriate types so that we can call
12260 determine_specialization. */
12261 gen_tmpl = NULL_TREE;
12262 }
12263
12264 if (DECL_CLASS_SCOPE_P (t))
12265 {
12266 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12267 member = 2;
12268 else
12269 member = 1;
12270 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12271 complain, t, /*entering_scope=*/1);
12272 }
12273 else
12274 {
12275 member = 0;
12276 ctx = DECL_CONTEXT (t);
12277 }
12278 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12279 if (type == error_mark_node)
12280 RETURN (error_mark_node);
12281
12282 /* If we hit excessive deduction depth, the type is bogus even if
12283 it isn't error_mark_node, so don't build a decl. */
12284 if (excessive_deduction_depth)
12285 RETURN (error_mark_node);
12286
12287 /* We do NOT check for matching decls pushed separately at this
12288 point, as they may not represent instantiations of this
12289 template, and in any case are considered separate under the
12290 discrete model. */
12291 r = copy_decl (t);
12292 DECL_USE_TEMPLATE (r) = 0;
12293 TREE_TYPE (r) = type;
12294 /* Clear out the mangled name and RTL for the instantiation. */
12295 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12296 SET_DECL_RTL (r, NULL);
12297 /* Leave DECL_INITIAL set on deleted instantiations. */
12298 if (!DECL_DELETED_FN (r))
12299 DECL_INITIAL (r) = NULL_TREE;
12300 DECL_CONTEXT (r) = ctx;
12301
12302 /* OpenMP UDRs have the only argument a reference to the declared
12303 type. We want to diagnose if the declared type is a reference,
12304 which is invalid, but as references to references are usually
12305 quietly merged, diagnose it here. */
12306 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12307 {
12308 tree argtype
12309 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12310 argtype = tsubst (argtype, args, complain, in_decl);
12311 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12312 error_at (DECL_SOURCE_LOCATION (t),
12313 "reference type %qT in "
12314 "%<#pragma omp declare reduction%>", argtype);
12315 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12316 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12317 argtype);
12318 }
12319
12320 if (member && DECL_CONV_FN_P (r))
12321 /* Type-conversion operator. Reconstruct the name, in
12322 case it's the name of one of the template's parameters. */
12323 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12324
12325 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12326 complain, t);
12327 DECL_RESULT (r) = NULL_TREE;
12328
12329 TREE_STATIC (r) = 0;
12330 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12331 DECL_EXTERNAL (r) = 1;
12332 /* If this is an instantiation of a function with internal
12333 linkage, we already know what object file linkage will be
12334 assigned to the instantiation. */
12335 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12336 DECL_DEFER_OUTPUT (r) = 0;
12337 DECL_CHAIN (r) = NULL_TREE;
12338 DECL_PENDING_INLINE_INFO (r) = 0;
12339 DECL_PENDING_INLINE_P (r) = 0;
12340 DECL_SAVED_TREE (r) = NULL_TREE;
12341 DECL_STRUCT_FUNCTION (r) = NULL;
12342 TREE_USED (r) = 0;
12343 /* We'll re-clone as appropriate in instantiate_template. */
12344 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12345
12346 /* If we aren't complaining now, return on error before we register
12347 the specialization so that we'll complain eventually. */
12348 if ((complain & tf_error) == 0
12349 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12350 && !grok_op_properties (r, /*complain=*/false))
12351 RETURN (error_mark_node);
12352
12353 /* When instantiating a constrained member, substitute
12354 into the constraints to create a new constraint. */
12355 if (tree ci = get_constraints (t))
12356 if (member)
12357 {
12358 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12359 set_constraints (r, ci);
12360 }
12361
12362 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12363 this in the special friend case mentioned above where
12364 GEN_TMPL is NULL. */
12365 if (gen_tmpl)
12366 {
12367 DECL_TEMPLATE_INFO (r)
12368 = build_template_info (gen_tmpl, argvec);
12369 SET_DECL_IMPLICIT_INSTANTIATION (r);
12370
12371 tree new_r
12372 = register_specialization (r, gen_tmpl, argvec, false, hash);
12373 if (new_r != r)
12374 /* We instantiated this while substituting into
12375 the type earlier (template/friend54.C). */
12376 RETURN (new_r);
12377
12378 /* We're not supposed to instantiate default arguments
12379 until they are called, for a template. But, for a
12380 declaration like:
12381
12382 template <class T> void f ()
12383 { extern void g(int i = T()); }
12384
12385 we should do the substitution when the template is
12386 instantiated. We handle the member function case in
12387 instantiate_class_template since the default arguments
12388 might refer to other members of the class. */
12389 if (!member
12390 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12391 && !uses_template_parms (argvec))
12392 tsubst_default_arguments (r, complain);
12393 }
12394 else
12395 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12396
12397 /* Copy the list of befriending classes. */
12398 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12399 *friends;
12400 friends = &TREE_CHAIN (*friends))
12401 {
12402 *friends = copy_node (*friends);
12403 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12404 args, complain,
12405 in_decl);
12406 }
12407
12408 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12409 {
12410 maybe_retrofit_in_chrg (r);
12411 if (DECL_CONSTRUCTOR_P (r))
12412 grok_ctor_properties (ctx, r);
12413 /* If this is an instantiation of a member template, clone it.
12414 If it isn't, that'll be handled by
12415 clone_constructors_and_destructors. */
12416 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12417 clone_function_decl (r, /*update_method_vec_p=*/0);
12418 }
12419 else if ((complain & tf_error) != 0
12420 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12421 && !grok_op_properties (r, /*complain=*/true))
12422 RETURN (error_mark_node);
12423
12424 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12425 SET_DECL_FRIEND_CONTEXT (r,
12426 tsubst (DECL_FRIEND_CONTEXT (t),
12427 args, complain, in_decl));
12428
12429 /* Possibly limit visibility based on template args. */
12430 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12431 if (DECL_VISIBILITY_SPECIFIED (t))
12432 {
12433 DECL_VISIBILITY_SPECIFIED (r) = 0;
12434 DECL_ATTRIBUTES (r)
12435 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12436 }
12437 determine_visibility (r);
12438 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12439 && !processing_template_decl)
12440 defaulted_late_check (r);
12441
12442 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12443 args, complain, in_decl);
12444 }
12445 break;
12446
12447 case PARM_DECL:
12448 {
12449 tree type = NULL_TREE;
12450 int i, len = 1;
12451 tree expanded_types = NULL_TREE;
12452 tree prev_r = NULL_TREE;
12453 tree first_r = NULL_TREE;
12454
12455 if (DECL_PACK_P (t))
12456 {
12457 /* If there is a local specialization that isn't a
12458 parameter pack, it means that we're doing a "simple"
12459 substitution from inside tsubst_pack_expansion. Just
12460 return the local specialization (which will be a single
12461 parm). */
12462 tree spec = retrieve_local_specialization (t);
12463 if (spec
12464 && TREE_CODE (spec) == PARM_DECL
12465 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12466 RETURN (spec);
12467
12468 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12469 the parameters in this function parameter pack. */
12470 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12471 complain, in_decl);
12472 if (TREE_CODE (expanded_types) == TREE_VEC)
12473 {
12474 len = TREE_VEC_LENGTH (expanded_types);
12475
12476 /* Zero-length parameter packs are boring. Just substitute
12477 into the chain. */
12478 if (len == 0)
12479 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12480 TREE_CHAIN (t)));
12481 }
12482 else
12483 {
12484 /* All we did was update the type. Make a note of that. */
12485 type = expanded_types;
12486 expanded_types = NULL_TREE;
12487 }
12488 }
12489
12490 /* Loop through all of the parameters we'll build. When T is
12491 a function parameter pack, LEN is the number of expanded
12492 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12493 r = NULL_TREE;
12494 for (i = 0; i < len; ++i)
12495 {
12496 prev_r = r;
12497 r = copy_node (t);
12498 if (DECL_TEMPLATE_PARM_P (t))
12499 SET_DECL_TEMPLATE_PARM_P (r);
12500
12501 if (expanded_types)
12502 /* We're on the Ith parameter of the function parameter
12503 pack. */
12504 {
12505 /* Get the Ith type. */
12506 type = TREE_VEC_ELT (expanded_types, i);
12507
12508 /* Rename the parameter to include the index. */
12509 DECL_NAME (r)
12510 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12511 }
12512 else if (!type)
12513 /* We're dealing with a normal parameter. */
12514 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12515
12516 type = type_decays_to (type);
12517 TREE_TYPE (r) = type;
12518 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12519
12520 if (DECL_INITIAL (r))
12521 {
12522 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12523 DECL_INITIAL (r) = TREE_TYPE (r);
12524 else
12525 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12526 complain, in_decl);
12527 }
12528
12529 DECL_CONTEXT (r) = NULL_TREE;
12530
12531 if (!DECL_TEMPLATE_PARM_P (r))
12532 DECL_ARG_TYPE (r) = type_passed_as (type);
12533
12534 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12535 args, complain, in_decl);
12536
12537 /* Keep track of the first new parameter we
12538 generate. That's what will be returned to the
12539 caller. */
12540 if (!first_r)
12541 first_r = r;
12542
12543 /* Build a proper chain of parameters when substituting
12544 into a function parameter pack. */
12545 if (prev_r)
12546 DECL_CHAIN (prev_r) = r;
12547 }
12548
12549 /* If cp_unevaluated_operand is set, we're just looking for a
12550 single dummy parameter, so don't keep going. */
12551 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12552 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12553 complain, DECL_CHAIN (t));
12554
12555 /* FIRST_R contains the start of the chain we've built. */
12556 r = first_r;
12557 }
12558 break;
12559
12560 case FIELD_DECL:
12561 {
12562 tree type = NULL_TREE;
12563 tree vec = NULL_TREE;
12564 tree expanded_types = NULL_TREE;
12565 int len = 1;
12566
12567 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12568 {
12569 /* This field is a lambda capture pack. Return a TREE_VEC of
12570 the expanded fields to instantiate_class_template_1 and
12571 store them in the specializations hash table as a
12572 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12573 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12574 complain, in_decl);
12575 if (TREE_CODE (expanded_types) == TREE_VEC)
12576 {
12577 len = TREE_VEC_LENGTH (expanded_types);
12578 vec = make_tree_vec (len);
12579 }
12580 else
12581 {
12582 /* All we did was update the type. Make a note of that. */
12583 type = expanded_types;
12584 expanded_types = NULL_TREE;
12585 }
12586 }
12587
12588 for (int i = 0; i < len; ++i)
12589 {
12590 r = copy_decl (t);
12591 if (expanded_types)
12592 {
12593 type = TREE_VEC_ELT (expanded_types, i);
12594 DECL_NAME (r)
12595 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12596 }
12597 else if (!type)
12598 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12599
12600 if (type == error_mark_node)
12601 RETURN (error_mark_node);
12602 TREE_TYPE (r) = type;
12603 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12604
12605 if (DECL_C_BIT_FIELD (r))
12606 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12607 non-bit-fields DECL_INITIAL is a non-static data member
12608 initializer, which gets deferred instantiation. */
12609 DECL_INITIAL (r)
12610 = tsubst_expr (DECL_INITIAL (t), args,
12611 complain, in_decl,
12612 /*integral_constant_expression_p=*/true);
12613 else if (DECL_INITIAL (t))
12614 {
12615 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12616 NSDMI in perform_member_init. Still set DECL_INITIAL
12617 so that we know there is one. */
12618 DECL_INITIAL (r) = void_node;
12619 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12620 retrofit_lang_decl (r);
12621 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12622 }
12623 /* We don't have to set DECL_CONTEXT here; it is set by
12624 finish_member_declaration. */
12625 DECL_CHAIN (r) = NULL_TREE;
12626
12627 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12628 args, complain, in_decl);
12629
12630 if (vec)
12631 TREE_VEC_ELT (vec, i) = r;
12632 }
12633
12634 if (vec)
12635 {
12636 r = vec;
12637 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12638 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12639 SET_ARGUMENT_PACK_ARGS (pack, vec);
12640 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12641 TREE_TYPE (pack) = tpack;
12642 register_specialization (pack, t, args, false, 0);
12643 }
12644 }
12645 break;
12646
12647 case USING_DECL:
12648 /* We reach here only for member using decls. We also need to check
12649 uses_template_parms because DECL_DEPENDENT_P is not set for a
12650 using-declaration that designates a member of the current
12651 instantiation (c++/53549). */
12652 if (DECL_DEPENDENT_P (t)
12653 || uses_template_parms (USING_DECL_SCOPE (t)))
12654 {
12655 tree scope = USING_DECL_SCOPE (t);
12656 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12657 if (PACK_EXPANSION_P (scope))
12658 {
12659 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12660 int len = TREE_VEC_LENGTH (vec);
12661 r = make_tree_vec (len);
12662 for (int i = 0; i < len; ++i)
12663 {
12664 tree escope = TREE_VEC_ELT (vec, i);
12665 tree elt = do_class_using_decl (escope, name);
12666 if (!elt)
12667 {
12668 r = error_mark_node;
12669 break;
12670 }
12671 else
12672 {
12673 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12674 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12675 }
12676 TREE_VEC_ELT (r, i) = elt;
12677 }
12678 }
12679 else
12680 {
12681 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12682 complain, in_decl);
12683 r = do_class_using_decl (inst_scope, name);
12684 if (!r)
12685 r = error_mark_node;
12686 else
12687 {
12688 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12689 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12690 }
12691 }
12692 }
12693 else
12694 {
12695 r = copy_node (t);
12696 DECL_CHAIN (r) = NULL_TREE;
12697 }
12698 break;
12699
12700 case TYPE_DECL:
12701 case VAR_DECL:
12702 {
12703 tree argvec = NULL_TREE;
12704 tree gen_tmpl = NULL_TREE;
12705 tree spec;
12706 tree tmpl = NULL_TREE;
12707 tree ctx;
12708 tree type = NULL_TREE;
12709 bool local_p;
12710
12711 if (TREE_TYPE (t) == error_mark_node)
12712 RETURN (error_mark_node);
12713
12714 if (TREE_CODE (t) == TYPE_DECL
12715 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12716 {
12717 /* If this is the canonical decl, we don't have to
12718 mess with instantiations, and often we can't (for
12719 typename, template type parms and such). Note that
12720 TYPE_NAME is not correct for the above test if
12721 we've copied the type for a typedef. */
12722 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12723 if (type == error_mark_node)
12724 RETURN (error_mark_node);
12725 r = TYPE_NAME (type);
12726 break;
12727 }
12728
12729 /* Check to see if we already have the specialization we
12730 need. */
12731 spec = NULL_TREE;
12732 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12733 {
12734 /* T is a static data member or namespace-scope entity.
12735 We have to substitute into namespace-scope variables
12736 (not just variable templates) because of cases like:
12737
12738 template <class T> void f() { extern T t; }
12739
12740 where the entity referenced is not known until
12741 instantiation time. */
12742 local_p = false;
12743 ctx = DECL_CONTEXT (t);
12744 if (DECL_CLASS_SCOPE_P (t))
12745 {
12746 ctx = tsubst_aggr_type (ctx, args,
12747 complain,
12748 in_decl, /*entering_scope=*/1);
12749 /* If CTX is unchanged, then T is in fact the
12750 specialization we want. That situation occurs when
12751 referencing a static data member within in its own
12752 class. We can use pointer equality, rather than
12753 same_type_p, because DECL_CONTEXT is always
12754 canonical... */
12755 if (ctx == DECL_CONTEXT (t)
12756 /* ... unless T is a member template; in which
12757 case our caller can be willing to create a
12758 specialization of that template represented
12759 by T. */
12760 && !(DECL_TI_TEMPLATE (t)
12761 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12762 spec = t;
12763 }
12764
12765 if (!spec)
12766 {
12767 tmpl = DECL_TI_TEMPLATE (t);
12768 gen_tmpl = most_general_template (tmpl);
12769 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12770 if (argvec != error_mark_node)
12771 argvec = (coerce_innermost_template_parms
12772 (DECL_TEMPLATE_PARMS (gen_tmpl),
12773 argvec, t, complain,
12774 /*all*/true, /*defarg*/true));
12775 if (argvec == error_mark_node)
12776 RETURN (error_mark_node);
12777 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12778 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12779 }
12780 }
12781 else
12782 {
12783 /* A local variable. */
12784 local_p = true;
12785 /* Subsequent calls to pushdecl will fill this in. */
12786 ctx = NULL_TREE;
12787 /* Unless this is a reference to a static variable from an
12788 enclosing function, in which case we need to fill it in now. */
12789 if (TREE_STATIC (t))
12790 {
12791 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12792 if (fn != current_function_decl)
12793 ctx = fn;
12794 }
12795 spec = retrieve_local_specialization (t);
12796 }
12797 /* If we already have the specialization we need, there is
12798 nothing more to do. */
12799 if (spec)
12800 {
12801 r = spec;
12802 break;
12803 }
12804
12805 /* Create a new node for the specialization we need. */
12806 r = copy_decl (t);
12807 if (type == NULL_TREE)
12808 {
12809 if (is_typedef_decl (t))
12810 type = DECL_ORIGINAL_TYPE (t);
12811 else
12812 type = TREE_TYPE (t);
12813 if (VAR_P (t)
12814 && VAR_HAD_UNKNOWN_BOUND (t)
12815 && type != error_mark_node)
12816 type = strip_array_domain (type);
12817 type = tsubst (type, args, complain, in_decl);
12818 }
12819 if (VAR_P (r))
12820 {
12821 /* Even if the original location is out of scope, the
12822 newly substituted one is not. */
12823 DECL_DEAD_FOR_LOCAL (r) = 0;
12824 DECL_INITIALIZED_P (r) = 0;
12825 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12826 if (type == error_mark_node)
12827 RETURN (error_mark_node);
12828 if (TREE_CODE (type) == FUNCTION_TYPE)
12829 {
12830 /* It may seem that this case cannot occur, since:
12831
12832 typedef void f();
12833 void g() { f x; }
12834
12835 declares a function, not a variable. However:
12836
12837 typedef void f();
12838 template <typename T> void g() { T t; }
12839 template void g<f>();
12840
12841 is an attempt to declare a variable with function
12842 type. */
12843 error ("variable %qD has function type",
12844 /* R is not yet sufficiently initialized, so we
12845 just use its name. */
12846 DECL_NAME (r));
12847 RETURN (error_mark_node);
12848 }
12849 type = complete_type (type);
12850 /* Wait until cp_finish_decl to set this again, to handle
12851 circular dependency (template/instantiate6.C). */
12852 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12853 type = check_var_type (DECL_NAME (r), type);
12854
12855 if (DECL_HAS_VALUE_EXPR_P (t))
12856 {
12857 tree ve = DECL_VALUE_EXPR (t);
12858 ve = tsubst_expr (ve, args, complain, in_decl,
12859 /*constant_expression_p=*/false);
12860 if (REFERENCE_REF_P (ve))
12861 {
12862 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12863 ve = TREE_OPERAND (ve, 0);
12864 }
12865 SET_DECL_VALUE_EXPR (r, ve);
12866 }
12867 if (CP_DECL_THREAD_LOCAL_P (r)
12868 && !processing_template_decl)
12869 set_decl_tls_model (r, decl_default_tls_model (r));
12870 }
12871 else if (DECL_SELF_REFERENCE_P (t))
12872 SET_DECL_SELF_REFERENCE_P (r);
12873 TREE_TYPE (r) = type;
12874 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12875 DECL_CONTEXT (r) = ctx;
12876 /* Clear out the mangled name and RTL for the instantiation. */
12877 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12878 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12879 SET_DECL_RTL (r, NULL);
12880 /* The initializer must not be expanded until it is required;
12881 see [temp.inst]. */
12882 DECL_INITIAL (r) = NULL_TREE;
12883 if (VAR_P (r))
12884 SET_DECL_MODE (r, VOIDmode);
12885 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12886 SET_DECL_RTL (r, NULL);
12887 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12888 if (VAR_P (r))
12889 {
12890 /* Possibly limit visibility based on template args. */
12891 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12892 if (DECL_VISIBILITY_SPECIFIED (t))
12893 {
12894 DECL_VISIBILITY_SPECIFIED (r) = 0;
12895 DECL_ATTRIBUTES (r)
12896 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12897 }
12898 determine_visibility (r);
12899 }
12900
12901 if (!local_p)
12902 {
12903 /* A static data member declaration is always marked
12904 external when it is declared in-class, even if an
12905 initializer is present. We mimic the non-template
12906 processing here. */
12907 DECL_EXTERNAL (r) = 1;
12908 if (DECL_NAMESPACE_SCOPE_P (t))
12909 DECL_NOT_REALLY_EXTERN (r) = 1;
12910
12911 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12912 SET_DECL_IMPLICIT_INSTANTIATION (r);
12913 register_specialization (r, gen_tmpl, argvec, false, hash);
12914 }
12915 else
12916 {
12917 if (DECL_LANG_SPECIFIC (r))
12918 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12919 if (!cp_unevaluated_operand)
12920 register_local_specialization (r, t);
12921 }
12922
12923 DECL_CHAIN (r) = NULL_TREE;
12924
12925 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12926 /*flags=*/0,
12927 args, complain, in_decl);
12928
12929 /* Preserve a typedef that names a type. */
12930 if (is_typedef_decl (r) && type != error_mark_node)
12931 {
12932 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12933 set_underlying_type (r);
12934 if (TYPE_DECL_ALIAS_P (r))
12935 /* An alias template specialization can be dependent
12936 even if its underlying type is not. */
12937 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12938 }
12939
12940 layout_decl (r, 0);
12941 }
12942 break;
12943
12944 default:
12945 gcc_unreachable ();
12946 }
12947 #undef RETURN
12948
12949 out:
12950 /* Restore the file and line information. */
12951 input_location = saved_loc;
12952
12953 return r;
12954 }
12955
12956 /* Substitute into the ARG_TYPES of a function type.
12957 If END is a TREE_CHAIN, leave it and any following types
12958 un-substituted. */
12959
12960 static tree
12961 tsubst_arg_types (tree arg_types,
12962 tree args,
12963 tree end,
12964 tsubst_flags_t complain,
12965 tree in_decl)
12966 {
12967 tree remaining_arg_types;
12968 tree type = NULL_TREE;
12969 int i = 1;
12970 tree expanded_args = NULL_TREE;
12971 tree default_arg;
12972
12973 if (!arg_types || arg_types == void_list_node || arg_types == end)
12974 return arg_types;
12975
12976 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12977 args, end, complain, in_decl);
12978 if (remaining_arg_types == error_mark_node)
12979 return error_mark_node;
12980
12981 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12982 {
12983 /* For a pack expansion, perform substitution on the
12984 entire expression. Later on, we'll handle the arguments
12985 one-by-one. */
12986 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12987 args, complain, in_decl);
12988
12989 if (TREE_CODE (expanded_args) == TREE_VEC)
12990 /* So that we'll spin through the parameters, one by one. */
12991 i = TREE_VEC_LENGTH (expanded_args);
12992 else
12993 {
12994 /* We only partially substituted into the parameter
12995 pack. Our type is TYPE_PACK_EXPANSION. */
12996 type = expanded_args;
12997 expanded_args = NULL_TREE;
12998 }
12999 }
13000
13001 while (i > 0) {
13002 --i;
13003
13004 if (expanded_args)
13005 type = TREE_VEC_ELT (expanded_args, i);
13006 else if (!type)
13007 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13008
13009 if (type == error_mark_node)
13010 return error_mark_node;
13011 if (VOID_TYPE_P (type))
13012 {
13013 if (complain & tf_error)
13014 {
13015 error ("invalid parameter type %qT", type);
13016 if (in_decl)
13017 error ("in declaration %q+D", in_decl);
13018 }
13019 return error_mark_node;
13020 }
13021 /* DR 657. */
13022 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13023 return error_mark_node;
13024
13025 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13026 top-level qualifiers as required. */
13027 type = cv_unqualified (type_decays_to (type));
13028
13029 /* We do not substitute into default arguments here. The standard
13030 mandates that they be instantiated only when needed, which is
13031 done in build_over_call. */
13032 default_arg = TREE_PURPOSE (arg_types);
13033
13034 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13035 {
13036 /* We've instantiated a template before its default arguments
13037 have been parsed. This can happen for a nested template
13038 class, and is not an error unless we require the default
13039 argument in a call of this function. */
13040 remaining_arg_types =
13041 tree_cons (default_arg, type, remaining_arg_types);
13042 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13043 }
13044 else
13045 remaining_arg_types =
13046 hash_tree_cons (default_arg, type, remaining_arg_types);
13047 }
13048
13049 return remaining_arg_types;
13050 }
13051
13052 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13053 *not* handle the exception-specification for FNTYPE, because the
13054 initial substitution of explicitly provided template parameters
13055 during argument deduction forbids substitution into the
13056 exception-specification:
13057
13058 [temp.deduct]
13059
13060 All references in the function type of the function template to the
13061 corresponding template parameters are replaced by the specified tem-
13062 plate argument values. If a substitution in a template parameter or
13063 in the function type of the function template results in an invalid
13064 type, type deduction fails. [Note: The equivalent substitution in
13065 exception specifications is done only when the function is instanti-
13066 ated, at which point a program is ill-formed if the substitution
13067 results in an invalid type.] */
13068
13069 static tree
13070 tsubst_function_type (tree t,
13071 tree args,
13072 tsubst_flags_t complain,
13073 tree in_decl)
13074 {
13075 tree return_type;
13076 tree arg_types = NULL_TREE;
13077 tree fntype;
13078
13079 /* The TYPE_CONTEXT is not used for function/method types. */
13080 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13081
13082 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13083 failure. */
13084 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13085
13086 if (late_return_type_p)
13087 {
13088 /* Substitute the argument types. */
13089 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13090 complain, in_decl);
13091 if (arg_types == error_mark_node)
13092 return error_mark_node;
13093
13094 tree save_ccp = current_class_ptr;
13095 tree save_ccr = current_class_ref;
13096 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13097 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13098 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13099 if (do_inject)
13100 {
13101 /* DR 1207: 'this' is in scope in the trailing return type. */
13102 inject_this_parameter (this_type, cp_type_quals (this_type));
13103 }
13104
13105 /* Substitute the return type. */
13106 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13107
13108 if (do_inject)
13109 {
13110 current_class_ptr = save_ccp;
13111 current_class_ref = save_ccr;
13112 }
13113 }
13114 else
13115 /* Substitute the return type. */
13116 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13117
13118 if (return_type == error_mark_node)
13119 return error_mark_node;
13120 /* DR 486 clarifies that creation of a function type with an
13121 invalid return type is a deduction failure. */
13122 if (TREE_CODE (return_type) == ARRAY_TYPE
13123 || TREE_CODE (return_type) == FUNCTION_TYPE)
13124 {
13125 if (complain & tf_error)
13126 {
13127 if (TREE_CODE (return_type) == ARRAY_TYPE)
13128 error ("function returning an array");
13129 else
13130 error ("function returning a function");
13131 }
13132 return error_mark_node;
13133 }
13134 /* And DR 657. */
13135 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13136 return error_mark_node;
13137
13138 if (!late_return_type_p)
13139 {
13140 /* Substitute the argument types. */
13141 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13142 complain, in_decl);
13143 if (arg_types == error_mark_node)
13144 return error_mark_node;
13145 }
13146
13147 /* Construct a new type node and return it. */
13148 if (TREE_CODE (t) == FUNCTION_TYPE)
13149 {
13150 fntype = build_function_type (return_type, arg_types);
13151 fntype = apply_memfn_quals (fntype,
13152 type_memfn_quals (t),
13153 type_memfn_rqual (t));
13154 }
13155 else
13156 {
13157 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13158 /* Don't pick up extra function qualifiers from the basetype. */
13159 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13160 if (! MAYBE_CLASS_TYPE_P (r))
13161 {
13162 /* [temp.deduct]
13163
13164 Type deduction may fail for any of the following
13165 reasons:
13166
13167 -- Attempting to create "pointer to member of T" when T
13168 is not a class type. */
13169 if (complain & tf_error)
13170 error ("creating pointer to member function of non-class type %qT",
13171 r);
13172 return error_mark_node;
13173 }
13174
13175 fntype = build_method_type_directly (r, return_type,
13176 TREE_CHAIN (arg_types));
13177 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13178 }
13179 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13180
13181 if (late_return_type_p)
13182 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13183
13184 return fntype;
13185 }
13186
13187 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13188 ARGS into that specification, and return the substituted
13189 specification. If there is no specification, return NULL_TREE. */
13190
13191 static tree
13192 tsubst_exception_specification (tree fntype,
13193 tree args,
13194 tsubst_flags_t complain,
13195 tree in_decl,
13196 bool defer_ok)
13197 {
13198 tree specs;
13199 tree new_specs;
13200
13201 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13202 new_specs = NULL_TREE;
13203 if (specs && TREE_PURPOSE (specs))
13204 {
13205 /* A noexcept-specifier. */
13206 tree expr = TREE_PURPOSE (specs);
13207 if (TREE_CODE (expr) == INTEGER_CST)
13208 new_specs = expr;
13209 else if (defer_ok)
13210 {
13211 /* Defer instantiation of noexcept-specifiers to avoid
13212 excessive instantiations (c++/49107). */
13213 new_specs = make_node (DEFERRED_NOEXCEPT);
13214 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13215 {
13216 /* We already partially instantiated this member template,
13217 so combine the new args with the old. */
13218 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13219 = DEFERRED_NOEXCEPT_PATTERN (expr);
13220 DEFERRED_NOEXCEPT_ARGS (new_specs)
13221 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13222 }
13223 else
13224 {
13225 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13226 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13227 }
13228 }
13229 else
13230 new_specs = tsubst_copy_and_build
13231 (expr, args, complain, in_decl, /*function_p=*/false,
13232 /*integral_constant_expression_p=*/true);
13233 new_specs = build_noexcept_spec (new_specs, complain);
13234 }
13235 else if (specs)
13236 {
13237 if (! TREE_VALUE (specs))
13238 new_specs = specs;
13239 else
13240 while (specs)
13241 {
13242 tree spec;
13243 int i, len = 1;
13244 tree expanded_specs = NULL_TREE;
13245
13246 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13247 {
13248 /* Expand the pack expansion type. */
13249 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13250 args, complain,
13251 in_decl);
13252
13253 if (expanded_specs == error_mark_node)
13254 return error_mark_node;
13255 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13256 len = TREE_VEC_LENGTH (expanded_specs);
13257 else
13258 {
13259 /* We're substituting into a member template, so
13260 we got a TYPE_PACK_EXPANSION back. Add that
13261 expansion and move on. */
13262 gcc_assert (TREE_CODE (expanded_specs)
13263 == TYPE_PACK_EXPANSION);
13264 new_specs = add_exception_specifier (new_specs,
13265 expanded_specs,
13266 complain);
13267 specs = TREE_CHAIN (specs);
13268 continue;
13269 }
13270 }
13271
13272 for (i = 0; i < len; ++i)
13273 {
13274 if (expanded_specs)
13275 spec = TREE_VEC_ELT (expanded_specs, i);
13276 else
13277 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13278 if (spec == error_mark_node)
13279 return spec;
13280 new_specs = add_exception_specifier (new_specs, spec,
13281 complain);
13282 }
13283
13284 specs = TREE_CHAIN (specs);
13285 }
13286 }
13287 return new_specs;
13288 }
13289
13290 /* Take the tree structure T and replace template parameters used
13291 therein with the argument vector ARGS. IN_DECL is an associated
13292 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13293 Issue error and warning messages under control of COMPLAIN. Note
13294 that we must be relatively non-tolerant of extensions here, in
13295 order to preserve conformance; if we allow substitutions that
13296 should not be allowed, we may allow argument deductions that should
13297 not succeed, and therefore report ambiguous overload situations
13298 where there are none. In theory, we could allow the substitution,
13299 but indicate that it should have failed, and allow our caller to
13300 make sure that the right thing happens, but we don't try to do this
13301 yet.
13302
13303 This function is used for dealing with types, decls and the like;
13304 for expressions, use tsubst_expr or tsubst_copy. */
13305
13306 tree
13307 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13308 {
13309 enum tree_code code;
13310 tree type, r = NULL_TREE;
13311
13312 if (t == NULL_TREE || t == error_mark_node
13313 || t == integer_type_node
13314 || t == void_type_node
13315 || t == char_type_node
13316 || t == unknown_type_node
13317 || TREE_CODE (t) == NAMESPACE_DECL
13318 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13319 return t;
13320
13321 if (DECL_P (t))
13322 return tsubst_decl (t, args, complain);
13323
13324 if (args == NULL_TREE)
13325 return t;
13326
13327 code = TREE_CODE (t);
13328
13329 if (code == IDENTIFIER_NODE)
13330 type = IDENTIFIER_TYPE_VALUE (t);
13331 else
13332 type = TREE_TYPE (t);
13333
13334 gcc_assert (type != unknown_type_node);
13335
13336 /* Reuse typedefs. We need to do this to handle dependent attributes,
13337 such as attribute aligned. */
13338 if (TYPE_P (t)
13339 && typedef_variant_p (t))
13340 {
13341 tree decl = TYPE_NAME (t);
13342
13343 if (alias_template_specialization_p (t))
13344 {
13345 /* DECL represents an alias template and we want to
13346 instantiate it. */
13347 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13348 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13349 r = instantiate_alias_template (tmpl, gen_args, complain);
13350 }
13351 else if (DECL_CLASS_SCOPE_P (decl)
13352 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13353 && uses_template_parms (DECL_CONTEXT (decl)))
13354 {
13355 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13356 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13357 r = retrieve_specialization (tmpl, gen_args, 0);
13358 }
13359 else if (DECL_FUNCTION_SCOPE_P (decl)
13360 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13361 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13362 r = retrieve_local_specialization (decl);
13363 else
13364 /* The typedef is from a non-template context. */
13365 return t;
13366
13367 if (r)
13368 {
13369 r = TREE_TYPE (r);
13370 r = cp_build_qualified_type_real
13371 (r, cp_type_quals (t) | cp_type_quals (r),
13372 complain | tf_ignore_bad_quals);
13373 return r;
13374 }
13375 else
13376 {
13377 /* We don't have an instantiation yet, so drop the typedef. */
13378 int quals = cp_type_quals (t);
13379 t = DECL_ORIGINAL_TYPE (decl);
13380 t = cp_build_qualified_type_real (t, quals,
13381 complain | tf_ignore_bad_quals);
13382 }
13383 }
13384
13385 bool fndecl_type = (complain & tf_fndecl_type);
13386 complain &= ~tf_fndecl_type;
13387
13388 if (type
13389 && code != TYPENAME_TYPE
13390 && code != TEMPLATE_TYPE_PARM
13391 && code != TEMPLATE_PARM_INDEX
13392 && code != IDENTIFIER_NODE
13393 && code != FUNCTION_TYPE
13394 && code != METHOD_TYPE)
13395 type = tsubst (type, args, complain, in_decl);
13396 if (type == error_mark_node)
13397 return error_mark_node;
13398
13399 switch (code)
13400 {
13401 case RECORD_TYPE:
13402 case UNION_TYPE:
13403 case ENUMERAL_TYPE:
13404 return tsubst_aggr_type (t, args, complain, in_decl,
13405 /*entering_scope=*/0);
13406
13407 case ERROR_MARK:
13408 case IDENTIFIER_NODE:
13409 case VOID_TYPE:
13410 case REAL_TYPE:
13411 case COMPLEX_TYPE:
13412 case VECTOR_TYPE:
13413 case BOOLEAN_TYPE:
13414 case NULLPTR_TYPE:
13415 case LANG_TYPE:
13416 return t;
13417
13418 case INTEGER_TYPE:
13419 if (t == integer_type_node)
13420 return t;
13421
13422 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13423 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13424 return t;
13425
13426 {
13427 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13428
13429 max = tsubst_expr (omax, args, complain, in_decl,
13430 /*integral_constant_expression_p=*/false);
13431
13432 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13433 needed. */
13434 if (TREE_CODE (max) == NOP_EXPR
13435 && TREE_SIDE_EFFECTS (omax)
13436 && !TREE_TYPE (max))
13437 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13438
13439 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13440 with TREE_SIDE_EFFECTS that indicates this is not an integral
13441 constant expression. */
13442 if (processing_template_decl
13443 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13444 {
13445 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13446 TREE_SIDE_EFFECTS (max) = 1;
13447 }
13448
13449 return compute_array_index_type (NULL_TREE, max, complain);
13450 }
13451
13452 case TEMPLATE_TYPE_PARM:
13453 case TEMPLATE_TEMPLATE_PARM:
13454 case BOUND_TEMPLATE_TEMPLATE_PARM:
13455 case TEMPLATE_PARM_INDEX:
13456 {
13457 int idx;
13458 int level;
13459 int levels;
13460 tree arg = NULL_TREE;
13461
13462 /* Early in template argument deduction substitution, we don't
13463 want to reduce the level of 'auto', or it will be confused
13464 with a normal template parm in subsequent deduction. */
13465 if (is_auto (t) && (complain & tf_partial))
13466 return t;
13467
13468 r = NULL_TREE;
13469
13470 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13471 template_parm_level_and_index (t, &level, &idx);
13472
13473 levels = TMPL_ARGS_DEPTH (args);
13474 if (level <= levels
13475 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13476 {
13477 arg = TMPL_ARG (args, level, idx);
13478
13479 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13480 {
13481 /* See through ARGUMENT_PACK_SELECT arguments. */
13482 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13483 /* If the selected argument is an expansion E, that most
13484 likely means we were called from
13485 gen_elem_of_pack_expansion_instantiation during the
13486 substituting of pack an argument pack (which Ith
13487 element is a pack expansion, where I is
13488 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13489 In this case, the Ith element resulting from this
13490 substituting is going to be a pack expansion, which
13491 pattern is the pattern of E. Let's return the
13492 pattern of E, and
13493 gen_elem_of_pack_expansion_instantiation will
13494 build the resulting pack expansion from it. */
13495 if (PACK_EXPANSION_P (arg))
13496 {
13497 /* Make sure we aren't throwing away arg info. */
13498 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13499 arg = PACK_EXPANSION_PATTERN (arg);
13500 }
13501 }
13502 }
13503
13504 if (arg == error_mark_node)
13505 return error_mark_node;
13506 else if (arg != NULL_TREE)
13507 {
13508 if (ARGUMENT_PACK_P (arg))
13509 /* If ARG is an argument pack, we don't actually want to
13510 perform a substitution here, because substitutions
13511 for argument packs are only done
13512 element-by-element. We can get to this point when
13513 substituting the type of a non-type template
13514 parameter pack, when that type actually contains
13515 template parameter packs from an outer template, e.g.,
13516
13517 template<typename... Types> struct A {
13518 template<Types... Values> struct B { };
13519 }; */
13520 return t;
13521
13522 if (code == TEMPLATE_TYPE_PARM)
13523 {
13524 int quals;
13525 gcc_assert (TYPE_P (arg));
13526
13527 quals = cp_type_quals (arg) | cp_type_quals (t);
13528
13529 return cp_build_qualified_type_real
13530 (arg, quals, complain | tf_ignore_bad_quals);
13531 }
13532 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13533 {
13534 /* We are processing a type constructed from a
13535 template template parameter. */
13536 tree argvec = tsubst (TYPE_TI_ARGS (t),
13537 args, complain, in_decl);
13538 if (argvec == error_mark_node)
13539 return error_mark_node;
13540
13541 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13542 || TREE_CODE (arg) == TEMPLATE_DECL
13543 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13544
13545 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13546 /* Consider this code:
13547
13548 template <template <class> class Template>
13549 struct Internal {
13550 template <class Arg> using Bind = Template<Arg>;
13551 };
13552
13553 template <template <class> class Template, class Arg>
13554 using Instantiate = Template<Arg>; //#0
13555
13556 template <template <class> class Template,
13557 class Argument>
13558 using Bind =
13559 Instantiate<Internal<Template>::template Bind,
13560 Argument>; //#1
13561
13562 When #1 is parsed, the
13563 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13564 parameter `Template' in #0 matches the
13565 UNBOUND_CLASS_TEMPLATE representing the argument
13566 `Internal<Template>::template Bind'; We then want
13567 to assemble the type `Bind<Argument>' that can't
13568 be fully created right now, because
13569 `Internal<Template>' not being complete, the Bind
13570 template cannot be looked up in that context. So
13571 we need to "store" `Bind<Argument>' for later
13572 when the context of Bind becomes complete. Let's
13573 store that in a TYPENAME_TYPE. */
13574 return make_typename_type (TYPE_CONTEXT (arg),
13575 build_nt (TEMPLATE_ID_EXPR,
13576 TYPE_IDENTIFIER (arg),
13577 argvec),
13578 typename_type,
13579 complain);
13580
13581 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13582 are resolving nested-types in the signature of a
13583 member function templates. Otherwise ARG is a
13584 TEMPLATE_DECL and is the real template to be
13585 instantiated. */
13586 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13587 arg = TYPE_NAME (arg);
13588
13589 r = lookup_template_class (arg,
13590 argvec, in_decl,
13591 DECL_CONTEXT (arg),
13592 /*entering_scope=*/0,
13593 complain);
13594 return cp_build_qualified_type_real
13595 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13596 }
13597 else if (code == TEMPLATE_TEMPLATE_PARM)
13598 return arg;
13599 else
13600 /* TEMPLATE_PARM_INDEX. */
13601 return convert_from_reference (unshare_expr (arg));
13602 }
13603
13604 if (level == 1)
13605 /* This can happen during the attempted tsubst'ing in
13606 unify. This means that we don't yet have any information
13607 about the template parameter in question. */
13608 return t;
13609
13610 /* If we get here, we must have been looking at a parm for a
13611 more deeply nested template. Make a new version of this
13612 template parameter, but with a lower level. */
13613 switch (code)
13614 {
13615 case TEMPLATE_TYPE_PARM:
13616 case TEMPLATE_TEMPLATE_PARM:
13617 case BOUND_TEMPLATE_TEMPLATE_PARM:
13618 if (cp_type_quals (t))
13619 {
13620 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13621 r = cp_build_qualified_type_real
13622 (r, cp_type_quals (t),
13623 complain | (code == TEMPLATE_TYPE_PARM
13624 ? tf_ignore_bad_quals : 0));
13625 }
13626 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13627 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13628 && (r = (TEMPLATE_PARM_DESCENDANTS
13629 (TEMPLATE_TYPE_PARM_INDEX (t))))
13630 && (r = TREE_TYPE (r))
13631 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13632 /* Break infinite recursion when substituting the constraints
13633 of a constrained placeholder. */;
13634 else
13635 {
13636 r = copy_type (t);
13637 TEMPLATE_TYPE_PARM_INDEX (r)
13638 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13639 r, levels, args, complain);
13640 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13641 TYPE_MAIN_VARIANT (r) = r;
13642 TYPE_POINTER_TO (r) = NULL_TREE;
13643 TYPE_REFERENCE_TO (r) = NULL_TREE;
13644
13645 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13646 {
13647 /* Propagate constraints on placeholders. */
13648 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13649 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13650 = tsubst_constraint (constr, args, complain, in_decl);
13651 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13652 {
13653 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13654 pl = tsubst (pl, args, complain, in_decl);
13655 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13656 }
13657 }
13658
13659 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13660 /* We have reduced the level of the template
13661 template parameter, but not the levels of its
13662 template parameters, so canonical_type_parameter
13663 will not be able to find the canonical template
13664 template parameter for this level. Thus, we
13665 require structural equality checking to compare
13666 TEMPLATE_TEMPLATE_PARMs. */
13667 SET_TYPE_STRUCTURAL_EQUALITY (r);
13668 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13669 SET_TYPE_STRUCTURAL_EQUALITY (r);
13670 else
13671 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13672
13673 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13674 {
13675 tree tinfo = TYPE_TEMPLATE_INFO (t);
13676 /* We might need to substitute into the types of non-type
13677 template parameters. */
13678 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13679 complain, in_decl);
13680 if (tmpl == error_mark_node)
13681 return error_mark_node;
13682 tree argvec = tsubst (TI_ARGS (tinfo), args,
13683 complain, in_decl);
13684 if (argvec == error_mark_node)
13685 return error_mark_node;
13686
13687 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13688 = build_template_info (tmpl, argvec);
13689 }
13690 }
13691 break;
13692
13693 case TEMPLATE_PARM_INDEX:
13694 /* OK, now substitute the type of the non-type parameter. We
13695 couldn't do it earlier because it might be an auto parameter,
13696 and we wouldn't need to if we had an argument. */
13697 type = tsubst (type, args, complain, in_decl);
13698 r = reduce_template_parm_level (t, type, levels, args, complain);
13699 break;
13700
13701 default:
13702 gcc_unreachable ();
13703 }
13704
13705 return r;
13706 }
13707
13708 case TREE_LIST:
13709 {
13710 tree purpose, value, chain;
13711
13712 if (t == void_list_node)
13713 return t;
13714
13715 purpose = TREE_PURPOSE (t);
13716 if (purpose)
13717 {
13718 purpose = tsubst (purpose, args, complain, in_decl);
13719 if (purpose == error_mark_node)
13720 return error_mark_node;
13721 }
13722 value = TREE_VALUE (t);
13723 if (value)
13724 {
13725 value = tsubst (value, args, complain, in_decl);
13726 if (value == error_mark_node)
13727 return error_mark_node;
13728 }
13729 chain = TREE_CHAIN (t);
13730 if (chain && chain != void_type_node)
13731 {
13732 chain = tsubst (chain, args, complain, in_decl);
13733 if (chain == error_mark_node)
13734 return error_mark_node;
13735 }
13736 if (purpose == TREE_PURPOSE (t)
13737 && value == TREE_VALUE (t)
13738 && chain == TREE_CHAIN (t))
13739 return t;
13740 return hash_tree_cons (purpose, value, chain);
13741 }
13742
13743 case TREE_BINFO:
13744 /* We should never be tsubsting a binfo. */
13745 gcc_unreachable ();
13746
13747 case TREE_VEC:
13748 /* A vector of template arguments. */
13749 gcc_assert (!type);
13750 return tsubst_template_args (t, args, complain, in_decl);
13751
13752 case POINTER_TYPE:
13753 case REFERENCE_TYPE:
13754 {
13755 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13756 return t;
13757
13758 /* [temp.deduct]
13759
13760 Type deduction may fail for any of the following
13761 reasons:
13762
13763 -- Attempting to create a pointer to reference type.
13764 -- Attempting to create a reference to a reference type or
13765 a reference to void.
13766
13767 Core issue 106 says that creating a reference to a reference
13768 during instantiation is no longer a cause for failure. We
13769 only enforce this check in strict C++98 mode. */
13770 if ((TREE_CODE (type) == REFERENCE_TYPE
13771 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13772 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13773 {
13774 static location_t last_loc;
13775
13776 /* We keep track of the last time we issued this error
13777 message to avoid spewing a ton of messages during a
13778 single bad template instantiation. */
13779 if (complain & tf_error
13780 && last_loc != input_location)
13781 {
13782 if (VOID_TYPE_P (type))
13783 error ("forming reference to void");
13784 else if (code == POINTER_TYPE)
13785 error ("forming pointer to reference type %qT", type);
13786 else
13787 error ("forming reference to reference type %qT", type);
13788 last_loc = input_location;
13789 }
13790
13791 return error_mark_node;
13792 }
13793 else if (TREE_CODE (type) == FUNCTION_TYPE
13794 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13795 || type_memfn_rqual (type) != REF_QUAL_NONE))
13796 {
13797 if (complain & tf_error)
13798 {
13799 if (code == POINTER_TYPE)
13800 error ("forming pointer to qualified function type %qT",
13801 type);
13802 else
13803 error ("forming reference to qualified function type %qT",
13804 type);
13805 }
13806 return error_mark_node;
13807 }
13808 else if (code == POINTER_TYPE)
13809 {
13810 r = build_pointer_type (type);
13811 if (TREE_CODE (type) == METHOD_TYPE)
13812 r = build_ptrmemfunc_type (r);
13813 }
13814 else if (TREE_CODE (type) == REFERENCE_TYPE)
13815 /* In C++0x, during template argument substitution, when there is an
13816 attempt to create a reference to a reference type, reference
13817 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13818
13819 "If a template-argument for a template-parameter T names a type
13820 that is a reference to a type A, an attempt to create the type
13821 'lvalue reference to cv T' creates the type 'lvalue reference to
13822 A,' while an attempt to create the type type rvalue reference to
13823 cv T' creates the type T"
13824 */
13825 r = cp_build_reference_type
13826 (TREE_TYPE (type),
13827 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13828 else
13829 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13830 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13831
13832 if (r != error_mark_node)
13833 /* Will this ever be needed for TYPE_..._TO values? */
13834 layout_type (r);
13835
13836 return r;
13837 }
13838 case OFFSET_TYPE:
13839 {
13840 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13841 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13842 {
13843 /* [temp.deduct]
13844
13845 Type deduction may fail for any of the following
13846 reasons:
13847
13848 -- Attempting to create "pointer to member of T" when T
13849 is not a class type. */
13850 if (complain & tf_error)
13851 error ("creating pointer to member of non-class type %qT", r);
13852 return error_mark_node;
13853 }
13854 if (TREE_CODE (type) == REFERENCE_TYPE)
13855 {
13856 if (complain & tf_error)
13857 error ("creating pointer to member reference type %qT", type);
13858 return error_mark_node;
13859 }
13860 if (VOID_TYPE_P (type))
13861 {
13862 if (complain & tf_error)
13863 error ("creating pointer to member of type void");
13864 return error_mark_node;
13865 }
13866 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13867 if (TREE_CODE (type) == FUNCTION_TYPE)
13868 {
13869 /* The type of the implicit object parameter gets its
13870 cv-qualifiers from the FUNCTION_TYPE. */
13871 tree memptr;
13872 tree method_type
13873 = build_memfn_type (type, r, type_memfn_quals (type),
13874 type_memfn_rqual (type));
13875 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13876 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13877 complain);
13878 }
13879 else
13880 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13881 cp_type_quals (t),
13882 complain);
13883 }
13884 case FUNCTION_TYPE:
13885 case METHOD_TYPE:
13886 {
13887 tree fntype;
13888 tree specs;
13889 fntype = tsubst_function_type (t, args, complain, in_decl);
13890 if (fntype == error_mark_node)
13891 return error_mark_node;
13892
13893 /* Substitute the exception specification. */
13894 specs = tsubst_exception_specification (t, args, complain, in_decl,
13895 /*defer_ok*/fndecl_type);
13896 if (specs == error_mark_node)
13897 return error_mark_node;
13898 if (specs)
13899 fntype = build_exception_variant (fntype, specs);
13900 return fntype;
13901 }
13902 case ARRAY_TYPE:
13903 {
13904 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13905 if (domain == error_mark_node)
13906 return error_mark_node;
13907
13908 /* As an optimization, we avoid regenerating the array type if
13909 it will obviously be the same as T. */
13910 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13911 return t;
13912
13913 /* These checks should match the ones in create_array_type_for_decl.
13914
13915 [temp.deduct]
13916
13917 The deduction may fail for any of the following reasons:
13918
13919 -- Attempting to create an array with an element type that
13920 is void, a function type, or a reference type, or [DR337]
13921 an abstract class type. */
13922 if (VOID_TYPE_P (type)
13923 || TREE_CODE (type) == FUNCTION_TYPE
13924 || (TREE_CODE (type) == ARRAY_TYPE
13925 && TYPE_DOMAIN (type) == NULL_TREE)
13926 || TREE_CODE (type) == REFERENCE_TYPE)
13927 {
13928 if (complain & tf_error)
13929 error ("creating array of %qT", type);
13930 return error_mark_node;
13931 }
13932
13933 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13934 return error_mark_node;
13935
13936 r = build_cplus_array_type (type, domain);
13937
13938 if (TYPE_USER_ALIGN (t))
13939 {
13940 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13941 TYPE_USER_ALIGN (r) = 1;
13942 }
13943
13944 return r;
13945 }
13946
13947 case TYPENAME_TYPE:
13948 {
13949 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13950 in_decl, /*entering_scope=*/1);
13951 if (ctx == error_mark_node)
13952 return error_mark_node;
13953
13954 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13955 complain, in_decl);
13956 if (f == error_mark_node)
13957 return error_mark_node;
13958
13959 if (!MAYBE_CLASS_TYPE_P (ctx))
13960 {
13961 if (complain & tf_error)
13962 error ("%qT is not a class, struct, or union type", ctx);
13963 return error_mark_node;
13964 }
13965 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13966 {
13967 /* Normally, make_typename_type does not require that the CTX
13968 have complete type in order to allow things like:
13969
13970 template <class T> struct S { typename S<T>::X Y; };
13971
13972 But, such constructs have already been resolved by this
13973 point, so here CTX really should have complete type, unless
13974 it's a partial instantiation. */
13975 ctx = complete_type (ctx);
13976 if (!COMPLETE_TYPE_P (ctx))
13977 {
13978 if (complain & tf_error)
13979 cxx_incomplete_type_error (NULL_TREE, ctx);
13980 return error_mark_node;
13981 }
13982 }
13983
13984 f = make_typename_type (ctx, f, typename_type,
13985 complain | tf_keep_type_decl);
13986 if (f == error_mark_node)
13987 return f;
13988 if (TREE_CODE (f) == TYPE_DECL)
13989 {
13990 complain |= tf_ignore_bad_quals;
13991 f = TREE_TYPE (f);
13992 }
13993
13994 if (TREE_CODE (f) != TYPENAME_TYPE)
13995 {
13996 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13997 {
13998 if (complain & tf_error)
13999 error ("%qT resolves to %qT, which is not an enumeration type",
14000 t, f);
14001 else
14002 return error_mark_node;
14003 }
14004 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14005 {
14006 if (complain & tf_error)
14007 error ("%qT resolves to %qT, which is is not a class type",
14008 t, f);
14009 else
14010 return error_mark_node;
14011 }
14012 }
14013
14014 return cp_build_qualified_type_real
14015 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14016 }
14017
14018 case UNBOUND_CLASS_TEMPLATE:
14019 {
14020 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14021 in_decl, /*entering_scope=*/1);
14022 tree name = TYPE_IDENTIFIER (t);
14023 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14024
14025 if (ctx == error_mark_node || name == error_mark_node)
14026 return error_mark_node;
14027
14028 if (parm_list)
14029 parm_list = tsubst_template_parms (parm_list, args, complain);
14030 return make_unbound_class_template (ctx, name, parm_list, complain);
14031 }
14032
14033 case TYPEOF_TYPE:
14034 {
14035 tree type;
14036
14037 ++cp_unevaluated_operand;
14038 ++c_inhibit_evaluation_warnings;
14039
14040 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14041 complain, in_decl,
14042 /*integral_constant_expression_p=*/false);
14043
14044 --cp_unevaluated_operand;
14045 --c_inhibit_evaluation_warnings;
14046
14047 type = finish_typeof (type);
14048 return cp_build_qualified_type_real (type,
14049 cp_type_quals (t)
14050 | cp_type_quals (type),
14051 complain);
14052 }
14053
14054 case DECLTYPE_TYPE:
14055 {
14056 tree type;
14057
14058 ++cp_unevaluated_operand;
14059 ++c_inhibit_evaluation_warnings;
14060
14061 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14062 complain|tf_decltype, in_decl,
14063 /*function_p*/false,
14064 /*integral_constant_expression*/false);
14065
14066 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14067 {
14068 if (type == NULL_TREE)
14069 {
14070 if (complain & tf_error)
14071 error ("empty initializer in lambda init-capture");
14072 type = error_mark_node;
14073 }
14074 else if (TREE_CODE (type) == TREE_LIST)
14075 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14076 }
14077
14078 --cp_unevaluated_operand;
14079 --c_inhibit_evaluation_warnings;
14080
14081 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14082 type = lambda_capture_field_type (type,
14083 DECLTYPE_FOR_INIT_CAPTURE (t),
14084 DECLTYPE_FOR_REF_CAPTURE (t));
14085 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14086 type = lambda_proxy_type (type);
14087 else
14088 {
14089 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14090 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14091 && EXPR_P (type))
14092 /* In a template ~id could be either a complement expression
14093 or an unqualified-id naming a destructor; if instantiating
14094 it produces an expression, it's not an id-expression or
14095 member access. */
14096 id = false;
14097 type = finish_decltype_type (type, id, complain);
14098 }
14099 return cp_build_qualified_type_real (type,
14100 cp_type_quals (t)
14101 | cp_type_quals (type),
14102 complain | tf_ignore_bad_quals);
14103 }
14104
14105 case UNDERLYING_TYPE:
14106 {
14107 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14108 complain, in_decl);
14109 return finish_underlying_type (type);
14110 }
14111
14112 case TYPE_ARGUMENT_PACK:
14113 case NONTYPE_ARGUMENT_PACK:
14114 {
14115 tree r;
14116
14117 if (code == NONTYPE_ARGUMENT_PACK)
14118 {
14119 r = make_node (code);
14120 /* Set the already-substituted type. */
14121 TREE_TYPE (r) = type;
14122 }
14123 else
14124 r = cxx_make_type (code);
14125
14126 tree pack_args = ARGUMENT_PACK_ARGS (t);
14127 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14128 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14129
14130 return r;
14131 }
14132
14133 case VOID_CST:
14134 case INTEGER_CST:
14135 case REAL_CST:
14136 case STRING_CST:
14137 case PLUS_EXPR:
14138 case MINUS_EXPR:
14139 case NEGATE_EXPR:
14140 case NOP_EXPR:
14141 case INDIRECT_REF:
14142 case ADDR_EXPR:
14143 case CALL_EXPR:
14144 case ARRAY_REF:
14145 case SCOPE_REF:
14146 /* We should use one of the expression tsubsts for these codes. */
14147 gcc_unreachable ();
14148
14149 default:
14150 sorry ("use of %qs in template", get_tree_code_name (code));
14151 return error_mark_node;
14152 }
14153 }
14154
14155 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14156 type of the expression on the left-hand side of the "." or "->"
14157 operator. */
14158
14159 static tree
14160 tsubst_baselink (tree baselink, tree object_type,
14161 tree args, tsubst_flags_t complain, tree in_decl)
14162 {
14163 tree name;
14164 tree qualifying_scope;
14165 tree fns;
14166 tree optype;
14167 tree template_args = 0;
14168 bool template_id_p = false;
14169 bool qualified = BASELINK_QUALIFIED_P (baselink);
14170
14171 /* A baselink indicates a function from a base class. Both the
14172 BASELINK_ACCESS_BINFO and the base class referenced may
14173 indicate bases of the template class, rather than the
14174 instantiated class. In addition, lookups that were not
14175 ambiguous before may be ambiguous now. Therefore, we perform
14176 the lookup again. */
14177 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14178 qualifying_scope = tsubst (qualifying_scope, args,
14179 complain, in_decl);
14180 fns = BASELINK_FUNCTIONS (baselink);
14181 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
14182 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14183 {
14184 template_id_p = true;
14185 template_args = TREE_OPERAND (fns, 1);
14186 fns = TREE_OPERAND (fns, 0);
14187 if (template_args)
14188 template_args = tsubst_template_args (template_args, args,
14189 complain, in_decl);
14190 }
14191 name = DECL_NAME (get_first_fn (fns));
14192 if (IDENTIFIER_TYPENAME_P (name))
14193 name = mangle_conv_op_name_for_type (optype);
14194 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14195 if (!baselink)
14196 {
14197 if (constructor_name_p (name, qualifying_scope))
14198 {
14199 if (complain & tf_error)
14200 error ("cannot call constructor %<%T::%D%> directly",
14201 qualifying_scope, name);
14202 }
14203 return error_mark_node;
14204 }
14205
14206 /* If lookup found a single function, mark it as used at this
14207 point. (If it lookup found multiple functions the one selected
14208 later by overload resolution will be marked as used at that
14209 point.) */
14210 if (BASELINK_P (baselink))
14211 fns = BASELINK_FUNCTIONS (baselink);
14212 if (!template_id_p && !really_overloaded_fn (fns)
14213 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
14214 return error_mark_node;
14215
14216 /* Add back the template arguments, if present. */
14217 if (BASELINK_P (baselink) && template_id_p)
14218 BASELINK_FUNCTIONS (baselink)
14219 = build2 (TEMPLATE_ID_EXPR,
14220 unknown_type_node,
14221 BASELINK_FUNCTIONS (baselink),
14222 template_args);
14223 /* Update the conversion operator type. */
14224 if (BASELINK_P (baselink))
14225 BASELINK_OPTYPE (baselink) = optype;
14226
14227 if (!object_type)
14228 object_type = current_class_type;
14229
14230 if (qualified || name == complete_dtor_identifier)
14231 {
14232 baselink = adjust_result_of_qualified_name_lookup (baselink,
14233 qualifying_scope,
14234 object_type);
14235 if (!qualified)
14236 /* We need to call adjust_result_of_qualified_name_lookup in case the
14237 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14238 so that we still get virtual function binding. */
14239 BASELINK_QUALIFIED_P (baselink) = false;
14240 }
14241 return baselink;
14242 }
14243
14244 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14245 true if the qualified-id will be a postfix-expression in-and-of
14246 itself; false if more of the postfix-expression follows the
14247 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14248 of "&". */
14249
14250 static tree
14251 tsubst_qualified_id (tree qualified_id, tree args,
14252 tsubst_flags_t complain, tree in_decl,
14253 bool done, bool address_p)
14254 {
14255 tree expr;
14256 tree scope;
14257 tree name;
14258 bool is_template;
14259 tree template_args;
14260 location_t loc = UNKNOWN_LOCATION;
14261
14262 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14263
14264 /* Figure out what name to look up. */
14265 name = TREE_OPERAND (qualified_id, 1);
14266 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14267 {
14268 is_template = true;
14269 loc = EXPR_LOCATION (name);
14270 template_args = TREE_OPERAND (name, 1);
14271 if (template_args)
14272 template_args = tsubst_template_args (template_args, args,
14273 complain, in_decl);
14274 if (template_args == error_mark_node)
14275 return error_mark_node;
14276 name = TREE_OPERAND (name, 0);
14277 }
14278 else
14279 {
14280 is_template = false;
14281 template_args = NULL_TREE;
14282 }
14283
14284 /* Substitute into the qualifying scope. When there are no ARGS, we
14285 are just trying to simplify a non-dependent expression. In that
14286 case the qualifying scope may be dependent, and, in any case,
14287 substituting will not help. */
14288 scope = TREE_OPERAND (qualified_id, 0);
14289 if (args)
14290 {
14291 scope = tsubst (scope, args, complain, in_decl);
14292 expr = tsubst_copy (name, args, complain, in_decl);
14293 }
14294 else
14295 expr = name;
14296
14297 if (dependent_scope_p (scope))
14298 {
14299 if (is_template)
14300 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14301 tree r = build_qualified_name (NULL_TREE, scope, expr,
14302 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14303 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14304 return r;
14305 }
14306
14307 if (!BASELINK_P (name) && !DECL_P (expr))
14308 {
14309 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14310 {
14311 /* A BIT_NOT_EXPR is used to represent a destructor. */
14312 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14313 {
14314 error ("qualifying type %qT does not match destructor name ~%qT",
14315 scope, TREE_OPERAND (expr, 0));
14316 expr = error_mark_node;
14317 }
14318 else
14319 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14320 /*is_type_p=*/0, false);
14321 }
14322 else
14323 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14324 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14325 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14326 {
14327 if (complain & tf_error)
14328 {
14329 error ("dependent-name %qE is parsed as a non-type, but "
14330 "instantiation yields a type", qualified_id);
14331 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14332 }
14333 return error_mark_node;
14334 }
14335 }
14336
14337 if (DECL_P (expr))
14338 {
14339 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14340 scope);
14341 /* Remember that there was a reference to this entity. */
14342 if (!mark_used (expr, complain) && !(complain & tf_error))
14343 return error_mark_node;
14344 }
14345
14346 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14347 {
14348 if (complain & tf_error)
14349 qualified_name_lookup_error (scope,
14350 TREE_OPERAND (qualified_id, 1),
14351 expr, input_location);
14352 return error_mark_node;
14353 }
14354
14355 if (is_template)
14356 {
14357 if (variable_template_p (expr))
14358 expr = lookup_and_finish_template_variable (expr, template_args,
14359 complain);
14360 else
14361 expr = lookup_template_function (expr, template_args);
14362 }
14363
14364 if (expr == error_mark_node && complain & tf_error)
14365 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14366 expr, input_location);
14367 else if (TYPE_P (scope))
14368 {
14369 expr = (adjust_result_of_qualified_name_lookup
14370 (expr, scope, current_nonlambda_class_type ()));
14371 expr = (finish_qualified_id_expr
14372 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14373 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14374 /*template_arg_p=*/false, complain));
14375 }
14376
14377 /* Expressions do not generally have reference type. */
14378 if (TREE_CODE (expr) != SCOPE_REF
14379 /* However, if we're about to form a pointer-to-member, we just
14380 want the referenced member referenced. */
14381 && TREE_CODE (expr) != OFFSET_REF)
14382 expr = convert_from_reference (expr);
14383
14384 if (REF_PARENTHESIZED_P (qualified_id))
14385 expr = force_paren_expr (expr);
14386
14387 return expr;
14388 }
14389
14390 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14391 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14392 for tsubst. */
14393
14394 static tree
14395 tsubst_init (tree init, tree decl, tree args,
14396 tsubst_flags_t complain, tree in_decl)
14397 {
14398 if (!init)
14399 return NULL_TREE;
14400
14401 init = tsubst_expr (init, args, complain, in_decl, false);
14402
14403 if (!init && TREE_TYPE (decl) != error_mark_node)
14404 {
14405 /* If we had an initializer but it
14406 instantiated to nothing,
14407 value-initialize the object. This will
14408 only occur when the initializer was a
14409 pack expansion where the parameter packs
14410 used in that expansion were of length
14411 zero. */
14412 init = build_value_init (TREE_TYPE (decl),
14413 complain);
14414 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14415 init = get_target_expr_sfinae (init, complain);
14416 }
14417
14418 return init;
14419 }
14420
14421 /* Like tsubst, but deals with expressions. This function just replaces
14422 template parms; to finish processing the resultant expression, use
14423 tsubst_copy_and_build or tsubst_expr. */
14424
14425 static tree
14426 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14427 {
14428 enum tree_code code;
14429 tree r;
14430
14431 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14432 return t;
14433
14434 code = TREE_CODE (t);
14435
14436 switch (code)
14437 {
14438 case PARM_DECL:
14439 r = retrieve_local_specialization (t);
14440
14441 if (r == NULL_TREE)
14442 {
14443 /* We get here for a use of 'this' in an NSDMI as part of a
14444 constructor call or as part of an aggregate initialization. */
14445 if (DECL_NAME (t) == this_identifier
14446 && ((current_function_decl
14447 && DECL_CONSTRUCTOR_P (current_function_decl))
14448 || (current_class_ref
14449 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14450 return current_class_ptr;
14451
14452 /* This can happen for a parameter name used later in a function
14453 declaration (such as in a late-specified return type). Just
14454 make a dummy decl, since it's only used for its type. */
14455 gcc_assert (cp_unevaluated_operand != 0);
14456 r = tsubst_decl (t, args, complain);
14457 /* Give it the template pattern as its context; its true context
14458 hasn't been instantiated yet and this is good enough for
14459 mangling. */
14460 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14461 }
14462
14463 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14464 r = ARGUMENT_PACK_SELECT_ARG (r);
14465 if (!mark_used (r, complain) && !(complain & tf_error))
14466 return error_mark_node;
14467 return r;
14468
14469 case CONST_DECL:
14470 {
14471 tree enum_type;
14472 tree v;
14473
14474 if (DECL_TEMPLATE_PARM_P (t))
14475 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14476 /* There is no need to substitute into namespace-scope
14477 enumerators. */
14478 if (DECL_NAMESPACE_SCOPE_P (t))
14479 return t;
14480 /* If ARGS is NULL, then T is known to be non-dependent. */
14481 if (args == NULL_TREE)
14482 return scalar_constant_value (t);
14483
14484 /* Unfortunately, we cannot just call lookup_name here.
14485 Consider:
14486
14487 template <int I> int f() {
14488 enum E { a = I };
14489 struct S { void g() { E e = a; } };
14490 };
14491
14492 When we instantiate f<7>::S::g(), say, lookup_name is not
14493 clever enough to find f<7>::a. */
14494 enum_type
14495 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14496 /*entering_scope=*/0);
14497
14498 for (v = TYPE_VALUES (enum_type);
14499 v != NULL_TREE;
14500 v = TREE_CHAIN (v))
14501 if (TREE_PURPOSE (v) == DECL_NAME (t))
14502 return TREE_VALUE (v);
14503
14504 /* We didn't find the name. That should never happen; if
14505 name-lookup found it during preliminary parsing, we
14506 should find it again here during instantiation. */
14507 gcc_unreachable ();
14508 }
14509 return t;
14510
14511 case FIELD_DECL:
14512 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14513 {
14514 /* Check for a local specialization set up by
14515 tsubst_pack_expansion. */
14516 if (tree r = retrieve_local_specialization (t))
14517 {
14518 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14519 r = ARGUMENT_PACK_SELECT_ARG (r);
14520 return r;
14521 }
14522
14523 /* When retrieving a capture pack from a generic lambda, remove the
14524 lambda call op's own template argument list from ARGS. Only the
14525 template arguments active for the closure type should be used to
14526 retrieve the pack specialization. */
14527 if (LAMBDA_FUNCTION_P (current_function_decl)
14528 && (template_class_depth (DECL_CONTEXT (t))
14529 != TMPL_ARGS_DEPTH (args)))
14530 args = strip_innermost_template_args (args, 1);
14531
14532 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14533 tsubst_decl put in the hash table. */
14534 return retrieve_specialization (t, args, 0);
14535 }
14536
14537 if (DECL_CONTEXT (t))
14538 {
14539 tree ctx;
14540
14541 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14542 /*entering_scope=*/1);
14543 if (ctx != DECL_CONTEXT (t))
14544 {
14545 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14546 if (!r)
14547 {
14548 if (complain & tf_error)
14549 error ("using invalid field %qD", t);
14550 return error_mark_node;
14551 }
14552 return r;
14553 }
14554 }
14555
14556 return t;
14557
14558 case VAR_DECL:
14559 case FUNCTION_DECL:
14560 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14561 r = tsubst (t, args, complain, in_decl);
14562 else if (local_variable_p (t)
14563 && uses_template_parms (DECL_CONTEXT (t)))
14564 {
14565 r = retrieve_local_specialization (t);
14566 if (r == NULL_TREE)
14567 {
14568 /* First try name lookup to find the instantiation. */
14569 r = lookup_name (DECL_NAME (t));
14570 if (r)
14571 {
14572 /* Make sure that the one we found is the one we want. */
14573 tree ctx = DECL_CONTEXT (t);
14574 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14575 ctx = tsubst (ctx, args, complain, in_decl);
14576 if (ctx != DECL_CONTEXT (r))
14577 r = NULL_TREE;
14578 }
14579
14580 if (r)
14581 /* OK */;
14582 else
14583 {
14584 /* This can happen for a variable used in a
14585 late-specified return type of a local lambda, or for a
14586 local static or constant. Building a new VAR_DECL
14587 should be OK in all those cases. */
14588 r = tsubst_decl (t, args, complain);
14589 if (local_specializations)
14590 /* Avoid infinite recursion (79640). */
14591 register_local_specialization (r, t);
14592 if (decl_maybe_constant_var_p (r))
14593 {
14594 /* We can't call cp_finish_decl, so handle the
14595 initializer by hand. */
14596 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14597 complain, in_decl);
14598 if (!processing_template_decl)
14599 init = maybe_constant_init (init);
14600 if (processing_template_decl
14601 ? potential_constant_expression (init)
14602 : reduced_constant_expression_p (init))
14603 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14604 = TREE_CONSTANT (r) = true;
14605 DECL_INITIAL (r) = init;
14606 }
14607 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14608 || decl_constant_var_p (r)
14609 || errorcount || sorrycount);
14610 if (!processing_template_decl
14611 && !TREE_STATIC (r))
14612 r = process_outer_var_ref (r, complain);
14613 }
14614 /* Remember this for subsequent uses. */
14615 if (local_specializations)
14616 register_local_specialization (r, t);
14617 }
14618 }
14619 else
14620 r = t;
14621 if (!mark_used (r, complain))
14622 return error_mark_node;
14623 return r;
14624
14625 case NAMESPACE_DECL:
14626 return t;
14627
14628 case OVERLOAD:
14629 /* An OVERLOAD will always be a non-dependent overload set; an
14630 overload set from function scope will just be represented with an
14631 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14632 gcc_assert (!uses_template_parms (t));
14633 return t;
14634
14635 case BASELINK:
14636 return tsubst_baselink (t, current_nonlambda_class_type (),
14637 args, complain, in_decl);
14638
14639 case TEMPLATE_DECL:
14640 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14641 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14642 args, complain, in_decl);
14643 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14644 return tsubst (t, args, complain, in_decl);
14645 else if (DECL_CLASS_SCOPE_P (t)
14646 && uses_template_parms (DECL_CONTEXT (t)))
14647 {
14648 /* Template template argument like the following example need
14649 special treatment:
14650
14651 template <template <class> class TT> struct C {};
14652 template <class T> struct D {
14653 template <class U> struct E {};
14654 C<E> c; // #1
14655 };
14656 D<int> d; // #2
14657
14658 We are processing the template argument `E' in #1 for
14659 the template instantiation #2. Originally, `E' is a
14660 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14661 have to substitute this with one having context `D<int>'. */
14662
14663 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14664 if (dependent_scope_p (context))
14665 {
14666 /* When rewriting a constructor into a deduction guide, a
14667 non-dependent name can become dependent, so memtmpl<args>
14668 becomes context::template memtmpl<args>. */
14669 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14670 return build_qualified_name (type, context, DECL_NAME (t),
14671 /*template*/true);
14672 }
14673 return lookup_field (context, DECL_NAME(t), 0, false);
14674 }
14675 else
14676 /* Ordinary template template argument. */
14677 return t;
14678
14679 case CAST_EXPR:
14680 case REINTERPRET_CAST_EXPR:
14681 case CONST_CAST_EXPR:
14682 case STATIC_CAST_EXPR:
14683 case DYNAMIC_CAST_EXPR:
14684 case IMPLICIT_CONV_EXPR:
14685 case CONVERT_EXPR:
14686 case NOP_EXPR:
14687 {
14688 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14689 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14690 return build1 (code, type, op0);
14691 }
14692
14693 case SIZEOF_EXPR:
14694 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14695 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14696 {
14697 tree expanded, op = TREE_OPERAND (t, 0);
14698 int len = 0;
14699
14700 if (SIZEOF_EXPR_TYPE_P (t))
14701 op = TREE_TYPE (op);
14702
14703 ++cp_unevaluated_operand;
14704 ++c_inhibit_evaluation_warnings;
14705 /* We only want to compute the number of arguments. */
14706 if (PACK_EXPANSION_P (op))
14707 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14708 else
14709 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14710 args, complain, in_decl);
14711 --cp_unevaluated_operand;
14712 --c_inhibit_evaluation_warnings;
14713
14714 if (TREE_CODE (expanded) == TREE_VEC)
14715 {
14716 len = TREE_VEC_LENGTH (expanded);
14717 /* Set TREE_USED for the benefit of -Wunused. */
14718 for (int i = 0; i < len; i++)
14719 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14720 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14721 }
14722
14723 if (expanded == error_mark_node)
14724 return error_mark_node;
14725 else if (PACK_EXPANSION_P (expanded)
14726 || (TREE_CODE (expanded) == TREE_VEC
14727 && pack_expansion_args_count (expanded)))
14728
14729 {
14730 if (PACK_EXPANSION_P (expanded))
14731 /* OK. */;
14732 else if (TREE_VEC_LENGTH (expanded) == 1)
14733 expanded = TREE_VEC_ELT (expanded, 0);
14734 else
14735 expanded = make_argument_pack (expanded);
14736
14737 if (TYPE_P (expanded))
14738 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14739 complain & tf_error);
14740 else
14741 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14742 complain & tf_error);
14743 }
14744 else
14745 return build_int_cst (size_type_node, len);
14746 }
14747 if (SIZEOF_EXPR_TYPE_P (t))
14748 {
14749 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14750 args, complain, in_decl);
14751 r = build1 (NOP_EXPR, r, error_mark_node);
14752 r = build1 (SIZEOF_EXPR,
14753 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14754 SIZEOF_EXPR_TYPE_P (r) = 1;
14755 return r;
14756 }
14757 /* Fall through */
14758
14759 case INDIRECT_REF:
14760 case NEGATE_EXPR:
14761 case TRUTH_NOT_EXPR:
14762 case BIT_NOT_EXPR:
14763 case ADDR_EXPR:
14764 case UNARY_PLUS_EXPR: /* Unary + */
14765 case ALIGNOF_EXPR:
14766 case AT_ENCODE_EXPR:
14767 case ARROW_EXPR:
14768 case THROW_EXPR:
14769 case TYPEID_EXPR:
14770 case REALPART_EXPR:
14771 case IMAGPART_EXPR:
14772 case PAREN_EXPR:
14773 {
14774 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14775 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14776 return build1 (code, type, op0);
14777 }
14778
14779 case COMPONENT_REF:
14780 {
14781 tree object;
14782 tree name;
14783
14784 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14785 name = TREE_OPERAND (t, 1);
14786 if (TREE_CODE (name) == BIT_NOT_EXPR)
14787 {
14788 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14789 complain, in_decl);
14790 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14791 }
14792 else if (TREE_CODE (name) == SCOPE_REF
14793 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14794 {
14795 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14796 complain, in_decl);
14797 name = TREE_OPERAND (name, 1);
14798 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14799 complain, in_decl);
14800 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14801 name = build_qualified_name (/*type=*/NULL_TREE,
14802 base, name,
14803 /*template_p=*/false);
14804 }
14805 else if (BASELINK_P (name))
14806 name = tsubst_baselink (name,
14807 non_reference (TREE_TYPE (object)),
14808 args, complain,
14809 in_decl);
14810 else
14811 name = tsubst_copy (name, args, complain, in_decl);
14812 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14813 }
14814
14815 case PLUS_EXPR:
14816 case MINUS_EXPR:
14817 case MULT_EXPR:
14818 case TRUNC_DIV_EXPR:
14819 case CEIL_DIV_EXPR:
14820 case FLOOR_DIV_EXPR:
14821 case ROUND_DIV_EXPR:
14822 case EXACT_DIV_EXPR:
14823 case BIT_AND_EXPR:
14824 case BIT_IOR_EXPR:
14825 case BIT_XOR_EXPR:
14826 case TRUNC_MOD_EXPR:
14827 case FLOOR_MOD_EXPR:
14828 case TRUTH_ANDIF_EXPR:
14829 case TRUTH_ORIF_EXPR:
14830 case TRUTH_AND_EXPR:
14831 case TRUTH_OR_EXPR:
14832 case RSHIFT_EXPR:
14833 case LSHIFT_EXPR:
14834 case RROTATE_EXPR:
14835 case LROTATE_EXPR:
14836 case EQ_EXPR:
14837 case NE_EXPR:
14838 case MAX_EXPR:
14839 case MIN_EXPR:
14840 case LE_EXPR:
14841 case GE_EXPR:
14842 case LT_EXPR:
14843 case GT_EXPR:
14844 case COMPOUND_EXPR:
14845 case DOTSTAR_EXPR:
14846 case MEMBER_REF:
14847 case PREDECREMENT_EXPR:
14848 case PREINCREMENT_EXPR:
14849 case POSTDECREMENT_EXPR:
14850 case POSTINCREMENT_EXPR:
14851 {
14852 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14853 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14854 return build_nt (code, op0, op1);
14855 }
14856
14857 case SCOPE_REF:
14858 {
14859 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14860 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14861 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14862 QUALIFIED_NAME_IS_TEMPLATE (t));
14863 }
14864
14865 case ARRAY_REF:
14866 {
14867 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14868 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14869 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14870 }
14871
14872 case CALL_EXPR:
14873 {
14874 int n = VL_EXP_OPERAND_LENGTH (t);
14875 tree result = build_vl_exp (CALL_EXPR, n);
14876 int i;
14877 for (i = 0; i < n; i++)
14878 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14879 complain, in_decl);
14880 return result;
14881 }
14882
14883 case COND_EXPR:
14884 case MODOP_EXPR:
14885 case PSEUDO_DTOR_EXPR:
14886 case VEC_PERM_EXPR:
14887 {
14888 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14889 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14890 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14891 r = build_nt (code, op0, op1, op2);
14892 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14893 return r;
14894 }
14895
14896 case NEW_EXPR:
14897 {
14898 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14899 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14900 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14901 r = build_nt (code, op0, op1, op2);
14902 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14903 return r;
14904 }
14905
14906 case DELETE_EXPR:
14907 {
14908 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14909 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14910 r = build_nt (code, op0, op1);
14911 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14912 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14913 return r;
14914 }
14915
14916 case TEMPLATE_ID_EXPR:
14917 {
14918 /* Substituted template arguments */
14919 tree fn = TREE_OPERAND (t, 0);
14920 tree targs = TREE_OPERAND (t, 1);
14921
14922 fn = tsubst_copy (fn, args, complain, in_decl);
14923 if (targs)
14924 targs = tsubst_template_args (targs, args, complain, in_decl);
14925
14926 return lookup_template_function (fn, targs);
14927 }
14928
14929 case TREE_LIST:
14930 {
14931 tree purpose, value, chain;
14932
14933 if (t == void_list_node)
14934 return t;
14935
14936 purpose = TREE_PURPOSE (t);
14937 if (purpose)
14938 purpose = tsubst_copy (purpose, args, complain, in_decl);
14939 value = TREE_VALUE (t);
14940 if (value)
14941 value = tsubst_copy (value, args, complain, in_decl);
14942 chain = TREE_CHAIN (t);
14943 if (chain && chain != void_type_node)
14944 chain = tsubst_copy (chain, args, complain, in_decl);
14945 if (purpose == TREE_PURPOSE (t)
14946 && value == TREE_VALUE (t)
14947 && chain == TREE_CHAIN (t))
14948 return t;
14949 return tree_cons (purpose, value, chain);
14950 }
14951
14952 case RECORD_TYPE:
14953 case UNION_TYPE:
14954 case ENUMERAL_TYPE:
14955 case INTEGER_TYPE:
14956 case TEMPLATE_TYPE_PARM:
14957 case TEMPLATE_TEMPLATE_PARM:
14958 case BOUND_TEMPLATE_TEMPLATE_PARM:
14959 case TEMPLATE_PARM_INDEX:
14960 case POINTER_TYPE:
14961 case REFERENCE_TYPE:
14962 case OFFSET_TYPE:
14963 case FUNCTION_TYPE:
14964 case METHOD_TYPE:
14965 case ARRAY_TYPE:
14966 case TYPENAME_TYPE:
14967 case UNBOUND_CLASS_TEMPLATE:
14968 case TYPEOF_TYPE:
14969 case DECLTYPE_TYPE:
14970 case TYPE_DECL:
14971 return tsubst (t, args, complain, in_decl);
14972
14973 case USING_DECL:
14974 t = DECL_NAME (t);
14975 /* Fall through. */
14976 case IDENTIFIER_NODE:
14977 if (IDENTIFIER_TYPENAME_P (t))
14978 {
14979 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14980 return mangle_conv_op_name_for_type (new_type);
14981 }
14982 else
14983 return t;
14984
14985 case CONSTRUCTOR:
14986 /* This is handled by tsubst_copy_and_build. */
14987 gcc_unreachable ();
14988
14989 case VA_ARG_EXPR:
14990 {
14991 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14992 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14993 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14994 }
14995
14996 case CLEANUP_POINT_EXPR:
14997 /* We shouldn't have built any of these during initial template
14998 generation. Instead, they should be built during instantiation
14999 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15000 gcc_unreachable ();
15001
15002 case OFFSET_REF:
15003 {
15004 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15005 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15006 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15007 r = build2 (code, type, op0, op1);
15008 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15009 if (!mark_used (TREE_OPERAND (r, 1), complain)
15010 && !(complain & tf_error))
15011 return error_mark_node;
15012 return r;
15013 }
15014
15015 case EXPR_PACK_EXPANSION:
15016 error ("invalid use of pack expansion expression");
15017 return error_mark_node;
15018
15019 case NONTYPE_ARGUMENT_PACK:
15020 error ("use %<...%> to expand argument pack");
15021 return error_mark_node;
15022
15023 case VOID_CST:
15024 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15025 return t;
15026
15027 case INTEGER_CST:
15028 case REAL_CST:
15029 case STRING_CST:
15030 case COMPLEX_CST:
15031 {
15032 /* Instantiate any typedefs in the type. */
15033 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15034 r = fold_convert (type, t);
15035 gcc_assert (TREE_CODE (r) == code);
15036 return r;
15037 }
15038
15039 case PTRMEM_CST:
15040 /* These can sometimes show up in a partial instantiation, but never
15041 involve template parms. */
15042 gcc_assert (!uses_template_parms (t));
15043 return t;
15044
15045 case UNARY_LEFT_FOLD_EXPR:
15046 return tsubst_unary_left_fold (t, args, complain, in_decl);
15047 case UNARY_RIGHT_FOLD_EXPR:
15048 return tsubst_unary_right_fold (t, args, complain, in_decl);
15049 case BINARY_LEFT_FOLD_EXPR:
15050 return tsubst_binary_left_fold (t, args, complain, in_decl);
15051 case BINARY_RIGHT_FOLD_EXPR:
15052 return tsubst_binary_right_fold (t, args, complain, in_decl);
15053
15054 default:
15055 /* We shouldn't get here, but keep going if !flag_checking. */
15056 if (flag_checking)
15057 gcc_unreachable ();
15058 return t;
15059 }
15060 }
15061
15062 /* Helper function for tsubst_omp_clauses, used for instantiation of
15063 OMP_CLAUSE_DECL of clauses. */
15064
15065 static tree
15066 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15067 tree in_decl)
15068 {
15069 if (decl == NULL_TREE)
15070 return NULL_TREE;
15071
15072 /* Handle an OpenMP array section represented as a TREE_LIST (or
15073 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15074 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15075 TREE_LIST. We can handle it exactly the same as an array section
15076 (purpose, value, and a chain), even though the nomenclature
15077 (low_bound, length, etc) is different. */
15078 if (TREE_CODE (decl) == TREE_LIST)
15079 {
15080 tree low_bound
15081 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15082 /*integral_constant_expression_p=*/false);
15083 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15084 /*integral_constant_expression_p=*/false);
15085 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15086 in_decl);
15087 if (TREE_PURPOSE (decl) == low_bound
15088 && TREE_VALUE (decl) == length
15089 && TREE_CHAIN (decl) == chain)
15090 return decl;
15091 tree ret = tree_cons (low_bound, length, chain);
15092 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15093 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15094 return ret;
15095 }
15096 tree ret = tsubst_expr (decl, args, complain, in_decl,
15097 /*integral_constant_expression_p=*/false);
15098 /* Undo convert_from_reference tsubst_expr could have called. */
15099 if (decl
15100 && REFERENCE_REF_P (ret)
15101 && !REFERENCE_REF_P (decl))
15102 ret = TREE_OPERAND (ret, 0);
15103 return ret;
15104 }
15105
15106 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15107
15108 static tree
15109 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15110 tree args, tsubst_flags_t complain, tree in_decl)
15111 {
15112 tree new_clauses = NULL_TREE, nc, oc;
15113 tree linear_no_step = NULL_TREE;
15114
15115 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15116 {
15117 nc = copy_node (oc);
15118 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15119 new_clauses = nc;
15120
15121 switch (OMP_CLAUSE_CODE (nc))
15122 {
15123 case OMP_CLAUSE_LASTPRIVATE:
15124 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15125 {
15126 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15127 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15128 in_decl, /*integral_constant_expression_p=*/false);
15129 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15130 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15131 }
15132 /* FALLTHRU */
15133 case OMP_CLAUSE_PRIVATE:
15134 case OMP_CLAUSE_SHARED:
15135 case OMP_CLAUSE_FIRSTPRIVATE:
15136 case OMP_CLAUSE_COPYIN:
15137 case OMP_CLAUSE_COPYPRIVATE:
15138 case OMP_CLAUSE_UNIFORM:
15139 case OMP_CLAUSE_DEPEND:
15140 case OMP_CLAUSE_FROM:
15141 case OMP_CLAUSE_TO:
15142 case OMP_CLAUSE_MAP:
15143 case OMP_CLAUSE_USE_DEVICE_PTR:
15144 case OMP_CLAUSE_IS_DEVICE_PTR:
15145 OMP_CLAUSE_DECL (nc)
15146 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15147 in_decl);
15148 break;
15149 case OMP_CLAUSE_TILE:
15150 case OMP_CLAUSE_IF:
15151 case OMP_CLAUSE_NUM_THREADS:
15152 case OMP_CLAUSE_SCHEDULE:
15153 case OMP_CLAUSE_COLLAPSE:
15154 case OMP_CLAUSE_FINAL:
15155 case OMP_CLAUSE_DEVICE:
15156 case OMP_CLAUSE_DIST_SCHEDULE:
15157 case OMP_CLAUSE_NUM_TEAMS:
15158 case OMP_CLAUSE_THREAD_LIMIT:
15159 case OMP_CLAUSE_SAFELEN:
15160 case OMP_CLAUSE_SIMDLEN:
15161 case OMP_CLAUSE_NUM_TASKS:
15162 case OMP_CLAUSE_GRAINSIZE:
15163 case OMP_CLAUSE_PRIORITY:
15164 case OMP_CLAUSE_ORDERED:
15165 case OMP_CLAUSE_HINT:
15166 case OMP_CLAUSE_NUM_GANGS:
15167 case OMP_CLAUSE_NUM_WORKERS:
15168 case OMP_CLAUSE_VECTOR_LENGTH:
15169 case OMP_CLAUSE_WORKER:
15170 case OMP_CLAUSE_VECTOR:
15171 case OMP_CLAUSE_ASYNC:
15172 case OMP_CLAUSE_WAIT:
15173 OMP_CLAUSE_OPERAND (nc, 0)
15174 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15175 in_decl, /*integral_constant_expression_p=*/false);
15176 break;
15177 case OMP_CLAUSE_REDUCTION:
15178 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15179 {
15180 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15181 if (TREE_CODE (placeholder) == SCOPE_REF)
15182 {
15183 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15184 complain, in_decl);
15185 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15186 = build_qualified_name (NULL_TREE, scope,
15187 TREE_OPERAND (placeholder, 1),
15188 false);
15189 }
15190 else
15191 gcc_assert (identifier_p (placeholder));
15192 }
15193 OMP_CLAUSE_DECL (nc)
15194 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15195 in_decl);
15196 break;
15197 case OMP_CLAUSE_GANG:
15198 case OMP_CLAUSE_ALIGNED:
15199 OMP_CLAUSE_DECL (nc)
15200 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15201 in_decl);
15202 OMP_CLAUSE_OPERAND (nc, 1)
15203 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15204 in_decl, /*integral_constant_expression_p=*/false);
15205 break;
15206 case OMP_CLAUSE_LINEAR:
15207 OMP_CLAUSE_DECL (nc)
15208 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15209 in_decl);
15210 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15211 {
15212 gcc_assert (!linear_no_step);
15213 linear_no_step = nc;
15214 }
15215 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15216 OMP_CLAUSE_LINEAR_STEP (nc)
15217 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15218 complain, in_decl);
15219 else
15220 OMP_CLAUSE_LINEAR_STEP (nc)
15221 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15222 in_decl,
15223 /*integral_constant_expression_p=*/false);
15224 break;
15225 case OMP_CLAUSE_NOWAIT:
15226 case OMP_CLAUSE_DEFAULT:
15227 case OMP_CLAUSE_UNTIED:
15228 case OMP_CLAUSE_MERGEABLE:
15229 case OMP_CLAUSE_INBRANCH:
15230 case OMP_CLAUSE_NOTINBRANCH:
15231 case OMP_CLAUSE_PROC_BIND:
15232 case OMP_CLAUSE_FOR:
15233 case OMP_CLAUSE_PARALLEL:
15234 case OMP_CLAUSE_SECTIONS:
15235 case OMP_CLAUSE_TASKGROUP:
15236 case OMP_CLAUSE_NOGROUP:
15237 case OMP_CLAUSE_THREADS:
15238 case OMP_CLAUSE_SIMD:
15239 case OMP_CLAUSE_DEFAULTMAP:
15240 case OMP_CLAUSE_INDEPENDENT:
15241 case OMP_CLAUSE_AUTO:
15242 case OMP_CLAUSE_SEQ:
15243 break;
15244 default:
15245 gcc_unreachable ();
15246 }
15247 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15248 switch (OMP_CLAUSE_CODE (nc))
15249 {
15250 case OMP_CLAUSE_SHARED:
15251 case OMP_CLAUSE_PRIVATE:
15252 case OMP_CLAUSE_FIRSTPRIVATE:
15253 case OMP_CLAUSE_LASTPRIVATE:
15254 case OMP_CLAUSE_COPYPRIVATE:
15255 case OMP_CLAUSE_LINEAR:
15256 case OMP_CLAUSE_REDUCTION:
15257 case OMP_CLAUSE_USE_DEVICE_PTR:
15258 case OMP_CLAUSE_IS_DEVICE_PTR:
15259 /* tsubst_expr on SCOPE_REF results in returning
15260 finish_non_static_data_member result. Undo that here. */
15261 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15262 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15263 == IDENTIFIER_NODE))
15264 {
15265 tree t = OMP_CLAUSE_DECL (nc);
15266 tree v = t;
15267 while (v)
15268 switch (TREE_CODE (v))
15269 {
15270 case COMPONENT_REF:
15271 case MEM_REF:
15272 case INDIRECT_REF:
15273 CASE_CONVERT:
15274 case POINTER_PLUS_EXPR:
15275 v = TREE_OPERAND (v, 0);
15276 continue;
15277 case PARM_DECL:
15278 if (DECL_CONTEXT (v) == current_function_decl
15279 && DECL_ARTIFICIAL (v)
15280 && DECL_NAME (v) == this_identifier)
15281 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15282 /* FALLTHRU */
15283 default:
15284 v = NULL_TREE;
15285 break;
15286 }
15287 }
15288 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15289 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15290 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15291 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15292 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15293 {
15294 tree decl = OMP_CLAUSE_DECL (nc);
15295 if (VAR_P (decl))
15296 {
15297 if (!DECL_LANG_SPECIFIC (decl))
15298 retrofit_lang_decl (decl);
15299 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15300 }
15301 }
15302 break;
15303 default:
15304 break;
15305 }
15306 }
15307
15308 new_clauses = nreverse (new_clauses);
15309 if (ort != C_ORT_OMP_DECLARE_SIMD)
15310 {
15311 new_clauses = finish_omp_clauses (new_clauses, ort);
15312 if (linear_no_step)
15313 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15314 if (nc == linear_no_step)
15315 {
15316 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15317 break;
15318 }
15319 }
15320 return new_clauses;
15321 }
15322
15323 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15324
15325 static tree
15326 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15327 tree in_decl)
15328 {
15329 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15330
15331 tree purpose, value, chain;
15332
15333 if (t == NULL)
15334 return t;
15335
15336 if (TREE_CODE (t) != TREE_LIST)
15337 return tsubst_copy_and_build (t, args, complain, in_decl,
15338 /*function_p=*/false,
15339 /*integral_constant_expression_p=*/false);
15340
15341 if (t == void_list_node)
15342 return t;
15343
15344 purpose = TREE_PURPOSE (t);
15345 if (purpose)
15346 purpose = RECUR (purpose);
15347 value = TREE_VALUE (t);
15348 if (value)
15349 {
15350 if (TREE_CODE (value) != LABEL_DECL)
15351 value = RECUR (value);
15352 else
15353 {
15354 value = lookup_label (DECL_NAME (value));
15355 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15356 TREE_USED (value) = 1;
15357 }
15358 }
15359 chain = TREE_CHAIN (t);
15360 if (chain && chain != void_type_node)
15361 chain = RECUR (chain);
15362 return tree_cons (purpose, value, chain);
15363 #undef RECUR
15364 }
15365
15366 /* Used to temporarily communicate the list of #pragma omp parallel
15367 clauses to #pragma omp for instantiation if they are combined
15368 together. */
15369
15370 static tree *omp_parallel_combined_clauses;
15371
15372 /* Substitute one OMP_FOR iterator. */
15373
15374 static void
15375 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15376 tree initv, tree condv, tree incrv, tree *clauses,
15377 tree args, tsubst_flags_t complain, tree in_decl,
15378 bool integral_constant_expression_p)
15379 {
15380 #define RECUR(NODE) \
15381 tsubst_expr ((NODE), args, complain, in_decl, \
15382 integral_constant_expression_p)
15383 tree decl, init, cond, incr;
15384
15385 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15386 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15387
15388 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15389 {
15390 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15391 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15392 }
15393
15394 decl = TREE_OPERAND (init, 0);
15395 init = TREE_OPERAND (init, 1);
15396 tree decl_expr = NULL_TREE;
15397 if (init && TREE_CODE (init) == DECL_EXPR)
15398 {
15399 /* We need to jump through some hoops to handle declarations in the
15400 init-statement, since we might need to handle auto deduction,
15401 but we need to keep control of initialization. */
15402 decl_expr = init;
15403 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15404 decl = tsubst_decl (decl, args, complain);
15405 }
15406 else
15407 {
15408 if (TREE_CODE (decl) == SCOPE_REF)
15409 {
15410 decl = RECUR (decl);
15411 if (TREE_CODE (decl) == COMPONENT_REF)
15412 {
15413 tree v = decl;
15414 while (v)
15415 switch (TREE_CODE (v))
15416 {
15417 case COMPONENT_REF:
15418 case MEM_REF:
15419 case INDIRECT_REF:
15420 CASE_CONVERT:
15421 case POINTER_PLUS_EXPR:
15422 v = TREE_OPERAND (v, 0);
15423 continue;
15424 case PARM_DECL:
15425 if (DECL_CONTEXT (v) == current_function_decl
15426 && DECL_ARTIFICIAL (v)
15427 && DECL_NAME (v) == this_identifier)
15428 {
15429 decl = TREE_OPERAND (decl, 1);
15430 decl = omp_privatize_field (decl, false);
15431 }
15432 /* FALLTHRU */
15433 default:
15434 v = NULL_TREE;
15435 break;
15436 }
15437 }
15438 }
15439 else
15440 decl = RECUR (decl);
15441 }
15442 init = RECUR (init);
15443
15444 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15445 if (auto_node && init)
15446 TREE_TYPE (decl)
15447 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15448
15449 gcc_assert (!type_dependent_expression_p (decl));
15450
15451 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15452 {
15453 if (decl_expr)
15454 {
15455 /* Declare the variable, but don't let that initialize it. */
15456 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15457 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15458 RECUR (decl_expr);
15459 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15460 }
15461
15462 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15463 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15464 if (TREE_CODE (incr) == MODIFY_EXPR)
15465 {
15466 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15467 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15468 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15469 NOP_EXPR, rhs, complain);
15470 }
15471 else
15472 incr = RECUR (incr);
15473 TREE_VEC_ELT (declv, i) = decl;
15474 TREE_VEC_ELT (initv, i) = init;
15475 TREE_VEC_ELT (condv, i) = cond;
15476 TREE_VEC_ELT (incrv, i) = incr;
15477 return;
15478 }
15479
15480 if (decl_expr)
15481 {
15482 /* Declare and initialize the variable. */
15483 RECUR (decl_expr);
15484 init = NULL_TREE;
15485 }
15486 else if (init)
15487 {
15488 tree *pc;
15489 int j;
15490 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15491 {
15492 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15493 {
15494 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15495 && OMP_CLAUSE_DECL (*pc) == decl)
15496 break;
15497 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15498 && OMP_CLAUSE_DECL (*pc) == decl)
15499 {
15500 if (j)
15501 break;
15502 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15503 tree c = *pc;
15504 *pc = OMP_CLAUSE_CHAIN (c);
15505 OMP_CLAUSE_CHAIN (c) = *clauses;
15506 *clauses = c;
15507 }
15508 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15509 && OMP_CLAUSE_DECL (*pc) == decl)
15510 {
15511 error ("iteration variable %qD should not be firstprivate",
15512 decl);
15513 *pc = OMP_CLAUSE_CHAIN (*pc);
15514 }
15515 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15516 && OMP_CLAUSE_DECL (*pc) == decl)
15517 {
15518 error ("iteration variable %qD should not be reduction",
15519 decl);
15520 *pc = OMP_CLAUSE_CHAIN (*pc);
15521 }
15522 else
15523 pc = &OMP_CLAUSE_CHAIN (*pc);
15524 }
15525 if (*pc)
15526 break;
15527 }
15528 if (*pc == NULL_TREE)
15529 {
15530 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15531 OMP_CLAUSE_DECL (c) = decl;
15532 c = finish_omp_clauses (c, C_ORT_OMP);
15533 if (c)
15534 {
15535 OMP_CLAUSE_CHAIN (c) = *clauses;
15536 *clauses = c;
15537 }
15538 }
15539 }
15540 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15541 if (COMPARISON_CLASS_P (cond))
15542 {
15543 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15544 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15545 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15546 }
15547 else
15548 cond = RECUR (cond);
15549 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15550 switch (TREE_CODE (incr))
15551 {
15552 case PREINCREMENT_EXPR:
15553 case PREDECREMENT_EXPR:
15554 case POSTINCREMENT_EXPR:
15555 case POSTDECREMENT_EXPR:
15556 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15557 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15558 break;
15559 case MODIFY_EXPR:
15560 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15561 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15562 {
15563 tree rhs = TREE_OPERAND (incr, 1);
15564 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15565 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15566 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15567 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15568 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15569 rhs0, rhs1));
15570 }
15571 else
15572 incr = RECUR (incr);
15573 break;
15574 case MODOP_EXPR:
15575 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15576 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15577 {
15578 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15579 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15580 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15581 TREE_TYPE (decl), lhs,
15582 RECUR (TREE_OPERAND (incr, 2))));
15583 }
15584 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15585 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15586 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15587 {
15588 tree rhs = TREE_OPERAND (incr, 2);
15589 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15590 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15591 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15592 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15593 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15594 rhs0, rhs1));
15595 }
15596 else
15597 incr = RECUR (incr);
15598 break;
15599 default:
15600 incr = RECUR (incr);
15601 break;
15602 }
15603
15604 TREE_VEC_ELT (declv, i) = decl;
15605 TREE_VEC_ELT (initv, i) = init;
15606 TREE_VEC_ELT (condv, i) = cond;
15607 TREE_VEC_ELT (incrv, i) = incr;
15608 #undef RECUR
15609 }
15610
15611 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15612 of OMP_TARGET's body. */
15613
15614 static tree
15615 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15616 {
15617 *walk_subtrees = 0;
15618 switch (TREE_CODE (*tp))
15619 {
15620 case OMP_TEAMS:
15621 return *tp;
15622 case BIND_EXPR:
15623 case STATEMENT_LIST:
15624 *walk_subtrees = 1;
15625 break;
15626 default:
15627 break;
15628 }
15629 return NULL_TREE;
15630 }
15631
15632 /* Helper function for tsubst_expr. For decomposition declaration
15633 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15634 also the corresponding decls representing the identifiers
15635 of the decomposition declaration. Return DECL if successful
15636 or error_mark_node otherwise, set *FIRST to the first decl
15637 in the list chained through DECL_CHAIN and *CNT to the number
15638 of such decls. */
15639
15640 static tree
15641 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15642 tsubst_flags_t complain, tree in_decl, tree *first,
15643 unsigned int *cnt)
15644 {
15645 tree decl2, decl3, prev = decl;
15646 *cnt = 0;
15647 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15648 for (decl2 = DECL_CHAIN (pattern_decl);
15649 decl2
15650 && VAR_P (decl2)
15651 && DECL_DECOMPOSITION_P (decl2)
15652 && DECL_NAME (decl2);
15653 decl2 = DECL_CHAIN (decl2))
15654 {
15655 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15656 {
15657 gcc_assert (errorcount);
15658 return error_mark_node;
15659 }
15660 (*cnt)++;
15661 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15662 tree v = DECL_VALUE_EXPR (decl2);
15663 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15664 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15665 decl3 = tsubst (decl2, args, complain, in_decl);
15666 SET_DECL_VALUE_EXPR (decl2, v);
15667 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15668 if (VAR_P (decl3))
15669 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15670 maybe_push_decl (decl3);
15671 if (error_operand_p (decl3))
15672 decl = error_mark_node;
15673 else if (decl != error_mark_node
15674 && DECL_CHAIN (decl3) != prev)
15675 {
15676 gcc_assert (errorcount);
15677 decl = error_mark_node;
15678 }
15679 else
15680 prev = decl3;
15681 }
15682 *first = prev;
15683 return decl;
15684 }
15685
15686 /* Like tsubst_copy for expressions, etc. but also does semantic
15687 processing. */
15688
15689 tree
15690 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15691 bool integral_constant_expression_p)
15692 {
15693 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15694 #define RECUR(NODE) \
15695 tsubst_expr ((NODE), args, complain, in_decl, \
15696 integral_constant_expression_p)
15697
15698 tree stmt, tmp;
15699 tree r;
15700 location_t loc;
15701
15702 if (t == NULL_TREE || t == error_mark_node)
15703 return t;
15704
15705 loc = input_location;
15706 if (EXPR_HAS_LOCATION (t))
15707 input_location = EXPR_LOCATION (t);
15708 if (STATEMENT_CODE_P (TREE_CODE (t)))
15709 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15710
15711 switch (TREE_CODE (t))
15712 {
15713 case STATEMENT_LIST:
15714 {
15715 tree_stmt_iterator i;
15716 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15717 RECUR (tsi_stmt (i));
15718 break;
15719 }
15720
15721 case CTOR_INITIALIZER:
15722 finish_mem_initializers (tsubst_initializer_list
15723 (TREE_OPERAND (t, 0), args));
15724 break;
15725
15726 case RETURN_EXPR:
15727 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15728 break;
15729
15730 case EXPR_STMT:
15731 tmp = RECUR (EXPR_STMT_EXPR (t));
15732 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15733 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15734 else
15735 finish_expr_stmt (tmp);
15736 break;
15737
15738 case USING_STMT:
15739 do_using_directive (USING_STMT_NAMESPACE (t));
15740 break;
15741
15742 case DECL_EXPR:
15743 {
15744 tree decl, pattern_decl;
15745 tree init;
15746
15747 pattern_decl = decl = DECL_EXPR_DECL (t);
15748 if (TREE_CODE (decl) == LABEL_DECL)
15749 finish_label_decl (DECL_NAME (decl));
15750 else if (TREE_CODE (decl) == USING_DECL)
15751 {
15752 tree scope = USING_DECL_SCOPE (decl);
15753 tree name = DECL_NAME (decl);
15754
15755 scope = tsubst (scope, args, complain, in_decl);
15756 decl = lookup_qualified_name (scope, name,
15757 /*is_type_p=*/false,
15758 /*complain=*/false);
15759 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15760 qualified_name_lookup_error (scope, name, decl, input_location);
15761 else
15762 do_local_using_decl (decl, scope, name);
15763 }
15764 else if (DECL_PACK_P (decl))
15765 {
15766 /* Don't build up decls for a variadic capture proxy, we'll
15767 instantiate the elements directly as needed. */
15768 break;
15769 }
15770 else
15771 {
15772 init = DECL_INITIAL (decl);
15773 decl = tsubst (decl, args, complain, in_decl);
15774 if (decl != error_mark_node)
15775 {
15776 /* By marking the declaration as instantiated, we avoid
15777 trying to instantiate it. Since instantiate_decl can't
15778 handle local variables, and since we've already done
15779 all that needs to be done, that's the right thing to
15780 do. */
15781 if (VAR_P (decl))
15782 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15783 if (VAR_P (decl)
15784 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15785 /* Anonymous aggregates are a special case. */
15786 finish_anon_union (decl);
15787 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15788 {
15789 DECL_CONTEXT (decl) = current_function_decl;
15790 if (DECL_NAME (decl) == this_identifier)
15791 {
15792 tree lam = DECL_CONTEXT (current_function_decl);
15793 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15794 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15795 }
15796 insert_capture_proxy (decl);
15797 }
15798 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15799 /* We already did a pushtag. */;
15800 else if (TREE_CODE (decl) == FUNCTION_DECL
15801 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15802 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15803 {
15804 DECL_CONTEXT (decl) = NULL_TREE;
15805 pushdecl (decl);
15806 DECL_CONTEXT (decl) = current_function_decl;
15807 cp_check_omp_declare_reduction (decl);
15808 }
15809 else
15810 {
15811 int const_init = false;
15812 maybe_push_decl (decl);
15813 if (VAR_P (decl)
15814 && DECL_PRETTY_FUNCTION_P (decl))
15815 {
15816 /* For __PRETTY_FUNCTION__ we have to adjust the
15817 initializer. */
15818 const char *const name
15819 = cxx_printable_name (current_function_decl, 2);
15820 init = cp_fname_init (name, &TREE_TYPE (decl));
15821 }
15822 else
15823 init = tsubst_init (init, decl, args, complain, in_decl);
15824
15825 if (VAR_P (decl))
15826 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15827 (pattern_decl));
15828 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15829 if (VAR_P (decl)
15830 && DECL_DECOMPOSITION_P (decl)
15831 && TREE_TYPE (pattern_decl) != error_mark_node)
15832 {
15833 unsigned int cnt;
15834 tree first;
15835 decl = tsubst_decomp_names (decl, pattern_decl, args,
15836 complain, in_decl, &first,
15837 &cnt);
15838 if (decl != error_mark_node)
15839 cp_finish_decomp (decl, first, cnt);
15840 }
15841 }
15842 }
15843 }
15844
15845 break;
15846 }
15847
15848 case FOR_STMT:
15849 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15850 RECUR (FOR_INIT_STMT (t));
15851 finish_init_stmt (stmt);
15852 tmp = RECUR (FOR_COND (t));
15853 finish_for_cond (tmp, stmt, false);
15854 tmp = RECUR (FOR_EXPR (t));
15855 finish_for_expr (tmp, stmt);
15856 RECUR (FOR_BODY (t));
15857 finish_for_stmt (stmt);
15858 break;
15859
15860 case RANGE_FOR_STMT:
15861 {
15862 tree decl, expr;
15863 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15864 decl = RANGE_FOR_DECL (t);
15865 decl = tsubst (decl, args, complain, in_decl);
15866 maybe_push_decl (decl);
15867 expr = RECUR (RANGE_FOR_EXPR (t));
15868 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15869 {
15870 unsigned int cnt;
15871 tree first;
15872 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15873 complain, in_decl, &first, &cnt);
15874 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15875 RANGE_FOR_IVDEP (t));
15876 }
15877 else
15878 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15879 RANGE_FOR_IVDEP (t));
15880 RECUR (RANGE_FOR_BODY (t));
15881 finish_for_stmt (stmt);
15882 }
15883 break;
15884
15885 case WHILE_STMT:
15886 stmt = begin_while_stmt ();
15887 tmp = RECUR (WHILE_COND (t));
15888 finish_while_stmt_cond (tmp, stmt, false);
15889 RECUR (WHILE_BODY (t));
15890 finish_while_stmt (stmt);
15891 break;
15892
15893 case DO_STMT:
15894 stmt = begin_do_stmt ();
15895 RECUR (DO_BODY (t));
15896 finish_do_body (stmt);
15897 tmp = RECUR (DO_COND (t));
15898 finish_do_stmt (tmp, stmt, false);
15899 break;
15900
15901 case IF_STMT:
15902 stmt = begin_if_stmt ();
15903 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15904 tmp = RECUR (IF_COND (t));
15905 tmp = finish_if_stmt_cond (tmp, stmt);
15906 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15907 /* Don't instantiate the THEN_CLAUSE. */;
15908 else
15909 {
15910 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15911 if (inhibit)
15912 ++c_inhibit_evaluation_warnings;
15913 RECUR (THEN_CLAUSE (t));
15914 if (inhibit)
15915 --c_inhibit_evaluation_warnings;
15916 }
15917 finish_then_clause (stmt);
15918
15919 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15920 /* Don't instantiate the ELSE_CLAUSE. */;
15921 else if (ELSE_CLAUSE (t))
15922 {
15923 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15924 begin_else_clause (stmt);
15925 if (inhibit)
15926 ++c_inhibit_evaluation_warnings;
15927 RECUR (ELSE_CLAUSE (t));
15928 if (inhibit)
15929 --c_inhibit_evaluation_warnings;
15930 finish_else_clause (stmt);
15931 }
15932
15933 finish_if_stmt (stmt);
15934 break;
15935
15936 case BIND_EXPR:
15937 if (BIND_EXPR_BODY_BLOCK (t))
15938 stmt = begin_function_body ();
15939 else
15940 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15941 ? BCS_TRY_BLOCK : 0);
15942
15943 RECUR (BIND_EXPR_BODY (t));
15944
15945 if (BIND_EXPR_BODY_BLOCK (t))
15946 finish_function_body (stmt);
15947 else
15948 finish_compound_stmt (stmt);
15949 break;
15950
15951 case BREAK_STMT:
15952 finish_break_stmt ();
15953 break;
15954
15955 case CONTINUE_STMT:
15956 finish_continue_stmt ();
15957 break;
15958
15959 case SWITCH_STMT:
15960 stmt = begin_switch_stmt ();
15961 tmp = RECUR (SWITCH_STMT_COND (t));
15962 finish_switch_cond (tmp, stmt);
15963 RECUR (SWITCH_STMT_BODY (t));
15964 finish_switch_stmt (stmt);
15965 break;
15966
15967 case CASE_LABEL_EXPR:
15968 {
15969 tree low = RECUR (CASE_LOW (t));
15970 tree high = RECUR (CASE_HIGH (t));
15971 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
15972 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
15973 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
15974 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
15975 }
15976 break;
15977
15978 case LABEL_EXPR:
15979 {
15980 tree decl = LABEL_EXPR_LABEL (t);
15981 tree label;
15982
15983 label = finish_label_stmt (DECL_NAME (decl));
15984 if (TREE_CODE (label) == LABEL_DECL)
15985 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
15986 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15987 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15988 }
15989 break;
15990
15991 case GOTO_EXPR:
15992 tmp = GOTO_DESTINATION (t);
15993 if (TREE_CODE (tmp) != LABEL_DECL)
15994 /* Computed goto's must be tsubst'd into. On the other hand,
15995 non-computed gotos must not be; the identifier in question
15996 will have no binding. */
15997 tmp = RECUR (tmp);
15998 else
15999 tmp = DECL_NAME (tmp);
16000 finish_goto_stmt (tmp);
16001 break;
16002
16003 case ASM_EXPR:
16004 {
16005 tree string = RECUR (ASM_STRING (t));
16006 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16007 complain, in_decl);
16008 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16009 complain, in_decl);
16010 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16011 complain, in_decl);
16012 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16013 complain, in_decl);
16014 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16015 clobbers, labels);
16016 tree asm_expr = tmp;
16017 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16018 asm_expr = TREE_OPERAND (asm_expr, 0);
16019 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16020 }
16021 break;
16022
16023 case TRY_BLOCK:
16024 if (CLEANUP_P (t))
16025 {
16026 stmt = begin_try_block ();
16027 RECUR (TRY_STMTS (t));
16028 finish_cleanup_try_block (stmt);
16029 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16030 }
16031 else
16032 {
16033 tree compound_stmt = NULL_TREE;
16034
16035 if (FN_TRY_BLOCK_P (t))
16036 stmt = begin_function_try_block (&compound_stmt);
16037 else
16038 stmt = begin_try_block ();
16039
16040 RECUR (TRY_STMTS (t));
16041
16042 if (FN_TRY_BLOCK_P (t))
16043 finish_function_try_block (stmt);
16044 else
16045 finish_try_block (stmt);
16046
16047 RECUR (TRY_HANDLERS (t));
16048 if (FN_TRY_BLOCK_P (t))
16049 finish_function_handler_sequence (stmt, compound_stmt);
16050 else
16051 finish_handler_sequence (stmt);
16052 }
16053 break;
16054
16055 case HANDLER:
16056 {
16057 tree decl = HANDLER_PARMS (t);
16058
16059 if (decl)
16060 {
16061 decl = tsubst (decl, args, complain, in_decl);
16062 /* Prevent instantiate_decl from trying to instantiate
16063 this variable. We've already done all that needs to be
16064 done. */
16065 if (decl != error_mark_node)
16066 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16067 }
16068 stmt = begin_handler ();
16069 finish_handler_parms (decl, stmt);
16070 RECUR (HANDLER_BODY (t));
16071 finish_handler (stmt);
16072 }
16073 break;
16074
16075 case TAG_DEFN:
16076 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16077 if (CLASS_TYPE_P (tmp))
16078 {
16079 /* Local classes are not independent templates; they are
16080 instantiated along with their containing function. And this
16081 way we don't have to deal with pushing out of one local class
16082 to instantiate a member of another local class. */
16083 tree fn;
16084 /* Closures are handled by the LAMBDA_EXPR. */
16085 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16086 complete_type (tmp);
16087 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16088 if (!DECL_ARTIFICIAL (fn))
16089 instantiate_decl (fn, /*defer_ok=*/false,
16090 /*expl_inst_class=*/false);
16091 }
16092 break;
16093
16094 case STATIC_ASSERT:
16095 {
16096 tree condition;
16097
16098 ++c_inhibit_evaluation_warnings;
16099 condition =
16100 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16101 args,
16102 complain, in_decl,
16103 /*integral_constant_expression_p=*/true);
16104 --c_inhibit_evaluation_warnings;
16105
16106 finish_static_assert (condition,
16107 STATIC_ASSERT_MESSAGE (t),
16108 STATIC_ASSERT_SOURCE_LOCATION (t),
16109 /*member_p=*/false);
16110 }
16111 break;
16112
16113 case OACC_KERNELS:
16114 case OACC_PARALLEL:
16115 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16116 in_decl);
16117 stmt = begin_omp_parallel ();
16118 RECUR (OMP_BODY (t));
16119 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16120 break;
16121
16122 case OMP_PARALLEL:
16123 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16124 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16125 complain, in_decl);
16126 if (OMP_PARALLEL_COMBINED (t))
16127 omp_parallel_combined_clauses = &tmp;
16128 stmt = begin_omp_parallel ();
16129 RECUR (OMP_PARALLEL_BODY (t));
16130 gcc_assert (omp_parallel_combined_clauses == NULL);
16131 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16132 = OMP_PARALLEL_COMBINED (t);
16133 pop_omp_privatization_clauses (r);
16134 break;
16135
16136 case OMP_TASK:
16137 r = push_omp_privatization_clauses (false);
16138 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16139 complain, in_decl);
16140 stmt = begin_omp_task ();
16141 RECUR (OMP_TASK_BODY (t));
16142 finish_omp_task (tmp, stmt);
16143 pop_omp_privatization_clauses (r);
16144 break;
16145
16146 case OMP_FOR:
16147 case OMP_SIMD:
16148 case CILK_SIMD:
16149 case CILK_FOR:
16150 case OMP_DISTRIBUTE:
16151 case OMP_TASKLOOP:
16152 case OACC_LOOP:
16153 {
16154 tree clauses, body, pre_body;
16155 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16156 tree orig_declv = NULL_TREE;
16157 tree incrv = NULL_TREE;
16158 enum c_omp_region_type ort = C_ORT_OMP;
16159 int i;
16160
16161 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16162 ort = C_ORT_CILK;
16163 else if (TREE_CODE (t) == OACC_LOOP)
16164 ort = C_ORT_ACC;
16165
16166 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16167 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16168 in_decl);
16169 if (OMP_FOR_INIT (t) != NULL_TREE)
16170 {
16171 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16172 if (OMP_FOR_ORIG_DECLS (t))
16173 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16174 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16175 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16176 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16177 }
16178
16179 stmt = begin_omp_structured_block ();
16180
16181 pre_body = push_stmt_list ();
16182 RECUR (OMP_FOR_PRE_BODY (t));
16183 pre_body = pop_stmt_list (pre_body);
16184
16185 if (OMP_FOR_INIT (t) != NULL_TREE)
16186 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16187 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16188 incrv, &clauses, args, complain, in_decl,
16189 integral_constant_expression_p);
16190 omp_parallel_combined_clauses = NULL;
16191
16192 body = push_stmt_list ();
16193 RECUR (OMP_FOR_BODY (t));
16194 body = pop_stmt_list (body);
16195
16196 if (OMP_FOR_INIT (t) != NULL_TREE)
16197 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16198 orig_declv, initv, condv, incrv, body, pre_body,
16199 NULL, clauses);
16200 else
16201 {
16202 t = make_node (TREE_CODE (t));
16203 TREE_TYPE (t) = void_type_node;
16204 OMP_FOR_BODY (t) = body;
16205 OMP_FOR_PRE_BODY (t) = pre_body;
16206 OMP_FOR_CLAUSES (t) = clauses;
16207 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16208 add_stmt (t);
16209 }
16210
16211 add_stmt (finish_omp_structured_block (stmt));
16212 pop_omp_privatization_clauses (r);
16213 }
16214 break;
16215
16216 case OMP_SECTIONS:
16217 omp_parallel_combined_clauses = NULL;
16218 /* FALLTHRU */
16219 case OMP_SINGLE:
16220 case OMP_TEAMS:
16221 case OMP_CRITICAL:
16222 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16223 && OMP_TEAMS_COMBINED (t));
16224 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16225 in_decl);
16226 stmt = push_stmt_list ();
16227 RECUR (OMP_BODY (t));
16228 stmt = pop_stmt_list (stmt);
16229
16230 t = copy_node (t);
16231 OMP_BODY (t) = stmt;
16232 OMP_CLAUSES (t) = tmp;
16233 add_stmt (t);
16234 pop_omp_privatization_clauses (r);
16235 break;
16236
16237 case OACC_DATA:
16238 case OMP_TARGET_DATA:
16239 case OMP_TARGET:
16240 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16241 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16242 in_decl);
16243 keep_next_level (true);
16244 stmt = begin_omp_structured_block ();
16245
16246 RECUR (OMP_BODY (t));
16247 stmt = finish_omp_structured_block (stmt);
16248
16249 t = copy_node (t);
16250 OMP_BODY (t) = stmt;
16251 OMP_CLAUSES (t) = tmp;
16252 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16253 {
16254 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16255 if (teams)
16256 {
16257 /* For combined target teams, ensure the num_teams and
16258 thread_limit clause expressions are evaluated on the host,
16259 before entering the target construct. */
16260 tree c;
16261 for (c = OMP_TEAMS_CLAUSES (teams);
16262 c; c = OMP_CLAUSE_CHAIN (c))
16263 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16264 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16265 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16266 {
16267 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16268 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16269 if (expr == error_mark_node)
16270 continue;
16271 tmp = TARGET_EXPR_SLOT (expr);
16272 add_stmt (expr);
16273 OMP_CLAUSE_OPERAND (c, 0) = expr;
16274 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16275 OMP_CLAUSE_FIRSTPRIVATE);
16276 OMP_CLAUSE_DECL (tc) = tmp;
16277 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16278 OMP_TARGET_CLAUSES (t) = tc;
16279 }
16280 }
16281 }
16282 add_stmt (t);
16283 break;
16284
16285 case OACC_DECLARE:
16286 t = copy_node (t);
16287 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16288 complain, in_decl);
16289 OACC_DECLARE_CLAUSES (t) = tmp;
16290 add_stmt (t);
16291 break;
16292
16293 case OMP_TARGET_UPDATE:
16294 case OMP_TARGET_ENTER_DATA:
16295 case OMP_TARGET_EXIT_DATA:
16296 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16297 complain, in_decl);
16298 t = copy_node (t);
16299 OMP_STANDALONE_CLAUSES (t) = tmp;
16300 add_stmt (t);
16301 break;
16302
16303 case OACC_ENTER_DATA:
16304 case OACC_EXIT_DATA:
16305 case OACC_UPDATE:
16306 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16307 complain, in_decl);
16308 t = copy_node (t);
16309 OMP_STANDALONE_CLAUSES (t) = tmp;
16310 add_stmt (t);
16311 break;
16312
16313 case OMP_ORDERED:
16314 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16315 complain, in_decl);
16316 stmt = push_stmt_list ();
16317 RECUR (OMP_BODY (t));
16318 stmt = pop_stmt_list (stmt);
16319
16320 t = copy_node (t);
16321 OMP_BODY (t) = stmt;
16322 OMP_ORDERED_CLAUSES (t) = tmp;
16323 add_stmt (t);
16324 break;
16325
16326 case OMP_SECTION:
16327 case OMP_MASTER:
16328 case OMP_TASKGROUP:
16329 stmt = push_stmt_list ();
16330 RECUR (OMP_BODY (t));
16331 stmt = pop_stmt_list (stmt);
16332
16333 t = copy_node (t);
16334 OMP_BODY (t) = stmt;
16335 add_stmt (t);
16336 break;
16337
16338 case OMP_ATOMIC:
16339 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16340 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16341 {
16342 tree op1 = TREE_OPERAND (t, 1);
16343 tree rhs1 = NULL_TREE;
16344 tree lhs, rhs;
16345 if (TREE_CODE (op1) == COMPOUND_EXPR)
16346 {
16347 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16348 op1 = TREE_OPERAND (op1, 1);
16349 }
16350 lhs = RECUR (TREE_OPERAND (op1, 0));
16351 rhs = RECUR (TREE_OPERAND (op1, 1));
16352 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16353 NULL_TREE, NULL_TREE, rhs1,
16354 OMP_ATOMIC_SEQ_CST (t));
16355 }
16356 else
16357 {
16358 tree op1 = TREE_OPERAND (t, 1);
16359 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16360 tree rhs1 = NULL_TREE;
16361 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16362 enum tree_code opcode = NOP_EXPR;
16363 if (code == OMP_ATOMIC_READ)
16364 {
16365 v = RECUR (TREE_OPERAND (op1, 0));
16366 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16367 }
16368 else if (code == OMP_ATOMIC_CAPTURE_OLD
16369 || code == OMP_ATOMIC_CAPTURE_NEW)
16370 {
16371 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16372 v = RECUR (TREE_OPERAND (op1, 0));
16373 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16374 if (TREE_CODE (op11) == COMPOUND_EXPR)
16375 {
16376 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16377 op11 = TREE_OPERAND (op11, 1);
16378 }
16379 lhs = RECUR (TREE_OPERAND (op11, 0));
16380 rhs = RECUR (TREE_OPERAND (op11, 1));
16381 opcode = TREE_CODE (op11);
16382 if (opcode == MODIFY_EXPR)
16383 opcode = NOP_EXPR;
16384 }
16385 else
16386 {
16387 code = OMP_ATOMIC;
16388 lhs = RECUR (TREE_OPERAND (op1, 0));
16389 rhs = RECUR (TREE_OPERAND (op1, 1));
16390 }
16391 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16392 OMP_ATOMIC_SEQ_CST (t));
16393 }
16394 break;
16395
16396 case TRANSACTION_EXPR:
16397 {
16398 int flags = 0;
16399 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16400 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16401
16402 if (TRANSACTION_EXPR_IS_STMT (t))
16403 {
16404 tree body = TRANSACTION_EXPR_BODY (t);
16405 tree noex = NULL_TREE;
16406 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16407 {
16408 noex = MUST_NOT_THROW_COND (body);
16409 if (noex == NULL_TREE)
16410 noex = boolean_true_node;
16411 body = TREE_OPERAND (body, 0);
16412 }
16413 stmt = begin_transaction_stmt (input_location, NULL, flags);
16414 RECUR (body);
16415 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16416 }
16417 else
16418 {
16419 stmt = build_transaction_expr (EXPR_LOCATION (t),
16420 RECUR (TRANSACTION_EXPR_BODY (t)),
16421 flags, NULL_TREE);
16422 RETURN (stmt);
16423 }
16424 }
16425 break;
16426
16427 case MUST_NOT_THROW_EXPR:
16428 {
16429 tree op0 = RECUR (TREE_OPERAND (t, 0));
16430 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16431 RETURN (build_must_not_throw_expr (op0, cond));
16432 }
16433
16434 case EXPR_PACK_EXPANSION:
16435 error ("invalid use of pack expansion expression");
16436 RETURN (error_mark_node);
16437
16438 case NONTYPE_ARGUMENT_PACK:
16439 error ("use %<...%> to expand argument pack");
16440 RETURN (error_mark_node);
16441
16442 case CILK_SPAWN_STMT:
16443 cfun->calls_cilk_spawn = 1;
16444 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16445
16446 case CILK_SYNC_STMT:
16447 RETURN (build_cilk_sync ());
16448
16449 case COMPOUND_EXPR:
16450 tmp = RECUR (TREE_OPERAND (t, 0));
16451 if (tmp == NULL_TREE)
16452 /* If the first operand was a statement, we're done with it. */
16453 RETURN (RECUR (TREE_OPERAND (t, 1)));
16454 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16455 RECUR (TREE_OPERAND (t, 1)),
16456 complain));
16457
16458 case ANNOTATE_EXPR:
16459 tmp = RECUR (TREE_OPERAND (t, 0));
16460 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16461 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16462
16463 default:
16464 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16465
16466 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16467 /*function_p=*/false,
16468 integral_constant_expression_p));
16469 }
16470
16471 RETURN (NULL_TREE);
16472 out:
16473 input_location = loc;
16474 return r;
16475 #undef RECUR
16476 #undef RETURN
16477 }
16478
16479 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16480 function. For description of the body see comment above
16481 cp_parser_omp_declare_reduction_exprs. */
16482
16483 static void
16484 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16485 {
16486 if (t == NULL_TREE || t == error_mark_node)
16487 return;
16488
16489 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16490
16491 tree_stmt_iterator tsi;
16492 int i;
16493 tree stmts[7];
16494 memset (stmts, 0, sizeof stmts);
16495 for (i = 0, tsi = tsi_start (t);
16496 i < 7 && !tsi_end_p (tsi);
16497 i++, tsi_next (&tsi))
16498 stmts[i] = tsi_stmt (tsi);
16499 gcc_assert (tsi_end_p (tsi));
16500
16501 if (i >= 3)
16502 {
16503 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16504 && TREE_CODE (stmts[1]) == DECL_EXPR);
16505 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16506 args, complain, in_decl);
16507 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16508 args, complain, in_decl);
16509 DECL_CONTEXT (omp_out) = current_function_decl;
16510 DECL_CONTEXT (omp_in) = current_function_decl;
16511 keep_next_level (true);
16512 tree block = begin_omp_structured_block ();
16513 tsubst_expr (stmts[2], args, complain, in_decl, false);
16514 block = finish_omp_structured_block (block);
16515 block = maybe_cleanup_point_expr_void (block);
16516 add_decl_expr (omp_out);
16517 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16518 TREE_NO_WARNING (omp_out) = 1;
16519 add_decl_expr (omp_in);
16520 finish_expr_stmt (block);
16521 }
16522 if (i >= 6)
16523 {
16524 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16525 && TREE_CODE (stmts[4]) == DECL_EXPR);
16526 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16527 args, complain, in_decl);
16528 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16529 args, complain, in_decl);
16530 DECL_CONTEXT (omp_priv) = current_function_decl;
16531 DECL_CONTEXT (omp_orig) = current_function_decl;
16532 keep_next_level (true);
16533 tree block = begin_omp_structured_block ();
16534 tsubst_expr (stmts[5], args, complain, in_decl, false);
16535 block = finish_omp_structured_block (block);
16536 block = maybe_cleanup_point_expr_void (block);
16537 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16538 add_decl_expr (omp_priv);
16539 add_decl_expr (omp_orig);
16540 finish_expr_stmt (block);
16541 if (i == 7)
16542 add_decl_expr (omp_orig);
16543 }
16544 }
16545
16546 /* T is a postfix-expression that is not being used in a function
16547 call. Return the substituted version of T. */
16548
16549 static tree
16550 tsubst_non_call_postfix_expression (tree t, tree args,
16551 tsubst_flags_t complain,
16552 tree in_decl)
16553 {
16554 if (TREE_CODE (t) == SCOPE_REF)
16555 t = tsubst_qualified_id (t, args, complain, in_decl,
16556 /*done=*/false, /*address_p=*/false);
16557 else
16558 t = tsubst_copy_and_build (t, args, complain, in_decl,
16559 /*function_p=*/false,
16560 /*integral_constant_expression_p=*/false);
16561
16562 return t;
16563 }
16564
16565 /* Like tsubst but deals with expressions and performs semantic
16566 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16567
16568 tree
16569 tsubst_copy_and_build (tree t,
16570 tree args,
16571 tsubst_flags_t complain,
16572 tree in_decl,
16573 bool function_p,
16574 bool integral_constant_expression_p)
16575 {
16576 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16577 #define RECUR(NODE) \
16578 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16579 /*function_p=*/false, \
16580 integral_constant_expression_p)
16581
16582 tree retval, op1;
16583 location_t loc;
16584
16585 if (t == NULL_TREE || t == error_mark_node)
16586 return t;
16587
16588 loc = input_location;
16589 if (EXPR_HAS_LOCATION (t))
16590 input_location = EXPR_LOCATION (t);
16591
16592 /* N3276 decltype magic only applies to calls at the top level or on the
16593 right side of a comma. */
16594 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16595 complain &= ~tf_decltype;
16596
16597 switch (TREE_CODE (t))
16598 {
16599 case USING_DECL:
16600 t = DECL_NAME (t);
16601 /* Fall through. */
16602 case IDENTIFIER_NODE:
16603 {
16604 tree decl;
16605 cp_id_kind idk;
16606 bool non_integral_constant_expression_p;
16607 const char *error_msg;
16608
16609 if (IDENTIFIER_TYPENAME_P (t))
16610 {
16611 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16612 t = mangle_conv_op_name_for_type (new_type);
16613 }
16614
16615 /* Look up the name. */
16616 decl = lookup_name (t);
16617
16618 /* By convention, expressions use ERROR_MARK_NODE to indicate
16619 failure, not NULL_TREE. */
16620 if (decl == NULL_TREE)
16621 decl = error_mark_node;
16622
16623 decl = finish_id_expression (t, decl, NULL_TREE,
16624 &idk,
16625 integral_constant_expression_p,
16626 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16627 &non_integral_constant_expression_p,
16628 /*template_p=*/false,
16629 /*done=*/true,
16630 /*address_p=*/false,
16631 /*template_arg_p=*/false,
16632 &error_msg,
16633 input_location);
16634 if (error_msg)
16635 error (error_msg);
16636 if (!function_p && identifier_p (decl))
16637 {
16638 if (complain & tf_error)
16639 unqualified_name_lookup_error (decl);
16640 decl = error_mark_node;
16641 }
16642 RETURN (decl);
16643 }
16644
16645 case TEMPLATE_ID_EXPR:
16646 {
16647 tree object;
16648 tree templ = RECUR (TREE_OPERAND (t, 0));
16649 tree targs = TREE_OPERAND (t, 1);
16650
16651 if (targs)
16652 targs = tsubst_template_args (targs, args, complain, in_decl);
16653 if (targs == error_mark_node)
16654 return error_mark_node;
16655
16656 if (TREE_CODE (templ) == SCOPE_REF)
16657 {
16658 tree name = TREE_OPERAND (templ, 1);
16659 tree tid = lookup_template_function (name, targs);
16660 TREE_OPERAND (templ, 1) = tid;
16661 return templ;
16662 }
16663
16664 if (variable_template_p (templ))
16665 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16666
16667 if (TREE_CODE (templ) == COMPONENT_REF)
16668 {
16669 object = TREE_OPERAND (templ, 0);
16670 templ = TREE_OPERAND (templ, 1);
16671 }
16672 else
16673 object = NULL_TREE;
16674 templ = lookup_template_function (templ, targs);
16675
16676 if (object)
16677 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16678 object, templ, NULL_TREE));
16679 else
16680 RETURN (baselink_for_fns (templ));
16681 }
16682
16683 case INDIRECT_REF:
16684 {
16685 tree r = RECUR (TREE_OPERAND (t, 0));
16686
16687 if (REFERENCE_REF_P (t))
16688 {
16689 /* A type conversion to reference type will be enclosed in
16690 such an indirect ref, but the substitution of the cast
16691 will have also added such an indirect ref. */
16692 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16693 r = convert_from_reference (r);
16694 }
16695 else
16696 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16697 complain|decltype_flag);
16698
16699 if (TREE_CODE (r) == INDIRECT_REF)
16700 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16701
16702 RETURN (r);
16703 }
16704
16705 case NOP_EXPR:
16706 {
16707 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16708 tree op0 = RECUR (TREE_OPERAND (t, 0));
16709 RETURN (build_nop (type, op0));
16710 }
16711
16712 case IMPLICIT_CONV_EXPR:
16713 {
16714 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16715 tree expr = RECUR (TREE_OPERAND (t, 0));
16716 int flags = LOOKUP_IMPLICIT;
16717 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16718 flags = LOOKUP_NORMAL;
16719 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16720 flags));
16721 }
16722
16723 case CONVERT_EXPR:
16724 {
16725 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16726 tree op0 = RECUR (TREE_OPERAND (t, 0));
16727 RETURN (build1 (CONVERT_EXPR, type, op0));
16728 }
16729
16730 case CAST_EXPR:
16731 case REINTERPRET_CAST_EXPR:
16732 case CONST_CAST_EXPR:
16733 case DYNAMIC_CAST_EXPR:
16734 case STATIC_CAST_EXPR:
16735 {
16736 tree type;
16737 tree op, r = NULL_TREE;
16738
16739 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16740 if (integral_constant_expression_p
16741 && !cast_valid_in_integral_constant_expression_p (type))
16742 {
16743 if (complain & tf_error)
16744 error ("a cast to a type other than an integral or "
16745 "enumeration type cannot appear in a constant-expression");
16746 RETURN (error_mark_node);
16747 }
16748
16749 op = RECUR (TREE_OPERAND (t, 0));
16750
16751 warning_sentinel s(warn_useless_cast);
16752 switch (TREE_CODE (t))
16753 {
16754 case CAST_EXPR:
16755 r = build_functional_cast (type, op, complain);
16756 break;
16757 case REINTERPRET_CAST_EXPR:
16758 r = build_reinterpret_cast (type, op, complain);
16759 break;
16760 case CONST_CAST_EXPR:
16761 r = build_const_cast (type, op, complain);
16762 break;
16763 case DYNAMIC_CAST_EXPR:
16764 r = build_dynamic_cast (type, op, complain);
16765 break;
16766 case STATIC_CAST_EXPR:
16767 r = build_static_cast (type, op, complain);
16768 break;
16769 default:
16770 gcc_unreachable ();
16771 }
16772
16773 RETURN (r);
16774 }
16775
16776 case POSTDECREMENT_EXPR:
16777 case POSTINCREMENT_EXPR:
16778 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16779 args, complain, in_decl);
16780 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16781 complain|decltype_flag));
16782
16783 case PREDECREMENT_EXPR:
16784 case PREINCREMENT_EXPR:
16785 case NEGATE_EXPR:
16786 case BIT_NOT_EXPR:
16787 case ABS_EXPR:
16788 case TRUTH_NOT_EXPR:
16789 case UNARY_PLUS_EXPR: /* Unary + */
16790 case REALPART_EXPR:
16791 case IMAGPART_EXPR:
16792 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16793 RECUR (TREE_OPERAND (t, 0)),
16794 complain|decltype_flag));
16795
16796 case FIX_TRUNC_EXPR:
16797 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16798 false, complain));
16799
16800 case ADDR_EXPR:
16801 op1 = TREE_OPERAND (t, 0);
16802 if (TREE_CODE (op1) == LABEL_DECL)
16803 RETURN (finish_label_address_expr (DECL_NAME (op1),
16804 EXPR_LOCATION (op1)));
16805 if (TREE_CODE (op1) == SCOPE_REF)
16806 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16807 /*done=*/true, /*address_p=*/true);
16808 else
16809 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16810 in_decl);
16811 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16812 complain|decltype_flag));
16813
16814 case PLUS_EXPR:
16815 case MINUS_EXPR:
16816 case MULT_EXPR:
16817 case TRUNC_DIV_EXPR:
16818 case CEIL_DIV_EXPR:
16819 case FLOOR_DIV_EXPR:
16820 case ROUND_DIV_EXPR:
16821 case EXACT_DIV_EXPR:
16822 case BIT_AND_EXPR:
16823 case BIT_IOR_EXPR:
16824 case BIT_XOR_EXPR:
16825 case TRUNC_MOD_EXPR:
16826 case FLOOR_MOD_EXPR:
16827 case TRUTH_ANDIF_EXPR:
16828 case TRUTH_ORIF_EXPR:
16829 case TRUTH_AND_EXPR:
16830 case TRUTH_OR_EXPR:
16831 case RSHIFT_EXPR:
16832 case LSHIFT_EXPR:
16833 case RROTATE_EXPR:
16834 case LROTATE_EXPR:
16835 case EQ_EXPR:
16836 case NE_EXPR:
16837 case MAX_EXPR:
16838 case MIN_EXPR:
16839 case LE_EXPR:
16840 case GE_EXPR:
16841 case LT_EXPR:
16842 case GT_EXPR:
16843 case MEMBER_REF:
16844 case DOTSTAR_EXPR:
16845 {
16846 warning_sentinel s1(warn_type_limits);
16847 warning_sentinel s2(warn_div_by_zero);
16848 warning_sentinel s3(warn_logical_op);
16849 warning_sentinel s4(warn_tautological_compare);
16850 tree op0 = RECUR (TREE_OPERAND (t, 0));
16851 tree op1 = RECUR (TREE_OPERAND (t, 1));
16852 tree r = build_x_binary_op
16853 (input_location, TREE_CODE (t),
16854 op0,
16855 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16856 ? ERROR_MARK
16857 : TREE_CODE (TREE_OPERAND (t, 0))),
16858 op1,
16859 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16860 ? ERROR_MARK
16861 : TREE_CODE (TREE_OPERAND (t, 1))),
16862 /*overload=*/NULL,
16863 complain|decltype_flag);
16864 if (EXPR_P (r) && TREE_NO_WARNING (t))
16865 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16866
16867 RETURN (r);
16868 }
16869
16870 case POINTER_PLUS_EXPR:
16871 {
16872 tree op0 = RECUR (TREE_OPERAND (t, 0));
16873 tree op1 = RECUR (TREE_OPERAND (t, 1));
16874 return fold_build_pointer_plus (op0, op1);
16875 }
16876
16877 case SCOPE_REF:
16878 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16879 /*address_p=*/false));
16880 case ARRAY_REF:
16881 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16882 args, complain, in_decl);
16883 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16884 RECUR (TREE_OPERAND (t, 1)),
16885 complain|decltype_flag));
16886
16887 case ARRAY_NOTATION_REF:
16888 {
16889 tree start_index, length, stride;
16890 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16891 args, complain, in_decl);
16892 start_index = RECUR (ARRAY_NOTATION_START (t));
16893 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16894 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16895 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16896 length, stride, TREE_TYPE (op1)));
16897 }
16898 case SIZEOF_EXPR:
16899 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16900 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16901 RETURN (tsubst_copy (t, args, complain, in_decl));
16902 /* Fall through */
16903
16904 case ALIGNOF_EXPR:
16905 {
16906 tree r;
16907
16908 op1 = TREE_OPERAND (t, 0);
16909 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16910 op1 = TREE_TYPE (op1);
16911 if (!args)
16912 {
16913 /* When there are no ARGS, we are trying to evaluate a
16914 non-dependent expression from the parser. Trying to do
16915 the substitutions may not work. */
16916 if (!TYPE_P (op1))
16917 op1 = TREE_TYPE (op1);
16918 }
16919 else
16920 {
16921 ++cp_unevaluated_operand;
16922 ++c_inhibit_evaluation_warnings;
16923 if (TYPE_P (op1))
16924 op1 = tsubst (op1, args, complain, in_decl);
16925 else
16926 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16927 /*function_p=*/false,
16928 /*integral_constant_expression_p=*/
16929 false);
16930 --cp_unevaluated_operand;
16931 --c_inhibit_evaluation_warnings;
16932 }
16933 if (TYPE_P (op1))
16934 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16935 complain & tf_error);
16936 else
16937 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16938 complain & tf_error);
16939 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16940 {
16941 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16942 {
16943 if (!processing_template_decl && TYPE_P (op1))
16944 {
16945 r = build_min (SIZEOF_EXPR, size_type_node,
16946 build1 (NOP_EXPR, op1, error_mark_node));
16947 SIZEOF_EXPR_TYPE_P (r) = 1;
16948 }
16949 else
16950 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16951 TREE_SIDE_EFFECTS (r) = 0;
16952 TREE_READONLY (r) = 1;
16953 }
16954 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16955 }
16956 RETURN (r);
16957 }
16958
16959 case AT_ENCODE_EXPR:
16960 {
16961 op1 = TREE_OPERAND (t, 0);
16962 ++cp_unevaluated_operand;
16963 ++c_inhibit_evaluation_warnings;
16964 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16965 /*function_p=*/false,
16966 /*integral_constant_expression_p=*/false);
16967 --cp_unevaluated_operand;
16968 --c_inhibit_evaluation_warnings;
16969 RETURN (objc_build_encode_expr (op1));
16970 }
16971
16972 case NOEXCEPT_EXPR:
16973 op1 = TREE_OPERAND (t, 0);
16974 ++cp_unevaluated_operand;
16975 ++c_inhibit_evaluation_warnings;
16976 ++cp_noexcept_operand;
16977 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16978 /*function_p=*/false,
16979 /*integral_constant_expression_p=*/false);
16980 --cp_unevaluated_operand;
16981 --c_inhibit_evaluation_warnings;
16982 --cp_noexcept_operand;
16983 RETURN (finish_noexcept_expr (op1, complain));
16984
16985 case MODOP_EXPR:
16986 {
16987 warning_sentinel s(warn_div_by_zero);
16988 tree lhs = RECUR (TREE_OPERAND (t, 0));
16989 tree rhs = RECUR (TREE_OPERAND (t, 2));
16990 tree r = build_x_modify_expr
16991 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16992 complain|decltype_flag);
16993 /* TREE_NO_WARNING must be set if either the expression was
16994 parenthesized or it uses an operator such as >>= rather
16995 than plain assignment. In the former case, it was already
16996 set and must be copied. In the latter case,
16997 build_x_modify_expr sets it and it must not be reset
16998 here. */
16999 if (TREE_NO_WARNING (t))
17000 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17001
17002 RETURN (r);
17003 }
17004
17005 case ARROW_EXPR:
17006 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17007 args, complain, in_decl);
17008 /* Remember that there was a reference to this entity. */
17009 if (DECL_P (op1)
17010 && !mark_used (op1, complain) && !(complain & tf_error))
17011 RETURN (error_mark_node);
17012 RETURN (build_x_arrow (input_location, op1, complain));
17013
17014 case NEW_EXPR:
17015 {
17016 tree placement = RECUR (TREE_OPERAND (t, 0));
17017 tree init = RECUR (TREE_OPERAND (t, 3));
17018 vec<tree, va_gc> *placement_vec;
17019 vec<tree, va_gc> *init_vec;
17020 tree ret;
17021
17022 if (placement == NULL_TREE)
17023 placement_vec = NULL;
17024 else
17025 {
17026 placement_vec = make_tree_vector ();
17027 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17028 vec_safe_push (placement_vec, TREE_VALUE (placement));
17029 }
17030
17031 /* If there was an initializer in the original tree, but it
17032 instantiated to an empty list, then we should pass a
17033 non-NULL empty vector to tell build_new that it was an
17034 empty initializer() rather than no initializer. This can
17035 only happen when the initializer is a pack expansion whose
17036 parameter packs are of length zero. */
17037 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17038 init_vec = NULL;
17039 else
17040 {
17041 init_vec = make_tree_vector ();
17042 if (init == void_node)
17043 gcc_assert (init_vec != NULL);
17044 else
17045 {
17046 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17047 vec_safe_push (init_vec, TREE_VALUE (init));
17048 }
17049 }
17050
17051 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17052 tree op2 = RECUR (TREE_OPERAND (t, 2));
17053 ret = build_new (&placement_vec, op1, op2, &init_vec,
17054 NEW_EXPR_USE_GLOBAL (t),
17055 complain);
17056
17057 if (placement_vec != NULL)
17058 release_tree_vector (placement_vec);
17059 if (init_vec != NULL)
17060 release_tree_vector (init_vec);
17061
17062 RETURN (ret);
17063 }
17064
17065 case DELETE_EXPR:
17066 {
17067 tree op0 = RECUR (TREE_OPERAND (t, 0));
17068 tree op1 = RECUR (TREE_OPERAND (t, 1));
17069 RETURN (delete_sanity (op0, op1,
17070 DELETE_EXPR_USE_VEC (t),
17071 DELETE_EXPR_USE_GLOBAL (t),
17072 complain));
17073 }
17074
17075 case COMPOUND_EXPR:
17076 {
17077 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17078 complain & ~tf_decltype, in_decl,
17079 /*function_p=*/false,
17080 integral_constant_expression_p);
17081 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17082 op0,
17083 RECUR (TREE_OPERAND (t, 1)),
17084 complain|decltype_flag));
17085 }
17086
17087 case CALL_EXPR:
17088 {
17089 tree function;
17090 vec<tree, va_gc> *call_args;
17091 unsigned int nargs, i;
17092 bool qualified_p;
17093 bool koenig_p;
17094 tree ret;
17095
17096 function = CALL_EXPR_FN (t);
17097 /* Internal function with no arguments. */
17098 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17099 RETURN (t);
17100
17101 /* When we parsed the expression, we determined whether or
17102 not Koenig lookup should be performed. */
17103 koenig_p = KOENIG_LOOKUP_P (t);
17104 if (function == NULL_TREE)
17105 {
17106 koenig_p = false;
17107 qualified_p = false;
17108 }
17109 else if (TREE_CODE (function) == SCOPE_REF)
17110 {
17111 qualified_p = true;
17112 function = tsubst_qualified_id (function, args, complain, in_decl,
17113 /*done=*/false,
17114 /*address_p=*/false);
17115 }
17116 else if (koenig_p && identifier_p (function))
17117 {
17118 /* Do nothing; calling tsubst_copy_and_build on an identifier
17119 would incorrectly perform unqualified lookup again.
17120
17121 Note that we can also have an IDENTIFIER_NODE if the earlier
17122 unqualified lookup found a member function; in that case
17123 koenig_p will be false and we do want to do the lookup
17124 again to find the instantiated member function.
17125
17126 FIXME but doing that causes c++/15272, so we need to stop
17127 using IDENTIFIER_NODE in that situation. */
17128 qualified_p = false;
17129 }
17130 else
17131 {
17132 if (TREE_CODE (function) == COMPONENT_REF)
17133 {
17134 tree op = TREE_OPERAND (function, 1);
17135
17136 qualified_p = (TREE_CODE (op) == SCOPE_REF
17137 || (BASELINK_P (op)
17138 && BASELINK_QUALIFIED_P (op)));
17139 }
17140 else
17141 qualified_p = false;
17142
17143 if (TREE_CODE (function) == ADDR_EXPR
17144 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17145 /* Avoid error about taking the address of a constructor. */
17146 function = TREE_OPERAND (function, 0);
17147
17148 function = tsubst_copy_and_build (function, args, complain,
17149 in_decl,
17150 !qualified_p,
17151 integral_constant_expression_p);
17152
17153 if (BASELINK_P (function))
17154 qualified_p = true;
17155 }
17156
17157 nargs = call_expr_nargs (t);
17158 call_args = make_tree_vector ();
17159 for (i = 0; i < nargs; ++i)
17160 {
17161 tree arg = CALL_EXPR_ARG (t, i);
17162
17163 if (!PACK_EXPANSION_P (arg))
17164 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17165 else
17166 {
17167 /* Expand the pack expansion and push each entry onto
17168 CALL_ARGS. */
17169 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17170 if (TREE_CODE (arg) == TREE_VEC)
17171 {
17172 unsigned int len, j;
17173
17174 len = TREE_VEC_LENGTH (arg);
17175 for (j = 0; j < len; ++j)
17176 {
17177 tree value = TREE_VEC_ELT (arg, j);
17178 if (value != NULL_TREE)
17179 value = convert_from_reference (value);
17180 vec_safe_push (call_args, value);
17181 }
17182 }
17183 else
17184 {
17185 /* A partial substitution. Add one entry. */
17186 vec_safe_push (call_args, arg);
17187 }
17188 }
17189 }
17190
17191 /* We do not perform argument-dependent lookup if normal
17192 lookup finds a non-function, in accordance with the
17193 expected resolution of DR 218. */
17194 if (koenig_p
17195 && ((is_overloaded_fn (function)
17196 /* If lookup found a member function, the Koenig lookup is
17197 not appropriate, even if an unqualified-name was used
17198 to denote the function. */
17199 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17200 || identifier_p (function))
17201 /* Only do this when substitution turns a dependent call
17202 into a non-dependent call. */
17203 && type_dependent_expression_p_push (t)
17204 && !any_type_dependent_arguments_p (call_args))
17205 function = perform_koenig_lookup (function, call_args, tf_none);
17206
17207 if (function != NULL_TREE
17208 && identifier_p (function)
17209 && !any_type_dependent_arguments_p (call_args))
17210 {
17211 if (koenig_p && (complain & tf_warning_or_error))
17212 {
17213 /* For backwards compatibility and good diagnostics, try
17214 the unqualified lookup again if we aren't in SFINAE
17215 context. */
17216 tree unq = (tsubst_copy_and_build
17217 (function, args, complain, in_decl, true,
17218 integral_constant_expression_p));
17219 if (unq == error_mark_node)
17220 {
17221 release_tree_vector (call_args);
17222 RETURN (error_mark_node);
17223 }
17224
17225 if (unq != function)
17226 {
17227 /* In a lambda fn, we have to be careful to not
17228 introduce new this captures. Legacy code can't
17229 be using lambdas anyway, so it's ok to be
17230 stricter. */
17231 bool in_lambda = (current_class_type
17232 && LAMBDA_TYPE_P (current_class_type));
17233 char const *const msg
17234 = G_("%qD was not declared in this scope, "
17235 "and no declarations were found by "
17236 "argument-dependent lookup at the point "
17237 "of instantiation");
17238
17239 bool diag = true;
17240 if (in_lambda)
17241 error_at (EXPR_LOC_OR_LOC (t, input_location),
17242 msg, function);
17243 else
17244 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17245 msg, function);
17246 if (diag)
17247 {
17248 tree fn = unq;
17249 if (INDIRECT_REF_P (fn))
17250 fn = TREE_OPERAND (fn, 0);
17251 if (TREE_CODE (fn) == COMPONENT_REF)
17252 fn = TREE_OPERAND (fn, 1);
17253 if (is_overloaded_fn (fn))
17254 fn = get_first_fn (fn);
17255
17256 if (!DECL_P (fn))
17257 /* Can't say anything more. */;
17258 else if (DECL_CLASS_SCOPE_P (fn))
17259 {
17260 location_t loc = EXPR_LOC_OR_LOC (t,
17261 input_location);
17262 inform (loc,
17263 "declarations in dependent base %qT are "
17264 "not found by unqualified lookup",
17265 DECL_CLASS_CONTEXT (fn));
17266 if (current_class_ptr)
17267 inform (loc,
17268 "use %<this->%D%> instead", function);
17269 else
17270 inform (loc,
17271 "use %<%T::%D%> instead",
17272 current_class_name, function);
17273 }
17274 else
17275 inform (DECL_SOURCE_LOCATION (fn),
17276 "%qD declared here, later in the "
17277 "translation unit", fn);
17278 if (in_lambda)
17279 {
17280 release_tree_vector (call_args);
17281 RETURN (error_mark_node);
17282 }
17283 }
17284
17285 function = unq;
17286 }
17287 }
17288 if (identifier_p (function))
17289 {
17290 if (complain & tf_error)
17291 unqualified_name_lookup_error (function);
17292 release_tree_vector (call_args);
17293 RETURN (error_mark_node);
17294 }
17295 }
17296
17297 /* Remember that there was a reference to this entity. */
17298 if (function != NULL_TREE
17299 && DECL_P (function)
17300 && !mark_used (function, complain) && !(complain & tf_error))
17301 {
17302 release_tree_vector (call_args);
17303 RETURN (error_mark_node);
17304 }
17305
17306 /* Put back tf_decltype for the actual call. */
17307 complain |= decltype_flag;
17308
17309 if (function == NULL_TREE)
17310 switch (CALL_EXPR_IFN (t))
17311 {
17312 case IFN_LAUNDER:
17313 gcc_assert (nargs == 1);
17314 if (vec_safe_length (call_args) != 1)
17315 {
17316 error_at (EXPR_LOC_OR_LOC (t, input_location),
17317 "wrong number of arguments to "
17318 "%<__builtin_launder%>");
17319 ret = error_mark_node;
17320 }
17321 else
17322 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17323 input_location),
17324 (*call_args)[0], complain);
17325 break;
17326
17327 default:
17328 /* Unsupported internal function with arguments. */
17329 gcc_unreachable ();
17330 }
17331 else if (TREE_CODE (function) == OFFSET_REF)
17332 ret = build_offset_ref_call_from_tree (function, &call_args,
17333 complain);
17334 else if (TREE_CODE (function) == COMPONENT_REF)
17335 {
17336 tree instance = TREE_OPERAND (function, 0);
17337 tree fn = TREE_OPERAND (function, 1);
17338
17339 if (processing_template_decl
17340 && (type_dependent_expression_p (instance)
17341 || (!BASELINK_P (fn)
17342 && TREE_CODE (fn) != FIELD_DECL)
17343 || type_dependent_expression_p (fn)
17344 || any_type_dependent_arguments_p (call_args)))
17345 ret = build_nt_call_vec (function, call_args);
17346 else if (!BASELINK_P (fn))
17347 ret = finish_call_expr (function, &call_args,
17348 /*disallow_virtual=*/false,
17349 /*koenig_p=*/false,
17350 complain);
17351 else
17352 ret = (build_new_method_call
17353 (instance, fn,
17354 &call_args, NULL_TREE,
17355 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17356 /*fn_p=*/NULL,
17357 complain));
17358 }
17359 else
17360 ret = finish_call_expr (function, &call_args,
17361 /*disallow_virtual=*/qualified_p,
17362 koenig_p,
17363 complain);
17364
17365 release_tree_vector (call_args);
17366
17367 if (ret != error_mark_node)
17368 {
17369 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17370 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17371 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17372 bool thk = CALL_FROM_THUNK_P (t);
17373 if (op || ord || rev || thk)
17374 {
17375 function = extract_call_expr (ret);
17376 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17377 CALL_EXPR_ORDERED_ARGS (function) = ord;
17378 CALL_EXPR_REVERSE_ARGS (function) = rev;
17379 if (thk)
17380 {
17381 CALL_FROM_THUNK_P (function) = true;
17382 /* The thunk location is not interesting. */
17383 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17384 }
17385 }
17386 }
17387
17388 RETURN (ret);
17389 }
17390
17391 case COND_EXPR:
17392 {
17393 tree cond = RECUR (TREE_OPERAND (t, 0));
17394 tree folded_cond = fold_non_dependent_expr (cond);
17395 tree exp1, exp2;
17396
17397 if (TREE_CODE (folded_cond) == INTEGER_CST)
17398 {
17399 if (integer_zerop (folded_cond))
17400 {
17401 ++c_inhibit_evaluation_warnings;
17402 exp1 = RECUR (TREE_OPERAND (t, 1));
17403 --c_inhibit_evaluation_warnings;
17404 exp2 = RECUR (TREE_OPERAND (t, 2));
17405 }
17406 else
17407 {
17408 exp1 = RECUR (TREE_OPERAND (t, 1));
17409 ++c_inhibit_evaluation_warnings;
17410 exp2 = RECUR (TREE_OPERAND (t, 2));
17411 --c_inhibit_evaluation_warnings;
17412 }
17413 cond = folded_cond;
17414 }
17415 else
17416 {
17417 exp1 = RECUR (TREE_OPERAND (t, 1));
17418 exp2 = RECUR (TREE_OPERAND (t, 2));
17419 }
17420
17421 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17422 cond, exp1, exp2, complain));
17423 }
17424
17425 case PSEUDO_DTOR_EXPR:
17426 {
17427 tree op0 = RECUR (TREE_OPERAND (t, 0));
17428 tree op1 = RECUR (TREE_OPERAND (t, 1));
17429 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17430 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17431 input_location));
17432 }
17433
17434 case TREE_LIST:
17435 {
17436 tree purpose, value, chain;
17437
17438 if (t == void_list_node)
17439 RETURN (t);
17440
17441 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17442 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17443 {
17444 /* We have pack expansions, so expand those and
17445 create a new list out of it. */
17446 tree purposevec = NULL_TREE;
17447 tree valuevec = NULL_TREE;
17448 tree chain;
17449 int i, len = -1;
17450
17451 /* Expand the argument expressions. */
17452 if (TREE_PURPOSE (t))
17453 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17454 complain, in_decl);
17455 if (TREE_VALUE (t))
17456 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17457 complain, in_decl);
17458
17459 /* Build the rest of the list. */
17460 chain = TREE_CHAIN (t);
17461 if (chain && chain != void_type_node)
17462 chain = RECUR (chain);
17463
17464 /* Determine the number of arguments. */
17465 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17466 {
17467 len = TREE_VEC_LENGTH (purposevec);
17468 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17469 }
17470 else if (TREE_CODE (valuevec) == TREE_VEC)
17471 len = TREE_VEC_LENGTH (valuevec);
17472 else
17473 {
17474 /* Since we only performed a partial substitution into
17475 the argument pack, we only RETURN (a single list
17476 node. */
17477 if (purposevec == TREE_PURPOSE (t)
17478 && valuevec == TREE_VALUE (t)
17479 && chain == TREE_CHAIN (t))
17480 RETURN (t);
17481
17482 RETURN (tree_cons (purposevec, valuevec, chain));
17483 }
17484
17485 /* Convert the argument vectors into a TREE_LIST */
17486 i = len;
17487 while (i > 0)
17488 {
17489 /* Grab the Ith values. */
17490 i--;
17491 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17492 : NULL_TREE;
17493 value
17494 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17495 : NULL_TREE;
17496
17497 /* Build the list (backwards). */
17498 chain = tree_cons (purpose, value, chain);
17499 }
17500
17501 RETURN (chain);
17502 }
17503
17504 purpose = TREE_PURPOSE (t);
17505 if (purpose)
17506 purpose = RECUR (purpose);
17507 value = TREE_VALUE (t);
17508 if (value)
17509 value = RECUR (value);
17510 chain = TREE_CHAIN (t);
17511 if (chain && chain != void_type_node)
17512 chain = RECUR (chain);
17513 if (purpose == TREE_PURPOSE (t)
17514 && value == TREE_VALUE (t)
17515 && chain == TREE_CHAIN (t))
17516 RETURN (t);
17517 RETURN (tree_cons (purpose, value, chain));
17518 }
17519
17520 case COMPONENT_REF:
17521 {
17522 tree object;
17523 tree object_type;
17524 tree member;
17525 tree r;
17526
17527 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17528 args, complain, in_decl);
17529 /* Remember that there was a reference to this entity. */
17530 if (DECL_P (object)
17531 && !mark_used (object, complain) && !(complain & tf_error))
17532 RETURN (error_mark_node);
17533 object_type = TREE_TYPE (object);
17534
17535 member = TREE_OPERAND (t, 1);
17536 if (BASELINK_P (member))
17537 member = tsubst_baselink (member,
17538 non_reference (TREE_TYPE (object)),
17539 args, complain, in_decl);
17540 else
17541 member = tsubst_copy (member, args, complain, in_decl);
17542 if (member == error_mark_node)
17543 RETURN (error_mark_node);
17544
17545 if (TREE_CODE (member) == FIELD_DECL)
17546 {
17547 r = finish_non_static_data_member (member, object, NULL_TREE);
17548 if (TREE_CODE (r) == COMPONENT_REF)
17549 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17550 RETURN (r);
17551 }
17552 else if (type_dependent_expression_p (object))
17553 /* We can't do much here. */;
17554 else if (!CLASS_TYPE_P (object_type))
17555 {
17556 if (scalarish_type_p (object_type))
17557 {
17558 tree s = NULL_TREE;
17559 tree dtor = member;
17560
17561 if (TREE_CODE (dtor) == SCOPE_REF)
17562 {
17563 s = TREE_OPERAND (dtor, 0);
17564 dtor = TREE_OPERAND (dtor, 1);
17565 }
17566 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17567 {
17568 dtor = TREE_OPERAND (dtor, 0);
17569 if (TYPE_P (dtor))
17570 RETURN (finish_pseudo_destructor_expr
17571 (object, s, dtor, input_location));
17572 }
17573 }
17574 }
17575 else if (TREE_CODE (member) == SCOPE_REF
17576 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17577 {
17578 /* Lookup the template functions now that we know what the
17579 scope is. */
17580 tree scope = TREE_OPERAND (member, 0);
17581 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17582 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17583 member = lookup_qualified_name (scope, tmpl,
17584 /*is_type_p=*/false,
17585 /*complain=*/false);
17586 if (BASELINK_P (member))
17587 {
17588 BASELINK_FUNCTIONS (member)
17589 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17590 args);
17591 member = (adjust_result_of_qualified_name_lookup
17592 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17593 object_type));
17594 }
17595 else
17596 {
17597 qualified_name_lookup_error (scope, tmpl, member,
17598 input_location);
17599 RETURN (error_mark_node);
17600 }
17601 }
17602 else if (TREE_CODE (member) == SCOPE_REF
17603 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17604 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17605 {
17606 if (complain & tf_error)
17607 {
17608 if (TYPE_P (TREE_OPERAND (member, 0)))
17609 error ("%qT is not a class or namespace",
17610 TREE_OPERAND (member, 0));
17611 else
17612 error ("%qD is not a class or namespace",
17613 TREE_OPERAND (member, 0));
17614 }
17615 RETURN (error_mark_node);
17616 }
17617
17618 r = finish_class_member_access_expr (object, member,
17619 /*template_p=*/false,
17620 complain);
17621 if (TREE_CODE (r) == COMPONENT_REF)
17622 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17623 RETURN (r);
17624 }
17625
17626 case THROW_EXPR:
17627 RETURN (build_throw
17628 (RECUR (TREE_OPERAND (t, 0))));
17629
17630 case CONSTRUCTOR:
17631 {
17632 vec<constructor_elt, va_gc> *n;
17633 constructor_elt *ce;
17634 unsigned HOST_WIDE_INT idx;
17635 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17636 bool process_index_p;
17637 int newlen;
17638 bool need_copy_p = false;
17639 tree r;
17640
17641 if (type == error_mark_node)
17642 RETURN (error_mark_node);
17643
17644 /* digest_init will do the wrong thing if we let it. */
17645 if (type && TYPE_PTRMEMFUNC_P (type))
17646 RETURN (t);
17647
17648 /* We do not want to process the index of aggregate
17649 initializers as they are identifier nodes which will be
17650 looked up by digest_init. */
17651 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17652
17653 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17654 newlen = vec_safe_length (n);
17655 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17656 {
17657 if (ce->index && process_index_p
17658 /* An identifier index is looked up in the type
17659 being initialized, not the current scope. */
17660 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17661 ce->index = RECUR (ce->index);
17662
17663 if (PACK_EXPANSION_P (ce->value))
17664 {
17665 /* Substitute into the pack expansion. */
17666 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17667 in_decl);
17668
17669 if (ce->value == error_mark_node
17670 || PACK_EXPANSION_P (ce->value))
17671 ;
17672 else if (TREE_VEC_LENGTH (ce->value) == 1)
17673 /* Just move the argument into place. */
17674 ce->value = TREE_VEC_ELT (ce->value, 0);
17675 else
17676 {
17677 /* Update the length of the final CONSTRUCTOR
17678 arguments vector, and note that we will need to
17679 copy.*/
17680 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17681 need_copy_p = true;
17682 }
17683 }
17684 else
17685 ce->value = RECUR (ce->value);
17686 }
17687
17688 if (need_copy_p)
17689 {
17690 vec<constructor_elt, va_gc> *old_n = n;
17691
17692 vec_alloc (n, newlen);
17693 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17694 {
17695 if (TREE_CODE (ce->value) == TREE_VEC)
17696 {
17697 int i, len = TREE_VEC_LENGTH (ce->value);
17698 for (i = 0; i < len; ++i)
17699 CONSTRUCTOR_APPEND_ELT (n, 0,
17700 TREE_VEC_ELT (ce->value, i));
17701 }
17702 else
17703 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17704 }
17705 }
17706
17707 r = build_constructor (init_list_type_node, n);
17708 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17709
17710 if (TREE_HAS_CONSTRUCTOR (t))
17711 RETURN (finish_compound_literal (type, r, complain));
17712
17713 TREE_TYPE (r) = type;
17714 RETURN (r);
17715 }
17716
17717 case TYPEID_EXPR:
17718 {
17719 tree operand_0 = TREE_OPERAND (t, 0);
17720 if (TYPE_P (operand_0))
17721 {
17722 operand_0 = tsubst (operand_0, args, complain, in_decl);
17723 RETURN (get_typeid (operand_0, complain));
17724 }
17725 else
17726 {
17727 operand_0 = RECUR (operand_0);
17728 RETURN (build_typeid (operand_0, complain));
17729 }
17730 }
17731
17732 case VAR_DECL:
17733 if (!args)
17734 RETURN (t);
17735 else if (DECL_PACK_P (t))
17736 {
17737 /* We don't build decls for an instantiation of a
17738 variadic capture proxy, we instantiate the elements
17739 when needed. */
17740 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17741 return RECUR (DECL_VALUE_EXPR (t));
17742 }
17743 /* Fall through */
17744
17745 case PARM_DECL:
17746 {
17747 tree r = tsubst_copy (t, args, complain, in_decl);
17748 /* ??? We're doing a subset of finish_id_expression here. */
17749 if (VAR_P (r)
17750 && !processing_template_decl
17751 && !cp_unevaluated_operand
17752 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17753 && CP_DECL_THREAD_LOCAL_P (r))
17754 {
17755 if (tree wrap = get_tls_wrapper_fn (r))
17756 /* Replace an evaluated use of the thread_local variable with
17757 a call to its wrapper. */
17758 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17759 }
17760 else if (outer_automatic_var_p (r))
17761 {
17762 r = process_outer_var_ref (r, complain);
17763 if (is_capture_proxy (r))
17764 register_local_specialization (r, t);
17765 }
17766
17767 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17768 /* If the original type was a reference, we'll be wrapped in
17769 the appropriate INDIRECT_REF. */
17770 r = convert_from_reference (r);
17771 RETURN (r);
17772 }
17773
17774 case VA_ARG_EXPR:
17775 {
17776 tree op0 = RECUR (TREE_OPERAND (t, 0));
17777 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17778 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17779 }
17780
17781 case OFFSETOF_EXPR:
17782 {
17783 tree object_ptr
17784 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17785 in_decl, /*function_p=*/false,
17786 /*integral_constant_expression_p=*/false);
17787 RETURN (finish_offsetof (object_ptr,
17788 RECUR (TREE_OPERAND (t, 0)),
17789 EXPR_LOCATION (t)));
17790 }
17791
17792 case ADDRESSOF_EXPR:
17793 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17794 RECUR (TREE_OPERAND (t, 0)), complain));
17795
17796 case TRAIT_EXPR:
17797 {
17798 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17799 complain, in_decl);
17800
17801 tree type2 = TRAIT_EXPR_TYPE2 (t);
17802 if (type2 && TREE_CODE (type2) == TREE_LIST)
17803 type2 = RECUR (type2);
17804 else if (type2)
17805 type2 = tsubst (type2, args, complain, in_decl);
17806
17807 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17808 }
17809
17810 case STMT_EXPR:
17811 {
17812 tree old_stmt_expr = cur_stmt_expr;
17813 tree stmt_expr = begin_stmt_expr ();
17814
17815 cur_stmt_expr = stmt_expr;
17816 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17817 integral_constant_expression_p);
17818 stmt_expr = finish_stmt_expr (stmt_expr, false);
17819 cur_stmt_expr = old_stmt_expr;
17820
17821 /* If the resulting list of expression statement is empty,
17822 fold it further into void_node. */
17823 if (empty_expr_stmt_p (stmt_expr))
17824 stmt_expr = void_node;
17825
17826 RETURN (stmt_expr);
17827 }
17828
17829 case LAMBDA_EXPR:
17830 {
17831 tree r = build_lambda_expr ();
17832
17833 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17834 LAMBDA_EXPR_CLOSURE (r) = type;
17835 CLASSTYPE_LAMBDA_EXPR (type) = r;
17836
17837 LAMBDA_EXPR_LOCATION (r)
17838 = LAMBDA_EXPR_LOCATION (t);
17839 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17840 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17841 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17842 LAMBDA_EXPR_DISCRIMINATOR (r)
17843 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17844 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17845 if (!scope)
17846 /* No substitution needed. */;
17847 else if (VAR_OR_FUNCTION_DECL_P (scope))
17848 /* For a function or variable scope, we want to use tsubst so that we
17849 don't complain about referring to an auto before deduction. */
17850 scope = tsubst (scope, args, complain, in_decl);
17851 else if (TREE_CODE (scope) == PARM_DECL)
17852 {
17853 /* Look up the parameter we want directly, as tsubst_copy
17854 doesn't do what we need. */
17855 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17856 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17857 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17858 parm = DECL_CHAIN (parm);
17859 scope = parm;
17860 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17861 if (DECL_CONTEXT (scope) == NULL_TREE)
17862 DECL_CONTEXT (scope) = fn;
17863 }
17864 else if (TREE_CODE (scope) == FIELD_DECL)
17865 /* For a field, use tsubst_copy so that we look up the existing field
17866 rather than build a new one. */
17867 scope = RECUR (scope);
17868 else
17869 gcc_unreachable ();
17870 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17871
17872 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17873 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17874
17875 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17876 determine_visibility (TYPE_NAME (type));
17877 /* Now that we know visibility, instantiate the type so we have a
17878 declaration of the op() for later calls to lambda_function. */
17879 complete_type (type);
17880
17881 if (tree fn = lambda_function (type))
17882 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17883
17884 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17885
17886 insert_pending_capture_proxies ();
17887
17888 RETURN (build_lambda_object (r));
17889 }
17890
17891 case TARGET_EXPR:
17892 /* We can get here for a constant initializer of non-dependent type.
17893 FIXME stop folding in cp_parser_initializer_clause. */
17894 {
17895 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17896 complain);
17897 RETURN (r);
17898 }
17899
17900 case TRANSACTION_EXPR:
17901 RETURN (tsubst_expr(t, args, complain, in_decl,
17902 integral_constant_expression_p));
17903
17904 case PAREN_EXPR:
17905 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17906
17907 case VEC_PERM_EXPR:
17908 {
17909 tree op0 = RECUR (TREE_OPERAND (t, 0));
17910 tree op1 = RECUR (TREE_OPERAND (t, 1));
17911 tree op2 = RECUR (TREE_OPERAND (t, 2));
17912 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17913 complain));
17914 }
17915
17916 case REQUIRES_EXPR:
17917 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17918
17919 default:
17920 /* Handle Objective-C++ constructs, if appropriate. */
17921 {
17922 tree subst
17923 = objcp_tsubst_copy_and_build (t, args, complain,
17924 in_decl, /*function_p=*/false);
17925 if (subst)
17926 RETURN (subst);
17927 }
17928 RETURN (tsubst_copy (t, args, complain, in_decl));
17929 }
17930
17931 #undef RECUR
17932 #undef RETURN
17933 out:
17934 input_location = loc;
17935 return retval;
17936 }
17937
17938 /* Verify that the instantiated ARGS are valid. For type arguments,
17939 make sure that the type's linkage is ok. For non-type arguments,
17940 make sure they are constants if they are integral or enumerations.
17941 Emit an error under control of COMPLAIN, and return TRUE on error. */
17942
17943 static bool
17944 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17945 {
17946 if (dependent_template_arg_p (t))
17947 return false;
17948 if (ARGUMENT_PACK_P (t))
17949 {
17950 tree vec = ARGUMENT_PACK_ARGS (t);
17951 int len = TREE_VEC_LENGTH (vec);
17952 bool result = false;
17953 int i;
17954
17955 for (i = 0; i < len; ++i)
17956 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17957 result = true;
17958 return result;
17959 }
17960 else if (TYPE_P (t))
17961 {
17962 /* [basic.link]: A name with no linkage (notably, the name
17963 of a class or enumeration declared in a local scope)
17964 shall not be used to declare an entity with linkage.
17965 This implies that names with no linkage cannot be used as
17966 template arguments
17967
17968 DR 757 relaxes this restriction for C++0x. */
17969 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17970 : no_linkage_check (t, /*relaxed_p=*/false));
17971
17972 if (nt)
17973 {
17974 /* DR 488 makes use of a type with no linkage cause
17975 type deduction to fail. */
17976 if (complain & tf_error)
17977 {
17978 if (TYPE_UNNAMED_P (nt))
17979 error ("%qT is/uses unnamed type", t);
17980 else
17981 error ("template argument for %qD uses local type %qT",
17982 tmpl, t);
17983 }
17984 return true;
17985 }
17986 /* In order to avoid all sorts of complications, we do not
17987 allow variably-modified types as template arguments. */
17988 else if (variably_modified_type_p (t, NULL_TREE))
17989 {
17990 if (complain & tf_error)
17991 error ("%qT is a variably modified type", t);
17992 return true;
17993 }
17994 }
17995 /* Class template and alias template arguments should be OK. */
17996 else if (DECL_TYPE_TEMPLATE_P (t))
17997 ;
17998 /* A non-type argument of integral or enumerated type must be a
17999 constant. */
18000 else if (TREE_TYPE (t)
18001 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18002 && !REFERENCE_REF_P (t)
18003 && !TREE_CONSTANT (t))
18004 {
18005 if (complain & tf_error)
18006 error ("integral expression %qE is not constant", t);
18007 return true;
18008 }
18009 return false;
18010 }
18011
18012 static bool
18013 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18014 {
18015 int ix, len = DECL_NTPARMS (tmpl);
18016 bool result = false;
18017
18018 for (ix = 0; ix != len; ix++)
18019 {
18020 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18021 result = true;
18022 }
18023 if (result && (complain & tf_error))
18024 error (" trying to instantiate %qD", tmpl);
18025 return result;
18026 }
18027
18028 /* We're out of SFINAE context now, so generate diagnostics for the access
18029 errors we saw earlier when instantiating D from TMPL and ARGS. */
18030
18031 static void
18032 recheck_decl_substitution (tree d, tree tmpl, tree args)
18033 {
18034 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18035 tree type = TREE_TYPE (pattern);
18036 location_t loc = input_location;
18037
18038 push_access_scope (d);
18039 push_deferring_access_checks (dk_no_deferred);
18040 input_location = DECL_SOURCE_LOCATION (pattern);
18041 tsubst (type, args, tf_warning_or_error, d);
18042 input_location = loc;
18043 pop_deferring_access_checks ();
18044 pop_access_scope (d);
18045 }
18046
18047 /* Instantiate the indicated variable, function, or alias template TMPL with
18048 the template arguments in TARG_PTR. */
18049
18050 static tree
18051 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18052 {
18053 tree targ_ptr = orig_args;
18054 tree fndecl;
18055 tree gen_tmpl;
18056 tree spec;
18057 bool access_ok = true;
18058
18059 if (tmpl == error_mark_node)
18060 return error_mark_node;
18061
18062 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18063
18064 /* If this function is a clone, handle it specially. */
18065 if (DECL_CLONED_FUNCTION_P (tmpl))
18066 {
18067 tree spec;
18068 tree clone;
18069
18070 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18071 DECL_CLONED_FUNCTION. */
18072 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18073 targ_ptr, complain);
18074 if (spec == error_mark_node)
18075 return error_mark_node;
18076
18077 /* Look for the clone. */
18078 FOR_EACH_CLONE (clone, spec)
18079 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18080 return clone;
18081 /* We should always have found the clone by now. */
18082 gcc_unreachable ();
18083 return NULL_TREE;
18084 }
18085
18086 if (targ_ptr == error_mark_node)
18087 return error_mark_node;
18088
18089 /* Check to see if we already have this specialization. */
18090 gen_tmpl = most_general_template (tmpl);
18091 if (TMPL_ARGS_DEPTH (targ_ptr)
18092 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18093 /* targ_ptr only has the innermost template args, so add the outer ones
18094 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18095 the case of a non-dependent call within a template definition). */
18096 targ_ptr = (add_outermost_template_args
18097 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18098 targ_ptr));
18099
18100 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18101 but it doesn't seem to be on the hot path. */
18102 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18103
18104 gcc_assert (tmpl == gen_tmpl
18105 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18106 == spec)
18107 || fndecl == NULL_TREE);
18108
18109 if (spec != NULL_TREE)
18110 {
18111 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18112 {
18113 if (complain & tf_error)
18114 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18115 return error_mark_node;
18116 }
18117 return spec;
18118 }
18119
18120 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18121 complain))
18122 return error_mark_node;
18123
18124 /* We are building a FUNCTION_DECL, during which the access of its
18125 parameters and return types have to be checked. However this
18126 FUNCTION_DECL which is the desired context for access checking
18127 is not built yet. We solve this chicken-and-egg problem by
18128 deferring all checks until we have the FUNCTION_DECL. */
18129 push_deferring_access_checks (dk_deferred);
18130
18131 /* Instantiation of the function happens in the context of the function
18132 template, not the context of the overload resolution we're doing. */
18133 push_to_top_level ();
18134 /* If there are dependent arguments, e.g. because we're doing partial
18135 ordering, make sure processing_template_decl stays set. */
18136 if (uses_template_parms (targ_ptr))
18137 ++processing_template_decl;
18138 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18139 {
18140 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18141 complain, gen_tmpl, true);
18142 push_nested_class (ctx);
18143 }
18144
18145 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18146
18147 fndecl = NULL_TREE;
18148 if (VAR_P (pattern))
18149 {
18150 /* We need to determine if we're using a partial or explicit
18151 specialization now, because the type of the variable could be
18152 different. */
18153 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18154 tree elt = most_specialized_partial_spec (tid, complain);
18155 if (elt == error_mark_node)
18156 pattern = error_mark_node;
18157 else if (elt)
18158 {
18159 tree partial_tmpl = TREE_VALUE (elt);
18160 tree partial_args = TREE_PURPOSE (elt);
18161 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18162 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18163 }
18164 }
18165
18166 /* Substitute template parameters to obtain the specialization. */
18167 if (fndecl == NULL_TREE)
18168 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18169 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18170 pop_nested_class ();
18171 pop_from_top_level ();
18172
18173 if (fndecl == error_mark_node)
18174 {
18175 pop_deferring_access_checks ();
18176 return error_mark_node;
18177 }
18178
18179 /* The DECL_TI_TEMPLATE should always be the immediate parent
18180 template, not the most general template. */
18181 DECL_TI_TEMPLATE (fndecl) = tmpl;
18182 DECL_TI_ARGS (fndecl) = targ_ptr;
18183
18184 /* Now we know the specialization, compute access previously
18185 deferred. Do no access control for inheriting constructors,
18186 as we already checked access for the inherited constructor. */
18187 if (!(flag_new_inheriting_ctors
18188 && DECL_INHERITED_CTOR (fndecl)))
18189 {
18190 push_access_scope (fndecl);
18191 if (!perform_deferred_access_checks (complain))
18192 access_ok = false;
18193 pop_access_scope (fndecl);
18194 }
18195 pop_deferring_access_checks ();
18196
18197 /* If we've just instantiated the main entry point for a function,
18198 instantiate all the alternate entry points as well. We do this
18199 by cloning the instantiation of the main entry point, not by
18200 instantiating the template clones. */
18201 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18202 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
18203
18204 if (!access_ok)
18205 {
18206 if (!(complain & tf_error))
18207 {
18208 /* Remember to reinstantiate when we're out of SFINAE so the user
18209 can see the errors. */
18210 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18211 }
18212 return error_mark_node;
18213 }
18214 return fndecl;
18215 }
18216
18217 /* Wrapper for instantiate_template_1. */
18218
18219 tree
18220 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18221 {
18222 tree ret;
18223 timevar_push (TV_TEMPLATE_INST);
18224 ret = instantiate_template_1 (tmpl, orig_args, complain);
18225 timevar_pop (TV_TEMPLATE_INST);
18226 return ret;
18227 }
18228
18229 /* Instantiate the alias template TMPL with ARGS. Also push a template
18230 instantiation level, which instantiate_template doesn't do because
18231 functions and variables have sufficient context established by the
18232 callers. */
18233
18234 static tree
18235 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18236 {
18237 struct pending_template *old_last_pend = last_pending_template;
18238 struct tinst_level *old_error_tinst = last_error_tinst_level;
18239 if (tmpl == error_mark_node || args == error_mark_node)
18240 return error_mark_node;
18241 tree tinst = build_tree_list (tmpl, args);
18242 if (!push_tinst_level (tinst))
18243 {
18244 ggc_free (tinst);
18245 return error_mark_node;
18246 }
18247
18248 args =
18249 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18250 args, tmpl, complain,
18251 /*require_all_args=*/true,
18252 /*use_default_args=*/true);
18253
18254 tree r = instantiate_template (tmpl, args, complain);
18255 pop_tinst_level ();
18256 /* We can't free this if a pending_template entry or last_error_tinst_level
18257 is pointing at it. */
18258 if (last_pending_template == old_last_pend
18259 && last_error_tinst_level == old_error_tinst)
18260 ggc_free (tinst);
18261
18262 return r;
18263 }
18264
18265 /* PARM is a template parameter pack for FN. Returns true iff
18266 PARM is used in a deducible way in the argument list of FN. */
18267
18268 static bool
18269 pack_deducible_p (tree parm, tree fn)
18270 {
18271 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18272 for (; t; t = TREE_CHAIN (t))
18273 {
18274 tree type = TREE_VALUE (t);
18275 tree packs;
18276 if (!PACK_EXPANSION_P (type))
18277 continue;
18278 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18279 packs; packs = TREE_CHAIN (packs))
18280 if (template_args_equal (TREE_VALUE (packs), parm))
18281 {
18282 /* The template parameter pack is used in a function parameter
18283 pack. If this is the end of the parameter list, the
18284 template parameter pack is deducible. */
18285 if (TREE_CHAIN (t) == void_list_node)
18286 return true;
18287 else
18288 /* Otherwise, not. Well, it could be deduced from
18289 a non-pack parameter, but doing so would end up with
18290 a deduction mismatch, so don't bother. */
18291 return false;
18292 }
18293 }
18294 /* The template parameter pack isn't used in any function parameter
18295 packs, but it might be used deeper, e.g. tuple<Args...>. */
18296 return true;
18297 }
18298
18299 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18300 NARGS elements of the arguments that are being used when calling
18301 it. TARGS is a vector into which the deduced template arguments
18302 are placed.
18303
18304 Returns either a FUNCTION_DECL for the matching specialization of FN or
18305 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18306 true, diagnostics will be printed to explain why it failed.
18307
18308 If FN is a conversion operator, or we are trying to produce a specific
18309 specialization, RETURN_TYPE is the return type desired.
18310
18311 The EXPLICIT_TARGS are explicit template arguments provided via a
18312 template-id.
18313
18314 The parameter STRICT is one of:
18315
18316 DEDUCE_CALL:
18317 We are deducing arguments for a function call, as in
18318 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18319 deducing arguments for a call to the result of a conversion
18320 function template, as in [over.call.object].
18321
18322 DEDUCE_CONV:
18323 We are deducing arguments for a conversion function, as in
18324 [temp.deduct.conv].
18325
18326 DEDUCE_EXACT:
18327 We are deducing arguments when doing an explicit instantiation
18328 as in [temp.explicit], when determining an explicit specialization
18329 as in [temp.expl.spec], or when taking the address of a function
18330 template, as in [temp.deduct.funcaddr]. */
18331
18332 tree
18333 fn_type_unification (tree fn,
18334 tree explicit_targs,
18335 tree targs,
18336 const tree *args,
18337 unsigned int nargs,
18338 tree return_type,
18339 unification_kind_t strict,
18340 int flags,
18341 bool explain_p,
18342 bool decltype_p)
18343 {
18344 tree parms;
18345 tree fntype;
18346 tree decl = NULL_TREE;
18347 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18348 bool ok;
18349 static int deduction_depth;
18350 struct pending_template *old_last_pend = last_pending_template;
18351 struct tinst_level *old_error_tinst = last_error_tinst_level;
18352
18353 tree orig_fn = fn;
18354 if (flag_new_inheriting_ctors)
18355 fn = strip_inheriting_ctors (fn);
18356
18357 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18358 tree tinst;
18359 tree r = error_mark_node;
18360
18361 tree full_targs = targs;
18362 if (TMPL_ARGS_DEPTH (targs)
18363 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18364 full_targs = (add_outermost_template_args
18365 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18366 targs));
18367
18368 if (decltype_p)
18369 complain |= tf_decltype;
18370
18371 /* In C++0x, it's possible to have a function template whose type depends
18372 on itself recursively. This is most obvious with decltype, but can also
18373 occur with enumeration scope (c++/48969). So we need to catch infinite
18374 recursion and reject the substitution at deduction time; this function
18375 will return error_mark_node for any repeated substitution.
18376
18377 This also catches excessive recursion such as when f<N> depends on
18378 f<N-1> across all integers, and returns error_mark_node for all the
18379 substitutions back up to the initial one.
18380
18381 This is, of course, not reentrant. */
18382 if (excessive_deduction_depth)
18383 return error_mark_node;
18384 tinst = build_tree_list (fn, NULL_TREE);
18385 ++deduction_depth;
18386
18387 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18388
18389 fntype = TREE_TYPE (fn);
18390 if (explicit_targs)
18391 {
18392 /* [temp.deduct]
18393
18394 The specified template arguments must match the template
18395 parameters in kind (i.e., type, nontype, template), and there
18396 must not be more arguments than there are parameters;
18397 otherwise type deduction fails.
18398
18399 Nontype arguments must match the types of the corresponding
18400 nontype template parameters, or must be convertible to the
18401 types of the corresponding nontype parameters as specified in
18402 _temp.arg.nontype_, otherwise type deduction fails.
18403
18404 All references in the function type of the function template
18405 to the corresponding template parameters are replaced by the
18406 specified template argument values. If a substitution in a
18407 template parameter or in the function type of the function
18408 template results in an invalid type, type deduction fails. */
18409 int i, len = TREE_VEC_LENGTH (tparms);
18410 location_t loc = input_location;
18411 bool incomplete = false;
18412
18413 if (explicit_targs == error_mark_node)
18414 goto fail;
18415
18416 if (TMPL_ARGS_DEPTH (explicit_targs)
18417 < TMPL_ARGS_DEPTH (full_targs))
18418 explicit_targs = add_outermost_template_args (full_targs,
18419 explicit_targs);
18420
18421 /* Adjust any explicit template arguments before entering the
18422 substitution context. */
18423 explicit_targs
18424 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18425 complain,
18426 /*require_all_args=*/false,
18427 /*use_default_args=*/false));
18428 if (explicit_targs == error_mark_node)
18429 goto fail;
18430
18431 /* Substitute the explicit args into the function type. This is
18432 necessary so that, for instance, explicitly declared function
18433 arguments can match null pointed constants. If we were given
18434 an incomplete set of explicit args, we must not do semantic
18435 processing during substitution as we could create partial
18436 instantiations. */
18437 for (i = 0; i < len; i++)
18438 {
18439 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18440 bool parameter_pack = false;
18441 tree targ = TREE_VEC_ELT (explicit_targs, i);
18442
18443 /* Dig out the actual parm. */
18444 if (TREE_CODE (parm) == TYPE_DECL
18445 || TREE_CODE (parm) == TEMPLATE_DECL)
18446 {
18447 parm = TREE_TYPE (parm);
18448 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18449 }
18450 else if (TREE_CODE (parm) == PARM_DECL)
18451 {
18452 parm = DECL_INITIAL (parm);
18453 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18454 }
18455
18456 if (!parameter_pack && targ == NULL_TREE)
18457 /* No explicit argument for this template parameter. */
18458 incomplete = true;
18459
18460 if (parameter_pack && pack_deducible_p (parm, fn))
18461 {
18462 /* Mark the argument pack as "incomplete". We could
18463 still deduce more arguments during unification.
18464 We remove this mark in type_unification_real. */
18465 if (targ)
18466 {
18467 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18468 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18469 = ARGUMENT_PACK_ARGS (targ);
18470 }
18471
18472 /* We have some incomplete argument packs. */
18473 incomplete = true;
18474 }
18475 }
18476
18477 TREE_VALUE (tinst) = explicit_targs;
18478 if (!push_tinst_level (tinst))
18479 {
18480 excessive_deduction_depth = true;
18481 goto fail;
18482 }
18483 processing_template_decl += incomplete;
18484 input_location = DECL_SOURCE_LOCATION (fn);
18485 /* Ignore any access checks; we'll see them again in
18486 instantiate_template and they might have the wrong
18487 access path at this point. */
18488 push_deferring_access_checks (dk_deferred);
18489 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18490 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18491 pop_deferring_access_checks ();
18492 input_location = loc;
18493 processing_template_decl -= incomplete;
18494 pop_tinst_level ();
18495
18496 if (fntype == error_mark_node)
18497 goto fail;
18498
18499 /* Place the explicitly specified arguments in TARGS. */
18500 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18501 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18502 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18503 }
18504
18505 /* Never do unification on the 'this' parameter. */
18506 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18507
18508 if (return_type && strict == DEDUCE_CALL)
18509 {
18510 /* We're deducing for a call to the result of a template conversion
18511 function. The parms we really want are in return_type. */
18512 if (POINTER_TYPE_P (return_type))
18513 return_type = TREE_TYPE (return_type);
18514 parms = TYPE_ARG_TYPES (return_type);
18515 }
18516 else if (return_type)
18517 {
18518 tree *new_args;
18519
18520 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18521 new_args = XALLOCAVEC (tree, nargs + 1);
18522 new_args[0] = return_type;
18523 memcpy (new_args + 1, args, nargs * sizeof (tree));
18524 args = new_args;
18525 ++nargs;
18526 }
18527
18528 /* We allow incomplete unification without an error message here
18529 because the standard doesn't seem to explicitly prohibit it. Our
18530 callers must be ready to deal with unification failures in any
18531 event. */
18532
18533 TREE_VALUE (tinst) = targs;
18534 /* If we aren't explaining yet, push tinst context so we can see where
18535 any errors (e.g. from class instantiations triggered by instantiation
18536 of default template arguments) come from. If we are explaining, this
18537 context is redundant. */
18538 if (!explain_p && !push_tinst_level (tinst))
18539 {
18540 excessive_deduction_depth = true;
18541 goto fail;
18542 }
18543
18544 /* type_unification_real will pass back any access checks from default
18545 template argument substitution. */
18546 vec<deferred_access_check, va_gc> *checks;
18547 checks = NULL;
18548
18549 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18550 full_targs, parms, args, nargs, /*subr=*/0,
18551 strict, flags, &checks, explain_p);
18552 if (!explain_p)
18553 pop_tinst_level ();
18554 if (!ok)
18555 goto fail;
18556
18557 /* Now that we have bindings for all of the template arguments,
18558 ensure that the arguments deduced for the template template
18559 parameters have compatible template parameter lists. We cannot
18560 check this property before we have deduced all template
18561 arguments, because the template parameter types of a template
18562 template parameter might depend on prior template parameters
18563 deduced after the template template parameter. The following
18564 ill-formed example illustrates this issue:
18565
18566 template<typename T, template<T> class C> void f(C<5>, T);
18567
18568 template<int N> struct X {};
18569
18570 void g() {
18571 f(X<5>(), 5l); // error: template argument deduction fails
18572 }
18573
18574 The template parameter list of 'C' depends on the template type
18575 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18576 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18577 time that we deduce 'C'. */
18578 if (!template_template_parm_bindings_ok_p
18579 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18580 {
18581 unify_inconsistent_template_template_parameters (explain_p);
18582 goto fail;
18583 }
18584
18585 /* All is well so far. Now, check:
18586
18587 [temp.deduct]
18588
18589 When all template arguments have been deduced, all uses of
18590 template parameters in nondeduced contexts are replaced with
18591 the corresponding deduced argument values. If the
18592 substitution results in an invalid type, as described above,
18593 type deduction fails. */
18594 TREE_VALUE (tinst) = targs;
18595 if (!push_tinst_level (tinst))
18596 {
18597 excessive_deduction_depth = true;
18598 goto fail;
18599 }
18600
18601 /* Also collect access checks from the instantiation. */
18602 reopen_deferring_access_checks (checks);
18603
18604 decl = instantiate_template (fn, targs, complain);
18605
18606 checks = get_deferred_access_checks ();
18607 pop_deferring_access_checks ();
18608
18609 pop_tinst_level ();
18610
18611 if (decl == error_mark_node)
18612 goto fail;
18613
18614 /* Now perform any access checks encountered during substitution. */
18615 push_access_scope (decl);
18616 ok = perform_access_checks (checks, complain);
18617 pop_access_scope (decl);
18618 if (!ok)
18619 goto fail;
18620
18621 /* If we're looking for an exact match, check that what we got
18622 is indeed an exact match. It might not be if some template
18623 parameters are used in non-deduced contexts. But don't check
18624 for an exact match if we have dependent template arguments;
18625 in that case we're doing partial ordering, and we already know
18626 that we have two candidates that will provide the actual type. */
18627 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18628 {
18629 tree substed = TREE_TYPE (decl);
18630 unsigned int i;
18631
18632 tree sarg
18633 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18634 if (return_type)
18635 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18636 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18637 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18638 {
18639 unify_type_mismatch (explain_p, args[i],
18640 TREE_VALUE (sarg));
18641 goto fail;
18642 }
18643 }
18644
18645 /* After doing deduction with the inherited constructor, actually return an
18646 instantiation of the inheriting constructor. */
18647 if (orig_fn != fn)
18648 decl = instantiate_template (orig_fn, targs, complain);
18649
18650 r = decl;
18651
18652 fail:
18653 --deduction_depth;
18654 if (excessive_deduction_depth)
18655 {
18656 if (deduction_depth == 0)
18657 /* Reset once we're all the way out. */
18658 excessive_deduction_depth = false;
18659 }
18660
18661 /* We can't free this if a pending_template entry or last_error_tinst_level
18662 is pointing at it. */
18663 if (last_pending_template == old_last_pend
18664 && last_error_tinst_level == old_error_tinst)
18665 ggc_free (tinst);
18666
18667 return r;
18668 }
18669
18670 /* Adjust types before performing type deduction, as described in
18671 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18672 sections are symmetric. PARM is the type of a function parameter
18673 or the return type of the conversion function. ARG is the type of
18674 the argument passed to the call, or the type of the value
18675 initialized with the result of the conversion function.
18676 ARG_EXPR is the original argument expression, which may be null. */
18677
18678 static int
18679 maybe_adjust_types_for_deduction (unification_kind_t strict,
18680 tree* parm,
18681 tree* arg,
18682 tree arg_expr)
18683 {
18684 int result = 0;
18685
18686 switch (strict)
18687 {
18688 case DEDUCE_CALL:
18689 break;
18690
18691 case DEDUCE_CONV:
18692 /* Swap PARM and ARG throughout the remainder of this
18693 function; the handling is precisely symmetric since PARM
18694 will initialize ARG rather than vice versa. */
18695 std::swap (parm, arg);
18696 break;
18697
18698 case DEDUCE_EXACT:
18699 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18700 too, but here handle it by stripping the reference from PARM
18701 rather than by adding it to ARG. */
18702 if (TREE_CODE (*parm) == REFERENCE_TYPE
18703 && TYPE_REF_IS_RVALUE (*parm)
18704 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18705 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18706 && TREE_CODE (*arg) == REFERENCE_TYPE
18707 && !TYPE_REF_IS_RVALUE (*arg))
18708 *parm = TREE_TYPE (*parm);
18709 /* Nothing else to do in this case. */
18710 return 0;
18711
18712 default:
18713 gcc_unreachable ();
18714 }
18715
18716 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18717 {
18718 /* [temp.deduct.call]
18719
18720 If P is not a reference type:
18721
18722 --If A is an array type, the pointer type produced by the
18723 array-to-pointer standard conversion (_conv.array_) is
18724 used in place of A for type deduction; otherwise,
18725
18726 --If A is a function type, the pointer type produced by
18727 the function-to-pointer standard conversion
18728 (_conv.func_) is used in place of A for type deduction;
18729 otherwise,
18730
18731 --If A is a cv-qualified type, the top level
18732 cv-qualifiers of A's type are ignored for type
18733 deduction. */
18734 if (TREE_CODE (*arg) == ARRAY_TYPE)
18735 *arg = build_pointer_type (TREE_TYPE (*arg));
18736 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18737 *arg = build_pointer_type (*arg);
18738 else
18739 *arg = TYPE_MAIN_VARIANT (*arg);
18740 }
18741
18742 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18743 reference to a cv-unqualified template parameter that does not represent a
18744 template parameter of a class template (during class template argument
18745 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18746 an lvalue, the type "lvalue reference to A" is used in place of A for type
18747 deduction. */
18748 if (TREE_CODE (*parm) == REFERENCE_TYPE
18749 && TYPE_REF_IS_RVALUE (*parm)
18750 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18751 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18752 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18753 && (arg_expr ? lvalue_p (arg_expr)
18754 /* try_one_overload doesn't provide an arg_expr, but
18755 functions are always lvalues. */
18756 : TREE_CODE (*arg) == FUNCTION_TYPE))
18757 *arg = build_reference_type (*arg);
18758
18759 /* [temp.deduct.call]
18760
18761 If P is a cv-qualified type, the top level cv-qualifiers
18762 of P's type are ignored for type deduction. If P is a
18763 reference type, the type referred to by P is used for
18764 type deduction. */
18765 *parm = TYPE_MAIN_VARIANT (*parm);
18766 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18767 {
18768 *parm = TREE_TYPE (*parm);
18769 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18770 }
18771
18772 /* DR 322. For conversion deduction, remove a reference type on parm
18773 too (which has been swapped into ARG). */
18774 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18775 *arg = TREE_TYPE (*arg);
18776
18777 return result;
18778 }
18779
18780 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18781 template which does contain any deducible template parameters; check if
18782 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18783 unify_one_argument. */
18784
18785 static int
18786 check_non_deducible_conversion (tree parm, tree arg, int strict,
18787 int flags, bool explain_p)
18788 {
18789 tree type;
18790
18791 if (!TYPE_P (arg))
18792 type = TREE_TYPE (arg);
18793 else
18794 type = arg;
18795
18796 if (same_type_p (parm, type))
18797 return unify_success (explain_p);
18798
18799 if (strict == DEDUCE_CONV)
18800 {
18801 if (can_convert_arg (type, parm, NULL_TREE, flags,
18802 explain_p ? tf_warning_or_error : tf_none))
18803 return unify_success (explain_p);
18804 }
18805 else if (strict != DEDUCE_EXACT)
18806 {
18807 if (can_convert_arg (parm, type,
18808 TYPE_P (arg) ? NULL_TREE : arg,
18809 flags, explain_p ? tf_warning_or_error : tf_none))
18810 return unify_success (explain_p);
18811 }
18812
18813 if (strict == DEDUCE_EXACT)
18814 return unify_type_mismatch (explain_p, parm, arg);
18815 else
18816 return unify_arg_conversion (explain_p, parm, type, arg);
18817 }
18818
18819 static bool uses_deducible_template_parms (tree type);
18820
18821 /* Returns true iff the expression EXPR is one from which a template
18822 argument can be deduced. In other words, if it's an undecorated
18823 use of a template non-type parameter. */
18824
18825 static bool
18826 deducible_expression (tree expr)
18827 {
18828 /* Strip implicit conversions. */
18829 while (CONVERT_EXPR_P (expr))
18830 expr = TREE_OPERAND (expr, 0);
18831 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18832 }
18833
18834 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18835 deducible way; that is, if it has a max value of <PARM> - 1. */
18836
18837 static bool
18838 deducible_array_bound (tree domain)
18839 {
18840 if (domain == NULL_TREE)
18841 return false;
18842
18843 tree max = TYPE_MAX_VALUE (domain);
18844 if (TREE_CODE (max) != MINUS_EXPR)
18845 return false;
18846
18847 return deducible_expression (TREE_OPERAND (max, 0));
18848 }
18849
18850 /* Returns true iff the template arguments ARGS use a template parameter
18851 in a deducible way. */
18852
18853 static bool
18854 deducible_template_args (tree args)
18855 {
18856 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18857 {
18858 bool deducible;
18859 tree elt = TREE_VEC_ELT (args, i);
18860 if (ARGUMENT_PACK_P (elt))
18861 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18862 else
18863 {
18864 if (PACK_EXPANSION_P (elt))
18865 elt = PACK_EXPANSION_PATTERN (elt);
18866 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18867 deducible = true;
18868 else if (TYPE_P (elt))
18869 deducible = uses_deducible_template_parms (elt);
18870 else
18871 deducible = deducible_expression (elt);
18872 }
18873 if (deducible)
18874 return true;
18875 }
18876 return false;
18877 }
18878
18879 /* Returns true iff TYPE contains any deducible references to template
18880 parameters, as per 14.8.2.5. */
18881
18882 static bool
18883 uses_deducible_template_parms (tree type)
18884 {
18885 if (PACK_EXPANSION_P (type))
18886 type = PACK_EXPANSION_PATTERN (type);
18887
18888 /* T
18889 cv-list T
18890 TT<T>
18891 TT<i>
18892 TT<> */
18893 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18894 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18895 return true;
18896
18897 /* T*
18898 T&
18899 T&& */
18900 if (POINTER_TYPE_P (type))
18901 return uses_deducible_template_parms (TREE_TYPE (type));
18902
18903 /* T[integer-constant ]
18904 type [i] */
18905 if (TREE_CODE (type) == ARRAY_TYPE)
18906 return (uses_deducible_template_parms (TREE_TYPE (type))
18907 || deducible_array_bound (TYPE_DOMAIN (type)));
18908
18909 /* T type ::*
18910 type T::*
18911 T T::*
18912 T (type ::*)()
18913 type (T::*)()
18914 type (type ::*)(T)
18915 type (T::*)(T)
18916 T (type ::*)(T)
18917 T (T::*)()
18918 T (T::*)(T) */
18919 if (TYPE_PTRMEM_P (type))
18920 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18921 || (uses_deducible_template_parms
18922 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18923
18924 /* template-name <T> (where template-name refers to a class template)
18925 template-name <i> (where template-name refers to a class template) */
18926 if (CLASS_TYPE_P (type)
18927 && CLASSTYPE_TEMPLATE_INFO (type)
18928 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18929 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18930 (CLASSTYPE_TI_ARGS (type)));
18931
18932 /* type (T)
18933 T()
18934 T(T) */
18935 if (TREE_CODE (type) == FUNCTION_TYPE
18936 || TREE_CODE (type) == METHOD_TYPE)
18937 {
18938 if (uses_deducible_template_parms (TREE_TYPE (type)))
18939 return true;
18940 tree parm = TYPE_ARG_TYPES (type);
18941 if (TREE_CODE (type) == METHOD_TYPE)
18942 parm = TREE_CHAIN (parm);
18943 for (; parm; parm = TREE_CHAIN (parm))
18944 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18945 return true;
18946 }
18947
18948 return false;
18949 }
18950
18951 /* Subroutine of type_unification_real and unify_pack_expansion to
18952 handle unification of a single P/A pair. Parameters are as
18953 for those functions. */
18954
18955 static int
18956 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18957 int subr, unification_kind_t strict,
18958 bool explain_p)
18959 {
18960 tree arg_expr = NULL_TREE;
18961 int arg_strict;
18962
18963 if (arg == error_mark_node || parm == error_mark_node)
18964 return unify_invalid (explain_p);
18965 if (arg == unknown_type_node)
18966 /* We can't deduce anything from this, but we might get all the
18967 template args from other function args. */
18968 return unify_success (explain_p);
18969
18970 /* Implicit conversions (Clause 4) will be performed on a function
18971 argument to convert it to the type of the corresponding function
18972 parameter if the parameter type contains no template-parameters that
18973 participate in template argument deduction. */
18974 if (strict != DEDUCE_EXACT
18975 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18976 /* For function parameters with no deducible template parameters,
18977 just return. We'll check non-dependent conversions later. */
18978 return unify_success (explain_p);
18979
18980 switch (strict)
18981 {
18982 case DEDUCE_CALL:
18983 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18984 | UNIFY_ALLOW_MORE_CV_QUAL
18985 | UNIFY_ALLOW_DERIVED);
18986 break;
18987
18988 case DEDUCE_CONV:
18989 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18990 break;
18991
18992 case DEDUCE_EXACT:
18993 arg_strict = UNIFY_ALLOW_NONE;
18994 break;
18995
18996 default:
18997 gcc_unreachable ();
18998 }
18999
19000 /* We only do these transformations if this is the top-level
19001 parameter_type_list in a call or declaration matching; in other
19002 situations (nested function declarators, template argument lists) we
19003 won't be comparing a type to an expression, and we don't do any type
19004 adjustments. */
19005 if (!subr)
19006 {
19007 if (!TYPE_P (arg))
19008 {
19009 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19010 if (type_unknown_p (arg))
19011 {
19012 /* [temp.deduct.type] A template-argument can be
19013 deduced from a pointer to function or pointer
19014 to member function argument if the set of
19015 overloaded functions does not contain function
19016 templates and at most one of a set of
19017 overloaded functions provides a unique
19018 match. */
19019
19020 if (resolve_overloaded_unification
19021 (tparms, targs, parm, arg, strict,
19022 arg_strict, explain_p))
19023 return unify_success (explain_p);
19024 return unify_overload_resolution_failure (explain_p, arg);
19025 }
19026
19027 arg_expr = arg;
19028 arg = unlowered_expr_type (arg);
19029 if (arg == error_mark_node)
19030 return unify_invalid (explain_p);
19031 }
19032
19033 arg_strict |=
19034 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19035 }
19036 else
19037 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19038 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19039 return unify_template_argument_mismatch (explain_p, parm, arg);
19040
19041 /* For deduction from an init-list we need the actual list. */
19042 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19043 arg = arg_expr;
19044 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19045 }
19046
19047 /* for_each_template_parm callback that always returns 0. */
19048
19049 static int
19050 zero_r (tree, void *)
19051 {
19052 return 0;
19053 }
19054
19055 /* for_each_template_parm any_fn callback to handle deduction of a template
19056 type argument from the type of an array bound. */
19057
19058 static int
19059 array_deduction_r (tree t, void *data)
19060 {
19061 tree_pair_p d = (tree_pair_p)data;
19062 tree &tparms = d->purpose;
19063 tree &targs = d->value;
19064
19065 if (TREE_CODE (t) == ARRAY_TYPE)
19066 if (tree dom = TYPE_DOMAIN (t))
19067 if (tree max = TYPE_MAX_VALUE (dom))
19068 {
19069 if (TREE_CODE (max) == MINUS_EXPR)
19070 max = TREE_OPERAND (max, 0);
19071 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19072 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19073 UNIFY_ALLOW_NONE, /*explain*/false);
19074 }
19075
19076 /* Keep walking. */
19077 return 0;
19078 }
19079
19080 /* Try to deduce any not-yet-deduced template type arguments from the type of
19081 an array bound. This is handled separately from unify because 14.8.2.5 says
19082 "The type of a type parameter is only deduced from an array bound if it is
19083 not otherwise deduced." */
19084
19085 static void
19086 try_array_deduction (tree tparms, tree targs, tree parm)
19087 {
19088 tree_pair_s data = { tparms, targs };
19089 hash_set<tree> visited;
19090 for_each_template_parm (parm, zero_r, &data, &visited,
19091 /*nondeduced*/false, array_deduction_r);
19092 }
19093
19094 /* Most parms like fn_type_unification.
19095
19096 If SUBR is 1, we're being called recursively (to unify the
19097 arguments of a function or method parameter of a function
19098 template).
19099
19100 CHECKS is a pointer to a vector of access checks encountered while
19101 substituting default template arguments. */
19102
19103 static int
19104 type_unification_real (tree tparms,
19105 tree full_targs,
19106 tree xparms,
19107 const tree *xargs,
19108 unsigned int xnargs,
19109 int subr,
19110 unification_kind_t strict,
19111 int flags,
19112 vec<deferred_access_check, va_gc> **checks,
19113 bool explain_p)
19114 {
19115 tree parm, arg;
19116 int i;
19117 int ntparms = TREE_VEC_LENGTH (tparms);
19118 int saw_undeduced = 0;
19119 tree parms;
19120 const tree *args;
19121 unsigned int nargs;
19122 unsigned int ia;
19123
19124 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19125 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19126 gcc_assert (ntparms > 0);
19127
19128 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19129
19130 /* Reset the number of non-defaulted template arguments contained
19131 in TARGS. */
19132 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19133
19134 again:
19135 parms = xparms;
19136 args = xargs;
19137 nargs = xnargs;
19138
19139 ia = 0;
19140 while (parms && parms != void_list_node
19141 && ia < nargs)
19142 {
19143 parm = TREE_VALUE (parms);
19144
19145 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19146 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19147 /* For a function parameter pack that occurs at the end of the
19148 parameter-declaration-list, the type A of each remaining
19149 argument of the call is compared with the type P of the
19150 declarator-id of the function parameter pack. */
19151 break;
19152
19153 parms = TREE_CHAIN (parms);
19154
19155 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19156 /* For a function parameter pack that does not occur at the
19157 end of the parameter-declaration-list, the type of the
19158 parameter pack is a non-deduced context. */
19159 continue;
19160
19161 arg = args[ia];
19162 ++ia;
19163
19164 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19165 explain_p))
19166 return 1;
19167 }
19168
19169 if (parms
19170 && parms != void_list_node
19171 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19172 {
19173 /* Unify the remaining arguments with the pack expansion type. */
19174 tree argvec;
19175 tree parmvec = make_tree_vec (1);
19176
19177 /* Allocate a TREE_VEC and copy in all of the arguments */
19178 argvec = make_tree_vec (nargs - ia);
19179 for (i = 0; ia < nargs; ++ia, ++i)
19180 TREE_VEC_ELT (argvec, i) = args[ia];
19181
19182 /* Copy the parameter into parmvec. */
19183 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19184 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19185 /*subr=*/subr, explain_p))
19186 return 1;
19187
19188 /* Advance to the end of the list of parameters. */
19189 parms = TREE_CHAIN (parms);
19190 }
19191
19192 /* Fail if we've reached the end of the parm list, and more args
19193 are present, and the parm list isn't variadic. */
19194 if (ia < nargs && parms == void_list_node)
19195 return unify_too_many_arguments (explain_p, nargs, ia);
19196 /* Fail if parms are left and they don't have default values and
19197 they aren't all deduced as empty packs (c++/57397). This is
19198 consistent with sufficient_parms_p. */
19199 if (parms && parms != void_list_node
19200 && TREE_PURPOSE (parms) == NULL_TREE)
19201 {
19202 unsigned int count = nargs;
19203 tree p = parms;
19204 bool type_pack_p;
19205 do
19206 {
19207 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19208 if (!type_pack_p)
19209 count++;
19210 p = TREE_CHAIN (p);
19211 }
19212 while (p && p != void_list_node);
19213 if (count != nargs)
19214 return unify_too_few_arguments (explain_p, ia, count,
19215 type_pack_p);
19216 }
19217
19218 if (!subr)
19219 {
19220 tsubst_flags_t complain = (explain_p
19221 ? tf_warning_or_error
19222 : tf_none);
19223 bool tried_array_deduction = (cxx_dialect < cxx1z);
19224
19225 for (i = 0; i < ntparms; i++)
19226 {
19227 tree targ = TREE_VEC_ELT (targs, i);
19228 tree tparm = TREE_VEC_ELT (tparms, i);
19229
19230 /* Clear the "incomplete" flags on all argument packs now so that
19231 substituting them into later default arguments works. */
19232 if (targ && ARGUMENT_PACK_P (targ))
19233 {
19234 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19235 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19236 }
19237
19238 if (targ || tparm == error_mark_node)
19239 continue;
19240 tparm = TREE_VALUE (tparm);
19241
19242 if (TREE_CODE (tparm) == TYPE_DECL
19243 && !tried_array_deduction)
19244 {
19245 try_array_deduction (tparms, targs, xparms);
19246 tried_array_deduction = true;
19247 if (TREE_VEC_ELT (targs, i))
19248 continue;
19249 }
19250
19251 /* If this is an undeduced nontype parameter that depends on
19252 a type parameter, try another pass; its type may have been
19253 deduced from a later argument than the one from which
19254 this parameter can be deduced. */
19255 if (TREE_CODE (tparm) == PARM_DECL
19256 && uses_template_parms (TREE_TYPE (tparm))
19257 && saw_undeduced < 2)
19258 {
19259 saw_undeduced = 1;
19260 continue;
19261 }
19262
19263 /* Core issue #226 (C++0x) [temp.deduct]:
19264
19265 If a template argument has not been deduced, its
19266 default template argument, if any, is used.
19267
19268 When we are in C++98 mode, TREE_PURPOSE will either
19269 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19270 to explicitly check cxx_dialect here. */
19271 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19272 /* OK, there is a default argument. Wait until after the
19273 conversion check to do substitution. */
19274 continue;
19275
19276 /* If the type parameter is a parameter pack, then it will
19277 be deduced to an empty parameter pack. */
19278 if (template_parameter_pack_p (tparm))
19279 {
19280 tree arg;
19281
19282 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19283 {
19284 arg = make_node (NONTYPE_ARGUMENT_PACK);
19285 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
19286 TREE_CONSTANT (arg) = 1;
19287 }
19288 else
19289 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19290
19291 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19292
19293 TREE_VEC_ELT (targs, i) = arg;
19294 continue;
19295 }
19296
19297 return unify_parameter_deduction_failure (explain_p, tparm);
19298 }
19299
19300 /* DR 1391: All parameters have args, now check non-dependent parms for
19301 convertibility. */
19302 if (saw_undeduced < 2)
19303 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19304 parms && parms != void_list_node && ia < nargs; )
19305 {
19306 parm = TREE_VALUE (parms);
19307
19308 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19309 && (!TREE_CHAIN (parms)
19310 || TREE_CHAIN (parms) == void_list_node))
19311 /* For a function parameter pack that occurs at the end of the
19312 parameter-declaration-list, the type A of each remaining
19313 argument of the call is compared with the type P of the
19314 declarator-id of the function parameter pack. */
19315 break;
19316
19317 parms = TREE_CHAIN (parms);
19318
19319 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19320 /* For a function parameter pack that does not occur at the
19321 end of the parameter-declaration-list, the type of the
19322 parameter pack is a non-deduced context. */
19323 continue;
19324
19325 arg = args[ia];
19326 ++ia;
19327
19328 if (uses_template_parms (parm))
19329 continue;
19330 if (check_non_deducible_conversion (parm, arg, strict, flags,
19331 explain_p))
19332 return 1;
19333 }
19334
19335 /* Now substitute into the default template arguments. */
19336 for (i = 0; i < ntparms; i++)
19337 {
19338 tree targ = TREE_VEC_ELT (targs, i);
19339 tree tparm = TREE_VEC_ELT (tparms, i);
19340
19341 if (targ || tparm == error_mark_node)
19342 continue;
19343 tree parm = TREE_VALUE (tparm);
19344
19345 if (TREE_CODE (parm) == PARM_DECL
19346 && uses_template_parms (TREE_TYPE (parm))
19347 && saw_undeduced < 2)
19348 continue;
19349
19350 tree arg = TREE_PURPOSE (tparm);
19351 reopen_deferring_access_checks (*checks);
19352 location_t save_loc = input_location;
19353 if (DECL_P (parm))
19354 input_location = DECL_SOURCE_LOCATION (parm);
19355 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19356 if (!uses_template_parms (arg))
19357 arg = convert_template_argument (parm, arg, full_targs, complain,
19358 i, NULL_TREE);
19359 else if (saw_undeduced < 2)
19360 arg = NULL_TREE;
19361 else
19362 arg = error_mark_node;
19363 input_location = save_loc;
19364 *checks = get_deferred_access_checks ();
19365 pop_deferring_access_checks ();
19366 if (arg == error_mark_node)
19367 return 1;
19368 else if (arg)
19369 {
19370 TREE_VEC_ELT (targs, i) = arg;
19371 /* The position of the first default template argument,
19372 is also the number of non-defaulted arguments in TARGS.
19373 Record that. */
19374 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19375 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19376 }
19377 }
19378
19379 if (saw_undeduced++ == 1)
19380 goto again;
19381 }
19382
19383 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19384 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19385
19386 return unify_success (explain_p);
19387 }
19388
19389 /* Subroutine of type_unification_real. Args are like the variables
19390 at the call site. ARG is an overloaded function (or template-id);
19391 we try deducing template args from each of the overloads, and if
19392 only one succeeds, we go with that. Modifies TARGS and returns
19393 true on success. */
19394
19395 static bool
19396 resolve_overloaded_unification (tree tparms,
19397 tree targs,
19398 tree parm,
19399 tree arg,
19400 unification_kind_t strict,
19401 int sub_strict,
19402 bool explain_p)
19403 {
19404 tree tempargs = copy_node (targs);
19405 int good = 0;
19406 tree goodfn = NULL_TREE;
19407 bool addr_p;
19408
19409 if (TREE_CODE (arg) == ADDR_EXPR)
19410 {
19411 arg = TREE_OPERAND (arg, 0);
19412 addr_p = true;
19413 }
19414 else
19415 addr_p = false;
19416
19417 if (TREE_CODE (arg) == COMPONENT_REF)
19418 /* Handle `&x' where `x' is some static or non-static member
19419 function name. */
19420 arg = TREE_OPERAND (arg, 1);
19421
19422 if (TREE_CODE (arg) == OFFSET_REF)
19423 arg = TREE_OPERAND (arg, 1);
19424
19425 /* Strip baselink information. */
19426 if (BASELINK_P (arg))
19427 arg = BASELINK_FUNCTIONS (arg);
19428
19429 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19430 {
19431 /* If we got some explicit template args, we need to plug them into
19432 the affected templates before we try to unify, in case the
19433 explicit args will completely resolve the templates in question. */
19434
19435 int ok = 0;
19436 tree expl_subargs = TREE_OPERAND (arg, 1);
19437 arg = TREE_OPERAND (arg, 0);
19438
19439 for (; arg; arg = OVL_NEXT (arg))
19440 {
19441 tree fn = OVL_CURRENT (arg);
19442 tree subargs, elem;
19443
19444 if (TREE_CODE (fn) != TEMPLATE_DECL)
19445 continue;
19446
19447 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19448 expl_subargs, NULL_TREE, tf_none,
19449 /*require_all_args=*/true,
19450 /*use_default_args=*/true);
19451 if (subargs != error_mark_node
19452 && !any_dependent_template_arguments_p (subargs))
19453 {
19454 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19455 if (try_one_overload (tparms, targs, tempargs, parm,
19456 elem, strict, sub_strict, addr_p, explain_p)
19457 && (!goodfn || !same_type_p (goodfn, elem)))
19458 {
19459 goodfn = elem;
19460 ++good;
19461 }
19462 }
19463 else if (subargs)
19464 ++ok;
19465 }
19466 /* If no templates (or more than one) are fully resolved by the
19467 explicit arguments, this template-id is a non-deduced context; it
19468 could still be OK if we deduce all template arguments for the
19469 enclosing call through other arguments. */
19470 if (good != 1)
19471 good = ok;
19472 }
19473 else if (TREE_CODE (arg) != OVERLOAD
19474 && TREE_CODE (arg) != FUNCTION_DECL)
19475 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19476 -- but the deduction does not succeed because the expression is
19477 not just the function on its own. */
19478 return false;
19479 else
19480 for (; arg; arg = OVL_NEXT (arg))
19481 if (try_one_overload (tparms, targs, tempargs, parm,
19482 TREE_TYPE (OVL_CURRENT (arg)),
19483 strict, sub_strict, addr_p, explain_p)
19484 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
19485 {
19486 goodfn = OVL_CURRENT (arg);
19487 ++good;
19488 }
19489
19490 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19491 to function or pointer to member function argument if the set of
19492 overloaded functions does not contain function templates and at most
19493 one of a set of overloaded functions provides a unique match.
19494
19495 So if we found multiple possibilities, we return success but don't
19496 deduce anything. */
19497
19498 if (good == 1)
19499 {
19500 int i = TREE_VEC_LENGTH (targs);
19501 for (; i--; )
19502 if (TREE_VEC_ELT (tempargs, i))
19503 {
19504 tree old = TREE_VEC_ELT (targs, i);
19505 tree new_ = TREE_VEC_ELT (tempargs, i);
19506 if (new_ && old && ARGUMENT_PACK_P (old)
19507 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19508 /* Don't forget explicit template arguments in a pack. */
19509 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19510 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19511 TREE_VEC_ELT (targs, i) = new_;
19512 }
19513 }
19514 if (good)
19515 return true;
19516
19517 return false;
19518 }
19519
19520 /* Core DR 115: In contexts where deduction is done and fails, or in
19521 contexts where deduction is not done, if a template argument list is
19522 specified and it, along with any default template arguments, identifies
19523 a single function template specialization, then the template-id is an
19524 lvalue for the function template specialization. */
19525
19526 tree
19527 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19528 {
19529 tree expr, offset, baselink;
19530 bool addr;
19531
19532 if (!type_unknown_p (orig_expr))
19533 return orig_expr;
19534
19535 expr = orig_expr;
19536 addr = false;
19537 offset = NULL_TREE;
19538 baselink = NULL_TREE;
19539
19540 if (TREE_CODE (expr) == ADDR_EXPR)
19541 {
19542 expr = TREE_OPERAND (expr, 0);
19543 addr = true;
19544 }
19545 if (TREE_CODE (expr) == OFFSET_REF)
19546 {
19547 offset = expr;
19548 expr = TREE_OPERAND (expr, 1);
19549 }
19550 if (BASELINK_P (expr))
19551 {
19552 baselink = expr;
19553 expr = BASELINK_FUNCTIONS (expr);
19554 }
19555
19556 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19557 {
19558 int good = 0;
19559 tree goodfn = NULL_TREE;
19560
19561 /* If we got some explicit template args, we need to plug them into
19562 the affected templates before we try to unify, in case the
19563 explicit args will completely resolve the templates in question. */
19564
19565 tree expl_subargs = TREE_OPERAND (expr, 1);
19566 tree arg = TREE_OPERAND (expr, 0);
19567 tree badfn = NULL_TREE;
19568 tree badargs = NULL_TREE;
19569
19570 for (; arg; arg = OVL_NEXT (arg))
19571 {
19572 tree fn = OVL_CURRENT (arg);
19573 tree subargs, elem;
19574
19575 if (TREE_CODE (fn) != TEMPLATE_DECL)
19576 continue;
19577
19578 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19579 expl_subargs, NULL_TREE, tf_none,
19580 /*require_all_args=*/true,
19581 /*use_default_args=*/true);
19582 if (subargs != error_mark_node
19583 && !any_dependent_template_arguments_p (subargs))
19584 {
19585 elem = instantiate_template (fn, subargs, tf_none);
19586 if (elem == error_mark_node)
19587 {
19588 badfn = fn;
19589 badargs = subargs;
19590 }
19591 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19592 {
19593 goodfn = elem;
19594 ++good;
19595 }
19596 }
19597 }
19598 if (good == 1)
19599 {
19600 mark_used (goodfn);
19601 expr = goodfn;
19602 if (baselink)
19603 expr = build_baselink (BASELINK_BINFO (baselink),
19604 BASELINK_ACCESS_BINFO (baselink),
19605 expr, BASELINK_OPTYPE (baselink));
19606 if (offset)
19607 {
19608 tree base
19609 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19610 expr = build_offset_ref (base, expr, addr, complain);
19611 }
19612 if (addr)
19613 expr = cp_build_addr_expr (expr, complain);
19614 return expr;
19615 }
19616 else if (good == 0 && badargs && (complain & tf_error))
19617 /* There were no good options and at least one bad one, so let the
19618 user know what the problem is. */
19619 instantiate_template (badfn, badargs, complain);
19620 }
19621 return orig_expr;
19622 }
19623
19624 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19625 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19626 different overloads deduce different arguments for a given parm.
19627 ADDR_P is true if the expression for which deduction is being
19628 performed was of the form "& fn" rather than simply "fn".
19629
19630 Returns 1 on success. */
19631
19632 static int
19633 try_one_overload (tree tparms,
19634 tree orig_targs,
19635 tree targs,
19636 tree parm,
19637 tree arg,
19638 unification_kind_t strict,
19639 int sub_strict,
19640 bool addr_p,
19641 bool explain_p)
19642 {
19643 int nargs;
19644 tree tempargs;
19645 int i;
19646
19647 if (arg == error_mark_node)
19648 return 0;
19649
19650 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19651 to function or pointer to member function argument if the set of
19652 overloaded functions does not contain function templates and at most
19653 one of a set of overloaded functions provides a unique match.
19654
19655 So if this is a template, just return success. */
19656
19657 if (uses_template_parms (arg))
19658 return 1;
19659
19660 if (TREE_CODE (arg) == METHOD_TYPE)
19661 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19662 else if (addr_p)
19663 arg = build_pointer_type (arg);
19664
19665 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19666
19667 /* We don't copy orig_targs for this because if we have already deduced
19668 some template args from previous args, unify would complain when we
19669 try to deduce a template parameter for the same argument, even though
19670 there isn't really a conflict. */
19671 nargs = TREE_VEC_LENGTH (targs);
19672 tempargs = make_tree_vec (nargs);
19673
19674 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19675 return 0;
19676
19677 /* First make sure we didn't deduce anything that conflicts with
19678 explicitly specified args. */
19679 for (i = nargs; i--; )
19680 {
19681 tree elt = TREE_VEC_ELT (tempargs, i);
19682 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19683
19684 if (!elt)
19685 /*NOP*/;
19686 else if (uses_template_parms (elt))
19687 /* Since we're unifying against ourselves, we will fill in
19688 template args used in the function parm list with our own
19689 template parms. Discard them. */
19690 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19691 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19692 {
19693 /* Check that the argument at each index of the deduced argument pack
19694 is equivalent to the corresponding explicitly specified argument.
19695 We may have deduced more arguments than were explicitly specified,
19696 and that's OK. */
19697
19698 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19699 that's wrong if we deduce the same argument pack from multiple
19700 function arguments: it's only incomplete the first time. */
19701
19702 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19703 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19704
19705 if (TREE_VEC_LENGTH (deduced_pack)
19706 < TREE_VEC_LENGTH (explicit_pack))
19707 return 0;
19708
19709 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19710 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19711 TREE_VEC_ELT (deduced_pack, j)))
19712 return 0;
19713 }
19714 else if (oldelt && !template_args_equal (oldelt, elt))
19715 return 0;
19716 }
19717
19718 for (i = nargs; i--; )
19719 {
19720 tree elt = TREE_VEC_ELT (tempargs, i);
19721
19722 if (elt)
19723 TREE_VEC_ELT (targs, i) = elt;
19724 }
19725
19726 return 1;
19727 }
19728
19729 /* PARM is a template class (perhaps with unbound template
19730 parameters). ARG is a fully instantiated type. If ARG can be
19731 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19732 TARGS are as for unify. */
19733
19734 static tree
19735 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19736 bool explain_p)
19737 {
19738 tree copy_of_targs;
19739
19740 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19741 return NULL_TREE;
19742 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19743 /* Matches anything. */;
19744 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19745 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19746 return NULL_TREE;
19747
19748 /* We need to make a new template argument vector for the call to
19749 unify. If we used TARGS, we'd clutter it up with the result of
19750 the attempted unification, even if this class didn't work out.
19751 We also don't want to commit ourselves to all the unifications
19752 we've already done, since unification is supposed to be done on
19753 an argument-by-argument basis. In other words, consider the
19754 following pathological case:
19755
19756 template <int I, int J, int K>
19757 struct S {};
19758
19759 template <int I, int J>
19760 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19761
19762 template <int I, int J, int K>
19763 void f(S<I, J, K>, S<I, I, I>);
19764
19765 void g() {
19766 S<0, 0, 0> s0;
19767 S<0, 1, 2> s2;
19768
19769 f(s0, s2);
19770 }
19771
19772 Now, by the time we consider the unification involving `s2', we
19773 already know that we must have `f<0, 0, 0>'. But, even though
19774 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19775 because there are two ways to unify base classes of S<0, 1, 2>
19776 with S<I, I, I>. If we kept the already deduced knowledge, we
19777 would reject the possibility I=1. */
19778 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19779
19780 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19781 {
19782 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19783 return NULL_TREE;
19784 return arg;
19785 }
19786
19787 /* If unification failed, we're done. */
19788 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19789 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19790 return NULL_TREE;
19791
19792 return arg;
19793 }
19794
19795 /* Given a template type PARM and a class type ARG, find the unique
19796 base type in ARG that is an instance of PARM. We do not examine
19797 ARG itself; only its base-classes. If there is not exactly one
19798 appropriate base class, return NULL_TREE. PARM may be the type of
19799 a partial specialization, as well as a plain template type. Used
19800 by unify. */
19801
19802 static enum template_base_result
19803 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19804 bool explain_p, tree *result)
19805 {
19806 tree rval = NULL_TREE;
19807 tree binfo;
19808
19809 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19810
19811 binfo = TYPE_BINFO (complete_type (arg));
19812 if (!binfo)
19813 {
19814 /* The type could not be completed. */
19815 *result = NULL_TREE;
19816 return tbr_incomplete_type;
19817 }
19818
19819 /* Walk in inheritance graph order. The search order is not
19820 important, and this avoids multiple walks of virtual bases. */
19821 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19822 {
19823 tree r = try_class_unification (tparms, targs, parm,
19824 BINFO_TYPE (binfo), explain_p);
19825
19826 if (r)
19827 {
19828 /* If there is more than one satisfactory baseclass, then:
19829
19830 [temp.deduct.call]
19831
19832 If they yield more than one possible deduced A, the type
19833 deduction fails.
19834
19835 applies. */
19836 if (rval && !same_type_p (r, rval))
19837 {
19838 *result = NULL_TREE;
19839 return tbr_ambiguous_baseclass;
19840 }
19841
19842 rval = r;
19843 }
19844 }
19845
19846 *result = rval;
19847 return tbr_success;
19848 }
19849
19850 /* Returns the level of DECL, which declares a template parameter. */
19851
19852 static int
19853 template_decl_level (tree decl)
19854 {
19855 switch (TREE_CODE (decl))
19856 {
19857 case TYPE_DECL:
19858 case TEMPLATE_DECL:
19859 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19860
19861 case PARM_DECL:
19862 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19863
19864 default:
19865 gcc_unreachable ();
19866 }
19867 return 0;
19868 }
19869
19870 /* Decide whether ARG can be unified with PARM, considering only the
19871 cv-qualifiers of each type, given STRICT as documented for unify.
19872 Returns nonzero iff the unification is OK on that basis. */
19873
19874 static int
19875 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19876 {
19877 int arg_quals = cp_type_quals (arg);
19878 int parm_quals = cp_type_quals (parm);
19879
19880 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19881 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19882 {
19883 /* Although a CVR qualifier is ignored when being applied to a
19884 substituted template parameter ([8.3.2]/1 for example), that
19885 does not allow us to unify "const T" with "int&" because both
19886 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19887 It is ok when we're allowing additional CV qualifiers
19888 at the outer level [14.8.2.1]/3,1st bullet. */
19889 if ((TREE_CODE (arg) == REFERENCE_TYPE
19890 || TREE_CODE (arg) == FUNCTION_TYPE
19891 || TREE_CODE (arg) == METHOD_TYPE)
19892 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19893 return 0;
19894
19895 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19896 && (parm_quals & TYPE_QUAL_RESTRICT))
19897 return 0;
19898 }
19899
19900 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19901 && (arg_quals & parm_quals) != parm_quals)
19902 return 0;
19903
19904 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19905 && (parm_quals & arg_quals) != arg_quals)
19906 return 0;
19907
19908 return 1;
19909 }
19910
19911 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19912 void
19913 template_parm_level_and_index (tree parm, int* level, int* index)
19914 {
19915 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19916 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19917 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19918 {
19919 *index = TEMPLATE_TYPE_IDX (parm);
19920 *level = TEMPLATE_TYPE_LEVEL (parm);
19921 }
19922 else
19923 {
19924 *index = TEMPLATE_PARM_IDX (parm);
19925 *level = TEMPLATE_PARM_LEVEL (parm);
19926 }
19927 }
19928
19929 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19930 do { \
19931 if (unify (TP, TA, P, A, S, EP)) \
19932 return 1; \
19933 } while (0)
19934
19935 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19936 expansion at the end of PACKED_PARMS. Returns 0 if the type
19937 deduction succeeds, 1 otherwise. STRICT is the same as in
19938 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19939 function call argument list. We'll need to adjust the arguments to make them
19940 types. SUBR tells us if this is from a recursive call to
19941 type_unification_real, or for comparing two template argument
19942 lists. */
19943
19944 static int
19945 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19946 tree packed_args, unification_kind_t strict,
19947 bool subr, bool explain_p)
19948 {
19949 tree parm
19950 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19951 tree pattern = PACK_EXPANSION_PATTERN (parm);
19952 tree pack, packs = NULL_TREE;
19953 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19954
19955 packed_args = expand_template_argument_pack (packed_args);
19956
19957 int len = TREE_VEC_LENGTH (packed_args);
19958
19959 /* Determine the parameter packs we will be deducing from the
19960 pattern, and record their current deductions. */
19961 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19962 pack; pack = TREE_CHAIN (pack))
19963 {
19964 tree parm_pack = TREE_VALUE (pack);
19965 int idx, level;
19966
19967 /* Determine the index and level of this parameter pack. */
19968 template_parm_level_and_index (parm_pack, &level, &idx);
19969
19970 /* Keep track of the parameter packs and their corresponding
19971 argument packs. */
19972 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19973 TREE_TYPE (packs) = make_tree_vec (len - start);
19974 }
19975
19976 /* Loop through all of the arguments that have not yet been
19977 unified and unify each with the pattern. */
19978 for (i = start; i < len; i++)
19979 {
19980 tree parm;
19981 bool any_explicit = false;
19982 tree arg = TREE_VEC_ELT (packed_args, i);
19983
19984 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19985 or the element of its argument pack at the current index if
19986 this argument was explicitly specified. */
19987 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19988 {
19989 int idx, level;
19990 tree arg, pargs;
19991 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19992
19993 arg = NULL_TREE;
19994 if (TREE_VALUE (pack)
19995 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19996 && (i - start < TREE_VEC_LENGTH (pargs)))
19997 {
19998 any_explicit = true;
19999 arg = TREE_VEC_ELT (pargs, i - start);
20000 }
20001 TMPL_ARG (targs, level, idx) = arg;
20002 }
20003
20004 /* If we had explicit template arguments, substitute them into the
20005 pattern before deduction. */
20006 if (any_explicit)
20007 {
20008 /* Some arguments might still be unspecified or dependent. */
20009 bool dependent;
20010 ++processing_template_decl;
20011 dependent = any_dependent_template_arguments_p (targs);
20012 if (!dependent)
20013 --processing_template_decl;
20014 parm = tsubst (pattern, targs,
20015 explain_p ? tf_warning_or_error : tf_none,
20016 NULL_TREE);
20017 if (dependent)
20018 --processing_template_decl;
20019 if (parm == error_mark_node)
20020 return 1;
20021 }
20022 else
20023 parm = pattern;
20024
20025 /* Unify the pattern with the current argument. */
20026 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20027 explain_p))
20028 return 1;
20029
20030 /* For each parameter pack, collect the deduced value. */
20031 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20032 {
20033 int idx, level;
20034 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20035
20036 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20037 TMPL_ARG (targs, level, idx);
20038 }
20039 }
20040
20041 /* Verify that the results of unification with the parameter packs
20042 produce results consistent with what we've seen before, and make
20043 the deduced argument packs available. */
20044 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20045 {
20046 tree old_pack = TREE_VALUE (pack);
20047 tree new_args = TREE_TYPE (pack);
20048 int i, len = TREE_VEC_LENGTH (new_args);
20049 int idx, level;
20050 bool nondeduced_p = false;
20051
20052 /* By default keep the original deduced argument pack.
20053 If necessary, more specific code is going to update the
20054 resulting deduced argument later down in this function. */
20055 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20056 TMPL_ARG (targs, level, idx) = old_pack;
20057
20058 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20059 actually deduce anything. */
20060 for (i = 0; i < len && !nondeduced_p; ++i)
20061 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20062 nondeduced_p = true;
20063 if (nondeduced_p)
20064 continue;
20065
20066 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20067 {
20068 /* If we had fewer function args than explicit template args,
20069 just use the explicits. */
20070 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20071 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20072 if (len < explicit_len)
20073 new_args = explicit_args;
20074 }
20075
20076 if (!old_pack)
20077 {
20078 tree result;
20079 /* Build the deduced *_ARGUMENT_PACK. */
20080 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20081 {
20082 result = make_node (NONTYPE_ARGUMENT_PACK);
20083 TREE_TYPE (result) =
20084 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
20085 TREE_CONSTANT (result) = 1;
20086 }
20087 else
20088 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20089
20090 SET_ARGUMENT_PACK_ARGS (result, new_args);
20091
20092 /* Note the deduced argument packs for this parameter
20093 pack. */
20094 TMPL_ARG (targs, level, idx) = result;
20095 }
20096 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20097 && (ARGUMENT_PACK_ARGS (old_pack)
20098 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20099 {
20100 /* We only had the explicitly-provided arguments before, but
20101 now we have a complete set of arguments. */
20102 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20103
20104 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20105 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20106 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20107 }
20108 else
20109 {
20110 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20111 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20112
20113 if (!comp_template_args (old_args, new_args,
20114 &bad_old_arg, &bad_new_arg))
20115 /* Inconsistent unification of this parameter pack. */
20116 return unify_parameter_pack_inconsistent (explain_p,
20117 bad_old_arg,
20118 bad_new_arg);
20119 }
20120 }
20121
20122 return unify_success (explain_p);
20123 }
20124
20125 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20126 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20127 parameters and return value are as for unify. */
20128
20129 static int
20130 unify_array_domain (tree tparms, tree targs,
20131 tree parm_dom, tree arg_dom,
20132 bool explain_p)
20133 {
20134 tree parm_max;
20135 tree arg_max;
20136 bool parm_cst;
20137 bool arg_cst;
20138
20139 /* Our representation of array types uses "N - 1" as the
20140 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20141 not an integer constant. We cannot unify arbitrarily
20142 complex expressions, so we eliminate the MINUS_EXPRs
20143 here. */
20144 parm_max = TYPE_MAX_VALUE (parm_dom);
20145 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20146 if (!parm_cst)
20147 {
20148 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20149 parm_max = TREE_OPERAND (parm_max, 0);
20150 }
20151 arg_max = TYPE_MAX_VALUE (arg_dom);
20152 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20153 if (!arg_cst)
20154 {
20155 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20156 trying to unify the type of a variable with the type
20157 of a template parameter. For example:
20158
20159 template <unsigned int N>
20160 void f (char (&) [N]);
20161 int g();
20162 void h(int i) {
20163 char a[g(i)];
20164 f(a);
20165 }
20166
20167 Here, the type of the ARG will be "int [g(i)]", and
20168 may be a SAVE_EXPR, etc. */
20169 if (TREE_CODE (arg_max) != MINUS_EXPR)
20170 return unify_vla_arg (explain_p, arg_dom);
20171 arg_max = TREE_OPERAND (arg_max, 0);
20172 }
20173
20174 /* If only one of the bounds used a MINUS_EXPR, compensate
20175 by adding one to the other bound. */
20176 if (parm_cst && !arg_cst)
20177 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20178 integer_type_node,
20179 parm_max,
20180 integer_one_node);
20181 else if (arg_cst && !parm_cst)
20182 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20183 integer_type_node,
20184 arg_max,
20185 integer_one_node);
20186
20187 return unify (tparms, targs, parm_max, arg_max,
20188 UNIFY_ALLOW_INTEGER, explain_p);
20189 }
20190
20191 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20192
20193 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20194
20195 static pa_kind_t
20196 pa_kind (tree t)
20197 {
20198 if (PACK_EXPANSION_P (t))
20199 t = PACK_EXPANSION_PATTERN (t);
20200 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20201 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20202 || DECL_TYPE_TEMPLATE_P (t))
20203 return pa_tmpl;
20204 else if (TYPE_P (t))
20205 return pa_type;
20206 else
20207 return pa_expr;
20208 }
20209
20210 /* Deduce the value of template parameters. TPARMS is the (innermost)
20211 set of template parameters to a template. TARGS is the bindings
20212 for those template parameters, as determined thus far; TARGS may
20213 include template arguments for outer levels of template parameters
20214 as well. PARM is a parameter to a template function, or a
20215 subcomponent of that parameter; ARG is the corresponding argument.
20216 This function attempts to match PARM with ARG in a manner
20217 consistent with the existing assignments in TARGS. If more values
20218 are deduced, then TARGS is updated.
20219
20220 Returns 0 if the type deduction succeeds, 1 otherwise. The
20221 parameter STRICT is a bitwise or of the following flags:
20222
20223 UNIFY_ALLOW_NONE:
20224 Require an exact match between PARM and ARG.
20225 UNIFY_ALLOW_MORE_CV_QUAL:
20226 Allow the deduced ARG to be more cv-qualified (by qualification
20227 conversion) than ARG.
20228 UNIFY_ALLOW_LESS_CV_QUAL:
20229 Allow the deduced ARG to be less cv-qualified than ARG.
20230 UNIFY_ALLOW_DERIVED:
20231 Allow the deduced ARG to be a template base class of ARG,
20232 or a pointer to a template base class of the type pointed to by
20233 ARG.
20234 UNIFY_ALLOW_INTEGER:
20235 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20236 case for more information.
20237 UNIFY_ALLOW_OUTER_LEVEL:
20238 This is the outermost level of a deduction. Used to determine validity
20239 of qualification conversions. A valid qualification conversion must
20240 have const qualified pointers leading up to the inner type which
20241 requires additional CV quals, except at the outer level, where const
20242 is not required [conv.qual]. It would be normal to set this flag in
20243 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20244 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20245 This is the outermost level of a deduction, and PARM can be more CV
20246 qualified at this point.
20247 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20248 This is the outermost level of a deduction, and PARM can be less CV
20249 qualified at this point. */
20250
20251 static int
20252 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20253 bool explain_p)
20254 {
20255 int idx;
20256 tree targ;
20257 tree tparm;
20258 int strict_in = strict;
20259 tsubst_flags_t complain = (explain_p
20260 ? tf_warning_or_error
20261 : tf_none);
20262
20263 /* I don't think this will do the right thing with respect to types.
20264 But the only case I've seen it in so far has been array bounds, where
20265 signedness is the only information lost, and I think that will be
20266 okay. */
20267 while (CONVERT_EXPR_P (parm))
20268 parm = TREE_OPERAND (parm, 0);
20269
20270 if (arg == error_mark_node)
20271 return unify_invalid (explain_p);
20272 if (arg == unknown_type_node
20273 || arg == init_list_type_node)
20274 /* We can't deduce anything from this, but we might get all the
20275 template args from other function args. */
20276 return unify_success (explain_p);
20277
20278 if (parm == any_targ_node || arg == any_targ_node)
20279 return unify_success (explain_p);
20280
20281 /* If PARM uses template parameters, then we can't bail out here,
20282 even if ARG == PARM, since we won't record unifications for the
20283 template parameters. We might need them if we're trying to
20284 figure out which of two things is more specialized. */
20285 if (arg == parm && !uses_template_parms (parm))
20286 return unify_success (explain_p);
20287
20288 /* Handle init lists early, so the rest of the function can assume
20289 we're dealing with a type. */
20290 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20291 {
20292 tree elt, elttype;
20293 unsigned i;
20294 tree orig_parm = parm;
20295
20296 /* Replace T with std::initializer_list<T> for deduction. */
20297 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20298 && flag_deduce_init_list)
20299 parm = listify (parm);
20300
20301 if (!is_std_init_list (parm)
20302 && TREE_CODE (parm) != ARRAY_TYPE)
20303 /* We can only deduce from an initializer list argument if the
20304 parameter is std::initializer_list or an array; otherwise this
20305 is a non-deduced context. */
20306 return unify_success (explain_p);
20307
20308 if (TREE_CODE (parm) == ARRAY_TYPE)
20309 elttype = TREE_TYPE (parm);
20310 else
20311 {
20312 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20313 /* Deduction is defined in terms of a single type, so just punt
20314 on the (bizarre) std::initializer_list<T...>. */
20315 if (PACK_EXPANSION_P (elttype))
20316 return unify_success (explain_p);
20317 }
20318
20319 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20320 {
20321 int elt_strict = strict;
20322
20323 if (elt == error_mark_node)
20324 return unify_invalid (explain_p);
20325
20326 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20327 {
20328 tree type = TREE_TYPE (elt);
20329 if (type == error_mark_node)
20330 return unify_invalid (explain_p);
20331 /* It should only be possible to get here for a call. */
20332 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20333 elt_strict |= maybe_adjust_types_for_deduction
20334 (DEDUCE_CALL, &elttype, &type, elt);
20335 elt = type;
20336 }
20337
20338 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20339 explain_p);
20340 }
20341
20342 if (TREE_CODE (parm) == ARRAY_TYPE
20343 && deducible_array_bound (TYPE_DOMAIN (parm)))
20344 {
20345 /* Also deduce from the length of the initializer list. */
20346 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20347 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20348 if (idx == error_mark_node)
20349 return unify_invalid (explain_p);
20350 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20351 idx, explain_p);
20352 }
20353
20354 /* If the std::initializer_list<T> deduction worked, replace the
20355 deduced A with std::initializer_list<A>. */
20356 if (orig_parm != parm)
20357 {
20358 idx = TEMPLATE_TYPE_IDX (orig_parm);
20359 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20360 targ = listify (targ);
20361 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20362 }
20363 return unify_success (explain_p);
20364 }
20365
20366 /* If parm and arg aren't the same kind of thing (template, type, or
20367 expression), fail early. */
20368 if (pa_kind (parm) != pa_kind (arg))
20369 return unify_invalid (explain_p);
20370
20371 /* Immediately reject some pairs that won't unify because of
20372 cv-qualification mismatches. */
20373 if (TREE_CODE (arg) == TREE_CODE (parm)
20374 && TYPE_P (arg)
20375 /* It is the elements of the array which hold the cv quals of an array
20376 type, and the elements might be template type parms. We'll check
20377 when we recurse. */
20378 && TREE_CODE (arg) != ARRAY_TYPE
20379 /* We check the cv-qualifiers when unifying with template type
20380 parameters below. We want to allow ARG `const T' to unify with
20381 PARM `T' for example, when computing which of two templates
20382 is more specialized, for example. */
20383 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20384 && !check_cv_quals_for_unify (strict_in, arg, parm))
20385 return unify_cv_qual_mismatch (explain_p, parm, arg);
20386
20387 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20388 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20389 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20390 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20391 strict &= ~UNIFY_ALLOW_DERIVED;
20392 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20393 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20394
20395 switch (TREE_CODE (parm))
20396 {
20397 case TYPENAME_TYPE:
20398 case SCOPE_REF:
20399 case UNBOUND_CLASS_TEMPLATE:
20400 /* In a type which contains a nested-name-specifier, template
20401 argument values cannot be deduced for template parameters used
20402 within the nested-name-specifier. */
20403 return unify_success (explain_p);
20404
20405 case TEMPLATE_TYPE_PARM:
20406 case TEMPLATE_TEMPLATE_PARM:
20407 case BOUND_TEMPLATE_TEMPLATE_PARM:
20408 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20409 if (error_operand_p (tparm))
20410 return unify_invalid (explain_p);
20411
20412 if (TEMPLATE_TYPE_LEVEL (parm)
20413 != template_decl_level (tparm))
20414 /* The PARM is not one we're trying to unify. Just check
20415 to see if it matches ARG. */
20416 {
20417 if (TREE_CODE (arg) == TREE_CODE (parm)
20418 && (is_auto (parm) ? is_auto (arg)
20419 : same_type_p (parm, arg)))
20420 return unify_success (explain_p);
20421 else
20422 return unify_type_mismatch (explain_p, parm, arg);
20423 }
20424 idx = TEMPLATE_TYPE_IDX (parm);
20425 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20426 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20427 if (error_operand_p (tparm))
20428 return unify_invalid (explain_p);
20429
20430 /* Check for mixed types and values. */
20431 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20432 && TREE_CODE (tparm) != TYPE_DECL)
20433 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20434 && TREE_CODE (tparm) != TEMPLATE_DECL))
20435 gcc_unreachable ();
20436
20437 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20438 {
20439 if ((strict_in & UNIFY_ALLOW_DERIVED)
20440 && CLASS_TYPE_P (arg))
20441 {
20442 /* First try to match ARG directly. */
20443 tree t = try_class_unification (tparms, targs, parm, arg,
20444 explain_p);
20445 if (!t)
20446 {
20447 /* Otherwise, look for a suitable base of ARG, as below. */
20448 enum template_base_result r;
20449 r = get_template_base (tparms, targs, parm, arg,
20450 explain_p, &t);
20451 if (!t)
20452 return unify_no_common_base (explain_p, r, parm, arg);
20453 arg = t;
20454 }
20455 }
20456 /* ARG must be constructed from a template class or a template
20457 template parameter. */
20458 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20459 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20460 return unify_template_deduction_failure (explain_p, parm, arg);
20461
20462 /* Deduce arguments T, i from TT<T> or TT<i>. */
20463 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20464 return 1;
20465
20466 arg = TYPE_TI_TEMPLATE (arg);
20467
20468 /* Fall through to deduce template name. */
20469 }
20470
20471 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20472 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20473 {
20474 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20475
20476 /* Simple cases: Value already set, does match or doesn't. */
20477 if (targ != NULL_TREE && template_args_equal (targ, arg))
20478 return unify_success (explain_p);
20479 else if (targ)
20480 return unify_inconsistency (explain_p, parm, targ, arg);
20481 }
20482 else
20483 {
20484 /* If PARM is `const T' and ARG is only `int', we don't have
20485 a match unless we are allowing additional qualification.
20486 If ARG is `const int' and PARM is just `T' that's OK;
20487 that binds `const int' to `T'. */
20488 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20489 arg, parm))
20490 return unify_cv_qual_mismatch (explain_p, parm, arg);
20491
20492 /* Consider the case where ARG is `const volatile int' and
20493 PARM is `const T'. Then, T should be `volatile int'. */
20494 arg = cp_build_qualified_type_real
20495 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20496 if (arg == error_mark_node)
20497 return unify_invalid (explain_p);
20498
20499 /* Simple cases: Value already set, does match or doesn't. */
20500 if (targ != NULL_TREE && same_type_p (targ, arg))
20501 return unify_success (explain_p);
20502 else if (targ)
20503 return unify_inconsistency (explain_p, parm, targ, arg);
20504
20505 /* Make sure that ARG is not a variable-sized array. (Note
20506 that were talking about variable-sized arrays (like
20507 `int[n]'), rather than arrays of unknown size (like
20508 `int[]').) We'll get very confused by such a type since
20509 the bound of the array is not constant, and therefore
20510 not mangleable. Besides, such types are not allowed in
20511 ISO C++, so we can do as we please here. We do allow
20512 them for 'auto' deduction, since that isn't ABI-exposed. */
20513 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20514 return unify_vla_arg (explain_p, arg);
20515
20516 /* Strip typedefs as in convert_template_argument. */
20517 arg = canonicalize_type_argument (arg, tf_none);
20518 }
20519
20520 /* If ARG is a parameter pack or an expansion, we cannot unify
20521 against it unless PARM is also a parameter pack. */
20522 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20523 && !template_parameter_pack_p (parm))
20524 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20525
20526 /* If the argument deduction results is a METHOD_TYPE,
20527 then there is a problem.
20528 METHOD_TYPE doesn't map to any real C++ type the result of
20529 the deduction can not be of that type. */
20530 if (TREE_CODE (arg) == METHOD_TYPE)
20531 return unify_method_type_error (explain_p, arg);
20532
20533 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20534 return unify_success (explain_p);
20535
20536 case TEMPLATE_PARM_INDEX:
20537 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20538 if (error_operand_p (tparm))
20539 return unify_invalid (explain_p);
20540
20541 if (TEMPLATE_PARM_LEVEL (parm)
20542 != template_decl_level (tparm))
20543 {
20544 /* The PARM is not one we're trying to unify. Just check
20545 to see if it matches ARG. */
20546 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20547 && cp_tree_equal (parm, arg));
20548 if (result)
20549 unify_expression_unequal (explain_p, parm, arg);
20550 return result;
20551 }
20552
20553 idx = TEMPLATE_PARM_IDX (parm);
20554 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20555
20556 if (targ)
20557 {
20558 int x = !cp_tree_equal (targ, arg);
20559 if (x)
20560 unify_inconsistency (explain_p, parm, targ, arg);
20561 return x;
20562 }
20563
20564 if (cxx_dialect >= cxx1z
20565 /* We deduce from array bounds in try_array_deduction. */
20566 && !(strict & UNIFY_ALLOW_INTEGER)
20567 && uses_template_parms (TREE_TYPE (parm))
20568 && !type_uses_auto (TREE_TYPE (parm)))
20569 {
20570 tree atype = TREE_TYPE (arg);
20571 RECUR_AND_CHECK_FAILURE (tparms, targs,
20572 TREE_TYPE (parm), atype,
20573 UNIFY_ALLOW_NONE, explain_p);
20574 }
20575
20576 /* [temp.deduct.type] If, in the declaration of a function template
20577 with a non-type template-parameter, the non-type
20578 template-parameter is used in an expression in the function
20579 parameter-list and, if the corresponding template-argument is
20580 deduced, the template-argument type shall match the type of the
20581 template-parameter exactly, except that a template-argument
20582 deduced from an array bound may be of any integral type.
20583 The non-type parameter might use already deduced type parameters. */
20584 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20585 if (tree a = type_uses_auto (tparm))
20586 {
20587 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20588 if (tparm == error_mark_node)
20589 return 1;
20590 }
20591
20592 if (!TREE_TYPE (arg))
20593 /* Template-parameter dependent expression. Just accept it for now.
20594 It will later be processed in convert_template_argument. */
20595 ;
20596 else if (same_type_p (TREE_TYPE (arg), tparm))
20597 /* OK */;
20598 else if ((strict & UNIFY_ALLOW_INTEGER)
20599 && CP_INTEGRAL_TYPE_P (tparm))
20600 /* Convert the ARG to the type of PARM; the deduced non-type
20601 template argument must exactly match the types of the
20602 corresponding parameter. */
20603 arg = fold (build_nop (tparm, arg));
20604 else if (uses_template_parms (tparm))
20605 /* We haven't deduced the type of this parameter yet. Try again
20606 later. */
20607 return unify_success (explain_p);
20608 else
20609 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20610
20611 /* If ARG is a parameter pack or an expansion, we cannot unify
20612 against it unless PARM is also a parameter pack. */
20613 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20614 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20615 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20616
20617 {
20618 bool removed_attr = false;
20619 arg = strip_typedefs_expr (arg, &removed_attr);
20620 }
20621 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20622 return unify_success (explain_p);
20623
20624 case PTRMEM_CST:
20625 {
20626 /* A pointer-to-member constant can be unified only with
20627 another constant. */
20628 if (TREE_CODE (arg) != PTRMEM_CST)
20629 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20630
20631 /* Just unify the class member. It would be useless (and possibly
20632 wrong, depending on the strict flags) to unify also
20633 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20634 arg refer to the same variable, even if through different
20635 classes. For instance:
20636
20637 struct A { int x; };
20638 struct B : A { };
20639
20640 Unification of &A::x and &B::x must succeed. */
20641 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20642 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20643 }
20644
20645 case POINTER_TYPE:
20646 {
20647 if (!TYPE_PTR_P (arg))
20648 return unify_type_mismatch (explain_p, parm, arg);
20649
20650 /* [temp.deduct.call]
20651
20652 A can be another pointer or pointer to member type that can
20653 be converted to the deduced A via a qualification
20654 conversion (_conv.qual_).
20655
20656 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20657 This will allow for additional cv-qualification of the
20658 pointed-to types if appropriate. */
20659
20660 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20661 /* The derived-to-base conversion only persists through one
20662 level of pointers. */
20663 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20664
20665 return unify (tparms, targs, TREE_TYPE (parm),
20666 TREE_TYPE (arg), strict, explain_p);
20667 }
20668
20669 case REFERENCE_TYPE:
20670 if (TREE_CODE (arg) != REFERENCE_TYPE)
20671 return unify_type_mismatch (explain_p, parm, arg);
20672 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20673 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20674
20675 case ARRAY_TYPE:
20676 if (TREE_CODE (arg) != ARRAY_TYPE)
20677 return unify_type_mismatch (explain_p, parm, arg);
20678 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20679 != (TYPE_DOMAIN (arg) == NULL_TREE))
20680 return unify_type_mismatch (explain_p, parm, arg);
20681 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20682 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20683 if (TYPE_DOMAIN (parm) != NULL_TREE)
20684 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20685 TYPE_DOMAIN (arg), explain_p);
20686 return unify_success (explain_p);
20687
20688 case REAL_TYPE:
20689 case COMPLEX_TYPE:
20690 case VECTOR_TYPE:
20691 case INTEGER_TYPE:
20692 case BOOLEAN_TYPE:
20693 case ENUMERAL_TYPE:
20694 case VOID_TYPE:
20695 case NULLPTR_TYPE:
20696 if (TREE_CODE (arg) != TREE_CODE (parm))
20697 return unify_type_mismatch (explain_p, parm, arg);
20698
20699 /* We have already checked cv-qualification at the top of the
20700 function. */
20701 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20702 return unify_type_mismatch (explain_p, parm, arg);
20703
20704 /* As far as unification is concerned, this wins. Later checks
20705 will invalidate it if necessary. */
20706 return unify_success (explain_p);
20707
20708 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20709 /* Type INTEGER_CST can come from ordinary constant template args. */
20710 case INTEGER_CST:
20711 while (CONVERT_EXPR_P (arg))
20712 arg = TREE_OPERAND (arg, 0);
20713
20714 if (TREE_CODE (arg) != INTEGER_CST)
20715 return unify_template_argument_mismatch (explain_p, parm, arg);
20716 return (tree_int_cst_equal (parm, arg)
20717 ? unify_success (explain_p)
20718 : unify_template_argument_mismatch (explain_p, parm, arg));
20719
20720 case TREE_VEC:
20721 {
20722 int i, len, argslen;
20723 int parm_variadic_p = 0;
20724
20725 if (TREE_CODE (arg) != TREE_VEC)
20726 return unify_template_argument_mismatch (explain_p, parm, arg);
20727
20728 len = TREE_VEC_LENGTH (parm);
20729 argslen = TREE_VEC_LENGTH (arg);
20730
20731 /* Check for pack expansions in the parameters. */
20732 for (i = 0; i < len; ++i)
20733 {
20734 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20735 {
20736 if (i == len - 1)
20737 /* We can unify against something with a trailing
20738 parameter pack. */
20739 parm_variadic_p = 1;
20740 else
20741 /* [temp.deduct.type]/9: If the template argument list of
20742 P contains a pack expansion that is not the last
20743 template argument, the entire template argument list
20744 is a non-deduced context. */
20745 return unify_success (explain_p);
20746 }
20747 }
20748
20749 /* If we don't have enough arguments to satisfy the parameters
20750 (not counting the pack expression at the end), or we have
20751 too many arguments for a parameter list that doesn't end in
20752 a pack expression, we can't unify. */
20753 if (parm_variadic_p
20754 ? argslen < len - parm_variadic_p
20755 : argslen != len)
20756 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20757
20758 /* Unify all of the parameters that precede the (optional)
20759 pack expression. */
20760 for (i = 0; i < len - parm_variadic_p; ++i)
20761 {
20762 RECUR_AND_CHECK_FAILURE (tparms, targs,
20763 TREE_VEC_ELT (parm, i),
20764 TREE_VEC_ELT (arg, i),
20765 UNIFY_ALLOW_NONE, explain_p);
20766 }
20767 if (parm_variadic_p)
20768 return unify_pack_expansion (tparms, targs, parm, arg,
20769 DEDUCE_EXACT,
20770 /*subr=*/true, explain_p);
20771 return unify_success (explain_p);
20772 }
20773
20774 case RECORD_TYPE:
20775 case UNION_TYPE:
20776 if (TREE_CODE (arg) != TREE_CODE (parm))
20777 return unify_type_mismatch (explain_p, parm, arg);
20778
20779 if (TYPE_PTRMEMFUNC_P (parm))
20780 {
20781 if (!TYPE_PTRMEMFUNC_P (arg))
20782 return unify_type_mismatch (explain_p, parm, arg);
20783
20784 return unify (tparms, targs,
20785 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20786 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20787 strict, explain_p);
20788 }
20789 else if (TYPE_PTRMEMFUNC_P (arg))
20790 return unify_type_mismatch (explain_p, parm, arg);
20791
20792 if (CLASSTYPE_TEMPLATE_INFO (parm))
20793 {
20794 tree t = NULL_TREE;
20795
20796 if (strict_in & UNIFY_ALLOW_DERIVED)
20797 {
20798 /* First, we try to unify the PARM and ARG directly. */
20799 t = try_class_unification (tparms, targs,
20800 parm, arg, explain_p);
20801
20802 if (!t)
20803 {
20804 /* Fallback to the special case allowed in
20805 [temp.deduct.call]:
20806
20807 If P is a class, and P has the form
20808 template-id, then A can be a derived class of
20809 the deduced A. Likewise, if P is a pointer to
20810 a class of the form template-id, A can be a
20811 pointer to a derived class pointed to by the
20812 deduced A. */
20813 enum template_base_result r;
20814 r = get_template_base (tparms, targs, parm, arg,
20815 explain_p, &t);
20816
20817 if (!t)
20818 {
20819 /* Don't give the derived diagnostic if we're
20820 already dealing with the same template. */
20821 bool same_template
20822 = (CLASSTYPE_TEMPLATE_INFO (arg)
20823 && (CLASSTYPE_TI_TEMPLATE (parm)
20824 == CLASSTYPE_TI_TEMPLATE (arg)));
20825 return unify_no_common_base (explain_p && !same_template,
20826 r, parm, arg);
20827 }
20828 }
20829 }
20830 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20831 && (CLASSTYPE_TI_TEMPLATE (parm)
20832 == CLASSTYPE_TI_TEMPLATE (arg)))
20833 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20834 Then, we should unify `int' and `U'. */
20835 t = arg;
20836 else
20837 /* There's no chance of unification succeeding. */
20838 return unify_type_mismatch (explain_p, parm, arg);
20839
20840 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20841 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20842 }
20843 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20844 return unify_type_mismatch (explain_p, parm, arg);
20845 return unify_success (explain_p);
20846
20847 case METHOD_TYPE:
20848 case FUNCTION_TYPE:
20849 {
20850 unsigned int nargs;
20851 tree *args;
20852 tree a;
20853 unsigned int i;
20854
20855 if (TREE_CODE (arg) != TREE_CODE (parm))
20856 return unify_type_mismatch (explain_p, parm, arg);
20857
20858 /* CV qualifications for methods can never be deduced, they must
20859 match exactly. We need to check them explicitly here,
20860 because type_unification_real treats them as any other
20861 cv-qualified parameter. */
20862 if (TREE_CODE (parm) == METHOD_TYPE
20863 && (!check_cv_quals_for_unify
20864 (UNIFY_ALLOW_NONE,
20865 class_of_this_parm (arg),
20866 class_of_this_parm (parm))))
20867 return unify_cv_qual_mismatch (explain_p, parm, arg);
20868 if (TREE_CODE (arg) == FUNCTION_TYPE
20869 && type_memfn_quals (parm) != type_memfn_quals (arg))
20870 return unify_cv_qual_mismatch (explain_p, parm, arg);
20871 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20872 return unify_type_mismatch (explain_p, parm, arg);
20873
20874 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20875 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20876
20877 nargs = list_length (TYPE_ARG_TYPES (arg));
20878 args = XALLOCAVEC (tree, nargs);
20879 for (a = TYPE_ARG_TYPES (arg), i = 0;
20880 a != NULL_TREE && a != void_list_node;
20881 a = TREE_CHAIN (a), ++i)
20882 args[i] = TREE_VALUE (a);
20883 nargs = i;
20884
20885 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20886 args, nargs, 1, DEDUCE_EXACT,
20887 LOOKUP_NORMAL, NULL, explain_p))
20888 return 1;
20889
20890 if (flag_noexcept_type)
20891 {
20892 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20893 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20894 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20895 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20896 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20897 && uses_template_parms (TREE_PURPOSE (pspec)))
20898 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20899 TREE_PURPOSE (aspec),
20900 UNIFY_ALLOW_NONE, explain_p);
20901 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20902 return unify_type_mismatch (explain_p, parm, arg);
20903 }
20904
20905 return 0;
20906 }
20907
20908 case OFFSET_TYPE:
20909 /* Unify a pointer to member with a pointer to member function, which
20910 deduces the type of the member as a function type. */
20911 if (TYPE_PTRMEMFUNC_P (arg))
20912 {
20913 /* Check top-level cv qualifiers */
20914 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20915 return unify_cv_qual_mismatch (explain_p, parm, arg);
20916
20917 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20918 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20919 UNIFY_ALLOW_NONE, explain_p);
20920
20921 /* Determine the type of the function we are unifying against. */
20922 tree fntype = static_fn_type (arg);
20923
20924 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20925 }
20926
20927 if (TREE_CODE (arg) != OFFSET_TYPE)
20928 return unify_type_mismatch (explain_p, parm, arg);
20929 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20930 TYPE_OFFSET_BASETYPE (arg),
20931 UNIFY_ALLOW_NONE, explain_p);
20932 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20933 strict, explain_p);
20934
20935 case CONST_DECL:
20936 if (DECL_TEMPLATE_PARM_P (parm))
20937 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20938 if (arg != scalar_constant_value (parm))
20939 return unify_template_argument_mismatch (explain_p, parm, arg);
20940 return unify_success (explain_p);
20941
20942 case FIELD_DECL:
20943 case TEMPLATE_DECL:
20944 /* Matched cases are handled by the ARG == PARM test above. */
20945 return unify_template_argument_mismatch (explain_p, parm, arg);
20946
20947 case VAR_DECL:
20948 /* We might get a variable as a non-type template argument in parm if the
20949 corresponding parameter is type-dependent. Make any necessary
20950 adjustments based on whether arg is a reference. */
20951 if (CONSTANT_CLASS_P (arg))
20952 parm = fold_non_dependent_expr (parm);
20953 else if (REFERENCE_REF_P (arg))
20954 {
20955 tree sub = TREE_OPERAND (arg, 0);
20956 STRIP_NOPS (sub);
20957 if (TREE_CODE (sub) == ADDR_EXPR)
20958 arg = TREE_OPERAND (sub, 0);
20959 }
20960 /* Now use the normal expression code to check whether they match. */
20961 goto expr;
20962
20963 case TYPE_ARGUMENT_PACK:
20964 case NONTYPE_ARGUMENT_PACK:
20965 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20966 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20967
20968 case TYPEOF_TYPE:
20969 case DECLTYPE_TYPE:
20970 case UNDERLYING_TYPE:
20971 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20972 or UNDERLYING_TYPE nodes. */
20973 return unify_success (explain_p);
20974
20975 case ERROR_MARK:
20976 /* Unification fails if we hit an error node. */
20977 return unify_invalid (explain_p);
20978
20979 case INDIRECT_REF:
20980 if (REFERENCE_REF_P (parm))
20981 {
20982 bool pexp = PACK_EXPANSION_P (arg);
20983 if (pexp)
20984 arg = PACK_EXPANSION_PATTERN (arg);
20985 if (REFERENCE_REF_P (arg))
20986 arg = TREE_OPERAND (arg, 0);
20987 if (pexp)
20988 arg = make_pack_expansion (arg);
20989 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20990 strict, explain_p);
20991 }
20992 /* FALLTHRU */
20993
20994 default:
20995 /* An unresolved overload is a nondeduced context. */
20996 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20997 return unify_success (explain_p);
20998 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
20999 expr:
21000 /* We must be looking at an expression. This can happen with
21001 something like:
21002
21003 template <int I>
21004 void foo(S<I>, S<I + 2>);
21005
21006 This is a "nondeduced context":
21007
21008 [deduct.type]
21009
21010 The nondeduced contexts are:
21011
21012 --A type that is a template-id in which one or more of
21013 the template-arguments is an expression that references
21014 a template-parameter.
21015
21016 In these cases, we assume deduction succeeded, but don't
21017 actually infer any unifications. */
21018
21019 if (!uses_template_parms (parm)
21020 && !template_args_equal (parm, arg))
21021 return unify_expression_unequal (explain_p, parm, arg);
21022 else
21023 return unify_success (explain_p);
21024 }
21025 }
21026 #undef RECUR_AND_CHECK_FAILURE
21027 \f
21028 /* Note that DECL can be defined in this translation unit, if
21029 required. */
21030
21031 static void
21032 mark_definable (tree decl)
21033 {
21034 tree clone;
21035 DECL_NOT_REALLY_EXTERN (decl) = 1;
21036 FOR_EACH_CLONE (clone, decl)
21037 DECL_NOT_REALLY_EXTERN (clone) = 1;
21038 }
21039
21040 /* Called if RESULT is explicitly instantiated, or is a member of an
21041 explicitly instantiated class. */
21042
21043 void
21044 mark_decl_instantiated (tree result, int extern_p)
21045 {
21046 SET_DECL_EXPLICIT_INSTANTIATION (result);
21047
21048 /* If this entity has already been written out, it's too late to
21049 make any modifications. */
21050 if (TREE_ASM_WRITTEN (result))
21051 return;
21052
21053 /* For anonymous namespace we don't need to do anything. */
21054 if (decl_anon_ns_mem_p (result))
21055 {
21056 gcc_assert (!TREE_PUBLIC (result));
21057 return;
21058 }
21059
21060 if (TREE_CODE (result) != FUNCTION_DECL)
21061 /* The TREE_PUBLIC flag for function declarations will have been
21062 set correctly by tsubst. */
21063 TREE_PUBLIC (result) = 1;
21064
21065 /* This might have been set by an earlier implicit instantiation. */
21066 DECL_COMDAT (result) = 0;
21067
21068 if (extern_p)
21069 DECL_NOT_REALLY_EXTERN (result) = 0;
21070 else
21071 {
21072 mark_definable (result);
21073 mark_needed (result);
21074 /* Always make artificials weak. */
21075 if (DECL_ARTIFICIAL (result) && flag_weak)
21076 comdat_linkage (result);
21077 /* For WIN32 we also want to put explicit instantiations in
21078 linkonce sections. */
21079 else if (TREE_PUBLIC (result))
21080 maybe_make_one_only (result);
21081 }
21082
21083 /* If EXTERN_P, then this function will not be emitted -- unless
21084 followed by an explicit instantiation, at which point its linkage
21085 will be adjusted. If !EXTERN_P, then this function will be
21086 emitted here. In neither circumstance do we want
21087 import_export_decl to adjust the linkage. */
21088 DECL_INTERFACE_KNOWN (result) = 1;
21089 }
21090
21091 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21092 important template arguments. If any are missing, we check whether
21093 they're important by using error_mark_node for substituting into any
21094 args that were used for partial ordering (the ones between ARGS and END)
21095 and seeing if it bubbles up. */
21096
21097 static bool
21098 check_undeduced_parms (tree targs, tree args, tree end)
21099 {
21100 bool found = false;
21101 int i;
21102 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21103 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21104 {
21105 found = true;
21106 TREE_VEC_ELT (targs, i) = error_mark_node;
21107 }
21108 if (found)
21109 {
21110 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21111 if (substed == error_mark_node)
21112 return true;
21113 }
21114 return false;
21115 }
21116
21117 /* Given two function templates PAT1 and PAT2, return:
21118
21119 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21120 -1 if PAT2 is more specialized than PAT1.
21121 0 if neither is more specialized.
21122
21123 LEN indicates the number of parameters we should consider
21124 (defaulted parameters should not be considered).
21125
21126 The 1998 std underspecified function template partial ordering, and
21127 DR214 addresses the issue. We take pairs of arguments, one from
21128 each of the templates, and deduce them against each other. One of
21129 the templates will be more specialized if all the *other*
21130 template's arguments deduce against its arguments and at least one
21131 of its arguments *does* *not* deduce against the other template's
21132 corresponding argument. Deduction is done as for class templates.
21133 The arguments used in deduction have reference and top level cv
21134 qualifiers removed. Iff both arguments were originally reference
21135 types *and* deduction succeeds in both directions, an lvalue reference
21136 wins against an rvalue reference and otherwise the template
21137 with the more cv-qualified argument wins for that pairing (if
21138 neither is more cv-qualified, they both are equal). Unlike regular
21139 deduction, after all the arguments have been deduced in this way,
21140 we do *not* verify the deduced template argument values can be
21141 substituted into non-deduced contexts.
21142
21143 The logic can be a bit confusing here, because we look at deduce1 and
21144 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21145 can find template arguments for pat1 to make arg1 look like arg2, that
21146 means that arg2 is at least as specialized as arg1. */
21147
21148 int
21149 more_specialized_fn (tree pat1, tree pat2, int len)
21150 {
21151 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21152 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21153 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21154 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21155 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21156 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21157 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21158 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21159 tree origs1, origs2;
21160 bool lose1 = false;
21161 bool lose2 = false;
21162
21163 /* Remove the this parameter from non-static member functions. If
21164 one is a non-static member function and the other is not a static
21165 member function, remove the first parameter from that function
21166 also. This situation occurs for operator functions where we
21167 locate both a member function (with this pointer) and non-member
21168 operator (with explicit first operand). */
21169 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21170 {
21171 len--; /* LEN is the number of significant arguments for DECL1 */
21172 args1 = TREE_CHAIN (args1);
21173 if (!DECL_STATIC_FUNCTION_P (decl2))
21174 args2 = TREE_CHAIN (args2);
21175 }
21176 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21177 {
21178 args2 = TREE_CHAIN (args2);
21179 if (!DECL_STATIC_FUNCTION_P (decl1))
21180 {
21181 len--;
21182 args1 = TREE_CHAIN (args1);
21183 }
21184 }
21185
21186 /* If only one is a conversion operator, they are unordered. */
21187 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21188 return 0;
21189
21190 /* Consider the return type for a conversion function */
21191 if (DECL_CONV_FN_P (decl1))
21192 {
21193 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21194 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21195 len++;
21196 }
21197
21198 processing_template_decl++;
21199
21200 origs1 = args1;
21201 origs2 = args2;
21202
21203 while (len--
21204 /* Stop when an ellipsis is seen. */
21205 && args1 != NULL_TREE && args2 != NULL_TREE)
21206 {
21207 tree arg1 = TREE_VALUE (args1);
21208 tree arg2 = TREE_VALUE (args2);
21209 int deduce1, deduce2;
21210 int quals1 = -1;
21211 int quals2 = -1;
21212 int ref1 = 0;
21213 int ref2 = 0;
21214
21215 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21216 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21217 {
21218 /* When both arguments are pack expansions, we need only
21219 unify the patterns themselves. */
21220 arg1 = PACK_EXPANSION_PATTERN (arg1);
21221 arg2 = PACK_EXPANSION_PATTERN (arg2);
21222
21223 /* This is the last comparison we need to do. */
21224 len = 0;
21225 }
21226
21227 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21228 {
21229 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21230 arg1 = TREE_TYPE (arg1);
21231 quals1 = cp_type_quals (arg1);
21232 }
21233
21234 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21235 {
21236 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21237 arg2 = TREE_TYPE (arg2);
21238 quals2 = cp_type_quals (arg2);
21239 }
21240
21241 arg1 = TYPE_MAIN_VARIANT (arg1);
21242 arg2 = TYPE_MAIN_VARIANT (arg2);
21243
21244 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21245 {
21246 int i, len2 = remaining_arguments (args2);
21247 tree parmvec = make_tree_vec (1);
21248 tree argvec = make_tree_vec (len2);
21249 tree ta = args2;
21250
21251 /* Setup the parameter vector, which contains only ARG1. */
21252 TREE_VEC_ELT (parmvec, 0) = arg1;
21253
21254 /* Setup the argument vector, which contains the remaining
21255 arguments. */
21256 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21257 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21258
21259 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21260 argvec, DEDUCE_EXACT,
21261 /*subr=*/true, /*explain_p=*/false)
21262 == 0);
21263
21264 /* We cannot deduce in the other direction, because ARG1 is
21265 a pack expansion but ARG2 is not. */
21266 deduce2 = 0;
21267 }
21268 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21269 {
21270 int i, len1 = remaining_arguments (args1);
21271 tree parmvec = make_tree_vec (1);
21272 tree argvec = make_tree_vec (len1);
21273 tree ta = args1;
21274
21275 /* Setup the parameter vector, which contains only ARG1. */
21276 TREE_VEC_ELT (parmvec, 0) = arg2;
21277
21278 /* Setup the argument vector, which contains the remaining
21279 arguments. */
21280 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21281 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21282
21283 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21284 argvec, DEDUCE_EXACT,
21285 /*subr=*/true, /*explain_p=*/false)
21286 == 0);
21287
21288 /* We cannot deduce in the other direction, because ARG2 is
21289 a pack expansion but ARG1 is not.*/
21290 deduce1 = 0;
21291 }
21292
21293 else
21294 {
21295 /* The normal case, where neither argument is a pack
21296 expansion. */
21297 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21298 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21299 == 0);
21300 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21301 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21302 == 0);
21303 }
21304
21305 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21306 arg2, then arg2 is not as specialized as arg1. */
21307 if (!deduce1)
21308 lose2 = true;
21309 if (!deduce2)
21310 lose1 = true;
21311
21312 /* "If, for a given type, deduction succeeds in both directions
21313 (i.e., the types are identical after the transformations above)
21314 and both P and A were reference types (before being replaced with
21315 the type referred to above):
21316 - if the type from the argument template was an lvalue reference and
21317 the type from the parameter template was not, the argument type is
21318 considered to be more specialized than the other; otherwise,
21319 - if the type from the argument template is more cv-qualified
21320 than the type from the parameter template (as described above),
21321 the argument type is considered to be more specialized than the other;
21322 otherwise,
21323 - neither type is more specialized than the other." */
21324
21325 if (deduce1 && deduce2)
21326 {
21327 if (ref1 && ref2 && ref1 != ref2)
21328 {
21329 if (ref1 > ref2)
21330 lose1 = true;
21331 else
21332 lose2 = true;
21333 }
21334 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21335 {
21336 if ((quals1 & quals2) == quals2)
21337 lose2 = true;
21338 if ((quals1 & quals2) == quals1)
21339 lose1 = true;
21340 }
21341 }
21342
21343 if (lose1 && lose2)
21344 /* We've failed to deduce something in either direction.
21345 These must be unordered. */
21346 break;
21347
21348 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21349 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21350 /* We have already processed all of the arguments in our
21351 handing of the pack expansion type. */
21352 len = 0;
21353
21354 args1 = TREE_CHAIN (args1);
21355 args2 = TREE_CHAIN (args2);
21356 }
21357
21358 /* "In most cases, all template parameters must have values in order for
21359 deduction to succeed, but for partial ordering purposes a template
21360 parameter may remain without a value provided it is not used in the
21361 types being used for partial ordering."
21362
21363 Thus, if we are missing any of the targs1 we need to substitute into
21364 origs1, then pat2 is not as specialized as pat1. This can happen when
21365 there is a nondeduced context. */
21366 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21367 lose2 = true;
21368 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21369 lose1 = true;
21370
21371 processing_template_decl--;
21372
21373 /* If both deductions succeed, the partial ordering selects the more
21374 constrained template. */
21375 if (!lose1 && !lose2)
21376 {
21377 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21378 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21379 lose1 = !subsumes_constraints (c1, c2);
21380 lose2 = !subsumes_constraints (c2, c1);
21381 }
21382
21383 /* All things being equal, if the next argument is a pack expansion
21384 for one function but not for the other, prefer the
21385 non-variadic function. FIXME this is bogus; see c++/41958. */
21386 if (lose1 == lose2
21387 && args1 && TREE_VALUE (args1)
21388 && args2 && TREE_VALUE (args2))
21389 {
21390 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21391 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21392 }
21393
21394 if (lose1 == lose2)
21395 return 0;
21396 else if (!lose1)
21397 return 1;
21398 else
21399 return -1;
21400 }
21401
21402 /* Determine which of two partial specializations of TMPL is more
21403 specialized.
21404
21405 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21406 to the first partial specialization. The TREE_PURPOSE is the
21407 innermost set of template parameters for the partial
21408 specialization. PAT2 is similar, but for the second template.
21409
21410 Return 1 if the first partial specialization is more specialized;
21411 -1 if the second is more specialized; 0 if neither is more
21412 specialized.
21413
21414 See [temp.class.order] for information about determining which of
21415 two templates is more specialized. */
21416
21417 static int
21418 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21419 {
21420 tree targs;
21421 int winner = 0;
21422 bool any_deductions = false;
21423
21424 tree tmpl1 = TREE_VALUE (pat1);
21425 tree tmpl2 = TREE_VALUE (pat2);
21426 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21427 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21428
21429 /* Just like what happens for functions, if we are ordering between
21430 different template specializations, we may encounter dependent
21431 types in the arguments, and we need our dependency check functions
21432 to behave correctly. */
21433 ++processing_template_decl;
21434 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21435 if (targs)
21436 {
21437 --winner;
21438 any_deductions = true;
21439 }
21440
21441 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21442 if (targs)
21443 {
21444 ++winner;
21445 any_deductions = true;
21446 }
21447 --processing_template_decl;
21448
21449 /* If both deductions succeed, the partial ordering selects the more
21450 constrained template. */
21451 if (!winner && any_deductions)
21452 return more_constrained (tmpl1, tmpl2);
21453
21454 /* In the case of a tie where at least one of the templates
21455 has a parameter pack at the end, the template with the most
21456 non-packed parameters wins. */
21457 if (winner == 0
21458 && any_deductions
21459 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21460 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21461 {
21462 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21463 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21464 int len1 = TREE_VEC_LENGTH (args1);
21465 int len2 = TREE_VEC_LENGTH (args2);
21466
21467 /* We don't count the pack expansion at the end. */
21468 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21469 --len1;
21470 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21471 --len2;
21472
21473 if (len1 > len2)
21474 return 1;
21475 else if (len1 < len2)
21476 return -1;
21477 }
21478
21479 return winner;
21480 }
21481
21482 /* Return the template arguments that will produce the function signature
21483 DECL from the function template FN, with the explicit template
21484 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21485 also match. Return NULL_TREE if no satisfactory arguments could be
21486 found. */
21487
21488 static tree
21489 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21490 {
21491 int ntparms = DECL_NTPARMS (fn);
21492 tree targs = make_tree_vec (ntparms);
21493 tree decl_type = TREE_TYPE (decl);
21494 tree decl_arg_types;
21495 tree *args;
21496 unsigned int nargs, ix;
21497 tree arg;
21498
21499 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21500
21501 /* Never do unification on the 'this' parameter. */
21502 decl_arg_types = skip_artificial_parms_for (decl,
21503 TYPE_ARG_TYPES (decl_type));
21504
21505 nargs = list_length (decl_arg_types);
21506 args = XALLOCAVEC (tree, nargs);
21507 for (arg = decl_arg_types, ix = 0;
21508 arg != NULL_TREE && arg != void_list_node;
21509 arg = TREE_CHAIN (arg), ++ix)
21510 args[ix] = TREE_VALUE (arg);
21511
21512 if (fn_type_unification (fn, explicit_args, targs,
21513 args, ix,
21514 (check_rettype || DECL_CONV_FN_P (fn)
21515 ? TREE_TYPE (decl_type) : NULL_TREE),
21516 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21517 /*decltype*/false)
21518 == error_mark_node)
21519 return NULL_TREE;
21520
21521 return targs;
21522 }
21523
21524 /* Return the innermost template arguments that, when applied to a partial
21525 specialization SPEC_TMPL of TMPL, yield the ARGS.
21526
21527 For example, suppose we have:
21528
21529 template <class T, class U> struct S {};
21530 template <class T> struct S<T*, int> {};
21531
21532 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21533 partial specialization and the ARGS will be {double*, int}. The resulting
21534 vector will be {double}, indicating that `T' is bound to `double'. */
21535
21536 static tree
21537 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21538 {
21539 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21540 tree spec_args
21541 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21542 int i, ntparms = TREE_VEC_LENGTH (tparms);
21543 tree deduced_args;
21544 tree innermost_deduced_args;
21545
21546 innermost_deduced_args = make_tree_vec (ntparms);
21547 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21548 {
21549 deduced_args = copy_node (args);
21550 SET_TMPL_ARGS_LEVEL (deduced_args,
21551 TMPL_ARGS_DEPTH (deduced_args),
21552 innermost_deduced_args);
21553 }
21554 else
21555 deduced_args = innermost_deduced_args;
21556
21557 bool tried_array_deduction = (cxx_dialect < cxx1z);
21558 again:
21559 if (unify (tparms, deduced_args,
21560 INNERMOST_TEMPLATE_ARGS (spec_args),
21561 INNERMOST_TEMPLATE_ARGS (args),
21562 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21563 return NULL_TREE;
21564
21565 for (i = 0; i < ntparms; ++i)
21566 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21567 {
21568 if (!tried_array_deduction)
21569 {
21570 try_array_deduction (tparms, innermost_deduced_args,
21571 INNERMOST_TEMPLATE_ARGS (spec_args));
21572 tried_array_deduction = true;
21573 if (TREE_VEC_ELT (innermost_deduced_args, i))
21574 goto again;
21575 }
21576 return NULL_TREE;
21577 }
21578
21579 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21580 if (!push_tinst_level (tinst))
21581 {
21582 excessive_deduction_depth = true;
21583 return NULL_TREE;
21584 }
21585
21586 /* Verify that nondeduced template arguments agree with the type
21587 obtained from argument deduction.
21588
21589 For example:
21590
21591 struct A { typedef int X; };
21592 template <class T, class U> struct C {};
21593 template <class T> struct C<T, typename T::X> {};
21594
21595 Then with the instantiation `C<A, int>', we can deduce that
21596 `T' is `A' but unify () does not check whether `typename T::X'
21597 is `int'. */
21598 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21599 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21600 spec_args, tmpl,
21601 tf_none, false, false);
21602
21603 pop_tinst_level ();
21604
21605 if (spec_args == error_mark_node
21606 /* We only need to check the innermost arguments; the other
21607 arguments will always agree. */
21608 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21609 INNERMOST_TEMPLATE_ARGS (args)))
21610 return NULL_TREE;
21611
21612 /* Now that we have bindings for all of the template arguments,
21613 ensure that the arguments deduced for the template template
21614 parameters have compatible template parameter lists. See the use
21615 of template_template_parm_bindings_ok_p in fn_type_unification
21616 for more information. */
21617 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21618 return NULL_TREE;
21619
21620 return deduced_args;
21621 }
21622
21623 // Compare two function templates T1 and T2 by deducing bindings
21624 // from one against the other. If both deductions succeed, compare
21625 // constraints to see which is more constrained.
21626 static int
21627 more_specialized_inst (tree t1, tree t2)
21628 {
21629 int fate = 0;
21630 int count = 0;
21631
21632 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21633 {
21634 --fate;
21635 ++count;
21636 }
21637
21638 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21639 {
21640 ++fate;
21641 ++count;
21642 }
21643
21644 // If both deductions succeed, then one may be more constrained.
21645 if (count == 2 && fate == 0)
21646 fate = more_constrained (t1, t2);
21647
21648 return fate;
21649 }
21650
21651 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21652 Return the TREE_LIST node with the most specialized template, if
21653 any. If there is no most specialized template, the error_mark_node
21654 is returned.
21655
21656 Note that this function does not look at, or modify, the
21657 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21658 returned is one of the elements of INSTANTIATIONS, callers may
21659 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21660 and retrieve it from the value returned. */
21661
21662 tree
21663 most_specialized_instantiation (tree templates)
21664 {
21665 tree fn, champ;
21666
21667 ++processing_template_decl;
21668
21669 champ = templates;
21670 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21671 {
21672 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21673 if (fate == -1)
21674 champ = fn;
21675 else if (!fate)
21676 {
21677 /* Equally specialized, move to next function. If there
21678 is no next function, nothing's most specialized. */
21679 fn = TREE_CHAIN (fn);
21680 champ = fn;
21681 if (!fn)
21682 break;
21683 }
21684 }
21685
21686 if (champ)
21687 /* Now verify that champ is better than everything earlier in the
21688 instantiation list. */
21689 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21690 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21691 {
21692 champ = NULL_TREE;
21693 break;
21694 }
21695 }
21696
21697 processing_template_decl--;
21698
21699 if (!champ)
21700 return error_mark_node;
21701
21702 return champ;
21703 }
21704
21705 /* If DECL is a specialization of some template, return the most
21706 general such template. Otherwise, returns NULL_TREE.
21707
21708 For example, given:
21709
21710 template <class T> struct S { template <class U> void f(U); };
21711
21712 if TMPL is `template <class U> void S<int>::f(U)' this will return
21713 the full template. This function will not trace past partial
21714 specializations, however. For example, given in addition:
21715
21716 template <class T> struct S<T*> { template <class U> void f(U); };
21717
21718 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21719 `template <class T> template <class U> S<T*>::f(U)'. */
21720
21721 tree
21722 most_general_template (tree decl)
21723 {
21724 if (TREE_CODE (decl) != TEMPLATE_DECL)
21725 {
21726 if (tree tinfo = get_template_info (decl))
21727 decl = TI_TEMPLATE (tinfo);
21728 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21729 template friend, or a FIELD_DECL for a capture pack. */
21730 if (TREE_CODE (decl) != TEMPLATE_DECL)
21731 return NULL_TREE;
21732 }
21733
21734 /* Look for more and more general templates. */
21735 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21736 {
21737 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21738 (See cp-tree.h for details.) */
21739 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21740 break;
21741
21742 if (CLASS_TYPE_P (TREE_TYPE (decl))
21743 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21744 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21745 break;
21746
21747 /* Stop if we run into an explicitly specialized class template. */
21748 if (!DECL_NAMESPACE_SCOPE_P (decl)
21749 && DECL_CONTEXT (decl)
21750 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21751 break;
21752
21753 decl = DECL_TI_TEMPLATE (decl);
21754 }
21755
21756 return decl;
21757 }
21758
21759 /* Return the most specialized of the template partial specializations
21760 which can produce TARGET, a specialization of some class or variable
21761 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21762 a TEMPLATE_DECL node corresponding to the partial specialization, while
21763 the TREE_PURPOSE is the set of template arguments that must be
21764 substituted into the template pattern in order to generate TARGET.
21765
21766 If the choice of partial specialization is ambiguous, a diagnostic
21767 is issued, and the error_mark_node is returned. If there are no
21768 partial specializations matching TARGET, then NULL_TREE is
21769 returned, indicating that the primary template should be used. */
21770
21771 static tree
21772 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21773 {
21774 tree list = NULL_TREE;
21775 tree t;
21776 tree champ;
21777 int fate;
21778 bool ambiguous_p;
21779 tree outer_args = NULL_TREE;
21780 tree tmpl, args;
21781
21782 if (TYPE_P (target))
21783 {
21784 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21785 tmpl = TI_TEMPLATE (tinfo);
21786 args = TI_ARGS (tinfo);
21787 }
21788 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21789 {
21790 tmpl = TREE_OPERAND (target, 0);
21791 args = TREE_OPERAND (target, 1);
21792 }
21793 else if (VAR_P (target))
21794 {
21795 tree tinfo = DECL_TEMPLATE_INFO (target);
21796 tmpl = TI_TEMPLATE (tinfo);
21797 args = TI_ARGS (tinfo);
21798 }
21799 else
21800 gcc_unreachable ();
21801
21802 tree main_tmpl = most_general_template (tmpl);
21803
21804 /* For determining which partial specialization to use, only the
21805 innermost args are interesting. */
21806 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21807 {
21808 outer_args = strip_innermost_template_args (args, 1);
21809 args = INNERMOST_TEMPLATE_ARGS (args);
21810 }
21811
21812 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21813 {
21814 tree spec_args;
21815 tree spec_tmpl = TREE_VALUE (t);
21816
21817 if (outer_args)
21818 {
21819 /* Substitute in the template args from the enclosing class. */
21820 ++processing_template_decl;
21821 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21822 --processing_template_decl;
21823 }
21824
21825 if (spec_tmpl == error_mark_node)
21826 return error_mark_node;
21827
21828 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21829 if (spec_args)
21830 {
21831 if (outer_args)
21832 spec_args = add_to_template_args (outer_args, spec_args);
21833
21834 /* Keep the candidate only if the constraints are satisfied,
21835 or if we're not compiling with concepts. */
21836 if (!flag_concepts
21837 || constraints_satisfied_p (spec_tmpl, spec_args))
21838 {
21839 list = tree_cons (spec_args, TREE_VALUE (t), list);
21840 TREE_TYPE (list) = TREE_TYPE (t);
21841 }
21842 }
21843 }
21844
21845 if (! list)
21846 return NULL_TREE;
21847
21848 ambiguous_p = false;
21849 t = list;
21850 champ = t;
21851 t = TREE_CHAIN (t);
21852 for (; t; t = TREE_CHAIN (t))
21853 {
21854 fate = more_specialized_partial_spec (tmpl, champ, t);
21855 if (fate == 1)
21856 ;
21857 else
21858 {
21859 if (fate == 0)
21860 {
21861 t = TREE_CHAIN (t);
21862 if (! t)
21863 {
21864 ambiguous_p = true;
21865 break;
21866 }
21867 }
21868 champ = t;
21869 }
21870 }
21871
21872 if (!ambiguous_p)
21873 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21874 {
21875 fate = more_specialized_partial_spec (tmpl, champ, t);
21876 if (fate != 1)
21877 {
21878 ambiguous_p = true;
21879 break;
21880 }
21881 }
21882
21883 if (ambiguous_p)
21884 {
21885 const char *str;
21886 char *spaces = NULL;
21887 if (!(complain & tf_error))
21888 return error_mark_node;
21889 if (TYPE_P (target))
21890 error ("ambiguous template instantiation for %q#T", target);
21891 else
21892 error ("ambiguous template instantiation for %q#D", target);
21893 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21894 for (t = list; t; t = TREE_CHAIN (t))
21895 {
21896 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21897 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21898 "%s %#S", spaces ? spaces : str, subst);
21899 spaces = spaces ? spaces : get_spaces (str);
21900 }
21901 free (spaces);
21902 return error_mark_node;
21903 }
21904
21905 return champ;
21906 }
21907
21908 /* Explicitly instantiate DECL. */
21909
21910 void
21911 do_decl_instantiation (tree decl, tree storage)
21912 {
21913 tree result = NULL_TREE;
21914 int extern_p = 0;
21915
21916 if (!decl || decl == error_mark_node)
21917 /* An error occurred, for which grokdeclarator has already issued
21918 an appropriate message. */
21919 return;
21920 else if (! DECL_LANG_SPECIFIC (decl))
21921 {
21922 error ("explicit instantiation of non-template %q#D", decl);
21923 return;
21924 }
21925
21926 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21927 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21928
21929 if (VAR_P (decl) && !var_templ)
21930 {
21931 /* There is an asymmetry here in the way VAR_DECLs and
21932 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21933 the latter, the DECL we get back will be marked as a
21934 template instantiation, and the appropriate
21935 DECL_TEMPLATE_INFO will be set up. This does not happen for
21936 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21937 should handle VAR_DECLs as it currently handles
21938 FUNCTION_DECLs. */
21939 if (!DECL_CLASS_SCOPE_P (decl))
21940 {
21941 error ("%qD is not a static data member of a class template", decl);
21942 return;
21943 }
21944 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21945 if (!result || !VAR_P (result))
21946 {
21947 error ("no matching template for %qD found", decl);
21948 return;
21949 }
21950 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21951 {
21952 error ("type %qT for explicit instantiation %qD does not match "
21953 "declared type %qT", TREE_TYPE (result), decl,
21954 TREE_TYPE (decl));
21955 return;
21956 }
21957 }
21958 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21959 {
21960 error ("explicit instantiation of %q#D", decl);
21961 return;
21962 }
21963 else
21964 result = decl;
21965
21966 /* Check for various error cases. Note that if the explicit
21967 instantiation is valid the RESULT will currently be marked as an
21968 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21969 until we get here. */
21970
21971 if (DECL_TEMPLATE_SPECIALIZATION (result))
21972 {
21973 /* DR 259 [temp.spec].
21974
21975 Both an explicit instantiation and a declaration of an explicit
21976 specialization shall not appear in a program unless the explicit
21977 instantiation follows a declaration of the explicit specialization.
21978
21979 For a given set of template parameters, if an explicit
21980 instantiation of a template appears after a declaration of an
21981 explicit specialization for that template, the explicit
21982 instantiation has no effect. */
21983 return;
21984 }
21985 else if (DECL_EXPLICIT_INSTANTIATION (result))
21986 {
21987 /* [temp.spec]
21988
21989 No program shall explicitly instantiate any template more
21990 than once.
21991
21992 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21993 the first instantiation was `extern' and the second is not,
21994 and EXTERN_P for the opposite case. */
21995 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21996 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21997 /* If an "extern" explicit instantiation follows an ordinary
21998 explicit instantiation, the template is instantiated. */
21999 if (extern_p)
22000 return;
22001 }
22002 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22003 {
22004 error ("no matching template for %qD found", result);
22005 return;
22006 }
22007 else if (!DECL_TEMPLATE_INFO (result))
22008 {
22009 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22010 return;
22011 }
22012
22013 if (storage == NULL_TREE)
22014 ;
22015 else if (storage == ridpointers[(int) RID_EXTERN])
22016 {
22017 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22018 pedwarn (input_location, OPT_Wpedantic,
22019 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22020 "instantiations");
22021 extern_p = 1;
22022 }
22023 else
22024 error ("storage class %qD applied to template instantiation", storage);
22025
22026 check_explicit_instantiation_namespace (result);
22027 mark_decl_instantiated (result, extern_p);
22028 if (! extern_p)
22029 instantiate_decl (result, /*defer_ok=*/true,
22030 /*expl_inst_class_mem_p=*/false);
22031 }
22032
22033 static void
22034 mark_class_instantiated (tree t, int extern_p)
22035 {
22036 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22037 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22038 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22039 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22040 if (! extern_p)
22041 {
22042 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22043 rest_of_type_compilation (t, 1);
22044 }
22045 }
22046
22047 /* Called from do_type_instantiation through binding_table_foreach to
22048 do recursive instantiation for the type bound in ENTRY. */
22049 static void
22050 bt_instantiate_type_proc (binding_entry entry, void *data)
22051 {
22052 tree storage = *(tree *) data;
22053
22054 if (MAYBE_CLASS_TYPE_P (entry->type)
22055 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22056 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22057 }
22058
22059 /* Called from do_type_instantiation to instantiate a member
22060 (a member function or a static member variable) of an
22061 explicitly instantiated class template. */
22062 static void
22063 instantiate_class_member (tree decl, int extern_p)
22064 {
22065 mark_decl_instantiated (decl, extern_p);
22066 if (! extern_p)
22067 instantiate_decl (decl, /*defer_ok=*/true,
22068 /*expl_inst_class_mem_p=*/true);
22069 }
22070
22071 /* Perform an explicit instantiation of template class T. STORAGE, if
22072 non-null, is the RID for extern, inline or static. COMPLAIN is
22073 nonzero if this is called from the parser, zero if called recursively,
22074 since the standard is unclear (as detailed below). */
22075
22076 void
22077 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22078 {
22079 int extern_p = 0;
22080 int nomem_p = 0;
22081 int static_p = 0;
22082 int previous_instantiation_extern_p = 0;
22083
22084 if (TREE_CODE (t) == TYPE_DECL)
22085 t = TREE_TYPE (t);
22086
22087 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22088 {
22089 tree tmpl =
22090 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22091 if (tmpl)
22092 error ("explicit instantiation of non-class template %qD", tmpl);
22093 else
22094 error ("explicit instantiation of non-template type %qT", t);
22095 return;
22096 }
22097
22098 complete_type (t);
22099
22100 if (!COMPLETE_TYPE_P (t))
22101 {
22102 if (complain & tf_error)
22103 error ("explicit instantiation of %q#T before definition of template",
22104 t);
22105 return;
22106 }
22107
22108 if (storage != NULL_TREE)
22109 {
22110 if (!in_system_header_at (input_location))
22111 {
22112 if (storage == ridpointers[(int) RID_EXTERN])
22113 {
22114 if (cxx_dialect == cxx98)
22115 pedwarn (input_location, OPT_Wpedantic,
22116 "ISO C++ 1998 forbids the use of %<extern%> on "
22117 "explicit instantiations");
22118 }
22119 else
22120 pedwarn (input_location, OPT_Wpedantic,
22121 "ISO C++ forbids the use of %qE"
22122 " on explicit instantiations", storage);
22123 }
22124
22125 if (storage == ridpointers[(int) RID_INLINE])
22126 nomem_p = 1;
22127 else if (storage == ridpointers[(int) RID_EXTERN])
22128 extern_p = 1;
22129 else if (storage == ridpointers[(int) RID_STATIC])
22130 static_p = 1;
22131 else
22132 {
22133 error ("storage class %qD applied to template instantiation",
22134 storage);
22135 extern_p = 0;
22136 }
22137 }
22138
22139 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22140 {
22141 /* DR 259 [temp.spec].
22142
22143 Both an explicit instantiation and a declaration of an explicit
22144 specialization shall not appear in a program unless the explicit
22145 instantiation follows a declaration of the explicit specialization.
22146
22147 For a given set of template parameters, if an explicit
22148 instantiation of a template appears after a declaration of an
22149 explicit specialization for that template, the explicit
22150 instantiation has no effect. */
22151 return;
22152 }
22153 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22154 {
22155 /* [temp.spec]
22156
22157 No program shall explicitly instantiate any template more
22158 than once.
22159
22160 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22161 instantiation was `extern'. If EXTERN_P then the second is.
22162 These cases are OK. */
22163 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22164
22165 if (!previous_instantiation_extern_p && !extern_p
22166 && (complain & tf_error))
22167 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22168
22169 /* If we've already instantiated the template, just return now. */
22170 if (!CLASSTYPE_INTERFACE_ONLY (t))
22171 return;
22172 }
22173
22174 check_explicit_instantiation_namespace (TYPE_NAME (t));
22175 mark_class_instantiated (t, extern_p);
22176
22177 if (nomem_p)
22178 return;
22179
22180 {
22181 tree tmp;
22182
22183 /* In contrast to implicit instantiation, where only the
22184 declarations, and not the definitions, of members are
22185 instantiated, we have here:
22186
22187 [temp.explicit]
22188
22189 The explicit instantiation of a class template specialization
22190 implies the instantiation of all of its members not
22191 previously explicitly specialized in the translation unit
22192 containing the explicit instantiation.
22193
22194 Of course, we can't instantiate member template classes, since
22195 we don't have any arguments for them. Note that the standard
22196 is unclear on whether the instantiation of the members are
22197 *explicit* instantiations or not. However, the most natural
22198 interpretation is that it should be an explicit instantiation. */
22199
22200 if (! static_p)
22201 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22202 if (TREE_CODE (tmp) == FUNCTION_DECL
22203 && DECL_TEMPLATE_INSTANTIATION (tmp)
22204 && user_provided_p (tmp))
22205 instantiate_class_member (tmp, extern_p);
22206
22207 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22208 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22209 instantiate_class_member (tmp, extern_p);
22210
22211 if (CLASSTYPE_NESTED_UTDS (t))
22212 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22213 bt_instantiate_type_proc, &storage);
22214 }
22215 }
22216
22217 /* Given a function DECL, which is a specialization of TMPL, modify
22218 DECL to be a re-instantiation of TMPL with the same template
22219 arguments. TMPL should be the template into which tsubst'ing
22220 should occur for DECL, not the most general template.
22221
22222 One reason for doing this is a scenario like this:
22223
22224 template <class T>
22225 void f(const T&, int i);
22226
22227 void g() { f(3, 7); }
22228
22229 template <class T>
22230 void f(const T& t, const int i) { }
22231
22232 Note that when the template is first instantiated, with
22233 instantiate_template, the resulting DECL will have no name for the
22234 first parameter, and the wrong type for the second. So, when we go
22235 to instantiate the DECL, we regenerate it. */
22236
22237 static void
22238 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22239 {
22240 /* The arguments used to instantiate DECL, from the most general
22241 template. */
22242 tree code_pattern;
22243
22244 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22245
22246 /* Make sure that we can see identifiers, and compute access
22247 correctly. */
22248 push_access_scope (decl);
22249
22250 if (TREE_CODE (decl) == FUNCTION_DECL)
22251 {
22252 tree decl_parm;
22253 tree pattern_parm;
22254 tree specs;
22255 int args_depth;
22256 int parms_depth;
22257
22258 args_depth = TMPL_ARGS_DEPTH (args);
22259 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22260 if (args_depth > parms_depth)
22261 args = get_innermost_template_args (args, parms_depth);
22262
22263 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22264 args, tf_error, NULL_TREE,
22265 /*defer_ok*/false);
22266 if (specs && specs != error_mark_node)
22267 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22268 specs);
22269
22270 /* Merge parameter declarations. */
22271 decl_parm = skip_artificial_parms_for (decl,
22272 DECL_ARGUMENTS (decl));
22273 pattern_parm
22274 = skip_artificial_parms_for (code_pattern,
22275 DECL_ARGUMENTS (code_pattern));
22276 while (decl_parm && !DECL_PACK_P (pattern_parm))
22277 {
22278 tree parm_type;
22279 tree attributes;
22280
22281 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22282 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22283 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22284 NULL_TREE);
22285 parm_type = type_decays_to (parm_type);
22286 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22287 TREE_TYPE (decl_parm) = parm_type;
22288 attributes = DECL_ATTRIBUTES (pattern_parm);
22289 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22290 {
22291 DECL_ATTRIBUTES (decl_parm) = attributes;
22292 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22293 }
22294 decl_parm = DECL_CHAIN (decl_parm);
22295 pattern_parm = DECL_CHAIN (pattern_parm);
22296 }
22297 /* Merge any parameters that match with the function parameter
22298 pack. */
22299 if (pattern_parm && DECL_PACK_P (pattern_parm))
22300 {
22301 int i, len;
22302 tree expanded_types;
22303 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22304 the parameters in this function parameter pack. */
22305 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22306 args, tf_error, NULL_TREE);
22307 len = TREE_VEC_LENGTH (expanded_types);
22308 for (i = 0; i < len; i++)
22309 {
22310 tree parm_type;
22311 tree attributes;
22312
22313 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22314 /* Rename the parameter to include the index. */
22315 DECL_NAME (decl_parm) =
22316 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22317 parm_type = TREE_VEC_ELT (expanded_types, i);
22318 parm_type = type_decays_to (parm_type);
22319 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22320 TREE_TYPE (decl_parm) = parm_type;
22321 attributes = DECL_ATTRIBUTES (pattern_parm);
22322 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22323 {
22324 DECL_ATTRIBUTES (decl_parm) = attributes;
22325 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22326 }
22327 decl_parm = DECL_CHAIN (decl_parm);
22328 }
22329 }
22330 /* Merge additional specifiers from the CODE_PATTERN. */
22331 if (DECL_DECLARED_INLINE_P (code_pattern)
22332 && !DECL_DECLARED_INLINE_P (decl))
22333 DECL_DECLARED_INLINE_P (decl) = 1;
22334 }
22335 else if (VAR_P (decl))
22336 {
22337 DECL_INITIAL (decl) =
22338 tsubst_expr (DECL_INITIAL (code_pattern), args,
22339 tf_error, DECL_TI_TEMPLATE (decl),
22340 /*integral_constant_expression_p=*/false);
22341 if (VAR_HAD_UNKNOWN_BOUND (decl))
22342 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22343 tf_error, DECL_TI_TEMPLATE (decl));
22344 }
22345 else
22346 gcc_unreachable ();
22347
22348 pop_access_scope (decl);
22349 }
22350
22351 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22352 substituted to get DECL. */
22353
22354 tree
22355 template_for_substitution (tree decl)
22356 {
22357 tree tmpl = DECL_TI_TEMPLATE (decl);
22358
22359 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22360 for the instantiation. This is not always the most general
22361 template. Consider, for example:
22362
22363 template <class T>
22364 struct S { template <class U> void f();
22365 template <> void f<int>(); };
22366
22367 and an instantiation of S<double>::f<int>. We want TD to be the
22368 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22369 while (/* An instantiation cannot have a definition, so we need a
22370 more general template. */
22371 DECL_TEMPLATE_INSTANTIATION (tmpl)
22372 /* We must also deal with friend templates. Given:
22373
22374 template <class T> struct S {
22375 template <class U> friend void f() {};
22376 };
22377
22378 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22379 so far as the language is concerned, but that's still
22380 where we get the pattern for the instantiation from. On
22381 other hand, if the definition comes outside the class, say:
22382
22383 template <class T> struct S {
22384 template <class U> friend void f();
22385 };
22386 template <class U> friend void f() {}
22387
22388 we don't need to look any further. That's what the check for
22389 DECL_INITIAL is for. */
22390 || (TREE_CODE (decl) == FUNCTION_DECL
22391 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22392 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22393 {
22394 /* The present template, TD, should not be a definition. If it
22395 were a definition, we should be using it! Note that we
22396 cannot restructure the loop to just keep going until we find
22397 a template with a definition, since that might go too far if
22398 a specialization was declared, but not defined. */
22399
22400 /* Fetch the more general template. */
22401 tmpl = DECL_TI_TEMPLATE (tmpl);
22402 }
22403
22404 return tmpl;
22405 }
22406
22407 /* Returns true if we need to instantiate this template instance even if we
22408 know we aren't going to emit it. */
22409
22410 bool
22411 always_instantiate_p (tree decl)
22412 {
22413 /* We always instantiate inline functions so that we can inline them. An
22414 explicit instantiation declaration prohibits implicit instantiation of
22415 non-inline functions. With high levels of optimization, we would
22416 normally inline non-inline functions -- but we're not allowed to do
22417 that for "extern template" functions. Therefore, we check
22418 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22419 return ((TREE_CODE (decl) == FUNCTION_DECL
22420 && (DECL_DECLARED_INLINE_P (decl)
22421 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22422 /* And we need to instantiate static data members so that
22423 their initializers are available in integral constant
22424 expressions. */
22425 || (VAR_P (decl)
22426 && decl_maybe_constant_var_p (decl)));
22427 }
22428
22429 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22430 instantiate it now, modifying TREE_TYPE (fn). */
22431
22432 void
22433 maybe_instantiate_noexcept (tree fn)
22434 {
22435 tree fntype, spec, noex, clone;
22436
22437 /* Don't instantiate a noexcept-specification from template context. */
22438 if (processing_template_decl)
22439 return;
22440
22441 if (DECL_CLONED_FUNCTION_P (fn))
22442 fn = DECL_CLONED_FUNCTION (fn);
22443 fntype = TREE_TYPE (fn);
22444 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22445
22446 if (!spec || !TREE_PURPOSE (spec))
22447 return;
22448
22449 noex = TREE_PURPOSE (spec);
22450
22451 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22452 {
22453 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22454 spec = get_defaulted_eh_spec (fn);
22455 else if (push_tinst_level (fn))
22456 {
22457 push_access_scope (fn);
22458 push_deferring_access_checks (dk_no_deferred);
22459 input_location = DECL_SOURCE_LOCATION (fn);
22460 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22461 DEFERRED_NOEXCEPT_ARGS (noex),
22462 tf_warning_or_error, fn,
22463 /*function_p=*/false,
22464 /*integral_constant_expression_p=*/true);
22465 pop_deferring_access_checks ();
22466 pop_access_scope (fn);
22467 pop_tinst_level ();
22468 spec = build_noexcept_spec (noex, tf_warning_or_error);
22469 if (spec == error_mark_node)
22470 spec = noexcept_false_spec;
22471 }
22472 else
22473 spec = noexcept_false_spec;
22474
22475 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22476 }
22477
22478 FOR_EACH_CLONE (clone, fn)
22479 {
22480 if (TREE_TYPE (clone) == fntype)
22481 TREE_TYPE (clone) = TREE_TYPE (fn);
22482 else
22483 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22484 }
22485 }
22486
22487 /* Produce the definition of D, a _DECL generated from a template. If
22488 DEFER_OK is true, then we don't have to actually do the
22489 instantiation now; we just have to do it sometime. Normally it is
22490 an error if this is an explicit instantiation but D is undefined.
22491 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22492 instantiated class template. */
22493
22494 tree
22495 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22496 {
22497 tree tmpl = DECL_TI_TEMPLATE (d);
22498 tree gen_args;
22499 tree args;
22500 tree td;
22501 tree code_pattern;
22502 tree spec;
22503 tree gen_tmpl;
22504 bool pattern_defined;
22505 location_t saved_loc = input_location;
22506 int saved_unevaluated_operand = cp_unevaluated_operand;
22507 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22508 bool external_p;
22509 bool deleted_p;
22510
22511 /* This function should only be used to instantiate templates for
22512 functions and static member variables. */
22513 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22514
22515 /* A concept is never instantiated. */
22516 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22517
22518 /* Variables are never deferred; if instantiation is required, they
22519 are instantiated right away. That allows for better code in the
22520 case that an expression refers to the value of the variable --
22521 if the variable has a constant value the referring expression can
22522 take advantage of that fact. */
22523 if (VAR_P (d))
22524 defer_ok = false;
22525
22526 /* Don't instantiate cloned functions. Instead, instantiate the
22527 functions they cloned. */
22528 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22529 d = DECL_CLONED_FUNCTION (d);
22530
22531 if (DECL_TEMPLATE_INSTANTIATED (d)
22532 || (TREE_CODE (d) == FUNCTION_DECL
22533 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22534 || DECL_TEMPLATE_SPECIALIZATION (d))
22535 /* D has already been instantiated or explicitly specialized, so
22536 there's nothing for us to do here.
22537
22538 It might seem reasonable to check whether or not D is an explicit
22539 instantiation, and, if so, stop here. But when an explicit
22540 instantiation is deferred until the end of the compilation,
22541 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22542 the instantiation. */
22543 return d;
22544
22545 /* Check to see whether we know that this template will be
22546 instantiated in some other file, as with "extern template"
22547 extension. */
22548 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22549
22550 /* In general, we do not instantiate such templates. */
22551 if (external_p && !always_instantiate_p (d))
22552 return d;
22553
22554 gen_tmpl = most_general_template (tmpl);
22555 gen_args = DECL_TI_ARGS (d);
22556
22557 if (tmpl != gen_tmpl)
22558 /* We should already have the extra args. */
22559 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22560 == TMPL_ARGS_DEPTH (gen_args));
22561 /* And what's in the hash table should match D. */
22562 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22563 || spec == NULL_TREE);
22564
22565 /* This needs to happen before any tsubsting. */
22566 if (! push_tinst_level (d))
22567 return d;
22568
22569 timevar_push (TV_TEMPLATE_INST);
22570
22571 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22572 for the instantiation. */
22573 td = template_for_substitution (d);
22574 args = gen_args;
22575
22576 if (VAR_P (d))
22577 {
22578 /* Look up an explicit specialization, if any. */
22579 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22580 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22581 if (elt && elt != error_mark_node)
22582 {
22583 td = TREE_VALUE (elt);
22584 args = TREE_PURPOSE (elt);
22585 }
22586 }
22587
22588 code_pattern = DECL_TEMPLATE_RESULT (td);
22589
22590 /* We should never be trying to instantiate a member of a class
22591 template or partial specialization. */
22592 gcc_assert (d != code_pattern);
22593
22594 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22595 || DECL_TEMPLATE_SPECIALIZATION (td))
22596 /* In the case of a friend template whose definition is provided
22597 outside the class, we may have too many arguments. Drop the
22598 ones we don't need. The same is true for specializations. */
22599 args = get_innermost_template_args
22600 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22601
22602 if (TREE_CODE (d) == FUNCTION_DECL)
22603 {
22604 deleted_p = DECL_DELETED_FN (code_pattern);
22605 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22606 && DECL_INITIAL (code_pattern) != error_mark_node)
22607 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22608 || deleted_p);
22609 }
22610 else
22611 {
22612 deleted_p = false;
22613 if (DECL_CLASS_SCOPE_P (code_pattern))
22614 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22615 || DECL_INLINE_VAR_P (code_pattern));
22616 else
22617 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22618 }
22619
22620 /* We may be in the middle of deferred access check. Disable it now. */
22621 push_deferring_access_checks (dk_no_deferred);
22622
22623 /* Unless an explicit instantiation directive has already determined
22624 the linkage of D, remember that a definition is available for
22625 this entity. */
22626 if (pattern_defined
22627 && !DECL_INTERFACE_KNOWN (d)
22628 && !DECL_NOT_REALLY_EXTERN (d))
22629 mark_definable (d);
22630
22631 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22632 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22633 input_location = DECL_SOURCE_LOCATION (d);
22634
22635 /* If D is a member of an explicitly instantiated class template,
22636 and no definition is available, treat it like an implicit
22637 instantiation. */
22638 if (!pattern_defined && expl_inst_class_mem_p
22639 && DECL_EXPLICIT_INSTANTIATION (d))
22640 {
22641 /* Leave linkage flags alone on instantiations with anonymous
22642 visibility. */
22643 if (TREE_PUBLIC (d))
22644 {
22645 DECL_NOT_REALLY_EXTERN (d) = 0;
22646 DECL_INTERFACE_KNOWN (d) = 0;
22647 }
22648 SET_DECL_IMPLICIT_INSTANTIATION (d);
22649 }
22650
22651 /* Defer all other templates, unless we have been explicitly
22652 forbidden from doing so. */
22653 if (/* If there is no definition, we cannot instantiate the
22654 template. */
22655 ! pattern_defined
22656 /* If it's OK to postpone instantiation, do so. */
22657 || defer_ok
22658 /* If this is a static data member that will be defined
22659 elsewhere, we don't want to instantiate the entire data
22660 member, but we do want to instantiate the initializer so that
22661 we can substitute that elsewhere. */
22662 || (external_p && VAR_P (d))
22663 /* Handle here a deleted function too, avoid generating
22664 its body (c++/61080). */
22665 || deleted_p)
22666 {
22667 /* The definition of the static data member is now required so
22668 we must substitute the initializer. */
22669 if (VAR_P (d)
22670 && !DECL_INITIAL (d)
22671 && DECL_INITIAL (code_pattern))
22672 {
22673 tree ns;
22674 tree init;
22675 bool const_init = false;
22676 bool enter_context = DECL_CLASS_SCOPE_P (d);
22677
22678 ns = decl_namespace_context (d);
22679 push_nested_namespace (ns);
22680 if (enter_context)
22681 push_nested_class (DECL_CONTEXT (d));
22682 init = tsubst_expr (DECL_INITIAL (code_pattern),
22683 args,
22684 tf_warning_or_error, NULL_TREE,
22685 /*integral_constant_expression_p=*/false);
22686 /* If instantiating the initializer involved instantiating this
22687 again, don't call cp_finish_decl twice. */
22688 if (!DECL_INITIAL (d))
22689 {
22690 /* Make sure the initializer is still constant, in case of
22691 circular dependency (template/instantiate6.C). */
22692 const_init
22693 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22694 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22695 /*asmspec_tree=*/NULL_TREE,
22696 LOOKUP_ONLYCONVERTING);
22697 }
22698 if (enter_context)
22699 pop_nested_class ();
22700 pop_nested_namespace (ns);
22701 }
22702
22703 /* We restore the source position here because it's used by
22704 add_pending_template. */
22705 input_location = saved_loc;
22706
22707 if (at_eof && !pattern_defined
22708 && DECL_EXPLICIT_INSTANTIATION (d)
22709 && DECL_NOT_REALLY_EXTERN (d))
22710 /* [temp.explicit]
22711
22712 The definition of a non-exported function template, a
22713 non-exported member function template, or a non-exported
22714 member function or static data member of a class template
22715 shall be present in every translation unit in which it is
22716 explicitly instantiated. */
22717 permerror (input_location, "explicit instantiation of %qD "
22718 "but no definition available", d);
22719
22720 /* If we're in unevaluated context, we just wanted to get the
22721 constant value; this isn't an odr use, so don't queue
22722 a full instantiation. */
22723 if (cp_unevaluated_operand != 0)
22724 goto out;
22725 /* ??? Historically, we have instantiated inline functions, even
22726 when marked as "extern template". */
22727 if (!(external_p && VAR_P (d)))
22728 add_pending_template (d);
22729 goto out;
22730 }
22731 /* Tell the repository that D is available in this translation unit
22732 -- and see if it is supposed to be instantiated here. */
22733 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22734 {
22735 /* In a PCH file, despite the fact that the repository hasn't
22736 requested instantiation in the PCH it is still possible that
22737 an instantiation will be required in a file that includes the
22738 PCH. */
22739 if (pch_file)
22740 add_pending_template (d);
22741 /* Instantiate inline functions so that the inliner can do its
22742 job, even though we'll not be emitting a copy of this
22743 function. */
22744 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22745 goto out;
22746 }
22747
22748 bool push_to_top, nested;
22749 tree fn_context;
22750 fn_context = decl_function_context (d);
22751 nested = current_function_decl != NULL_TREE;
22752 push_to_top = !(nested && fn_context == current_function_decl);
22753
22754 vec<tree> omp_privatization_save;
22755 if (nested)
22756 save_omp_privatization_clauses (omp_privatization_save);
22757
22758 if (push_to_top)
22759 push_to_top_level ();
22760 else
22761 {
22762 push_function_context ();
22763 cp_unevaluated_operand = 0;
22764 c_inhibit_evaluation_warnings = 0;
22765 }
22766
22767 /* Mark D as instantiated so that recursive calls to
22768 instantiate_decl do not try to instantiate it again. */
22769 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22770
22771 /* Regenerate the declaration in case the template has been modified
22772 by a subsequent redeclaration. */
22773 regenerate_decl_from_template (d, td, args);
22774
22775 /* We already set the file and line above. Reset them now in case
22776 they changed as a result of calling regenerate_decl_from_template. */
22777 input_location = DECL_SOURCE_LOCATION (d);
22778
22779 if (VAR_P (d))
22780 {
22781 tree init;
22782 bool const_init = false;
22783
22784 /* Clear out DECL_RTL; whatever was there before may not be right
22785 since we've reset the type of the declaration. */
22786 SET_DECL_RTL (d, NULL);
22787 DECL_IN_AGGR_P (d) = 0;
22788
22789 /* The initializer is placed in DECL_INITIAL by
22790 regenerate_decl_from_template so we don't need to
22791 push/pop_access_scope again here. Pull it out so that
22792 cp_finish_decl can process it. */
22793 init = DECL_INITIAL (d);
22794 DECL_INITIAL (d) = NULL_TREE;
22795 DECL_INITIALIZED_P (d) = 0;
22796
22797 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22798 initializer. That function will defer actual emission until
22799 we have a chance to determine linkage. */
22800 DECL_EXTERNAL (d) = 0;
22801
22802 /* Enter the scope of D so that access-checking works correctly. */
22803 bool enter_context = DECL_CLASS_SCOPE_P (d);
22804 if (enter_context)
22805 push_nested_class (DECL_CONTEXT (d));
22806
22807 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22808 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22809
22810 if (enter_context)
22811 pop_nested_class ();
22812
22813 if (variable_template_p (gen_tmpl))
22814 note_variable_template_instantiation (d);
22815 }
22816 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22817 synthesize_method (d);
22818 else if (TREE_CODE (d) == FUNCTION_DECL)
22819 {
22820 hash_map<tree, tree> *saved_local_specializations;
22821 tree tmpl_parm;
22822 tree spec_parm;
22823 tree block = NULL_TREE;
22824 tree lambda_ctx = NULL_TREE;
22825
22826 /* Save away the current list, in case we are instantiating one
22827 template from within the body of another. */
22828 saved_local_specializations = local_specializations;
22829
22830 /* Set up the list of local specializations. */
22831 local_specializations = new hash_map<tree, tree>;
22832
22833 /* Set up context. */
22834 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22835 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22836 block = push_stmt_list ();
22837 else
22838 {
22839 if (push_to_top && LAMBDA_FUNCTION_P (d))
22840 {
22841 /* When instantiating a lambda's templated function
22842 operator, we need to push the non-lambda class scope
22843 of the lambda itself so that the nested function
22844 stack is sufficiently correct to deal with this
22845 capture. */
22846 lambda_ctx = DECL_CONTEXT (d);
22847 do
22848 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22849 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22850 if (lambda_ctx)
22851 push_nested_class (lambda_ctx);
22852 }
22853 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22854 }
22855
22856 /* Some typedefs referenced from within the template code need to be
22857 access checked at template instantiation time, i.e now. These
22858 types were added to the template at parsing time. Let's get those
22859 and perform the access checks then. */
22860 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22861 args);
22862
22863 /* Create substitution entries for the parameters. */
22864 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22865 spec_parm = DECL_ARGUMENTS (d);
22866 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22867 {
22868 register_local_specialization (spec_parm, tmpl_parm);
22869 spec_parm = skip_artificial_parms_for (d, spec_parm);
22870 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22871 }
22872 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22873 {
22874 if (!DECL_PACK_P (tmpl_parm))
22875 {
22876 register_local_specialization (spec_parm, tmpl_parm);
22877 spec_parm = DECL_CHAIN (spec_parm);
22878 }
22879 else
22880 {
22881 /* Register the (value) argument pack as a specialization of
22882 TMPL_PARM, then move on. */
22883 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22884 register_local_specialization (argpack, tmpl_parm);
22885 }
22886 }
22887 gcc_assert (!spec_parm);
22888
22889 /* Substitute into the body of the function. */
22890 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22891 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22892 tf_warning_or_error, tmpl);
22893 else
22894 {
22895 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22896 tf_warning_or_error, tmpl,
22897 /*integral_constant_expression_p=*/false);
22898
22899 /* Set the current input_location to the end of the function
22900 so that finish_function knows where we are. */
22901 input_location
22902 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22903
22904 /* Remember if we saw an infinite loop in the template. */
22905 current_function_infinite_loop
22906 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22907 }
22908
22909 /* We don't need the local specializations any more. */
22910 delete local_specializations;
22911 local_specializations = saved_local_specializations;
22912
22913 /* Finish the function. */
22914 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22915 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22916 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22917 else
22918 {
22919 d = finish_function (0);
22920 expand_or_defer_fn (d);
22921 }
22922 if (lambda_ctx)
22923 pop_nested_class ();
22924
22925 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22926 cp_check_omp_declare_reduction (d);
22927 }
22928
22929 /* We're not deferring instantiation any more. */
22930 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22931
22932 if (push_to_top)
22933 pop_from_top_level ();
22934 else
22935 pop_function_context ();
22936
22937 if (nested)
22938 restore_omp_privatization_clauses (omp_privatization_save);
22939
22940 out:
22941 pop_deferring_access_checks ();
22942 timevar_pop (TV_TEMPLATE_INST);
22943 pop_tinst_level ();
22944 input_location = saved_loc;
22945 cp_unevaluated_operand = saved_unevaluated_operand;
22946 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22947
22948 return d;
22949 }
22950
22951 /* Run through the list of templates that we wish we could
22952 instantiate, and instantiate any we can. RETRIES is the
22953 number of times we retry pending template instantiation. */
22954
22955 void
22956 instantiate_pending_templates (int retries)
22957 {
22958 int reconsider;
22959 location_t saved_loc = input_location;
22960
22961 /* Instantiating templates may trigger vtable generation. This in turn
22962 may require further template instantiations. We place a limit here
22963 to avoid infinite loop. */
22964 if (pending_templates && retries >= max_tinst_depth)
22965 {
22966 tree decl = pending_templates->tinst->decl;
22967
22968 fatal_error (input_location,
22969 "template instantiation depth exceeds maximum of %d"
22970 " instantiating %q+D, possibly from virtual table generation"
22971 " (use -ftemplate-depth= to increase the maximum)",
22972 max_tinst_depth, decl);
22973 if (TREE_CODE (decl) == FUNCTION_DECL)
22974 /* Pretend that we defined it. */
22975 DECL_INITIAL (decl) = error_mark_node;
22976 return;
22977 }
22978
22979 do
22980 {
22981 struct pending_template **t = &pending_templates;
22982 struct pending_template *last = NULL;
22983 reconsider = 0;
22984 while (*t)
22985 {
22986 tree instantiation = reopen_tinst_level ((*t)->tinst);
22987 bool complete = false;
22988
22989 if (TYPE_P (instantiation))
22990 {
22991 tree fn;
22992
22993 if (!COMPLETE_TYPE_P (instantiation))
22994 {
22995 instantiate_class_template (instantiation);
22996 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22997 for (fn = TYPE_METHODS (instantiation);
22998 fn;
22999 fn = TREE_CHAIN (fn))
23000 if (! DECL_ARTIFICIAL (fn))
23001 instantiate_decl (fn,
23002 /*defer_ok=*/false,
23003 /*expl_inst_class_mem_p=*/false);
23004 if (COMPLETE_TYPE_P (instantiation))
23005 reconsider = 1;
23006 }
23007
23008 complete = COMPLETE_TYPE_P (instantiation);
23009 }
23010 else
23011 {
23012 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23013 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23014 {
23015 instantiation
23016 = instantiate_decl (instantiation,
23017 /*defer_ok=*/false,
23018 /*expl_inst_class_mem_p=*/false);
23019 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23020 reconsider = 1;
23021 }
23022
23023 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23024 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23025 }
23026
23027 if (complete)
23028 /* If INSTANTIATION has been instantiated, then we don't
23029 need to consider it again in the future. */
23030 *t = (*t)->next;
23031 else
23032 {
23033 last = *t;
23034 t = &(*t)->next;
23035 }
23036 tinst_depth = 0;
23037 current_tinst_level = NULL;
23038 }
23039 last_pending_template = last;
23040 }
23041 while (reconsider);
23042
23043 input_location = saved_loc;
23044 }
23045
23046 /* Substitute ARGVEC into T, which is a list of initializers for
23047 either base class or a non-static data member. The TREE_PURPOSEs
23048 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23049 instantiate_decl. */
23050
23051 static tree
23052 tsubst_initializer_list (tree t, tree argvec)
23053 {
23054 tree inits = NULL_TREE;
23055
23056 for (; t; t = TREE_CHAIN (t))
23057 {
23058 tree decl;
23059 tree init;
23060 tree expanded_bases = NULL_TREE;
23061 tree expanded_arguments = NULL_TREE;
23062 int i, len = 1;
23063
23064 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23065 {
23066 tree expr;
23067 tree arg;
23068
23069 /* Expand the base class expansion type into separate base
23070 classes. */
23071 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23072 tf_warning_or_error,
23073 NULL_TREE);
23074 if (expanded_bases == error_mark_node)
23075 continue;
23076
23077 /* We'll be building separate TREE_LISTs of arguments for
23078 each base. */
23079 len = TREE_VEC_LENGTH (expanded_bases);
23080 expanded_arguments = make_tree_vec (len);
23081 for (i = 0; i < len; i++)
23082 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23083
23084 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23085 expand each argument in the TREE_VALUE of t. */
23086 expr = make_node (EXPR_PACK_EXPANSION);
23087 PACK_EXPANSION_LOCAL_P (expr) = true;
23088 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23089 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23090
23091 if (TREE_VALUE (t) == void_type_node)
23092 /* VOID_TYPE_NODE is used to indicate
23093 value-initialization. */
23094 {
23095 for (i = 0; i < len; i++)
23096 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23097 }
23098 else
23099 {
23100 /* Substitute parameter packs into each argument in the
23101 TREE_LIST. */
23102 in_base_initializer = 1;
23103 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23104 {
23105 tree expanded_exprs;
23106
23107 /* Expand the argument. */
23108 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23109 expanded_exprs
23110 = tsubst_pack_expansion (expr, argvec,
23111 tf_warning_or_error,
23112 NULL_TREE);
23113 if (expanded_exprs == error_mark_node)
23114 continue;
23115
23116 /* Prepend each of the expanded expressions to the
23117 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23118 for (i = 0; i < len; i++)
23119 {
23120 TREE_VEC_ELT (expanded_arguments, i) =
23121 tree_cons (NULL_TREE,
23122 TREE_VEC_ELT (expanded_exprs, i),
23123 TREE_VEC_ELT (expanded_arguments, i));
23124 }
23125 }
23126 in_base_initializer = 0;
23127
23128 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23129 since we built them backwards. */
23130 for (i = 0; i < len; i++)
23131 {
23132 TREE_VEC_ELT (expanded_arguments, i) =
23133 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23134 }
23135 }
23136 }
23137
23138 for (i = 0; i < len; ++i)
23139 {
23140 if (expanded_bases)
23141 {
23142 decl = TREE_VEC_ELT (expanded_bases, i);
23143 decl = expand_member_init (decl);
23144 init = TREE_VEC_ELT (expanded_arguments, i);
23145 }
23146 else
23147 {
23148 tree tmp;
23149 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23150 tf_warning_or_error, NULL_TREE);
23151
23152 decl = expand_member_init (decl);
23153 if (decl && !DECL_P (decl))
23154 in_base_initializer = 1;
23155
23156 init = TREE_VALUE (t);
23157 tmp = init;
23158 if (init != void_type_node)
23159 init = tsubst_expr (init, argvec,
23160 tf_warning_or_error, NULL_TREE,
23161 /*integral_constant_expression_p=*/false);
23162 if (init == NULL_TREE && tmp != NULL_TREE)
23163 /* If we had an initializer but it instantiated to nothing,
23164 value-initialize the object. This will only occur when
23165 the initializer was a pack expansion where the parameter
23166 packs used in that expansion were of length zero. */
23167 init = void_type_node;
23168 in_base_initializer = 0;
23169 }
23170
23171 if (decl)
23172 {
23173 init = build_tree_list (decl, init);
23174 TREE_CHAIN (init) = inits;
23175 inits = init;
23176 }
23177 }
23178 }
23179 return inits;
23180 }
23181
23182 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23183
23184 static void
23185 set_current_access_from_decl (tree decl)
23186 {
23187 if (TREE_PRIVATE (decl))
23188 current_access_specifier = access_private_node;
23189 else if (TREE_PROTECTED (decl))
23190 current_access_specifier = access_protected_node;
23191 else
23192 current_access_specifier = access_public_node;
23193 }
23194
23195 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23196 is the instantiation (which should have been created with
23197 start_enum) and ARGS are the template arguments to use. */
23198
23199 static void
23200 tsubst_enum (tree tag, tree newtag, tree args)
23201 {
23202 tree e;
23203
23204 if (SCOPED_ENUM_P (newtag))
23205 begin_scope (sk_scoped_enum, newtag);
23206
23207 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23208 {
23209 tree value;
23210 tree decl;
23211
23212 decl = TREE_VALUE (e);
23213 /* Note that in a template enum, the TREE_VALUE is the
23214 CONST_DECL, not the corresponding INTEGER_CST. */
23215 value = tsubst_expr (DECL_INITIAL (decl),
23216 args, tf_warning_or_error, NULL_TREE,
23217 /*integral_constant_expression_p=*/true);
23218
23219 /* Give this enumeration constant the correct access. */
23220 set_current_access_from_decl (decl);
23221
23222 /* Actually build the enumerator itself. Here we're assuming that
23223 enumerators can't have dependent attributes. */
23224 build_enumerator (DECL_NAME (decl), value, newtag,
23225 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23226 }
23227
23228 if (SCOPED_ENUM_P (newtag))
23229 finish_scope ();
23230
23231 finish_enum_value_list (newtag);
23232 finish_enum (newtag);
23233
23234 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23235 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23236 }
23237
23238 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23239 its type -- but without substituting the innermost set of template
23240 arguments. So, innermost set of template parameters will appear in
23241 the type. */
23242
23243 tree
23244 get_mostly_instantiated_function_type (tree decl)
23245 {
23246 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23247 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23248 }
23249
23250 /* Return truthvalue if we're processing a template different from
23251 the last one involved in diagnostics. */
23252 bool
23253 problematic_instantiation_changed (void)
23254 {
23255 return current_tinst_level != last_error_tinst_level;
23256 }
23257
23258 /* Remember current template involved in diagnostics. */
23259 void
23260 record_last_problematic_instantiation (void)
23261 {
23262 last_error_tinst_level = current_tinst_level;
23263 }
23264
23265 struct tinst_level *
23266 current_instantiation (void)
23267 {
23268 return current_tinst_level;
23269 }
23270
23271 /* Return TRUE if current_function_decl is being instantiated, false
23272 otherwise. */
23273
23274 bool
23275 instantiating_current_function_p (void)
23276 {
23277 return (current_instantiation ()
23278 && current_instantiation ()->decl == current_function_decl);
23279 }
23280
23281 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23282 type. Return zero for ok, nonzero for disallowed. Issue error and
23283 warning messages under control of COMPLAIN. */
23284
23285 static int
23286 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23287 {
23288 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23289 return 0;
23290 else if (POINTER_TYPE_P (type))
23291 return 0;
23292 else if (TYPE_PTRMEM_P (type))
23293 return 0;
23294 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23295 return 0;
23296 else if (TREE_CODE (type) == TYPENAME_TYPE)
23297 return 0;
23298 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23299 return 0;
23300 else if (TREE_CODE (type) == NULLPTR_TYPE)
23301 return 0;
23302 /* A bound template template parm could later be instantiated to have a valid
23303 nontype parm type via an alias template. */
23304 else if (cxx_dialect >= cxx11
23305 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23306 return 0;
23307
23308 if (complain & tf_error)
23309 {
23310 if (type == error_mark_node)
23311 inform (input_location, "invalid template non-type parameter");
23312 else
23313 error ("%q#T is not a valid type for a template non-type parameter",
23314 type);
23315 }
23316 return 1;
23317 }
23318
23319 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23320 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23321
23322 static bool
23323 dependent_type_p_r (tree type)
23324 {
23325 tree scope;
23326
23327 /* [temp.dep.type]
23328
23329 A type is dependent if it is:
23330
23331 -- a template parameter. Template template parameters are types
23332 for us (since TYPE_P holds true for them) so we handle
23333 them here. */
23334 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23335 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23336 return true;
23337 /* -- a qualified-id with a nested-name-specifier which contains a
23338 class-name that names a dependent type or whose unqualified-id
23339 names a dependent type. */
23340 if (TREE_CODE (type) == TYPENAME_TYPE)
23341 return true;
23342
23343 /* An alias template specialization can be dependent even if the
23344 resulting type is not. */
23345 if (dependent_alias_template_spec_p (type))
23346 return true;
23347
23348 /* -- a cv-qualified type where the cv-unqualified type is
23349 dependent.
23350 No code is necessary for this bullet; the code below handles
23351 cv-qualified types, and we don't want to strip aliases with
23352 TYPE_MAIN_VARIANT because of DR 1558. */
23353 /* -- a compound type constructed from any dependent type. */
23354 if (TYPE_PTRMEM_P (type))
23355 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23356 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23357 (type)));
23358 else if (TYPE_PTR_P (type)
23359 || TREE_CODE (type) == REFERENCE_TYPE)
23360 return dependent_type_p (TREE_TYPE (type));
23361 else if (TREE_CODE (type) == FUNCTION_TYPE
23362 || TREE_CODE (type) == METHOD_TYPE)
23363 {
23364 tree arg_type;
23365
23366 if (dependent_type_p (TREE_TYPE (type)))
23367 return true;
23368 for (arg_type = TYPE_ARG_TYPES (type);
23369 arg_type;
23370 arg_type = TREE_CHAIN (arg_type))
23371 if (dependent_type_p (TREE_VALUE (arg_type)))
23372 return true;
23373 return false;
23374 }
23375 /* -- an array type constructed from any dependent type or whose
23376 size is specified by a constant expression that is
23377 value-dependent.
23378
23379 We checked for type- and value-dependence of the bounds in
23380 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23381 if (TREE_CODE (type) == ARRAY_TYPE)
23382 {
23383 if (TYPE_DOMAIN (type)
23384 && dependent_type_p (TYPE_DOMAIN (type)))
23385 return true;
23386 return dependent_type_p (TREE_TYPE (type));
23387 }
23388
23389 /* -- a template-id in which either the template name is a template
23390 parameter ... */
23391 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23392 return true;
23393 /* ... or any of the template arguments is a dependent type or
23394 an expression that is type-dependent or value-dependent. */
23395 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23396 && (any_dependent_template_arguments_p
23397 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23398 return true;
23399
23400 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23401 dependent; if the argument of the `typeof' expression is not
23402 type-dependent, then it should already been have resolved. */
23403 if (TREE_CODE (type) == TYPEOF_TYPE
23404 || TREE_CODE (type) == DECLTYPE_TYPE
23405 || TREE_CODE (type) == UNDERLYING_TYPE)
23406 return true;
23407
23408 /* A template argument pack is dependent if any of its packed
23409 arguments are. */
23410 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23411 {
23412 tree args = ARGUMENT_PACK_ARGS (type);
23413 int i, len = TREE_VEC_LENGTH (args);
23414 for (i = 0; i < len; ++i)
23415 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23416 return true;
23417 }
23418
23419 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23420 be template parameters. */
23421 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23422 return true;
23423
23424 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23425 return true;
23426
23427 /* The standard does not specifically mention types that are local
23428 to template functions or local classes, but they should be
23429 considered dependent too. For example:
23430
23431 template <int I> void f() {
23432 enum E { a = I };
23433 S<sizeof (E)> s;
23434 }
23435
23436 The size of `E' cannot be known until the value of `I' has been
23437 determined. Therefore, `E' must be considered dependent. */
23438 scope = TYPE_CONTEXT (type);
23439 if (scope && TYPE_P (scope))
23440 return dependent_type_p (scope);
23441 /* Don't use type_dependent_expression_p here, as it can lead
23442 to infinite recursion trying to determine whether a lambda
23443 nested in a lambda is dependent (c++/47687). */
23444 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23445 && DECL_LANG_SPECIFIC (scope)
23446 && DECL_TEMPLATE_INFO (scope)
23447 && (any_dependent_template_arguments_p
23448 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23449 return true;
23450
23451 /* Other types are non-dependent. */
23452 return false;
23453 }
23454
23455 /* Returns TRUE if TYPE is dependent, in the sense of
23456 [temp.dep.type]. Note that a NULL type is considered dependent. */
23457
23458 bool
23459 dependent_type_p (tree type)
23460 {
23461 /* If there are no template parameters in scope, then there can't be
23462 any dependent types. */
23463 if (!processing_template_decl)
23464 {
23465 /* If we are not processing a template, then nobody should be
23466 providing us with a dependent type. */
23467 gcc_assert (type);
23468 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23469 return false;
23470 }
23471
23472 /* If the type is NULL, we have not computed a type for the entity
23473 in question; in that case, the type is dependent. */
23474 if (!type)
23475 return true;
23476
23477 /* Erroneous types can be considered non-dependent. */
23478 if (type == error_mark_node)
23479 return false;
23480
23481 /* If we have not already computed the appropriate value for TYPE,
23482 do so now. */
23483 if (!TYPE_DEPENDENT_P_VALID (type))
23484 {
23485 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23486 TYPE_DEPENDENT_P_VALID (type) = 1;
23487 }
23488
23489 return TYPE_DEPENDENT_P (type);
23490 }
23491
23492 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23493 lookup. In other words, a dependent type that is not the current
23494 instantiation. */
23495
23496 bool
23497 dependent_scope_p (tree scope)
23498 {
23499 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23500 && !currently_open_class (scope));
23501 }
23502
23503 /* T is a SCOPE_REF; return whether we need to consider it
23504 instantiation-dependent so that we can check access at instantiation
23505 time even though we know which member it resolves to. */
23506
23507 static bool
23508 instantiation_dependent_scope_ref_p (tree t)
23509 {
23510 if (DECL_P (TREE_OPERAND (t, 1))
23511 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23512 && accessible_in_template_p (TREE_OPERAND (t, 0),
23513 TREE_OPERAND (t, 1)))
23514 return false;
23515 else
23516 return true;
23517 }
23518
23519 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23520 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23521 expression. */
23522
23523 /* Note that this predicate is not appropriate for general expressions;
23524 only constant expressions (that satisfy potential_constant_expression)
23525 can be tested for value dependence. */
23526
23527 bool
23528 value_dependent_expression_p (tree expression)
23529 {
23530 if (!processing_template_decl || expression == NULL_TREE)
23531 return false;
23532
23533 /* A name declared with a dependent type. */
23534 if (DECL_P (expression) && type_dependent_expression_p (expression))
23535 return true;
23536
23537 switch (TREE_CODE (expression))
23538 {
23539 case BASELINK:
23540 /* A dependent member function of the current instantiation. */
23541 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23542
23543 case FUNCTION_DECL:
23544 /* A dependent member function of the current instantiation. */
23545 if (DECL_CLASS_SCOPE_P (expression)
23546 && dependent_type_p (DECL_CONTEXT (expression)))
23547 return true;
23548 break;
23549
23550 case IDENTIFIER_NODE:
23551 /* A name that has not been looked up -- must be dependent. */
23552 return true;
23553
23554 case TEMPLATE_PARM_INDEX:
23555 /* A non-type template parm. */
23556 return true;
23557
23558 case CONST_DECL:
23559 /* A non-type template parm. */
23560 if (DECL_TEMPLATE_PARM_P (expression))
23561 return true;
23562 return value_dependent_expression_p (DECL_INITIAL (expression));
23563
23564 case VAR_DECL:
23565 /* A constant with literal type and is initialized
23566 with an expression that is value-dependent.
23567
23568 Note that a non-dependent parenthesized initializer will have
23569 already been replaced with its constant value, so if we see
23570 a TREE_LIST it must be dependent. */
23571 if (DECL_INITIAL (expression)
23572 && decl_constant_var_p (expression)
23573 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23574 /* cp_finish_decl doesn't fold reference initializers. */
23575 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23576 || type_dependent_expression_p (DECL_INITIAL (expression))
23577 || value_dependent_expression_p (DECL_INITIAL (expression))))
23578 return true;
23579 return false;
23580
23581 case DYNAMIC_CAST_EXPR:
23582 case STATIC_CAST_EXPR:
23583 case CONST_CAST_EXPR:
23584 case REINTERPRET_CAST_EXPR:
23585 case CAST_EXPR:
23586 /* These expressions are value-dependent if the type to which
23587 the cast occurs is dependent or the expression being casted
23588 is value-dependent. */
23589 {
23590 tree type = TREE_TYPE (expression);
23591
23592 if (dependent_type_p (type))
23593 return true;
23594
23595 /* A functional cast has a list of operands. */
23596 expression = TREE_OPERAND (expression, 0);
23597 if (!expression)
23598 {
23599 /* If there are no operands, it must be an expression such
23600 as "int()". This should not happen for aggregate types
23601 because it would form non-constant expressions. */
23602 gcc_assert (cxx_dialect >= cxx11
23603 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23604
23605 return false;
23606 }
23607
23608 if (TREE_CODE (expression) == TREE_LIST)
23609 return any_value_dependent_elements_p (expression);
23610
23611 return value_dependent_expression_p (expression);
23612 }
23613
23614 case SIZEOF_EXPR:
23615 if (SIZEOF_EXPR_TYPE_P (expression))
23616 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23617 /* FALLTHRU */
23618 case ALIGNOF_EXPR:
23619 case TYPEID_EXPR:
23620 /* A `sizeof' expression is value-dependent if the operand is
23621 type-dependent or is a pack expansion. */
23622 expression = TREE_OPERAND (expression, 0);
23623 if (PACK_EXPANSION_P (expression))
23624 return true;
23625 else if (TYPE_P (expression))
23626 return dependent_type_p (expression);
23627 return instantiation_dependent_uneval_expression_p (expression);
23628
23629 case AT_ENCODE_EXPR:
23630 /* An 'encode' expression is value-dependent if the operand is
23631 type-dependent. */
23632 expression = TREE_OPERAND (expression, 0);
23633 return dependent_type_p (expression);
23634
23635 case NOEXCEPT_EXPR:
23636 expression = TREE_OPERAND (expression, 0);
23637 return instantiation_dependent_uneval_expression_p (expression);
23638
23639 case SCOPE_REF:
23640 /* All instantiation-dependent expressions should also be considered
23641 value-dependent. */
23642 return instantiation_dependent_scope_ref_p (expression);
23643
23644 case COMPONENT_REF:
23645 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23646 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23647
23648 case NONTYPE_ARGUMENT_PACK:
23649 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23650 is value-dependent. */
23651 {
23652 tree values = ARGUMENT_PACK_ARGS (expression);
23653 int i, len = TREE_VEC_LENGTH (values);
23654
23655 for (i = 0; i < len; ++i)
23656 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23657 return true;
23658
23659 return false;
23660 }
23661
23662 case TRAIT_EXPR:
23663 {
23664 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23665 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23666 || (type2 ? dependent_type_p (type2) : false));
23667 }
23668
23669 case MODOP_EXPR:
23670 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23671 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23672
23673 case ARRAY_REF:
23674 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23675 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23676
23677 case ADDR_EXPR:
23678 {
23679 tree op = TREE_OPERAND (expression, 0);
23680 return (value_dependent_expression_p (op)
23681 || has_value_dependent_address (op));
23682 }
23683
23684 case REQUIRES_EXPR:
23685 /* Treat all requires-expressions as value-dependent so
23686 we don't try to fold them. */
23687 return true;
23688
23689 case TYPE_REQ:
23690 return dependent_type_p (TREE_OPERAND (expression, 0));
23691
23692 case CALL_EXPR:
23693 {
23694 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23695 return true;
23696 tree fn = get_callee_fndecl (expression);
23697 int i, nargs;
23698 nargs = call_expr_nargs (expression);
23699 for (i = 0; i < nargs; ++i)
23700 {
23701 tree op = CALL_EXPR_ARG (expression, i);
23702 /* In a call to a constexpr member function, look through the
23703 implicit ADDR_EXPR on the object argument so that it doesn't
23704 cause the call to be considered value-dependent. We also
23705 look through it in potential_constant_expression. */
23706 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23707 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23708 && TREE_CODE (op) == ADDR_EXPR)
23709 op = TREE_OPERAND (op, 0);
23710 if (value_dependent_expression_p (op))
23711 return true;
23712 }
23713 return false;
23714 }
23715
23716 case TEMPLATE_ID_EXPR:
23717 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23718 type-dependent. */
23719 return type_dependent_expression_p (expression)
23720 || variable_concept_p (TREE_OPERAND (expression, 0));
23721
23722 case CONSTRUCTOR:
23723 {
23724 unsigned ix;
23725 tree val;
23726 if (dependent_type_p (TREE_TYPE (expression)))
23727 return true;
23728 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23729 if (value_dependent_expression_p (val))
23730 return true;
23731 return false;
23732 }
23733
23734 case STMT_EXPR:
23735 /* Treat a GNU statement expression as dependent to avoid crashing
23736 under instantiate_non_dependent_expr; it can't be constant. */
23737 return true;
23738
23739 default:
23740 /* A constant expression is value-dependent if any subexpression is
23741 value-dependent. */
23742 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23743 {
23744 case tcc_reference:
23745 case tcc_unary:
23746 case tcc_comparison:
23747 case tcc_binary:
23748 case tcc_expression:
23749 case tcc_vl_exp:
23750 {
23751 int i, len = cp_tree_operand_length (expression);
23752
23753 for (i = 0; i < len; i++)
23754 {
23755 tree t = TREE_OPERAND (expression, i);
23756
23757 /* In some cases, some of the operands may be missing.l
23758 (For example, in the case of PREDECREMENT_EXPR, the
23759 amount to increment by may be missing.) That doesn't
23760 make the expression dependent. */
23761 if (t && value_dependent_expression_p (t))
23762 return true;
23763 }
23764 }
23765 break;
23766 default:
23767 break;
23768 }
23769 break;
23770 }
23771
23772 /* The expression is not value-dependent. */
23773 return false;
23774 }
23775
23776 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23777 [temp.dep.expr]. Note that an expression with no type is
23778 considered dependent. Other parts of the compiler arrange for an
23779 expression with type-dependent subexpressions to have no type, so
23780 this function doesn't have to be fully recursive. */
23781
23782 bool
23783 type_dependent_expression_p (tree expression)
23784 {
23785 if (!processing_template_decl)
23786 return false;
23787
23788 if (expression == NULL_TREE || expression == error_mark_node)
23789 return false;
23790
23791 /* An unresolved name is always dependent. */
23792 if (identifier_p (expression)
23793 || TREE_CODE (expression) == USING_DECL
23794 || TREE_CODE (expression) == WILDCARD_DECL)
23795 return true;
23796
23797 /* A fold expression is type-dependent. */
23798 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23799 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23800 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23801 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23802 return true;
23803
23804 /* Some expression forms are never type-dependent. */
23805 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23806 || TREE_CODE (expression) == SIZEOF_EXPR
23807 || TREE_CODE (expression) == ALIGNOF_EXPR
23808 || TREE_CODE (expression) == AT_ENCODE_EXPR
23809 || TREE_CODE (expression) == NOEXCEPT_EXPR
23810 || TREE_CODE (expression) == TRAIT_EXPR
23811 || TREE_CODE (expression) == TYPEID_EXPR
23812 || TREE_CODE (expression) == DELETE_EXPR
23813 || TREE_CODE (expression) == VEC_DELETE_EXPR
23814 || TREE_CODE (expression) == THROW_EXPR
23815 || TREE_CODE (expression) == REQUIRES_EXPR)
23816 return false;
23817
23818 /* The types of these expressions depends only on the type to which
23819 the cast occurs. */
23820 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23821 || TREE_CODE (expression) == STATIC_CAST_EXPR
23822 || TREE_CODE (expression) == CONST_CAST_EXPR
23823 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23824 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23825 || TREE_CODE (expression) == CAST_EXPR)
23826 return dependent_type_p (TREE_TYPE (expression));
23827
23828 /* The types of these expressions depends only on the type created
23829 by the expression. */
23830 if (TREE_CODE (expression) == NEW_EXPR
23831 || TREE_CODE (expression) == VEC_NEW_EXPR)
23832 {
23833 /* For NEW_EXPR tree nodes created inside a template, either
23834 the object type itself or a TREE_LIST may appear as the
23835 operand 1. */
23836 tree type = TREE_OPERAND (expression, 1);
23837 if (TREE_CODE (type) == TREE_LIST)
23838 /* This is an array type. We need to check array dimensions
23839 as well. */
23840 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23841 || value_dependent_expression_p
23842 (TREE_OPERAND (TREE_VALUE (type), 1));
23843 else
23844 return dependent_type_p (type);
23845 }
23846
23847 if (TREE_CODE (expression) == SCOPE_REF)
23848 {
23849 tree scope = TREE_OPERAND (expression, 0);
23850 tree name = TREE_OPERAND (expression, 1);
23851
23852 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23853 contains an identifier associated by name lookup with one or more
23854 declarations declared with a dependent type, or...a
23855 nested-name-specifier or qualified-id that names a member of an
23856 unknown specialization. */
23857 return (type_dependent_expression_p (name)
23858 || dependent_scope_p (scope));
23859 }
23860
23861 if (TREE_CODE (expression) == TEMPLATE_DECL
23862 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23863 return uses_outer_template_parms (expression);
23864
23865 if (TREE_CODE (expression) == STMT_EXPR)
23866 expression = stmt_expr_value_expr (expression);
23867
23868 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23869 {
23870 tree elt;
23871 unsigned i;
23872
23873 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23874 {
23875 if (type_dependent_expression_p (elt))
23876 return true;
23877 }
23878 return false;
23879 }
23880
23881 /* A static data member of the current instantiation with incomplete
23882 array type is type-dependent, as the definition and specializations
23883 can have different bounds. */
23884 if (VAR_P (expression)
23885 && DECL_CLASS_SCOPE_P (expression)
23886 && dependent_type_p (DECL_CONTEXT (expression))
23887 && VAR_HAD_UNKNOWN_BOUND (expression))
23888 return true;
23889
23890 /* An array of unknown bound depending on a variadic parameter, eg:
23891
23892 template<typename... Args>
23893 void foo (Args... args)
23894 {
23895 int arr[] = { args... };
23896 }
23897
23898 template<int... vals>
23899 void bar ()
23900 {
23901 int arr[] = { vals... };
23902 }
23903
23904 If the array has no length and has an initializer, it must be that
23905 we couldn't determine its length in cp_complete_array_type because
23906 it is dependent. */
23907 if (VAR_P (expression)
23908 && TREE_TYPE (expression) != NULL_TREE
23909 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23910 && !TYPE_DOMAIN (TREE_TYPE (expression))
23911 && DECL_INITIAL (expression))
23912 return true;
23913
23914 /* A function or variable template-id is type-dependent if it has any
23915 dependent template arguments. Note that we only consider the innermost
23916 template arguments here, since those are the ones that come from the
23917 template-id; the template arguments for the enclosing class do not make it
23918 type-dependent, they only make a member function value-dependent. */
23919 if (VAR_OR_FUNCTION_DECL_P (expression)
23920 && DECL_LANG_SPECIFIC (expression)
23921 && DECL_TEMPLATE_INFO (expression)
23922 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23923 && (any_dependent_template_arguments_p
23924 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23925 return true;
23926
23927 /* Always dependent, on the number of arguments if nothing else. */
23928 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23929 return true;
23930
23931 if (TREE_TYPE (expression) == unknown_type_node)
23932 {
23933 if (TREE_CODE (expression) == ADDR_EXPR)
23934 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23935 if (TREE_CODE (expression) == COMPONENT_REF
23936 || TREE_CODE (expression) == OFFSET_REF)
23937 {
23938 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23939 return true;
23940 expression = TREE_OPERAND (expression, 1);
23941 if (identifier_p (expression))
23942 return false;
23943 }
23944 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23945 if (TREE_CODE (expression) == SCOPE_REF)
23946 return false;
23947
23948 if (BASELINK_P (expression))
23949 {
23950 if (BASELINK_OPTYPE (expression)
23951 && dependent_type_p (BASELINK_OPTYPE (expression)))
23952 return true;
23953 expression = BASELINK_FUNCTIONS (expression);
23954 }
23955
23956 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23957 {
23958 if (any_dependent_template_arguments_p
23959 (TREE_OPERAND (expression, 1)))
23960 return true;
23961 expression = TREE_OPERAND (expression, 0);
23962 if (identifier_p (expression))
23963 return true;
23964 }
23965
23966 gcc_assert (TREE_CODE (expression) == OVERLOAD
23967 || TREE_CODE (expression) == FUNCTION_DECL);
23968
23969 while (expression)
23970 {
23971 if (type_dependent_expression_p (OVL_CURRENT (expression)))
23972 return true;
23973 expression = OVL_NEXT (expression);
23974 }
23975 return false;
23976 }
23977
23978 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23979
23980 /* Dependent type attributes might not have made it from the decl to
23981 the type yet. */
23982 if (DECL_P (expression)
23983 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
23984 return true;
23985
23986 return (dependent_type_p (TREE_TYPE (expression)));
23987 }
23988
23989 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23990 type-dependent if the expression refers to a member of the current
23991 instantiation and the type of the referenced member is dependent, or the
23992 class member access expression refers to a member of an unknown
23993 specialization.
23994
23995 This function returns true if the OBJECT in such a class member access
23996 expression is of an unknown specialization. */
23997
23998 bool
23999 type_dependent_object_expression_p (tree object)
24000 {
24001 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24002 dependent. */
24003 if (TREE_CODE (object) == IDENTIFIER_NODE)
24004 return true;
24005 tree scope = TREE_TYPE (object);
24006 return (!scope || dependent_scope_p (scope));
24007 }
24008
24009 /* walk_tree callback function for instantiation_dependent_expression_p,
24010 below. Returns non-zero if a dependent subexpression is found. */
24011
24012 static tree
24013 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24014 void * /*data*/)
24015 {
24016 if (TYPE_P (*tp))
24017 {
24018 /* We don't have to worry about decltype currently because decltype
24019 of an instantiation-dependent expr is a dependent type. This
24020 might change depending on the resolution of DR 1172. */
24021 *walk_subtrees = false;
24022 return NULL_TREE;
24023 }
24024 enum tree_code code = TREE_CODE (*tp);
24025 switch (code)
24026 {
24027 /* Don't treat an argument list as dependent just because it has no
24028 TREE_TYPE. */
24029 case TREE_LIST:
24030 case TREE_VEC:
24031 return NULL_TREE;
24032
24033 case TEMPLATE_PARM_INDEX:
24034 return *tp;
24035
24036 /* Handle expressions with type operands. */
24037 case SIZEOF_EXPR:
24038 case ALIGNOF_EXPR:
24039 case TYPEID_EXPR:
24040 case AT_ENCODE_EXPR:
24041 {
24042 tree op = TREE_OPERAND (*tp, 0);
24043 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24044 op = TREE_TYPE (op);
24045 if (TYPE_P (op))
24046 {
24047 if (dependent_type_p (op))
24048 return *tp;
24049 else
24050 {
24051 *walk_subtrees = false;
24052 return NULL_TREE;
24053 }
24054 }
24055 break;
24056 }
24057
24058 case COMPONENT_REF:
24059 if (identifier_p (TREE_OPERAND (*tp, 1)))
24060 /* In a template, finish_class_member_access_expr creates a
24061 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24062 type-dependent, so that we can check access control at
24063 instantiation time (PR 42277). See also Core issue 1273. */
24064 return *tp;
24065 break;
24066
24067 case SCOPE_REF:
24068 if (instantiation_dependent_scope_ref_p (*tp))
24069 return *tp;
24070 else
24071 break;
24072
24073 /* Treat statement-expressions as dependent. */
24074 case BIND_EXPR:
24075 return *tp;
24076
24077 /* Treat requires-expressions as dependent. */
24078 case REQUIRES_EXPR:
24079 return *tp;
24080
24081 case CALL_EXPR:
24082 /* Treat calls to function concepts as dependent. */
24083 if (function_concept_check_p (*tp))
24084 return *tp;
24085 break;
24086
24087 case TEMPLATE_ID_EXPR:
24088 /* And variable concepts. */
24089 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24090 return *tp;
24091 break;
24092
24093 default:
24094 break;
24095 }
24096
24097 if (type_dependent_expression_p (*tp))
24098 return *tp;
24099 else
24100 return NULL_TREE;
24101 }
24102
24103 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24104 sense defined by the ABI:
24105
24106 "An expression is instantiation-dependent if it is type-dependent
24107 or value-dependent, or it has a subexpression that is type-dependent
24108 or value-dependent."
24109
24110 Except don't actually check value-dependence for unevaluated expressions,
24111 because in sizeof(i) we don't care about the value of i. Checking
24112 type-dependence will in turn check value-dependence of array bounds/template
24113 arguments as needed. */
24114
24115 bool
24116 instantiation_dependent_uneval_expression_p (tree expression)
24117 {
24118 tree result;
24119
24120 if (!processing_template_decl)
24121 return false;
24122
24123 if (expression == error_mark_node)
24124 return false;
24125
24126 result = cp_walk_tree_without_duplicates (&expression,
24127 instantiation_dependent_r, NULL);
24128 return result != NULL_TREE;
24129 }
24130
24131 /* As above, but also check value-dependence of the expression as a whole. */
24132
24133 bool
24134 instantiation_dependent_expression_p (tree expression)
24135 {
24136 return (instantiation_dependent_uneval_expression_p (expression)
24137 || value_dependent_expression_p (expression));
24138 }
24139
24140 /* Like type_dependent_expression_p, but it also works while not processing
24141 a template definition, i.e. during substitution or mangling. */
24142
24143 bool
24144 type_dependent_expression_p_push (tree expr)
24145 {
24146 bool b;
24147 ++processing_template_decl;
24148 b = type_dependent_expression_p (expr);
24149 --processing_template_decl;
24150 return b;
24151 }
24152
24153 /* Returns TRUE if ARGS contains a type-dependent expression. */
24154
24155 bool
24156 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24157 {
24158 unsigned int i;
24159 tree arg;
24160
24161 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24162 {
24163 if (type_dependent_expression_p (arg))
24164 return true;
24165 }
24166 return false;
24167 }
24168
24169 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24170 expressions) contains any type-dependent expressions. */
24171
24172 bool
24173 any_type_dependent_elements_p (const_tree list)
24174 {
24175 for (; list; list = TREE_CHAIN (list))
24176 if (type_dependent_expression_p (TREE_VALUE (list)))
24177 return true;
24178
24179 return false;
24180 }
24181
24182 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24183 expressions) contains any value-dependent expressions. */
24184
24185 bool
24186 any_value_dependent_elements_p (const_tree list)
24187 {
24188 for (; list; list = TREE_CHAIN (list))
24189 if (value_dependent_expression_p (TREE_VALUE (list)))
24190 return true;
24191
24192 return false;
24193 }
24194
24195 /* Returns TRUE if the ARG (a template argument) is dependent. */
24196
24197 bool
24198 dependent_template_arg_p (tree arg)
24199 {
24200 if (!processing_template_decl)
24201 return false;
24202
24203 /* Assume a template argument that was wrongly written by the user
24204 is dependent. This is consistent with what
24205 any_dependent_template_arguments_p [that calls this function]
24206 does. */
24207 if (!arg || arg == error_mark_node)
24208 return true;
24209
24210 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24211 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24212
24213 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24214 return true;
24215 if (TREE_CODE (arg) == TEMPLATE_DECL)
24216 {
24217 if (DECL_TEMPLATE_PARM_P (arg))
24218 return true;
24219 /* A member template of a dependent class is not necessarily
24220 type-dependent, but it is a dependent template argument because it
24221 will be a member of an unknown specialization to that template. */
24222 tree scope = CP_DECL_CONTEXT (arg);
24223 return TYPE_P (scope) && dependent_type_p (scope);
24224 }
24225 else if (ARGUMENT_PACK_P (arg))
24226 {
24227 tree args = ARGUMENT_PACK_ARGS (arg);
24228 int i, len = TREE_VEC_LENGTH (args);
24229 for (i = 0; i < len; ++i)
24230 {
24231 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24232 return true;
24233 }
24234
24235 return false;
24236 }
24237 else if (TYPE_P (arg))
24238 return dependent_type_p (arg);
24239 else
24240 return (type_dependent_expression_p (arg)
24241 || value_dependent_expression_p (arg));
24242 }
24243
24244 /* Returns true if ARGS (a collection of template arguments) contains
24245 any types that require structural equality testing. */
24246
24247 bool
24248 any_template_arguments_need_structural_equality_p (tree args)
24249 {
24250 int i;
24251 int j;
24252
24253 if (!args)
24254 return false;
24255 if (args == error_mark_node)
24256 return true;
24257
24258 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24259 {
24260 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24261 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24262 {
24263 tree arg = TREE_VEC_ELT (level, j);
24264 tree packed_args = NULL_TREE;
24265 int k, len = 1;
24266
24267 if (ARGUMENT_PACK_P (arg))
24268 {
24269 /* Look inside the argument pack. */
24270 packed_args = ARGUMENT_PACK_ARGS (arg);
24271 len = TREE_VEC_LENGTH (packed_args);
24272 }
24273
24274 for (k = 0; k < len; ++k)
24275 {
24276 if (packed_args)
24277 arg = TREE_VEC_ELT (packed_args, k);
24278
24279 if (error_operand_p (arg))
24280 return true;
24281 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24282 continue;
24283 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24284 return true;
24285 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24286 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24287 return true;
24288 }
24289 }
24290 }
24291
24292 return false;
24293 }
24294
24295 /* Returns true if ARGS (a collection of template arguments) contains
24296 any dependent arguments. */
24297
24298 bool
24299 any_dependent_template_arguments_p (const_tree args)
24300 {
24301 int i;
24302 int j;
24303
24304 if (!args)
24305 return false;
24306 if (args == error_mark_node)
24307 return true;
24308
24309 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24310 {
24311 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24312 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24313 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24314 return true;
24315 }
24316
24317 return false;
24318 }
24319
24320 /* Returns TRUE if the template TMPL is type-dependent. */
24321
24322 bool
24323 dependent_template_p (tree tmpl)
24324 {
24325 if (TREE_CODE (tmpl) == OVERLOAD)
24326 {
24327 while (tmpl)
24328 {
24329 if (dependent_template_p (OVL_CURRENT (tmpl)))
24330 return true;
24331 tmpl = OVL_NEXT (tmpl);
24332 }
24333 return false;
24334 }
24335
24336 /* Template template parameters are dependent. */
24337 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24338 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24339 return true;
24340 /* So are names that have not been looked up. */
24341 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24342 return true;
24343 return false;
24344 }
24345
24346 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24347
24348 bool
24349 dependent_template_id_p (tree tmpl, tree args)
24350 {
24351 return (dependent_template_p (tmpl)
24352 || any_dependent_template_arguments_p (args));
24353 }
24354
24355 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24356 are dependent. */
24357
24358 bool
24359 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24360 {
24361 int i;
24362
24363 if (!processing_template_decl)
24364 return false;
24365
24366 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24367 {
24368 tree decl = TREE_VEC_ELT (declv, i);
24369 tree init = TREE_VEC_ELT (initv, i);
24370 tree cond = TREE_VEC_ELT (condv, i);
24371 tree incr = TREE_VEC_ELT (incrv, i);
24372
24373 if (type_dependent_expression_p (decl)
24374 || TREE_CODE (decl) == SCOPE_REF)
24375 return true;
24376
24377 if (init && type_dependent_expression_p (init))
24378 return true;
24379
24380 if (type_dependent_expression_p (cond))
24381 return true;
24382
24383 if (COMPARISON_CLASS_P (cond)
24384 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24385 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24386 return true;
24387
24388 if (TREE_CODE (incr) == MODOP_EXPR)
24389 {
24390 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24391 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24392 return true;
24393 }
24394 else if (type_dependent_expression_p (incr))
24395 return true;
24396 else if (TREE_CODE (incr) == MODIFY_EXPR)
24397 {
24398 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24399 return true;
24400 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24401 {
24402 tree t = TREE_OPERAND (incr, 1);
24403 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24404 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24405 return true;
24406 }
24407 }
24408 }
24409
24410 return false;
24411 }
24412
24413 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24414 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24415 no such TYPE can be found. Note that this function peers inside
24416 uninstantiated templates and therefore should be used only in
24417 extremely limited situations. ONLY_CURRENT_P restricts this
24418 peering to the currently open classes hierarchy (which is required
24419 when comparing types). */
24420
24421 tree
24422 resolve_typename_type (tree type, bool only_current_p)
24423 {
24424 tree scope;
24425 tree name;
24426 tree decl;
24427 int quals;
24428 tree pushed_scope;
24429 tree result;
24430
24431 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24432
24433 scope = TYPE_CONTEXT (type);
24434 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24435 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24436 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24437 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24438 identifier of the TYPENAME_TYPE anymore.
24439 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24440 TYPENAME_TYPE instead, we avoid messing up with a possible
24441 typedef variant case. */
24442 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24443
24444 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24445 it first before we can figure out what NAME refers to. */
24446 if (TREE_CODE (scope) == TYPENAME_TYPE)
24447 {
24448 if (TYPENAME_IS_RESOLVING_P (scope))
24449 /* Given a class template A with a dependent base with nested type C,
24450 typedef typename A::C::C C will land us here, as trying to resolve
24451 the initial A::C leads to the local C typedef, which leads back to
24452 A::C::C. So we break the recursion now. */
24453 return type;
24454 else
24455 scope = resolve_typename_type (scope, only_current_p);
24456 }
24457 /* If we don't know what SCOPE refers to, then we cannot resolve the
24458 TYPENAME_TYPE. */
24459 if (!CLASS_TYPE_P (scope))
24460 return type;
24461 /* If this is a typedef, we don't want to look inside (c++/11987). */
24462 if (typedef_variant_p (type))
24463 return type;
24464 /* If SCOPE isn't the template itself, it will not have a valid
24465 TYPE_FIELDS list. */
24466 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24467 /* scope is either the template itself or a compatible instantiation
24468 like X<T>, so look up the name in the original template. */
24469 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24470 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24471 gcc_checking_assert (uses_template_parms (scope));
24472 /* If scope has no fields, it can't be a current instantiation. Check this
24473 before currently_open_class to avoid infinite recursion (71515). */
24474 if (!TYPE_FIELDS (scope))
24475 return type;
24476 /* If the SCOPE is not the current instantiation, there's no reason
24477 to look inside it. */
24478 if (only_current_p && !currently_open_class (scope))
24479 return type;
24480 /* Enter the SCOPE so that name lookup will be resolved as if we
24481 were in the class definition. In particular, SCOPE will no
24482 longer be considered a dependent type. */
24483 pushed_scope = push_scope (scope);
24484 /* Look up the declaration. */
24485 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24486 tf_warning_or_error);
24487
24488 result = NULL_TREE;
24489
24490 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24491 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24492 if (!decl)
24493 /*nop*/;
24494 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24495 && TREE_CODE (decl) == TYPE_DECL)
24496 {
24497 result = TREE_TYPE (decl);
24498 if (result == error_mark_node)
24499 result = NULL_TREE;
24500 }
24501 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24502 && DECL_CLASS_TEMPLATE_P (decl))
24503 {
24504 tree tmpl;
24505 tree args;
24506 /* Obtain the template and the arguments. */
24507 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24508 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24509 /* Instantiate the template. */
24510 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24511 /*entering_scope=*/0,
24512 tf_error | tf_user);
24513 if (result == error_mark_node)
24514 result = NULL_TREE;
24515 }
24516
24517 /* Leave the SCOPE. */
24518 if (pushed_scope)
24519 pop_scope (pushed_scope);
24520
24521 /* If we failed to resolve it, return the original typename. */
24522 if (!result)
24523 return type;
24524
24525 /* If lookup found a typename type, resolve that too. */
24526 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24527 {
24528 /* Ill-formed programs can cause infinite recursion here, so we
24529 must catch that. */
24530 TYPENAME_IS_RESOLVING_P (result) = 1;
24531 result = resolve_typename_type (result, only_current_p);
24532 TYPENAME_IS_RESOLVING_P (result) = 0;
24533 }
24534
24535 /* Qualify the resulting type. */
24536 quals = cp_type_quals (type);
24537 if (quals)
24538 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24539
24540 return result;
24541 }
24542
24543 /* EXPR is an expression which is not type-dependent. Return a proxy
24544 for EXPR that can be used to compute the types of larger
24545 expressions containing EXPR. */
24546
24547 tree
24548 build_non_dependent_expr (tree expr)
24549 {
24550 tree inner_expr;
24551
24552 /* When checking, try to get a constant value for all non-dependent
24553 expressions in order to expose bugs in *_dependent_expression_p
24554 and constexpr. This can affect code generation, see PR70704, so
24555 only do this for -fchecking=2. */
24556 if (flag_checking > 1
24557 && cxx_dialect >= cxx11
24558 /* Don't do this during nsdmi parsing as it can lead to
24559 unexpected recursive instantiations. */
24560 && !parsing_nsdmi ()
24561 /* Don't do this during concept expansion either and for
24562 the same reason. */
24563 && !expanding_concept ())
24564 fold_non_dependent_expr (expr);
24565
24566 /* Preserve OVERLOADs; the functions must be available to resolve
24567 types. */
24568 inner_expr = expr;
24569 if (TREE_CODE (inner_expr) == STMT_EXPR)
24570 inner_expr = stmt_expr_value_expr (inner_expr);
24571 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24572 inner_expr = TREE_OPERAND (inner_expr, 0);
24573 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24574 inner_expr = TREE_OPERAND (inner_expr, 1);
24575 if (is_overloaded_fn (inner_expr)
24576 || TREE_CODE (inner_expr) == OFFSET_REF)
24577 return expr;
24578 /* There is no need to return a proxy for a variable. */
24579 if (VAR_P (expr))
24580 return expr;
24581 /* Preserve string constants; conversions from string constants to
24582 "char *" are allowed, even though normally a "const char *"
24583 cannot be used to initialize a "char *". */
24584 if (TREE_CODE (expr) == STRING_CST)
24585 return expr;
24586 /* Preserve void and arithmetic constants, as an optimization -- there is no
24587 reason to create a new node. */
24588 if (TREE_CODE (expr) == VOID_CST
24589 || TREE_CODE (expr) == INTEGER_CST
24590 || TREE_CODE (expr) == REAL_CST)
24591 return expr;
24592 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24593 There is at least one place where we want to know that a
24594 particular expression is a throw-expression: when checking a ?:
24595 expression, there are special rules if the second or third
24596 argument is a throw-expression. */
24597 if (TREE_CODE (expr) == THROW_EXPR)
24598 return expr;
24599
24600 /* Don't wrap an initializer list, we need to be able to look inside. */
24601 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24602 return expr;
24603
24604 /* Don't wrap a dummy object, we need to be able to test for it. */
24605 if (is_dummy_object (expr))
24606 return expr;
24607
24608 if (TREE_CODE (expr) == COND_EXPR)
24609 return build3 (COND_EXPR,
24610 TREE_TYPE (expr),
24611 TREE_OPERAND (expr, 0),
24612 (TREE_OPERAND (expr, 1)
24613 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24614 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24615 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24616 if (TREE_CODE (expr) == COMPOUND_EXPR
24617 && !COMPOUND_EXPR_OVERLOADED (expr))
24618 return build2 (COMPOUND_EXPR,
24619 TREE_TYPE (expr),
24620 TREE_OPERAND (expr, 0),
24621 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24622
24623 /* If the type is unknown, it can't really be non-dependent */
24624 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24625
24626 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24627 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24628 }
24629
24630 /* ARGS is a vector of expressions as arguments to a function call.
24631 Replace the arguments with equivalent non-dependent expressions.
24632 This modifies ARGS in place. */
24633
24634 void
24635 make_args_non_dependent (vec<tree, va_gc> *args)
24636 {
24637 unsigned int ix;
24638 tree arg;
24639
24640 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24641 {
24642 tree newarg = build_non_dependent_expr (arg);
24643 if (newarg != arg)
24644 (*args)[ix] = newarg;
24645 }
24646 }
24647
24648 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24649 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24650 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24651
24652 static tree
24653 make_auto_1 (tree name, bool set_canonical)
24654 {
24655 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24656 TYPE_NAME (au) = build_decl (input_location,
24657 TYPE_DECL, name, au);
24658 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24659 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24660 (0, processing_template_decl + 1, processing_template_decl + 1,
24661 TYPE_NAME (au), NULL_TREE);
24662 if (set_canonical)
24663 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24664 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24665 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24666
24667 return au;
24668 }
24669
24670 tree
24671 make_decltype_auto (void)
24672 {
24673 return make_auto_1 (decltype_auto_identifier, true);
24674 }
24675
24676 tree
24677 make_auto (void)
24678 {
24679 return make_auto_1 (auto_identifier, true);
24680 }
24681
24682 /* Return a C++17 deduction placeholder for class template TMPL. */
24683
24684 tree
24685 make_template_placeholder (tree tmpl)
24686 {
24687 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24688 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24689 return t;
24690 }
24691
24692 /* Make a "constrained auto" type-specifier. This is an
24693 auto type with constraints that must be associated after
24694 deduction. The constraint is formed from the given
24695 CONC and its optional sequence of arguments, which are
24696 non-null if written as partial-concept-id. */
24697
24698 tree
24699 make_constrained_auto (tree con, tree args)
24700 {
24701 tree type = make_auto_1 (auto_identifier, false);
24702
24703 /* Build the constraint. */
24704 tree tmpl = DECL_TI_TEMPLATE (con);
24705 tree expr;
24706 if (VAR_P (con))
24707 expr = build_concept_check (tmpl, type, args);
24708 else
24709 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
24710
24711 tree constr = normalize_expression (expr);
24712 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24713
24714 /* Our canonical type depends on the constraint. */
24715 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24716
24717 /* Attach the constraint to the type declaration. */
24718 tree decl = TYPE_NAME (type);
24719 return decl;
24720 }
24721
24722 /* Given type ARG, return std::initializer_list<ARG>. */
24723
24724 static tree
24725 listify (tree arg)
24726 {
24727 tree std_init_list = namespace_binding
24728 (get_identifier ("initializer_list"), std_node);
24729 tree argvec;
24730 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24731 {
24732 error ("deducing from brace-enclosed initializer list requires "
24733 "#include <initializer_list>");
24734 return error_mark_node;
24735 }
24736 argvec = make_tree_vec (1);
24737 TREE_VEC_ELT (argvec, 0) = arg;
24738 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24739 NULL_TREE, 0, tf_warning_or_error);
24740 }
24741
24742 /* Replace auto in TYPE with std::initializer_list<auto>. */
24743
24744 static tree
24745 listify_autos (tree type, tree auto_node)
24746 {
24747 tree init_auto = listify (auto_node);
24748 tree argvec = make_tree_vec (1);
24749 TREE_VEC_ELT (argvec, 0) = init_auto;
24750 if (processing_template_decl)
24751 argvec = add_to_template_args (current_template_args (), argvec);
24752 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24753 }
24754
24755 /* Hash traits for hashing possibly constrained 'auto'
24756 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24757
24758 struct auto_hash : default_hash_traits<tree>
24759 {
24760 static inline hashval_t hash (tree);
24761 static inline bool equal (tree, tree);
24762 };
24763
24764 /* Hash the 'auto' T. */
24765
24766 inline hashval_t
24767 auto_hash::hash (tree t)
24768 {
24769 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24770 /* Matching constrained-type-specifiers denote the same template
24771 parameter, so hash the constraint. */
24772 return hash_placeholder_constraint (c);
24773 else
24774 /* But unconstrained autos are all separate, so just hash the pointer. */
24775 return iterative_hash_object (t, 0);
24776 }
24777
24778 /* Compare two 'auto's. */
24779
24780 inline bool
24781 auto_hash::equal (tree t1, tree t2)
24782 {
24783 if (t1 == t2)
24784 return true;
24785
24786 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24787 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24788
24789 /* Two unconstrained autos are distinct. */
24790 if (!c1 || !c2)
24791 return false;
24792
24793 return equivalent_placeholder_constraints (c1, c2);
24794 }
24795
24796 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24797 constrained) auto, add it to the vector. */
24798
24799 static int
24800 extract_autos_r (tree t, void *data)
24801 {
24802 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24803 if (is_auto_or_concept (t))
24804 {
24805 /* All the autos were built with index 0; fix that up now. */
24806 tree *p = hash.find_slot (t, INSERT);
24807 unsigned idx;
24808 if (*p)
24809 /* If this is a repeated constrained-type-specifier, use the index we
24810 chose before. */
24811 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24812 else
24813 {
24814 /* Otherwise this is new, so use the current count. */
24815 *p = t;
24816 idx = hash.elements () - 1;
24817 }
24818 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24819 }
24820
24821 /* Always keep walking. */
24822 return 0;
24823 }
24824
24825 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24826 says they can appear anywhere in the type. */
24827
24828 static tree
24829 extract_autos (tree type)
24830 {
24831 hash_set<tree> visited;
24832 hash_table<auto_hash> hash (2);
24833
24834 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24835
24836 tree tree_vec = make_tree_vec (hash.elements());
24837 for (hash_table<auto_hash>::iterator iter = hash.begin();
24838 iter != hash.end(); ++iter)
24839 {
24840 tree elt = *iter;
24841 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24842 TREE_VEC_ELT (tree_vec, i)
24843 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24844 }
24845
24846 return tree_vec;
24847 }
24848
24849 /* The stem for deduction guide names. */
24850 const char *const dguide_base = "__dguide_";
24851
24852 /* Return the name for a deduction guide for class template TMPL. */
24853
24854 tree
24855 dguide_name (tree tmpl)
24856 {
24857 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24858 tree tname = TYPE_IDENTIFIER (type);
24859 char *buf = (char *) alloca (1 + strlen (dguide_base)
24860 + IDENTIFIER_LENGTH (tname));
24861 memcpy (buf, dguide_base, strlen (dguide_base));
24862 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24863 IDENTIFIER_LENGTH (tname) + 1);
24864 tree dname = get_identifier (buf);
24865 TREE_TYPE (dname) = type;
24866 return dname;
24867 }
24868
24869 /* True if NAME is the name of a deduction guide. */
24870
24871 bool
24872 dguide_name_p (tree name)
24873 {
24874 return (TREE_TYPE (name)
24875 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24876 strlen (dguide_base)));
24877 }
24878
24879 /* True if FN is a deduction guide. */
24880
24881 bool
24882 deduction_guide_p (const_tree fn)
24883 {
24884 if (DECL_P (fn))
24885 if (tree name = DECL_NAME (fn))
24886 return dguide_name_p (name);
24887 return false;
24888 }
24889
24890 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
24891
24892 bool
24893 copy_guide_p (const_tree fn)
24894 {
24895 gcc_assert (deduction_guide_p (fn));
24896 if (!DECL_ARTIFICIAL (fn))
24897 return false;
24898 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
24899 return (TREE_CHAIN (parms) == void_list_node
24900 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
24901 }
24902
24903 /* True if FN is a guide generated from a constructor template. */
24904
24905 bool
24906 template_guide_p (const_tree fn)
24907 {
24908 gcc_assert (deduction_guide_p (fn));
24909 if (!DECL_ARTIFICIAL (fn))
24910 return false;
24911 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
24912 {
24913 tree tmpl = DECL_TI_TEMPLATE (ctor);
24914 return PRIMARY_TEMPLATE_P (tmpl);
24915 }
24916 return false;
24917 }
24918
24919 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24920 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24921 template parameter types. Note that the handling of template template
24922 parameters relies on current_template_parms being set appropriately for the
24923 new template. */
24924
24925 static tree
24926 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
24927 tree tsubst_args, tsubst_flags_t complain)
24928 {
24929 tree oldidx = get_template_parm_index (olddecl);
24930
24931 tree newtype;
24932 if (TREE_CODE (olddecl) == TYPE_DECL
24933 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24934 {
24935 tree oldtype = TREE_TYPE (olddecl);
24936 newtype = cxx_make_type (TREE_CODE (oldtype));
24937 TYPE_MAIN_VARIANT (newtype) = newtype;
24938 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
24939 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
24940 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
24941 }
24942 else
24943 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
24944 complain, NULL_TREE);
24945
24946 tree newdecl
24947 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
24948 DECL_NAME (olddecl), newtype);
24949 SET_DECL_TEMPLATE_PARM_P (newdecl);
24950
24951 tree newidx;
24952 if (TREE_CODE (olddecl) == TYPE_DECL
24953 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24954 {
24955 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
24956 = build_template_parm_index (index, level, level,
24957 newdecl, newtype);
24958 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
24959 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
24960
24961 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
24962 {
24963 DECL_TEMPLATE_RESULT (newdecl)
24964 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
24965 DECL_NAME (olddecl), newtype);
24966 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
24967 // First create a copy (ttargs) of tsubst_args with an
24968 // additional level for the template template parameter's own
24969 // template parameters (ttparms).
24970 tree ttparms = (INNERMOST_TEMPLATE_PARMS
24971 (DECL_TEMPLATE_PARMS (olddecl)));
24972 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
24973 tree ttargs = make_tree_vec (depth + 1);
24974 for (int i = 0; i < depth; ++i)
24975 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
24976 TREE_VEC_ELT (ttargs, depth)
24977 = template_parms_level_to_args (ttparms);
24978 // Substitute ttargs into ttparms to fix references to
24979 // other template parameters.
24980 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24981 complain);
24982 // Now substitute again with args based on tparms, to reduce
24983 // the level of the ttparms.
24984 ttargs = current_template_args ();
24985 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24986 complain);
24987 // Finally, tack the adjusted parms onto tparms.
24988 ttparms = tree_cons (size_int (depth), ttparms,
24989 current_template_parms);
24990 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
24991 }
24992 }
24993 else
24994 {
24995 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
24996 tree newconst
24997 = build_decl (DECL_SOURCE_LOCATION (oldconst),
24998 TREE_CODE (oldconst),
24999 DECL_NAME (oldconst), newtype);
25000 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25001 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25002 SET_DECL_TEMPLATE_PARM_P (newconst);
25003 newidx = build_template_parm_index (index, level, level,
25004 newconst, newtype);
25005 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25006 }
25007
25008 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25009 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25010 return newdecl;
25011 }
25012
25013 /* Returns a C++17 class deduction guide template based on the constructor
25014 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25015 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25016
25017 static tree
25018 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25019 {
25020 tree type, tparms, targs, fparms, fargs, ci;
25021 bool memtmpl = false;
25022 bool explicit_p;
25023 location_t loc;
25024
25025 if (TYPE_P (ctor))
25026 {
25027 type = ctor;
25028 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25029 if (copy_p)
25030 {
25031 type = TREE_TYPE (type);
25032 fparms = tree_cons (NULL_TREE, type, void_list_node);
25033 }
25034 else
25035 fparms = void_list_node;
25036
25037 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25038 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25039 targs = CLASSTYPE_TI_ARGS (type);
25040 ci = NULL_TREE;
25041 fargs = NULL_TREE;
25042 loc = DECL_SOURCE_LOCATION (ctmpl);
25043 explicit_p = false;
25044 }
25045 else
25046 {
25047 if (outer_args)
25048 ctor = tsubst (ctor, outer_args, complain, ctor);
25049 type = DECL_CONTEXT (ctor);
25050 tree fn_tmpl;
25051 if (TREE_CODE (ctor) == TEMPLATE_DECL)
25052 {
25053 fn_tmpl = ctor;
25054 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25055 }
25056 else
25057 fn_tmpl = DECL_TI_TEMPLATE (ctor);
25058
25059 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25060 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25061 fully specialized args for the enclosing class. Strip those off, as
25062 the deduction guide won't have those template parameters. */
25063 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25064 TMPL_PARMS_DEPTH (tparms));
25065 /* Discard the 'this' parameter. */
25066 fparms = FUNCTION_ARG_CHAIN (ctor);
25067 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25068 ci = get_constraints (ctor);
25069 loc = DECL_SOURCE_LOCATION (ctor);
25070 explicit_p = DECL_NONCONVERTING_P (ctor);
25071
25072 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25073 {
25074 memtmpl = true;
25075
25076 /* For a member template constructor, we need to flatten the two
25077 template parameter lists into one, and then adjust the function
25078 signature accordingly. This gets...complicated. */
25079 ++processing_template_decl;
25080 tree save_parms = current_template_parms;
25081
25082 /* For a member template we should have two levels of parms/args, one
25083 for the class and one for the constructor. We stripped
25084 specialized args for further enclosing classes above. */
25085 const int depth = 2;
25086 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25087
25088 /* Template args for translating references to the two-level template
25089 parameters into references to the one-level template parameters we
25090 are creating. */
25091 tree tsubst_args = copy_node (targs);
25092 TMPL_ARGS_LEVEL (tsubst_args, depth)
25093 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25094
25095 /* Template parms for the constructor template. */
25096 tree ftparms = TREE_VALUE (tparms);
25097 unsigned flen = TREE_VEC_LENGTH (ftparms);
25098 /* Template parms for the class template. */
25099 tparms = TREE_CHAIN (tparms);
25100 tree ctparms = TREE_VALUE (tparms);
25101 unsigned clen = TREE_VEC_LENGTH (ctparms);
25102 /* Template parms for the deduction guide start as a copy of the
25103 template parms for the class. We set current_template_parms for
25104 lookup_template_class_1. */
25105 current_template_parms = tparms = copy_node (tparms);
25106 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25107 for (unsigned i = 0; i < clen; ++i)
25108 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25109
25110 /* Now we need to rewrite the constructor parms to append them to the
25111 class parms. */
25112 for (unsigned i = 0; i < flen; ++i)
25113 {
25114 unsigned index = i + clen;
25115 unsigned level = 1;
25116 tree oldelt = TREE_VEC_ELT (ftparms, i);
25117 tree olddecl = TREE_VALUE (oldelt);
25118 tree newdecl = rewrite_template_parm (olddecl, index, level,
25119 tsubst_args, complain);
25120 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25121 tsubst_args, complain, ctor);
25122 tree list = build_tree_list (newdef, newdecl);
25123 TEMPLATE_PARM_CONSTRAINTS (list)
25124 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25125 tsubst_args, complain, ctor);
25126 TREE_VEC_ELT (new_vec, index) = list;
25127 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25128 }
25129
25130 /* Now we have a final set of template parms to substitute into the
25131 function signature. */
25132 targs = template_parms_to_args (tparms);
25133 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25134 complain, ctor);
25135 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25136 if (ci)
25137 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25138
25139 current_template_parms = save_parms;
25140 --processing_template_decl;
25141 }
25142 }
25143
25144 if (!memtmpl)
25145 {
25146 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25147 tparms = copy_node (tparms);
25148 INNERMOST_TEMPLATE_PARMS (tparms)
25149 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25150 }
25151
25152 tree fntype = build_function_type (type, fparms);
25153 tree ded_fn = build_lang_decl_loc (loc,
25154 FUNCTION_DECL,
25155 dguide_name (type), fntype);
25156 DECL_ARGUMENTS (ded_fn) = fargs;
25157 DECL_ARTIFICIAL (ded_fn) = true;
25158 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25159 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25160 DECL_ARTIFICIAL (ded_tmpl) = true;
25161 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25162 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25163 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25164 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25165 if (DECL_P (ctor))
25166 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25167 if (ci)
25168 set_constraints (ded_tmpl, ci);
25169
25170 return ded_tmpl;
25171 }
25172
25173 /* Deduce template arguments for the class template placeholder PTYPE for
25174 template TMPL based on the initializer INIT, and return the resulting
25175 type. */
25176
25177 static tree
25178 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25179 tsubst_flags_t complain)
25180 {
25181 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25182 {
25183 /* We should have handled this in the caller. */
25184 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25185 return ptype;
25186 if (complain & tf_error)
25187 error ("non-class template %qT used without template arguments", tmpl);
25188 return error_mark_node;
25189 }
25190
25191 tree type = TREE_TYPE (tmpl);
25192
25193 vec<tree,va_gc> *args;
25194 if (init == NULL_TREE
25195 || TREE_CODE (init) == TREE_LIST)
25196 args = make_tree_vector_from_list (init);
25197 else if (BRACE_ENCLOSED_INITIALIZER_P (init)
25198 && !TYPE_HAS_LIST_CTOR (type)
25199 && !is_std_init_list (type))
25200 args = make_tree_vector_from_ctor (init);
25201 else
25202 args = make_tree_vector_single (init);
25203
25204 tree dname = dguide_name (tmpl);
25205 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25206 /*type*/false, /*complain*/false,
25207 /*hidden*/false);
25208 if (cands == error_mark_node)
25209 cands = NULL_TREE;
25210
25211 tree outer_args = NULL_TREE;
25212 if (DECL_CLASS_SCOPE_P (tmpl)
25213 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25214 {
25215 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25216 type = TREE_TYPE (most_general_template (tmpl));
25217 }
25218
25219 bool saw_ctor = false;
25220 if (CLASSTYPE_METHOD_VEC (type))
25221 // FIXME cache artificial deduction guides
25222 for (tree fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
25223 {
25224 tree fn = OVL_CURRENT (fns);
25225 tree guide = build_deduction_guide (fn, outer_args, complain);
25226 cands = ovl_cons (guide, cands);
25227
25228 saw_ctor = true;
25229 }
25230
25231 if (!saw_ctor && args->length() == 0)
25232 {
25233 tree guide = build_deduction_guide (type, outer_args, complain);
25234 cands = ovl_cons (guide, cands);
25235 }
25236 if (args->length() == 1)
25237 {
25238 tree guide = build_deduction_guide (build_reference_type (type),
25239 outer_args, complain);
25240 cands = ovl_cons (guide, cands);
25241 }
25242
25243 /* Prune explicit deduction guides in copy-initialization context. */
25244 tree old_cands = cands;
25245 if (flags & LOOKUP_ONLYCONVERTING)
25246 {
25247 tree t = cands;
25248 for (; t; t = OVL_NEXT (t))
25249 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (OVL_CURRENT (t))))
25250 break;
25251 if (t)
25252 {
25253 tree pruned = NULL_TREE;
25254 for (t = cands; t; t = OVL_NEXT (t))
25255 {
25256 tree f = OVL_CURRENT (t);
25257 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (f)))
25258 pruned = build_overload (f, pruned);
25259 }
25260 cands = pruned;
25261 if (cands == NULL_TREE)
25262 {
25263 error ("cannot deduce template arguments for copy-initialization"
25264 " of %qT, as it has no non-explicit deduction guides or "
25265 "user-declared constructors", type);
25266 return error_mark_node;
25267 }
25268 }
25269 }
25270
25271 ++cp_unevaluated_operand;
25272 tree t = build_new_function_call (cands, &args, /*koenig*/false,
25273 tf_decltype);
25274
25275 if (t == error_mark_node && (complain & tf_warning_or_error))
25276 {
25277 error ("class template argument deduction failed:");
25278 t = build_new_function_call (cands, &args, /*koenig*/false,
25279 complain | tf_decltype);
25280 if (old_cands != cands)
25281 inform (input_location, "explicit deduction guides not considered "
25282 "for copy-initialization");
25283 }
25284
25285 --cp_unevaluated_operand;
25286 release_tree_vector (args);
25287
25288 return TREE_TYPE (t);
25289 }
25290
25291 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25292 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25293
25294 tree
25295 do_auto_deduction (tree type, tree init, tree auto_node)
25296 {
25297 return do_auto_deduction (type, init, auto_node,
25298 tf_warning_or_error,
25299 adc_unspecified);
25300 }
25301
25302 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25303 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25304 The CONTEXT determines the context in which auto deduction is performed
25305 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25306 OUTER_TARGS are used during template argument deduction
25307 (context == adc_unify) to properly substitute the result, and is ignored
25308 in other contexts.
25309
25310 For partial-concept-ids, extra args may be appended to the list of deduced
25311 template arguments prior to determining constraint satisfaction. */
25312
25313 tree
25314 do_auto_deduction (tree type, tree init, tree auto_node,
25315 tsubst_flags_t complain, auto_deduction_context context,
25316 tree outer_targs, int flags)
25317 {
25318 tree targs;
25319
25320 if (init == error_mark_node)
25321 return error_mark_node;
25322
25323 if (init && type_dependent_expression_p (init)
25324 && context != adc_unify)
25325 /* Defining a subset of type-dependent expressions that we can deduce
25326 from ahead of time isn't worth the trouble. */
25327 return type;
25328
25329 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25330 /* C++17 class template argument deduction. */
25331 return do_class_deduction (type, tmpl, init, flags, complain);
25332
25333 if (TREE_TYPE (init) == NULL_TREE)
25334 /* Nothing we can do with this, even in deduction context. */
25335 return type;
25336
25337 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25338 with either a new invented type template parameter U or, if the
25339 initializer is a braced-init-list (8.5.4), with
25340 std::initializer_list<U>. */
25341 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25342 {
25343 if (!DIRECT_LIST_INIT_P (init))
25344 type = listify_autos (type, auto_node);
25345 else if (CONSTRUCTOR_NELTS (init) == 1)
25346 init = CONSTRUCTOR_ELT (init, 0)->value;
25347 else
25348 {
25349 if (complain & tf_warning_or_error)
25350 {
25351 if (permerror (input_location, "direct-list-initialization of "
25352 "%<auto%> requires exactly one element"))
25353 inform (input_location,
25354 "for deduction to %<std::initializer_list%>, use copy-"
25355 "list-initialization (i.e. add %<=%> before the %<{%>)");
25356 }
25357 type = listify_autos (type, auto_node);
25358 }
25359 }
25360
25361 if (type == error_mark_node)
25362 return error_mark_node;
25363
25364 init = resolve_nondeduced_context (init, complain);
25365
25366 if (context == adc_decomp_type
25367 && auto_node == type
25368 && init != error_mark_node
25369 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25370 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25371 and initializer has array type, deduce cv-qualified array type. */
25372 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25373 complain);
25374 else if (AUTO_IS_DECLTYPE (auto_node))
25375 {
25376 bool id = (DECL_P (init)
25377 || ((TREE_CODE (init) == COMPONENT_REF
25378 || TREE_CODE (init) == SCOPE_REF)
25379 && !REF_PARENTHESIZED_P (init)));
25380 targs = make_tree_vec (1);
25381 TREE_VEC_ELT (targs, 0)
25382 = finish_decltype_type (init, id, tf_warning_or_error);
25383 if (type != auto_node)
25384 {
25385 if (complain & tf_error)
25386 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25387 return error_mark_node;
25388 }
25389 }
25390 else
25391 {
25392 tree parms = build_tree_list (NULL_TREE, type);
25393 tree tparms;
25394
25395 if (flag_concepts)
25396 tparms = extract_autos (type);
25397 else
25398 {
25399 tparms = make_tree_vec (1);
25400 TREE_VEC_ELT (tparms, 0)
25401 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25402 }
25403
25404 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25405 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25406 DEDUCE_CALL, LOOKUP_NORMAL,
25407 NULL, /*explain_p=*/false);
25408 if (val > 0)
25409 {
25410 if (processing_template_decl)
25411 /* Try again at instantiation time. */
25412 return type;
25413 if (type && type != error_mark_node
25414 && (complain & tf_error))
25415 /* If type is error_mark_node a diagnostic must have been
25416 emitted by now. Also, having a mention to '<type error>'
25417 in the diagnostic is not really useful to the user. */
25418 {
25419 if (cfun && auto_node == current_function_auto_return_pattern
25420 && LAMBDA_FUNCTION_P (current_function_decl))
25421 error ("unable to deduce lambda return type from %qE", init);
25422 else
25423 error ("unable to deduce %qT from %qE", type, init);
25424 type_unification_real (tparms, targs, parms, &init, 1, 0,
25425 DEDUCE_CALL, LOOKUP_NORMAL,
25426 NULL, /*explain_p=*/true);
25427 }
25428 return error_mark_node;
25429 }
25430 }
25431
25432 /* Check any placeholder constraints against the deduced type. */
25433 if (flag_concepts && !processing_template_decl)
25434 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25435 {
25436 /* Use the deduced type to check the associated constraints. If we
25437 have a partial-concept-id, rebuild the argument list so that
25438 we check using the extra arguments. */
25439 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25440 tree cargs = CHECK_CONSTR_ARGS (constr);
25441 if (TREE_VEC_LENGTH (cargs) > 1)
25442 {
25443 cargs = copy_node (cargs);
25444 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25445 }
25446 else
25447 cargs = targs;
25448 if (!constraints_satisfied_p (constr, cargs))
25449 {
25450 if (complain & tf_warning_or_error)
25451 {
25452 switch (context)
25453 {
25454 case adc_unspecified:
25455 case adc_unify:
25456 error("placeholder constraints not satisfied");
25457 break;
25458 case adc_variable_type:
25459 case adc_decomp_type:
25460 error ("deduced initializer does not satisfy "
25461 "placeholder constraints");
25462 break;
25463 case adc_return_type:
25464 error ("deduced return type does not satisfy "
25465 "placeholder constraints");
25466 break;
25467 case adc_requirement:
25468 error ("deduced expression type does not satisfy "
25469 "placeholder constraints");
25470 break;
25471 }
25472 diagnose_constraints (input_location, constr, targs);
25473 }
25474 return error_mark_node;
25475 }
25476 }
25477
25478 if (processing_template_decl && context != adc_unify)
25479 outer_targs = current_template_args ();
25480 targs = add_to_template_args (outer_targs, targs);
25481 return tsubst (type, targs, complain, NULL_TREE);
25482 }
25483
25484 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25485 result. */
25486
25487 tree
25488 splice_late_return_type (tree type, tree late_return_type)
25489 {
25490 if (is_auto (type))
25491 {
25492 if (late_return_type)
25493 return late_return_type;
25494
25495 tree idx = get_template_parm_index (type);
25496 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25497 /* In an abbreviated function template we didn't know we were dealing
25498 with a function template when we saw the auto return type, so update
25499 it to have the correct level. */
25500 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25501 }
25502 return type;
25503 }
25504
25505 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25506 'decltype(auto)' or a deduced class template. */
25507
25508 bool
25509 is_auto (const_tree type)
25510 {
25511 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25512 && (TYPE_IDENTIFIER (type) == auto_identifier
25513 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25514 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25515 return true;
25516 else
25517 return false;
25518 }
25519
25520 /* for_each_template_parm callback for type_uses_auto. */
25521
25522 int
25523 is_auto_r (tree tp, void */*data*/)
25524 {
25525 return is_auto_or_concept (tp);
25526 }
25527
25528 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25529 a use of `auto'. Returns NULL_TREE otherwise. */
25530
25531 tree
25532 type_uses_auto (tree type)
25533 {
25534 if (type == NULL_TREE)
25535 return NULL_TREE;
25536 else if (flag_concepts)
25537 {
25538 /* The Concepts TS allows multiple autos in one type-specifier; just
25539 return the first one we find, do_auto_deduction will collect all of
25540 them. */
25541 if (uses_template_parms (type))
25542 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25543 /*visited*/NULL, /*nondeduced*/true);
25544 else
25545 return NULL_TREE;
25546 }
25547 else
25548 return find_type_usage (type, is_auto);
25549 }
25550
25551 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
25552 'decltype(auto)' or a concept. */
25553
25554 bool
25555 is_auto_or_concept (const_tree type)
25556 {
25557 return is_auto (type); // or concept
25558 }
25559
25560 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
25561 a concept identifier) iff TYPE contains a use of a generic type. Returns
25562 NULL_TREE otherwise. */
25563
25564 tree
25565 type_uses_auto_or_concept (tree type)
25566 {
25567 return find_type_usage (type, is_auto_or_concept);
25568 }
25569
25570
25571 /* For a given template T, return the vector of typedefs referenced
25572 in T for which access check is needed at T instantiation time.
25573 T is either a FUNCTION_DECL or a RECORD_TYPE.
25574 Those typedefs were added to T by the function
25575 append_type_to_template_for_access_check. */
25576
25577 vec<qualified_typedef_usage_t, va_gc> *
25578 get_types_needing_access_check (tree t)
25579 {
25580 tree ti;
25581 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25582
25583 if (!t || t == error_mark_node)
25584 return NULL;
25585
25586 if (!(ti = get_template_info (t)))
25587 return NULL;
25588
25589 if (CLASS_TYPE_P (t)
25590 || TREE_CODE (t) == FUNCTION_DECL)
25591 {
25592 if (!TI_TEMPLATE (ti))
25593 return NULL;
25594
25595 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25596 }
25597
25598 return result;
25599 }
25600
25601 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25602 tied to T. That list of typedefs will be access checked at
25603 T instantiation time.
25604 T is either a FUNCTION_DECL or a RECORD_TYPE.
25605 TYPE_DECL is a TYPE_DECL node representing a typedef.
25606 SCOPE is the scope through which TYPE_DECL is accessed.
25607 LOCATION is the location of the usage point of TYPE_DECL.
25608
25609 This function is a subroutine of
25610 append_type_to_template_for_access_check. */
25611
25612 static void
25613 append_type_to_template_for_access_check_1 (tree t,
25614 tree type_decl,
25615 tree scope,
25616 location_t location)
25617 {
25618 qualified_typedef_usage_t typedef_usage;
25619 tree ti;
25620
25621 if (!t || t == error_mark_node)
25622 return;
25623
25624 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25625 || CLASS_TYPE_P (t))
25626 && type_decl
25627 && TREE_CODE (type_decl) == TYPE_DECL
25628 && scope);
25629
25630 if (!(ti = get_template_info (t)))
25631 return;
25632
25633 gcc_assert (TI_TEMPLATE (ti));
25634
25635 typedef_usage.typedef_decl = type_decl;
25636 typedef_usage.context = scope;
25637 typedef_usage.locus = location;
25638
25639 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25640 }
25641
25642 /* Append TYPE_DECL to the template TEMPL.
25643 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25644 At TEMPL instanciation time, TYPE_DECL will be checked to see
25645 if it can be accessed through SCOPE.
25646 LOCATION is the location of the usage point of TYPE_DECL.
25647
25648 e.g. consider the following code snippet:
25649
25650 class C
25651 {
25652 typedef int myint;
25653 };
25654
25655 template<class U> struct S
25656 {
25657 C::myint mi; // <-- usage point of the typedef C::myint
25658 };
25659
25660 S<char> s;
25661
25662 At S<char> instantiation time, we need to check the access of C::myint
25663 In other words, we need to check the access of the myint typedef through
25664 the C scope. For that purpose, this function will add the myint typedef
25665 and the scope C through which its being accessed to a list of typedefs
25666 tied to the template S. That list will be walked at template instantiation
25667 time and access check performed on each typedefs it contains.
25668 Note that this particular code snippet should yield an error because
25669 myint is private to C. */
25670
25671 void
25672 append_type_to_template_for_access_check (tree templ,
25673 tree type_decl,
25674 tree scope,
25675 location_t location)
25676 {
25677 qualified_typedef_usage_t *iter;
25678 unsigned i;
25679
25680 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25681
25682 /* Make sure we don't append the type to the template twice. */
25683 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25684 if (iter->typedef_decl == type_decl && scope == iter->context)
25685 return;
25686
25687 append_type_to_template_for_access_check_1 (templ, type_decl,
25688 scope, location);
25689 }
25690
25691 /* Convert the generic type parameters in PARM that match the types given in the
25692 range [START_IDX, END_IDX) from the current_template_parms into generic type
25693 packs. */
25694
25695 tree
25696 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25697 {
25698 tree current = current_template_parms;
25699 int depth = TMPL_PARMS_DEPTH (current);
25700 current = INNERMOST_TEMPLATE_PARMS (current);
25701 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25702
25703 for (int i = 0; i < start_idx; ++i)
25704 TREE_VEC_ELT (replacement, i)
25705 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25706
25707 for (int i = start_idx; i < end_idx; ++i)
25708 {
25709 /* Create a distinct parameter pack type from the current parm and add it
25710 to the replacement args to tsubst below into the generic function
25711 parameter. */
25712
25713 tree o = TREE_TYPE (TREE_VALUE
25714 (TREE_VEC_ELT (current, i)));
25715 tree t = copy_type (o);
25716 TEMPLATE_TYPE_PARM_INDEX (t)
25717 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25718 o, 0, 0, tf_none);
25719 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25720 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25721 TYPE_MAIN_VARIANT (t) = t;
25722 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25723 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25724 TREE_VEC_ELT (replacement, i) = t;
25725 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25726 }
25727
25728 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25729 TREE_VEC_ELT (replacement, i)
25730 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25731
25732 /* If there are more levels then build up the replacement with the outer
25733 template parms. */
25734 if (depth > 1)
25735 replacement = add_to_template_args (template_parms_to_args
25736 (TREE_CHAIN (current_template_parms)),
25737 replacement);
25738
25739 return tsubst (parm, replacement, tf_none, NULL_TREE);
25740 }
25741
25742 /* Entries in the decl_constraint hash table. */
25743 struct GTY((for_user)) constr_entry
25744 {
25745 tree decl;
25746 tree ci;
25747 };
25748
25749 /* Hashing function and equality for constraint entries. */
25750 struct constr_hasher : ggc_ptr_hash<constr_entry>
25751 {
25752 static hashval_t hash (constr_entry *e)
25753 {
25754 return (hashval_t)DECL_UID (e->decl);
25755 }
25756
25757 static bool equal (constr_entry *e1, constr_entry *e2)
25758 {
25759 return e1->decl == e2->decl;
25760 }
25761 };
25762
25763 /* A mapping from declarations to constraint information. Note that
25764 both templates and their underlying declarations are mapped to the
25765 same constraint information.
25766
25767 FIXME: This is defined in pt.c because garbage collection
25768 code is not being generated for constraint.cc. */
25769
25770 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25771
25772 /* Returns the template constraints of declaration T. If T is not
25773 constrained, return NULL_TREE. Note that T must be non-null. */
25774
25775 tree
25776 get_constraints (tree t)
25777 {
25778 if (!flag_concepts)
25779 return NULL_TREE;
25780
25781 gcc_assert (DECL_P (t));
25782 if (TREE_CODE (t) == TEMPLATE_DECL)
25783 t = DECL_TEMPLATE_RESULT (t);
25784 constr_entry elt = { t, NULL_TREE };
25785 constr_entry* found = decl_constraints->find (&elt);
25786 if (found)
25787 return found->ci;
25788 else
25789 return NULL_TREE;
25790 }
25791
25792 /* Associate the given constraint information CI with the declaration
25793 T. If T is a template, then the constraints are associated with
25794 its underlying declaration. Don't build associations if CI is
25795 NULL_TREE. */
25796
25797 void
25798 set_constraints (tree t, tree ci)
25799 {
25800 if (!ci)
25801 return;
25802 gcc_assert (t && flag_concepts);
25803 if (TREE_CODE (t) == TEMPLATE_DECL)
25804 t = DECL_TEMPLATE_RESULT (t);
25805 gcc_assert (!get_constraints (t));
25806 constr_entry elt = {t, ci};
25807 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25808 constr_entry* entry = ggc_alloc<constr_entry> ();
25809 *entry = elt;
25810 *slot = entry;
25811 }
25812
25813 /* Remove the associated constraints of the declaration T. */
25814
25815 void
25816 remove_constraints (tree t)
25817 {
25818 gcc_assert (DECL_P (t));
25819 if (TREE_CODE (t) == TEMPLATE_DECL)
25820 t = DECL_TEMPLATE_RESULT (t);
25821
25822 constr_entry elt = {t, NULL_TREE};
25823 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25824 if (slot)
25825 decl_constraints->clear_slot (slot);
25826 }
25827
25828 /* Memoized satisfaction results for declarations. This
25829 maps the pair (constraint_info, arguments) to the result computed
25830 by constraints_satisfied_p. */
25831
25832 struct GTY((for_user)) constraint_sat_entry
25833 {
25834 tree ci;
25835 tree args;
25836 tree result;
25837 };
25838
25839 /* Hashing function and equality for constraint entries. */
25840
25841 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25842 {
25843 static hashval_t hash (constraint_sat_entry *e)
25844 {
25845 hashval_t val = iterative_hash_object(e->ci, 0);
25846 return iterative_hash_template_arg (e->args, val);
25847 }
25848
25849 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25850 {
25851 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25852 }
25853 };
25854
25855 /* Memoized satisfaction results for concept checks. */
25856
25857 struct GTY((for_user)) concept_spec_entry
25858 {
25859 tree tmpl;
25860 tree args;
25861 tree result;
25862 };
25863
25864 /* Hashing function and equality for constraint entries. */
25865
25866 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25867 {
25868 static hashval_t hash (concept_spec_entry *e)
25869 {
25870 return hash_tmpl_and_args (e->tmpl, e->args);
25871 }
25872
25873 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25874 {
25875 ++comparing_specializations;
25876 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25877 --comparing_specializations;
25878 return eq;
25879 }
25880 };
25881
25882 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25883 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25884
25885 /* Search for a memoized satisfaction result. Returns one of the
25886 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25887
25888 tree
25889 lookup_constraint_satisfaction (tree ci, tree args)
25890 {
25891 constraint_sat_entry elt = { ci, args, NULL_TREE };
25892 constraint_sat_entry* found = constraint_memos->find (&elt);
25893 if (found)
25894 return found->result;
25895 else
25896 return NULL_TREE;
25897 }
25898
25899 /* Memoize the result of a satisfication test. Returns the saved result. */
25900
25901 tree
25902 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25903 {
25904 constraint_sat_entry elt = {ci, args, result};
25905 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25906 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25907 *entry = elt;
25908 *slot = entry;
25909 return result;
25910 }
25911
25912 /* Search for a memoized satisfaction result for a concept. */
25913
25914 tree
25915 lookup_concept_satisfaction (tree tmpl, tree args)
25916 {
25917 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25918 concept_spec_entry* found = concept_memos->find (&elt);
25919 if (found)
25920 return found->result;
25921 else
25922 return NULL_TREE;
25923 }
25924
25925 /* Memoize the result of a concept check. Returns the saved result. */
25926
25927 tree
25928 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
25929 {
25930 concept_spec_entry elt = {tmpl, args, result};
25931 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
25932 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25933 *entry = elt;
25934 *slot = entry;
25935 return result;
25936 }
25937
25938 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
25939
25940 /* Returns a prior concept specialization. This returns the substituted
25941 and normalized constraints defined by the concept. */
25942
25943 tree
25944 get_concept_expansion (tree tmpl, tree args)
25945 {
25946 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25947 concept_spec_entry* found = concept_expansions->find (&elt);
25948 if (found)
25949 return found->result;
25950 else
25951 return NULL_TREE;
25952 }
25953
25954 /* Save a concept expansion for later. */
25955
25956 tree
25957 save_concept_expansion (tree tmpl, tree args, tree def)
25958 {
25959 concept_spec_entry elt = {tmpl, args, def};
25960 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
25961 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25962 *entry = elt;
25963 *slot = entry;
25964 return def;
25965 }
25966
25967 static hashval_t
25968 hash_subsumption_args (tree t1, tree t2)
25969 {
25970 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
25971 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
25972 int val = 0;
25973 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
25974 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
25975 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
25976 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
25977 return val;
25978 }
25979
25980 /* Compare the constraints of two subsumption entries. The LEFT1 and
25981 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
25982 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
25983
25984 static bool
25985 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
25986 {
25987 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
25988 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
25989 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
25990 CHECK_CONSTR_ARGS (right1)))
25991 return comp_template_args (CHECK_CONSTR_ARGS (left2),
25992 CHECK_CONSTR_ARGS (right2));
25993 return false;
25994 }
25995
25996 /* Key/value pair for learning and memoizing subsumption results. This
25997 associates a pair of check constraints (including arguments) with
25998 a boolean value indicating the result. */
25999
26000 struct GTY((for_user)) subsumption_entry
26001 {
26002 tree t1;
26003 tree t2;
26004 bool result;
26005 };
26006
26007 /* Hashing function and equality for constraint entries. */
26008
26009 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26010 {
26011 static hashval_t hash (subsumption_entry *e)
26012 {
26013 return hash_subsumption_args (e->t1, e->t2);
26014 }
26015
26016 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26017 {
26018 ++comparing_specializations;
26019 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26020 --comparing_specializations;
26021 return eq;
26022 }
26023 };
26024
26025 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26026
26027 /* Search for a previously cached subsumption result. */
26028
26029 bool*
26030 lookup_subsumption_result (tree t1, tree t2)
26031 {
26032 subsumption_entry elt = { t1, t2, false };
26033 subsumption_entry* found = subsumption_table->find (&elt);
26034 if (found)
26035 return &found->result;
26036 else
26037 return 0;
26038 }
26039
26040 /* Save a subsumption result. */
26041
26042 bool
26043 save_subsumption_result (tree t1, tree t2, bool result)
26044 {
26045 subsumption_entry elt = {t1, t2, result};
26046 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26047 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26048 *entry = elt;
26049 *slot = entry;
26050 return result;
26051 }
26052
26053 /* Set up the hash table for constraint association. */
26054
26055 void
26056 init_constraint_processing (void)
26057 {
26058 if (!flag_concepts)
26059 return;
26060
26061 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26062 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26063 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26064 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26065 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26066 }
26067
26068 /* Set up the hash tables for template instantiations. */
26069
26070 void
26071 init_template_processing (void)
26072 {
26073 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26074 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26075 }
26076
26077 /* Print stats about the template hash tables for -fstats. */
26078
26079 void
26080 print_template_statistics (void)
26081 {
26082 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26083 "%f collisions\n", (long) decl_specializations->size (),
26084 (long) decl_specializations->elements (),
26085 decl_specializations->collisions ());
26086 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26087 "%f collisions\n", (long) type_specializations->size (),
26088 (long) type_specializations->elements (),
26089 type_specializations->collisions ());
26090 }
26091
26092 #include "gt-cp-pt.h"
This page took 1.263169 seconds and 5 git commands to generate.